Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 1 | import pytest |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 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()) |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 12 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 13 | assert capture == """ |
| 14 | Allocating child. |
| 15 | Releasing child. |
| 16 | """ |
| 17 | with capture: |
| 18 | del p |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 19 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 20 | assert capture == "Releasing parent." |
| 21 | |
| 22 | with capture: |
| 23 | p = Parent() |
| 24 | assert capture == "Allocating parent." |
| 25 | with capture: |
| 26 | p.addChildKeepAlive(Child()) |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 27 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 28 | assert capture == "Allocating child." |
| 29 | with capture: |
| 30 | del p |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 31 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 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() |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 46 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 47 | assert capture == """ |
| 48 | Allocating child. |
| 49 | Releasing child. |
| 50 | """ |
| 51 | with capture: |
| 52 | del p |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 53 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 54 | assert capture == "Releasing parent." |
| 55 | |
| 56 | with capture: |
| 57 | p = Parent() |
| 58 | assert capture == "Allocating parent." |
| 59 | with capture: |
| 60 | p.returnChildKeepAlive() |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 61 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 62 | assert capture == "Allocating child." |
| 63 | with capture: |
| 64 | del p |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 65 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 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() |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 80 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 81 | assert capture == "" |
| 82 | with capture: |
| 83 | del p |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 84 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 85 | assert capture == "Releasing parent." |
| 86 | |
| 87 | with capture: |
| 88 | p = Parent() |
| 89 | assert capture == "Allocating parent." |
| 90 | with capture: |
| 91 | p.returnNullChildKeepAliveParent() |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 92 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 93 | assert capture == "" |
| 94 | with capture: |
| 95 | del p |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 96 | pytest.gc_collect() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 97 | assert capture == "Releasing parent." |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 98 | |
| 99 | |
| 100 | def test_call_guard(): |
| 101 | from pybind11_tests import call_policies |
| 102 | |
| 103 | assert call_policies.unguarded_call() == "unguarded" |
| 104 | assert call_policies.guarded_call() == "guarded" |
| 105 | |
| 106 | assert call_policies.multiple_guards_correct_order() == "guarded & guarded" |
| 107 | assert call_policies.multiple_guards_wrong_order() == "unguarded & guarded" |
| 108 | |
| 109 | if hasattr(call_policies, "with_gil"): |
| 110 | assert call_policies.with_gil() == "GIL held" |
| 111 | assert call_policies.without_gil() == "GIL released" |