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

Method test_ndarray_getbuf

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

Source from the content-addressed store, hash-verified

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