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

Tim Golden mail at timgolden.me.uk
Fri Jul 2 11:14:33 CEST 2010


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)


Thanks

TJG


More information about the capi-sig mailing list