Commit aa1917cf authored by Stefan Behnel's avatar Stefan Behnel

Merge branch 'fix-2644' of https://github.com/aparamon/cython into aparamon-fix-2644

parents d774524e 41f1d2e6
...@@ -426,7 +426,7 @@ cdef class memoryview(object): ...@@ -426,7 +426,7 @@ cdef class memoryview(object):
cdef is_slice(self, obj): cdef is_slice(self, obj):
if not isinstance(obj, memoryview): if not isinstance(obj, memoryview):
try: try:
obj = memoryview(obj, self.flags|PyBUF_ANY_CONTIGUOUS, obj = memoryview(obj, self.flags & ~PyBUF_WRITABLE | PyBUF_ANY_CONTIGUOUS,
self.dtype_is_object) self.dtype_is_object)
except TypeError: except TypeError:
return None return None
......
...@@ -14,6 +14,9 @@ from cpython.object cimport PyObject ...@@ -14,6 +14,9 @@ from cpython.object cimport PyObject
from cpython.ref cimport Py_INCREF, Py_DECREF from cpython.ref cimport Py_INCREF, Py_DECREF
cimport cython cimport cython
import array as pyarray
from libc.stdlib cimport malloc, free
cdef extern from "Python.h": cdef extern from "Python.h":
cdef int PyBUF_C_CONTIGUOUS cdef int PyBUF_C_CONTIGUOUS
...@@ -1083,3 +1086,37 @@ def optimised_index_of_slice(int[:,:,:] arr, int x, int y, int z): ...@@ -1083,3 +1086,37 @@ def optimised_index_of_slice(int[:,:,:] arr, int x, int y, int z):
print(arr[x, y, z], arr[x][:][:][y][:][:][z]) print(arr[x, y, z], arr[x][:][:][y][:][:][z])
print(arr[x, y, z], arr[:][x][:][y][:][:][z]) print(arr[x, y, z], arr[:][x][:][y][:][:][z])
print(arr[x, y, z], arr[:, :][x][:, :][y][:][z]) print(arr[x, y, z], arr[:, :][x][:, :][y][:][z])
def test_assign_from_byteslike(byteslike):
# Once http://python3statement.org is accepted, should be just
# >>> test_assign_from_byteslike(bytes(b'hello'))
# b'hello'
# ...
"""
>>> print(test_assign_from_byteslike(bytes(b'hello')).decode())
hello
>>> print(test_assign_from_byteslike(bytearray(b'howdy')).decode())
howdy
"""
# fails on Python 2.7- with
# TypeError: an integer is required
# >>> print(test_assign_from_byteslike(pyarray.array('B', b'aloha')).decode())
# aloha
# fails on Python 2.6- with
# NameError: name 'memoryview' is not defined
# >>> print(test_assign_from_byteslike(memoryview(b'bye!!')).decode())
# bye!!
def assign(m):
m[:] = byteslike
cdef void *buf
cdef unsigned char[:] mview
buf = malloc(5)
try:
mview = <unsigned char[:5]>(buf)
assign(mview)
return (<unsigned char*>buf)[:5]
finally:
free(buf)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment