30 #ifndef _PY2PY3CPPWRAPPER_H_
31 #define _PY2PY3CPPWRAPPER_H_
40 #include <boost/locale/encoding_utf.hpp>
45 void initializePython(
const std::string &main,
const std::string &home=
"") {
46 static const std::string mainStatic=main;
47 static const std::string homeStatic=home;
48 #if PY_MAJOR_VERSION < 3
49 Py_SetProgramName(const_cast<char*>(mainStatic.c_str()));
50 if(!homeStatic.empty())
51 Py_SetPythonHome(const_cast<char*>(homeStatic.c_str()));
53 Py_SetProgramName(const_cast<wchar_t*>(boost::locale::conv::utf_to_utf<wchar_t>(mainStatic).c_str()));
54 if(!homeStatic.empty())
55 Py_SetPythonHome(const_cast<wchar_t*>(boost::locale::conv::utf_to_utf<wchar_t>(homeStatic).c_str()));
63 inline bool PyLong_Check_Py2Py3(PyObject *o) {
64 #if PY_MAJOR_VERSION < 3
65 return PyLong_Check(o) || PyInt_Check(o);
67 return PyLong_Check(o);
71 inline long PyLong_AsLong_Py2Py3(PyObject *o) {
72 #if PY_MAJOR_VERSION < 3
74 return PyInt_AsLong(o);
75 return PyLong_AsLong(o);
77 return PyLong_AsLong(o);
81 inline bool PyUnicode_Check_Py2Py3(PyObject *o) {
82 #if PY_MAJOR_VERSION < 3
83 return PyUnicode_Check(o) || PyString_Check(o);
85 return PyUnicode_Check(o);
90 inline std::string PyUnicode_AsUTF8_Py2Py3(PyObject *o) {
91 #if PY_MAJOR_VERSION < 3
92 if(PyString_Check(o)) {
93 char *retc=PyString_AsString(o);
97 PyObject *str=PyUnicode_AsUTF8String(o);
99 char *retc=PyString_AsString(str);
104 std::string ret=retc;
108 char *retc=PyUnicode_AsUTF8(o);
117 #undef PyUnicode_Check
118 #define PyLong_Check PythonCpp::PyLong_Check_Py2Py3
119 #define PyLong_AsLong PythonCpp::PyLong_AsLong_Py2Py3
120 #define PyUnicode_Check PythonCpp::PyUnicode_Check_Py2Py3
121 #define PyUnicode_AsUTF8 PythonCpp::PyUnicode_AsUTF8_Py2Py3
124 inline PyObject* PyRun_String_func(
const char *str,
int start, PyObject *globals, PyObject *locals) {
return PyRun_String(str, start, globals, locals); }
126 #define PyRun_String PythonCpp::PyRun_String_func
129 inline int PyFloat_Check_func(PyObject *p) {
return PyFloat_Check(p); }
131 #define PyFloat_Check PythonCpp::PyFloat_Check_func
134 inline int PyFloat_AS_DOUBLE_func(PyObject *p) {
return PyFloat_AS_DOUBLE(p); }
135 #undef PyFloat_AS_DOUBLE
136 #define PyFloat_AS_DOUBLE PythonCpp::PyFloat_AS_DOUBLE_func
139 inline int PyBool_Check_func(PyObject *p) {
return PyBool_Check(p); }
141 #define PyBool_Check PythonCpp::PyBool_Check_func
144 inline int PyList_Check_func(PyObject *p) {
return PyList_Check(p); }
146 #define PyList_Check PythonCpp::PyList_Check_func
149 inline int PyObject_TypeCheck_func(PyObject *p, PyTypeObject *type) {
return PyObject_TypeCheck(p, type); }
150 #undef PyObject_TypeCheck
151 #define PyObject_TypeCheck PythonCpp::PyObject_TypeCheck_func
154 inline PyObject* Py_InitModule_func(
char *name, PyMethodDef *methods) {
return Py_InitModule(name, methods); }
156 #define Py_InitModule PythonCpp::Py_InitModule_func
161 constexpr
PyO() : p(
nullptr) {}
162 explicit PyO(PyObject *src,
bool srcIsBorrowedRef=
false) : p(src) {
if(srcIsBorrowedRef) Py_XINCREF(p); }
163 PyO(
const PyO &r) : p(r.p) { Py_XINCREF(p); }
164 PyO(PyO &&r) : p(r.p) { r.p=
nullptr; }
165 ~PyO() { Py_XDECREF(p); }
166 PyO& operator=(
const PyO &r) { Py_XDECREF(p); p=r.p; Py_XINCREF(p);
return *
this; }
167 PyO& operator=(PyO &&r) { Py_XDECREF(p); p=r.p; r.p=
nullptr;
return *
this; }
168 void reset() { Py_XDECREF(p); p=
nullptr; }
169 void reset(PyObject *src,
bool srcIsBorrowedRef=
false) { Py_XDECREF(p); p=src;
if(srcIsBorrowedRef) Py_XINCREF(p); }
170 void swap(PyO &r) { PyObject *temp=p; p=r.p; r.p=temp; }
171 PyObject*
get(
bool incRef=
false)
const {
if(incRef) Py_XINCREF(p);
return p; }
172 long use_count()
const {
return p ? Py_REFCNT(p) : 0; }
173 PyObject* operator->()
const {
return p; }
174 PyO& incRef() { Py_XINCREF(p);
return *
this; }
175 operator bool()
const {
return p!=
nullptr; }
180 inline bool operator==(
const PyO& l,
const PyO& r) {
return l.get()==r.get(); }
181 inline bool operator!=(
const PyO& l,
const PyO& r) {
return l.get()!=r.get(); }
182 inline bool operator< (
const PyO& l,
const PyO& r) {
return l.get()< r.get(); }
183 inline bool operator> (
const PyO& l,
const PyO& r) {
return l.get()> r.get(); }
184 inline bool operator<=(
const PyO& l,
const PyO& r) {
return l.get()<=r.get(); }
185 inline bool operator>=(
const PyO& l,
const PyO& r) {
return l.get()>=r.get(); }
194 std::string getFile() {
return file; }
195 int getLine() {
return line; }
196 PyO getType() {
return type; }
197 PyO getValue() {
return value; }
198 PyO getTraceback() {
return traceback; }
199 const char* what()
const throw();
203 PyO type, value, traceback;
204 mutable std::string msg;
208 void checkPythonError() {
218 inline static type convert(
const T &r) {
225 inline static PyO convert(PyObject *r) {
227 throw std::runtime_error(
"Internal error: Expected python object but got NULL pointer and no python exception is set.");
229 throw std::runtime_error(
"Internal error: Expected python object but a python object with 0 refcount and no python exception is set.");
236 template<
typename Arg>
237 inline Arg convertArg(
const Arg &arg) {
241 inline PyObject* convertArg(
const PyO &o) {
242 if(o && o.use_count()<=0)
243 throw std::runtime_error(
"Internal error: access object with reference count <= 0. Check the source code.");
247 inline const char* convertArg(
const std::string &o) {
253 template<
typename PyRet,
typename... PyArgs,
typename... CallArgs>
254 inline typename MapRetType<PyRet>::type callPy(
const char *file,
int line, PyRet (*func)(PyArgs...), CallArgs&&... args) {
255 PyRet ret=func(convertArg(std::forward<CallArgs>(args))...);
257 throw PythonException(file, line);
258 return MapRetType<PyRet>::convert(ret);
265 #define CALLPY(...) PythonCpp::callPy(__FILE__, __LINE__, __VA_ARGS__)
271 #define CALLPYB(...) PythonCpp::callPy(__FILE__, __LINE__, __VA_ARGS__).incRef()
274 PythonException::PythonException(
const char *file_,
int line_) : file(file_), line(line_) {
276 if(!PyErr_Occurred())
277 throw std::runtime_error(
"Internal error: PythonException object created but no python error occured.");
279 PyObject *type_, *value_, *traceback_;
280 PyErr_Fetch(&type_, &value_, &traceback_);
283 traceback=PyO(traceback_);
286 const char* PythonException::what()
const throw() {
291 PyObject *savedstderr=PySys_GetObject(const_cast<char*>(
"stderr"));
293 throw std::runtime_error(
"Internal error: no sys.stderr available.");
294 PyObject *io=PyImport_ImportModule(
"io");
296 throw std::runtime_error(
"Internal error: cannot load io module.");
297 #if PY_MAJOR_VERSION < 3
298 PyObject *fileIO=PyObject_GetAttrString(io,
"BytesIO");
300 PyObject *fileIO=PyObject_GetAttrString(io,
"StringIO");
303 throw std::runtime_error(
"Internal error: cannot get in memory file class.");
305 PyObject *buf=PyObject_CallObject(fileIO, NULL);
308 throw std::runtime_error(
"Internal error: cannot create new in memory file instance");
309 if(PySys_SetObject(const_cast<char*>(
"stderr"), buf)!=0)
310 throw std::runtime_error(
"Internal error: cannot redirect stderr");
312 PyErr_Restore(type.get(), value.get(), traceback.get());
313 Py_XINCREF(type.get());
314 Py_XINCREF(value.get());
315 Py_XINCREF(traceback.get());
319 if(PySys_SetObject(const_cast<char*>(
"stderr"), savedstderr)!=0)
320 throw std::runtime_error(
"Internal error: cannot revert redirect stderr");
322 PyObject *getvalue=PyObject_GetAttrString(buf,
"getvalue");
324 throw std::runtime_error(
"Internal error: cannot get getvalue attribute");
325 PyObject *pybufstr=PyObject_CallObject(getvalue, NULL);
327 throw std::runtime_error(
"Internal error: cannot get string from in memory file output");
330 #if PY_MAJOR_VERSION < 3
331 char *strc=PyBytes_AsString(pybufstr);
333 throw std::runtime_error(
"Internal error: cannot get c string");
334 std::string str(strc);
336 std::string str=PyUnicode_AsUTF8(pybufstr);
338 throw std::runtime_error(
"Internal error: cannot get c string");
341 std::stringstream strstr;
342 strstr<<
"Python exception";
344 strstr<<
" at "<<file<<
":"<<line;
345 strstr<<
":"<<std::endl<<str;
351 template<
typename... Args>
352 PyO Py_BuildValue_(
const char *format, Args&&... args) {
353 return PyO(Py_BuildValue(format, convertArg(std::forward<Args>(args))...));
Definition: py2py3cppwrapper.h:159
Definition: py2py3cppwrapper.h:190
Definition: py2py3cppwrapper.h:216