[Bug]: `complex(real, imag)` converts `imag` before rejecting a non-number `real`
Mantenedores costumam responder em até 1 dia
Avaliação
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Facilidade para iniciantes
- 78/100
Direção de pesquisa
Leia ComplexBuiltins.java, especialmente complexFromComplexObject perto das linhas citadas e complexFromComplexLong para conferir as verificações de argumentos existentes. Reproduza o problema com o exemplo Python fornecido e, em seguida, execute os testes relevantes do construtor complex. O trabalho estará concluído quando um real inválido for rejeitado antes da conversão de imag, seguindo o erro e a ordem de conversão do CPython.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
Describe the bug
When imag is a user object, complex(real, imag) calls its __float__ or __index__ before checking that real is a number. An exception from that conversion replaces the TypeError CPython raises for the first argument, and CPython never calls the method at all.
Operating system
Linux
CPU architecture
ARM64
GraalPy version
25.0.2 (Python 3.12.8); also reproduced on 25.4.4.1.1 (Python 3.13.14)
JDK version
No response
Context configuration
No response
Steps to reproduce
class RaisingFloat:
def __float__(self):
raise Exception()
complex({}, RaisingFloat())
GraalPy 25.0.2 raises the Exception from RaisingFloat.__float__:
Exception
Expected behavior
CPython 3.12.13 (same on 3.13.16):
TypeError: complex() first argument must be a string or a number, not 'dict'
Stack trace
Additional context
The conversion order is reversed too: with both arguments defining a raising __float__, GraalPy calls only imag.__float__ and CPython calls only real.__float__. When imag is an int or float, another specialization checks real first, so complex({1: 2}, 0) already matches CPython.
Root Cause
ComplexNewNode.complexFromComplexObject, taken when imag is neither complex nor a string, converts two at ComplexBuiltins.java#L621-L622 and only then checks one (#L623-L630). CPython's complex_new_impl checks the type of real and then imag (complexobject.c#L945-L972) before converting either (#L995, #L1018).
Fix Suggestion
Reorder complexFromComplexObject to raise the first-argument error, then the second-argument error, and only then convert one and two, as complexFromComplexLong (#L532-L539) already checks one first.
- Linguagem predominante
- Python
- Estrelas
- 1.7k
- Forks
- 158
- Merge médio
- 8h 58min
- PRs com merge (30d)
- 44
Preparar o ambiente
Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de oracle/graalpython
-
[Bug]: `reversed()` on a dict or dict view returns the forward iterator type instead of `dict_reverse*iterator`Talvez já em andamento Um pull request vinculado a esta issue está aberto ou já foi mesclado. Abertabug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
oracle/graalpython#1226 ·
Mantenedores costumam responder em até 1 dia
-
[Bug]: sys.flags.debug is 1 whenever optimization is offTalvez já em andamento @hippi345 assumiu há 3 dias. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
oracle/graalpython#1213 ·
Mantenedores costumam responder em até 1 dia
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
oracle/graalpython#1105 ·
Mantenedores costumam responder em até 1 dia
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
oracle/graalpython#1102 ·
Mantenedores costumam responder em até 1 dia
-
[Bug]: `compile()` checks the source before `mode`, raising `TypeError` instead of `ValueError`Talvez já em andamento Um pull request vinculado a esta issue está aberto ou já foi mesclado. Abertabug
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 76/100
oracle/graalpython#1224 ·
Mantenedores costumam responder em até 1 dia
Todas as issues de oracle/graalpython
Issues semelhantes
-
Use mhctools predictor_required=False for saved-result rescoringTalvez já em andamento @iskandr assumiu hoje. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 79/100
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 85/100
521xueweihan/HelloGitHub#4051 · 1 comentário ·
-
bug documentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
awslabs/visual-asset-management-system#514 ·
Mantenedores costumam responder em até 1 dia
-
backend bug spec:REQ-053
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 66/100
nolte/kamerplanter#2547 ·
Mantenedores costumam responder em até 1 dia