From df9027dcbfa98f466ef7f7993a54242d8c0cdefb Mon Sep 17 00:00:00 2001 From: Christian Tonn Date: Sun, 1 Dec 2024 17:22:54 +0100 Subject: [PATCH] added E57_ENABLE_DIAGNOSTIC_OUTPUT (precompiler macro of libe57format) to build script parameters to display better C++ exception infos for python --- setup.py | 2 ++ src/pye57/libe57_wrapper.cpp | 15 ++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/setup.py b/setup.py index 8e0cff6..e343f20 100644 --- a/setup.py +++ b/setup.py @@ -90,12 +90,14 @@ def build_extensions(self): opts.append("-DCRCPP_USE_CPP11") opts.append("-DCRCPP_BRANCHLESS") opts.append("-Wno-unused-variable") + opts.append("-DE57_ENABLE_DIAGNOSTIC_OUTPUT") elif ct == "msvc": opts.append(f'/DVERSION_INFO="{version}"') opts.append(rf'/DREVISION_ID="\"{revision_id}\""') opts.append("/DCRCPP_USE_CPP11") opts.append("/DCRCPP_BRANCHLESS") opts.append("/DWINDOWS") + opts.append("/DE57_ENABLE_DIAGNOSTIC_OUTPUT") for ext in self.extensions: ext.extra_compile_args = opts diff --git a/src/pye57/libe57_wrapper.cpp b/src/pye57/libe57_wrapper.cpp index 31f9016..4b038a3 100644 --- a/src/pye57/libe57_wrapper.cpp +++ b/src/pye57/libe57_wrapper.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include namespace py = pybind11; using namespace pybind11::literals; @@ -40,11 +42,14 @@ PYBIND11_MODULE(libe57, m) { static py::exception exc(m, "E57Exception"); py::register_exception_translator([](std::exception_ptr p) { - try { - if (p) std::rethrow_exception(p); - } catch (const E57Exception &e) { - exc(Utilities::errorCodeToString(e.errorCode()).c_str()); - } + try { + if (p) std::rethrow_exception(p); + } catch (const E57Exception &e) { + std::stringstream ss; + e.report(__FILE__, __LINE__, __FUNCTION__, ss); + std::string output = e.errorStr() + std::string("\n\n") + ss.str(); + exc(output.c_str()); + } }); m.attr("E57_FORMAT_MAJOR") = E57_FORMAT_MAJOR;