[Python-checkins] CVS: python/dist/src/Python errors.c,2.64,2.65

Jeremy Hylton jhylton@users.sourceforge.net
Wed, 26 Sep 2001 12:58:41 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv3911

Modified Files:
	errors.c 
Log Message:
PyErr_NormalizeException()

If a new exception occurs while an exception instance is being
created, try harder to make sure there is a traceback.  If the
original exception had a traceback associated with it and the new
exception does not, keep the old exception.

Of course, callers to PyErr_NormalizeException() must still be
prepared to have tb set to NULL.

XXX This isn't an ideal solution, but it's better than no traceback at
all.  It occurs if, for example, the exception occurs when the call to
the constructor fails before any Python code is executed.  Guido
suggests that it there is Python code that was about to be executed
-- but wasn't, say, because it was called with the wrong number of
arguments -- then we should point at the first line of the code object
anyway.


Index: errors.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/errors.c,v
retrieving revision 2.64
retrieving revision 2.65
diff -C2 -d -r2.64 -r2.65
*** errors.c	2001/08/24 18:35:23	2.64
--- errors.c	2001/09/26 19:58:38	2.65
***************
*** 129,132 ****
--- 129,133 ----
  	PyObject *value = *val;
  	PyObject *inclass = NULL;
+ 	PyObject *initial_tb = NULL;
  
  	if (type == NULL) {
***************
*** 192,197 ****
  	Py_DECREF(type);
  	Py_DECREF(value);
! 	Py_XDECREF(*tb);
  	PyErr_Fetch(exc, val, tb);
  	/* normalize recursively */
  	PyErr_NormalizeException(exc, val, tb);
--- 193,208 ----
  	Py_DECREF(type);
  	Py_DECREF(value);
! 	/* If the new exception doesn't set a traceback and the old
! 	   exception had a traceback, use the old traceback for the
! 	   new exception.  It's better than nothing.
! 	*/
! 	initial_tb = *tb;
  	PyErr_Fetch(exc, val, tb);
+ 	if (initial_tb != NULL) {
+ 		if (*tb == NULL)
+ 			*tb = initial_tb;
+ 		else
+ 			Py_DECREF(initial_tb);
+ 	}
  	/* normalize recursively */
  	PyErr_NormalizeException(exc, val, tb);