[capi-sig] Reference counts and dictionary creadtion

Dan Colesworthy ColesworthyD at Otologics.com
Tue Dec 29 21:38:30 CET 2009



I am attempting to write a python extension in C to "glue" the ASIO SDK
to python 2.5.4.  However I am really confused about reference counts.
Below is a code snippet that "seems" to work, however I suspect that
some of the object reference counts have been incremented too many


static PyObject *

getAsioInfo(PyObject *self, PyObject *args) {

   PyObject *dict = Py_None;

   if (loadAsioDriver (ASIO_DRIVER_NAME)) {

      if (ASIOInit (&asioDriverInfo.driverInfo) == ASE_OK){

         dict = PyDict_New();



PyInt_FromLong(asioDriverInfo.driverInfo.asioVersion) );



PyString_FromString(asioDriverInfo.driverInfo.name) );

         return dict;



   return Null;



As nearly as I can tell from the Python source code, PyDict_SetItem will
do a Py_INCREF on both the key and value parameters.  However, it looks
as if PyInt_FromLong will incref the object it is returning - at least
if it is a "small value".  Am I on the right track here?  Or am I better
off doing something like:


            PyObject *str, *lng;


            str = PyString_FromString("asioVersion");

            lng = PyInt_FromLong(asioDriverInfo.driverInfo.asioVersion)

PyDict_SetItem(dict, str, lng);




Thanks in advance for all advice!


Dan Colesworthy



More information about the capi-sig mailing list