passing list from C to Python

Miike Mueller pyp at gmx.net
Tue Dec 12 18:16:00 CET 2000


"Alex Martelli" <aleaxit at yahoo.com> schrieb im Newsbeitrag
news:9158la030jp at news2.newsguy.com...

>
> PyListObject *newList = PyObject_CallMethod(module_object,
>     "pythonModul", "(O)", list);
>
> Note that PyObject_CallMethod, much like Py_BuildValue, takes
> a format-string describing the following arguments, and builds
> the tuple according to it.  I think you do need the parenthesis
> in the case of a format-string describing just one object, to
> make sure it's still wrapped into a (1-element) tuple, which
> is how Python callables expect their arguments (but maybe the
> PyObject_CallMethod is smart enough to do it implicitly; the
> docs don't make it clear and I haven't checked the sources).
>

O works wihtout (O)

I managed to pass the list in. The calculations in Python works as expected.
I can get a list back from Python (checked with PyListCheck()).
The only problem remaining to convert the list back to the C-args.
I tried PyArgs_ParseTuple() but I could not get to work, the C-args just
stay the same as before.
PyListGetItem works but I have 35 arguments and writing one
PyArgs_ParseTuple() seems to be more elegant than 35 PyListGetGetItem and 35
PyFloat_AsDouble(). (Don't know how to loop over them.)


> I think that it would be even simpler if the Python code you
> call just modified its list-argument, but it's a minor issue.
I think this would the the simplest way.
I tried this by passing the list in as an argument and changing the list in
place but this has no effect an the *args in C. I am not sure if understand
the concept of pointers right here.

Mike






More information about the Python-list mailing list