python / python/cpython

Function version numbers do not obey necessary invariants

Offen
#117,086 5 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@markshannon arbeitet bereits daran.

Seit 20.3.2024.

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

Beschreibung

Bug report

Bug description:

It is a necessary invariant of version numbers that an equal version number means that the objects are equivalent. That is, all operations on the two objects will give the same result.

This works for class and dict version numbers as no version number is shared between different objects and any change to the object results in a new version number.

However, we share version numbers for functions and that causes problems. https://github.com/python/cpython/issues/117051
We have good reasons for sharing version numbers. It allows much better optimization of closures. https://github.com/python/cpython/pull/98525

Want we want is a scheme that allows us to share version numbers, but preserves the invariant that the same function number means a function that is functionally identical.

Assuming that the function version gets modified whenever the function version is modified, if we want to share versions we only need to ensure the invariant when creating a function.
We do not need to ensure that all functions that are functionally identical share a version, just enough to optimize most common cases.

A function consists of the following fields that we care about when optimizing:

  • Code object
  • Globals
  • Builtins
  • Defaults (only the number, not the actual values)

It is MAKE_FUNCTION that is responsible for setting the version number., which we get only from the code object, meaning that if the globals, builtins or defaults differ from any other function with the same code object, the version number is invalid.

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-117093

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.