All Classes Namespaces Functions Variables Typedefs Enumerations
py2py3cppwrapper.h
1 /*
2  * Author: Markus Friedrich
3  *
4  * This file is free and unencumbered software released into the public domain.
5  *
6  * Anyone is free to copy, modify, publish, use, compile, sell, or
7  * distribute this software, either in source code form or as a compiled
8  * binary, for any purpose, commercial or non-commercial, and by any
9  * means.
10  *
11  * In jurisdictions that recognize copyright laws, the author or authors
12  * of this software dedicate any and all copyright interest in the
13  * software to the public domain. We make this dedication for the benefit
14  * of the public at large and to the detriment of our heirs and
15  * successors. We intend this dedication to be an overt act of
16  * relinquishment in perpetuity of all present and future rights to this
17  * software under copyright law.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * For more information, please refer to <http://unlicense.org/>
28  */
29 
30 #ifndef _PY2PY3CPPWRAPPER_H_
31 #define _PY2PY3CPPWRAPPER_H_
32 
33 #include <Python.h>
34 #include <string>
35 #include <vector>
36 #include <stdexcept>
37 #include <memory>
38 #include <sstream>
39 #include <memory>
40 #include <boost/locale/encoding_utf.hpp> // gcc does not support <codecvt> yet
41 
42 namespace PythonCpp {
43 
44 // initialize python giving main as program name to python
45 inline 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()));
52 #else
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()));
56 #endif
57  Py_InitializeEx(0);
58 }
59 
60 // wrap some python 3 function to also work in python 2 (the wrappers have suffix _Py2Py2
61 // and are later defined without the suffix as a macro)
62 
63 inline bool PyLong_Check_Py2Py3(PyObject *o) {
64 #if PY_MAJOR_VERSION < 3
65  return PyLong_Check(o) || PyInt_Check(o);
66 #else
67  return PyLong_Check(o);
68 #endif
69 }
70 
71 inline long PyLong_AsLong_Py2Py3(PyObject *o) {
72 #if PY_MAJOR_VERSION < 3
73  if(PyInt_Check(o))
74  return PyInt_AsLong(o);
75  return PyLong_AsLong(o);
76 #else
77  return PyLong_AsLong(o);
78 #endif
79 }
80 
81 inline bool PyUnicode_Check_Py2Py3(PyObject *o) {
82 #if PY_MAJOR_VERSION < 3
83  return PyUnicode_Check(o) || PyString_Check(o);
84 #else
85  return PyUnicode_Check(o);
86 #endif
87 }
88 
89 // we cannot return char* here since for python 3 this would lead to a reference to a temporary
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);
94  if(!retc) return "";
95  return retc;
96  }
97  PyObject *str=PyUnicode_AsUTF8String(o);
98  if(!str) return "";
99  char *retc=PyString_AsString(str);
100  if(!retc) {
101  Py_DECREF(str);
102  return "";
103  }
104  std::string ret=retc;
105  Py_DECREF(str);
106  return ret;
107 #else
108  char *retc=PyUnicode_AsUTF8(o);
109  if(!retc) return "";
110  return retc;
111 #endif
112 }
113 
114 // now define the wrappers without the suffix: now the wrappers are active!!!!!
115 
116 #undef PyLong_Check
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
122 
123 // make PyRun_String a function
124 inline PyObject* PyRun_String_func(const char *str, int start, PyObject *globals, PyObject *locals) { return PyRun_String(str, start, globals, locals); }
125 #undef PyRun_String
126 #define PyRun_String PythonCpp::PyRun_String_func
127 
128 // make PyFloat_Check a function
129 inline int PyFloat_Check_func(PyObject *p) { return PyFloat_Check(p); }
130 #undef PyFloat_Check
131 #define PyFloat_Check PythonCpp::PyFloat_Check_func
132 
133 // make PyFloat_AS_DOUBLE a function
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
137 
138 // make PyBool_Check a function
139 inline int PyBool_Check_func(PyObject *p) { return PyBool_Check(p); }
140 #undef PyBool_Check
141 #define PyBool_Check PythonCpp::PyBool_Check_func
142 
143 // make PyList_Check a function
144 inline int PyList_Check_func(PyObject *p) { return PyList_Check(p); }
145 #undef PyList_Check
146 #define PyList_Check PythonCpp::PyList_Check_func
147 
148 // make PyObject_TypeCheck a function
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
152 
153 // make Py_InitModule a function
154 inline PyObject* Py_InitModule_func(char *name, PyMethodDef *methods) { return Py_InitModule(name, methods); }
155 #undef Py_InitModule
156 #define Py_InitModule PythonCpp::Py_InitModule_func
157 
158 // we use this for python object for c++ reference counting
159 class PyO {
160  public:
161  constexpr PyO() = default;
162  explicit PyO(PyObject *src, bool srcIsBorrowedRef=false) : p(src) { if(srcIsBorrowedRef) Py_XINCREF(p); } // use srcIsBorrowedRef=true if src is a borrowed reference
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); } // use srcIsBorrowedRef=true if src is a borrowed reference
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; } // use incRef=true if the caller steals a reference of the returned PyObject
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; } // use if the caller steals a reference of the returned PyObject
175  operator bool() const { return p!=nullptr; }
176  protected:
177  PyObject *p{nullptr};
178 };
179 
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(); }
186 
187 // A Python error exception object.
188 // Stores the file and line number of the C++ file where the error occured.
189 // Stores also the Python error objects type, value and traceback.
190 class PythonException : public std::exception {
191  public:
192  inline PythonException(const char *file_, int line_);
193  ~PythonException() noexcept override = default;
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  inline const char* what() const noexcept override;
200  private:
201  std::string file;
202  int line;
203  PyO type, value, traceback;
204  mutable std::string msg;
205 };
206 
207 // check for a python exception and throw a PythonException if one exists
208 inline void checkPythonError() {
209  if(PyErr_Occurred())
210  throw PythonException("", 0);
211 }
212 
213 // helper struct to map the return type of the callPy function
214 // default: map to the same type
215 template<typename T>
216 struct MapRetType {
217  typedef T type;
218  inline static type convert(const T &r) {
219  return r;
220  }
221 };
222 // specialization: map PyObject* to PyO
223 template<> struct MapRetType<PyObject*> {
224  typedef PyO type;
225  inline static PyO convert(PyObject *r) {
226  if(!r)
227  throw std::runtime_error("Internal error: Expected python object but got NULL pointer and no python exception is set.");
228  if(Py_REFCNT(r)<=0)
229  throw std::runtime_error("Internal error: Expected python object but a python object with 0 refcount and no python exception is set.");
230  return PyO(r);
231  }
232 };
233 
234 // helper function to map the argument types of the callPy function
235 // default: map to the same type
236 template<typename Arg>
237 inline Arg convertArg(const Arg &arg) {
238  return arg;
239 }
240 // specialization:: map PyO to PyObject*
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.");
244  return o.get();
245 }
246 // specialization:: map std::string to const char*
247 inline const char* convertArg(const std::string &o) {
248  return o.c_str();
249 }
250 
251 // Call Python function func with arguments args.
252 // Use the macro CALLPY or CALLPYB, see below.
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))...);
256  if(PyErr_Occurred())
257  throw PythonException(file, line);
258  return MapRetType<PyRet>::convert(ret);
259 }
260 
261 // Macro to call callPy(...)
262 // Use this macro to call a python function returning a new reference to a python object or any other return type.
263 // Note, if the python function steals a reference of any of this arguments you have to call arg.incRef() on
264 // each such arguments.
265 #define CALLPY(...) PythonCpp::callPy(__FILE__, __LINE__, __VA_ARGS__)
266 
267 // Macro to call callPy(...).incRef()
268 // Use this macro to call a python function returning a borrowed reference to a python object.
269 // Note, if the python function steals a reference of any of this arguments you have to call arg.incRef() on
270 // each such arguments.
271 #define CALLPYB(...) PythonCpp::callPy(__FILE__, __LINE__, __VA_ARGS__).incRef()
272 
273 // c++ PythonException exception with the content of a python exception
274 PythonException::PythonException(const char *file_, int line_) : file(file_), line(line_) {
275  // fetch if error has occured
276  if(!PyErr_Occurred())
277  throw std::runtime_error("Internal error: PythonException object created but no python error occured.");
278  // fetch error objects and save objects
279  PyObject *type_, *value_, *traceback_;
280  PyErr_Fetch(&type_, &value_, &traceback_);
281  type=PyO(type_);
282  value=PyO(value_);
283  traceback=PyO(traceback_);
284 }
285 
286 const inline char* PythonException::what() const noexcept {
287  if(!msg.empty())
288  return msg.c_str();
289 
290  // redirect stderr
291  PyObject *savedstderr=PySys_GetObject(const_cast<char*>("stderr"));
292  if(!savedstderr)
293  return "Unable to create Python error message: no sys.stderr available.";
294  PyObject *io=PyImport_ImportModule("io");
295  if(!io)
296  return "Unable to create Python error message: cannot load io module.";
297 #if PY_MAJOR_VERSION < 3
298  PyObject *fileIO=PyObject_GetAttrString(io, "BytesIO"); // sys.stderr is a file is bytes mode
299 #else
300  PyObject *fileIO=PyObject_GetAttrString(io, "StringIO"); // sys.stderr is a file in text mode
301 #endif
302  if(!fileIO)
303  return "Unable to create Python error message: cannot get in memory file class.";
304  Py_DECREF(io);
305  PyObject *buf=PyObject_CallObject(fileIO, nullptr);
306  Py_DECREF(fileIO);
307  if(!buf)
308  return "Unable to create Python error message: cannot create new in memory file instance";
309  if(PySys_SetObject(const_cast<char*>("stderr"), buf)!=0)
310  return "Unable to create Python error message: cannot redirect stderr";
311  // restore error
312  PyErr_Restore(type.get(), value.get(), traceback.get());
313  Py_XINCREF(type.get());
314  Py_XINCREF(value.get());
315  Py_XINCREF(traceback.get());
316  // print to redirected stderr
317  PyErr_Print();
318  // unredirect stderr
319  if(PySys_SetObject(const_cast<char*>("stderr"), savedstderr)!=0)
320  return "Unable to create Python error message: cannot revert redirect stderr";
321  // get redirected output as string
322  PyObject *getvalue=PyObject_GetAttrString(buf, "getvalue");
323  if(!getvalue)
324  return "Unable to create Python error message: cannot get getvalue attribute";
325  PyObject *pybufstr=PyObject_CallObject(getvalue, nullptr);
326  if(!pybufstr)
327  return "Unable to create Python error message: cannot get string from in memory file output";
328  Py_DECREF(getvalue);
329  Py_DECREF(buf);
330 #if PY_MAJOR_VERSION < 3
331  char *strc=PyBytes_AsString(pybufstr); // sys.stderr is a file in bytes mode
332  if(!strc)
333  return "Unable to create Python error message: cannot get c string";
334  std::string str(strc);
335 #else
336  std::string str=PyUnicode_AsUTF8(pybufstr); // sys.stderr is a file in text mode
337  if(PyErr_Occurred())
338  return "Unable to create Python error message: cannot get c string";
339 #endif
340  Py_DECREF(pybufstr);
341  std::stringstream strstr;
342  strstr<<"Python exception";
343  if(!file.empty())
344  strstr<<" at "<<file<<":"<<line;
345  strstr<<":"<<std::endl<<str;
346  msg=strstr.str();
347  return msg.c_str();
348 }
349 
350 // a Py_BuildValue variant working like called with CALLPY
351 template<typename... Args>
352 PyO Py_BuildValue_(const char *format, Args&&... args) {
353  return PyO(Py_BuildValue(format, convertArg(std::forward<Args>(args))...));
354 }
355 
356 }
357 
358 #endif
Definition: py2py3cppwrapper.h:159
Definition: py2py3cppwrapper.h:190
Definition: py2py3cppwrapper.h:216

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home