Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5de3f04
Add PyInterpreterState.types.
ericsnowcurrently Jul 8, 2022
24aea28
Add _PyStaticType_InitBuiltin().
ericsnowcurrently Jul 9, 2022
b1cc7d2
Call PyType_Ready() in _PyStaticType_InitBuiltin().
ericsnowcurrently Jul 15, 2022
c63bccb
Add _PyStructSequence_InitBuiltin().
ericsnowcurrently Jul 15, 2022
0aacc7a
Hard-code _Py_NUM_STATIC_BUILTIN_TYPES.
ericsnowcurrently Jul 18, 2022
a006e21
Store per-interpreter state for static builtin types.
ericsnowcurrently Jul 19, 2022
e3ed4fd
Export _PyStaticType_GetState().
ericsnowcurrently Jul 19, 2022
0c695b5
Move _PyStaticType_GetState() down next to _PyStaticType_InitBuiltin().
ericsnowcurrently Jul 19, 2022
2df59c1
make it compile
kumaraditya303 Jul 19, 2022
5d76cfa
Fix a typo.
ericsnowcurrently Jul 19, 2022
d05be58
fix test_sys
kumaraditya303 Jul 19, 2022
1a3d6f1
Merge branch 'main' into init-static-builtin
ericsnowcurrently Jul 20, 2022
160738b
Ignore non-builtin static types in _PyStaticType_Dealloc().
ericsnowcurrently Jul 20, 2022
7655114
Remember if a type is a static builtin.
ericsnowcurrently Jul 20, 2022
6820fca
Factor out get_static_builtin_index() and set_static_builtin_index().
ericsnowcurrently Jul 20, 2022
98f9eaf
Revert to skipping on subclasses for now.
ericsnowcurrently Jul 20, 2022
a3fd8e3
Throw away the leaking subclasses.
ericsnowcurrently Jul 20, 2022
cb3895c
Reset state->type later on.
ericsnowcurrently Jul 21, 2022
8d7edd7
Add some asserts.
ericsnowcurrently Jul 21, 2022
f057fcf
Decrement num_builtins_initialized.
ericsnowcurrently Jul 21, 2022
bb8264d
Factor out subclass_from_ref().
ericsnowcurrently Jul 22, 2022
d680e8e
Handle MemoryError in remove_subclass().
ericsnowcurrently Jul 22, 2022
41850e8
Call _PyTypes_FiniTypes() before _PyTypes_Fini().
ericsnowcurrently Jul 22, 2022
f1bbbd2
Verify the per-interpreter type state has been cleared.
ericsnowcurrently Jul 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Ignore non-builtin static types in _PyStaticType_Dealloc().
  • Loading branch information
ericsnowcurrently committed Jul 20, 2022
commit 160738bc39b4befba78caf455a5232d1feb8b810
34 changes: 29 additions & 5 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4212,13 +4212,37 @@ type_dealloc_common(PyTypeObject *type)
}


static int
has_lingering_subclasses(PyTypeObject *type)
{
if (type->tp_subclasses == NULL) {
return 0;
}
// We can ignore non-builtin static types.
Py_ssize_t i = 0;
PyObject *ref; // borrowed ref
while (PyDict_Next((PyObject *)type->tp_subclasses, &i, NULL, &ref)) {
PyTypeObject *sub = (PyTypeObject *)((struct _PyWeakReference *)ref)->wr_object;
if (sub->tp_flags & Py_TPFLAGS_HEAPTYPE) {
return 1;
}
// All static builtin subtypes should have been finalized already.
assert(sub->tp_static_builtin_index == 0);
}
/* At this point, all the subclasses are non-builtin static types.
We can ignore them since they only come from extension modules
and such extension modules are already unsafe if the runtime
is re-used after finalization (or in multiple interpreters). */
return 0;
}

void
_PyStaticType_Dealloc(PyTypeObject *type)
{
// If a type still has subtypes, it cannot be deallocated.
// A subtype can inherit attributes and methods of its parent type,
// and a type must no longer be used once it's deallocated.
if (type->tp_subclasses != NULL) {
// If a static type still has non-static subtypes, it cannot be
// deallocated. A subtype can inherit attributes and methods of its
// parent type, and a type must no longer be used once it's deallocated.
if (has_lingering_subclasses(type)) {
return;
}

Expand All @@ -4233,7 +4257,7 @@ _PyStaticType_Dealloc(PyTypeObject *type)
Py_CLEAR(type->tp_bases);
Py_CLEAR(type->tp_mro);
Py_CLEAR(type->tp_cache);
// type->tp_subclasses is NULL
Py_CLEAR(type->tp_subclasses);

// PyObject_ClearWeakRefs() raises an exception if Py_REFCNT() != 0
if (Py_REFCNT(type) == 0) {
Expand Down