diff --git a/libsrc/general/ngpython.hpp b/libsrc/general/ngpython.hpp index 12b31cec..2ccad390 100644 --- a/libsrc/general/ngpython.hpp +++ b/libsrc/general/ngpython.hpp @@ -2,11 +2,35 @@ #include namespace bp = boost::python; - +#include namespace netgen { + class ModuleScope { + bp::scope *local_scope; + public: + ModuleScope(const std::string name) : local_scope(nullptr) { + std::string nested_name = name; + if (bp::scope()) + nested_name = bp::extract(bp::scope().attr("__name__") + "." + name); + + bp::object module(bp::handle<>(bp::borrowed(PyImport_AddModule(nested_name.c_str())))); + + std::cout << "exporting " << nested_name << std::endl; + bp::object parent = bp::scope() ? bp::scope() : bp::import("__main__"); + parent.attr(name.c_str()) = module; + + local_scope = new bp::scope(module); + } + + ~ModuleScope() { + if (local_scope) + delete (local_scope); + } + + }; + ////////////////////////////////////////////////////////////////////// // Lambda to function pointer conversion template