8000 bpo-46417: Py_Finalize() clears static exceptioins (GH-30805) · python/cpython@621a45c · GitHub
[go: up one dir, main page]

Skip to content

Commit 621a45c

Browse files
authored
bpo-46417: Py_Finalize() clears static exceptioins (GH-30805)
The Py_Finalize() function now clears exceptions implemented as static types. Add _PyExc_FiniTypes() function, called by _PyExc_Fini().
1 parent f1bcdea commit 621a45c

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Objects/exceptions.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3536,13 +3536,36 @@ _PyExc_InitTypes(PyInterpreterState *interp)
35363536
}
35373537

35383538

3539+
static void
3540+
_PyExc_FiniTypes(PyInterpreterState *interp)
3541+
{
3542+
if (!_Py_IsMainInterpreter(interp)) {
3543+
return;
3544+
}
3545+
3546+
for (Py_ssize_t i=Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {
3547+
PyTypeObject *exc = static_exceptions[i].exc;
3548+
3549+
// Cannot delete a type if it still has subclasses
3550+
if (exc->tp_subclasses != NULL) {
3551+
continue;
3552+
}
3553+
3554+
_PyStaticType_Dealloc(exc);
3555+
}
3556+
}
3557+
3558+
35393559
PyStatus
35403560
_PyExc_InitGlobalObjects(PyInterpreterState *interp)
35413561
{
3562+
if (!_Py_IsMainInterpreter(interp)) {
3563+
return _PyStatus_OK();
3564+
}
3565+
35423566
if (preallocate_memerrors() < 0) {
35433567
return _PyStatus_NO_MEMORY();
35443568
}
3545-
35463569
return _PyStatus_OK();
35473570
}
35483571

@@ -3656,6 +3679,8 @@ _PyExc_Fini(PyInterpreterState *interp)
36563679
struct _Py_exc_state *state = &interp->exc_state;
36573680
free_preallocated_memerrors(state);
36583681
Py_CLEAR(state->errnomap);
3682+
3683+
_PyExc_FiniTypes(interp);
36593684
}
36603685

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

0 commit comments

Comments
 (0)
0