embedding problem with dictionary
Howard Lightstone
howard at eegsoftware.com
Thu Jul 25 17:34:09 EDT 2002
Following up on myself after "Finding the Problems" (two of them):
> pnd=PyDict_GetItemString(idict,basename); // borrowed reference
> if (!pnd)
> {
> // must "create" such a subdictionary
> pnd=PyDict_New(); // new reference
> // add reference into main dict
> PyDict_SetItemString(idict,basename,pnd);
> // always leave with valid pnd
> }
The problem here is that the dictionary-within-a-dictionary was sometimes
None. I was only checking for NULL output from PyDict_GetItemString.
The new test became
if (!pnd || pnd == Py_None)
This works because None is a singleton (it actually IS documented, just not
obvious the first time). And I forgot about None as a possibility.
The second problem was reference counting.
The code below worked sometimes but not others.
> static PyObject * idict;
>
> static PyObject *
> pyaccess_Run(PyObject *self, PyObject *args)
> {
>
> if (idict) <==REMOVED
> Py_DECREF(idict) <==REMOVED
> if (!PyArg_ParseTuple(args, "O",&idict))
> return NULL;
Of course, carefull reading of the docs again and again pointed me to the
fact that the outputs of PyArg_ParseTuple are BORROWED references. I
deleted the spurious DECREF above it and things began working reliably. I
assume that when it didn't work, the object had been destroyed somehow when
I erroneously DECREF'ed it.
Even though I have read the embedding docs many times, it seems I don't
"see" the key phrases when I need them. I need a little hammer that smacks
me on the head at those subtle points.
More information about the Python-list
mailing list