473,396 Members | 1,982 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,396 software developers and data experts.

Module Extension C/CPI Question

Just a quick question before I waste time doing something that will
immediately be disregarded as unacceptable code... :)

When using the C API and writing extension modules, how do you normally
pass a structure up into the python module? For instance, if I have a
structure:

typedef struct Foo {
int x;
int y;
int z;
char xyz[100];
} Foo;

Is there an "accepted" way of propagating this upstream? I was thinking
one of these two:

1. Returning a enormous tuple of the struct's members using
Py_BuildValue("(iiis)", f.x, f.y, f.z, f.xyz) which would later be
converted to a similar object in Python for clients of my module to use.

2. Creating a PyObject and calling PyObject_SetAttrString(O, "x",
Py_BuildValue("i", f.x)) for each member, finally returning a generic
PyObject using the "O" flag in BuildValue.

Option 1 I "know" is possible; 2 I haven't tried. The object is
substantially larger and more complicated than what I used here as an
example, so Option 2 would require a bit more work. However, I'd be more
than glad to do it if the community sees it as being a cleaner solution.

Or, perhaps, there's even a better way? I'm getting more and more
experienced with the Python/C API, so I don't need a walk-through or
anything. :) Just an experienced recommendation...

Aug 10 '05 #1
3 1351
Jeremy Moles wrote:
Or, perhaps, there's even a better way? I'm getting more and more
experienced with the Python/C API, so I don't need a walk-through or
anything. :) Just an experienced recommendation...


Better by far to create a new python type in C that has your struct
inside it and use PyMemberDefs to make those members automagically
available to Python. This has the monster advantage that members are
only converted from C objects to Python objects if they are referenced -
which is likely to have serious performance advantages if (as seems
likely with really large data objects) most of the members of most of
the objects aren't ever referenced.

See the "Embedding & Extending" document, Chapter 2.
Aug 10 '05 #2
Jeremy Moles wrote:
typedef struct Foo {
int x;
int y;
int z;
char xyz[100];
} Foo;

Is there an "accepted" way of propagating this upstream? I was thinking
one of these two:


It really depends on the struct. Would a C programmer automatically
be able to list all members, in their natural order? If they are
'x','y','z', I would say 'yes': it's obviously a point in a cartesian
coordinate system. If the members are readily known, and if you
would not use one without the others, a tuple is the right structure.

If, on the other hand, different applications would access different
fields, and if there are many fields so nobody could memorize them,
you would be better off with named access. A dictionary would work,
but people would prefer attribute names.

A typical example is the result of os.stat(), which is a type
named stat_result these days. It used to be a tuple, but that was
very inconvenient. For backwards compatibility, it had to remain
tuple-like, so we now have the StructSeq mechanism. This might
be the easiest way to get a struct-like value back to Python.

Regards,
Martin
Aug 10 '05 #3
Jeremy Moles wrote:
typedef struct Foo {
int x;
int y;
int z;
char xyz[100];
} Foo;

Is there an "accepted" way of propagating this upstream? I was thinking
one of these two:


It really depends on the struct. Would a C programmer automatically
be able to list all members, in their natural order? If they are
'x','y','z', I would say 'yes': it's obviously a point in a cartesian
coordinate system. If the members are readily known, and if you
would not use one without the others, a tuple is the right structure.

If, on the other hand, different applications would access different
fields, and if there are many fields so nobody could memorize them,
you would be better off with named access. A dictionary would work,
but people would prefer attribute names.

A typical example is the result of os.stat(), which is a type
named stat_result these days. It used to be a tuple, but that was
very inconvenient. For backwards compatibility, it had to remain
tuple-like, so we now have the StructSeq mechanism. This might
be the easiest way to get a struct-like value back to Python.

Regards,
Martin
Aug 10 '05 #4

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

Similar topics

4
by: Don: Bucknell Web Application Developer | last post by:
I've been scouring the Google Usenet archives, and I can find neither any article that addresses this question, nor any group that seems dead-on appropriate for asking it; I posted this in alt.php,...
1
by: Oliver Spiesshofer | last post by:
Hi, I am trying to load the LDAP and the MCrypt module under WinXP and PHP Version 4.3.7. The strange thing is that I have several other modules that work just fine: extension=php_bz2.dll...
0
by: F. GEIGER | last post by:
py2exe and datetime -> No module named datetime I've begun to use the stdlib module datetime instead of my home brewn classes. Since then a py2exe app doesn't run anymore: Traceback (most...
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
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. ...
10
by: Guoqi Zheng | last post by:
I know I can use http module in my asp.net project to rewrite url. However that is only true for .aspx page, there are many other static page in my application such as .htm. .asp, etc. My...
10
by: Ben Finney | last post by:
Howdy all, Question: I have Python modules named without '.py' as the extension, and I'd like to be able to import them. How can I do that? Background: On Unix, I write programs intended to...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
3
by: mrstephengross | last post by:
Hi all. I've got a python file called 'foo' (no extension). I want to be able to load it as a module, like so: m = __import__('foo') However, the interpreter tells me "No module named foo". If...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.