[Cython] BUG: assignment to typed memoryview

Daniele Nicolodi daniele at grinta.net
Wed Dec 4 13:15:19 CET 2013


On 04/12/2013 12:55, Sturla Molden wrote:
> 
> On 26 Nov 2013, at 16:18, Daniele Nicolodi <daniele at grinta.net> wrote:
> 
>> Hello,
>>
>> I believe there is a bug in how assignment to typed memoryviews is
>> handled in the compiler.  I think the following code should be valid code:
>>
>>  cdef double[::1] a = np.arange(10, dtype=np.double)
>>  cdef double[::1] b = np.empty(a.size // 2)
>>  b[:] = a[::2]
>>
> 
> Then you are wrong. This should not be valid code. You have declared
> b to be contiguous, a[::2] is a discontiguous slice. Thus a[::2]
> cannot be assigned to b.

Hello Sturla,

please note that I'm copying the array elements, not assigning to the
memoryview object: there is an important difference between `b[:] =
a[::2]` and  `b = a[::2]`. Removing the IMHO wrong check, the generated
C code correctly copies the array elements from a to b.

Indeed the following code is perfectly fine:

  cdef double[::1] a = np.arange(10, dtype=np.double)
  cdef double[::1] b = np.empty(a.size // 2)
  cdef double[:] t

  c = a[::2]
  b[:] = c

Thank for looking at this.

Cheers,
Daniele



More information about the cython-devel mailing list