From 751e67e86c6a1bc0e77dbd4f10df6fc2b6eb134b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Thu, 21 May 2020 22:08:20 +0200 Subject: [PATCH] Fix issue with Python errors not being retrieved correctly in C++. --- src/runner/pythonwrapperutilities.h | 54 ++++++++++++++--------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/runner/pythonwrapperutilities.h b/src/runner/pythonwrapperutilities.h index 6b25478..ab44ddc 100644 --- a/src/runner/pythonwrapperutilities.h +++ b/src/runner/pythonwrapperutilities.h @@ -5,6 +5,7 @@ #include +#include #include #include "error.h" @@ -17,20 +18,17 @@ template ReturnType basicWrapperFunctionImplementation(const WrapperType *wrapper, const char *methodName, Args... args) { + GILock lock; + boost::python::override implementation = wrapper->get_override(methodName); + if (!implementation) { + throw pyexcept::MissingImplementation(wrapper->className, methodName); + } try { - GILock lock; - boost::python::override implementation = wrapper->get_override(methodName); - if (!implementation) { - throw pyexcept::MissingImplementation(wrapper->className, methodName); - } return implementation(args...).as(); } catch (const boost::python::error_already_set&) { throw pyexcept::PythonError(); } - catch (pyexcept::MissingImplementation const& missingImplementation) { - throw missingImplementation; - } catch (...) { throw pyexcept::UnknownException(); } @@ -42,12 +40,12 @@ ReturnType basicWrapperFunctionImplementation(const WrapperType *wrapper, const template ReturnType basicWrapperFunctionImplementation(const WrapperType* wrapper, boost::python::object &ref, const char* methodName, Args... args) { + GILock lock; + boost::python::override implementation = wrapper->get_override(methodName); + if (!implementation) { + throw pyexcept::MissingImplementation(wrapper->className, methodName); + } try { - GILock lock; - boost::python::override implementation = wrapper->get_override(methodName); - if (!implementation) { - throw pyexcept::MissingImplementation(wrapper->className, methodName); - } ref = implementation(args...); return boost::python::extract(ref)(); } @@ -65,12 +63,15 @@ ReturnType basicWrapperFunctionImplementation(const WrapperType* wrapper, boost: template ReturnType basicWrapperFunctionImplementationWithDefault(WrapperType* wrapper, Fn fn, const char* methodName, Args... args) { + GILock lock; + boost::python::override implementation = wrapper->get_override(methodName); + + if (!implementation) { + return std::invoke(fn, wrapper, args...); + } + try { - GILock lock; - boost::python::override implementation = wrapper->get_override(methodName); - if (implementation) { - return implementation(args...).as(); - } + return implementation(args...).as(); } catch (const boost::python::error_already_set&) { throw pyexcept::PythonError(); @@ -78,19 +79,20 @@ ReturnType basicWrapperFunctionImplementationWithDefault(WrapperType* wrapper, F catch (...) { throw pyexcept::UnknownException(); } - - return std::invoke(fn, wrapper, args...); } template ReturnType basicWrapperFunctionImplementationWithDefault(const WrapperType* wrapper, Fn fn, const char* methodName, Args... args) { + GILock lock; + boost::python::override implementation = wrapper->get_override(methodName); + + if (!implementation) { + return std::invoke(fn, wrapper, args...); + } + try { - GILock lock; - boost::python::override implementation = wrapper->get_override(methodName); - if (implementation) { - return implementation(args...).as(); - } + return implementation(args...).as(); } catch (const boost::python::error_already_set&) { throw pyexcept::PythonError(); @@ -98,8 +100,6 @@ ReturnType basicWrapperFunctionImplementationWithDefault(const WrapperType* wrap catch (...) { throw pyexcept::UnknownException(); } - - return std::invoke(fn, wrapper, args...); } #endif // PYTHONWRAPPERUTILITIES_H