[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 14:11:04 CET 2006


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 = &lt;xEvent*&gt; 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-&gt;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 = &lt;xEvent*&gt; hook.data<br>
>>     if (hook.category != XRecordFromServer):<br>
>>         XRecordFreeData (hook)<br>
>>         return<br>
>>     ''' inline c code:<br>
>>     switch (data-&gt;type) {<br>
>>     case KeyPress:<br>
>>     case KeyRelease:<br>
>>     ... ...<br>
>>     }<br>
>>     '''<br>
>> </blockquote>
>> Regards,<br>
>> <br>
>> </body>
>> </html>
>>
>>     
>
>   




More information about the Pyrex mailing list