bpo-46417: Py_Finalize() clears static exceptioins (GH-30805) · python/cpython@621a45c

@@ -3536,13 +3536,36 @@ _PyExc_InitTypes(PyInterpreterState *interp)

35363536

}

35373537353835383539+

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

}

3660368536613686

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