Several questions about embedding Python
Duncan Booth
me at privacy.net
Wed May 5 09:11:19 EDT 2004
Miki Tebeka <miki.tebeka at zoran.com> wrote in
news:c79ucp$4i6$1 at news2.netvision.net.il:
> I'm trying to extend a hardware simulator (written in C/C++) by
> embedding Python.
> After reading the documentation and playing around several questions
> popped up:
>
> 1. Is there a way (using SWIG/Boost.Python ...) to export existing C/C++
> functions other than hand writing the wrapper functions.
> Something like: py_export(a_c_function)
> Since the Python module need to get information from the simulator I
> can't just write pure SWIG wrapper (or can I?)
If you have C-like functions (rather than C++ classes) take a look at Pyrex
(http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/). You still need to
hand write wrapper functions, but in most cases they are pretty short.
e.g. to wrap char *a_c_function(char *, int)
you might get away with:
cdef extern from "someheader.h":
char *real_a_c_function(char *, int) "a_c_function"
def a_c_function(s, i):
return real_a_c_function(s, i)
This would give you a module loadable in Python with a Python function
a_c_function that passes its arguments through to to the underlying
a_c_function written in C and converts the result back into a Python
string. The quoted string in the function prototype renames the C function
when used by Pyrex so you can reuse the same name.
In most cases you are likely to want to do a bit more with arguments and
results than just passing them through, and as soon as you do this Pyrex
wins over SWIG.
More information about the Python-list
mailing list