mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-42990: Functions inherit current builtins (GH-24564)
The types.FunctionType constructor now inherits the current builtins if the globals dictionary has no "__builtins__" key, rather than using {"None": None} as builtins: same behavior as eval() and exec() functions. Defining a function with "def function(...): ..." in Python is not affected, globals cannot be overriden with this syntax: it also inherits the current builtins. PyFrame_New(), PyEval_EvalCode(), PyEval_EvalCodeEx(), PyFunction_New() and PyFunction_NewWithQualName() now inherits the current builtins namespace if the globals dictionary has no "__builtins__" key. * Add _PyEval_GetBuiltins() function. * _PyEval_BuiltinsFromGlobals() now uses _PyEval_GetBuiltins() if builtins cannot be found in globals. * Add tstate parameter to _PyEval_BuiltinsFromGlobals().
This commit is contained in:
parent
4233ff3ee4
commit
46496f9d12
7 changed files with 74 additions and 31 deletions
|
@ -4,6 +4,7 @@
|
|||
#include "Python.h"
|
||||
#include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
|
||||
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
|
||||
#include "pycore_pyerrors.h" // _PyErr_Occurred()
|
||||
#include "structmember.h" // PyMemberDef
|
||||
|
||||
PyObject *
|
||||
|
@ -13,6 +14,8 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
|
|||
assert(PyDict_Check(globals));
|
||||
Py_INCREF(globals);
|
||||
|
||||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
|
||||
PyCodeObject *code_obj = (PyCodeObject *)code;
|
||||
Py_INCREF(code_obj);
|
||||
|
||||
|
@ -42,15 +45,16 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
|
|||
_Py_IDENTIFIER(__name__);
|
||||
PyObject *module = _PyDict_GetItemIdWithError(globals, &PyId___name__);
|
||||
PyObject *builtins = NULL;
|
||||
if (module == NULL && PyErr_Occurred()) {
|
||||
if (module == NULL && _PyErr_Occurred(tstate)) {
|
||||
goto error;
|
||||
}
|
||||
Py_XINCREF(module);
|
||||
|
||||
builtins = _PyEval_BuiltinsFromGlobals(globals);
|
||||
builtins = _PyEval_BuiltinsFromGlobals(tstate, globals);
|
||||
if (builtins == NULL) {
|
||||
goto error;
|
||||
}
|
||||
Py_INCREF(builtins);
|
||||
|
||||
PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
|
||||
if (op == NULL) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue