comparison Python/errors.c @ 20116:55874d8e0dfb legacy-trunk

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.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Wed, 26 Sep 2001 19:58:38 +0000
parents e1c91ac55cca
children abaf9ea2e560
comparison
equal deleted inserted replaced
20115:8ec95ce78735 20116:55874d8e0dfb
126 PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) 126 PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
127 { 127 {
128 PyObject *type = *exc; 128 PyObject *type = *exc;
129 PyObject *value = *val; 129 PyObject *value = *val;
130 PyObject *inclass = NULL; 130 PyObject *inclass = NULL;
131 PyObject *initial_tb = NULL;
131 132
132 if (type == NULL) { 133 if (type == NULL) {
133 /* This is a bug. Should never happen. Don't dump core. */ 134 /* This is a bug. Should never happen. Don't dump core. */
134 PyErr_SetString(PyExc_SystemError, 135 PyErr_SetString(PyExc_SystemError,
135 "PyErr_NormalizeException() called without exception"); 136 "PyErr_NormalizeException() called without exception");
189 *val = value; 190 *val = value;
190 return; 191 return;
191 finally: 192 finally:
192 Py_DECREF(type); 193 Py_DECREF(type);
193 Py_DECREF(value); 194 Py_DECREF(value);
194 Py_XDECREF(*tb); 195 /* If the new exception doesn't set a traceback and the old
196 exception had a traceback, use the old traceback for the
197 new exception. It's better than nothing.
198 */
199 initial_tb = *tb;
195 PyErr_Fetch(exc, val, tb); 200 PyErr_Fetch(exc, val, tb);
201 if (initial_tb != NULL) {
202 if (*tb == NULL)
203 *tb = initial_tb;
204 else
205 Py_DECREF(initial_tb);
206 }
196 /* normalize recursively */ 207 /* normalize recursively */
197 PyErr_NormalizeException(exc, val, tb); 208 PyErr_NormalizeException(exc, val, tb);
198 } 209 }
199 210
200 211