Skip to content

Commit 6feef7a

Browse files
Review addressed
1 parent f2bd853 commit 6feef7a

3 files changed

Lines changed: 28 additions & 21 deletions

File tree

Include/internal/pycore_stackref.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ static const _PyStackRef PyStackRef_ERROR = { .index = (1 << Py_TAGGED_SHIFT) };
7474

7575
#define INITIAL_STACKREF_INDEX (5 << Py_TAGGED_SHIFT)
7676

77+
static inline PyObject *
78+
_PyStackRef_AsTuple(_PyStackRef ref, PyObject *op)
79+
{
80+
int flags = ref.index & Py_TAG_BITS;
81+
return Py_BuildValue("(Ii)", Py_REFCNT(op), flags);
82+
}
83+
7784
static inline _PyStackRef
7885
PyStackRef_Wrap(void *ptr)
7986
{
@@ -447,6 +454,12 @@ static const _PyStackRef PyStackRef_NULL = { .bits = Py_TAG_DEFERRED};
447454

448455
#define PyStackRef_IsNullOrInt(stackref) (PyStackRef_IsNull(stackref) || PyStackRef_IsTaggedInt(stackref))
449456

457+
static inline PyObject *
458+
_PyStackRef_AsTuple(_PyStackRef ref, PyObject *op)
459+
{
460+
return Py_BuildValue("(Ii)", Py_REFCNT(op), 0);
461+
}
462+
450463
static inline PyObject *
451464
PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
452465
{
@@ -633,6 +646,13 @@ static const _PyStackRef PyStackRef_NULL = { .bits = PyStackRef_NULL_BITS };
633646

634647
#ifdef Py_DEBUG
635648

649+
static inline PyObject *
650+
_PyStackRef_AsTuple(_PyStackRef ref, PyObject *op)
651+
{
652+
int flags = ref.bits & Py_TAG_BITS;
653+
return Py_BuildValue("(Ii)", Py_REFCNT(op), flags);
654+
}
655+
636656
static inline void PyStackRef_CheckValid(_PyStackRef ref) {
637657
assert(ref.bits != 0);
638658
int tag = ref.bits & Py_TAG_BITS;

Lib/test/test_stackrefs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def run_with_refcount_check(self, func, obj):
3838
results.add(res)
3939
self.assertEqual(len(results), 1)
4040

41-
mortal_objs = (5000, 3+2j, range(10))
41+
mortal_objs = (5000, 3+2j, range(10), object())
4242

4343
for obj in mortal_objs:
4444
results = set()

Modules/_testinternalcapi.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,24 +2419,11 @@ set_vectorcall_nop(PyObject *self, PyObject *func)
24192419
Py_RETURN_NONE;
24202420
}
24212421

2422-
static PyObject *
2423-
stackref_to_tuple(_PyStackRef ref, PyObject *op)
2424-
{
2425-
#if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG)
2426-
int flags = ref.index & Py_TAG_BITS;
2427-
#elif defined(Py_GIL_DISABLED)
2428-
int flags = 0;
2429-
#else
2430-
int flags = ref.bits & Py_TAG_BITS;
2431-
#endif
2432-
return Py_BuildValue("(Ii)", Py_REFCNT(op), flags);
2433-
}
2434-
24352422
static PyObject *
24362423
stackref_from_object_new(PyObject *self, PyObject *op)
24372424
{
24382425
_PyStackRef ref = PyStackRef_FromPyObjectNew(op);
2439-
PyObject *obj = stackref_to_tuple(ref, op);
2426+
PyObject *obj = _PyStackRef_AsTuple(ref, op);
24402427
PyStackRef_CLOSE(ref);
24412428
return obj;
24422429
}
@@ -2446,7 +2433,7 @@ stackref_from_object_steal_with_incref(PyObject *self, PyObject *op)
24462433
{
24472434
Py_INCREF(op);
24482435
_PyStackRef ref = PyStackRef_FromPyObjectSteal(op);
2449-
PyObject *obj = stackref_to_tuple(ref, op);
2436+
PyObject *obj = _PyStackRef_AsTuple(ref, op);
24502437
PyObject *op2 = PyStackRef_AsPyObjectSteal(ref);
24512438
Py_DECREF(op2);
24522439
return obj;
@@ -2457,7 +2444,7 @@ stackref_make_heap_safe(PyObject *self, PyObject *op)
24572444
{
24582445
_PyStackRef ref = PyStackRef_FromPyObjectNew(op);
24592446
_PyStackRef ref2 = PyStackRef_MakeHeapSafe(ref);
2460-
PyObject *obj = stackref_to_tuple(ref2, op);
2447+
PyObject *obj = _PyStackRef_AsTuple(ref2, op);
24612448
PyStackRef_CLOSE(ref2);
24622449
return obj;
24632450
}
@@ -2469,7 +2456,7 @@ stackref_make_heap_safe_with_borrow(PyObject *self, PyObject *op)
24692456
_PyStackRef ref2 = PyStackRef_Borrow(ref);
24702457
_PyStackRef ref3 = PyStackRef_MakeHeapSafe(ref2);
24712458
PyStackRef_CLOSE(ref);
2472-
PyObject *obj = stackref_to_tuple(ref3, op);
2459+
PyObject *obj = _PyStackRef_AsTuple(ref3, op);
24732460
PyStackRef_CLOSE(ref3);
24742461
return obj;
24752462
}
@@ -2480,7 +2467,7 @@ stackref_strong_reference(PyObject *self, PyObject *op)
24802467
_PyStackRef ref = PyStackRef_FromPyObjectBorrow(op);
24812468
PyObject *op2 = PyStackRef_AsPyObjectSteal(ref);
24822469
_PyStackRef ref2 = PyStackRef_FromPyObjectSteal(op2);
2483-
PyObject *obj = stackref_to_tuple(ref2, op);
2470+
PyObject *obj = _PyStackRef_AsTuple(ref2, op);
24842471
PyStackRef_CLOSE(ref2);
24852472
return obj;
24862473
}
@@ -2489,7 +2476,7 @@ static PyObject *
24892476
stackref_from_object_borrow(PyObject *self, PyObject *op)
24902477
{
24912478
_PyStackRef ref = PyStackRef_FromPyObjectBorrow(op);
2492-
PyObject *obj = stackref_to_tuple(ref, op);
2479+
PyObject *obj = _PyStackRef_AsTuple(ref, op);
24932480
PyStackRef_CLOSE(ref);
24942481
return obj;
24952482
}
@@ -2500,7 +2487,7 @@ stackref_dup_borrowed_with_close(PyObject *self, PyObject *op)
25002487
_PyStackRef ref = PyStackRef_FromPyObjectBorrow(op);
25012488
_PyStackRef ref2 = PyStackRef_DUP(ref);
25022489
PyStackRef_XCLOSE(ref);
2503-
PyObject *obj = stackref_to_tuple(ref2, op);
2490+
PyObject *obj = _PyStackRef_AsTuple(ref2, op);
25042491
PyStackRef_XCLOSE(ref2);
25052492
return obj;
25062493
}

0 commit comments

Comments
 (0)