-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
bpo-45711: use exc_value instead of exc_type to determine if exc_info is valid. Add more assertions. #29627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-45711: use exc_value instead of exc_type to determine if exc_info is valid. Add more assertions. #29627
Changes from 3 commits
9275583
fbff4f6
bc889bc
3c451a1
277c509
437cebb
de67ed6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,11 +79,15 @@ _PyErr_StackItem * | |
| _PyErr_GetTopmostException(PyThreadState *tstate) | ||
| { | ||
| _PyErr_StackItem *exc_info = tstate->exc_info; | ||
| while ((exc_info->exc_type == NULL || exc_info->exc_type == Py_None) && | ||
| while ((exc_info->exc_value == NULL || exc_info->exc_value == Py_None) && | ||
| exc_info->previous_item != NULL) | ||
| { | ||
| assert(exc_info->exc_type == NULL || exc_info->exc_type == Py_None); | ||
| exc_info = exc_info->previous_item; | ||
| } | ||
| assert(exc_info == NULL || | ||
| exc_info->previous_item == NULL || | ||
| (exc_info->exc_type != NULL && exc_info->exc_type != Py_None)); | ||
| return exc_info; | ||
| } | ||
|
|
||
|
|
@@ -471,10 +475,25 @@ _PyErr_GetExcInfo(PyThreadState *tstate, | |
| PyObject **p_type, PyObject **p_value, PyObject **p_traceback) | ||
| { | ||
| _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); | ||
| *p_type = exc_info->exc_type; | ||
|
|
||
| *p_value = exc_info->exc_value; | ||
| *p_traceback = exc_info->exc_traceback; | ||
|
gvanrossum marked this conversation as resolved.
|
||
|
|
||
| assert(*p_value == NULL || | ||
| *p_value == Py_None || | ||
| PyExceptionInstance_Check(*p_value)); | ||
|
|
||
| *p_type = (*p_value != NULL && PyExceptionInstance_Check(*p_value)) ? | ||
| PyExceptionInstance_Class(*p_value) : | ||
| Py_None; | ||
|
|
||
| if (exc_info->exc_type == NULL || exc_info->exc_type == Py_None) { | ||
| assert(*p_type == Py_None); | ||
| } | ||
| else { | ||
| assert(*p_type == exc_info->exc_type); | ||
| } | ||
|
gvanrossum marked this conversation as resolved.
Outdated
|
||
|
|
||
| Py_XINCREF(*p_type); | ||
| Py_XINCREF(*p_value); | ||
| Py_XINCREF(*p_traceback); | ||
|
|
@@ -507,6 +526,30 @@ PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback) | |
| Py_XDECREF(oldtraceback); | ||
| } | ||
|
|
||
|
|
||
| PyObject* | ||
| _PyErr_StackItemToExcInfoTuple(_PyErr_StackItem *err_info) | ||
| { | ||
| PyObject *exc_value = err_info->exc_value; | ||
| if (exc_value == NULL) { | ||
| exc_value = Py_None; | ||
| } | ||
|
|
||
| assert(exc_value == Py_None || PyExceptionInstance_Check(exc_value)); | ||
|
|
||
| PyObject *exc_type = PyExceptionInstance_Check(exc_value) ? | ||
| PyExceptionInstance_Class(err_info->exc_value) : | ||
|
iritkatriel marked this conversation as resolved.
Outdated
|
||
| Py_None; | ||
|
|
||
| return Py_BuildValue( | ||
| "(OOO)", | ||
| exc_type, | ||
| exc_value, | ||
| err_info->exc_traceback != NULL ? | ||
| err_info->exc_traceback : Py_None); | ||
| } | ||
|
|
||
|
|
||
| /* Like PyErr_Restore(), but if an exception is already set, | ||
| set the context associated with it. | ||
|
|
||
|
|
@@ -567,7 +610,11 @@ _PyErr_ChainStackItem(_PyErr_StackItem *exc_info) | |
| } else { | ||
| exc_info_given = 1; | ||
| } | ||
| if (exc_info->exc_type == NULL || exc_info->exc_type == Py_None) { | ||
|
|
||
| assert( (exc_info->exc_type == NULL || exc_info->exc_type == Py_None) == | ||
| (exc_info->exc_value == NULL || exc_info->exc_value == Py_None) ); | ||
|
|
||
| if (exc_info->exc_value == NULL || exc_info->exc_value == Py_None) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -586,7 +633,18 @@ _PyErr_ChainStackItem(_PyErr_StackItem *exc_info) | |
| exc2 = exc_info->exc_type; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While you're here could you change 'exc' and 'exc2' to 'type' and 'type2' (or 'typ' and 'typ2')? It drives me nuts that we mix "exc[exption]" and "typ[e]" for the same concept (this must date back to Python 1 when the value was not an exception instance and the exception could be any object). |
||
| val2 = exc_info->exc_value; | ||
| tb2 = exc_info->exc_traceback; | ||
| #ifdef Py_DEBUG | ||
| PyObject *exc2_before = exc2; | ||
| PyObject *val2_before = val2; | ||
| PyObject *tb2_before = tb2; | ||
| #endif | ||
| _PyErr_NormalizeException(tstate, &exc2, &val2, &tb2); | ||
| #ifdef Py_DEBUG | ||
| /* exc_info should already be normalized */ | ||
| assert(exc2 == exc2_before); | ||
| assert(val2 == val2_before); | ||
| assert(tb2 == tb2_before); | ||
| #endif | ||
| if (tb2 != NULL) { | ||
| PyException_SetTraceback(val2, tb2); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.