Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 1 | import pytest |
| 2 | |
| 3 | from pybind11_tests import local_bindings as m |
| 4 | |
| 5 | |
| 6 | def test_local_bindings(): |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame^] | 7 | """Tests that duplicate `py::module_local` class bindings work across modules""" |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 8 | |
| 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 Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 13 | 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 | |
| 23 | assert m.local_value(i1) == 5 |
| 24 | assert cm.local_value(i2) == 10 |
| 25 | |
| 26 | with pytest.raises(TypeError) as excinfo: |
| 27 | m.local_value(i2) |
| 28 | assert "incompatible function arguments" in str(excinfo.value) |
| 29 | |
| 30 | with pytest.raises(TypeError) as excinfo: |
| 31 | cm.local_value(i1) |
| 32 | assert "incompatible function arguments" in str(excinfo.value) |
| 33 | |
| 34 | |
| 35 | def test_nonlocal_failure(): |
| 36 | """Tests that attempting to register a non-local type in multiple modules fails""" |
| 37 | import pybind11_cross_module_tests as cm |
| 38 | |
| 39 | with pytest.raises(RuntimeError) as excinfo: |
| 40 | cm.register_nonlocal() |
| 41 | assert str(excinfo.value) == 'generic_type: type "NonLocalType" is already registered!' |
| 42 | |
| 43 | |
| 44 | def test_duplicate_local(): |
| 45 | """Tests expected failure when registering a class twice with py::local in the same module""" |
| 46 | with pytest.raises(RuntimeError) as excinfo: |
| 47 | m.register_local_external() |
| 48 | import pybind11_tests |
| 49 | assert str(excinfo.value) == ( |
| 50 | 'generic_type: type "LocalExternal" is already registered!' |
| 51 | if hasattr(pybind11_tests, 'class_') else 'test_class not enabled') |
| 52 | |
| 53 | |
| 54 | def test_stl_bind_local(): |
| 55 | import pybind11_cross_module_tests as cm |
| 56 | |
| 57 | v1, v2 = m.LocalVec(), cm.LocalVec() |
| 58 | v1.append(m.LocalType(1)) |
| 59 | v1.append(m.LocalType(2)) |
| 60 | v2.append(cm.LocalType(1)) |
| 61 | v2.append(cm.LocalType(2)) |
| 62 | |
| 63 | with pytest.raises(TypeError): |
| 64 | v1.append(cm.LocalType(3)) |
| 65 | with pytest.raises(TypeError): |
| 66 | v2.append(m.LocalType(3)) |
| 67 | |
| 68 | assert [i.get() for i in v1] == [0, 1] |
| 69 | assert [i.get() for i in v2] == [2, 3] |
| 70 | |
| 71 | v3, v4 = m.NonLocalVec(), cm.NonLocalVec2() |
| 72 | v3.append(m.NonLocalType(1)) |
| 73 | v3.append(m.NonLocalType(2)) |
| 74 | v4.append(m.NonLocal2(3)) |
| 75 | v4.append(m.NonLocal2(4)) |
| 76 | |
| 77 | assert [i.get() for i in v3] == [1, 2] |
| 78 | assert [i.get() for i in v4] == [13, 14] |
| 79 | |
| 80 | d1, d2 = m.LocalMap(), cm.LocalMap() |
| 81 | d1["a"] = v1[0] |
| 82 | d1["b"] = v1[1] |
| 83 | d2["c"] = v2[0] |
| 84 | d2["d"] = v2[1] |
| 85 | assert {i: d1[i].get() for i in d1} == {'a': 0, 'b': 1} |
| 86 | assert {i: d2[i].get() for i in d2} == {'c': 2, 'd': 3} |
| 87 | |
| 88 | |
| 89 | def test_stl_bind_global(): |
| 90 | import pybind11_cross_module_tests as cm |
| 91 | |
| 92 | with pytest.raises(RuntimeError) as excinfo: |
| 93 | cm.register_nonlocal_map() |
| 94 | assert str(excinfo.value) == 'generic_type: type "NonLocalMap" is already registered!' |
| 95 | |
| 96 | with pytest.raises(RuntimeError) as excinfo: |
| 97 | cm.register_nonlocal_vec() |
| 98 | assert str(excinfo.value) == 'generic_type: type "NonLocalVec" is already registered!' |
| 99 | |
| 100 | with pytest.raises(RuntimeError) as excinfo: |
| 101 | cm.register_nonlocal_map2() |
| 102 | assert str(excinfo.value) == 'generic_type: type "NonLocalMap2" is already registered!' |
| 103 | |
| 104 | |
Jason Rhinelander | 4b15923 | 2017-08-04 13:05:12 -0400 | [diff] [blame] | 105 | def test_mixed_local_global(): |
| 106 | """Local types take precedence over globally registered types: a module with a `module_local` |
| 107 | type can be registered even if the type is already registered globally. With the module, |
| 108 | casting will go to the local type; outside the module casting goes to the global type.""" |
| 109 | import pybind11_cross_module_tests as cm |
| 110 | m.register_mixed_global() |
| 111 | m.register_mixed_local() |
| 112 | |
| 113 | a = [] |
| 114 | a.append(m.MixedGlobalLocal(1)) |
| 115 | a.append(m.MixedLocalGlobal(2)) |
| 116 | a.append(m.get_mixed_gl(3)) |
| 117 | a.append(m.get_mixed_lg(4)) |
| 118 | |
| 119 | assert [x.get() for x in a] == [101, 1002, 103, 1004] |
| 120 | |
| 121 | cm.register_mixed_global_local() |
| 122 | cm.register_mixed_local_global() |
| 123 | a.append(m.MixedGlobalLocal(5)) |
| 124 | a.append(m.MixedLocalGlobal(6)) |
| 125 | a.append(cm.MixedGlobalLocal(7)) |
| 126 | a.append(cm.MixedLocalGlobal(8)) |
| 127 | a.append(m.get_mixed_gl(9)) |
| 128 | a.append(m.get_mixed_lg(10)) |
| 129 | a.append(cm.get_mixed_gl(11)) |
| 130 | a.append(cm.get_mixed_lg(12)) |
| 131 | |
| 132 | assert [x.get() for x in a] == \ |
| 133 | [101, 1002, 103, 1004, 105, 1006, 207, 2008, 109, 1010, 211, 2012] |
| 134 | |
| 135 | |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 136 | def test_internal_locals_differ(): |
| 137 | """Makes sure the internal local type map differs across the two modules""" |
| 138 | import pybind11_cross_module_tests as cm |
| 139 | assert m.local_cpp_types_addr() != cm.local_cpp_types_addr() |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame^] | 140 | |
| 141 | |
| 142 | def test_stl_caster_vs_stl_bind(msg): |
| 143 | """One module uses a generic vector caster from `<pybind11/stl.h>` while the other |
| 144 | exports `std::vector<int>` via `py:bind_vector` and `py::module_local`""" |
| 145 | import pybind11_cross_module_tests as cm |
| 146 | |
| 147 | v1 = cm.VectorInt([1, 2, 3]) |
| 148 | assert m.load_vector_via_caster(v1) == 6 |
| 149 | assert cm.load_vector_via_binding(v1) == 6 |
| 150 | |
| 151 | v2 = [1, 2, 3] |
| 152 | assert m.load_vector_via_caster(v2) == 6 |
| 153 | with pytest.raises(TypeError) as excinfo: |
| 154 | cm.load_vector_via_binding(v2) == 6 |
| 155 | assert msg(excinfo.value) == """ |
| 156 | load_vector_via_binding(): incompatible function arguments. The following argument types are supported: |
| 157 | 1. (arg0: pybind11_cross_module_tests.VectorInt) -> int |
| 158 | |
| 159 | Invoked with: [1, 2, 3] |
| 160 | """ # noqa: E501 line too long |