MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_ndarray_getbuf

Method test_ndarray_getbuf

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

Source from the content-addressed store, hash-verified

1031
1032 @support.requires_resource('cpu')
1033 def test_ndarray_getbuf(self):
1034 requests = (
1035 # distinct flags
1036 PyBUF_INDIRECT, PyBUF_STRIDES, PyBUF_ND, PyBUF_SIMPLE,
1037 PyBUF_C_CONTIGUOUS, PyBUF_F_CONTIGUOUS, PyBUF_ANY_CONTIGUOUS,
1038 # compound requests
1039 PyBUF_FULL, PyBUF_FULL_RO,
1040 PyBUF_RECORDS, PyBUF_RECORDS_RO,
1041 PyBUF_STRIDED, PyBUF_STRIDED_RO,
1042 PyBUF_CONTIG, PyBUF_CONTIG_RO,
1043 )
1044 # items and format
1045 items_fmt = (
1046 ([True if x % 2 else False for x in range(12)], '?'),
1047 ([1,2,3,4,5,6,7,8,9,10,11,12], 'b'),
1048 ([1,2,3,4,5,6,7,8,9,10,11,12], 'B'),
1049 ([(2**31-x) if x % 2 else (-2**31+x) for x in range(12)], 'l')
1050 )
1051 # shape, strides, offset
1052 structure = (
1053 ([], [], 0),
1054 ([1,3,1], [], 0),
1055 ([12], [], 0),
1056 ([12], [-1], 11),
1057 ([6], [2], 0),
1058 ([6], [-2], 11),
1059 ([3, 4], [], 0),
1060 ([3, 4], [-4, -1], 11),
1061 ([2, 2], [4, 1], 4),
1062 ([2, 2], [-4, -1], 8)
1063 )
1064 # ndarray creation flags
1065 ndflags = (
1066 0, ND_WRITABLE, ND_FORTRAN, ND_FORTRAN|ND_WRITABLE,
1067 ND_PIL, ND_PIL|ND_WRITABLE
1068 )
1069 # flags that can actually be used as flags
1070 real_flags = (0, PyBUF_WRITABLE, PyBUF_FORMAT,
1071 PyBUF_WRITABLE|PyBUF_FORMAT)
1072
1073 for items, fmt in items_fmt:
1074 itemsize = struct.calcsize(fmt)
1075 for shape, strides, offset in structure:
1076 strides = [v * itemsize for v in strides]
1077 offset *= itemsize
1078 for flags in ndflags:
1079
1080 if strides and (flags&ND_FORTRAN):
1081 continue
1082 if not shape and (flags&ND_PIL):
1083 continue
1084
1085 _items = items if shape else items[0]
1086 ex1 = ndarray(_items, format=fmt, flags=flags,
1087 shape=shape, strides=strides, offset=offset)
1088 ex2 = ex1[::-2] if shape else None
1089
1090 m1 = memoryview(ex1)

Callers

nothing calls this directly

Calls 4

verify_getbufMethod · 0.95
assertTrueMethod · 0.80
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected