Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1)

Delta Between Two Patch Sets: Lib/test/test_buffer.py

Issue 15573: Support unknown formats in memoryview comparisons
Left Patch Set: Created 7 years, 7 months ago
Right Patch Set: Created 7 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/whatsnew/3.3.rst ('k') | Objects/memoryobject.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 # 1 #
2 # The ndarray object from _testbuffer.c is a complete implementation of 2 # The ndarray object from _testbuffer.c is a complete implementation of
3 # a PEP-3118 buffer provider. It is independent from NumPy's ndarray 3 # a PEP-3118 buffer provider. It is independent from NumPy's ndarray
4 # and the tests don't require NumPy. 4 # and the tests don't require NumPy.
5 # 5 #
6 # If NumPy is present, some tests check both ndarray implementations 6 # If NumPy is present, some tests check both ndarray implementations
7 # against each other. 7 # against each other.
8 # 8 #
9 # Most ndarray tests also check that memoryview(ndarray) behaves in 9 # Most ndarray tests also check that memoryview(ndarray) behaves in
10 # the same way as the original. Thus, a substantial part of the 10 # the same way as the original. Thus, a substantial part of the
(...skipping 12 matching lines...) Expand all
23 23
24 try: 24 try:
25 from _testbuffer import * 25 from _testbuffer import *
26 except ImportError: 26 except ImportError:
27 ndarray = None 27 ndarray = None
28 28
29 try: 29 try:
30 import struct 30 import struct
31 except ImportError: 31 except ImportError:
32 struct = None 32 struct = None
33
34 try:
35 import ctypes
36 except ImportError:
37 ctypes = None
33 38
34 try: 39 try:
35 with warnings.catch_warnings(): 40 with warnings.catch_warnings():
36 from numpy import ndarray as numpy_array 41 from numpy import ndarray as numpy_array
37 except ImportError: 42 except ImportError:
38 numpy_array = None 43 numpy_array = None
39 44
40 45
41 SHORT_TEST = True 46 SHORT_TEST = True
42 47
(...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after
828 b.extend(x) 833 b.extend(x)
829 except BufferError: 834 except BufferError:
830 buf_err = True # re-exporter does not provide full buffer 835 buf_err = True # re-exporter does not provide full buffer
831 break 836 break
832 self.assertEqual(item1, item2) 837 self.assertEqual(item1, item2)
833 838
834 if not buf_err: 839 if not buf_err:
835 # test tobytes() 840 # test tobytes()
836 self.assertEqual(result.tobytes(), b) 841 self.assertEqual(result.tobytes(), b)
837 842
838 if not buf_err and is_memoryview_format(fmt):
839
840 # lst := expected multi-dimensional logical representation 843 # lst := expected multi-dimensional logical representation
841 # flatten(lst) := elements in C-order 844 # flatten(lst) := elements in C-order
842 ff = fmt if fmt else 'B' 845 ff = fmt if fmt else 'B'
843 flattened = flatten(lst) 846 flattened = flatten(lst)
844 847
845 # Rules for 'A': if the array is already contiguous, return 848 # Rules for 'A': if the array is already contiguous, return
846 # the array unaltered. Otherwise, return a contiguous 'C' 849 # the array unaltered. Otherwise, return a contiguous 'C'
847 # representation. 850 # representation.
848 for order in ['C', 'F', 'A']: 851 for order in ['C', 'F', 'A']:
849 expected = result 852 expected = result
(...skipping 20 matching lines...) Expand all
870 873
871 nmemb = len(flattened) 874 nmemb = len(flattened)
872 ro = 0 if readonly else ND_WRITABLE 875 ro = 0 if readonly else ND_WRITABLE
873 876
874 ### See comment in test_py_buffer_to_contiguous for an 877 ### See comment in test_py_buffer_to_contiguous for an
875 ### explanation why these tests are valid. 878 ### explanation why these tests are valid.
876 879
877 # To 'C' 880 # To 'C'
878 contig = py_buffer_to_contiguous(result, 'C', PyBUF_FULL_RO) 881 contig = py_buffer_to_contiguous(result, 'C', PyBUF_FULL_RO)
879 self.assertEqual(len(contig), nmemb * itemsize) 882 self.assertEqual(len(contig), nmemb * itemsize)
880 initlst = [struct.unpack_from(fmt, contig, n*itemsize)[0] 883 initlst = [struct.unpack_from(fmt, contig, n*itemsize)
881 for n in range(nmemb)] 884 for n in range(nmemb)]
885 if len(initlst[0]) == 1:
886 initlst = [v[0] for v in initlst]
882 887
883 y = ndarray(initlst, shape=shape, flags=ro, format=fmt) 888 y = ndarray(initlst, shape=shape, flags=ro, format=fmt)
884 self.assertEqual(memoryview(y), memoryview(result)) 889 self.assertEqual(memoryview(y), memoryview(result))
885 890
886 # To 'F' 891 # To 'F'
887 contig = py_buffer_to_contiguous(result, 'F', PyBUF_FULL_RO) 892 contig = py_buffer_to_contiguous(result, 'F', PyBUF_FULL_RO)
888 self.assertEqual(len(contig), nmemb * itemsize) 893 self.assertEqual(len(contig), nmemb * itemsize)
889 initlst = [struct.unpack_from(fmt, contig, n*itemsize)[0] 894 initlst = [struct.unpack_from(fmt, contig, n*itemsize)
890 for n in range(nmemb)] 895 for n in range(nmemb)]
896 if len(initlst[0]) == 1:
897 initlst = [v[0] for v in initlst]
891 898
892 y = ndarray(initlst, shape=shape, flags=ro|ND_FORTRAN, 899 y = ndarray(initlst, shape=shape, flags=ro|ND_FORTRAN,
893 format=fmt) 900 format=fmt)
894 self.assertEqual(memoryview(y), memoryview(result)) 901 self.assertEqual(memoryview(y), memoryview(result))
895 902
896 # To 'A' 903 # To 'A'
897 contig = py_buffer_to_contiguous(result, 'A', PyBUF_FULL_RO) 904 contig = py_buffer_to_contiguous(result, 'A', PyBUF_FULL_RO)
898 self.assertEqual(len(contig), nmemb * itemsize) 905 self.assertEqual(len(contig), nmemb * itemsize)
899 initlst = [struct.unpack_from(fmt, contig, n*itemsize)[0] 906 initlst = [struct.unpack_from(fmt, contig, n*itemsize)
900 for n in range(nmemb)] 907 for n in range(nmemb)]
908 if len(initlst[0]) == 1:
909 initlst = [v[0] for v in initlst]
901 910
902 f = ND_FORTRAN if is_contiguous(result, 'F') else 0 911 f = ND_FORTRAN if is_contiguous(result, 'F') else 0
903 y = ndarray(initlst, shape=shape, flags=f|ro, format=fmt) 912 y = ndarray(initlst, shape=shape, flags=f|ro, format=fmt)
904 self.assertEqual(memoryview(y), memoryview(result)) 913 self.assertEqual(memoryview(y), memoryview(result))
905 914
906 if is_memoryview_format(fmt): 915 if is_memoryview_format(fmt):
907 try: 916 try:
908 m = memoryview(result) 917 m = memoryview(result)
909 except BufferError: # re-exporter does not provide full information 918 except BufferError: # re-exporter does not provide full information
910 return 919 return
(...skipping 2111 matching lines...) Expand 10 before | Expand all | Expand 10 after
3022 if have_resize: # memoryview cannot change shape 3031 if have_resize: # memoryview cannot change shape
3023 self.assertIs(m_err, ValueError) 3032 self.assertIs(m_err, ValueError)
3024 elif m_err or array_err: 3033 elif m_err or array_err:
3025 self.assertIs(m_err, array_err) 3034 self.assertIs(m_err, array_err)
3026 else: 3035 else:
3027 self.assertEqual(m, a) 3036 self.assertEqual(m, a)
3028 self.assertEqual(m.tolist(), a.tolist()) 3037 self.assertEqual(m.tolist(), a.tolist())
3029 self.assertEqual(m.tobytes(), a.tobytes()) 3038 self.assertEqual(m.tobytes(), a.tobytes())
3030 cmptest(self, a, b, m, singleitem) 3039 cmptest(self, a, b, m, singleitem)
3031 3040
3032 def test_memoryview_compare(self): 3041 def test_memoryview_compare_special_cases(self):
3033 3042
3034 a = array.array('L', [1, 2, 3]) 3043 a = array.array('L', [1, 2, 3])
3035 b = array.array('L', [1, 2, 7]) 3044 b = array.array('L', [1, 2, 7])
3036 3045
3037 # Ordering comparisons raise: 3046 # Ordering comparisons raise:
3038 v = memoryview(a) 3047 v = memoryview(a)
3039 w = memoryview(b) 3048 w = memoryview(b)
3040 for attr in ('__lt__', '__le__', '__gt__', '__ge__'): 3049 for attr in ('__lt__', '__le__', '__gt__', '__ge__'):
3041 self.assertIs(getattr(v, attr)(w), NotImplemented) 3050 self.assertIs(getattr(v, attr)(w), NotImplemented)
3042 self.assertIs(getattr(a, attr)(v), NotImplemented) 3051 self.assertIs(getattr(a, attr)(v), NotImplemented)
3043 3052
3044 # Released views compare equal to themselves: 3053 # Released views compare equal to themselves:
3045 v = memoryview(a) 3054 v = memoryview(a)
3046 v.release() 3055 v.release()
3047 self.assertEqual(v, v) 3056 self.assertEqual(v, v)
3048 self.assertNotEqual(v, a) 3057 self.assertNotEqual(v, a)
3049 self.assertNotEqual(a, v) 3058 self.assertNotEqual(a, v)
3050 3059
3051 v = memoryview(a) 3060 v = memoryview(a)
3052 w = memoryview(a) 3061 w = memoryview(a)
3053 w.release() 3062 w.release()
3054 self.assertNotEqual(v, w) 3063 self.assertNotEqual(v, w)
3055 self.assertNotEqual(w, v) 3064 self.assertNotEqual(w, v)
3056 3065
3057 # Operand does not implement the buffer protocol: 3066 # Operand does not implement the buffer protocol:
3058 v = memoryview(a) 3067 v = memoryview(a)
3059 self.assertNotEqual(v, [1, 2, 3]) 3068 self.assertNotEqual(v, [1, 2, 3])
3060 3069
3061 # Different formats: 3070 # NaNs
3062 c = array.array('l', [1, 2, 3]) 3071 nd = ndarray([(0, 0)], shape=[1], format='l x d x', flags=ND_WRITABLE)
3072 nd[0] = (-1, float('nan'))
3073 self.assertNotEqual(memoryview(nd), nd)
3074
3075 # Depends on issue #15625: the struct module does not understand 'u'.
3076 a = array.array('u', 'xyz')
3063 v = memoryview(a) 3077 v = memoryview(a)
3064 self.assertNotEqual(v, c) 3078 self.assertNotEqual(a, v)
3065 self.assertNotEqual(c, v)
3066
3067 # Not implemented formats. Ugly, but inevitable. This is the same as
3068 # issue #2531: equality is also used for membership testing and must
3069 # return a result.
3070 a = ndarray([(1, 1.5), (2, 2.7)], shape=[2], format='ld')
3071 v = memoryview(a)
3072 self.assertNotEqual(v, a) 3079 self.assertNotEqual(v, a)
3073 self.assertNotEqual(a, v) 3080
3074 3081 # Some ctypes format strings are unknown to the struct module.
3075 a = ndarray([b'12345'], shape=[1], format="s") 3082 if ctypes:
3076 v = memoryview(a) 3083 # format: "T{>l:x:>l:y:}"
3077 self.assertNotEqual(v, a) 3084 class BEPoint(ctypes.BigEndianStructure):
3078 self.assertNotEqual(a, v) 3085 _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
3079 3086 point = BEPoint(100, 200)
3080 nd = ndarray([(1,1,1), (2,2,2), (3,3,3)], shape=[3], format='iii') 3087 a = memoryview(point)
3081 v = memoryview(nd) 3088 b = memoryview(point)
3082 self.assertNotEqual(v, nd) 3089 self.assertNotEqual(a, b)
3083 self.assertNotEqual(nd, v) 3090 self.assertNotEqual(a, point)
3084 3091 self.assertNotEqual(point, a)
3085 # '@' prefix can be dropped: 3092 self.assertRaises(NotImplementedError, a.tolist)
3086 nd1 = ndarray([1,2,3], shape=[3], format='@i') 3093
3087 nd2 = ndarray([1,2,3], shape=[3], format='i') 3094 def test_memoryview_compare_ndim_zero(self):
3088 v = memoryview(nd1) 3095
3089 w = memoryview(nd2)
3090 self.assertEqual(v, w)
3091 self.assertEqual(w, v)
3092 self.assertEqual(v, nd2)
3093 self.assertEqual(nd2, v)
3094 self.assertEqual(w, nd1)
3095 self.assertEqual(nd1, w)
3096
3097 # ndim = 0
3098 nd1 = ndarray(1729, shape=[], format='@L') 3096 nd1 = ndarray(1729, shape=[], format='@L')
3099 nd2 = ndarray(1729, shape=[], format='L', flags=ND_WRITABLE) 3097 nd2 = ndarray(1729, shape=[], format='L', flags=ND_WRITABLE)
3100 v = memoryview(nd1) 3098 v = memoryview(nd1)
3101 w = memoryview(nd2) 3099 w = memoryview(nd2)
3102 self.assertEqual(v, w) 3100 self.assertEqual(v, w)
3103 self.assertEqual(w, v) 3101 self.assertEqual(w, v)
3104 self.assertEqual(v, nd2) 3102 self.assertEqual(v, nd2)
3105 self.assertEqual(nd2, v) 3103 self.assertEqual(nd2, v)
3106 self.assertEqual(w, nd1) 3104 self.assertEqual(w, nd1)
3107 self.assertEqual(nd1, w) 3105 self.assertEqual(nd1, w)
(...skipping 13 matching lines...) Expand all
3121 self.assertFalse(w.__eq__(v)) 3119 self.assertFalse(w.__eq__(v))
3122 3120
3123 nd = ndarray(list(range(12)), shape=[12], flags=ND_WRITABLE|ND_PIL) 3121 nd = ndarray(list(range(12)), shape=[12], flags=ND_WRITABLE|ND_PIL)
3124 ex = ndarray(list(range(12)), shape=[12], flags=ND_WRITABLE|ND_PIL) 3122 ex = ndarray(list(range(12)), shape=[12], flags=ND_WRITABLE|ND_PIL)
3125 m = memoryview(ex) 3123 m = memoryview(ex)
3126 3124
3127 self.assertEqual(m, nd) 3125 self.assertEqual(m, nd)
3128 m[9] = 100 3126 m[9] = 100
3129 self.assertNotEqual(m, nd) 3127 self.assertNotEqual(m, nd)
3130 3128
3131 # ndim = 1: contiguous 3129 # struct module: equal
3130 nd1 = ndarray((1729, 1.2, b'12345'), shape=[], format='Lf5s')
3131 nd2 = ndarray((1729, 1.2, b'12345'), shape=[], format='hf5s',
3132 flags=ND_WRITABLE)
3133 v = memoryview(nd1)
3134 w = memoryview(nd2)
3135 self.assertEqual(v, w)
3136 self.assertEqual(w, v)
3137 self.assertEqual(v, nd2)
3138 self.assertEqual(nd2, v)
3139 self.assertEqual(w, nd1)
3140 self.assertEqual(nd1, w)
3141
3142 # struct module: not equal
3143 nd1 = ndarray((1729, 1.2, b'12345'), shape=[], format='Lf5s')
3144 nd2 = ndarray((-1729, 1.2, b'12345'), shape=[], format='hf5s',
3145 flags=ND_WRITABLE)
3146 v = memoryview(nd1)
3147 w = memoryview(nd2)
3148 self.assertNotEqual(v, w)
3149 self.assertNotEqual(w, v)
3150 self.assertNotEqual(v, nd2)
3151 self.assertNotEqual(nd2, v)
3152 self.assertNotEqual(w, nd1)
3153 self.assertNotEqual(nd1, w)
3154 self.assertEqual(v, nd1)
3155 self.assertEqual(w, nd2)
3156
3157 def test_memoryview_compare_ndim_one(self):
3158
3159 # contiguous
3132 nd1 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='@h') 3160 nd1 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='@h')
3133 nd2 = ndarray([-529, 576, -625, 676, 729], shape=[5], format='@h') 3161 nd2 = ndarray([-529, 576, -625, 676, 729], shape=[5], format='@h')
3134 v = memoryview(nd1) 3162 v = memoryview(nd1)
3135 w = memoryview(nd2) 3163 w = memoryview(nd2)
3136 3164
3137 self.assertEqual(v, nd1) 3165 self.assertEqual(v, nd1)
3138 self.assertEqual(w, nd2) 3166 self.assertEqual(w, nd2)
3139 self.assertNotEqual(v, nd2) 3167 self.assertNotEqual(v, nd2)
3140 self.assertNotEqual(w, nd1) 3168 self.assertNotEqual(w, nd1)
3141 self.assertNotEqual(v, w) 3169 self.assertNotEqual(v, w)
3142 3170
3143 # ndim = 1: non-contiguous 3171 # contiguous, struct module
3172 nd1 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='<i')
3173 nd2 = ndarray([-529, 576, -625, 676, 729], shape=[5], format='>h')
3174 v = memoryview(nd1)
3175 w = memoryview(nd2)
3176
3177 self.assertEqual(v, nd1)
3178 self.assertEqual(w, nd2)
3179 self.assertNotEqual(v, nd2)
3180 self.assertNotEqual(w, nd1)
3181 self.assertNotEqual(v, w)
3182
3183 # non-contiguous
3144 nd1 = ndarray([-529, -625, -729], shape=[3], format='@h') 3184 nd1 = ndarray([-529, -625, -729], shape=[3], format='@h')
3145 nd2 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='@h') 3185 nd2 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='@h')
3146 v = memoryview(nd1) 3186 v = memoryview(nd1)
3147 w = memoryview(nd2) 3187 w = memoryview(nd2)
3148 3188
3149 self.assertEqual(v, nd2[::2]) 3189 self.assertEqual(v, nd2[::2])
3150 self.assertEqual(w[::2], nd1) 3190 self.assertEqual(w[::2], nd1)
3151 self.assertEqual(v, w[::2]) 3191 self.assertEqual(v, w[::2])
3152 self.assertEqual(v[::-1], w[::-2]) 3192 self.assertEqual(v[::-1], w[::-2])
3153 3193
3154 # ndim = 1: non-contiguous, suboffsets 3194 # non-contiguous, struct module
3195 nd1 = ndarray([-529, -625, -729], shape=[3], format='!h')
3196 nd2 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='<l')
3197 v = memoryview(nd1)
3198 w = memoryview(nd2)
3199
3200 self.assertEqual(v, nd2[::2])
3201 self.assertEqual(w[::2], nd1)
3202 self.assertEqual(v, w[::2])
3203 self.assertEqual(v[::-1], w[::-2])
3204
3205 # non-contiguous, suboffsets
3155 nd1 = ndarray([-529, -625, -729], shape=[3], format='@h') 3206 nd1 = ndarray([-529, -625, -729], shape=[3], format='@h')
3156 nd2 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='@h', 3207 nd2 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='@h',
3157 flags=ND_PIL) 3208 flags=ND_PIL)
3158 v = memoryview(nd1) 3209 v = memoryview(nd1)
3159 w = memoryview(nd2) 3210 w = memoryview(nd2)
3160 3211
3161 self.assertEqual(v, nd2[::2]) 3212 self.assertEqual(v, nd2[::2])
3162 self.assertEqual(w[::2], nd1) 3213 self.assertEqual(w[::2], nd1)
3163 self.assertEqual(v, w[::2]) 3214 self.assertEqual(v, w[::2])
3164 self.assertEqual(v[::-1], w[::-2]) 3215 self.assertEqual(v[::-1], w[::-2])
3165 3216
3166 # ndim = 1: zeros in shape 3217 # non-contiguous, suboffsets, struct module
3218 nd1 = ndarray([-529, -625, -729], shape=[3], format='h 0c')
3219 nd2 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='> h',
3220 flags=ND_PIL)
3221 v = memoryview(nd1)
3222 w = memoryview(nd2)
3223
3224 self.assertEqual(v, nd2[::2])
3225 self.assertEqual(w[::2], nd1)
3226 self.assertEqual(v, w[::2])
3227 self.assertEqual(v[::-1], w[::-2])
3228
3229 def test_memoryview_compare_zero_shape(self):
3230
3231 # zeros in shape
3167 nd1 = ndarray([900, 961], shape=[0], format='@h') 3232 nd1 = ndarray([900, 961], shape=[0], format='@h')
3168 nd2 = ndarray([-900, -961], shape=[0], format='@h') 3233 nd2 = ndarray([-900, -961], shape=[0], format='@h')
3169 v = memoryview(nd1) 3234 v = memoryview(nd1)
3170 w = memoryview(nd2) 3235 w = memoryview(nd2)
3171 3236
3172 self.assertEqual(v, nd1) 3237 self.assertEqual(v, nd1)
3173 self.assertEqual(w, nd2) 3238 self.assertEqual(w, nd2)
3174 self.assertEqual(v, nd2) 3239 self.assertEqual(v, nd2)
3175 self.assertEqual(w, nd1) 3240 self.assertEqual(w, nd1)
3176 self.assertEqual(v, w) 3241 self.assertEqual(v, w)
3177 3242
3178 # ndim = 1: zero strides 3243 # zeros in shape, struct module
3179 nd1 = ndarray([900, 900, 900, 900], shape=[4], format='@L') 3244 nd1 = ndarray([900, 961], shape=[0], format='= h0c')
3180 nd2 = ndarray([900], shape=[4], strides=[0], format='L') 3245 nd2 = ndarray([-900, -961], shape=[0], format='@ i')
3181 v = memoryview(nd1) 3246 v = memoryview(nd1)
3182 w = memoryview(nd2) 3247 w = memoryview(nd2)
3183 3248
3184 self.assertEqual(v, nd1) 3249 self.assertEqual(v, nd1)
3185 self.assertEqual(w, nd2) 3250 self.assertEqual(w, nd2)
3186 self.assertEqual(v, nd2) 3251 self.assertEqual(v, nd2)
3187 self.assertEqual(w, nd1) 3252 self.assertEqual(w, nd1)
3188 self.assertEqual(v, w) 3253 self.assertEqual(v, w)
3189 3254
3255 def test_memoryview_compare_zero_strides(self):
3256
3257 # zero strides
3258 nd1 = ndarray([900, 900, 900, 900], shape=[4], format='@L')
3259 nd2 = ndarray([900], shape=[4], strides=[0], format='L')
3260 v = memoryview(nd1)
3261 w = memoryview(nd2)
3262
3263 self.assertEqual(v, nd1)
3264 self.assertEqual(w, nd2)
3265 self.assertEqual(v, nd2)
3266 self.assertEqual(w, nd1)
3267 self.assertEqual(v, w)
3268
3269 # zero strides, struct module
3270 nd1 = ndarray([(900, 900)]*4, shape=[4], format='@ Li')
3271 nd2 = ndarray([(900, 900)], shape=[4], strides=[0], format='!L h')
3272 v = memoryview(nd1)
3273 w = memoryview(nd2)
3274
3275 self.assertEqual(v, nd1)
3276 self.assertEqual(w, nd2)
3277 self.assertEqual(v, nd2)
3278 self.assertEqual(w, nd1)
3279 self.assertEqual(v, w)
3280
3281 def test_memoryview_compare_random_formats(self):
3282
3283 # random single character native formats
3190 n = 10 3284 n = 10
3191 for char in fmtdict['@m']: 3285 for char in fmtdict['@m']:
3192 fmt, items, singleitem = randitems(n, 'memoryview', '@', char) 3286 fmt, items, singleitem = randitems(n, 'memoryview', '@', char)
3193 for flags in (0, ND_PIL): 3287 for flags in (0, ND_PIL):
3194 nd = ndarray(items, shape=[n], format=fmt, flags=flags) 3288 nd = ndarray(items, shape=[n], format=fmt, flags=flags)
3195 m = memoryview(nd) 3289 m = memoryview(nd)
3196 self.assertEqual(m, nd) 3290 self.assertEqual(m, nd)
3197 3291
3198 nd = nd[::-3] 3292 nd = nd[::-3]
3199 m = memoryview(nd) 3293 m = memoryview(nd)
3200 self.assertEqual(m, nd) 3294 self.assertEqual(m, nd)
3201 3295
3202 ##### ndim > 1: C-contiguous 3296 # random formats
3203 # different values 3297 n = 10
3298 for _ in range(100):
3299 fmt, items, singleitem = randitems(n)
3300 for flags in (0, ND_PIL):
3301 nd = ndarray(items, shape=[n], format=fmt, flags=flags)
3302 m = memoryview(nd)
3303 self.assertEqual(m, nd)
3304
3305 nd = nd[::-3]
3306 m = memoryview(nd)
3307 self.assertEqual(m, nd)
3308
3309 def test_memoryview_compare_multidim_c(self):
3310
3311 # C-contiguous, different values
3204 nd1 = ndarray(list(range(-15, 15)), shape=[3, 2, 5], format='@h') 3312 nd1 = ndarray(list(range(-15, 15)), shape=[3, 2, 5], format='@h')
3205 nd2 = ndarray(list(range(0, 30)), shape=[3, 2, 5], format='@h') 3313 nd2 = ndarray(list(range(0, 30)), shape=[3, 2, 5], format='@h')
3206 v = memoryview(nd1) 3314 v = memoryview(nd1)
3207 w = memoryview(nd2) 3315 w = memoryview(nd2)
3208 3316
3209 self.assertEqual(v, nd1) 3317 self.assertEqual(v, nd1)
3210 self.assertEqual(w, nd2) 3318 self.assertEqual(w, nd2)
3211 self.assertNotEqual(v, nd2) 3319 self.assertNotEqual(v, nd2)
3212 self.assertNotEqual(w, nd1) 3320 self.assertNotEqual(w, nd1)
3213 self.assertNotEqual(v, w) 3321 self.assertNotEqual(v, w)
3214 3322
3215 # different shape 3323 # C-contiguous, different values, struct module
3216 nd1 = ndarray(list(range(30)), shape=[2, 3, 5], format='L') 3324 nd1 = ndarray([(0, 1, 2)]*30, shape=[3, 2, 5], format='=f q xxL')
3217 nd2 = ndarray(list(range(30)), shape=[3, 2, 5], format='L') 3325 nd2 = ndarray([(-1.2, 1, 2)]*30, shape=[3, 2, 5], format='< f 2Q')
3218 v = memoryview(nd1) 3326 v = memoryview(nd1)
3219 w = memoryview(nd2) 3327 w = memoryview(nd2)
3220 3328
3221 self.assertEqual(v, nd1) 3329 self.assertEqual(v, nd1)
3222 self.assertEqual(w, nd2) 3330 self.assertEqual(w, nd2)
3223 self.assertNotEqual(v, nd2) 3331 self.assertNotEqual(v, nd2)
3224 self.assertNotEqual(w, nd1) 3332 self.assertNotEqual(w, nd1)
3225 self.assertNotEqual(v, w) 3333 self.assertNotEqual(v, w)
3226 3334
3227 # different format 3335 # C-contiguous, different shape
3228 nd1 = ndarray(list(range(30)), shape=[2, 3, 5], format='L') 3336 nd1 = ndarray(list(range(30)), shape=[2, 3, 5], format='L')
3229 nd2 = ndarray(list(range(30)), shape=[2, 3, 5], format='l') 3337 nd2 = ndarray(list(range(30)), shape=[3, 2, 5], format='L')
3230 v = memoryview(nd1) 3338 v = memoryview(nd1)
3231 w = memoryview(nd2) 3339 w = memoryview(nd2)
3232 3340
3233 self.assertEqual(v, nd1) 3341 self.assertEqual(v, nd1)
3234 self.assertEqual(w, nd2) 3342 self.assertEqual(w, nd2)
3235 self.assertNotEqual(v, nd2) 3343 self.assertNotEqual(v, nd2)
3236 self.assertNotEqual(w, nd1) 3344 self.assertNotEqual(w, nd1)
3237 self.assertNotEqual(v, w) 3345 self.assertNotEqual(v, w)
3238 3346
3239 ##### ndim > 1: Fortran contiguous 3347 # C-contiguous, different shape, struct module
3240 # different values 3348 nd1 = ndarray([(0, 1, 2)]*21, shape=[3, 7], format='! b B xL')
3349 nd2 = ndarray([(0, 1, 2)]*21, shape=[7, 3], format='= Qx l xxL')
3350 v = memoryview(nd1)
3351 w = memoryview(nd2)
3352
3353 self.assertEqual(v, nd1)
3354 self.assertEqual(w, nd2)
3355 self.assertNotEqual(v, nd2)
3356 self.assertNotEqual(w, nd1)
3357 self.assertNotEqual(v, w)
3358
3359 # C-contiguous, different format, struct module
3360 nd1 = ndarray(list(range(30)), shape=[2, 3, 5], format='L')
3361 nd2 = ndarray(list(range(30)), shape=[2, 3, 5], format='l')
3362 v = memoryview(nd1)
3363 w = memoryview(nd2)
3364
3365 self.assertEqual(v, nd1)
3366 self.assertEqual(w, nd2)
3367 self.assertEqual(v, nd2)
3368 self.assertEqual(w, nd1)
3369 self.assertEqual(v, w)
3370
3371 def test_memoryview_compare_multidim_fortran(self):
3372
3373 # Fortran-contiguous, different values
3241 nd1 = ndarray(list(range(-15, 15)), shape=[5, 2, 3], format='@h', 3374 nd1 = ndarray(list(range(-15, 15)), shape=[5, 2, 3], format='@h',
3242 flags=ND_FORTRAN) 3375 flags=ND_FORTRAN)
3243 nd2 = ndarray(list(range(0, 30)), shape=[5, 2, 3], format='@h', 3376 nd2 = ndarray(list(range(0, 30)), shape=[5, 2, 3], format='@h',
3244 flags=ND_FORTRAN) 3377 flags=ND_FORTRAN)
3245 v = memoryview(nd1) 3378 v = memoryview(nd1)
3246 w = memoryview(nd2) 3379 w = memoryview(nd2)
3247 3380
3248 self.assertEqual(v, nd1) 3381 self.assertEqual(v, nd1)
3249 self.assertEqual(w, nd2) 3382 self.assertEqual(w, nd2)
3250 self.assertNotEqual(v, nd2) 3383 self.assertNotEqual(v, nd2)
3251 self.assertNotEqual(w, nd1) 3384 self.assertNotEqual(w, nd1)
3252 self.assertNotEqual(v, w) 3385 self.assertNotEqual(v, w)
3253 3386
3254 # different shape 3387 # Fortran-contiguous, different values, struct module
3388 nd1 = ndarray([(2**64-1, -1)]*6, shape=[2, 3], format='=Qq',
3389 flags=ND_FORTRAN)
3390 nd2 = ndarray([(-1, 2**64-1)]*6, shape=[2, 3], format='=qQ',
3391 flags=ND_FORTRAN)
3392 v = memoryview(nd1)
3393 w = memoryview(nd2)
3394
3395 self.assertEqual(v, nd1)
3396 self.assertEqual(w, nd2)
3397 self.assertNotEqual(v, nd2)
3398 self.assertNotEqual(w, nd1)
3399 self.assertNotEqual(v, w)
3400
3401 # Fortran-contiguous, different shape
3255 nd1 = ndarray(list(range(-15, 15)), shape=[2, 3, 5], format='l', 3402 nd1 = ndarray(list(range(-15, 15)), shape=[2, 3, 5], format='l',
3256 flags=ND_FORTRAN) 3403 flags=ND_FORTRAN)
3257 nd2 = ndarray(list(range(-15, 15)), shape=[3, 2, 5], format='l', 3404 nd2 = ndarray(list(range(-15, 15)), shape=[3, 2, 5], format='l',
3258 flags=ND_FORTRAN) 3405 flags=ND_FORTRAN)
3259 v = memoryview(nd1) 3406 v = memoryview(nd1)
3260 w = memoryview(nd2) 3407 w = memoryview(nd2)
3261 3408
3262 self.assertEqual(v, nd1) 3409 self.assertEqual(v, nd1)
3263 self.assertEqual(w, nd2) 3410 self.assertEqual(w, nd2)
3264 self.assertNotEqual(v, nd2) 3411 self.assertNotEqual(v, nd2)
3265 self.assertNotEqual(w, nd1) 3412 self.assertNotEqual(w, nd1)
3266 self.assertNotEqual(v, w) 3413 self.assertNotEqual(v, w)
3267 3414
3268 # different format 3415 # Fortran-contiguous, different shape, struct module
3416 nd1 = ndarray(list(range(-15, 15)), shape=[2, 3, 5], format='0ll',
3417 flags=ND_FORTRAN)
3418 nd2 = ndarray(list(range(-15, 15)), shape=[3, 2, 5], format='l',
3419 flags=ND_FORTRAN)
3420 v = memoryview(nd1)
3421 w = memoryview(nd2)
3422
3423 self.assertEqual(v, nd1)
3424 self.assertEqual(w, nd2)
3425 self.assertNotEqual(v, nd2)
3426 self.assertNotEqual(w, nd1)
3427 self.assertNotEqual(v, w)
3428
3429 # Fortran-contiguous, different format, struct module
3269 nd1 = ndarray(list(range(30)), shape=[5, 2, 3], format='@h', 3430 nd1 = ndarray(list(range(30)), shape=[5, 2, 3], format='@h',
3270 flags=ND_FORTRAN) 3431 flags=ND_FORTRAN)
3271 nd2 = ndarray(list(range(30)), shape=[5, 2, 3], format='@b', 3432 nd2 = ndarray(list(range(30)), shape=[5, 2, 3], format='@b',
3272 flags=ND_FORTRAN) 3433 flags=ND_FORTRAN)
3273 v = memoryview(nd1) 3434 v = memoryview(nd1)
3274 w = memoryview(nd2) 3435 w = memoryview(nd2)
3275 3436
3276 self.assertEqual(v, nd1) 3437 self.assertEqual(v, nd1)
3277 self.assertEqual(w, nd2) 3438 self.assertEqual(w, nd2)
3278 self.assertNotEqual(v, nd2) 3439 self.assertEqual(v, nd2)
3279 self.assertNotEqual(w, nd1) 3440 self.assertEqual(w, nd1)
3280 self.assertNotEqual(v, w) 3441 self.assertEqual(v, w)
3281 3442
3282 ##### ndim > 1: mixed C/Fortran contiguous 3443 def test_memoryview_compare_multidim_mixed(self):
3444
3445 # mixed C/Fortran contiguous
3283 lst1 = list(range(-15, 15)) 3446 lst1 = list(range(-15, 15))
3284 lst2 = transpose(lst1, [3, 2, 5]) 3447 lst2 = transpose(lst1, [3, 2, 5])
3285 nd1 = ndarray(lst1, shape=[3, 2, 5], format='@l') 3448 nd1 = ndarray(lst1, shape=[3, 2, 5], format='@l')
3286 nd2 = ndarray(lst2, shape=[3, 2, 5], format='l', flags=ND_FORTRAN) 3449 nd2 = ndarray(lst2, shape=[3, 2, 5], format='l', flags=ND_FORTRAN)
3287 v = memoryview(nd1) 3450 v = memoryview(nd1)
3288 w = memoryview(nd2) 3451 w = memoryview(nd2)
3289 3452
3290 self.assertEqual(v, nd1) 3453 self.assertEqual(v, nd1)
3291 self.assertEqual(w, nd2) 3454 self.assertEqual(w, nd2)
3292 self.assertEqual(v, w) 3455 self.assertEqual(v, w)
3293 3456
3294 ##### ndim > 1: non-contiguous 3457 # mixed C/Fortran contiguous, struct module
3295 # different values 3458 lst1 = [(-3.3, -22, b'x')]*30
3459 lst1[5] = (-2.2, -22, b'x')
3460 lst2 = transpose(lst1, [3, 2, 5])
3461 nd1 = ndarray(lst1, shape=[3, 2, 5], format='d b c')
3462 nd2 = ndarray(lst2, shape=[3, 2, 5], format='d h c', flags=ND_FORTRAN)
3463 v = memoryview(nd1)
3464 w = memoryview(nd2)
3465
3466 self.assertEqual(v, nd1)
3467 self.assertEqual(w, nd2)
3468 self.assertEqual(v, w)
3469
3470 # different values, non-contiguous
3296 ex1 = ndarray(list(range(40)), shape=[5, 8], format='@I') 3471 ex1 = ndarray(list(range(40)), shape=[5, 8], format='@I')
3297 nd1 = ex1[3:1:-1, ::-2] 3472 nd1 = ex1[3:1:-1, ::-2]
3298 ex2 = ndarray(list(range(40)), shape=[5, 8], format='I') 3473 ex2 = ndarray(list(range(40)), shape=[5, 8], format='I')
3299 nd2 = ex2[1:3:1, ::-2] 3474 nd2 = ex2[1:3:1, ::-2]
3300 v = memoryview(nd1) 3475 v = memoryview(nd1)
3301 w = memoryview(nd2) 3476 w = memoryview(nd2)
3302 3477
3303 self.assertEqual(v, nd1) 3478 self.assertEqual(v, nd1)
3304 self.assertEqual(w, nd2) 3479 self.assertEqual(w, nd2)
3305 self.assertNotEqual(v, nd2) 3480 self.assertNotEqual(v, nd2)
3306 self.assertNotEqual(w, nd1) 3481 self.assertNotEqual(w, nd1)
3307 self.assertNotEqual(v, w) 3482 self.assertNotEqual(v, w)
3483
3484 # same values, non-contiguous, struct module
3485 ex1 = ndarray([(2**31-1, -2**31)]*22, shape=[11, 2], format='=ii')
3486 nd1 = ex1[3:1:-1, ::-2]
3487 ex2 = ndarray([(2**31-1, -2**31)]*22, shape=[11, 2], format='>ii')
3488 nd2 = ex2[1:3:1, ::-2]
3489 v = memoryview(nd1)
3490 w = memoryview(nd2)
3491
3492 self.assertEqual(v, nd1)
3493 self.assertEqual(w, nd2)
3494 self.assertEqual(v, nd2)
3495 self.assertEqual(w, nd1)
3496 self.assertEqual(v, w)
3308 3497
3309 # different shape 3498 # different shape
3310 ex1 = ndarray(list(range(30)), shape=[2, 3, 5], format='b') 3499 ex1 = ndarray(list(range(30)), shape=[2, 3, 5], format='b')
3311 nd1 = ex1[1:3:, ::-2] 3500 nd1 = ex1[1:3:, ::-2]
3312 nd2 = ndarray(list(range(30)), shape=[3, 2, 5], format='b') 3501 nd2 = ndarray(list(range(30)), shape=[3, 2, 5], format='b')
3313 nd2 = ex2[1:3:, ::-2] 3502 nd2 = ex2[1:3:, ::-2]
3314 v = memoryview(nd1) 3503 v = memoryview(nd1)
3315 w = memoryview(nd2) 3504 w = memoryview(nd2)
3316 3505
3317 self.assertEqual(v, nd1) 3506 self.assertEqual(v, nd1)
3318 self.assertEqual(w, nd2) 3507 self.assertEqual(w, nd2)
3319 self.assertNotEqual(v, nd2) 3508 self.assertNotEqual(v, nd2)
3320 self.assertNotEqual(w, nd1) 3509 self.assertNotEqual(w, nd1)
3321 self.assertNotEqual(v, w) 3510 self.assertNotEqual(v, w)
3322 3511
3323 # different format 3512 # different shape, struct module
3324 ex1 = ndarray(list(range(30)), shape=[5, 3, 2], format='i') 3513 ex1 = ndarray(list(range(30)), shape=[2, 3, 5], format='B')
3325 nd1 = ex1[1:3:, ::-2] 3514 nd1 = ex1[1:3:, ::-2]
3326 nd2 = ndarray(list(range(30)), shape=[5, 3, 2], format='@I') 3515 nd2 = ndarray(list(range(30)), shape=[3, 2, 5], format='b')
3327 nd2 = ex2[1:3:, ::-2] 3516 nd2 = ex2[1:3:, ::-2]
3328 v = memoryview(nd1) 3517 v = memoryview(nd1)
3329 w = memoryview(nd2) 3518 w = memoryview(nd2)
3330 3519
3331 self.assertEqual(v, nd1) 3520 self.assertEqual(v, nd1)
3332 self.assertEqual(w, nd2) 3521 self.assertEqual(w, nd2)
3333 self.assertNotEqual(v, nd2) 3522 self.assertNotEqual(v, nd2)
3334 self.assertNotEqual(w, nd1) 3523 self.assertNotEqual(w, nd1)
3335 self.assertNotEqual(v, w) 3524 self.assertNotEqual(v, w)
3336 3525
3337 ##### ndim > 1: zeros in shape 3526 # different format, struct module
3338 nd1 = ndarray(list(range(30)), shape=[0, 3, 2], format='i') 3527 ex1 = ndarray([(2, b'123')]*30, shape=[5, 3, 2], format='b3s')
3339 nd2 = ndarray(list(range(30)), shape=[5, 0, 2], format='@i') 3528 nd1 = ex1[1:3:, ::-2]
3529 nd2 = ndarray([(2, b'123')]*30, shape=[5, 3, 2], format='i3s')
3530 nd2 = ex2[1:3:, ::-2]
3340 v = memoryview(nd1) 3531 v = memoryview(nd1)
3341 w = memoryview(nd2) 3532 w = memoryview(nd2)
3342 3533
3343 self.assertEqual(v, nd1) 3534 self.assertEqual(v, nd1)
3344 self.assertEqual(w, nd2) 3535 self.assertEqual(w, nd2)
3345 self.assertNotEqual(v, nd2) 3536 self.assertNotEqual(v, nd2)
3346 self.assertNotEqual(w, nd1) 3537 self.assertNotEqual(w, nd1)
3347 self.assertNotEqual(v, w) 3538 self.assertNotEqual(v, w)
3348 3539
3349 # ndim > 1: zero strides 3540 def test_memoryview_compare_multidim_zero_shape(self):
3541
3542 # zeros in shape
3543 nd1 = ndarray(list(range(30)), shape=[0, 3, 2], format='i')
3544 nd2 = ndarray(list(range(30)), shape=[5, 0, 2], format='@i')
3545 v = memoryview(nd1)
3546 w = memoryview(nd2)
3547
3548 self.assertEqual(v, nd1)
3549 self.assertEqual(w, nd2)
3550 self.assertNotEqual(v, nd2)
3551 self.assertNotEqual(w, nd1)
3552 self.assertNotEqual(v, w)
3553
3554 # zeros in shape, struct module
3555 nd1 = ndarray(list(range(30)), shape=[0, 3, 2], format='i')
3556 nd2 = ndarray(list(range(30)), shape=[5, 0, 2], format='@i')
3557 v = memoryview(nd1)
3558 w = memoryview(nd2)
3559
3560 self.assertEqual(v, nd1)
3561 self.assertEqual(w, nd2)
3562 self.assertNotEqual(v, nd2)
3563 self.assertNotEqual(w, nd1)
3564 self.assertNotEqual(v, w)
3565
3566 def test_memoryview_compare_multidim_zero_strides(self):
3567
3568 # zero strides
3350 nd1 = ndarray([900]*80, shape=[4, 5, 4], format='@L') 3569 nd1 = ndarray([900]*80, shape=[4, 5, 4], format='@L')
3351 nd2 = ndarray([900], shape=[4, 5, 4], strides=[0, 0, 0], format='L') 3570 nd2 = ndarray([900], shape=[4, 5, 4], strides=[0, 0, 0], format='L')
3352 v = memoryview(nd1) 3571 v = memoryview(nd1)
3353 w = memoryview(nd2) 3572 w = memoryview(nd2)
3354 3573
3355 self.assertEqual(v, nd1) 3574 self.assertEqual(v, nd1)
3356 self.assertEqual(w, nd2) 3575 self.assertEqual(w, nd2)
3357 self.assertEqual(v, nd2) 3576 self.assertEqual(v, nd2)
3358 self.assertEqual(w, nd1) 3577 self.assertEqual(w, nd1)
3359 self.assertEqual(v, w) 3578 self.assertEqual(v, w)
3360 self.assertEqual(v.tolist(), w.tolist()) 3579 self.assertEqual(v.tolist(), w.tolist())
3361 3580
3362 ##### ndim > 1: suboffsets 3581 # zero strides, struct module
3582 nd1 = ndarray([(1, 2)]*10, shape=[2, 5], format='=lQ')
3583 nd2 = ndarray([(1, 2)], shape=[2, 5], strides=[0, 0], format='<lQ')
3584 v = memoryview(nd1)
3585 w = memoryview(nd2)
3586
3587 self.assertEqual(v, nd1)
3588 self.assertEqual(w, nd2)
3589 self.assertEqual(v, nd2)
3590 self.assertEqual(w, nd1)
3591 self.assertEqual(v, w)
3592
3593 def test_memoryview_compare_multidim_suboffsets(self):
3594
3595 # suboffsets
3363 ex1 = ndarray(list(range(40)), shape=[5, 8], format='@I') 3596 ex1 = ndarray(list(range(40)), shape=[5, 8], format='@I')
3364 nd1 = ex1[3:1:-1, ::-2] 3597 nd1 = ex1[3:1:-1, ::-2]
3365 ex2 = ndarray(list(range(40)), shape=[5, 8], format='I', flags=ND_PIL) 3598 ex2 = ndarray(list(range(40)), shape=[5, 8], format='I', flags=ND_PIL)
3366 nd2 = ex2[1:3:1, ::-2] 3599 nd2 = ex2[1:3:1, ::-2]
3367 v = memoryview(nd1) 3600 v = memoryview(nd1)
3368 w = memoryview(nd2) 3601 w = memoryview(nd2)
3369 3602
3370 self.assertEqual(v, nd1) 3603 self.assertEqual(v, nd1)
3371 self.assertEqual(w, nd2) 3604 self.assertEqual(w, nd2)
3372 self.assertNotEqual(v, nd2) 3605 self.assertNotEqual(v, nd2)
3373 self.assertNotEqual(w, nd1) 3606 self.assertNotEqual(w, nd1)
3374 self.assertNotEqual(v, w) 3607 self.assertNotEqual(v, w)
3375 3608
3376 # different shape 3609 # suboffsets, struct module
3377 ex1 = ndarray(list(range(30)), shape=[2, 3, 5], format='b', flags=ND_PIL ) 3610 ex1 = ndarray([(2**64-1, -1)]*40, shape=[5, 8], format='=Qq',
3611 flags=ND_WRITABLE)
3612 ex1[2][7] = (1, -2)
3613 nd1 = ex1[3:1:-1, ::-2]
3614
3615 ex2 = ndarray([(2**64-1, -1)]*40, shape=[5, 8], format='>Qq',
3616 flags=ND_PIL|ND_WRITABLE)
3617 ex2[2][7] = (1, -2)
3618 nd2 = ex2[1:3:1, ::-2]
3619
3620 v = memoryview(nd1)
3621 w = memoryview(nd2)
3622
3623 self.assertEqual(v, nd1)
3624 self.assertEqual(w, nd2)
3625 self.assertEqual(v, nd2)
3626 self.assertEqual(w, nd1)
3627 self.assertEqual(v, w)
3628
3629 # suboffsets, different shape
3630 ex1 = ndarray(list(range(30)), shape=[2, 3, 5], format='b',
3631 flags=ND_PIL)
3378 nd1 = ex1[1:3:, ::-2] 3632 nd1 = ex1[1:3:, ::-2]
3379 nd2 = ndarray(list(range(30)), shape=[3, 2, 5], format='b') 3633 nd2 = ndarray(list(range(30)), shape=[3, 2, 5], format='b')
3380 nd2 = ex2[1:3:, ::-2] 3634 nd2 = ex2[1:3:, ::-2]
3381 v = memoryview(nd1) 3635 v = memoryview(nd1)
3382 w = memoryview(nd2) 3636 w = memoryview(nd2)
3383 3637
3384 self.assertEqual(v, nd1) 3638 self.assertEqual(v, nd1)
3385 self.assertEqual(w, nd2) 3639 self.assertEqual(w, nd2)
3386 self.assertNotEqual(v, nd2) 3640 self.assertNotEqual(v, nd2)
3387 self.assertNotEqual(w, nd1) 3641 self.assertNotEqual(w, nd1)
3388 self.assertNotEqual(v, w) 3642 self.assertNotEqual(v, w)
3389 3643
3390 # different format 3644 # suboffsets, different shape, struct module
3645 ex1 = ndarray([(2**8-1, -1)]*40, shape=[2, 3, 5], format='Bb',
3646 flags=ND_PIL|ND_WRITABLE)
3647 nd1 = ex1[1:2:, ::-2]
3648
3649 ex2 = ndarray([(2**8-1, -1)]*40, shape=[3, 2, 5], format='Bb')
3650 nd2 = ex2[1:2:, ::-2]
3651
3652 v = memoryview(nd1)
3653 w = memoryview(nd2)
3654
3655 self.assertEqual(v, nd1)
3656 self.assertEqual(w, nd2)
3657 self.assertNotEqual(v, nd2)
3658 self.assertNotEqual(w, nd1)
3659 self.assertNotEqual(v, w)
3660
3661 # suboffsets, different format
3391 ex1 = ndarray(list(range(30)), shape=[5, 3, 2], format='i', flags=ND_PIL ) 3662 ex1 = ndarray(list(range(30)), shape=[5, 3, 2], format='i', flags=ND_PIL )
3392 nd1 = ex1[1:3:, ::-2] 3663 nd1 = ex1[1:3:, ::-2]
3393 nd2 = ndarray(list(range(30)), shape=[5, 3, 2], format='@I', flags=ND_PI L) 3664 ex2 = ndarray(list(range(30)), shape=[5, 3, 2], format='@I', flags=ND_PI L)
3394 nd2 = ex2[1:3:, ::-2] 3665 nd2 = ex2[1:3:, ::-2]
3666 v = memoryview(nd1)
3667 w = memoryview(nd2)
3668
3669 self.assertEqual(v, nd1)
3670 self.assertEqual(w, nd2)
3671 self.assertEqual(v, nd2)
3672 self.assertEqual(w, nd1)
3673 self.assertEqual(v, w)
3674
3675 # suboffsets, different format, struct module
3676 ex1 = ndarray([(b'hello', b'', 1)]*27, shape=[3, 3, 3], format='5s0sP',
3677 flags=ND_PIL|ND_WRITABLE)
3678 ex1[1][2][2] = (b'sushi', b'', 1)
3679 nd1 = ex1[1:3:, ::-2]
3680
3681 ex2 = ndarray([(b'hello', b'', 1)]*27, shape=[3, 3, 3], format='5s0sP',
3682 flags=ND_PIL|ND_WRITABLE)
3683 ex1[1][2][2] = (b'sushi', b'', 1)
3684 nd2 = ex2[1:3:, ::-2]
3685
3395 v = memoryview(nd1) 3686 v = memoryview(nd1)
3396 w = memoryview(nd2) 3687 w = memoryview(nd2)
3397 3688
3398 self.assertEqual(v, nd1) 3689 self.assertEqual(v, nd1)
3399 self.assertEqual(w, nd2) 3690 self.assertEqual(w, nd2)
3400 self.assertNotEqual(v, nd2) 3691 self.assertNotEqual(v, nd2)
3401 self.assertNotEqual(w, nd1) 3692 self.assertNotEqual(w, nd1)
3402 self.assertNotEqual(v, w) 3693 self.assertNotEqual(v, w)
3403 3694
3404 # initialize mixed C/Fortran + suboffsets 3695 # initialize mixed C/Fortran + suboffsets
3405 lst1 = list(range(-15, 15)) 3696 lst1 = list(range(-15, 15))
3406 lst2 = transpose(lst1, [3, 2, 5]) 3697 lst2 = transpose(lst1, [3, 2, 5])
3407 nd1 = ndarray(lst1, shape=[3, 2, 5], format='@l', flags=ND_PIL) 3698 nd1 = ndarray(lst1, shape=[3, 2, 5], format='@l', flags=ND_PIL)
3408 nd2 = ndarray(lst2, shape=[3, 2, 5], format='l', flags=ND_FORTRAN|ND_PIL ) 3699 nd2 = ndarray(lst2, shape=[3, 2, 5], format='l', flags=ND_FORTRAN|ND_PIL )
3409 v = memoryview(nd1) 3700 v = memoryview(nd1)
3410 w = memoryview(nd2) 3701 w = memoryview(nd2)
3411 3702
3412 self.assertEqual(v, nd1) 3703 self.assertEqual(v, nd1)
3413 self.assertEqual(w, nd2) 3704 self.assertEqual(w, nd2)
3414 self.assertEqual(v, w) 3705 self.assertEqual(v, w)
3706
3707 # initialize mixed C/Fortran + suboffsets, struct module
3708 lst1 = [(b'sashimi', b'sliced', 20.05)]*30
3709 lst1[11] = (b'ramen', b'spicy', 9.45)
3710 lst2 = transpose(lst1, [3, 2, 5])
3711
3712 nd1 = ndarray(lst1, shape=[3, 2, 5], format='< 10p 9p d', flags=ND_PIL)
3713 nd2 = ndarray(lst2, shape=[3, 2, 5], format='> 10p 9p d',
3714 flags=ND_FORTRAN|ND_PIL)
3715 v = memoryview(nd1)
3716 w = memoryview(nd2)
3717
3718 self.assertEqual(v, nd1)
3719 self.assertEqual(w, nd2)
3720 self.assertEqual(v, w)
3721
3722 def test_memoryview_compare_not_equal(self):
3723
3724 # items not equal
3725 for byteorder in ['=', '<', '>', '!']:
3726 x = ndarray([2**63]*120, shape=[3,5,2,2,2], format=byteorder+'Q')
3727 y = ndarray([2**63]*120, shape=[3,5,2,2,2], format=byteorder+'Q',
3728 flags=ND_WRITABLE|ND_FORTRAN)
3729 y[2][3][1][1][1] = 1
3730 a = memoryview(x)
3731 b = memoryview(y)
3732 self.assertEqual(a, x)
3733 self.assertEqual(b, y)
3734 self.assertNotEqual(a, b)
3735 self.assertNotEqual(a, y)
3736 self.assertNotEqual(b, x)
3737
3738 x = ndarray([(2**63, 2**31, 2**15)]*120, shape=[3,5,2,2,2],
3739 format=byteorder+'QLH')
3740 y = ndarray([(2**63, 2**31, 2**15)]*120, shape=[3,5,2,2,2],
3741 format=byteorder+'QLH', flags=ND_WRITABLE|ND_FORTRAN)
3742 y[2][3][1][1][1] = (1, 1, 1)
3743 a = memoryview(x)
3744 b = memoryview(y)
3745 self.assertEqual(a, x)
3746 self.assertEqual(b, y)
3747 self.assertNotEqual(a, b)
3748 self.assertNotEqual(a, y)
3749 self.assertNotEqual(b, x)
3415 3750
3416 def test_memoryview_check_released(self): 3751 def test_memoryview_check_released(self):
3417 3752
3418 a = array.array('d', [1.1, 2.2, 3.3]) 3753 a = array.array('d', [1.1, 2.2, 3.3])
3419 3754
3420 m = memoryview(a) 3755 m = memoryview(a)
3421 m.release() 3756 m.release()
3422 3757
3423 # PyMemoryView_FromObject() 3758 # PyMemoryView_FromObject()
3424 self.assertRaises(ValueError, memoryview, m) 3759 self.assertRaises(ValueError, memoryview, m)
(...skipping 24 matching lines...) Expand all
3449 3784
3450 self.assertEqual(m1, m2) 3785 self.assertEqual(m1, m2)
3451 m1.release() 3786 m1.release()
3452 self.assertNotEqual(m1, m2) 3787 self.assertNotEqual(m1, m2)
3453 self.assertNotEqual(m1, a) 3788 self.assertNotEqual(m1, a)
3454 self.assertEqual(m1, m1) 3789 self.assertEqual(m1, m1)
3455 3790
3456 def test_memoryview_tobytes(self): 3791 def test_memoryview_tobytes(self):
3457 # Many implicit tests are already in self.verify(). 3792 # Many implicit tests are already in self.verify().
3458 3793
3459 nd = ndarray([-529, 576, -625, 676, -729], shape=[5], format='@h') 3794 t = (-529, 576, -625, 676, -729)
3460 3795
3796 nd = ndarray(t, shape=[5], format='@h')
3461 m = memoryview(nd) 3797 m = memoryview(nd)
3798 self.assertEqual(m, nd)
3462 self.assertEqual(m.tobytes(), nd.tobytes()) 3799 self.assertEqual(m.tobytes(), nd.tobytes())
3800
3801 nd = ndarray([t], shape=[1], format='>hQiLl')
3802 m = memoryview(nd)
3803 self.assertEqual(m, nd)
3804 self.assertEqual(m.tobytes(), nd.tobytes())
3805
3806 nd = ndarray([t for _ in range(12)], shape=[2,2,3], format='=hQiLl')
3807 m = memoryview(nd)
3808 self.assertEqual(m, nd)
3809 self.assertEqual(m.tobytes(), nd.tobytes())
3810
3811 nd = ndarray([t for _ in range(120)], shape=[5,2,2,3,2],
3812 format='<hQiLl')
3813 m = memoryview(nd)
3814 self.assertEqual(m, nd)
3815 self.assertEqual(m.tobytes(), nd.tobytes())
3816
3817 # Unknown formats are handled: tobytes() purely depends on itemsize.
3818 if ctypes:
3819 # format: "T{>l:x:>l:y:}"
3820 class BEPoint(ctypes.BigEndianStructure):
3821 _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
3822 point = BEPoint(100, 200)
3823 a = memoryview(point)
3824 self.assertEqual(a.tobytes(), bytes(point))
3463 3825
3464 def test_memoryview_get_contiguous(self): 3826 def test_memoryview_get_contiguous(self):
3465 # Many implicit tests are already in self.verify(). 3827 # Many implicit tests are already in self.verify().
3466 3828
3467 # no buffer interface 3829 # no buffer interface
3468 self.assertRaises(TypeError, get_contiguous, {}, PyBUF_READ, 'F') 3830 self.assertRaises(TypeError, get_contiguous, {}, PyBUF_READ, 'F')
3469 3831
3470 # writable request to read-only object 3832 # writable request to read-only object
3471 self.assertRaises(BufferError, get_contiguous, b'x', PyBUF_WRITE, 'C') 3833 self.assertRaises(BufferError, get_contiguous, b'x', PyBUF_WRITE, 'C')
3472 3834
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
3906 x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL) 4268 x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL)
3907 self.assertRaises(BufferError, memoryview, x) 4269 self.assertRaises(BufferError, memoryview, x)
3908 4270
3909 4271
3910 def test_main(): 4272 def test_main():
3911 support.run_unittest(TestBufferProtocol) 4273 support.run_unittest(TestBufferProtocol)
3912 4274
3913 4275
3914 if __name__ == "__main__": 4276 if __name__ == "__main__":
3915 test_main() 4277 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+