python / python/cpython

Code object creation (const interning) can mutate tuples

Offen
#121,954 0 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@encukou arbeitet bereits daran.

Seit 18.7.2024.

type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

In https://github.com/python/cpython/issues/121863#issuecomment-2232849007, 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
  • gh-122180

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.