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

Method verify_getbuf

Lib/test/test_buffer.py:984–1030  ·  view source on GitHub ↗
(self, orig_ex, ex, req, sliced=False)

Source from the content-addressed store, hash-verified

982 m.tobytes() # Releasing mm didn't release m
983
984 def verify_getbuf(self, orig_ex, ex, req, sliced=False):
985 def match(req, flag):
986 return ((req&flag) == flag)
987
988 if (# writable request to read-only exporter
989 (ex.readonly and match(req, PyBUF_WRITABLE)) or
990 # cannot match explicit contiguity request
991 (match(req, PyBUF_C_CONTIGUOUS) and not ex.c_contiguous) or
992 (match(req, PyBUF_F_CONTIGUOUS) and not ex.f_contiguous) or
993 (match(req, PyBUF_ANY_CONTIGUOUS) and not ex.contiguous) or
994 # buffer needs suboffsets
995 (not match(req, PyBUF_INDIRECT) and ex.suboffsets) or
996 # buffer without strides must be C-contiguous
997 (not match(req, PyBUF_STRIDES) and not ex.c_contiguous) or
998 # PyBUF_SIMPLE|PyBUF_FORMAT and PyBUF_WRITABLE|PyBUF_FORMAT
999 (not match(req, PyBUF_ND) and match(req, PyBUF_FORMAT))):
1000
1001 self.assertRaises(BufferError, ndarray, ex, getbuf=req)
1002 return
1003
1004 if isinstance(ex, ndarray) or is_memoryview_format(ex.format):
1005 lst = ex.tolist()
1006 else:
1007 nd = ndarray(ex, getbuf=PyBUF_FULL_RO)
1008 lst = nd.tolist()
1009
1010 # The consumer may have requested default values or a NULL format.
1011 ro = False if match(req, PyBUF_WRITABLE) else ex.readonly
1012 fmt = ex.format
1013 itemsize = ex.itemsize
1014 ndim = ex.ndim
1015 if not match(req, PyBUF_FORMAT):
1016 # itemsize refers to the original itemsize before the cast.
1017 # The equality product(shape) * itemsize = len still holds.
1018 # The equality calcsize(format) = itemsize does _not_ hold.
1019 fmt = ''
1020 lst = orig_ex.tobytes() # Issue 12834
1021 if not match(req, PyBUF_ND):
1022 ndim = 1
1023 shape = orig_ex.shape if match(req, PyBUF_ND) else ()
1024 strides = orig_ex.strides if match(req, PyBUF_STRIDES) else ()
1025
1026 nd = ndarray(ex, getbuf=req)
1027 self.verify(nd, obj=ex,
1028 itemsize=itemsize, fmt=fmt, readonly=ro,
1029 ndim=ndim, shape=shape, strides=strides,
1030 lst=lst, sliced=sliced)
1031
1032 @support.requires_resource('cpu')
1033 def test_ndarray_getbuf(self):

Callers 1

test_ndarray_getbufMethod · 0.95

Calls 7

verifyMethod · 0.95
matchFunction · 0.85
isinstanceFunction · 0.85
is_memoryview_formatFunction · 0.85
assertRaisesMethod · 0.45
tolistMethod · 0.45
tobytesMethod · 0.45

Tested by

no test coverage detected