Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,18 @@ PyErr_ExceptionMatches(PyObject *exc)
void
PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
{
PyObject *type = *exc;
PyObject *value = *val;
PyObject *inclass = NULL;
PyObject *initial_tb = NULL;
PyThreadState *tstate = NULL;
int recursion_depth = 0;
PyObject *type, *value, *initial_tb;
PyThreadState *tstate;

restart:
type = *exc;
if (type == NULL) {
/* There was no exception, so nothing to do. */
return;
}

value = *val;
/* If PyErr_SetNone() was used, the value will have been actually
set to NULL.
*/
Expand All @@ -245,14 +246,18 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Py_INCREF(value);
}

if (PyExceptionInstance_Check(value))
inclass = PyExceptionInstance_Class(value);

/* Normalize the exception so that if the type is a class, the
value will be an instance.
*/
if (PyExceptionClass_Check(type)) {
PyObject *inclass;
int is_subclass;

if (PyExceptionInstance_Check(value))
inclass = PyExceptionInstance_Class(value);
else
inclass = NULL;

if (inclass) {
is_subclass = PyObject_IsSubclass(inclass, type);
if (is_subclass < 0)
Expand All @@ -266,7 +271,7 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
value as an argument to instantiation of the type
class.
*/
if (!inclass || !is_subclass) {
if (!is_subclass) {
PyObject *fixed_value;

fixed_value = _PyErr_CreateException(type, value);
Expand All @@ -289,6 +294,7 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
*exc = type;
*val = value;
return;

finally:
Py_DECREF(type);
Py_DECREF(value);
Expand All @@ -298,6 +304,7 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
*/
initial_tb = *tb;
PyErr_Fetch(exc, val, tb);
assert(*exc != NULL);
if (initial_tb != NULL) {
if (*tb == NULL)
*tb = initial_tb;
Expand All @@ -306,18 +313,17 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
}
/* normalize recursively */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this comment perhaps?

tstate = PyThreadState_GET();
if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
--tstate->recursion_depth;
if (++recursion_depth > Py_GetRecursionLimit() - tstate->recursion_depth) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the function isn't recursive anymore, is there any reason you're still bothering with recursion_depth?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A guard against infinite loop.

/* throw away the old exception and use the recursion error instead */
Py_INCREF(PyExc_RecursionError);
Py_SETREF(*exc, PyExc_RecursionError);
Py_INCREF(PyExc_RecursionErrorInst);
Py_SETREF(*val, PyExc_RecursionErrorInst);
Py_XSETREF(*val, PyExc_RecursionErrorInst);
/* just keeping the old traceback */
return;
}
PyErr_NormalizeException(exc, val, tb);
--tstate->recursion_depth;
/* eliminate tail recursion */
goto restart;
}


Expand Down