I have a python framework that I am using, along with a couple C
libraries that are wrapped using swig.
Is there a way to make the functions that I have written in python
callable by one of the wrapped functions?
I don't have to stay with swig, pyrex would be fine too. (Or almost any
other similar tool, those are the only two that I know.)
In short: I want python to be able to call wrapped functions like they
are python functions, and a wrapped C library to call wrapped python
functions like they were in C.
Is there any way to do this?
Thanks,
VanL
Yes, there are ways of doing this. I have a demo showing
how to do it with Pyrex, but I don't have it here right
now. I'll try to remember to let you know when I've
posted it somewhere.
--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Well, I found the demo. It shows how you can pass a
Python function to a C library as a callback.
cheesefinder.h:
-------------------------------------------------------
typedef void (*user_func)(char *name, void *user_data);
void find_cheeses(user_func f, void *user_data);
-------------------------------------------------------
cheesefinder.c:
-------------------------------------------------------
#include "cheesefinder.h"
static char *cheese_list[] = {
"cheddar",
"camembert",
"that runny one",
};
void find_cheeses(user_func f, void *user_data) {
char **p = cheese_list;
while (*p) {
f(*p, user_data);
++p;
}
}
-------------------------------------------------------
cheese.pyx:
-------------------------------------------------------
cdef extern from "cheesefinder.h":
ctypedef void (*user_func)(char *name, void *user_data)
void find_cheeses(user_func f, void *user_data)
def find(f):
find_cheeses(callback, <void*>f)
cdef void callback(char *name, void *f):
(<object>f)(name)
-------------------------------------------------------
run_cheese.py:
-------------------------------------------------------
import cheese
def report_cheese(name):
print "Found cheese:", name
cheese.find(report_cheese)
-------------------------------------------------------
The online docs need alot of work, but the test cases under "test"
in the distribution is the best place to look for examples (test2
shows running a Python module from a C program, which sounds like what
you want to do). The code is still considered alpha, and any feedback
is greatly appreciated. Thanks, and let me know if that helps...
VanL <vlin...@verio.net> wrote in message news:<mailman.1049722812...@python.org>...