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