@@ -881,21 +881,26 @@ common_constants_clear(PyInterpreterState *interp)
881881 PyStackRef_XCLOSE (ref );
882882 interp -> common_consts [i ] = PyStackRef_NULL ;
883883 // Refcount reclamation skips heap immortals; release manually.
884- if (_Py_IsImmortal (obj )) {
885- // Only `all` and `any` are heap objects; the rest are statically
886- // allocated.
887- // Don't rely on _Py_IsStaticImmortal().
888- bool is_heap = (
889- i == CONSTANT_BUILTIN_ALL
890- || i == CONSTANT_BUILTIN_ANY );
884+ // - All constants are immortal.
885+ // - Only `all` and `any` are heap objects; the rest are statically
886+ // allocated.
887+ // On GIL builds, old Stable ABI extensions can de-immortalize
888+ // objects. We only use Py_Is*Immortal functions to *check*
889+ // these assumptions on builds where the functions can be trusted.
890+ bool is_heap = (
891+ i == CONSTANT_BUILTIN_ALL
892+ || i == CONSTANT_BUILTIN_ANY );
893+ #if defined(Py_GIL_DISABLED )
894+ // On free-threading builds, we can trust _Py_IsImmortal.
895+ assert (_Py_IsImmortal (obj ));
896+ #endif
891897#if defined(Py_GIL_DISABLED ) || SIZEOF_VOID_P >= 4
892- // On 64-bit and free-threading builds,
893- // we can trust _Py_IsStaticImmortal.
894- assert (is_heap == !_Py_IsStaticImmortal (obj ));
898+ // On 64-bit and free-threading builds,
899+ // we can trust _Py_IsStaticImmortal.
900+ assert (is_heap == !_Py_IsStaticImmortal (obj ));
895901#endif
896- if (is_heap ) {
897- _Py_ClearImmortal (obj );
898- }
902+ if (is_heap ) {
903+ _Py_ClearImmortal (obj );
899904 }
900905 }
901906}
0 commit comments