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 <boost/optional.hpp>
34 
35 #include <Python.h>
36 
38 
39 #include <error/Logging.h>
40 #include <error/Exceptions.h>
41 #include <utils/Singleton.h>
42 #include <utils/Path.h>
43 #include <python/PythonException.h>
44 #include <python/ScopedGIL.h>
45 
46 namespace mira { namespace python {
47 
49 
50 class PythonEnvironment : public EagerSingleton<PythonEnvironment>
51 {
52 public:
56  void setVirtualEnvironmentPath(const Path& path);
57 
59  boost::optional<Path> getVirtualEnvironmentPath() const { return mVirtualEnvironmentPath; }
60 
61  // Tell the singleton, that the Python interpreter was initialized
62  void setInterpreterInitialized() { mInterpreterInitialized = true; }
63 
64 private:
65  bool mInterpreterInitialized = false;
66  boost::optional<Path> mVirtualEnvironmentPath;
67 };
68 
70 
71 class PythonInterpreter : public LazySingleton<PythonInterpreter>
72 {
73 public:
74 
78  boost::python::object getMainModule() { return mMainModule; }
82  boost::python::dict getGlobalDict() { return boost::python::extract<boost::python::dict>(mGlobalDict); }
86  boost::python::dict getLocalDict() { return getGlobalDict().copy(); }
90  boost::python::object importModule(const std::string & name);
94  void extendPath(const std::string& path);
95 
96 protected:
97 
100 
102  boost::python::object mMainModule;
103  boost::python::object mGlobalDict;
104 
110  PyThreadState* mMainThreadState;
111 };
112 
114 
115 }
116 
117 } // namespace
118 
119 #endif
boost::python::object mGlobalDict
Definition: PythonInterpreter.h:103
Definition: PythonInterpreter.h:50
boost::filesystem::path Path
void extendPath(const std::string &path)
Extend the list of search paths for python modules by the given path.
boost::optional< Path > getVirtualEnvironmentPath() const
Return the configured virtual Python environment.
Definition: PythonInterpreter.h:59
Definition: PythonInterpreter.h:71
boost::python::dict getLocalDict()
Get a local copy of the global dict (dict) of the main module.
Definition: PythonInterpreter.h:86
boost::python::object getMainModule()
Get the main (main) module.
Definition: PythonInterpreter.h:78
PyThreadState * mMainThreadState
PyEval_ReleaseLock() is deprecated and should not be used anymore.
Definition: PythonInterpreter.h:110
Exception handling for python exceptions.
boost::python::dict getGlobalDict()
Get the global dict (dict) of the main module.
Definition: PythonInterpreter.h:82
void setVirtualEnvironmentPath(const Path &path)
Set the virtual Python environment, which should be used by the Python interpreter.
Include this instead of boost/python.hpp to reduce compile time warning spam from Boost internal inco...
boost::python::object mMainModule
Definition: PythonInterpreter.h:102
boost::python::object importModule(const std::string &name)
Import a python module.
void setInterpreterInitialized()
Definition: PythonInterpreter.h:62