Skip to content

bpo-46417: Py_Finalize() clears static exceptioins #30805

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 2 commits into from
Jan 22, 2022
Merged
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
27 changes: 26 additions & 1 deletion Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3536,13 +3536,36 @@ _PyExc_InitTypes(PyInterpreterState *interp)
}


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

for (Py_ssize_t i=Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (Py_ssize_t i=Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {
for (Py_ssize_t i = Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well well well. I would make the line more consistent, but I decided to merge my PR since I'm working on a looong serie of changes: see https://bugs.python.org/issue46417

The CPython C code base is far from being consistent and the PEP 7 is quite relaxed in terms of coding style.

PyTypeObject *exc = static_exceptions[i].exc;

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

_PyStaticType_Dealloc(exc);
}
}


PyStatus
_PyExc_InitGlobalObjects(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return _PyStatus_OK();
}

if (preallocate_memerrors() < 0) {
return _PyStatus_NO_MEMORY();
}

return _PyStatus_OK();
}

Expand Down Expand Up @@ -3656,6 +3679,8 @@ _PyExc_Fini(PyInterpreterState *interp)
struct _Py_exc_state *state = &interp->exc_state;
free_preallocated_memerrors(state);
Py_CLEAR(state->errnomap);

_PyExc_FiniTypes(interp);
}

/* Helper to do the equivalent of "raise X from Y" in C, but always using
Expand Down