[capi-sig] DECREF when using PyDict_SetItem...

Stefan Behnel stefan_ml at behnel.de
Fri Jul 2 11:44:21 CEST 2010


Tim Golden, 02.07.2010 11:14:
> I'm using PyDict_SetItemString to add a long from a C struct
> into a Python dictionary. The full code is in the usn2dict
> function in here:
>
> http://svn.timgolden.me.uk/extensions/change-journal/_usn.c
>
> but the sort of thing I'm talking about is this:
>
> PyDict_SetItemString (
> dict,
> "RecordLength",
> PyLong_FromLongLong (usn_record->RecordLength)
> );
>
> My understanding is that PyLong_FromLongLong passes its reference
> to my function, which then passes it to PyDict_Set... which INCREFs
> it. That means, I think, that my function should DECREF it before
> exit since I'm only creating it to store it in the dict. Is that correct?
>
> In other words, should my code really do this?
>
> record_length = PyLong_FromLongLong (usn_record->RecordLength);
> PyDict_SetItemString (dict, "RecordLength", record_length);
> Py_DECREF (record_length)

Yes.

Stefan


More information about the capi-sig mailing list