[New-bugs-announce] [issue46034] Patch-adding __init__ in a class takes no effect when called from a subinterpreter

Miro Hrončok report at bugs.python.org
Fri Dec 10 07:01:00 EST 2021


New submission from Miro Hrončok <miro at hroncok.cz>:

Hello,

based on some bug reports reported in:

- https://github.com/GrahamDumpleton/mod_wsgi/issues/729
- https://github.com/poljar/weechat-matrix/issues/293
- https://bugzilla.redhat.com/show_bug.cgi?id=2030621

I have isolated the following reproducer that shows a regression in Python 3.10+. It happens at least with 3.10.0, 3.10.1 and 3.11.0a2. It works as expected in Python 3.9.

The following code is valid and works on 3.9, 3.10, 3.11:

=========================================
class Greeting():
    pass

def new_init(inst, name):
    inst.text = f"Hello, {name}\n"

Greeting.__init__ = new_init

Greeting("Miro")
=========================================


But if we run it via a subinterpreter, it breaks on 3.10+:

=========================================
import _testcapi

code = r"""class Greeting():
    pass

def new_init(inst, name):
    inst.text = f"Hello, {name}\n"

Greeting.__init__ = new_init

Greeting("Miro")"""

_testcapi.run_in_subinterp(code)
=========================================


$ python3.9 reproducer.py
(exits cleanly)

$ python3.10 reproducer.py 
Traceback (most recent call last):
  File "<string>", line 9, in <module>
TypeError: Greeting() takes no arguments

$ python3.11 reproducer.py 
Traceback (most recent call last):
  File "<string>", line 9, in <module>
TypeError: Greeting() takes no arguments


My observation also shows that if the Greeting class already had (any) __init__:

=========================================
class Greeting():
    __init__ = ...
=========================================

It works as expected on 3.9, 3.10, 3.11.



In reality, this is most likely to affect classes with decorators, like:

=========================================
import attr

@attr.s
class TestClass:
    foo = attr.ib()

TestClass("Miro")
=========================================

This works standalone, but fails in a subinterpreter:

$ python3.10 reproducer.py 
Traceback (most recent call last):
  File "<string>", line 7, in <module>
TypeError: TestClass() takes no arguments

----------
components: Subinterpreters
messages: 408196
nosy: eric.snow, hroncok, vstinner
priority: normal
severity: normal
status: open
title: Patch-adding __init__ in a class takes no effect when called from a subinterpreter
type: behavior
versions: Python 3.10, Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46034>
_______________________________________


More information about the New-bugs-announce mailing list