- 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:
parent
4ca52deb1b
commit
e3724108c0
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -80,8 +80,8 @@ namespace sgpem
|
|||
|
||||
void wait_unlock() const throw(UserInterruptException);
|
||||
|
||||
PyObject* _upolicy_dict;
|
||||
PyObject* _adapter;
|
||||
PyObject* _adapter_dict;
|
||||
|
||||
Glib::ustring _name;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue