diff --git a/plugins/pyloader/python_policy.cc b/plugins/pyloader/python_policy.cc index dd30dd2..b4fc82e 100644 --- a/plugins/pyloader/python_policy.cc +++ b/plugins/pyloader/python_policy.cc @@ -31,7 +31,7 @@ using namespace std; // *strong* exception checking / handling! PythonPolicy::PythonPolicy(const char* name) - : _adapter(NULL), _adapter_dict(NULL), _name(name) + : _upolicy_dict(NULL), _adapter(NULL), _name(name) { PyObject* pLoadmeStr = PyString_FromString(name); PyObject* pUserPolicyModule = PyImport_Import(pLoadmeStr); @@ -45,26 +45,24 @@ PythonPolicy::PythonPolicy(const char* name) } // Dictionary with defined ``symbols'' for .pyc file - PyObject* pUserPolicyDict = PyModule_GetDict(pUserPolicyModule); - assert(pUserPolicyDict); + _upolicy_dict = PyModule_GetDict(pUserPolicyModule); + assert(_upolicy_dict); // Loads ScriptAdapter module and get its dictionary pLoadmeStr = PyString_FromString("ScriptAdapter"); PyObject* pScriptAdapterModule = PyImport_Import(pLoadmeStr); Py_DECREF(pLoadmeStr); assert(pScriptAdapterModule); - _adapter_dict = PyModule_GetDict(pScriptAdapterModule); - assert(_adapter_dict); - // We want to keep a reference to it - Py_INCREF(_adapter_dict); + PyObject* pAdapterDict = PyModule_GetDict(pScriptAdapterModule); + assert(pAdapterDict); // Now takes the user-defined policy class from pUserPolicyDict - PyObject* pPolicyClass = PyDict_GetItemString(pUserPolicyDict, name); + PyObject* pPolicyClass = PyDict_GetItemString(_upolicy_dict, name); assert(pPolicyClass); // FIXME needs stricter checking and exception throwing // Creates a new object of type ScriptAdapter : // takes init function from ScriptAdapter class - PyObject* pAdapterClass = PyDict_GetItemString(_adapter_dict, "ScriptAdapter"); + PyObject* pAdapterClass = PyDict_GetItemString(pAdapterDict, "ScriptAdapter"); PyObject* pAdapterCtorParam = PyTuple_New(1); Py_INCREF(pPolicyClass); // PyTuple_SetItem steals a reference PyTuple_SetItem(pAdapterCtorParam, 0, pPolicyClass); @@ -72,7 +70,6 @@ PythonPolicy::PythonPolicy(const char* name) Py_DECREF(pAdapterCtorParam); assert(_adapter); - Py_DECREF(pUserPolicyModule); Py_DECREF(pScriptAdapterModule); // And now, who's your daddy, huh? @@ -82,7 +79,11 @@ PythonPolicy::PythonPolicy(const char* name) PythonPolicy::~PythonPolicy() { if(_adapter) Py_DECREF(_adapter); - if(_adapter_dict) Py_DECREF(_adapter_dict); + + // We keep this alive until dtor time, because + // the user may have defined some static global-space + // variables and they make use of them. + if(_upolicy_dict) Py_DECREF(_upolicy_dict); } diff --git a/plugins/pyloader/python_policy.hh b/plugins/pyloader/python_policy.hh index 9d6aaa1..217b811 100644 --- a/plugins/pyloader/python_policy.hh +++ b/plugins/pyloader/python_policy.hh @@ -80,8 +80,8 @@ namespace sgpem void wait_unlock() const throw(UserInterruptException); + PyObject* _upolicy_dict; PyObject* _adapter; - PyObject* _adapter_dict; Glib::ustring _name; };