Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > C Extension question-> How handle lists and tuples as arguments?

Reply
Thread Tools

C Extension question-> How handle lists and tuples as arguments?

 
 
Christian Seberino
Guest
Posts: n/a
 
      10-08-2003
Py_ParseTuple is a great C function to set C variables from
Python objects in a C extension.

Usage is straight forward for integers and strings but I'm wondering
what to do if I ever want to send in a list or tuple.

First problem is that we don't know the LENGTH of the list/tuple.

Second problem is we don't know the TYPES of the elements.

Chris
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Gerhard_H=E4ring?=
Guest
Posts: n/a
 
      10-08-2003
Christian Seberino wrote:
> Py_ParseTuple is a great C function to set C variables from
> Python objects in a C extension.
>
> Usage is straight forward for integers and strings but I'm wondering
> what to do if I ever want to send in a list or tuple.
>
> First problem is that we don't know the LENGTH of the list/tuple.
>
> Second problem is we don't know the TYPES of the elements.


I don't understand your problem. For "other" parameters, use "O" as
format character in ParseTuple...(). Then if you need to check if the
passed parameter is a sequence (either tuple or list), you can just use
PySequenceCheck().

For the rest, use the functions for the sequence protocol, like
PySequence_Length() and the like.

HTH,

-- Gerhard

 
Reply With Quote
 
 
 
 
Martin v. =?iso-8859-15?q?L=F6wis?=
Guest
Posts: n/a
 
      10-09-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Christian Seberino) writes:

> Py_ParseTuple is a great C function to set C variables from
> Python objects in a C extension.


I disagree. It is much better to use specific functions.

> Usage is straight forward for integers and strings but I'm wondering
> what to do if I ever want to send in a list or tuple.
>
> First problem is that we don't know the LENGTH of the list/tuple.
>
> Second problem is we don't know the TYPES of the elements.


Just like you would do it in Python:

len(x) --> PyObject_Length(x)
x[i] --> PySequence_GetItem(x, i) (assuming x is a sequence)
isinstance(o, int) --> PyInt_Check(o)
obtain native value --> PyInt_AsLong(o)

HTH,
Martin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Dictionaries with tuples or tuples of tuples Jon Reyes Python 18 02-19-2013 03:56 AM
tuples within tuples korovev76@gmail.com Python 12 10-27-2007 08:16 PM
Re: Lists of lists and tuples, and finding things within them Daniel Nogradi Python 3 11-10-2006 07:57 AM
Different tuples to one container? (One type of a pointer to point to different kinds of tuples?) fff_afafaf@yahoo.com C++ 5 10-05-2006 11:17 PM



Advertisments