[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 摘要。