MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_c_contiguous_to_pybuffer

Function test_c_contiguous_to_pybuffer

tests/test_buffers.py:245–294  ·  view source on GitHub ↗
(type)

Source from the content-addressed store, hash-verified

243
244@pytest.mark.parametrize("type", ["pybind11", "numpy"])
245def test_c_contiguous_to_pybuffer(type):
246 if type == "pybind11":
247 mat = m.Matrix(5, 4)
248 elif type == "numpy":
249 mat = np.empty((5, 4), dtype=np.float32)
250 else:
251 raise ValueError(f"Unknown parametrization {type}")
252
253 info = m.get_py_buffer(mat, m.PyBUF_SIMPLE)
254 assert info.format is None
255 assert info.itemsize == ctypes.sizeof(ctypes.c_float)
256 assert info.len == 5 * 4 * info.itemsize
257 assert info.ndim == 0 # See discussion on PR #5407.
258 assert info.shape is None
259 assert info.strides is None
260 assert info.suboffsets is None
261 assert not info.readonly
262 info = m.get_py_buffer(mat, m.PyBUF_SIMPLE | m.PyBUF_FORMAT)
263 assert info.format == "f"
264 assert info.itemsize == ctypes.sizeof(ctypes.c_float)
265 assert info.len == 5 * 4 * info.itemsize
266 assert info.ndim == 0 # See discussion on PR #5407.
267 assert info.shape is None
268 assert info.strides is None
269 assert info.suboffsets is None
270 assert not info.readonly
271 info = m.get_py_buffer(mat, m.PyBUF_ND)
272 assert info.itemsize == ctypes.sizeof(ctypes.c_float)
273 assert info.len == 5 * 4 * info.itemsize
274 assert info.ndim == 2
275 assert info.shape == [5, 4]
276 assert info.strides is None
277 assert info.suboffsets is None
278 assert not info.readonly
279 info = m.get_py_buffer(mat, m.PyBUF_STRIDES)
280 assert info.itemsize == ctypes.sizeof(ctypes.c_float)
281 assert info.len == 5 * 4 * info.itemsize
282 assert info.ndim == 2
283 assert info.shape == [5, 4]
284 assert info.strides == [4 * info.itemsize, info.itemsize]
285 assert info.suboffsets is None
286 assert not info.readonly
287 info = m.get_py_buffer(mat, m.PyBUF_INDIRECT)
288 assert info.itemsize == ctypes.sizeof(ctypes.c_float)
289 assert info.len == 5 * 4 * info.itemsize
290 assert info.ndim == 2
291 assert info.shape == [5, 4]
292 assert info.strides == [4 * info.itemsize, info.itemsize]
293 assert info.suboffsets is None # Should be filled in here, but we don't use it.
294 assert not info.readonly
295
296
297@pytest.mark.parametrize("type", ["pybind11", "numpy"])

Callers

nothing calls this directly

Calls 2

MatrixMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected