[Cython] Correct way of defining enums

Stéfan van der Walt stefan at sun.ac.za
Tue Nov 29 00:49:24 CET 2011


Hi all,

What is currently the correct way of defining an enum?  I am trying to call
a NumPy function with an enum as its third argument, and the following does
not compile:

cimport numpy as np
np.import_array()

cdef enum NPY_SEARCHSIDE:
    NPY_SEARCHLEFT = 0
   NPY_SEARCHRIGHT = 1

cdef sort_something():
   cdef np.ndarray[double, ndim=1] sample_prob = np.empty(5, np.float64)
    cdef np.ndarray[double, ndim=1] cum_prob = np.empty(5, np.float64)

    cdef NPY_SEARCHSIDE side = NPY_SEARCHRIGHT

   results = np.PyArray_SearchSorted(cum_prob, sample_prob, side)

It fails with:

enum.c:1081:3: error: incompatible type for argument 3 of ‘(struct PyObject
* (*)(struct PyArrayObject *, struct PyObject *, enum
NPY_SEARCHSIDE))*(PyArray_API + 1048u)’
enum.c:1081:3: note: expected ‘NPY_SEARCHSIDE’ but argument is of type
‘struct PyObject *’

I tried different ways of defining the enum, e.g.

cdef extern from "ndarraytypes.h":
    cpdef enum NPY_SEARCHSIDE:
        NPY_SEARCHLEFT = 0
        NPY_SEARCHRIGHT = 1

But that doesn't work either.  Any suggestions would be appreciated.

Regards
Stéfan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20111128/bd438a7b/attachment.html>


More information about the cython-devel mailing list