mirror of
https://github.com/python/cpython.git
synced 2026-05-06 12:49:07 -04:00
gh-145055: Accept frozendict for globals in exec() and eval() (#145072)
This commit is contained in:
@@ -594,7 +594,7 @@ are always available. They are listed here in alphabetical order.
|
||||
|
||||
:param globals:
|
||||
The global namespace (default: ``None``).
|
||||
:type globals: :class:`dict` | ``None``
|
||||
:type globals: :class:`dict` | :class:`frozendict` | ``None``
|
||||
|
||||
:param locals:
|
||||
The local namespace (default: ``None``).
|
||||
@@ -660,6 +660,10 @@ are always available. They are listed here in alphabetical order.
|
||||
The semantics of the default *locals* namespace have been adjusted as
|
||||
described for the :func:`locals` builtin.
|
||||
|
||||
.. versionchanged:: next
|
||||
|
||||
*globals* can now be a :class:`frozendict`.
|
||||
|
||||
.. index:: pair: built-in function; exec
|
||||
|
||||
.. function:: exec(source, /, globals=None, locals=None, *, closure=None)
|
||||
@@ -737,6 +741,10 @@ are always available. They are listed here in alphabetical order.
|
||||
The semantics of the default *locals* namespace have been adjusted as
|
||||
described for the :func:`locals` builtin.
|
||||
|
||||
.. versionchanged:: next
|
||||
|
||||
*globals* can now be a :class:`frozendict`.
|
||||
|
||||
|
||||
.. function:: filter(function, iterable, /)
|
||||
|
||||
|
||||
@@ -372,7 +372,7 @@ _PyDict_UniqueId(PyDictObject *mp)
|
||||
static inline void
|
||||
_Py_INCREF_DICT(PyObject *op)
|
||||
{
|
||||
assert(PyDict_Check(op));
|
||||
assert(PyAnyDict_Check(op));
|
||||
Py_ssize_t id = _PyDict_UniqueId((PyDictObject *)op);
|
||||
_Py_THREAD_INCREF_OBJECT(op, id);
|
||||
}
|
||||
@@ -380,7 +380,7 @@ _Py_INCREF_DICT(PyObject *op)
|
||||
static inline void
|
||||
_Py_DECREF_DICT(PyObject *op)
|
||||
{
|
||||
assert(PyDict_Check(op));
|
||||
assert(PyAnyDict_Check(op));
|
||||
Py_ssize_t id = _PyDict_UniqueId((PyDictObject *)op);
|
||||
_Py_THREAD_DECREF_OBJECT(op, id);
|
||||
}
|
||||
|
||||
@@ -784,6 +784,16 @@ class BuiltinTest(ComplexesAreIdenticalMixin, unittest.TestCase):
|
||||
raise ValueError
|
||||
self.assertRaises(ValueError, eval, "foo", {}, X())
|
||||
|
||||
def test_eval_frozendict(self):
|
||||
ns = frozendict(x=1, data=[], __builtins__=__builtins__)
|
||||
eval("data.append(x)", ns, ns)
|
||||
self.assertEqual(ns['data'], [1])
|
||||
|
||||
ns = frozendict()
|
||||
errmsg = "cannot assign __builtins__ to frozendict globals"
|
||||
with self.assertRaisesRegex(TypeError, errmsg):
|
||||
eval("", ns, ns)
|
||||
|
||||
def test_eval_kwargs(self):
|
||||
data = {"A_GLOBAL_VALUE": 456}
|
||||
self.assertEqual(eval("globals()['A_GLOBAL_VALUE']", globals=data), 456)
|
||||
@@ -882,6 +892,21 @@ class BuiltinTest(ComplexesAreIdenticalMixin, unittest.TestCase):
|
||||
del l['__builtins__']
|
||||
self.assertEqual((g, l), ({'a': 1}, {'b': 2}))
|
||||
|
||||
def test_exec_frozendict(self):
|
||||
ns = frozendict(x=1, data=[], __builtins__=__builtins__)
|
||||
exec("data.append(x)", ns, ns)
|
||||
self.assertEqual(ns['data'], [1])
|
||||
|
||||
ns = frozendict(__builtins__=__builtins__)
|
||||
errmsg = "'frozendict' object does not support item assignment"
|
||||
with self.assertRaisesRegex(TypeError, errmsg):
|
||||
exec("x = 1", ns, ns)
|
||||
|
||||
ns = frozendict()
|
||||
errmsg = "cannot assign __builtins__ to frozendict globals"
|
||||
with self.assertRaisesRegex(TypeError, errmsg):
|
||||
exec("", ns, ns)
|
||||
|
||||
def test_exec_kwargs(self):
|
||||
g = {}
|
||||
exec('global z\nz = 1', globals=g)
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
:func:`exec` and :func:`eval` now accept :class:`frozendict` for *globals*.
|
||||
Patch by Victor Stinner.
|
||||
@@ -1830,7 +1830,7 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co,
|
||||
assert(attrnames != NULL);
|
||||
assert(PySet_Check(attrnames));
|
||||
assert(PySet_GET_SIZE(attrnames) == 0 || counts != NULL);
|
||||
assert(globalsns == NULL || PyDict_Check(globalsns));
|
||||
assert(globalsns == NULL || PyAnyDict_Check(globalsns));
|
||||
assert(builtinsns == NULL || PyDict_Check(builtinsns));
|
||||
assert(counts == NULL || counts->total == 0);
|
||||
struct co_unbound_counts unbound = {0};
|
||||
|
||||
@@ -2687,7 +2687,7 @@ _PyDict_LoadGlobalStackRef(PyDictObject *globals, PyDictObject *builtins, PyObje
|
||||
PyObject *
|
||||
_PyDict_LoadBuiltinsFromGlobals(PyObject *globals)
|
||||
{
|
||||
if (!PyDict_Check(globals)) {
|
||||
if (!PyAnyDict_Check(globals)) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ PyObject *
|
||||
PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
|
||||
{
|
||||
assert(globals != NULL);
|
||||
assert(PyDict_Check(globals));
|
||||
assert(PyAnyDict_Check(globals));
|
||||
_Py_INCREF_DICT(globals);
|
||||
|
||||
PyCodeObject *code_obj = (PyCodeObject *)code;
|
||||
|
||||
+5
-4
@@ -1045,7 +1045,7 @@ setup_context(Py_ssize_t stack_level,
|
||||
|
||||
/* Setup registry. */
|
||||
assert(globals != NULL);
|
||||
assert(PyDict_Check(globals));
|
||||
assert(PyAnyDict_Check(globals));
|
||||
int rc = PyDict_GetItemRef(globals, &_Py_ID(__warningregistry__),
|
||||
registry);
|
||||
if (rc < 0) {
|
||||
@@ -1269,10 +1269,11 @@ warnings_warn_explicit_impl(PyObject *module, PyObject *message,
|
||||
}
|
||||
|
||||
if (module_globals && module_globals != Py_None) {
|
||||
if (!PyDict_Check(module_globals)) {
|
||||
if (!PyAnyDict_Check(module_globals)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"module_globals must be a dict, not '%.200s'",
|
||||
Py_TYPE(module_globals)->tp_name);
|
||||
"module_globals must be a dict or a frozendict, "
|
||||
"not %T",
|
||||
module_globals);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -1040,10 +1040,11 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
|
||||
PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
|
||||
return NULL;
|
||||
}
|
||||
if (globals != Py_None && !PyDict_Check(globals)) {
|
||||
if (globals != Py_None && !PyAnyDict_Check(globals)) {
|
||||
PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
|
||||
"globals must be a real dict; try eval(expr, {}, mapping)"
|
||||
: "globals must be a dict");
|
||||
"globals must be a real dict or a frozendict; "
|
||||
"try eval(expr, {}, mapping)"
|
||||
: "globals must be a dict or a frozendict");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1197,9 +1198,10 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
|
||||
locals = Py_NewRef(globals);
|
||||
}
|
||||
|
||||
if (!PyDict_Check(globals)) {
|
||||
PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
|
||||
Py_TYPE(globals)->tp_name);
|
||||
if (!PyAnyDict_Check(globals)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"exec() globals must be a dict or a frozendict, not %T",
|
||||
globals);
|
||||
goto error;
|
||||
}
|
||||
if (!PyMapping_Check(locals)) {
|
||||
|
||||
+6
-2
@@ -2718,7 +2718,7 @@ static PyObject *
|
||||
get_globals_builtins(PyObject *globals)
|
||||
{
|
||||
PyObject *builtins = NULL;
|
||||
if (PyDict_Check(globals)) {
|
||||
if (PyAnyDict_Check(globals)) {
|
||||
if (PyDict_GetItemRef(globals, &_Py_ID(__builtins__), &builtins) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -2743,6 +2743,10 @@ set_globals_builtins(PyObject *globals, PyObject *builtins)
|
||||
}
|
||||
else {
|
||||
if (PyObject_SetItem(globals, &_Py_ID(__builtins__), builtins) < 0) {
|
||||
if (PyFrozenDict_Check(globals)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"cannot assign __builtins__ to frozendict globals");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -3584,7 +3588,7 @@ _PyEval_GetANext(PyObject *aiter)
|
||||
void
|
||||
_PyEval_LoadGlobalStackRef(PyObject *globals, PyObject *builtins, PyObject *name, _PyStackRef *writeto)
|
||||
{
|
||||
if (PyDict_CheckExact(globals) && PyDict_CheckExact(builtins)) {
|
||||
if (PyAnyDict_CheckExact(globals) && PyAnyDict_CheckExact(builtins)) {
|
||||
_PyDict_LoadGlobalStackRef((PyDictObject *)globals,
|
||||
(PyDictObject *)builtins,
|
||||
name, writeto);
|
||||
|
||||
+3
-2
@@ -3728,8 +3728,9 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
|
||||
_PyErr_SetString(tstate, PyExc_KeyError, "'__name__' not in globals");
|
||||
goto error;
|
||||
}
|
||||
if (!PyDict_Check(globals)) {
|
||||
_PyErr_SetString(tstate, PyExc_TypeError, "globals must be a dict");
|
||||
if (!PyAnyDict_Check(globals)) {
|
||||
_PyErr_SetString(tstate, PyExc_TypeError,
|
||||
"globals must be a dict or a frozendict");
|
||||
goto error;
|
||||
}
|
||||
if (PyDict_GetItemRef(globals, &_Py_ID(__package__), &package) < 0) {
|
||||
|
||||
+3
-2
@@ -1348,8 +1348,9 @@ static PyObject *
|
||||
run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, PyObject *locals)
|
||||
{
|
||||
/* Set globals['__builtins__'] if it doesn't exist */
|
||||
if (!globals || !PyDict_Check(globals)) {
|
||||
PyErr_SetString(PyExc_SystemError, "globals must be a real dict");
|
||||
if (!globals || !PyAnyDict_Check(globals)) {
|
||||
PyErr_SetString(PyExc_SystemError,
|
||||
"globals must be a real dict or a real frozendict");
|
||||
return NULL;
|
||||
}
|
||||
int has_builtins = PyDict_ContainsString(globals, "__builtins__");
|
||||
|
||||
Reference in New Issue
Block a user