30 #ifndef _MBXMLUTILS_PYCPPWRAPPER_H_ 31 #define _MBXMLUTILS_PYCPPWRAPPER_H_ 37 #include <boost/filesystem/path.hpp> 38 #if PY_MAJOR_VERSION < 3 39 #error "This file can only handle python >= 3" 45 inline PyObject* PyRun_String_func(
const char *str,
int start, PyObject *globals, PyObject *locals) {
return PyRun_String(str, start, globals, locals); }
47 #define PyRun_String PythonCpp::PyRun_String_func 50 inline int PyFloat_Check_func(PyObject *p) {
return PyFloat_Check(p); }
52 #define PyFloat_Check PythonCpp::PyFloat_Check_func 55 inline int PyFloat_AS_DOUBLE_func(PyObject *p) {
return PyFloat_AS_DOUBLE(p); }
56 #undef PyFloat_AS_DOUBLE 57 #define PyFloat_AS_DOUBLE PythonCpp::PyFloat_AS_DOUBLE_func 60 inline int PyBool_Check_func(PyObject *p) {
return PyBool_Check(p); }
62 #define PyBool_Check PythonCpp::PyBool_Check_func 65 inline int PyList_Check_func(PyObject *p) {
return PyList_Check(p); }
67 #define PyList_Check PythonCpp::PyList_Check_func 70 inline int PyObject_TypeCheck_func(PyObject *p, PyTypeObject *type) {
return PyObject_TypeCheck(p, type); }
71 #undef PyObject_TypeCheck 72 #define PyObject_TypeCheck PythonCpp::PyObject_TypeCheck_func 75 inline int PyLong_Check_func(PyObject *p) {
return PyLong_Check(p); }
77 #define PyLong_Check PythonCpp::PyLong_Check_func 80 inline int PyUnicode_Check_func(PyObject *p) {
return PyUnicode_Check(p); }
81 #undef PyUnicode_Check 82 #define PyUnicode_Check PythonCpp::PyUnicode_Check_func 87 constexpr
PyO() =
default;
88 explicit PyO(PyObject *src,
bool srcIsBorrowedRef=
false) : p(src) {
if(srcIsBorrowedRef) Py_XINCREF(p); }
89 PyO(
const PyO &r) : p(r.p) { Py_XINCREF(p); }
90 PyO(
PyO &&r) noexcept : p(r.p) { r.p=
nullptr; }
91 ~
PyO() { Py_XDECREF(p); }
92 PyO& operator=(
const PyO &r) { Py_XDECREF(p); p=r.p; Py_XINCREF(p);
return *
this; }
93 PyO& operator=(
PyO &&r) noexcept { Py_XDECREF(p); p=r.p; r.p=
nullptr;
return *
this; }
94 void reset() { Py_XDECREF(p); p=
nullptr; }
95 void reset(PyObject *src,
bool srcIsBorrowedRef=
false) { Py_XDECREF(p); p=src;
if(srcIsBorrowedRef) Py_XINCREF(p); }
96 void swap(
PyO &r) { PyObject *temp=p; p=r.p; r.p=temp; }
97 PyObject* get(
bool incRef=
false)
const {
if(incRef) Py_XINCREF(p);
return p; }
98 long use_count()
const {
return p ? Py_REFCNT(p) : 0; }
99 PyObject* operator->()
const {
return p; }
100 PyO& incRef() { Py_XINCREF(p);
return *
this; }
101 operator bool()
const {
return p!=
nullptr; }
103 PyObject *p{
nullptr};
106 inline bool operator==(
const PyO& l,
const PyO& r) {
return l.get()==r.get(); }
107 inline bool operator!=(
const PyO& l,
const PyO& r) {
return l.get()!=r.get(); }
108 inline bool operator< (
const PyO& l,
const PyO& r) {
return l.get()< r.get(); }
109 inline bool operator> (
const PyO& l,
const PyO& r) {
return l.get()> r.get(); }
110 inline bool operator<=(
const PyO& l,
const PyO& r) {
return l.get()<=r.get(); }
111 inline bool operator>=(
const PyO& l,
const PyO& r) {
return l.get()>=r.get(); }
120 std::string getFile() {
return file; }
121 int getLine() {
return line; }
122 PyO getType() {
return type; }
123 PyO getValue() {
return value; }
124 PyO getTraceback() {
return traceback; }
125 const char* what()
const noexcept
override;
129 PyO type, value, traceback;
130 mutable std::string msg;
134 void checkPythonError();
141 inline static type convert(
const T &r) {
148 inline static PyO convert(PyObject *r) {
150 throw std::runtime_error(
"Internal error: Expected python object but got NULL pointer and no python exception is set.");
152 throw std::runtime_error(
"Internal error: Expected python object but a python object with 0 refcount and no python exception is set.");
159 template<
typename Arg>
160 inline Arg convertArg(
const Arg &arg) {
164 inline PyObject* convertArg(
const PyO &o) {
165 if(o && o.use_count()<=0)
166 throw std::runtime_error(
"Internal error: access object with reference count <= 0. Check the source code.");
170 inline const char* convertArg(
const std::string &o) {
184 template<
typename PyRet,
typename... PyArgs,
typename... CallArgs>
185 inline typename MapRetType<PyRet>::type callPy(
const char *file,
int line, PyRet (*func)(PyArgs...), CallArgs&&... args) {
190 ret=func(convertArg(std::forward<CallArgs>(args))...);
193 throw PythonException(file, line);
194 return MapRetType<PyRet>::convert(ret);
201 #define CALLPY(...) PythonCpp::callPy(__FILE__, __LINE__, __VA_ARGS__) 207 #define CALLPYB(...) PythonCpp::callPy(__FILE__, __LINE__, __VA_ARGS__).incRef() 213 void initializePython(
const boost::filesystem::path &main,
const std::string &pythonVersion,
214 const std::vector<boost::filesystem::path> &sysPathAppend={},
215 const std::vector<boost::filesystem::path> &possiblePrefix={});
218 template<
typename... Args>
219 PyO Py_BuildValue_(
const char *format, Args&&... args) {
220 return PyO(Py_BuildValue(format, convertArg(std::forward<Args>(args))...));
Definition: pycppwrapper.h:85
Definition: pycppwrapper.h:116
Definition: pycppwrapper.h:139
Definition: pycppwrapper.h:174