bpo-38858: Fix Py_Finalize() when called from a subinterpreter (GH-17… · python/cpython@b93f31f (original) (raw)

`@@ -558,9 +558,10 @@ pycore_create_interpreter(_PyRuntimeState *runtime,

`

558

558

``

559

559

``

560

560

`static PyStatus

`

561

``

`-

pycore_init_types(PyThreadState *tstate, int is_main_interp)

`

``

561

`+

pycore_init_types(PyThreadState *tstate)

`

562

562

`{

`

563

563

`PyStatus status;

`

``

564

`+

int is_main_interp = _Py_IsMainInterpreter(tstate);

`

564

565

``

565

566

`status = _PyGC_Init(tstate);

`

566

567

`if (_PyStatus_EXCEPTION(status)) {

`

`@@ -576,7 +577,9 @@ pycore_init_types(PyThreadState *tstate, int is_main_interp)

`

576

577

`if (!_PyLong_Init()) {

`

577

578

`return _PyStatus_ERR("can't init longs");

`

578

579

` }

`

``

580

`+

}

`

579

581

``

``

582

`+

if (is_main_interp) {

`

580

583

`status = _PyUnicode_Init();

`

581

584

`if (_PyStatus_EXCEPTION(status)) {

`

582

585

`return status;

`

`@@ -696,7 +699,7 @@ pyinit_config(_PyRuntimeState *runtime,

`

696

699

`config = &tstate->interp->config;

`

697

700

`*tstate_p = tstate;

`

698

701

``

699

``

`-

status = pycore_init_types(tstate, 1);

`

``

702

`+

status = pycore_init_types(tstate);

`

700

703

`if (_PyStatus_EXCEPTION(status)) {

`

701

704

`return status;

`

702

705

` }

`

`@@ -1179,6 +1182,9 @@ finalize_interp_types(PyThreadState *tstate, int is_main_interp)

`

1179

1182

`_PySet_Fini();

`

1180

1183

`_PyBytes_Fini();

`

1181

1184

`_PyLong_Fini();

`

``

1185

`+

}

`

``

1186

+

``

1187

`+

if (is_main_interp) {

`

1182

1188

`_PyFloat_Fini();

`

1183

1189

`_PyDict_Fini();

`

1184

1190

`_PySlice_Fini();

`

`@@ -1200,8 +1206,10 @@ finalize_interp_types(PyThreadState *tstate, int is_main_interp)

`

1200

1206

``

1201

1207

``

1202

1208

`static void

`

1203

``

`-

finalize_interp_clear(PyThreadState *tstate, int is_main_interp)

`

``

1209

`+

finalize_interp_clear(PyThreadState *tstate)

`

1204

1210

`{

`

``

1211

`+

int is_main_interp = _Py_IsMainInterpreter(tstate);

`

``

1212

+

1205

1213

`/* Clear interpreter state and all thread states */

`

1206

1214

`PyInterpreterState_Clear(tstate->interp);

`

1207

1215

``

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

`

1224

1232

``

1225

1233

``

1226

1234

`static void

`

1227

``

`-

finalize_interp_delete(PyThreadState *tstate, int is_main_interp)

`

``

1235

`+

finalize_interp_delete(PyThreadState *tstate)

`

1228

1236

`{

`

1229

``

`-

if (is_main_interp) {

`

``

1237

`+

if (_Py_IsMainInterpreter(tstate)) {

`

1230

1238

`/* Cleanup auto-thread-state */

`

1231

1239

`_PyGILState_Fini(tstate);

`

1232

1240

` }

`

`@@ -1388,9 +1396,8 @@ Py_FinalizeEx(void)

`

1388

1396

` }

`

1389

1397

`#endif /* Py_TRACE_REFS */

`

1390

1398

``

1391

``

`-

finalize_interp_clear(tstate, 1);

`

1392

``

-

1393

``

`-

finalize_interp_delete(tstate, 1);

`

``

1399

`+

finalize_interp_clear(tstate);

`

``

1400

`+

finalize_interp_delete(tstate);

`

1394

1401

``

1395

1402

`#ifdef Py_TRACE_REFS

`

1396

1403

`/* Display addresses (& refcnts) of all objects still alive.

`

`@@ -1482,7 +1489,7 @@ new_interpreter(PyThreadState **tstate_p)

`

1482

1489

` }

`

1483

1490

`config = &interp->config;

`

1484

1491

``

1485

``

`-

status = pycore_init_types(tstate, 0);

`

``

1492

`+

status = pycore_init_types(tstate);

`

1486

1493

``

1487

1494

`/* XXX The following is lax in error checking */

`

1488

1495

`PyObject *modules = PyDict_New();

`

`@@ -1634,8 +1641,8 @@ Py_EndInterpreter(PyThreadState *tstate)

`

1634

1641

` }

`

1635

1642

``

1636

1643

`_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);

`

1639

1646

`}

`

1640

1647

``

1641

1648

`/* Add the main module */

`