8000 gh-102755: Add PyErr_DisplayException(exc) · python/cpython@cd19310 · GitHub
[go: up one dir, main page]

Skip to content

Commit cd19310

Browse files
committed
gh-102755: Add PyErr_DisplayException(exc)
1 parent 51d693c commit cd19310

File tree

7 files changed

+60
-78
lines changed

7 files changed

+60
-78
lines changed

Doc/c-api/exceptions.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ Printing and clearing
8686
8787
An exception must be set when calling this function.
8888
89+
.. c:function: void PyErr_DisplayException(PyObject *exc)
90+
91+
Print the standard traceback of ``exc`` to ``sys.stderr``.
92+
93+
.. versionadded:: 3.12
8994
9095
Raising exceptions
9196
==================

Include/internal/pycore_pylifecycle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ PyAPI_FUNC(PyObject*) _PyErr_WriteUnraisableDefaultHook(PyObject *unraisable);
8787
PyAPI_FUNC(void) _PyErr_Print(PyThreadState *tstate);
8888
PyAPI_FUNC(void) _PyErr_Display(PyObject *file, PyObject *exception,
8989
PyObject *value, PyObject *tb);
90+
PyAPI_FUNC(void) _PyErr_DisplayException(PyObject *file, PyObject *exc);
9091

9192
PyAPI_FUNC(void) _PyThreadState_DeleteCurrent(PyThreadState *tstate);
9293

Include/pythonrun.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ PyAPI_FUNC(PyObject *) Py_CompileString(const char *, const char *, int);
1212
PyAPI_FUNC(void) PyErr_Print(void);
1313
PyAPI_FUNC(void) PyErr_PrintEx(int);
1414
PyAPI_FUNC(void) PyErr_Display(PyObject *, PyObject *, PyObject *);
15+
PyAPI_FUNC(void) PyErr_DisplayException(PyObject *);
1516

1617

1718
/* Stuff with no proper home (yet) */

Modules/_testcapi/exceptions.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,13 @@ err_restore(PyObject *self, PyObject *args) {
3939
static PyObject *
4040
exception_print(PyObject *self, PyObject *args)
4141
{
42-
PyObject *value;
43-
PyObject *tb = NULL;
42+
PyObject *exc;
4443

45-
if (!PyArg_ParseTuple(args, "O:exception_print", &value)) {
44+
if (!PyArg_ParseTuple(args, "O:exception_print", &exc)) {
4645
return NULL;
4746
}
4847

49-
if (PyExceptionInstance_Check(value)) {
50-
tb = PyException_GetTraceback(value);
51-
}
52-
53-
PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
54-
Py_XDECREF(tb);
55-
48+
PyErr_DisplayException(exc);
5649
Py_RETURN_NONE;
5750
}
5851

Python/pylifecycle.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,41 +2537,28 @@ _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
25372537
static int
25382538
_Py_FatalError_PrintExc(PyThreadState *tstate)
25392539
{
2540-
PyObject *ferr, *res;
2541-
PyObject *exception, *v, *tb;
2542-
int has_tb;
2543-
2544-
_PyErr_Fetch(tstate, &exception, &v, &tb);
2545-
if (exception == NULL) {
2540+
PyObject *exc = _PyErr_GetRaisedException(tstate);
2541+
if (exc == NULL) {
25462542
/* No current exception */
25472543
return 0;
25482544
}
25492545

2550-
ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
2546+
PyObject *ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
25512547
if (ferr == NULL || ferr == Py_None) {
25522548
/* sys.stderr is not set yet or set to None,
25532549
no need to try to display the exception */
25542550
return 0;
25552551
}
25562552

2557-
_PyErr_NormalizeException(tstate, &exception, &v, &tb);
2558-
if (tb == NULL) {
2559-
tb = Py_NewRef(Py_None);
2560-
}
2561-
PyException_SetTraceback(v, tb);
2562-
if (exception == NULL) {
2563-
/* PyErr_NormalizeException() failed */
2564-
return 0;
2565-
}
2553+
PyErr_DisplayException(exc);
25662554

2567-
has_tb = (tb != Py_None);
2568-
PyErr_Display(exception, v, tb);
2569-
Py_XDECREF(exception);
2570-
Py_XDECREF(v);
2555+
PyObject *tb = PyException_GetTraceback(exc);
2556+
int has_tb = (tb != NULL) && (tb != Py_None);
25712557
Py_XDECREF(tb);
2558+
Py_XDECREF(exc);
25722559

25732560
/* sys.stderr may be buffered: call sys.stderr.flush() */
2574-
res = PyObject_CallMethodNoArgs(ferr, &_Py_ID(flush));
2561+
PyObject *res = PyObject_CallMethodNoArgs(ferr, &_Py_ID(flush));
25752562
if (res == NULL) {
25762563
_PyErr_Clear(tstate);
25772564
}

Python/pythonrun.c

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -764,39 +764,34 @@ handle_system_exit(void)
764764
static void
765765
_PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
766766
{
767-
PyObject *exception, *v, *tb, *hook;
768-
767+
PyObject *typ = NULL, *tb = NULL;
769768
handle_system_exit();
770769

771-
_PyErr_Fetch(tstate, &exception, &v, &tb);
772-
if (exception == NULL) {
770+
PyObject *exc = _PyErr_GetRaisedException(tstate);
771+
if (exc == NULL) {
773772
goto done;
774773
}
775-
776-
_PyErr_NormalizeException(tstate, &exception, &v, &tb);
774+
assert(PyExceptionInstance_Check(exc));
775+
typ = Py_NewRef(Py_TYPE(exc));
776+
tb = PyException_GetTraceback(exc);
777777
if (tb == NULL) {
778778
tb = Py_NewRef(Py_None);
779779
}
780-
PyException_SetTraceback(v, tb);
781-
if (exception == NULL) {
782-
goto done;
783-
}
784780

785-
/* Now we know v != NULL too */
786781
if (set_sys_last_vars) {
787-
if (_PySys_SetAttr(&_Py_ID(last_type), exception) < 0) {
782+
if (_PySys_SetAttr(&_Py_ID(last_type), typ) < 0) {
788783
_PyErr_Clear(tstate);
789784
}
790-
if (_PySys_SetAttr(&_Py_ID(last_value), v) < 0) {
785+
if (_PySys_SetAttr(&_Py_ID(last_value), exc) < 0) {
791786
_PyErr_Clear(tstate);
792787
}
793788
if (_PySys_SetAttr(&_Py_ID(last_traceback), tb) < 0) {
794789
_PyErr_Clear(tstate);
795790
}
796791
}
797-
hook = _PySys_GetAttr(tstate, &_Py_ID(excepthook));
792+
PyObject *hook = _PySys_GetAttr(tstate, &_Py_ID(excepthook));
798793
if (_PySys_Audit(tstate, "sys.excepthook", "OOOO", hook ? hook : Py_None,
799-
exception, v, tb) < 0) {
794+
typ, exc, tb) < 0) {
800795
if (PyErr_ExceptionMatches(PyExc_RuntimeError)) {
801796
PyErr_Clear();
802797
goto done;
@@ -805,46 +800,34 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
805800
}
806801
if (hook) {
807802
PyObject* stack[3];
808-
PyObject *result;
809-
810-
stack[0] = exception;
811-
stack[1] = v;
803+
stack[0] = typ;
804+
stack[1] = exc;
812805
stack[2] = tb;
813-
result = _PyObject_FastCall(hook, stack, 3);
806+
PyObject *result = _PyObject_FastCall(hook, stack, 3);
814807
if (result == NULL) {
815808
handle_system_exit();
816809

817-
PyObject *exception2, *v2, *tb2;
818-
_PyErr_Fetch(tstate, &exception2, &v2, &tb2);
819-
_PyErr_NormalizeException(tstate, &exception2, &v2, &tb2);
820-
/* It should not be possible for exception2 or v2
821-
to be NULL. However PyErr_Display() can't
822-
tolerate NULLs, so just be safe. */
823-
if (exception2 == NULL) {
824-
exception2 = Py_NewRef(Py_None);
825-
}
826-
if (v2 == NULL) {
827-
v2 = Py_NewRef(Py_None);
828-
}
810+
PyObject *exc2 = _PyErr_GetRaisedException(tstate);
811+
assert(exc2 && PyExceptionInstance_Check(exc2));
829812
fflush(stdout);
830813
PySys_WriteStderr("Error in sys.excepthook:\n");
831-
PyErr_Display(exception2, v2, tb2);
814+
PyErr_DisplayException(exc2);
832815
PySys_WriteStderr("\nOriginal exception was:\n");
833-
PyErr_Display(exception, v, tb);
834-
Py_DECREF(exception2);
835-
Py_DECREF(v2);
836-
Py_XDECREF(tb2);
816+
PyErr_DisplayException(exc);
817+
Py_DECREF(exc2);
818+
}
819+
else {
820+
Py_DECREF(result);
837821
}
838-
Py_XDECREF(result);
839822
}
840823
else {
841824
PySys_WriteStderr("sys.excepthook is missing\n");
842-
PyErr_Display(exception, v, tb);
825+
PyErr_DisplayException(exc);
843826
}
844827

845828
done:
846-
Py_XDECREF(exception);
847-
Py_XDECREF(v);
829+
Py_XDECREF(typ);
830+
Py_XDECREF(exc);
848831
Py_XDECREF(tb);
849832
}
850833

@@ -1508,18 +1491,20 @@ print_exception_recursive(struct exception_print_context *ctx, PyObject *value)
15081491
#define PyErr_MAX_GROUP_DEPTH 10
15091492

15101493
void
1511-
_PyErr_Display(PyObject *file, PyObject *exception, PyObject *value, PyObject *tb)
1494+
_PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
15121495
{
15131496
assert(file != NULL && file != Py_None);
15141497
if (PyExceptionInstance_Check(value)
15151498
&& tb != NULL && PyTraceBack_Check(tb)) {
15161499
/* Put the traceback on the exception, otherwise it won't get
15171500
displayed. See issue #18776. */
15181501
PyObject *cur_tb = PyException_GetTraceback(value);
1519-
if (cur_tb == NULL)
1502+
if (cur_tb == NULL) {
15201503
PyException_SetTraceback(value, tb);
1521-
else
1504+
}
1505+
else {
15221506
Py_DECREF(cur_tb);
1507+
}
15231508
}
15241509

15251510
struct exception_print_context ctx;
@@ -1555,7 +1540,7 @@ _PyErr_Display(PyObject *file, PyObject *exception, PyObject *value, PyObject *t
15551540
}
15561541

15571542
void
1558-
PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1543+
PyErr_Display(PyObject *unused, PyObject *value, PyObject *tb)
15591544
{
15601545
PyThreadState *tstate = _PyThreadState_GET();
15611546
PyObject *file = _PySys_GetAttr(tstate, &_Py_ID(stderr));
@@ -1568,10 +1553,20 @@ PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
15681553
return;
15691554
}
15701555
Py_INCREF(file);
1571-
_PyErr_Display(file, exception, value, tb);
1556+
_PyErr_Display(file, NULL, value, tb);
15721557
Py_DECREF(file);
15731558
}
15741559

1560+
void _PyErr_DisplayException(PyObject *file, PyObject *exc)
1561+
{
1562+
_PyErr_Display(file, NULL, exc, NULL);
1563+
}
1564+
1565+
void PyErr_DisplayException(PyObject *exc)
1566+
{
1567+
PyErr_Display(NULL, exc, NULL);
1568+
}
1569+
15751570
PyObject *
15761571
PyRun_StringFlags(const char *str, int start, PyObject *globals,
15771572
PyObject *locals, PyCompilerFlags *flags)

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ sys_excepthook_impl(PyObject *module, PyObject *exctype, PyObject *value,
742742
PyObject *traceback)
743743
/*[clinic end generated code: output=18d99fdda21b6b5e input=ecf606fa826f19d9]*/
744744
{
745-
PyErr_Display(exctype, value, traceback);
745+
PyErr_Display(NULL, value, traceback);
746746
Py_RETURN_NONE;
747747
}
748748

0 commit comments

Comments
 (0)
0