MCPcopy Create free account
hub / github.com/Cryolite/kanachan / translatePythonException

Function translatePythonException

src/simulation/utility.cpp:43–96  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41}
42
43void translatePythonException()
44{
45 std::exception_ptr const p = std::current_exception();
46 if (!p) {
47 KANACHAN_THROW<std::runtime_error>("No exception.");
48 }
49
50 try {
51 std::rethrow_exception(p);
52 }
53 catch (python::error_already_set const &) {
54 Kanachan::GIL::RecursiveLock gil_lock;
55
56 auto const [type, value, traceback] = [](){
57 PyObject *p_type = nullptr;
58 PyObject *p_value = nullptr;
59 PyObject *p_traceback = nullptr;
60 PyErr_Fetch(&p_type, &p_value, &p_traceback);
61
62 python::object type{ python::handle<>(p_type) };
63
64 python::object value;
65 if (p_value != nullptr) {
66 value = python::object{ python::handle<>(p_value) };
67 }
68 else {
69 value = python::object();
70 }
71
72 python::object traceback;
73 if (p_traceback != nullptr) {
74 traceback = python::object{python::handle<>(p_traceback)};
75 }
76
77 return std::tuple(type, value, traceback);
78 }();
79
80 python::object m = python::import("traceback");
81
82 if (python::extract<std::string>(value).check()) {
83 python::object o = m.attr("format_tb")(traceback);
84 o = python::str("").attr("join")(o);
85 std::string message = python::extract<std::string>(o);
86 message += python::extract<std::string>(value);
87 KANACHAN_THROW<std::runtime_error>(message);
88 }
89
90 python::object o = m.attr("format_exception")(type, value, traceback);
91 o = python::str("").attr("join")(o);
92 python::extract<std::string> str(o);
93 KANACHAN_ASSERT((str.check()));
94 KANACHAN_THROW<std::runtime_error>(str());
95 }
96}
97
98} // namespace Kanachan

Callers 7

simulateFunction · 0.85
testFunction · 0.85
Impl_Method · 0.85
decide_Method · 0.85
threadMain_Method · 0.85
operator()Method · 0.85
threadMain_Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected