[Python-checkins] python/dist/src/Python bltinmodule.c, 2.309,
2.310 ceval.c, 2.407, 2.408
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Fri Jul 2 02:41:10 EDT 2004
Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18086/Python
Modified Files:
bltinmodule.c ceval.c
Log Message:
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.
Index: bltinmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.309
retrieving revision 2.310
diff -C2 -d -r2.309 -r2.310
*** bltinmodule.c 29 Mar 2004 11:50:55 -0000 2.309
--- bltinmodule.c 2 Jul 2004 06:41:07 -0000 2.310
***************
*** 456,464 ****
PyCompilerFlags cf;
! if (!PyArg_ParseTuple(args, "O|O!O!:eval",
! &cmd,
! &PyDict_Type, &globals,
! &PyDict_Type, &locals))
return NULL;
if (globals == Py_None) {
globals = PyEval_GetGlobals();
--- 456,470 ----
PyCompilerFlags cf;
! 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();
***************
*** 518,523 ****
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.");
--- 524,530 ----
The source may be a string representing a Python expression\n\
or a code object as returned by compile().\n\
! 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");
Index: ceval.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/ceval.c,v
retrieving revision 2.407
retrieving revision 2.408
diff -C2 -d -r2.407 -r2.408
*** ceval.c 27 Jun 2004 15:43:12 -0000 2.407
--- ceval.c 2 Jul 2004 06:41:07 -0000 2.408
***************
*** 1644,1648 ****
v = POP();
if ((x = f->f_locals) != NULL) {
! err = PyDict_SetItem(x, w, v);
Py_DECREF(v);
if (err == 0) continue;
--- 1644,1651 ----
v = POP();
if ((x = f->f_locals) != NULL) {
! if (PyDict_CheckExact(v))
! err = PyDict_SetItem(x, w, v);
! else
! err = PyObject_SetItem(x, w, v);
Py_DECREF(v);
if (err == 0) continue;
***************
*** 1657,1661 ****
w = GETITEM(names, oparg);
if ((x = f->f_locals) != NULL) {
! if ((err = PyDict_DelItem(x, w)) != 0)
format_exc_check_arg(PyExc_NameError,
NAME_ERROR_MSG ,w);
--- 1660,1664 ----
w = GETITEM(names, oparg);
if ((x = f->f_locals) != NULL) {
! if ((err = PyObject_DelItem(x, w)) != 0)
format_exc_check_arg(PyExc_NameError,
NAME_ERROR_MSG ,w);
***************
*** 1734,1738 ****
case LOAD_NAME:
w = GETITEM(names, oparg);
! if ((x = f->f_locals) == NULL) {
PyErr_Format(PyExc_SystemError,
"no locals when loading %s",
--- 1737,1741 ----
case LOAD_NAME:
w = GETITEM(names, oparg);
! if ((v = f->f_locals) == NULL) {
PyErr_Format(PyExc_SystemError,
"no locals when loading %s",
***************
*** 1740,1744 ****
break;
}
! x = PyDict_GetItem(x, w);
if (x == NULL) {
x = PyDict_GetItem(f->f_globals, w);
--- 1743,1756 ----
break;
}
! if (PyDict_CheckExact(v))
! x = PyDict_GetItem(v, w);
! else {
! x = PyObject_GetItem(v, w);
! if (x == NULL && PyErr_Occurred()) {
! if (!PyErr_ExceptionMatches(PyExc_KeyError))
! break;
! PyErr_Clear();
! }
! }
if (x == NULL) {
x = PyDict_GetItem(f->f_globals, w);
More information about the Python-checkins
mailing list