|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <boost/python.hpp> |
| 4 | +#include <string> |
| 5 | + |
| 6 | +#include <G3Logging.h> |
| 7 | + |
| 8 | +namespace py = boost::python; |
| 9 | + |
| 10 | +// Python runtime context to simplify acquiring or releasing the GIL as necessary. |
| 11 | +// To use, simply construct the context object where necessary, e.g. |
| 12 | +// G3PythonContext ctx("mycontext", false); |
| 13 | +// The context destructor will clean up after itself (releasing the GIL if acquired, and |
| 14 | +// vice versa). If hold_gil is true, the context will ensure the GIL is held at construction, |
| 15 | +// and released at destruction. If hold_gil is false, the context will save the current thread |
| 16 | +// state and release the GIL at construction, and re-acquire it at destruction. |
| 17 | +class G3PythonContext { |
| 18 | +public: |
| 19 | + G3PythonContext(std::string name, bool hold_gil=false); |
| 20 | + ~G3PythonContext(); |
| 21 | + |
| 22 | +private: |
| 23 | + std::string name_; |
| 24 | + bool hold_; |
| 25 | + PyGILState_STATE gil_; |
| 26 | + PyThreadState *thread_; |
| 27 | + |
| 28 | + SET_LOGGER("G3PythonContext"); |
| 29 | +}; |
| 30 | + |
| 31 | +// Convenience class for initializing and finalizing the Python interpreter. This class |
| 32 | +// will initialize the python interpeter at construction (e.g. at the beginning of a C++ |
| 33 | +// compiled program), and immediately initialize the appropriate G3PythonContext depending |
| 34 | +// on the value of hold_gil. At destruction, it will exit the python context and finalize |
| 35 | +// the interpreter. The python interpreter should be initialized only once, typically at |
| 36 | +// the beginning of the main program. |
| 37 | +class G3PythonInterpreter { |
| 38 | +public: |
| 39 | + G3PythonInterpreter(); |
| 40 | + ~G3PythonInterpreter(); |
| 41 | + |
| 42 | +private: |
| 43 | + bool init_; |
| 44 | + |
| 45 | + SET_LOGGER("G3PythonInterpreter"); |
| 46 | +}; |
| 47 | + |
| 48 | +// pybind11 compatibility functions |
| 49 | +namespace boost { namespace python { |
| 50 | + |
| 51 | +class module_ : public scope |
| 52 | +{ |
| 53 | +public: |
| 54 | + static auto import(const std::string &name) { |
| 55 | + return py::import(name.c_str()); |
| 56 | + } |
| 57 | + |
| 58 | + py::object def_submodule(const std::string &name); |
| 59 | + |
| 60 | + template <typename...Args> |
| 61 | + static auto def(Args&&... args) { |
| 62 | + return py::def(std::forward<Args>(args)...); |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +class gil_scoped_release : public G3PythonContext |
| 67 | +{ |
| 68 | +public: |
| 69 | + gil_scoped_release() : G3PythonContext("release", false) {}; |
| 70 | +}; |
| 71 | + |
| 72 | +class gil_scoped_acquire : public G3PythonContext |
| 73 | +{ |
| 74 | +public: |
| 75 | + gil_scoped_acquire() : G3PythonContext("acquire", true) {}; |
| 76 | +}; |
| 77 | + |
| 78 | +class scoped_interpreter : public G3PythonInterpreter {}; |
| 79 | + |
| 80 | +template <typename T> |
| 81 | +T cast(const py::object &obj) |
| 82 | +{ |
| 83 | + return py::extract<T>(obj)(); |
| 84 | +} |
| 85 | + |
| 86 | +template <typename T> |
| 87 | +bool isinstance(const py::object &obj) |
| 88 | +{ |
| 89 | + return py::extract<T>(obj).check(); |
| 90 | +} |
| 91 | + |
| 92 | +class value_error : std::exception |
| 93 | +{ |
| 94 | +public: |
| 95 | + std::string msg; |
| 96 | + value_error(std::string msg="") : msg(msg) {}; |
| 97 | + const char * what() const throw() { return msg.c_str(); } |
| 98 | +}; |
| 99 | + |
| 100 | +class index_error : std::exception |
| 101 | +{ |
| 102 | +public: |
| 103 | + std::string msg; |
| 104 | + index_error(std::string msg="") : msg(msg) {}; |
| 105 | + const char * what() const throw() { return msg.c_str(); } |
| 106 | +}; |
| 107 | + |
| 108 | +class type_error : std::exception |
| 109 | +{ |
| 110 | +public: |
| 111 | + std::string msg; |
| 112 | + type_error(std::string msg="") : msg(msg) {}; |
| 113 | + const char * what() const throw() { return msg.c_str(); } |
| 114 | +}; |
| 115 | + |
| 116 | +class key_error : std::exception |
| 117 | +{ |
| 118 | +public: |
| 119 | + std::string msg; |
| 120 | + key_error(std::string msg="") : msg(msg) {}; |
| 121 | + const char * what() const throw() { return msg.c_str(); } |
| 122 | +}; |
| 123 | + |
| 124 | +class buffer_error : std::exception |
| 125 | +{ |
| 126 | +public: |
| 127 | + std::string msg; |
| 128 | + buffer_error(std::string msg="") : msg(msg) {}; |
| 129 | + const char * what() const throw() { return msg.c_str(); } |
| 130 | +}; |
| 131 | + |
| 132 | +}} |
0 commit comments