Skip to content

Commit 5725821

Browse files
committed
Update debug stackref
1 parent a10eadb commit 5725821

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Include/internal/pycore_stackref.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ PyStackRef_IsNull(_PyStackRef ref)
7878
return ref.index == 0;
7979
}
8080

81+
static inline bool
82+
PyStackRef_IsError(_PyStackRef ref)
83+
{
84+
return ref.index == 2;
85+
}
86+
87+
static inline bool
88+
PyStackRef_IsValid(_PyStackRef ref)
89+
{
90+
/* Invalid values are ERROR and NULL */
91+
return !PyStackRef_IsError(ref) && !PyStackRef_IsNull(ref);
92+
}
93+
8194
static inline int
8295
PyStackRef_IsTrue(_PyStackRef ref)
8396
{
@@ -105,6 +118,7 @@ PyStackRef_IsTaggedInt(_PyStackRef ref)
105118
static inline PyObject *
106119
_PyStackRef_AsPyObjectBorrow(_PyStackRef ref, const char *filename, int linenumber)
107120
{
121+
assert(!PyStackRef_IsError(ref));
108122
assert(!PyStackRef_IsTaggedInt(ref));
109123
_Py_stackref_record_borrow(ref, filename, linenumber);
110124
return _Py_stackref_get_object(ref);
@@ -156,6 +170,7 @@ _PyStackRef_CLOSE(_PyStackRef ref, const char *filename, int linenumber)
156170
static inline void
157171
_PyStackRef_XCLOSE(_PyStackRef ref, const char *filename, int linenumber)
158172
{
173+
assert(!PyStackRef_IsError(ref));
159174
if (PyStackRef_IsNull(ref)) {
160175
return;
161176
}
@@ -166,6 +181,7 @@ _PyStackRef_XCLOSE(_PyStackRef ref, const char *filename, int linenumber)
166181
static inline _PyStackRef
167182
_PyStackRef_DUP(_PyStackRef ref, const char *filename, int linenumber)
168183
{
184+
assert(!PyStackRef_IsError(ref));
169185
if (PyStackRef_IsTaggedInt(ref)) {
170186
return ref;
171187
}
@@ -246,6 +262,8 @@ PyStackRef_IsNullOrInt(_PyStackRef ref);
246262
#define Py_TAG_REFCNT 1
247263
#define Py_TAG_BITS 3
248264

265+
static const _PyStackRef PyStackRef_ERROR = { .bits = Py_TAG_INVALID };
266+
249267
static inline bool
250268
PyStackRef_IsError(_PyStackRef ref)
251269
{
@@ -299,7 +317,6 @@ PyStackRef_IncrementTaggedIntNoOverflow(_PyStackRef ref)
299317

300318

301319
static const _PyStackRef PyStackRef_NULL = { .bits = Py_TAG_DEFERRED};
302-
static const _PyStackRef PyStackRef_ERROR = { .bits = Py_TAG_INVALID};
303320

304321
#define PyStackRef_IsNull(stackref) ((stackref).bits == PyStackRef_NULL.bits)
305322
#define PyStackRef_True ((_PyStackRef){.bits = ((uintptr_t)&_Py_TrueStruct) | Py_TAG_DEFERRED })
@@ -481,7 +498,6 @@ PyStackRef_AsStrongReference(_PyStackRef stackref)
481498

482499
#define PyStackRef_NULL_BITS Py_TAG_REFCNT
483500
static const _PyStackRef PyStackRef_NULL = { .bits = PyStackRef_NULL_BITS };
484-
static const _PyStackRef PyStackRef_ERROR = { .bits = Py_TAG_INVALID };
485501

486502
#define PyStackRef_IsNull(ref) ((ref).bits == PyStackRef_NULL_BITS)
487503
#define PyStackRef_True ((_PyStackRef){.bits = ((uintptr_t)&_Py_TrueStruct) | Py_TAG_REFCNT })

Python/stackrefs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ make_table_entry(PyObject *obj, const char *filename, int linenumber)
4040
PyObject *
4141
_Py_stackref_get_object(_PyStackRef ref)
4242
{
43+
assert(!PyStackRef_IsError(ref));
4344
if (ref.index == 0) {
4445
return NULL;
4546
}
@@ -64,6 +65,7 @@ PyStackRef_Is(_PyStackRef a, _PyStackRef b)
6465
PyObject *
6566
_Py_stackref_close(_PyStackRef ref, const char *filename, int linenumber)
6667
{
68+
assert(!PyStackRef_IsError(ref));
6769
PyInterpreterState *interp = PyInterpreterState_Get();
6870
if (ref.index >= interp->next_stackref) {
6971
_Py_FatalErrorFormat(__func__, "Invalid StackRef with ID %" PRIu64 " at %s:%d\n", (void *)ref.index, filename, linenumber);
@@ -128,6 +130,7 @@ _Py_stackref_create(PyObject *obj, const char *filename, int linenumber)
128130
void
129131
_Py_stackref_record_borrow(_PyStackRef ref, const char *filename, int linenumber)
130132
{
133+
assert(!PyStackRef_IsError(ref));
131134
if (ref.index < INITIAL_STACKREF_INDEX) {
132135
return;
133136
}
@@ -152,6 +155,7 @@ _Py_stackref_record_borrow(_PyStackRef ref, const char *filename, int linenumber
152155
void
153156
_Py_stackref_associate(PyInterpreterState *interp, PyObject *obj, _PyStackRef ref)
154157
{
158+
assert(!PyStackRef_IsError(ref));
155159
assert(ref.index < INITIAL_STACKREF_INDEX);
156160
TableEntry *entry = make_table_entry(obj, "builtin-object", 0);
157161
if (entry == NULL) {

0 commit comments

Comments
 (0)