|
Packit |
534379 |
import pytest
|
|
Packit |
534379 |
from pybind11_tests import call_policies as m
|
|
Packit |
534379 |
from pybind11_tests import ConstructorStats
|
|
Packit |
534379 |
|
|
Packit |
534379 |
|
|
Packit |
534379 |
def test_keep_alive_argument(capture):
|
|
Packit |
534379 |
n_inst = ConstructorStats.detail_reg_inst()
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p = m.Parent()
|
|
Packit |
534379 |
assert capture == "Allocating parent."
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p.addChild(m.Child())
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst + 1
|
|
Packit |
534379 |
assert capture == """
|
|
Packit |
534379 |
Allocating child.
|
|
Packit |
534379 |
Releasing child.
|
|
Packit |
534379 |
"""
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
del p
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
Packit |
534379 |
assert capture == "Releasing parent."
|
|
Packit |
534379 |
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p = m.Parent()
|
|
Packit |
534379 |
assert capture == "Allocating parent."
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p.addChildKeepAlive(m.Child())
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst + 2
|
|
Packit |
534379 |
assert capture == "Allocating child."
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
del p
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
Packit |
534379 |
assert capture == """
|
|
Packit |
534379 |
Releasing parent.
|
|
Packit |
534379 |
Releasing child.
|
|
Packit |
534379 |
"""
|
|
Packit |
534379 |
|
|
Packit |
534379 |
|
|
Packit |
534379 |
def test_keep_alive_return_value(capture):
|
|
Packit |
534379 |
n_inst = ConstructorStats.detail_reg_inst()
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p = m.Parent()
|
|
Packit |
534379 |
assert capture == "Allocating parent."
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p.returnChild()
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst + 1
|
|
Packit |
534379 |
assert capture == """
|
|
Packit |
534379 |
Allocating child.
|
|
Packit |
534379 |
Releasing child.
|
|
Packit |
534379 |
"""
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
del p
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
Packit |
534379 |
assert capture == "Releasing parent."
|
|
Packit |
534379 |
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p = m.Parent()
|
|
Packit |
534379 |
assert capture == "Allocating parent."
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p.returnChildKeepAlive()
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst + 2
|
|
Packit |
534379 |
assert capture == "Allocating child."
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
del p
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
Packit |
534379 |
assert capture == """
|
|
Packit |
534379 |
Releasing parent.
|
|
Packit |
534379 |
Releasing child.
|
|
Packit |
534379 |
"""
|
|
Packit |
534379 |
|
|
Packit |
534379 |
|
|
Packit |
534379 |
# https://bitbucket.org/pypy/pypy/issues/2447
|
|
Packit |
534379 |
@pytest.unsupported_on_pypy
|
|
Packit |
534379 |
def test_alive_gc(capture):
|
|
Packit |
534379 |
n_inst = ConstructorStats.detail_reg_inst()
|
|
Packit |
534379 |
p = m.ParentGC()
|
|
Packit |
534379 |
p.addChildKeepAlive(m.Child())
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst + 2
|
|
Packit |
534379 |
lst = [p]
|
|
Packit |
534379 |
lst.append(lst) # creates a circular reference
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
del p, lst
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
Packit |
534379 |
assert capture == """
|
|
Packit |
534379 |
Releasing parent.
|
|
Packit |
534379 |
Releasing child.
|
|
Packit |
534379 |
"""
|
|
Packit |
534379 |
|
|
Packit |
534379 |
|
|
Packit |
534379 |
def test_alive_gc_derived(capture):
|
|
Packit |
534379 |
class Derived(m.Parent):
|
|
Packit |
534379 |
pass
|
|
Packit |
534379 |
|
|
Packit |
534379 |
n_inst = ConstructorStats.detail_reg_inst()
|
|
Packit |
534379 |
p = Derived()
|
|
Packit |
534379 |
p.addChildKeepAlive(m.Child())
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst + 2
|
|
Packit |
534379 |
lst = [p]
|
|
Packit |
534379 |
lst.append(lst) # creates a circular reference
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
del p, lst
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
Packit |
534379 |
assert capture == """
|
|
Packit |
534379 |
Releasing parent.
|
|
Packit |
534379 |
Releasing child.
|
|
Packit |
534379 |
"""
|
|
Packit |
534379 |
|
|
Packit |
534379 |
|
|
Packit |
534379 |
def test_alive_gc_multi_derived(capture):
|
|
Packit |
534379 |
class Derived(m.Parent, m.Child):
|
|
Packit |
534379 |
def __init__(self):
|
|
Packit |
534379 |
m.Parent.__init__(self)
|
|
Packit |
534379 |
m.Child.__init__(self)
|
|
Packit |
534379 |
|
|
Packit |
534379 |
n_inst = ConstructorStats.detail_reg_inst()
|
|
Packit |
534379 |
p = Derived()
|
|
Packit |
534379 |
p.addChildKeepAlive(m.Child())
|
|
Packit |
534379 |
# +3 rather than +2 because Derived corresponds to two registered instances
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst + 3
|
|
Packit |
534379 |
lst = [p]
|
|
Packit |
534379 |
lst.append(lst) # creates a circular reference
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
del p, lst
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
Packit |
534379 |
assert capture == """
|
|
Packit |
534379 |
Releasing parent.
|
|
Packit |
534379 |
Releasing child.
|
|
Packit |
534379 |
Releasing child.
|
|
Packit |
534379 |
"""
|
|
Packit |
534379 |
|
|
Packit |
534379 |
|
|
Packit |
534379 |
def test_return_none(capture):
|
|
Packit |
534379 |
n_inst = ConstructorStats.detail_reg_inst()
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p = m.Parent()
|
|
Packit |
534379 |
assert capture == "Allocating parent."
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p.returnNullChildKeepAliveChild()
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst + 1
|
|
Packit |
534379 |
assert capture == ""
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
del p
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
Packit |
534379 |
assert capture == "Releasing parent."
|
|
Packit |
534379 |
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p = m.Parent()
|
|
Packit |
534379 |
assert capture == "Allocating parent."
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p.returnNullChildKeepAliveParent()
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst + 1
|
|
Packit |
534379 |
assert capture == ""
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
del p
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
Packit |
534379 |
assert capture == "Releasing parent."
|
|
Packit |
534379 |
|
|
Packit |
534379 |
|
|
Packit |
534379 |
def test_keep_alive_constructor(capture):
|
|
Packit |
534379 |
n_inst = ConstructorStats.detail_reg_inst()
|
|
Packit |
534379 |
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
p = m.Parent(m.Child())
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst + 2
|
|
Packit |
534379 |
assert capture == """
|
|
Packit |
534379 |
Allocating child.
|
|
Packit |
534379 |
Allocating parent.
|
|
Packit |
534379 |
"""
|
|
Packit |
534379 |
with capture:
|
|
Packit |
534379 |
del p
|
|
Packit |
534379 |
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
Packit |
534379 |
assert capture == """
|
|
Packit |
534379 |
Releasing parent.
|
|
Packit |
534379 |
Releasing child.
|
|
Packit |
534379 |
"""
|
|
Packit |
534379 |
|
|
Packit |
534379 |
|
|
Packit |
534379 |
def test_call_guard():
|
|
Packit |
534379 |
assert m.unguarded_call() == "unguarded"
|
|
Packit |
534379 |
assert m.guarded_call() == "guarded"
|
|
Packit |
534379 |
|
|
Packit |
534379 |
assert m.multiple_guards_correct_order() == "guarded & guarded"
|
|
Packit |
534379 |
assert m.multiple_guards_wrong_order() == "unguarded & guarded"
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if hasattr(m, "with_gil"):
|
|
Packit |
534379 |
assert m.with_gil() == "GIL held"
|
|
Packit |
534379 |
assert m.without_gil() == "GIL released"
|