Skip to content

Commit b93f31f

Browse files
authored
bpo-38858: Fix Py_Finalize() when called from a subinterpreter (GH-17297)
Use _Py_IsMainInterpreter() in Py_Initialize() and Py_Finalize() to detect if the current interpreter is the main interpreter or not.
1 parent de148f2 commit b93f31f

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

Python/pylifecycle.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,10 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
558558

559559

560560
static PyStatus
561-
pycore_init_types(PyThreadState *tstate, int is_main_interp)
561+
pycore_init_types(PyThreadState *tstate)
562562
{
563563
PyStatus status;
564+
int is_main_interp = _Py_IsMainInterpreter(tstate);
564565

565566
status = _PyGC_Init(tstate);
566567
if (_PyStatus_EXCEPTION(status)) {
@@ -576,7 +577,9 @@ pycore_init_types(PyThreadState *tstate, int is_main_interp)
576577
if (!_PyLong_Init()) {
577578
return _PyStatus_ERR("can't init longs");
578579
}
580+
}
579581

582+
if (is_main_interp) {
580583
status = _PyUnicode_Init();
581584
if (_PyStatus_EXCEPTION(status)) {
582585
return status;
@@ -696,7 +699,7 @@ pyinit_config(_PyRuntimeState *runtime,
696699
config = &tstate->interp->config;
697700
*tstate_p = tstate;
698701

699-
status = pycore_init_types(tstate, 1);
702+
status = pycore_init_types(tstate);
700703
if (_PyStatus_EXCEPTION(status)) {
701704
return status;
702705
}
@@ -1179,6 +1182,9 @@ finalize_interp_types(PyThreadState *tstate, int is_main_interp)
11791182
_PySet_Fini();
11801183
_PyBytes_Fini();
11811184
_PyLong_Fini();
1185+
}
1186+
1187+
if (is_main_interp) {
11821188
_PyFloat_Fini();
11831189
_PyDict_Fini();
11841190
_PySlice_Fini();
@@ -1200,8 +1206,10 @@ finalize_interp_types(PyThreadState *tstate, int is_main_interp)
12001206

12011207

12021208
static void
1203-
finalize_interp_clear(PyThreadState *tstate, int is_main_interp)
1209+
finalize_interp_clear(PyThreadState *tstate)
12041210
{
1211+
int is_main_interp = _Py_IsMainInterpreter(tstate);
1212+
12051213
/* Clear interpreter state and all thread states */
12061214
PyInterpreterState_Clear(tstate->interp);
12071215

@@ -1224,9 +1232,9 @@ finalize_interp_clear(PyThreadState *tstate, int is_main_interp)
12241232

12251233

12261234
static void
1227-
finalize_interp_delete(PyThreadState *tstate, int is_main_interp)
1235+
finalize_interp_delete(PyThreadState *tstate)
12281236
{
1229-
if (is_main_interp) {
1237+
if (_Py_IsMainInterpreter(tstate)) {
12301238
/* Cleanup auto-thread-state */
12311239
_PyGILState_Fini(tstate);
12321240
}
@@ -1388,9 +1396,8 @@ Py_FinalizeEx(void)
13881396
}
13891397
#endif /* Py_TRACE_REFS */
13901398

1391-
finalize_interp_clear(tstate, 1);
1392-
1393-
finalize_interp_delete(tstate, 1);
1399+
finalize_interp_clear(tstate);
1400+
finalize_interp_delete(tstate);
13941401

13951402
#ifdef Py_TRACE_REFS
13961403
/* Display addresses (& refcnts) of all objects still alive.
@@ -1482,7 +1489,7 @@ new_interpreter(PyThreadState **tstate_p)
14821489
}
14831490
config = &interp->config;
14841491

1485-
status = pycore_init_types(tstate, 0);
1492+
status = pycore_init_types(tstate);
14861493

14871494
/* XXX The following is lax in error checking */
14881495
PyObject *modules = PyDict_New();
@@ -1634,8 +1641,8 @@ Py_EndInterpreter(PyThreadState *tstate)
16341641
}
16351642

16361643
_PyImport_Cleanup(tstate);
1637-
finalize_interp_clear(tstate, 0);
1638-
finalize_interp_delete(tstate, 0);
1644+
finalize_interp_clear(tstate);
1645+
finalize_interp_delete(tstate);
16391646
}
16401647

16411648
/* Add the __main__ module */

0 commit comments

Comments
 (0)