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.

251 lines
6.9 KiB

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