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