OpenLexocad  27.1
pythonize.h
Go to the documentation of this file.
1 /*
2 copyright 2003 Jim Bublitz <jbublitz@nwinternet.com>
3 
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8 
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13 
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 */
19 
20 #ifndef __pythonize_h__
21 #define __pythonize_h__
22 
23 // Pythonize is a general purpose library that wraps the Python
24 // interpreter with an interface to a set of common operations
25 // used when embedding the interpreter.
26 
27 #ifdef slots
28 #undef slots
29 #endif
30 
31 #ifdef _DEBUG
32 #undef _DEBUG
33 #include <Python.h>
34 #define _DEBUG
35 #else
36 #include <Python.h>
37 #endif
38 
39 #ifndef signals
40 #define signals Q_SIGNALS
41 #endif
42 
43 #ifndef slots
44 #define slots Q_SLOTS
45 #endif
46 
47 #include <Base/String.h>
48 
49 namespace Core
50 {
51 struct ObjectRef
52 {
53  ObjectRef(ObjectRef* oi, PyObject* o);
54  ~ObjectRef();
55 
56  PyObject* object = nullptr; // pointer to an object we created
57  ObjectRef* prevObject = nullptr; // pointer to next object on the stack
58 };
59 
60 class LX_CORE_EXPORT Pythonize
61 {
62 public:
63  explicit Pythonize(const Base::String& pythonHome);
64  ~Pythonize();
65 
66  // adds a path to sys.path
67  bool appendToSysPath(const Base::String& newPath) const;
68 
69  // insert a path to sys.path
70  bool insertToSysPath(int pos, const Base::String& newPath) const;
71 
72  // imports a module into the interpreter
73  // or gets a PyObject for an already loaded module
74  PyObject* importModule(char* moduleName);
75 
76  // returns an object from a loaded module
77  // you must decref the object returned when done with it (new reference returned)
78  PyObject* getNewObjectRef(PyObject* module, char* object) const;
79  PyObject* getSysModule() const;
80  PyObject* getMainModule() const;
81 
82  void setMainModule();
83  bool getPythonInit() const;
84 
85  // runs a script on the current sys.path
86  bool runScript(const Base::String& scr) const;
87  bool runScript2(const Base::String& scr, Base::String& error) const;
88 
89  // executes a string of Python in the interpreter
90  bool runString(const Base::String& str) const;
91  bool runString2(const Base::String& str, Base::String& err) const;
92  bool runString3(std::string input, std::string resultname, bool& result) const;
93  bool runStringWithNameSpace(char* str, char* ns) const;
94 
95  std::string getPythonErrorString() const;
96 
97  // runs a callable Python object
98  PyObject* runFunction(PyObject* object, PyObject* args) const;
99 
100  // handle the thread state and global interpreter lock
101  void releaseLock() const;
102  void acquireLock() const;
103  PyThreadState* getThreadState() const;
104  PyThreadState* setThreadState(PyThreadState* tstate) const;
105 
106  int runExpression(const Base::String& command, double& val) const;
107 
108 private:
109  bool _pythonInit = false; // status of Py_Initialize
110  PyObject* _sysModule = nullptr; // a pointer to the sys module which is always loaded first
111  PyObject* _mainModule = nullptr; // a pointer to __main__
112  ObjectRef* _objects = nullptr; // a stack of PyObjects (used in destructor)
113 };
114 } // namespace Core
115 
116 #endif
ObjectRef * prevObject
Definition: pythonize.h:57
Definition: pythonize.h:51
A Utf-16 (windows) or ucs4 (unix) encoded string class.
Definition: String.h:23
Definition: Base.h:12
struct _object PyObject
Definition: PyExport.h:35
Definition: pythonize.h:60
ObjectRef(ObjectRef *oi, PyObject *o)