From c9508f3215b85ac3e9c791de899a783a39f31076 Mon Sep 17 00:00:00 2001 From: rdb Date: Mon, 31 May 2021 16:07:50 +0200 Subject: [PATCH] task: Prevent error indicator being sporadically cleared --- panda/src/event/asyncFuture_ext.cxx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/panda/src/event/asyncFuture_ext.cxx b/panda/src/event/asyncFuture_ext.cxx index 7bc23769137..26c97f90e18 100644 --- a/panda/src/event/asyncFuture_ext.cxx +++ b/panda/src/event/asyncFuture_ext.cxx @@ -398,6 +398,10 @@ PyObject *Extension:: 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; }