Skip to content

Weird int.__str__ behaviour inside sub-interpreters #117482

Open

Description

@mliezun

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: 1

In 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=False flag, 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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Apr 2, 2024
  2. Eclips4 commented on Apr 2, 2024

    @Eclips4
    Member

    Bisected to de64e75

  3. Elchinchel commented on Apr 2, 2024

    @Elchinchel

    I think i've got a shorter MRE, i believe this issue is not related to enums.

    import _xxsubinterpreters as interpreters
    
    script = """print(int.__str__)"""
    
    
    exec(script)
    # Output: <slot wrapper '__str__' of 'object' objects>
    
    interp_id = interpreters.create()
    interpreters.run_string(interp_id, script)
    # Output: <slot wrapper '__str__' of 'int' objects>
  4. Eclips4 commented on Apr 2, 2024

    @Eclips4
    Member

    I think i've got a shorter MRE, i believe this issue is not related to enums.

    import _xxsubinterpreters as interpreters
    
    script = """print(int.__str__)"""
    
    
    exec(script)
    # Output: <slot wrapper '__str__' of 'object' objects>
    
    interp_id = interpreters.create()
    interpreters.run_string(interp_id, script)
    # Output: <slot wrapper '__str__' of 'int' objects>

    This sheds light on what is going on. Thanks, @Elchinchel!

  5. changed the title [-]Weird Enum behaviour inside Subinterpreter[/-] [+]Weird `int.__str__` behaviour inside sub-interpreters[/+] on Apr 2, 2024
  6. mliezun commented on Apr 3, 2024

    @mliezun
    Author

    Awesome! Thanks for looking into this @Eclips4 and @Elchinchel.

    By running the simplified script, I noticed that other base types have the same issue: bool, float, complex.

    The commit de64e75 you mentioned seems to change how the MRO works for base-types. I think that's probably related to the problem.

  7. ericsnowcurrently commented on Jul 10, 2024

    @ericsnowcurrently
    Member

    FTR, #117660 (comment) provides further analysis.

  8. added
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Jul 10, 2024
  9. added a commit that references this issue on Jul 11, 2024
  10. added a commit that references this issue on Jul 11, 2024
  11. 8 remaining items

  12. added 6 commits that reference this issue on Jul 23, 2024
  13. added 2 commits that reference this issue on Jul 24, 2024
  14. added 3 commits that reference this issue on Jul 29, 2024
  15. added a commit that references this issue on Aug 12, 2024
  16. added a commit that references this issue on Aug 22, 2024
  17. added a commit that references this issue on Sep 9, 2024
  18. encukou commented on Sep 9, 2024

    @encukou
    Member

    Note that current PRs are closed, the issue should stay open. @ericsnowcurrently said in #122865:

    I'll circle back to it in the future, particularly after I've improved
    the tests so no corner cases slip through the cracks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13only security fixes3.14bugs and security fixestopic-subinterpreterstype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions