- Change alive references for pyobjects

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@581 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-05-14 15:35:27 +00:00
parent 4ca52deb1b
commit e3724108c0
2 changed files with 13 additions and 12 deletions

View File

@ -31,7 +31,7 @@ using namespace std;
// *strong* exception checking / handling! // *strong* exception checking / handling!
PythonPolicy::PythonPolicy(const char* name) 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* pLoadmeStr = PyString_FromString(name);
PyObject* pUserPolicyModule = PyImport_Import(pLoadmeStr); PyObject* pUserPolicyModule = PyImport_Import(pLoadmeStr);
@ -45,26 +45,24 @@ PythonPolicy::PythonPolicy(const char* name)
} }
// Dictionary with defined ``symbols'' for .pyc file // Dictionary with defined ``symbols'' for .pyc file
PyObject* pUserPolicyDict = PyModule_GetDict(pUserPolicyModule); _upolicy_dict = PyModule_GetDict(pUserPolicyModule);
assert(pUserPolicyDict); assert(_upolicy_dict);
// Loads ScriptAdapter module and get its dictionary // Loads ScriptAdapter module and get its dictionary
pLoadmeStr = PyString_FromString("ScriptAdapter"); pLoadmeStr = PyString_FromString("ScriptAdapter");
PyObject* pScriptAdapterModule = PyImport_Import(pLoadmeStr); PyObject* pScriptAdapterModule = PyImport_Import(pLoadmeStr);
Py_DECREF(pLoadmeStr); Py_DECREF(pLoadmeStr);
assert(pScriptAdapterModule); assert(pScriptAdapterModule);
_adapter_dict = PyModule_GetDict(pScriptAdapterModule); PyObject* pAdapterDict = PyModule_GetDict(pScriptAdapterModule);
assert(_adapter_dict); assert(pAdapterDict);
// We want to keep a reference to it
Py_INCREF(_adapter_dict);
// Now takes the user-defined policy class from pUserPolicyDict // 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 assert(pPolicyClass); // FIXME needs stricter checking and exception throwing
// Creates a new object of type ScriptAdapter : // Creates a new object of type ScriptAdapter :
// takes init function from ScriptAdapter class // takes init function from ScriptAdapter class
PyObject* pAdapterClass = PyDict_GetItemString(_adapter_dict, "ScriptAdapter"); PyObject* pAdapterClass = PyDict_GetItemString(pAdapterDict, "ScriptAdapter");
PyObject* pAdapterCtorParam = PyTuple_New(1); PyObject* pAdapterCtorParam = PyTuple_New(1);
Py_INCREF(pPolicyClass); // PyTuple_SetItem steals a reference Py_INCREF(pPolicyClass); // PyTuple_SetItem steals a reference
PyTuple_SetItem(pAdapterCtorParam, 0, pPolicyClass); PyTuple_SetItem(pAdapterCtorParam, 0, pPolicyClass);
@ -72,7 +70,6 @@ PythonPolicy::PythonPolicy(const char* name)
Py_DECREF(pAdapterCtorParam); Py_DECREF(pAdapterCtorParam);
assert(_adapter); assert(_adapter);
Py_DECREF(pUserPolicyModule);
Py_DECREF(pScriptAdapterModule); Py_DECREF(pScriptAdapterModule);
// And now, who's your daddy, huh? // And now, who's your daddy, huh?
@ -82,7 +79,11 @@ PythonPolicy::PythonPolicy(const char* name)
PythonPolicy::~PythonPolicy() PythonPolicy::~PythonPolicy()
{ {
if(_adapter) Py_DECREF(_adapter); 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);
} }

View File

@ -80,8 +80,8 @@ namespace sgpem
void wait_unlock() const throw(UserInterruptException); void wait_unlock() const throw(UserInterruptException);
PyObject* _upolicy_dict;
PyObject* _adapter; PyObject* _adapter;
PyObject* _adapter_dict;
Glib::ustring _name; Glib::ustring _name;
}; };