mmm....yes.... I should... -_-&#39; Now it works!!! I paste the all code if someone has the same problem ;)<br><br>------ funcs.h ------<br><br>int returnFoo(int);<br><br>int returnBar(int*);<br><br>char* returnFooBar(char*);<br>
<br>int callFoo(int(*)(int), int);<br><br>int callBar(int(*)(int*), int*);<br><br>char* callFooBar(char*(*)(char*), char*);<br><br><br>------ funcs.c ------<br><br>#include &lt;c_funcs.h&gt;<br><br>int returnFoo(int foo){<br>
&nbsp; return foo;<br>}<br><br>int returnBar(int* bar){<br>&nbsp; if (bar)<br>&nbsp;&nbsp;&nbsp; return *bar;<br>}<br><br>char* returnFooBar(char* fooBar){<br>&nbsp; if (fooBar)<br>&nbsp;&nbsp;&nbsp; return fooBar;<br>}<br><br>int callFoo(int(*func)(int), int foo){<br>
&nbsp; return (*func)(foo);<br>}<br><br>int callBar(int(*func)(int*), int* bar){<br>&nbsp; return (*func)(bar);<br>}<br><br>char* callFooBar(char*(*func)(char*), char* FooBar){<br>&nbsp; return (*func)(FooBar);<br>}<br><br><br>------ funcs.pyx ------<br>
<br>cdef extern from &quot;c_funcs.h&quot;:<br>&nbsp; int returnFoo(int)<br>&nbsp; int returnBar(int*)<br>&nbsp; char* returnFooBar(char*)<br>&nbsp; int callFoo(int(*)(int), int)<br>&nbsp; int callBar(int(*)(int*), int*)<br>&nbsp; char* callFooBar(char*(*)(char*), char*)<br>
<br>cdef class funcs:<br>&nbsp; cdef object funcsDict<br>&nbsp; def __init__(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef unsigned long fooPointer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fooPointer = &lt;unsigned long&gt;&amp;returnFoo<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.funcsDict = {}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.funcsDict[&#39;returnFoo&#39;] = fooPointer<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print self.funcsDict[&#39;returnFoo&#39;]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; def pyCallFoo(self, foo):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef unsigned long tmp<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tmp = self.funcsDict[&#39;returnFoo&#39;]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print tmp<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return callFoo(&lt;int(*)(int)&gt;tmp, foo)<br>
<br><br><br><div><span class="gmail_quote">2008/4/11, Robert Bradshaw &lt;<a href="mailto:robertwb@math.washington.edu">robertwb@math.washington.edu</a>&gt;:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<span class="q"><br>
On Apr 11, 2008, at 2:58 AM, Daniele Pianu wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
I&#39;ve correct the pyrex code<br>
<br>
-------- funcs.pyx --------<br>
cdef extern from &quot;c_funcs.h&quot;:<br>
 &nbsp;int callFoo(int(*)(int), int)<br>
 &nbsp;int callBar(int(*)(int*), int*)<br>
 &nbsp;char* callFooBar(char*(*)(char*), char*)<br>
<br>
cdef class funcs:<br>
 &nbsp;cdef object funcsDict<br>
 &nbsp;def __init__(self):<br>
 &nbsp; &nbsp; &nbsp;cdef unsigned long fooPointer<br>
 &nbsp; &nbsp; &nbsp;fooPointer = &lt;unsigned long&gt;&amp;callFoo<br>
</blockquote>
<br></span>
Shouldn&#39;t you be taking the address of returnFoo here?<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><span class="e" id="q_1193cedc04a354b6_2">
<br>
 &nbsp; &nbsp; &nbsp;self.funcsDict = {}<br>
 &nbsp; &nbsp; &nbsp;self.funcsDict[&#39;returnFoo&#39;] = fooPointer<br>
<br>
 &nbsp;def pyCallFoo(self, foo):<br>
 &nbsp; &nbsp; &nbsp;cdef unsigned long tmp<br>
 &nbsp; &nbsp; &nbsp;tmp = self.funcsDict[&#39;returnFoo&#39;]<br>
 &nbsp; &nbsp; &nbsp;return callFoo(&lt;int(*)(int)&gt;tmp, foo)<br>
<br>
<br>
But it gives me a segmentation fault again... :(<br>
<br>
<br>
2008/4/11, Robert Bradshaw &lt;<a href="mailto:robertwb@math.washington.edu" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">robertwb@math.washington.edu</a>&gt;:<br>
On Apr 11, 2008, at 2:12 AM, Daniele Pianu wrote:<br>
I&#39;m trying to wrap a C function that requires a C function pointer as argument. I&#39;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.<br>

<br>
An example:<br>
<br>
-------- c_funcs.h ---------<br>
<br>
int returnFoo(int);<br>
<br>
int returnBar(int*);<br>
<br>
char* returnFooBar(char*);<br>
<br>
int callFoo(int(*)(int), int);<br>
<br>
int callBar(int(*)(int*), int*);<br>
<br>
char* callFooBar(char*(*)(char*), char*);<br>
<br>
<br>
------- c_funcs.c -------<br>
#include &lt;c_funcs.h&gt;<br>
<br>
int returnFoo(int foo){<br>
&nbsp;return foo;<br>
}<br>
<br>
int returnBar(int* bar){<br>
&nbsp;if (bar)<br>
 &nbsp; return *bar;<br>
}<br>
<br>
char* returnFooBar(char* fooBar){<br>
&nbsp;if (fooBar)<br>
 &nbsp; return fooBar;<br>
}<br>
<br>
int callFoo(int(*func)(int), int foo){<br>
&nbsp;return (*func)(foo);<br>
}<br>
<br>
int callBar(int(*func)(int*), int* bar){<br>
&nbsp;return (*func)(bar);<br>
}<br>
<br>
char* callFooBar(char*(*func)(char*), char* FooBar){<br>
&nbsp;return (*func)(FooBar);<br>
}<br>
<br>
------- funcs.pyx ----------<br>
cdef extern from &quot;c_funcs.h&quot;:<br>
&nbsp;int callFoo(int(*)(int), int)<br>
&nbsp;int callBar(int(*)(int*), int*)<br>
&nbsp;char* callFooBar(char*(*)(char*), char*)<br>
<br>
cdef class funcs:<br>
&nbsp;cdef object funcsDict<br>
&nbsp;def __init__(self):<br>
 &nbsp; &nbsp; cdef unsigned long fooPointer<br>
 &nbsp; &nbsp; fooPointer = &lt;unsigned long&gt;&amp;callFoo<br>
 &nbsp; &nbsp; self.funcsDict = {}<br>
 &nbsp; &nbsp; self.funcsDict[&#39;returnFoo&#39;] = fooPointer<br>
 &nbsp; &nbsp; # Here the python interpreter prints an integer, not a long as expected!!!<br>
<br>
Python ints are c longs, which may not actually be the size of pointers on all systems.<br>
<br>
 &nbsp; &nbsp; print self.funcsDict[&#39;returnFoo&#39;]<br>
<br>
&nbsp;def pyCallFoo(self, foo):<br>
 &nbsp; &nbsp; # The call to C function produces a Segmentatio Fault<br>
 &nbsp; &nbsp; return callFoo(&lt;int(*)(int)&gt;self.funcsDict[&#39;returnFoo&#39;], foo)<br>
<br>
self.funcsDict[&#39;returnFoo&#39;] returns a PyObject* (that points to a Python int), and it is the memory location of the object that you are casting to an int(*)(int).<br>
<br>
In Cython you can write<br>
<br>
 &nbsp; return callFoo(&lt;int(*)(int)&gt;&lt;long&gt;self.funcsDict[&#39;returnFoo&#39;], foo)<br>
<br>
to force it to unpack the Python long. With in Pyrex you need an extra variable<br>
<br>
 &nbsp; cdef long my_temp_var<br>
 &nbsp; my_temp_var = self.funcsDict[&#39;returnFoo&#39;]<br>
 &nbsp; return callFoo(&lt;int(*)(int)&gt;my_temp_var, foo)<br>
<br>
Just make sure no one messes with your dictionary!<br>
<br>
<br>
<br>
<br>
How can I solve the problem? Every advice about alternative implementation is also appreciated :)<br>
<br>
<br>
<br>
<br></span></div>
_______________________________________________<br>
Pyrex mailing list<br>
<a href="mailto:Pyrex@lists.copyleft.no" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">Pyrex@lists.copyleft.no</a><br>
<a href="http://lists.copyleft.no/mailman/listinfo/pyrex" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://lists.copyleft.no/mailman/listinfo/pyrex</a><br>
</blockquote>
<br>
</blockquote></div><br>