Skip to content
Snippets Groups Projects
Commit c7c7e385 authored by Dr. Felix Tobias Schindler's avatar Dr. Felix Tobias Schindler
Browse files

[interpreter] add a global Python interpreter instance

parent 736cf88e
No related branches found
No related tags found
2 merge requests!10Fix compilation with new clang,!6Merge pybind
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
# Authors: # Authors:
# Felix Schindler (2016) # Felix Schindler (2016)
dune_library_add_sources(dunepybindxi SOURCES interpreter.cc)
install(FILES install(FILES
attr.h attr.h
buffer_info.h buffer_info.h
...@@ -16,6 +18,7 @@ install(FILES ...@@ -16,6 +18,7 @@ install(FILES
embed.h embed.h
eval.h eval.h
functional.h functional.h
interpreter.hh
iostream.h iostream.h
numpy.h numpy.h
operators.h operators.h
......
// This file is part of the dune-pybindxi project:
// https://github.com/dune-community/dune-pybindxi
// Copyright 2009-2017 dune-xt-la developers and contributors. All rights
// reserved.
// License: Dual licensed as BSD 2-Clause License
// (http://opensource.org/licenses/BSD-2-Clause)
// or GPL-2.0+ (http://opensource.org/licenses/gpl-license)
// with "runtime exception" (http://www.dune-project.org/license.html)
// Authors:
// Felix Schindler (2017)
#include "interpreter.hh"
namespace Dune {
namespace PybindXI {
pybind11::module
ScopedInterpreter::import_module(const std::string &module_name) {
auto result = modules_.find(module_name);
if (result != modules_.end())
return result->second;
else {
modules_[module_name] = pybind11::module::import(module_name.c_str());
return modules_[module_name];
}
} // ... load_module(...)
ScopedInterpreter &GlobalInterpreter() {
static ScopedInterpreter global_interpreter;
return global_interpreter;
}
} // namespace PybindXI
} // namespace Dune
// This file is part of the dune-pybindxi project:
// https://github.com/dune-community/dune-pybindxi
// Copyright 2009-2017 dune-xt-la developers and contributors. All rights
// reserved.
// License: Dual licensed as BSD 2-Clause License
// (http://opensource.org/licenses/BSD-2-Clause)
// or GPL-2.0+ (http://opensource.org/licenses/gpl-license)
// with "runtime exception" (http://www.dune-project.org/license.html)
// Authors:
// Felix Schindler (2017)
#ifndef DUNE_PYBINDXI_INTERPRETER_HH
#define DUNE_PYBINDXI_INTERPRETER_HH
#include <map>
#include <string>
#include "embed.h"
namespace Dune {
namespace PybindXI {
/**
* \note Most likely, you do not want to use this class directly, but
* GlobalInterpreter instead!
*/
class ScopedInterpreter {
public:
pybind11::module import_module(const std::string &module_name);
private:
pybind11::scoped_interpreter interpreter_;
std::map<std::string, pybind11::module> modules_;
}; // class ScopedInterpreter
ScopedInterpreter &GlobalInterpreter();
} // namespace PybindXI
} // namespace Dune
#endif // DUNE_PYBINDXI_INTERPRETER_HH
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment