From 137967a7ef32a902d6b4c9488b39340e9afa5df0 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Fri, 24 Jul 2020 20:38:27 +0800 Subject: [PATCH 1/5] Clean sysdict and builtins of interpreter. --- Python/pystate.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Python/pystate.c b/Python/pystate.c index d0cbf5cb8364bf..bd44559a5c7f5f 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -286,6 +286,9 @@ PyInterpreterState_Clear(PyInterpreterState *interp) } HEAD_UNLOCK(runtime); + PyDict_Clear(interp->sysdict); + PyDict_Clear(interp->builtins); + Py_CLEAR(interp->audit_hooks); PyConfig_Clear(&interp->config); From 2910570accce70fcdfd3af398456e2f53b066c2d Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Wed, 5 Aug 2020 01:59:31 +0800 Subject: [PATCH 2/5] update code according victor's comment --- Python/pystate.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Python/pystate.c b/Python/pystate.c index bd44559a5c7f5f..7447346ca6364c 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -286,8 +286,8 @@ PyInterpreterState_Clear(PyInterpreterState *interp) } HEAD_UNLOCK(runtime); - PyDict_Clear(interp->sysdict); - PyDict_Clear(interp->builtins); + PyObject *sysdict = interp->sysdict; + PyObject *builtins = interp->builtins; Py_CLEAR(interp->audit_hooks); @@ -311,6 +311,13 @@ 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 reuqires sysdict and builtins. */ + PyDict_Clear(sysdict); + PyDict_Clear(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. From ba98fc1c35a9ce6119fb1f13b025d5cc0a476604 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Wed, 5 Aug 2020 13:27:33 +0800 Subject: [PATCH 3/5] Fix typo --- Python/pystate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/pystate.c b/Python/pystate.c index 7447346ca6364c..baea8a8cd4e90e 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -314,7 +314,7 @@ PyInterpreterState_Clear(PyInterpreterState *interp) /* We don't clear sysdict and builtins until the end of this function. Because clearing other attributes can execute arbitrary Python code - which reuqires sysdict and builtins. */ + which requires sysdict and builtins. */ PyDict_Clear(sysdict); PyDict_Clear(builtins); From d08c18facc1d23f7b30e8505a85c261ab5fe0d6d Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Wed, 5 Aug 2020 23:12:07 +0800 Subject: [PATCH 4/5] update code order --- Python/pystate.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Python/pystate.c b/Python/pystate.c index baea8a8cd4e90e..2d399ba8ff6501 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -286,9 +286,6 @@ PyInterpreterState_Clear(PyInterpreterState *interp) } HEAD_UNLOCK(runtime); - PyObject *sysdict = interp->sysdict; - PyObject *builtins = interp->builtins; - Py_CLEAR(interp->audit_hooks); PyConfig_Clear(&interp->config); @@ -297,6 +294,8 @@ PyInterpreterState_Clear(PyInterpreterState *interp) Py_CLEAR(interp->codec_error_registry); Py_CLEAR(interp->modules); Py_CLEAR(interp->modules_by_index); + PyDict_Clear(interp->sysdict); + PyDict_Clear(interp->builtins); Py_CLEAR(interp->sysdict); Py_CLEAR(interp->builtins); Py_CLEAR(interp->builtins_copy); @@ -311,13 +310,6 @@ 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(sysdict); - PyDict_Clear(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. From a9df92379b8c4794a9ae7c0788402369dca7d769 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 6 Aug 2020 19:42:24 +0800 Subject: [PATCH 5/5] update code order --- Python/pystate.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Python/pystate.c b/Python/pystate.c index 2d399ba8ff6501..f6d1956e9dce9a 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -294,10 +294,6 @@ PyInterpreterState_Clear(PyInterpreterState *interp) Py_CLEAR(interp->codec_error_registry); Py_CLEAR(interp->modules); Py_CLEAR(interp->modules_by_index); - PyDict_Clear(interp->sysdict); - PyDict_Clear(interp->builtins); - Py_CLEAR(interp->sysdict); - Py_CLEAR(interp->builtins); Py_CLEAR(interp->builtins_copy); Py_CLEAR(interp->importlib); Py_CLEAR(interp->import_func); @@ -310,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.