Skip to content

Commit

Permalink
pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient al…
Browse files Browse the repository at this point in the history
…ternatives (python#102769)
  • Loading branch information
iritkatriel authored and warsaw committed Apr 11, 2023
1 parent bb6764d commit e8188bd
Showing 1 changed file with 26 additions and 41 deletions.
67 changes: 26 additions & 41 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,17 +666,15 @@ _PyErr_ChainExceptions(PyObject *typ, PyObject *val, PyObject *tb)
}

if (_PyErr_Occurred(tstate)) {
PyObject *typ2, *val2, *tb2;
_PyErr_Fetch(tstate, &typ2, &val2, &tb2);
_PyErr_NormalizeException(tstate, &typ, &val, &tb);
if (tb != NULL) {
PyException_SetTraceback(val, tb);
Py_DECREF(tb);
}
Py_DECREF(typ);
_PyErr_NormalizeException(tstate, &typ2, &val2, &tb2);
PyException_SetContext(val2, val);
_PyErr_Restore(tstate, typ2, val2, tb2);
PyObject *exc2 = _PyErr_GetRaisedException(tstate);
PyException_SetContext(exc2, val);
_PyErr_SetRaisedException(tstate, exc2);
}
else {
_PyErr_Restore(tstate, typ, val, tb);
Expand Down Expand Up @@ -757,27 +755,15 @@ static PyObject *
_PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception,
const char *format, va_list vargs)
{
PyObject *exc, *val, *val2, *tb;

assert(_PyErr_Occurred(tstate));
_PyErr_Fetch(tstate, &exc, &val, &tb);
_PyErr_NormalizeException(tstate, &exc, &val, &tb);
if (tb != NULL) {
PyException_SetTraceback(val, tb);
Py_DECREF(tb);
}
Py_DECREF(exc);
PyObject *exc = _PyErr_GetRaisedException(tstate);
assert(!_PyErr_Occurred(tstate));

_PyErr_FormatV(tstate, exception, format, vargs);

_PyErr_Fetch(tstate, &exc, &val2, &tb);
_PyErr_NormalizeException(tstate, &exc, &val2, &tb);
PyException_SetCause(val2, Py_NewRef(val));
PyException_SetContext(val2, Py_NewRef(val));
Py_DECREF(val);
_PyErr_Restore(tstate, exc, val2, tb);

PyObject *exc2 = _PyErr_GetRaisedException(tstate);
PyException_SetCause(exc2, Py_NewRef(exc));
PyException_SetContext(exc2, Py_NewRef(exc));
Py_DECREF(exc);
_PyErr_SetRaisedException(tstate, exc2);
return NULL;
}

Expand Down Expand Up @@ -1698,19 +1684,18 @@ static void
PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
int end_lineno, int end_col_offset)
{
PyObject *exc, *v, *tb, *tmp;
PyThreadState *tstate = _PyThreadState_GET();

/* add attributes for the line number and filename for the error */
_PyErr_Fetch(tstate, &exc, &v, &tb);
_PyErr_NormalizeException(tstate, &exc, &v, &tb);
PyObject *exc = _PyErr_GetRaisedException(tstate);
/* XXX check that it is, indeed, a syntax error. It might not
* be, though. */
tmp = PyLong_FromLong(lineno);
if (tmp == NULL)
PyObject *tmp = PyLong_FromLong(lineno);
if (tmp == NULL) {
_PyErr_Clear(tstate);
}
else {
if (PyObject_SetAttr(v, &_Py_ID(lineno), tmp)) {
if (PyObject_SetAttr(exc, &_Py_ID(lineno), tmp)) {
_PyErr_Clear(tstate);
}
Py_DECREF(tmp);
Expand All @@ -1722,7 +1707,7 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
_PyErr_Clear(tstate);
}
}
if (PyObject_SetAttr(v, &_Py_ID(offset), tmp ? tmp : Py_None)) {
if (PyObject_SetAttr(exc, &_Py_ID(offset), tmp ? tmp : Py_None)) {
_PyErr_Clear(tstate);
}
Py_XDECREF(tmp);
Expand All @@ -1734,7 +1719,7 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
_PyErr_Clear(tstate);
}
}
if (PyObject_SetAttr(v, &_Py_ID(end_lineno), tmp ? tmp : Py_None)) {
if (PyObject_SetAttr(exc, &_Py_ID(end_lineno), tmp ? tmp : Py_None)) {
_PyErr_Clear(tstate);
}
Py_XDECREF(tmp);
Expand All @@ -1746,20 +1731,20 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
_PyErr_Clear(tstate);
}
}
if (PyObject_SetAttr(v, &_Py_ID(end_offset), tmp ? tmp : Py_None)) {
if (PyObject_SetAttr(exc, &_Py_ID(end_offset), tmp ? tmp : Py_None)) {
_PyErr_Clear(tstate);
}
Py_XDECREF(tmp);

tmp = NULL;
if (filename != NULL) {
if (PyObject_SetAttr(v, &_Py_ID(filename), filename)) {
if (PyObject_SetAttr(exc, &_Py_ID(filename), filename)) {
_PyErr_Clear(tstate);
}

tmp = PyErr_ProgramTextObject(filename, lineno);
if (tmp) {
if (PyObject_SetAttr(v, &_Py_ID(text), tmp)) {
if (PyObject_SetAttr(exc, &_Py_ID(text), tmp)) {
_PyErr_Clear(tstate);
}
Py_DECREF(tmp);
Expand All @@ -1768,17 +1753,17 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
_PyErr_Clear(tstate);
}
}
if (exc != PyExc_SyntaxError) {
if (_PyObject_LookupAttr(v, &_Py_ID(msg), &tmp) < 0) {
if ((PyObject *)Py_TYPE(exc) != PyExc_SyntaxError) {
if (_PyObject_LookupAttr(exc, &_Py_ID(msg), &tmp) < 0) {
_PyErr_Clear(tstate);
}
else if (tmp) {
Py_DECREF(tmp);
}
else {
tmp = PyObject_Str(v);
tmp = PyObject_Str(exc);
if (tmp) {
if (PyObject_SetAttr(v, &_Py_ID(msg), tmp)) {
if (PyObject_SetAttr(exc, &_Py_ID(msg), tmp)) {
_PyErr_Clear(tstate);
}
Py_DECREF(tmp);
Expand All @@ -1788,19 +1773,19 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
}
}

if (_PyObject_LookupAttr(v, &_Py_ID(print_file_and_line), &tmp) < 0) {
if (_PyObject_LookupAttr(exc, &_Py_ID(print_file_and_line), &tmp) < 0) {
_PyErr_Clear(tstate);
}
else if (tmp) {
Py_DECREF(tmp);
}
else {
if (PyObject_SetAttr(v, &_Py_ID(print_file_and_line), Py_None)) {
if (PyObject_SetAttr(exc, &_Py_ID(print_file_and_line), Py_None)) {
_PyErr_Clear(tstate);
}
}
}
_PyErr_Restore(tstate, exc, v, tb);
_PyErr_SetRaisedException(tstate, exc);
}

void
Expand Down

0 comments on commit e8188bd

Please sign in to comment.