mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
PEP 415: Implement suppression of __context__ display with an exception attribute
This replaces the original PEP 409 implementation. See #14133.
This commit is contained in:
parent
d91dc62379
commit
d5a1c44455
12 changed files with 80 additions and 79 deletions
|
@ -3572,23 +3572,26 @@ do_raise(PyObject *exc, PyObject *cause)
|
|||
|
||||
if (cause) {
|
||||
PyObject *fixed_cause;
|
||||
int result;
|
||||
if (PyExceptionClass_Check(cause)) {
|
||||
fixed_cause = PyObject_CallObject(cause, NULL);
|
||||
if (fixed_cause == NULL)
|
||||
goto raise_error;
|
||||
Py_CLEAR(cause);
|
||||
} else {
|
||||
/* Let "exc.__cause__ = cause" handle all further checks */
|
||||
fixed_cause = cause;
|
||||
cause = NULL; /* Steal the reference */
|
||||
Py_DECREF(cause);
|
||||
}
|
||||
/* We retain ownership of the reference to fixed_cause */
|
||||
result = _PyException_SetCauseChecked(value, fixed_cause);
|
||||
Py_DECREF(fixed_cause);
|
||||
if (result < 0) {
|
||||
else if (PyExceptionInstance_Check(cause)) {
|
||||
fixed_cause = cause;
|
||||
}
|
||||
else if (cause == Py_None) {
|
||||
Py_DECREF(cause);
|
||||
fixed_cause = NULL;
|
||||
}
|
||||
else {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"exception causes must derive from "
|
||||
"BaseException");
|
||||
goto raise_error;
|
||||
}
|
||||
PyException_SetCause(value, fixed_cause);
|
||||
}
|
||||
|
||||
PyErr_SetObject(type, value);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue