diff options
Diffstat (limited to 'src/sim')
-rw-r--r-- | src/sim/init.cc | 24 | ||||
-rw-r--r-- | src/sim/init.hh | 11 |
2 files changed, 24 insertions, 11 deletions
diff --git a/src/sim/init.cc b/src/sim/init.cc index 0a15c384d..2e1dd629c 100644 --- a/src/sim/init.cc +++ b/src/sim/init.cc @@ -148,8 +148,8 @@ EmbeddedPython::initAll() return 0; } -EmbeddedSwig::EmbeddedSwig(void (*init_func)()) - : initFunc(init_func) +EmbeddedSwig::EmbeddedSwig(void (*init_func)(), const string& _context) + : initFunc(init_func), context(_context) { getList().push_back(this); } @@ -164,12 +164,22 @@ EmbeddedSwig::getList() void EmbeddedSwig::initAll() { - // initialize SWIG modules. initSwig() is autogenerated and calls + char* old_context = _Py_PackageContext; + // initialize SWIG modules. initFunc() is autogenerated and calls // all of the individual swig initialization functions. - list<EmbeddedSwig *>::iterator i = getList().begin(); - list<EmbeddedSwig *>::iterator end = getList().end(); - for (; i != end; ++i) - (*i)->initFunc(); + for (auto i : getList()) { + // to ensure that the loaded modules are placed in the right + // package we have to be a bit unorthodox and directly + // manipulate the package context since swig simply calls + // Py_InitModule with nothing but the module name of the + // wrapper + char* cstr = new char[i->context.size() + 1]; + strcpy(cstr, i->context.c_str()); + _Py_PackageContext = cstr; + i->initFunc(); + delete[] cstr; + } + _Py_PackageContext = old_context; } int diff --git a/src/sim/init.hh b/src/sim/init.hh index 766cb4365..2bbcd23da 100644 --- a/src/sim/init.hh +++ b/src/sim/init.hh @@ -33,10 +33,8 @@ #include <Python.h> -/* - * Data structure describing an embedded python file. - */ #include <list> +#include <string> #include <inttypes.h> @@ -45,6 +43,9 @@ struct _object; typedef _object PyObject; #endif +/* + * Data structure describing an embedded python file. + */ struct EmbeddedPython { const char *filename; @@ -70,7 +71,9 @@ struct EmbeddedSwig { void (*initFunc)(); - EmbeddedSwig(void (*init_func)()); + std::string context; + + EmbeddedSwig(void (*init_func)(), const std::string& _context); static std::list<EmbeddedSwig *> &getList(); static void initAll(); |