You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is dangerous especially in the free-threaded build, which
de-duplicates constants and
immortalizes constants and relies them to be immortal
The “feature” is untested, as seen from the above snippet failing an assertion in free-threaded build.
Mutable subclasses of immutable built-in types are especially “interesting” here.
A solution would be to only allow built-in immutable marshallable types (booleans, integers, floating-point numbers, complex numbers, strings, bytes, tuples, frozensets, code objects, None, Ellipsis, StopIteration), excluding sublasses. That's a backwards-incompatible change.
Note that CPython now already recursively analyzes co_consts; there's little cost to doing additional inspection.
My current plan is:
Disallow “bad” values in free-threaded builds in 3.14, as this build is still experimental and this breaks vital invariants.
Deprecate “bad” values in regular build, and initially plan to disallow them in 3.16. (Minimal PEP-387 deprecation period chosen because this “feature” is very likely to interfere with optimizations, and unlikely to get proper test coverage.)
For optimization purposes, _PyCode_ConstantKey is generally going to be useful since we'll want to deduplicate references to both constant and non-constant objects (we won't be putting them on the code object though). It would be great if the change were made in such a way that "other objects get compared by identity without raising a warning" could still be available via a boolean parameter.
I ask, because I'm trying to write a class that ensures single ownership for threaded applications and knowing the set of allowed types in co_consts would be helpful (since co_consts gives me an extra reference to an object to account for).
Activity
brandtbucher commentedon Aug 14, 2024
Got it, thanks for the heads-up.
For optimization purposes,
_PyCode_ConstantKeyis generally going to be useful since we'll want to deduplicate references to both constant and non-constant objects (we won't be putting them on the code object though). It would be great if the change were made in such a way that "other objects get compared by identity without raising a warning" could still be available via a boolean parameter.hlovatt commentedon Oct 14, 2025
Was this ever implemented?
I ask, because I'm trying to write a class that ensures single ownership for threaded applications and knowing the set of allowed types in
co_constswould be helpful (sinceco_constsgives me an extra reference to an object to account for).