gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (in Modules/) (#102196)

This commit is contained in:
Irit Katriel 2023-02-24 21:43:03 +00:00 committed by GitHub
parent 568fc0dee4
commit 2db23d10bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 136 additions and 169 deletions

View file

@ -1422,7 +1422,6 @@ _asyncio_Future__make_cancelled_error_impl(FutureObj *self)
static void static void
FutureObj_finalize(FutureObj *fut) FutureObj_finalize(FutureObj *fut)
{ {
PyObject *error_type, *error_value, *error_traceback;
PyObject *context; PyObject *context;
PyObject *message = NULL; PyObject *message = NULL;
PyObject *func; PyObject *func;
@ -1434,7 +1433,7 @@ FutureObj_finalize(FutureObj *fut)
fut->fut_log_tb = 0; fut->fut_log_tb = 0;
/* Save the current exception, if any. */ /* Save the current exception, if any. */
PyErr_Fetch(&error_type, &error_value, &error_traceback); PyObject *exc = PyErr_GetRaisedException();
context = PyDict_New(); context = PyDict_New();
if (context == NULL) { if (context == NULL) {
@ -1476,7 +1475,7 @@ finally:
Py_XDECREF(message); Py_XDECREF(message);
/* Restore the saved exception. */ /* Restore the saved exception. */
PyErr_Restore(error_type, error_value, error_traceback); PyErr_SetRaisedException(exc);
} }
static PyMethodDef FutureType_methods[] = { static PyMethodDef FutureType_methods[] = {
@ -2491,14 +2490,13 @@ TaskObj_finalize(TaskObj *task)
PyObject *context; PyObject *context;
PyObject *message = NULL; PyObject *message = NULL;
PyObject *func; PyObject *func;
PyObject *error_type, *error_value, *error_traceback;
if (task->task_state != STATE_PENDING || !task->task_log_destroy_pending) { if (task->task_state != STATE_PENDING || !task->task_log_destroy_pending) {
goto done; goto done;
} }
/* Save the current exception, if any. */ /* Save the current exception, if any. */
PyErr_Fetch(&error_type, &error_value, &error_traceback); PyObject *exc = PyErr_GetRaisedException();
context = PyDict_New(); context = PyDict_New();
if (context == NULL) { if (context == NULL) {
@ -2541,7 +2539,7 @@ finally:
Py_XDECREF(message); Py_XDECREF(message);
/* Restore the saved exception. */ /* Restore the saved exception. */
PyErr_Restore(error_type, error_value, error_traceback); PyErr_SetRaisedException(exc);
done: done:
FutureObj_finalize((FutureObj*)task); FutureObj_finalize((FutureObj*)task);
@ -2766,8 +2764,6 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc)
} }
if (gen_status == PYGEN_RETURN || gen_status == PYGEN_ERROR) { if (gen_status == PYGEN_RETURN || gen_status == PYGEN_ERROR) {
PyObject *et, *ev, *tb;
if (result != NULL) { if (result != NULL) {
/* The error is StopIteration and that means that /* The error is StopIteration and that means that
the underlying coroutine has resolved */ the underlying coroutine has resolved */
@ -2794,52 +2790,39 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc)
if (PyErr_ExceptionMatches(state->asyncio_CancelledError)) { if (PyErr_ExceptionMatches(state->asyncio_CancelledError)) {
/* CancelledError */ /* CancelledError */
PyErr_Fetch(&et, &ev, &tb);
assert(et); PyObject *exc = PyErr_GetRaisedException();
PyErr_NormalizeException(&et, &ev, &tb); assert(exc);
if (tb != NULL) {
PyException_SetTraceback(ev, tb);
Py_DECREF(tb);
}
Py_XDECREF(et);
FutureObj *fut = (FutureObj*)task; FutureObj *fut = (FutureObj*)task;
/* transfer ownership */ /* transfer ownership */
fut->fut_cancelled_exc = ev; fut->fut_cancelled_exc = exc;
return future_cancel(state, fut, NULL); return future_cancel(state, fut, NULL);
} }
/* Some other exception; pop it and call Task.set_exception() */ /* Some other exception; pop it and call Task.set_exception() */
PyErr_Fetch(&et, &ev, &tb); PyObject *exc = PyErr_GetRaisedException();
assert(et); assert(exc);
PyErr_NormalizeException(&et, &ev, &tb);
if (tb != NULL) {
PyException_SetTraceback(ev, tb);
}
o = future_set_exception(state, (FutureObj*)task, ev); o = future_set_exception(state, (FutureObj*)task, exc);
if (!o) { if (!o) {
/* An exception in Task.set_exception() */ /* An exception in Task.set_exception() */
Py_DECREF(et); Py_DECREF(exc);
Py_XDECREF(tb);
Py_XDECREF(ev);
goto fail; goto fail;
} }
assert(o == Py_None); assert(o == Py_None);
Py_DECREF(o); Py_DECREF(o);
if (PyErr_GivenExceptionMatches(et, PyExc_KeyboardInterrupt) || if (PyErr_GivenExceptionMatches(exc, PyExc_KeyboardInterrupt) ||
PyErr_GivenExceptionMatches(et, PyExc_SystemExit)) PyErr_GivenExceptionMatches(exc, PyExc_SystemExit))
{ {
/* We've got a KeyboardInterrupt or a SystemError; re-raise it */ /* We've got a KeyboardInterrupt or a SystemError; re-raise it */
PyErr_Restore(et, ev, tb); PyErr_SetRaisedException(exc);
goto fail; goto fail;
} }
Py_DECREF(et); Py_DECREF(exc);
Py_XDECREF(tb);
Py_XDECREF(ev);
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -3059,10 +3042,9 @@ task_step(asyncio_state *state, TaskObj *task, PyObject *exc)
res = task_step_impl(state, task, exc); res = task_step_impl(state, task, exc);
if (res == NULL) { if (res == NULL) {
PyObject *et, *ev, *tb; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&et, &ev, &tb);
leave_task(state, task->task_loop, (PyObject*)task); leave_task(state, task->task_loop, (PyObject*)task);
_PyErr_ChainExceptions(et, ev, tb); /* Normalizes (et, ev, tb) */ _PyErr_ChainExceptions1(exc);
return NULL; return NULL;
} }
else { else {
@ -3079,7 +3061,6 @@ task_step(asyncio_state *state, TaskObj *task, PyObject *exc)
static PyObject * static PyObject *
task_wakeup(TaskObj *task, PyObject *o) task_wakeup(TaskObj *task, PyObject *o)
{ {
PyObject *et, *ev, *tb;
PyObject *result; PyObject *result;
assert(o); assert(o);
@ -3111,18 +3092,12 @@ task_wakeup(TaskObj *task, PyObject *o)
/* exception raised */ /* exception raised */
} }
PyErr_Fetch(&et, &ev, &tb); PyObject *exc = PyErr_GetRaisedException();
assert(et); assert(exc);
PyErr_NormalizeException(&et, &ev, &tb);
if (tb != NULL) {
PyException_SetTraceback(ev, tb);
}
result = task_step(state, task, ev); result = task_step(state, task, exc);
Py_DECREF(et); Py_DECREF(exc);
Py_XDECREF(tb);
Py_XDECREF(ev);
return result; return result;
} }

View file

@ -437,10 +437,9 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
error: error:
if (result != NULL) { if (result != NULL) {
PyObject *exc, *val, *tb, *close_result; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&exc, &val, &tb); PyObject *close_result = PyObject_CallMethodNoArgs(result, &_Py_ID(close));
close_result = PyObject_CallMethodNoArgs(result, &_Py_ID(close)); _PyErr_ChainExceptions1(exc);
_PyErr_ChainExceptions(exc, val, tb);
Py_XDECREF(close_result); Py_XDECREF(close_result);
Py_DECREF(result); Py_DECREF(result);
} }

View file

@ -472,12 +472,13 @@ buffered_closed_get(buffered *self, void *context)
static PyObject * static PyObject *
buffered_close(buffered *self, PyObject *args) buffered_close(buffered *self, PyObject *args)
{ {
PyObject *res = NULL, *exc = NULL, *val, *tb; PyObject *res = NULL;
int r; int r;
CHECK_INITIALIZED(self) CHECK_INITIALIZED(self)
if (!ENTER_BUFFERED(self)) if (!ENTER_BUFFERED(self)) {
return NULL; return NULL;
}
r = buffered_closed(self); r = buffered_closed(self);
if (r < 0) if (r < 0)
@ -497,12 +498,16 @@ buffered_close(buffered *self, PyObject *args)
/* flush() will most probably re-take the lock, so drop it first */ /* flush() will most probably re-take the lock, so drop it first */
LEAVE_BUFFERED(self) LEAVE_BUFFERED(self)
res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush)); res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
if (!ENTER_BUFFERED(self)) if (!ENTER_BUFFERED(self)) {
return NULL; return NULL;
if (res == NULL) }
PyErr_Fetch(&exc, &val, &tb); PyObject *exc = NULL;
else if (res == NULL) {
exc = PyErr_GetRaisedException();
}
else {
Py_DECREF(res); Py_DECREF(res);
}
res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(close)); res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(close));
@ -512,7 +517,7 @@ buffered_close(buffered *self, PyObject *args)
} }
if (exc != NULL) { if (exc != NULL) {
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions1(exc);
Py_CLEAR(res); Py_CLEAR(res);
} }
@ -637,17 +642,14 @@ _set_BlockingIOError(const char *msg, Py_ssize_t written)
static Py_ssize_t * static Py_ssize_t *
_buffered_check_blocking_error(void) _buffered_check_blocking_error(void)
{ {
PyObject *t, *v, *tb; PyObject *exc = PyErr_GetRaisedException();
PyOSErrorObject *err; if (exc == NULL || !PyErr_GivenExceptionMatches(exc, PyExc_BlockingIOError)) {
PyErr_SetRaisedException(exc);
PyErr_Fetch(&t, &v, &tb);
if (v == NULL || !PyErr_GivenExceptionMatches(v, PyExc_BlockingIOError)) {
PyErr_Restore(t, v, tb);
return NULL; return NULL;
} }
err = (PyOSErrorObject *) v; PyOSErrorObject *err = (PyOSErrorObject *)exc;
/* TODO: sanity check (err->written >= 0) */ /* TODO: sanity check (err->written >= 0) */
PyErr_Restore(t, v, tb); PyErr_SetRaisedException(exc);
return &err->written; return &err->written;
} }
@ -749,13 +751,11 @@ _buffered_init(buffered *self)
int int
_PyIO_trap_eintr(void) _PyIO_trap_eintr(void)
{ {
PyObject *typ, *val, *tb; if (!PyErr_ExceptionMatches(PyExc_OSError)) {
PyOSErrorObject *env_err;
if (!PyErr_ExceptionMatches(PyExc_OSError))
return 0; return 0;
PyErr_Fetch(&typ, &val, &tb); }
PyErr_NormalizeException(&typ, &val, &tb); PyObject *exc = PyErr_GetRaisedException();
env_err = (PyOSErrorObject *) val; PyOSErrorObject *env_err = (PyOSErrorObject *)exc;
assert(env_err != NULL); assert(env_err != NULL);
if (env_err->myerrno != NULL) { if (env_err->myerrno != NULL) {
assert(EINTR > 0 && EINTR < INT_MAX); assert(EINTR > 0 && EINTR < INT_MAX);
@ -764,14 +764,12 @@ _PyIO_trap_eintr(void)
int myerrno = PyLong_AsLongAndOverflow(env_err->myerrno, &overflow); int myerrno = PyLong_AsLongAndOverflow(env_err->myerrno, &overflow);
PyErr_Clear(); PyErr_Clear();
if (myerrno == EINTR) { if (myerrno == EINTR) {
Py_DECREF(typ); Py_DECREF(exc);
Py_DECREF(val);
Py_XDECREF(tb);
return 1; return 1;
} }
} }
/* This silences any error set by PyObject_RichCompareBool() */ /* This silences any error set by PyObject_RichCompareBool() */
PyErr_Restore(typ, val, tb); PyErr_SetRaisedException(exc);
return 0; return 0;
} }
@ -2228,15 +2226,17 @@ bufferedrwpair_writable(rwpair *self, PyObject *Py_UNUSED(ignored))
static PyObject * static PyObject *
bufferedrwpair_close(rwpair *self, PyObject *Py_UNUSED(ignored)) bufferedrwpair_close(rwpair *self, PyObject *Py_UNUSED(ignored))
{ {
PyObject *exc = NULL, *val, *tb; PyObject *exc = NULL;
PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL); PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL);
if (ret == NULL) if (ret == NULL) {
PyErr_Fetch(&exc, &val, &tb); exc = PyErr_GetRaisedException();
else }
else {
Py_DECREF(ret); Py_DECREF(ret);
}
ret = _forward_call(self->reader, &_Py_ID(close), NULL); ret = _forward_call(self->reader, &_Py_ID(close), NULL);
if (exc != NULL) { if (exc != NULL) {
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions1(exc);
Py_CLEAR(ret); Py_CLEAR(ret);
} }
return ret; return ret;

View file

@ -88,14 +88,13 @@ static PyObject *
fileio_dealloc_warn(fileio *self, PyObject *source) fileio_dealloc_warn(fileio *self, PyObject *source)
{ {
if (self->fd >= 0 && self->closefd) { if (self->fd >= 0 && self->closefd) {
PyObject *exc, *val, *tb; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&exc, &val, &tb);
if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) { if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) {
/* Spurious errors can appear at shutdown */ /* Spurious errors can appear at shutdown */
if (PyErr_ExceptionMatches(PyExc_Warning)) if (PyErr_ExceptionMatches(PyExc_Warning))
PyErr_WriteUnraisable((PyObject *) self); PyErr_WriteUnraisable((PyObject *) self);
} }
PyErr_Restore(exc, val, tb); PyErr_SetRaisedException(exc);
} }
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -140,7 +139,7 @@ _io_FileIO_close_impl(fileio *self)
/*[clinic end generated code: output=7737a319ef3bad0b input=f35231760d54a522]*/ /*[clinic end generated code: output=7737a319ef3bad0b input=f35231760d54a522]*/
{ {
PyObject *res; PyObject *res;
PyObject *exc, *val, *tb; PyObject *exc;
int rc; int rc;
res = PyObject_CallMethodOneArg((PyObject*)&PyRawIOBase_Type, res = PyObject_CallMethodOneArg((PyObject*)&PyRawIOBase_Type,
&_Py_ID(close), (PyObject *)self); &_Py_ID(close), (PyObject *)self);
@ -148,20 +147,25 @@ _io_FileIO_close_impl(fileio *self)
self->fd = -1; self->fd = -1;
return res; return res;
} }
if (res == NULL) if (res == NULL) {
PyErr_Fetch(&exc, &val, &tb); exc = PyErr_GetRaisedException();
}
if (self->finalizing) { if (self->finalizing) {
PyObject *r = fileio_dealloc_warn(self, (PyObject *) self); PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
if (r) if (r) {
Py_DECREF(r); Py_DECREF(r);
else }
else {
PyErr_Clear(); PyErr_Clear();
}
} }
rc = internal_close(self); rc = internal_close(self);
if (res == NULL) if (res == NULL) {
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions1(exc);
if (rc < 0) }
if (rc < 0) {
Py_CLEAR(res); Py_CLEAR(res);
}
return res; return res;
} }
@ -487,10 +491,9 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
if (!fd_is_own) if (!fd_is_own)
self->fd = -1; self->fd = -1;
if (self->fd >= 0) { if (self->fd >= 0) {
PyObject *exc, *val, *tb; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&exc, &val, &tb);
internal_close(self); internal_close(self);
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions1(exc);
} }
done: done:

View file

@ -220,7 +220,6 @@ static PyObject *
_io__IOBase_close_impl(PyObject *self) _io__IOBase_close_impl(PyObject *self)
/*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/ /*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/
{ {
PyObject *res, *exc, *val, *tb;
int rc, closed = iobase_is_closed(self); int rc, closed = iobase_is_closed(self);
if (closed < 0) { if (closed < 0) {
@ -230,11 +229,11 @@ _io__IOBase_close_impl(PyObject *self)
Py_RETURN_NONE; Py_RETURN_NONE;
} }
res = PyObject_CallMethodNoArgs(self, &_Py_ID(flush)); PyObject *res = PyObject_CallMethodNoArgs(self, &_Py_ID(flush));
PyErr_Fetch(&exc, &val, &tb); PyObject *exc = PyErr_GetRaisedException();
rc = PyObject_SetAttr(self, &_Py_ID(__IOBase_closed), Py_True); rc = PyObject_SetAttr(self, &_Py_ID(__IOBase_closed), Py_True);
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions1(exc);
if (rc < 0) { if (rc < 0) {
Py_CLEAR(res); Py_CLEAR(res);
} }
@ -252,11 +251,10 @@ static void
iobase_finalize(PyObject *self) iobase_finalize(PyObject *self)
{ {
PyObject *res; PyObject *res;
PyObject *error_type, *error_value, *error_traceback;
int closed; int closed;
/* Save the current exception, if any. */ /* Save the current exception, if any. */
PyErr_Fetch(&error_type, &error_value, &error_traceback); PyObject *exc = PyErr_GetRaisedException();
/* If `closed` doesn't exist or can't be evaluated as bool, then the /* If `closed` doesn't exist or can't be evaluated as bool, then the
object is probably in an unusable state, so ignore. */ object is probably in an unusable state, so ignore. */
@ -297,7 +295,7 @@ iobase_finalize(PyObject *self)
} }
/* Restore the saved exception. */ /* Restore the saved exception. */
PyErr_Restore(error_type, error_value, error_traceback); PyErr_SetRaisedException(exc);
} }
int int

View file

@ -2827,11 +2827,10 @@ finally:
fail: fail:
if (saved_state) { if (saved_state) {
PyObject *type, *value, *traceback; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&type, &value, &traceback);
res = PyObject_CallMethodOneArg( res = PyObject_CallMethodOneArg(
self->decoder, &_Py_ID(setstate), saved_state); self->decoder, &_Py_ID(setstate), saved_state);
_PyErr_ChainExceptions(type, value, traceback); _PyErr_ChainExceptions1(exc);
Py_DECREF(saved_state); Py_DECREF(saved_state);
Py_XDECREF(res); Py_XDECREF(res);
} }
@ -3028,24 +3027,28 @@ _io_TextIOWrapper_close_impl(textio *self)
Py_RETURN_NONE; /* stream already closed */ Py_RETURN_NONE; /* stream already closed */
} }
else { else {
PyObject *exc = NULL, *val, *tb; PyObject *exc = NULL;
if (self->finalizing) { if (self->finalizing) {
res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(_dealloc_warn), res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(_dealloc_warn),
(PyObject *)self); (PyObject *)self);
if (res) if (res) {
Py_DECREF(res); Py_DECREF(res);
else }
else {
PyErr_Clear(); PyErr_Clear();
}
} }
res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush)); res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
if (res == NULL) if (res == NULL) {
PyErr_Fetch(&exc, &val, &tb); exc = PyErr_GetRaisedException();
else }
else {
Py_DECREF(res); Py_DECREF(res);
}
res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(close)); res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(close));
if (exc != NULL) { if (exc != NULL) {
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions1(exc);
Py_CLEAR(res); Py_CLEAR(res);
} }
return res; return res;

View file

@ -192,7 +192,7 @@ _io__WindowsConsoleIO_close_impl(winconsoleio *self)
/*[clinic end generated code: output=27ef95b66c29057b input=68c4e5754f8136c2]*/ /*[clinic end generated code: output=27ef95b66c29057b input=68c4e5754f8136c2]*/
{ {
PyObject *res; PyObject *res;
PyObject *exc, *val, *tb; PyObject *exc;
int rc; int rc;
res = PyObject_CallMethodOneArg((PyObject*)&PyRawIOBase_Type, res = PyObject_CallMethodOneArg((PyObject*)&PyRawIOBase_Type,
&_Py_ID(close), (PyObject*)self); &_Py_ID(close), (PyObject*)self);
@ -200,13 +200,16 @@ _io__WindowsConsoleIO_close_impl(winconsoleio *self)
self->fd = -1; self->fd = -1;
return res; return res;
} }
if (res == NULL) if (res == NULL) {
PyErr_Fetch(&exc, &val, &tb); exc = PyErr_GetRaisedException();
}
rc = internal_close(self); rc = internal_close(self);
if (res == NULL) if (res == NULL) {
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions1(exc);
if (rc < 0) }
if (rc < 0) {
Py_CLEAR(res); Py_CLEAR(res);
}
return res; return res;
} }

View file

@ -348,8 +348,7 @@ ptrace_enter_call(PyObject *self, void *key, PyObject *userObj)
* exception, and some of the code under here assumes that * exception, and some of the code under here assumes that
* PyErr_* is its own to mess around with, so we have to * PyErr_* is its own to mess around with, so we have to
* save and restore any current exception. */ * save and restore any current exception. */
PyObject *last_type, *last_value, *last_tb; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&last_type, &last_value, &last_tb);
profEntry = getEntry(pObj, key); profEntry = getEntry(pObj, key);
if (profEntry == NULL) { if (profEntry == NULL) {
@ -374,7 +373,7 @@ ptrace_enter_call(PyObject *self, void *key, PyObject *userObj)
initContext(pObj, pContext, profEntry); initContext(pObj, pContext, profEntry);
restorePyerr: restorePyerr:
PyErr_Restore(last_type, last_value, last_tb); PyErr_SetRaisedException(exc);
} }
static void static void

View file

@ -908,7 +908,6 @@ final_callback(sqlite3_context *context)
PyObject* function_result; PyObject* function_result;
PyObject** aggregate_instance; PyObject** aggregate_instance;
int ok; int ok;
PyObject *exception, *value, *tb;
aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0); aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
if (aggregate_instance == NULL) { if (aggregate_instance == NULL) {
@ -923,7 +922,7 @@ final_callback(sqlite3_context *context)
} }
// Keep the exception (if any) of the last call to step, value, or inverse // Keep the exception (if any) of the last call to step, value, or inverse
PyErr_Fetch(&exception, &value, &tb); PyObject *exc = PyErr_GetRaisedException();
callback_context *ctx = (callback_context *)sqlite3_user_data(context); callback_context *ctx = (callback_context *)sqlite3_user_data(context);
assert(ctx != NULL); assert(ctx != NULL);
@ -938,7 +937,7 @@ final_callback(sqlite3_context *context)
} }
if (!ok) { if (!ok) {
int attr_err = PyErr_ExceptionMatches(PyExc_AttributeError); int attr_err = PyErr_ExceptionMatches(PyExc_AttributeError);
_PyErr_ChainExceptions(exception, value, tb); _PyErr_ChainExceptions1(exc);
/* Note: contrary to the step, value, and inverse callbacks, SQLite /* Note: contrary to the step, value, and inverse callbacks, SQLite
* does _not_, as of SQLite 3.38.0, propagate errors to sqlite3_step() * does _not_, as of SQLite 3.38.0, propagate errors to sqlite3_step()
@ -949,7 +948,7 @@ final_callback(sqlite3_context *context)
: "user-defined aggregate's 'finalize' method raised error"); : "user-defined aggregate's 'finalize' method raised error");
} }
else { else {
PyErr_Restore(exception, value, tb); PyErr_SetRaisedException(exc);
} }
error: error:
@ -2274,15 +2273,14 @@ pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
if (commit) { if (commit) {
/* Commit failed; try to rollback in order to unlock the database. /* Commit failed; try to rollback in order to unlock the database.
* If rollback also fails, chain the exceptions. */ * If rollback also fails, chain the exceptions. */
PyObject *exc, *val, *tb; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&exc, &val, &tb);
result = pysqlite_connection_rollback_impl(self); result = pysqlite_connection_rollback_impl(self);
if (result == NULL) { if (result == NULL) {
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions1(exc);
} }
else { else {
Py_DECREF(result); Py_DECREF(result);
PyErr_Restore(exc, val, tb); PyErr_SetRaisedException(exc);
} }
} }
return NULL; return NULL;

View file

@ -705,11 +705,10 @@ bind_parameters(pysqlite_state *state, pysqlite_Statement *self,
Py_DECREF(adapted); Py_DECREF(adapted);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
PyObject *exc, *val, *tb; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&exc, &val, &tb);
sqlite3 *db = sqlite3_db_handle(self->st); sqlite3 *db = sqlite3_db_handle(self->st);
_pysqlite_seterror(state, db); _pysqlite_seterror(state, db);
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions1(exc);
return; return;
} }
} }
@ -765,11 +764,10 @@ bind_parameters(pysqlite_state *state, pysqlite_Statement *self,
Py_DECREF(adapted); Py_DECREF(adapted);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
PyObject *exc, *val, *tb; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&exc, &val, &tb);
sqlite3 *db = sqlite3_db_handle(self->st); sqlite3 *db = sqlite3_db_handle(self->st);
_pysqlite_seterror(state, db); _pysqlite_seterror(state, db);
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions1(exc);
return; return;
} }
} }

View file

@ -623,16 +623,15 @@ heapctypesubclasswithfinalizer_init(PyObject *self, PyObject *args, PyObject *kw
static void static void
heapctypesubclasswithfinalizer_finalize(PyObject *self) heapctypesubclasswithfinalizer_finalize(PyObject *self)
{ {
PyObject *error_type, *error_value, *error_traceback, *m;
PyObject *oldtype = NULL, *newtype = NULL, *refcnt = NULL; PyObject *oldtype = NULL, *newtype = NULL, *refcnt = NULL;
/* Save the current exception, if any. */ /* Save the current exception, if any. */
PyErr_Fetch(&error_type, &error_value, &error_traceback); PyObject *exc = PyErr_GetRaisedException();
if (_testcapimodule == NULL) { if (_testcapimodule == NULL) {
goto cleanup_finalize; goto cleanup_finalize;
} }
m = PyState_FindModule(_testcapimodule); PyObject *m = PyState_FindModule(_testcapimodule);
if (m == NULL) { if (m == NULL) {
goto cleanup_finalize; goto cleanup_finalize;
} }
@ -667,7 +666,7 @@ cleanup_finalize:
Py_XDECREF(refcnt); Py_XDECREF(refcnt);
/* Restore the saved exception. */ /* Restore the saved exception. */
PyErr_Restore(error_type, error_value, error_traceback); PyErr_SetRaisedException(exc);
} }
static PyType_Slot HeapCTypeSubclassWithFinalizer_slots[] = { static PyType_Slot HeapCTypeSubclassWithFinalizer_slots[] = {

View file

@ -389,16 +389,15 @@ allocate_too_many_code_watchers(PyObject *self, PyObject *args)
watcher_ids[i] = watcher_id; watcher_ids[i] = watcher_id;
num_watchers++; num_watchers++;
} }
PyObject *type, *value, *traceback; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&type, &value, &traceback);
for (int i = 0; i < num_watchers; i++) { for (int i = 0; i < num_watchers; i++) {
if (PyCode_ClearWatcher(watcher_ids[i]) < 0) { if (PyCode_ClearWatcher(watcher_ids[i]) < 0) {
PyErr_WriteUnraisable(Py_None); PyErr_WriteUnraisable(Py_None);
break; break;
} }
} }
if (type) { if (exc) {
PyErr_Restore(type, value, traceback); PyErr_SetRaisedException(exc);
return NULL; return NULL;
} }
else if (PyErr_Occurred()) { else if (PyErr_Occurred()) {
@ -578,16 +577,15 @@ allocate_too_many_func_watchers(PyObject *self, PyObject *args)
watcher_ids[i] = watcher_id; watcher_ids[i] = watcher_id;
num_watchers++; num_watchers++;
} }
PyObject *type, *value, *traceback; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&type, &value, &traceback);
for (int i = 0; i < num_watchers; i++) { for (int i = 0; i < num_watchers; i++) {
if (PyFunction_ClearWatcher(watcher_ids[i]) < 0) { if (PyFunction_ClearWatcher(watcher_ids[i]) < 0) {
PyErr_WriteUnraisable(Py_None); PyErr_WriteUnraisable(Py_None);
break; break;
} }
} }
if (type) { if (exc) {
PyErr_Restore(type, value, traceback); PyErr_SetRaisedException(exc);
return NULL; return NULL;
} }
else if (PyErr_Occurred()) { else if (PyErr_Occurred()) {

View file

@ -1611,19 +1611,18 @@ static void
slot_tp_del(PyObject *self) slot_tp_del(PyObject *self)
{ {
PyObject *del, *res; PyObject *del, *res;
PyObject *error_type, *error_value, *error_traceback;
/* Temporarily resurrect the object. */ /* Temporarily resurrect the object. */
assert(Py_REFCNT(self) == 0); assert(Py_REFCNT(self) == 0);
Py_SET_REFCNT(self, 1); Py_SET_REFCNT(self, 1);
/* Save the current exception, if any. */ /* Save the current exception, if any. */
PyErr_Fetch(&error_type, &error_value, &error_traceback); PyObject *exc = PyErr_GetRaisedException();
PyObject *tp_del = PyUnicode_InternFromString("__tp_del__"); PyObject *tp_del = PyUnicode_InternFromString("__tp_del__");
if (tp_del == NULL) { if (tp_del == NULL) {
PyErr_WriteUnraisable(NULL); PyErr_WriteUnraisable(NULL);
PyErr_Restore(error_type, error_value, error_traceback); PyErr_SetRaisedException(exc);
return; return;
} }
/* Execute __del__ method, if any. */ /* Execute __del__ method, if any. */
@ -1638,7 +1637,7 @@ slot_tp_del(PyObject *self)
} }
/* Restore the saved exception. */ /* Restore the saved exception. */
PyErr_Restore(error_type, error_value, error_traceback); PyErr_SetRaisedException(exc);
/* Undo the temporary resurrection; can't use DECREF here, it would /* Undo the temporary resurrection; can't use DECREF here, it would
* cause a recursive call. * cause a recursive call.

View file

@ -100,9 +100,9 @@ add_new_type(PyObject *mod, PyType_Spec *spec, crossinterpdatafunc shared)
static int static int
_release_xid_data(_PyCrossInterpreterData *data, int ignoreexc) _release_xid_data(_PyCrossInterpreterData *data, int ignoreexc)
{ {
PyObject *exctype, *excval, *exctb; PyObject *exc;
if (ignoreexc) { if (ignoreexc) {
PyErr_Fetch(&exctype, &excval, &exctb); exc = PyErr_GetRaisedException();
} }
int res = _PyCrossInterpreterData_Release(data); int res = _PyCrossInterpreterData_Release(data);
if (res < 0) { if (res < 0) {
@ -125,7 +125,7 @@ _release_xid_data(_PyCrossInterpreterData *data, int ignoreexc)
} }
} }
if (ignoreexc) { if (ignoreexc) {
PyErr_Restore(exctype, excval, exctb); PyErr_SetRaisedException(exc);
} }
return res; return res;
} }

View file

@ -270,10 +270,9 @@ error:
Py_CLEAR(self); Py_CLEAR(self);
cleanup: cleanup:
if (file_obj != NULL) { if (file_obj != NULL) {
PyObject *exc, *val, *tb; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&exc, &val, &tb);
PyObject *tmp = PyObject_CallMethod(file_obj, "close", NULL); PyObject *tmp = PyObject_CallMethod(file_obj, "close", NULL);
_PyErr_ChainExceptions(exc, val, tb); _PyErr_ChainExceptions1(exc);
if (tmp == NULL) { if (tmp == NULL) {
Py_CLEAR(self); Py_CLEAR(self);
} }

View file

@ -14759,10 +14759,9 @@ ScandirIterator_exit(ScandirIterator *self, PyObject *args)
static void static void
ScandirIterator_finalize(ScandirIterator *iterator) ScandirIterator_finalize(ScandirIterator *iterator)
{ {
PyObject *error_type, *error_value, *error_traceback;
/* Save the current exception, if any. */ /* Save the current exception, if any. */
PyErr_Fetch(&error_type, &error_value, &error_traceback); PyObject *exc = PyErr_GetRaisedException();
if (!ScandirIterator_is_closed(iterator)) { if (!ScandirIterator_is_closed(iterator)) {
ScandirIterator_closedir(iterator); ScandirIterator_closedir(iterator);
@ -14779,7 +14778,7 @@ ScandirIterator_finalize(ScandirIterator *iterator)
path_cleanup(&iterator->path); path_cleanup(&iterator->path);
/* Restore the saved exception. */ /* Restore the saved exception. */
PyErr_Restore(error_type, error_value, error_traceback); PyErr_SetRaisedException(exc);
} }
static void static void

View file

@ -234,14 +234,13 @@ signal_default_int_handler_impl(PyObject *module, int signalnum,
static int static int
report_wakeup_write_error(void *data) report_wakeup_write_error(void *data)
{ {
PyObject *exc, *val, *tb;
int save_errno = errno; int save_errno = errno;
errno = (int) (intptr_t) data; errno = (int) (intptr_t) data;
PyErr_Fetch(&exc, &val, &tb); PyObject *exc = PyErr_GetRaisedException();
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
_PyErr_WriteUnraisableMsg("when trying to write to the signal wakeup fd", _PyErr_WriteUnraisableMsg("when trying to write to the signal wakeup fd",
NULL); NULL);
PyErr_Restore(exc, val, tb); PyErr_SetRaisedException(exc);
errno = save_errno; errno = save_errno;
return 0; return 0;
} }
@ -252,14 +251,13 @@ report_wakeup_send_error(void* data)
{ {
int send_errno = (int) (intptr_t) data; int send_errno = (int) (intptr_t) data;
PyObject *exc, *val, *tb; PyObject *exc = PyErr_GetRaisedException();
PyErr_Fetch(&exc, &val, &tb);
/* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
recognizes the error codes used by both GetLastError() and recognizes the error codes used by both GetLastError() and
WSAGetLastError */ WSAGetLastError */
PyErr_SetExcFromWindowsErr(PyExc_OSError, send_errno); PyErr_SetExcFromWindowsErr(PyExc_OSError, send_errno);
_PyErr_WriteUnraisableMsg("when trying to send to the signal wakeup fd", NULL); _PyErr_WriteUnraisableMsg("when trying to send to the signal wakeup fd", NULL);
PyErr_Restore(exc, val, tb); PyErr_SetRaisedException(exc);
return 0; return 0;
} }
#endif /* MS_WINDOWS */ #endif /* MS_WINDOWS */

View file

@ -5175,10 +5175,9 @@ static void
sock_finalize(PySocketSockObject *s) sock_finalize(PySocketSockObject *s)
{ {
SOCKET_T fd; SOCKET_T fd;
PyObject *error_type, *error_value, *error_traceback;
/* Save the current exception, if any. */ /* Save the current exception, if any. */
PyErr_Fetch(&error_type, &error_value, &error_traceback); PyObject *exc = PyErr_GetRaisedException();
if (s->sock_fd != INVALID_SOCKET) { if (s->sock_fd != INVALID_SOCKET) {
if (PyErr_ResourceWarning((PyObject *)s, 1, "unclosed %R", s)) { if (PyErr_ResourceWarning((PyObject *)s, 1, "unclosed %R", s)) {
@ -5202,7 +5201,7 @@ sock_finalize(PySocketSockObject *s)
} }
/* Restore the saved exception. */ /* Restore the saved exception. */
PyErr_Restore(error_type, error_value, error_traceback); PyErr_SetRaisedException(exc);
} }
static void static void