MIRA
PythonInterpreter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Redistribution and modification of this code is strictly prohibited.
9  *
10  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
11  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
12  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
13  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
16  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
18  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
19  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
20  */
21 
30 #ifndef _MIRA_TOOLBOXES_PYTHON_PYTHONINTERPRETER_H_
31 #define _MIRA_TOOLBOXES_PYTHON_PYTHONINTERPRETER_H_
32 
33 #include <Python.h>
34 
36 
37 #include <error/Logging.h>
38 #include <error/Exceptions.h>
39 #include <utils/Singleton.h>
40 #include <python/PythonException.h>
41 #include <python/ScopedGIL.h>
42 
43 namespace mira { namespace python {
44 
46 
47 class PythonInterpreter : public LazySingleton<PythonInterpreter>
48 {
49 public:
50 
54  boost::python::object getMainModule() { return mMainModule; }
58  boost::python::dict getGlobalDict() { return boost::python::extract<boost::python::dict>(mGlobalDict); }
62  boost::python::dict getLocalDict() { return getGlobalDict().copy(); }
66  boost::python::object importModule(const std::string & name);
70  void extendPath(const std::string& path);
71 
72 protected:
73 
76 
78  boost::python::object mMainModule;
79  boost::python::object mGlobalDict;
80 
86  PyThreadState* mMainThreadState;
87 };
88 
90 
91 }
92 
93 } // namespace
94 
95 #endif
boost::python::object mGlobalDict
Definition: PythonInterpreter.h:79
void extendPath(const std::string &path)
Extend the list of search paths for python modules by the given path.
Definition: PythonInterpreter.h:47
boost::python::dict getLocalDict()
Get a local copy of the global dict (dict) of the main module.
Definition: PythonInterpreter.h:62
boost::python::object getMainModule()
Get the main (main) module.
Definition: PythonInterpreter.h:54
PyThreadState * mMainThreadState
PyEval_ReleaseLock() is deprecated and should not be used anymore.
Definition: PythonInterpreter.h:86
Exception handling for python exceptions.
boost::python::dict getGlobalDict()
Get the global dict (dict) of the main module.
Definition: PythonInterpreter.h:58
Include this instead of boost/python.hpp to reduce compile time warning spam from Boost internal inco...
boost::python::object mMainModule
Definition: PythonInterpreter.h:78
boost::python::object importModule(const std::string &name)
Import a python module.