Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling python from a C function that is embedded in python?

18 views
Skip to first unread message

VanL

unread,
Apr 7, 2003, 9:40:23 AM4/7/03
to
Hello,

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


Greg Ewing (using news.cis.dfn.de)

unread,
Apr 8, 2003, 12:07:52 AM4/8/03
to
VanL wrote:
> 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?

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

Greg Ewing (using news.cis.dfn.de)

unread,
Apr 8, 2003, 1:18:55 AM4/8/03
to
Greg Ewing (using news.cis.dfn.de) wrote:

> VanL wrote:
>> 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.
>
> I have a demo showing how to do it with Pyrex, but I don't have it
> here right now.

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)
-------------------------------------------------------

Rick Ratzel

unread,
Apr 8, 2003, 12:46:17 PM4/8/03
to
I just released elmer 1.0.2, which might be able to help. Elmer lets
you write code in Python and run it in either C or Tcl. It will pass
function calls and data types (both native and custom) between the two
languages...at the moment, you can write a module in Python and call
it from C or Tcl, as if it was written in C or Tcl.

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

http://elmer.sourceforge.net


VanL <vlin...@verio.net> wrote in message news:<mailman.1049722812...@python.org>...

0 new messages