task: Prevent error indicator being sporadically cleared

This commit is contained in:
rdb 2021-05-31 16:07:50 +02:00
parent e63eab9aa2
commit c9508f3215

View File

@ -398,6 +398,10 @@ PyObject *Extension<AsyncFuture>::
get_cancelled_error_type() {
static PyObject *exc_type = nullptr;
if (exc_type == nullptr) {
// This method should not affect the current exception, so stash it.
PyObject *curexc_type, *curexc_value, *curexc_traceback;
PyErr_Fetch(&curexc_type, &curexc_value, &curexc_traceback);
// Get the CancelledError that asyncio uses, too.
#if PY_VERSION_HEX >= 0x03080000
PyObject *module = PyImport_ImportModule("asyncio.exceptions");
@ -408,9 +412,6 @@ get_cancelled_error_type() {
exc_type = PyObject_GetAttrString(module, "CancelledError");
Py_DECREF(module);
}
else {
PyErr_Clear();
}
// If we can't get that, we should pretend and make our own.
if (exc_type == nullptr) {
@ -424,6 +425,8 @@ get_cancelled_error_type() {
nullptr, nullptr);
#endif
}
PyErr_Restore(curexc_type, curexc_value, curexc_traceback);
}
return exc_type;
}