Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở Phù hợp với người mới
#1,225 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@jseop-lim đang làm issue này rồi.

Từ ngày 20/10/2026.

  • #1230 của @jseop-lim — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java, python
Lĩnh vực
backend

Hướng nghiên cứu

Đọc ComplexBuiltins.java, đặc biệt là complexFromComplexObject quanh các dòng được trích dẫn và complexFromComplexLong để xem các bước kiểm tra đối số hiện có. Tái hiện lỗi bằng ví dụ Python được cung cấp, sau đó chạy các bài kiểm thử liên quan đến hàm tạo complex. Hoàn tất khi real không hợp lệ bị từ chối trước khi chuyển đổi imag, đúng với lỗi và thứ tự chuyển đổi của CPython.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
1.7k
Fork
163
Merge trung bình
8 giờ 15 phút
Pull request đã merge (30 ngày)
49

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của oracle/graalpython

Tất cả issue của oracle/graalpython

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.