112391Sjason@lowepower.comimport pytest 212391Sjason@lowepower.com 312391Sjason@lowepower.comfrom pybind11_tests import local_bindings as m 412391Sjason@lowepower.com 512391Sjason@lowepower.com 612391Sjason@lowepower.comdef test_load_external(): 712391Sjason@lowepower.com """Load a `py::module_local` type that's only registered in an external module""" 812391Sjason@lowepower.com import pybind11_cross_module_tests as cm 912391Sjason@lowepower.com 1012391Sjason@lowepower.com assert m.load_external1(cm.ExternalType1(11)) == 11 1112391Sjason@lowepower.com assert m.load_external2(cm.ExternalType2(22)) == 22 1212391Sjason@lowepower.com 1312391Sjason@lowepower.com with pytest.raises(TypeError) as excinfo: 1412391Sjason@lowepower.com assert m.load_external2(cm.ExternalType1(21)) == 21 1512391Sjason@lowepower.com assert "incompatible function arguments" in str(excinfo.value) 1612391Sjason@lowepower.com 1712391Sjason@lowepower.com with pytest.raises(TypeError) as excinfo: 1812391Sjason@lowepower.com assert m.load_external1(cm.ExternalType2(12)) == 12 1912391Sjason@lowepower.com assert "incompatible function arguments" in str(excinfo.value) 2012391Sjason@lowepower.com 2112391Sjason@lowepower.com 2212391Sjason@lowepower.comdef test_local_bindings(): 2312391Sjason@lowepower.com """Tests that duplicate `py::module_local` class bindings work across modules""" 2412391Sjason@lowepower.com 2512391Sjason@lowepower.com # Make sure we can load the second module with the conflicting (but local) definition: 2612391Sjason@lowepower.com import pybind11_cross_module_tests as cm 2712391Sjason@lowepower.com 2812391Sjason@lowepower.com i1 = m.LocalType(5) 2912391Sjason@lowepower.com assert i1.get() == 4 3012391Sjason@lowepower.com assert i1.get3() == 8 3112391Sjason@lowepower.com 3212391Sjason@lowepower.com i2 = cm.LocalType(10) 3312391Sjason@lowepower.com assert i2.get() == 11 3412391Sjason@lowepower.com assert i2.get2() == 12 3512391Sjason@lowepower.com 3612391Sjason@lowepower.com assert not hasattr(i1, 'get2') 3712391Sjason@lowepower.com assert not hasattr(i2, 'get3') 3812391Sjason@lowepower.com 3912391Sjason@lowepower.com # Loading within the local module 4012391Sjason@lowepower.com assert m.local_value(i1) == 5 4112391Sjason@lowepower.com assert cm.local_value(i2) == 10 4212391Sjason@lowepower.com 4312391Sjason@lowepower.com # Cross-module loading works as well (on failure, the type loader looks for 4412391Sjason@lowepower.com # external module-local converters): 4512391Sjason@lowepower.com assert m.local_value(i2) == 10 4612391Sjason@lowepower.com assert cm.local_value(i1) == 5 4712391Sjason@lowepower.com 4812391Sjason@lowepower.com 4912391Sjason@lowepower.comdef test_nonlocal_failure(): 5012391Sjason@lowepower.com """Tests that attempting to register a non-local type in multiple modules fails""" 5112391Sjason@lowepower.com import pybind11_cross_module_tests as cm 5212391Sjason@lowepower.com 5312391Sjason@lowepower.com with pytest.raises(RuntimeError) as excinfo: 5412391Sjason@lowepower.com cm.register_nonlocal() 5512391Sjason@lowepower.com assert str(excinfo.value) == 'generic_type: type "NonLocalType" is already registered!' 5612391Sjason@lowepower.com 5712391Sjason@lowepower.com 5812391Sjason@lowepower.comdef test_duplicate_local(): 5912391Sjason@lowepower.com """Tests expected failure when registering a class twice with py::local in the same module""" 6012391Sjason@lowepower.com with pytest.raises(RuntimeError) as excinfo: 6112391Sjason@lowepower.com m.register_local_external() 6212391Sjason@lowepower.com import pybind11_tests 6312391Sjason@lowepower.com assert str(excinfo.value) == ( 6412391Sjason@lowepower.com 'generic_type: type "LocalExternal" is already registered!' 6512391Sjason@lowepower.com if hasattr(pybind11_tests, 'class_') else 'test_class not enabled') 6612391Sjason@lowepower.com 6712391Sjason@lowepower.com 6812391Sjason@lowepower.comdef test_stl_bind_local(): 6912391Sjason@lowepower.com import pybind11_cross_module_tests as cm 7012391Sjason@lowepower.com 7112391Sjason@lowepower.com v1, v2 = m.LocalVec(), cm.LocalVec() 7212391Sjason@lowepower.com v1.append(m.LocalType(1)) 7312391Sjason@lowepower.com v1.append(m.LocalType(2)) 7412391Sjason@lowepower.com v2.append(cm.LocalType(1)) 7512391Sjason@lowepower.com v2.append(cm.LocalType(2)) 7612391Sjason@lowepower.com 7712391Sjason@lowepower.com # Cross module value loading: 7812391Sjason@lowepower.com v1.append(cm.LocalType(3)) 7912391Sjason@lowepower.com v2.append(m.LocalType(3)) 8012391Sjason@lowepower.com 8112391Sjason@lowepower.com assert [i.get() for i in v1] == [0, 1, 2] 8212391Sjason@lowepower.com assert [i.get() for i in v2] == [2, 3, 4] 8312391Sjason@lowepower.com 8412391Sjason@lowepower.com v3, v4 = m.NonLocalVec(), cm.NonLocalVec2() 8512391Sjason@lowepower.com v3.append(m.NonLocalType(1)) 8612391Sjason@lowepower.com v3.append(m.NonLocalType(2)) 8712391Sjason@lowepower.com v4.append(m.NonLocal2(3)) 8812391Sjason@lowepower.com v4.append(m.NonLocal2(4)) 8912391Sjason@lowepower.com 9012391Sjason@lowepower.com assert [i.get() for i in v3] == [1, 2] 9112391Sjason@lowepower.com assert [i.get() for i in v4] == [13, 14] 9212391Sjason@lowepower.com 9312391Sjason@lowepower.com d1, d2 = m.LocalMap(), cm.LocalMap() 9412391Sjason@lowepower.com d1["a"] = v1[0] 9512391Sjason@lowepower.com d1["b"] = v1[1] 9612391Sjason@lowepower.com d2["c"] = v2[0] 9712391Sjason@lowepower.com d2["d"] = v2[1] 9812391Sjason@lowepower.com assert {i: d1[i].get() for i in d1} == {'a': 0, 'b': 1} 9912391Sjason@lowepower.com assert {i: d2[i].get() for i in d2} == {'c': 2, 'd': 3} 10012391Sjason@lowepower.com 10112391Sjason@lowepower.com 10212391Sjason@lowepower.comdef test_stl_bind_global(): 10312391Sjason@lowepower.com import pybind11_cross_module_tests as cm 10412391Sjason@lowepower.com 10512391Sjason@lowepower.com with pytest.raises(RuntimeError) as excinfo: 10612391Sjason@lowepower.com cm.register_nonlocal_map() 10712391Sjason@lowepower.com assert str(excinfo.value) == 'generic_type: type "NonLocalMap" is already registered!' 10812391Sjason@lowepower.com 10912391Sjason@lowepower.com with pytest.raises(RuntimeError) as excinfo: 11012391Sjason@lowepower.com cm.register_nonlocal_vec() 11112391Sjason@lowepower.com assert str(excinfo.value) == 'generic_type: type "NonLocalVec" is already registered!' 11212391Sjason@lowepower.com 11312391Sjason@lowepower.com with pytest.raises(RuntimeError) as excinfo: 11412391Sjason@lowepower.com cm.register_nonlocal_map2() 11512391Sjason@lowepower.com assert str(excinfo.value) == 'generic_type: type "NonLocalMap2" is already registered!' 11612391Sjason@lowepower.com 11712391Sjason@lowepower.com 11812391Sjason@lowepower.comdef test_mixed_local_global(): 11912391Sjason@lowepower.com """Local types take precedence over globally registered types: a module with a `module_local` 12012391Sjason@lowepower.com type can be registered even if the type is already registered globally. With the module, 12112391Sjason@lowepower.com casting will go to the local type; outside the module casting goes to the global type.""" 12212391Sjason@lowepower.com import pybind11_cross_module_tests as cm 12312391Sjason@lowepower.com m.register_mixed_global() 12412391Sjason@lowepower.com m.register_mixed_local() 12512391Sjason@lowepower.com 12612391Sjason@lowepower.com a = [] 12712391Sjason@lowepower.com a.append(m.MixedGlobalLocal(1)) 12812391Sjason@lowepower.com a.append(m.MixedLocalGlobal(2)) 12912391Sjason@lowepower.com a.append(m.get_mixed_gl(3)) 13012391Sjason@lowepower.com a.append(m.get_mixed_lg(4)) 13112391Sjason@lowepower.com 13212391Sjason@lowepower.com assert [x.get() for x in a] == [101, 1002, 103, 1004] 13312391Sjason@lowepower.com 13412391Sjason@lowepower.com cm.register_mixed_global_local() 13512391Sjason@lowepower.com cm.register_mixed_local_global() 13612391Sjason@lowepower.com a.append(m.MixedGlobalLocal(5)) 13712391Sjason@lowepower.com a.append(m.MixedLocalGlobal(6)) 13812391Sjason@lowepower.com a.append(cm.MixedGlobalLocal(7)) 13912391Sjason@lowepower.com a.append(cm.MixedLocalGlobal(8)) 14012391Sjason@lowepower.com a.append(m.get_mixed_gl(9)) 14112391Sjason@lowepower.com a.append(m.get_mixed_lg(10)) 14212391Sjason@lowepower.com a.append(cm.get_mixed_gl(11)) 14312391Sjason@lowepower.com a.append(cm.get_mixed_lg(12)) 14412391Sjason@lowepower.com 14512391Sjason@lowepower.com assert [x.get() for x in a] == \ 14612391Sjason@lowepower.com [101, 1002, 103, 1004, 105, 1006, 207, 2008, 109, 1010, 211, 2012] 14712391Sjason@lowepower.com 14812391Sjason@lowepower.com 14912391Sjason@lowepower.comdef test_internal_locals_differ(): 15012391Sjason@lowepower.com """Makes sure the internal local type map differs across the two modules""" 15112391Sjason@lowepower.com import pybind11_cross_module_tests as cm 15212391Sjason@lowepower.com assert m.local_cpp_types_addr() != cm.local_cpp_types_addr() 15312391Sjason@lowepower.com 15412391Sjason@lowepower.com 15512391Sjason@lowepower.comdef test_stl_caster_vs_stl_bind(msg): 15612391Sjason@lowepower.com """One module uses a generic vector caster from `<pybind11/stl.h>` while the other 15712391Sjason@lowepower.com exports `std::vector<int>` via `py:bind_vector` and `py::module_local`""" 15812391Sjason@lowepower.com import pybind11_cross_module_tests as cm 15912391Sjason@lowepower.com 16012391Sjason@lowepower.com v1 = cm.VectorInt([1, 2, 3]) 16112391Sjason@lowepower.com assert m.load_vector_via_caster(v1) == 6 16212391Sjason@lowepower.com assert cm.load_vector_via_binding(v1) == 6 16312391Sjason@lowepower.com 16412391Sjason@lowepower.com v2 = [1, 2, 3] 16512391Sjason@lowepower.com assert m.load_vector_via_caster(v2) == 6 16612391Sjason@lowepower.com with pytest.raises(TypeError) as excinfo: 16712391Sjason@lowepower.com cm.load_vector_via_binding(v2) == 6 16812391Sjason@lowepower.com assert msg(excinfo.value) == """ 16912391Sjason@lowepower.com load_vector_via_binding(): incompatible function arguments. The following argument types are supported: 17012391Sjason@lowepower.com 1. (arg0: pybind11_cross_module_tests.VectorInt) -> int 17112391Sjason@lowepower.com 17212391Sjason@lowepower.com Invoked with: [1, 2, 3] 17312391Sjason@lowepower.com """ # noqa: E501 line too long 17412391Sjason@lowepower.com 17512391Sjason@lowepower.com 17612391Sjason@lowepower.comdef test_cross_module_calls(): 17712391Sjason@lowepower.com import pybind11_cross_module_tests as cm 17812391Sjason@lowepower.com 17912391Sjason@lowepower.com v1 = m.LocalVec() 18012391Sjason@lowepower.com v1.append(m.LocalType(1)) 18112391Sjason@lowepower.com v2 = cm.LocalVec() 18212391Sjason@lowepower.com v2.append(cm.LocalType(2)) 18312391Sjason@lowepower.com 18412391Sjason@lowepower.com # Returning the self pointer should get picked up as returning an existing 18512391Sjason@lowepower.com # instance (even when that instance is of a foreign, non-local type). 18612391Sjason@lowepower.com assert m.return_self(v1) is v1 18712391Sjason@lowepower.com assert cm.return_self(v2) is v2 18812391Sjason@lowepower.com assert m.return_self(v2) is v2 18912391Sjason@lowepower.com assert cm.return_self(v1) is v1 19012391Sjason@lowepower.com 19112391Sjason@lowepower.com assert m.LocalVec is not cm.LocalVec 19212391Sjason@lowepower.com # Returning a copy, on the other hand, always goes to the local type, 19312391Sjason@lowepower.com # regardless of where the source type came from. 19412391Sjason@lowepower.com assert type(m.return_copy(v1)) is m.LocalVec 19512391Sjason@lowepower.com assert type(m.return_copy(v2)) is m.LocalVec 19612391Sjason@lowepower.com assert type(cm.return_copy(v1)) is cm.LocalVec 19712391Sjason@lowepower.com assert type(cm.return_copy(v2)) is cm.LocalVec 19812391Sjason@lowepower.com 19912391Sjason@lowepower.com # Test the example given in the documentation (which also tests inheritance casting): 20012391Sjason@lowepower.com mycat = m.Cat("Fluffy") 20112391Sjason@lowepower.com mydog = cm.Dog("Rover") 20212391Sjason@lowepower.com assert mycat.get_name() == "Fluffy" 20312391Sjason@lowepower.com assert mydog.name() == "Rover" 20412391Sjason@lowepower.com assert m.Cat.__base__.__name__ == "Pet" 20512391Sjason@lowepower.com assert cm.Dog.__base__.__name__ == "Pet" 20612391Sjason@lowepower.com assert m.Cat.__base__ is not cm.Dog.__base__ 20712391Sjason@lowepower.com assert m.pet_name(mycat) == "Fluffy" 20812391Sjason@lowepower.com assert m.pet_name(mydog) == "Rover" 20912391Sjason@lowepower.com assert cm.pet_name(mycat) == "Fluffy" 21012391Sjason@lowepower.com assert cm.pet_name(mydog) == "Rover" 21112391Sjason@lowepower.com 21212391Sjason@lowepower.com assert m.MixGL is not cm.MixGL 21312391Sjason@lowepower.com a = m.MixGL(1) 21412391Sjason@lowepower.com b = cm.MixGL(2) 21512391Sjason@lowepower.com assert m.get_gl_value(a) == 11 21612391Sjason@lowepower.com assert m.get_gl_value(b) == 12 21712391Sjason@lowepower.com assert cm.get_gl_value(a) == 101 21812391Sjason@lowepower.com assert cm.get_gl_value(b) == 102 21912391Sjason@lowepower.com 22012391Sjason@lowepower.com c, d = m.MixGL2(3), cm.MixGL2(4) 22112391Sjason@lowepower.com with pytest.raises(TypeError) as excinfo: 22212391Sjason@lowepower.com m.get_gl_value(c) 22314299Sbbruce@ucdavis.edu assert "incompatible function arguments" in str(excinfo.value) 22412391Sjason@lowepower.com with pytest.raises(TypeError) as excinfo: 22512391Sjason@lowepower.com m.get_gl_value(d) 22614299Sbbruce@ucdavis.edu assert "incompatible function arguments" in str(excinfo.value) 227