Skip to content

Commit 8d3c3ea

Browse files
committed
Clarify division between pre-defined stackref indexes and others
1 parent b1b1252 commit 8d3c3ea

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

Include/internal/pycore_stackref.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ extern void _Py_stackref_associate(PyInterpreterState *interp, PyObject *obj, _P
6363

6464
static const _PyStackRef PyStackRef_NULL = { .index = 0 };
6565

66+
// Use the first 3 even numbers for None, True and False.
67+
// Odd numbers are reserved for (tagged) integers
6668
#define PyStackRef_None ((_PyStackRef){ .index = 2 } )
6769
#define PyStackRef_False ((_PyStackRef){ .index = 4 })
6870
#define PyStackRef_True ((_PyStackRef){ .index = 6 })
6971

70-
#define LAST_PREDEFINED_STACKREF_INDEX 6
72+
#define INITIAL_STACKREF_INDEX 8
7173

7274
static inline int
7375
PyStackRef_IsNull(_PyStackRef ref)

Python/pystate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ init_interpreter(PyInterpreterState *interp,
673673
interp->dtoa = (struct _dtoa_state)_dtoa_state_INIT(interp);
674674
}
675675
#if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG)
676-
interp->next_stackref = 2;
676+
interp->next_stackref = INITIAL_STACKREF_INDEX;
677677
_Py_hashtable_allocator_t alloc = {
678678
.malloc = malloc,
679679
.free = free,

Python/stackrefs.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ _Py_stackref_close(_PyStackRef ref, const char *filename, int linenumber)
7070

7171
}
7272
PyObject *obj;
73-
if (ref.index <= LAST_PREDEFINED_STACKREF_INDEX) {
73+
if (ref.index < INITIAL_STACKREF_INDEX) {
7474
if (ref.index == 0) {
7575
_Py_FatalErrorFormat(__func__, "Passing NULL to PyStackRef_CLOSE at %s:%d\n", filename, linenumber);
7676
}
@@ -128,7 +128,7 @@ _Py_stackref_create(PyObject *obj, const char *filename, int linenumber)
128128
void
129129
_Py_stackref_record_borrow(_PyStackRef ref, const char *filename, int linenumber)
130130
{
131-
if (ref.index <= LAST_PREDEFINED_STACKREF_INDEX) {
131+
if (ref.index < INITIAL_STACKREF_INDEX) {
132132
return;
133133
}
134134
PyInterpreterState *interp = PyInterpreterState_Get();
@@ -152,8 +152,7 @@ _Py_stackref_record_borrow(_PyStackRef ref, const char *filename, int linenumber
152152
void
153153
_Py_stackref_associate(PyInterpreterState *interp, PyObject *obj, _PyStackRef ref)
154154
{
155-
assert(interp->next_stackref >= ref.index);
156-
interp->next_stackref = ref.index+2;
155+
assert(ref.index < INITIAL_STACKREF_INDEX);
157156
TableEntry *entry = make_table_entry(obj, "builtin-object", 0);
158157
if (entry == NULL) {
159158
Py_FatalError("No memory left for stackref debug table");

0 commit comments

Comments
 (0)