Out of bounds on buffer after numpy.resize

723 views
Skip to first unread message

Alexander Vedeneev

unread,
Oct 28, 2014, 1:25:33 PM10/28/14
to cython...@googlegroups.com
Hello all

I have found strange behavior of cython code after calling numpy.ndarray.resize:

Simple code:

%%cython
import numpy as np
cimport numpy as np
cimport cython
def test():
    cdef np.ndarray[np.int64_t, ndim=1] x 
    x = np.array([1,2,3,4])
    x.resize(x.size + 10, refcheck=False)
    print x
    print len(x)
    print x[4]

Output of test():
[1 2 3 4 0 0 0 0 0 0 0 0 0 0]
14
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-57-ea594c21b25d> in <module>()
----> 1 test()

/home/ubertrader/.cache/ipython/cython/_cython_magic_84b75606843843e3fe982579e029eb5a.so in _cython_magic_84b75606843843e3fe982579e029eb5a.test (/home/ubertrader/.cache/ipython/cython/_cython_magic_84b75606843843e3fe982579e029eb5a.c:1421)()
 
IndexError: Out of bounds on buffer access (axis 0) 

Is that bug or I'm doing something wrong ? Is there workaround ?

Thx, all

Alok Singhal

unread,
Oct 28, 2014, 2:40:39 PM10/28/14
to cython...@googlegroups.com
This is because Cython stores a reference to the array when you create
it. You bypass Numpy's detection of this by passing "refcheck=False",
so it's no surprise that bad things happen.

A stupid way to fix this would be to add:

x = x

after x.resize() call, to update Cython's references.

Hopefully there is a better way to do this, but if not, I would add a
comment explaining the need for the seemingly meaningless assignment.

-Alok
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages