Fix global python modules.

This commit is contained in:
Alexey Sokolov
2011-08-10 01:04:58 +07:00
committed by Kyle Fuller
parent 76aaafd4d0
commit 0c9eae226e
4 changed files with 32 additions and 3 deletions

View File

@@ -61,3 +61,4 @@ void OnServerCapResult(const CString& sCap, bool bSuccess)
EModRet OnTimerAutoJoin(CChan& Channel)
bool OnEmbeddedWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl)=false
EModRet OnModuleLoading(const CString& sModName, const CString& sArgs, bool& bSuccess, CString& sRetMsg)

View File

@@ -73,6 +73,15 @@ namespace std {
}
}
%typemap(out) CString&, CString* {
if ($1) {
$result = CPyRetString::wrap(*$1);
} else {
$result = Py_None;
Py_INCREF(Py_None);
}
}
#define u_short unsigned short
#define u_int unsigned int
#include "../ZNCString.h"

View File

@@ -23,6 +23,13 @@ class CPyModule : public CModule {
CModPython* m_pModPython;
VWebSubPages* _GetSubPages();
public:
CPyModule(const CString& sModName, const CString& sDataPath,
PyObject* pyObj, CModule* pModPython)
: CModule(NULL, sModName, sDataPath) {
m_pyObj = pyObj;
Py_INCREF(pyObj);
m_pModPython = reinterpret_cast<CModPython*>(pModPython);
}
CPyModule(CUser* pUser, const CString& sModName, const CString& sDataPath,
PyObject* pyObj, CModule* pModPython)
: CModule(NULL, pUser, sModName, sDataPath) {
@@ -108,16 +115,21 @@ public:
virtual void OnServerCapResult(const CString& sCap, bool bSuccess);
virtual EModRet OnTimerAutoJoin(CChan& Channel);
bool OnEmbeddedWebRequest(CWebSock&, const CString&, CTemplate&);
EModRet OnModuleLoading(const CString& sModName, const CString& sArgs, bool& bSuccess, CString& sRetMsg);
};
static inline CPyModule* AsPyModule(CModule* p) {
return dynamic_cast<CPyModule*>(p);
}
inline CPyModule* CreatePyModule(CUser* pUser, const CString& sModName, const CString& sDataPath, PyObject* pyObj, CModule* pModPython) {
inline CPyModule* CreateUserPyModule(CUser* pUser, const CString& sModName, const CString& sDataPath, PyObject* pyObj, CModule* pModPython) {
return new CPyModule(pUser, sModName, sDataPath, pyObj, pModPython);
}
inline CPyModule* CreateGlobalPyModule(const CString& sModName, const CString& sDataPath, PyObject* pyObj, CModule* pModPython) {
return new CPyModule(sModName, sDataPath, pyObj, pModPython);
}
class CPyTimer : public CTimer {
PyObject* m_pyObj;
CModPython* m_pModPython;

View File

@@ -438,11 +438,15 @@ def load_module(modname, args, user, retmsg, modpython):
return 1
cl = pymodule.__dict__[modname]
module = cl()
module._cmod = CreatePyModule(user, modname, datapath, module, modpython)
if user:
module._cmod = CreateUserPyModule(user, modname, datapath, module, modpython)
else:
module._cmod = CreateGlobalPyModule(modname, datapath, module, modpython)
module.nv = ModuleNV(module._cmod)
module.SetDescription(cl.description)
module.SetArgs(args)
module.SetModPath(pymodule.__file__)
module.SetType(cl.module_type)
if user:
user.GetModules().push_back(module._cmod)
@@ -488,8 +492,11 @@ def load_module(modname, args, user, retmsg, modpython):
def unload_module(module):
module.OnShutdown()
cmod = module._cmod
if module.GetType() == ModuleTypeUser:
cmod.GetUser().GetModules().removeModule(cmod)
elif module.GetType() == ModuleTypeGlobal:
CZNC.Get().GetModules().removeModule(cmod)
del module._cmod
cmod.GetUser().GetModules().removeModule(cmod)
cmod.DeletePyModule()
del cmod