ยปCore Development>Code coverage>Lib/test/test_buffer.py

Python code coverage for Lib/test/test_buffer.py

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