You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
6.9 KiB

  1. import pytest
  2. import gc
  3. from pybind11_tests import ConstructorStats
  4. def test_regressions():
  5. from pybind11_tests.issues import print_cchar, print_char
  6. # #137: const char* isn't handled properly
  7. assert print_cchar("const char *") == "const char *"
  8. # #150: char bindings broken
  9. assert print_char("c") == "c"
  10. def test_dispatch_issue(msg):
  11. """#159: virtual function dispatch has problems with similar-named functions"""
  12. from pybind11_tests.issues import DispatchIssue, dispatch_issue_go
  13. class PyClass1(DispatchIssue):
  14. def dispatch(self):
  15. return "Yay.."
  16. class PyClass2(DispatchIssue):
  17. def dispatch(self):
  18. with pytest.raises(RuntimeError) as excinfo:
  19. super(PyClass2, self).dispatch()
  20. assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"'
  21. p = PyClass1()
  22. return dispatch_issue_go(p)
  23. b = PyClass2()
  24. assert dispatch_issue_go(b) == "Yay.."
  25. def test_reference_wrapper():
  26. """#171: Can't return reference wrappers (or STL data structures containing them)"""
  27. from pybind11_tests.issues import Placeholder, return_vec_of_reference_wrapper
  28. assert str(return_vec_of_reference_wrapper(Placeholder(4))) == \
  29. "[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]"
  30. def test_iterator_passthrough():
  31. """#181: iterator passthrough did not compile"""
  32. from pybind11_tests.issues import iterator_passthrough
  33. assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
  34. def test_shared_ptr_gc():
  35. """// #187: issue involving std::shared_ptr<> return value policy & garbage collection"""
  36. from pybind11_tests.issues import ElementList, ElementA
  37. el = ElementList()
  38. for i in range(10):
  39. el.add(ElementA(i))
  40. gc.collect()
  41. for i, v in enumerate(el.get()):
  42. assert i == v.value()
  43. def test_no_id(msg):
  44. from pybind11_tests.issues import get_element, expect_float, expect_int
  45. with pytest.raises(TypeError) as excinfo:
  46. get_element(None)
  47. assert msg(excinfo.value) == """
  48. get_element(): incompatible function arguments. The following argument types are supported:
  49. 1. (arg0: m.issues.ElementA) -> int
  50. Invoked with: None
  51. """
  52. with pytest.raises(TypeError) as excinfo:
  53. expect_int(5.2)
  54. assert msg(excinfo.value) == """
  55. expect_int(): incompatible function arguments. The following argument types are supported:
  56. 1. (arg0: int) -> int
  57. Invoked with: 5.2
  58. """
  59. assert expect_float(12) == 12
  60. def test_str_issue(msg):
  61. """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
  62. from pybind11_tests.issues import StrIssue
  63. assert str(StrIssue(3)) == "StrIssue[3]"
  64. with pytest.raises(TypeError) as excinfo:
  65. str(StrIssue("no", "such", "constructor"))
  66. assert msg(excinfo.value) == """
  67. __init__(): incompatible constructor arguments. The following argument types are supported:
  68. 1. m.issues.StrIssue(arg0: int)
  69. 2. m.issues.StrIssue()
  70. Invoked with: 'no', 'such', 'constructor'
  71. """
  72. def test_nested():
  73. """ #328: first member in a class can't be used in operators"""
  74. from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
  75. a = NestA()
  76. b = NestB()
  77. c = NestC()
  78. a += 10
  79. assert get_NestA(a) == 13
  80. b.a += 100
  81. assert get_NestA(b.a) == 103
  82. c.b.a += 1000
  83. assert get_NestA(c.b.a) == 1003
  84. b -= 1
  85. assert get_NestB(b) == 3
  86. c.b -= 3
  87. assert get_NestB(c.b) == 1
  88. c *= 7
  89. assert get_NestC(c) == 35
  90. abase = a.as_base()
  91. assert abase.value == -2
  92. a.as_base().value += 44
  93. assert abase.value == 42
  94. assert c.b.a.as_base().value == -2
  95. c.b.a.as_base().value += 44
  96. assert c.b.a.as_base().value == 42
  97. del c
  98. gc.collect()
  99. del a # Should't delete while abase is still alive
  100. gc.collect()
  101. assert abase.value == 42
  102. del abase, b
  103. gc.collect()
  104. def test_move_fallback():
  105. from pybind11_tests.issues import get_moveissue1, get_moveissue2
  106. m2 = get_moveissue2(2)
  107. assert m2.value == 2
  108. m1 = get_moveissue1(1)
  109. assert m1.value == 1
  110. def test_override_ref():
  111. from pybind11_tests.issues import OverrideTest
  112. o = OverrideTest("asdf")
  113. # Not allowed (see associated .cpp comment)
  114. # i = o.str_ref()
  115. # assert o.str_ref() == "asdf"
  116. assert o.str_value() == "asdf"
  117. assert o.A_value().value == "hi"
  118. a = o.A_ref()
  119. assert a.value == "hi"
  120. a.value = "bye"
  121. assert a.value == "bye"
  122. def test_operators_notimplemented(capture):
  123. from pybind11_tests.issues import OpTest1, OpTest2
  124. with capture:
  125. c1, c2 = OpTest1(), OpTest2()
  126. c1 + c1
  127. c2 + c2
  128. c2 + c1
  129. c1 + c2
  130. assert capture == """
  131. Add OpTest1 with OpTest1
  132. Add OpTest2 with OpTest2
  133. Add OpTest2 with OpTest1
  134. Add OpTest2 with OpTest1
  135. """
  136. def test_iterator_rvpolicy():
  137. """ Issue 388: Can't make iterators via make_iterator() with different r/v policies """
  138. from pybind11_tests.issues import make_iterator_1
  139. from pybind11_tests.issues import make_iterator_2
  140. assert list(make_iterator_1()) == [1, 2, 3]
  141. assert list(make_iterator_2()) == [1, 2, 3]
  142. assert not isinstance(make_iterator_1(), type(make_iterator_2()))
  143. def test_dupe_assignment():
  144. """ Issue 461: overwriting a class with a function """
  145. from pybind11_tests.issues import dupe_exception_failures
  146. assert dupe_exception_failures() == []
  147. def test_enable_shared_from_this_with_reference_rvp():
  148. """ Issue #471: shared pointer instance not dellocated """
  149. from pybind11_tests import SharedParent, SharedChild
  150. parent = SharedParent()
  151. child = parent.get_child()
  152. cstats = ConstructorStats.get(SharedChild)
  153. assert cstats.alive() == 1
  154. del child, parent
  155. assert cstats.alive() == 0
  156. def test_non_destructed_holders():
  157. """ Issue #478: unique ptrs constructed and freed without destruction """
  158. from pybind11_tests import SpecialHolderObj
  159. a = SpecialHolderObj(123)
  160. b = a.child()
  161. assert a.val == 123
  162. assert b.val == 124
  163. cstats = SpecialHolderObj.holder_cstats()
  164. assert cstats.alive() == 1
  165. del b
  166. assert cstats.alive() == 1
  167. del a
  168. assert cstats.alive() == 0
  169. def test_complex_cast(capture):
  170. """ Issue #484: number conversion generates unhandled exceptions """
  171. from pybind11_tests.issues import test_complex
  172. with capture:
  173. test_complex(1)
  174. test_complex(2j)
  175. assert capture == """
  176. 1.0
  177. (0.0, 2.0)
  178. """
  179. def test_inheritance_override_def_static():
  180. from pybind11_tests.issues import MyBase, MyDerived
  181. b = MyBase.make()
  182. d1 = MyDerived.make2()
  183. d2 = MyDerived.make()
  184. assert isinstance(b, MyBase)
  185. assert isinstance(d1, MyDerived)
  186. assert isinstance(d2, MyDerived)