]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-132775: Always Set __builtins__ In _PyFunction_FromXIData() (gh-134794)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 27 May 2025 16:07:44 +0000 (18:07 +0200)
committerGitHub <noreply@github.com>
Tue, 27 May 2025 16:07:44 +0000 (16:07 +0000)
This is a small follow-up to gh-133481.  There's a corner case
in the behavior of PyImport_ImportModuleAttrString(), where
it expects __builtins__ to be set if __globals__ is set.

(cherry picked from commit 9b5e80000, AKA gh-134758)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Python/crossinterp_data_lookup.h

index 88eb41da89ee40dbcf4cba33ed4138e16cd5698e..b16f38b847fc6646c9f700923ced23896956792d 100644 (file)
@@ -701,6 +701,14 @@ _PyFunction_FromXIData(_PyXIData_t *xidata)
         Py_DECREF(code);
         return NULL;
     }
+    PyThreadState *tstate = _PyThreadState_GET();
+    if (PyDict_SetItem(globals, &_Py_ID(__builtins__),
+                       tstate->interp->builtins) < 0)
+    {
+        Py_DECREF(code);
+        Py_DECREF(globals);
+        return NULL;
+    }
     PyObject *func = PyFunction_New(code, globals);
     Py_DECREF(code);
     Py_DECREF(globals);