New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boundscheck directive not applied to memoryview slices? #2588

Open
h-vetinari opened this Issue Aug 30, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@h-vetinari

h-vetinari commented Aug 30, 2018

I'm trying to write a cython implementation for some code based on memoryviews, and am wondering if the boundscheck (and maybe wraparound) directive are not taken into consideration for memoryview slices?

I've narrowed it down to:

%%cython --force --annotate

cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
cdef double myfunc(double[:, :] x):
    cdef Py_ssize_t numrow = x.shape[0]
    cdef Py_ssize_t i
    for i in range(numrow):
        x[i, 0]
        x[i]
        x[i, ...]
        x[i, :]

2

The output for the lines containing the slices is something like:

    __pyx_t_6.data = __pyx_v_x.data;
    __pyx_t_6.memview = __pyx_v_x.memview;
    __PYX_INC_MEMVIEW(&__pyx_t_6, 0);
    {
    Py_ssize_t __pyx_tmp_idx = __pyx_v_i;
    Py_ssize_t __pyx_tmp_shape = __pyx_v_x.shape[0];
    Py_ssize_t __pyx_tmp_stride = __pyx_v_x.strides[0];
    if (0 && (__pyx_tmp_idx < 0))
        __pyx_tmp_idx += __pyx_tmp_shape;
    if (0 && (__pyx_tmp_idx < 0 || __pyx_tmp_idx >= __pyx_tmp_shape)) {
        PyErr_SetString(PyExc_IndexError, "Index out of bounds (axis 0)");
        __PYX_ERR(0, 11, __pyx_L1_error)
    }
        __pyx_t_6.data += __pyx_tmp_idx * __pyx_tmp_stride;
}

__pyx_t_6.shape[0] = __pyx_v_x.shape[1];
__pyx_t_6.strides[0] = __pyx_v_x.strides[1];
    __pyx_t_6.suboffsets[0] = -1;

__PYX_XDEC_MEMVIEW(&__pyx_t_6, 1);
    __pyx_t_6.memview = NULL;
    __pyx_t_6.data = NULL;

and so it seems to me like the boundscheck is still applied?

@scoder

This comment has been minimized.

Show comment
Hide comment
@scoder

scoder Aug 30, 2018

Contributor

Note the if (0 && …. Any C compiler out there will happily remove this code.

Contributor

scoder commented Aug 30, 2018

Note the if (0 && …. Any C compiler out there will happily remove this code.

@scoder scoder closed this Aug 30, 2018

@scoder

This comment has been minimized.

Show comment
Hide comment
@scoder

scoder Aug 30, 2018

Contributor

Hmm, but the annotator shouldn't mark these lines in yellow. Would be better to remove them after all.

Contributor

scoder commented Aug 30, 2018

Hmm, but the annotator shouldn't mark these lines in yellow. Would be better to remove them after all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment