MCPcopy Index your code
hub / github.com/python/cpython / test_memoryview_cast_1D_ND

Method test_memoryview_cast_1D_ND

Lib/test/test_buffer.py:2777–2859  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2775
2776 @support.requires_resource('cpu')
2777 def test_memoryview_cast_1D_ND(self):
2778 # Cast between C-contiguous buffers. At least one buffer must
2779 # be 1D, at least one format must be 'c', 'b' or 'B'.
2780 for _tshape in gencastshapes():
2781 for char in fmtdict['@']:
2782 # Casts to _Bool are undefined if the source contains values
2783 # other than 0 or 1.
2784 if char == "?":
2785 continue
2786 tfmt = ('', '@')[randrange(2)] + char
2787 tsize = struct.calcsize(tfmt)
2788 n = prod(_tshape) * tsize
2789 obj = 'memoryview' if is_byte_format(tfmt) else 'bytefmt'
2790 for fmt, items, _ in iter_format(n, obj):
2791 size = struct.calcsize(fmt)
2792 shape = [n] if n > 0 else []
2793 tshape = _tshape + [size]
2794
2795 ex = ndarray(items, shape=shape, format=fmt)
2796 m = memoryview(ex)
2797
2798 titems, tshape = cast_items(ex, tfmt, tsize, shape=tshape)
2799
2800 if titems is None:
2801 self.assertRaises(TypeError, m.cast, tfmt, tshape)
2802 continue
2803 if titems == 'nan':
2804 continue # NaNs in lists are a recipe for trouble.
2805
2806 # 1D -> ND
2807 nd = ndarray(titems, shape=tshape, format=tfmt)
2808
2809 m2 = m.cast(tfmt, shape=tshape)
2810 ndim = len(tshape)
2811 strides = nd.strides
2812 lst = nd.tolist()
2813 self.verify(m2, obj=ex,
2814 itemsize=tsize, fmt=tfmt, readonly=True,
2815 ndim=ndim, shape=tshape, strides=strides,
2816 lst=lst, cast=True)
2817
2818 # ND -> 1D
2819 m3 = m2.cast(fmt)
2820 m4 = m2.cast(fmt, shape=shape)
2821 ndim = len(shape)
2822 strides = ex.strides
2823 lst = ex.tolist()
2824
2825 self.verify(m3, obj=ex,
2826 itemsize=size, fmt=fmt, readonly=True,
2827 ndim=ndim, shape=shape, strides=strides,
2828 lst=lst, cast=True)
2829
2830 self.verify(m4, obj=ex,
2831 itemsize=size, fmt=fmt, readonly=True,
2832 ndim=ndim, shape=shape, strides=strides,
2833 lst=lst, cast=True)
2834

Callers

nothing calls this directly

Calls 11

verifyMethod · 0.95
gencastshapesFunction · 0.85
is_byte_formatFunction · 0.85
iter_formatFunction · 0.85
cast_itemsFunction · 0.85
castMethod · 0.80
prodFunction · 0.70
BEPointClass · 0.70
assertRaisesMethod · 0.45
assertEqualMethod · 0.45
assertIsMethod · 0.45

Tested by

no test coverage detected