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()
58 assert 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()
66 assert str(excinfo.value) == (
67 'generic_type: type "LocalExternal" is already registered!' 68 if hasattr(pybind11_tests,
'class_')
else 'test_class not enabled')
72 import pybind11_cross_module_tests
as cm
74 v1, v2 = m.LocalVec(), cm.LocalVec()
75 v1.append(m.LocalType(1))
76 v1.append(m.LocalType(2))
77 v2.append(cm.LocalType(1))
78 v2.append(cm.LocalType(2))
81 v1.append(cm.LocalType(3))
82 v2.append(m.LocalType(3))
84 assert [i.get()
for i
in v1] == [0, 1, 2]
85 assert [i.get()
for i
in v2] == [2, 3, 4]
87 v3, v4 = m.NonLocalVec(), cm.NonLocalVec2()
88 v3.append(m.NonLocalType(1))
89 v3.append(m.NonLocalType(2))
90 v4.append(m.NonLocal2(3))
91 v4.append(m.NonLocal2(4))
93 assert [i.get()
for i
in v3] == [1, 2]
94 assert [i.get()
for i
in v4] == [13, 14]
96 d1, d2 = m.LocalMap(), cm.LocalMap()
101 assert {i: d1[i].
get()
for i
in d1} == {
'a': 0,
'b': 1}
102 assert {i: d2[i].
get()
for i
in d2} == {
'c': 2,
'd': 3}
106 import pybind11_cross_module_tests
as cm
108 with pytest.raises(RuntimeError)
as excinfo:
109 cm.register_nonlocal_map()
110 assert str(excinfo.value) ==
'generic_type: type "NonLocalMap" is already registered!' 112 with pytest.raises(RuntimeError)
as excinfo:
113 cm.register_nonlocal_vec()
114 assert str(excinfo.value) ==
'generic_type: type "NonLocalVec" is already registered!' 116 with pytest.raises(RuntimeError)
as excinfo:
117 cm.register_nonlocal_map2()
118 assert str(excinfo.value) ==
'generic_type: type "NonLocalMap2" is already registered!' 122 """Local types take precedence over globally registered types: a module with a `module_local` 123 type can be registered even if the type is already registered globally. With the module, 124 casting will go to the local type; outside the module casting goes to the global type.""" 125 import pybind11_cross_module_tests
as cm
126 m.register_mixed_global()
127 m.register_mixed_local()
130 a.append(m.MixedGlobalLocal(1))
131 a.append(m.MixedLocalGlobal(2))
132 a.append(m.get_mixed_gl(3))
133 a.append(m.get_mixed_lg(4))
135 assert [x.get()
for x
in a] == [101, 1002, 103, 1004]
137 cm.register_mixed_global_local()
138 cm.register_mixed_local_global()
139 a.append(m.MixedGlobalLocal(5))
140 a.append(m.MixedLocalGlobal(6))
141 a.append(cm.MixedGlobalLocal(7))
142 a.append(cm.MixedLocalGlobal(8))
143 a.append(m.get_mixed_gl(9))
144 a.append(m.get_mixed_lg(10))
145 a.append(cm.get_mixed_gl(11))
146 a.append(cm.get_mixed_lg(12))
148 assert [x.get()
for x
in a] == \
149 [101, 1002, 103, 1004, 105, 1006, 207, 2008, 109, 1010, 211, 2012]
153 """Makes sure the internal local type map differs across the two modules""" 154 import pybind11_cross_module_tests
as cm
155 assert m.local_cpp_types_addr() != cm.local_cpp_types_addr()
158 @pytest.mark.xfail(
"env.PYPY")
160 """One module uses a generic vector caster from `<pybind11/stl.h>` while the other 161 exports `std::vector<int>` via `py:bind_vector` and `py::module_local`""" 162 import pybind11_cross_module_tests
as cm
164 v1 = cm.VectorInt([1, 2, 3])
165 assert m.load_vector_via_caster(v1) == 6
166 assert cm.load_vector_via_binding(v1) == 6
169 assert m.load_vector_via_caster(v2) == 6
170 with pytest.raises(TypeError)
as excinfo:
171 cm.load_vector_via_binding(v2) == 6
172 assert msg(excinfo.value) ==
""" 173 load_vector_via_binding(): incompatible function arguments. The following argument types are supported: 174 1. (arg0: pybind11_cross_module_tests.VectorInt) -> int 176 Invoked with: [1, 2, 3] 181 import pybind11_cross_module_tests
as cm
184 v1.append(m.LocalType(1))
186 v2.append(cm.LocalType(2))
190 assert m.return_self(v1)
is v1
191 assert cm.return_self(v2)
is v2
192 assert m.return_self(v2)
is v2
193 assert cm.return_self(v1)
is v1
195 assert m.LocalVec
is not cm.LocalVec
198 assert type(m.return_copy(v1))
is m.LocalVec
199 assert type(m.return_copy(v2))
is m.LocalVec
200 assert type(cm.return_copy(v1))
is cm.LocalVec
201 assert type(cm.return_copy(v2))
is cm.LocalVec
204 mycat = m.Cat(
"Fluffy")
205 mydog = cm.Dog(
"Rover")
206 assert mycat.get_name() ==
"Fluffy" 207 assert mydog.name() ==
"Rover" 208 assert m.Cat.__base__.__name__ ==
"Pet" 209 assert cm.Dog.__base__.__name__ ==
"Pet" 210 assert m.Cat.__base__
is not cm.Dog.__base__
211 assert m.pet_name(mycat) ==
"Fluffy" 212 assert m.pet_name(mydog) ==
"Rover" 213 assert cm.pet_name(mycat) ==
"Fluffy" 214 assert cm.pet_name(mydog) ==
"Rover" 216 assert m.MixGL
is not cm.MixGL
219 assert m.get_gl_value(a) == 11
220 assert m.get_gl_value(b) == 12
221 assert cm.get_gl_value(a) == 101
222 assert cm.get_gl_value(b) == 102
224 c, d = m.MixGL2(3), cm.MixGL2(4)
225 with pytest.raises(TypeError)
as excinfo:
227 assert "incompatible function arguments" in str(excinfo.value)
228 with pytest.raises(TypeError)
as excinfo:
230 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()