mirror of
https://github.com/NGSolve/netgen.git
synced 2025-01-11 21:50:34 +05:00
ModuleScope for Python modules added
This commit is contained in:
parent
4fa1b1d9d1
commit
4bef2d7617
@ -2,11 +2,35 @@
|
||||
|
||||
#include <boost/python.hpp>
|
||||
namespace bp = boost::python;
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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<std::string>(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 <typename Function>
|
||||
|
Loading…
Reference in New Issue
Block a user