8 from pybind11_tests
import ConstructorStats
9 from pybind11_tests
import buffers
as m
11 np = pytest.importorskip(
"numpy")
15 with pytest.raises(RuntimeError)
as excinfo:
16 m.Matrix(np.array([1, 2, 3]))
17 assert str(excinfo.value) ==
"Incompatible buffer format!" 19 m3 = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
22 for i
in range(m4.rows()):
23 for j
in range(m4.cols()):
24 assert m3[i, j] == m4[i, j]
27 assert cstats.alive() == 1
29 assert cstats.alive() == 0
30 assert cstats.values() == [
"2x3 matrix"]
31 assert cstats.copy_constructions == 0
33 assert cstats.copy_assignments == 0
34 assert cstats.move_assignments == 0
40 env.PYPY, reason=
"PyPy 7.3.7 doesn't clear this anymore", strict=
False 51 assert struct.unpack_from(
"f", mat, (3 * 4 + 2) * 4) == (7,)
52 assert struct.unpack_from(
"f", mat, (2 * 4 + 3) * 4) == (4,)
54 mat2 = np.array(mat, copy=
False)
55 assert mat2.shape == (5, 4)
56 assert abs(mat2).sum() == 11
57 assert mat2[2, 3] == 4
and mat2[3, 2] == 7
59 assert mat2[2, 3] == 5
62 assert cstats.alive() == 1
65 assert cstats.alive() == 1
68 assert cstats.alive() == 0
69 assert cstats.values() == [
"5x4 matrix"]
70 assert cstats.copy_constructions == 0
72 assert cstats.copy_assignments == 0
73 assert cstats.move_assignments == 0
77 """SquareMatrix is derived from Matrix and inherits the buffer protocol""" 79 matrix = m.SquareMatrix(5)
81 assert np.asarray(matrix).shape == (5, 5)
85 for cls
in [m.Buffer, m.ConstBuffer, m.DerivedBuffer]:
87 buf.value = 0x12345678
88 value = struct.unpack(
"i",
bytearray(buf))[0]
89 assert value == 0x12345678
93 buf = m.BufferReadOnly(0x64)
95 assert view[0] == 0x64
97 with pytest.raises(TypeError):
102 buf = m.BufferReadOnlySelect()
105 assert buf.value == 0x64
107 io.BytesIO(b
"A").readinto(buf)
108 assert buf.value == ord(b
"A")
111 with pytest.raises(TypeError):
113 with pytest.raises(TypeError):
114 io.BytesIO(b
"1").readinto(buf)
118 char1d = (ctypes.c_char * 10)()
119 int1d = (ctypes.c_int * 15)()
120 long1d = (ctypes.c_long * 7)()
122 for carray
in (char1d, int1d, long1d):
123 info = m.get_buffer_info(carray)
124 assert info.itemsize == ctypes.sizeof(carray._type_)
125 assert info.size ==
len(carray)
126 assert info.ndim == 1
127 assert info.shape == [info.size]
128 assert info.strides == [info.itemsize]
129 assert not info.readonly
133 char2d = ((ctypes.c_char * 10) * 4)()
134 int2d = ((ctypes.c_int * 15) * 3)()
135 long2d = ((ctypes.c_long * 7) * 2)()
137 for carray
in (char2d, int2d, long2d):
138 info = m.get_buffer_info(carray)
139 assert info.itemsize == ctypes.sizeof(carray[0]._type_)
140 assert info.size ==
len(carray) *
len(carray[0])
141 assert info.ndim == 2
142 assert info.shape == [
len(carray),
len(carray[0])]
143 assert info.strides == [info.itemsize *
len(carray[0]), info.itemsize]
144 assert not info.readonly
148 test_pystr = b
"0123456789" 149 for pyarray
in (test_pystr,
bytearray(test_pystr)):
150 pyinfo = m.get_buffer_info(pyarray)
153 cbytes = (ctypes.c_char *
len(pyarray)).from_buffer_copy(pyarray)
154 cinfo = m.get_buffer_info(cbytes)
156 cbytes = (ctypes.c_char *
len(pyarray)).from_buffer(pyarray)
157 cinfo = m.get_buffer_info(cbytes)
159 assert cinfo.size == pyinfo.size
160 assert cinfo.ndim == pyinfo.ndim
161 assert cinfo.shape == pyinfo.shape
162 assert cinfo.strides == pyinfo.strides
163 assert not cinfo.readonly
def test_ctypes_from_buffer()
def test_pointer_to_member_fn()
static ConstructorStats & get(std::type_index type)
def test_ctypes_array_2d()
Double_ range(const Point2_ &p, const Point2_ &q)
def test_readonly_buffer()
def test_inherited_protocol()
size_t len(handle h)
Get the length of a Python object.
def test_ctypes_array_1d()
def test_selective_readonly_buffer()