From 88f532c937763e6953f3cbbe5a0b52efdbf827f4 Mon Sep 17 00:00:00 2001 From: bhavishyagopesh Date: Mon, 9 Oct 2017 11:10:41 +0530 Subject: [PATCH] Module classes are not forced to be called "Module" Signed-off-by: bhavishyagopesh --- src/mgr/MgrPyModule.cc | 46 ++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/mgr/MgrPyModule.cc b/src/mgr/MgrPyModule.cc index 49b70d8a24bbd3..8229a2fb9497c2 100644 --- a/src/mgr/MgrPyModule.cc +++ b/src/mgr/MgrPyModule.cc @@ -172,31 +172,43 @@ int MgrPyModule::load() // Find the class // TODO: let them call it what they want instead of just 'Module' - auto pClass = PyObject_GetAttrString(pModule, (const char*)"Module"); + auto tempClass = PyObject_GetAttrString(pModule, (const char*)"MgrModule"); Py_DECREF(pModule); - if (pClass == nullptr) { + + auto pClassList = tempClass->tp_subclasses; + + if (pClassList == nullptr) { derr << "Class not found in module '" << module_name << "'" << dendl; derr << handle_pyerror() << dendl; return -EINVAL; } - - // Just using the module name as the handle, replace with a - // uuidish thing if needed - auto pyHandle = PyString_FromString(module_name.c_str()); - auto pArgs = PyTuple_Pack(1, pyHandle); - pClassInstance = PyObject_CallObject(pClass, pArgs); - Py_DECREF(pClass); - Py_DECREF(pyHandle); - Py_DECREF(pArgs); - if (pClassInstance == nullptr) { - derr << "Failed to construct class in '" << module_name << "'" << dendl; - derr << handle_pyerror() << dendl; - return -EINVAL; - } else { - dout(1) << "Constructed class from module: " << module_name << dendl; + int n = PyList_GET_SIZE(pClassList); + + for (i = 0; i < n; i++){ + PyTypeObject *pClass; + pClass = (PyTypeObject *)PyList_GET_ITEM(pClassList, i); + + // Just using the module name as the handle, replace with a + // uuidish thing if needed + auto pyHandle = PyString_FromString(module_name.c_str()); + auto pArgs = PyTuple_Pack(1, pyHandle); + pClassInstance = PyObject_CallObject(pClass, pArgs); + Py_DECREF(pClass); + Py_DECREF(pyHandle); + Py_DECREF(pArgs); + if (pClassInstance == nullptr) { + derr << "Failed to construct class in '" << module_name << "'" << dendl; + derr << handle_pyerror() << dendl; + return -EINVAL; + } else { + dout(1) << "Constructed class from module: " << module_name << dendl; + } + } + Py_DECREF(pClassList); + return load_commands(); }