9from pybind11_tests
import ConstructorStats
10from pybind11_tests
import buffers
as m
12np = pytest.importorskip(
"numpy")
16 with pytest.raises(RuntimeError)
as excinfo:
17 m.Matrix(np.array([1, 2, 3]))
18 assert str(excinfo.value) ==
"Incompatible buffer format!"
20 m3 = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
23 for i
in range(m4.rows()):
24 for j
in range(m4.cols()):
25 assert m3[i, j] == m4[i, j]
28 assert cstats.alive() == 1
30 assert cstats.alive() == 0
31 assert cstats.values() == [
"2x3 matrix"]
32 assert cstats.copy_constructions == 0
34 assert cstats.copy_assignments == 0
35 assert cstats.move_assignments == 0
41 env.PYPY, reason=
"PyPy 7.3.7 doesn't clear this anymore", strict=
False
52 assert struct.unpack_from(
"f", mat, (3 * 4 + 2) * 4) == (7,)
53 assert struct.unpack_from(
"f", mat, (2 * 4 + 3) * 4) == (4,)
55 mat2 = np.array(mat, copy=
False)
56 assert mat2.shape == (5, 4)
57 assert abs(mat2).sum() == 11
58 assert mat2[2, 3] == 4
and mat2[3, 2] == 7
60 assert mat2[2, 3] == 5
63 assert cstats.alive() == 1
66 assert cstats.alive() == 1
69 assert cstats.alive() == 0
70 assert cstats.values() == [
"5x4 matrix"]
71 assert cstats.copy_constructions == 0
73 assert cstats.copy_assignments == 0
74 assert cstats.move_assignments == 0
78 """SquareMatrix is derived from Matrix and inherits the buffer protocol"""
80 matrix = m.SquareMatrix(5)
82 assert np.asarray(matrix).shape == (5, 5)
86 for cls
in [m.Buffer, m.ConstBuffer, m.DerivedBuffer]:
88 buf.value = 0x12345678
89 value = struct.unpack(
"i",
bytearray(buf))[0]
90 assert value == 0x12345678
94 buf = m.BufferReadOnly(0x64)
96 assert view[0] == b
"d" if env.PY2
else 0x64
98 with pytest.raises(TypeError):
99 view[0] = b
"\0" if env.PY2
else 0
103 buf = m.BufferReadOnlySelect()
105 memoryview(buf)[0] = b
"d" if env.PY2
else 0x64
106 assert buf.value == 0x64
108 io.BytesIO(b
"A").readinto(buf)
109 assert buf.value == ord(b
"A")
112 with pytest.raises(TypeError):
114 with pytest.raises(TypeError):
115 io.BytesIO(b
"1").readinto(buf)
119 char1d = (ctypes.c_char * 10)()
120 int1d = (ctypes.c_int * 15)()
121 long1d = (ctypes.c_long * 7)()
123 for carray
in (char1d, int1d, long1d):
124 info = m.get_buffer_info(carray)
125 assert info.itemsize == ctypes.sizeof(carray._type_)
126 assert info.size ==
len(carray)
127 assert info.ndim == 1
128 assert info.shape == [info.size]
129 assert info.strides == [info.itemsize]
130 assert not info.readonly
134 char2d = ((ctypes.c_char * 10) * 4)()
135 int2d = ((ctypes.c_int * 15) * 3)()
136 long2d = ((ctypes.c_long * 7) * 2)()
138 for carray
in (char2d, int2d, long2d):
139 info = m.get_buffer_info(carray)
140 assert info.itemsize == ctypes.sizeof(carray[0]._type_)
141 assert info.size ==
len(carray) *
len(carray[0])
142 assert info.ndim == 2
143 assert info.shape == [
len(carray),
len(carray[0])]
144 assert info.strides == [info.itemsize *
len(carray[0]), info.itemsize]
145 assert not info.readonly
149 "env.PYPY and env.PY2", reason=
"PyPy2 bytes buffer not reported as readonly"
152 test_pystr = b
"0123456789"
153 for pyarray
in (test_pystr,
bytearray(test_pystr)):
154 pyinfo = m.get_buffer_info(pyarray)
157 cbytes = (ctypes.c_char *
len(pyarray)).from_buffer_copy(pyarray)
158 cinfo = m.get_buffer_info(cbytes)
160 cbytes = (ctypes.c_char *
len(pyarray)).from_buffer(pyarray)
161 cinfo = m.get_buffer_info(cbytes)
163 assert cinfo.size == pyinfo.size
164 assert cinfo.ndim == pyinfo.ndim
165 assert cinfo.shape == pyinfo.shape
166 assert cinfo.strides == pyinfo.strides
167 assert not cinfo.readonly
static ConstructorStats & get(std::type_index type)
size_t len(handle h)
Get the length of a Python object.
def test_selective_readonly_buffer()
def test_readonly_buffer()
def test_pointer_to_member_fn()
def test_ctypes_array_2d()
def test_inherited_protocol()
def test_ctypes_from_buffer()
def test_ctypes_array_1d()
std::string abs(const Vector2 &)