[Pyrex] ctypedef in a "cdef extern from" block

Matthias Baas baas at ira.uka.de
Sat Jun 7 18:39:17 CEST 2003


Hi,

I have a small problem with ctypedef in pyrex 0.7.2. Here's the situation: 
I want to wrap a C library that contains a function, let's call it 
getProc(), which returns a pointer to another function. In the wrapper code 
I want to call getProc() and then the returned function (the function 
pointer won't get exposed to Python, everything stays within the Pyrex 
code). The problem with the function pointer is that I can't exactly 
describe it within Pyrex, so I put the exact typedef in a file called 
declarations.h. My Pyrex code then looks like this:

cdef extern from "declarations.h":

     # Declare the function pointer to the function returned by getProc
     ctypedef void (*myFuncPtr)()

     # This function actually returns a function pointer
     cdef void* getProc()

def foo():
     cdef myFuncPtr p

     # Retrieve the function pointer
     p = <myFuncPtr>getProc()


And this is what Pyrex generates:

   void ((*__pyx_v_p)(void));
   ...
   __pyx_v_p = ((void ((*)(void)))getProc());

So it doesn't keep the typedef'd name but replaces it with its definition 
which is not the "real" one (that's why I put it in a "cdef extern from" 
block in the first place). I'd rather exptected C code like this:

   myFuncPtr __pyx_v_p;
   ...
   __pyx_v_p = (myFuncPtr)getProc();

In that case the C compiler would get to see the exact definition of 
myFuncPtr that comes from the header file declaration.h.

Well, the workaround for me is pretty straightforward, I just have to 
create a new C file that contains the function foo() which then I can call 
from within Pyrex. But I thought I should post this nevertheless because 
I'm not even sure if this has to be considered being a bug since the 
ctypedef was place in a "cdef extern from" block. So is it a bug? Or is 
there already another way to get the "myFuncPtr" literally in the generated 
C file?

- Matthias -





More information about the Pyrex mailing list