gh-105879: Add support for keyword arguments to eval and exec (#105885)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Raphael Gaschignard 2024-05-03 01:55:29 +10:00 committed by GitHub
parent 72867c962c
commit 2770d5caca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 98 additions and 33 deletions

View File

@ -524,11 +524,11 @@ are always available. They are listed here in alphabetical order.
.. _func-eval: .. _func-eval:
.. function:: eval(expression, globals=None, locals=None) .. function:: eval(source, /, globals=None, locals=None)
:param expression: :param source:
A Python expression. A Python expression.
:type expression: :class:`str` | :ref:`code object <code-objects>` :type source: :class:`str` | :ref:`code object <code-objects>`
:param globals: :param globals:
The global namespace (default: ``None``). The global namespace (default: ``None``).
@ -583,11 +583,15 @@ are always available. They are listed here in alphabetical order.
Raises an :ref:`auditing event <auditing>` ``exec`` with the code object Raises an :ref:`auditing event <auditing>` ``exec`` with the code object
as the argument. Code compilation events may also be raised. as the argument. Code compilation events may also be raised.
.. versionchanged:: 3.13
The *globals* and *locals* arguments can now be passed as keywords.
.. index:: pair: built-in function; exec .. index:: pair: built-in function; exec
.. function:: exec(object, globals=None, locals=None, /, *, closure=None) .. function:: exec(source, /, globals=None, locals=None, *, closure=None)
This function supports dynamic execution of Python code. *object* must be This function supports dynamic execution of Python code. *source* must be
either a string or a code object. If it is a string, the string is parsed as either a string or a code object. If it is a string, the string is parsed as
a suite of Python statements which is then executed (unless a syntax error a suite of Python statements which is then executed (unless a syntax error
occurs). [#]_ If it is a code object, it is simply executed. In all cases, occurs). [#]_ If it is a code object, it is simply executed. In all cases,
@ -640,6 +644,10 @@ are always available. They are listed here in alphabetical order.
.. versionchanged:: 3.11 .. versionchanged:: 3.11
Added the *closure* parameter. Added the *closure* parameter.
.. versionchanged:: 3.13
The *globals* and *locals* arguments can now be passed as keywords.
.. function:: filter(function, iterable) .. function:: filter(function, iterable)

View File

@ -46,6 +46,8 @@ except ImportError:
x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4) HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
# used as proof of globals being used
A_GLOBAL_VALUE = 123
class Squares: class Squares:
@ -684,6 +686,11 @@ class BuiltinTest(unittest.TestCase):
raise ValueError raise ValueError
self.assertRaises(ValueError, eval, "foo", {}, X()) self.assertRaises(ValueError, eval, "foo", {}, X())
def test_eval_kwargs(self):
data = {"A_GLOBAL_VALUE": 456}
self.assertEqual(eval("globals()['A_GLOBAL_VALUE']", globals=data), 456)
self.assertEqual(eval("globals()['A_GLOBAL_VALUE']", locals=data), 123)
def test_general_eval(self): def test_general_eval(self):
# Tests that general mappings can be used for the locals argument # Tests that general mappings can be used for the locals argument
@ -777,6 +784,19 @@ class BuiltinTest(unittest.TestCase):
del l['__builtins__'] del l['__builtins__']
self.assertEqual((g, l), ({'a': 1}, {'b': 2})) self.assertEqual((g, l), ({'a': 1}, {'b': 2}))
def test_exec_kwargs(self):
g = {}
exec('global z\nz = 1', globals=g)
if '__builtins__' in g:
del g['__builtins__']
self.assertEqual(g, {'z': 1})
# if we only set locals, the global assignment will not
# reach this locals dictionary
g = {}
exec('global z\nz = 1', locals=g)
self.assertEqual(g, {})
def test_exec_globals(self): def test_exec_globals(self):
code = compile("print('Hello World!')", "", "exec") code = compile("print('Hello World!')", "", "exec")
# no builtin function # no builtin function

View File

@ -0,0 +1,2 @@
Allow the *globals* and *locals* arguments to :func:`exec`
and :func:`eval` to be passed as keywords.

View File

@ -925,9 +925,9 @@ builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
eval as builtin_eval eval as builtin_eval
source: object source: object
/
globals: object = None globals: object = None
locals: object = None locals: object = None
/
Evaluate the given source in the context of globals and locals. Evaluate the given source in the context of globals and locals.
@ -941,7 +941,7 @@ If only globals is given, locals defaults to it.
static PyObject * static PyObject *
builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals, builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
PyObject *locals) PyObject *locals)
/*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/ /*[clinic end generated code: output=0a0824aa70093116 input=7c7bce5299a89062]*/
{ {
PyObject *result = NULL, *source_copy; PyObject *result = NULL, *source_copy;
const char *str; const char *str;
@ -1024,9 +1024,9 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
exec as builtin_exec exec as builtin_exec
source: object source: object
/
globals: object = None globals: object = None
locals: object = None locals: object = None
/
* *
closure: object(c_default="NULL") = None closure: object(c_default="NULL") = None
@ -1044,7 +1044,7 @@ when source is a code object requiring exactly that many cellvars.
static PyObject * static PyObject *
builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals, builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
PyObject *locals, PyObject *closure) PyObject *locals, PyObject *closure)
/*[clinic end generated code: output=7579eb4e7646743d input=f13a7e2b503d1d9a]*/ /*[clinic end generated code: output=7579eb4e7646743d input=25e989b6d87a3a21]*/
{ {
PyObject *v; PyObject *v;

View File

@ -395,7 +395,7 @@ exit:
} }
PyDoc_STRVAR(builtin_eval__doc__, PyDoc_STRVAR(builtin_eval__doc__,
"eval($module, source, globals=None, locals=None, /)\n" "eval($module, source, /, globals=None, locals=None)\n"
"--\n" "--\n"
"\n" "\n"
"Evaluate the given source in the context of globals and locals.\n" "Evaluate the given source in the context of globals and locals.\n"
@ -407,33 +407,63 @@ PyDoc_STRVAR(builtin_eval__doc__,
"If only globals is given, locals defaults to it."); "If only globals is given, locals defaults to it.");
#define BUILTIN_EVAL_METHODDEF \ #define BUILTIN_EVAL_METHODDEF \
{"eval", _PyCFunction_CAST(builtin_eval), METH_FASTCALL, builtin_eval__doc__}, {"eval", _PyCFunction_CAST(builtin_eval), METH_FASTCALL|METH_KEYWORDS, builtin_eval__doc__},
static PyObject * static PyObject *
builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals, builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
PyObject *locals); PyObject *locals);
static PyObject * static PyObject *
builtin_eval(PyObject *module, PyObject *const *args, Py_ssize_t nargs) builtin_eval(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{ {
PyObject *return_value = NULL; PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
#define NUM_KEYWORDS 2
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(globals), &_Py_ID(locals), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
static const char * const _keywords[] = {"", "globals", "locals", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "eval",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[3];
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
PyObject *source; PyObject *source;
PyObject *globals = Py_None; PyObject *globals = Py_None;
PyObject *locals = Py_None; PyObject *locals = Py_None;
if (!_PyArg_CheckPositional("eval", nargs, 1, 3)) { args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
if (!args) {
goto exit; goto exit;
} }
source = args[0]; source = args[0];
if (nargs < 2) { if (!noptargs) {
goto skip_optional; goto skip_optional_pos;
} }
globals = args[1]; if (args[1]) {
if (nargs < 3) { globals = args[1];
goto skip_optional; if (!--noptargs) {
goto skip_optional_pos;
}
} }
locals = args[2]; locals = args[2];
skip_optional: skip_optional_pos:
return_value = builtin_eval_impl(module, source, globals, locals); return_value = builtin_eval_impl(module, source, globals, locals);
exit: exit:
@ -441,7 +471,7 @@ exit:
} }
PyDoc_STRVAR(builtin_exec__doc__, PyDoc_STRVAR(builtin_exec__doc__,
"exec($module, source, globals=None, locals=None, /, *, closure=None)\n" "exec($module, source, /, globals=None, locals=None, *, closure=None)\n"
"--\n" "--\n"
"\n" "\n"
"Execute the given source in the context of globals and locals.\n" "Execute the given source in the context of globals and locals.\n"
@ -467,14 +497,14 @@ builtin_exec(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject
PyObject *return_value = NULL; PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
#define NUM_KEYWORDS 1 #define NUM_KEYWORDS 3
static struct { static struct {
PyGC_Head _this_is_not_used; PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS]; PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = { } _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(closure), }, .ob_item = { &_Py_ID(globals), &_Py_ID(locals), &_Py_ID(closure), },
}; };
#undef NUM_KEYWORDS #undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base) #define KWTUPLE (&_kwtuple.ob_base.ob_base)
@ -483,7 +513,7 @@ builtin_exec(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject
# define KWTUPLE NULL # define KWTUPLE NULL
#endif // !Py_BUILD_CORE #endif // !Py_BUILD_CORE
static const char * const _keywords[] = {"", "", "", "closure", NULL}; static const char * const _keywords[] = {"", "globals", "locals", "closure", NULL};
static _PyArg_Parser _parser = { static _PyArg_Parser _parser = {
.keywords = _keywords, .keywords = _keywords,
.fname = "exec", .fname = "exec",
@ -502,17 +532,22 @@ builtin_exec(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject
goto exit; goto exit;
} }
source = args[0]; source = args[0];
if (nargs < 2) { if (!noptargs) {
goto skip_optional_posonly; goto skip_optional_pos;
} }
noptargs--; if (args[1]) {
globals = args[1]; globals = args[1];
if (nargs < 3) { if (!--noptargs) {
goto skip_optional_posonly; goto skip_optional_pos;
}
} }
noptargs--; if (args[2]) {
locals = args[2]; locals = args[2];
skip_optional_posonly: if (!--noptargs) {
goto skip_optional_pos;
}
}
skip_optional_pos:
if (!noptargs) { if (!noptargs) {
goto skip_optional_kwonly; goto skip_optional_kwonly;
} }
@ -1193,4 +1228,4 @@ builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
exit: exit:
return return_value; return return_value;
} }
/*[clinic end generated code: output=6d15edfc194b2c08 input=a9049054013a1b77]*/ /*[clinic end generated code: output=435d3f286a863c49 input=a9049054013a1b77]*/