[Pyrex] ctypedef problem and improvement

Serge Barbosa Da Torre sergiobdt at yahoo.com
Fri May 21 17:52:33 CEST 2004


Hello,
I am new to the list, so my two points here might have been raised before.

A quick point first. Often we are defining some functions which do not take any parameters, or in case of methods do not use the self parameter at all. In  these cases, I think it should be easy to suppress the call to  PyArg_ParseTupleAndKeywords in the resulting C code. It would speed up repetitive calls to tiny C functions.

My second point is that often we want to wrap a C library which basically defines some data structure
and some functions.
In the C header file, we might have something like
//----------------------
// module.h
typedef struct {
  int blah1;
  int blah2;
} MyStruct;

void  init(*MyStruct);
void  dothis(*MyStruct);
//----------------------

Then what we typically want to do in the pyx file is:

cdef from "module.h":
  ctypedef struct MyStruct
  void init(*MyStruct)
  void dothis(*MyStruct)

cdef class MyClass:
  cdef MyStruct thisstruct
  def init(self):
    init(&self.thistruct)
  def dothis(self):
    dothis(&self.thistruct)

However this is currently not possible: the pyrexc compiler fails the compilation with a "variable type MyStruct is incomplete", because he does not know the size of the MyStruct.  However it should be noted that the resulting C (should the pyrexc not complain) could be easily compiled. From the header file, the C compiler knows the size of Mystruct. 
Currently one way to overcome this is to duplicate the structure declaration outside of the cdef from "module.h" and with a different name:
ctypedef struct MyStruct1:
  int blah1
  int blah2

which then creates several problem:
  + when doing this, then we need to do some explicit casting each time we use it:
    def dothis(self):
      dothis(<MyStruct*>&self.thisstruct)
  + if the structure changes in the C file, we need to duplicate it correctly here

Are you aware of other method to overcome this? I guess modifying pyrexc would be the better way to go, unless it creates some other problems I cannot think of.

Regards,
Serge Barbosa Da Torre
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.copyleft.no/pipermail/pyrex/attachments/20040521/0a784bc8/attachment.html


More information about the Pyrex mailing list