| 41 | } |
| 42 | |
| 43 | void 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 |
no outgoing calls
no test coverage detected