Cython “Not allowed in a constant expression”, boundscheck False doesn't work

时光怂恿深爱的人放手 提交于 2019-11-29 04:53:58

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 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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!