Skip to content

Commit 9a9d20d

Browse files
committed
Address review comments
1 parent d60ef23 commit 9a9d20d

File tree

3 files changed

+15
-44
lines changed

3 files changed

+15
-44
lines changed

Include/internal/pycore_stackref.h

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ PyStackRef_FromPyObjectBorrow(PyObject *obj)
392392
do { \
393393
_PyStackRef _close_tmp = (REF); \
394394
assert(!PyStackRef_IsNull(_close_tmp)); \
395-
if (!PyStackRef_IsDeferred(_close_tmp)) { \
395+
if (!PyStackRef_IsDeferredOrTaggedInt(_close_tmp)) { \
396396
Py_DECREF(PyStackRef_AsPyObjectBorrow(_close_tmp)); \
397397
} \
398398
} while (0)
@@ -704,23 +704,20 @@ PyStackRef_TYPE(_PyStackRef stackref) {
704704

705705
// StackRef type checks
706706

707-
static inline bool
708-
PyStackRef_GenCheck(_PyStackRef stackref)
709-
{
710-
if (PyStackRef_IsTaggedInt(stackref)) {
711-
return false;
707+
#define STACKREF_CHECK_FUNC(T) \
708+
static inline bool \
709+
PyStackRef_ ## T ## Check(_PyStackRef stackref) { \
710+
if (PyStackRef_IsTaggedInt(stackref)) { \
711+
return false; \
712+
} \
713+
return Py ## T ## _Check(PyStackRef_AsPyObjectBorrow(stackref)); \
712714
}
713-
return PyGen_Check(PyStackRef_AsPyObjectBorrow(stackref));
714-
}
715715

716-
static inline bool
717-
PyStackRef_BoolCheck(_PyStackRef stackref)
718-
{
719-
if (PyStackRef_IsTaggedInt(stackref)) {
720-
return false;
721-
}
722-
return PyBool_Check(PyStackRef_AsPyObjectBorrow(stackref));
723-
}
716+
STACKREF_CHECK_FUNC(Gen)
717+
STACKREF_CHECK_FUNC(Bool)
718+
STACKREF_CHECK_FUNC(ExceptionInstance)
719+
STACKREF_CHECK_FUNC(Code)
720+
STACKREF_CHECK_FUNC(Function)
724721

725722
static inline bool
726723
PyStackRef_LongCheck(_PyStackRef stackref)
@@ -731,33 +728,6 @@ PyStackRef_LongCheck(_PyStackRef stackref)
731728
return PyLong_Check(PyStackRef_AsPyObjectBorrow(stackref));
732729
}
733730

734-
static inline bool
735-
PyStackRef_ExceptionInstanceCheck(_PyStackRef stackref)
736-
{
737-
if (PyStackRef_IsTaggedInt(stackref)) {
738-
return false;
739-
}
740-
return PyExceptionInstance_Check(PyStackRef_AsPyObjectBorrow(stackref));
741-
}
742-
743-
static inline bool
744-
PyStackRef_CodeCheck(_PyStackRef stackref)
745-
{
746-
if (PyStackRef_IsTaggedInt(stackref)) {
747-
return false;
748-
}
749-
return PyCode_Check(PyStackRef_AsPyObjectBorrow(stackref));
750-
}
751-
752-
static inline bool
753-
PyStackRef_FunctionCheck(_PyStackRef stackref)
754-
{
755-
if (PyStackRef_IsTaggedInt(stackref)) {
756-
return false;
757-
}
758-
return PyFunction_Check(PyStackRef_AsPyObjectBorrow(stackref));
759-
}
760-
761731
static inline void
762732
_PyThreadState_PushCStackRef(PyThreadState *tstate, _PyCStackRef *ref)
763733
{

Lib/test/test_list.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ def test_list_overwrite_local(self):
370370
iterable doesn't prematurely free the iterable"""
371371

372372
def foo(x):
373+
self.assertEqual(sys.getrefcount(x), 1)
373374
r = 0
374375
for i in x:
375376
r += i

Python/stackrefs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ PyStackRef_IsNullOrInt(_PyStackRef ref)
219219
_PyStackRef
220220
PyStackRef_IncrementTaggedIntNoOverflow(_PyStackRef ref)
221221
{
222-
assert(ref.index != (uintptr_t)-1); // Overflow
222+
assert(ref.index <= INT_MAX - 2); // No overflow
223223
return (_PyStackRef){ .index = ref.index + 2 };
224224
}
225225

0 commit comments

Comments
 (0)