Skip to content

Commit b4bbfcb

Browse files
Hide the 1-based indexing behind the helper functions.
1 parent d313dbd commit b4bbfcb

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Objects/typeobject.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,16 @@ static inline size_t
8080
static_builtin_index_get(PyTypeObject *self)
8181
{
8282
assert(self->tp_static_builtin_index > 0);
83-
return self->tp_static_builtin_index;
83+
/* We store a 1-based index so 0 can mean "not initialized". */
84+
return self->tp_static_builtin_index - 1;
8485
}
8586

8687
static inline void
8788
static_builtin_index_set(PyTypeObject *self, size_t index)
8889
{
8990
assert(index < _Py_MAX_STATIC_BUILTIN_TYPES);
90-
self->tp_static_builtin_index = index;
91+
/* We store a 1-based index so 0 can mean "not initialized". */
92+
self->tp_static_builtin_index = index + 1;
9193
}
9294

9395
static inline void
@@ -104,7 +106,7 @@ _PyStaticType_GetState(PyTypeObject *self)
104106
return NULL;
105107
}
106108
PyInterpreterState *interp = _PyInterpreterState_GET();
107-
return &(interp->types.builtins[static_builtin_index_get(self) - 1]);
109+
return &(interp->types.builtins[static_builtin_index_get(self)]);
108110
}
109111

110112
// Also see _PyStaticType_InitBuiltin() and _PyStaticType_Dealloc().
@@ -6754,11 +6756,8 @@ _PyStaticType_InitBuiltin(PyTypeObject *self)
67546756

67556757
/* For static types we store some state in an array on each interpreter. */
67566758
PyInterpreterState *interp = _PyInterpreterState_GET();
6757-
interp->types.num_builtins_initialized++;
6758-
assert(interp->types.num_builtins_initialized < _Py_MAX_STATIC_BUILTIN_TYPES);
6759-
6760-
/* We use 1-based indexing so 0 can mean "not initialized". */
67616759
static_builtin_index_set(self, interp->types.num_builtins_initialized);
6760+
interp->types.num_builtins_initialized++;
67626761

67636762
/* Now we initialize the type's per-interpreter state. */
67646763
static_builtin_type_state *state = _PyStaticType_GetState(self);

0 commit comments

Comments
 (0)