1#include <csignal>
2
3#include "pybind11/pybind11.h"
4
5namespace taichi {
6
7namespace {
8
9// Translates and propagates a CPP exception to Python.
10// TODO(#2198): How can we glob all these global variables used as initializaers
11// into a single function?
12class ExceptionTranslationImpl {
13 public:
14 explicit ExceptionTranslationImpl() {
15 pybind11::register_exception_translator([](std::exception_ptr p) {
16 try {
17 if (p)
18 std::rethrow_exception(p);
19 } catch (const std::string &e) {
20 PyErr_SetString(PyExc_RuntimeError, e.c_str());
21 }
22 });
23 }
24};
25
26ExceptionTranslationImpl _;
27
28} // namespace
29} // namespace taichi
30