mbxmlutils  1.3.0
Multi-Body XML Utils
pycppwrapper.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 _PYCPPWRAPPER_H_
31 #define _PYCPPWRAPPER_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 #include <boost/filesystem/path.hpp>
42 #include <boost/filesystem.hpp>
43 #include <cfenv>
44 
45 #if PY_MAJOR_VERSION < 3
46  #error "This file can only handle python >= 3"
47 #endif
48 
49 namespace PythonCpp {
50 
51 // make PyRun_String a function
52 inline PyObject* PyRun_String_func(const char *str, int start, PyObject *globals, PyObject *locals) { return PyRun_String(str, start, globals, locals); }
53 #undef PyRun_String
54 #define PyRun_String PythonCpp::PyRun_String_func
55 
56 // make PyFloat_Check a function
57 inline int PyFloat_Check_func(PyObject *p) { return PyFloat_Check(p); }
58 #undef PyFloat_Check
59 #define PyFloat_Check PythonCpp::PyFloat_Check_func
60 
61 // make PyFloat_AS_DOUBLE a function
62 inline int PyFloat_AS_DOUBLE_func(PyObject *p) { return PyFloat_AS_DOUBLE(p); }
63 #undef PyFloat_AS_DOUBLE
64 #define PyFloat_AS_DOUBLE PythonCpp::PyFloat_AS_DOUBLE_func
65 
66 // make PyBool_Check a function
67 inline int PyBool_Check_func(PyObject *p) { return PyBool_Check(p); }
68 #undef PyBool_Check
69 #define PyBool_Check PythonCpp::PyBool_Check_func
70 
71 // make PyList_Check a function
72 inline int PyList_Check_func(PyObject *p) { return PyList_Check(p); }
73 #undef PyList_Check
74 #define PyList_Check PythonCpp::PyList_Check_func
75 
76 // make PyObject_TypeCheck a function
77 inline int PyObject_TypeCheck_func(PyObject *p, PyTypeObject *type) { return PyObject_TypeCheck(p, type); }
78 #undef PyObject_TypeCheck
79 #define PyObject_TypeCheck PythonCpp::PyObject_TypeCheck_func
80 
81 // make PyLong_Check a function
82 inline int PyLong_Check_func(PyObject *p) { return PyLong_Check(p); }
83 #undef PyLong_Check
84 #define PyLong_Check PythonCpp::PyLong_Check_func
85 
86 // make PyUnicode_Check a function
87 inline int PyUnicode_Check_func(PyObject *p) { return PyUnicode_Check(p); }
88 #undef PyUnicode_Check
89 #define PyUnicode_Check PythonCpp::PyUnicode_Check_func
90 
91 // we use this for python object for c++ reference counting
92 class PyO {
93  public:
94  constexpr PyO() = default;
95  explicit PyO(PyObject *src, bool srcIsBorrowedRef=false) : p(src) { if(srcIsBorrowedRef) Py_XINCREF(p); } // use srcIsBorrowedRef=true if src is a borrowed reference
96  PyO(const PyO &r) : p(r.p) { Py_XINCREF(p); }
97  PyO(PyO &&r) noexcept : p(r.p) { r.p=nullptr; }
98  ~PyO() { Py_XDECREF(p); }
99  PyO& operator=(const PyO &r) { Py_XDECREF(p); p=r.p; Py_XINCREF(p); return *this; }
100  PyO& operator=(PyO &&r) noexcept { Py_XDECREF(p); p=r.p; r.p=nullptr; return *this; }
101  void reset() { Py_XDECREF(p); p=nullptr; }
102  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
103  void swap(PyO &r) { PyObject *temp=p; p=r.p; r.p=temp; }
104  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
105  long use_count() const { return p ? Py_REFCNT(p) : 0; }
106  PyObject* operator->() const { return p; }
107  PyO& incRef() { Py_XINCREF(p); return *this; } // use if the caller steals a reference of the returned PyObject
108  operator bool() const { return p!=nullptr; }
109  protected:
110  PyObject *p{nullptr};
111 };
112 
113 inline bool operator==(const PyO& l, const PyO& r) { return l.get()==r.get(); }
114 inline bool operator!=(const PyO& l, const PyO& r) { return l.get()!=r.get(); }
115 inline bool operator< (const PyO& l, const PyO& r) { return l.get()< r.get(); }
116 inline bool operator> (const PyO& l, const PyO& r) { return l.get()> r.get(); }
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 
120 // A Python error exception object.
121 // Stores the file and line number of the C++ file where the error occured.
122 // Stores also the Python error objects type, value and traceback.
123 class PythonException : public std::exception {
124  public:
125  inline PythonException(const char *file_, int line_);
126  ~PythonException() noexcept override = default;
127  std::string getFile() { return file; }
128  int getLine() { return line; }
129  PyO getType() { return type; }
130  PyO getValue() { return value; }
131  PyO getTraceback() { return traceback; }
132  inline const char* what() const noexcept override;
133  private:
134  std::string file;
135  int line;
136  PyO type, value, traceback;
137  mutable std::string msg;
138 };
139 
140 // check for a python exception and throw a PythonException if one exists
141 inline void checkPythonError() {
142  if(PyErr_Occurred())
143  throw PythonException("", 0);
144 }
145 
146 // helper struct to map the return type of the callPy function
147 // default: map to the same type
148 template<typename T>
149 struct MapRetType {
150  using type = T;
151  inline static type convert(const T &r) {
152  return r;
153  }
154 };
155 // specialization: map PyObject* to PyO
156 template<> struct MapRetType<PyObject*> {
157  using type = PyO;
158  inline static PyO convert(PyObject *r) {
159  if(!r)
160  throw std::runtime_error("Internal error: Expected python object but got NULL pointer and no python exception is set.");
161  if(Py_REFCNT(r)<=0)
162  throw std::runtime_error("Internal error: Expected python object but a python object with 0 refcount and no python exception is set.");
163  return PyO(r);
164  }
165 };
166 
167 // helper function to map the argument types of the callPy function
168 // default: map to the same type
169 template<typename Arg>
170 inline Arg convertArg(const Arg &arg) {
171  return arg;
172 }
173 // specialization:: map PyO to PyObject*
174 inline PyObject* convertArg(const PyO &o) {
175  if(o && o.use_count()<=0)
176  throw std::runtime_error("Internal error: access object with reference count <= 0. Check the source code.");
177  return o.get();
178 }
179 // specialization:: map std::string to const char*
180 inline const char* convertArg(const std::string &o) {
181  return o.c_str();
182 }
183 
184 // Call Python function func with arguments args.
185 // Use the macro CALLPY or CALLPYB, see below.
186 template<typename PyRet, typename... PyArgs, typename... CallArgs>
187 inline typename MapRetType<PyRet>::type callPy(const char *file, int line, PyRet (*func)(PyArgs...), CallArgs&&... args) {
188 #if !defined(_WIN32) && !defined(NDEBUG)
189  // sympy/numpy/others may generate FPEs during calls -> disable FPE exceptions during load
190  int fpeExcept=fedisableexcept(FE_OVERFLOW | FE_INVALID | FE_OVERFLOW);
191  assert(fpeExcept!=-1);
192 #endif
193  PyRet ret=func(convertArg(std::forward<CallArgs>(args))...);
194 #if !defined(_WIN32) && !defined(NDEBUG)
195  assert(feenableexcept(fpeExcept)!=-1);
196 #endif
197  if(PyErr_Occurred())
198  throw PythonException(file, line);
199  return MapRetType<PyRet>::convert(ret);
200 }
201 
202 // Macro to call callPy(...)
203 // Use this macro to call a python function returning a new reference to a python object or any other return type.
204 // Note, if the python function steals a reference of any of this arguments you have to call arg.incRef() on
205 // each such arguments.
206 #define CALLPY(...) PythonCpp::callPy(__FILE__, __LINE__, __VA_ARGS__)
207 
208 // Macro to call callPy(...).incRef()
209 // Use this macro to call a python function returning a borrowed reference to a python object.
210 // Note, if the python function steals a reference of any of this arguments you have to call arg.incRef() on
211 // each such arguments.
212 #define CALLPYB(...) PythonCpp::callPy(__FILE__, __LINE__, __VA_ARGS__).incRef()
213 
214 // Initialize python giving main as program name to python.
215 // All path in sysPathAppend are added to python's sys.path array.
216 // If PYTHONHOME is not set all possiblePrefix dirs are tested for a possible PYTHONHOME
217 // and if one is found envvar is set
218 inline void initializePython(const boost::filesystem::path &main, const std::string &pythonVersion,
219  const std::vector<boost::filesystem::path> &sysPathAppend={},
220  const std::vector<boost::filesystem::path> &possiblePrefix={}) {
221  boost::filesystem::path PYTHONHOME;
222  if(!getenv("PYTHONHOME")) {
223  for(auto &p : possiblePrefix) {
224  if(boost::filesystem::is_directory(p/"lib64"/("python"+pythonVersion)/"encodings") ||
225  boost::filesystem::is_directory(p/"lib"/("python"+pythonVersion)/"encodings") ||
226 #ifdef _WIN32
227  boost::filesystem::exists(p/"bin"/"python.exe") ||
228  boost::filesystem::exists(p/"bin"/"python3.exe") ||
229  boost::filesystem::exists(p/"bin"/"python.bat") ||
230  boost::filesystem::exists(p/"bin"/"python3.bat")
231 #else
232  boost::filesystem::exists(p/"bin"/"python") ||
233  boost::filesystem::exists(p/"bin"/"python3")
234 #endif
235  ) {
236  PYTHONHOME=p;
237  break;
238  }
239  }
240  if(!PYTHONHOME.empty()) {
241  // the string for putenv must have program life time
242  static std::string PYTHONHOME_ENV(std::string("PYTHONHOME=")+PYTHONHOME.string());
243  putenv((char*)PYTHONHOME_ENV.c_str());
244  }
245  }
246 
247  static auto mainW=boost::locale::conv::utf_to_utf<wchar_t>(main.string());
248  #if __GNUC__ >= 11
249  // python >= 3.8 has deprecated this call
250  #pragma GCC diagnostic push
251  #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
252  #endif
253  Py_SetProgramName(const_cast<wchar_t*>(mainW.c_str()));
254  #if __GNUC__ >= 11
255  #pragma GCC diagnostic pop
256  #endif
257  Py_InitializeEx(0);
258 
259 #ifdef _WIN32
260  boost::filesystem::path dllDir("bin");
261  std::string pathsep(";");
262 #else
263  boost::filesystem::path dllDir("lib");
264  std::string pathsep(":");
265 #endif
266  if(!PYTHONHOME.empty()) {
267 #if _WIN32 && PY_MAJOR_VERSION==3 && PY_MINOR_VERSION>=8
268  PyO os=CALLPY(PyImport_ImportModule, "os");
269  PyO os_add_dll_directory=CALLPY(PyObject_GetAttrString, os, "add_dll_directory");
270  PyO arg(CALLPY(PyTuple_New, 1));
271  PyO libdir(CALLPY(PyUnicode_FromString, (PYTHONHOME/dllDir).string()));
272  CALLPY(PyTuple_SetItem, arg, 0, libdir.incRef());
273  CALLPY(PyObject_CallObject, os_add_dll_directory, arg);
274 #else
275  // the string for putenv must have program life time
276  std::string PATH_OLD(getenv("PATH"));
277  static std::string PATH_ENV("PATH="+PATH_OLD+pathsep+(PYTHONHOME/dllDir).string());
278  putenv((char*)PATH_ENV.c_str());
279 #endif
280  }
281 
282  // add to sys.path
283  PyO sysPath(CALLPYB(PySys_GetObject, const_cast<char*>("path")));
284  for(auto &p : sysPathAppend)
285  CALLPY(PyList_Append, sysPath, CALLPY(PyUnicode_FromString, p.string()));
286 }
287 
288 // c++ PythonException exception with the content of a python exception
289 PythonException::PythonException(const char *file_, int line_) : file(file_), line(line_) {
290  // fetch if error has occured
291  if(!PyErr_Occurred())
292  throw std::runtime_error("Internal error: PythonException object created but no python error occured.");
293  // fetch error objects and save objects
294  PyObject *type_, *value_, *traceback_;
295  PyErr_Fetch(&type_, &value_, &traceback_);
296  type=PyO(type_);
297  value=PyO(value_);
298  traceback=PyO(traceback_);
299 }
300 
301 const inline char* PythonException::what() const noexcept {
302  if(!msg.empty())
303  return msg.c_str();
304 
305  // redirect stderr
306  PyObject *savedstderr=PySys_GetObject(const_cast<char*>("stderr"));
307  if(!savedstderr)
308  return "Unable to create Python error message: no sys.stderr available.";
309  PyObject *io=PyImport_ImportModule("io");
310  if(!io)
311  return "Unable to create Python error message: cannot load io module.";
312  PyObject *fileIO=PyObject_GetAttrString(io, "StringIO"); // sys.stderr is a file in text mode
313  if(!fileIO)
314  return "Unable to create Python error message: cannot get in memory file class.";
315  Py_DECREF(io);
316  PyObject *buf=PyObject_CallObject(fileIO, nullptr);
317  Py_DECREF(fileIO);
318  if(!buf)
319  return "Unable to create Python error message: cannot create new in memory file instance";
320  if(PySys_SetObject(const_cast<char*>("stderr"), buf)!=0)
321  return "Unable to create Python error message: cannot redirect stderr";
322  // restore error
323  PyErr_Restore(type.get(), value.get(), traceback.get());
324  Py_XINCREF(type.get());
325  Py_XINCREF(value.get());
326  Py_XINCREF(traceback.get());
327  // print to redirected stderr
328  PyErr_Print();
329  // unredirect stderr
330  if(PySys_SetObject(const_cast<char*>("stderr"), savedstderr)!=0)
331  return "Unable to create Python error message: cannot revert redirect stderr";
332  // get redirected output as string
333  PyObject *getvalue=PyObject_GetAttrString(buf, "getvalue");
334  if(!getvalue)
335  return "Unable to create Python error message: cannot get getvalue attribute";
336  PyObject *pybufstr=PyObject_CallObject(getvalue, nullptr);
337  if(!pybufstr)
338  return "Unable to create Python error message: cannot get string from in memory file output";
339  Py_DECREF(getvalue);
340  Py_DECREF(buf);
341  std::string str=PyUnicode_AsUTF8(pybufstr); // sys.stderr is a file in text mode
342  if(PyErr_Occurred())
343  return "Unable to create Python error message: cannot get c string";
344  Py_DECREF(pybufstr);
345  std::stringstream strstr;
346  strstr<<"Python exception";
347  if(!file.empty())
348  strstr<<" at "<<file<<":"<<line;
349  strstr<<":"<<std::endl<<str;
350  msg=strstr.str();
351  return msg.c_str();
352 }
353 
354 // a Py_BuildValue variant working like called with CALLPY
355 template<typename... Args>
356 PyO Py_BuildValue_(const char *format, Args&&... args) {
357  return PyO(Py_BuildValue(format, convertArg(std::forward<Args>(args))...));
358 }
359 
360 }
361 
362 #endif
Definition: pycppwrapper.h:92
Definition: pycppwrapper.h:123
Definition: pycppwrapper.h:149