Skip to content

Commit 2f9627e

Browse files
committed
Use macros on Windows
1 parent 522c0e7 commit 2f9627e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Include/internal/pycore_stackref.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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? */
422429
static inline int
423430
PyStackRef_IsUncountedMortal(_PyStackRef ref)
@@ -444,6 +451,7 @@ PyStackRef_AsPyObjectBorrow(_PyStackRef ref)
444451
{
445452
return BITS_TO_PTR_MASKED(ref);
446453
}
454+
#endif
447455

448456
static inline PyObject *
449457
PyStackRef_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
512524
static inline _PyStackRef
513525
PyStackRef_DUP(_PyStackRef ref)
514526
{
@@ -518,6 +530,7 @@ PyStackRef_DUP(_PyStackRef ref)
518530
}
519531
return ref;
520532
}
533+
#endif
521534

522535
static inline bool
523536
PyStackRef_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
541560
static inline void
542561
PyStackRef_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

550570
static inline void
551571
PyStackRef_CLOSE_SPECIALIZED(_PyStackRef ref, destructor destruct)

0 commit comments

Comments
 (0)