From 7ec86ca55e92e4d331ce1f19d67ff342a5aadf76 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 26 Apr 2017 17:17:38 +0800 Subject: [PATCH] mgr/MgrPyModule: no need to keep pClass and pModule around Signed-off-by: Kefu Chai --- src/mgr/MgrPyModule.cc | 10 +++++----- src/mgr/MgrPyModule.h | 2 -- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/mgr/MgrPyModule.cc b/src/mgr/MgrPyModule.cc index 2997be1b27b8b..078f7f503503f 100644 --- a/src/mgr/MgrPyModule.cc +++ b/src/mgr/MgrPyModule.cc @@ -50,7 +50,7 @@ std::string handle_pyerror() #define dout_prefix *_dout << "mgr " << __func__ << " " MgrPyModule::MgrPyModule(const std::string &module_name_) - : module_name(module_name_), pModule(nullptr), pClass(nullptr), + : module_name(module_name_), pClassInstance(nullptr) {} @@ -60,8 +60,6 @@ MgrPyModule::~MgrPyModule() gstate = PyGILState_Ensure(); Py_XDECREF(pClassInstance); - Py_XDECREF(pClass); - Py_XDECREF(pModule); PyGILState_Release(gstate); } @@ -70,7 +68,7 @@ int MgrPyModule::load() { // Load the module PyObject *pName = PyString_FromString(module_name.c_str()); - pModule = PyImport_Import(pName); + auto pModule = PyImport_Import(pName); Py_DECREF(pName); if (pModule == nullptr) { derr << "Module not found: '" << module_name << "'" << dendl; @@ -79,7 +77,8 @@ int MgrPyModule::load() // Find the class // TODO: let them call it what they want instead of just 'Module' - pClass = PyObject_GetAttrString(pModule, (const char*)"Module"); + auto pClass = PyObject_GetAttrString(pModule, (const char*)"Module"); + Py_DECREF(pModule); if (pClass == nullptr) { derr << "Class not found in module '" << module_name << "'" << dendl; return -EINVAL; @@ -91,6 +90,7 @@ int MgrPyModule::load() 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) { diff --git a/src/mgr/MgrPyModule.h b/src/mgr/MgrPyModule.h index b466d1ac20f48..7d91275eacb4b 100644 --- a/src/mgr/MgrPyModule.h +++ b/src/mgr/MgrPyModule.h @@ -43,8 +43,6 @@ class MgrPyModule { private: const std::string module_name; - PyObject *pModule; - PyObject *pClass; PyObject *pClassInstance; std::vector commands; -- 2.39.5