@@ -370,6 +370,7 @@ PyStackRef_AsStrongReference(_PyStackRef stackref)
370370 } \
371371 } while (0)
372372
373+
373374#else // Py_GIL_DISABLED
374375
375376// With GIL
@@ -418,6 +419,12 @@ static inline void PyStackRef_CheckValid(_PyStackRef ref) {
418419
419420#endif
420421
422+ #ifdef _WIN32
423+ #define PyStackRef_IsUncountedMortal (REF ) (((REF).bits & Py_TAG_BITS) == 0)
424+ #define PyStackRef_IsCountedMortal (REF ) (((REF).bits & Py_TAG_BITS) == Py_TAG_REFCNT)
425+ #define PyStackRef_IsMortal (REF ) (((REF).bits & Py_TAG_BITS) != Py_TAG_IMMORTAL)
426+ #define PyStackRef_AsPyObjectBorrow BITS_TO_PTR_MASKED
427+ #else
421428/* Does this ref not have an embedded refcount and refer to a mortal object? */
422429static inline int
423430PyStackRef_IsUncountedMortal (_PyStackRef ref )
@@ -444,6 +451,7 @@ PyStackRef_AsPyObjectBorrow(_PyStackRef ref)
444451{
445452 return BITS_TO_PTR_MASKED (ref );
446453}
454+ #endif
447455
448456static inline PyObject *
449457PyStackRef_AsPyObjectSteal (_PyStackRef ref )
@@ -509,6 +517,10 @@ PyStackRef_FromPyObjectImmortal(PyObject *obj)
509517 return (_PyStackRef ){ .bits = (uintptr_t )obj | Py_TAG_IMMORTAL };
510518}
511519
520+ #ifdef _WIN32
521+ #define PyStackRef_DUP (REF ) \
522+ (PyStackRef_IsUncountedMortal(REF) ? Py_INCREF_MORTAL(BITS_TO_PTR(ref)) : (REF))
523+ #else
512524static inline _PyStackRef
513525PyStackRef_DUP (_PyStackRef ref )
514526{
@@ -518,6 +530,7 @@ PyStackRef_DUP(_PyStackRef ref)
518530 }
519531 return ref ;
520532}
533+ #endif
521534
522535static inline bool
523536PyStackRef_IsHeapSafe (_PyStackRef ref )
@@ -538,6 +551,12 @@ PyStackRef_MakeHeapSafe(_PyStackRef ref)
538551 return ref ;
539552}
540553
554+ #ifdef _WIN32
555+ #define PyStackRef_DUP (REF ) \
556+ do { \
557+ if (PyStackRef_IsUncountedMortal(REF)) Py_DECREF_MORTAL(BITS_TO_PTR(ref));
558+ } while (0 )
559+ #else
541560static inline void
542561PyStackRef_CLOSE (_PyStackRef ref )
543562{
@@ -546,6 +565,7 @@ PyStackRef_CLOSE(_PyStackRef ref)
546565 Py_DECREF_MORTAL (BITS_TO_PTR (ref ));
547566 }
548567}
568+ #endif
549569
550570static inline void
551571PyStackRef_CLOSE_SPECIALIZED (_PyStackRef ref , destructor destruct )
0 commit comments