diff options
Diffstat (limited to 'externals/grill/py/source/main.cpp')
-rw-r--r-- | externals/grill/py/source/main.cpp | 81 |
1 files changed, 60 insertions, 21 deletions
diff --git a/externals/grill/py/source/main.cpp b/externals/grill/py/source/main.cpp index fadb0173..fa5169b7 100644 --- a/externals/grill/py/source/main.cpp +++ b/externals/grill/py/source/main.cpp @@ -288,12 +288,9 @@ void pybase::SetArgs() bool pybase::ImportModule(const char *name) { if(!name) return false; - - SetArgs(); - module = PyImport_ImportModule((char *)name); // increases module_obj ref count by one - dict = module?PyModule_GetDict(module):NULL; - - return module != NULL; + if(modname == name) return true; + modname = name; + return ReloadModule(); } void pybase::UnimportModule() @@ -315,22 +312,22 @@ void pybase::UnimportModule() bool pybase::ReloadModule() { bool ok = false; - if(module) { - SetArgs(); - PyObject *newmod = PyImport_ReloadModule(module); - if(!newmod) { - // old module still exists?! -// dict = NULL; - } - else { - Py_XDECREF(module); - module = newmod; - dict = PyModule_GetDict(module); // borrowed - ok = true; - } + + SetArgs(); + PyObject *newmod = module + ?PyImport_ReloadModule(module) + :PyImport_ImportModule((char *)modname.c_str()); + + if(!newmod) { + // unload faulty module + if(module) UnimportModule(); + } + else { + Py_XDECREF(module); + module = newmod; + dict = PyModule_GetDict(module); // borrowed + ok = true; } - else - post("py/pyext - No module to reload"); return ok; } @@ -402,6 +399,48 @@ void pybase::Respond(bool b) } } +void pybase::Reload() +{ + PyThreadState *state = PyLockSys(); + + PyObject *reg = GetRegistry(REGNAME); + + if(reg) { + PyObject *key; + int pos = 0; + while(PyDict_Next(reg,&pos,&key,NULL)) { + pybase *th = (pybase *)PyLong_AsLong(key); + FLEXT_ASSERT(th); + th->Unload(); + } + + UnloadModule(); + } + + bool ok = ReloadModule(); + + if(ok) { + LoadModule(); + + if(reg) { + SetRegistry(REGNAME,reg); + + PyObject *key; + int pos = 0; + while(PyDict_Next(reg,&pos,&key,NULL)) { + pybase *th = (pybase *)PyLong_AsLong(key); + FLEXT_ASSERT(th); + th->Load(); + } + } + else + Load(); + } + + Report(); + PyUnlock(state); +} + static PyObject *output = NULL; |