μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_local_bindings.py
Go to the documentation of this file.
1# -*- coding: utf-8 -*-
2import pytest
3
4import env # noqa: F401
5from pybind11_tests import local_bindings as m
6
7
9 """Load a `py::module_local` type that's only registered in an external module"""
10 import pybind11_cross_module_tests as cm
11
12 assert m.load_external1(cm.ExternalType1(11)) == 11
13 assert m.load_external2(cm.ExternalType2(22)) == 22
14
15 with pytest.raises(TypeError) as excinfo:
16 assert m.load_external2(cm.ExternalType1(21)) == 21
17 assert "incompatible function arguments" in str(excinfo.value)
18
19 with pytest.raises(TypeError) as excinfo:
20 assert m.load_external1(cm.ExternalType2(12)) == 12
21 assert "incompatible function arguments" in str(excinfo.value)
22
23
25 """Tests that duplicate `py::module_local` class bindings work across modules"""
26
27 # Make sure we can load the second module with the conflicting (but local) definition:
28 import pybind11_cross_module_tests as cm
29
30 i1 = m.LocalType(5)
31 assert i1.get() == 4
32 assert i1.get3() == 8
33
34 i2 = cm.LocalType(10)
35 assert i2.get() == 11
36 assert i2.get2() == 12
37
38 assert not hasattr(i1, "get2")
39 assert not hasattr(i2, "get3")
40
41 # Loading within the local module
42 assert m.local_value(i1) == 5
43 assert cm.local_value(i2) == 10
44
45 # Cross-module loading works as well (on failure, the type loader looks for
46 # external module-local converters):
47 assert m.local_value(i2) == 10
48 assert cm.local_value(i1) == 5
49
50
52 """Tests that attempting to register a non-local type in multiple modules fails"""
53 import pybind11_cross_module_tests as cm
54
55 with pytest.raises(RuntimeError) as excinfo:
56 cm.register_nonlocal()
57 assert (
58 str(excinfo.value) == 'generic_type: type "NonLocalType" is already registered!'
59 )
60
61
63 """Tests expected failure when registering a class twice with py::local in the same module"""
64 with pytest.raises(RuntimeError) as excinfo:
65 m.register_local_external()
66 import pybind11_tests
67
68 assert str(excinfo.value) == (
69 'generic_type: type "LocalExternal" is already registered!'
70 if hasattr(pybind11_tests, "class_")
71 else "test_class not enabled"
72 )
73
74
76 import pybind11_cross_module_tests as cm
77
78 v1, v2 = m.LocalVec(), cm.LocalVec()
79 v1.append(m.LocalType(1))
80 v1.append(m.LocalType(2))
81 v2.append(cm.LocalType(1))
82 v2.append(cm.LocalType(2))
83
84 # Cross module value loading:
85 v1.append(cm.LocalType(3))
86 v2.append(m.LocalType(3))
87
88 assert [i.get() for i in v1] == [0, 1, 2]
89 assert [i.get() for i in v2] == [2, 3, 4]
90
91 v3, v4 = m.NonLocalVec(), cm.NonLocalVec2()
92 v3.append(m.NonLocalType(1))
93 v3.append(m.NonLocalType(2))
94 v4.append(m.NonLocal2(3))
95 v4.append(m.NonLocal2(4))
96
97 assert [i.get() for i in v3] == [1, 2]
98 assert [i.get() for i in v4] == [13, 14]
99
100 d1, d2 = m.LocalMap(), cm.LocalMap()
101 d1["a"] = v1[0]
102 d1["b"] = v1[1]
103 d2["c"] = v2[0]
104 d2["d"] = v2[1]
105 assert {i: d1[i].get() for i in d1} == {"a": 0, "b": 1}
106 assert {i: d2[i].get() for i in d2} == {"c": 2, "d": 3}
107
108
110 import pybind11_cross_module_tests as cm
111
112 with pytest.raises(RuntimeError) as excinfo:
113 cm.register_nonlocal_map()
114 assert (
115 str(excinfo.value) == 'generic_type: type "NonLocalMap" is already registered!'
116 )
117
118 with pytest.raises(RuntimeError) as excinfo:
119 cm.register_nonlocal_vec()
120 assert (
121 str(excinfo.value) == 'generic_type: type "NonLocalVec" is already registered!'
122 )
123
124 with pytest.raises(RuntimeError) as excinfo:
125 cm.register_nonlocal_map2()
126 assert (
127 str(excinfo.value) == 'generic_type: type "NonLocalMap2" is already registered!'
128 )
129
130
132 """Local types take precedence over globally registered types: a module with a `module_local`
133 type can be registered even if the type is already registered globally. With the module,
134 casting will go to the local type; outside the module casting goes to the global type."""
135 import pybind11_cross_module_tests as cm
136
137 m.register_mixed_global()
138 m.register_mixed_local()
139
140 a = []
141 a.append(m.MixedGlobalLocal(1))
142 a.append(m.MixedLocalGlobal(2))
143 a.append(m.get_mixed_gl(3))
144 a.append(m.get_mixed_lg(4))
145
146 assert [x.get() for x in a] == [101, 1002, 103, 1004]
147
148 cm.register_mixed_global_local()
149 cm.register_mixed_local_global()
150 a.append(m.MixedGlobalLocal(5))
151 a.append(m.MixedLocalGlobal(6))
152 a.append(cm.MixedGlobalLocal(7))
153 a.append(cm.MixedLocalGlobal(8))
154 a.append(m.get_mixed_gl(9))
155 a.append(m.get_mixed_lg(10))
156 a.append(cm.get_mixed_gl(11))
157 a.append(cm.get_mixed_lg(12))
158
159 assert [x.get() for x in a] == [
160 101,
161 1002,
162 103,
163 1004,
164 105,
165 1006,
166 207,
167 2008,
168 109,
169 1010,
170 211,
171 2012,
172 ]
173
174
176 """Makes sure the internal local type map differs across the two modules"""
177 import pybind11_cross_module_tests as cm
178
179 assert m.local_cpp_types_addr() != cm.local_cpp_types_addr()
180
181
182@pytest.mark.xfail("env.PYPY and sys.pypy_version_info < (7, 3, 2)")
184 """One module uses a generic vector caster from `<pybind11/stl.h>` while the other
185 exports `std::vector<int>` via `py:bind_vector` and `py::module_local`"""
186 import pybind11_cross_module_tests as cm
187
188 v1 = cm.VectorInt([1, 2, 3])
189 assert m.load_vector_via_caster(v1) == 6
190 assert cm.load_vector_via_binding(v1) == 6
191
192 v2 = [1, 2, 3]
193 assert m.load_vector_via_caster(v2) == 6
194 with pytest.raises(TypeError) as excinfo:
195 cm.load_vector_via_binding(v2)
196 assert (
197 msg(excinfo.value)
198 == """
199 load_vector_via_binding(): incompatible function arguments. The following argument types are supported:
200 1. (arg0: pybind11_cross_module_tests.VectorInt) -> int
201
202 Invoked with: [1, 2, 3]
203 """ # noqa: E501 line too long
204 )
205
206
208 import pybind11_cross_module_tests as cm
209
210 v1 = m.LocalVec()
211 v1.append(m.LocalType(1))
212 v2 = cm.LocalVec()
213 v2.append(cm.LocalType(2))
214
215 # Returning the self pointer should get picked up as returning an existing
216 # instance (even when that instance is of a foreign, non-local type).
217 assert m.return_self(v1) is v1
218 assert cm.return_self(v2) is v2
219 assert m.return_self(v2) is v2
220 assert cm.return_self(v1) is v1
221
222 assert m.LocalVec is not cm.LocalVec
223 # Returning a copy, on the other hand, always goes to the local type,
224 # regardless of where the source type came from.
225 assert type(m.return_copy(v1)) is m.LocalVec
226 assert type(m.return_copy(v2)) is m.LocalVec
227 assert type(cm.return_copy(v1)) is cm.LocalVec
228 assert type(cm.return_copy(v2)) is cm.LocalVec
229
230 # Test the example given in the documentation (which also tests inheritance casting):
231 mycat = m.Cat("Fluffy")
232 mydog = cm.Dog("Rover")
233 assert mycat.get_name() == "Fluffy"
234 assert mydog.name() == "Rover"
235 assert m.Cat.__base__.__name__ == "Pet"
236 assert cm.Dog.__base__.__name__ == "Pet"
237 assert m.Cat.__base__ is not cm.Dog.__base__
238 assert m.pet_name(mycat) == "Fluffy"
239 assert m.pet_name(mydog) == "Rover"
240 assert cm.pet_name(mycat) == "Fluffy"
241 assert cm.pet_name(mydog) == "Rover"
242
243 assert m.MixGL is not cm.MixGL
244 a = m.MixGL(1)
245 b = cm.MixGL(2)
246 assert m.get_gl_value(a) == 11
247 assert m.get_gl_value(b) == 12
248 assert cm.get_gl_value(a) == 101
249 assert cm.get_gl_value(b) == 102
250
251 c, d = m.MixGL2(3), cm.MixGL2(4)
252 with pytest.raises(TypeError) as excinfo:
253 m.get_gl_value(c)
254 assert "incompatible function arguments" in str(excinfo.value)
255 with pytest.raises(TypeError) as excinfo:
256 m.get_gl_value(d)
257 assert "incompatible function arguments" in str(excinfo.value)
Definition: pytypes.h:1200
Definition: pytypes.h:1167
bool hasattr(handle obj, handle name)
Definition: pytypes.h:517