Repository navigation
Description
Bug report
Bug description:
Hi Python maintainers!
I noticed something weird when using subinterpreters, while converting an Enum to string I get an unexpected result. This occurs in Python 3.12 and 3.13.
Here's an script to reproduce it:
import _xxsubinterpreters as interpreters
script = """from enum import _simple_enum, IntEnum
@_simple_enum(IntEnum)
class MyEnum:
DATA = 1
print(str(MyEnum.DATA))
"""
exec(script)
# Output: 1
interp_id = interpreters.create(isolated=False)
interpreters.run_string(interp_id, script)
# Output: <MyEnum.DATA: 1>, Expected: 1In all python versions previous to 3.12 this prints "1" two times, on newer versions I get <MyEnum.DATA: 1> when running inside a subinterpreter. For some reason, the __str__ function being used is different on new Python versions.
I also noticed that the function pointed by __str__ is different inside and outside the subinterpreter.
Outside:
...
print(MyEnum.DATA.__str__)
# Output: <method-wrapper '__repr__' of MyEnum object at 0x7f9a09a2e910>Inside:
...
print(MyEnum.DATA.__str__)
# Output: <method-wrapper '__str__' of MyEnum object at 0x7f9a099a5e90>NOTE: I'm creating subinterpreters passing the
isolated=Falseflag, which uses the Legacy init config.
I first noticed the error on MacOS, then reproduced using Docker with various Python versions.
I hope this is enough to get to the source of the issue.
Appreciate all your work and effort on building Python, thank you!
CPython versions tested on:
3.8, 3.9, 3.10, 3.11, 3.12, 3.13
Operating systems tested on:
Linux, macOS
Linked PRs
- gh-117482: Cleaning up tp_dict for static builtin types in subinterpreters #117660
- gh-117482: Fix Builtin Types Slot Wrappers #121602
- [3.13] gh-117482: Fix Builtin Types Slot Wrappers (gh-121602) #121630
- [3.12] gh-117482: Fix Builtin Types Slot Wrappers (gh-121602) #121632
- gh-117482: Fix Builtin Types Slot Wrappers When Embedded #121636
- [3.12] gh-117482: Simplify the Fix For Builtin Types Slot Wrappers #121862
- gh-117482: Simplify the Fix For Builtin Types Slot Wrappers #121882
- [3.13] gh-117482: Simplify the Fix For Builtin Types Slot Wrappers #121932
- gh-117482: Expand Tests for Slot Wrappers of Inherited Slots of Static Builtin Types #122192
- [3.13] gh-117482: Expand Tests for Slot Wrappers of Inherited Slots of Static Builtin Types (gh-122192) #122195
- [3.12] gh-117482: Expand Tests for Slot Wrappers of Inherited Slots of Static Builtin Types (gh-122192) #122197
- [3.12] gh-117482: Simplify the Fix For Builtin Types Slot Wrappers #122241
- gh-117482: Fix the Slot Wrapper Inheritance Tests #122248
- [3.13] gh-117482: Fix the Slot Wrapper Inheritance Tests #122249
- [3.12] gh-117482: Fix the Slot Wrapper Inheritance Tests #122250
- gh-117482: Simplify the Fix For Builtin Types Slot Wrappers #122865
- gh-117482: Preserve the Mapping Between Type Slot Name and Attributes #122866
- gh-117482: Make the Slot Wrapper Inheritance Tests Much More Thorough #122867
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsTodo
Activity
[3.13] gh-117482: Fix Builtin Types Slot Wrappers (gh-121630)
pythongh-117482: Fix Builtin Types Slot Wrappers (pythongh-121602)
[3.12] gh-117482: Fix Builtin Types Slot Wrappers (gh-121632)
AraHaan commentedon Jul 12, 2024
I think an alternative option is for
tp_dictto be computed once and for those members of the type objects to remain untouched between main and sub-interpreters. As in once the main interpreter computes it and the inherited tp slots, it then uses those slots in any sub-interpreter and permanently solves this issue I think (I think this way it provides a performance boost for free in sub-interpreters on top of fixing this issue). Basically the entirePyTypeObjectstructure after the main interpreter properly loads and prepares it, the sub interpreters would be able to make use of the same pointers the main one used (by first copying the pointers to a new one for the subinterpreter usingmemcpy).ericsnowcurrently commentedon Jul 16, 2024
@markshannon pointed out an alternate solution to the one I merged: in
add_operators(), if*ptrisn't NULL then check if it is the same as the corresponding tp slot as the base (tp_base). If it is then act as though it were NULL.There are good reasons to keep (and build on) the changes we've made already, for 3.13+. That isn't so much the case for 3.12 though. Thus, I'll probably update 3.12 to take the simpler approach.
pythongh-117482: Fix Builtin Types Slot Wrappers (pythongh-121602)
gh-117482: Expand Tests for Slot Wrappers of Inherited Slots of Stati…
pythongh-117482: Expand Tests for Slot Wrappers of Inherited Slots of…
[3.13] gh-117482: Expand Tests for Slot Wrappers of Inherited Slots o…
pythongh-117482: Expand Tests for Slot Wrappers of Inherited Slots of…
[3.12] gh-117482: Expand Tests for Slot Wrappers of Inherited Slots o…
[3.13] gh-117482: Simplify the Fix For Builtin Types Slot Wrappers (g…
[3.13] pythongh-117482: Simplify the Fix For Builtin Types Slot Wrapp…
[3.12] gh-117482: Simplify the Fix For Builtin Types Slot Wrappers (g…
pythongh-117482: Expand Tests for Slot Wrappers of Inherited Slots of…
pythongh-117482: Expand Tests for Slot Wrappers of Inherited Slots of…
gh-117482: Fix the Slot Wrapper Inheritance Tests (gh-122248)
[3.13] gh-117482: Fix the Slot Wrapper Inheritance Tests (gh-122249)
[3.12] gh-117482: Fix the Slot Wrapper Inheritance Tests (gh-122250)
gh-117482: Make the Slot Wrapper Inheritance Tests Much More Thorough (…
pythongh-117482: Make the Slot Wrapper Inheritance Tests Much More Th…
gh-117482: Simplify the Fix For Builtin Types Slot Wrappers (GH-122865)
encukou commentedon Sep 9, 2024
Note that current PRs are closed, the issue should stay open. @ericsnowcurrently said in #122865: