From 2042ea2800256bf54b304967a0f246e0f1a185c1 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Wed, 18 Nov 2026 18:01:19 +0100 Subject: [PATCH 1/5] gh-158401: Mask out the deferred bit in PyStackRef_Is[True,False,None] --- Include/internal/pycore_stackref.h | 8 +------- .../2026-11-19-15-40-29.gh-issue-158401.0HWFNv.rst | 2 ++ 2 files changed, 3 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-19-15-40-29.gh-issue-158401.0HWFNv.rst diff --git a/Include/internal/pycore_stackref.h b/Include/internal/pycore_stackref.h index 9495ccc8ac38896..a3893c96b866b50 100644 --- a/Include/internal/pycore_stackref.h +++ b/Include/internal/pycore_stackref.h @@ -484,16 +484,10 @@ static const _PyStackRef PyStackRef_NULL = { .bits = PyStackRef_NULL_BITS }; #define PyStackRef_False ((_PyStackRef){.bits = ((uintptr_t)&_Py_FalseStruct) | Py_TAG_REFCNT }) #define PyStackRef_None ((_PyStackRef){.bits = ((uintptr_t)&_Py_NoneStruct) | Py_TAG_REFCNT }) -#ifdef Py_GIL_DISABLED -// Checks that mask out the deferred bit in the free threading build. +// Checks mask out the flag bit. #define PyStackRef_IsNone(REF) (((REF).bits & ~Py_TAG_REFCNT) == (uintptr_t)&_Py_NoneStruct) #define PyStackRef_IsTrue(REF) (((REF).bits & ~Py_TAG_REFCNT) == (uintptr_t)&_Py_TrueStruct) #define PyStackRef_IsFalse(REF) (((REF).bits & ~Py_TAG_REFCNT) == (uintptr_t)&_Py_FalseStruct) -#else -#define PyStackRef_IsTrue(REF) ((REF).bits == (((uintptr_t)&_Py_TrueStruct) | Py_TAG_REFCNT)) -#define PyStackRef_IsFalse(REF) ((REF).bits == (((uintptr_t)&_Py_FalseStruct) | Py_TAG_REFCNT)) -#define PyStackRef_IsNone(REF) ((REF).bits == (((uintptr_t)&_Py_NoneStruct) | Py_TAG_REFCNT)) -#endif #define PyStackRef_IsNullOrInt(stackref) (PyStackRef_IsNull(stackref) || PyStackRef_IsTaggedInt(stackref)) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-19-15-40-29.gh-issue-158401.0HWFNv.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-19-15-40-29.gh-issue-158401.0HWFNv.rst new file mode 100644 index 000000000000000..734602b82137adb --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-19-15-40-29.gh-issue-158401.0HWFNv.rst @@ -0,0 +1,2 @@ +Fixed unreliable truthiness checks in long-running applications that use +Stable ABI extensions compiled on Python 3.11 or lower. From 5345b7f9a55d7df21cc37052c1b7e1a9a2462bd5 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 24 Nov 2026 16:27:39 +0100 Subject: [PATCH 2/5] Add regression test for the bool StackRefs --- Lib/test/test_capi/test_immortal.py | 41 ++++++++++++++++++++++++++++- Modules/_testinternalcapi.c | 17 ++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_immortal.py b/Lib/test/test_capi/test_immortal.py index 6b882d65a423910..5e24d7637e3b899 100644 --- a/Lib/test/test_capi/test_immortal.py +++ b/Lib/test/test_capi/test_immortal.py @@ -1,5 +1,6 @@ import unittest -from test.support import import_helper +from test.support import import_helper, Py_GIL_DISABLED, subTests +from test.support.script_helper import assert_python_ok _testcapi = import_helper.import_module('_testcapi') _testinternalcapi = import_helper.import_module('_testinternalcapi') @@ -38,5 +39,43 @@ def test_immortal_builtins(self): self.assertFalse(_testinternalcapi.is_static_immortal(obj)) + @unittest.skipIf(Py_GIL_DISABLED, + "can't de-immortalize on free-threaded build") + def test_deimmortalized_bool(self): + # gh-158401: Old Stable ABI extensions de-immortalize objects, + # which could change a bool's truth value. + # Run in a subprocess, since other tests assume deimmortalization + # doesn't happen. + assert_python_ok("-c", f""" + import sys + import _testcapi + import _testinternalcapi + + class C: + def __init__(self): + self.flag = True + + def check_attr(o): + if not o.flag: + raise RuntimeError("o.flag is True but tested false") + return "ok" + + def get_true(): + return True + + def check_return(): + if not get_true(): + raise RuntimeError("get_true() returned True but tested false") + return "ok" + + o = C() + for _ in range(1000): + check_attr(o), check_return() + + _testinternalcapi.deimmortalize(True) + check_attr(o) + check_return() + """) + if __name__ == "__main__": unittest.main() diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 586b687f233ce02..dca0fe6befb0e4e 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -3130,6 +3130,21 @@ is_static_immortal(PyObject *self, PyObject *op) Py_RETURN_FALSE; } +#ifndef Py_GIL_DISABLED +// Make an immortal object mortal, which old Stable ABI extensions can do. +static PyObject * +deimmortalize(PyObject *self, PyObject *op) +{ + if (!_Py_IsImmortal(op)) { + PyErr_SetString(PyExc_ValueError, "object is not immortal"); + return NULL; + } + op->ob_refcnt = _Py_IMMORTAL_MINIMUM_REFCNT - (1 << 20); + assert(!_Py_IsImmortal(op)); + Py_RETURN_NONE; +} +#endif + static PyObject * incref_decref_delayed(PyObject *self, PyObject *op) { @@ -3591,6 +3606,8 @@ static PyMethodDef module_functions[] = { {"get_tlbc", get_tlbc, METH_O, NULL}, {"get_tlbc_id", get_tlbc_id, METH_O, NULL}, {"get_long_lived_total", get_long_lived_total, METH_NOARGS}, +#else + {"deimmortalize", deimmortalize, METH_O}, #endif #ifdef _Py_TIER2 {"uop_symbols_test", _Py_uop_symbols_test, METH_NOARGS}, From 18f4dcdb45db8ff5ebc5f7ba7e69d9db275baaba Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 24 Nov 2026 16:29:21 +0100 Subject: [PATCH 3/5] Keep _Py_STATICALLY_ALLOCATED_FLAG in _Py_SetImmortal --- Lib/test/test_capi/test_immortal.py | 26 ++++++++++++++++++++++++++ Objects/object.c | 2 +- Python/pystate.c | 16 ++++++++++++++-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_capi/test_immortal.py b/Lib/test/test_capi/test_immortal.py index 5e24d7637e3b899..a855a47f43ddfce 100644 --- a/Lib/test/test_capi/test_immortal.py +++ b/Lib/test/test_capi/test_immortal.py @@ -38,6 +38,32 @@ def test_immortal_builtins(self): for obj in ([], {}, set()): self.assertFalse(_testinternalcapi.is_static_immortal(obj)) + @unittest.skipIf(Py_GIL_DISABLED, + "can't de-immortalize on free-threaded build") + @subTests( + 'obj_repr', + ['None', 'True', 'False', '-1', '256', '""', 'b""', 'type'], + ) + def test_deimmortalized_static_object_in_subinterpreter(self, obj_repr): + # gh-158401: Old Stable ABI extensions de-immortalize objects. + # Re-immortalizing such an object (e.g. when a subinterpreter + # is created) must keep it static. + # Run in a subprocess, since other tests assume deimmortalization + # doesn't happen. + assert_python_ok("-c", f""" + import sys + import _testcapi + import _testinternalcapi + + obj = {obj_repr} + assert _testinternalcapi.is_static_immortal(obj) + _testinternalcapi.deimmortalize(obj) + _testcapi.run_in_subinterp("pass") + # On 64-builds, the static flag is retained + if sys.maxsize > 2**32: + assert _testinternalcapi.is_static_immortal(obj) + """) + @unittest.skipIf(Py_GIL_DISABLED, "can't de-immortalize on free-threaded build") diff --git a/Objects/object.c b/Objects/object.c index 0695cc090c979c4..fb94d6abdd99e47 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -2796,7 +2796,7 @@ _Py_SetImmortalUntracked(PyObject *op) _Py_atomic_store_ssize_relaxed(&op->ob_ref_shared, 0); _Py_atomic_or_uint8(&op->ob_gc_bits, _PyGC_BITS_DEFERRED); #elif SIZEOF_VOID_P > 4 - op->ob_flags = _Py_IMMORTAL_FLAGS; + op->ob_flags |= _Py_IMMORTAL_FLAGS; op->ob_refcnt = _Py_IMMORTAL_INITIAL_REFCNT; #else op->ob_refcnt = _Py_IMMORTAL_INITIAL_REFCNT; diff --git a/Python/pystate.c b/Python/pystate.c index 4c95669f9fa1b09..efb3ff6576a1be4 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -881,8 +881,20 @@ common_constants_clear(PyInterpreterState *interp) PyStackRef_XCLOSE(ref); interp->common_consts[i] = PyStackRef_NULL; // Refcount reclamation skips heap immortals; release manually. - if (_Py_IsImmortal(obj) && !_Py_IsStaticImmortal(obj)) { - _Py_ClearImmortal(obj); + if (_Py_IsImmortal(obj)) { + // Only `all` and `any` are heap objects; the rest are statically + // allocated. + // Don't rely on _Py_IsStaticImmortal(). + if (i == CONSTANT_BUILTIN_ALL || i == CONSTANT_BUILTIN_ANY) { + _Py_ClearImmortal(obj); + } +#if !defined(Py_GIL_DISABLED) && SIZEOF_VOID_P <= 4 + else { + // On 64-bit and free-threading builds, + // we can trust _Py_IsStaticImmortal. + assert (!_Py_IsStaticImmortal(obj)); + } +#endif } } } From fa85cadd48c2d11a8a5cf0203d980d9a984b8dcf Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Wed, 25 Nov 2026 17:34:45 +0100 Subject: [PATCH 4/5] Treat flag-based static immortals as immortal --- Include/refcount.h | 3 ++- Modules/_testinternalcapi.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Include/refcount.h b/Include/refcount.h index 4804caec2bc8457..0c20932e78d6d67 100644 --- a/Include/refcount.h +++ b/Include/refcount.h @@ -129,7 +129,8 @@ static inline Py_ALWAYS_INLINE int _Py_IsImmortal(PyObject *op) return (_Py_atomic_load_uint32_relaxed(&op->ob_ref_local) == _Py_IMMORTAL_REFCNT_LOCAL); #elif SIZEOF_VOID_P > 4 - return _Py_CAST(int32_t, op->ob_refcnt) < 0; + return (_Py_CAST(int32_t, op->ob_refcnt) < 0) + || ((op->ob_flags & _Py_STATICALLY_ALLOCATED_FLAG) != 0); #else return op->ob_refcnt >= _Py_IMMORTAL_MINIMUM_REFCNT; #endif diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index dca0fe6befb0e4e..6fe11d040cc9110 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -3140,7 +3140,7 @@ deimmortalize(PyObject *self, PyObject *op) return NULL; } op->ob_refcnt = _Py_IMMORTAL_MINIMUM_REFCNT - (1 << 20); - assert(!_Py_IsImmortal(op)); + assert(!_Py_IsImmortal(op) || _Py_IsStaticImmortal(op)); Py_RETURN_NONE; } #endif From fce2c23bb1396466d4502cc3880141449a45dc7c Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 24 Nov 2026 16:48:28 +0100 Subject: [PATCH 5/5] Skip unreliable assertions Only check immortality in GIL builds, and only check static immortality if it's flag-based. --- Include/internal/pycore_global_objects_fini.h | 5 +- Include/internal/pycore_long.h | 3 +- Include/internal/pycore_object.h | 53 ++++++++++++++++--- Include/internal/pycore_stackref.h | 8 +-- Modules/_asynciomodule.c | 2 +- Modules/_testinternalcapi/test_cases.c.h | 3 +- Modules/_typesmodule.c | 2 +- Objects/bytearrayobject.c | 2 +- Objects/bytesobject.c | 4 +- Objects/codeobject.c | 2 +- Objects/exceptions.c | 2 +- Objects/listobject.c | 2 +- Objects/object.c | 2 +- Objects/structseq.c | 4 +- Objects/typeobject.c | 10 ++-- Objects/unicodeobject.c | 18 +++---- Python/bytecodes.c | 6 ++- Python/crossinterp.c | 6 ++- Python/executor_cases.c.h | 15 ++++-- Python/flowgraph.c | 4 +- Python/generated_cases.c.h | 3 +- Python/instrumentation.c | 2 +- Python/pystate.c | 6 +-- 23 files changed, 104 insertions(+), 60 deletions(-) diff --git a/Include/internal/pycore_global_objects_fini.h b/Include/internal/pycore_global_objects_fini.h index 41bc2c5d8fb1405..b47111e1c9dffe3 100644 --- a/Include/internal/pycore_global_objects_fini.h +++ b/Include/internal/pycore_global_objects_fini.h @@ -12,12 +12,13 @@ extern "C" { #include "pycore_bytesobject.h" // _PyBytes_CheckOverflow() #include "pycore_long.h" // TAG_FROM_SIGN_AND_SIZE() +#include "pycore_object.h" // _Py_IMMORTALITY_IS_RELIABLE static inline void _PyStaticObject_CheckSingleton(PyObject *obj, PyTypeObject *type) { - // Check PyObject.ob_refcnt - _PyObject_ASSERT(obj, _Py_IsImmortal(obj)); + // Check immortality + _PyObject_ASSERT(obj, _Py_IsImmortal(obj) || !_Py_IMMORTALITY_IS_RELIABLE); // Check PyObject.ob_type _PyObject_ASSERT(obj, Py_TYPE(obj) == type); diff --git a/Include/internal/pycore_long.h b/Include/internal/pycore_long.h index fb5622c99f7a135..ff3cb289f73a8d7 100644 --- a/Include/internal/pycore_long.h +++ b/Include/internal/pycore_long.h @@ -10,6 +10,7 @@ extern "C" { #include "pycore_bytesobject.h" // _PyBytesWriter #include "pycore_runtime.h" // _Py_SINGLETON() +#include "pycore_object.h" // _Py_ASSERT_STATIC_IMMORTAL /* * Default int base conversion size limitation: Denial of Service prevention. @@ -240,7 +241,7 @@ _PyLong_IsSmallInt(const PyLongObject *op) bool is_small_int = (op->long_value.lv_tag & IMMORTALITY_BIT_MASK) != 0; if (is_small_int) { assert(PyLong_CheckExact(op)); - assert(_Py_IsImmortal(op)); + _Py_ASSERT_STATIC_IMMORTAL(op); assert((_PyLong_IsCompact(op) && _PY_IS_SMALL_INT(_PyLong_CompactValue(op)))); } diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h index 30333e14655a60e..49502d22792e611 100644 --- a/Include/internal/pycore_object.h +++ b/Include/internal/pycore_object.h @@ -18,6 +18,41 @@ extern "C" { #include // bool +// Stable ABI extensions compiled for Python 3.11 and lower can make +// immortal objects, including statically allocated ones, mortal. +// free-threading and 64-bit-GIL builds use flags to track immortality, +// so _Py_IsImmortal and _Py_IsStaticImmortal are trustworthy. +// On 32-bit-GIL builds, they are not. +#ifndef _Py_IMMORTALITY_IS_RELIABLE +# if defined(Py_GIL_DISABLED) +# define _Py_IMMORTALITY_IS_RELIABLE 1 +# else +# define _Py_IMMORTALITY_IS_RELIABLE 0 +# endif +#endif +#ifndef _Py_STATIC_IMMORTALITY_IS_RELIABLE +# if defined(Py_GIL_DISABLED) || (SIZEOF_VOID_P > 4) +# define _Py_STATIC_IMMORTALITY_IS_RELIABLE 1 +# else +# define _Py_STATIC_IMMORTALITY_IS_RELIABLE 0 +# endif +#endif +// Define the most common assertion macros. +#if _Py_IMMORTALITY_IS_RELIABLE +# define _Py_ASSERT_IMMORTAL(op) assert(_Py_IsImmortal(op)) +# define _Py_ASSERT_MORTAL(op) assert(!_Py_IsImmortal(op)) +#else +# define _Py_ASSERT_IMMORTAL(op) ((void)(op)) +# define _Py_ASSERT_MORTAL(op) ((void)(op)) +#endif +#if _Py_STATIC_IMMORTALITY_IS_RELIABLE +# define _Py_ASSERT_STATIC_IMMORTAL(op) assert(_Py_IsStaticImmortal(op)) +# define _Py_ASSERT_NOT_STATIC_IMMORTAL(op) assert(!_Py_IsStaticImmortal(op)) +#else +# define _Py_ASSERT_STATIC_IMMORTAL(op) ((void)(op)) +# define _Py_ASSERT_NOT_STATIC_IMMORTAL(op) ((void)(op)) +#endif + // This value is added to `ob_ref_shared` for objects that use deferred // reference counting so that they are not immediately deallocated when the @@ -200,7 +235,7 @@ PyAPI_FUNC(void) _Py_SetImmortalUntracked(PyObject *op); static inline void _Py_SetMortal(PyObject *op, short refcnt) { if (op) { - assert(_Py_IsImmortal(op)); + _Py_ASSERT_IMMORTAL(op); #ifdef Py_GIL_DISABLED op->ob_tid = _Py_UNOWNED_TID; op->ob_ref_local = 0; @@ -406,7 +441,7 @@ static inline void Py_DECREF_MORTAL(const char *filename, int lineno, PyObject * _Py_NegativeRefcount(filename, lineno, op); } _Py_DECREF_STAT_INC(); - assert(!_Py_IsStaticImmortal(op)); + _Py_ASSERT_NOT_STATIC_IMMORTAL(op); if (!_Py_IsImmortal(op)) { _Py_DECREF_DecRefTotal(); } @@ -422,7 +457,7 @@ static inline void _Py_DECREF_MORTAL_SPECIALIZED(const char *filename, int linen _Py_NegativeRefcount(filename, lineno, op); } _Py_DECREF_STAT_INC(); - assert(!_Py_IsStaticImmortal(op)); + _Py_ASSERT_NOT_STATIC_IMMORTAL(op); if (!_Py_IsImmortal(op)) { _Py_DECREF_DecRefTotal(); } @@ -440,7 +475,7 @@ static inline void _Py_DECREF_MORTAL_SPECIALIZED(const char *filename, int linen static inline void Py_DECREF_MORTAL(PyObject *op) { - assert(!_Py_IsStaticImmortal(op)); + _Py_ASSERT_NOT_STATIC_IMMORTAL(op); _Py_DECREF_STAT_INC(); if (--op->ob_refcnt == 0) { _Py_Dealloc(op); @@ -450,7 +485,7 @@ static inline void Py_DECREF_MORTAL(PyObject *op) static inline void Py_DECREF_MORTAL_SPECIALIZED(PyObject *op, destructor destruct) { - assert(!_Py_IsStaticImmortal(op)); + _Py_ASSERT_NOT_STATIC_IMMORTAL(op); _Py_DECREF_STAT_INC(); if (--op->ob_refcnt == 0) { _PyReftracerTrack(op, PyRefTracer_DESTROY); @@ -475,7 +510,11 @@ _PyObject_Init(PyObject *op, PyTypeObject *typeobj) { assert(op != NULL); Py_SET_TYPE(op, typeobj); - assert(_PyType_HasFeature(typeobj, Py_TPFLAGS_HEAPTYPE) || _Py_IsImmortal(typeobj)); +#ifndef NDEBUG + if (!_PyType_HasFeature(typeobj, Py_TPFLAGS_HEAPTYPE)) { + _Py_ASSERT_IMMORTAL(typeobj); + } +#endif _Py_INCREF_TYPE(typeobj); _Py_NewReference(op); } @@ -987,7 +1026,7 @@ extern int _PyObject_SetManagedDict(PyObject *obj, PyObject *new_dict); #ifndef Py_GIL_DISABLED static inline Py_ALWAYS_INLINE void _Py_INCREF_MORTAL(PyObject *op) { - assert(!_Py_IsStaticImmortal(op)); + _Py_ASSERT_MORTAL(op); op->ob_refcnt++; _Py_INCREF_STAT_INC(); #if defined(Py_REF_DEBUG) && !defined(Py_LIMITED_API) diff --git a/Include/internal/pycore_stackref.h b/Include/internal/pycore_stackref.h index a3893c96b866b50..76cca65b2c52413 100644 --- a/Include/internal/pycore_stackref.h +++ b/Include/internal/pycore_stackref.h @@ -270,7 +270,7 @@ _PyStackRef_DupImmortal(_PyStackRef ref, const char *filename, int linenumber) assert(!PyStackRef_IsTaggedInt(ref)); assert(!PyStackRef_RefcountOnObject(ref)); PyObject *obj = _Py_stackref_get_object(ref); - assert(_Py_IsImmortal(obj)); + _Py_ASSERT_IMMORTAL(obj); return _Py_stackref_create(obj, Py_TAG_REFCNT, filename, linenumber); } #define PyStackRef_DupImmortal(REF) _PyStackRef_DupImmortal((REF), __FILE__, __LINE__) @@ -361,7 +361,7 @@ static inline _PyStackRef _PyStackRef_FromPyObjectNewMortal(PyObject *obj, const char *filename, int linenumber) { assert(obj != NULL); - assert(!_Py_IsStaticImmortal(obj)); + _Py_ASSERT_NOT_STATIC_IMMORTAL(obj); Py_INCREF(obj); return _Py_stackref_create(obj, 0, filename, linenumber); } @@ -502,7 +502,7 @@ PyStackRef_CheckValid(_PyStackRef ref) switch (tag) { case 0: /* Can be immortal if object was made immortal after reference came into existence */ - assert(!_Py_IsStaticImmortal(obj)); + _Py_ASSERT_NOT_STATIC_IMMORTAL(obj); break; case Py_TAG_REFCNT: break; @@ -644,7 +644,7 @@ PyStackRef_DupImmortal(_PyStackRef ref) { assert(!PyStackRef_IsNull(ref)); assert(!PyStackRef_RefcountOnObject(ref)); - assert(_Py_IsImmortal(BITS_TO_PTR_MASKED(ref))); + _Py_ASSERT_IMMORTAL(BITS_TO_PTR_MASKED(ref)); return ref; } diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 5381cda4067ac0f..55330f9ce3e548c 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -1600,7 +1600,7 @@ _asyncio_Future__state_get_impl(FutureObj *self) default: assert (0); } - assert(_Py_IsImmortal(ret)); + _Py_ASSERT_IMMORTAL(ret); return ret; } diff --git a/Modules/_testinternalcapi/test_cases.c.h b/Modules/_testinternalcapi/test_cases.c.h index 8bb3f857437792a..add12991ed989bb 100644 --- a/Modules/_testinternalcapi/test_cases.c.h +++ b/Modules/_testinternalcapi/test_cases.c.h @@ -376,7 +376,8 @@ JUMP_TO_LABEL(error); } assert(d->result_type == NULL || Py_TYPE(res_o) == d->result_type); - assert(!d->result_unique || Py_REFCNT(res_o) == 1 || _Py_IsImmortal(res_o)); + assert(!d->result_unique || Py_REFCNT(res_o) == 1 || _Py_IsImmortal(res_o) + || !_Py_IMMORTALITY_IS_RELIABLE); assert(!PyFloat_CheckExact(res_o) || Py_REFCNT(res_o) == 1); res = PyStackRef_FromPyObjectSteal(res_o); l = left; diff --git a/Modules/_typesmodule.c b/Modules/_typesmodule.c index 232c6cd46d1aa83..709411cb0444cc9 100644 --- a/Modules/_typesmodule.c +++ b/Modules/_typesmodule.c @@ -12,7 +12,7 @@ _types_exec(PyObject *m) { #define EXPORT_STATIC_TYPE(NAME, TYPE) \ do { \ - assert(PyUnstable_IsImmortal((PyObject *)&(TYPE))); \ + _Py_ASSERT_STATIC_IMMORTAL((PyObject *)&(TYPE)); \ if (PyModule_AddObjectRef(m, (NAME), (PyObject *)&(TYPE)) < 0) { \ return -1; \ } \ diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index b9d547575bd2eba..354dfa1e9c8d2ca 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -63,7 +63,7 @@ bytearray_reinit_from_bytes(PyByteArrayObject *self, Py_ssize_t size) } else { // Use the empty bytes string singleton for an empty bytearray - assert(_Py_IsImmortal(self->ob_bytes_object)); + _Py_ASSERT_IMMORTAL(self->ob_bytes_object); } self->ob_bytes = self->ob_start = PyBytes_AS_STRING(self->ob_bytes_object); diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index e9264f765a08e6d..823fc95480d32e2 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -46,7 +46,7 @@ static inline PyObject* bytes_get_empty(void) { PyObject *empty = &EMPTY->ob_base.ob_base; - assert(_Py_IsImmortal(empty)); + _Py_ASSERT_STATIC_IMMORTAL(empty); return empty; } @@ -56,7 +56,7 @@ static inline PyObject* bytes_get_char(uint8_t ch) { PyObject *obj = (PyObject*)CHARACTER(ch); - assert(_Py_IsImmortal(obj)); + _Py_ASSERT_STATIC_IMMORTAL(obj); return obj; } diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 2255220bec3d307..371da16cdf4f19d 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -3127,7 +3127,7 @@ intern_one_constant(PyObject *op) return op; } - assert(_Py_IsImmortal(entry->value)); + _Py_ASSERT_IMMORTAL(entry->value); return (PyObject *)entry->value; } diff --git a/Objects/exceptions.c b/Objects/exceptions.c index dc0ba0d7e533dda..aff7ffac3d56a60 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1435,7 +1435,7 @@ BaseExceptionGroup_split_impl(PyBaseExceptionGroupObject *self, return NULL; } - assert(_Py_IsStaticImmortal(Py_None)); + _Py_ASSERT_STATIC_IMMORTAL(Py_None); PyObject *result = _PyTuple_FromPairSteal( split_result.match ? split_result.match : Py_None, split_result.rest ? split_result.rest : Py_None); diff --git a/Objects/listobject.c b/Objects/listobject.c index c5fe704df0e23de..6a9c1e1c3d2c59c 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2850,7 +2850,7 @@ unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms) if (PyBool_Check(res_obj)) { res = (res_obj == Py_True); - assert(_Py_IsImmortal(res_obj)); + _Py_ASSERT_STATIC_IMMORTAL(res_obj); } else { res = PyObject_IsTrue(res_obj); diff --git a/Objects/object.c b/Objects/object.c index 38538d86860e66e..0f29c0c3f2f5331 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1144,7 +1144,7 @@ PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) return -1; if (PyBool_Check(res)) { ok = (res == Py_True); - assert(_Py_IsImmortal(res)); + _Py_ASSERT_STATIC_IMMORTAL(res); } else { ok = PyObject_IsTrue(res); diff --git a/Objects/structseq.c b/Objects/structseq.c index 949790e6f416e95..f67bd0eb34218b5 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -637,7 +637,7 @@ _PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp, assert(type->tp_members != NULL); assert(type->tp_base == &PyTuple_Type); assert((type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN)); - assert(_Py_IsImmortal(type)); + _Py_ASSERT_IMMORTAL(type); } #endif @@ -715,7 +715,7 @@ _PyStructSequence_FiniBuiltin(PyInterpreterState *interp, PyTypeObject *type) assert(type->tp_name != NULL); assert(type->tp_base == &PyTuple_Type); assert((type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN)); - assert(_Py_IsImmortal(type)); + _Py_ASSERT_IMMORTAL(type); // Cannot delete a type if it still has subclasses if (_PyType_HasSubclasses(type)) { diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 68908c3f0bae626..ad80e5fa892d41f 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -553,7 +553,7 @@ set_tp_bases(PyTypeObject *self, PyObject *bases) assert(PyTuple_GET_SIZE(bases) == 1); assert(PyTuple_GET_ITEM(bases, 0) == (PyObject *)self->tp_base); assert(self->tp_base->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN); - assert(_Py_IsImmortal(self->tp_base)); + _Py_ASSERT_IMMORTAL(self->tp_base); } _Py_SetImmortal(bases); } @@ -592,7 +592,7 @@ clear_tp_bases(PyTypeObject *self, int final) Py_CLEAR(self->tp_bases); } else { - assert(_Py_IsImmortal(self->tp_bases)); + _Py_ASSERT_IMMORTAL(self->tp_bases); _Py_ClearImmortal(self->tp_bases); } } @@ -661,7 +661,7 @@ clear_tp_mro(PyTypeObject *self, int final) Py_CLEAR(self->tp_mro); } else { - assert(_Py_IsImmortal(self->tp_mro)); + _Py_ASSERT_IMMORTAL(self->tp_mro); _Py_ClearImmortal(self->tp_mro); } } @@ -6889,7 +6889,7 @@ fini_static_type(PyInterpreterState *interp, PyTypeObject *type, int isbuiltin, int final) { assert(type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN); - assert(_Py_IsImmortal((PyObject *)type)); + _Py_ASSERT_IMMORTAL((PyObject *)type); type_dealloc_common(type); @@ -9704,7 +9704,7 @@ static int init_static_type(PyInterpreterState *interp, PyTypeObject *self, int isbuiltin, int initial) { - assert(_Py_IsImmortal((PyObject *)self)); + _Py_ASSERT_IMMORTAL((PyObject *)self); assert(!(self->tp_flags & Py_TPFLAGS_HEAPTYPE)); assert(!(self->tp_flags & Py_TPFLAGS_MANAGED_DICT)); assert(!(self->tp_flags & Py_TPFLAGS_MANAGED_WEAKREF)); diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 309621acdaa3c8c..7489d26b1ed58fb 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -579,13 +579,6 @@ _PyUnicode_CheckConsistency(PyObject *op, int check_content) { #define CHECK(expr) \ do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0) -#ifdef Py_GIL_DISABLED -# define CHECK_IF_GIL(expr) (void)(expr) -# define CHECK_IF_FT(expr) CHECK(expr) -#else -# define CHECK_IF_GIL(expr) CHECK(expr) -# define CHECK_IF_FT(expr) (void)(expr) -#endif assert(op != NULL); CHECK(PyUnicode_Check(op)); @@ -680,20 +673,23 @@ _PyUnicode_CheckConsistency(PyObject *op, int check_content) // are static but use SSTATE_NOT_INTERNED } else { - CHECK_IF_GIL(!_Py_IsImmortal(op)); + _Py_ASSERT_MORTAL(op); } break; case SSTATE_INTERNED_MORTAL: CHECK(!ascii->state.statically_allocated); - CHECK_IF_GIL(!_Py_IsImmortal(op)); + _Py_ASSERT_MORTAL(op); + _Py_ASSERT_NOT_STATIC_IMMORTAL(op); break; case SSTATE_INTERNED_IMMORTAL: CHECK(!ascii->state.statically_allocated); - CHECK_IF_FT(_Py_IsImmortal(op)); + _Py_ASSERT_IMMORTAL(op); + _Py_ASSERT_NOT_STATIC_IMMORTAL(op); break; case SSTATE_INTERNED_IMMORTAL_STATIC: CHECK(ascii->state.statically_allocated); - CHECK_IF_FT(_Py_IsImmortal(op)); + _Py_ASSERT_IMMORTAL(op); + _Py_ASSERT_STATIC_IMMORTAL(op); break; default: Py_UNREACHABLE(); diff --git a/Python/bytecodes.c b/Python/bytecodes.c index c6f1f61f3e08fe1..771485eb43fcec0 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -364,7 +364,8 @@ dummy_func( op(_POP_TOP_NOP, (value --)) { assert(PyStackRef_IsNull(value) || (!PyStackRef_RefcountOnObject(value)) || - _Py_IsImmortal((PyStackRef_AsPyObjectBorrow(value)))); + _Py_IsImmortal((PyStackRef_AsPyObjectBorrow(value))) + || !_Py_IMMORTALITY_IS_RELIABLE); DEAD(value); } @@ -1056,7 +1057,8 @@ dummy_func( ERROR_NO_POP(); } assert(d->result_type == NULL || Py_TYPE(res_o) == d->result_type); - assert(!d->result_unique || Py_REFCNT(res_o) == 1 || _Py_IsImmortal(res_o)); + assert(!d->result_unique || Py_REFCNT(res_o) == 1 || _Py_IsImmortal(res_o) + || !_Py_IMMORTALITY_IS_RELIABLE); // The JIT and tier 2 optimizer assume that float results from // binary operations are always uniquely referenced (refcount == 1). // If this assertion fails, update the optimizer to stop marking diff --git a/Python/crossinterp.c b/Python/crossinterp.c index a91b45ba2cbcf45..5a4a9f1e169707c 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -1235,7 +1235,8 @@ _excinfo_init_type_from_exception(struct _excinfo_type *info, PyObject *exc) PyTypeObject *type = Py_TYPE(exc); if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) { - assert(_Py_IsImmortal((PyObject *)type)); + _Py_ASSERT_STATIC_IMMORTAL((PyObject *)type); + _Py_ASSERT_IMMORTAL((PyObject *)type); info->builtin = type; } else { @@ -1325,7 +1326,8 @@ _excinfo_clear_type(struct _excinfo_type *info) { if (info->builtin != NULL) { assert(info->builtin->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN); - assert(_Py_IsImmortal((PyObject *)info->builtin)); + _Py_ASSERT_STATIC_IMMORTAL((PyObject *)info->builtin); + _Py_ASSERT_IMMORTAL((PyObject *)info->builtin); } if (info->name != NULL) { PyMem_RawFree((void *)info->name); diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 5df8120e7424b93..f30dcff52a2988c 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -2273,7 +2273,8 @@ _PyStackRef value; value = stack_pointer[-1]; assert(PyStackRef_IsNull(value) || (!PyStackRef_RefcountOnObject(value)) || - _Py_IsImmortal((PyStackRef_AsPyObjectBorrow(value)))); + _Py_IsImmortal((PyStackRef_AsPyObjectBorrow(value))) + || !_Py_IMMORTALITY_IS_RELIABLE); SET_CURRENT_CACHED_VALUES(0); stack_pointer += -1; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); @@ -2288,7 +2289,8 @@ _PyStackRef _stack_item_0 = _tos_cache0; value = _stack_item_0; assert(PyStackRef_IsNull(value) || (!PyStackRef_RefcountOnObject(value)) || - _Py_IsImmortal((PyStackRef_AsPyObjectBorrow(value)))); + _Py_IsImmortal((PyStackRef_AsPyObjectBorrow(value))) + || !_Py_IMMORTALITY_IS_RELIABLE); SET_CURRENT_CACHED_VALUES(0); ASSERT_WITHIN_STACK_BOUNDS_IGNORING_CACHE(__FILE__, __LINE__); break; @@ -2302,7 +2304,8 @@ _PyStackRef _stack_item_1 = _tos_cache1; value = _stack_item_1; assert(PyStackRef_IsNull(value) || (!PyStackRef_RefcountOnObject(value)) || - _Py_IsImmortal((PyStackRef_AsPyObjectBorrow(value)))); + _Py_IsImmortal((PyStackRef_AsPyObjectBorrow(value))) + || !_Py_IMMORTALITY_IS_RELIABLE); _tos_cache0 = _stack_item_0; SET_CURRENT_CACHED_VALUES(1); ASSERT_WITHIN_STACK_BOUNDS_IGNORING_CACHE(__FILE__, __LINE__); @@ -2318,7 +2321,8 @@ _PyStackRef _stack_item_2 = _tos_cache2; value = _stack_item_2; assert(PyStackRef_IsNull(value) || (!PyStackRef_RefcountOnObject(value)) || - _Py_IsImmortal((PyStackRef_AsPyObjectBorrow(value)))); + _Py_IsImmortal((PyStackRef_AsPyObjectBorrow(value))) + || !_Py_IMMORTALITY_IS_RELIABLE); _tos_cache1 = _stack_item_1; _tos_cache0 = _stack_item_0; SET_CURRENT_CACHED_VALUES(2); @@ -6784,7 +6788,8 @@ JUMP_TO_ERROR(); } assert(d->result_type == NULL || Py_TYPE(res_o) == d->result_type); - assert(!d->result_unique || Py_REFCNT(res_o) == 1 || _Py_IsImmortal(res_o)); + assert(!d->result_unique || Py_REFCNT(res_o) == 1 || _Py_IsImmortal(res_o) + || !_Py_IMMORTALITY_IS_RELIABLE); assert(!PyFloat_CheckExact(res_o) || Py_REFCNT(res_o) == 1); res = PyStackRef_FromPyObjectSteal(res_o); l = left; diff --git a/Python/flowgraph.c b/Python/flowgraph.c index fc676dc6feb1674..ef8a005807440e9 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1444,7 +1444,7 @@ maybe_instr_make_load_smallint(cfg_instr *instr, PyObject *newconst, return -1; } if (!overflow && _PY_IS_SMALL_INT(val) && 0 <= val && val <= 255) { - assert(_Py_IsImmortal(newconst)); + _Py_ASSERT_IMMORTAL(newconst); INSTR_SET_OP1(instr, LOAD_SMALL_INT, (int)val); return 1; } @@ -1492,7 +1492,7 @@ maybe_instr_make_load_common_const(cfg_instr *instr, PyObject *newconst) else { return 0; } - assert(_Py_IsImmortal(newconst)); + _Py_ASSERT_IMMORTAL(newconst); INSTR_SET_OP1(instr, LOAD_COMMON_CONSTANT, oparg); return 1; } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 3af7863e80d2c6a..5f0bbd02813c20c 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -376,7 +376,8 @@ JUMP_TO_LABEL(error); } assert(d->result_type == NULL || Py_TYPE(res_o) == d->result_type); - assert(!d->result_unique || Py_REFCNT(res_o) == 1 || _Py_IsImmortal(res_o)); + assert(!d->result_unique || Py_REFCNT(res_o) == 1 || _Py_IsImmortal(res_o) + || !_Py_IMMORTALITY_IS_RELIABLE); assert(!PyFloat_CheckExact(res_o) || Py_REFCNT(res_o) == 1); res = PyStackRef_FromPyObjectSteal(res_o); l = left; diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 48daa91259d3c9e..2fd818815f2be3c 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -995,7 +995,7 @@ call_one_instrument( return -1; } if (res == &_PyInstrumentation_DISABLE) { - assert(_Py_IsImmortal(res)); + _Py_ASSERT_IMMORTAL(res); return 1; } Py_DECREF(res); diff --git a/Python/pystate.c b/Python/pystate.c index efb3ff6576a1be4..ae80a70a8031adc 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -888,13 +888,9 @@ common_constants_clear(PyInterpreterState *interp) if (i == CONSTANT_BUILTIN_ALL || i == CONSTANT_BUILTIN_ANY) { _Py_ClearImmortal(obj); } -#if !defined(Py_GIL_DISABLED) && SIZEOF_VOID_P <= 4 else { - // On 64-bit and free-threading builds, - // we can trust _Py_IsStaticImmortal. - assert (!_Py_IsStaticImmortal(obj)); + _Py_ASSERT_STATIC_IMMORTAL(obj); } -#endif } } }