diff --git a/src/Base/Interpreter.cpp b/src/Base/Interpreter.cpp index aabe9fc667..3edca84a6d 100644 --- a/src/Base/Interpreter.cpp +++ b/src/Base/Interpreter.cpp @@ -487,6 +487,19 @@ bool InterpreterSingleton::loadModule(const char* psModName) return true; } +PyObject* InterpreterSingleton::addModule(Py::ExtensionModuleBase* mod) +{ + _modules.push_back(mod); + return mod->module().ptr(); +} + +void InterpreterSingleton::cleanupModules() +{ + for (auto it : _modules) + delete it; + _modules.clear(); +} + void InterpreterSingleton::addType(PyTypeObject* Type,PyObject* Module, const char * Name) { // NOTE: To finish the initialization of our own type objects we must @@ -564,6 +577,7 @@ void InterpreterSingleton::finalize() { try { PyEval_RestoreThread(this->_global); + cleanupModules(); Py_Finalize(); } catch (...) { diff --git a/src/Base/Interpreter.h b/src/Base/Interpreter.h index dd3d101fd7..472b74602e 100644 --- a/src/Base/Interpreter.h +++ b/src/Base/Interpreter.h @@ -42,6 +42,7 @@ #endif #include +#include #include #include "Exception.h" @@ -240,6 +241,10 @@ public: /// Add an additional python path void addPythonPath(const char* Path); static void addType(PyTypeObject* Type,PyObject* Module, const char * Name); + /// Add a module and return a PyObject to it + PyObject* addModule(Py::ExtensionModuleBase*); + /// Clean-up registered modules + void cleanupModules(); //@} /** @name Cleanup @@ -311,6 +316,7 @@ protected: private: std::string _cDebugFileName; PyThreadState* _global; + std::list _modules; };