Help with C API

John E. Barham jbarham at jbarham.com
Sat May 1 15:20:44 EDT 2004


"Nick Jacobson" wrote:
> I don't think I'm stating my question clearly. :(
>
> Say I create a list in my C function:
> seq = PyList_New(0);
> Then append some numbers to it, whatever.
>
> Then, when I return from the C function and want to use that list in
> my Python code, do I have to increment its refcount:
> return Py_BuildValue("O", seq);
>
> Or do I leave it alone and say:
> return seq;
>
> Thanks in advance.

Grepping through the modules shipped w/ Python seems to suggest that you
should just return it since calling PyList_New() returns a new reference.
See for example its use in the strop module (http://tinyurl.com/25s4e) which
does something similar to what you describe.

HTH,

    John





More information about the Python-list mailing list