30 #ifndef _PYCPPWRAPPER_H_ 31 #define _PYCPPWRAPPER_H_ 40 #include <boost/locale/encoding_utf.hpp> 42 #if PY_MAJOR_VERSION < 3 43 #error "This file can only handle python >= 3" 49 inline void initializePython(
const std::string &main) {
50 static auto mainW=boost::locale::conv::utf_to_utf<wchar_t>(main);
51 Py_SetProgramName(const_cast<wchar_t*>(mainW.c_str()));
56 inline PyObject* PyRun_String_func(
const char *str,
int start, PyObject *globals, PyObject *locals) {
return PyRun_String(str, start, globals, locals); }
58 #define PyRun_String PythonCpp::PyRun_String_func 61 inline int PyFloat_Check_func(PyObject *p) {
return PyFloat_Check(p); }
63 #define PyFloat_Check PythonCpp::PyFloat_Check_func 66 inline int PyFloat_AS_DOUBLE_func(PyObject *p) {
return PyFloat_AS_DOUBLE(p); }
67 #undef PyFloat_AS_DOUBLE 68 #define PyFloat_AS_DOUBLE PythonCpp::PyFloat_AS_DOUBLE_func 71 inline int PyBool_Check_func(PyObject *p) {
return PyBool_Check(p); }
73 #define PyBool_Check PythonCpp::PyBool_Check_func 76 inline int PyList_Check_func(PyObject *p) {
return PyList_Check(p); }
78 #define PyList_Check PythonCpp::PyList_Check_func 81 inline int PyObject_TypeCheck_func(PyObject *p, PyTypeObject *type) {
return PyObject_TypeCheck(p, type); }
82 #undef PyObject_TypeCheck 83 #define PyObject_TypeCheck PythonCpp::PyObject_TypeCheck_func 86 inline int PyLong_Check_func(PyObject *p) {
return PyLong_Check(p); }
88 #define PyLong_Check PythonCpp::PyLong_Check_func 91 inline int PyUnicode_Check_func(PyObject *p) {
return PyUnicode_Check(p); }
92 #undef PyUnicode_Check 93 #define PyUnicode_Check PythonCpp::PyUnicode_Check_func 98 constexpr
PyO() =
default;
99 explicit PyO(PyObject *src,
bool srcIsBorrowedRef=
false) : p(src) {
if(srcIsBorrowedRef) Py_XINCREF(p); }
100 PyO(
const PyO &r) : p(r.p) { Py_XINCREF(p); }
101 PyO(
PyO &&r) noexcept : p(r.p) { r.p=
nullptr; }
102 ~
PyO() { Py_XDECREF(p); }
103 PyO& operator=(
const PyO &r) { Py_XDECREF(p); p=r.p; Py_XINCREF(p);
return *
this; }
104 PyO& operator=(
PyO &&r) noexcept { Py_XDECREF(p); p=r.p; r.p=
nullptr;
return *
this; }
105 void reset() { Py_XDECREF(p); p=
nullptr; }
106 void reset(PyObject *src,
bool srcIsBorrowedRef=
false) { Py_XDECREF(p); p=src;
if(srcIsBorrowedRef) Py_XINCREF(p); }
107 void swap(
PyO &r) { PyObject *temp=p; p=r.p; r.p=temp; }
108 PyObject* get(
bool incRef=
false)
const {
if(incRef) Py_XINCREF(p);
return p; }
109 long use_count()
const {
return p ? Py_REFCNT(p) : 0; }
110 PyObject* operator->()
const {
return p; }
111 PyO& incRef() { Py_XINCREF(p);
return *
this; }
112 operator bool()
const {
return p!=
nullptr; }
114 PyObject *p{
nullptr};
117 inline bool operator==(
const PyO& l,
const PyO& r) {
return l.get()==r.get(); }
118 inline bool operator!=(
const PyO& l,
const PyO& r) {
return l.get()!=r.get(); }
119 inline bool operator< (
const PyO& l,
const PyO& r) {
return l.get()< r.get(); }
120 inline bool operator> (
const PyO& l,
const PyO& r) {
return l.get()> r.get(); }
121 inline bool operator<=(
const PyO& l,
const PyO& r) {
return l.get()<=r.get(); }
122 inline bool operator>=(
const PyO& l,
const PyO& r) {
return l.get()>=r.get(); }
131 std::string getFile() {
return file; }
132 int getLine() {
return line; }
133 PyO getType() {
return type; }
134 PyO getValue() {
return value; }
135 PyO getTraceback() {
return traceback; }
136 inline const char* what()
const noexcept
override;
140 PyO type, value, traceback;
141 mutable std::string msg;
145 inline void checkPythonError() {
155 inline static type convert(
const T &r) {
162 inline static PyO convert(PyObject *r) {
164 throw std::runtime_error(
"Internal error: Expected python object but got NULL pointer and no python exception is set.");
166 throw std::runtime_error(
"Internal error: Expected python object but a python object with 0 refcount and no python exception is set.");
173 template<
typename Arg>
174 inline Arg convertArg(
const Arg &arg) {
178 inline PyObject* convertArg(
const PyO &o) {
179 if(o && o.use_count()<=0)
180 throw std::runtime_error(
"Internal error: access object with reference count <= 0. Check the source code.");
184 inline const char* convertArg(
const std::string &o) {
190 template<
typename PyRet,
typename... PyArgs,
typename... CallArgs>
191 inline typename MapRetType<PyRet>::type callPy(
const char *file,
int line, PyRet (*func)(PyArgs...), CallArgs&&... args) {
192 PyRet ret=func(convertArg(std::forward<CallArgs>(args))...);
194 throw PythonException(file, line);
195 return MapRetType<PyRet>::convert(ret);
202 #define CALLPY(...) PythonCpp::callPy(__FILE__, __LINE__, __VA_ARGS__) 208 #define CALLPYB(...) PythonCpp::callPy(__FILE__, __LINE__, __VA_ARGS__).incRef() 211 PythonException::PythonException(
const char *file_,
int line_) : file(file_), line(line_) {
213 if(!PyErr_Occurred())
214 throw std::runtime_error(
"Internal error: PythonException object created but no python error occured.");
216 PyObject *type_, *value_, *traceback_;
217 PyErr_Fetch(&type_, &value_, &traceback_);
220 traceback=PyO(traceback_);
223 const inline char* PythonException::what() const noexcept {
228 PyObject *savedstderr=PySys_GetObject(const_cast<char*>(
"stderr"));
230 return "Unable to create Python error message: no sys.stderr available.";
231 PyObject *io=PyImport_ImportModule(
"io");
233 return "Unable to create Python error message: cannot load io module.";
234 PyObject *fileIO=PyObject_GetAttrString(io,
"StringIO");
236 return "Unable to create Python error message: cannot get in memory file class.";
238 PyObject *buf=PyObject_CallObject(fileIO,
nullptr);
241 return "Unable to create Python error message: cannot create new in memory file instance";
242 if(PySys_SetObject(const_cast<char*>(
"stderr"), buf)!=0)
243 return "Unable to create Python error message: cannot redirect stderr";
245 PyErr_Restore(type.get(), value.get(), traceback.get());
246 Py_XINCREF(type.get());
247 Py_XINCREF(value.get());
248 Py_XINCREF(traceback.get());
252 if(PySys_SetObject(const_cast<char*>(
"stderr"), savedstderr)!=0)
253 return "Unable to create Python error message: cannot revert redirect stderr";
255 PyObject *getvalue=PyObject_GetAttrString(buf,
"getvalue");
257 return "Unable to create Python error message: cannot get getvalue attribute";
258 PyObject *pybufstr=PyObject_CallObject(getvalue,
nullptr);
260 return "Unable to create Python error message: cannot get string from in memory file output";
263 std::string str=PyUnicode_AsUTF8(pybufstr);
265 return "Unable to create Python error message: cannot get c string";
267 std::stringstream strstr;
268 strstr<<
"Python exception";
270 strstr<<
" at "<<file<<
":"<<line;
271 strstr<<
":"<<std::endl<<str;
277 template<
typename... Args>
278 PyO Py_BuildValue_(
const char *format, Args&&... args) {
279 return PyO(Py_BuildValue(format, convertArg(std::forward<Args>(args))...));
Definition: pycppwrapper.h:96
Definition: pycppwrapper.h:127
Definition: pycppwrapper.h:153