[Cython] MemoryViews require writeable arrays?

Dave Hirschfeld dave.hirschfeld at gmail.com
Wed Feb 27 20:16:08 CET 2013


Dave Hirschfeld <dave.hirschfeld at ...> writes:

> 
> cpdef double[:] return_one(double[:] x):
>     return np.array([1.0])
> 
> In [43]: x = randn(3)
>     ...: return_one(x)
> Out[43]: <MemoryView of 'ndarray' at 0x8ae14e0>
> 
> In [44]: x.flags['WRITEABLE'] = False
>     ...: return_one(x)
> ValueError: buffer source array is read-only
> 
> 
> Any help, esp. in regards to a workaround would be greatly appreciated!
> 
> Thanks,
> Dave
> 
> 


It seems using the numpy buffer interface works but I guess it would still
be good if this worked for memviews too:


%%cython
cimport cython

import numpy as np
cimport numpy as np

ctypedef np.float64_t float64_t

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cpdef double[:] return_one_np(np.ndarray[float64_t, ndim=1] x):
    return np.array([1.0])


In [203]: return_one_np(x)
Out[203]: <MemoryView of 'ndarray' at 0x7d2d558>


Cheers,
Dave



More information about the cython-devel mailing list