@@ -761,39 +761,34 @@ handle_system_exit(void)
761
761
static void
762
762
_PyErr_PrintEx (PyThreadState * tstate , int set_sys_last_vars )
763
763
{
764
- PyObject * exception , * v , * tb , * hook ;
765
-
764
+ PyObject * typ = NULL , * tb = NULL ;
766
765
handle_system_exit ();
767
766
768
- _PyErr_Fetch ( tstate , & exception , & v , & tb );
769
- if (exception == NULL ) {
767
+ PyObject * exc = _PyErr_GetRaisedException ( tstate );
768
+ if (exc == NULL ) {
770
769
goto done ;
771
770
}
772
-
773
- _PyErr_NormalizeException (tstate , & exception , & v , & tb );
771
+ assert (PyExceptionInstance_Check (exc ));
772
+ typ = Py_NewRef (Py_TYPE (exc ));
773
+ tb = PyException_GetTraceback (exc );
774
774
if (tb == NULL ) {
775
775
tb = Py_NewRef (Py_None );
776
776
}
777
- PyException_SetTraceback (v , tb );
778
- if (exception == NULL ) {
779
- goto done ;
780
- }
781
777
782
- /* Now we know v != NULL too */
783
778
if (set_sys_last_vars ) {
784
- if (_PySys_SetAttr (& _Py_ID (last_type ), exception ) < 0 ) {
779
+ if (_PySys_SetAttr (& _Py_ID (last_type ), typ ) < 0 ) {
785
780
_PyErr_Clear (tstate );
786
781
}
787
- if (_PySys_SetAttr (& _Py_ID (last_value ), v ) < 0 ) {
782
+ if (_PySys_SetAttr (& _Py_ID (last_value ), exc ) < 0 ) {
788
783
_PyErr_Clear (tstate );
789
784
}
790
785
if (_PySys_SetAttr (& _Py_ID (last_traceback ), tb ) < 0 ) {
791
786
_PyErr_Clear (tstate );
792
787
}
793
788
}
794
- hook = _PySys_GetAttr (tstate , & _Py_ID (excepthook ));
789
+ PyObject * hook = _PySys_GetAttr (tstate , & _Py_ID (excepthook ));
795
790
if (_PySys_Audit (tstate , "sys.excepthook" , "OOOO" , hook ? hook : Py_None ,
796
- exception , v , tb ) < 0 ) {
791
+ typ , exc , tb ) < 0 ) {
797
792
if (PyErr_ExceptionMatches (PyExc_RuntimeError )) {
798
793
PyErr_Clear ();
799
794
goto done ;
@@ -802,46 +797,34 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
802
797
}
803
798
if (hook ) {
804
799
PyObject * stack [3 ];
805
- PyObject * result ;
806
-
807
- stack [0 ] = exception ;
808
- stack [1 ] = v ;
800
+ stack [0 ] = typ ;
801
+ stack [1 ] = exc ;
809
802
stack [2 ] = tb ;
810
- result = _PyObject_FastCall (hook , stack , 3 );
803
+ PyObject * result = _PyObject_FastCall (hook , stack , 3 );
811
804
if (result == NULL ) {
812
805
handle_system_exit ();
813
806
814
- PyObject * exception2 , * v2 , * tb2 ;
815
- _PyErr_Fetch (tstate , & exception2 , & v2 , & tb2 );
816
- _PyErr_NormalizeException (tstate , & exception2 , & v2 , & tb2 );
817
- /* It should not be possible for exception2 or v2
818
- to be NULL. However PyErr_Display() can't
819
- tolerate NULLs, so just be safe. */
820
- if (exception2 == NULL ) {
821
- exception2 = Py_NewRef (Py_None );
822
- }
823
- if (v2 == NULL ) {
824
- v2 = Py_NewRef (Py_None );
825
- }
807
+ PyObject * exc2 = _PyErr_GetRaisedException (tstate );
808
+ assert (exc2 && PyExceptionInstance_Check (exc2 ));
826
809
fflush (stdout );
827
810
PySys_WriteStderr ("Error in sys.excepthook:\n" );
828
- PyErr_Display ( exception2 , v2 , tb2 );
811
+ PyErr_DisplayException ( exc2 );
829
812
PySys_WriteStderr ("\nOriginal exception was:\n" );
830
- PyErr_Display (exception , v , tb );
831
- Py_DECREF (exception2 );
832
- Py_DECREF (v2 );
833
- Py_XDECREF (tb2 );
813
+ PyErr_DisplayException (exc );
814
+ Py_DECREF (exc2 );
815
+ }
816
+ else {
817
+ Py_DECREF (result );
834
818
}
835
- Py_XDECREF (result );
836
819
}
837
820
else {
838
821
PySys_WriteStderr ("sys.excepthook is missing\n" );
839
- PyErr_Display ( exception , v , tb );
822
+ PyErr_DisplayException ( exc );
840
823
}
841
824
842
825
done :
843
- Py_XDECREF (exception );
844
- Py_XDECREF (v );
826
+ Py_XDECREF (typ );
827
+ Py_XDECREF (exc );
845
828
Py_XDECREF (tb );
846
829
}
847
830
@@ -1505,18 +1488,20 @@ print_exception_recursive(struct exception_print_context *ctx, PyObject *value)
1505
1488
#define PyErr_MAX_GROUP_DEPTH 10
1506
1489
1507
1490
void
1508
- _PyErr_Display (PyObject * file , PyObject * exception , PyObject * value , PyObject * tb )
1491
+ _PyErr_Display (PyObject * file , PyObject * unused , PyObject * value , PyObject * tb )
1509
1492
{
1510
1493
assert (file != NULL && file != Py_None );
1511
1494
if (PyExceptionInstance_Check (value )
1512
1495
&& tb != NULL && PyTraceBack_Check (tb )) {
1513
1496
/* Put the traceback on the exception, otherwise it won't get
1514
1497
displayed. See issue #18776. */
1515
1498
PyObject * cur_tb = PyException_GetTraceback (value );
1516
- if (cur_tb == NULL )
1499
+ if (cur_tb == NULL ) {
1517
1500
PyException_SetTraceback (value , tb );
1518
- else
1501
+ }
1502
+ else {
1519
1503
Py_DECREF (cur_tb );
1504
+ }
1520
1505
}
1521
1506
1522
1507
struct exception_print_context ctx ;
@@ -1552,7 +1537,7 @@ _PyErr_Display(PyObject *file, PyObject *exception, PyObject *value, PyObject *t
1552
1537
}
1553
1538
1554
1539
void
1555
- PyErr_Display (PyObject * exception , PyObject * value , PyObject * tb )
1540
+ PyErr_Display (PyObject * unused , PyObject * value , PyObject * tb )
1556
1541
{
1557
1542
PyThreadState * tstate = _PyThreadState_GET ();
1558
1543
PyObject * file = _PySys_GetAttr (tstate , & _Py_ID (stderr ));
@@ -1565,10 +1550,20 @@ PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1565
1550
return ;
1566
1551
}
1567
1552
Py_INCREF (file );
1568
- _PyErr_Display (file , exception , value , tb );
1553
+ _PyErr_Display (file , NULL , value , tb );
1569
1554
Py_DECREF (file );
1570
1555
}
1571
1556
1557
+ void _PyErr_DisplayException (PyObject * file , PyObject * exc )
1558
+ {
1559
+ _PyErr_Display (file , NULL , exc , NULL );
1560
+ }
1561
+
1562
+ void PyErr_DisplayException (PyObject * exc )
1563
+ {
1564
+ PyErr_Display (NULL , exc , NULL );
1565
+ }
1566
+
1572
1567
PyObject *
1573
1568
PyRun_StringFlags (const char * str , int start , PyObject * globals ,
1574
1569
PyObject * locals , PyCompilerFlags * flags )
0 commit comments