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