Skip to content

bpo-46417: Clear Unicode static types at exit #30806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern void _PyUnicode_InitState(PyInterpreterState *);
extern PyStatus _PyUnicode_InitGlobalObjects(PyInterpreterState *);
extern PyStatus _PyUnicode_InitTypes(PyInterpreterState *);
extern void _PyUnicode_Fini(PyInterpreterState *);
extern void _PyUnicode_FiniTypes(PyInterpreterState *);


/* other API */
Expand Down
6 changes: 0 additions & 6 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3545,12 +3545,6 @@ _PyExc_FiniTypes(PyInterpreterState *interp)

for (Py_ssize_t i=Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {
PyTypeObject *exc = static_exceptions[i].exc;

// Cannot delete a type if it still has subclasses
if (exc->tp_subclasses != NULL) {
continue;
}

_PyStaticType_Dealloc(exc);
}
}
Expand Down
4 changes: 0 additions & 4 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1994,10 +1994,6 @@ _PyTypes_FiniTypes(PyInterpreterState *interp)
// their base classes.
for (Py_ssize_t i=Py_ARRAY_LENGTH(static_types)-1; i>=0; i--) {
PyTypeObject *type = static_types[i];
// Cannot delete a type if it still has subclasses
if (type->tp_subclasses != NULL) {
continue;
}
_PyStaticType_Dealloc(type);
}
}
Expand Down
6 changes: 4 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4079,10 +4079,12 @@ type_dealloc_common(PyTypeObject *type)
void
_PyStaticType_Dealloc(PyTypeObject *type)
{
// _PyStaticType_Dealloc() must not be called if a type has subtypes.
// 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.
assert(type->tp_subclasses == NULL);
if (type->tp_subclasses != NULL) {
return;
}

type_dealloc_common(type);

Expand Down
29 changes: 19 additions & 10 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -15567,23 +15567,19 @@ _PyUnicode_InitTypes(PyInterpreterState *interp)
return _PyStatus_OK();
}

if (PyType_Ready(&PyUnicode_Type) < 0) {
return _PyStatus_ERR("Can't initialize unicode type");
}
if (PyType_Ready(&PyUnicodeIter_Type) < 0) {
return _PyStatus_ERR("Can't initialize unicode iterator type");
}

if (PyType_Ready(&EncodingMapType) < 0) {
return _PyStatus_ERR("Can't initialize encoding map type");
goto error;
}
if (PyType_Ready(&PyFieldNameIter_Type) < 0) {
return _PyStatus_ERR("Can't initialize field name iterator type");
goto error;
}
if (PyType_Ready(&PyFormatterIter_Type) < 0) {
return _PyStatus_ERR("Can't initialize formatter iter type");
goto error;
}
return _PyStatus_OK();

error:
return _PyStatus_ERR("Can't initialize unicode types");
}


Expand Down Expand Up @@ -16111,6 +16107,19 @@ unicode_is_finalizing(void)
#endif


void
_PyUnicode_FiniTypes(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return;
}

_PyStaticType_Dealloc(&EncodingMapType);
_PyStaticType_Dealloc(&PyFieldNameIter_Type);
_PyStaticType_Dealloc(&PyFormatterIter_Type);
}


void
_PyUnicode_Fini(PyInterpreterState *interp)
{
Expand Down
1 change: 1 addition & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,7 @@ flush_std_files(void)
static void
finalize_interp_types(PyInterpreterState *interp)
{
_PyUnicode_FiniTypes(interp);
_PySys_Fini(interp);
_PyExc_Fini(interp);
_PyFrame_Fini(interp);
Expand Down