[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

未關閉
#1,342 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
22/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
java, python
領域
backend

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
Python
星號
1.7k
分支
167
平均合併
3 天 11 小時
30 天內合併 PR
68

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

oracle/graalpython 的其他 Issue

查看 oracle/graalpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。