gh-132775: Always Set __builtins__ In _PyFunction_FromXIData() (gh-134758)

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.
This commit is contained in:
Eric Snow 2025-05-27 09:42:24 -06:00 committed by GitHub
parent c3c88064f5
commit 9b5e80000e
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); Py_DECREF(code);
return NULL; 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); PyObject *func = PyFunction_New(code, globals);
Py_DECREF(code); Py_DECREF(code);
Py_DECREF(globals); Py_DECREF(globals);