[Pyrex] How could I embed/inline a small piece of c code in pyrex
file?
Yong Sun
Yong.Sun at Sun.COM
Mon Mar 13 16:04:12 CET 2006
Hi, guys,
In pyrex source file, how could I wrap 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,
Yong Sun wrote:
> Hi, Helmut,
>
> Thank you very much!
> However, I only want to embed a c code snippet instead of a c
> function, I don't know whether pyrex will support this feature in
> future :)
>
> BTW, how did you deal with the #define constant in C source? I use
> "ctypedef enum:" block to re-declare them.
>
> Regards,
>
> Helmut Jarausch wrote:
>> I don't know any way of including inline code.
>> What I have done is this
>>
>> Put your inline code into a C function 'Myfct'
>> and put the complete function definition into
>> a file 'MyCcode.h'
>>
>> Then put the following 2 lines into your Pyrex code
>>
>> cdef extern from "MyCcode.h"
>> <here repeat just the declaration of Myfct>
>>
>> Then you can call 'Myfct' in any of your Pyrex functions
>>
>> Helmut.
>>
>>
>> On 13 Mar, Yong Sun wrote:
>>
>>> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>>> <html>
>>> <head>
>>> <meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
>>> </head>
>>> <body bgcolor="#ffffff" text="#000000">
>>> Hi, guys,<br>
>>> <br>
>>> I am writing a python module for X record extension. I have a callback
>>> need to access the data member of xEvent:<br>
>>> <blockquote>cdef void _event_callback_wrapper (XPointer priv,
>>> XRecordInterceptData *hook):<br>
>>> cdef xEvent *data<br>
>>> data = <xEvent*> hook.data<br>
>>> if (hook.category != XRecordFromServer):<br>
>>> XRecordFreeData (hook)<br>
>>> return<br>
>>> ... ...<br>
>>> </blockquote>
>>> In order to access the members of one xEvent structure, I need declare
>>> it as:<br>
>>> <blockquote>cdef extern from "X11/Xproto.h":<br>
>>> ctypedef struct xEvent<br>
>>> ... ...<br>
>>> ... ...<br>
>>> </blockquote>
>>> Then I could use xEvent.member instead of xEvent->member to access
>>> the members.<br>
>>> <br>
>>> 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?
>>> <br>
>>> <br>
>>> Just like the following:<br>
>>> <blockquote>cdef void _event_callback_wrapper (XPointer priv,
>>> XRecordInterceptData *hook):<br>
>>> cdef xEvent *data<br>
>>> data = <xEvent*> hook.data<br>
>>> if (hook.category != XRecordFromServer):<br>
>>> XRecordFreeData (hook)<br>
>>> return<br>
>>> ''' inline c code:<br>
>>> switch (data->type) {<br>
>>> case KeyPress:<br>
>>> case KeyRelease:<br>
>>> ... ...<br>
>>> }<br>
>>> '''<br>
>>> </blockquote>
>>> Regards,<br>
>>> <br>
>>> </body>
>>> </html>
>>>
>>>
>>
>>
>
>
> _______________________________________________
> Pyrex mailing list
> Pyrex at lists.copyleft.no
> http://lists.copyleft.no/mailman/listinfo/pyrex
More information about the Pyrex
mailing list