[Tutor] Help with returning a list object from a C extension.

James Oren contactjamesoren at gmail.com
Sat Nov 28 01:04:23 EST 2015


Hi all, this is my first time using the mailing list.

I'm trying to learn how to use C to extend my code, and have already worked
out how to use ctypes. I'm now trying to learn the full C extension module
approach with Python.h and have worked through the Python doc and a couple
other examples. They all use examples where the C function expects a
primitive, so now I'd like to learn how to use a Python list object. I
guess that a C-based numpy array may be easier to write for, but just for
now I'd like to stick to lists.

So far I've worked out to use the parse tuple command with the "O" argument
and I think thanks to an archived post on this mailing list I've worked out
how to get the Python list data into a C array and pass it to the function
to modify, now I just need to return a list object of the updated C array
data.

I'd like it to be that calling this function from Python has the same
effect as modifying the list in-place, so I guess I need to either empty
the original and repopulate it with the c array data, or build a new list
object with the data and reassign it to the original list name .... Which
seems like it'd be troublesome? Unfortunately I've hit a brick wall and
can't find any examples :-(

Any help would be greatly appreciated!

Jim


More information about the Tutor mailing list