Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code object creation (const interning) can mutate tuples #121954

Open
encukou opened this issue Jul 18, 2024 · 0 comments
Open

Code object creation (const interning) can mutate tuples #121954

encukou opened this issue Jul 18, 2024 · 0 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@encukou
Copy link
Member

encukou commented Jul 18, 2024

In #121863 (comment), Mark suspected something is off in const interning for code objects. Indeed, there is. For example:

# a bit of setup
import sys
interned = sys.intern("foobar")
def f():
    var = "foobar"

# my_tuple = (None, 'foobar'), but with an un-interned string
my_tuple = (None, str(b'foobar', 'ascii'))  

print(id(my_tuple), id(my_tuple[1]))

# pass my_tuple as an input argument for a new code object
f.__code__.replace(co_consts=my_tuple)

# there's now a different object in the tuple!
print(id(my_tuple), id(my_tuple[1]))

I'm working on a change to:

  • create new tuples instead of mutating old ones
  • intern more strings before passing them to PyCode constructors, so that we usually don't need to create new tuples

Linked PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant