[3.14] gh-132775: Always Set __builtins__ In _PyFunction_FromXIData() (gh-134794)

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>
This commit is contained in:
Miss Islington (bot) 2025-05-27 18:07:44 +02:00 committed by GitHub
parent a1b6252c88
commit 6493395f4b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View 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);