Opened 7 years ago

Last modified 7 years ago

#451 new defect

Missing cast for numpy array slicing in C++ Mode

Reported by: hoytak Owned by: somebody
Priority: major Milestone: wishlist
Component: Code Generation Keywords:



With the language="c++" option, the following code snippet using array slicing fails to compile properly:

from numpy cimport ndarray as ar
import numpy as np

def f():
    cdef ar a1 = np.arange(10)
    cdef ar new_a1 = np.zeros(a1.size + 2)
    new_a1[1:-1] = a1

It produces the following compilation error:

test_slices.cpp: In function ‘PyObject* __pyx_pf_11test_slices_f(PyObject*, PyObject*)’:
test_slices.cpp:746: error: cannot convert ‘PyArrayObject*’ to ‘PyObject*’ for argument ‘4’ to ‘int PySequence_SetSlice(PyObject*, Py_ssize_t, Py_ssize_t, PyObject*)’

It seems that there is a cast missing, as the code generated is:

  /* "/home/hoytak/workspace/cython-tests/slices/test_slices.pyx":9
 *     cdef ar new_a1 = np.zeros(a1.size + 2)
 *     new_a1[1:-1] = a1             # <<<<<<<<<<<<<<
 *     return new_a1
  if (PySequence_SetSlice(((PyObject *)__pyx_v_new_a1), 1, -1, __pyx_v_a1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

Note the cast is present in the first argument. Furthermore, this ONLY happens in C++ mode; I can't reproduce it unless language="c++" is specified.

Change History (1)

comment:1 Changed 7 years ago by hoytak

I should have noted that this is with the current -devel branch, commit 2665:7e9af512c2eb.

Note: See TracTickets for help on using tickets.