Module Extension C/CPI Question

Jeremy Moles jeremy at
Wed Aug 10 03:15:17 CEST 2005

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

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, 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...

More information about the Python-list mailing list