Commit 8a6c4d3d authored by Kurt Smith's avatar Kurt Smith Committed by Mark Florisson

added .shape .strides and .suboffsets fields to MemoryViewSliceType scope

parent f6273d89
......@@ -385,7 +385,7 @@ class MemoryViewSliceType(PyrexType):
def attributes_known(self):
if self.scope is None:
import Symtab, MemoryView
import Symtab, MemoryView, Options
from MemoryView import axes_to_str
self.scope = scope = Symtab.CClassScope(
......@@ -397,6 +397,27 @@ class MemoryViewSliceType(PyrexType):
scope.declare_var('_data', c_char_ptr_type, None, cname='data', is_cdef=1)
scope.declare_var('shape',
c_array_type(c_py_ssize_t_type,
Options.buffer_max_dims),
None,
cname='shape',
is_cdef=1)
scope.declare_var('strides',
c_array_type(c_py_ssize_t_type,
Options.buffer_max_dims),
None,
cname='strides',
is_cdef=1)
scope.declare_var('suboffsets',
c_array_type(c_py_ssize_t_type,
Options.buffer_max_dims),
None,
cname='suboffsets',
is_cdef=1)
mangle_dtype = MemoryView.mangle_dtype_name(self.dtype)
ndim = len(self.axes)
......
u'''
>>> test_copy_mismatch()
Traceback (most recent call last):
...
ValueError: memoryview shapes not the same in dimension 0
>>> test_copy_to()
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
>>> test_is_contiguous()
1 1
0 1
1 0
1 0
<BLANKLINE>
0 1
1 0
>>> call()
1000 2000 3000
1000
2000 3000
3000
1 1 1000
>>> two_dee()
1 2 3 4
-4 -4
1 2 3 -4
1 2 3 -4
>>> fort_two_dee()
1 2 3 4
-4 -4
1 2 3 -4
1 3 2 -4
1 2 3 -4
>>> test_nonecheck1()
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'is_c_contig'
>>> test_nonecheck2()
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'is_f_contig'
>>> test_nonecheck3()
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'copy'
>>> test_nonecheck4()
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'copy_fortran'
>>> test_nonecheck5()
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute '_data'
'''
__test__ = {}
def testcase(func):
__test__[func.__name__] = func.__doc__
return func
# from cython.view cimport memoryview
cimport cython
from cython cimport array
import numpy as np
cimport numpy as np
@testcase
def test_shape_stride_suboffset():
u'''
>>> test_shape_stride_suboffset()
5 7 11
616 88 8
-1 -1 -1
5 7 11
8 40 280
-1 -1 -1
5 7 11
616 88 8
-1 -1 -1
'''
cdef unsigned long[:,:,:] larr = array((5,7,11), sizeof(unsigned long), 'L')
print larr.shape[0], larr.shape[1], larr.shape[2]
print larr.strides[0], larr.strides[1], larr.strides[2]
print larr.suboffsets[0], larr.suboffsets[1], larr.suboffsets[2]
larr = array((5,7,11), sizeof(unsigned long), 'L', mode='fortran')
print larr.shape[0], larr.shape[1], larr.shape[2]
print larr.strides[0], larr.strides[1], larr.strides[2]
print larr.suboffsets[0], larr.suboffsets[1], larr.suboffsets[2]
cdef unsigned long[:,:,:] c_contig = larr.copy()
print c_contig.shape[0], c_contig.shape[1], c_contig.shape[2]
print c_contig.strides[0], c_contig.strides[1], c_contig.strides[2]
print c_contig.suboffsets[0], c_contig.suboffsets[1], c_contig.suboffsets[2]
@testcase
def test_copy_to():
u'''
>>> test_copy_to()
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
'''
cdef int[:,:,:] from_mvs, to_mvs
from_mvs = np.arange(8, dtype=np.int32).reshape(2,2,2)
cdef int *from_dta = <int*>from_mvs._data
......@@ -81,38 +65,91 @@ def test_copy_to():
print to_data[i],
print
@testcase
@cython.nonecheck(True)
def test_nonecheck1():
u'''
>>> test_nonecheck1()
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'is_c_contig'
'''
cdef int[:,:,:] uninitialized
print uninitialized.is_c_contig()
@testcase
@cython.nonecheck(True)
def test_nonecheck2():
u'''
>>> test_nonecheck2()
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'is_f_contig'
'''
cdef int[:,:,:] uninitialized
print uninitialized.is_f_contig()
@testcase
@cython.nonecheck(True)
def test_nonecheck3():
u'''
>>> test_nonecheck3()
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'copy'
'''
cdef int[:,:,:] uninitialized
uninitialized.copy()
@testcase
@cython.nonecheck(True)
def test_nonecheck4():
u'''
>>> test_nonecheck4()
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'copy_fortran'
'''
cdef int[:,:,:] uninitialized
uninitialized.copy_fortran()
@testcase
@cython.nonecheck(True)
def test_nonecheck5():
u'''
>>> test_nonecheck5()
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute '_data'
'''
cdef int[:,:,:] uninitialized
uninitialized._data
@testcase
def test_copy_mismatch():
u'''
>>> test_copy_mismatch()
Traceback (most recent call last):
...
ValueError: memoryview shapes not the same in dimension 0
'''
cdef int[:,:,::1] mv1 = array((2,2,3), sizeof(int), 'i')
cdef int[:,:,::1] mv2 = array((1,2,3), sizeof(int), 'i')
mv1[...] = mv2
@testcase
def test_is_contiguous():
u'''
>>> test_is_contiguous()
1 1
0 1
1 0
1 0
<BLANKLINE>
0 1
1 0
'''
cdef int[::1, :, :] fort_contig = array((1,1,1), sizeof(int), 'i', mode='fortran')
print fort_contig.is_c_contig() , fort_contig.is_f_contig()
fort_contig = array((200,100,100), sizeof(int), 'i', mode='fortran')
......@@ -127,7 +164,16 @@ def test_is_contiguous():
print strided.is_c_contig(), strided.is_f_contig()
@testcase
def call():
u'''
>>> call()
1000 2000 3000
1000
2000 3000
3000
1 1 1000
'''
cdef int[::1] mv1, mv2, mv3
cdef array arr = array((3,), sizeof(int), 'i')
mv1 = arr
......@@ -156,7 +202,15 @@ def call():
print (<int*>mv3._data)[0] , (<int*>mv2._data)[0] , (<int*>mv1._data)[0]
@testcase
def two_dee():
u'''
>>> two_dee()
1 2 3 4
-4 -4
1 2 3 -4
1 2 3 -4
'''
cdef long[:,::1] mv1, mv2, mv3
cdef array arr = array((2,2), sizeof(long), 'l')
......@@ -186,7 +240,16 @@ def two_dee():
print (<long*>mv3._data)[0] , (<long*>mv3._data)[1] , (<long*>mv3._data)[2] , (<long*>mv3._data)[3]
@testcase
def fort_two_dee():
u'''
>>> fort_two_dee()
1 2 3 4
-4 -4
1 2 3 -4
1 3 2 -4
1 2 3 -4
'''
cdef array arr = array((2,2), sizeof(long), 'l', mode='fortran')
cdef long[::1,:] mv1, mv2, mv3
......
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