[Patches] fix for bug open/81
Charles G Waldman
cgw@fnal.gov
Tue, 9 May 2000 14:45:34 -0500 (CDT)
According to the buglist (open/81):
def f(a,a):
return a
is not caught as an error by the bytecode compiler.
I thought this would be quick and easy to fix and so I added an
approprate com_error call to com_newlocal_o in compile.c. However, to
my surprise, this didn't work. On closer examination I realized it is
because a bunch of functions (e.g. PyDict_GetItem, PyDict_SetItem,
PyObject_Compare) call PyErr_Clear, clobbering the exception set by
com_error. (Subsequently PyErr_Print dumps core, since
tstate->curexc_type is 0). Basically, if one does any dictionary
operations after an error has occurred, that error is clobbered. If
one checks for errors after every operation, this is OK, but for
instance in compile_node, many operations get done and the error check
is only done at the end, so the intervening dictionary operations have
clobbered the exception. I have fixed this in dictobject.c and
object.c - replacing the PyErr_Clear with a PyErr_Fetch/PyErr_Restore
pair, to avoid generating new exceptions while not clobbering any old
exceptions. It seems to me that a lot of the other places where
PyErr_Clear is used, it really ought to be replaced with
PyErr_Fetch/PyErr_Restore in a similar fashion.
Commit notice:
compile.c (com_newlocal_o): check that new local variable is
actually new. Trap errors like def f(a,a): return a
dictobject.c (PyDict_GetItem, dict_compare): don't clobber
an existing exception
object.c (get_inprogress_dict): don't clobber an existing exception
Disclaimer:
I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims"). To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.
I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation. I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.
index: Objects/dictobject.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Objects/dictobject.c,v
retrieving revision 2.52
diff -c -r2.52 dictobject.c
*** dictobject.c 2000/05/03 23:44:34 2.52
--- dictobject.c 2000/05/09 19:18:45
***************
*** 312,317 ****
--- 312,319 ----
PyObject *key;
{
long hash;
+ PyObject *exception, *value, *traceback;
+
if (!PyDict_Check(op)) {
return NULL;
}
***************
*** 322,330 ****
(hash = ((PyStringObject *) key)->ob_shash) == -1)
#endif
{
hash = PyObject_Hash(key);
if (hash == -1) {
- PyErr_Clear();
return NULL;
}
}
--- 324,333 ----
(hash = ((PyStringObject *) key)->ob_shash) == -1)
#endif
{
+ PyErr_Fetch(&exception, &value, &traceback);
hash = PyObject_Hash(key);
+ PyErr_Restore(exception, value, traceback);
if (hash == -1) {
return NULL;
}
}
***************
*** 936,954 ****
if (!PyString_Check(akey) ||
(ahash = ((PyStringObject *) akey)->ob_shash) == -1)
#endif
! {
ahash = PyObject_Hash(akey);
! if (ahash == -1)
! PyErr_Clear(); /* Don't want errors here */
}
#ifdef CACHE_HASH
if (!PyString_Check(bkey) ||
(bhash = ((PyStringObject *) bkey)->ob_shash) == -1)
#endif
{
bhash = PyObject_Hash(bkey);
! if (bhash == -1)
! PyErr_Clear(); /* Don't want errors here */
}
aval = lookdict(a, akey, ahash) -> me_value;
bval = lookdict(b, bkey, bhash) -> me_value;
--- 939,959 ----
if (!PyString_Check(akey) ||
(ahash = ((PyStringObject *) akey)->ob_shash) == -1)
#endif
! {
! /* Don't want errors here */
! PyErr_Fetch(&exception, &value, &traceback);
ahash = PyObject_Hash(akey);
! PyErr_Restore(exception, value, traceback);
}
#ifdef CACHE_HASH
if (!PyString_Check(bkey) ||
(bhash = ((PyStringObject *) bkey)->ob_shash) == -1)
#endif
{
+ /* Don't want errors here */
+ PyErr_Fetch(&exception, &value, &traceback);
bhash = PyObject_Hash(bkey);
! PyErr_Restore(exception, value, traceback);
}
aval = lookdict(a, akey, ahash) -> me_value;
bval = lookdict(b, bkey, bhash) -> me_value;
Index: Objects/object.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Objects/object.c,v
retrieving revision 2.70
diff -c -r2.70 object.c
*** object.c 2000/05/03 23:44:35 2.70
--- object.c 2000/05/09 19:18:46
***************
*** 334,348 ****
get_inprogress_dict()
{
PyObject *tstate_dict, *inprogress;
tstate_dict = PyThreadState_GetDict();
if (tstate_dict == NULL) {
PyErr_BadInternalCall();
return NULL;
}
inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
if (inprogress == NULL) {
- PyErr_Clear();
inprogress = PyDict_New();
if (inprogress == NULL)
return NULL;
--- 334,350 ----
get_inprogress_dict()
{
PyObject *tstate_dict, *inprogress;
+ PyObject *exception, *value, *traceback;
tstate_dict = PyThreadState_GetDict();
if (tstate_dict == NULL) {
PyErr_BadInternalCall();
return NULL;
}
+ PyErr_Fetch(&exception, &value, &traceback);
inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
+ PyErr_Restore(exception, value, traceback);
if (inprogress == NULL) {
inprogress = PyDict_New();
if (inprogress == NULL)
return NULL;
Index: Python/compile.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Python/compile.c,v
retrieving revision 2.108
diff -c -r2.108 compile.c
*** compile.c 2000/05/03 23:44:38 2.108
--- compile.c 2000/05/09 19:18:48
***************
*** 2228,2233 ****
--- 2228,2238 ----
}
return 0;
}
+ if (PyDict_GetItem(c->c_locals, nameval) != NULL) {
+ com_error(c, PyExc_SyntaxError,
+ "duplicate argument name");
+ return 0;
+ }
ival = PyInt_FromLong(i = c->c_nlocals++);
if (ival == NULL)
c->c_errors++;