[Pyrex] Functions pointers
Daniele Pianu
muogoro at gmail.com
Fri Apr 11 11:12:01 CEST 2008
I'm trying to wrap a C function that requires a C function pointer as
argument. I've wrote a pyrex extension with a dictionary where every key is
a C function name, and every entry is a C function pointer, stored as a
long. I call the C function from python passing the function name and the
necessary data.
An example:
-------- c_funcs.h ---------
int returnFoo(int);
int returnBar(int*);
char* returnFooBar(char*);
int callFoo(int(*)(int), int);
int callBar(int(*)(int*), int*);
char* callFooBar(char*(*)(char*), char*);
------- c_funcs.c -------
#include <c_funcs.h>
int returnFoo(int foo){
return foo;
}
int returnBar(int* bar){
if (bar)
return *bar;
}
char* returnFooBar(char* fooBar){
if (fooBar)
return fooBar;
}
int callFoo(int(*func)(int), int foo){
return (*func)(foo);
}
int callBar(int(*func)(int*), int* bar){
return (*func)(bar);
}
char* callFooBar(char*(*func)(char*), char* FooBar){
return (*func)(FooBar);
}
------- funcs.pyx ----------
cdef extern from "c_funcs.h":
int callFoo(int(*)(int), int)
int callBar(int(*)(int*), int*)
char* callFooBar(char*(*)(char*), char*)
cdef class funcs:
cdef object funcsDict
def __init__(self):
cdef unsigned long fooPointer
fooPointer = <unsigned long>&callFoo
self.funcsDict = {}
self.funcsDict['returnFoo'] = fooPointer
# Here the python interpreter prints an integer, not a long as
expected!!!
print self.funcsDict['returnFoo']
def pyCallFoo(self, foo):
# The call to C function produces a Segmentatio Fault
return callFoo(<int(*)(int)>self.funcsDict['returnFoo'], foo)
How can I solve the problem? Every advice about alternative implementation
is also appreciated :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.copyleft.no/pipermail/pyrex/attachments/20080411/225693ad/attachment.html
More information about the Pyrex
mailing list