test_keep_alive.py 2.18 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
import pytest
Dean Moldovan's avatar
Dean Moldovan committed
2
3
4
5
6
7
8
9
10
11


def test_keep_alive_argument(capture):
    from pybind11_tests import Parent, Child

    with capture:
        p = Parent()
    assert capture == "Allocating parent."
    with capture:
        p.addChild(Child())
Wenzel Jakob's avatar
Wenzel Jakob committed
12
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
13
14
15
16
17
18
    assert capture == """
        Allocating child.
        Releasing child.
    """
    with capture:
        del p
Wenzel Jakob's avatar
Wenzel Jakob committed
19
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
20
21
22
23
24
25
26
    assert capture == "Releasing parent."

    with capture:
        p = Parent()
    assert capture == "Allocating parent."
    with capture:
        p.addChildKeepAlive(Child())
Wenzel Jakob's avatar
Wenzel Jakob committed
27
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
28
29
30
    assert capture == "Allocating child."
    with capture:
        del p
Wenzel Jakob's avatar
Wenzel Jakob committed
31
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    assert capture == """
        Releasing parent.
        Releasing child.
    """


def test_keep_alive_return_value(capture):
    from pybind11_tests import Parent

    with capture:
        p = Parent()
    assert capture == "Allocating parent."
    with capture:
        p.returnChild()
Wenzel Jakob's avatar
Wenzel Jakob committed
46
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
47
48
49
50
51
52
    assert capture == """
        Allocating child.
        Releasing child.
    """
    with capture:
        del p
Wenzel Jakob's avatar
Wenzel Jakob committed
53
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
54
55
56
57
58
59
60
    assert capture == "Releasing parent."

    with capture:
        p = Parent()
    assert capture == "Allocating parent."
    with capture:
        p.returnChildKeepAlive()
Wenzel Jakob's avatar
Wenzel Jakob committed
61
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
62
63
64
    assert capture == "Allocating child."
    with capture:
        del p
Wenzel Jakob's avatar
Wenzel Jakob committed
65
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    assert capture == """
        Releasing parent.
        Releasing child.
    """


def test_return_none(capture):
    from pybind11_tests import Parent

    with capture:
        p = Parent()
    assert capture == "Allocating parent."
    with capture:
        p.returnNullChildKeepAliveChild()
Wenzel Jakob's avatar
Wenzel Jakob committed
80
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
81
82
83
    assert capture == ""
    with capture:
        del p
Wenzel Jakob's avatar
Wenzel Jakob committed
84
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
85
86
87
88
89
90
91
    assert capture == "Releasing parent."

    with capture:
        p = Parent()
    assert capture == "Allocating parent."
    with capture:
        p.returnNullChildKeepAliveParent()
Wenzel Jakob's avatar
Wenzel Jakob committed
92
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
93
94
95
    assert capture == ""
    with capture:
        del p
Wenzel Jakob's avatar
Wenzel Jakob committed
96
        pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
97
    assert capture == "Releasing parent."