test_local_bindings.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import pytest
  2. from pybind11_tests import local_bindings as m
  3. def test_load_external():
  4. """Load a `py::module_local` type that's only registered in an external module"""
  5. import pybind11_cross_module_tests as cm
  6. assert m.load_external1(cm.ExternalType1(11)) == 11
  7. assert m.load_external2(cm.ExternalType2(22)) == 22
  8. with pytest.raises(TypeError) as excinfo:
  9. assert m.load_external2(cm.ExternalType1(21)) == 21
  10. assert "incompatible function arguments" in str(excinfo.value)
  11. with pytest.raises(TypeError) as excinfo:
  12. assert m.load_external1(cm.ExternalType2(12)) == 12
  13. assert "incompatible function arguments" in str(excinfo.value)
  14. def test_local_bindings():
  15. """Tests that duplicate `py::module_local` class bindings work across modules"""
  16. # Make sure we can load the second module with the conflicting (but local) definition:
  17. import pybind11_cross_module_tests as cm
  18. i1 = m.LocalType(5)
  19. assert i1.get() == 4
  20. assert i1.get3() == 8
  21. i2 = cm.LocalType(10)
  22. assert i2.get() == 11
  23. assert i2.get2() == 12
  24. assert not hasattr(i1, 'get2')
  25. assert not hasattr(i2, 'get3')
  26. # Loading within the local module
  27. assert m.local_value(i1) == 5
  28. assert cm.local_value(i2) == 10
  29. # Cross-module loading works as well (on failure, the type loader looks for
  30. # external module-local converters):
  31. assert m.local_value(i2) == 10
  32. assert cm.local_value(i1) == 5
  33. def test_nonlocal_failure():
  34. """Tests that attempting to register a non-local type in multiple modules fails"""
  35. import pybind11_cross_module_tests as cm
  36. with pytest.raises(RuntimeError) as excinfo:
  37. cm.register_nonlocal()
  38. assert str(excinfo.value) == 'generic_type: type "NonLocalType" is already registered!'
  39. def test_duplicate_local():
  40. """Tests expected failure when registering a class twice with py::local in the same module"""
  41. with pytest.raises(RuntimeError) as excinfo:
  42. m.register_local_external()
  43. import pybind11_tests
  44. assert str(excinfo.value) == (
  45. 'generic_type: type "LocalExternal" is already registered!'
  46. if hasattr(pybind11_tests, 'class_') else 'test_class not enabled')
  47. def test_stl_bind_local():
  48. import pybind11_cross_module_tests as cm
  49. v1, v2 = m.LocalVec(), cm.LocalVec()
  50. v1.append(m.LocalType(1))
  51. v1.append(m.LocalType(2))
  52. v2.append(cm.LocalType(1))
  53. v2.append(cm.LocalType(2))
  54. # Cross module value loading:
  55. v1.append(cm.LocalType(3))
  56. v2.append(m.LocalType(3))
  57. assert [i.get() for i in v1] == [0, 1, 2]
  58. assert [i.get() for i in v2] == [2, 3, 4]
  59. v3, v4 = m.NonLocalVec(), cm.NonLocalVec2()
  60. v3.append(m.NonLocalType(1))
  61. v3.append(m.NonLocalType(2))
  62. v4.append(m.NonLocal2(3))
  63. v4.append(m.NonLocal2(4))
  64. assert [i.get() for i in v3] == [1, 2]
  65. assert [i.get() for i in v4] == [13, 14]
  66. d1, d2 = m.LocalMap(), cm.LocalMap()
  67. d1["a"] = v1[0]
  68. d1["b"] = v1[1]
  69. d2["c"] = v2[0]
  70. d2["d"] = v2[1]
  71. assert {i: d1[i].get() for i in d1} == {'a': 0, 'b': 1}
  72. assert {i: d2[i].get() for i in d2} == {'c': 2, 'd': 3}
  73. def test_stl_bind_global():
  74. import pybind11_cross_module_tests as cm
  75. with pytest.raises(RuntimeError) as excinfo:
  76. cm.register_nonlocal_map()
  77. assert str(excinfo.value) == 'generic_type: type "NonLocalMap" is already registered!'
  78. with pytest.raises(RuntimeError) as excinfo:
  79. cm.register_nonlocal_vec()
  80. assert str(excinfo.value) == 'generic_type: type "NonLocalVec" is already registered!'
  81. with pytest.raises(RuntimeError) as excinfo:
  82. cm.register_nonlocal_map2()
  83. assert str(excinfo.value) == 'generic_type: type "NonLocalMap2" is already registered!'
  84. def test_mixed_local_global():
  85. """Local types take precedence over globally registered types: a module with a `module_local`
  86. type can be registered even if the type is already registered globally. With the module,
  87. casting will go to the local type; outside the module casting goes to the global type."""
  88. import pybind11_cross_module_tests as cm
  89. m.register_mixed_global()
  90. m.register_mixed_local()
  91. a = []
  92. a.append(m.MixedGlobalLocal(1))
  93. a.append(m.MixedLocalGlobal(2))
  94. a.append(m.get_mixed_gl(3))
  95. a.append(m.get_mixed_lg(4))
  96. assert [x.get() for x in a] == [101, 1002, 103, 1004]
  97. cm.register_mixed_global_local()
  98. cm.register_mixed_local_global()
  99. a.append(m.MixedGlobalLocal(5))
  100. a.append(m.MixedLocalGlobal(6))
  101. a.append(cm.MixedGlobalLocal(7))
  102. a.append(cm.MixedLocalGlobal(8))
  103. a.append(m.get_mixed_gl(9))
  104. a.append(m.get_mixed_lg(10))
  105. a.append(cm.get_mixed_gl(11))
  106. a.append(cm.get_mixed_lg(12))
  107. assert [x.get() for x in a] == \
  108. [101, 1002, 103, 1004, 105, 1006, 207, 2008, 109, 1010, 211, 2012]
  109. def test_internal_locals_differ():
  110. """Makes sure the internal local type map differs across the two modules"""
  111. import pybind11_cross_module_tests as cm
  112. assert m.local_cpp_types_addr() != cm.local_cpp_types_addr()
  113. def test_stl_caster_vs_stl_bind(msg):
  114. """One module uses a generic vector caster from `<pybind11/stl.h>` while the other
  115. exports `std::vector<int>` via `py:bind_vector` and `py::module_local`"""
  116. import pybind11_cross_module_tests as cm
  117. v1 = cm.VectorInt([1, 2, 3])
  118. assert m.load_vector_via_caster(v1) == 6
  119. assert cm.load_vector_via_binding(v1) == 6
  120. v2 = [1, 2, 3]
  121. assert m.load_vector_via_caster(v2) == 6
  122. with pytest.raises(TypeError) as excinfo:
  123. cm.load_vector_via_binding(v2) == 6
  124. assert msg(excinfo.value) == """
  125. load_vector_via_binding(): incompatible function arguments. The following argument types are supported:
  126. 1. (arg0: pybind11_cross_module_tests.VectorInt) -> int
  127. Invoked with: [1, 2, 3]
  128. """ # noqa: E501 line too long
  129. def test_cross_module_calls():
  130. import pybind11_cross_module_tests as cm
  131. v1 = m.LocalVec()
  132. v1.append(m.LocalType(1))
  133. v2 = cm.LocalVec()
  134. v2.append(cm.LocalType(2))
  135. # Returning the self pointer should get picked up as returning an existing
  136. # instance (even when that instance is of a foreign, non-local type).
  137. assert m.return_self(v1) is v1
  138. assert cm.return_self(v2) is v2
  139. assert m.return_self(v2) is v2
  140. assert cm.return_self(v1) is v1
  141. assert m.LocalVec is not cm.LocalVec
  142. # Returning a copy, on the other hand, always goes to the local type,
  143. # regardless of where the source type came from.
  144. assert type(m.return_copy(v1)) is m.LocalVec
  145. assert type(m.return_copy(v2)) is m.LocalVec
  146. assert type(cm.return_copy(v1)) is cm.LocalVec
  147. assert type(cm.return_copy(v2)) is cm.LocalVec
  148. # Test the example given in the documentation (which also tests inheritance casting):
  149. mycat = m.Cat("Fluffy")
  150. mydog = cm.Dog("Rover")
  151. assert mycat.get_name() == "Fluffy"
  152. assert mydog.name() == "Rover"
  153. assert m.Cat.__base__.__name__ == "Pet"
  154. assert cm.Dog.__base__.__name__ == "Pet"
  155. assert m.Cat.__base__ is not cm.Dog.__base__
  156. assert m.pet_name(mycat) == "Fluffy"
  157. assert m.pet_name(mydog) == "Rover"
  158. assert cm.pet_name(mycat) == "Fluffy"
  159. assert cm.pet_name(mydog) == "Rover"
  160. assert m.MixGL is not cm.MixGL
  161. a = m.MixGL(1)
  162. b = cm.MixGL(2)
  163. assert m.get_gl_value(a) == 11
  164. assert m.get_gl_value(b) == 12
  165. assert cm.get_gl_value(a) == 101
  166. assert cm.get_gl_value(b) == 102
  167. c, d = m.MixGL2(3), cm.MixGL2(4)
  168. with pytest.raises(TypeError) as excinfo:
  169. m.get_gl_value(c)
  170. assert "incompatible function arguments" in str(excinfo)
  171. with pytest.raises(TypeError) as excinfo:
  172. m.get_gl_value(d)
  173. assert "incompatible function arguments" in str(excinfo)