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