@@ -195,25 +195,32 @@ PyStackRef_AsStrongReference(_PyStackRef stackref)
195195
196196// With GIL
197197
198- #define Py_TAG_BITS 1
198+ #define Py_TAG_BITS 3
199199#define Py_TAG_REFCNT 1
200+ #define Py_TAG_IMMORTAL 3
200201#define BITS_TO_PTR (REF ) ((PyObject *)((REF).bits))
201202#define BITS_TO_PTR_MASKED (REF ) ((PyObject *)(((REF).bits) & (~Py_TAG_BITS)))
202203
203- #define PyStackRef_NULL_BITS Py_TAG_REFCNT
204+ #define PyStackRef_NULL_BITS Py_TAG_IMMORTAL
204205static const _PyStackRef PyStackRef_NULL = { .bits = PyStackRef_NULL_BITS };
205206
206207#define PyStackRef_IsNull (ref ) ((ref).bits == PyStackRef_NULL_BITS)
207- #define PyStackRef_True ((_PyStackRef){.bits = ((uintptr_t)&_Py_TrueStruct) | Py_TAG_REFCNT })
208- #define PyStackRef_False ((_PyStackRef){.bits = ((uintptr_t)&_Py_FalseStruct) | Py_TAG_REFCNT })
209- #define PyStackRef_None ((_PyStackRef){.bits = ((uintptr_t)&_Py_NoneStruct) | Py_TAG_REFCNT })
208+ #define PyStackRef_True ((_PyStackRef){.bits = ((uintptr_t)&_Py_TrueStruct) | Py_TAG_IMMORTAL })
209+ #define PyStackRef_False ((_PyStackRef){.bits = ((uintptr_t)&_Py_FalseStruct) | Py_TAG_IMMORTAL })
210+ #define PyStackRef_None ((_PyStackRef){.bits = ((uintptr_t)&_Py_NoneStruct) | Py_TAG_IMMORTAL })
210211
211212static inline int
212213PyStackRef_HasCount (_PyStackRef ref )
213214{
214215 return ref .bits & Py_TAG_REFCNT ;
215216}
216217
218+ static inline int
219+ PyStackRef_HasCountAndMortal (_PyStackRef ref )
220+ {
221+ return (ref .bits & Py_TAG_BITS ) == Py_TAG_REFCNT ;
222+ }
223+
217224static inline PyObject *
218225PyStackRef_AsPyObjectBorrow (_PyStackRef ref )
219226{
@@ -238,7 +245,7 @@ static inline _PyStackRef
238245PyStackRef_FromPyObjectSteal (PyObject * obj )
239246{
240247 assert (obj != NULL );
241- unsigned int tag = _Py_IsDeferrable (obj ) ? Py_TAG_REFCNT : 0 ;
248+ unsigned int tag = _Py_IsImmortal (obj ) ? Py_TAG_IMMORTAL : 0 ;
242249 _PyStackRef ref = ((_PyStackRef ){.bits = ((uintptr_t )(obj )) | tag });
243250 return ref ;
244251}
@@ -289,12 +296,10 @@ PyStackRef_IsHeapSafe(_PyStackRef ref)
289296static inline _PyStackRef
290297PyStackRef_HeapSafe (_PyStackRef ref )
291298{
292- if (PyStackRef_HasCount (ref )) {
299+ if (PyStackRef_HasCountAndMortal (ref )) {
293300 PyObject * obj = BITS_TO_PTR_MASKED (ref );
294- if (obj != NULL && !_Py_IsImmortal (obj )) {
295- Py_INCREF_MORTAL (obj );
296- ref .bits = (uintptr_t )obj ;
297- }
301+ Py_INCREF_MORTAL (obj );
302+ ref .bits = (uintptr_t )obj ;
298303 }
299304 return ref ;
300305}
0 commit comments