Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
module.hh
Go to the documentation of this file.
1// -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set ts=8 sw=4 et sts=4:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUFEM_PYTHON_MODULE_HH
8#define DUNE_FUFEM_PYTHON_MODULE_HH
9
10// Only introduce the dune-python interface if python
11// was found and enabled.
12#if HAVE_PYTHON || DOXYGEN
13
14#include <Python.h>
15
16#include <string>
17#include <sstream>
18#include <vector>
19#include <map>
20
27
28#include <dune/grid/common/gridfactory.hh>
29
31
32
33namespace Python
34{
35
36// forward declarations
37void handlePythonError(const std::string& origin, const std::string& message);
38void run(const std::string& code);
39
40
41
48class Module :
49 public Reference
50{
51 public:
52
61 {
62 public:
70 {
71 if (module_)
72 Module(module_).run(this->str());
73 else
74 Python::run(this->str());
75 }
76
82
87 module_(module)
88 {}
89
98 module_(other.module_)
99 {}
100
101 private:
102 Reference module_;
103 };
104
105
106
107 public:
108
113 Reference()
114 {}
115
141 Module(PyObject* p) :
142 Reference(p)
143 {
144 assertModule(p_, "Module(PyObject*)");
145 if (p_)
146 dict_ = get("__dict__");
147 }
148
160 Module(const Reference& other) :
161 Reference(other)
162 {
163 assertModule(p_, "Module(Reference&)");
164 if (p_)
165 dict_ = get("__dict__");
166 }
167
171 virtual ~Module()
172 {}
173
180 virtual Module& operator= (const Reference& other)
181 {
182 assertModule(other, "Module::operator=(Reference&)");
184 if (p_)
185 dict_ = get("__dict__");
186 return *this;
187 }
188
197 virtual void run(const std::string& code)
198 {
199 assertPyObject("Module::run()");
200 if (not PyRun_String(code.c_str(), Py_file_input, dict_, dict_))
201 handlePythonError("Module::run()", Dune::formatString("failed to execute the following code '%s'", code.c_str()));
202 }
203
214 {
215 assertPyObject("Module::runStream()");
216 return AutoRunCodeStream(*this);
217 }
218
228 void runFile(const std::string& fileName)
229 {
230 assertPyObject("Module::runFile()");
231 // the last argument specifies that the file should be closed
232 // after executing the code
233 PyRun_FileEx(fopen(fileName.c_str(), "r"), fileName.c_str(), Py_file_input, dict_, dict_, 1);
234 }
235
245 Reference evaluate(const std::string& expression)
246 {
247 return PyRun_String(expression.c_str(), Py_eval_input, dict_, dict_);
248 }
249
250
261 Module importAs(Module module, const std::string& importedName)
262 {
263 if (not module)
264 handlePythonError("Module::importAs(Module,string)", "failed to import module");
265 set(importedName, module);
266 return module;
267 }
268
279 Module importAs(const std::string& moduleName, const std::string& importedName)
280 {
281 Module module(PyImport_ImportModule(moduleName.c_str()));
282 return importAs(module, importedName);
283 }
284
294 Module import(Module module)
295 {
296 if (not module)
297 handlePythonError("Module::import(Module)", "failed to import module");
298 return importAs(module, module.get("__name__").str());
299 }
300
310 Module import(const std::string& moduleName)
311 {
312 return importAs(moduleName, moduleName);
313 }
314
315 protected:
316
323 static void assertModule(PyObject* p, const std::string& origin)
324 {
325 if (p and (not PyModule_Check(p)))
327 "Python error occurred." << std::endl <<
328 " Origin: " << origin << std::endl <<
329 " Error: Trying to use a non-module as module");
330 }
331
333};
334
335
336} // end of namespace Python
337
338
339
340#else
341 #warning dunepython.hh was included but python was not found or enabled!
342#endif // DUNE_FUFEM_PYTHON_MODULE_HH
343
344
345#endif
346
static std::string formatString(const std::string &s, const T &... args)
void message(const std::string &msg)
#define DUNE_THROW(E,...)
Definition callable.hh:34
void run(const std::string &code)
Run python code given as string.
Definition common.hh:223
void handlePythonError(const std::string &origin, const std::string &message)
If a python error occurred throw an exception and clear python error indicator.
Definition common.hh:589
Wrapper class for python modules.
Definition module.hh:50
virtual void run(const std::string &code)
Run python code given as string.
Definition module.hh:197
Module(PyObject *p)
Construct Module from PyObject*.
Definition module.hh:141
virtual Module & operator=(const Reference &other)
Assignment.
Definition module.hh:180
Reference dict_
Definition module.hh:332
virtual ~Module()
Destructor.
Definition module.hh:171
Module importAs(Module module, const std::string &importedName)
Import another module into this one.
Definition module.hh:261
Reference evaluate(const std::string &expression)
Evaluate python expression given as string.
Definition module.hh:245
Module()
Construct empty Module.
Definition module.hh:112
Module importAs(const std::string &moduleName, const std::string &importedName)
Import another module into this one.
Definition module.hh:279
AutoRunCodeStream runStream()
Obtain a stream to feed the code with multiple lines of python code.
Definition module.hh:213
Module(const Reference &other)
Construct Module from Reference.
Definition module.hh:160
void runFile(const std::string &fileName)
Run python code in file given by name.
Definition module.hh:228
static void assertModule(PyObject *p, const std::string &origin)
Assert that internal PyObject* is not NULL and module and raise exception otherwise.
Definition module.hh:323
A stream that executed all python code it is fed with on destruction.
Definition module.hh:61
AutoRunCodeStream()
Default constructor.
Definition module.hh:80
AutoRunCodeStream(const AutoRunCodeStream &other)
Copy constructor.
Definition module.hh:97
AutoRunCodeStream(Reference &module)
Default constructor.
Definition module.hh:86
~AutoRunCodeStream()
Destructor.
Definition module.hh:69
Wrapper class for python objects.
Definition reference.hh:77
void set(const std::string &name, const V &value)
Set attribute of given name.
Definition reference.hh:258
Reference get(const std::string &name) const
Query attribute of given name.
Definition reference.hh:236
virtual Reference & operator=(const Reference &other)
Assignment.
Definition reference.hh:145
PyObject * p_
Definition reference.hh:299
void assertPyObject(const std::string &origin) const
Assert that internal PyObject* is not NULL and raise exception otherwise.
Definition reference.hh:288
std::string str() const
String representation of this object.
Definition reference.hh:268
T c_str(T... args)
T endl(T... args)