8000 Objects/exceptions.c · python/cpython@0298a04 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0298a04

Browse files
committed
Objects/exceptions.c
1 parent d83af14 commit 0298a04

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

Objects/exceptions.c

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3786,16 +3786,13 @@ PyObject *
37863786
_PyErr_TrySetFromCause(const char *format, ...)
37873787
{
37883788
PyObject* msg_prefix;
3789-
PyObject *exc, *val, *tb;
3790-
PyTypeObject *caught_type;
37913789
PyObject *instance_args;
37923790
Py_ssize_t num_args, caught_type_size, base_exc_size;
3793-
PyObject *new_exc, *new_val, *new_tb;
37943791
va_list vargs;
37953792
int same_basic_size;
37963793

3797-
PyErr_Fetch(&exc, &val, &tb);
3798-
caught_type = (PyTypeObject *)exc;
3794+
PyObject *exc = PyErr_GetRaisedException();
3795+
PyTypeObject *caught_type = Py_TYPE(exc);
37993796
/* Ensure type info indicates no extra state is stored at the C level
38003797
* and that the type can be reinstantiated using PyErr_Format
38013798
*/
@@ -3815,31 +3812,30 @@ _PyErr_TrySetFromCause(const char *format, ...)
38153812
* more state than just the exception type. Accordingly, we just
38163813
* leave it alone.
38173814
*/
3818-
PyErr_Restore(exc, val, tb);
3815+
PyErr_SetRaisedException(exc);
38193816
return NULL;
38203817
}
38213818

38223819
/* Check the args are empty or contain a single string */
3823-
PyErr_NormalizeException(&exc, &val, &tb);
3824-
instance_args = ((PyBaseExceptionObject *)val)->args;
3820+
instance_args = ((PyBaseExceptionObject *)exc)->args;
38253821
num_args = PyTuple_GET_SIZE(instance_args);
38263822
if (num_args > 1 ||
38273823
(num_args == 1 &&
38283824
!PyUnicode_CheckExact(PyTuple_GET_ITEM(instance_args, 0)))) {
38293825
/* More than 1 arg, or the one arg we do have isn't a string
38303826
*/
3831-
PyErr_Restore(exc, val, tb);
3827+
PyErr_SetRaisedException(exc);
38323828
return NULL;
38333829
}
38343830

38353831
/* Ensure the instance dict is also empty */
3836-
if (!_PyObject_IsInstanceDictEmpty(val)) {
3832+
if (!_PyObject_IsInstanceDictEmpty(exc)) {
38373833
/* While we could potentially copy a non-empty instance dictionary
38383834
* to the replacement exception, for now we take the more
38393835
* conservative path of leaving exceptions with attributes set
38403836
* alone.
38413837
*/
3842-
PyErr_Restore(exc, val, tb);
3838+
PyErr_SetRaisedException(exc);
38433839
return NULL;
38443840
}
38453841

@@ -3852,28 +3848,18 @@ _PyErr_TrySetFromCause(const char *format, ...)
38523848
* types as well, but that's quite a bit trickier due to the extra
38533849
* state potentially stored on OSError instances.
38543850
*/
3855-
/* Ensure the traceback is set correctly on the existing exception */
3856-
if (tb != NULL) {
3857-
PyException_SetTraceback(val, tb);
3858-
Py_DECREF(tb);
3859-
}
3860-
38613851
va_start(vargs, format);
38623852
msg_prefix = PyUnicode_FromFormatV(format, vargs);
38633853
va_end(vargs);
38643854
if (msg_prefix == NULL) {
3865-
Py_DECREF(exc);
3866-
Py_DECREF(val);
38673855
return NULL;
38683856
}
38693857

3870-
PyErr_Format(exc, "%U (%s: %S)",
3871-
msg_prefix, Py_TYPE(val)->tp_name, val);
3872-
Py_DECREF(exc);
3858+
PyErr_Format((PyObject*)Py_TYPE(exc), "%U (%s: %S)",
3859+
msg_prefix, Py_TYPE(exc)->tp_name, exc);
38733860
Py_DECREF(msg_prefix);
3874-
PyErr_Fetch(&new_exc, &new_val, &new_tb);
3875-
PyErr_NormalizeException(&new_exc, &new_val, &new_tb);
3876-
PyException_SetCause(new_val, val);
3877-
PyErr_Restore(new_exc, new_val, new_tb);
3878-
return new_val;
3861+
PyObject *new_exc = PyErr_GetRaisedException();
3862+
PyException_SetCause(new_exc, exc);
3863+
PyErr_SetRaisedException(new_exc);
3864+
return new_exc;
38793865
}

0 commit comments

Comments
 (0)
0