[Pyrex] defining module constants

Greg Ewing greg.ewing at canterbury.ac.nz
Sat Apr 12 04:17:10 CEST 2008


Rob Shortt wrote:

> cdef extern from "directfb.h":
>     ctypedef enum DFBResult:
>         DFB_OK
>         DFB_FAILURE
>         DFB_INIT
>         DFB_BUG
> 
> Please don't tell me I have to redefine or reassign them all in my pyx
> file,

A shortcut for this is on my todo list, but for now you'll
have to assign them all to module-level names one way or
another.

The main awkwardness will be managing the clash between the
enum names and the Python names. The tidiest way is probably to
put the enum names into a different namespace using a separate
.pxd file. e.g.

   # constants.pxd

   cdef extern from "directfb.h":
       ctypedef enum DFBResult:
           DFB_OK
           DFB_FAILURE
           DFB_INIT
           DFB_BUG

   # your main .pyx file

   cimport constants as k

   DFB_OK = k.DFB_OK
   DFB_FAILURE = k.DFB_FAILURE
   DFB_INIT = k.DFB_INIT
   DFB_BUG = k.DFB_BUG

Note that if you do this you'll need to remember to refer
to them as k.DFB_OK etc. in your .pyx file.

-- 
Greg



More information about the Pyrex mailing list