Skip to content

Commit 474f650

Browse files
Use -1 in flags only for immortal objects on ft build
1 parent c9ae051 commit 474f650

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

Include/internal/pycore_stackref.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,19 @@ PyStackRef_IsNullOrInt(_PyStackRef ref);
376376

377377
static const _PyStackRef PyStackRef_ERROR = { .bits = Py_TAG_INVALID };
378378

379+
static inline PyObject *
380+
_PyStackRef_AsTuple(_PyStackRef ref, PyObject *op)
381+
{
382+
int flags = ref.bits & Py_TAG_BITS;
383+
#ifdef Py_GIL_DISABLED
384+
if (_Py_IsImmortal(op)) {
385+
// Do not check flags on immortal objects in the free threading build.
386+
flags = -1;
387+
}
388+
#endif
389+
return Py_BuildValue("(ni)", Py_REFCNT(op), flags);
390+
}
391+
379392
/* Wrap a pointer in a stack ref.
380393
* The resulting stack reference is not safe and should only be used
381394
* in the interpreter to pass values from one uop to another.
@@ -476,13 +489,6 @@ static const _PyStackRef PyStackRef_NULL = { .bits = Py_TAG_DEFERRED};
476489

477490
#define PyStackRef_IsNullOrInt(stackref) (PyStackRef_IsNull(stackref) || PyStackRef_IsTaggedInt(stackref))
478491

479-
static inline PyObject *
480-
_PyStackRef_AsTuple(_PyStackRef ref, PyObject *op)
481-
{
482-
// Do not check StackRef flags in the free threading build.
483-
return Py_BuildValue("(ni)", Py_REFCNT(op), -1);
484-
}
485-
486492
static inline PyObject *
487493
PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
488494
{
@@ -667,13 +673,6 @@ static const _PyStackRef PyStackRef_NULL = { .bits = PyStackRef_NULL_BITS };
667673
#define PyStackRef_IsFalse(REF) ((REF).bits == (((uintptr_t)&_Py_FalseStruct) | Py_TAG_REFCNT))
668674
#define PyStackRef_IsNone(REF) ((REF).bits == (((uintptr_t)&_Py_NoneStruct) | Py_TAG_REFCNT))
669675

670-
static inline PyObject *
671-
_PyStackRef_AsTuple(_PyStackRef ref, PyObject *op)
672-
{
673-
int flags = ref.bits & Py_TAG_BITS;
674-
return Py_BuildValue("(ni)", Py_REFCNT(op), flags);
675-
}
676-
677676
#ifdef Py_DEBUG
678677

679678
static inline void PyStackRef_CheckValid(_PyStackRef ref) {

Lib/test/test_stackrefs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ def run_with_refcount_check(self, func, obj):
4949
for func in funcs_with_incref:
5050
refcount, flags = run_with_refcount_check(self, func, obj)
5151
self.assertLess(refcount, self.BIG_REFCOUNT)
52-
self.assertIn(flags, (0, -1))
52+
self.assertEqual(flags, 0)
5353
results.add((refcount, flags))
5454
self.assertEqual(len(results), 1)
5555

5656
results = set()
5757
for func in funcs_with_borrow:
5858
refcount, flags = run_with_refcount_check(self, func, obj)
5959
self.assertLess(refcount, self.BIG_REFCOUNT)
60-
self.assertIn(flags, (1, -1))
60+
self.assertEqual(flags, 1)
6161
results.add((refcount, flags))
6262
self.assertEqual(len(results), 1)
6363

0 commit comments

Comments
 (0)