1 from __future__
import annotations
6 from pybind11_tests
import local_bindings
as m
10 """Load a `py::module_local` type that's only registered in an external module"""
11 import pybind11_cross_module_tests
as cm
13 assert m.load_external1(cm.ExternalType1(11)) == 11
14 assert m.load_external2(cm.ExternalType2(22)) == 22
16 with pytest.raises(TypeError)
as excinfo:
17 assert m.load_external2(cm.ExternalType1(21)) == 21
18 assert "incompatible function arguments" in str(excinfo.value)
20 with pytest.raises(TypeError)
as excinfo:
21 assert m.load_external1(cm.ExternalType2(12)) == 12
22 assert "incompatible function arguments" in str(excinfo.value)
26 """Tests that duplicate `py::module_local` class bindings work across modules"""
29 import pybind11_cross_module_tests
as cm
37 assert i2.get2() == 12
43 assert m.local_value(i1) == 5
44 assert cm.local_value(i2) == 10
48 assert m.local_value(i2) == 10
49 assert cm.local_value(i1) == 5
53 """Tests that attempting to register a non-local type in multiple modules fails"""
54 import pybind11_cross_module_tests
as cm
56 with pytest.raises(RuntimeError)
as excinfo:
57 cm.register_nonlocal()
59 str(excinfo.value) ==
'generic_type: type "NonLocalType" is already registered!'
64 """Tests expected failure when registering a class twice with py::local in the same module"""
65 with pytest.raises(RuntimeError)
as excinfo:
66 m.register_local_external()
69 assert str(excinfo.value) == (
70 'generic_type: type "LocalExternal" is already registered!'
71 if hasattr(pybind11_tests,
"class_")
72 else "test_class not enabled"
77 import pybind11_cross_module_tests
as cm
79 v1, v2 = m.LocalVec(), cm.LocalVec()
80 v1.append(m.LocalType(1))
81 v1.append(m.LocalType(2))
82 v2.append(cm.LocalType(1))
83 v2.append(cm.LocalType(2))
86 v1.append(cm.LocalType(3))
87 v2.append(m.LocalType(3))
89 assert [i.get()
for i
in v1] == [0, 1, 2]
90 assert [i.get()
for i
in v2] == [2, 3, 4]
92 v3, v4 = m.NonLocalVec(), cm.NonLocalVec2()
93 v3.append(m.NonLocalType(1))
94 v3.append(m.NonLocalType(2))
95 v4.append(m.NonLocal2(3))
96 v4.append(m.NonLocal2(4))
98 assert [i.get()
for i
in v3] == [1, 2]
99 assert [i.get()
for i
in v4] == [13, 14]
101 d1, d2 = m.LocalMap(), cm.LocalMap()
106 assert {i: d1[i].
get()
for i
in d1} == {
"a": 0,
"b": 1}
107 assert {i: d2[i].
get()
for i
in d2} == {
"c": 2,
"d": 3}
111 import pybind11_cross_module_tests
as cm
113 with pytest.raises(RuntimeError)
as excinfo:
114 cm.register_nonlocal_map()
116 str(excinfo.value) ==
'generic_type: type "NonLocalMap" is already registered!'
119 with pytest.raises(RuntimeError)
as excinfo:
120 cm.register_nonlocal_vec()
122 str(excinfo.value) ==
'generic_type: type "NonLocalVec" is already registered!'
125 with pytest.raises(RuntimeError)
as excinfo:
126 cm.register_nonlocal_map2()
128 str(excinfo.value) ==
'generic_type: type "NonLocalMap2" is already registered!'
133 """Local types take precedence over globally registered types: a module with a `module_local`
134 type can be registered even if the type is already registered globally. With the module,
135 casting will go to the local type; outside the module casting goes to the global type.
137 import pybind11_cross_module_tests
as cm
139 m.register_mixed_global()
140 m.register_mixed_local()
143 a.append(m.MixedGlobalLocal(1))
144 a.append(m.MixedLocalGlobal(2))
145 a.append(m.get_mixed_gl(3))
146 a.append(m.get_mixed_lg(4))
148 assert [x.get()
for x
in a] == [101, 1002, 103, 1004]
150 cm.register_mixed_global_local()
151 cm.register_mixed_local_global()
152 a.append(m.MixedGlobalLocal(5))
153 a.append(m.MixedLocalGlobal(6))
154 a.append(cm.MixedGlobalLocal(7))
155 a.append(cm.MixedLocalGlobal(8))
156 a.append(m.get_mixed_gl(9))
157 a.append(m.get_mixed_lg(10))
158 a.append(cm.get_mixed_gl(11))
159 a.append(cm.get_mixed_lg(12))
161 assert [x.get()
for x
in a] == [
178 """Makes sure the internal local type map differs across the two modules"""
179 import pybind11_cross_module_tests
as cm
181 assert m.local_cpp_types_addr() != cm.local_cpp_types_addr()
184 @pytest.mark.xfail(
"env.PYPY and sys.pypy_version_info < (7, 3, 2)")
186 """One module uses a generic vector caster from `<pybind11/stl.h>` while the other
187 exports `std::vector<int>` via `py:bind_vector` and `py::module_local`"""
188 import pybind11_cross_module_tests
as cm
190 v1 = cm.VectorInt([1, 2, 3])
191 assert m.load_vector_via_caster(v1) == 6
192 assert cm.load_vector_via_binding(v1) == 6
195 assert m.load_vector_via_caster(v2) == 6
196 with pytest.raises(TypeError)
as excinfo:
197 cm.load_vector_via_binding(v2)
201 load_vector_via_binding(): incompatible function arguments. The following argument types are supported:
202 1. (arg0: pybind11_cross_module_tests.VectorInt) -> int
204 Invoked with: [1, 2, 3]
210 import pybind11_cross_module_tests
as cm
213 v1.append(m.LocalType(1))
215 v2.append(cm.LocalType(2))
219 assert m.return_self(v1)
is v1
220 assert cm.return_self(v2)
is v2
221 assert m.return_self(v2)
is v2
222 assert cm.return_self(v1)
is v1
224 assert m.LocalVec
is not cm.LocalVec
227 assert type(m.return_copy(v1))
is m.LocalVec
228 assert type(m.return_copy(v2))
is m.LocalVec
229 assert type(cm.return_copy(v1))
is cm.LocalVec
230 assert type(cm.return_copy(v2))
is cm.LocalVec
233 mycat = m.Cat(
"Fluffy")
234 mydog = cm.Dog(
"Rover")
235 assert mycat.get_name() ==
"Fluffy"
236 assert mydog.name() ==
"Rover"
237 assert m.Cat.__base__.__name__ ==
"Pet"
238 assert cm.Dog.__base__.__name__ ==
"Pet"
239 assert m.Cat.__base__
is not cm.Dog.__base__
240 assert m.pet_name(mycat) ==
"Fluffy"
241 assert m.pet_name(mydog) ==
"Rover"
242 assert cm.pet_name(mycat) ==
"Fluffy"
243 assert cm.pet_name(mydog) ==
"Rover"
245 assert m.MixGL
is not cm.MixGL
248 assert m.get_gl_value(a) == 11
249 assert m.get_gl_value(b) == 12
250 assert cm.get_gl_value(a) == 101
251 assert cm.get_gl_value(b) == 102
253 c, d = m.MixGL2(3), cm.MixGL2(4)
254 with pytest.raises(TypeError)
as excinfo:
256 assert "incompatible function arguments" in str(excinfo.value)
257 with pytest.raises(TypeError)
as excinfo:
259 assert "incompatible function arguments" in str(excinfo.value)