C API: PyXXX_SetItem

Frank Sonnenburg frank.sonnenburg at biosolveit.de
Thu Jun 27 12:30:19 EDT 2002


Hi all,

am i right, that PyTuple_SetItem() and PyList_SetItem() only *transfer* 
references to objects, but don't increment reference counts?

And, in contrast, PyDict_SetItem() *does* increment the object reference 
count?


So,


        PyTuple_SetItem(tup,idx,PyInt_FromLong(123));

is perfect, but


        PyDict_SetItemString(dict,"key_name",PyInt_FromLong(123));

will lead to a memory leak. To avoid this:


        PyObject *py_int;
        py_int = PyInt_FromLong(123);
        PyDict_SetItemString(dict,"key_name",py_int);
        Py_DECREF(py_int);


I would appreciate any suggestions/corrections to this point. Thanks in 
advance

Frank Sonnenburg
-- 
____________________________________________________________________________
       _________
               / _________          Frank Sonnenburg
           Bio \ \                  Software Developer
       ________/ / olve
                 \________          BioSolveIT GmbH, An der Ziegelei 75

     I N F O R M A T I O N -        53757 Sankt Augustin, Germany
     - - T E C H N O L O G Y
                                    Tel: +49 - 2241 - 973 66 81
                                    Fax: +49 - 2241 - 973 66 88
       www.biosolveit.de            frank.sonnenburg at biosolveit.de
____________________________________________________________________________





More information about the Python-list mailing list