test_call_policies.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import env # noqa: F401
  4. from pybind11_tests import call_policies as m
  5. from pybind11_tests import ConstructorStats
  6. @pytest.mark.xfail("env.PYPY", reason="sometimes comes out 1 off on PyPy", strict=False)
  7. def test_keep_alive_argument(capture):
  8. n_inst = ConstructorStats.detail_reg_inst()
  9. with capture:
  10. p = m.Parent()
  11. assert capture == "Allocating parent."
  12. with capture:
  13. p.addChild(m.Child())
  14. assert ConstructorStats.detail_reg_inst() == n_inst + 1
  15. assert (
  16. capture
  17. == """
  18. Allocating child.
  19. Releasing child.
  20. """
  21. )
  22. with capture:
  23. del p
  24. assert ConstructorStats.detail_reg_inst() == n_inst
  25. assert capture == "Releasing parent."
  26. with capture:
  27. p = m.Parent()
  28. assert capture == "Allocating parent."
  29. with capture:
  30. p.addChildKeepAlive(m.Child())
  31. assert ConstructorStats.detail_reg_inst() == n_inst + 2
  32. assert capture == "Allocating child."
  33. with capture:
  34. del p
  35. assert ConstructorStats.detail_reg_inst() == n_inst
  36. assert (
  37. capture
  38. == """
  39. Releasing parent.
  40. Releasing child.
  41. """
  42. )
  43. def test_keep_alive_return_value(capture):
  44. n_inst = ConstructorStats.detail_reg_inst()
  45. with capture:
  46. p = m.Parent()
  47. assert capture == "Allocating parent."
  48. with capture:
  49. p.returnChild()
  50. assert ConstructorStats.detail_reg_inst() == n_inst + 1
  51. assert (
  52. capture
  53. == """
  54. Allocating child.
  55. Releasing child.
  56. """
  57. )
  58. with capture:
  59. del p
  60. assert ConstructorStats.detail_reg_inst() == n_inst
  61. assert capture == "Releasing parent."
  62. with capture:
  63. p = m.Parent()
  64. assert capture == "Allocating parent."
  65. with capture:
  66. p.returnChildKeepAlive()
  67. assert ConstructorStats.detail_reg_inst() == n_inst + 2
  68. assert capture == "Allocating child."
  69. with capture:
  70. del p
  71. assert ConstructorStats.detail_reg_inst() == n_inst
  72. assert (
  73. capture
  74. == """
  75. Releasing parent.
  76. Releasing child.
  77. """
  78. )
  79. # https://foss.heptapod.net/pypy/pypy/-/issues/2447
  80. @pytest.mark.xfail("env.PYPY", reason="_PyObject_GetDictPtr is unimplemented")
  81. def test_alive_gc(capture):
  82. n_inst = ConstructorStats.detail_reg_inst()
  83. p = m.ParentGC()
  84. p.addChildKeepAlive(m.Child())
  85. assert ConstructorStats.detail_reg_inst() == n_inst + 2
  86. lst = [p]
  87. lst.append(lst) # creates a circular reference
  88. with capture:
  89. del p, lst
  90. assert ConstructorStats.detail_reg_inst() == n_inst
  91. assert (
  92. capture
  93. == """
  94. Releasing parent.
  95. Releasing child.
  96. """
  97. )
  98. def test_alive_gc_derived(capture):
  99. class Derived(m.Parent):
  100. pass
  101. n_inst = ConstructorStats.detail_reg_inst()
  102. p = Derived()
  103. p.addChildKeepAlive(m.Child())
  104. assert ConstructorStats.detail_reg_inst() == n_inst + 2
  105. lst = [p]
  106. lst.append(lst) # creates a circular reference
  107. with capture:
  108. del p, lst
  109. assert ConstructorStats.detail_reg_inst() == n_inst
  110. assert (
  111. capture
  112. == """
  113. Releasing parent.
  114. Releasing child.
  115. """
  116. )
  117. def test_alive_gc_multi_derived(capture):
  118. class Derived(m.Parent, m.Child):
  119. def __init__(self):
  120. m.Parent.__init__(self)
  121. m.Child.__init__(self)
  122. n_inst = ConstructorStats.detail_reg_inst()
  123. p = Derived()
  124. p.addChildKeepAlive(m.Child())
  125. # +3 rather than +2 because Derived corresponds to two registered instances
  126. assert ConstructorStats.detail_reg_inst() == n_inst + 3
  127. lst = [p]
  128. lst.append(lst) # creates a circular reference
  129. with capture:
  130. del p, lst
  131. assert ConstructorStats.detail_reg_inst() == n_inst
  132. assert (
  133. capture
  134. == """
  135. Releasing parent.
  136. Releasing child.
  137. Releasing child.
  138. """
  139. )
  140. def test_return_none(capture):
  141. n_inst = ConstructorStats.detail_reg_inst()
  142. with capture:
  143. p = m.Parent()
  144. assert capture == "Allocating parent."
  145. with capture:
  146. p.returnNullChildKeepAliveChild()
  147. assert ConstructorStats.detail_reg_inst() == n_inst + 1
  148. assert capture == ""
  149. with capture:
  150. del p
  151. assert ConstructorStats.detail_reg_inst() == n_inst
  152. assert capture == "Releasing parent."
  153. with capture:
  154. p = m.Parent()
  155. assert capture == "Allocating parent."
  156. with capture:
  157. p.returnNullChildKeepAliveParent()
  158. assert ConstructorStats.detail_reg_inst() == n_inst + 1
  159. assert capture == ""
  160. with capture:
  161. del p
  162. assert ConstructorStats.detail_reg_inst() == n_inst
  163. assert capture == "Releasing parent."
  164. def test_keep_alive_constructor(capture):
  165. n_inst = ConstructorStats.detail_reg_inst()
  166. with capture:
  167. p = m.Parent(m.Child())
  168. assert ConstructorStats.detail_reg_inst() == n_inst + 2
  169. assert (
  170. capture
  171. == """
  172. Allocating child.
  173. Allocating parent.
  174. """
  175. )
  176. with capture:
  177. del p
  178. assert ConstructorStats.detail_reg_inst() == n_inst
  179. assert (
  180. capture
  181. == """
  182. Releasing parent.
  183. Releasing child.
  184. """
  185. )
  186. def test_call_guard():
  187. assert m.unguarded_call() == "unguarded"
  188. assert m.guarded_call() == "guarded"
  189. assert m.multiple_guards_correct_order() == "guarded & guarded"
  190. assert m.multiple_guards_wrong_order() == "unguarded & guarded"
  191. if hasattr(m, "with_gil"):
  192. assert m.with_gil() == "GIL held"
  193. assert m.without_gil() == "GIL released"