Skip to content

Commit 8ecc0c4

Browse files
authored
bpo-1635741: Clean sysdict and builtins of interpreter at exit (GH-21605)
1 parent 423e77d commit 8ecc0c4

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Python/pystate.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,6 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
294294
Py_CLEAR(interp->codec_error_registry);
295295
Py_CLEAR(interp->modules);
296296
Py_CLEAR(interp->modules_by_index);
297-
Py_CLEAR(interp->sysdict);
298-
Py_CLEAR(interp->builtins);
299297
Py_CLEAR(interp->builtins_copy);
300298
Py_CLEAR(interp->importlib);
301299
Py_CLEAR(interp->import_func);
@@ -308,6 +306,14 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
308306
if (_PyRuntimeState_GetFinalizing(runtime) == NULL) {
309307
_PyWarnings_Fini(interp);
310308
}
309+
/* We don't clear sysdict and builtins until the end of this function.
310+
Because clearing other attributes can execute arbitrary Python code
311+
which requires sysdict and builtins. */
312+
PyDict_Clear(interp->sysdict);
313+
PyDict_Clear(interp->builtins);
314+
Py_CLEAR(interp->sysdict);
315+
Py_CLEAR(interp->builtins);
316+
311317
// XXX Once we have one allocator per interpreter (i.e.
312318
// per-interpreter GC) we must ensure that all of the interpreter's
313319
// objects have been cleaned up at the point.

0 commit comments

Comments
 (0)