3 from pybind11_tests
import local_bindings
as m
6 def test_load_external():
7 """Load a `py::module_local` type that's only registered in an external module"""
8 import pybind11_cross_module_tests
as cm
10 assert m
.load_external1(cm
.ExternalType1(11)) == 11
11 assert m
.load_external2(cm
.ExternalType2(22)) == 22
13 with pytest
.raises(TypeError) as excinfo
:
14 assert m
.load_external2(cm
.ExternalType1(21)) == 21
15 assert "incompatible function arguments" in str(excinfo
.value
)
17 with pytest
.raises(TypeError) as excinfo
:
18 assert m
.load_external1(cm
.ExternalType2(12)) == 12
19 assert "incompatible function arguments" in str(excinfo
.value
)
22 def test_local_bindings():
23 """Tests that duplicate `py::module_local` class bindings work across modules"""
25 # Make sure we can load the second module with the conflicting (but local) definition:
26 import pybind11_cross_module_tests
as cm
34 assert i2
.get2() == 12
36 assert not hasattr(i1
, 'get2')
37 assert not hasattr(i2
, 'get3')
39 # Loading within the local module
40 assert m
.local_value(i1
) == 5
41 assert cm
.local_value(i2
) == 10
43 # Cross-module loading works as well (on failure, the type loader looks for
44 # external module-local converters):
45 assert m
.local_value(i2
) == 10
46 assert cm
.local_value(i1
) == 5
49 def test_nonlocal_failure():
50 """Tests that attempting to register a non-local type in multiple modules fails"""
51 import pybind11_cross_module_tests
as cm
53 with pytest
.raises(RuntimeError) as excinfo
:
54 cm
.register_nonlocal()
55 assert str(excinfo
.value
) == 'generic_type: type "NonLocalType" is already registered!'
58 def test_duplicate_local():
59 """Tests expected failure when registering a class twice with py::local in the same module"""
60 with pytest
.raises(RuntimeError) as excinfo
:
61 m
.register_local_external()
63 assert str(excinfo
.value
) == (
64 'generic_type: type "LocalExternal" is already registered!'
65 if hasattr(pybind11_tests
, 'class_') else 'test_class not enabled')
68 def test_stl_bind_local():
69 import pybind11_cross_module_tests
as cm
71 v1
, v2
= m
.LocalVec(), cm
.LocalVec()
72 v1
.append(m
.LocalType(1))
73 v1
.append(m
.LocalType(2))
74 v2
.append(cm
.LocalType(1))
75 v2
.append(cm
.LocalType(2))
77 # Cross module value loading:
78 v1
.append(cm
.LocalType(3))
79 v2
.append(m
.LocalType(3))
81 assert [i
.get() for i
in v1
] == [0, 1, 2]
82 assert [i
.get() for i
in v2
] == [2, 3, 4]
84 v3
, v4
= m
.NonLocalVec(), cm
.NonLocalVec2()
85 v3
.append(m
.NonLocalType(1))
86 v3
.append(m
.NonLocalType(2))
87 v4
.append(m
.NonLocal2(3))
88 v4
.append(m
.NonLocal2(4))
90 assert [i
.get() for i
in v3
] == [1, 2]
91 assert [i
.get() for i
in v4
] == [13, 14]
93 d1
, d2
= m
.LocalMap(), cm
.LocalMap()
98 assert {i
: d1
[i
].get() for i
in d1
} == {'a': 0, 'b': 1}
99 assert {i
: d2
[i
].get() for i
in d2
} == {'c': 2, 'd': 3}
102 def test_stl_bind_global():
103 import pybind11_cross_module_tests
as cm
105 with pytest
.raises(RuntimeError) as excinfo
:
106 cm
.register_nonlocal_map()
107 assert str(excinfo
.value
) == 'generic_type: type "NonLocalMap" is already registered!'
109 with pytest
.raises(RuntimeError) as excinfo
:
110 cm
.register_nonlocal_vec()
111 assert str(excinfo
.value
) == 'generic_type: type "NonLocalVec" is already registered!'
113 with pytest
.raises(RuntimeError) as excinfo
:
114 cm
.register_nonlocal_map2()
115 assert str(excinfo
.value
) == 'generic_type: type "NonLocalMap2" is already registered!'
118 def test_mixed_local_global():
119 """Local types take precedence over globally registered types: a module with a `module_local`
120 type can be registered even if the type is already registered globally. With the module,
121 casting will go to the local type; outside the module casting goes to the global type."""
122 import pybind11_cross_module_tests
as cm
123 m
.register_mixed_global()
124 m
.register_mixed_local()
127 a
.append(m
.MixedGlobalLocal(1))
128 a
.append(m
.MixedLocalGlobal(2))
129 a
.append(m
.get_mixed_gl(3))
130 a
.append(m
.get_mixed_lg(4))
132 assert [x
.get() for x
in a
] == [101, 1002, 103, 1004]
134 cm
.register_mixed_global_local()
135 cm
.register_mixed_local_global()
136 a
.append(m
.MixedGlobalLocal(5))
137 a
.append(m
.MixedLocalGlobal(6))
138 a
.append(cm
.MixedGlobalLocal(7))
139 a
.append(cm
.MixedLocalGlobal(8))
140 a
.append(m
.get_mixed_gl(9))
141 a
.append(m
.get_mixed_lg(10))
142 a
.append(cm
.get_mixed_gl(11))
143 a
.append(cm
.get_mixed_lg(12))
145 assert [x
.get() for x
in a
] == \
146 [101, 1002, 103, 1004, 105, 1006, 207, 2008, 109, 1010, 211, 2012]
149 def test_internal_locals_differ():
150 """Makes sure the internal local type map differs across the two modules"""
151 import pybind11_cross_module_tests
as cm
152 assert m
.local_cpp_types_addr() != cm
.local_cpp_types_addr()
155 def test_stl_caster_vs_stl_bind(msg
):
156 """One module uses a generic vector caster from `<pybind11/stl.h>` while the other
157 exports `std::vector<int>` via `py:bind_vector` and `py::module_local`"""
158 import pybind11_cross_module_tests
as cm
160 v1
= cm
.VectorInt([1, 2, 3])
161 assert m
.load_vector_via_caster(v1
) == 6
162 assert cm
.load_vector_via_binding(v1
) == 6
165 assert m
.load_vector_via_caster(v2
) == 6
166 with pytest
.raises(TypeError) as excinfo
:
167 cm
.load_vector_via_binding(v2
) == 6
168 assert msg(excinfo
.value
) == """
169 load_vector_via_binding(): incompatible function arguments. The following argument types are supported:
170 1. (arg0: pybind11_cross_module_tests.VectorInt) -> int
172 Invoked with: [1, 2, 3]
173 """ # noqa: E501 line too long
176 def test_cross_module_calls():
177 import pybind11_cross_module_tests
as cm
180 v1
.append(m
.LocalType(1))
182 v2
.append(cm
.LocalType(2))
184 # Returning the self pointer should get picked up as returning an existing
185 # instance (even when that instance is of a foreign, non-local type).
186 assert m
.return_self(v1
) is v1
187 assert cm
.return_self(v2
) is v2
188 assert m
.return_self(v2
) is v2
189 assert cm
.return_self(v1
) is v1
191 assert m
.LocalVec
is not cm
.LocalVec
192 # Returning a copy, on the other hand, always goes to the local type,
193 # regardless of where the source type came from.
194 assert type(m
.return_copy(v1
)) is m
.LocalVec
195 assert type(m
.return_copy(v2
)) is m
.LocalVec
196 assert type(cm
.return_copy(v1
)) is cm
.LocalVec
197 assert type(cm
.return_copy(v2
)) is cm
.LocalVec
199 # Test the example given in the documentation (which also tests inheritance casting):
200 mycat
= m
.Cat("Fluffy")
201 mydog
= cm
.Dog("Rover")
202 assert mycat
.get_name() == "Fluffy"
203 assert mydog
.name() == "Rover"
204 assert m
.Cat
.__base
__.__name
__ == "Pet"
205 assert cm
.Dog
.__base
__.__name
__ == "Pet"
206 assert m
.Cat
.__base
__ is not cm
.Dog
.__base
__
207 assert m
.pet_name(mycat
) == "Fluffy"
208 assert m
.pet_name(mydog
) == "Rover"
209 assert cm
.pet_name(mycat
) == "Fluffy"
210 assert cm
.pet_name(mydog
) == "Rover"
212 assert m
.MixGL
is not cm
.MixGL
215 assert m
.get_gl_value(a
) == 11
216 assert m
.get_gl_value(b
) == 12
217 assert cm
.get_gl_value(a
) == 101
218 assert cm
.get_gl_value(b
) == 102
220 c
, d
= m
.MixGL2(3), cm
.MixGL2(4)
221 with pytest
.raises(TypeError) as excinfo
:
223 assert "incompatible function arguments" in str(excinfo
.value
)
224 with pytest
.raises(TypeError) as excinfo
:
226 assert "incompatible function arguments" in str(excinfo
.value
)