Is there an extension library?

Alex Martelli aleax at aleax.it
Sat Sep 27 12:33:14 EDT 2003


Letbetter, Jason wrote:

> I'm creating Python extensions for several c/c++ components.    I'm using
> swig to create the extensions.  The biggest challenge so far is working
> with
> the c args between Python and the Python extension.  Is there a 3rd party
> library of extension helpers that assist in representing c-types between
> Python and c?  Are there any tips and tricks?

The vast SWIG manual, at www.swig.org, does give many tips and tricks;
SWIG also comes with a librayr of helpers such as you request.


> Here are some specific scenarios I am running into.
> 
> 1) Refrencing and derefrencing:
> For example, condsider this c api:
> 
> void foo_alloc(void** handle_ptr);
> void foo_use(void* handle);
> 
> In c &handle.  How to get pointer to pointer in Python?
> In c *handle_ptr.  How to derefrence a pointer in Python?

You can use typemaps to ensure "void** handle_ptr" as an
argument becomes a "return value" (some opaque representation
of the resulting void*) in Python, and you'll just pass that
opaque value back into foo_use, no need to dereference.


> 2) Coercion:
> For example, suppose you want to initialize a char array in Python.
> 
> buf = CharArray(100)
> for i in range(100):
>    buf[i] = i
> 
> The above results in:
> "TypeError: CharArray___setitem__() argument 3 must be char, not int"
> In c, buf[i] = (char)i, is implicit.  How do I do it in Python.  Why
> doesn't Python make this conversion implicitly?

In Python, "a character" is "a string of length one" and implicitly
converting that to/from "an integer that happens to be the ASCII code
for that character" would be an utter, extremely error-prone design
disaster.  Use ord() if you want the ASCII code for the one and only
character in a string of length 1, chr() if you want to make a string
of length one starting from a character's ASCII code.  [I don't know
what a CharArray _IS_, and how come you got that weird indirectness
requested, but I'm just answering your questions anyway].

 
> 3) It seems like I'm doing something unatural with my Python extension. 
> It seems someone else may have already figured out the best way to
> transform c arguments between Python and Python extensions genericly.

SWIG does a good job (for C).  Boost Python does an arguably even
better one (for C++, though -- and you do need a decently standard
compliant C++ compiler that won't choke on abstruse templates; also,
I'm not sure if Boost Python is out yet in a release supporting
Python 2.3 -- when I tried to build from the CVS sources I found
the task quite tiresome).


Alex





More information about the Python-list mailing list