Re: defining module constants

Greg Ewing <[email protected]>
Newsgroups gmane.comp.python.pyrex
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.