Skip to content

Commit d220a9f

Browse files
committed
gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives
1 parent 675b97a commit d220a9f

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

Python/pythonrun.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -698,30 +698,31 @@ _Py_HandleSystemExit(int *exitcode_p)
698698
return 0;
699699
}
700700

701-
PyObject *exception, *value, *tb;
702-
PyErr_Fetch(&exception, &value, &tb);
703-
704701
fflush(stdout);
705702

706703
int exitcode = 0;
707-
if (value == NULL || value == Py_None) {
704+
705+
PyObject *exc = PyErr_GetRaisedException();
706+
assert(exc != Py_None);
707+
if (exc == NULL) {
708708
goto done;
709709
}
710710

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+
assert(PyExceptionInstance_Check(exc));
712+
/* The error code should be in the `code' attribute. */
713+
PyObject *code = PyObject_GetAttr(exc, &_Py_ID(code));
714+
if (code) {
715+
Py_SETREF(exc, code);
716+
if (exc == Py_None) {
717+
goto done;
718718
}
719-
/* If we failed to dig out the 'code' attribute,
720-
just let the else clause below print the error. */
721719
}
720+
/* If we failed to dig out the 'code' attribute,
721+
* just let the else clause below print the error.
722+
*/
722723

723-
if (PyLong_Check(value)) {
724-
exitcode = (int)PyLong_AsLong(value);
724+
if (PyLong_Check(exc)) {
725+
exitcode = (int)PyLong_AsLong(exc);
725726
}
726727
else {
727728
PyThreadState *tstate = _PyThreadState_GET();
@@ -732,20 +733,17 @@ _Py_HandleSystemExit(int *exitcode_p)
732733
*/
733734
PyErr_Clear();
734735
if (sys_stderr != NULL && sys_stderr != Py_None) {
735-
PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
736+
PyFile_WriteObject(exc, sys_stderr, Py_PRINT_RAW);
736737
} else {
737-
PyObject_Print(value, stderr, Py_PRINT_RAW);
738+
PyObject_Print(exc, stderr, Py_PRINT_RAW);
738739
fflush(stderr);
739740
}
740741
PySys_WriteStderr("\n");
741742
exitcode = 1;
742743
}
743744

744-
done:
745-
/* Cleanup the exception */
746-
Py_CLEAR(exception);
747-
Py_CLEAR(value);
748-
Py_CLEAR(tb);
745+
done:
746+
Py_CLEAR(exc);
749747
*exitcode_p = exitcode;
750748
return 1;
751749
}

0 commit comments

Comments
 (0)