OpenLexocad  27.0
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 #include <Python.h>
32 
33 #ifndef signals
34 #define signals Q_SIGNALS
35 #endif
36 
37 #ifndef slots
38 #define slots Q_SLOTS
39 #endif
40 
41 #include <Base/String.h>
42 
43 namespace Core
44 {
45 struct ObjectRef
46 {
47  ObjectRef(ObjectRef* oi, PyObject* o);
48  ~ObjectRef();
49 
50  PyObject* object = nullptr; // pointer to an object we created
51  ObjectRef* prevObject = nullptr; // pointer to next object on the stack
52 };
53 
55 {
56 public:
57  explicit Pythonize(const Base::String& pythonHome);
58  ~Pythonize();
59 
60  // adds a path to sys.path
61  bool appendToSysPath(const Base::String& newPath) const;
62 
63  // insert a path to sys.path
64  bool insertToSysPath(int pos, const Base::String& newPath) const;
65 
66  // imports a module into the interpreter
67  // or gets a PyObject for an already loaded module
68  PyObject* importModule(char* moduleName);
69 
70  // returns an object from a loaded module
71  // you must decref the object returned when done with it (new reference returned)
72  PyObject* getNewObjectRef(PyObject* module, char* object) const;
73  PyObject* getSysModule() const;
74  PyObject* getMainModule() const;
75 
76  void setMainModule();
77  bool getPythonInit() const;
78 
79  // runs a script on the current sys.path
80  bool runScript(const Base::String& scr) const;
81  bool runScript2(const Base::String& scr, Base::String& err) const;
82 
83  // executes a string of Python in the interpreter
84  bool runString(const Base::String& str) const;
85  bool runString2(const Base::String& str, Base::String& err) const;
86  bool runString3(std::string input, std::string resultname, bool& result) const;
87  bool runStringWithNameSpace(char* str, char* ns) const;
88 
89  std::string getPythonErrorString() const;
90 
91  // runs a callable Python object
92  PyObject* runFunction(PyObject* object, PyObject* args) const;
93 
94  // handle the thread state and global interpreter lock
95  void releaseLock() const;
96  void acquireLock() const;
97  PyThreadState* getThreadState() const;
98  PyThreadState* setThreadState(PyThreadState* tstate) const;
99 
100  int runExpression(const Base::String& command, double& val) const;
101 
102 private:
103  bool _pythonInit = false; // status of Py_Initialize
104  PyObject* _sysModule = nullptr; // a pointer to the sys module which is always loaded first
105  PyObject* _mainModule = nullptr; // a pointer to __main__
106  ObjectRef* _objects = nullptr; // a stack of PyObjects (used in destructor)
107 };
108 } // namespace Core
109 
110 #endif
#define CORE_EXPORT
Definition: core_defines2.h:10
ObjectRef * prevObject
Definition: pythonize.h:51
Definition: pythonize.h:45
A Utf-16 (windows) or ucs4 (unix) encoded string class.
Definition: String.h:29
Definition: Base.h:19
struct _object PyObject
Definition: PyExport.h:35
Definition: pythonize.h:54
ObjectRef(ObjectRef *oi, PyObject *o)