changeset: 84647:f35f82f47a2f user: Victor Stinner date: Mon Jul 15 19:46:22 2013 +0200 files: Include/frameobject.h Objects/frameobject.c Python/ceval.c Python/sysmodule.c description: Issue #18408: Add a new PyFrame_FastToLocalsWithError() function Same than PyFrame_FastToLocals(), except that errors are reported. diff -r 71a572a516f9 -r f35f82f47a2f Include/frameobject.h --- a/Include/frameobject.h Mon Jul 15 21:16:27 2013 +0200 +++ b/Include/frameobject.h Mon Jul 15 19:46:22 2013 +0200 @@ -75,6 +75,8 @@ PyAPI_FUNC(PyObject **) PyFrame_ExtendSt /* Conversions between "fast locals" and locals in dictionary */ PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int); + +PyAPI_FUNC(int) PyFrame_FastToLocalsWithError(PyFrameObject *f); PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *); PyAPI_FUNC(int) PyFrame_ClearFreeList(void); diff -r 71a572a516f9 -r f35f82f47a2f Objects/frameobject.c --- a/Objects/frameobject.c Mon Jul 15 21:16:27 2013 +0200 +++ b/Objects/frameobject.c Mon Jul 15 19:46:22 2013 +0200 @@ -21,7 +21,8 @@ static PyMemberDef frame_memberlist[] = static PyObject * frame_getlocals(PyFrameObject *f, void *closure) { - PyFrame_FastToLocals(f); + if (PyFrame_FastToLocalsWithError(f) < 0) + return NULL; Py_INCREF(f->f_locals); return f->f_locals; } @@ -748,12 +749,9 @@ PyFrame_BlockPop(PyFrameObject *f) If deref is true, then the values being copied are cell variables and the value is extracted from the cell variable before being put in dict. - - Exceptions raised while modifying the dict are silently ignored, - because there is no good way to report them. */ -static void +static int map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values, int deref) { @@ -771,13 +769,14 @@ map_to_dict(PyObject *map, Py_ssize_t nm } if (value == NULL) { if (PyObject_DelItem(dict, key) != 0) - PyErr_Clear(); + return -1; } else { if (PyObject_SetItem(dict, key, value) != 0) - PyErr_Clear(); + return -1; } } + return 0; } /* Copy values from the "locals" dict into the fast locals. @@ -834,42 +833,50 @@ dict_to_map(PyObject *map, Py_ssize_t nm } } -void -PyFrame_FastToLocals(PyFrameObject *f) +int +PyFrame_FastToLocalsWithError(PyFrameObject *f) { /* Merge fast locals into f->f_locals */ PyObject *locals, *map; PyObject **fast; - PyObject *error_type, *error_value, *error_traceback; PyCodeObject *co; Py_ssize_t j; Py_ssize_t ncells, nfreevars; - if (f == NULL) - return; + int res; + + if (f == NULL) { + PyErr_BadInternalCall(); + return -1; + } locals = f->f_locals; if (locals == NULL) { locals = f->f_locals = PyDict_New(); - if (locals == NULL) { - PyErr_Clear(); /* Can't report it :-( */ - return; - } + if (locals == NULL) + return -1; } co = f->f_code; map = co->co_varnames; - if (!PyTuple_Check(map)) - return; - PyErr_Fetch(&error_type, &error_value, &error_traceback); + if (!PyTuple_Check(map)) { + PyErr_Format(PyExc_SystemError, + "co_varnames must be a tuple, not %s", + Py_TYPE(map)->tp_name); + return -1; + } fast = f->f_localsplus; j = PyTuple_GET_SIZE(map); if (j > co->co_nlocals) j = co->co_nlocals; - if (co->co_nlocals) - map_to_dict(map, j, locals, fast, 0); + if (co->co_nlocals) { + if (map_to_dict(map, j, locals, fast, 0) < 0) + return -1; + } ncells = PyTuple_GET_SIZE(co->co_cellvars); nfreevars = PyTuple_GET_SIZE(co->co_freevars); if (ncells || nfreevars) { - map_to_dict(co->co_cellvars, ncells, - locals, fast + co->co_nlocals, 1); + if (map_to_dict(co->co_cellvars, ncells, + locals, fast + co->co_nlocals, 1)) + return -1; + /* If the namespace is unoptimized, then one of the following cases applies: 1. It does not contain free variables, because it @@ -879,11 +886,26 @@ PyFrame_FastToLocals(PyFrameObject *f) into the locals dict used by the class. */ if (co->co_flags & CO_OPTIMIZED) { - map_to_dict(co->co_freevars, nfreevars, - locals, fast + co->co_nlocals + ncells, 1); + if (map_to_dict(co->co_freevars, nfreevars, + locals, fast + co->co_nlocals + ncells, 1) < 0) + return -1; } } + return 0; +} + +void +PyFrame_FastToLocals(PyFrameObject *f) +{ + int res; + PyObject *error_type, *error_value, *error_traceback; + + PyErr_Fetch(&error_type, &error_value, &error_traceback); + res = PyFrame_FastToLocalsWithError(f); PyErr_Restore(error_type, error_value, error_traceback); + + if (res < 0) + PyErr_Clear(); } void diff -r 71a572a516f9 -r f35f82f47a2f Python/ceval.c --- a/Python/ceval.c Mon Jul 15 21:16:27 2013 +0200 +++ b/Python/ceval.c Mon Jul 15 19:46:22 2013 +0200 @@ -2464,7 +2464,9 @@ PyEval_EvalFrameEx(PyFrameObject *f, int TARGET(IMPORT_STAR) { PyObject *from = POP(), *locals; int err; - PyFrame_FastToLocals(f); + if (PyFrame_FastToLocalsWithError(f) < 0) + goto error; + locals = f->f_locals; if (locals == NULL) { PyErr_SetString(PyExc_SystemError, @@ -3991,7 +3993,8 @@ PyEval_GetLocals(void) PyFrameObject *current_frame = PyEval_GetFrame(); if (current_frame == NULL) return NULL; - PyFrame_FastToLocals(current_frame); + if (PyFrame_FastToLocalsWithError(current_frame) < 0) + return NULL; return current_frame->f_locals; } diff -r 71a572a516f9 -r f35f82f47a2f Python/sysmodule.c --- a/Python/sysmodule.c Mon Jul 15 21:16:27 2013 +0200 +++ b/Python/sysmodule.c Mon Jul 15 19:46:22 2013 +0200 @@ -332,12 +332,16 @@ static PyObject * call_trampoline(PyThreadState *tstate, PyObject* callback, PyFrameObject *frame, int what, PyObject *arg) { - PyObject *args = PyTuple_New(3); + PyObject *args; PyObject *whatstr; PyObject *result; + args = PyTuple_New(3); if (args == NULL) return NULL; + if (PyFrame_FastToLocalsWithError(frame) < 0) + return NULL; + Py_INCREF(frame); whatstr = whatstrings[what]; Py_INCREF(whatstr); @@ -349,7 +353,6 @@ call_trampoline(PyThreadState *tstate, P PyTuple_SET_ITEM(args, 2, arg); /* call the Python-level function */ - PyFrame_FastToLocals(frame); result = PyEval_CallObject(callback, args); PyFrame_LocalsToFast(frame, 1); if (result == NULL)