Henry Schreiner | d8c7ee0 | 2020-07-20 13:35:21 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 2 | import pytest |
| 3 | |
Henry Schreiner | 4d9024e | 2020-08-16 16:02:12 -0400 | [diff] [blame] | 4 | import env # noqa: F401 |
| 5 | |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 6 | from pybind11_tests import local_bindings as m |
| 7 | |
| 8 | |
Dean Moldovan | 7b1de1e | 2017-09-03 01:31:47 +0200 | [diff] [blame] | 9 | def test_load_external(): |
| 10 | """Load a `py::module_local` type that's only registered in an external module""" |
| 11 | import pybind11_cross_module_tests as cm |
| 12 | |
| 13 | assert m.load_external1(cm.ExternalType1(11)) == 11 |
| 14 | assert m.load_external2(cm.ExternalType2(22)) == 22 |
| 15 | |
| 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) |
| 19 | |
| 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) |
| 23 | |
| 24 | |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 25 | def test_local_bindings(): |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame] | 26 | """Tests that duplicate `py::module_local` class bindings work across modules""" |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 27 | |
| 28 | # Make sure we can load the second module with the conflicting (but local) definition: |
| 29 | import pybind11_cross_module_tests as cm |
| 30 | |
| 31 | i1 = m.LocalType(5) |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 32 | assert i1.get() == 4 |
| 33 | assert i1.get3() == 8 |
| 34 | |
| 35 | i2 = cm.LocalType(10) |
| 36 | assert i2.get() == 11 |
| 37 | assert i2.get2() == 12 |
| 38 | |
| 39 | assert not hasattr(i1, 'get2') |
| 40 | assert not hasattr(i2, 'get3') |
| 41 | |
Jason Rhinelander | 5e14aa6 | 2017-08-17 11:38:05 -0400 | [diff] [blame] | 42 | # Loading within the local module |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 43 | assert m.local_value(i1) == 5 |
| 44 | assert cm.local_value(i2) == 10 |
| 45 | |
Jason Rhinelander | 5e14aa6 | 2017-08-17 11:38:05 -0400 | [diff] [blame] | 46 | # Cross-module loading works as well (on failure, the type loader looks for |
| 47 | # external module-local converters): |
| 48 | assert m.local_value(i2) == 10 |
| 49 | assert cm.local_value(i1) == 5 |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def test_nonlocal_failure(): |
| 53 | """Tests that attempting to register a non-local type in multiple modules fails""" |
| 54 | import pybind11_cross_module_tests as cm |
| 55 | |
| 56 | with pytest.raises(RuntimeError) as excinfo: |
| 57 | cm.register_nonlocal() |
| 58 | assert str(excinfo.value) == 'generic_type: type "NonLocalType" is already registered!' |
| 59 | |
| 60 | |
| 61 | def test_duplicate_local(): |
| 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() |
| 65 | import pybind11_tests |
| 66 | assert str(excinfo.value) == ( |
| 67 | 'generic_type: type "LocalExternal" is already registered!' |
| 68 | if hasattr(pybind11_tests, 'class_') else 'test_class not enabled') |
| 69 | |
| 70 | |
| 71 | def test_stl_bind_local(): |
| 72 | import pybind11_cross_module_tests as cm |
| 73 | |
| 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)) |
| 79 | |
Jason Rhinelander | 5e14aa6 | 2017-08-17 11:38:05 -0400 | [diff] [blame] | 80 | # Cross module value loading: |
| 81 | v1.append(cm.LocalType(3)) |
| 82 | v2.append(m.LocalType(3)) |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 83 | |
Jason Rhinelander | 5e14aa6 | 2017-08-17 11:38:05 -0400 | [diff] [blame] | 84 | assert [i.get() for i in v1] == [0, 1, 2] |
| 85 | assert [i.get() for i in v2] == [2, 3, 4] |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 86 | |
| 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)) |
| 92 | |
| 93 | assert [i.get() for i in v3] == [1, 2] |
| 94 | assert [i.get() for i in v4] == [13, 14] |
| 95 | |
| 96 | d1, d2 = m.LocalMap(), cm.LocalMap() |
| 97 | d1["a"] = v1[0] |
| 98 | d1["b"] = v1[1] |
| 99 | d2["c"] = v2[0] |
| 100 | d2["d"] = v2[1] |
| 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} |
| 103 | |
| 104 | |
| 105 | def test_stl_bind_global(): |
| 106 | import pybind11_cross_module_tests as cm |
| 107 | |
| 108 | with pytest.raises(RuntimeError) as excinfo: |
| 109 | cm.register_nonlocal_map() |
| 110 | assert str(excinfo.value) == 'generic_type: type "NonLocalMap" is already registered!' |
| 111 | |
| 112 | with pytest.raises(RuntimeError) as excinfo: |
| 113 | cm.register_nonlocal_vec() |
| 114 | assert str(excinfo.value) == 'generic_type: type "NonLocalVec" is already registered!' |
| 115 | |
| 116 | with pytest.raises(RuntimeError) as excinfo: |
| 117 | cm.register_nonlocal_map2() |
| 118 | assert str(excinfo.value) == 'generic_type: type "NonLocalMap2" is already registered!' |
| 119 | |
| 120 | |
Jason Rhinelander | 4b15923 | 2017-08-04 13:05:12 -0400 | [diff] [blame] | 121 | def test_mixed_local_global(): |
| 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() |
| 128 | |
| 129 | a = [] |
| 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)) |
| 134 | |
| 135 | assert [x.get() for x in a] == [101, 1002, 103, 1004] |
| 136 | |
| 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)) |
| 147 | |
| 148 | assert [x.get() for x in a] == \ |
| 149 | [101, 1002, 103, 1004, 105, 1006, 207, 2008, 109, 1010, 211, 2012] |
| 150 | |
| 151 | |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 152 | def test_internal_locals_differ(): |
| 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() |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame] | 156 | |
| 157 | |
Henry Schreiner | 4d9024e | 2020-08-16 16:02:12 -0400 | [diff] [blame] | 158 | @pytest.mark.xfail("env.PYPY") |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame] | 159 | def test_stl_caster_vs_stl_bind(msg): |
| 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 |
| 163 | |
| 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 |
| 167 | |
| 168 | v2 = [1, 2, 3] |
| 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 |
| 175 | |
| 176 | Invoked with: [1, 2, 3] |
| 177 | """ # noqa: E501 line too long |
Jason Rhinelander | 5e14aa6 | 2017-08-17 11:38:05 -0400 | [diff] [blame] | 178 | |
| 179 | |
| 180 | def test_cross_module_calls(): |
| 181 | import pybind11_cross_module_tests as cm |
| 182 | |
| 183 | v1 = m.LocalVec() |
| 184 | v1.append(m.LocalType(1)) |
| 185 | v2 = cm.LocalVec() |
| 186 | v2.append(cm.LocalType(2)) |
| 187 | |
| 188 | # Returning the self pointer should get picked up as returning an existing |
| 189 | # instance (even when that instance is of a foreign, non-local type). |
| 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 |
| 194 | |
| 195 | assert m.LocalVec is not cm.LocalVec |
| 196 | # Returning a copy, on the other hand, always goes to the local type, |
| 197 | # regardless of where the source type came from. |
| 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 |
| 202 | |
| 203 | # Test the example given in the documentation (which also tests inheritance casting): |
| 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" |
| 215 | |
| 216 | assert m.MixGL is not cm.MixGL |
| 217 | a = m.MixGL(1) |
| 218 | b = cm.MixGL(2) |
| 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 |
| 223 | |
| 224 | c, d = m.MixGL2(3), cm.MixGL2(4) |
| 225 | with pytest.raises(TypeError) as excinfo: |
| 226 | m.get_gl_value(c) |
Wenzel Jakob | 9fd4712 | 2019-07-06 16:57:17 +0200 | [diff] [blame] | 227 | assert "incompatible function arguments" in str(excinfo.value) |
Jason Rhinelander | 5e14aa6 | 2017-08-17 11:38:05 -0400 | [diff] [blame] | 228 | with pytest.raises(TypeError) as excinfo: |
| 229 | m.get_gl_value(d) |
Wenzel Jakob | 9fd4712 | 2019-07-06 16:57:17 +0200 | [diff] [blame] | 230 | assert "incompatible function arguments" in str(excinfo.value) |