Skip to content

Commit dde26e5

Browse files
Add _PyExc_FiniHeapObjects(), to call before _PyInterpreterState_Clear().
1 parent cd66234 commit dde26e5

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

Include/internal/pycore_exceptions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ extern "C" {
1616
extern PyStatus _PyExc_InitState(PyInterpreterState *);
1717
extern PyStatus _PyExc_InitGlobalObjects(PyInterpreterState *);
1818
extern int _PyExc_InitTypes(PyInterpreterState *);
19+
extern void _PyExc_FiniHeapObjects(PyInterpreterState *);
20+
extern void _PyExc_FiniTypes(PyInterpreterState *);
1921
extern void _PyExc_Fini(PyInterpreterState *);
2022

2123

@@ -32,7 +34,6 @@ struct _Py_exc_state {
3234
PyTypeObject *ExceptionSnapshotType;
3335
};
3436

35-
extern void _PyExc_ClearExceptionGroupType(PyInterpreterState *);
3637

3738
/* other API */
3839

Objects/exceptions.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3696,14 +3696,9 @@ _PyExc_InitTypes(PyInterpreterState *interp)
36963696
return 0;
36973697
}
36983698

3699-
3700-
static void
3701-
_exc_snapshot_clear_type(PyInterpreterState *interp);
3702-
3703-
static void
3699+
void
37043700
_PyExc_FiniTypes(PyInterpreterState *interp)
37053701
{
3706-
_exc_snapshot_clear_type(interp);
37073702
for (Py_ssize_t i=Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {
37083703
PyTypeObject *exc = static_exceptions[i].exc;
37093704
_PyStaticType_Dealloc(interp, exc);
@@ -3820,11 +3815,16 @@ _PyBuiltins_AddExceptions(PyObject *bltinmod)
38203815
return 0;
38213816
}
38223817

3818+
3819+
// _PyExc_FiniHeapObjects() must be called before the interpreter
3820+
// state is cleared, since there are heap types to clean up.
3821+
38233822
void
3824-
_PyExc_ClearExceptionGroupType(PyInterpreterState *interp)
3823+
_PyExc_FiniHeapObjects(PyInterpreterState *interp)
38253824
{
3826-
struct _Py_exc_state *state = &interp->exc_state;
3825+
struct _Py_exc_state *state = get_exc_state(interp);
38273826
Py_CLEAR(state->PyExc_ExceptionGroup);
3827+
Py_CLEAR(state->ExceptionSnapshotType);
38283828
}
38293829

38303830
void
@@ -3833,8 +3833,6 @@ _PyExc_Fini(PyInterpreterState *interp)
38333833
struct _Py_exc_state *state = &interp->exc_state;
38343834
free_preallocated_memerrors(state);
38353835
Py_CLEAR(state->errnomap);
3836-
3837-
_PyExc_FiniTypes(interp);
38383836
}
38393837

38403838
int
@@ -3986,13 +3984,6 @@ _exc_snapshot_init_type(PyInterpreterState *interp)
39863984
return 0;
39873985
}
39883986

3989-
static void
3990-
_exc_snapshot_clear_type(PyInterpreterState *interp)
3991-
{
3992-
struct _Py_exc_state *state = get_exc_state(interp);
3993-
Py_CLEAR(state->ExceptionSnapshotType);
3994-
}
3995-
39963987
PyTypeObject *
39973988
_PyExc_GetExceptionSnapshotType(PyInterpreterState *interp)
39983989
{

Python/pylifecycle.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,7 @@ finalize_interp_types(PyInterpreterState *interp)
17421742
{
17431743
_PyUnicode_FiniTypes(interp);
17441744
_PySys_FiniTypes(interp);
1745-
_PyExc_Fini(interp);
1745+
_PyExc_FiniTypes(interp);
17461746
_PyAsyncGen_Fini(interp);
17471747
_PyContext_Fini(interp);
17481748
_PyFloat_FiniType(interp);
@@ -1779,7 +1779,7 @@ finalize_interp_clear(PyThreadState *tstate)
17791779
int is_main_interp = _Py_IsMainInterpreter(tstate->interp);
17801780

17811781
_PyXI_Fini(tstate->interp);
1782-
_PyExc_ClearExceptionGroupType(tstate->interp);
1782+
_PyExc_FiniHeapObjects(tstate->interp);
17831783
_Py_clear_generic_types(tstate->interp);
17841784

17851785
/* Clear interpreter state and all thread states */
@@ -1799,6 +1799,7 @@ finalize_interp_clear(PyThreadState *tstate)
17991799
_PyPerfTrampoline_Fini();
18001800
}
18011801

1802+
_PyExc_Fini(tstate->interp);
18021803
finalize_interp_types(tstate->interp);
18031804
}
18041805

0 commit comments

Comments
 (0)