[Python-checkins] bpo-43541: Fix PyEval_EvalCodeEx() regression (GH-24918)

vstinner webhook-mailer at python.org
Thu Mar 18 09:51:43 EDT 2021


https://github.com/python/cpython/commit/fc980e0be19776ee05dfc5380eb5d6a8092935cb
commit: fc980e0be19776ee05dfc5380eb5d6a8092935cb
branch: master
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-03-18T14:51:24+01:00
summary:

bpo-43541: Fix PyEval_EvalCodeEx() regression (GH-24918)

* Remove an assertion which required CO_NEWLOCALS and CO_OPTIMIZED
  code flags. It is ok to call this function on a code with these
  flags set.
* Fix reference counting on builtins: remove Py_DECREF().
  Fix regression introduced in the
  commit 46496f9d12582bf11f4911ad0f23315d6f277907.

Add also a comment to document that _PyEval_BuiltinsFromGlobals()
returns a borrowed reference.

files:
A Misc/NEWS.d/next/C API/2021-03-18-12-44-33.bpo-43541.ICigzd.rst
M Objects/frameobject.c
M Objects/funcobject.c
M Python/ceval.c

diff --git a/Misc/NEWS.d/next/C API/2021-03-18-12-44-33.bpo-43541.ICigzd.rst b/Misc/NEWS.d/next/C API/2021-03-18-12-44-33.bpo-43541.ICigzd.rst
new file mode 100644
index 0000000000000..0f5d9384942be
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2021-03-18-12-44-33.bpo-43541.ICigzd.rst	
@@ -0,0 +1,2 @@
+Fix a ``PyEval_EvalCodeEx()`` regression: fix reference counting on
+builtins. Patch by Victor Stinner.
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index 056d42a0d9ebb..a1413d79cc248 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -847,7 +847,7 @@ PyFrameObject*
 PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
             PyObject *globals, PyObject *locals)
 {
-    PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals);
+    PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
     if (builtins == NULL) {
         return NULL;
     }
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 36df88a28100d..45135a8c98a70 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -50,7 +50,7 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
     }
     Py_XINCREF(module);
 
-    builtins = _PyEval_BuiltinsFromGlobals(tstate, globals);
+    builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
     if (builtins == NULL) {
         goto error;
     }
diff --git a/Python/ceval.c b/Python/ceval.c
index 7418b15176d2a..3a37017c000d4 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1127,7 +1127,7 @@ PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
     if (locals == NULL) {
         locals = globals;
     }
-    PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals);
+    PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
     if (builtins == NULL) {
         return NULL;
     }
@@ -5140,12 +5140,11 @@ PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
     if (defaults == NULL) {
         return NULL;
     }
-    PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals);
+    PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
     if (builtins == NULL) {
         Py_DECREF(defaults);
         return NULL;
     }
-    assert ((((PyCodeObject *)_co)->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) == 0);
     if (locals == NULL) {
         locals = globals;
     }
@@ -5208,7 +5207,6 @@ PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
     }
 fail:
     Py_DECREF(defaults);
-    Py_DECREF(builtins);
     return res;
 }
 



More information about the Python-checkins mailing list