8000
We read every piece of feedback, and take your input very seriously.
1 parent e1d8dc2 commit 5c12dcbCopy full SHA for 5c12dcb
Python/pythonrun.c
@@ -698,30 +698,30 @@ _Py_HandleSystemExit(int *exitcode_p)
698
return 0;
699
}
700
701
- PyObject *exception, *value, *tb;
702
- PyErr_Fetch(&exception, &value, &tb);
703
-
704
fflush(stdout);
705
706
int exitcode = 0;
707
- if (value == NULL || value == Py_None) {
+
+ PyObject *exc = PyErr_GetRaisedException();
+ if (exc == NULL) {
708
goto done;
709
+ assert(PyExceptionInstance_Check(exc));
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;
+ /* The error code should be in the `code' attribute. */
+ PyObject *code = PyObject_GetAttr(exc, &_Py_ID(code));
+ if (code) {
+ Py_SETREF(exc, code);
+ if (exc == Py_None) {
+ goto done;
718
719
- /* If we failed to dig out the 'code' attribute,
720
- just let the else clause below print the error. */
721
+ /* If we failed to dig out the 'code' attribute,
+ * just let the else clause below print the error.
+ */
722
723
- if (PyLong_Check(value)) {
724
- exitcode = (int)PyLong_AsLong(value);
+ if (PyLong_Check(exc)) {
+ exitcode = (int)PyLong_AsLong(exc);
725
726
else {
727
PyThreadState *tstate = _PyThreadState_GET();
@@ -732,20 +732,17 @@ _Py_HandleSystemExit(int *exitcode_p)
732
*/
733
PyErr_Clear();
734
if (sys_stderr != NULL && sys_stderr != Py_None) {
735
- PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
+ PyFile_WriteObject(exc, sys_stderr, Py_PRINT_RAW);
736
} else {
737
- PyObject_Print(value, stderr, Py_PRINT_RAW);
+ PyObject_Print(exc, stderr, Py_PRINT_RAW);
738
fflush(stderr);
739
740
PySys_WriteStderr("\n");
741
exitcode = 1;
742
743
744
- done:
745
- /* Cleanup the exception */
746
- Py_CLEAR(exception);
747
- Py_CLEAR(value);
748
- Py_CLEAR(tb);
+done:
+ Py_CLEAR(exc);
749
*exitcode_p = exitcode;
750
return 1;
751
@@ -1641,35 +1638,29 @@ PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyO 10000 bject *globals,
1641
1638
1642
1639
1643
1640
1644
1645
static void
1646
-flush_io(void)
+flush_io_stream(PyThreadState *tstate, PyObject *name)
1647
{
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));
+ PyObject *f = _PySys_GetAttr(tstate, name);
1664
if (f != NULL) {
1665
1666
+ PyObject *r = _PyObject_CallMethodNoArgs(f, &_Py_ID(flush));
+ if (r) {
1667
Py_DECREF(r);
1668
+ }
+ else {
1669
1670
+}
1671
1672
- PyErr_Restore(type, value, traceback);
+static void
+flush_io(void)
+{
+ PyThreadState *tstate = _PyThreadState_GET();
+ PyObject *exc = _PyErr_GetRaisedException(tstate);
+ flush_io_stream(tstate, &_Py_ID(stderr));
+ flush_io_stream(tstate, &_Py_ID(stdout));
+ _PyErr_SetRaisedException(tstate, exc);
1673
1674
1675
static PyObject *