How send LIST to C extension?

C

Christian Seberino

I already sent a post about problems sending list as argument in a C extension.

I think I'll start over and just ask if anyone has done this successfully.

Can you send me code that does this??

(I tried PyArg_ParseTuple("[ii]",...) but apparently there is more to it than
just adding brackets.

chris
 
V

vincent wehren

| I already sent a post about problems sending list as argument in a C
extension.
|
| I think I'll start over and just ask if anyone has done this successfully.
|
| Can you send me code that does this??
|
| (I tried PyArg_ParseTuple("[ii]",...) but apparently there is more to it
than
| just adding brackets.


In your function you need something like

PyObject* seq;

if(!PyArg_ParsTuple(args, "O", &seq))
return 0;


To do something useful with seq, you might want to look at the PySequence_*
function family in the Python/C API Reference Manual (s.
http://www.python.org/doc/current/api/sequence.html)

HTH,
Vincent Wehren





|
| chris
 
C

Christian Seberino

Vincent

Thanks. Your example with "0" will make *seq point to the list I believe.

Can I extract the elements of the list into C variables

with PyArg_ParseTuple if I didn't want to work with PyObject directly??

Perhaps this is a bad idea and your way is always better???

(python.org docs had example of extracting elements of a tuple
with PyArg_ParseTuple but I couldn't do it.)

Chris
 
V

vincent wehren

| Vincent
|
| Thanks. Your example with "0" will make *seq point to the list I believe.

Make sure it's "O" (the letter O) not "0"...



|
| Can I extract the elements of the list into C variables
|
| with PyArg_ParseTuple if I didn't want to work with PyObject directly??

Sure. That's the whole point... You can use seq (which is a PyObject) and
subject it to all kinds
of functions ( e.g. PyList_GET_ITEM(seq, i) which retuns the i'th item of
list seq ))


|
| Perhaps this is a bad idea and your way is always better???
| (python.org docs had example of extracting elements of a tuple
| with PyArg_ParseTuple but I couldn't do it.)

So, what did you try?

Vincent Wehren


|
| Chris
|
|
|
| > | > | I already sent a post about problems sending list as argument in a C
| > extension.
| > |
| > | I think I'll start over and just ask if anyone has done this
successfully.
| > |
| > | Can you send me code that does this??
| > |
| > | (I tried PyArg_ParseTuple("[ii]",...) but apparently there is more to
it
| > than
| > | just adding brackets.
| >
| >
| > In your function you need something like
| >
| > PyObject* seq;
| >
| > if(!PyArg_ParsTuple(args, "O", &seq))
| > return 0;
| >
| >
| > To do something useful with seq, you might want to look at the
PySequence_*
| > function family in the Python/C API Reference Manual (s.
| > http://www.python.org/doc/current/api/sequence.html)
| >
| > HTH,
| > Vincent Wehren
| >
| >
| >
| >
| >
| > |
| > | chris
 
C

Christian Seberino

Vincent

Why would you extract elements of list/tuples with your
way instead of PyArg_ParseTuple?? (I finally got
PyArg_ParseTuple to work for tuples.)

Chris
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

Why would you extract elements of list/tuples with your
way instead of PyArg_ParseTuple??

There are many reasons to do tuple access explicitly, and actually few
reasons to have PyArg_ParseTuple to do it for you.

You might want to do it explicitly if:
- you don't know the total number of elements in the tuple in advance.
- you are willing to accept not just tuples, but arbitrary sequences,
for better polymorphism.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top