Cython “Not allowed in a constant expression”, bou

2019-02-10 19:03发布

问题:

I am relatively new to Cython and have encountered an error that my research has failed me on (I am using Python3 in spyder and my Sython version is 0.26)

I tried this:

import cython

@cython.boundscheck(False)
def boundtest():
     cdef int r=4
     cdef double l[3]

and it works fine. But then I tried this:

import cython

@cython.boundscheck(False)
def boundtest():
     cdef int r=4
     cdef double l[r]

and I receive the error

[1/1] Cythonizing test.pyx

Error compiling Cython file:
------------------------------------------------------------
...
import cython

@cython.boundscheck(False)
def boundtest():
     cdef int r=4
     cdef double l[r]
                   ^
------------------------------------------------------------

test.pyx:13:20: Not allowed in a constant expression

The decorator was added due to finding this related stackexchange post and reading the Cython book by Kurt W. Smith. As far as I can tell this should work to tell Cython not to worry about out of bounds errors that may result from having a dynamic indexing variable but for some reason it does not. I have also tried changing boundscheck in the compiler options and globally to no avail.

If it weren't for the Cython documentation claiming to be up to date I would think boundscheck has been depreciated.

UPDATE

I realized I have used import Cython instead of cimport cython. I tried again with

cimport cython

But get the same error.

UPDATE 2

On a similar note the code

cdef int N = 3
cdef double[:] lout = array.array('d', N)

throws the error

TypeError: 'int' object is not iterable

I assume for the same reason that C cannot deal with (possibly) dynamic array allocation. Instead we must use

cdef double[:] lout = numpy.empty(N, 'd')

and I assume there is a line that converts N into a static type somewhere before putting it into the C-array

回答1:

The failure has nothing to do with cython.boundscheck.

Boundchecking is just checking if you try to access an element of an array that isn't there. For example if you have a size 4 array and try to access element 5 - with boundscheck(True) it will give you an Exception, with boundscheck(False) it will result in undefined behavior (probably causing a segmentation fault).

The reason for the compilation failure is another: You can't create a static array with a dynamic length! The number of elements need to be known at compile time, that's just something c enforces (I guess).

However you could define r to be known at compile time:

DEF r=4

cimport cython

@cython.boundscheck(False)
def boundtest():
    cdef double l[r]

You could however simply create a NumPy array and store it in a memoryview variable:

cimport cython
import numpy as np

@cython.boundscheck(False)
def boundtest():
    cdef int r=4
    cdef double[:] l = np.empty(r, dtype=np.double)