@@ -18,6 +18,41 @@ extern "C" {
1818
1919#include <stdbool.h> // bool
2020
21+ // Stable ABI extensions compiled for Python 3.11 and lower can make
22+ // immortal objects, including statically allocated ones, mortal.
23+ // free-threading and 64-bit-GIL builds use flags to track immortality,
24+ // so _Py_IsImmortal and _Py_IsStaticImmortal are trustworthy.
25+ // On 32-bit-GIL builds, they are not.
26+ #ifndef _Py_IMMORTALITY_IS_RELIABLE
27+ # if defined(Py_GIL_DISABLED )
28+ # define _Py_IMMORTALITY_IS_RELIABLE 1
29+ # else
30+ # define _Py_IMMORTALITY_IS_RELIABLE 0
31+ # endif
32+ #endif
33+ #ifndef _Py_STATIC_IMMORTALITY_IS_RELIABLE
34+ # if defined(Py_GIL_DISABLED ) || (SIZEOF_VOID_P > 4 )
35+ # define _Py_STATIC_IMMORTALITY_IS_RELIABLE 1
36+ # else
37+ # define _Py_STATIC_IMMORTALITY_IS_RELIABLE 0
38+ # endif
39+ #endif
40+ // Define the most common assertion macros.
41+ #if _Py_IMMORTALITY_IS_RELIABLE
42+ # define _Py_ASSERT_IMMORTAL (op ) assert(_Py_IsImmortal(op))
43+ # define _Py_ASSERT_MORTAL (op ) assert(!_Py_IsImmortal(op))
44+ #else
45+ # define _Py_ASSERT_IMMORTAL (op ) ((void)(op))
46+ # define _Py_ASSERT_MORTAL (op ) ((void)(op))
47+ #endif
48+ #if _Py_STATIC_IMMORTALITY_IS_RELIABLE
49+ # define _Py_ASSERT_STATIC_IMMORTAL (op ) assert(_Py_IsStaticImmortal(op))
50+ # define _Py_ASSERT_NOT_STATIC_IMMORTAL (op ) assert(!_Py_IsStaticImmortal(op))
51+ #else
52+ # define _Py_ASSERT_STATIC_IMMORTAL (op ) ((void)(op))
53+ # define _Py_ASSERT_NOT_STATIC_IMMORTAL (op ) ((void)(op))
54+ #endif
55+
2156
2257// This value is added to `ob_ref_shared` for objects that use deferred
2358// reference counting so that they are not immediately deallocated when the
@@ -200,7 +235,7 @@ PyAPI_FUNC(void) _Py_SetImmortalUntracked(PyObject *op);
200235static inline void _Py_SetMortal (PyObject * op , short refcnt )
201236{
202237 if (op ) {
203- assert ( _Py_IsImmortal ( op ) );
238+ _Py_ASSERT_IMMORTAL ( op );
204239#ifdef Py_GIL_DISABLED
205240 op -> ob_tid = _Py_UNOWNED_TID ;
206241 op -> ob_ref_local = 0 ;
@@ -406,7 +441,7 @@ static inline void Py_DECREF_MORTAL(const char *filename, int lineno, PyObject *
406441 _Py_NegativeRefcount (filename , lineno , op );
407442 }
408443 _Py_DECREF_STAT_INC ();
409- assert (! _Py_IsStaticImmortal ( op ) );
444+ _Py_ASSERT_NOT_STATIC_IMMORTAL ( op );
410445 if (!_Py_IsImmortal (op )) {
411446 _Py_DECREF_DecRefTotal ();
412447 }
@@ -422,7 +457,7 @@ static inline void _Py_DECREF_MORTAL_SPECIALIZED(const char *filename, int linen
422457 _Py_NegativeRefcount (filename , lineno , op );
423458 }
424459 _Py_DECREF_STAT_INC ();
425- assert (! _Py_IsStaticImmortal ( op ) );
460+ _Py_ASSERT_NOT_STATIC_IMMORTAL ( op );
426461 if (!_Py_IsImmortal (op )) {
427462 _Py_DECREF_DecRefTotal ();
428463 }
@@ -440,7 +475,7 @@ static inline void _Py_DECREF_MORTAL_SPECIALIZED(const char *filename, int linen
440475
441476static inline void Py_DECREF_MORTAL (PyObject * op )
442477{
443- assert (! _Py_IsStaticImmortal ( op ) );
478+ _Py_ASSERT_NOT_STATIC_IMMORTAL ( op );
444479 _Py_DECREF_STAT_INC ();
445480 if (-- op -> ob_refcnt == 0 ) {
446481 _Py_Dealloc (op );
@@ -450,7 +485,7 @@ static inline void Py_DECREF_MORTAL(PyObject *op)
450485
451486static inline void Py_DECREF_MORTAL_SPECIALIZED (PyObject * op , destructor destruct )
452487{
453- assert (! _Py_IsStaticImmortal ( op ) );
488+ _Py_ASSERT_NOT_STATIC_IMMORTAL ( op );
454489 _Py_DECREF_STAT_INC ();
455490 if (-- op -> ob_refcnt == 0 ) {
456491 _PyReftracerTrack (op , PyRefTracer_DESTROY );
@@ -475,7 +510,11 @@ _PyObject_Init(PyObject *op, PyTypeObject *typeobj)
475510{
476511 assert (op != NULL );
477512 Py_SET_TYPE (op , typeobj );
478- assert (_PyType_HasFeature (typeobj , Py_TPFLAGS_HEAPTYPE ) || _Py_IsImmortal (typeobj ));
513+ #ifndef NDEBUG
514+ if (!_PyType_HasFeature (typeobj , Py_TPFLAGS_HEAPTYPE )) {
515+ _Py_ASSERT_IMMORTAL (typeobj );
516+ }
517+ #endif
479518 _Py_INCREF_TYPE (typeobj );
480519 _Py_NewReference (op );
481520}
@@ -987,7 +1026,7 @@ extern int _PyObject_SetManagedDict(PyObject *obj, PyObject *new_dict);
9871026#ifndef Py_GIL_DISABLED
9881027static inline Py_ALWAYS_INLINE void _Py_INCREF_MORTAL (PyObject * op )
9891028{
990- assert (! _Py_IsStaticImmortal ( op ) );
1029+ _Py_ASSERT_MORTAL ( op );
9911030 op -> ob_refcnt ++ ;
9921031 _Py_INCREF_STAT_INC ();
9931032#if defined(Py_REF_DEBUG ) && !defined(Py_LIMITED_API )
0 commit comments