473,408 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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
Jul 18 '05 #1
2 1744
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

Jul 18 '05 #2
se******@spawar.navy.mil (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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gyger | last post by:
Hello, Three weeks ago, I have started to develop a binding extension for Qt and PHP 5. Now, I can display a dialog box containing some widgets like label, buttons and edit line. I have just...
2
by: Lee Stewart | last post by:
Another question about writing PHP 5 extensions. What's the difference in PHP_MINIT() / PHP_MSHUTDOWN() and PHP_RINIT() / PHP_RSHUTDOWN()? When would a "per request" init be made? Thanks,...
8
by: Torsten Mohr | last post by:
Hi, i write an extension module in C at the moment. This module does some work on some own data types that consist of some values. The functions that can change the data are written in C. ...
3
by: man-in-nature | last post by:
Hello, I have already read several existing posts about xsd:extension, but do not find something useful to my test case. I have one xml file and one xsd file. I can use a simple command line...
7
by: Chris Kennedy | last post by:
Does anyone know a regular expression that will validate the file extension but also allow multiple file extensions if necessary. It also needs to be case insensitive. Basically, what I want is to...
0
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService"...
2
by: Samuel Siren | last post by:
Have just read about LINQ ant the new planned features of C# 3.0. I think it's fantastic, but I have a few questions about extension methods: Question 1: Syntax ------------------ Why is the...
5
by: malkarouri | last post by:
Hi everyone, Is it possible to write a Python extension that uses the Boehm garbage collector? I have a C library written that makes use of boehm-gc for memory management. To use that, I have...
13
by: llothar | last post by:
On windows everything is '.pyd' but there seems to be two ways to get this on unix? Why and what is the rule?
4
by: Steffen Bobek | last post by:
Extension methods are made for use with instances. I'd like to "misuse" them as static methods, too. Let me tell you my ambition: I use an extension method to serialize objects somehow like this:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.