mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
[3.14] gh-135443: Sometimes Fall Back to __main__.__dict__ For Globals (gh-135593)
For several builtin functions, we now fall back to __main__.__dict__ for the globals
when there is no current frame and _PyInterpreterState_IsRunningMain() returns
true. This allows those functions to be run with Interpreter.call().
The affected builtins:
* exec()
* eval()
* globals()
* locals()
* vars()
* dir()
We take a similar approach with "stateless" functions, which don't use any
global variables.
(cherry picked from commit a450a0ddec, AKA gh-135491)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
This commit is contained in:
parent
7aeddeaf93
commit
2b1c0a76dc
7 changed files with 394 additions and 68 deletions
|
|
@ -3958,25 +3958,28 @@ PyImport_Import(PyObject *module_name)
|
|||
}
|
||||
|
||||
/* Get the builtins from current globals */
|
||||
globals = PyEval_GetGlobals();
|
||||
globals = PyEval_GetGlobals(); // borrowed
|
||||
if (globals != NULL) {
|
||||
Py_INCREF(globals);
|
||||
// XXX Use _PyEval_EnsureBuiltins()?
|
||||
builtins = PyObject_GetItem(globals, &_Py_ID(__builtins__));
|
||||
if (builtins == NULL) {
|
||||
// XXX Fall back to interp->builtins or sys.modules['builtins']?
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
else if (_PyErr_Occurred(tstate)) {
|
||||
goto err;
|
||||
}
|
||||
else {
|
||||
/* No globals -- use standard builtins, and fake globals */
|
||||
builtins = PyImport_ImportModuleLevel("builtins",
|
||||
NULL, NULL, NULL, 0);
|
||||
if (builtins == NULL) {
|
||||
globals = PyDict_New();
|
||||
if (globals == NULL) {
|
||||
goto err;
|
||||
}
|
||||
globals = Py_BuildValue("{OO}", &_Py_ID(__builtins__), builtins);
|
||||
if (globals == NULL)
|
||||
if (_PyEval_EnsureBuiltinsWithModule(tstate, globals, &builtins) < 0) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the __import__ function from the builtins */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue