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

Oscar Benjamin oscar.j.benjamin at gmail.com
Sat Nov 28 13:43:56 EST 2015


On 28 Nov 2015 12:06, "James Oren" <contactjamesoren at gmail.com> wrote:
>
> 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 :-(

Can you post an example of a function written in Python that does what you
want?

Does PyObject_SetItem do what you want:
https://docs.python.org/3.5/c-api/object.html

--
Oscar


More information about the Tutor mailing list