static inline PyTypeObject * subclass_from_ref(PyObject *ref);
+
+/* helpers for for static builtin types */
+
+static inline int
+static_builtin_index_is_set(PyTypeObject *self)
+{
+ return self->tp_static_builtin_index > 0;
+}
+
+static inline size_t
+static_builtin_index_get(PyTypeObject *self)
+{
+ assert(static_builtin_index_is_set(self));
+ /* We store a 1-based index so 0 can mean "not initialized". */
+ return self->tp_static_builtin_index - 1;
+}
+
+static inline void
+static_builtin_index_set(PyTypeObject *self, size_t index)
+{
+ assert(index < _Py_MAX_STATIC_BUILTIN_TYPES);
+ /* We store a 1-based index so 0 can mean "not initialized". */
+ self->tp_static_builtin_index = index + 1;
+}
+
+static inline void
+static_builtin_index_clear(PyTypeObject *self)
+{
+ self->tp_static_builtin_index = 0;
+}
+
+static inline static_builtin_state *
+static_builtin_state_get(PyInterpreterState *interp, PyTypeObject *self)
+{
+ return &(interp->types.builtins[static_builtin_index_get(self)]);
+}
+
+/* For static types we store some state in an array on each interpreter. */
+static_builtin_state *
+_PyStaticType_GetState(PyTypeObject *self)
+{
+ assert(self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN);
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ return static_builtin_state_get(interp, self);
+}
+
+static void
+static_builtin_state_init(PyTypeObject *self)
+{
+ /* Set the type's per-interpreter state. */
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+
+ /* It should only be called once for each builtin type. */
+ assert(!static_builtin_index_is_set(self));
+
+ static_builtin_index_set(self, interp->types.num_builtins_initialized);
+ interp->types.num_builtins_initialized++;
+
+ static_builtin_state *state = static_builtin_state_get(interp, self);
+ state->type = self;
+}
+
+static void
+static_builtin_state_clear(PyTypeObject *self)
+{
+ /* Reset the type's per-interpreter state.
+ This basically undoes what static_builtin_state_init() did. */
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+
+ static_builtin_state *state = static_builtin_state_get(interp, self);
+ state->type = NULL;
+ static_builtin_index_clear(self);
+
+ assert(interp->types.num_builtins_initialized > 0);
+ interp->types.num_builtins_initialized--;
+}
+
+// Also see _PyStaticType_InitBuiltin() and _PyStaticType_Dealloc().
+
+/* end static builtin helpers */
+
+
/*
* finds the beginning of the docstring's introspection signature.
* if present, returns a pointer pointing to the first '('.
get_type_cache(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
- return &interp->type_cache;
+ return &interp->types.type_cache;
}
void
_PyType_InitCache(PyInterpreterState *interp)
{
- struct type_cache *cache = &interp->type_cache;
+ struct type_cache *cache = &interp->types.type_cache;
for (Py_ssize_t i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
struct type_cache_entry *entry = &cache->hashtable[i];
assert(entry->name == NULL);
static unsigned int
_PyType_ClearCache(PyInterpreterState *interp)
{
- struct type_cache *cache = &interp->type_cache;
+ struct type_cache *cache = &interp->types.type_cache;
#if MCACHE_STATS
size_t total = cache->hits + cache->collisions + cache->misses;
fprintf(stderr, "-- Method cache hits = %zd (%d%%)\n",
void
_PyTypes_Fini(PyInterpreterState *interp)
{
- struct type_cache *cache = &interp->type_cache;
+ struct type_cache *cache = &interp->types.type_cache;
type_cache_clear(cache, NULL);
if (_Py_IsMainInterpreter(interp)) {
clear_slotdefs();
}
+
+ assert(interp->types.num_builtins_initialized == 0);
+ // All the static builtin types should have been finalized already.
+ for (size_t i = 0; i < _Py_MAX_STATIC_BUILTIN_TYPES; i++) {
+ assert(interp->types.builtins[i].type == NULL);
+ }
}
void
_PyStaticType_Dealloc(PyTypeObject *type)
{
+ assert(!(type->tp_flags & Py_TPFLAGS_HEAPTYPE));
+
type_dealloc_common(type);
Py_CLEAR(type->tp_dict);
}
type->tp_flags &= ~Py_TPFLAGS_READY;
+
+ if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
+ static_builtin_state_clear(type);
+ /* We leave _Py_TPFLAGS_STATIC_BUILTIN set on tp_flags. */
+ }
}
{
self->tp_flags = self->tp_flags | _Py_TPFLAGS_STATIC_BUILTIN;
- return PyType_Ready(self);
+ static_builtin_state_init(self);
+
+ int res = PyType_Ready(self);
+ if (res < 0) {
+ static_builtin_state_clear(self);
+ }
+ return res;
}