4 from pybind11_tests
import local_bindings
as m
8 """Load a `py::module_local` type that's only registered in an external module""" 9 import pybind11_cross_module_tests
as cm
11 assert m.load_external1(cm.ExternalType1(11)) == 11
12 assert m.load_external2(cm.ExternalType2(22)) == 22
14 with pytest.raises(TypeError)
as excinfo:
15 assert m.load_external2(cm.ExternalType1(21)) == 21
16 assert "incompatible function arguments" in str(excinfo.value)
18 with pytest.raises(TypeError)
as excinfo:
19 assert m.load_external1(cm.ExternalType2(12)) == 12
20 assert "incompatible function arguments" in str(excinfo.value)
24 """Tests that duplicate `py::module_local` class bindings work across modules""" 27 import pybind11_cross_module_tests
as cm
35 assert i2.get2() == 12
41 assert m.local_value(i1) == 5
42 assert cm.local_value(i2) == 10
46 assert m.local_value(i2) == 10
47 assert cm.local_value(i1) == 5
51 """Tests that attempting to register a non-local type in multiple modules fails""" 52 import pybind11_cross_module_tests
as cm
54 with pytest.raises(RuntimeError)
as excinfo:
55 cm.register_nonlocal()
57 str(excinfo.value) ==
'generic_type: type "NonLocalType" is already registered!' 62 """Tests expected failure when registering a class twice with py::local in the same module""" 63 with pytest.raises(RuntimeError)
as excinfo:
64 m.register_local_external()
67 assert str(excinfo.value) == (
68 'generic_type: type "LocalExternal" is already registered!' 69 if hasattr(pybind11_tests,
"class_")
70 else "test_class not enabled" 75 import pybind11_cross_module_tests
as cm
77 v1, v2 = m.LocalVec(), cm.LocalVec()
78 v1.append(m.LocalType(1))
79 v1.append(m.LocalType(2))
80 v2.append(cm.LocalType(1))
81 v2.append(cm.LocalType(2))
84 v1.append(cm.LocalType(3))
85 v2.append(m.LocalType(3))
87 assert [i.get()
for i
in v1] == [0, 1, 2]
88 assert [i.get()
for i
in v2] == [2, 3, 4]
90 v3, v4 = m.NonLocalVec(), cm.NonLocalVec2()
91 v3.append(m.NonLocalType(1))
92 v3.append(m.NonLocalType(2))
93 v4.append(m.NonLocal2(3))
94 v4.append(m.NonLocal2(4))
96 assert [i.get()
for i
in v3] == [1, 2]
97 assert [i.get()
for i
in v4] == [13, 14]
99 d1, d2 = m.LocalMap(), cm.LocalMap()
104 assert {i: d1[i].
get()
for i
in d1} == {
"a": 0,
"b": 1}
105 assert {i: d2[i].
get()
for i
in d2} == {
"c": 2,
"d": 3}
109 import pybind11_cross_module_tests
as cm
111 with pytest.raises(RuntimeError)
as excinfo:
112 cm.register_nonlocal_map()
114 str(excinfo.value) ==
'generic_type: type "NonLocalMap" is already registered!' 117 with pytest.raises(RuntimeError)
as excinfo:
118 cm.register_nonlocal_vec()
120 str(excinfo.value) ==
'generic_type: type "NonLocalVec" is already registered!' 123 with pytest.raises(RuntimeError)
as excinfo:
124 cm.register_nonlocal_map2()
126 str(excinfo.value) ==
'generic_type: type "NonLocalMap2" is already registered!' 131 """Local types take precedence over globally registered types: a module with a `module_local` 132 type can be registered even if the type is already registered globally. With the module, 133 casting will go to the local type; outside the module casting goes to the global type.""" 134 import pybind11_cross_module_tests
as cm
136 m.register_mixed_global()
137 m.register_mixed_local()
140 a.append(m.MixedGlobalLocal(1))
141 a.append(m.MixedLocalGlobal(2))
142 a.append(m.get_mixed_gl(3))
143 a.append(m.get_mixed_lg(4))
145 assert [x.get()
for x
in a] == [101, 1002, 103, 1004]
147 cm.register_mixed_global_local()
148 cm.register_mixed_local_global()
149 a.append(m.MixedGlobalLocal(5))
150 a.append(m.MixedLocalGlobal(6))
151 a.append(cm.MixedGlobalLocal(7))
152 a.append(cm.MixedLocalGlobal(8))
153 a.append(m.get_mixed_gl(9))
154 a.append(m.get_mixed_lg(10))
155 a.append(cm.get_mixed_gl(11))
156 a.append(cm.get_mixed_lg(12))
158 assert [x.get()
for x
in a] == [
175 """Makes sure the internal local type map differs across the two modules""" 176 import pybind11_cross_module_tests
as cm
178 assert m.local_cpp_types_addr() != cm.local_cpp_types_addr()
181 @pytest.mark.xfail(
"env.PYPY and sys.pypy_version_info < (7, 3, 2)")
183 """One module uses a generic vector caster from `<pybind11/stl.h>` while the other 184 exports `std::vector<int>` via `py:bind_vector` and `py::module_local`""" 185 import pybind11_cross_module_tests
as cm
187 v1 = cm.VectorInt([1, 2, 3])
188 assert m.load_vector_via_caster(v1) == 6
189 assert cm.load_vector_via_binding(v1) == 6
192 assert m.load_vector_via_caster(v2) == 6
193 with pytest.raises(TypeError)
as excinfo:
194 cm.load_vector_via_binding(v2)
198 load_vector_via_binding(): incompatible function arguments. The following argument types are supported: 199 1. (arg0: pybind11_cross_module_tests.VectorInt) -> int 201 Invoked with: [1, 2, 3] 207 import pybind11_cross_module_tests
as cm
210 v1.append(m.LocalType(1))
212 v2.append(cm.LocalType(2))
216 assert m.return_self(v1)
is v1
217 assert cm.return_self(v2)
is v2
218 assert m.return_self(v2)
is v2
219 assert cm.return_self(v1)
is v1
221 assert m.LocalVec
is not cm.LocalVec
224 assert type(m.return_copy(v1))
is m.LocalVec
225 assert type(m.return_copy(v2))
is m.LocalVec
226 assert type(cm.return_copy(v1))
is cm.LocalVec
227 assert type(cm.return_copy(v2))
is cm.LocalVec
230 mycat = m.Cat(
"Fluffy")
231 mydog = cm.Dog(
"Rover")
232 assert mycat.get_name() ==
"Fluffy" 233 assert mydog.name() ==
"Rover" 234 assert m.Cat.__base__.__name__ ==
"Pet" 235 assert cm.Dog.__base__.__name__ ==
"Pet" 236 assert m.Cat.__base__
is not cm.Dog.__base__
237 assert m.pet_name(mycat) ==
"Fluffy" 238 assert m.pet_name(mydog) ==
"Rover" 239 assert cm.pet_name(mycat) ==
"Fluffy" 240 assert cm.pet_name(mydog) ==
"Rover" 242 assert m.MixGL
is not cm.MixGL
245 assert m.get_gl_value(a) == 11
246 assert m.get_gl_value(b) == 12
247 assert cm.get_gl_value(a) == 101
248 assert cm.get_gl_value(b) == 102
250 c, d = m.MixGL2(3), cm.MixGL2(4)
251 with pytest.raises(TypeError)
as excinfo:
253 assert "incompatible function arguments" in str(excinfo.value)
254 with pytest.raises(TypeError)
as excinfo:
256 assert "incompatible function arguments" in str(excinfo.value)
def test_stl_bind_local()
bool hasattr(handle obj, handle name)
def test_mixed_local_global()
def test_internal_locals_differ()
def test_stl_caster_vs_stl_bind(msg)
def test_duplicate_local()
def test_local_bindings()
def test_nonlocal_failure()
def test_stl_bind_global()
Container::iterator get(Container &c, Position position)
def test_cross_module_calls()