Exemple #1
0
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())
        pytest.gc_collect()
    assert capture == """
        Allocating child.
        Releasing child.
    """
    with capture:
        del p
        pytest.gc_collect()
    assert capture == "Releasing parent."

    with capture:
        p = Parent()
    assert capture == "Allocating parent."
    with capture:
        p.addChildKeepAlive(Child())
        pytest.gc_collect()
    assert capture == "Allocating child."
    with capture:
        del p
        pytest.gc_collect()
    assert capture == """
Exemple #2
0
def test_keep_alive_argument(capture):
    from pybind11_tests import Parent, Child, ConstructorStats

    n_inst = ConstructorStats.detail_reg_inst()
    with capture:
        p = Parent()
    assert capture == "Allocating parent."
    with capture:
        p.addChild(Child())
        assert ConstructorStats.detail_reg_inst() == n_inst + 1
    assert capture == """
        Allocating child.
        Releasing child.
    """
    with capture:
        del p
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert capture == "Releasing parent."

    with capture:
        p = Parent()
    assert capture == "Allocating parent."
    with capture:
        p.addChildKeepAlive(Child())
        assert ConstructorStats.detail_reg_inst() == n_inst + 2
    assert capture == "Allocating child."
    with capture:
        del p
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert capture == """
Exemple #3
0
def test_alive_gc(capture):
    from pybind11_tests import ParentGC, Child, ConstructorStats

    n_inst = ConstructorStats.detail_reg_inst()
    p = ParentGC()
    p.addChildKeepAlive(Child())
    assert ConstructorStats.detail_reg_inst() == n_inst + 2
    lst = [p]
    lst.append(lst)  # creates a circular reference
    with capture:
        del p, lst
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert capture == """
def test_keep_alive_argument(capture):
    from pybind11_tests import Parent, Child

    with capture:
        p = Parent()
    if capture != "Allocating parent.":
        raise AssertionError
    with capture:
        p.addChild(Child())
        pytest.gc_collect()
    if capture != """
        Allocating child.
        Releasing child.
    """:
        raise AssertionError
    with capture:
        del p
        pytest.gc_collect()
    if capture != "Releasing parent.":
        raise AssertionError

    with capture:
        p = Parent()
    if capture != "Allocating parent.":
        raise AssertionError
    with capture:
        p.addChildKeepAlive(Child())
        pytest.gc_collect()
    if capture != "Allocating child.":
        raise AssertionError
    with capture:
        del p
        pytest.gc_collect()
    if capture != """
        Releasing parent.
        Releasing child.
    """:
        raise AssertionError
Exemple #5
0
def test_alive_gc_multi_derived(capture):
    from pybind11_tests import Parent, Child, ConstructorStats

    class Derived(Parent, Child):
        pass

    n_inst = ConstructorStats.detail_reg_inst()
    p = Derived()
    p.addChildKeepAlive(Child())
    # +3 rather than +2 because Derived corresponds to two registered instances
    assert ConstructorStats.detail_reg_inst() == n_inst + 3
    lst = [p]
    lst.append(lst)  # creates a circular reference
    with capture:
        del p, lst
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert capture == """
Exemple #6
0
 def __init__(self):
     Parent.__init__(self)
     Child.__init__(self)