Base: let interpreter keep track of created modules

This commit is contained in:
wmayer
2022-03-13 22:34:01 +01:00
parent 2908150282
commit a22608b7b5
2 changed files with 20 additions and 0 deletions

View File

@@ -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 (...) {

View File

@@ -42,6 +42,7 @@
#endif
#include <CXX/Extensions.hxx>
#include <list>
#include <string>
#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<Py::ExtensionModuleBase*> _modules;
};