From e3724108c0b3b5cd6576ff1e8517faba1b8feda6 Mon Sep 17 00:00:00 2001 From: tchernobog Date: Sun, 14 May 2006 15:35:27 +0000 Subject: [PATCH] - Change alive references for pyobjects git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@581 3ecf2c5c-341e-0410-92b4-d18e462d057c --- plugins/pyloader/python_policy.cc | 23 ++++++++++++----------- plugins/pyloader/python_policy.hh | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) 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; };