The __code__ attribute of function objects is assignable.
As a consequence, frames must hold a strong reference to the code object in case the reference held by the function is lost.
Ideally, the __code__ attribute would be read-only, but to fix that would be a multi-year deprecation effort.
Until that happens, if ever, we can replace the strong reference with a borrowed reference to the code object as long as code object is guaranteed to outlive the frame.
Since the function will outlive the frame, if the function object keeps references to its prior __code__ attributes
when __code__ is assigned, then the code object will always outlive the frame.
This will need an extra field in each function, but the cost of one assignment to NULL during function creation, is much less than the cost of an incref and a decref on every call to that function.
The only downside is that in some obscure cases the lifetime of a few code objects will be extended.
Since the free-threading build makes all code objects immortal, this could actually be a big saving for the free-threading build.
The
__code__attribute of function objects is assignable.As a consequence, frames must hold a strong reference to the code object in case the reference held by the function is lost.
Ideally, the
__code__attribute would be read-only, but to fix that would be a multi-year deprecation effort.Until that happens, if ever, we can replace the strong reference with a borrowed reference to the code object as long as code object is guaranteed to outlive the frame.
Since the function will outlive the frame, if the function object keeps references to its prior
__code__attributeswhen
__code__is assigned, then the code object will always outlive the frame.This will need an extra field in each function, but the cost of one assignment to
NULLduring function creation, is much less than the cost of an incref and a decref on every call to that function.The only downside is that in some obscure cases the lifetime of a few code objects will be extended.
Since the free-threading build makes all code objects immortal, this could actually be a big saving for the free-threading build.