Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2e2c9f

Browse files
committed
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.
1 parent 4f38c1e commit e2e2c9f

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

Python/errors.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
128128
PyObject *type = *exc;
129129
PyObject *value = *val;
130130
PyObject *inclass = NULL;
131+
PyObject *initial_tb = NULL;
131132

132133
if (type == NULL) {
133134
/* This is a bug. Should never happen. Don't dump core. */
@@ -191,8 +192,18 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
191192
finally:
192193
Py_DECREF(type);
193194
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;
195200
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+
}
196207
/* normalize recursively */
197208
PyErr_NormalizeException(exc, val, tb);
198209
}

0 commit comments

Comments
 (0)