Commit c8be949c authored by Mark Florisson's avatar Mark Florisson

Fix cythonarray test

parent d58cd33d
...@@ -261,44 +261,39 @@ cdef class array: ...@@ -261,44 +261,39 @@ cdef class array:
self.format = format self.format = format
self.shape = <Py_ssize_t *>malloc(sizeof(Py_ssize_t)*self.ndim) self.shape = <Py_ssize_t *> malloc(sizeof(Py_ssize_t)*self.ndim)
self.strides = <Py_ssize_t *>malloc(sizeof(Py_ssize_t)*self.ndim) self.strides = <Py_ssize_t *> malloc(sizeof(Py_ssize_t)*self.ndim)
if not self.shape or not self.strides: if not self.shape or not self.strides:
raise MemoryError("unable to allocate shape or strides.") raise MemoryError("unable to allocate shape or strides.")
cdef int idx cdef int idx
cdef Py_ssize_t int_dim, stride # cdef Py_ssize_t dim, stride
idx = 0 idx = 0
for dim in shape: for dim in shape:
int_dim = <Py_ssize_t>dim if dim <= 0:
if int_dim <= 0:
raise ValueError("Invalid shape.") raise ValueError("Invalid shape.")
self.shape[idx] = int_dim
self.shape[idx] = dim
idx += 1 idx += 1
assert idx == self.ndim
stride = itemsize
if mode == "fortran": if mode == "fortran":
idx = 0; stride = itemsize idx = 0
for dim in shape: for dim in shape:
self.strides[idx] = stride self.strides[idx] = stride
int_dim = <Py_ssize_t>dim stride = stride * dim
stride = stride * int_dim
idx += 1 idx += 1
assert idx == self.ndim
self.len = stride * self.itemsize
elif mode == "c": elif mode == "c":
idx = self.ndim-1; stride = itemsize idx = self.ndim-1
for dim in shape[::-1]: for dim in shape[::-1]:
self.strides[idx] = stride self.strides[idx] = stride
int_dim = <Py_ssize_t>dim stride = stride * dim
stride = stride * int_dim
idx -= 1 idx -= 1
assert idx == -1
self.len = stride * self.itemsize
else: else:
raise ValueError("Invalid mode, expected 'c' or 'fortran', got %s" % mode) raise ValueError("Invalid mode, expected 'c' or 'fortran', got %s" % mode)
self.len = stride
self.mode = mode self.mode = mode
if allocate_buffer: if allocate_buffer:
......
# mode: compile
cimport cython cimport cython
from cython.view cimport contig as foo, full as bar #, follow from cython.view cimport contig as foo, full as bar #, follow
from cython cimport view from cython cimport view
......
...@@ -6,19 +6,23 @@ cimport cython as cy ...@@ -6,19 +6,23 @@ cimport cython as cy
def contiguity(): def contiguity():
''' '''
>>> contiguity() >>> contiguity()
3 1 12 4
2 3 2 3
2 2
1 2 <BLANKLINE>
4 8
2 3 2 3
2 2
''' '''
cdef cy.array cvarray = cy.array(shape=(2,3), itemsize=sizeof(int), format="i", mode='c') cdef cy.array cvarray = cy.array(shape=(2,3), itemsize=sizeof(int), format="i", mode='c')
assert cvarray.len == 2*3*sizeof(int) assert cvarray.len == 2*3*sizeof(int), (cvarray.len, 2*3*sizeof(int))
assert cvarray.itemsize == sizeof(int) assert cvarray.itemsize == sizeof(int)
print cvarray.strides[0], cvarray.strides[1] print cvarray.strides[0], cvarray.strides[1]
print cvarray.shape[0], cvarray.shape[1] print cvarray.shape[0], cvarray.shape[1]
print cvarray.ndim print cvarray.ndim
print
cdef cy.array farray = cy.array(shape=(2,3), itemsize=sizeof(int), format="i", mode='fortran') cdef cy.array farray = cy.array(shape=(2,3), itemsize=sizeof(int), format="i", mode='fortran')
assert farray.len == 2*3*sizeof(int) assert farray.len == 2*3*sizeof(int)
assert farray.itemsize == sizeof(int) assert farray.itemsize == sizeof(int)
......
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