summaryrefslogtreecommitdiffstats
path: root/Python/bltinmodule.c
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-07-02 06:41:07 (GMT)
committerRaymond Hettinger <python@rcn.com>2004-07-02 06:41:07 (GMT)
commit214b1c3aaea3e83302df9ea37a37b3c7548b92b1 (patch)
tree4ddeda01308aec48c7edda3188a4aad84dc9435b /Python/bltinmodule.c
parent78bace7442bb89aa005950a1e37f71d4704d4cb6 (diff)
downloadcpython-214b1c3aaea3e83302df9ea37a37b3c7548b92b1.zip
cpython-214b1c3aaea3e83302df9ea37a37b3c7548b92b1.tar.gz
cpython-214b1c3aaea3e83302df9ea37a37b3c7548b92b1.tar.bz2
SF Bug #215126: Over restricted type checking on eval() function
The builtin eval() function now accepts any mapping for the locals argument. Time sensitive steps guarded by PyDict_CheckExact() to keep from slowing down the normal case. My timings so no measurable impact.
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r--Python/bltinmodule.c19
1 files changed, 13 insertions, 6 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index b3c8b09..f4f8b7a 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -455,11 +455,17 @@ builtin_eval(PyObject *self, PyObject *args)
char *str;
PyCompilerFlags cf;
- if (!PyArg_ParseTuple(args, "O|O!O!:eval",
- &cmd,
- &PyDict_Type, &globals,
- &PyDict_Type, &locals))
+ if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
return NULL;
+ if (locals != Py_None && !PyMapping_Check(locals)) {
+ PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
+ return NULL;
+ }
+ if (globals != Py_None && !PyDict_Check(globals)) {
+ PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
+ "globals must be a real dict; try eval(expr, {}, mapping)"
+ : "globals must be a dict");
+ }
if (globals == Py_None) {
globals = PyEval_GetGlobals();
if (locals == Py_None)
@@ -517,8 +523,9 @@ PyDoc_STRVAR(eval_doc,
Evaluate the source in the context of globals and locals.\n\
The source may be a string representing a Python expression\n\
or a code object as returned by compile().\n\
-The globals and locals are dictionaries, defaulting to the current\n\
-globals and locals. If only globals is given, locals defaults to it.");
+The globals must be a dictionary and locals can be any mappping,\n\
+defaulting to the current globals and locals.\n\
+If only globals is given, locals defaults to it.\n");
static PyObject *