mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
gh-102755: Add PyErr_DisplayException(exc) (#102756)
This commit is contained in:
parent
405739f916
commit
3f9285a8c5
13 changed files with 76 additions and 78 deletions
|
@ -2537,41 +2537,28 @@ _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
|
|||
static int
|
||||
_Py_FatalError_PrintExc(PyThreadState *tstate)
|
||||
{
|
||||
PyObject *ferr, *res;
|
||||
PyObject *exception, *v, *tb;
|
||||
int has_tb;
|
||||
|
||||
_PyErr_Fetch(tstate, &exception, &v, &tb);
|
||||
if (exception == NULL) {
|
||||
PyObject *exc = _PyErr_GetRaisedException(tstate);
|
||||
if (exc == NULL) {
|
||||
/* No current exception */
|
||||
return 0;
|
||||
}
|
||||
|
||||
ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
|
||||
PyObject *ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
|
||||
if (ferr == NULL || ferr == Py_None) {
|
||||
/* sys.stderr is not set yet or set to None,
|
||||
no need to try to display the exception */
|
||||
return 0;
|
||||
}
|
||||
|
||||
_PyErr_NormalizeException(tstate, &exception, &v, &tb);
|
||||
if (tb == NULL) {
|
||||
tb = Py_NewRef(Py_None);
|
||||
}
|
||||
PyException_SetTraceback(v, tb);
|
||||
if (exception == NULL) {
|
||||
/* PyErr_NormalizeException() failed */
|
||||
return 0;
|
||||
}
|
||||
PyErr_DisplayException(exc);
|
||||
|
||||
has_tb = (tb != Py_None);
|
||||
PyErr_Display(exception, v, tb);
|
||||
Py_XDECREF(exception);
|
||||
Py_XDECREF(v);
|
||||
PyObject *tb = PyException_GetTraceback(exc);
|
||||
int has_tb = (tb != NULL) && (tb != Py_None);
|
||||
Py_XDECREF(tb);
|
||||
Py_XDECREF(exc);
|
||||
|
||||
/* sys.stderr may be buffered: call sys.stderr.flush() */
|
||||
res = PyObject_CallMethodNoArgs(ferr, &_Py_ID(flush));
|
||||
PyObject *res = PyObject_CallMethodNoArgs(ferr, &_Py_ID(flush));
|
||||
if (res == NULL) {
|
||||
_PyErr_Clear(tstate);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue