| 1 | n/a | # |
|---|
| 2 | n/a | # The ndarray object from _testbuffer.c is a complete implementation of |
|---|
| 3 | n/a | # a PEP-3118 buffer provider. It is independent from NumPy's ndarray |
|---|
| 4 | n/a | # and the tests don't require NumPy. |
|---|
| 5 | n/a | # |
|---|
| 6 | n/a | # If NumPy is present, some tests check both ndarray implementations |
|---|
| 7 | n/a | # against each other. |
|---|
| 8 | n/a | # |
|---|
| 9 | n/a | # Most ndarray tests also check that memoryview(ndarray) behaves in |
|---|
| 10 | n/a | # the same way as the original. Thus, a substantial part of the |
|---|
| 11 | n/a | # memoryview tests is now in this module. |
|---|
| 12 | n/a | # |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | import contextlib |
|---|
| 15 | n/a | import unittest |
|---|
| 16 | n/a | from test import support |
|---|
| 17 | n/a | from itertools import permutations, product |
|---|
| 18 | n/a | from random import randrange, sample, choice |
|---|
| 19 | n/a | import warnings |
|---|
| 20 | n/a | import sys, array, io |
|---|
| 21 | n/a | from decimal import Decimal |
|---|
| 22 | n/a | from fractions import Fraction |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | try: |
|---|
| 25 | n/a | from _testbuffer import * |
|---|
| 26 | n/a | except ImportError: |
|---|
| 27 | n/a | ndarray = None |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | try: |
|---|
| 30 | n/a | import struct |
|---|
| 31 | n/a | except ImportError: |
|---|
| 32 | n/a | struct = None |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | try: |
|---|
| 35 | n/a | import ctypes |
|---|
| 36 | n/a | except ImportError: |
|---|
| 37 | n/a | ctypes = None |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | try: |
|---|
| 40 | n/a | with warnings.catch_warnings(): |
|---|
| 41 | n/a | from numpy import ndarray as numpy_array |
|---|
| 42 | n/a | except ImportError: |
|---|
| 43 | n/a | numpy_array = None |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | SHORT_TEST = True |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | # ====================================================================== |
|---|
| 50 | n/a | # Random lists by format specifier |
|---|
| 51 | n/a | # ====================================================================== |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | # Native format chars and their ranges. |
|---|
| 54 | n/a | NATIVE = { |
|---|
| 55 | n/a | '?':0, 'c':0, 'b':0, 'B':0, |
|---|
| 56 | n/a | 'h':0, 'H':0, 'i':0, 'I':0, |
|---|
| 57 | n/a | 'l':0, 'L':0, 'n':0, 'N':0, |
|---|
| 58 | n/a | 'f':0, 'd':0, 'P':0 |
|---|
| 59 | n/a | } |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | # NumPy does not have 'n' or 'N': |
|---|
| 62 | n/a | if numpy_array: |
|---|
| 63 | n/a | del NATIVE['n'] |
|---|
| 64 | n/a | del NATIVE['N'] |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | if struct: |
|---|
| 67 | n/a | try: |
|---|
| 68 | n/a | # Add "qQ" if present in native mode. |
|---|
| 69 | n/a | struct.pack('Q', 2**64-1) |
|---|
| 70 | n/a | NATIVE['q'] = 0 |
|---|
| 71 | n/a | NATIVE['Q'] = 0 |
|---|
| 72 | n/a | except struct.error: |
|---|
| 73 | n/a | pass |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | # Standard format chars and their ranges. |
|---|
| 76 | n/a | STANDARD = { |
|---|
| 77 | n/a | '?':(0, 2), 'c':(0, 1<<8), |
|---|
| 78 | n/a | 'b':(-(1<<7), 1<<7), 'B':(0, 1<<8), |
|---|
| 79 | n/a | 'h':(-(1<<15), 1<<15), 'H':(0, 1<<16), |
|---|
| 80 | n/a | 'i':(-(1<<31), 1<<31), 'I':(0, 1<<32), |
|---|
| 81 | n/a | 'l':(-(1<<31), 1<<31), 'L':(0, 1<<32), |
|---|
| 82 | n/a | 'q':(-(1<<63), 1<<63), 'Q':(0, 1<<64), |
|---|
| 83 | n/a | 'f':(-(1<<63), 1<<63), 'd':(-(1<<1023), 1<<1023) |
|---|
| 84 | n/a | } |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | def native_type_range(fmt): |
|---|
| 87 | n/a | """Return range of a native type.""" |
|---|
| 88 | n/a | if fmt == 'c': |
|---|
| 89 | n/a | lh = (0, 256) |
|---|
| 90 | n/a | elif fmt == '?': |
|---|
| 91 | n/a | lh = (0, 2) |
|---|
| 92 | n/a | elif fmt == 'f': |
|---|
| 93 | n/a | lh = (-(1<<63), 1<<63) |
|---|
| 94 | n/a | elif fmt == 'd': |
|---|
| 95 | n/a | lh = (-(1<<1023), 1<<1023) |
|---|
| 96 | n/a | else: |
|---|
| 97 | n/a | for exp in (128, 127, 64, 63, 32, 31, 16, 15, 8, 7): |
|---|
| 98 | n/a | try: |
|---|
| 99 | n/a | struct.pack(fmt, (1<<exp)-1) |
|---|
| 100 | n/a | break |
|---|
| 101 | n/a | except struct.error: |
|---|
| 102 | n/a | pass |
|---|
| 103 | n/a | lh = (-(1<<exp), 1<<exp) if exp & 1 else (0, 1<<exp) |
|---|
| 104 | n/a | return lh |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | fmtdict = { |
|---|
| 107 | n/a | '':NATIVE, |
|---|
| 108 | n/a | '@':NATIVE, |
|---|
| 109 | n/a | '<':STANDARD, |
|---|
| 110 | n/a | '>':STANDARD, |
|---|
| 111 | n/a | '=':STANDARD, |
|---|
| 112 | n/a | '!':STANDARD |
|---|
| 113 | n/a | } |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | if struct: |
|---|
| 116 | n/a | for fmt in fmtdict['@']: |
|---|
| 117 | n/a | fmtdict['@'][fmt] = native_type_range(fmt) |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | MEMORYVIEW = NATIVE.copy() |
|---|
| 120 | n/a | ARRAY = NATIVE.copy() |
|---|
| 121 | n/a | for k in NATIVE: |
|---|
| 122 | n/a | if not k in "bBhHiIlLfd": |
|---|
| 123 | n/a | del ARRAY[k] |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | BYTEFMT = NATIVE.copy() |
|---|
| 126 | n/a | for k in NATIVE: |
|---|
| 127 | n/a | if not k in "Bbc": |
|---|
| 128 | n/a | del BYTEFMT[k] |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | fmtdict['m'] = MEMORYVIEW |
|---|
| 131 | n/a | fmtdict['@m'] = MEMORYVIEW |
|---|
| 132 | n/a | fmtdict['a'] = ARRAY |
|---|
| 133 | n/a | fmtdict['b'] = BYTEFMT |
|---|
| 134 | n/a | fmtdict['@b'] = BYTEFMT |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | # Capabilities of the test objects: |
|---|
| 137 | n/a | MODE = 0 |
|---|
| 138 | n/a | MULT = 1 |
|---|
| 139 | n/a | cap = { # format chars # multiplier |
|---|
| 140 | n/a | 'ndarray': (['', '@', '<', '>', '=', '!'], ['', '1', '2', '3']), |
|---|
| 141 | n/a | 'array': (['a'], ['']), |
|---|
| 142 | n/a | 'numpy': ([''], ['']), |
|---|
| 143 | n/a | 'memoryview': (['@m', 'm'], ['']), |
|---|
| 144 | n/a | 'bytefmt': (['@b', 'b'], ['']), |
|---|
| 145 | n/a | } |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | def randrange_fmt(mode, char, obj): |
|---|
| 148 | n/a | """Return random item for a type specified by a mode and a single |
|---|
| 149 | n/a | format character.""" |
|---|
| 150 | n/a | x = randrange(*fmtdict[mode][char]) |
|---|
| 151 | n/a | if char == 'c': |
|---|
| 152 | n/a | x = bytes([x]) |
|---|
| 153 | n/a | if obj == 'numpy' and x == b'\x00': |
|---|
| 154 | n/a | # http://projects.scipy.org/numpy/ticket/1925 |
|---|
| 155 | n/a | x = b'\x01' |
|---|
| 156 | n/a | if char == '?': |
|---|
| 157 | n/a | x = bool(x) |
|---|
| 158 | n/a | if char == 'f' or char == 'd': |
|---|
| 159 | n/a | x = struct.pack(char, x) |
|---|
| 160 | n/a | x = struct.unpack(char, x)[0] |
|---|
| 161 | n/a | return x |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | def gen_item(fmt, obj): |
|---|
| 164 | n/a | """Return single random item.""" |
|---|
| 165 | n/a | mode, chars = fmt.split('#') |
|---|
| 166 | n/a | x = [] |
|---|
| 167 | n/a | for c in chars: |
|---|
| 168 | n/a | x.append(randrange_fmt(mode, c, obj)) |
|---|
| 169 | n/a | return x[0] if len(x) == 1 else tuple(x) |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | def gen_items(n, fmt, obj): |
|---|
| 172 | n/a | """Return a list of random items (or a scalar).""" |
|---|
| 173 | n/a | if n == 0: |
|---|
| 174 | n/a | return gen_item(fmt, obj) |
|---|
| 175 | n/a | lst = [0] * n |
|---|
| 176 | n/a | for i in range(n): |
|---|
| 177 | n/a | lst[i] = gen_item(fmt, obj) |
|---|
| 178 | n/a | return lst |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | def struct_items(n, obj): |
|---|
| 181 | n/a | mode = choice(cap[obj][MODE]) |
|---|
| 182 | n/a | xfmt = mode + '#' |
|---|
| 183 | n/a | fmt = mode.strip('amb') |
|---|
| 184 | n/a | nmemb = randrange(2, 10) # number of struct members |
|---|
| 185 | n/a | for _ in range(nmemb): |
|---|
| 186 | n/a | char = choice(tuple(fmtdict[mode])) |
|---|
| 187 | n/a | multiplier = choice(cap[obj][MULT]) |
|---|
| 188 | n/a | xfmt += (char * int(multiplier if multiplier else 1)) |
|---|
| 189 | n/a | fmt += (multiplier + char) |
|---|
| 190 | n/a | items = gen_items(n, xfmt, obj) |
|---|
| 191 | n/a | item = gen_item(xfmt, obj) |
|---|
| 192 | n/a | return fmt, items, item |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | def randitems(n, obj='ndarray', mode=None, char=None): |
|---|
| 195 | n/a | """Return random format, items, item.""" |
|---|
| 196 | n/a | if mode is None: |
|---|
| 197 | n/a | mode = choice(cap[obj][MODE]) |
|---|
| 198 | n/a | if char is None: |
|---|
| 199 | n/a | char = choice(tuple(fmtdict[mode])) |
|---|
| 200 | n/a | multiplier = choice(cap[obj][MULT]) |
|---|
| 201 | n/a | fmt = mode + '#' + char * int(multiplier if multiplier else 1) |
|---|
| 202 | n/a | items = gen_items(n, fmt, obj) |
|---|
| 203 | n/a | item = gen_item(fmt, obj) |
|---|
| 204 | n/a | fmt = mode.strip('amb') + multiplier + char |
|---|
| 205 | n/a | return fmt, items, item |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | def iter_mode(n, obj='ndarray'): |
|---|
| 208 | n/a | """Iterate through supported mode/char combinations.""" |
|---|
| 209 | n/a | for mode in cap[obj][MODE]: |
|---|
| 210 | n/a | for char in fmtdict[mode]: |
|---|
| 211 | n/a | yield randitems(n, obj, mode, char) |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | def iter_format(nitems, testobj='ndarray'): |
|---|
| 214 | n/a | """Yield (format, items, item) for all possible modes and format |
|---|
| 215 | n/a | characters plus one random compound format string.""" |
|---|
| 216 | n/a | for t in iter_mode(nitems, testobj): |
|---|
| 217 | n/a | yield t |
|---|
| 218 | n/a | if testobj != 'ndarray': |
|---|
| 219 | n/a | return |
|---|
| 220 | n/a | yield struct_items(nitems, testobj) |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | def is_byte_format(fmt): |
|---|
| 224 | n/a | return 'c' in fmt or 'b' in fmt or 'B' in fmt |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | def is_memoryview_format(fmt): |
|---|
| 227 | n/a | """format suitable for memoryview""" |
|---|
| 228 | n/a | x = len(fmt) |
|---|
| 229 | n/a | return ((x == 1 or (x == 2 and fmt[0] == '@')) and |
|---|
| 230 | n/a | fmt[x-1] in MEMORYVIEW) |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | NON_BYTE_FORMAT = [c for c in fmtdict['@'] if not is_byte_format(c)] |
|---|
| 233 | n/a | |
|---|
| 234 | n/a | |
|---|
| 235 | n/a | # ====================================================================== |
|---|
| 236 | n/a | # Multi-dimensional tolist(), slicing and slice assignments |
|---|
| 237 | n/a | # ====================================================================== |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | def atomp(lst): |
|---|
| 240 | n/a | """Tuple items (representing structs) are regarded as atoms.""" |
|---|
| 241 | n/a | return not isinstance(lst, list) |
|---|
| 242 | n/a | |
|---|
| 243 | n/a | def listp(lst): |
|---|
| 244 | n/a | return isinstance(lst, list) |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | def prod(lst): |
|---|
| 247 | n/a | """Product of list elements.""" |
|---|
| 248 | n/a | if len(lst) == 0: |
|---|
| 249 | n/a | return 0 |
|---|
| 250 | n/a | x = lst[0] |
|---|
| 251 | n/a | for v in lst[1:]: |
|---|
| 252 | n/a | x *= v |
|---|
| 253 | n/a | return x |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | def strides_from_shape(ndim, shape, itemsize, layout): |
|---|
| 256 | n/a | """Calculate strides of a contiguous array. Layout is 'C' or |
|---|
| 257 | n/a | 'F' (Fortran).""" |
|---|
| 258 | n/a | if ndim == 0: |
|---|
| 259 | n/a | return () |
|---|
| 260 | n/a | if layout == 'C': |
|---|
| 261 | n/a | strides = list(shape[1:]) + [itemsize] |
|---|
| 262 | n/a | for i in range(ndim-2, -1, -1): |
|---|
| 263 | n/a | strides[i] *= strides[i+1] |
|---|
| 264 | n/a | else: |
|---|
| 265 | n/a | strides = [itemsize] + list(shape[:-1]) |
|---|
| 266 | n/a | for i in range(1, ndim): |
|---|
| 267 | n/a | strides[i] *= strides[i-1] |
|---|
| 268 | n/a | return strides |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | def _ca(items, s): |
|---|
| 271 | n/a | """Convert flat item list to the nested list representation of a |
|---|
| 272 | n/a | multidimensional C array with shape 's'.""" |
|---|
| 273 | n/a | if atomp(items): |
|---|
| 274 | n/a | return items |
|---|
| 275 | n/a | if len(s) == 0: |
|---|
| 276 | n/a | return items[0] |
|---|
| 277 | n/a | lst = [0] * s[0] |
|---|
| 278 | n/a | stride = len(items) // s[0] if s[0] else 0 |
|---|
| 279 | n/a | for i in range(s[0]): |
|---|
| 280 | n/a | start = i*stride |
|---|
| 281 | n/a | lst[i] = _ca(items[start:start+stride], s[1:]) |
|---|
| 282 | n/a | return lst |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | def _fa(items, s): |
|---|
| 285 | n/a | """Convert flat item list to the nested list representation of a |
|---|
| 286 | n/a | multidimensional Fortran array with shape 's'.""" |
|---|
| 287 | n/a | if atomp(items): |
|---|
| 288 | n/a | return items |
|---|
| 289 | n/a | if len(s) == 0: |
|---|
| 290 | n/a | return items[0] |
|---|
| 291 | n/a | lst = [0] * s[0] |
|---|
| 292 | n/a | stride = s[0] |
|---|
| 293 | n/a | for i in range(s[0]): |
|---|
| 294 | n/a | lst[i] = _fa(items[i::stride], s[1:]) |
|---|
| 295 | n/a | return lst |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | def carray(items, shape): |
|---|
| 298 | n/a | if listp(items) and not 0 in shape and prod(shape) != len(items): |
|---|
| 299 | n/a | raise ValueError("prod(shape) != len(items)") |
|---|
| 300 | n/a | return _ca(items, shape) |
|---|
| 301 | n/a | |
|---|
| 302 | n/a | def farray(items, shape): |
|---|
| 303 | n/a | if listp(items) and not 0 in shape and prod(shape) != len(items): |
|---|
| 304 | n/a | raise ValueError("prod(shape) != len(items)") |
|---|
| 305 | n/a | return _fa(items, shape) |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | def indices(shape): |
|---|
| 308 | n/a | """Generate all possible tuples of indices.""" |
|---|
| 309 | n/a | iterables = [range(v) for v in shape] |
|---|
| 310 | n/a | return product(*iterables) |
|---|
| 311 | n/a | |
|---|
| 312 | n/a | def getindex(ndim, ind, strides): |
|---|
| 313 | n/a | """Convert multi-dimensional index to the position in the flat list.""" |
|---|
| 314 | n/a | ret = 0 |
|---|
| 315 | n/a | for i in range(ndim): |
|---|
| 316 | n/a | ret += strides[i] * ind[i] |
|---|
| 317 | n/a | return ret |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | def transpose(src, shape): |
|---|
| 320 | n/a | """Transpose flat item list that is regarded as a multi-dimensional |
|---|
| 321 | n/a | matrix defined by shape: dest...[k][j][i] = src[i][j][k]... """ |
|---|
| 322 | n/a | if not shape: |
|---|
| 323 | n/a | return src |
|---|
| 324 | n/a | ndim = len(shape) |
|---|
| 325 | n/a | sstrides = strides_from_shape(ndim, shape, 1, 'C') |
|---|
| 326 | n/a | dstrides = strides_from_shape(ndim, shape[::-1], 1, 'C') |
|---|
| 327 | n/a | dest = [0] * len(src) |
|---|
| 328 | n/a | for ind in indices(shape): |
|---|
| 329 | n/a | fr = getindex(ndim, ind, sstrides) |
|---|
| 330 | n/a | to = getindex(ndim, ind[::-1], dstrides) |
|---|
| 331 | n/a | dest[to] = src[fr] |
|---|
| 332 | n/a | return dest |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | def _flatten(lst): |
|---|
| 335 | n/a | """flatten list""" |
|---|
| 336 | n/a | if lst == []: |
|---|
| 337 | n/a | return lst |
|---|
| 338 | n/a | if atomp(lst): |
|---|
| 339 | n/a | return [lst] |
|---|
| 340 | n/a | return _flatten(lst[0]) + _flatten(lst[1:]) |
|---|
| 341 | n/a | |
|---|
| 342 | n/a | def flatten(lst): |
|---|
| 343 | n/a | """flatten list or return scalar""" |
|---|
| 344 | n/a | if atomp(lst): # scalar |
|---|
| 345 | n/a | return lst |
|---|
| 346 | n/a | return _flatten(lst) |
|---|
| 347 | n/a | |
|---|
| 348 | n/a | def slice_shape(lst, slices): |
|---|
| 349 | n/a | """Get the shape of lst after slicing: slices is a list of slice |
|---|
| 350 | n/a | objects.""" |
|---|
| 351 | n/a | if atomp(lst): |
|---|
| 352 | n/a | return [] |
|---|
| 353 | n/a | return [len(lst[slices[0]])] + slice_shape(lst[0], slices[1:]) |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | def multislice(lst, slices): |
|---|
| 356 | n/a | """Multi-dimensional slicing: slices is a list of slice objects.""" |
|---|
| 357 | n/a | if atomp(lst): |
|---|
| 358 | n/a | return lst |
|---|
| 359 | n/a | return [multislice(sublst, slices[1:]) for sublst in lst[slices[0]]] |
|---|
| 360 | n/a | |
|---|
| 361 | n/a | def m_assign(llst, rlst, lslices, rslices): |
|---|
| 362 | n/a | """Multi-dimensional slice assignment: llst and rlst are the operands, |
|---|
| 363 | n/a | lslices and rslices are lists of slice objects. llst and rlst must |
|---|
| 364 | n/a | have the same structure. |
|---|
| 365 | n/a | |
|---|
| 366 | n/a | For a two-dimensional example, this is not implemented in Python: |
|---|
| 367 | n/a | |
|---|
| 368 | n/a | llst[0:3:2, 0:3:2] = rlst[1:3:1, 1:3:1] |
|---|
| 369 | n/a | |
|---|
| 370 | n/a | Instead we write: |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | lslices = [slice(0,3,2), slice(0,3,2)] |
|---|
| 373 | n/a | rslices = [slice(1,3,1), slice(1,3,1)] |
|---|
| 374 | n/a | multislice_assign(llst, rlst, lslices, rslices) |
|---|
| 375 | n/a | """ |
|---|
| 376 | n/a | if atomp(rlst): |
|---|
| 377 | n/a | return rlst |
|---|
| 378 | n/a | rlst = [m_assign(l, r, lslices[1:], rslices[1:]) |
|---|
| 379 | n/a | for l, r in zip(llst[lslices[0]], rlst[rslices[0]])] |
|---|
| 380 | n/a | llst[lslices[0]] = rlst |
|---|
| 381 | n/a | return llst |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | def cmp_structure(llst, rlst, lslices, rslices): |
|---|
| 384 | n/a | """Compare the structure of llst[lslices] and rlst[rslices].""" |
|---|
| 385 | n/a | lshape = slice_shape(llst, lslices) |
|---|
| 386 | n/a | rshape = slice_shape(rlst, rslices) |
|---|
| 387 | n/a | if (len(lshape) != len(rshape)): |
|---|
| 388 | n/a | return -1 |
|---|
| 389 | n/a | for i in range(len(lshape)): |
|---|
| 390 | n/a | if lshape[i] != rshape[i]: |
|---|
| 391 | n/a | return -1 |
|---|
| 392 | n/a | if lshape[i] == 0: |
|---|
| 393 | n/a | return 0 |
|---|
| 394 | n/a | return 0 |
|---|
| 395 | n/a | |
|---|
| 396 | n/a | def multislice_assign(llst, rlst, lslices, rslices): |
|---|
| 397 | n/a | """Return llst after assigning: llst[lslices] = rlst[rslices]""" |
|---|
| 398 | n/a | if cmp_structure(llst, rlst, lslices, rslices) < 0: |
|---|
| 399 | n/a | raise ValueError("lvalue and rvalue have different structures") |
|---|
| 400 | n/a | return m_assign(llst, rlst, lslices, rslices) |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | # ====================================================================== |
|---|
| 404 | n/a | # Random structures |
|---|
| 405 | n/a | # ====================================================================== |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | # |
|---|
| 408 | n/a | # PEP-3118 is very permissive with respect to the contents of a |
|---|
| 409 | n/a | # Py_buffer. In particular: |
|---|
| 410 | n/a | # |
|---|
| 411 | n/a | # - shape can be zero |
|---|
| 412 | n/a | # - strides can be any integer, including zero |
|---|
| 413 | n/a | # - offset can point to any location in the underlying |
|---|
| 414 | n/a | # memory block, provided that it is a multiple of |
|---|
| 415 | n/a | # itemsize. |
|---|
| 416 | n/a | # |
|---|
| 417 | n/a | # The functions in this section test and verify random structures |
|---|
| 418 | n/a | # in full generality. A structure is valid iff it fits in the |
|---|
| 419 | n/a | # underlying memory block. |
|---|
| 420 | n/a | # |
|---|
| 421 | n/a | # The structure 't' (short for 'tuple') is fully defined by: |
|---|
| 422 | n/a | # |
|---|
| 423 | n/a | # t = (memlen, itemsize, ndim, shape, strides, offset) |
|---|
| 424 | n/a | # |
|---|
| 425 | n/a | |
|---|
| 426 | n/a | def verify_structure(memlen, itemsize, ndim, shape, strides, offset): |
|---|
| 427 | n/a | """Verify that the parameters represent a valid array within |
|---|
| 428 | n/a | the bounds of the allocated memory: |
|---|
| 429 | n/a | char *mem: start of the physical memory block |
|---|
| 430 | n/a | memlen: length of the physical memory block |
|---|
| 431 | n/a | offset: (char *)buf - mem |
|---|
| 432 | n/a | """ |
|---|
| 433 | n/a | if offset % itemsize: |
|---|
| 434 | n/a | return False |
|---|
| 435 | n/a | if offset < 0 or offset+itemsize > memlen: |
|---|
| 436 | n/a | return False |
|---|
| 437 | n/a | if any(v % itemsize for v in strides): |
|---|
| 438 | n/a | return False |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | if ndim <= 0: |
|---|
| 441 | n/a | return ndim == 0 and not shape and not strides |
|---|
| 442 | n/a | if 0 in shape: |
|---|
| 443 | n/a | return True |
|---|
| 444 | n/a | |
|---|
| 445 | n/a | imin = sum(strides[j]*(shape[j]-1) for j in range(ndim) |
|---|
| 446 | n/a | if strides[j] <= 0) |
|---|
| 447 | n/a | imax = sum(strides[j]*(shape[j]-1) for j in range(ndim) |
|---|
| 448 | n/a | if strides[j] > 0) |
|---|
| 449 | n/a | |
|---|
| 450 | n/a | return 0 <= offset+imin and offset+imax+itemsize <= memlen |
|---|
| 451 | n/a | |
|---|
| 452 | n/a | def get_item(lst, indices): |
|---|
| 453 | n/a | for i in indices: |
|---|
| 454 | n/a | lst = lst[i] |
|---|
| 455 | n/a | return lst |
|---|
| 456 | n/a | |
|---|
| 457 | n/a | def memory_index(indices, t): |
|---|
| 458 | n/a | """Location of an item in the underlying memory.""" |
|---|
| 459 | n/a | memlen, itemsize, ndim, shape, strides, offset = t |
|---|
| 460 | n/a | p = offset |
|---|
| 461 | n/a | for i in range(ndim): |
|---|
| 462 | n/a | p += strides[i]*indices[i] |
|---|
| 463 | n/a | return p |
|---|
| 464 | n/a | |
|---|
| 465 | n/a | def is_overlapping(t): |
|---|
| 466 | n/a | """The structure 't' is overlapping if at least one memory location |
|---|
| 467 | n/a | is visited twice while iterating through all possible tuples of |
|---|
| 468 | n/a | indices.""" |
|---|
| 469 | n/a | memlen, itemsize, ndim, shape, strides, offset = t |
|---|
| 470 | n/a | visited = 1<<memlen |
|---|
| 471 | n/a | for ind in indices(shape): |
|---|
| 472 | n/a | i = memory_index(ind, t) |
|---|
| 473 | n/a | bit = 1<<i |
|---|
| 474 | n/a | if visited & bit: |
|---|
| 475 | n/a | return True |
|---|
| 476 | n/a | visited |= bit |
|---|
| 477 | n/a | return False |
|---|
| 478 | n/a | |
|---|
| 479 | n/a | def rand_structure(itemsize, valid, maxdim=5, maxshape=16, shape=()): |
|---|
| 480 | n/a | """Return random structure: |
|---|
| 481 | n/a | (memlen, itemsize, ndim, shape, strides, offset) |
|---|
| 482 | n/a | If 'valid' is true, the returned structure is valid, otherwise invalid. |
|---|
| 483 | n/a | If 'shape' is given, use that instead of creating a random shape. |
|---|
| 484 | n/a | """ |
|---|
| 485 | n/a | if not shape: |
|---|
| 486 | n/a | ndim = randrange(maxdim+1) |
|---|
| 487 | n/a | if (ndim == 0): |
|---|
| 488 | n/a | if valid: |
|---|
| 489 | n/a | return itemsize, itemsize, ndim, (), (), 0 |
|---|
| 490 | n/a | else: |
|---|
| 491 | n/a | nitems = randrange(1, 16+1) |
|---|
| 492 | n/a | memlen = nitems * itemsize |
|---|
| 493 | n/a | offset = -itemsize if randrange(2) == 0 else memlen |
|---|
| 494 | n/a | return memlen, itemsize, ndim, (), (), offset |
|---|
| 495 | n/a | |
|---|
| 496 | n/a | minshape = 2 |
|---|
| 497 | n/a | n = randrange(100) |
|---|
| 498 | n/a | if n >= 95 and valid: |
|---|
| 499 | n/a | minshape = 0 |
|---|
| 500 | n/a | elif n >= 90: |
|---|
| 501 | n/a | minshape = 1 |
|---|
| 502 | n/a | shape = [0] * ndim |
|---|
| 503 | n/a | |
|---|
| 504 | n/a | for i in range(ndim): |
|---|
| 505 | n/a | shape[i] = randrange(minshape, maxshape+1) |
|---|
| 506 | n/a | else: |
|---|
| 507 | n/a | ndim = len(shape) |
|---|
| 508 | n/a | |
|---|
| 509 | n/a | maxstride = 5 |
|---|
| 510 | n/a | n = randrange(100) |
|---|
| 511 | n/a | zero_stride = True if n >= 95 and n & 1 else False |
|---|
| 512 | n/a | |
|---|
| 513 | n/a | strides = [0] * ndim |
|---|
| 514 | n/a | strides[ndim-1] = itemsize * randrange(-maxstride, maxstride+1) |
|---|
| 515 | n/a | if not zero_stride and strides[ndim-1] == 0: |
|---|
| 516 | n/a | strides[ndim-1] = itemsize |
|---|
| 517 | n/a | |
|---|
| 518 | n/a | for i in range(ndim-2, -1, -1): |
|---|
| 519 | n/a | maxstride *= shape[i+1] if shape[i+1] else 1 |
|---|
| 520 | n/a | if zero_stride: |
|---|
| 521 | n/a | strides[i] = itemsize * randrange(-maxstride, maxstride+1) |
|---|
| 522 | n/a | else: |
|---|
| 523 | n/a | strides[i] = ((1,-1)[randrange(2)] * |
|---|
| 524 | n/a | itemsize * randrange(1, maxstride+1)) |
|---|
| 525 | n/a | |
|---|
| 526 | n/a | imin = imax = 0 |
|---|
| 527 | n/a | if not 0 in shape: |
|---|
| 528 | n/a | imin = sum(strides[j]*(shape[j]-1) for j in range(ndim) |
|---|
| 529 | n/a | if strides[j] <= 0) |
|---|
| 530 | n/a | imax = sum(strides[j]*(shape[j]-1) for j in range(ndim) |
|---|
| 531 | n/a | if strides[j] > 0) |
|---|
| 532 | n/a | |
|---|
| 533 | n/a | nitems = imax - imin |
|---|
| 534 | n/a | if valid: |
|---|
| 535 | n/a | offset = -imin * itemsize |
|---|
| 536 | n/a | memlen = offset + (imax+1) * itemsize |
|---|
| 537 | n/a | else: |
|---|
| 538 | n/a | memlen = (-imin + imax) * itemsize |
|---|
| 539 | n/a | offset = -imin-itemsize if randrange(2) == 0 else memlen |
|---|
| 540 | n/a | return memlen, itemsize, ndim, shape, strides, offset |
|---|
| 541 | n/a | |
|---|
| 542 | n/a | def randslice_from_slicelen(slicelen, listlen): |
|---|
| 543 | n/a | """Create a random slice of len slicelen that fits into listlen.""" |
|---|
| 544 | n/a | maxstart = listlen - slicelen |
|---|
| 545 | n/a | start = randrange(maxstart+1) |
|---|
| 546 | n/a | maxstep = (listlen - start) // slicelen if slicelen else 1 |
|---|
| 547 | n/a | step = randrange(1, maxstep+1) |
|---|
| 548 | n/a | stop = start + slicelen * step |
|---|
| 549 | n/a | s = slice(start, stop, step) |
|---|
| 550 | n/a | _, _, _, control = slice_indices(s, listlen) |
|---|
| 551 | n/a | if control != slicelen: |
|---|
| 552 | n/a | raise RuntimeError |
|---|
| 553 | n/a | return s |
|---|
| 554 | n/a | |
|---|
| 555 | n/a | def randslice_from_shape(ndim, shape): |
|---|
| 556 | n/a | """Create two sets of slices for an array x with shape 'shape' |
|---|
| 557 | n/a | such that shapeof(x[lslices]) == shapeof(x[rslices]).""" |
|---|
| 558 | n/a | lslices = [0] * ndim |
|---|
| 559 | n/a | rslices = [0] * ndim |
|---|
| 560 | n/a | for n in range(ndim): |
|---|
| 561 | n/a | l = shape[n] |
|---|
| 562 | n/a | slicelen = randrange(1, l+1) if l > 0 else 0 |
|---|
| 563 | n/a | lslices[n] = randslice_from_slicelen(slicelen, l) |
|---|
| 564 | n/a | rslices[n] = randslice_from_slicelen(slicelen, l) |
|---|
| 565 | n/a | return tuple(lslices), tuple(rslices) |
|---|
| 566 | n/a | |
|---|
| 567 | n/a | def rand_aligned_slices(maxdim=5, maxshape=16): |
|---|
| 568 | n/a | """Create (lshape, rshape, tuple(lslices), tuple(rslices)) such that |
|---|
| 569 | n/a | shapeof(x[lslices]) == shapeof(y[rslices]), where x is an array |
|---|
| 570 | n/a | with shape 'lshape' and y is an array with shape 'rshape'.""" |
|---|
| 571 | n/a | ndim = randrange(1, maxdim+1) |
|---|
| 572 | n/a | minshape = 2 |
|---|
| 573 | n/a | n = randrange(100) |
|---|
| 574 | n/a | if n >= 95: |
|---|
| 575 | n/a | minshape = 0 |
|---|
| 576 | n/a | elif n >= 90: |
|---|
| 577 | n/a | minshape = 1 |
|---|
| 578 | n/a | all_random = True if randrange(100) >= 80 else False |
|---|
| 579 | n/a | lshape = [0]*ndim; rshape = [0]*ndim |
|---|
| 580 | n/a | lslices = [0]*ndim; rslices = [0]*ndim |
|---|
| 581 | n/a | |
|---|
| 582 | n/a | for n in range(ndim): |
|---|
| 583 | n/a | small = randrange(minshape, maxshape+1) |
|---|
| 584 | n/a | big = randrange(minshape, maxshape+1) |
|---|
| 585 | n/a | if big < small: |
|---|
| 586 | n/a | big, small = small, big |
|---|
| 587 | n/a | |
|---|
| 588 | n/a | # Create a slice that fits the smaller value. |
|---|
| 589 | n/a | if all_random: |
|---|
| 590 | n/a | start = randrange(-small, small+1) |
|---|
| 591 | n/a | stop = randrange(-small, small+1) |
|---|
| 592 | n/a | step = (1,-1)[randrange(2)] * randrange(1, small+2) |
|---|
| 593 | n/a | s_small = slice(start, stop, step) |
|---|
| 594 | n/a | _, _, _, slicelen = slice_indices(s_small, small) |
|---|
| 595 | n/a | else: |
|---|
| 596 | n/a | slicelen = randrange(1, small+1) if small > 0 else 0 |
|---|
| 597 | n/a | s_small = randslice_from_slicelen(slicelen, small) |
|---|
| 598 | n/a | |
|---|
| 599 | n/a | # Create a slice of the same length for the bigger value. |
|---|
| 600 | n/a | s_big = randslice_from_slicelen(slicelen, big) |
|---|
| 601 | n/a | if randrange(2) == 0: |
|---|
| 602 | n/a | rshape[n], lshape[n] = big, small |
|---|
| 603 | n/a | rslices[n], lslices[n] = s_big, s_small |
|---|
| 604 | n/a | else: |
|---|
| 605 | n/a | rshape[n], lshape[n] = small, big |
|---|
| 606 | n/a | rslices[n], lslices[n] = s_small, s_big |
|---|
| 607 | n/a | |
|---|
| 608 | n/a | return lshape, rshape, tuple(lslices), tuple(rslices) |
|---|
| 609 | n/a | |
|---|
| 610 | n/a | def randitems_from_structure(fmt, t): |
|---|
| 611 | n/a | """Return a list of random items for structure 't' with format |
|---|
| 612 | n/a | 'fmtchar'.""" |
|---|
| 613 | n/a | memlen, itemsize, _, _, _, _ = t |
|---|
| 614 | n/a | return gen_items(memlen//itemsize, '#'+fmt, 'numpy') |
|---|
| 615 | n/a | |
|---|
| 616 | n/a | def ndarray_from_structure(items, fmt, t, flags=0): |
|---|
| 617 | n/a | """Return ndarray from the tuple returned by rand_structure()""" |
|---|
| 618 | n/a | memlen, itemsize, ndim, shape, strides, offset = t |
|---|
| 619 | n/a | return ndarray(items, shape=shape, strides=strides, format=fmt, |
|---|
| 620 | n/a | offset=offset, flags=ND_WRITABLE|flags) |
|---|
| 621 | n/a | |
|---|
| 622 | n/a | def numpy_array_from_structure(items, fmt, t): |
|---|
| 623 | n/a | """Return numpy_array from the tuple returned by rand_structure()""" |
|---|
| 624 | n/a | memlen, itemsize, ndim, shape, strides, offset = t |
|---|
| 625 | n/a | buf = bytearray(memlen) |
|---|
| 626 | n/a | for j, v in enumerate(items): |
|---|
| 627 | n/a | struct.pack_into(fmt, buf, j*itemsize, v) |
|---|
| 628 | n/a | return numpy_array(buffer=buf, shape=shape, strides=strides, |
|---|
| 629 | n/a | dtype=fmt, offset=offset) |
|---|
| 630 | n/a | |
|---|
| 631 | n/a | |
|---|
| 632 | n/a | # ====================================================================== |
|---|
| 633 | n/a | # memoryview casts |
|---|
| 634 | n/a | # ====================================================================== |
|---|
| 635 | n/a | |
|---|
| 636 | n/a | def cast_items(exporter, fmt, itemsize, shape=None): |
|---|
| 637 | n/a | """Interpret the raw memory of 'exporter' as a list of items with |
|---|
| 638 | n/a | size 'itemsize'. If shape=None, the new structure is assumed to |
|---|
| 639 | n/a | be 1-D with n * itemsize = bytelen. If shape is given, the usual |
|---|
| 640 | n/a | constraint for contiguous arrays prod(shape) * itemsize = bytelen |
|---|
| 641 | n/a | applies. On success, return (items, shape). If the constraints |
|---|
| 642 | n/a | cannot be met, return (None, None). If a chunk of bytes is interpreted |
|---|
| 643 | n/a | as NaN as a result of float conversion, return ('nan', None).""" |
|---|
| 644 | n/a | bytelen = exporter.nbytes |
|---|
| 645 | n/a | if shape: |
|---|
| 646 | n/a | if prod(shape) * itemsize != bytelen: |
|---|
| 647 | n/a | return None, shape |
|---|
| 648 | n/a | elif shape == []: |
|---|
| 649 | n/a | if exporter.ndim == 0 or itemsize != bytelen: |
|---|
| 650 | n/a | return None, shape |
|---|
| 651 | n/a | else: |
|---|
| 652 | n/a | n, r = divmod(bytelen, itemsize) |
|---|
| 653 | n/a | shape = [n] |
|---|
| 654 | n/a | if r != 0: |
|---|
| 655 | n/a | return None, shape |
|---|
| 656 | n/a | |
|---|
| 657 | n/a | mem = exporter.tobytes() |
|---|
| 658 | n/a | byteitems = [mem[i:i+itemsize] for i in range(0, len(mem), itemsize)] |
|---|
| 659 | n/a | |
|---|
| 660 | n/a | items = [] |
|---|
| 661 | n/a | for v in byteitems: |
|---|
| 662 | n/a | item = struct.unpack(fmt, v)[0] |
|---|
| 663 | n/a | if item != item: |
|---|
| 664 | n/a | return 'nan', shape |
|---|
| 665 | n/a | items.append(item) |
|---|
| 666 | n/a | |
|---|
| 667 | n/a | return (items, shape) if shape != [] else (items[0], shape) |
|---|
| 668 | n/a | |
|---|
| 669 | n/a | def gencastshapes(): |
|---|
| 670 | n/a | """Generate shapes to test casting.""" |
|---|
| 671 | n/a | for n in range(32): |
|---|
| 672 | n/a | yield [n] |
|---|
| 673 | n/a | ndim = randrange(4, 6) |
|---|
| 674 | n/a | minshape = 1 if randrange(100) > 80 else 2 |
|---|
| 675 | n/a | yield [randrange(minshape, 5) for _ in range(ndim)] |
|---|
| 676 | n/a | ndim = randrange(2, 4) |
|---|
| 677 | n/a | minshape = 1 if randrange(100) > 80 else 2 |
|---|
| 678 | n/a | yield [randrange(minshape, 5) for _ in range(ndim)] |
|---|
| 679 | n/a | |
|---|
| 680 | n/a | |
|---|
| 681 | n/a | # ====================================================================== |
|---|
| 682 | n/a | # Actual tests |
|---|
| 683 | n/a | # ====================================================================== |
|---|
| 684 | n/a | |
|---|
| 685 | n/a | def genslices(n): |
|---|
| 686 | n/a | """Generate all possible slices for a single dimension.""" |
|---|
| 687 | n/a | return product(range(-n, n+1), range(-n, n+1), range(-n, n+1)) |
|---|
| 688 | n/a | |
|---|
| 689 | n/a | def genslices_ndim(ndim, shape): |
|---|
| 690 | n/a | """Generate all possible slice tuples for 'shape'.""" |
|---|
| 691 | n/a | iterables = [genslices(shape[n]) for n in range(ndim)] |
|---|
| 692 | n/a | return product(*iterables) |
|---|
| 693 | n/a | |
|---|
| 694 | n/a | def rslice(n, allow_empty=False): |
|---|
| 695 | n/a | """Generate random slice for a single dimension of length n. |
|---|
| 696 | n/a | If zero=True, the slices may be empty, otherwise they will |
|---|
| 697 | n/a | be non-empty.""" |
|---|
| 698 | n/a | minlen = 0 if allow_empty or n == 0 else 1 |
|---|
| 699 | n/a | slicelen = randrange(minlen, n+1) |
|---|
| 700 | n/a | return randslice_from_slicelen(slicelen, n) |
|---|
| 701 | n/a | |
|---|
| 702 | n/a | def rslices(n, allow_empty=False): |
|---|
| 703 | n/a | """Generate random slices for a single dimension.""" |
|---|
| 704 | n/a | for _ in range(5): |
|---|
| 705 | n/a | yield rslice(n, allow_empty) |
|---|
| 706 | n/a | |
|---|
| 707 | n/a | def rslices_ndim(ndim, shape, iterations=5): |
|---|
| 708 | n/a | """Generate random slice tuples for 'shape'.""" |
|---|
| 709 | n/a | # non-empty slices |
|---|
| 710 | n/a | for _ in range(iterations): |
|---|
| 711 | n/a | yield tuple(rslice(shape[n]) for n in range(ndim)) |
|---|
| 712 | n/a | # possibly empty slices |
|---|
| 713 | n/a | for _ in range(iterations): |
|---|
| 714 | n/a | yield tuple(rslice(shape[n], allow_empty=True) for n in range(ndim)) |
|---|
| 715 | n/a | # invalid slices |
|---|
| 716 | n/a | yield tuple(slice(0,1,0) for _ in range(ndim)) |
|---|
| 717 | n/a | |
|---|
| 718 | n/a | def rpermutation(iterable, r=None): |
|---|
| 719 | n/a | pool = tuple(iterable) |
|---|
| 720 | n/a | r = len(pool) if r is None else r |
|---|
| 721 | n/a | yield tuple(sample(pool, r)) |
|---|
| 722 | n/a | |
|---|
| 723 | n/a | def ndarray_print(nd): |
|---|
| 724 | n/a | """Print ndarray for debugging.""" |
|---|
| 725 | n/a | try: |
|---|
| 726 | n/a | x = nd.tolist() |
|---|
| 727 | n/a | except (TypeError, NotImplementedError): |
|---|
| 728 | n/a | x = nd.tobytes() |
|---|
| 729 | n/a | if isinstance(nd, ndarray): |
|---|
| 730 | n/a | offset = nd.offset |
|---|
| 731 | n/a | flags = nd.flags |
|---|
| 732 | n/a | else: |
|---|
| 733 | n/a | offset = 'unknown' |
|---|
| 734 | n/a | flags = 'unknown' |
|---|
| 735 | n/a | print("ndarray(%s, shape=%s, strides=%s, suboffsets=%s, offset=%s, " |
|---|
| 736 | n/a | "format='%s', itemsize=%s, flags=%s)" % |
|---|
| 737 | n/a | (x, nd.shape, nd.strides, nd.suboffsets, offset, |
|---|
| 738 | n/a | nd.format, nd.itemsize, flags)) |
|---|
| 739 | n/a | sys.stdout.flush() |
|---|
| 740 | n/a | |
|---|
| 741 | n/a | |
|---|
| 742 | n/a | ITERATIONS = 100 |
|---|
| 743 | n/a | MAXDIM = 5 |
|---|
| 744 | n/a | MAXSHAPE = 10 |
|---|
| 745 | n/a | |
|---|
| 746 | n/a | if SHORT_TEST: |
|---|
| 747 | n/a | ITERATIONS = 10 |
|---|
| 748 | n/a | MAXDIM = 3 |
|---|
| 749 | n/a | MAXSHAPE = 4 |
|---|
| 750 | n/a | genslices = rslices |
|---|
| 751 | n/a | genslices_ndim = rslices_ndim |
|---|
| 752 | n/a | permutations = rpermutation |
|---|
| 753 | n/a | |
|---|
| 754 | n/a | |
|---|
| 755 | n/a | @unittest.skipUnless(struct, 'struct module required for this test.') |
|---|
| 756 | n/a | @unittest.skipUnless(ndarray, 'ndarray object required for this test') |
|---|
| 757 | n/a | class TestBufferProtocol(unittest.TestCase): |
|---|
| 758 | n/a | |
|---|
| 759 | n/a | def setUp(self): |
|---|
| 760 | n/a | # The suboffsets tests need sizeof(void *). |
|---|
| 761 | n/a | self.sizeof_void_p = get_sizeof_void_p() |
|---|
| 762 | n/a | |
|---|
| 763 | n/a | def verify(self, result, obj=-1, |
|---|
| 764 | n/a | itemsize={1}, fmt=-1, readonly={1}, |
|---|
| 765 | n/a | ndim={1}, shape=-1, strides=-1, |
|---|
| 766 | n/a | lst=-1, sliced=False, cast=False): |
|---|
| 767 | n/a | # Verify buffer contents against expected values. Default values |
|---|
| 768 | n/a | # are deliberately initialized to invalid types. |
|---|
| 769 | n/a | if shape: |
|---|
| 770 | n/a | expected_len = prod(shape)*itemsize |
|---|
| 771 | n/a | else: |
|---|
| 772 | n/a | if not fmt: # array has been implicitly cast to unsigned bytes |
|---|
| 773 | n/a | expected_len = len(lst) |
|---|
| 774 | n/a | else: # ndim = 0 |
|---|
| 775 | n/a | expected_len = itemsize |
|---|
| 776 | n/a | |
|---|
| 777 | n/a | # Reconstruct suboffsets from strides. Support for slicing |
|---|
| 778 | n/a | # could be added, but is currently only needed for test_getbuf(). |
|---|
| 779 | n/a | suboffsets = () |
|---|
| 780 | n/a | if result.suboffsets: |
|---|
| 781 | n/a | self.assertGreater(ndim, 0) |
|---|
| 782 | n/a | |
|---|
| 783 | n/a | suboffset0 = 0 |
|---|
| 784 | n/a | for n in range(1, ndim): |
|---|
| 785 | n/a | if shape[n] == 0: |
|---|
| 786 | n/a | break |
|---|
| 787 | n/a | if strides[n] <= 0: |
|---|
| 788 | n/a | suboffset0 += -strides[n] * (shape[n]-1) |
|---|
| 789 | n/a | |
|---|
| 790 | n/a | suboffsets = [suboffset0] + [-1 for v in range(ndim-1)] |
|---|
| 791 | n/a | |
|---|
| 792 | n/a | # Not correct if slicing has occurred in the first dimension. |
|---|
| 793 | n/a | stride0 = self.sizeof_void_p |
|---|
| 794 | n/a | if strides[0] < 0: |
|---|
| 795 | n/a | stride0 = -stride0 |
|---|
| 796 | n/a | strides = [stride0] + list(strides[1:]) |
|---|
| 797 | n/a | |
|---|
| 798 | n/a | self.assertIs(result.obj, obj) |
|---|
| 799 | n/a | self.assertEqual(result.nbytes, expected_len) |
|---|
| 800 | n/a | self.assertEqual(result.itemsize, itemsize) |
|---|
| 801 | n/a | self.assertEqual(result.format, fmt) |
|---|
| 802 | n/a | self.assertEqual(result.readonly, readonly) |
|---|
| 803 | n/a | self.assertEqual(result.ndim, ndim) |
|---|
| 804 | n/a | self.assertEqual(result.shape, tuple(shape)) |
|---|
| 805 | n/a | if not (sliced and suboffsets): |
|---|
| 806 | n/a | self.assertEqual(result.strides, tuple(strides)) |
|---|
| 807 | n/a | self.assertEqual(result.suboffsets, tuple(suboffsets)) |
|---|
| 808 | n/a | |
|---|
| 809 | n/a | if isinstance(result, ndarray) or is_memoryview_format(fmt): |
|---|
| 810 | n/a | rep = result.tolist() if fmt else result.tobytes() |
|---|
| 811 | n/a | self.assertEqual(rep, lst) |
|---|
| 812 | n/a | |
|---|
| 813 | n/a | if not fmt: # array has been cast to unsigned bytes, |
|---|
| 814 | n/a | return # the remaining tests won't work. |
|---|
| 815 | n/a | |
|---|
| 816 | n/a | # PyBuffer_GetPointer() is the definition how to access an item. |
|---|
| 817 | n/a | # If PyBuffer_GetPointer(indices) is correct for all possible |
|---|
| 818 | n/a | # combinations of indices, the buffer is correct. |
|---|
| 819 | n/a | # |
|---|
| 820 | n/a | # Also test tobytes() against the flattened 'lst', with all items |
|---|
| 821 | n/a | # packed to bytes. |
|---|
| 822 | n/a | if not cast: # casts chop up 'lst' in different ways |
|---|
| 823 | n/a | b = bytearray() |
|---|
| 824 | n/a | buf_err = None |
|---|
| 825 | n/a | for ind in indices(shape): |
|---|
| 826 | n/a | try: |
|---|
| 827 | n/a | item1 = get_pointer(result, ind) |
|---|
| 828 | n/a | item2 = get_item(lst, ind) |
|---|
| 829 | n/a | if isinstance(item2, tuple): |
|---|
| 830 | n/a | x = struct.pack(fmt, *item2) |
|---|
| 831 | n/a | else: |
|---|
| 832 | n/a | x = struct.pack(fmt, item2) |
|---|
| 833 | n/a | b.extend(x) |
|---|
| 834 | n/a | except BufferError: |
|---|
| 835 | n/a | buf_err = True # re-exporter does not provide full buffer |
|---|
| 836 | n/a | break |
|---|
| 837 | n/a | self.assertEqual(item1, item2) |
|---|
| 838 | n/a | |
|---|
| 839 | n/a | if not buf_err: |
|---|
| 840 | n/a | # test tobytes() |
|---|
| 841 | n/a | self.assertEqual(result.tobytes(), b) |
|---|
| 842 | n/a | |
|---|
| 843 | n/a | # test hex() |
|---|
| 844 | n/a | m = memoryview(result) |
|---|
| 845 | n/a | h = "".join("%02x" % c for c in b) |
|---|
| 846 | n/a | self.assertEqual(m.hex(), h) |
|---|
| 847 | n/a | |
|---|
| 848 | n/a | # lst := expected multi-dimensional logical representation |
|---|
| 849 | n/a | # flatten(lst) := elements in C-order |
|---|
| 850 | n/a | ff = fmt if fmt else 'B' |
|---|
| 851 | n/a | flattened = flatten(lst) |
|---|
| 852 | n/a | |
|---|
| 853 | n/a | # Rules for 'A': if the array is already contiguous, return |
|---|
| 854 | n/a | # the array unaltered. Otherwise, return a contiguous 'C' |
|---|
| 855 | n/a | # representation. |
|---|
| 856 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 857 | n/a | expected = result |
|---|
| 858 | n/a | if order == 'F': |
|---|
| 859 | n/a | if not is_contiguous(result, 'A') or \ |
|---|
| 860 | n/a | is_contiguous(result, 'C'): |
|---|
| 861 | n/a | # For constructing the ndarray, convert the |
|---|
| 862 | n/a | # flattened logical representation to Fortran order. |
|---|
| 863 | n/a | trans = transpose(flattened, shape) |
|---|
| 864 | n/a | expected = ndarray(trans, shape=shape, format=ff, |
|---|
| 865 | n/a | flags=ND_FORTRAN) |
|---|
| 866 | n/a | else: # 'C', 'A' |
|---|
| 867 | n/a | if not is_contiguous(result, 'A') or \ |
|---|
| 868 | n/a | is_contiguous(result, 'F') and order == 'C': |
|---|
| 869 | n/a | # The flattened list is already in C-order. |
|---|
| 870 | n/a | expected = ndarray(flattened, shape=shape, format=ff) |
|---|
| 871 | n/a | |
|---|
| 872 | n/a | contig = get_contiguous(result, PyBUF_READ, order) |
|---|
| 873 | n/a | self.assertEqual(contig.tobytes(), b) |
|---|
| 874 | n/a | self.assertTrue(cmp_contig(contig, expected)) |
|---|
| 875 | n/a | |
|---|
| 876 | n/a | if ndim == 0: |
|---|
| 877 | n/a | continue |
|---|
| 878 | n/a | |
|---|
| 879 | n/a | nmemb = len(flattened) |
|---|
| 880 | n/a | ro = 0 if readonly else ND_WRITABLE |
|---|
| 881 | n/a | |
|---|
| 882 | n/a | ### See comment in test_py_buffer_to_contiguous for an |
|---|
| 883 | n/a | ### explanation why these tests are valid. |
|---|
| 884 | n/a | |
|---|
| 885 | n/a | # To 'C' |
|---|
| 886 | n/a | contig = py_buffer_to_contiguous(result, 'C', PyBUF_FULL_RO) |
|---|
| 887 | n/a | self.assertEqual(len(contig), nmemb * itemsize) |
|---|
| 888 | n/a | initlst = [struct.unpack_from(fmt, contig, n*itemsize) |
|---|
| 889 | n/a | for n in range(nmemb)] |
|---|
| 890 | n/a | if len(initlst[0]) == 1: |
|---|
| 891 | n/a | initlst = [v[0] for v in initlst] |
|---|
| 892 | n/a | |
|---|
| 893 | n/a | y = ndarray(initlst, shape=shape, flags=ro, format=fmt) |
|---|
| 894 | n/a | self.assertEqual(memoryview(y), memoryview(result)) |
|---|
| 895 | n/a | |
|---|
| 896 | n/a | # To 'F' |
|---|
| 897 | n/a | contig = py_buffer_to_contiguous(result, 'F', PyBUF_FULL_RO) |
|---|
| 898 | n/a | self.assertEqual(len(contig), nmemb * itemsize) |
|---|
| 899 | n/a | initlst = [struct.unpack_from(fmt, contig, n*itemsize) |
|---|
| 900 | n/a | for n in range(nmemb)] |
|---|
| 901 | n/a | if len(initlst[0]) == 1: |
|---|
| 902 | n/a | initlst = [v[0] for v in initlst] |
|---|
| 903 | n/a | |
|---|
| 904 | n/a | y = ndarray(initlst, shape=shape, flags=ro|ND_FORTRAN, |
|---|
| 905 | n/a | format=fmt) |
|---|
| 906 | n/a | self.assertEqual(memoryview(y), memoryview(result)) |
|---|
| 907 | n/a | |
|---|
| 908 | n/a | # To 'A' |
|---|
| 909 | n/a | contig = py_buffer_to_contiguous(result, 'A', PyBUF_FULL_RO) |
|---|
| 910 | n/a | self.assertEqual(len(contig), nmemb * itemsize) |
|---|
| 911 | n/a | initlst = [struct.unpack_from(fmt, contig, n*itemsize) |
|---|
| 912 | n/a | for n in range(nmemb)] |
|---|
| 913 | n/a | if len(initlst[0]) == 1: |
|---|
| 914 | n/a | initlst = [v[0] for v in initlst] |
|---|
| 915 | n/a | |
|---|
| 916 | n/a | f = ND_FORTRAN if is_contiguous(result, 'F') else 0 |
|---|
| 917 | n/a | y = ndarray(initlst, shape=shape, flags=f|ro, format=fmt) |
|---|
| 918 | n/a | self.assertEqual(memoryview(y), memoryview(result)) |
|---|
| 919 | n/a | |
|---|
| 920 | n/a | if is_memoryview_format(fmt): |
|---|
| 921 | n/a | try: |
|---|
| 922 | n/a | m = memoryview(result) |
|---|
| 923 | n/a | except BufferError: # re-exporter does not provide full information |
|---|
| 924 | n/a | return |
|---|
| 925 | n/a | ex = result.obj if isinstance(result, memoryview) else result |
|---|
| 926 | n/a | self.assertIs(m.obj, ex) |
|---|
| 927 | n/a | self.assertEqual(m.nbytes, expected_len) |
|---|
| 928 | n/a | self.assertEqual(m.itemsize, itemsize) |
|---|
| 929 | n/a | self.assertEqual(m.format, fmt) |
|---|
| 930 | n/a | self.assertEqual(m.readonly, readonly) |
|---|
| 931 | n/a | self.assertEqual(m.ndim, ndim) |
|---|
| 932 | n/a | self.assertEqual(m.shape, tuple(shape)) |
|---|
| 933 | n/a | if not (sliced and suboffsets): |
|---|
| 934 | n/a | self.assertEqual(m.strides, tuple(strides)) |
|---|
| 935 | n/a | self.assertEqual(m.suboffsets, tuple(suboffsets)) |
|---|
| 936 | n/a | |
|---|
| 937 | n/a | n = 1 if ndim == 0 else len(lst) |
|---|
| 938 | n/a | self.assertEqual(len(m), n) |
|---|
| 939 | n/a | |
|---|
| 940 | n/a | rep = result.tolist() if fmt else result.tobytes() |
|---|
| 941 | n/a | self.assertEqual(rep, lst) |
|---|
| 942 | n/a | self.assertEqual(m, result) |
|---|
| 943 | n/a | |
|---|
| 944 | n/a | def verify_getbuf(self, orig_ex, ex, req, sliced=False): |
|---|
| 945 | n/a | def simple_fmt(ex): |
|---|
| 946 | n/a | return ex.format == '' or ex.format == 'B' |
|---|
| 947 | n/a | def match(req, flag): |
|---|
| 948 | n/a | return ((req&flag) == flag) |
|---|
| 949 | n/a | |
|---|
| 950 | n/a | if (# writable request to read-only exporter |
|---|
| 951 | n/a | (ex.readonly and match(req, PyBUF_WRITABLE)) or |
|---|
| 952 | n/a | # cannot match explicit contiguity request |
|---|
| 953 | n/a | (match(req, PyBUF_C_CONTIGUOUS) and not ex.c_contiguous) or |
|---|
| 954 | n/a | (match(req, PyBUF_F_CONTIGUOUS) and not ex.f_contiguous) or |
|---|
| 955 | n/a | (match(req, PyBUF_ANY_CONTIGUOUS) and not ex.contiguous) or |
|---|
| 956 | n/a | # buffer needs suboffsets |
|---|
| 957 | n/a | (not match(req, PyBUF_INDIRECT) and ex.suboffsets) or |
|---|
| 958 | n/a | # buffer without strides must be C-contiguous |
|---|
| 959 | n/a | (not match(req, PyBUF_STRIDES) and not ex.c_contiguous) or |
|---|
| 960 | n/a | # PyBUF_SIMPLE|PyBUF_FORMAT and PyBUF_WRITABLE|PyBUF_FORMAT |
|---|
| 961 | n/a | (not match(req, PyBUF_ND) and match(req, PyBUF_FORMAT))): |
|---|
| 962 | n/a | |
|---|
| 963 | n/a | self.assertRaises(BufferError, ndarray, ex, getbuf=req) |
|---|
| 964 | n/a | return |
|---|
| 965 | n/a | |
|---|
| 966 | n/a | if isinstance(ex, ndarray) or is_memoryview_format(ex.format): |
|---|
| 967 | n/a | lst = ex.tolist() |
|---|
| 968 | n/a | else: |
|---|
| 969 | n/a | nd = ndarray(ex, getbuf=PyBUF_FULL_RO) |
|---|
| 970 | n/a | lst = nd.tolist() |
|---|
| 971 | n/a | |
|---|
| 972 | n/a | # The consumer may have requested default values or a NULL format. |
|---|
| 973 | n/a | ro = 0 if match(req, PyBUF_WRITABLE) else ex.readonly |
|---|
| 974 | n/a | fmt = ex.format |
|---|
| 975 | n/a | itemsize = ex.itemsize |
|---|
| 976 | n/a | ndim = ex.ndim |
|---|
| 977 | n/a | if not match(req, PyBUF_FORMAT): |
|---|
| 978 | n/a | # itemsize refers to the original itemsize before the cast. |
|---|
| 979 | n/a | # The equality product(shape) * itemsize = len still holds. |
|---|
| 980 | n/a | # The equality calcsize(format) = itemsize does _not_ hold. |
|---|
| 981 | n/a | fmt = '' |
|---|
| 982 | n/a | lst = orig_ex.tobytes() # Issue 12834 |
|---|
| 983 | n/a | if not match(req, PyBUF_ND): |
|---|
| 984 | n/a | ndim = 1 |
|---|
| 985 | n/a | shape = orig_ex.shape if match(req, PyBUF_ND) else () |
|---|
| 986 | n/a | strides = orig_ex.strides if match(req, PyBUF_STRIDES) else () |
|---|
| 987 | n/a | |
|---|
| 988 | n/a | nd = ndarray(ex, getbuf=req) |
|---|
| 989 | n/a | self.verify(nd, obj=ex, |
|---|
| 990 | n/a | itemsize=itemsize, fmt=fmt, readonly=ro, |
|---|
| 991 | n/a | ndim=ndim, shape=shape, strides=strides, |
|---|
| 992 | n/a | lst=lst, sliced=sliced) |
|---|
| 993 | n/a | |
|---|
| 994 | n/a | def test_ndarray_getbuf(self): |
|---|
| 995 | n/a | requests = ( |
|---|
| 996 | n/a | # distinct flags |
|---|
| 997 | n/a | PyBUF_INDIRECT, PyBUF_STRIDES, PyBUF_ND, PyBUF_SIMPLE, |
|---|
| 998 | n/a | PyBUF_C_CONTIGUOUS, PyBUF_F_CONTIGUOUS, PyBUF_ANY_CONTIGUOUS, |
|---|
| 999 | n/a | # compound requests |
|---|
| 1000 | n/a | PyBUF_FULL, PyBUF_FULL_RO, |
|---|
| 1001 | n/a | PyBUF_RECORDS, PyBUF_RECORDS_RO, |
|---|
| 1002 | n/a | PyBUF_STRIDED, PyBUF_STRIDED_RO, |
|---|
| 1003 | n/a | PyBUF_CONTIG, PyBUF_CONTIG_RO, |
|---|
| 1004 | n/a | ) |
|---|
| 1005 | n/a | # items and format |
|---|
| 1006 | n/a | items_fmt = ( |
|---|
| 1007 | n/a | ([True if x % 2 else False for x in range(12)], '?'), |
|---|
| 1008 | n/a | ([1,2,3,4,5,6,7,8,9,10,11,12], 'b'), |
|---|
| 1009 | n/a | ([1,2,3,4,5,6,7,8,9,10,11,12], 'B'), |
|---|
| 1010 | n/a | ([(2**31-x) if x % 2 else (-2**31+x) for x in range(12)], 'l') |
|---|
| 1011 | n/a | ) |
|---|
| 1012 | n/a | # shape, strides, offset |
|---|
| 1013 | n/a | structure = ( |
|---|
| 1014 | n/a | ([], [], 0), |
|---|
| 1015 | n/a | ([1,3,1], [], 0), |
|---|
| 1016 | n/a | ([12], [], 0), |
|---|
| 1017 | n/a | ([12], [-1], 11), |
|---|
| 1018 | n/a | ([6], [2], 0), |
|---|
| 1019 | n/a | ([6], [-2], 11), |
|---|
| 1020 | n/a | ([3, 4], [], 0), |
|---|
| 1021 | n/a | ([3, 4], [-4, -1], 11), |
|---|
| 1022 | n/a | ([2, 2], [4, 1], 4), |
|---|
| 1023 | n/a | ([2, 2], [-4, -1], 8) |
|---|
| 1024 | n/a | ) |
|---|
| 1025 | n/a | # ndarray creation flags |
|---|
| 1026 | n/a | ndflags = ( |
|---|
| 1027 | n/a | 0, ND_WRITABLE, ND_FORTRAN, ND_FORTRAN|ND_WRITABLE, |
|---|
| 1028 | n/a | ND_PIL, ND_PIL|ND_WRITABLE |
|---|
| 1029 | n/a | ) |
|---|
| 1030 | n/a | # flags that can actually be used as flags |
|---|
| 1031 | n/a | real_flags = (0, PyBUF_WRITABLE, PyBUF_FORMAT, |
|---|
| 1032 | n/a | PyBUF_WRITABLE|PyBUF_FORMAT) |
|---|
| 1033 | n/a | |
|---|
| 1034 | n/a | for items, fmt in items_fmt: |
|---|
| 1035 | n/a | itemsize = struct.calcsize(fmt) |
|---|
| 1036 | n/a | for shape, strides, offset in structure: |
|---|
| 1037 | n/a | strides = [v * itemsize for v in strides] |
|---|
| 1038 | n/a | offset *= itemsize |
|---|
| 1039 | n/a | for flags in ndflags: |
|---|
| 1040 | n/a | |
|---|
| 1041 | n/a | if strides and (flags&ND_FORTRAN): |
|---|
| 1042 | n/a | continue |
|---|
| 1043 | n/a | if not shape and (flags&ND_PIL): |
|---|
| 1044 | n/a | continue |
|---|
| 1045 | n/a | |
|---|
| 1046 | n/a | _items = items if shape else items[0] |
|---|
| 1047 | n/a | ex1 = ndarray(_items, format=fmt, flags=flags, |
|---|
| 1048 | n/a | shape=shape, strides=strides, offset=offset) |
|---|
| 1049 | n/a | ex2 = ex1[::-2] if shape else None |
|---|
| 1050 | n/a | |
|---|
| 1051 | n/a | m1 = memoryview(ex1) |
|---|
| 1052 | n/a | if ex2: |
|---|
| 1053 | n/a | m2 = memoryview(ex2) |
|---|
| 1054 | n/a | if ex1.ndim == 0 or (ex1.ndim == 1 and shape and strides): |
|---|
| 1055 | n/a | self.assertEqual(m1, ex1) |
|---|
| 1056 | n/a | if ex2 and ex2.ndim == 1 and shape and strides: |
|---|
| 1057 | n/a | self.assertEqual(m2, ex2) |
|---|
| 1058 | n/a | |
|---|
| 1059 | n/a | for req in requests: |
|---|
| 1060 | n/a | for bits in real_flags: |
|---|
| 1061 | n/a | self.verify_getbuf(ex1, ex1, req|bits) |
|---|
| 1062 | n/a | self.verify_getbuf(ex1, m1, req|bits) |
|---|
| 1063 | n/a | if ex2: |
|---|
| 1064 | n/a | self.verify_getbuf(ex2, ex2, req|bits, |
|---|
| 1065 | n/a | sliced=True) |
|---|
| 1066 | n/a | self.verify_getbuf(ex2, m2, req|bits, |
|---|
| 1067 | n/a | sliced=True) |
|---|
| 1068 | n/a | |
|---|
| 1069 | n/a | items = [1,2,3,4,5,6,7,8,9,10,11,12] |
|---|
| 1070 | n/a | |
|---|
| 1071 | n/a | # ND_GETBUF_FAIL |
|---|
| 1072 | n/a | ex = ndarray(items, shape=[12], flags=ND_GETBUF_FAIL) |
|---|
| 1073 | n/a | self.assertRaises(BufferError, ndarray, ex) |
|---|
| 1074 | n/a | |
|---|
| 1075 | n/a | # Request complex structure from a simple exporter. In this |
|---|
| 1076 | n/a | # particular case the test object is not PEP-3118 compliant. |
|---|
| 1077 | n/a | base = ndarray([9], [1]) |
|---|
| 1078 | n/a | ex = ndarray(base, getbuf=PyBUF_SIMPLE) |
|---|
| 1079 | n/a | self.assertRaises(BufferError, ndarray, ex, getbuf=PyBUF_WRITABLE) |
|---|
| 1080 | n/a | self.assertRaises(BufferError, ndarray, ex, getbuf=PyBUF_ND) |
|---|
| 1081 | n/a | self.assertRaises(BufferError, ndarray, ex, getbuf=PyBUF_STRIDES) |
|---|
| 1082 | n/a | self.assertRaises(BufferError, ndarray, ex, getbuf=PyBUF_C_CONTIGUOUS) |
|---|
| 1083 | n/a | self.assertRaises(BufferError, ndarray, ex, getbuf=PyBUF_F_CONTIGUOUS) |
|---|
| 1084 | n/a | self.assertRaises(BufferError, ndarray, ex, getbuf=PyBUF_ANY_CONTIGUOUS) |
|---|
| 1085 | n/a | nd = ndarray(ex, getbuf=PyBUF_SIMPLE) |
|---|
| 1086 | n/a | |
|---|
| 1087 | n/a | # Issue #22445: New precise contiguity definition. |
|---|
| 1088 | n/a | for shape in [1,12,1], [7,0,7]: |
|---|
| 1089 | n/a | for order in 0, ND_FORTRAN: |
|---|
| 1090 | n/a | ex = ndarray(items, shape=shape, flags=order|ND_WRITABLE) |
|---|
| 1091 | n/a | self.assertTrue(is_contiguous(ex, 'F')) |
|---|
| 1092 | n/a | self.assertTrue(is_contiguous(ex, 'C')) |
|---|
| 1093 | n/a | |
|---|
| 1094 | n/a | for flags in requests: |
|---|
| 1095 | n/a | nd = ndarray(ex, getbuf=flags) |
|---|
| 1096 | n/a | self.assertTrue(is_contiguous(nd, 'F')) |
|---|
| 1097 | n/a | self.assertTrue(is_contiguous(nd, 'C')) |
|---|
| 1098 | n/a | |
|---|
| 1099 | n/a | def test_ndarray_exceptions(self): |
|---|
| 1100 | n/a | nd = ndarray([9], [1]) |
|---|
| 1101 | n/a | ndm = ndarray([9], [1], flags=ND_VAREXPORT) |
|---|
| 1102 | n/a | |
|---|
| 1103 | n/a | # Initialization of a new ndarray or mutation of an existing array. |
|---|
| 1104 | n/a | for c in (ndarray, nd.push, ndm.push): |
|---|
| 1105 | n/a | # Invalid types. |
|---|
| 1106 | n/a | self.assertRaises(TypeError, c, {1,2,3}) |
|---|
| 1107 | n/a | self.assertRaises(TypeError, c, [1,2,'3']) |
|---|
| 1108 | n/a | self.assertRaises(TypeError, c, [1,2,(3,4)]) |
|---|
| 1109 | n/a | self.assertRaises(TypeError, c, [1,2,3], shape={3}) |
|---|
| 1110 | n/a | self.assertRaises(TypeError, c, [1,2,3], shape=[3], strides={1}) |
|---|
| 1111 | n/a | self.assertRaises(TypeError, c, [1,2,3], shape=[3], offset=[]) |
|---|
| 1112 | n/a | self.assertRaises(TypeError, c, [1], shape=[1], format={}) |
|---|
| 1113 | n/a | self.assertRaises(TypeError, c, [1], shape=[1], flags={}) |
|---|
| 1114 | n/a | self.assertRaises(TypeError, c, [1], shape=[1], getbuf={}) |
|---|
| 1115 | n/a | |
|---|
| 1116 | n/a | # ND_FORTRAN flag is only valid without strides. |
|---|
| 1117 | n/a | self.assertRaises(TypeError, c, [1], shape=[1], strides=[1], |
|---|
| 1118 | n/a | flags=ND_FORTRAN) |
|---|
| 1119 | n/a | |
|---|
| 1120 | n/a | # ND_PIL flag is only valid with ndim > 0. |
|---|
| 1121 | n/a | self.assertRaises(TypeError, c, [1], shape=[], flags=ND_PIL) |
|---|
| 1122 | n/a | |
|---|
| 1123 | n/a | # Invalid items. |
|---|
| 1124 | n/a | self.assertRaises(ValueError, c, [], shape=[1]) |
|---|
| 1125 | n/a | self.assertRaises(ValueError, c, ['XXX'], shape=[1], format="L") |
|---|
| 1126 | n/a | # Invalid combination of items and format. |
|---|
| 1127 | n/a | self.assertRaises(struct.error, c, [1000], shape=[1], format="B") |
|---|
| 1128 | n/a | self.assertRaises(ValueError, c, [1,(2,3)], shape=[2], format="B") |
|---|
| 1129 | n/a | self.assertRaises(ValueError, c, [1,2,3], shape=[3], format="QL") |
|---|
| 1130 | n/a | |
|---|
| 1131 | n/a | # Invalid ndim. |
|---|
| 1132 | n/a | n = ND_MAX_NDIM+1 |
|---|
| 1133 | n/a | self.assertRaises(ValueError, c, [1]*n, shape=[1]*n) |
|---|
| 1134 | n/a | |
|---|
| 1135 | n/a | # Invalid shape. |
|---|
| 1136 | n/a | self.assertRaises(ValueError, c, [1], shape=[-1]) |
|---|
| 1137 | n/a | self.assertRaises(ValueError, c, [1,2,3], shape=['3']) |
|---|
| 1138 | n/a | self.assertRaises(OverflowError, c, [1], shape=[2**128]) |
|---|
| 1139 | n/a | # prod(shape) * itemsize != len(items) |
|---|
| 1140 | n/a | self.assertRaises(ValueError, c, [1,2,3,4,5], shape=[2,2], offset=3) |
|---|
| 1141 | n/a | |
|---|
| 1142 | n/a | # Invalid strides. |
|---|
| 1143 | n/a | self.assertRaises(ValueError, c, [1,2,3], shape=[3], strides=['1']) |
|---|
| 1144 | n/a | self.assertRaises(OverflowError, c, [1], shape=[1], |
|---|
| 1145 | n/a | strides=[2**128]) |
|---|
| 1146 | n/a | |
|---|
| 1147 | n/a | # Invalid combination of strides and shape. |
|---|
| 1148 | n/a | self.assertRaises(ValueError, c, [1,2], shape=[2,1], strides=[1]) |
|---|
| 1149 | n/a | # Invalid combination of strides and format. |
|---|
| 1150 | n/a | self.assertRaises(ValueError, c, [1,2,3,4], shape=[2], strides=[3], |
|---|
| 1151 | n/a | format="L") |
|---|
| 1152 | n/a | |
|---|
| 1153 | n/a | # Invalid offset. |
|---|
| 1154 | n/a | self.assertRaises(ValueError, c, [1,2,3], shape=[3], offset=4) |
|---|
| 1155 | n/a | self.assertRaises(ValueError, c, [1,2,3], shape=[1], offset=3, |
|---|
| 1156 | n/a | format="L") |
|---|
| 1157 | n/a | |
|---|
| 1158 | n/a | # Invalid format. |
|---|
| 1159 | n/a | self.assertRaises(ValueError, c, [1,2,3], shape=[3], format="") |
|---|
| 1160 | n/a | self.assertRaises(struct.error, c, [(1,2,3)], shape=[1], |
|---|
| 1161 | n/a | format="@#$") |
|---|
| 1162 | n/a | |
|---|
| 1163 | n/a | # Striding out of the memory bounds. |
|---|
| 1164 | n/a | items = [1,2,3,4,5,6,7,8,9,10] |
|---|
| 1165 | n/a | self.assertRaises(ValueError, c, items, shape=[2,3], |
|---|
| 1166 | n/a | strides=[-3, -2], offset=5) |
|---|
| 1167 | n/a | |
|---|
| 1168 | n/a | # Constructing consumer: format argument invalid. |
|---|
| 1169 | n/a | self.assertRaises(TypeError, c, bytearray(), format="Q") |
|---|
| 1170 | n/a | |
|---|
| 1171 | n/a | # Constructing original base object: getbuf argument invalid. |
|---|
| 1172 | n/a | self.assertRaises(TypeError, c, [1], shape=[1], getbuf=PyBUF_FULL) |
|---|
| 1173 | n/a | |
|---|
| 1174 | n/a | # Shape argument is mandatory for original base objects. |
|---|
| 1175 | n/a | self.assertRaises(TypeError, c, [1]) |
|---|
| 1176 | n/a | |
|---|
| 1177 | n/a | |
|---|
| 1178 | n/a | # PyBUF_WRITABLE request to read-only provider. |
|---|
| 1179 | n/a | self.assertRaises(BufferError, ndarray, b'123', getbuf=PyBUF_WRITABLE) |
|---|
| 1180 | n/a | |
|---|
| 1181 | n/a | # ND_VAREXPORT can only be specified during construction. |
|---|
| 1182 | n/a | nd = ndarray([9], [1], flags=ND_VAREXPORT) |
|---|
| 1183 | n/a | self.assertRaises(ValueError, nd.push, [1], [1], flags=ND_VAREXPORT) |
|---|
| 1184 | n/a | |
|---|
| 1185 | n/a | # Invalid operation for consumers: push/pop |
|---|
| 1186 | n/a | nd = ndarray(b'123') |
|---|
| 1187 | n/a | self.assertRaises(BufferError, nd.push, [1], [1]) |
|---|
| 1188 | n/a | self.assertRaises(BufferError, nd.pop) |
|---|
| 1189 | n/a | |
|---|
| 1190 | n/a | # ND_VAREXPORT not set: push/pop fail with exported buffers |
|---|
| 1191 | n/a | nd = ndarray([9], [1]) |
|---|
| 1192 | n/a | nd.push([1], [1]) |
|---|
| 1193 | n/a | m = memoryview(nd) |
|---|
| 1194 | n/a | self.assertRaises(BufferError, nd.push, [1], [1]) |
|---|
| 1195 | n/a | self.assertRaises(BufferError, nd.pop) |
|---|
| 1196 | n/a | m.release() |
|---|
| 1197 | n/a | nd.pop() |
|---|
| 1198 | n/a | |
|---|
| 1199 | n/a | # Single remaining buffer: pop fails |
|---|
| 1200 | n/a | self.assertRaises(BufferError, nd.pop) |
|---|
| 1201 | n/a | del nd |
|---|
| 1202 | n/a | |
|---|
| 1203 | n/a | # get_pointer() |
|---|
| 1204 | n/a | self.assertRaises(TypeError, get_pointer, {}, [1,2,3]) |
|---|
| 1205 | n/a | self.assertRaises(TypeError, get_pointer, b'123', {}) |
|---|
| 1206 | n/a | |
|---|
| 1207 | n/a | nd = ndarray(list(range(100)), shape=[1]*100) |
|---|
| 1208 | n/a | self.assertRaises(ValueError, get_pointer, nd, [5]) |
|---|
| 1209 | n/a | |
|---|
| 1210 | n/a | nd = ndarray(list(range(12)), shape=[3,4]) |
|---|
| 1211 | n/a | self.assertRaises(ValueError, get_pointer, nd, [2,3,4]) |
|---|
| 1212 | n/a | self.assertRaises(ValueError, get_pointer, nd, [3,3]) |
|---|
| 1213 | n/a | self.assertRaises(ValueError, get_pointer, nd, [-3,3]) |
|---|
| 1214 | n/a | self.assertRaises(OverflowError, get_pointer, nd, [1<<64,3]) |
|---|
| 1215 | n/a | |
|---|
| 1216 | n/a | # tolist() needs format |
|---|
| 1217 | n/a | ex = ndarray([1,2,3], shape=[3], format='L') |
|---|
| 1218 | n/a | nd = ndarray(ex, getbuf=PyBUF_SIMPLE) |
|---|
| 1219 | n/a | self.assertRaises(ValueError, nd.tolist) |
|---|
| 1220 | n/a | |
|---|
| 1221 | n/a | # memoryview_from_buffer() |
|---|
| 1222 | n/a | ex1 = ndarray([1,2,3], shape=[3], format='L') |
|---|
| 1223 | n/a | ex2 = ndarray(ex1) |
|---|
| 1224 | n/a | nd = ndarray(ex2) |
|---|
| 1225 | n/a | self.assertRaises(TypeError, nd.memoryview_from_buffer) |
|---|
| 1226 | n/a | |
|---|
| 1227 | n/a | nd = ndarray([(1,)*200], shape=[1], format='L'*200) |
|---|
| 1228 | n/a | self.assertRaises(TypeError, nd.memoryview_from_buffer) |
|---|
| 1229 | n/a | |
|---|
| 1230 | n/a | n = ND_MAX_NDIM |
|---|
| 1231 | n/a | nd = ndarray(list(range(n)), shape=[1]*n) |
|---|
| 1232 | n/a | self.assertRaises(ValueError, nd.memoryview_from_buffer) |
|---|
| 1233 | n/a | |
|---|
| 1234 | n/a | # get_contiguous() |
|---|
| 1235 | n/a | nd = ndarray([1], shape=[1]) |
|---|
| 1236 | n/a | self.assertRaises(TypeError, get_contiguous, 1, 2, 3, 4, 5) |
|---|
| 1237 | n/a | self.assertRaises(TypeError, get_contiguous, nd, "xyz", 'C') |
|---|
| 1238 | n/a | self.assertRaises(OverflowError, get_contiguous, nd, 2**64, 'C') |
|---|
| 1239 | n/a | self.assertRaises(TypeError, get_contiguous, nd, PyBUF_READ, 961) |
|---|
| 1240 | n/a | self.assertRaises(UnicodeEncodeError, get_contiguous, nd, PyBUF_READ, |
|---|
| 1241 | n/a | '\u2007') |
|---|
| 1242 | n/a | self.assertRaises(ValueError, get_contiguous, nd, PyBUF_READ, 'Z') |
|---|
| 1243 | n/a | self.assertRaises(ValueError, get_contiguous, nd, 255, 'A') |
|---|
| 1244 | n/a | |
|---|
| 1245 | n/a | # cmp_contig() |
|---|
| 1246 | n/a | nd = ndarray([1], shape=[1]) |
|---|
| 1247 | n/a | self.assertRaises(TypeError, cmp_contig, 1, 2, 3, 4, 5) |
|---|
| 1248 | n/a | self.assertRaises(TypeError, cmp_contig, {}, nd) |
|---|
| 1249 | n/a | self.assertRaises(TypeError, cmp_contig, nd, {}) |
|---|
| 1250 | n/a | |
|---|
| 1251 | n/a | # is_contiguous() |
|---|
| 1252 | n/a | nd = ndarray([1], shape=[1]) |
|---|
| 1253 | n/a | self.assertRaises(TypeError, is_contiguous, 1, 2, 3, 4, 5) |
|---|
| 1254 | n/a | self.assertRaises(TypeError, is_contiguous, {}, 'A') |
|---|
| 1255 | n/a | self.assertRaises(TypeError, is_contiguous, nd, 201) |
|---|
| 1256 | n/a | |
|---|
| 1257 | n/a | def test_ndarray_linked_list(self): |
|---|
| 1258 | n/a | for perm in permutations(range(5)): |
|---|
| 1259 | n/a | m = [0]*5 |
|---|
| 1260 | n/a | nd = ndarray([1,2,3], shape=[3], flags=ND_VAREXPORT) |
|---|
| 1261 | n/a | m[0] = memoryview(nd) |
|---|
| 1262 | n/a | |
|---|
| 1263 | n/a | for i in range(1, 5): |
|---|
| 1264 | n/a | nd.push([1,2,3], shape=[3]) |
|---|
| 1265 | n/a | m[i] = memoryview(nd) |
|---|
| 1266 | n/a | |
|---|
| 1267 | n/a | for i in range(5): |
|---|
| 1268 | n/a | m[perm[i]].release() |
|---|
| 1269 | n/a | |
|---|
| 1270 | n/a | self.assertRaises(BufferError, nd.pop) |
|---|
| 1271 | n/a | del nd |
|---|
| 1272 | n/a | |
|---|
| 1273 | n/a | def test_ndarray_format_scalar(self): |
|---|
| 1274 | n/a | # ndim = 0: scalar |
|---|
| 1275 | n/a | for fmt, scalar, _ in iter_format(0): |
|---|
| 1276 | n/a | itemsize = struct.calcsize(fmt) |
|---|
| 1277 | n/a | nd = ndarray(scalar, shape=(), format=fmt) |
|---|
| 1278 | n/a | self.verify(nd, obj=None, |
|---|
| 1279 | n/a | itemsize=itemsize, fmt=fmt, readonly=1, |
|---|
| 1280 | n/a | ndim=0, shape=(), strides=(), |
|---|
| 1281 | n/a | lst=scalar) |
|---|
| 1282 | n/a | |
|---|
| 1283 | n/a | def test_ndarray_format_shape(self): |
|---|
| 1284 | n/a | # ndim = 1, shape = [n] |
|---|
| 1285 | n/a | nitems = randrange(1, 10) |
|---|
| 1286 | n/a | for fmt, items, _ in iter_format(nitems): |
|---|
| 1287 | n/a | itemsize = struct.calcsize(fmt) |
|---|
| 1288 | n/a | for flags in (0, ND_PIL): |
|---|
| 1289 | n/a | nd = ndarray(items, shape=[nitems], format=fmt, flags=flags) |
|---|
| 1290 | n/a | self.verify(nd, obj=None, |
|---|
| 1291 | n/a | itemsize=itemsize, fmt=fmt, readonly=1, |
|---|
| 1292 | n/a | ndim=1, shape=(nitems,), strides=(itemsize,), |
|---|
| 1293 | n/a | lst=items) |
|---|
| 1294 | n/a | |
|---|
| 1295 | n/a | def test_ndarray_format_strides(self): |
|---|
| 1296 | n/a | # ndim = 1, strides |
|---|
| 1297 | n/a | nitems = randrange(1, 30) |
|---|
| 1298 | n/a | for fmt, items, _ in iter_format(nitems): |
|---|
| 1299 | n/a | itemsize = struct.calcsize(fmt) |
|---|
| 1300 | n/a | for step in range(-5, 5): |
|---|
| 1301 | n/a | if step == 0: |
|---|
| 1302 | n/a | continue |
|---|
| 1303 | n/a | |
|---|
| 1304 | n/a | shape = [len(items[::step])] |
|---|
| 1305 | n/a | strides = [step*itemsize] |
|---|
| 1306 | n/a | offset = itemsize*(nitems-1) if step < 0 else 0 |
|---|
| 1307 | n/a | |
|---|
| 1308 | n/a | for flags in (0, ND_PIL): |
|---|
| 1309 | n/a | nd = ndarray(items, shape=shape, strides=strides, |
|---|
| 1310 | n/a | format=fmt, offset=offset, flags=flags) |
|---|
| 1311 | n/a | self.verify(nd, obj=None, |
|---|
| 1312 | n/a | itemsize=itemsize, fmt=fmt, readonly=1, |
|---|
| 1313 | n/a | ndim=1, shape=shape, strides=strides, |
|---|
| 1314 | n/a | lst=items[::step]) |
|---|
| 1315 | n/a | |
|---|
| 1316 | n/a | def test_ndarray_fortran(self): |
|---|
| 1317 | n/a | items = [1,2,3,4,5,6,7,8,9,10,11,12] |
|---|
| 1318 | n/a | ex = ndarray(items, shape=(3, 4), strides=(1, 3)) |
|---|
| 1319 | n/a | nd = ndarray(ex, getbuf=PyBUF_F_CONTIGUOUS|PyBUF_FORMAT) |
|---|
| 1320 | n/a | self.assertEqual(nd.tolist(), farray(items, (3, 4))) |
|---|
| 1321 | n/a | |
|---|
| 1322 | n/a | def test_ndarray_multidim(self): |
|---|
| 1323 | n/a | for ndim in range(5): |
|---|
| 1324 | n/a | shape_t = [randrange(2, 10) for _ in range(ndim)] |
|---|
| 1325 | n/a | nitems = prod(shape_t) |
|---|
| 1326 | n/a | for shape in permutations(shape_t): |
|---|
| 1327 | n/a | |
|---|
| 1328 | n/a | fmt, items, _ = randitems(nitems) |
|---|
| 1329 | n/a | itemsize = struct.calcsize(fmt) |
|---|
| 1330 | n/a | |
|---|
| 1331 | n/a | for flags in (0, ND_PIL): |
|---|
| 1332 | n/a | if ndim == 0 and flags == ND_PIL: |
|---|
| 1333 | n/a | continue |
|---|
| 1334 | n/a | |
|---|
| 1335 | n/a | # C array |
|---|
| 1336 | n/a | nd = ndarray(items, shape=shape, format=fmt, flags=flags) |
|---|
| 1337 | n/a | |
|---|
| 1338 | n/a | strides = strides_from_shape(ndim, shape, itemsize, 'C') |
|---|
| 1339 | n/a | lst = carray(items, shape) |
|---|
| 1340 | n/a | self.verify(nd, obj=None, |
|---|
| 1341 | n/a | itemsize=itemsize, fmt=fmt, readonly=1, |
|---|
| 1342 | n/a | ndim=ndim, shape=shape, strides=strides, |
|---|
| 1343 | n/a | lst=lst) |
|---|
| 1344 | n/a | |
|---|
| 1345 | n/a | if is_memoryview_format(fmt): |
|---|
| 1346 | n/a | # memoryview: reconstruct strides |
|---|
| 1347 | n/a | ex = ndarray(items, shape=shape, format=fmt) |
|---|
| 1348 | n/a | nd = ndarray(ex, getbuf=PyBUF_CONTIG_RO|PyBUF_FORMAT) |
|---|
| 1349 | n/a | self.assertTrue(nd.strides == ()) |
|---|
| 1350 | n/a | mv = nd.memoryview_from_buffer() |
|---|
| 1351 | n/a | self.verify(mv, obj=None, |
|---|
| 1352 | n/a | itemsize=itemsize, fmt=fmt, readonly=1, |
|---|
| 1353 | n/a | ndim=ndim, shape=shape, strides=strides, |
|---|
| 1354 | n/a | lst=lst) |
|---|
| 1355 | n/a | |
|---|
| 1356 | n/a | # Fortran array |
|---|
| 1357 | n/a | nd = ndarray(items, shape=shape, format=fmt, |
|---|
| 1358 | n/a | flags=flags|ND_FORTRAN) |
|---|
| 1359 | n/a | |
|---|
| 1360 | n/a | strides = strides_from_shape(ndim, shape, itemsize, 'F') |
|---|
| 1361 | n/a | lst = farray(items, shape) |
|---|
| 1362 | n/a | self.verify(nd, obj=None, |
|---|
| 1363 | n/a | itemsize=itemsize, fmt=fmt, readonly=1, |
|---|
| 1364 | n/a | ndim=ndim, shape=shape, strides=strides, |
|---|
| 1365 | n/a | lst=lst) |
|---|
| 1366 | n/a | |
|---|
| 1367 | n/a | def test_ndarray_index_invalid(self): |
|---|
| 1368 | n/a | # not writable |
|---|
| 1369 | n/a | nd = ndarray([1], shape=[1]) |
|---|
| 1370 | n/a | self.assertRaises(TypeError, nd.__setitem__, 1, 8) |
|---|
| 1371 | n/a | mv = memoryview(nd) |
|---|
| 1372 | n/a | self.assertEqual(mv, nd) |
|---|
| 1373 | n/a | self.assertRaises(TypeError, mv.__setitem__, 1, 8) |
|---|
| 1374 | n/a | |
|---|
| 1375 | n/a | # cannot be deleted |
|---|
| 1376 | n/a | nd = ndarray([1], shape=[1], flags=ND_WRITABLE) |
|---|
| 1377 | n/a | self.assertRaises(TypeError, nd.__delitem__, 1) |
|---|
| 1378 | n/a | mv = memoryview(nd) |
|---|
| 1379 | n/a | self.assertEqual(mv, nd) |
|---|
| 1380 | n/a | self.assertRaises(TypeError, mv.__delitem__, 1) |
|---|
| 1381 | n/a | |
|---|
| 1382 | n/a | # overflow |
|---|
| 1383 | n/a | nd = ndarray([1], shape=[1], flags=ND_WRITABLE) |
|---|
| 1384 | n/a | self.assertRaises(OverflowError, nd.__getitem__, 1<<64) |
|---|
| 1385 | n/a | self.assertRaises(OverflowError, nd.__setitem__, 1<<64, 8) |
|---|
| 1386 | n/a | mv = memoryview(nd) |
|---|
| 1387 | n/a | self.assertEqual(mv, nd) |
|---|
| 1388 | n/a | self.assertRaises(IndexError, mv.__getitem__, 1<<64) |
|---|
| 1389 | n/a | self.assertRaises(IndexError, mv.__setitem__, 1<<64, 8) |
|---|
| 1390 | n/a | |
|---|
| 1391 | n/a | # format |
|---|
| 1392 | n/a | items = [1,2,3,4,5,6,7,8] |
|---|
| 1393 | n/a | nd = ndarray(items, shape=[len(items)], format="B", flags=ND_WRITABLE) |
|---|
| 1394 | n/a | self.assertRaises(struct.error, nd.__setitem__, 2, 300) |
|---|
| 1395 | n/a | self.assertRaises(ValueError, nd.__setitem__, 1, (100, 200)) |
|---|
| 1396 | n/a | mv = memoryview(nd) |
|---|
| 1397 | n/a | self.assertEqual(mv, nd) |
|---|
| 1398 | n/a | self.assertRaises(ValueError, mv.__setitem__, 2, 300) |
|---|
| 1399 | n/a | self.assertRaises(TypeError, mv.__setitem__, 1, (100, 200)) |
|---|
| 1400 | n/a | |
|---|
| 1401 | n/a | items = [(1,2), (3,4), (5,6)] |
|---|
| 1402 | n/a | nd = ndarray(items, shape=[len(items)], format="LQ", flags=ND_WRITABLE) |
|---|
| 1403 | n/a | self.assertRaises(ValueError, nd.__setitem__, 2, 300) |
|---|
| 1404 | n/a | self.assertRaises(struct.error, nd.__setitem__, 1, (b'\x001', 200)) |
|---|
| 1405 | n/a | |
|---|
| 1406 | n/a | def test_ndarray_index_scalar(self): |
|---|
| 1407 | n/a | # scalar |
|---|
| 1408 | n/a | nd = ndarray(1, shape=(), flags=ND_WRITABLE) |
|---|
| 1409 | n/a | mv = memoryview(nd) |
|---|
| 1410 | n/a | self.assertEqual(mv, nd) |
|---|
| 1411 | n/a | |
|---|
| 1412 | n/a | x = nd[()]; self.assertEqual(x, 1) |
|---|
| 1413 | n/a | x = nd[...]; self.assertEqual(x.tolist(), nd.tolist()) |
|---|
| 1414 | n/a | |
|---|
| 1415 | n/a | x = mv[()]; self.assertEqual(x, 1) |
|---|
| 1416 | n/a | x = mv[...]; self.assertEqual(x.tolist(), nd.tolist()) |
|---|
| 1417 | n/a | |
|---|
| 1418 | n/a | self.assertRaises(TypeError, nd.__getitem__, 0) |
|---|
| 1419 | n/a | self.assertRaises(TypeError, mv.__getitem__, 0) |
|---|
| 1420 | n/a | self.assertRaises(TypeError, nd.__setitem__, 0, 8) |
|---|
| 1421 | n/a | self.assertRaises(TypeError, mv.__setitem__, 0, 8) |
|---|
| 1422 | n/a | |
|---|
| 1423 | n/a | self.assertEqual(nd.tolist(), 1) |
|---|
| 1424 | n/a | self.assertEqual(mv.tolist(), 1) |
|---|
| 1425 | n/a | |
|---|
| 1426 | n/a | nd[()] = 9; self.assertEqual(nd.tolist(), 9) |
|---|
| 1427 | n/a | mv[()] = 9; self.assertEqual(mv.tolist(), 9) |
|---|
| 1428 | n/a | |
|---|
| 1429 | n/a | nd[...] = 5; self.assertEqual(nd.tolist(), 5) |
|---|
| 1430 | n/a | mv[...] = 5; self.assertEqual(mv.tolist(), 5) |
|---|
| 1431 | n/a | |
|---|
| 1432 | n/a | def test_ndarray_index_null_strides(self): |
|---|
| 1433 | n/a | ex = ndarray(list(range(2*4)), shape=[2, 4], flags=ND_WRITABLE) |
|---|
| 1434 | n/a | nd = ndarray(ex, getbuf=PyBUF_CONTIG) |
|---|
| 1435 | n/a | |
|---|
| 1436 | n/a | # Sub-views are only possible for full exporters. |
|---|
| 1437 | n/a | self.assertRaises(BufferError, nd.__getitem__, 1) |
|---|
| 1438 | n/a | # Same for slices. |
|---|
| 1439 | n/a | self.assertRaises(BufferError, nd.__getitem__, slice(3,5,1)) |
|---|
| 1440 | n/a | |
|---|
| 1441 | n/a | def test_ndarray_index_getitem_single(self): |
|---|
| 1442 | n/a | # getitem |
|---|
| 1443 | n/a | for fmt, items, _ in iter_format(5): |
|---|
| 1444 | n/a | nd = ndarray(items, shape=[5], format=fmt) |
|---|
| 1445 | n/a | for i in range(-5, 5): |
|---|
| 1446 | n/a | self.assertEqual(nd[i], items[i]) |
|---|
| 1447 | n/a | |
|---|
| 1448 | n/a | self.assertRaises(IndexError, nd.__getitem__, -6) |
|---|
| 1449 | n/a | self.assertRaises(IndexError, nd.__getitem__, 5) |
|---|
| 1450 | n/a | |
|---|
| 1451 | n/a | if is_memoryview_format(fmt): |
|---|
| 1452 | n/a | mv = memoryview(nd) |
|---|
| 1453 | n/a | self.assertEqual(mv, nd) |
|---|
| 1454 | n/a | for i in range(-5, 5): |
|---|
| 1455 | n/a | self.assertEqual(mv[i], items[i]) |
|---|
| 1456 | n/a | |
|---|
| 1457 | n/a | self.assertRaises(IndexError, mv.__getitem__, -6) |
|---|
| 1458 | n/a | self.assertRaises(IndexError, mv.__getitem__, 5) |
|---|
| 1459 | n/a | |
|---|
| 1460 | n/a | # getitem with null strides |
|---|
| 1461 | n/a | for fmt, items, _ in iter_format(5): |
|---|
| 1462 | n/a | ex = ndarray(items, shape=[5], flags=ND_WRITABLE, format=fmt) |
|---|
| 1463 | n/a | nd = ndarray(ex, getbuf=PyBUF_CONTIG|PyBUF_FORMAT) |
|---|
| 1464 | n/a | |
|---|
| 1465 | n/a | for i in range(-5, 5): |
|---|
| 1466 | n/a | self.assertEqual(nd[i], items[i]) |
|---|
| 1467 | n/a | |
|---|
| 1468 | n/a | if is_memoryview_format(fmt): |
|---|
| 1469 | n/a | mv = nd.memoryview_from_buffer() |
|---|
| 1470 | n/a | self.assertIs(mv.__eq__(nd), NotImplemented) |
|---|
| 1471 | n/a | for i in range(-5, 5): |
|---|
| 1472 | n/a | self.assertEqual(mv[i], items[i]) |
|---|
| 1473 | n/a | |
|---|
| 1474 | n/a | # getitem with null format |
|---|
| 1475 | n/a | items = [1,2,3,4,5] |
|---|
| 1476 | n/a | ex = ndarray(items, shape=[5]) |
|---|
| 1477 | n/a | nd = ndarray(ex, getbuf=PyBUF_CONTIG_RO) |
|---|
| 1478 | n/a | for i in range(-5, 5): |
|---|
| 1479 | n/a | self.assertEqual(nd[i], items[i]) |
|---|
| 1480 | n/a | |
|---|
| 1481 | n/a | # getitem with null shape/strides/format |
|---|
| 1482 | n/a | items = [1,2,3,4,5] |
|---|
| 1483 | n/a | ex = ndarray(items, shape=[5]) |
|---|
| 1484 | n/a | nd = ndarray(ex, getbuf=PyBUF_SIMPLE) |
|---|
| 1485 | n/a | |
|---|
| 1486 | n/a | for i in range(-5, 5): |
|---|
| 1487 | n/a | self.assertEqual(nd[i], items[i]) |
|---|
| 1488 | n/a | |
|---|
| 1489 | n/a | def test_ndarray_index_setitem_single(self): |
|---|
| 1490 | n/a | # assign single value |
|---|
| 1491 | n/a | for fmt, items, single_item in iter_format(5): |
|---|
| 1492 | n/a | nd = ndarray(items, shape=[5], format=fmt, flags=ND_WRITABLE) |
|---|
| 1493 | n/a | for i in range(5): |
|---|
| 1494 | n/a | items[i] = single_item |
|---|
| 1495 | n/a | nd[i] = single_item |
|---|
| 1496 | n/a | self.assertEqual(nd.tolist(), items) |
|---|
| 1497 | n/a | |
|---|
| 1498 | n/a | self.assertRaises(IndexError, nd.__setitem__, -6, single_item) |
|---|
| 1499 | n/a | self.assertRaises(IndexError, nd.__setitem__, 5, single_item) |
|---|
| 1500 | n/a | |
|---|
| 1501 | n/a | if not is_memoryview_format(fmt): |
|---|
| 1502 | n/a | continue |
|---|
| 1503 | n/a | |
|---|
| 1504 | n/a | nd = ndarray(items, shape=[5], format=fmt, flags=ND_WRITABLE) |
|---|
| 1505 | n/a | mv = memoryview(nd) |
|---|
| 1506 | n/a | self.assertEqual(mv, nd) |
|---|
| 1507 | n/a | for i in range(5): |
|---|
| 1508 | n/a | items[i] = single_item |
|---|
| 1509 | n/a | mv[i] = single_item |
|---|
| 1510 | n/a | self.assertEqual(mv.tolist(), items) |
|---|
| 1511 | n/a | |
|---|
| 1512 | n/a | self.assertRaises(IndexError, mv.__setitem__, -6, single_item) |
|---|
| 1513 | n/a | self.assertRaises(IndexError, mv.__setitem__, 5, single_item) |
|---|
| 1514 | n/a | |
|---|
| 1515 | n/a | |
|---|
| 1516 | n/a | # assign single value: lobject = robject |
|---|
| 1517 | n/a | for fmt, items, single_item in iter_format(5): |
|---|
| 1518 | n/a | nd = ndarray(items, shape=[5], format=fmt, flags=ND_WRITABLE) |
|---|
| 1519 | n/a | for i in range(-5, 4): |
|---|
| 1520 | n/a | items[i] = items[i+1] |
|---|
| 1521 | n/a | nd[i] = nd[i+1] |
|---|
| 1522 | n/a | self.assertEqual(nd.tolist(), items) |
|---|
| 1523 | n/a | |
|---|
| 1524 | n/a | if not is_memoryview_format(fmt): |
|---|
| 1525 | n/a | continue |
|---|
| 1526 | n/a | |
|---|
| 1527 | n/a | nd = ndarray(items, shape=[5], format=fmt, flags=ND_WRITABLE) |
|---|
| 1528 | n/a | mv = memoryview(nd) |
|---|
| 1529 | n/a | self.assertEqual(mv, nd) |
|---|
| 1530 | n/a | for i in range(-5, 4): |
|---|
| 1531 | n/a | items[i] = items[i+1] |
|---|
| 1532 | n/a | mv[i] = mv[i+1] |
|---|
| 1533 | n/a | self.assertEqual(mv.tolist(), items) |
|---|
| 1534 | n/a | |
|---|
| 1535 | n/a | def test_ndarray_index_getitem_multidim(self): |
|---|
| 1536 | n/a | shape_t = (2, 3, 5) |
|---|
| 1537 | n/a | nitems = prod(shape_t) |
|---|
| 1538 | n/a | for shape in permutations(shape_t): |
|---|
| 1539 | n/a | |
|---|
| 1540 | n/a | fmt, items, _ = randitems(nitems) |
|---|
| 1541 | n/a | |
|---|
| 1542 | n/a | for flags in (0, ND_PIL): |
|---|
| 1543 | n/a | # C array |
|---|
| 1544 | n/a | nd = ndarray(items, shape=shape, format=fmt, flags=flags) |
|---|
| 1545 | n/a | lst = carray(items, shape) |
|---|
| 1546 | n/a | |
|---|
| 1547 | n/a | for i in range(-shape[0], shape[0]): |
|---|
| 1548 | n/a | self.assertEqual(lst[i], nd[i].tolist()) |
|---|
| 1549 | n/a | for j in range(-shape[1], shape[1]): |
|---|
| 1550 | n/a | self.assertEqual(lst[i][j], nd[i][j].tolist()) |
|---|
| 1551 | n/a | for k in range(-shape[2], shape[2]): |
|---|
| 1552 | n/a | self.assertEqual(lst[i][j][k], nd[i][j][k]) |
|---|
| 1553 | n/a | |
|---|
| 1554 | n/a | # Fortran array |
|---|
| 1555 | n/a | nd = ndarray(items, shape=shape, format=fmt, |
|---|
| 1556 | n/a | flags=flags|ND_FORTRAN) |
|---|
| 1557 | n/a | lst = farray(items, shape) |
|---|
| 1558 | n/a | |
|---|
| 1559 | n/a | for i in range(-shape[0], shape[0]): |
|---|
| 1560 | n/a | self.assertEqual(lst[i], nd[i].tolist()) |
|---|
| 1561 | n/a | for j in range(-shape[1], shape[1]): |
|---|
| 1562 | n/a | self.assertEqual(lst[i][j], nd[i][j].tolist()) |
|---|
| 1563 | n/a | for k in range(shape[2], shape[2]): |
|---|
| 1564 | n/a | self.assertEqual(lst[i][j][k], nd[i][j][k]) |
|---|
| 1565 | n/a | |
|---|
| 1566 | n/a | def test_ndarray_sequence(self): |
|---|
| 1567 | n/a | nd = ndarray(1, shape=()) |
|---|
| 1568 | n/a | self.assertRaises(TypeError, eval, "1 in nd", locals()) |
|---|
| 1569 | n/a | mv = memoryview(nd) |
|---|
| 1570 | n/a | self.assertEqual(mv, nd) |
|---|
| 1571 | n/a | self.assertRaises(TypeError, eval, "1 in mv", locals()) |
|---|
| 1572 | n/a | |
|---|
| 1573 | n/a | for fmt, items, _ in iter_format(5): |
|---|
| 1574 | n/a | nd = ndarray(items, shape=[5], format=fmt) |
|---|
| 1575 | n/a | for i, v in enumerate(nd): |
|---|
| 1576 | n/a | self.assertEqual(v, items[i]) |
|---|
| 1577 | n/a | self.assertTrue(v in nd) |
|---|
| 1578 | n/a | |
|---|
| 1579 | n/a | if is_memoryview_format(fmt): |
|---|
| 1580 | n/a | mv = memoryview(nd) |
|---|
| 1581 | n/a | for i, v in enumerate(mv): |
|---|
| 1582 | n/a | self.assertEqual(v, items[i]) |
|---|
| 1583 | n/a | self.assertTrue(v in mv) |
|---|
| 1584 | n/a | |
|---|
| 1585 | n/a | def test_ndarray_slice_invalid(self): |
|---|
| 1586 | n/a | items = [1,2,3,4,5,6,7,8] |
|---|
| 1587 | n/a | |
|---|
| 1588 | n/a | # rvalue is not an exporter |
|---|
| 1589 | n/a | xl = ndarray(items, shape=[8], flags=ND_WRITABLE) |
|---|
| 1590 | n/a | ml = memoryview(xl) |
|---|
| 1591 | n/a | self.assertRaises(TypeError, xl.__setitem__, slice(0,8,1), items) |
|---|
| 1592 | n/a | self.assertRaises(TypeError, ml.__setitem__, slice(0,8,1), items) |
|---|
| 1593 | n/a | |
|---|
| 1594 | n/a | # rvalue is not a full exporter |
|---|
| 1595 | n/a | xl = ndarray(items, shape=[8], flags=ND_WRITABLE) |
|---|
| 1596 | n/a | ex = ndarray(items, shape=[8], flags=ND_WRITABLE) |
|---|
| 1597 | n/a | xr = ndarray(ex, getbuf=PyBUF_ND) |
|---|
| 1598 | n/a | self.assertRaises(BufferError, xl.__setitem__, slice(0,8,1), xr) |
|---|
| 1599 | n/a | |
|---|
| 1600 | n/a | # zero step |
|---|
| 1601 | n/a | nd = ndarray(items, shape=[8], format="L", flags=ND_WRITABLE) |
|---|
| 1602 | n/a | mv = memoryview(nd) |
|---|
| 1603 | n/a | self.assertRaises(ValueError, nd.__getitem__, slice(0,1,0)) |
|---|
| 1604 | n/a | self.assertRaises(ValueError, mv.__getitem__, slice(0,1,0)) |
|---|
| 1605 | n/a | |
|---|
| 1606 | n/a | nd = ndarray(items, shape=[2,4], format="L", flags=ND_WRITABLE) |
|---|
| 1607 | n/a | mv = memoryview(nd) |
|---|
| 1608 | n/a | |
|---|
| 1609 | n/a | self.assertRaises(ValueError, nd.__getitem__, |
|---|
| 1610 | n/a | (slice(0,1,1), slice(0,1,0))) |
|---|
| 1611 | n/a | self.assertRaises(ValueError, nd.__getitem__, |
|---|
| 1612 | n/a | (slice(0,1,0), slice(0,1,1))) |
|---|
| 1613 | n/a | self.assertRaises(TypeError, nd.__getitem__, "@%$") |
|---|
| 1614 | n/a | self.assertRaises(TypeError, nd.__getitem__, ("@%$", slice(0,1,1))) |
|---|
| 1615 | n/a | self.assertRaises(TypeError, nd.__getitem__, (slice(0,1,1), {})) |
|---|
| 1616 | n/a | |
|---|
| 1617 | n/a | # memoryview: not implemented |
|---|
| 1618 | n/a | self.assertRaises(NotImplementedError, mv.__getitem__, |
|---|
| 1619 | n/a | (slice(0,1,1), slice(0,1,0))) |
|---|
| 1620 | n/a | self.assertRaises(TypeError, mv.__getitem__, "@%$") |
|---|
| 1621 | n/a | |
|---|
| 1622 | n/a | # differing format |
|---|
| 1623 | n/a | xl = ndarray(items, shape=[8], format="B", flags=ND_WRITABLE) |
|---|
| 1624 | n/a | xr = ndarray(items, shape=[8], format="b") |
|---|
| 1625 | n/a | ml = memoryview(xl) |
|---|
| 1626 | n/a | mr = memoryview(xr) |
|---|
| 1627 | n/a | self.assertRaises(ValueError, xl.__setitem__, slice(0,1,1), xr[7:8]) |
|---|
| 1628 | n/a | self.assertEqual(xl.tolist(), items) |
|---|
| 1629 | n/a | self.assertRaises(ValueError, ml.__setitem__, slice(0,1,1), mr[7:8]) |
|---|
| 1630 | n/a | self.assertEqual(ml.tolist(), items) |
|---|
| 1631 | n/a | |
|---|
| 1632 | n/a | # differing itemsize |
|---|
| 1633 | n/a | xl = ndarray(items, shape=[8], format="B", flags=ND_WRITABLE) |
|---|
| 1634 | n/a | yr = ndarray(items, shape=[8], format="L") |
|---|
| 1635 | n/a | ml = memoryview(xl) |
|---|
| 1636 | n/a | mr = memoryview(xr) |
|---|
| 1637 | n/a | self.assertRaises(ValueError, xl.__setitem__, slice(0,1,1), xr[7:8]) |
|---|
| 1638 | n/a | self.assertEqual(xl.tolist(), items) |
|---|
| 1639 | n/a | self.assertRaises(ValueError, ml.__setitem__, slice(0,1,1), mr[7:8]) |
|---|
| 1640 | n/a | self.assertEqual(ml.tolist(), items) |
|---|
| 1641 | n/a | |
|---|
| 1642 | n/a | # differing ndim |
|---|
| 1643 | n/a | xl = ndarray(items, shape=[2, 4], format="b", flags=ND_WRITABLE) |
|---|
| 1644 | n/a | xr = ndarray(items, shape=[8], format="b") |
|---|
| 1645 | n/a | ml = memoryview(xl) |
|---|
| 1646 | n/a | mr = memoryview(xr) |
|---|
| 1647 | n/a | self.assertRaises(ValueError, xl.__setitem__, slice(0,1,1), xr[7:8]) |
|---|
| 1648 | n/a | self.assertEqual(xl.tolist(), [[1,2,3,4], [5,6,7,8]]) |
|---|
| 1649 | n/a | self.assertRaises(NotImplementedError, ml.__setitem__, slice(0,1,1), |
|---|
| 1650 | n/a | mr[7:8]) |
|---|
| 1651 | n/a | |
|---|
| 1652 | n/a | # differing shape |
|---|
| 1653 | n/a | xl = ndarray(items, shape=[8], format="b", flags=ND_WRITABLE) |
|---|
| 1654 | n/a | xr = ndarray(items, shape=[8], format="b") |
|---|
| 1655 | n/a | ml = memoryview(xl) |
|---|
| 1656 | n/a | mr = memoryview(xr) |
|---|
| 1657 | n/a | self.assertRaises(ValueError, xl.__setitem__, slice(0,2,1), xr[7:8]) |
|---|
| 1658 | n/a | self.assertEqual(xl.tolist(), items) |
|---|
| 1659 | n/a | self.assertRaises(ValueError, ml.__setitem__, slice(0,2,1), mr[7:8]) |
|---|
| 1660 | n/a | self.assertEqual(ml.tolist(), items) |
|---|
| 1661 | n/a | |
|---|
| 1662 | n/a | # _testbuffer.c module functions |
|---|
| 1663 | n/a | self.assertRaises(TypeError, slice_indices, slice(0,1,2), {}) |
|---|
| 1664 | n/a | self.assertRaises(TypeError, slice_indices, "###########", 1) |
|---|
| 1665 | n/a | self.assertRaises(ValueError, slice_indices, slice(0,1,0), 4) |
|---|
| 1666 | n/a | |
|---|
| 1667 | n/a | x = ndarray(items, shape=[8], format="b", flags=ND_PIL) |
|---|
| 1668 | n/a | self.assertRaises(TypeError, x.add_suboffsets) |
|---|
| 1669 | n/a | |
|---|
| 1670 | n/a | ex = ndarray(items, shape=[8], format="B") |
|---|
| 1671 | n/a | x = ndarray(ex, getbuf=PyBUF_SIMPLE) |
|---|
| 1672 | n/a | self.assertRaises(TypeError, x.add_suboffsets) |
|---|
| 1673 | n/a | |
|---|
| 1674 | n/a | def test_ndarray_slice_zero_shape(self): |
|---|
| 1675 | n/a | items = [1,2,3,4,5,6,7,8,9,10,11,12] |
|---|
| 1676 | n/a | |
|---|
| 1677 | n/a | x = ndarray(items, shape=[12], format="L", flags=ND_WRITABLE) |
|---|
| 1678 | n/a | y = ndarray(items, shape=[12], format="L") |
|---|
| 1679 | n/a | x[4:4] = y[9:9] |
|---|
| 1680 | n/a | self.assertEqual(x.tolist(), items) |
|---|
| 1681 | n/a | |
|---|
| 1682 | n/a | ml = memoryview(x) |
|---|
| 1683 | n/a | mr = memoryview(y) |
|---|
| 1684 | n/a | self.assertEqual(ml, x) |
|---|
| 1685 | n/a | self.assertEqual(ml, y) |
|---|
| 1686 | n/a | ml[4:4] = mr[9:9] |
|---|
| 1687 | n/a | self.assertEqual(ml.tolist(), items) |
|---|
| 1688 | n/a | |
|---|
| 1689 | n/a | x = ndarray(items, shape=[3, 4], format="L", flags=ND_WRITABLE) |
|---|
| 1690 | n/a | y = ndarray(items, shape=[4, 3], format="L") |
|---|
| 1691 | n/a | x[1:2, 2:2] = y[1:2, 3:3] |
|---|
| 1692 | n/a | self.assertEqual(x.tolist(), carray(items, [3, 4])) |
|---|
| 1693 | n/a | |
|---|
| 1694 | n/a | def test_ndarray_slice_multidim(self): |
|---|
| 1695 | n/a | shape_t = (2, 3, 5) |
|---|
| 1696 | n/a | ndim = len(shape_t) |
|---|
| 1697 | n/a | nitems = prod(shape_t) |
|---|
| 1698 | n/a | for shape in permutations(shape_t): |
|---|
| 1699 | n/a | |
|---|
| 1700 | n/a | fmt, items, _ = randitems(nitems) |
|---|
| 1701 | n/a | itemsize = struct.calcsize(fmt) |
|---|
| 1702 | n/a | |
|---|
| 1703 | n/a | for flags in (0, ND_PIL): |
|---|
| 1704 | n/a | nd = ndarray(items, shape=shape, format=fmt, flags=flags) |
|---|
| 1705 | n/a | lst = carray(items, shape) |
|---|
| 1706 | n/a | |
|---|
| 1707 | n/a | for slices in rslices_ndim(ndim, shape): |
|---|
| 1708 | n/a | |
|---|
| 1709 | n/a | listerr = None |
|---|
| 1710 | n/a | try: |
|---|
| 1711 | n/a | sliced = multislice(lst, slices) |
|---|
| 1712 | n/a | except Exception as e: |
|---|
| 1713 | n/a | listerr = e.__class__ |
|---|
| 1714 | n/a | |
|---|
| 1715 | n/a | nderr = None |
|---|
| 1716 | n/a | try: |
|---|
| 1717 | n/a | ndsliced = nd[slices] |
|---|
| 1718 | n/a | except Exception as e: |
|---|
| 1719 | n/a | nderr = e.__class__ |
|---|
| 1720 | n/a | |
|---|
| 1721 | n/a | if nderr or listerr: |
|---|
| 1722 | n/a | self.assertIs(nderr, listerr) |
|---|
| 1723 | n/a | else: |
|---|
| 1724 | n/a | self.assertEqual(ndsliced.tolist(), sliced) |
|---|
| 1725 | n/a | |
|---|
| 1726 | n/a | def test_ndarray_slice_redundant_suboffsets(self): |
|---|
| 1727 | n/a | shape_t = (2, 3, 5, 2) |
|---|
| 1728 | n/a | ndim = len(shape_t) |
|---|
| 1729 | n/a | nitems = prod(shape_t) |
|---|
| 1730 | n/a | for shape in permutations(shape_t): |
|---|
| 1731 | n/a | |
|---|
| 1732 | n/a | fmt, items, _ = randitems(nitems) |
|---|
| 1733 | n/a | itemsize = struct.calcsize(fmt) |
|---|
| 1734 | n/a | |
|---|
| 1735 | n/a | nd = ndarray(items, shape=shape, format=fmt) |
|---|
| 1736 | n/a | nd.add_suboffsets() |
|---|
| 1737 | n/a | ex = ndarray(items, shape=shape, format=fmt) |
|---|
| 1738 | n/a | ex.add_suboffsets() |
|---|
| 1739 | n/a | mv = memoryview(ex) |
|---|
| 1740 | n/a | lst = carray(items, shape) |
|---|
| 1741 | n/a | |
|---|
| 1742 | n/a | for slices in rslices_ndim(ndim, shape): |
|---|
| 1743 | n/a | |
|---|
| 1744 | n/a | listerr = None |
|---|
| 1745 | n/a | try: |
|---|
| 1746 | n/a | sliced = multislice(lst, slices) |
|---|
| 1747 | n/a | except Exception as e: |
|---|
| 1748 | n/a | listerr = e.__class__ |
|---|
| 1749 | n/a | |
|---|
| 1750 | n/a | nderr = None |
|---|
| 1751 | n/a | try: |
|---|
| 1752 | n/a | ndsliced = nd[slices] |
|---|
| 1753 | n/a | except Exception as e: |
|---|
| 1754 | n/a | nderr = e.__class__ |
|---|
| 1755 | n/a | |
|---|
| 1756 | n/a | if nderr or listerr: |
|---|
| 1757 | n/a | self.assertIs(nderr, listerr) |
|---|
| 1758 | n/a | else: |
|---|
| 1759 | n/a | self.assertEqual(ndsliced.tolist(), sliced) |
|---|
| 1760 | n/a | |
|---|
| 1761 | n/a | def test_ndarray_slice_assign_single(self): |
|---|
| 1762 | n/a | for fmt, items, _ in iter_format(5): |
|---|
| 1763 | n/a | for lslice in genslices(5): |
|---|
| 1764 | n/a | for rslice in genslices(5): |
|---|
| 1765 | n/a | for flags in (0, ND_PIL): |
|---|
| 1766 | n/a | |
|---|
| 1767 | n/a | f = flags|ND_WRITABLE |
|---|
| 1768 | n/a | nd = ndarray(items, shape=[5], format=fmt, flags=f) |
|---|
| 1769 | n/a | ex = ndarray(items, shape=[5], format=fmt, flags=f) |
|---|
| 1770 | n/a | mv = memoryview(ex) |
|---|
| 1771 | n/a | |
|---|
| 1772 | n/a | lsterr = None |
|---|
| 1773 | n/a | diff_structure = None |
|---|
| 1774 | n/a | lst = items[:] |
|---|
| 1775 | n/a | try: |
|---|
| 1776 | n/a | lval = lst[lslice] |
|---|
| 1777 | n/a | rval = lst[rslice] |
|---|
| 1778 | n/a | lst[lslice] = lst[rslice] |
|---|
| 1779 | n/a | diff_structure = len(lval) != len(rval) |
|---|
| 1780 | n/a | except Exception as e: |
|---|
| 1781 | n/a | lsterr = e.__class__ |
|---|
| 1782 | n/a | |
|---|
| 1783 | n/a | nderr = None |
|---|
| 1784 | n/a | try: |
|---|
| 1785 | n/a | nd[lslice] = nd[rslice] |
|---|
| 1786 | n/a | except Exception as e: |
|---|
| 1787 | n/a | nderr = e.__class__ |
|---|
| 1788 | n/a | |
|---|
| 1789 | n/a | if diff_structure: # ndarray cannot change shape |
|---|
| 1790 | n/a | self.assertIs(nderr, ValueError) |
|---|
| 1791 | n/a | else: |
|---|
| 1792 | n/a | self.assertEqual(nd.tolist(), lst) |
|---|
| 1793 | n/a | self.assertIs(nderr, lsterr) |
|---|
| 1794 | n/a | |
|---|
| 1795 | n/a | if not is_memoryview_format(fmt): |
|---|
| 1796 | n/a | continue |
|---|
| 1797 | n/a | |
|---|
| 1798 | n/a | mverr = None |
|---|
| 1799 | n/a | try: |
|---|
| 1800 | n/a | mv[lslice] = mv[rslice] |
|---|
| 1801 | n/a | except Exception as e: |
|---|
| 1802 | n/a | mverr = e.__class__ |
|---|
| 1803 | n/a | |
|---|
| 1804 | n/a | if diff_structure: # memoryview cannot change shape |
|---|
| 1805 | n/a | self.assertIs(mverr, ValueError) |
|---|
| 1806 | n/a | else: |
|---|
| 1807 | n/a | self.assertEqual(mv.tolist(), lst) |
|---|
| 1808 | n/a | self.assertEqual(mv, nd) |
|---|
| 1809 | n/a | self.assertIs(mverr, lsterr) |
|---|
| 1810 | n/a | self.verify(mv, obj=ex, |
|---|
| 1811 | n/a | itemsize=nd.itemsize, fmt=fmt, readonly=0, |
|---|
| 1812 | n/a | ndim=nd.ndim, shape=nd.shape, strides=nd.strides, |
|---|
| 1813 | n/a | lst=nd.tolist()) |
|---|
| 1814 | n/a | |
|---|
| 1815 | n/a | def test_ndarray_slice_assign_multidim(self): |
|---|
| 1816 | n/a | shape_t = (2, 3, 5) |
|---|
| 1817 | n/a | ndim = len(shape_t) |
|---|
| 1818 | n/a | nitems = prod(shape_t) |
|---|
| 1819 | n/a | for shape in permutations(shape_t): |
|---|
| 1820 | n/a | |
|---|
| 1821 | n/a | fmt, items, _ = randitems(nitems) |
|---|
| 1822 | n/a | |
|---|
| 1823 | n/a | for flags in (0, ND_PIL): |
|---|
| 1824 | n/a | for _ in range(ITERATIONS): |
|---|
| 1825 | n/a | lslices, rslices = randslice_from_shape(ndim, shape) |
|---|
| 1826 | n/a | |
|---|
| 1827 | n/a | nd = ndarray(items, shape=shape, format=fmt, |
|---|
| 1828 | n/a | flags=flags|ND_WRITABLE) |
|---|
| 1829 | n/a | lst = carray(items, shape) |
|---|
| 1830 | n/a | |
|---|
| 1831 | n/a | listerr = None |
|---|
| 1832 | n/a | try: |
|---|
| 1833 | n/a | result = multislice_assign(lst, lst, lslices, rslices) |
|---|
| 1834 | n/a | except Exception as e: |
|---|
| 1835 | n/a | listerr = e.__class__ |
|---|
| 1836 | n/a | |
|---|
| 1837 | n/a | nderr = None |
|---|
| 1838 | n/a | try: |
|---|
| 1839 | n/a | nd[lslices] = nd[rslices] |
|---|
| 1840 | n/a | except Exception as e: |
|---|
| 1841 | n/a | nderr = e.__class__ |
|---|
| 1842 | n/a | |
|---|
| 1843 | n/a | if nderr or listerr: |
|---|
| 1844 | n/a | self.assertIs(nderr, listerr) |
|---|
| 1845 | n/a | else: |
|---|
| 1846 | n/a | self.assertEqual(nd.tolist(), result) |
|---|
| 1847 | n/a | |
|---|
| 1848 | n/a | def test_ndarray_random(self): |
|---|
| 1849 | n/a | # construction of valid arrays |
|---|
| 1850 | n/a | for _ in range(ITERATIONS): |
|---|
| 1851 | n/a | for fmt in fmtdict['@']: |
|---|
| 1852 | n/a | itemsize = struct.calcsize(fmt) |
|---|
| 1853 | n/a | |
|---|
| 1854 | n/a | t = rand_structure(itemsize, True, maxdim=MAXDIM, |
|---|
| 1855 | n/a | maxshape=MAXSHAPE) |
|---|
| 1856 | n/a | self.assertTrue(verify_structure(*t)) |
|---|
| 1857 | n/a | items = randitems_from_structure(fmt, t) |
|---|
| 1858 | n/a | |
|---|
| 1859 | n/a | x = ndarray_from_structure(items, fmt, t) |
|---|
| 1860 | n/a | xlist = x.tolist() |
|---|
| 1861 | n/a | |
|---|
| 1862 | n/a | mv = memoryview(x) |
|---|
| 1863 | n/a | if is_memoryview_format(fmt): |
|---|
| 1864 | n/a | mvlist = mv.tolist() |
|---|
| 1865 | n/a | self.assertEqual(mvlist, xlist) |
|---|
| 1866 | n/a | |
|---|
| 1867 | n/a | if t[2] > 0: |
|---|
| 1868 | n/a | # ndim > 0: test against suboffsets representation. |
|---|
| 1869 | n/a | y = ndarray_from_structure(items, fmt, t, flags=ND_PIL) |
|---|
| 1870 | n/a | ylist = y.tolist() |
|---|
| 1871 | n/a | self.assertEqual(xlist, ylist) |
|---|
| 1872 | n/a | |
|---|
| 1873 | n/a | mv = memoryview(y) |
|---|
| 1874 | n/a | if is_memoryview_format(fmt): |
|---|
| 1875 | n/a | self.assertEqual(mv, y) |
|---|
| 1876 | n/a | mvlist = mv.tolist() |
|---|
| 1877 | n/a | self.assertEqual(mvlist, ylist) |
|---|
| 1878 | n/a | |
|---|
| 1879 | n/a | if numpy_array: |
|---|
| 1880 | n/a | shape = t[3] |
|---|
| 1881 | n/a | if 0 in shape: |
|---|
| 1882 | n/a | continue # http://projects.scipy.org/numpy/ticket/1910 |
|---|
| 1883 | n/a | z = numpy_array_from_structure(items, fmt, t) |
|---|
| 1884 | n/a | self.verify(x, obj=None, |
|---|
| 1885 | n/a | itemsize=z.itemsize, fmt=fmt, readonly=0, |
|---|
| 1886 | n/a | ndim=z.ndim, shape=z.shape, strides=z.strides, |
|---|
| 1887 | n/a | lst=z.tolist()) |
|---|
| 1888 | n/a | |
|---|
| 1889 | n/a | def test_ndarray_random_invalid(self): |
|---|
| 1890 | n/a | # exceptions during construction of invalid arrays |
|---|
| 1891 | n/a | for _ in range(ITERATIONS): |
|---|
| 1892 | n/a | for fmt in fmtdict['@']: |
|---|
| 1893 | n/a | itemsize = struct.calcsize(fmt) |
|---|
| 1894 | n/a | |
|---|
| 1895 | n/a | t = rand_structure(itemsize, False, maxdim=MAXDIM, |
|---|
| 1896 | n/a | maxshape=MAXSHAPE) |
|---|
| 1897 | n/a | self.assertFalse(verify_structure(*t)) |
|---|
| 1898 | n/a | items = randitems_from_structure(fmt, t) |
|---|
| 1899 | n/a | |
|---|
| 1900 | n/a | nderr = False |
|---|
| 1901 | n/a | try: |
|---|
| 1902 | n/a | x = ndarray_from_structure(items, fmt, t) |
|---|
| 1903 | n/a | except Exception as e: |
|---|
| 1904 | n/a | nderr = e.__class__ |
|---|
| 1905 | n/a | self.assertTrue(nderr) |
|---|
| 1906 | n/a | |
|---|
| 1907 | n/a | if numpy_array: |
|---|
| 1908 | n/a | numpy_err = False |
|---|
| 1909 | n/a | try: |
|---|
| 1910 | n/a | y = numpy_array_from_structure(items, fmt, t) |
|---|
| 1911 | n/a | except Exception as e: |
|---|
| 1912 | n/a | numpy_err = e.__class__ |
|---|
| 1913 | n/a | |
|---|
| 1914 | n/a | if 0: # http://projects.scipy.org/numpy/ticket/1910 |
|---|
| 1915 | n/a | self.assertTrue(numpy_err) |
|---|
| 1916 | n/a | |
|---|
| 1917 | n/a | def test_ndarray_random_slice_assign(self): |
|---|
| 1918 | n/a | # valid slice assignments |
|---|
| 1919 | n/a | for _ in range(ITERATIONS): |
|---|
| 1920 | n/a | for fmt in fmtdict['@']: |
|---|
| 1921 | n/a | itemsize = struct.calcsize(fmt) |
|---|
| 1922 | n/a | |
|---|
| 1923 | n/a | lshape, rshape, lslices, rslices = \ |
|---|
| 1924 | n/a | rand_aligned_slices(maxdim=MAXDIM, maxshape=MAXSHAPE) |
|---|
| 1925 | n/a | tl = rand_structure(itemsize, True, shape=lshape) |
|---|
| 1926 | n/a | tr = rand_structure(itemsize, True, shape=rshape) |
|---|
| 1927 | n/a | self.assertTrue(verify_structure(*tl)) |
|---|
| 1928 | n/a | self.assertTrue(verify_structure(*tr)) |
|---|
| 1929 | n/a | litems = randitems_from_structure(fmt, tl) |
|---|
| 1930 | n/a | ritems = randitems_from_structure(fmt, tr) |
|---|
| 1931 | n/a | |
|---|
| 1932 | n/a | xl = ndarray_from_structure(litems, fmt, tl) |
|---|
| 1933 | n/a | xr = ndarray_from_structure(ritems, fmt, tr) |
|---|
| 1934 | n/a | xl[lslices] = xr[rslices] |
|---|
| 1935 | n/a | xllist = xl.tolist() |
|---|
| 1936 | n/a | xrlist = xr.tolist() |
|---|
| 1937 | n/a | |
|---|
| 1938 | n/a | ml = memoryview(xl) |
|---|
| 1939 | n/a | mr = memoryview(xr) |
|---|
| 1940 | n/a | self.assertEqual(ml.tolist(), xllist) |
|---|
| 1941 | n/a | self.assertEqual(mr.tolist(), xrlist) |
|---|
| 1942 | n/a | |
|---|
| 1943 | n/a | if tl[2] > 0 and tr[2] > 0: |
|---|
| 1944 | n/a | # ndim > 0: test against suboffsets representation. |
|---|
| 1945 | n/a | yl = ndarray_from_structure(litems, fmt, tl, flags=ND_PIL) |
|---|
| 1946 | n/a | yr = ndarray_from_structure(ritems, fmt, tr, flags=ND_PIL) |
|---|
| 1947 | n/a | yl[lslices] = yr[rslices] |
|---|
| 1948 | n/a | yllist = yl.tolist() |
|---|
| 1949 | n/a | yrlist = yr.tolist() |
|---|
| 1950 | n/a | self.assertEqual(xllist, yllist) |
|---|
| 1951 | n/a | self.assertEqual(xrlist, yrlist) |
|---|
| 1952 | n/a | |
|---|
| 1953 | n/a | ml = memoryview(yl) |
|---|
| 1954 | n/a | mr = memoryview(yr) |
|---|
| 1955 | n/a | self.assertEqual(ml.tolist(), yllist) |
|---|
| 1956 | n/a | self.assertEqual(mr.tolist(), yrlist) |
|---|
| 1957 | n/a | |
|---|
| 1958 | n/a | if numpy_array: |
|---|
| 1959 | n/a | if 0 in lshape or 0 in rshape: |
|---|
| 1960 | n/a | continue # http://projects.scipy.org/numpy/ticket/1910 |
|---|
| 1961 | n/a | |
|---|
| 1962 | n/a | zl = numpy_array_from_structure(litems, fmt, tl) |
|---|
| 1963 | n/a | zr = numpy_array_from_structure(ritems, fmt, tr) |
|---|
| 1964 | n/a | zl[lslices] = zr[rslices] |
|---|
| 1965 | n/a | |
|---|
| 1966 | n/a | if not is_overlapping(tl) and not is_overlapping(tr): |
|---|
| 1967 | n/a | # Slice assignment of overlapping structures |
|---|
| 1968 | n/a | # is undefined in NumPy. |
|---|
| 1969 | n/a | self.verify(xl, obj=None, |
|---|
| 1970 | n/a | itemsize=zl.itemsize, fmt=fmt, readonly=0, |
|---|
| 1971 | n/a | ndim=zl.ndim, shape=zl.shape, |
|---|
| 1972 | n/a | strides=zl.strides, lst=zl.tolist()) |
|---|
| 1973 | n/a | |
|---|
| 1974 | n/a | self.verify(xr, obj=None, |
|---|
| 1975 | n/a | itemsize=zr.itemsize, fmt=fmt, readonly=0, |
|---|
| 1976 | n/a | ndim=zr.ndim, shape=zr.shape, |
|---|
| 1977 | n/a | strides=zr.strides, lst=zr.tolist()) |
|---|
| 1978 | n/a | |
|---|
| 1979 | n/a | def test_ndarray_re_export(self): |
|---|
| 1980 | n/a | items = [1,2,3,4,5,6,7,8,9,10,11,12] |
|---|
| 1981 | n/a | |
|---|
| 1982 | n/a | nd = ndarray(items, shape=[3,4], flags=ND_PIL) |
|---|
| 1983 | n/a | ex = ndarray(nd) |
|---|
| 1984 | n/a | |
|---|
| 1985 | n/a | self.assertTrue(ex.flags & ND_PIL) |
|---|
| 1986 | n/a | self.assertIs(ex.obj, nd) |
|---|
| 1987 | n/a | self.assertEqual(ex.suboffsets, (0, -1)) |
|---|
| 1988 | n/a | self.assertFalse(ex.c_contiguous) |
|---|
| 1989 | n/a | self.assertFalse(ex.f_contiguous) |
|---|
| 1990 | n/a | self.assertFalse(ex.contiguous) |
|---|
| 1991 | n/a | |
|---|
| 1992 | n/a | def test_ndarray_zero_shape(self): |
|---|
| 1993 | n/a | # zeros in shape |
|---|
| 1994 | n/a | for flags in (0, ND_PIL): |
|---|
| 1995 | n/a | nd = ndarray([1,2,3], shape=[0], flags=flags) |
|---|
| 1996 | n/a | mv = memoryview(nd) |
|---|
| 1997 | n/a | self.assertEqual(mv, nd) |
|---|
| 1998 | n/a | self.assertEqual(nd.tolist(), []) |
|---|
| 1999 | n/a | self.assertEqual(mv.tolist(), []) |
|---|
| 2000 | n/a | |
|---|
| 2001 | n/a | nd = ndarray([1,2,3], shape=[0,3,3], flags=flags) |
|---|
| 2002 | n/a | self.assertEqual(nd.tolist(), []) |
|---|
| 2003 | n/a | |
|---|
| 2004 | n/a | nd = ndarray([1,2,3], shape=[3,0,3], flags=flags) |
|---|
| 2005 | n/a | self.assertEqual(nd.tolist(), [[], [], []]) |
|---|
| 2006 | n/a | |
|---|
| 2007 | n/a | nd = ndarray([1,2,3], shape=[3,3,0], flags=flags) |
|---|
| 2008 | n/a | self.assertEqual(nd.tolist(), |
|---|
| 2009 | n/a | [[[], [], []], [[], [], []], [[], [], []]]) |
|---|
| 2010 | n/a | |
|---|
| 2011 | n/a | def test_ndarray_zero_strides(self): |
|---|
| 2012 | n/a | # zero strides |
|---|
| 2013 | n/a | for flags in (0, ND_PIL): |
|---|
| 2014 | n/a | nd = ndarray([1], shape=[5], strides=[0], flags=flags) |
|---|
| 2015 | n/a | mv = memoryview(nd) |
|---|
| 2016 | n/a | self.assertEqual(mv, nd) |
|---|
| 2017 | n/a | self.assertEqual(nd.tolist(), [1, 1, 1, 1, 1]) |
|---|
| 2018 | n/a | self.assertEqual(mv.tolist(), [1, 1, 1, 1, 1]) |
|---|
| 2019 | n/a | |
|---|
| 2020 | n/a | def test_ndarray_offset(self): |
|---|
| 2021 | n/a | nd = ndarray(list(range(20)), shape=[3], offset=7) |
|---|
| 2022 | n/a | self.assertEqual(nd.offset, 7) |
|---|
| 2023 | n/a | self.assertEqual(nd.tolist(), [7,8,9]) |
|---|
| 2024 | n/a | |
|---|
| 2025 | n/a | def test_ndarray_memoryview_from_buffer(self): |
|---|
| 2026 | n/a | for flags in (0, ND_PIL): |
|---|
| 2027 | n/a | nd = ndarray(list(range(3)), shape=[3], flags=flags) |
|---|
| 2028 | n/a | m = nd.memoryview_from_buffer() |
|---|
| 2029 | n/a | self.assertEqual(m, nd) |
|---|
| 2030 | n/a | |
|---|
| 2031 | n/a | def test_ndarray_get_pointer(self): |
|---|
| 2032 | n/a | for flags in (0, ND_PIL): |
|---|
| 2033 | n/a | nd = ndarray(list(range(3)), shape=[3], flags=flags) |
|---|
| 2034 | n/a | for i in range(3): |
|---|
| 2035 | n/a | self.assertEqual(nd[i], get_pointer(nd, [i])) |
|---|
| 2036 | n/a | |
|---|
| 2037 | n/a | def test_ndarray_tolist_null_strides(self): |
|---|
| 2038 | n/a | ex = ndarray(list(range(20)), shape=[2,2,5]) |
|---|
| 2039 | n/a | |
|---|
| 2040 | n/a | nd = ndarray(ex, getbuf=PyBUF_ND|PyBUF_FORMAT) |
|---|
| 2041 | n/a | self.assertEqual(nd.tolist(), ex.tolist()) |
|---|
| 2042 | n/a | |
|---|
| 2043 | n/a | m = memoryview(ex) |
|---|
| 2044 | n/a | self.assertEqual(m.tolist(), ex.tolist()) |
|---|
| 2045 | n/a | |
|---|
| 2046 | n/a | def test_ndarray_cmp_contig(self): |
|---|
| 2047 | n/a | |
|---|
| 2048 | n/a | self.assertFalse(cmp_contig(b"123", b"456")) |
|---|
| 2049 | n/a | |
|---|
| 2050 | n/a | x = ndarray(list(range(12)), shape=[3,4]) |
|---|
| 2051 | n/a | y = ndarray(list(range(12)), shape=[4,3]) |
|---|
| 2052 | n/a | self.assertFalse(cmp_contig(x, y)) |
|---|
| 2053 | n/a | |
|---|
| 2054 | n/a | x = ndarray([1], shape=[1], format="B") |
|---|
| 2055 | n/a | self.assertTrue(cmp_contig(x, b'\x01')) |
|---|
| 2056 | n/a | self.assertTrue(cmp_contig(b'\x01', x)) |
|---|
| 2057 | n/a | |
|---|
| 2058 | n/a | def test_ndarray_hash(self): |
|---|
| 2059 | n/a | |
|---|
| 2060 | n/a | a = array.array('L', [1,2,3]) |
|---|
| 2061 | n/a | nd = ndarray(a) |
|---|
| 2062 | n/a | self.assertRaises(ValueError, hash, nd) |
|---|
| 2063 | n/a | |
|---|
| 2064 | n/a | # one-dimensional |
|---|
| 2065 | n/a | b = bytes(list(range(12))) |
|---|
| 2066 | n/a | |
|---|
| 2067 | n/a | nd = ndarray(list(range(12)), shape=[12]) |
|---|
| 2068 | n/a | self.assertEqual(hash(nd), hash(b)) |
|---|
| 2069 | n/a | |
|---|
| 2070 | n/a | # C-contiguous |
|---|
| 2071 | n/a | nd = ndarray(list(range(12)), shape=[3,4]) |
|---|
| 2072 | n/a | self.assertEqual(hash(nd), hash(b)) |
|---|
| 2073 | n/a | |
|---|
| 2074 | n/a | nd = ndarray(list(range(12)), shape=[3,2,2]) |
|---|
| 2075 | n/a | self.assertEqual(hash(nd), hash(b)) |
|---|
| 2076 | n/a | |
|---|
| 2077 | n/a | # Fortran contiguous |
|---|
| 2078 | n/a | b = bytes(transpose(list(range(12)), shape=[4,3])) |
|---|
| 2079 | n/a | nd = ndarray(list(range(12)), shape=[3,4], flags=ND_FORTRAN) |
|---|
| 2080 | n/a | self.assertEqual(hash(nd), hash(b)) |
|---|
| 2081 | n/a | |
|---|
| 2082 | n/a | b = bytes(transpose(list(range(12)), shape=[2,3,2])) |
|---|
| 2083 | n/a | nd = ndarray(list(range(12)), shape=[2,3,2], flags=ND_FORTRAN) |
|---|
| 2084 | n/a | self.assertEqual(hash(nd), hash(b)) |
|---|
| 2085 | n/a | |
|---|
| 2086 | n/a | # suboffsets |
|---|
| 2087 | n/a | b = bytes(list(range(12))) |
|---|
| 2088 | n/a | nd = ndarray(list(range(12)), shape=[2,2,3], flags=ND_PIL) |
|---|
| 2089 | n/a | self.assertEqual(hash(nd), hash(b)) |
|---|
| 2090 | n/a | |
|---|
| 2091 | n/a | # non-byte formats |
|---|
| 2092 | n/a | nd = ndarray(list(range(12)), shape=[2,2,3], format='L') |
|---|
| 2093 | n/a | self.assertEqual(hash(nd), hash(nd.tobytes())) |
|---|
| 2094 | n/a | |
|---|
| 2095 | n/a | def test_py_buffer_to_contiguous(self): |
|---|
| 2096 | n/a | |
|---|
| 2097 | n/a | # The requests are used in _testbuffer.c:py_buffer_to_contiguous |
|---|
| 2098 | n/a | # to generate buffers without full information for testing. |
|---|
| 2099 | n/a | requests = ( |
|---|
| 2100 | n/a | # distinct flags |
|---|
| 2101 | n/a | PyBUF_INDIRECT, PyBUF_STRIDES, PyBUF_ND, PyBUF_SIMPLE, |
|---|
| 2102 | n/a | # compound requests |
|---|
| 2103 | n/a | PyBUF_FULL, PyBUF_FULL_RO, |
|---|
| 2104 | n/a | PyBUF_RECORDS, PyBUF_RECORDS_RO, |
|---|
| 2105 | n/a | PyBUF_STRIDED, PyBUF_STRIDED_RO, |
|---|
| 2106 | n/a | PyBUF_CONTIG, PyBUF_CONTIG_RO, |
|---|
| 2107 | n/a | ) |
|---|
| 2108 | n/a | |
|---|
| 2109 | n/a | # no buffer interface |
|---|
| 2110 | n/a | self.assertRaises(TypeError, py_buffer_to_contiguous, {}, 'F', |
|---|
| 2111 | n/a | PyBUF_FULL_RO) |
|---|
| 2112 | n/a | |
|---|
| 2113 | n/a | # scalar, read-only request |
|---|
| 2114 | n/a | nd = ndarray(9, shape=(), format="L", flags=ND_WRITABLE) |
|---|
| 2115 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 2116 | n/a | for request in requests: |
|---|
| 2117 | n/a | b = py_buffer_to_contiguous(nd, order, request) |
|---|
| 2118 | n/a | self.assertEqual(b, nd.tobytes()) |
|---|
| 2119 | n/a | |
|---|
| 2120 | n/a | # zeros in shape |
|---|
| 2121 | n/a | nd = ndarray([1], shape=[0], format="L", flags=ND_WRITABLE) |
|---|
| 2122 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 2123 | n/a | for request in requests: |
|---|
| 2124 | n/a | b = py_buffer_to_contiguous(nd, order, request) |
|---|
| 2125 | n/a | self.assertEqual(b, b'') |
|---|
| 2126 | n/a | |
|---|
| 2127 | n/a | nd = ndarray(list(range(8)), shape=[2, 0, 7], format="L", |
|---|
| 2128 | n/a | flags=ND_WRITABLE) |
|---|
| 2129 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 2130 | n/a | for request in requests: |
|---|
| 2131 | n/a | b = py_buffer_to_contiguous(nd, order, request) |
|---|
| 2132 | n/a | self.assertEqual(b, b'') |
|---|
| 2133 | n/a | |
|---|
| 2134 | n/a | ### One-dimensional arrays are trivial, since Fortran and C order |
|---|
| 2135 | n/a | ### are the same. |
|---|
| 2136 | n/a | |
|---|
| 2137 | n/a | # one-dimensional |
|---|
| 2138 | n/a | for f in [0, ND_FORTRAN]: |
|---|
| 2139 | n/a | nd = ndarray([1], shape=[1], format="h", flags=f|ND_WRITABLE) |
|---|
| 2140 | n/a | ndbytes = nd.tobytes() |
|---|
| 2141 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 2142 | n/a | for request in requests: |
|---|
| 2143 | n/a | b = py_buffer_to_contiguous(nd, order, request) |
|---|
| 2144 | n/a | self.assertEqual(b, ndbytes) |
|---|
| 2145 | n/a | |
|---|
| 2146 | n/a | nd = ndarray([1, 2, 3], shape=[3], format="b", flags=f|ND_WRITABLE) |
|---|
| 2147 | n/a | ndbytes = nd.tobytes() |
|---|
| 2148 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 2149 | n/a | for request in requests: |
|---|
| 2150 | n/a | b = py_buffer_to_contiguous(nd, order, request) |
|---|
| 2151 | n/a | self.assertEqual(b, ndbytes) |
|---|
| 2152 | n/a | |
|---|
| 2153 | n/a | # one-dimensional, non-contiguous input |
|---|
| 2154 | n/a | nd = ndarray([1, 2, 3], shape=[2], strides=[2], flags=ND_WRITABLE) |
|---|
| 2155 | n/a | ndbytes = nd.tobytes() |
|---|
| 2156 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 2157 | n/a | for request in [PyBUF_STRIDES, PyBUF_FULL]: |
|---|
| 2158 | n/a | b = py_buffer_to_contiguous(nd, order, request) |
|---|
| 2159 | n/a | self.assertEqual(b, ndbytes) |
|---|
| 2160 | n/a | |
|---|
| 2161 | n/a | nd = nd[::-1] |
|---|
| 2162 | n/a | ndbytes = nd.tobytes() |
|---|
| 2163 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 2164 | n/a | for request in requests: |
|---|
| 2165 | n/a | try: |
|---|
| 2166 | n/a | b = py_buffer_to_contiguous(nd, order, request) |
|---|
| 2167 | n/a | except BufferError: |
|---|
| 2168 | n/a | continue |
|---|
| 2169 | n/a | self.assertEqual(b, ndbytes) |
|---|
| 2170 | n/a | |
|---|
| 2171 | n/a | ### |
|---|
| 2172 | n/a | ### Multi-dimensional arrays: |
|---|
| 2173 | n/a | ### |
|---|
| 2174 | n/a | ### The goal here is to preserve the logical representation of the |
|---|
| 2175 | n/a | ### input array but change the physical representation if necessary. |
|---|
| 2176 | n/a | ### |
|---|
| 2177 | n/a | ### _testbuffer example: |
|---|
| 2178 | n/a | ### ==================== |
|---|
| 2179 | n/a | ### |
|---|
| 2180 | n/a | ### C input array: |
|---|
| 2181 | n/a | ### -------------- |
|---|
| 2182 | n/a | ### >>> nd = ndarray(list(range(12)), shape=[3, 4]) |
|---|
| 2183 | n/a | ### >>> nd.tolist() |
|---|
| 2184 | n/a | ### [[0, 1, 2, 3], |
|---|
| 2185 | n/a | ### [4, 5, 6, 7], |
|---|
| 2186 | n/a | ### [8, 9, 10, 11]] |
|---|
| 2187 | n/a | ### |
|---|
| 2188 | n/a | ### Fortran output: |
|---|
| 2189 | n/a | ### --------------- |
|---|
| 2190 | n/a | ### >>> py_buffer_to_contiguous(nd, 'F', PyBUF_FULL_RO) |
|---|
| 2191 | n/a | ### >>> b'\x00\x04\x08\x01\x05\t\x02\x06\n\x03\x07\x0b' |
|---|
| 2192 | n/a | ### |
|---|
| 2193 | n/a | ### The return value corresponds to this input list for |
|---|
| 2194 | n/a | ### _testbuffer's ndarray: |
|---|
| 2195 | n/a | ### >>> nd = ndarray([0,4,8,1,5,9,2,6,10,3,7,11], shape=[3,4], |
|---|
| 2196 | n/a | ### flags=ND_FORTRAN) |
|---|
| 2197 | n/a | ### >>> nd.tolist() |
|---|
| 2198 | n/a | ### [[0, 1, 2, 3], |
|---|
| 2199 | n/a | ### [4, 5, 6, 7], |
|---|
| 2200 | n/a | ### [8, 9, 10, 11]] |
|---|
| 2201 | n/a | ### |
|---|
| 2202 | n/a | ### The logical array is the same, but the values in memory are now |
|---|
| 2203 | n/a | ### in Fortran order. |
|---|
| 2204 | n/a | ### |
|---|
| 2205 | n/a | ### NumPy example: |
|---|
| 2206 | n/a | ### ============== |
|---|
| 2207 | n/a | ### _testbuffer's ndarray takes lists to initialize the memory. |
|---|
| 2208 | n/a | ### Here's the same sequence in NumPy: |
|---|
| 2209 | n/a | ### |
|---|
| 2210 | n/a | ### C input: |
|---|
| 2211 | n/a | ### -------- |
|---|
| 2212 | n/a | ### >>> nd = ndarray(buffer=bytearray(list(range(12))), |
|---|
| 2213 | n/a | ### shape=[3, 4], dtype='B') |
|---|
| 2214 | n/a | ### >>> nd |
|---|
| 2215 | n/a | ### array([[ 0, 1, 2, 3], |
|---|
| 2216 | n/a | ### [ 4, 5, 6, 7], |
|---|
| 2217 | n/a | ### [ 8, 9, 10, 11]], dtype=uint8) |
|---|
| 2218 | n/a | ### |
|---|
| 2219 | n/a | ### Fortran output: |
|---|
| 2220 | n/a | ### --------------- |
|---|
| 2221 | n/a | ### >>> fortran_buf = nd.tostring(order='F') |
|---|
| 2222 | n/a | ### >>> fortran_buf |
|---|
| 2223 | n/a | ### b'\x00\x04\x08\x01\x05\t\x02\x06\n\x03\x07\x0b' |
|---|
| 2224 | n/a | ### |
|---|
| 2225 | n/a | ### >>> nd = ndarray(buffer=fortran_buf, shape=[3, 4], |
|---|
| 2226 | n/a | ### dtype='B', order='F') |
|---|
| 2227 | n/a | ### |
|---|
| 2228 | n/a | ### >>> nd |
|---|
| 2229 | n/a | ### array([[ 0, 1, 2, 3], |
|---|
| 2230 | n/a | ### [ 4, 5, 6, 7], |
|---|
| 2231 | n/a | ### [ 8, 9, 10, 11]], dtype=uint8) |
|---|
| 2232 | n/a | ### |
|---|
| 2233 | n/a | |
|---|
| 2234 | n/a | # multi-dimensional, contiguous input |
|---|
| 2235 | n/a | lst = list(range(12)) |
|---|
| 2236 | n/a | for f in [0, ND_FORTRAN]: |
|---|
| 2237 | n/a | nd = ndarray(lst, shape=[3, 4], flags=f|ND_WRITABLE) |
|---|
| 2238 | n/a | if numpy_array: |
|---|
| 2239 | n/a | na = numpy_array(buffer=bytearray(lst), |
|---|
| 2240 | n/a | shape=[3, 4], dtype='B', |
|---|
| 2241 | n/a | order='C' if f == 0 else 'F') |
|---|
| 2242 | n/a | |
|---|
| 2243 | n/a | # 'C' request |
|---|
| 2244 | n/a | if f == ND_FORTRAN: # 'F' to 'C' |
|---|
| 2245 | n/a | x = ndarray(transpose(lst, [4, 3]), shape=[3, 4], |
|---|
| 2246 | n/a | flags=ND_WRITABLE) |
|---|
| 2247 | n/a | expected = x.tobytes() |
|---|
| 2248 | n/a | else: |
|---|
| 2249 | n/a | expected = nd.tobytes() |
|---|
| 2250 | n/a | for request in requests: |
|---|
| 2251 | n/a | try: |
|---|
| 2252 | n/a | b = py_buffer_to_contiguous(nd, 'C', request) |
|---|
| 2253 | n/a | except BufferError: |
|---|
| 2254 | n/a | continue |
|---|
| 2255 | n/a | |
|---|
| 2256 | n/a | self.assertEqual(b, expected) |
|---|
| 2257 | n/a | |
|---|
| 2258 | n/a | # Check that output can be used as the basis for constructing |
|---|
| 2259 | n/a | # a C array that is logically identical to the input array. |
|---|
| 2260 | n/a | y = ndarray([v for v in b], shape=[3, 4], flags=ND_WRITABLE) |
|---|
| 2261 | n/a | self.assertEqual(memoryview(y), memoryview(nd)) |
|---|
| 2262 | n/a | |
|---|
| 2263 | n/a | if numpy_array: |
|---|
| 2264 | n/a | self.assertEqual(b, na.tostring(order='C')) |
|---|
| 2265 | n/a | |
|---|
| 2266 | n/a | # 'F' request |
|---|
| 2267 | n/a | if f == 0: # 'C' to 'F' |
|---|
| 2268 | n/a | x = ndarray(transpose(lst, [3, 4]), shape=[4, 3], |
|---|
| 2269 | n/a | flags=ND_WRITABLE) |
|---|
| 2270 | n/a | else: |
|---|
| 2271 | n/a | x = ndarray(lst, shape=[3, 4], flags=ND_WRITABLE) |
|---|
| 2272 | n/a | expected = x.tobytes() |
|---|
| 2273 | n/a | for request in [PyBUF_FULL, PyBUF_FULL_RO, PyBUF_INDIRECT, |
|---|
| 2274 | n/a | PyBUF_STRIDES, PyBUF_ND]: |
|---|
| 2275 | n/a | try: |
|---|
| 2276 | n/a | b = py_buffer_to_contiguous(nd, 'F', request) |
|---|
| 2277 | n/a | except BufferError: |
|---|
| 2278 | n/a | continue |
|---|
| 2279 | n/a | self.assertEqual(b, expected) |
|---|
| 2280 | n/a | |
|---|
| 2281 | n/a | # Check that output can be used as the basis for constructing |
|---|
| 2282 | n/a | # a Fortran array that is logically identical to the input array. |
|---|
| 2283 | n/a | y = ndarray([v for v in b], shape=[3, 4], flags=ND_FORTRAN|ND_WRITABLE) |
|---|
| 2284 | n/a | self.assertEqual(memoryview(y), memoryview(nd)) |
|---|
| 2285 | n/a | |
|---|
| 2286 | n/a | if numpy_array: |
|---|
| 2287 | n/a | self.assertEqual(b, na.tostring(order='F')) |
|---|
| 2288 | n/a | |
|---|
| 2289 | n/a | # 'A' request |
|---|
| 2290 | n/a | if f == ND_FORTRAN: |
|---|
| 2291 | n/a | x = ndarray(lst, shape=[3, 4], flags=ND_WRITABLE) |
|---|
| 2292 | n/a | expected = x.tobytes() |
|---|
| 2293 | n/a | else: |
|---|
| 2294 | n/a | expected = nd.tobytes() |
|---|
| 2295 | n/a | for request in [PyBUF_FULL, PyBUF_FULL_RO, PyBUF_INDIRECT, |
|---|
| 2296 | n/a | PyBUF_STRIDES, PyBUF_ND]: |
|---|
| 2297 | n/a | try: |
|---|
| 2298 | n/a | b = py_buffer_to_contiguous(nd, 'A', request) |
|---|
| 2299 | n/a | except BufferError: |
|---|
| 2300 | n/a | continue |
|---|
| 2301 | n/a | |
|---|
| 2302 | n/a | self.assertEqual(b, expected) |
|---|
| 2303 | n/a | |
|---|
| 2304 | n/a | # Check that output can be used as the basis for constructing |
|---|
| 2305 | n/a | # an array with order=f that is logically identical to the input |
|---|
| 2306 | n/a | # array. |
|---|
| 2307 | n/a | y = ndarray([v for v in b], shape=[3, 4], flags=f|ND_WRITABLE) |
|---|
| 2308 | n/a | self.assertEqual(memoryview(y), memoryview(nd)) |
|---|
| 2309 | n/a | |
|---|
| 2310 | n/a | if numpy_array: |
|---|
| 2311 | n/a | self.assertEqual(b, na.tostring(order='A')) |
|---|
| 2312 | n/a | |
|---|
| 2313 | n/a | # multi-dimensional, non-contiguous input |
|---|
| 2314 | n/a | nd = ndarray(list(range(12)), shape=[3, 4], flags=ND_WRITABLE|ND_PIL) |
|---|
| 2315 | n/a | |
|---|
| 2316 | n/a | # 'C' |
|---|
| 2317 | n/a | b = py_buffer_to_contiguous(nd, 'C', PyBUF_FULL_RO) |
|---|
| 2318 | n/a | self.assertEqual(b, nd.tobytes()) |
|---|
| 2319 | n/a | y = ndarray([v for v in b], shape=[3, 4], flags=ND_WRITABLE) |
|---|
| 2320 | n/a | self.assertEqual(memoryview(y), memoryview(nd)) |
|---|
| 2321 | n/a | |
|---|
| 2322 | n/a | # 'F' |
|---|
| 2323 | n/a | b = py_buffer_to_contiguous(nd, 'F', PyBUF_FULL_RO) |
|---|
| 2324 | n/a | x = ndarray(transpose(lst, [3, 4]), shape=[4, 3], flags=ND_WRITABLE) |
|---|
| 2325 | n/a | self.assertEqual(b, x.tobytes()) |
|---|
| 2326 | n/a | y = ndarray([v for v in b], shape=[3, 4], flags=ND_FORTRAN|ND_WRITABLE) |
|---|
| 2327 | n/a | self.assertEqual(memoryview(y), memoryview(nd)) |
|---|
| 2328 | n/a | |
|---|
| 2329 | n/a | # 'A' |
|---|
| 2330 | n/a | b = py_buffer_to_contiguous(nd, 'A', PyBUF_FULL_RO) |
|---|
| 2331 | n/a | self.assertEqual(b, nd.tobytes()) |
|---|
| 2332 | n/a | y = ndarray([v for v in b], shape=[3, 4], flags=ND_WRITABLE) |
|---|
| 2333 | n/a | self.assertEqual(memoryview(y), memoryview(nd)) |
|---|
| 2334 | n/a | |
|---|
| 2335 | n/a | def test_memoryview_construction(self): |
|---|
| 2336 | n/a | |
|---|
| 2337 | n/a | items_shape = [(9, []), ([1,2,3], [3]), (list(range(2*3*5)), [2,3,5])] |
|---|
| 2338 | n/a | |
|---|
| 2339 | n/a | # NumPy style, C-contiguous: |
|---|
| 2340 | n/a | for items, shape in items_shape: |
|---|
| 2341 | n/a | |
|---|
| 2342 | n/a | # From PEP-3118 compliant exporter: |
|---|
| 2343 | n/a | ex = ndarray(items, shape=shape) |
|---|
| 2344 | n/a | m = memoryview(ex) |
|---|
| 2345 | n/a | self.assertTrue(m.c_contiguous) |
|---|
| 2346 | n/a | self.assertTrue(m.contiguous) |
|---|
| 2347 | n/a | |
|---|
| 2348 | n/a | ndim = len(shape) |
|---|
| 2349 | n/a | strides = strides_from_shape(ndim, shape, 1, 'C') |
|---|
| 2350 | n/a | lst = carray(items, shape) |
|---|
| 2351 | n/a | |
|---|
| 2352 | n/a | self.verify(m, obj=ex, |
|---|
| 2353 | n/a | itemsize=1, fmt='B', readonly=1, |
|---|
| 2354 | n/a | ndim=ndim, shape=shape, strides=strides, |
|---|
| 2355 | n/a | lst=lst) |
|---|
| 2356 | n/a | |
|---|
| 2357 | n/a | # From memoryview: |
|---|
| 2358 | n/a | m2 = memoryview(m) |
|---|
| 2359 | n/a | self.verify(m2, obj=ex, |
|---|
| 2360 | n/a | itemsize=1, fmt='B', readonly=1, |
|---|
| 2361 | n/a | ndim=ndim, shape=shape, strides=strides, |
|---|
| 2362 | n/a | lst=lst) |
|---|
| 2363 | n/a | |
|---|
| 2364 | n/a | # PyMemoryView_FromBuffer(): no strides |
|---|
| 2365 | n/a | nd = ndarray(ex, getbuf=PyBUF_CONTIG_RO|PyBUF_FORMAT) |
|---|
| 2366 | n/a | self.assertEqual(nd.strides, ()) |
|---|
| 2367 | n/a | m = nd.memoryview_from_buffer() |
|---|
| 2368 | n/a | self.verify(m, obj=None, |
|---|
| 2369 | n/a | itemsize=1, fmt='B', readonly=1, |
|---|
| 2370 | n/a | ndim=ndim, shape=shape, strides=strides, |
|---|
| 2371 | n/a | lst=lst) |
|---|
| 2372 | n/a | |
|---|
| 2373 | n/a | # PyMemoryView_FromBuffer(): no format, shape, strides |
|---|
| 2374 | n/a | nd = ndarray(ex, getbuf=PyBUF_SIMPLE) |
|---|
| 2375 | n/a | self.assertEqual(nd.format, '') |
|---|
| 2376 | n/a | self.assertEqual(nd.shape, ()) |
|---|
| 2377 | n/a | self.assertEqual(nd.strides, ()) |
|---|
| 2378 | n/a | m = nd.memoryview_from_buffer() |
|---|
| 2379 | n/a | |
|---|
| 2380 | n/a | lst = [items] if ndim == 0 else items |
|---|
| 2381 | n/a | self.verify(m, obj=None, |
|---|
| 2382 | n/a | itemsize=1, fmt='B', readonly=1, |
|---|
| 2383 | n/a | ndim=1, shape=[ex.nbytes], strides=(1,), |
|---|
| 2384 | n/a | lst=lst) |
|---|
| 2385 | n/a | |
|---|
| 2386 | n/a | # NumPy style, Fortran contiguous: |
|---|
| 2387 | n/a | for items, shape in items_shape: |
|---|
| 2388 | n/a | |
|---|
| 2389 | n/a | # From PEP-3118 compliant exporter: |
|---|
| 2390 | n/a | ex = ndarray(items, shape=shape, flags=ND_FORTRAN) |
|---|
| 2391 | n/a | m = memoryview(ex) |
|---|
| 2392 | n/a | self.assertTrue(m.f_contiguous) |
|---|
| 2393 | n/a | self.assertTrue(m.contiguous) |
|---|
| 2394 | n/a | |
|---|
| 2395 | n/a | ndim = len(shape) |
|---|
| 2396 | n/a | strides = strides_from_shape(ndim, shape, 1, 'F') |
|---|
| 2397 | n/a | lst = farray(items, shape) |
|---|
| 2398 | n/a | |
|---|
| 2399 | n/a | self.verify(m, obj=ex, |
|---|
| 2400 | n/a | itemsize=1, fmt='B', readonly=1, |
|---|
| 2401 | n/a | ndim=ndim, shape=shape, strides=strides, |
|---|
| 2402 | n/a | lst=lst) |
|---|
| 2403 | n/a | |
|---|
| 2404 | n/a | # From memoryview: |
|---|
| 2405 | n/a | m2 = memoryview(m) |
|---|
| 2406 | n/a | self.verify(m2, obj=ex, |
|---|
| 2407 | n/a | itemsize=1, fmt='B', readonly=1, |
|---|
| 2408 | n/a | ndim=ndim, shape=shape, strides=strides, |
|---|
| 2409 | n/a | lst=lst) |
|---|
| 2410 | n/a | |
|---|
| 2411 | n/a | # PIL style: |
|---|
| 2412 | n/a | for items, shape in items_shape[1:]: |
|---|
| 2413 | n/a | |
|---|
| 2414 | n/a | # From PEP-3118 compliant exporter: |
|---|
| 2415 | n/a | ex = ndarray(items, shape=shape, flags=ND_PIL) |
|---|
| 2416 | n/a | m = memoryview(ex) |
|---|
| 2417 | n/a | |
|---|
| 2418 | n/a | ndim = len(shape) |
|---|
| 2419 | n/a | lst = carray(items, shape) |
|---|
| 2420 | n/a | |
|---|
| 2421 | n/a | self.verify(m, obj=ex, |
|---|
| 2422 | n/a | itemsize=1, fmt='B', readonly=1, |
|---|
| 2423 | n/a | ndim=ndim, shape=shape, strides=ex.strides, |
|---|
| 2424 | n/a | lst=lst) |
|---|
| 2425 | n/a | |
|---|
| 2426 | n/a | # From memoryview: |
|---|
| 2427 | n/a | m2 = memoryview(m) |
|---|
| 2428 | n/a | self.verify(m2, obj=ex, |
|---|
| 2429 | n/a | itemsize=1, fmt='B', readonly=1, |
|---|
| 2430 | n/a | ndim=ndim, shape=shape, strides=ex.strides, |
|---|
| 2431 | n/a | lst=lst) |
|---|
| 2432 | n/a | |
|---|
| 2433 | n/a | # Invalid number of arguments: |
|---|
| 2434 | n/a | self.assertRaises(TypeError, memoryview, b'9', 'x') |
|---|
| 2435 | n/a | # Not a buffer provider: |
|---|
| 2436 | n/a | self.assertRaises(TypeError, memoryview, {}) |
|---|
| 2437 | n/a | # Non-compliant buffer provider: |
|---|
| 2438 | n/a | ex = ndarray([1,2,3], shape=[3]) |
|---|
| 2439 | n/a | nd = ndarray(ex, getbuf=PyBUF_SIMPLE) |
|---|
| 2440 | n/a | self.assertRaises(BufferError, memoryview, nd) |
|---|
| 2441 | n/a | nd = ndarray(ex, getbuf=PyBUF_CONTIG_RO|PyBUF_FORMAT) |
|---|
| 2442 | n/a | self.assertRaises(BufferError, memoryview, nd) |
|---|
| 2443 | n/a | |
|---|
| 2444 | n/a | # ndim > 64 |
|---|
| 2445 | n/a | nd = ndarray([1]*128, shape=[1]*128, format='L') |
|---|
| 2446 | n/a | self.assertRaises(ValueError, memoryview, nd) |
|---|
| 2447 | n/a | self.assertRaises(ValueError, nd.memoryview_from_buffer) |
|---|
| 2448 | n/a | self.assertRaises(ValueError, get_contiguous, nd, PyBUF_READ, 'C') |
|---|
| 2449 | n/a | self.assertRaises(ValueError, get_contiguous, nd, PyBUF_READ, 'F') |
|---|
| 2450 | n/a | self.assertRaises(ValueError, get_contiguous, nd[::-1], PyBUF_READ, 'C') |
|---|
| 2451 | n/a | |
|---|
| 2452 | n/a | def test_memoryview_cast_zero_shape(self): |
|---|
| 2453 | n/a | # Casts are undefined if buffer is multidimensional and shape |
|---|
| 2454 | n/a | # contains zeros. These arrays are regarded as C-contiguous by |
|---|
| 2455 | n/a | # Numpy and PyBuffer_GetContiguous(), so they are not caught by |
|---|
| 2456 | n/a | # the test for C-contiguity in memory_cast(). |
|---|
| 2457 | n/a | items = [1,2,3] |
|---|
| 2458 | n/a | for shape in ([0,3,3], [3,0,3], [0,3,3]): |
|---|
| 2459 | n/a | ex = ndarray(items, shape=shape) |
|---|
| 2460 | n/a | self.assertTrue(ex.c_contiguous) |
|---|
| 2461 | n/a | msrc = memoryview(ex) |
|---|
| 2462 | n/a | self.assertRaises(TypeError, msrc.cast, 'c') |
|---|
| 2463 | n/a | # Monodimensional empty view can be cast (issue #19014). |
|---|
| 2464 | n/a | for fmt, _, _ in iter_format(1, 'memoryview'): |
|---|
| 2465 | n/a | msrc = memoryview(b'') |
|---|
| 2466 | n/a | m = msrc.cast(fmt) |
|---|
| 2467 | n/a | self.assertEqual(m.tobytes(), b'') |
|---|
| 2468 | n/a | self.assertEqual(m.tolist(), []) |
|---|
| 2469 | n/a | |
|---|
| 2470 | n/a | check_sizeof = support.check_sizeof |
|---|
| 2471 | n/a | |
|---|
| 2472 | n/a | def test_memoryview_sizeof(self): |
|---|
| 2473 | n/a | check = self.check_sizeof |
|---|
| 2474 | n/a | vsize = support.calcvobjsize |
|---|
| 2475 | n/a | base_struct = 'Pnin 2P2n2i5P P' |
|---|
| 2476 | n/a | per_dim = '3n' |
|---|
| 2477 | n/a | |
|---|
| 2478 | n/a | items = list(range(8)) |
|---|
| 2479 | n/a | check(memoryview(b''), vsize(base_struct + 1 * per_dim)) |
|---|
| 2480 | n/a | a = ndarray(items, shape=[2, 4], format="b") |
|---|
| 2481 | n/a | check(memoryview(a), vsize(base_struct + 2 * per_dim)) |
|---|
| 2482 | n/a | a = ndarray(items, shape=[2, 2, 2], format="b") |
|---|
| 2483 | n/a | check(memoryview(a), vsize(base_struct + 3 * per_dim)) |
|---|
| 2484 | n/a | |
|---|
| 2485 | n/a | def test_memoryview_struct_module(self): |
|---|
| 2486 | n/a | |
|---|
| 2487 | n/a | class INT(object): |
|---|
| 2488 | n/a | def __init__(self, val): |
|---|
| 2489 | n/a | self.val = val |
|---|
| 2490 | n/a | def __int__(self): |
|---|
| 2491 | n/a | return self.val |
|---|
| 2492 | n/a | |
|---|
| 2493 | n/a | class IDX(object): |
|---|
| 2494 | n/a | def __init__(self, val): |
|---|
| 2495 | n/a | self.val = val |
|---|
| 2496 | n/a | def __index__(self): |
|---|
| 2497 | n/a | return self.val |
|---|
| 2498 | n/a | |
|---|
| 2499 | n/a | def f(): return 7 |
|---|
| 2500 | n/a | |
|---|
| 2501 | n/a | values = [INT(9), IDX(9), |
|---|
| 2502 | n/a | 2.2+3j, Decimal("-21.1"), 12.2, Fraction(5, 2), |
|---|
| 2503 | n/a | [1,2,3], {4,5,6}, {7:8}, (), (9,), |
|---|
| 2504 | n/a | True, False, None, NotImplemented, |
|---|
| 2505 | n/a | b'a', b'abc', bytearray(b'a'), bytearray(b'abc'), |
|---|
| 2506 | n/a | 'a', 'abc', r'a', r'abc', |
|---|
| 2507 | n/a | f, lambda x: x] |
|---|
| 2508 | n/a | |
|---|
| 2509 | n/a | for fmt, items, item in iter_format(10, 'memoryview'): |
|---|
| 2510 | n/a | ex = ndarray(items, shape=[10], format=fmt, flags=ND_WRITABLE) |
|---|
| 2511 | n/a | nd = ndarray(items, shape=[10], format=fmt, flags=ND_WRITABLE) |
|---|
| 2512 | n/a | m = memoryview(ex) |
|---|
| 2513 | n/a | |
|---|
| 2514 | n/a | struct.pack_into(fmt, nd, 0, item) |
|---|
| 2515 | n/a | m[0] = item |
|---|
| 2516 | n/a | self.assertEqual(m[0], nd[0]) |
|---|
| 2517 | n/a | |
|---|
| 2518 | n/a | itemsize = struct.calcsize(fmt) |
|---|
| 2519 | n/a | if 'P' in fmt: |
|---|
| 2520 | n/a | continue |
|---|
| 2521 | n/a | |
|---|
| 2522 | n/a | for v in values: |
|---|
| 2523 | n/a | struct_err = None |
|---|
| 2524 | n/a | try: |
|---|
| 2525 | n/a | struct.pack_into(fmt, nd, itemsize, v) |
|---|
| 2526 | n/a | except struct.error: |
|---|
| 2527 | n/a | struct_err = struct.error |
|---|
| 2528 | n/a | |
|---|
| 2529 | n/a | mv_err = None |
|---|
| 2530 | n/a | try: |
|---|
| 2531 | n/a | m[1] = v |
|---|
| 2532 | n/a | except (TypeError, ValueError) as e: |
|---|
| 2533 | n/a | mv_err = e.__class__ |
|---|
| 2534 | n/a | |
|---|
| 2535 | n/a | if struct_err or mv_err: |
|---|
| 2536 | n/a | self.assertIsNot(struct_err, None) |
|---|
| 2537 | n/a | self.assertIsNot(mv_err, None) |
|---|
| 2538 | n/a | else: |
|---|
| 2539 | n/a | self.assertEqual(m[1], nd[1]) |
|---|
| 2540 | n/a | |
|---|
| 2541 | n/a | def test_memoryview_cast_zero_strides(self): |
|---|
| 2542 | n/a | # Casts are undefined if strides contains zeros. These arrays are |
|---|
| 2543 | n/a | # (sometimes!) regarded as C-contiguous by Numpy, but not by |
|---|
| 2544 | n/a | # PyBuffer_GetContiguous(). |
|---|
| 2545 | n/a | ex = ndarray([1,2,3], shape=[3], strides=[0]) |
|---|
| 2546 | n/a | self.assertFalse(ex.c_contiguous) |
|---|
| 2547 | n/a | msrc = memoryview(ex) |
|---|
| 2548 | n/a | self.assertRaises(TypeError, msrc.cast, 'c') |
|---|
| 2549 | n/a | |
|---|
| 2550 | n/a | def test_memoryview_cast_invalid(self): |
|---|
| 2551 | n/a | # invalid format |
|---|
| 2552 | n/a | for sfmt in NON_BYTE_FORMAT: |
|---|
| 2553 | n/a | sformat = '@' + sfmt if randrange(2) else sfmt |
|---|
| 2554 | n/a | ssize = struct.calcsize(sformat) |
|---|
| 2555 | n/a | for dfmt in NON_BYTE_FORMAT: |
|---|
| 2556 | n/a | dformat = '@' + dfmt if randrange(2) else dfmt |
|---|
| 2557 | n/a | dsize = struct.calcsize(dformat) |
|---|
| 2558 | n/a | ex = ndarray(list(range(32)), shape=[32//ssize], format=sformat) |
|---|
| 2559 | n/a | msrc = memoryview(ex) |
|---|
| 2560 | n/a | self.assertRaises(TypeError, msrc.cast, dfmt, [32//dsize]) |
|---|
| 2561 | n/a | |
|---|
| 2562 | n/a | for sfmt, sitems, _ in iter_format(1): |
|---|
| 2563 | n/a | ex = ndarray(sitems, shape=[1], format=sfmt) |
|---|
| 2564 | n/a | msrc = memoryview(ex) |
|---|
| 2565 | n/a | for dfmt, _, _ in iter_format(1): |
|---|
| 2566 | n/a | if not is_memoryview_format(dfmt): |
|---|
| 2567 | n/a | self.assertRaises(ValueError, msrc.cast, dfmt, |
|---|
| 2568 | n/a | [32//dsize]) |
|---|
| 2569 | n/a | else: |
|---|
| 2570 | n/a | if not is_byte_format(sfmt) and not is_byte_format(dfmt): |
|---|
| 2571 | n/a | self.assertRaises(TypeError, msrc.cast, dfmt, |
|---|
| 2572 | n/a | [32//dsize]) |
|---|
| 2573 | n/a | |
|---|
| 2574 | n/a | # invalid shape |
|---|
| 2575 | n/a | size_h = struct.calcsize('h') |
|---|
| 2576 | n/a | size_d = struct.calcsize('d') |
|---|
| 2577 | n/a | ex = ndarray(list(range(2*2*size_d)), shape=[2,2,size_d], format='h') |
|---|
| 2578 | n/a | msrc = memoryview(ex) |
|---|
| 2579 | n/a | self.assertRaises(TypeError, msrc.cast, shape=[2,2,size_h], format='d') |
|---|
| 2580 | n/a | |
|---|
| 2581 | n/a | ex = ndarray(list(range(120)), shape=[1,2,3,4,5]) |
|---|
| 2582 | n/a | m = memoryview(ex) |
|---|
| 2583 | n/a | |
|---|
| 2584 | n/a | # incorrect number of args |
|---|
| 2585 | n/a | self.assertRaises(TypeError, m.cast) |
|---|
| 2586 | n/a | self.assertRaises(TypeError, m.cast, 1, 2, 3) |
|---|
| 2587 | n/a | |
|---|
| 2588 | n/a | # incorrect dest format type |
|---|
| 2589 | n/a | self.assertRaises(TypeError, m.cast, {}) |
|---|
| 2590 | n/a | |
|---|
| 2591 | n/a | # incorrect dest format |
|---|
| 2592 | n/a | self.assertRaises(ValueError, m.cast, "X") |
|---|
| 2593 | n/a | self.assertRaises(ValueError, m.cast, "@X") |
|---|
| 2594 | n/a | self.assertRaises(ValueError, m.cast, "@XY") |
|---|
| 2595 | n/a | |
|---|
| 2596 | n/a | # dest format not implemented |
|---|
| 2597 | n/a | self.assertRaises(ValueError, m.cast, "=B") |
|---|
| 2598 | n/a | self.assertRaises(ValueError, m.cast, "!L") |
|---|
| 2599 | n/a | self.assertRaises(ValueError, m.cast, "<P") |
|---|
| 2600 | n/a | self.assertRaises(ValueError, m.cast, ">l") |
|---|
| 2601 | n/a | self.assertRaises(ValueError, m.cast, "BI") |
|---|
| 2602 | n/a | self.assertRaises(ValueError, m.cast, "xBI") |
|---|
| 2603 | n/a | |
|---|
| 2604 | n/a | # src format not implemented |
|---|
| 2605 | n/a | ex = ndarray([(1,2), (3,4)], shape=[2], format="II") |
|---|
| 2606 | n/a | m = memoryview(ex) |
|---|
| 2607 | n/a | self.assertRaises(NotImplementedError, m.__getitem__, 0) |
|---|
| 2608 | n/a | self.assertRaises(NotImplementedError, m.__setitem__, 0, 8) |
|---|
| 2609 | n/a | self.assertRaises(NotImplementedError, m.tolist) |
|---|
| 2610 | n/a | |
|---|
| 2611 | n/a | # incorrect shape type |
|---|
| 2612 | n/a | ex = ndarray(list(range(120)), shape=[1,2,3,4,5]) |
|---|
| 2613 | n/a | m = memoryview(ex) |
|---|
| 2614 | n/a | self.assertRaises(TypeError, m.cast, "B", shape={}) |
|---|
| 2615 | n/a | |
|---|
| 2616 | n/a | # incorrect shape elements |
|---|
| 2617 | n/a | ex = ndarray(list(range(120)), shape=[2*3*4*5]) |
|---|
| 2618 | n/a | m = memoryview(ex) |
|---|
| 2619 | n/a | self.assertRaises(OverflowError, m.cast, "B", shape=[2**64]) |
|---|
| 2620 | n/a | self.assertRaises(ValueError, m.cast, "B", shape=[-1]) |
|---|
| 2621 | n/a | self.assertRaises(ValueError, m.cast, "B", shape=[2,3,4,5,6,7,-1]) |
|---|
| 2622 | n/a | self.assertRaises(ValueError, m.cast, "B", shape=[2,3,4,5,6,7,0]) |
|---|
| 2623 | n/a | self.assertRaises(TypeError, m.cast, "B", shape=[2,3,4,5,6,7,'x']) |
|---|
| 2624 | n/a | |
|---|
| 2625 | n/a | # N-D -> N-D cast |
|---|
| 2626 | n/a | ex = ndarray(list([9 for _ in range(3*5*7*11)]), shape=[3,5,7,11]) |
|---|
| 2627 | n/a | m = memoryview(ex) |
|---|
| 2628 | n/a | self.assertRaises(TypeError, m.cast, "I", shape=[2,3,4,5]) |
|---|
| 2629 | n/a | |
|---|
| 2630 | n/a | # cast with ndim > 64 |
|---|
| 2631 | n/a | nd = ndarray(list(range(128)), shape=[128], format='I') |
|---|
| 2632 | n/a | m = memoryview(nd) |
|---|
| 2633 | n/a | self.assertRaises(ValueError, m.cast, 'I', [1]*128) |
|---|
| 2634 | n/a | |
|---|
| 2635 | n/a | # view->len not a multiple of itemsize |
|---|
| 2636 | n/a | ex = ndarray(list([9 for _ in range(3*5*7*11)]), shape=[3*5*7*11]) |
|---|
| 2637 | n/a | m = memoryview(ex) |
|---|
| 2638 | n/a | self.assertRaises(TypeError, m.cast, "I", shape=[2,3,4,5]) |
|---|
| 2639 | n/a | |
|---|
| 2640 | n/a | # product(shape) * itemsize != buffer size |
|---|
| 2641 | n/a | ex = ndarray(list([9 for _ in range(3*5*7*11)]), shape=[3*5*7*11]) |
|---|
| 2642 | n/a | m = memoryview(ex) |
|---|
| 2643 | n/a | self.assertRaises(TypeError, m.cast, "B", shape=[2,3,4,5]) |
|---|
| 2644 | n/a | |
|---|
| 2645 | n/a | # product(shape) * itemsize overflow |
|---|
| 2646 | n/a | nd = ndarray(list(range(128)), shape=[128], format='I') |
|---|
| 2647 | n/a | m1 = memoryview(nd) |
|---|
| 2648 | n/a | nd = ndarray(list(range(128)), shape=[128], format='B') |
|---|
| 2649 | n/a | m2 = memoryview(nd) |
|---|
| 2650 | n/a | if sys.maxsize == 2**63-1: |
|---|
| 2651 | n/a | self.assertRaises(TypeError, m1.cast, 'B', |
|---|
| 2652 | n/a | [7, 7, 73, 127, 337, 92737, 649657]) |
|---|
| 2653 | n/a | self.assertRaises(ValueError, m1.cast, 'B', |
|---|
| 2654 | n/a | [2**20, 2**20, 2**10, 2**10, 2**3]) |
|---|
| 2655 | n/a | self.assertRaises(ValueError, m2.cast, 'I', |
|---|
| 2656 | n/a | [2**20, 2**20, 2**10, 2**10, 2**1]) |
|---|
| 2657 | n/a | else: |
|---|
| 2658 | n/a | self.assertRaises(TypeError, m1.cast, 'B', |
|---|
| 2659 | n/a | [1, 2147483647]) |
|---|
| 2660 | n/a | self.assertRaises(ValueError, m1.cast, 'B', |
|---|
| 2661 | n/a | [2**10, 2**10, 2**5, 2**5, 2**1]) |
|---|
| 2662 | n/a | self.assertRaises(ValueError, m2.cast, 'I', |
|---|
| 2663 | n/a | [2**10, 2**10, 2**5, 2**3, 2**1]) |
|---|
| 2664 | n/a | |
|---|
| 2665 | n/a | def test_memoryview_cast(self): |
|---|
| 2666 | n/a | bytespec = ( |
|---|
| 2667 | n/a | ('B', lambda ex: list(ex.tobytes())), |
|---|
| 2668 | n/a | ('b', lambda ex: [x-256 if x > 127 else x for x in list(ex.tobytes())]), |
|---|
| 2669 | n/a | ('c', lambda ex: [bytes(chr(x), 'latin-1') for x in list(ex.tobytes())]), |
|---|
| 2670 | n/a | ) |
|---|
| 2671 | n/a | |
|---|
| 2672 | n/a | def iter_roundtrip(ex, m, items, fmt): |
|---|
| 2673 | n/a | srcsize = struct.calcsize(fmt) |
|---|
| 2674 | n/a | for bytefmt, to_bytelist in bytespec: |
|---|
| 2675 | n/a | |
|---|
| 2676 | n/a | m2 = m.cast(bytefmt) |
|---|
| 2677 | n/a | lst = to_bytelist(ex) |
|---|
| 2678 | n/a | self.verify(m2, obj=ex, |
|---|
| 2679 | n/a | itemsize=1, fmt=bytefmt, readonly=0, |
|---|
| 2680 | n/a | ndim=1, shape=[31*srcsize], strides=(1,), |
|---|
| 2681 | n/a | lst=lst, cast=True) |
|---|
| 2682 | n/a | |
|---|
| 2683 | n/a | m3 = m2.cast(fmt) |
|---|
| 2684 | n/a | self.assertEqual(m3, ex) |
|---|
| 2685 | n/a | lst = ex.tolist() |
|---|
| 2686 | n/a | self.verify(m3, obj=ex, |
|---|
| 2687 | n/a | itemsize=srcsize, fmt=fmt, readonly=0, |
|---|
| 2688 | n/a | ndim=1, shape=[31], strides=(srcsize,), |
|---|
| 2689 | n/a | lst=lst, cast=True) |
|---|
| 2690 | n/a | |
|---|
| 2691 | n/a | # cast from ndim = 0 to ndim = 1 |
|---|
| 2692 | n/a | srcsize = struct.calcsize('I') |
|---|
| 2693 | n/a | ex = ndarray(9, shape=[], format='I') |
|---|
| 2694 | n/a | destitems, destshape = cast_items(ex, 'B', 1) |
|---|
| 2695 | n/a | m = memoryview(ex) |
|---|
| 2696 | n/a | m2 = m.cast('B') |
|---|
| 2697 | n/a | self.verify(m2, obj=ex, |
|---|
| 2698 | n/a | itemsize=1, fmt='B', readonly=1, |
|---|
| 2699 | n/a | ndim=1, shape=destshape, strides=(1,), |
|---|
| 2700 | n/a | lst=destitems, cast=True) |
|---|
| 2701 | n/a | |
|---|
| 2702 | n/a | # cast from ndim = 1 to ndim = 0 |
|---|
| 2703 | n/a | destsize = struct.calcsize('I') |
|---|
| 2704 | n/a | ex = ndarray([9]*destsize, shape=[destsize], format='B') |
|---|
| 2705 | n/a | destitems, destshape = cast_items(ex, 'I', destsize, shape=[]) |
|---|
| 2706 | n/a | m = memoryview(ex) |
|---|
| 2707 | n/a | m2 = m.cast('I', shape=[]) |
|---|
| 2708 | n/a | self.verify(m2, obj=ex, |
|---|
| 2709 | n/a | itemsize=destsize, fmt='I', readonly=1, |
|---|
| 2710 | n/a | ndim=0, shape=(), strides=(), |
|---|
| 2711 | n/a | lst=destitems, cast=True) |
|---|
| 2712 | n/a | |
|---|
| 2713 | n/a | # array.array: roundtrip to/from bytes |
|---|
| 2714 | n/a | for fmt, items, _ in iter_format(31, 'array'): |
|---|
| 2715 | n/a | ex = array.array(fmt, items) |
|---|
| 2716 | n/a | m = memoryview(ex) |
|---|
| 2717 | n/a | iter_roundtrip(ex, m, items, fmt) |
|---|
| 2718 | n/a | |
|---|
| 2719 | n/a | # ndarray: roundtrip to/from bytes |
|---|
| 2720 | n/a | for fmt, items, _ in iter_format(31, 'memoryview'): |
|---|
| 2721 | n/a | ex = ndarray(items, shape=[31], format=fmt, flags=ND_WRITABLE) |
|---|
| 2722 | n/a | m = memoryview(ex) |
|---|
| 2723 | n/a | iter_roundtrip(ex, m, items, fmt) |
|---|
| 2724 | n/a | |
|---|
| 2725 | n/a | def test_memoryview_cast_1D_ND(self): |
|---|
| 2726 | n/a | # Cast between C-contiguous buffers. At least one buffer must |
|---|
| 2727 | n/a | # be 1D, at least one format must be 'c', 'b' or 'B'. |
|---|
| 2728 | n/a | for _tshape in gencastshapes(): |
|---|
| 2729 | n/a | for char in fmtdict['@']: |
|---|
| 2730 | n/a | tfmt = ('', '@')[randrange(2)] + char |
|---|
| 2731 | n/a | tsize = struct.calcsize(tfmt) |
|---|
| 2732 | n/a | n = prod(_tshape) * tsize |
|---|
| 2733 | n/a | obj = 'memoryview' if is_byte_format(tfmt) else 'bytefmt' |
|---|
| 2734 | n/a | for fmt, items, _ in iter_format(n, obj): |
|---|
| 2735 | n/a | size = struct.calcsize(fmt) |
|---|
| 2736 | n/a | shape = [n] if n > 0 else [] |
|---|
| 2737 | n/a | tshape = _tshape + [size] |
|---|
| 2738 | n/a | |
|---|
| 2739 | n/a | ex = ndarray(items, shape=shape, format=fmt) |
|---|
| 2740 | n/a | m = memoryview(ex) |
|---|
| 2741 | n/a | |
|---|
| 2742 | n/a | titems, tshape = cast_items(ex, tfmt, tsize, shape=tshape) |
|---|
| 2743 | n/a | |
|---|
| 2744 | n/a | if titems is None: |
|---|
| 2745 | n/a | self.assertRaises(TypeError, m.cast, tfmt, tshape) |
|---|
| 2746 | n/a | continue |
|---|
| 2747 | n/a | if titems == 'nan': |
|---|
| 2748 | n/a | continue # NaNs in lists are a recipe for trouble. |
|---|
| 2749 | n/a | |
|---|
| 2750 | n/a | # 1D -> ND |
|---|
| 2751 | n/a | nd = ndarray(titems, shape=tshape, format=tfmt) |
|---|
| 2752 | n/a | |
|---|
| 2753 | n/a | m2 = m.cast(tfmt, shape=tshape) |
|---|
| 2754 | n/a | ndim = len(tshape) |
|---|
| 2755 | n/a | strides = nd.strides |
|---|
| 2756 | n/a | lst = nd.tolist() |
|---|
| 2757 | n/a | self.verify(m2, obj=ex, |
|---|
| 2758 | n/a | itemsize=tsize, fmt=tfmt, readonly=1, |
|---|
| 2759 | n/a | ndim=ndim, shape=tshape, strides=strides, |
|---|
| 2760 | n/a | lst=lst, cast=True) |
|---|
| 2761 | n/a | |
|---|
| 2762 | n/a | # ND -> 1D |
|---|
| 2763 | n/a | m3 = m2.cast(fmt) |
|---|
| 2764 | n/a | m4 = m2.cast(fmt, shape=shape) |
|---|
| 2765 | n/a | ndim = len(shape) |
|---|
| 2766 | n/a | strides = ex.strides |
|---|
| 2767 | n/a | lst = ex.tolist() |
|---|
| 2768 | n/a | |
|---|
| 2769 | n/a | self.verify(m3, obj=ex, |
|---|
| 2770 | n/a | itemsize=size, fmt=fmt, readonly=1, |
|---|
| 2771 | n/a | ndim=ndim, shape=shape, strides=strides, |
|---|
| 2772 | n/a | lst=lst, cast=True) |
|---|
| 2773 | n/a | |
|---|
| 2774 | n/a | self.verify(m4, obj=ex, |
|---|
| 2775 | n/a | itemsize=size, fmt=fmt, readonly=1, |
|---|
| 2776 | n/a | ndim=ndim, shape=shape, strides=strides, |
|---|
| 2777 | n/a | lst=lst, cast=True) |
|---|
| 2778 | n/a | |
|---|
| 2779 | n/a | if ctypes: |
|---|
| 2780 | n/a | # format: "T{>l:x:>d:y:}" |
|---|
| 2781 | n/a | class BEPoint(ctypes.BigEndianStructure): |
|---|
| 2782 | n/a | _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_double)] |
|---|
| 2783 | n/a | point = BEPoint(100, 200.1) |
|---|
| 2784 | n/a | m1 = memoryview(point) |
|---|
| 2785 | n/a | m2 = m1.cast('B') |
|---|
| 2786 | n/a | self.assertEqual(m2.obj, point) |
|---|
| 2787 | n/a | self.assertEqual(m2.itemsize, 1) |
|---|
| 2788 | n/a | self.assertEqual(m2.readonly, 0) |
|---|
| 2789 | n/a | self.assertEqual(m2.ndim, 1) |
|---|
| 2790 | n/a | self.assertEqual(m2.shape, (m2.nbytes,)) |
|---|
| 2791 | n/a | self.assertEqual(m2.strides, (1,)) |
|---|
| 2792 | n/a | self.assertEqual(m2.suboffsets, ()) |
|---|
| 2793 | n/a | |
|---|
| 2794 | n/a | x = ctypes.c_double(1.2) |
|---|
| 2795 | n/a | m1 = memoryview(x) |
|---|
| 2796 | n/a | m2 = m1.cast('c') |
|---|
| 2797 | n/a | self.assertEqual(m2.obj, x) |
|---|
| 2798 | n/a | self.assertEqual(m2.itemsize, 1) |
|---|
| 2799 | n/a | self.assertEqual(m2.readonly, 0) |
|---|
| 2800 | n/a | self.assertEqual(m2.ndim, 1) |
|---|
| 2801 | n/a | self.assertEqual(m2.shape, (m2.nbytes,)) |
|---|
| 2802 | n/a | self.assertEqual(m2.strides, (1,)) |
|---|
| 2803 | n/a | self.assertEqual(m2.suboffsets, ()) |
|---|
| 2804 | n/a | |
|---|
| 2805 | n/a | def test_memoryview_tolist(self): |
|---|
| 2806 | n/a | |
|---|
| 2807 | n/a | # Most tolist() tests are in self.verify() etc. |
|---|
| 2808 | n/a | |
|---|
| 2809 | n/a | a = array.array('h', list(range(-6, 6))) |
|---|
| 2810 | n/a | m = memoryview(a) |
|---|
| 2811 | n/a | self.assertEqual(m, a) |
|---|
| 2812 | n/a | self.assertEqual(m.tolist(), a.tolist()) |
|---|
| 2813 | n/a | |
|---|
| 2814 | n/a | a = a[2::3] |
|---|
| 2815 | n/a | m = m[2::3] |
|---|
| 2816 | n/a | self.assertEqual(m, a) |
|---|
| 2817 | n/a | self.assertEqual(m.tolist(), a.tolist()) |
|---|
| 2818 | n/a | |
|---|
| 2819 | n/a | ex = ndarray(list(range(2*3*5*7*11)), shape=[11,2,7,3,5], format='L') |
|---|
| 2820 | n/a | m = memoryview(ex) |
|---|
| 2821 | n/a | self.assertEqual(m.tolist(), ex.tolist()) |
|---|
| 2822 | n/a | |
|---|
| 2823 | n/a | ex = ndarray([(2, 5), (7, 11)], shape=[2], format='lh') |
|---|
| 2824 | n/a | m = memoryview(ex) |
|---|
| 2825 | n/a | self.assertRaises(NotImplementedError, m.tolist) |
|---|
| 2826 | n/a | |
|---|
| 2827 | n/a | ex = ndarray([b'12345'], shape=[1], format="s") |
|---|
| 2828 | n/a | m = memoryview(ex) |
|---|
| 2829 | n/a | self.assertRaises(NotImplementedError, m.tolist) |
|---|
| 2830 | n/a | |
|---|
| 2831 | n/a | ex = ndarray([b"a",b"b",b"c",b"d",b"e",b"f"], shape=[2,3], format='s') |
|---|
| 2832 | n/a | m = memoryview(ex) |
|---|
| 2833 | n/a | self.assertRaises(NotImplementedError, m.tolist) |
|---|
| 2834 | n/a | |
|---|
| 2835 | n/a | def test_memoryview_repr(self): |
|---|
| 2836 | n/a | m = memoryview(bytearray(9)) |
|---|
| 2837 | n/a | r = m.__repr__() |
|---|
| 2838 | n/a | self.assertTrue(r.startswith("<memory")) |
|---|
| 2839 | n/a | |
|---|
| 2840 | n/a | m.release() |
|---|
| 2841 | n/a | r = m.__repr__() |
|---|
| 2842 | n/a | self.assertTrue(r.startswith("<released")) |
|---|
| 2843 | n/a | |
|---|
| 2844 | n/a | def test_memoryview_sequence(self): |
|---|
| 2845 | n/a | |
|---|
| 2846 | n/a | for fmt in ('d', 'f'): |
|---|
| 2847 | n/a | inf = float(3e400) |
|---|
| 2848 | n/a | ex = array.array(fmt, [1.0, inf, 3.0]) |
|---|
| 2849 | n/a | m = memoryview(ex) |
|---|
| 2850 | n/a | self.assertIn(1.0, m) |
|---|
| 2851 | n/a | self.assertIn(5e700, m) |
|---|
| 2852 | n/a | self.assertIn(3.0, m) |
|---|
| 2853 | n/a | |
|---|
| 2854 | n/a | ex = ndarray(9.0, [], format='f') |
|---|
| 2855 | n/a | m = memoryview(ex) |
|---|
| 2856 | n/a | self.assertRaises(TypeError, eval, "9.0 in m", locals()) |
|---|
| 2857 | n/a | |
|---|
| 2858 | n/a | @contextlib.contextmanager |
|---|
| 2859 | n/a | def assert_out_of_bounds_error(self, dim): |
|---|
| 2860 | n/a | with self.assertRaises(IndexError) as cm: |
|---|
| 2861 | n/a | yield |
|---|
| 2862 | n/a | self.assertEqual(str(cm.exception), |
|---|
| 2863 | n/a | "index out of bounds on dimension %d" % (dim,)) |
|---|
| 2864 | n/a | |
|---|
| 2865 | n/a | def test_memoryview_index(self): |
|---|
| 2866 | n/a | |
|---|
| 2867 | n/a | # ndim = 0 |
|---|
| 2868 | n/a | ex = ndarray(12.5, shape=[], format='d') |
|---|
| 2869 | n/a | m = memoryview(ex) |
|---|
| 2870 | n/a | self.assertEqual(m[()], 12.5) |
|---|
| 2871 | n/a | self.assertEqual(m[...], m) |
|---|
| 2872 | n/a | self.assertEqual(m[...], ex) |
|---|
| 2873 | n/a | self.assertRaises(TypeError, m.__getitem__, 0) |
|---|
| 2874 | n/a | |
|---|
| 2875 | n/a | ex = ndarray((1,2,3), shape=[], format='iii') |
|---|
| 2876 | n/a | m = memoryview(ex) |
|---|
| 2877 | n/a | self.assertRaises(NotImplementedError, m.__getitem__, ()) |
|---|
| 2878 | n/a | |
|---|
| 2879 | n/a | # range |
|---|
| 2880 | n/a | ex = ndarray(list(range(7)), shape=[7], flags=ND_WRITABLE) |
|---|
| 2881 | n/a | m = memoryview(ex) |
|---|
| 2882 | n/a | |
|---|
| 2883 | n/a | self.assertRaises(IndexError, m.__getitem__, 2**64) |
|---|
| 2884 | n/a | self.assertRaises(TypeError, m.__getitem__, 2.0) |
|---|
| 2885 | n/a | self.assertRaises(TypeError, m.__getitem__, 0.0) |
|---|
| 2886 | n/a | |
|---|
| 2887 | n/a | # out of bounds |
|---|
| 2888 | n/a | self.assertRaises(IndexError, m.__getitem__, -8) |
|---|
| 2889 | n/a | self.assertRaises(IndexError, m.__getitem__, 8) |
|---|
| 2890 | n/a | |
|---|
| 2891 | n/a | # multi-dimensional |
|---|
| 2892 | n/a | ex = ndarray(list(range(12)), shape=[3,4], flags=ND_WRITABLE) |
|---|
| 2893 | n/a | m = memoryview(ex) |
|---|
| 2894 | n/a | |
|---|
| 2895 | n/a | self.assertEqual(m[0, 0], 0) |
|---|
| 2896 | n/a | self.assertEqual(m[2, 0], 8) |
|---|
| 2897 | n/a | self.assertEqual(m[2, 3], 11) |
|---|
| 2898 | n/a | self.assertEqual(m[-1, -1], 11) |
|---|
| 2899 | n/a | self.assertEqual(m[-3, -4], 0) |
|---|
| 2900 | n/a | |
|---|
| 2901 | n/a | # out of bounds |
|---|
| 2902 | n/a | for index in (3, -4): |
|---|
| 2903 | n/a | with self.assert_out_of_bounds_error(dim=1): |
|---|
| 2904 | n/a | m[index, 0] |
|---|
| 2905 | n/a | for index in (4, -5): |
|---|
| 2906 | n/a | with self.assert_out_of_bounds_error(dim=2): |
|---|
| 2907 | n/a | m[0, index] |
|---|
| 2908 | n/a | self.assertRaises(IndexError, m.__getitem__, (2**64, 0)) |
|---|
| 2909 | n/a | self.assertRaises(IndexError, m.__getitem__, (0, 2**64)) |
|---|
| 2910 | n/a | |
|---|
| 2911 | n/a | self.assertRaises(TypeError, m.__getitem__, (0, 0, 0)) |
|---|
| 2912 | n/a | self.assertRaises(TypeError, m.__getitem__, (0.0, 0.0)) |
|---|
| 2913 | n/a | |
|---|
| 2914 | n/a | # Not implemented: multidimensional sub-views |
|---|
| 2915 | n/a | self.assertRaises(NotImplementedError, m.__getitem__, ()) |
|---|
| 2916 | n/a | self.assertRaises(NotImplementedError, m.__getitem__, 0) |
|---|
| 2917 | n/a | |
|---|
| 2918 | n/a | def test_memoryview_assign(self): |
|---|
| 2919 | n/a | |
|---|
| 2920 | n/a | # ndim = 0 |
|---|
| 2921 | n/a | ex = ndarray(12.5, shape=[], format='f', flags=ND_WRITABLE) |
|---|
| 2922 | n/a | m = memoryview(ex) |
|---|
| 2923 | n/a | m[()] = 22.5 |
|---|
| 2924 | n/a | self.assertEqual(m[()], 22.5) |
|---|
| 2925 | n/a | m[...] = 23.5 |
|---|
| 2926 | n/a | self.assertEqual(m[()], 23.5) |
|---|
| 2927 | n/a | self.assertRaises(TypeError, m.__setitem__, 0, 24.7) |
|---|
| 2928 | n/a | |
|---|
| 2929 | n/a | # read-only |
|---|
| 2930 | n/a | ex = ndarray(list(range(7)), shape=[7]) |
|---|
| 2931 | n/a | m = memoryview(ex) |
|---|
| 2932 | n/a | self.assertRaises(TypeError, m.__setitem__, 2, 10) |
|---|
| 2933 | n/a | |
|---|
| 2934 | n/a | # range |
|---|
| 2935 | n/a | ex = ndarray(list(range(7)), shape=[7], flags=ND_WRITABLE) |
|---|
| 2936 | n/a | m = memoryview(ex) |
|---|
| 2937 | n/a | |
|---|
| 2938 | n/a | self.assertRaises(IndexError, m.__setitem__, 2**64, 9) |
|---|
| 2939 | n/a | self.assertRaises(TypeError, m.__setitem__, 2.0, 10) |
|---|
| 2940 | n/a | self.assertRaises(TypeError, m.__setitem__, 0.0, 11) |
|---|
| 2941 | n/a | |
|---|
| 2942 | n/a | # out of bounds |
|---|
| 2943 | n/a | self.assertRaises(IndexError, m.__setitem__, -8, 20) |
|---|
| 2944 | n/a | self.assertRaises(IndexError, m.__setitem__, 8, 25) |
|---|
| 2945 | n/a | |
|---|
| 2946 | n/a | # pack_single() success: |
|---|
| 2947 | n/a | for fmt in fmtdict['@']: |
|---|
| 2948 | n/a | if fmt == 'c' or fmt == '?': |
|---|
| 2949 | n/a | continue |
|---|
| 2950 | n/a | ex = ndarray([1,2,3], shape=[3], format=fmt, flags=ND_WRITABLE) |
|---|
| 2951 | n/a | m = memoryview(ex) |
|---|
| 2952 | n/a | i = randrange(-3, 3) |
|---|
| 2953 | n/a | m[i] = 8 |
|---|
| 2954 | n/a | self.assertEqual(m[i], 8) |
|---|
| 2955 | n/a | self.assertEqual(m[i], ex[i]) |
|---|
| 2956 | n/a | |
|---|
| 2957 | n/a | ex = ndarray([b'1', b'2', b'3'], shape=[3], format='c', |
|---|
| 2958 | n/a | flags=ND_WRITABLE) |
|---|
| 2959 | n/a | m = memoryview(ex) |
|---|
| 2960 | n/a | m[2] = b'9' |
|---|
| 2961 | n/a | self.assertEqual(m[2], b'9') |
|---|
| 2962 | n/a | |
|---|
| 2963 | n/a | ex = ndarray([True, False, True], shape=[3], format='?', |
|---|
| 2964 | n/a | flags=ND_WRITABLE) |
|---|
| 2965 | n/a | m = memoryview(ex) |
|---|
| 2966 | n/a | m[1] = True |
|---|
| 2967 | n/a | self.assertEqual(m[1], True) |
|---|
| 2968 | n/a | |
|---|
| 2969 | n/a | # pack_single() exceptions: |
|---|
| 2970 | n/a | nd = ndarray([b'x'], shape=[1], format='c', flags=ND_WRITABLE) |
|---|
| 2971 | n/a | m = memoryview(nd) |
|---|
| 2972 | n/a | self.assertRaises(TypeError, m.__setitem__, 0, 100) |
|---|
| 2973 | n/a | |
|---|
| 2974 | n/a | ex = ndarray(list(range(120)), shape=[1,2,3,4,5], flags=ND_WRITABLE) |
|---|
| 2975 | n/a | m1 = memoryview(ex) |
|---|
| 2976 | n/a | |
|---|
| 2977 | n/a | for fmt, _range in fmtdict['@'].items(): |
|---|
| 2978 | n/a | if (fmt == '?'): # PyObject_IsTrue() accepts anything |
|---|
| 2979 | n/a | continue |
|---|
| 2980 | n/a | if fmt == 'c': # special case tested above |
|---|
| 2981 | n/a | continue |
|---|
| 2982 | n/a | m2 = m1.cast(fmt) |
|---|
| 2983 | n/a | lo, hi = _range |
|---|
| 2984 | n/a | if fmt == 'd' or fmt == 'f': |
|---|
| 2985 | n/a | lo, hi = -2**1024, 2**1024 |
|---|
| 2986 | n/a | if fmt != 'P': # PyLong_AsVoidPtr() accepts negative numbers |
|---|
| 2987 | n/a | self.assertRaises(ValueError, m2.__setitem__, 0, lo-1) |
|---|
| 2988 | n/a | self.assertRaises(TypeError, m2.__setitem__, 0, "xyz") |
|---|
| 2989 | n/a | self.assertRaises(ValueError, m2.__setitem__, 0, hi) |
|---|
| 2990 | n/a | |
|---|
| 2991 | n/a | # invalid item |
|---|
| 2992 | n/a | m2 = m1.cast('c') |
|---|
| 2993 | n/a | self.assertRaises(ValueError, m2.__setitem__, 0, b'\xff\xff') |
|---|
| 2994 | n/a | |
|---|
| 2995 | n/a | # format not implemented |
|---|
| 2996 | n/a | ex = ndarray(list(range(1)), shape=[1], format="xL", flags=ND_WRITABLE) |
|---|
| 2997 | n/a | m = memoryview(ex) |
|---|
| 2998 | n/a | self.assertRaises(NotImplementedError, m.__setitem__, 0, 1) |
|---|
| 2999 | n/a | |
|---|
| 3000 | n/a | ex = ndarray([b'12345'], shape=[1], format="s", flags=ND_WRITABLE) |
|---|
| 3001 | n/a | m = memoryview(ex) |
|---|
| 3002 | n/a | self.assertRaises(NotImplementedError, m.__setitem__, 0, 1) |
|---|
| 3003 | n/a | |
|---|
| 3004 | n/a | # multi-dimensional |
|---|
| 3005 | n/a | ex = ndarray(list(range(12)), shape=[3,4], flags=ND_WRITABLE) |
|---|
| 3006 | n/a | m = memoryview(ex) |
|---|
| 3007 | n/a | m[0,1] = 42 |
|---|
| 3008 | n/a | self.assertEqual(ex[0][1], 42) |
|---|
| 3009 | n/a | m[-1,-1] = 43 |
|---|
| 3010 | n/a | self.assertEqual(ex[2][3], 43) |
|---|
| 3011 | n/a | # errors |
|---|
| 3012 | n/a | for index in (3, -4): |
|---|
| 3013 | n/a | with self.assert_out_of_bounds_error(dim=1): |
|---|
| 3014 | n/a | m[index, 0] = 0 |
|---|
| 3015 | n/a | for index in (4, -5): |
|---|
| 3016 | n/a | with self.assert_out_of_bounds_error(dim=2): |
|---|
| 3017 | n/a | m[0, index] = 0 |
|---|
| 3018 | n/a | self.assertRaises(IndexError, m.__setitem__, (2**64, 0), 0) |
|---|
| 3019 | n/a | self.assertRaises(IndexError, m.__setitem__, (0, 2**64), 0) |
|---|
| 3020 | n/a | |
|---|
| 3021 | n/a | self.assertRaises(TypeError, m.__setitem__, (0, 0, 0), 0) |
|---|
| 3022 | n/a | self.assertRaises(TypeError, m.__setitem__, (0.0, 0.0), 0) |
|---|
| 3023 | n/a | |
|---|
| 3024 | n/a | # Not implemented: multidimensional sub-views |
|---|
| 3025 | n/a | self.assertRaises(NotImplementedError, m.__setitem__, 0, [2, 3]) |
|---|
| 3026 | n/a | |
|---|
| 3027 | n/a | def test_memoryview_slice(self): |
|---|
| 3028 | n/a | |
|---|
| 3029 | n/a | ex = ndarray(list(range(12)), shape=[12], flags=ND_WRITABLE) |
|---|
| 3030 | n/a | m = memoryview(ex) |
|---|
| 3031 | n/a | |
|---|
| 3032 | n/a | # zero step |
|---|
| 3033 | n/a | self.assertRaises(ValueError, m.__getitem__, slice(0,2,0)) |
|---|
| 3034 | n/a | self.assertRaises(ValueError, m.__setitem__, slice(0,2,0), |
|---|
| 3035 | n/a | bytearray([1,2])) |
|---|
| 3036 | n/a | |
|---|
| 3037 | n/a | # 0-dim slicing (identity function) |
|---|
| 3038 | n/a | self.assertRaises(NotImplementedError, m.__getitem__, ()) |
|---|
| 3039 | n/a | |
|---|
| 3040 | n/a | # multidimensional slices |
|---|
| 3041 | n/a | ex = ndarray(list(range(12)), shape=[12], flags=ND_WRITABLE) |
|---|
| 3042 | n/a | m = memoryview(ex) |
|---|
| 3043 | n/a | |
|---|
| 3044 | n/a | self.assertRaises(NotImplementedError, m.__getitem__, |
|---|
| 3045 | n/a | (slice(0,2,1), slice(0,2,1))) |
|---|
| 3046 | n/a | self.assertRaises(NotImplementedError, m.__setitem__, |
|---|
| 3047 | n/a | (slice(0,2,1), slice(0,2,1)), bytearray([1,2])) |
|---|
| 3048 | n/a | |
|---|
| 3049 | n/a | # invalid slice tuple |
|---|
| 3050 | n/a | self.assertRaises(TypeError, m.__getitem__, (slice(0,2,1), {})) |
|---|
| 3051 | n/a | self.assertRaises(TypeError, m.__setitem__, (slice(0,2,1), {}), |
|---|
| 3052 | n/a | bytearray([1,2])) |
|---|
| 3053 | n/a | |
|---|
| 3054 | n/a | # rvalue is not an exporter |
|---|
| 3055 | n/a | self.assertRaises(TypeError, m.__setitem__, slice(0,1,1), [1]) |
|---|
| 3056 | n/a | |
|---|
| 3057 | n/a | # non-contiguous slice assignment |
|---|
| 3058 | n/a | for flags in (0, ND_PIL): |
|---|
| 3059 | n/a | ex1 = ndarray(list(range(12)), shape=[12], strides=[-1], offset=11, |
|---|
| 3060 | n/a | flags=ND_WRITABLE|flags) |
|---|
| 3061 | n/a | ex2 = ndarray(list(range(24)), shape=[12], strides=[2], flags=flags) |
|---|
| 3062 | n/a | m1 = memoryview(ex1) |
|---|
| 3063 | n/a | m2 = memoryview(ex2) |
|---|
| 3064 | n/a | |
|---|
| 3065 | n/a | ex1[2:5] = ex1[2:5] |
|---|
| 3066 | n/a | m1[2:5] = m2[2:5] |
|---|
| 3067 | n/a | |
|---|
| 3068 | n/a | self.assertEqual(m1, ex1) |
|---|
| 3069 | n/a | self.assertEqual(m2, ex2) |
|---|
| 3070 | n/a | |
|---|
| 3071 | n/a | ex1[1:3][::-1] = ex2[0:2][::1] |
|---|
| 3072 | n/a | m1[1:3][::-1] = m2[0:2][::1] |
|---|
| 3073 | n/a | |
|---|
| 3074 | n/a | self.assertEqual(m1, ex1) |
|---|
| 3075 | n/a | self.assertEqual(m2, ex2) |
|---|
| 3076 | n/a | |
|---|
| 3077 | n/a | ex1[4:1:-2][::-1] = ex1[1:4:2][::1] |
|---|
| 3078 | n/a | m1[4:1:-2][::-1] = m1[1:4:2][::1] |
|---|
| 3079 | n/a | |
|---|
| 3080 | n/a | self.assertEqual(m1, ex1) |
|---|
| 3081 | n/a | self.assertEqual(m2, ex2) |
|---|
| 3082 | n/a | |
|---|
| 3083 | n/a | def test_memoryview_array(self): |
|---|
| 3084 | n/a | |
|---|
| 3085 | n/a | def cmptest(testcase, a, b, m, singleitem): |
|---|
| 3086 | n/a | for i, _ in enumerate(a): |
|---|
| 3087 | n/a | ai = a[i] |
|---|
| 3088 | n/a | mi = m[i] |
|---|
| 3089 | n/a | testcase.assertEqual(ai, mi) |
|---|
| 3090 | n/a | a[i] = singleitem |
|---|
| 3091 | n/a | if singleitem != ai: |
|---|
| 3092 | n/a | testcase.assertNotEqual(a, m) |
|---|
| 3093 | n/a | testcase.assertNotEqual(a, b) |
|---|
| 3094 | n/a | else: |
|---|
| 3095 | n/a | testcase.assertEqual(a, m) |
|---|
| 3096 | n/a | testcase.assertEqual(a, b) |
|---|
| 3097 | n/a | m[i] = singleitem |
|---|
| 3098 | n/a | testcase.assertEqual(a, m) |
|---|
| 3099 | n/a | testcase.assertEqual(b, m) |
|---|
| 3100 | n/a | a[i] = ai |
|---|
| 3101 | n/a | m[i] = mi |
|---|
| 3102 | n/a | |
|---|
| 3103 | n/a | for n in range(1, 5): |
|---|
| 3104 | n/a | for fmt, items, singleitem in iter_format(n, 'array'): |
|---|
| 3105 | n/a | for lslice in genslices(n): |
|---|
| 3106 | n/a | for rslice in genslices(n): |
|---|
| 3107 | n/a | |
|---|
| 3108 | n/a | a = array.array(fmt, items) |
|---|
| 3109 | n/a | b = array.array(fmt, items) |
|---|
| 3110 | n/a | m = memoryview(b) |
|---|
| 3111 | n/a | |
|---|
| 3112 | n/a | self.assertEqual(m, a) |
|---|
| 3113 | n/a | self.assertEqual(m.tolist(), a.tolist()) |
|---|
| 3114 | n/a | self.assertEqual(m.tobytes(), a.tobytes()) |
|---|
| 3115 | n/a | self.assertEqual(len(m), len(a)) |
|---|
| 3116 | n/a | |
|---|
| 3117 | n/a | cmptest(self, a, b, m, singleitem) |
|---|
| 3118 | n/a | |
|---|
| 3119 | n/a | array_err = None |
|---|
| 3120 | n/a | have_resize = None |
|---|
| 3121 | n/a | try: |
|---|
| 3122 | n/a | al = a[lslice] |
|---|
| 3123 | n/a | ar = a[rslice] |
|---|
| 3124 | n/a | a[lslice] = a[rslice] |
|---|
| 3125 | n/a | have_resize = len(al) != len(ar) |
|---|
| 3126 | n/a | except Exception as e: |
|---|
| 3127 | n/a | array_err = e.__class__ |
|---|
| 3128 | n/a | |
|---|
| 3129 | n/a | m_err = None |
|---|
| 3130 | n/a | try: |
|---|
| 3131 | n/a | m[lslice] = m[rslice] |
|---|
| 3132 | n/a | except Exception as e: |
|---|
| 3133 | n/a | m_err = e.__class__ |
|---|
| 3134 | n/a | |
|---|
| 3135 | n/a | if have_resize: # memoryview cannot change shape |
|---|
| 3136 | n/a | self.assertIs(m_err, ValueError) |
|---|
| 3137 | n/a | elif m_err or array_err: |
|---|
| 3138 | n/a | self.assertIs(m_err, array_err) |
|---|
| 3139 | n/a | else: |
|---|
| 3140 | n/a | self.assertEqual(m, a) |
|---|
| 3141 | n/a | self.assertEqual(m.tolist(), a.tolist()) |
|---|
| 3142 | n/a | self.assertEqual(m.tobytes(), a.tobytes()) |
|---|
| 3143 | n/a | cmptest(self, a, b, m, singleitem) |
|---|
| 3144 | n/a | |
|---|
| 3145 | n/a | def test_memoryview_compare_special_cases(self): |
|---|
| 3146 | n/a | |
|---|
| 3147 | n/a | a = array.array('L', [1, 2, 3]) |
|---|
| 3148 | n/a | b = array.array('L', [1, 2, 7]) |
|---|
| 3149 | n/a | |
|---|
| 3150 | n/a | # Ordering comparisons raise: |
|---|
| 3151 | n/a | v = memoryview(a) |
|---|
| 3152 | n/a | w = memoryview(b) |
|---|
| 3153 | n/a | for attr in ('__lt__', '__le__', '__gt__', '__ge__'): |
|---|
| 3154 | n/a | self.assertIs(getattr(v, attr)(w), NotImplemented) |
|---|
| 3155 | n/a | self.assertIs(getattr(a, attr)(v), NotImplemented) |
|---|
| 3156 | n/a | |
|---|
| 3157 | n/a | # Released views compare equal to themselves: |
|---|
| 3158 | n/a | v = memoryview(a) |
|---|
| 3159 | n/a | v.release() |
|---|
| 3160 | n/a | self.assertEqual(v, v) |
|---|
| 3161 | n/a | self.assertNotEqual(v, a) |
|---|
| 3162 | n/a | self.assertNotEqual(a, v) |
|---|
| 3163 | n/a | |
|---|
| 3164 | n/a | v = memoryview(a) |
|---|
| 3165 | n/a | w = memoryview(a) |
|---|
| 3166 | n/a | w.release() |
|---|
| 3167 | n/a | self.assertNotEqual(v, w) |
|---|
| 3168 | n/a | self.assertNotEqual(w, v) |
|---|
| 3169 | n/a | |
|---|
| 3170 | n/a | # Operand does not implement the buffer protocol: |
|---|
| 3171 | n/a | v = memoryview(a) |
|---|
| 3172 | n/a | self.assertNotEqual(v, [1, 2, 3]) |
|---|
| 3173 | n/a | |
|---|
| 3174 | n/a | # NaNs |
|---|
| 3175 | n/a | nd = ndarray([(0, 0)], shape=[1], format='l x d x', flags=ND_WRITABLE) |
|---|
| 3176 | n/a | nd[0] = (-1, float('nan')) |
|---|
| 3177 | n/a | self.assertNotEqual(memoryview(nd), nd) |
|---|
| 3178 | n/a | |
|---|
| 3179 | n/a | # Depends on issue #15625: the struct module does not understand 'u'. |
|---|
| 3180 | n/a | a = array.array('u', 'xyz') |
|---|
| 3181 | n/a | v = memoryview(a) |
|---|
| 3182 | n/a | self.assertNotEqual(a, v) |
|---|
| 3183 | n/a | self.assertNotEqual(v, a) |
|---|
| 3184 | n/a | |
|---|
| 3185 | n/a | # Some ctypes format strings are unknown to the struct module. |
|---|
| 3186 | n/a | if ctypes: |
|---|
| 3187 | n/a | # format: "T{>l:x:>l:y:}" |
|---|
| 3188 | n/a | class BEPoint(ctypes.BigEndianStructure): |
|---|
| 3189 | n/a | _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)] |
|---|
| 3190 | n/a | point = BEPoint(100, 200) |
|---|
| 3191 | n/a | a = memoryview(point) |
|---|
| 3192 | n/a | b = memoryview(point) |
|---|
| 3193 | n/a | self.assertNotEqual(a, b) |
|---|
| 3194 | n/a | self.assertNotEqual(a, point) |
|---|
| 3195 | n/a | self.assertNotEqual(point, a) |
|---|
| 3196 | n/a | self.assertRaises(NotImplementedError, a.tolist) |
|---|
| 3197 | n/a | |
|---|
| 3198 | n/a | def test_memoryview_compare_ndim_zero(self): |
|---|
| 3199 | n/a | |
|---|
| 3200 | n/a | nd1 = ndarray(1729, shape=[], format='@L') |
|---|
| 3201 | n/a | nd2 = ndarray(1729, shape=[], format='L', flags=ND_WRITABLE) |
|---|
| 3202 | n/a | v = memoryview(nd1) |
|---|
| 3203 | n/a | w = memoryview(nd2) |
|---|
| 3204 | n/a | self.assertEqual(v, w) |
|---|
| 3205 | n/a | self.assertEqual(w, v) |
|---|
| 3206 | n/a | self.assertEqual(v, nd2) |
|---|
| 3207 | n/a | self.assertEqual(nd2, v) |
|---|
| 3208 | n/a | self.assertEqual(w, nd1) |
|---|
| 3209 | n/a | self.assertEqual(nd1, w) |
|---|
| 3210 | n/a | |
|---|
| 3211 | n/a | self.assertFalse(v.__ne__(w)) |
|---|
| 3212 | n/a | self.assertFalse(w.__ne__(v)) |
|---|
| 3213 | n/a | |
|---|
| 3214 | n/a | w[()] = 1728 |
|---|
| 3215 | n/a | self.assertNotEqual(v, w) |
|---|
| 3216 | n/a | self.assertNotEqual(w, v) |
|---|
| 3217 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3218 | n/a | self.assertNotEqual(nd2, v) |
|---|
| 3219 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3220 | n/a | self.assertNotEqual(nd1, w) |
|---|
| 3221 | n/a | |
|---|
| 3222 | n/a | self.assertFalse(v.__eq__(w)) |
|---|
| 3223 | n/a | self.assertFalse(w.__eq__(v)) |
|---|
| 3224 | n/a | |
|---|
| 3225 | n/a | nd = ndarray(list(range(12)), shape=[12], flags=ND_WRITABLE|ND_PIL) |
|---|
| 3226 | n/a | ex = ndarray(list(range(12)), shape=[12], flags=ND_WRITABLE|ND_PIL) |
|---|
| 3227 | n/a | m = memoryview(ex) |
|---|
| 3228 | n/a | |
|---|
| 3229 | n/a | self.assertEqual(m, nd) |
|---|
| 3230 | n/a | m[9] = 100 |
|---|
| 3231 | n/a | self.assertNotEqual(m, nd) |
|---|
| 3232 | n/a | |
|---|
| 3233 | n/a | # struct module: equal |
|---|
| 3234 | n/a | nd1 = ndarray((1729, 1.2, b'12345'), shape=[], format='Lf5s') |
|---|
| 3235 | n/a | nd2 = ndarray((1729, 1.2, b'12345'), shape=[], format='hf5s', |
|---|
| 3236 | n/a | flags=ND_WRITABLE) |
|---|
| 3237 | n/a | v = memoryview(nd1) |
|---|
| 3238 | n/a | w = memoryview(nd2) |
|---|
| 3239 | n/a | self.assertEqual(v, w) |
|---|
| 3240 | n/a | self.assertEqual(w, v) |
|---|
| 3241 | n/a | self.assertEqual(v, nd2) |
|---|
| 3242 | n/a | self.assertEqual(nd2, v) |
|---|
| 3243 | n/a | self.assertEqual(w, nd1) |
|---|
| 3244 | n/a | self.assertEqual(nd1, w) |
|---|
| 3245 | n/a | |
|---|
| 3246 | n/a | # struct module: not equal |
|---|
| 3247 | n/a | nd1 = ndarray((1729, 1.2, b'12345'), shape=[], format='Lf5s') |
|---|
| 3248 | n/a | nd2 = ndarray((-1729, 1.2, b'12345'), shape=[], format='hf5s', |
|---|
| 3249 | n/a | flags=ND_WRITABLE) |
|---|
| 3250 | n/a | v = memoryview(nd1) |
|---|
| 3251 | n/a | w = memoryview(nd2) |
|---|
| 3252 | n/a | self.assertNotEqual(v, w) |
|---|
| 3253 | n/a | self.assertNotEqual(w, v) |
|---|
| 3254 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3255 | n/a | self.assertNotEqual(nd2, v) |
|---|
| 3256 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3257 | n/a | self.assertNotEqual(nd1, w) |
|---|
| 3258 | n/a | self.assertEqual(v, nd1) |
|---|
| 3259 | n/a | self.assertEqual(w, nd2) |
|---|
| 3260 | n/a | |
|---|
| 3261 | n/a | def test_memoryview_compare_ndim_one(self): |
|---|
| 3262 | n/a | |
|---|
| 3263 | n/a | # contiguous |
|---|
| 3264 | n/a | nd1 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='@h') |
|---|
| 3265 | n/a | nd2 = ndarray([-529, 576, -625, 676, 729], shape=[5], format='@h') |
|---|
| 3266 | n/a | v = memoryview(nd1) |
|---|
| 3267 | n/a | w = memoryview(nd2) |
|---|
| 3268 | n/a | |
|---|
| 3269 | n/a | self.assertEqual(v, nd1) |
|---|
| 3270 | n/a | self.assertEqual(w, nd2) |
|---|
| 3271 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3272 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3273 | n/a | self.assertNotEqual(v, w) |
|---|
| 3274 | n/a | |
|---|
| 3275 | n/a | # contiguous, struct module |
|---|
| 3276 | n/a | nd1 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='<i') |
|---|
| 3277 | n/a | nd2 = ndarray([-529, 576, -625, 676, 729], shape=[5], format='>h') |
|---|
| 3278 | n/a | v = memoryview(nd1) |
|---|
| 3279 | n/a | w = memoryview(nd2) |
|---|
| 3280 | n/a | |
|---|
| 3281 | n/a | self.assertEqual(v, nd1) |
|---|
| 3282 | n/a | self.assertEqual(w, nd2) |
|---|
| 3283 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3284 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3285 | n/a | self.assertNotEqual(v, w) |
|---|
| 3286 | n/a | |
|---|
| 3287 | n/a | # non-contiguous |
|---|
| 3288 | n/a | nd1 = ndarray([-529, -625, -729], shape=[3], format='@h') |
|---|
| 3289 | n/a | nd2 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='@h') |
|---|
| 3290 | n/a | v = memoryview(nd1) |
|---|
| 3291 | n/a | w = memoryview(nd2) |
|---|
| 3292 | n/a | |
|---|
| 3293 | n/a | self.assertEqual(v, nd2[::2]) |
|---|
| 3294 | n/a | self.assertEqual(w[::2], nd1) |
|---|
| 3295 | n/a | self.assertEqual(v, w[::2]) |
|---|
| 3296 | n/a | self.assertEqual(v[::-1], w[::-2]) |
|---|
| 3297 | n/a | |
|---|
| 3298 | n/a | # non-contiguous, struct module |
|---|
| 3299 | n/a | nd1 = ndarray([-529, -625, -729], shape=[3], format='!h') |
|---|
| 3300 | n/a | nd2 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='<l') |
|---|
| 3301 | n/a | v = memoryview(nd1) |
|---|
| 3302 | n/a | w = memoryview(nd2) |
|---|
| 3303 | n/a | |
|---|
| 3304 | n/a | self.assertEqual(v, nd2[::2]) |
|---|
| 3305 | n/a | self.assertEqual(w[::2], nd1) |
|---|
| 3306 | n/a | self.assertEqual(v, w[::2]) |
|---|
| 3307 | n/a | self.assertEqual(v[::-1], w[::-2]) |
|---|
| 3308 | n/a | |
|---|
| 3309 | n/a | # non-contiguous, suboffsets |
|---|
| 3310 | n/a | nd1 = ndarray([-529, -625, -729], shape=[3], format='@h') |
|---|
| 3311 | n/a | nd2 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='@h', |
|---|
| 3312 | n/a | flags=ND_PIL) |
|---|
| 3313 | n/a | v = memoryview(nd1) |
|---|
| 3314 | n/a | w = memoryview(nd2) |
|---|
| 3315 | n/a | |
|---|
| 3316 | n/a | self.assertEqual(v, nd2[::2]) |
|---|
| 3317 | n/a | self.assertEqual(w[::2], nd1) |
|---|
| 3318 | n/a | self.assertEqual(v, w[::2]) |
|---|
| 3319 | n/a | self.assertEqual(v[::-1], w[::-2]) |
|---|
| 3320 | n/a | |
|---|
| 3321 | n/a | # non-contiguous, suboffsets, struct module |
|---|
| 3322 | n/a | nd1 = ndarray([-529, -625, -729], shape=[3], format='h 0c') |
|---|
| 3323 | n/a | nd2 = ndarray([-529, 576, -625, 676, -729], shape=[5], format='> h', |
|---|
| 3324 | n/a | flags=ND_PIL) |
|---|
| 3325 | n/a | v = memoryview(nd1) |
|---|
| 3326 | n/a | w = memoryview(nd2) |
|---|
| 3327 | n/a | |
|---|
| 3328 | n/a | self.assertEqual(v, nd2[::2]) |
|---|
| 3329 | n/a | self.assertEqual(w[::2], nd1) |
|---|
| 3330 | n/a | self.assertEqual(v, w[::2]) |
|---|
| 3331 | n/a | self.assertEqual(v[::-1], w[::-2]) |
|---|
| 3332 | n/a | |
|---|
| 3333 | n/a | def test_memoryview_compare_zero_shape(self): |
|---|
| 3334 | n/a | |
|---|
| 3335 | n/a | # zeros in shape |
|---|
| 3336 | n/a | nd1 = ndarray([900, 961], shape=[0], format='@h') |
|---|
| 3337 | n/a | nd2 = ndarray([-900, -961], shape=[0], format='@h') |
|---|
| 3338 | n/a | v = memoryview(nd1) |
|---|
| 3339 | n/a | w = memoryview(nd2) |
|---|
| 3340 | n/a | |
|---|
| 3341 | n/a | self.assertEqual(v, nd1) |
|---|
| 3342 | n/a | self.assertEqual(w, nd2) |
|---|
| 3343 | n/a | self.assertEqual(v, nd2) |
|---|
| 3344 | n/a | self.assertEqual(w, nd1) |
|---|
| 3345 | n/a | self.assertEqual(v, w) |
|---|
| 3346 | n/a | |
|---|
| 3347 | n/a | # zeros in shape, struct module |
|---|
| 3348 | n/a | nd1 = ndarray([900, 961], shape=[0], format='= h0c') |
|---|
| 3349 | n/a | nd2 = ndarray([-900, -961], shape=[0], format='@ i') |
|---|
| 3350 | n/a | v = memoryview(nd1) |
|---|
| 3351 | n/a | w = memoryview(nd2) |
|---|
| 3352 | n/a | |
|---|
| 3353 | n/a | self.assertEqual(v, nd1) |
|---|
| 3354 | n/a | self.assertEqual(w, nd2) |
|---|
| 3355 | n/a | self.assertEqual(v, nd2) |
|---|
| 3356 | n/a | self.assertEqual(w, nd1) |
|---|
| 3357 | n/a | self.assertEqual(v, w) |
|---|
| 3358 | n/a | |
|---|
| 3359 | n/a | def test_memoryview_compare_zero_strides(self): |
|---|
| 3360 | n/a | |
|---|
| 3361 | n/a | # zero strides |
|---|
| 3362 | n/a | nd1 = ndarray([900, 900, 900, 900], shape=[4], format='@L') |
|---|
| 3363 | n/a | nd2 = ndarray([900], shape=[4], strides=[0], format='L') |
|---|
| 3364 | n/a | v = memoryview(nd1) |
|---|
| 3365 | n/a | w = memoryview(nd2) |
|---|
| 3366 | n/a | |
|---|
| 3367 | n/a | self.assertEqual(v, nd1) |
|---|
| 3368 | n/a | self.assertEqual(w, nd2) |
|---|
| 3369 | n/a | self.assertEqual(v, nd2) |
|---|
| 3370 | n/a | self.assertEqual(w, nd1) |
|---|
| 3371 | n/a | self.assertEqual(v, w) |
|---|
| 3372 | n/a | |
|---|
| 3373 | n/a | # zero strides, struct module |
|---|
| 3374 | n/a | nd1 = ndarray([(900, 900)]*4, shape=[4], format='@ Li') |
|---|
| 3375 | n/a | nd2 = ndarray([(900, 900)], shape=[4], strides=[0], format='!L h') |
|---|
| 3376 | n/a | v = memoryview(nd1) |
|---|
| 3377 | n/a | w = memoryview(nd2) |
|---|
| 3378 | n/a | |
|---|
| 3379 | n/a | self.assertEqual(v, nd1) |
|---|
| 3380 | n/a | self.assertEqual(w, nd2) |
|---|
| 3381 | n/a | self.assertEqual(v, nd2) |
|---|
| 3382 | n/a | self.assertEqual(w, nd1) |
|---|
| 3383 | n/a | self.assertEqual(v, w) |
|---|
| 3384 | n/a | |
|---|
| 3385 | n/a | def test_memoryview_compare_random_formats(self): |
|---|
| 3386 | n/a | |
|---|
| 3387 | n/a | # random single character native formats |
|---|
| 3388 | n/a | n = 10 |
|---|
| 3389 | n/a | for char in fmtdict['@m']: |
|---|
| 3390 | n/a | fmt, items, singleitem = randitems(n, 'memoryview', '@', char) |
|---|
| 3391 | n/a | for flags in (0, ND_PIL): |
|---|
| 3392 | n/a | nd = ndarray(items, shape=[n], format=fmt, flags=flags) |
|---|
| 3393 | n/a | m = memoryview(nd) |
|---|
| 3394 | n/a | self.assertEqual(m, nd) |
|---|
| 3395 | n/a | |
|---|
| 3396 | n/a | nd = nd[::-3] |
|---|
| 3397 | n/a | m = memoryview(nd) |
|---|
| 3398 | n/a | self.assertEqual(m, nd) |
|---|
| 3399 | n/a | |
|---|
| 3400 | n/a | # random formats |
|---|
| 3401 | n/a | n = 10 |
|---|
| 3402 | n/a | for _ in range(100): |
|---|
| 3403 | n/a | fmt, items, singleitem = randitems(n) |
|---|
| 3404 | n/a | for flags in (0, ND_PIL): |
|---|
| 3405 | n/a | nd = ndarray(items, shape=[n], format=fmt, flags=flags) |
|---|
| 3406 | n/a | m = memoryview(nd) |
|---|
| 3407 | n/a | self.assertEqual(m, nd) |
|---|
| 3408 | n/a | |
|---|
| 3409 | n/a | nd = nd[::-3] |
|---|
| 3410 | n/a | m = memoryview(nd) |
|---|
| 3411 | n/a | self.assertEqual(m, nd) |
|---|
| 3412 | n/a | |
|---|
| 3413 | n/a | def test_memoryview_compare_multidim_c(self): |
|---|
| 3414 | n/a | |
|---|
| 3415 | n/a | # C-contiguous, different values |
|---|
| 3416 | n/a | nd1 = ndarray(list(range(-15, 15)), shape=[3, 2, 5], format='@h') |
|---|
| 3417 | n/a | nd2 = ndarray(list(range(0, 30)), shape=[3, 2, 5], format='@h') |
|---|
| 3418 | n/a | v = memoryview(nd1) |
|---|
| 3419 | n/a | w = memoryview(nd2) |
|---|
| 3420 | n/a | |
|---|
| 3421 | n/a | self.assertEqual(v, nd1) |
|---|
| 3422 | n/a | self.assertEqual(w, nd2) |
|---|
| 3423 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3424 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3425 | n/a | self.assertNotEqual(v, w) |
|---|
| 3426 | n/a | |
|---|
| 3427 | n/a | # C-contiguous, different values, struct module |
|---|
| 3428 | n/a | nd1 = ndarray([(0, 1, 2)]*30, shape=[3, 2, 5], format='=f q xxL') |
|---|
| 3429 | n/a | nd2 = ndarray([(-1.2, 1, 2)]*30, shape=[3, 2, 5], format='< f 2Q') |
|---|
| 3430 | n/a | v = memoryview(nd1) |
|---|
| 3431 | n/a | w = memoryview(nd2) |
|---|
| 3432 | n/a | |
|---|
| 3433 | n/a | self.assertEqual(v, nd1) |
|---|
| 3434 | n/a | self.assertEqual(w, nd2) |
|---|
| 3435 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3436 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3437 | n/a | self.assertNotEqual(v, w) |
|---|
| 3438 | n/a | |
|---|
| 3439 | n/a | # C-contiguous, different shape |
|---|
| 3440 | n/a | nd1 = ndarray(list(range(30)), shape=[2, 3, 5], format='L') |
|---|
| 3441 | n/a | nd2 = ndarray(list(range(30)), shape=[3, 2, 5], format='L') |
|---|
| 3442 | n/a | v = memoryview(nd1) |
|---|
| 3443 | n/a | w = memoryview(nd2) |
|---|
| 3444 | n/a | |
|---|
| 3445 | n/a | self.assertEqual(v, nd1) |
|---|
| 3446 | n/a | self.assertEqual(w, nd2) |
|---|
| 3447 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3448 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3449 | n/a | self.assertNotEqual(v, w) |
|---|
| 3450 | n/a | |
|---|
| 3451 | n/a | # C-contiguous, different shape, struct module |
|---|
| 3452 | n/a | nd1 = ndarray([(0, 1, 2)]*21, shape=[3, 7], format='! b B xL') |
|---|
| 3453 | n/a | nd2 = ndarray([(0, 1, 2)]*21, shape=[7, 3], format='= Qx l xxL') |
|---|
| 3454 | n/a | v = memoryview(nd1) |
|---|
| 3455 | n/a | w = memoryview(nd2) |
|---|
| 3456 | n/a | |
|---|
| 3457 | n/a | self.assertEqual(v, nd1) |
|---|
| 3458 | n/a | self.assertEqual(w, nd2) |
|---|
| 3459 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3460 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3461 | n/a | self.assertNotEqual(v, w) |
|---|
| 3462 | n/a | |
|---|
| 3463 | n/a | # C-contiguous, different format, struct module |
|---|
| 3464 | n/a | nd1 = ndarray(list(range(30)), shape=[2, 3, 5], format='L') |
|---|
| 3465 | n/a | nd2 = ndarray(list(range(30)), shape=[2, 3, 5], format='l') |
|---|
| 3466 | n/a | v = memoryview(nd1) |
|---|
| 3467 | n/a | w = memoryview(nd2) |
|---|
| 3468 | n/a | |
|---|
| 3469 | n/a | self.assertEqual(v, nd1) |
|---|
| 3470 | n/a | self.assertEqual(w, nd2) |
|---|
| 3471 | n/a | self.assertEqual(v, nd2) |
|---|
| 3472 | n/a | self.assertEqual(w, nd1) |
|---|
| 3473 | n/a | self.assertEqual(v, w) |
|---|
| 3474 | n/a | |
|---|
| 3475 | n/a | def test_memoryview_compare_multidim_fortran(self): |
|---|
| 3476 | n/a | |
|---|
| 3477 | n/a | # Fortran-contiguous, different values |
|---|
| 3478 | n/a | nd1 = ndarray(list(range(-15, 15)), shape=[5, 2, 3], format='@h', |
|---|
| 3479 | n/a | flags=ND_FORTRAN) |
|---|
| 3480 | n/a | nd2 = ndarray(list(range(0, 30)), shape=[5, 2, 3], format='@h', |
|---|
| 3481 | n/a | flags=ND_FORTRAN) |
|---|
| 3482 | n/a | v = memoryview(nd1) |
|---|
| 3483 | n/a | w = memoryview(nd2) |
|---|
| 3484 | n/a | |
|---|
| 3485 | n/a | self.assertEqual(v, nd1) |
|---|
| 3486 | n/a | self.assertEqual(w, nd2) |
|---|
| 3487 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3488 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3489 | n/a | self.assertNotEqual(v, w) |
|---|
| 3490 | n/a | |
|---|
| 3491 | n/a | # Fortran-contiguous, different values, struct module |
|---|
| 3492 | n/a | nd1 = ndarray([(2**64-1, -1)]*6, shape=[2, 3], format='=Qq', |
|---|
| 3493 | n/a | flags=ND_FORTRAN) |
|---|
| 3494 | n/a | nd2 = ndarray([(-1, 2**64-1)]*6, shape=[2, 3], format='=qQ', |
|---|
| 3495 | n/a | flags=ND_FORTRAN) |
|---|
| 3496 | n/a | v = memoryview(nd1) |
|---|
| 3497 | n/a | w = memoryview(nd2) |
|---|
| 3498 | n/a | |
|---|
| 3499 | n/a | self.assertEqual(v, nd1) |
|---|
| 3500 | n/a | self.assertEqual(w, nd2) |
|---|
| 3501 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3502 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3503 | n/a | self.assertNotEqual(v, w) |
|---|
| 3504 | n/a | |
|---|
| 3505 | n/a | # Fortran-contiguous, different shape |
|---|
| 3506 | n/a | nd1 = ndarray(list(range(-15, 15)), shape=[2, 3, 5], format='l', |
|---|
| 3507 | n/a | flags=ND_FORTRAN) |
|---|
| 3508 | n/a | nd2 = ndarray(list(range(-15, 15)), shape=[3, 2, 5], format='l', |
|---|
| 3509 | n/a | flags=ND_FORTRAN) |
|---|
| 3510 | n/a | v = memoryview(nd1) |
|---|
| 3511 | n/a | w = memoryview(nd2) |
|---|
| 3512 | n/a | |
|---|
| 3513 | n/a | self.assertEqual(v, nd1) |
|---|
| 3514 | n/a | self.assertEqual(w, nd2) |
|---|
| 3515 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3516 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3517 | n/a | self.assertNotEqual(v, w) |
|---|
| 3518 | n/a | |
|---|
| 3519 | n/a | # Fortran-contiguous, different shape, struct module |
|---|
| 3520 | n/a | nd1 = ndarray(list(range(-15, 15)), shape=[2, 3, 5], format='0ll', |
|---|
| 3521 | n/a | flags=ND_FORTRAN) |
|---|
| 3522 | n/a | nd2 = ndarray(list(range(-15, 15)), shape=[3, 2, 5], format='l', |
|---|
| 3523 | n/a | flags=ND_FORTRAN) |
|---|
| 3524 | n/a | v = memoryview(nd1) |
|---|
| 3525 | n/a | w = memoryview(nd2) |
|---|
| 3526 | n/a | |
|---|
| 3527 | n/a | self.assertEqual(v, nd1) |
|---|
| 3528 | n/a | self.assertEqual(w, nd2) |
|---|
| 3529 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3530 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3531 | n/a | self.assertNotEqual(v, w) |
|---|
| 3532 | n/a | |
|---|
| 3533 | n/a | # Fortran-contiguous, different format, struct module |
|---|
| 3534 | n/a | nd1 = ndarray(list(range(30)), shape=[5, 2, 3], format='@h', |
|---|
| 3535 | n/a | flags=ND_FORTRAN) |
|---|
| 3536 | n/a | nd2 = ndarray(list(range(30)), shape=[5, 2, 3], format='@b', |
|---|
| 3537 | n/a | flags=ND_FORTRAN) |
|---|
| 3538 | n/a | v = memoryview(nd1) |
|---|
| 3539 | n/a | w = memoryview(nd2) |
|---|
| 3540 | n/a | |
|---|
| 3541 | n/a | self.assertEqual(v, nd1) |
|---|
| 3542 | n/a | self.assertEqual(w, nd2) |
|---|
| 3543 | n/a | self.assertEqual(v, nd2) |
|---|
| 3544 | n/a | self.assertEqual(w, nd1) |
|---|
| 3545 | n/a | self.assertEqual(v, w) |
|---|
| 3546 | n/a | |
|---|
| 3547 | n/a | def test_memoryview_compare_multidim_mixed(self): |
|---|
| 3548 | n/a | |
|---|
| 3549 | n/a | # mixed C/Fortran contiguous |
|---|
| 3550 | n/a | lst1 = list(range(-15, 15)) |
|---|
| 3551 | n/a | lst2 = transpose(lst1, [3, 2, 5]) |
|---|
| 3552 | n/a | nd1 = ndarray(lst1, shape=[3, 2, 5], format='@l') |
|---|
| 3553 | n/a | nd2 = ndarray(lst2, shape=[3, 2, 5], format='l', flags=ND_FORTRAN) |
|---|
| 3554 | n/a | v = memoryview(nd1) |
|---|
| 3555 | n/a | w = memoryview(nd2) |
|---|
| 3556 | n/a | |
|---|
| 3557 | n/a | self.assertEqual(v, nd1) |
|---|
| 3558 | n/a | self.assertEqual(w, nd2) |
|---|
| 3559 | n/a | self.assertEqual(v, w) |
|---|
| 3560 | n/a | |
|---|
| 3561 | n/a | # mixed C/Fortran contiguous, struct module |
|---|
| 3562 | n/a | lst1 = [(-3.3, -22, b'x')]*30 |
|---|
| 3563 | n/a | lst1[5] = (-2.2, -22, b'x') |
|---|
| 3564 | n/a | lst2 = transpose(lst1, [3, 2, 5]) |
|---|
| 3565 | n/a | nd1 = ndarray(lst1, shape=[3, 2, 5], format='d b c') |
|---|
| 3566 | n/a | nd2 = ndarray(lst2, shape=[3, 2, 5], format='d h c', flags=ND_FORTRAN) |
|---|
| 3567 | n/a | v = memoryview(nd1) |
|---|
| 3568 | n/a | w = memoryview(nd2) |
|---|
| 3569 | n/a | |
|---|
| 3570 | n/a | self.assertEqual(v, nd1) |
|---|
| 3571 | n/a | self.assertEqual(w, nd2) |
|---|
| 3572 | n/a | self.assertEqual(v, w) |
|---|
| 3573 | n/a | |
|---|
| 3574 | n/a | # different values, non-contiguous |
|---|
| 3575 | n/a | ex1 = ndarray(list(range(40)), shape=[5, 8], format='@I') |
|---|
| 3576 | n/a | nd1 = ex1[3:1:-1, ::-2] |
|---|
| 3577 | n/a | ex2 = ndarray(list(range(40)), shape=[5, 8], format='I') |
|---|
| 3578 | n/a | nd2 = ex2[1:3:1, ::-2] |
|---|
| 3579 | n/a | v = memoryview(nd1) |
|---|
| 3580 | n/a | w = memoryview(nd2) |
|---|
| 3581 | n/a | |
|---|
| 3582 | n/a | self.assertEqual(v, nd1) |
|---|
| 3583 | n/a | self.assertEqual(w, nd2) |
|---|
| 3584 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3585 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3586 | n/a | self.assertNotEqual(v, w) |
|---|
| 3587 | n/a | |
|---|
| 3588 | n/a | # same values, non-contiguous, struct module |
|---|
| 3589 | n/a | ex1 = ndarray([(2**31-1, -2**31)]*22, shape=[11, 2], format='=ii') |
|---|
| 3590 | n/a | nd1 = ex1[3:1:-1, ::-2] |
|---|
| 3591 | n/a | ex2 = ndarray([(2**31-1, -2**31)]*22, shape=[11, 2], format='>ii') |
|---|
| 3592 | n/a | nd2 = ex2[1:3:1, ::-2] |
|---|
| 3593 | n/a | v = memoryview(nd1) |
|---|
| 3594 | n/a | w = memoryview(nd2) |
|---|
| 3595 | n/a | |
|---|
| 3596 | n/a | self.assertEqual(v, nd1) |
|---|
| 3597 | n/a | self.assertEqual(w, nd2) |
|---|
| 3598 | n/a | self.assertEqual(v, nd2) |
|---|
| 3599 | n/a | self.assertEqual(w, nd1) |
|---|
| 3600 | n/a | self.assertEqual(v, w) |
|---|
| 3601 | n/a | |
|---|
| 3602 | n/a | # different shape |
|---|
| 3603 | n/a | ex1 = ndarray(list(range(30)), shape=[2, 3, 5], format='b') |
|---|
| 3604 | n/a | nd1 = ex1[1:3:, ::-2] |
|---|
| 3605 | n/a | nd2 = ndarray(list(range(30)), shape=[3, 2, 5], format='b') |
|---|
| 3606 | n/a | nd2 = ex2[1:3:, ::-2] |
|---|
| 3607 | n/a | v = memoryview(nd1) |
|---|
| 3608 | n/a | w = memoryview(nd2) |
|---|
| 3609 | n/a | |
|---|
| 3610 | n/a | self.assertEqual(v, nd1) |
|---|
| 3611 | n/a | self.assertEqual(w, nd2) |
|---|
| 3612 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3613 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3614 | n/a | self.assertNotEqual(v, w) |
|---|
| 3615 | n/a | |
|---|
| 3616 | n/a | # different shape, struct module |
|---|
| 3617 | n/a | ex1 = ndarray(list(range(30)), shape=[2, 3, 5], format='B') |
|---|
| 3618 | n/a | nd1 = ex1[1:3:, ::-2] |
|---|
| 3619 | n/a | nd2 = ndarray(list(range(30)), shape=[3, 2, 5], format='b') |
|---|
| 3620 | n/a | nd2 = ex2[1:3:, ::-2] |
|---|
| 3621 | n/a | v = memoryview(nd1) |
|---|
| 3622 | n/a | w = memoryview(nd2) |
|---|
| 3623 | n/a | |
|---|
| 3624 | n/a | self.assertEqual(v, nd1) |
|---|
| 3625 | n/a | self.assertEqual(w, nd2) |
|---|
| 3626 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3627 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3628 | n/a | self.assertNotEqual(v, w) |
|---|
| 3629 | n/a | |
|---|
| 3630 | n/a | # different format, struct module |
|---|
| 3631 | n/a | ex1 = ndarray([(2, b'123')]*30, shape=[5, 3, 2], format='b3s') |
|---|
| 3632 | n/a | nd1 = ex1[1:3:, ::-2] |
|---|
| 3633 | n/a | nd2 = ndarray([(2, b'123')]*30, shape=[5, 3, 2], format='i3s') |
|---|
| 3634 | n/a | nd2 = ex2[1:3:, ::-2] |
|---|
| 3635 | n/a | v = memoryview(nd1) |
|---|
| 3636 | n/a | w = memoryview(nd2) |
|---|
| 3637 | n/a | |
|---|
| 3638 | n/a | self.assertEqual(v, nd1) |
|---|
| 3639 | n/a | self.assertEqual(w, nd2) |
|---|
| 3640 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3641 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3642 | n/a | self.assertNotEqual(v, w) |
|---|
| 3643 | n/a | |
|---|
| 3644 | n/a | def test_memoryview_compare_multidim_zero_shape(self): |
|---|
| 3645 | n/a | |
|---|
| 3646 | n/a | # zeros in shape |
|---|
| 3647 | n/a | nd1 = ndarray(list(range(30)), shape=[0, 3, 2], format='i') |
|---|
| 3648 | n/a | nd2 = ndarray(list(range(30)), shape=[5, 0, 2], format='@i') |
|---|
| 3649 | n/a | v = memoryview(nd1) |
|---|
| 3650 | n/a | w = memoryview(nd2) |
|---|
| 3651 | n/a | |
|---|
| 3652 | n/a | self.assertEqual(v, nd1) |
|---|
| 3653 | n/a | self.assertEqual(w, nd2) |
|---|
| 3654 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3655 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3656 | n/a | self.assertNotEqual(v, w) |
|---|
| 3657 | n/a | |
|---|
| 3658 | n/a | # zeros in shape, struct module |
|---|
| 3659 | n/a | nd1 = ndarray(list(range(30)), shape=[0, 3, 2], format='i') |
|---|
| 3660 | n/a | nd2 = ndarray(list(range(30)), shape=[5, 0, 2], format='@i') |
|---|
| 3661 | n/a | v = memoryview(nd1) |
|---|
| 3662 | n/a | w = memoryview(nd2) |
|---|
| 3663 | n/a | |
|---|
| 3664 | n/a | self.assertEqual(v, nd1) |
|---|
| 3665 | n/a | self.assertEqual(w, nd2) |
|---|
| 3666 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3667 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3668 | n/a | self.assertNotEqual(v, w) |
|---|
| 3669 | n/a | |
|---|
| 3670 | n/a | def test_memoryview_compare_multidim_zero_strides(self): |
|---|
| 3671 | n/a | |
|---|
| 3672 | n/a | # zero strides |
|---|
| 3673 | n/a | nd1 = ndarray([900]*80, shape=[4, 5, 4], format='@L') |
|---|
| 3674 | n/a | nd2 = ndarray([900], shape=[4, 5, 4], strides=[0, 0, 0], format='L') |
|---|
| 3675 | n/a | v = memoryview(nd1) |
|---|
| 3676 | n/a | w = memoryview(nd2) |
|---|
| 3677 | n/a | |
|---|
| 3678 | n/a | self.assertEqual(v, nd1) |
|---|
| 3679 | n/a | self.assertEqual(w, nd2) |
|---|
| 3680 | n/a | self.assertEqual(v, nd2) |
|---|
| 3681 | n/a | self.assertEqual(w, nd1) |
|---|
| 3682 | n/a | self.assertEqual(v, w) |
|---|
| 3683 | n/a | self.assertEqual(v.tolist(), w.tolist()) |
|---|
| 3684 | n/a | |
|---|
| 3685 | n/a | # zero strides, struct module |
|---|
| 3686 | n/a | nd1 = ndarray([(1, 2)]*10, shape=[2, 5], format='=lQ') |
|---|
| 3687 | n/a | nd2 = ndarray([(1, 2)], shape=[2, 5], strides=[0, 0], format='<lQ') |
|---|
| 3688 | n/a | v = memoryview(nd1) |
|---|
| 3689 | n/a | w = memoryview(nd2) |
|---|
| 3690 | n/a | |
|---|
| 3691 | n/a | self.assertEqual(v, nd1) |
|---|
| 3692 | n/a | self.assertEqual(w, nd2) |
|---|
| 3693 | n/a | self.assertEqual(v, nd2) |
|---|
| 3694 | n/a | self.assertEqual(w, nd1) |
|---|
| 3695 | n/a | self.assertEqual(v, w) |
|---|
| 3696 | n/a | |
|---|
| 3697 | n/a | def test_memoryview_compare_multidim_suboffsets(self): |
|---|
| 3698 | n/a | |
|---|
| 3699 | n/a | # suboffsets |
|---|
| 3700 | n/a | ex1 = ndarray(list(range(40)), shape=[5, 8], format='@I') |
|---|
| 3701 | n/a | nd1 = ex1[3:1:-1, ::-2] |
|---|
| 3702 | n/a | ex2 = ndarray(list(range(40)), shape=[5, 8], format='I', flags=ND_PIL) |
|---|
| 3703 | n/a | nd2 = ex2[1:3:1, ::-2] |
|---|
| 3704 | n/a | v = memoryview(nd1) |
|---|
| 3705 | n/a | w = memoryview(nd2) |
|---|
| 3706 | n/a | |
|---|
| 3707 | n/a | self.assertEqual(v, nd1) |
|---|
| 3708 | n/a | self.assertEqual(w, nd2) |
|---|
| 3709 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3710 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3711 | n/a | self.assertNotEqual(v, w) |
|---|
| 3712 | n/a | |
|---|
| 3713 | n/a | # suboffsets, struct module |
|---|
| 3714 | n/a | ex1 = ndarray([(2**64-1, -1)]*40, shape=[5, 8], format='=Qq', |
|---|
| 3715 | n/a | flags=ND_WRITABLE) |
|---|
| 3716 | n/a | ex1[2][7] = (1, -2) |
|---|
| 3717 | n/a | nd1 = ex1[3:1:-1, ::-2] |
|---|
| 3718 | n/a | |
|---|
| 3719 | n/a | ex2 = ndarray([(2**64-1, -1)]*40, shape=[5, 8], format='>Qq', |
|---|
| 3720 | n/a | flags=ND_PIL|ND_WRITABLE) |
|---|
| 3721 | n/a | ex2[2][7] = (1, -2) |
|---|
| 3722 | n/a | nd2 = ex2[1:3:1, ::-2] |
|---|
| 3723 | n/a | |
|---|
| 3724 | n/a | v = memoryview(nd1) |
|---|
| 3725 | n/a | w = memoryview(nd2) |
|---|
| 3726 | n/a | |
|---|
| 3727 | n/a | self.assertEqual(v, nd1) |
|---|
| 3728 | n/a | self.assertEqual(w, nd2) |
|---|
| 3729 | n/a | self.assertEqual(v, nd2) |
|---|
| 3730 | n/a | self.assertEqual(w, nd1) |
|---|
| 3731 | n/a | self.assertEqual(v, w) |
|---|
| 3732 | n/a | |
|---|
| 3733 | n/a | # suboffsets, different shape |
|---|
| 3734 | n/a | ex1 = ndarray(list(range(30)), shape=[2, 3, 5], format='b', |
|---|
| 3735 | n/a | flags=ND_PIL) |
|---|
| 3736 | n/a | nd1 = ex1[1:3:, ::-2] |
|---|
| 3737 | n/a | nd2 = ndarray(list(range(30)), shape=[3, 2, 5], format='b') |
|---|
| 3738 | n/a | nd2 = ex2[1:3:, ::-2] |
|---|
| 3739 | n/a | v = memoryview(nd1) |
|---|
| 3740 | n/a | w = memoryview(nd2) |
|---|
| 3741 | n/a | |
|---|
| 3742 | n/a | self.assertEqual(v, nd1) |
|---|
| 3743 | n/a | self.assertEqual(w, nd2) |
|---|
| 3744 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3745 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3746 | n/a | self.assertNotEqual(v, w) |
|---|
| 3747 | n/a | |
|---|
| 3748 | n/a | # suboffsets, different shape, struct module |
|---|
| 3749 | n/a | ex1 = ndarray([(2**8-1, -1)]*40, shape=[2, 3, 5], format='Bb', |
|---|
| 3750 | n/a | flags=ND_PIL|ND_WRITABLE) |
|---|
| 3751 | n/a | nd1 = ex1[1:2:, ::-2] |
|---|
| 3752 | n/a | |
|---|
| 3753 | n/a | ex2 = ndarray([(2**8-1, -1)]*40, shape=[3, 2, 5], format='Bb') |
|---|
| 3754 | n/a | nd2 = ex2[1:2:, ::-2] |
|---|
| 3755 | n/a | |
|---|
| 3756 | n/a | v = memoryview(nd1) |
|---|
| 3757 | n/a | w = memoryview(nd2) |
|---|
| 3758 | n/a | |
|---|
| 3759 | n/a | self.assertEqual(v, nd1) |
|---|
| 3760 | n/a | self.assertEqual(w, nd2) |
|---|
| 3761 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3762 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3763 | n/a | self.assertNotEqual(v, w) |
|---|
| 3764 | n/a | |
|---|
| 3765 | n/a | # suboffsets, different format |
|---|
| 3766 | n/a | ex1 = ndarray(list(range(30)), shape=[5, 3, 2], format='i', flags=ND_PIL) |
|---|
| 3767 | n/a | nd1 = ex1[1:3:, ::-2] |
|---|
| 3768 | n/a | ex2 = ndarray(list(range(30)), shape=[5, 3, 2], format='@I', flags=ND_PIL) |
|---|
| 3769 | n/a | nd2 = ex2[1:3:, ::-2] |
|---|
| 3770 | n/a | v = memoryview(nd1) |
|---|
| 3771 | n/a | w = memoryview(nd2) |
|---|
| 3772 | n/a | |
|---|
| 3773 | n/a | self.assertEqual(v, nd1) |
|---|
| 3774 | n/a | self.assertEqual(w, nd2) |
|---|
| 3775 | n/a | self.assertEqual(v, nd2) |
|---|
| 3776 | n/a | self.assertEqual(w, nd1) |
|---|
| 3777 | n/a | self.assertEqual(v, w) |
|---|
| 3778 | n/a | |
|---|
| 3779 | n/a | # suboffsets, different format, struct module |
|---|
| 3780 | n/a | ex1 = ndarray([(b'hello', b'', 1)]*27, shape=[3, 3, 3], format='5s0sP', |
|---|
| 3781 | n/a | flags=ND_PIL|ND_WRITABLE) |
|---|
| 3782 | n/a | ex1[1][2][2] = (b'sushi', b'', 1) |
|---|
| 3783 | n/a | nd1 = ex1[1:3:, ::-2] |
|---|
| 3784 | n/a | |
|---|
| 3785 | n/a | ex2 = ndarray([(b'hello', b'', 1)]*27, shape=[3, 3, 3], format='5s0sP', |
|---|
| 3786 | n/a | flags=ND_PIL|ND_WRITABLE) |
|---|
| 3787 | n/a | ex1[1][2][2] = (b'sushi', b'', 1) |
|---|
| 3788 | n/a | nd2 = ex2[1:3:, ::-2] |
|---|
| 3789 | n/a | |
|---|
| 3790 | n/a | v = memoryview(nd1) |
|---|
| 3791 | n/a | w = memoryview(nd2) |
|---|
| 3792 | n/a | |
|---|
| 3793 | n/a | self.assertEqual(v, nd1) |
|---|
| 3794 | n/a | self.assertEqual(w, nd2) |
|---|
| 3795 | n/a | self.assertNotEqual(v, nd2) |
|---|
| 3796 | n/a | self.assertNotEqual(w, nd1) |
|---|
| 3797 | n/a | self.assertNotEqual(v, w) |
|---|
| 3798 | n/a | |
|---|
| 3799 | n/a | # initialize mixed C/Fortran + suboffsets |
|---|
| 3800 | n/a | lst1 = list(range(-15, 15)) |
|---|
| 3801 | n/a | lst2 = transpose(lst1, [3, 2, 5]) |
|---|
| 3802 | n/a | nd1 = ndarray(lst1, shape=[3, 2, 5], format='@l', flags=ND_PIL) |
|---|
| 3803 | n/a | nd2 = ndarray(lst2, shape=[3, 2, 5], format='l', flags=ND_FORTRAN|ND_PIL) |
|---|
| 3804 | n/a | v = memoryview(nd1) |
|---|
| 3805 | n/a | w = memoryview(nd2) |
|---|
| 3806 | n/a | |
|---|
| 3807 | n/a | self.assertEqual(v, nd1) |
|---|
| 3808 | n/a | self.assertEqual(w, nd2) |
|---|
| 3809 | n/a | self.assertEqual(v, w) |
|---|
| 3810 | n/a | |
|---|
| 3811 | n/a | # initialize mixed C/Fortran + suboffsets, struct module |
|---|
| 3812 | n/a | lst1 = [(b'sashimi', b'sliced', 20.05)]*30 |
|---|
| 3813 | n/a | lst1[11] = (b'ramen', b'spicy', 9.45) |
|---|
| 3814 | n/a | lst2 = transpose(lst1, [3, 2, 5]) |
|---|
| 3815 | n/a | |
|---|
| 3816 | n/a | nd1 = ndarray(lst1, shape=[3, 2, 5], format='< 10p 9p d', flags=ND_PIL) |
|---|
| 3817 | n/a | nd2 = ndarray(lst2, shape=[3, 2, 5], format='> 10p 9p d', |
|---|
| 3818 | n/a | flags=ND_FORTRAN|ND_PIL) |
|---|
| 3819 | n/a | v = memoryview(nd1) |
|---|
| 3820 | n/a | w = memoryview(nd2) |
|---|
| 3821 | n/a | |
|---|
| 3822 | n/a | self.assertEqual(v, nd1) |
|---|
| 3823 | n/a | self.assertEqual(w, nd2) |
|---|
| 3824 | n/a | self.assertEqual(v, w) |
|---|
| 3825 | n/a | |
|---|
| 3826 | n/a | def test_memoryview_compare_not_equal(self): |
|---|
| 3827 | n/a | |
|---|
| 3828 | n/a | # items not equal |
|---|
| 3829 | n/a | for byteorder in ['=', '<', '>', '!']: |
|---|
| 3830 | n/a | x = ndarray([2**63]*120, shape=[3,5,2,2,2], format=byteorder+'Q') |
|---|
| 3831 | n/a | y = ndarray([2**63]*120, shape=[3,5,2,2,2], format=byteorder+'Q', |
|---|
| 3832 | n/a | flags=ND_WRITABLE|ND_FORTRAN) |
|---|
| 3833 | n/a | y[2][3][1][1][1] = 1 |
|---|
| 3834 | n/a | a = memoryview(x) |
|---|
| 3835 | n/a | b = memoryview(y) |
|---|
| 3836 | n/a | self.assertEqual(a, x) |
|---|
| 3837 | n/a | self.assertEqual(b, y) |
|---|
| 3838 | n/a | self.assertNotEqual(a, b) |
|---|
| 3839 | n/a | self.assertNotEqual(a, y) |
|---|
| 3840 | n/a | self.assertNotEqual(b, x) |
|---|
| 3841 | n/a | |
|---|
| 3842 | n/a | x = ndarray([(2**63, 2**31, 2**15)]*120, shape=[3,5,2,2,2], |
|---|
| 3843 | n/a | format=byteorder+'QLH') |
|---|
| 3844 | n/a | y = ndarray([(2**63, 2**31, 2**15)]*120, shape=[3,5,2,2,2], |
|---|
| 3845 | n/a | format=byteorder+'QLH', flags=ND_WRITABLE|ND_FORTRAN) |
|---|
| 3846 | n/a | y[2][3][1][1][1] = (1, 1, 1) |
|---|
| 3847 | n/a | a = memoryview(x) |
|---|
| 3848 | n/a | b = memoryview(y) |
|---|
| 3849 | n/a | self.assertEqual(a, x) |
|---|
| 3850 | n/a | self.assertEqual(b, y) |
|---|
| 3851 | n/a | self.assertNotEqual(a, b) |
|---|
| 3852 | n/a | self.assertNotEqual(a, y) |
|---|
| 3853 | n/a | self.assertNotEqual(b, x) |
|---|
| 3854 | n/a | |
|---|
| 3855 | n/a | def test_memoryview_check_released(self): |
|---|
| 3856 | n/a | |
|---|
| 3857 | n/a | a = array.array('d', [1.1, 2.2, 3.3]) |
|---|
| 3858 | n/a | |
|---|
| 3859 | n/a | m = memoryview(a) |
|---|
| 3860 | n/a | m.release() |
|---|
| 3861 | n/a | |
|---|
| 3862 | n/a | # PyMemoryView_FromObject() |
|---|
| 3863 | n/a | self.assertRaises(ValueError, memoryview, m) |
|---|
| 3864 | n/a | # memoryview.cast() |
|---|
| 3865 | n/a | self.assertRaises(ValueError, m.cast, 'c') |
|---|
| 3866 | n/a | # getbuffer() |
|---|
| 3867 | n/a | self.assertRaises(ValueError, ndarray, m) |
|---|
| 3868 | n/a | # memoryview.tolist() |
|---|
| 3869 | n/a | self.assertRaises(ValueError, m.tolist) |
|---|
| 3870 | n/a | # memoryview.tobytes() |
|---|
| 3871 | n/a | self.assertRaises(ValueError, m.tobytes) |
|---|
| 3872 | n/a | # sequence |
|---|
| 3873 | n/a | self.assertRaises(ValueError, eval, "1.0 in m", locals()) |
|---|
| 3874 | n/a | # subscript |
|---|
| 3875 | n/a | self.assertRaises(ValueError, m.__getitem__, 0) |
|---|
| 3876 | n/a | # assignment |
|---|
| 3877 | n/a | self.assertRaises(ValueError, m.__setitem__, 0, 1) |
|---|
| 3878 | n/a | |
|---|
| 3879 | n/a | for attr in ('obj', 'nbytes', 'readonly', 'itemsize', 'format', 'ndim', |
|---|
| 3880 | n/a | 'shape', 'strides', 'suboffsets', 'c_contiguous', |
|---|
| 3881 | n/a | 'f_contiguous', 'contiguous'): |
|---|
| 3882 | n/a | self.assertRaises(ValueError, m.__getattribute__, attr) |
|---|
| 3883 | n/a | |
|---|
| 3884 | n/a | # richcompare |
|---|
| 3885 | n/a | b = array.array('d', [1.1, 2.2, 3.3]) |
|---|
| 3886 | n/a | m1 = memoryview(a) |
|---|
| 3887 | n/a | m2 = memoryview(b) |
|---|
| 3888 | n/a | |
|---|
| 3889 | n/a | self.assertEqual(m1, m2) |
|---|
| 3890 | n/a | m1.release() |
|---|
| 3891 | n/a | self.assertNotEqual(m1, m2) |
|---|
| 3892 | n/a | self.assertNotEqual(m1, a) |
|---|
| 3893 | n/a | self.assertEqual(m1, m1) |
|---|
| 3894 | n/a | |
|---|
| 3895 | n/a | def test_memoryview_tobytes(self): |
|---|
| 3896 | n/a | # Many implicit tests are already in self.verify(). |
|---|
| 3897 | n/a | |
|---|
| 3898 | n/a | t = (-529, 576, -625, 676, -729) |
|---|
| 3899 | n/a | |
|---|
| 3900 | n/a | nd = ndarray(t, shape=[5], format='@h') |
|---|
| 3901 | n/a | m = memoryview(nd) |
|---|
| 3902 | n/a | self.assertEqual(m, nd) |
|---|
| 3903 | n/a | self.assertEqual(m.tobytes(), nd.tobytes()) |
|---|
| 3904 | n/a | |
|---|
| 3905 | n/a | nd = ndarray([t], shape=[1], format='>hQiLl') |
|---|
| 3906 | n/a | m = memoryview(nd) |
|---|
| 3907 | n/a | self.assertEqual(m, nd) |
|---|
| 3908 | n/a | self.assertEqual(m.tobytes(), nd.tobytes()) |
|---|
| 3909 | n/a | |
|---|
| 3910 | n/a | nd = ndarray([t for _ in range(12)], shape=[2,2,3], format='=hQiLl') |
|---|
| 3911 | n/a | m = memoryview(nd) |
|---|
| 3912 | n/a | self.assertEqual(m, nd) |
|---|
| 3913 | n/a | self.assertEqual(m.tobytes(), nd.tobytes()) |
|---|
| 3914 | n/a | |
|---|
| 3915 | n/a | nd = ndarray([t for _ in range(120)], shape=[5,2,2,3,2], |
|---|
| 3916 | n/a | format='<hQiLl') |
|---|
| 3917 | n/a | m = memoryview(nd) |
|---|
| 3918 | n/a | self.assertEqual(m, nd) |
|---|
| 3919 | n/a | self.assertEqual(m.tobytes(), nd.tobytes()) |
|---|
| 3920 | n/a | |
|---|
| 3921 | n/a | # Unknown formats are handled: tobytes() purely depends on itemsize. |
|---|
| 3922 | n/a | if ctypes: |
|---|
| 3923 | n/a | # format: "T{>l:x:>l:y:}" |
|---|
| 3924 | n/a | class BEPoint(ctypes.BigEndianStructure): |
|---|
| 3925 | n/a | _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)] |
|---|
| 3926 | n/a | point = BEPoint(100, 200) |
|---|
| 3927 | n/a | a = memoryview(point) |
|---|
| 3928 | n/a | self.assertEqual(a.tobytes(), bytes(point)) |
|---|
| 3929 | n/a | |
|---|
| 3930 | n/a | def test_memoryview_get_contiguous(self): |
|---|
| 3931 | n/a | # Many implicit tests are already in self.verify(). |
|---|
| 3932 | n/a | |
|---|
| 3933 | n/a | # no buffer interface |
|---|
| 3934 | n/a | self.assertRaises(TypeError, get_contiguous, {}, PyBUF_READ, 'F') |
|---|
| 3935 | n/a | |
|---|
| 3936 | n/a | # writable request to read-only object |
|---|
| 3937 | n/a | self.assertRaises(BufferError, get_contiguous, b'x', PyBUF_WRITE, 'C') |
|---|
| 3938 | n/a | |
|---|
| 3939 | n/a | # writable request to non-contiguous object |
|---|
| 3940 | n/a | nd = ndarray([1, 2, 3], shape=[2], strides=[2]) |
|---|
| 3941 | n/a | self.assertRaises(BufferError, get_contiguous, nd, PyBUF_WRITE, 'A') |
|---|
| 3942 | n/a | |
|---|
| 3943 | n/a | # scalar, read-only request from read-only exporter |
|---|
| 3944 | n/a | nd = ndarray(9, shape=(), format="L") |
|---|
| 3945 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 3946 | n/a | m = get_contiguous(nd, PyBUF_READ, order) |
|---|
| 3947 | n/a | self.assertEqual(m, nd) |
|---|
| 3948 | n/a | self.assertEqual(m[()], 9) |
|---|
| 3949 | n/a | |
|---|
| 3950 | n/a | # scalar, read-only request from writable exporter |
|---|
| 3951 | n/a | nd = ndarray(9, shape=(), format="L", flags=ND_WRITABLE) |
|---|
| 3952 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 3953 | n/a | m = get_contiguous(nd, PyBUF_READ, order) |
|---|
| 3954 | n/a | self.assertEqual(m, nd) |
|---|
| 3955 | n/a | self.assertEqual(m[()], 9) |
|---|
| 3956 | n/a | |
|---|
| 3957 | n/a | # scalar, writable request |
|---|
| 3958 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 3959 | n/a | nd[()] = 9 |
|---|
| 3960 | n/a | m = get_contiguous(nd, PyBUF_WRITE, order) |
|---|
| 3961 | n/a | self.assertEqual(m, nd) |
|---|
| 3962 | n/a | self.assertEqual(m[()], 9) |
|---|
| 3963 | n/a | |
|---|
| 3964 | n/a | m[()] = 10 |
|---|
| 3965 | n/a | self.assertEqual(m[()], 10) |
|---|
| 3966 | n/a | self.assertEqual(nd[()], 10) |
|---|
| 3967 | n/a | |
|---|
| 3968 | n/a | # zeros in shape |
|---|
| 3969 | n/a | nd = ndarray([1], shape=[0], format="L", flags=ND_WRITABLE) |
|---|
| 3970 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 3971 | n/a | m = get_contiguous(nd, PyBUF_READ, order) |
|---|
| 3972 | n/a | self.assertRaises(IndexError, m.__getitem__, 0) |
|---|
| 3973 | n/a | self.assertEqual(m, nd) |
|---|
| 3974 | n/a | self.assertEqual(m.tolist(), []) |
|---|
| 3975 | n/a | |
|---|
| 3976 | n/a | nd = ndarray(list(range(8)), shape=[2, 0, 7], format="L", |
|---|
| 3977 | n/a | flags=ND_WRITABLE) |
|---|
| 3978 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 3979 | n/a | m = get_contiguous(nd, PyBUF_READ, order) |
|---|
| 3980 | n/a | self.assertEqual(ndarray(m).tolist(), [[], []]) |
|---|
| 3981 | n/a | |
|---|
| 3982 | n/a | # one-dimensional |
|---|
| 3983 | n/a | nd = ndarray([1], shape=[1], format="h", flags=ND_WRITABLE) |
|---|
| 3984 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 3985 | n/a | m = get_contiguous(nd, PyBUF_WRITE, order) |
|---|
| 3986 | n/a | self.assertEqual(m, nd) |
|---|
| 3987 | n/a | self.assertEqual(m.tolist(), nd.tolist()) |
|---|
| 3988 | n/a | |
|---|
| 3989 | n/a | nd = ndarray([1, 2, 3], shape=[3], format="b", flags=ND_WRITABLE) |
|---|
| 3990 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 3991 | n/a | m = get_contiguous(nd, PyBUF_WRITE, order) |
|---|
| 3992 | n/a | self.assertEqual(m, nd) |
|---|
| 3993 | n/a | self.assertEqual(m.tolist(), nd.tolist()) |
|---|
| 3994 | n/a | |
|---|
| 3995 | n/a | # one-dimensional, non-contiguous |
|---|
| 3996 | n/a | nd = ndarray([1, 2, 3], shape=[2], strides=[2], flags=ND_WRITABLE) |
|---|
| 3997 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 3998 | n/a | m = get_contiguous(nd, PyBUF_READ, order) |
|---|
| 3999 | n/a | self.assertEqual(m, nd) |
|---|
| 4000 | n/a | self.assertEqual(m.tolist(), nd.tolist()) |
|---|
| 4001 | n/a | self.assertRaises(TypeError, m.__setitem__, 1, 20) |
|---|
| 4002 | n/a | self.assertEqual(m[1], 3) |
|---|
| 4003 | n/a | self.assertEqual(nd[1], 3) |
|---|
| 4004 | n/a | |
|---|
| 4005 | n/a | nd = nd[::-1] |
|---|
| 4006 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 4007 | n/a | m = get_contiguous(nd, PyBUF_READ, order) |
|---|
| 4008 | n/a | self.assertEqual(m, nd) |
|---|
| 4009 | n/a | self.assertEqual(m.tolist(), nd.tolist()) |
|---|
| 4010 | n/a | self.assertRaises(TypeError, m.__setitem__, 1, 20) |
|---|
| 4011 | n/a | self.assertEqual(m[1], 1) |
|---|
| 4012 | n/a | self.assertEqual(nd[1], 1) |
|---|
| 4013 | n/a | |
|---|
| 4014 | n/a | # multi-dimensional, contiguous input |
|---|
| 4015 | n/a | nd = ndarray(list(range(12)), shape=[3, 4], flags=ND_WRITABLE) |
|---|
| 4016 | n/a | for order in ['C', 'A']: |
|---|
| 4017 | n/a | m = get_contiguous(nd, PyBUF_WRITE, order) |
|---|
| 4018 | n/a | self.assertEqual(ndarray(m).tolist(), nd.tolist()) |
|---|
| 4019 | n/a | |
|---|
| 4020 | n/a | self.assertRaises(BufferError, get_contiguous, nd, PyBUF_WRITE, 'F') |
|---|
| 4021 | n/a | m = get_contiguous(nd, PyBUF_READ, order) |
|---|
| 4022 | n/a | self.assertEqual(ndarray(m).tolist(), nd.tolist()) |
|---|
| 4023 | n/a | |
|---|
| 4024 | n/a | nd = ndarray(list(range(12)), shape=[3, 4], |
|---|
| 4025 | n/a | flags=ND_WRITABLE|ND_FORTRAN) |
|---|
| 4026 | n/a | for order in ['F', 'A']: |
|---|
| 4027 | n/a | m = get_contiguous(nd, PyBUF_WRITE, order) |
|---|
| 4028 | n/a | self.assertEqual(ndarray(m).tolist(), nd.tolist()) |
|---|
| 4029 | n/a | |
|---|
| 4030 | n/a | self.assertRaises(BufferError, get_contiguous, nd, PyBUF_WRITE, 'C') |
|---|
| 4031 | n/a | m = get_contiguous(nd, PyBUF_READ, order) |
|---|
| 4032 | n/a | self.assertEqual(ndarray(m).tolist(), nd.tolist()) |
|---|
| 4033 | n/a | |
|---|
| 4034 | n/a | # multi-dimensional, non-contiguous input |
|---|
| 4035 | n/a | nd = ndarray(list(range(12)), shape=[3, 4], flags=ND_WRITABLE|ND_PIL) |
|---|
| 4036 | n/a | for order in ['C', 'F', 'A']: |
|---|
| 4037 | n/a | self.assertRaises(BufferError, get_contiguous, nd, PyBUF_WRITE, |
|---|
| 4038 | n/a | order) |
|---|
| 4039 | n/a | m = get_contiguous(nd, PyBUF_READ, order) |
|---|
| 4040 | n/a | self.assertEqual(ndarray(m).tolist(), nd.tolist()) |
|---|
| 4041 | n/a | |
|---|
| 4042 | n/a | # flags |
|---|
| 4043 | n/a | nd = ndarray([1,2,3,4,5], shape=[3], strides=[2]) |
|---|
| 4044 | n/a | m = get_contiguous(nd, PyBUF_READ, 'C') |
|---|
| 4045 | n/a | self.assertTrue(m.c_contiguous) |
|---|
| 4046 | n/a | |
|---|
| 4047 | n/a | def test_memoryview_serializing(self): |
|---|
| 4048 | n/a | |
|---|
| 4049 | n/a | # C-contiguous |
|---|
| 4050 | n/a | size = struct.calcsize('i') |
|---|
| 4051 | n/a | a = array.array('i', [1,2,3,4,5]) |
|---|
| 4052 | n/a | m = memoryview(a) |
|---|
| 4053 | n/a | buf = io.BytesIO(m) |
|---|
| 4054 | n/a | b = bytearray(5*size) |
|---|
| 4055 | n/a | buf.readinto(b) |
|---|
| 4056 | n/a | self.assertEqual(m.tobytes(), b) |
|---|
| 4057 | n/a | |
|---|
| 4058 | n/a | # C-contiguous, multi-dimensional |
|---|
| 4059 | n/a | size = struct.calcsize('L') |
|---|
| 4060 | n/a | nd = ndarray(list(range(12)), shape=[2,3,2], format="L") |
|---|
| 4061 | n/a | m = memoryview(nd) |
|---|
| 4062 | n/a | buf = io.BytesIO(m) |
|---|
| 4063 | n/a | b = bytearray(2*3*2*size) |
|---|
| 4064 | n/a | buf.readinto(b) |
|---|
| 4065 | n/a | self.assertEqual(m.tobytes(), b) |
|---|
| 4066 | n/a | |
|---|
| 4067 | n/a | # Fortran contiguous, multi-dimensional |
|---|
| 4068 | n/a | #size = struct.calcsize('L') |
|---|
| 4069 | n/a | #nd = ndarray(list(range(12)), shape=[2,3,2], format="L", |
|---|
| 4070 | n/a | # flags=ND_FORTRAN) |
|---|
| 4071 | n/a | #m = memoryview(nd) |
|---|
| 4072 | n/a | #buf = io.BytesIO(m) |
|---|
| 4073 | n/a | #b = bytearray(2*3*2*size) |
|---|
| 4074 | n/a | #buf.readinto(b) |
|---|
| 4075 | n/a | #self.assertEqual(m.tobytes(), b) |
|---|
| 4076 | n/a | |
|---|
| 4077 | n/a | def test_memoryview_hash(self): |
|---|
| 4078 | n/a | |
|---|
| 4079 | n/a | # bytes exporter |
|---|
| 4080 | n/a | b = bytes(list(range(12))) |
|---|
| 4081 | n/a | m = memoryview(b) |
|---|
| 4082 | n/a | self.assertEqual(hash(b), hash(m)) |
|---|
| 4083 | n/a | |
|---|
| 4084 | n/a | # C-contiguous |
|---|
| 4085 | n/a | mc = m.cast('c', shape=[3,4]) |
|---|
| 4086 | n/a | self.assertEqual(hash(mc), hash(b)) |
|---|
| 4087 | n/a | |
|---|
| 4088 | n/a | # non-contiguous |
|---|
| 4089 | n/a | mx = m[::-2] |
|---|
| 4090 | n/a | b = bytes(list(range(12))[::-2]) |
|---|
| 4091 | n/a | self.assertEqual(hash(mx), hash(b)) |
|---|
| 4092 | n/a | |
|---|
| 4093 | n/a | # Fortran contiguous |
|---|
| 4094 | n/a | nd = ndarray(list(range(30)), shape=[3,2,5], flags=ND_FORTRAN) |
|---|
| 4095 | n/a | m = memoryview(nd) |
|---|
| 4096 | n/a | self.assertEqual(hash(m), hash(nd)) |
|---|
| 4097 | n/a | |
|---|
| 4098 | n/a | # multi-dimensional slice |
|---|
| 4099 | n/a | nd = ndarray(list(range(30)), shape=[3,2,5]) |
|---|
| 4100 | n/a | x = nd[::2, ::, ::-1] |
|---|
| 4101 | n/a | m = memoryview(x) |
|---|
| 4102 | n/a | self.assertEqual(hash(m), hash(x)) |
|---|
| 4103 | n/a | |
|---|
| 4104 | n/a | # multi-dimensional slice with suboffsets |
|---|
| 4105 | n/a | nd = ndarray(list(range(30)), shape=[2,5,3], flags=ND_PIL) |
|---|
| 4106 | n/a | x = nd[::2, ::, ::-1] |
|---|
| 4107 | n/a | m = memoryview(x) |
|---|
| 4108 | n/a | self.assertEqual(hash(m), hash(x)) |
|---|
| 4109 | n/a | |
|---|
| 4110 | n/a | # equality-hash invariant |
|---|
| 4111 | n/a | x = ndarray(list(range(12)), shape=[12], format='B') |
|---|
| 4112 | n/a | a = memoryview(x) |
|---|
| 4113 | n/a | |
|---|
| 4114 | n/a | y = ndarray(list(range(12)), shape=[12], format='b') |
|---|
| 4115 | n/a | b = memoryview(y) |
|---|
| 4116 | n/a | |
|---|
| 4117 | n/a | self.assertEqual(a, b) |
|---|
| 4118 | n/a | self.assertEqual(hash(a), hash(b)) |
|---|
| 4119 | n/a | |
|---|
| 4120 | n/a | # non-byte formats |
|---|
| 4121 | n/a | nd = ndarray(list(range(12)), shape=[2,2,3], format='L') |
|---|
| 4122 | n/a | m = memoryview(nd) |
|---|
| 4123 | n/a | self.assertRaises(ValueError, m.__hash__) |
|---|
| 4124 | n/a | |
|---|
| 4125 | n/a | nd = ndarray(list(range(-6, 6)), shape=[2,2,3], format='h') |
|---|
| 4126 | n/a | m = memoryview(nd) |
|---|
| 4127 | n/a | self.assertRaises(ValueError, m.__hash__) |
|---|
| 4128 | n/a | |
|---|
| 4129 | n/a | nd = ndarray(list(range(12)), shape=[2,2,3], format='= L') |
|---|
| 4130 | n/a | m = memoryview(nd) |
|---|
| 4131 | n/a | self.assertRaises(ValueError, m.__hash__) |
|---|
| 4132 | n/a | |
|---|
| 4133 | n/a | nd = ndarray(list(range(-6, 6)), shape=[2,2,3], format='< h') |
|---|
| 4134 | n/a | m = memoryview(nd) |
|---|
| 4135 | n/a | self.assertRaises(ValueError, m.__hash__) |
|---|
| 4136 | n/a | |
|---|
| 4137 | n/a | def test_memoryview_release(self): |
|---|
| 4138 | n/a | |
|---|
| 4139 | n/a | # Create re-exporter from getbuffer(memoryview), then release the view. |
|---|
| 4140 | n/a | a = bytearray([1,2,3]) |
|---|
| 4141 | n/a | m = memoryview(a) |
|---|
| 4142 | n/a | nd = ndarray(m) # re-exporter |
|---|
| 4143 | n/a | self.assertRaises(BufferError, m.release) |
|---|
| 4144 | n/a | del nd |
|---|
| 4145 | n/a | m.release() |
|---|
| 4146 | n/a | |
|---|
| 4147 | n/a | a = bytearray([1,2,3]) |
|---|
| 4148 | n/a | m = memoryview(a) |
|---|
| 4149 | n/a | nd1 = ndarray(m, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4150 | n/a | nd2 = ndarray(nd1, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4151 | n/a | self.assertIs(nd2.obj, m) |
|---|
| 4152 | n/a | self.assertRaises(BufferError, m.release) |
|---|
| 4153 | n/a | del nd1, nd2 |
|---|
| 4154 | n/a | m.release() |
|---|
| 4155 | n/a | |
|---|
| 4156 | n/a | # chained views |
|---|
| 4157 | n/a | a = bytearray([1,2,3]) |
|---|
| 4158 | n/a | m1 = memoryview(a) |
|---|
| 4159 | n/a | m2 = memoryview(m1) |
|---|
| 4160 | n/a | nd = ndarray(m2) # re-exporter |
|---|
| 4161 | n/a | m1.release() |
|---|
| 4162 | n/a | self.assertRaises(BufferError, m2.release) |
|---|
| 4163 | n/a | del nd |
|---|
| 4164 | n/a | m2.release() |
|---|
| 4165 | n/a | |
|---|
| 4166 | n/a | a = bytearray([1,2,3]) |
|---|
| 4167 | n/a | m1 = memoryview(a) |
|---|
| 4168 | n/a | m2 = memoryview(m1) |
|---|
| 4169 | n/a | nd1 = ndarray(m2, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4170 | n/a | nd2 = ndarray(nd1, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4171 | n/a | self.assertIs(nd2.obj, m2) |
|---|
| 4172 | n/a | m1.release() |
|---|
| 4173 | n/a | self.assertRaises(BufferError, m2.release) |
|---|
| 4174 | n/a | del nd1, nd2 |
|---|
| 4175 | n/a | m2.release() |
|---|
| 4176 | n/a | |
|---|
| 4177 | n/a | # Allow changing layout while buffers are exported. |
|---|
| 4178 | n/a | nd = ndarray([1,2,3], shape=[3], flags=ND_VAREXPORT) |
|---|
| 4179 | n/a | m1 = memoryview(nd) |
|---|
| 4180 | n/a | |
|---|
| 4181 | n/a | nd.push([4,5,6,7,8], shape=[5]) # mutate nd |
|---|
| 4182 | n/a | m2 = memoryview(nd) |
|---|
| 4183 | n/a | |
|---|
| 4184 | n/a | x = memoryview(m1) |
|---|
| 4185 | n/a | self.assertEqual(x.tolist(), m1.tolist()) |
|---|
| 4186 | n/a | |
|---|
| 4187 | n/a | y = memoryview(m2) |
|---|
| 4188 | n/a | self.assertEqual(y.tolist(), m2.tolist()) |
|---|
| 4189 | n/a | self.assertEqual(y.tolist(), nd.tolist()) |
|---|
| 4190 | n/a | m2.release() |
|---|
| 4191 | n/a | y.release() |
|---|
| 4192 | n/a | |
|---|
| 4193 | n/a | nd.pop() # pop the current view |
|---|
| 4194 | n/a | self.assertEqual(x.tolist(), nd.tolist()) |
|---|
| 4195 | n/a | |
|---|
| 4196 | n/a | del nd |
|---|
| 4197 | n/a | m1.release() |
|---|
| 4198 | n/a | x.release() |
|---|
| 4199 | n/a | |
|---|
| 4200 | n/a | # If multiple memoryviews share the same managed buffer, implicit |
|---|
| 4201 | n/a | # release() in the context manager's __exit__() method should still |
|---|
| 4202 | n/a | # work. |
|---|
| 4203 | n/a | def catch22(b): |
|---|
| 4204 | n/a | with memoryview(b) as m2: |
|---|
| 4205 | n/a | pass |
|---|
| 4206 | n/a | |
|---|
| 4207 | n/a | x = bytearray(b'123') |
|---|
| 4208 | n/a | with memoryview(x) as m1: |
|---|
| 4209 | n/a | catch22(m1) |
|---|
| 4210 | n/a | self.assertEqual(m1[0], ord(b'1')) |
|---|
| 4211 | n/a | |
|---|
| 4212 | n/a | x = ndarray(list(range(12)), shape=[2,2,3], format='l') |
|---|
| 4213 | n/a | y = ndarray(x, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4214 | n/a | z = ndarray(y, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4215 | n/a | self.assertIs(z.obj, x) |
|---|
| 4216 | n/a | with memoryview(z) as m: |
|---|
| 4217 | n/a | catch22(m) |
|---|
| 4218 | n/a | self.assertEqual(m[0:1].tolist(), [[[0, 1, 2], [3, 4, 5]]]) |
|---|
| 4219 | n/a | |
|---|
| 4220 | n/a | # Test garbage collection. |
|---|
| 4221 | n/a | for flags in (0, ND_REDIRECT): |
|---|
| 4222 | n/a | x = bytearray(b'123') |
|---|
| 4223 | n/a | with memoryview(x) as m1: |
|---|
| 4224 | n/a | del x |
|---|
| 4225 | n/a | y = ndarray(m1, getbuf=PyBUF_FULL_RO, flags=flags) |
|---|
| 4226 | n/a | with memoryview(y) as m2: |
|---|
| 4227 | n/a | del y |
|---|
| 4228 | n/a | z = ndarray(m2, getbuf=PyBUF_FULL_RO, flags=flags) |
|---|
| 4229 | n/a | with memoryview(z) as m3: |
|---|
| 4230 | n/a | del z |
|---|
| 4231 | n/a | catch22(m3) |
|---|
| 4232 | n/a | catch22(m2) |
|---|
| 4233 | n/a | catch22(m1) |
|---|
| 4234 | n/a | self.assertEqual(m1[0], ord(b'1')) |
|---|
| 4235 | n/a | self.assertEqual(m2[1], ord(b'2')) |
|---|
| 4236 | n/a | self.assertEqual(m3[2], ord(b'3')) |
|---|
| 4237 | n/a | del m3 |
|---|
| 4238 | n/a | del m2 |
|---|
| 4239 | n/a | del m1 |
|---|
| 4240 | n/a | |
|---|
| 4241 | n/a | x = bytearray(b'123') |
|---|
| 4242 | n/a | with memoryview(x) as m1: |
|---|
| 4243 | n/a | del x |
|---|
| 4244 | n/a | y = ndarray(m1, getbuf=PyBUF_FULL_RO, flags=flags) |
|---|
| 4245 | n/a | with memoryview(y) as m2: |
|---|
| 4246 | n/a | del y |
|---|
| 4247 | n/a | z = ndarray(m2, getbuf=PyBUF_FULL_RO, flags=flags) |
|---|
| 4248 | n/a | with memoryview(z) as m3: |
|---|
| 4249 | n/a | del z |
|---|
| 4250 | n/a | catch22(m1) |
|---|
| 4251 | n/a | catch22(m2) |
|---|
| 4252 | n/a | catch22(m3) |
|---|
| 4253 | n/a | self.assertEqual(m1[0], ord(b'1')) |
|---|
| 4254 | n/a | self.assertEqual(m2[1], ord(b'2')) |
|---|
| 4255 | n/a | self.assertEqual(m3[2], ord(b'3')) |
|---|
| 4256 | n/a | del m1, m2, m3 |
|---|
| 4257 | n/a | |
|---|
| 4258 | n/a | # memoryview.release() fails if the view has exported buffers. |
|---|
| 4259 | n/a | x = bytearray(b'123') |
|---|
| 4260 | n/a | with self.assertRaises(BufferError): |
|---|
| 4261 | n/a | with memoryview(x) as m: |
|---|
| 4262 | n/a | ex = ndarray(m) |
|---|
| 4263 | n/a | m[0] == ord(b'1') |
|---|
| 4264 | n/a | |
|---|
| 4265 | n/a | def test_memoryview_redirect(self): |
|---|
| 4266 | n/a | |
|---|
| 4267 | n/a | nd = ndarray([1.0 * x for x in range(12)], shape=[12], format='d') |
|---|
| 4268 | n/a | a = array.array('d', [1.0 * x for x in range(12)]) |
|---|
| 4269 | n/a | |
|---|
| 4270 | n/a | for x in (nd, a): |
|---|
| 4271 | n/a | y = ndarray(x, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4272 | n/a | z = ndarray(y, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4273 | n/a | m = memoryview(z) |
|---|
| 4274 | n/a | |
|---|
| 4275 | n/a | self.assertIs(y.obj, x) |
|---|
| 4276 | n/a | self.assertIs(z.obj, x) |
|---|
| 4277 | n/a | self.assertIs(m.obj, x) |
|---|
| 4278 | n/a | |
|---|
| 4279 | n/a | self.assertEqual(m, x) |
|---|
| 4280 | n/a | self.assertEqual(m, y) |
|---|
| 4281 | n/a | self.assertEqual(m, z) |
|---|
| 4282 | n/a | |
|---|
| 4283 | n/a | self.assertEqual(m[1:3], x[1:3]) |
|---|
| 4284 | n/a | self.assertEqual(m[1:3], y[1:3]) |
|---|
| 4285 | n/a | self.assertEqual(m[1:3], z[1:3]) |
|---|
| 4286 | n/a | del y, z |
|---|
| 4287 | n/a | self.assertEqual(m[1:3], x[1:3]) |
|---|
| 4288 | n/a | |
|---|
| 4289 | n/a | def test_memoryview_from_static_exporter(self): |
|---|
| 4290 | n/a | |
|---|
| 4291 | n/a | fmt = 'B' |
|---|
| 4292 | n/a | lst = [0,1,2,3,4,5,6,7,8,9,10,11] |
|---|
| 4293 | n/a | |
|---|
| 4294 | n/a | # exceptions |
|---|
| 4295 | n/a | self.assertRaises(TypeError, staticarray, 1, 2, 3) |
|---|
| 4296 | n/a | |
|---|
| 4297 | n/a | # view.obj==x |
|---|
| 4298 | n/a | x = staticarray() |
|---|
| 4299 | n/a | y = memoryview(x) |
|---|
| 4300 | n/a | self.verify(y, obj=x, |
|---|
| 4301 | n/a | itemsize=1, fmt=fmt, readonly=1, |
|---|
| 4302 | n/a | ndim=1, shape=[12], strides=[1], |
|---|
| 4303 | n/a | lst=lst) |
|---|
| 4304 | n/a | for i in range(12): |
|---|
| 4305 | n/a | self.assertEqual(y[i], i) |
|---|
| 4306 | n/a | del x |
|---|
| 4307 | n/a | del y |
|---|
| 4308 | n/a | |
|---|
| 4309 | n/a | x = staticarray() |
|---|
| 4310 | n/a | y = memoryview(x) |
|---|
| 4311 | n/a | del y |
|---|
| 4312 | n/a | del x |
|---|
| 4313 | n/a | |
|---|
| 4314 | n/a | x = staticarray() |
|---|
| 4315 | n/a | y = ndarray(x, getbuf=PyBUF_FULL_RO) |
|---|
| 4316 | n/a | z = ndarray(y, getbuf=PyBUF_FULL_RO) |
|---|
| 4317 | n/a | m = memoryview(z) |
|---|
| 4318 | n/a | self.assertIs(y.obj, x) |
|---|
| 4319 | n/a | self.assertIs(m.obj, z) |
|---|
| 4320 | n/a | self.verify(m, obj=z, |
|---|
| 4321 | n/a | itemsize=1, fmt=fmt, readonly=1, |
|---|
| 4322 | n/a | ndim=1, shape=[12], strides=[1], |
|---|
| 4323 | n/a | lst=lst) |
|---|
| 4324 | n/a | del x, y, z, m |
|---|
| 4325 | n/a | |
|---|
| 4326 | n/a | x = staticarray() |
|---|
| 4327 | n/a | y = ndarray(x, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4328 | n/a | z = ndarray(y, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4329 | n/a | m = memoryview(z) |
|---|
| 4330 | n/a | self.assertIs(y.obj, x) |
|---|
| 4331 | n/a | self.assertIs(z.obj, x) |
|---|
| 4332 | n/a | self.assertIs(m.obj, x) |
|---|
| 4333 | n/a | self.verify(m, obj=x, |
|---|
| 4334 | n/a | itemsize=1, fmt=fmt, readonly=1, |
|---|
| 4335 | n/a | ndim=1, shape=[12], strides=[1], |
|---|
| 4336 | n/a | lst=lst) |
|---|
| 4337 | n/a | del x, y, z, m |
|---|
| 4338 | n/a | |
|---|
| 4339 | n/a | # view.obj==NULL |
|---|
| 4340 | n/a | x = staticarray(legacy_mode=True) |
|---|
| 4341 | n/a | y = memoryview(x) |
|---|
| 4342 | n/a | self.verify(y, obj=None, |
|---|
| 4343 | n/a | itemsize=1, fmt=fmt, readonly=1, |
|---|
| 4344 | n/a | ndim=1, shape=[12], strides=[1], |
|---|
| 4345 | n/a | lst=lst) |
|---|
| 4346 | n/a | for i in range(12): |
|---|
| 4347 | n/a | self.assertEqual(y[i], i) |
|---|
| 4348 | n/a | del x |
|---|
| 4349 | n/a | del y |
|---|
| 4350 | n/a | |
|---|
| 4351 | n/a | x = staticarray(legacy_mode=True) |
|---|
| 4352 | n/a | y = memoryview(x) |
|---|
| 4353 | n/a | del y |
|---|
| 4354 | n/a | del x |
|---|
| 4355 | n/a | |
|---|
| 4356 | n/a | x = staticarray(legacy_mode=True) |
|---|
| 4357 | n/a | y = ndarray(x, getbuf=PyBUF_FULL_RO) |
|---|
| 4358 | n/a | z = ndarray(y, getbuf=PyBUF_FULL_RO) |
|---|
| 4359 | n/a | m = memoryview(z) |
|---|
| 4360 | n/a | self.assertIs(y.obj, None) |
|---|
| 4361 | n/a | self.assertIs(m.obj, z) |
|---|
| 4362 | n/a | self.verify(m, obj=z, |
|---|
| 4363 | n/a | itemsize=1, fmt=fmt, readonly=1, |
|---|
| 4364 | n/a | ndim=1, shape=[12], strides=[1], |
|---|
| 4365 | n/a | lst=lst) |
|---|
| 4366 | n/a | del x, y, z, m |
|---|
| 4367 | n/a | |
|---|
| 4368 | n/a | x = staticarray(legacy_mode=True) |
|---|
| 4369 | n/a | y = ndarray(x, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4370 | n/a | z = ndarray(y, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT) |
|---|
| 4371 | n/a | m = memoryview(z) |
|---|
| 4372 | n/a | # Clearly setting view.obj==NULL is inferior, since it |
|---|
| 4373 | n/a | # messes up the redirection chain: |
|---|
| 4374 | n/a | self.assertIs(y.obj, None) |
|---|
| 4375 | n/a | self.assertIs(z.obj, y) |
|---|
| 4376 | n/a | self.assertIs(m.obj, y) |
|---|
| 4377 | n/a | self.verify(m, obj=y, |
|---|
| 4378 | n/a | itemsize=1, fmt=fmt, readonly=1, |
|---|
| 4379 | n/a | ndim=1, shape=[12], strides=[1], |
|---|
| 4380 | n/a | lst=lst) |
|---|
| 4381 | n/a | del x, y, z, m |
|---|
| 4382 | n/a | |
|---|
| 4383 | n/a | def test_memoryview_getbuffer_undefined(self): |
|---|
| 4384 | n/a | |
|---|
| 4385 | n/a | # getbufferproc does not adhere to the new documentation |
|---|
| 4386 | n/a | nd = ndarray([1,2,3], [3], flags=ND_GETBUF_FAIL|ND_GETBUF_UNDEFINED) |
|---|
| 4387 | n/a | self.assertRaises(BufferError, memoryview, nd) |
|---|
| 4388 | n/a | |
|---|
| 4389 | n/a | def test_issue_7385(self): |
|---|
| 4390 | n/a | x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL) |
|---|
| 4391 | n/a | self.assertRaises(BufferError, memoryview, x) |
|---|
| 4392 | n/a | |
|---|
| 4393 | n/a | |
|---|
| 4394 | n/a | if __name__ == "__main__": |
|---|
| 4395 | n/a | unittest.main() |
|---|