Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When and how does cython do boundscheck?

Tags:

cython

c doesn't do bounds check. So how does cython do the check if it compiles to c?

%%cython --annotate
cimport cython
@cython.boundscheck(True)
cpdef myf():
    cdef double pd[8]
    for i in range(100):
        pd[i] = 0
        print pd[i]

The above code compiles to the same C code no matter whether I set True or False for boundscheck. And if I run myf() there is no warnings (it happens to not crash...).

Update

So cython doens't do bounds check on c arrays anyway.

like image 280
colinfang Avatar asked Dec 10 '25 01:12

colinfang


1 Answers

http://docs.cython.org/src/reference/compilation.html#compiler-directives

"Cython is free to assume that indexing operations ([]-operator) in the code will not cause any IndexErrors to be raised. Lists, tuples, and strings are affected..."

I think in your code a C double array doesn't store its length anywhere, and so it's impossible for Cython to do any useful checks (except in your very trivial example). However, a built in Python type which can raise IndexErrors should be different (I'd assume numpy arrays, python arrays and cython memoryviews should also be affected since they all have a mechanism for Cython to tell if it's gone off the end).

like image 96
DavidW Avatar answered Dec 16 '25 00:12

DavidW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!