]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-1635741: Clean sysdict and builtins of interpreter at exit (GH-21605)
authorHai Shi <shihai1992@gmail.com>
Wed, 12 Aug 2020 21:23:30 +0000 (05:23 +0800)
committerGitHub <noreply@github.com>
Wed, 12 Aug 2020 21:23:30 +0000 (23:23 +0200)
Python/pystate.c

index d0cbf5cb8364bf475125130bbf647aef46058e0e..f6d1956e9dce9aef35b47d45a4fea3fdd330700c 100644 (file)
@@ -294,8 +294,6 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
     Py_CLEAR(interp->codec_error_registry);
     Py_CLEAR(interp->modules);
     Py_CLEAR(interp->modules_by_index);
-    Py_CLEAR(interp->sysdict);
-    Py_CLEAR(interp->builtins);
     Py_CLEAR(interp->builtins_copy);
     Py_CLEAR(interp->importlib);
     Py_CLEAR(interp->import_func);
@@ -308,6 +306,14 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
     if (_PyRuntimeState_GetFinalizing(runtime) == NULL) {
         _PyWarnings_Fini(interp);
     }
+    /* We don't clear sysdict and builtins until the end of this function.
+       Because clearing other attributes can execute arbitrary Python code
+       which requires sysdict and builtins. */
+    PyDict_Clear(interp->sysdict);
+    PyDict_Clear(interp->builtins);
+    Py_CLEAR(interp->sysdict);
+    Py_CLEAR(interp->builtins);
+
     // XXX Once we have one allocator per interpreter (i.e.
     // per-interpreter GC) we must ensure that all of the interpreter's
     // objects have been cleaned up at the point.