memory leak - C API

Tim Peters at
Fri Dec 5 20:41:03 CET 2003

[John Hunter]
> ...
> My understanding is that PySequence_GetItem returns a borrowed ref so I
> don't need to do any memory management.

Where did that understanding come from?  It's important to track it down,
because whatever source you got it from is unreliable.  This one is such a
common misconception that the docs (Python C/API Reference Manual, section
"Reference Count Details") point it out explicitly:

    It is important to realize that whether you own a reference returned
    by a function depends on which function you call only -- the plumage
    (the type of the object passed as an argument to the function) doesn't
    enter into it!  Thus, if you extract an item from a list using
    PyList_GetItem(), you don't own the reference -- but if you obtain the
    same item from the same list using PySequence_GetItem() (which
    happens to take exactly the same arguments), you do own a reference
    to the returned object.

More information about the Python-list mailing list