passing list from C to Python
Alex Martelli
aleaxit at yahoo.com
Tue Dec 12 08:09:25 EST 2000
"Mike Müller" <mmueller at dgfz.de> wrote in message
news:9154s2$2sb at micky.ibh-dd.de...
[snip]
> PyListObject *list = Py_BuildValue("[ff], *arg1, *arg2);
> PyErr_Clear();
>
> // the following does not work, need something that calls python function
> with list
> PyListObject *newList = PyObject_CallMethod(module_object,
> "pythonModul", list);
There's just a small error in the last call (also, this is C++, or
the-very-latest-C-standard -- in classic ISO C89, you have to declare
everything at the START of the block, *and* comments are /* ... */,
NOT //-to-end-of-line).
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).
> If there is any other simple way to do it, please let me know.
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.
Alex
More information about the Python-list
mailing list