diff --git a/src/py/py.hpp b/src/py/py.hpp index da8e91ad0..ed76cb99d 100644 --- a/src/py/py.hpp +++ b/src/py/py.hpp @@ -230,6 +230,11 @@ inline std::ostream &operator<<(std::ostream &os, return ExceptionInfo{Object(exc_type), Object(exc_value), Object(traceback)}; } +inline void RestoreError(ExceptionInfo exc_info) { + PyErr_Restore(exc_info.type.Steal(), exc_info.value.Steal(), + exc_info.traceback.Steal()); +} + /// Append `dir` to Python's `sys.path`. /// /// The function does not check whether the directory exists, or is readable. diff --git a/src/query/procedure/py_module.cpp b/src/query/procedure/py_module.cpp index 0f843fca1..95587ddc8 100644 --- a/src/query/procedure/py_module.cpp +++ b/src/query/procedure/py_module.cpp @@ -1405,7 +1405,11 @@ auto WithMgpModule(mgp_module *module_def, const TFun &fun) { CHECK(py_query_module); CHECK(py_mgp.SetAttr("_MODULE", py_query_module)); auto ret = fun(); + auto maybe_exc = py::FetchError(); CHECK(py_mgp.SetAttr("_MODULE", Py_None)); + if (maybe_exc) { + py::RestoreError(*maybe_exc); + } return ret; }