[Pyrex] How could I embed/inline a small piece of c code in pyrex file?

Lenard Lindstrom len-l at telus.net
Tue Mar 14 21:06:37 CET 2006


Sorry, I don't know of any other way to declare embedded structures 
in Pyrex. One could argue that the Pyrex way is cleaner. It is a 
matter of preference. Remember that the declarations of s1, s2, and 
such do not actually create C declarations for those structures. The 
cdef extern only describes the contents of a C header file. The Pyrex 
compiler simply generates a C #include for the header file and lets 
the C compiler process it.

Lenard Lindstrom
<len-l at telus.net>


On 14 Mar 2006 at 9:42, Yong Sun wrote:

> Hi, Lenard,
> 
> Thank you very much for the great info!
> I got to know I only have to re-declare the interesting filed in pyrex. :)
> 
> However, do you know how to declare the c struct like that:
> 
> typedef struct _xEvent {
>     union {
>         struct {
>             BYTE type;
>             BYTE detail;
>             CARD16 sequenceNumber B16;
>             } u;
>         struct {
>             CARD32 pad00 B32;
>             Time time B32;
>             Window root B32, event B32, child B32;
>             INT16 rootX B16, rootY B16, eventX B16, eventY B16;
>             ... ...
>         } keyButtonPointer;
>     ... ...
>    }u;
> }xEvent;
> 
> I found I can not use nested ctypedef in pyrex. Currently, I am doing 
> like this way:
> 
> cdef extern from "X11/Xproto.h":
>     ctypedef struct s1:
>         unsigned char type
>         unsigned char detail
> 
>     ctypedef struct s2:
>         unsigned short rootX
>         unsigned short rooty
> 
>     ctypedef union s3:
>         s1 u
>         s2 keyButtonPointer
> 
>     ctypedef struct xEvent:
>         s3 u
> 
> Do you have cleaner and better solutions?
> 
> Regards,
> 
> 
> Lenard Lindstrom wrote:
> > David McNab released a Pyrex precompiler that lets one embed C/C++
> > code in Pyrex. It is found at:
> >
> > http://www.freenet.org.nz/python/pyrexembed
> >
> > But I don't quite understand your concern. If your Pyrex module is
> > only accessing the data field of the xEvent C structure then only
> > include that field in the cdef struct xEvent declaration. For
> > structure declarations in a cdef extern you only need to declare
> > those fields that you will actually use in Pyrex.
> >
> > Lenard Lindstrom
> > <len-l at telus.net>
> >
> > On 13 Mar 2006 at 18:25, Yong Sun wrote:
> >
> >   
> >> Hi, guys,
> >>
> >> I am writing a python module for X record extension. I have a
> >> callback need to access the data member of xEvent:
> >>     cdef void _event_callback_wrapper (XPointer priv,
> >>     XRecordInterceptData *hook):
> >>     Â Â Â  cdef xEvent *data
> >>     Â Â Â  data = <xEvent*> hook.data
> >>     Â Â Â  if (hook.category != XRecordFromServer):
> >>     Â Â Â Â Â Â Â  XRecordFreeData (hook)
> >>     Â Â Â Â Â Â Â  return
> >>     Â Â Â  ... ...
> >> In order to access the members of one xEvent structure, I need
> >> declare it as:
> >>     cdef extern from "X11/Xproto.h":
> >>     Â Â Â  ctypedef struct xEvent
> >>     Â Â Â Â Â Â Â  ... ...
> >>     Â Â Â Â Â Â Â  ... ...
> >> Then I could use xEvent.member instead of xEvent->member to access
> >> the members.
> >>
> >> However, the xEvent definition is too long and too complex, I am
> >> wondering could I directly inline a small piece of c code to access
> >> this structure, if I can ensure the reference counting or other
> >> issues?
> >>
> >> Just like the following:
> >>     cdef void _event_callback_wrapper (XPointer priv,
> >>     XRecordInterceptData *hook):
> >>     Â Â Â  cdef xEvent *data
> >>     Â Â Â  data = <xEvent*> hook.data
> >>     Â Â Â  if (hook.category != XRecordFromServer):
> >>     Â Â Â Â Â Â Â  XRecordFreeData (hook)
> >>     Â Â Â Â Â Â Â  return
> >>     Â Â Â  ''' inline c code:
> >>     Â Â Â  switch (data->type) {
> >>     Â Â Â  case KeyPress:
> >>     Â Â Â  case KeyRelease:
> >>     Â Â Â  ... ...
> >>     Â Â Â  }
> >>     Â Â Â  '''
> >> Regards,
> >>
> >>
> >>     
> >
> >
> >   
> 
> 





More information about the Pyrex mailing list