blob: e9a63ca0f627b49f47089752ab13a3c5b215d77d [file] [log] [blame]
Jason Rhinelander7437c692017-07-28 22:03:44 -04001import pytest
2
3from pybind11_tests import local_bindings as m
4
5
6def test_local_bindings():
Dean Moldovan8d3cedb2017-08-13 03:03:06 +02007 """Tests that duplicate `py::module_local` class bindings work across modules"""
Jason Rhinelander7437c692017-07-28 22:03:44 -04008
9 # Make sure we can load the second module with the conflicting (but local) definition:
10 import pybind11_cross_module_tests as cm
11
12 i1 = m.LocalType(5)
Jason Rhinelander7437c692017-07-28 22:03:44 -040013 assert i1.get() == 4
14 assert i1.get3() == 8
15
16 i2 = cm.LocalType(10)
17 assert i2.get() == 11
18 assert i2.get2() == 12
19
20 assert not hasattr(i1, 'get2')
21 assert not hasattr(i2, 'get3')
22
Jason Rhinelander5e14aa62017-08-17 11:38:05 -040023 # Loading within the local module
Jason Rhinelander7437c692017-07-28 22:03:44 -040024 assert m.local_value(i1) == 5
25 assert cm.local_value(i2) == 10
26
Jason Rhinelander5e14aa62017-08-17 11:38:05 -040027 # Cross-module loading works as well (on failure, the type loader looks for
28 # external module-local converters):
29 assert m.local_value(i2) == 10
30 assert cm.local_value(i1) == 5
Jason Rhinelander7437c692017-07-28 22:03:44 -040031
32
33def test_nonlocal_failure():
34 """Tests that attempting to register a non-local type in multiple modules fails"""
35 import pybind11_cross_module_tests as cm
36
37 with pytest.raises(RuntimeError) as excinfo:
38 cm.register_nonlocal()
39 assert str(excinfo.value) == 'generic_type: type "NonLocalType" is already registered!'
40
41
42def test_duplicate_local():
43 """Tests expected failure when registering a class twice with py::local in the same module"""
44 with pytest.raises(RuntimeError) as excinfo:
45 m.register_local_external()
46 import pybind11_tests
47 assert str(excinfo.value) == (
48 'generic_type: type "LocalExternal" is already registered!'
49 if hasattr(pybind11_tests, 'class_') else 'test_class not enabled')
50
51
52def test_stl_bind_local():
53 import pybind11_cross_module_tests as cm
54
55 v1, v2 = m.LocalVec(), cm.LocalVec()
56 v1.append(m.LocalType(1))
57 v1.append(m.LocalType(2))
58 v2.append(cm.LocalType(1))
59 v2.append(cm.LocalType(2))
60
Jason Rhinelander5e14aa62017-08-17 11:38:05 -040061 # Cross module value loading:
62 v1.append(cm.LocalType(3))
63 v2.append(m.LocalType(3))
Jason Rhinelander7437c692017-07-28 22:03:44 -040064
Jason Rhinelander5e14aa62017-08-17 11:38:05 -040065 assert [i.get() for i in v1] == [0, 1, 2]
66 assert [i.get() for i in v2] == [2, 3, 4]
Jason Rhinelander7437c692017-07-28 22:03:44 -040067
68 v3, v4 = m.NonLocalVec(), cm.NonLocalVec2()
69 v3.append(m.NonLocalType(1))
70 v3.append(m.NonLocalType(2))
71 v4.append(m.NonLocal2(3))
72 v4.append(m.NonLocal2(4))
73
74 assert [i.get() for i in v3] == [1, 2]
75 assert [i.get() for i in v4] == [13, 14]
76
77 d1, d2 = m.LocalMap(), cm.LocalMap()
78 d1["a"] = v1[0]
79 d1["b"] = v1[1]
80 d2["c"] = v2[0]
81 d2["d"] = v2[1]
82 assert {i: d1[i].get() for i in d1} == {'a': 0, 'b': 1}
83 assert {i: d2[i].get() for i in d2} == {'c': 2, 'd': 3}
84
85
86def test_stl_bind_global():
87 import pybind11_cross_module_tests as cm
88
89 with pytest.raises(RuntimeError) as excinfo:
90 cm.register_nonlocal_map()
91 assert str(excinfo.value) == 'generic_type: type "NonLocalMap" is already registered!'
92
93 with pytest.raises(RuntimeError) as excinfo:
94 cm.register_nonlocal_vec()
95 assert str(excinfo.value) == 'generic_type: type "NonLocalVec" is already registered!'
96
97 with pytest.raises(RuntimeError) as excinfo:
98 cm.register_nonlocal_map2()
99 assert str(excinfo.value) == 'generic_type: type "NonLocalMap2" is already registered!'
100
101
Jason Rhinelander4b159232017-08-04 13:05:12 -0400102def test_mixed_local_global():
103 """Local types take precedence over globally registered types: a module with a `module_local`
104 type can be registered even if the type is already registered globally. With the module,
105 casting will go to the local type; outside the module casting goes to the global type."""
106 import pybind11_cross_module_tests as cm
107 m.register_mixed_global()
108 m.register_mixed_local()
109
110 a = []
111 a.append(m.MixedGlobalLocal(1))
112 a.append(m.MixedLocalGlobal(2))
113 a.append(m.get_mixed_gl(3))
114 a.append(m.get_mixed_lg(4))
115
116 assert [x.get() for x in a] == [101, 1002, 103, 1004]
117
118 cm.register_mixed_global_local()
119 cm.register_mixed_local_global()
120 a.append(m.MixedGlobalLocal(5))
121 a.append(m.MixedLocalGlobal(6))
122 a.append(cm.MixedGlobalLocal(7))
123 a.append(cm.MixedLocalGlobal(8))
124 a.append(m.get_mixed_gl(9))
125 a.append(m.get_mixed_lg(10))
126 a.append(cm.get_mixed_gl(11))
127 a.append(cm.get_mixed_lg(12))
128
129 assert [x.get() for x in a] == \
130 [101, 1002, 103, 1004, 105, 1006, 207, 2008, 109, 1010, 211, 2012]
131
132
Jason Rhinelander7437c692017-07-28 22:03:44 -0400133def test_internal_locals_differ():
134 """Makes sure the internal local type map differs across the two modules"""
135 import pybind11_cross_module_tests as cm
136 assert m.local_cpp_types_addr() != cm.local_cpp_types_addr()
Dean Moldovan8d3cedb2017-08-13 03:03:06 +0200137
138
139def test_stl_caster_vs_stl_bind(msg):
140 """One module uses a generic vector caster from `<pybind11/stl.h>` while the other
141 exports `std::vector<int>` via `py:bind_vector` and `py::module_local`"""
142 import pybind11_cross_module_tests as cm
143
144 v1 = cm.VectorInt([1, 2, 3])
145 assert m.load_vector_via_caster(v1) == 6
146 assert cm.load_vector_via_binding(v1) == 6
147
148 v2 = [1, 2, 3]
149 assert m.load_vector_via_caster(v2) == 6
150 with pytest.raises(TypeError) as excinfo:
151 cm.load_vector_via_binding(v2) == 6
152 assert msg(excinfo.value) == """
153 load_vector_via_binding(): incompatible function arguments. The following argument types are supported:
154 1. (arg0: pybind11_cross_module_tests.VectorInt) -> int
155
156 Invoked with: [1, 2, 3]
157 """ # noqa: E501 line too long
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400158
159
160def test_cross_module_calls():
161 import pybind11_cross_module_tests as cm
162
163 v1 = m.LocalVec()
164 v1.append(m.LocalType(1))
165 v2 = cm.LocalVec()
166 v2.append(cm.LocalType(2))
167
168 # Returning the self pointer should get picked up as returning an existing
169 # instance (even when that instance is of a foreign, non-local type).
170 assert m.return_self(v1) is v1
171 assert cm.return_self(v2) is v2
172 assert m.return_self(v2) is v2
173 assert cm.return_self(v1) is v1
174
175 assert m.LocalVec is not cm.LocalVec
176 # Returning a copy, on the other hand, always goes to the local type,
177 # regardless of where the source type came from.
178 assert type(m.return_copy(v1)) is m.LocalVec
179 assert type(m.return_copy(v2)) is m.LocalVec
180 assert type(cm.return_copy(v1)) is cm.LocalVec
181 assert type(cm.return_copy(v2)) is cm.LocalVec
182
183 # Test the example given in the documentation (which also tests inheritance casting):
184 mycat = m.Cat("Fluffy")
185 mydog = cm.Dog("Rover")
186 assert mycat.get_name() == "Fluffy"
187 assert mydog.name() == "Rover"
188 assert m.Cat.__base__.__name__ == "Pet"
189 assert cm.Dog.__base__.__name__ == "Pet"
190 assert m.Cat.__base__ is not cm.Dog.__base__
191 assert m.pet_name(mycat) == "Fluffy"
192 assert m.pet_name(mydog) == "Rover"
193 assert cm.pet_name(mycat) == "Fluffy"
194 assert cm.pet_name(mydog) == "Rover"
195
196 assert m.MixGL is not cm.MixGL
197 a = m.MixGL(1)
198 b = cm.MixGL(2)
199 assert m.get_gl_value(a) == 11
200 assert m.get_gl_value(b) == 12
201 assert cm.get_gl_value(a) == 101
202 assert cm.get_gl_value(b) == 102
203
204 c, d = m.MixGL2(3), cm.MixGL2(4)
205 with pytest.raises(TypeError) as excinfo:
206 m.get_gl_value(c)
207 assert "incompatible function arguments" in str(excinfo)
208 with pytest.raises(TypeError) as excinfo:
209 m.get_gl_value(d)
210 assert "incompatible function arguments" in str(excinfo)