[Bug]: After `obj.__class__ = C`, calling `C(arg)` for a class with a metaclass such as `ABCMeta` fails with `object.__new__() takes exactly one argument` from the third call on

Đang mở
#1,342 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

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
22/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

The failure sits in how GraalPy resolves __new__ after __class__ is reassigned, so the search starts at the __class__ setter and the type call path for classes with a metaclass. Run the reproduction script from the issue; the fault appears from the third call at the cls(*args) line. Done when all four rounds print C and the metaclass-as-type-subclass variant also passes.

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

Mô tả

Describe the bug

C inherits a __new__ that accepts extra arguments and has a metaclass (here abc.ABCMeta). Once some object's __class__ has been set to C, a call site that has already called both the base class and C starts raising TypeError: object.__new__() takes exactly one argument (the type to instantiate) for C(arg), as if C had no __new__ of its own. The first two rounds work; every later round fails. CPython constructs the object every time.

Operating system

macOS

CPU architecture

ARM64

GraalPy version

25.6.5.1 (Python 3.13.14), community build from the GitHub release. Same on master 2e574af1d8.

JDK version

No response

Context configuration

No response

Steps to reproduce
import abc

class Base:
    def __new__(cls, *args):
        return object.__new__(cls)
    def __init__(self, *args):
        pass

class C(Base, metaclass=abc.ABCMeta):
    pass

def make(cls, *args):
    return cls(*args)

for i in range(4):
    try:
        make(Base, "a")
        Base().__class__ = C
        print(i, type(make(C, "b")).__name__)
    except TypeError as e:
        print(i, "TypeError:", e)

GraalPy:

0 C
1 C
2 TypeError: object.__new__() takes exactly one argument (the type to instantiate)
3 TypeError: object.__new__() takes exactly one argument (the type to instantiate)
Expected behavior

CPython 3.13.18:

0 C
1 C
2 C
3 C
Additional context

All three ingredients are needed on GraalPy:

  • Without the __class__ assignment, all four rounds print C.
  • With class C(Base) (metaclass type), all four rounds print C. Any type subclass as metaclass fails, not only ABCMeta.
  • Without the make(Base, "a") call, so that the call site only sees C, all four rounds print C.

The same happens when the call is type(self)(*args) inside a method and self is the object whose __class__ was assigned. The traceback ends at the cls(*args) call, not inside Base.__new__, so the __new__ that gets called is object.__new__ itself.

Ngôn ngữ chính
Python
Star
1.7k
Fork
167
Merge trung bình
3 ngày 11 giờ
Pull request đã merge (30 ngày)
68

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.