Hacktoberfest 2026: as issues que os mantenedores marcaram para outubro, abertas e boas para iniciantes. Ver issues do Hacktoberfest

[Bug]: `complex(real, imag)` converts `imag` before rejecting a non-number `real`

Aberta Para iniciantes
#1,225 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

@jseop-lim já está trabalhando nisso.

Desde 20/10/2026.

  • #1230 de @jseop-lim — aberto

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
78/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
java, python
Domínio
backend

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

bug
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

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de oracle/graalpython

Todas as issues de oracle/graalpython

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.