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.

97 lines
2.1 KiB

  1. import gc
  2. def test_keep_alive_argument(capture):
  3. from pybind11_tests import Parent, Child
  4. with capture:
  5. p = Parent()
  6. assert capture == "Allocating parent."
  7. with capture:
  8. p.addChild(Child())
  9. gc.collect()
  10. assert capture == """
  11. Allocating child.
  12. Releasing child.
  13. """
  14. with capture:
  15. del p
  16. gc.collect()
  17. assert capture == "Releasing parent."
  18. with capture:
  19. p = Parent()
  20. assert capture == "Allocating parent."
  21. with capture:
  22. p.addChildKeepAlive(Child())
  23. gc.collect()
  24. assert capture == "Allocating child."
  25. with capture:
  26. del p
  27. gc.collect()
  28. assert capture == """
  29. Releasing parent.
  30. Releasing child.
  31. """
  32. def test_keep_alive_return_value(capture):
  33. from pybind11_tests import Parent
  34. with capture:
  35. p = Parent()
  36. assert capture == "Allocating parent."
  37. with capture:
  38. p.returnChild()
  39. gc.collect()
  40. assert capture == """
  41. Allocating child.
  42. Releasing child.
  43. """
  44. with capture:
  45. del p
  46. gc.collect()
  47. assert capture == "Releasing parent."
  48. with capture:
  49. p = Parent()
  50. assert capture == "Allocating parent."
  51. with capture:
  52. p.returnChildKeepAlive()
  53. gc.collect()
  54. assert capture == "Allocating child."
  55. with capture:
  56. del p
  57. gc.collect()
  58. assert capture == """
  59. Releasing parent.
  60. Releasing child.
  61. """
  62. def test_return_none(capture):
  63. from pybind11_tests import Parent
  64. with capture:
  65. p = Parent()
  66. assert capture == "Allocating parent."
  67. with capture:
  68. p.returnNullChildKeepAliveChild()
  69. gc.collect()
  70. assert capture == ""
  71. with capture:
  72. del p
  73. gc.collect()
  74. assert capture == "Releasing parent."
  75. with capture:
  76. p = Parent()
  77. assert capture == "Allocating parent."
  78. with capture:
  79. p.returnNullChildKeepAliveParent()
  80. gc.collect()
  81. assert capture == ""
  82. with capture:
  83. del p
  84. gc.collect()
  85. assert capture == "Releasing parent."