Skip to content

Commit e8188bd

Browse files
iritkatrielwarsaw
authored andcommitted
pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (python#102769)
1 parent bb6764d commit e8188bd

File tree

1 file changed

+26
-41
lines changed

1 file changed

+26
-41
lines changed

Python/errors.c

+26-41
Original file line numberDiff line numberDiff line change
@@ -666,17 +666,15 @@ _PyErr_ChainExceptions(PyObject *typ, PyObject *val, PyObject *tb)
666666
}
667667

668668
if (_PyErr_Occurred(tstate)) {
669-
PyObject *typ2, *val2, *tb2;
670-
_PyErr_Fetch(tstate, &typ2, &val2, &tb2);
671669
_PyErr_NormalizeException(tstate, &typ, &val, &tb);
672670
if (tb != NULL) {
673671
PyException_SetTraceback(val, tb);
674672
Py_DECREF(tb);
675673
}
676674
Py_DECREF(typ);
677-
_PyErr_NormalizeException(tstate, &typ2, &val2, &tb2);
678-
PyException_SetContext(val2, val);
679-
_PyErr_Restore(tstate, typ2, val2, tb2);
675+
PyObject *exc2 = _PyErr_GetRaisedException(tstate);
676+
PyException_SetContext(exc2, val);
677+
_PyErr_SetRaisedException(tstate, exc2);
680678
}
681679
else {
682680
_PyErr_Restore(tstate, typ, val, tb);
@@ -757,27 +755,15 @@ static PyObject *
757755
_PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception,
758756
const char *format, va_list vargs)
759757
{
760-
PyObject *exc, *val, *val2, *tb;
761-
762758
assert(_PyErr_Occurred(tstate));
763-
_PyErr_Fetch(tstate, &exc, &val, &tb);
764-
_PyErr_NormalizeException(tstate, &exc, &val, &tb);
765-
if (tb != NULL) {
766-
PyException_SetTraceback(val, tb);
767-
Py_DECREF(tb);
768-
}
769-
Py_DECREF(exc);
759+
PyObject *exc = _PyErr_GetRaisedException(tstate);
770760
assert(!_PyErr_Occurred(tstate));
771-
772761
_PyErr_FormatV(tstate, exception, format, vargs);
773-
774-
_PyErr_Fetch(tstate, &exc, &val2, &tb);
775-
_PyErr_NormalizeException(tstate, &exc, &val2, &tb);
776-
PyException_SetCause(val2, Py_NewRef(val));
777-
PyException_SetContext(val2, Py_NewRef(val));
778-
Py_DECREF(val);
779-
_PyErr_Restore(tstate, exc, val2, tb);
780-
762+
PyObject *exc2 = _PyErr_GetRaisedException(tstate);
763+
PyException_SetCause(exc2, Py_NewRef(exc));
764+
PyException_SetContext(exc2, Py_NewRef(exc));
765+
Py_DECREF(exc);
766+
_PyErr_SetRaisedException(tstate, exc2);
781767
return NULL;
782768
}
783769

@@ -1698,19 +1684,18 @@ static void
16981684
PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
16991685
int end_lineno, int end_col_offset)
17001686
{
1701-
PyObject *exc, *v, *tb, *tmp;
17021687
PyThreadState *tstate = _PyThreadState_GET();
17031688

17041689
/* add attributes for the line number and filename for the error */
1705-
_PyErr_Fetch(tstate, &exc, &v, &tb);
1706-
_PyErr_NormalizeException(tstate, &exc, &v, &tb);
1690+
PyObject *exc = _PyErr_GetRaisedException(tstate);
17071691
/* XXX check that it is, indeed, a syntax error. It might not
17081692
* be, though. */
1709-
tmp = PyLong_FromLong(lineno);
1710-
if (tmp == NULL)
1693+
PyObject *tmp = PyLong_FromLong(lineno);
1694+
if (tmp == NULL) {
17111695
_PyErr_Clear(tstate);
1696+
}
17121697
else {
1713-
if (PyObject_SetAttr(v, &_Py_ID(lineno), tmp)) {
1698+
if (PyObject_SetAttr(exc, &_Py_ID(lineno), tmp)) {
17141699
_PyErr_Clear(tstate);
17151700
}
17161701
Py_DECREF(tmp);
@@ -1722,7 +1707,7 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
17221707
_PyErr_Clear(tstate);
17231708
}
17241709
}
1725-
if (PyObject_SetAttr(v, &_Py_ID(offset), tmp ? tmp : Py_None)) {
1710+
if (PyObject_SetAttr(exc, &_Py_ID(offset), tmp ? tmp : Py_None)) {
17261711
_PyErr_Clear(tstate);
17271712
}
17281713
Py_XDECREF(tmp);
@@ -1734,7 +1719,7 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
17341719
_PyErr_Clear(tstate);
17351720
}
17361721
}
1737-
if (PyObject_SetAttr(v, &_Py_ID(end_lineno), tmp ? tmp : Py_None)) {
1722+
if (PyObject_SetAttr(exc, &_Py_ID(end_lineno), tmp ? tmp : Py_None)) {
17381723
_PyErr_Clear(tstate);
17391724
}
17401725
Py_XDECREF(tmp);
@@ -1746,20 +1731,20 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
17461731
_PyErr_Clear(tstate);
17471732
}
17481733
}
1749-
if (PyObject_SetAttr(v, &_Py_ID(end_offset), tmp ? tmp : Py_None)) {
1734+
if (PyObject_SetAttr(exc, &_Py_ID(end_offset), tmp ? tmp : Py_None)) {
17501735
_PyErr_Clear(tstate);
17511736
}
17521737
Py_XDECREF(tmp);
17531738

17541739
tmp = NULL;
17551740
if (filename != NULL) {
1756-
if (PyObject_SetAttr(v, &_Py_ID(filename), filename)) {
1741+
if (PyObject_SetAttr(exc, &_Py_ID(filename), filename)) {
17571742
_PyErr_Clear(tstate);
17581743
}
17591744

17601745
tmp = PyErr_ProgramTextObject(filename, lineno);
17611746
if (tmp) {
1762-
if (PyObject_SetAttr(v, &_Py_ID(text), tmp)) {
1747+
if (PyObject_SetAttr(exc, &_Py_ID(text), tmp)) {
17631748
_PyErr_Clear(tstate);
17641749
}
17651750
Py_DECREF(tmp);
@@ -1768,17 +1753,17 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
17681753
_PyErr_Clear(tstate);
17691754
}
17701755
}
1771-
if (exc != PyExc_SyntaxError) {
1772-
if (_PyObject_LookupAttr(v, &_Py_ID(msg), &tmp) < 0) {
1756+
if ((PyObject *)Py_TYPE(exc) != PyExc_SyntaxError) {
1757+
if (_PyObject_LookupAttr(exc, &_Py_ID(msg), &tmp) < 0) {
17731758
_PyErr_Clear(tstate);
17741759
}
17751760
else if (tmp) {
17761761
Py_DECREF(tmp);
17771762
}
17781763
else {
1779-
tmp = PyObject_Str(v);
1764+
tmp = PyObject_Str(exc);
17801765
if (tmp) {
1781-
if (PyObject_SetAttr(v, &_Py_ID(msg), tmp)) {
1766+
if (PyObject_SetAttr(exc, &_Py_ID(msg), tmp)) {
17821767
_PyErr_Clear(tstate);
17831768
}
17841769
Py_DECREF(tmp);
@@ -1788,19 +1773,19 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
17881773
}
17891774
}
17901775

1791-
if (_PyObject_LookupAttr(v, &_Py_ID(print_file_and_line), &tmp) < 0) {
1776+
if (_PyObject_LookupAttr(exc, &_Py_ID(print_file_and_line), &tmp) < 0) {
17921777
_PyErr_Clear(tstate);
17931778
}
17941779
else if (tmp) {
17951780
Py_DECREF(tmp);
17961781
}
17971782
else {
1798-
if (PyObject_SetAttr(v, &_Py_ID(print_file_and_line), Py_None)) {
1783+
if (PyObject_SetAttr(exc, &_Py_ID(print_file_and_line), Py_None)) {
17991784
_PyErr_Clear(tstate);
18001785
}
18011786
}
18021787
}
1803-
_PyErr_Restore(tstate, exc, v, tb);
1788+
_PyErr_SetRaisedException(tstate, exc);
18041789
}
18051790

18061791
void

0 commit comments

Comments
 (0)