@@ -698,30 +698,30 @@ _Py_HandleSystemExit(int *exitcode_p)
698
698
return 0 ;
699
699
}
700
700
701
- PyObject * exception , * value , * tb ;
702
- PyErr_Fetch (& exception , & value , & tb );
703
-
704
701
fflush (stdout );
705
702
706
703
int exitcode = 0 ;
707
- if (value == NULL || value == Py_None ) {
704
+
705
+ PyObject * exc = PyErr_GetRaisedException ();
706
+ if (exc == NULL ) {
708
707
goto done ;
709
708
}
709
+ assert (PyExceptionInstance_Check (exc ));
710
710
711
- if (PyExceptionInstance_Check (value )) {
712
- /* The error code should be in the `code' attribute. */
713
- PyObject * code = PyObject_GetAttr (value , & _Py_ID (code ));
714
- if (code ) {
715
- Py_SETREF (value , code );
716
- if (value == Py_None )
717
- goto done ;
711
+ /* The error code should be in the `code' attribute. */
712
+ PyObject * code = PyObject_GetAttr (exc , & _Py_ID (code ));
713
+ if (code ) {
714
+ Py_SETREF (exc , code );
715
+ if (exc == Py_None ) {
716
+ goto done ;
718
717
}
719
- /* If we failed to dig out the 'code' attribute,
720
- just let the else clause below print the error. */
721
718
}
719
+ /* If we failed to dig out the 'code' attribute,
720
+ * just let the else clause below print the error.
721
+ */
722
722
723
- if (PyLong_Check (value )) {
724
- exitcode = (int )PyLong_AsLong (value );
723
+ if (PyLong_Check (exc )) {
724
+ exitcode = (int )PyLong_AsLong (exc );
725
725
}
726
726
else {
727
727
PyThreadState * tstate = _PyThreadState_GET ();
@@ -732,20 +732,17 @@ _Py_HandleSystemExit(int *exitcode_p)
732
732
*/
733
733
PyErr_Clear ();
734
734
if (sys_stderr != NULL && sys_stderr != Py_None ) {
735
- PyFile_WriteObject (value , sys_stderr , Py_PRINT_RAW );
735
+ PyFile_WriteObject (exc , sys_stderr , Py_PRINT_RAW );
736
736
} else {
737
- PyObject_Print (value , stderr , Py_PRINT_RAW );
737
+ PyObject_Print (exc , stderr , Py_PRINT_RAW );
738
738
fflush (stderr );
739
739
}
740
740
PySys_WriteStderr ("\n" );
741
741
exitcode = 1 ;
742
742
}
743
743
744
- done :
745
- /* Cleanup the exception */
746
- Py_CLEAR (exception );
747
- Py_CLEAR (value );
748
- Py_CLEAR (tb );
744
+ done :
745
+ Py_CLEAR (exc );
749
746
* exitcode_p = exitcode ;
750
747
return 1 ;
751
748
}
@@ -1641,35 +1638,29 @@ PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1641
1638
1642
1639
}
1643
1640
1644
-
1645
1641
static void
1646
- flush_io ( void )
1642
+ flush_io_stream ( PyThreadState * tstate , PyObject * name )
1647
1643
{
1648
- PyObject * f , * r ;
1649
- PyObject * type , * value , * traceback ;
1650
-
1651
- /* Save the current exception */
1652
- PyErr_Fetch (& type , & value , & traceback );
1653
-
1654
- PyThreadState * tstate = _PyThreadState_GET ();
1655
- f = _PySys_GetAttr (tstate , & _Py_ID (stderr ));
1656
- if (f != NULL ) {
1657
- r = _PyObject_CallMethodNoArgs (f , & _Py_ID (flush ));
1658
- if (r )
1659
- Py_DECREF (r );
1660
- else
1661
- PyErr_Clear ();
1662
- }
1663
- f = _PySys_GetAttr (tstate , & _Py_ID (stdout ));
1644
+ PyObject * f = _PySys_GetAttr (tstate , name );
1664
1645
if (f != NULL ) {
1665
- r = _PyObject_CallMethodNoArgs (f , & _Py_ID (flush ));
1666
- if (r )
1646
+ PyObject * r = _PyObject_CallMethodNoArgs (f , & _Py_ID (flush ));
1647
+ if (r ) {
1667
1648
Py_DECREF (r );
1668
- else
1649
+ }
1650
+ else {
1669
1651
PyErr_Clear ();
1652
+ }
1670
1653
}
1654
+ }
1671
1655
1672
- PyErr_Restore (type , value , traceback );
1656
+ static void
1657
+ flush_io (void )
1658
+ {
1659
+ PyThreadState * tstate = _PyThreadState_GET ();
1660
+ PyObject * exc = _PyErr_GetRaisedException (tstate );
1661
+ flush_io_stream (tstate , & _Py_ID (stderr ));
1662
+ flush_io_stream (tstate , & _Py_ID (stdout ));
1663
+ _PyErr_SetRaisedException (tstate , exc );
1673
1664
}
1674
1665
1675
1666
static PyObject *
0 commit comments