Need help with C extension module
Robert Kern
rkern at ucsd.edu
Wed Sep 7 18:38:51 EDT 2005
chris wrote:
> This is my first attempt at undertaking a C extension module. I want
> to wrap an existing C library so I can call the functions from Python.
> There are only two functions I'm interested in calling. I did mess
> with Pyrex a bit and Swig, to no avail, so I turned to doing it by
> hand. Using the example in Programming Python, I did get the easier of
> the two functions working--only takes a string parameter. I'm stuck
> now on the other function and not sure how to wrap it, because it
> involves some structs. Here's a simplified version of the C:
>
> struct In
> {
> int x;
> char* s;
> ... (only primitive data types)
> };
>
> struct Out
> {
> int y;
> char* s;
> ... (only primitive data types)
> };
>
> Out* func(In* x, Out* y);
>
> So the function takes pointers to the two structs, and fills out the
> output struct and also returns the pointer to it. I would envision the
> Python looking like
>
> in = In()
> in.y = 1
> in.s = "abc"
> ...
>
> out = func(in)
>
> maybe? Just no idea how to deal with the structs in the C extension
> module code.
It's *not* a straightforward task. You have to define an extension type. See
http://docs.python.org/ext/defining-new-types.html
However, Pyrex really does make defining extension types much, much
easier. I highly suggest trying to debug your Pyrex code before learning
all of the boilerplate necessary to create extension types in pure C.
--
Robert Kern
rkern at ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
More information about the Python-list
mailing list