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 | |
| 106 | def test_internal_locals_differ(): |
| 107 | """Makes sure the internal local type map differs across the two modules""" |
| 108 | import pybind11_cross_module_tests as cm |
| 109 | assert m.local_cpp_types_addr() != cm.local_cpp_types_addr() |