mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-25 14:45:12 +00:00
391c75447d
This udpates all the remaining tests to the new test suite code and comment styles started in #898. For the most part, the test coverage here is unchanged, with a few minor exceptions as noted below. - test_constants_and_functions: this adds more overload tests with overloads with different number of arguments for more comprehensive overload_cast testing. The test style conversion broke the overload tests under MSVC 2015, prompting the additional tests while looking for a workaround. - test_eigen: this dropped the unused functions `get_cm_corners` and `get_cm_corners_const`--these same tests were duplicates of the same things provided (and used) via ReturnTester methods. - test_opaque_types: this test had a hidden dependence on ExampleMandA which is now fixed by using the global UserType which suffices for the relevant test. - test_methods_and_attributes: this required some additions to UserType to make it usable as a replacement for the test's previous SimpleType: UserType gained a value mutator, and the `value` property is not mutable (it was previously readonly). Some overload tests were also added to better test overload_cast (as described above). - test_numpy_array: removed the untemplated mutate_data/mutate_data_t: the templated versions with an empty parameter pack expand to the same thing. - test_stl: this was already mostly in the new style; this just tweaks things a bit, localizing a class, and adding some missing `// test_whatever` comments. - test_virtual_functions: like `test_stl`, this was mostly in the new test style already, but needed some `// test_whatever` comments. This commit also moves the inherited virtual example code to the end of the file, after the main set of tests (since it is less important than the other tests, and rather length); it also got renamed to `test_inherited_virtuals` (from `test_inheriting_repeat`) because it tests both inherited virtual approaches, not just the repeat approach.
169 lines
4.7 KiB
Python
169 lines
4.7 KiB
Python
import pytest
|
|
from pybind11_tests import call_policies as m
|
|
from pybind11_tests import ConstructorStats
|
|
|
|
|
|
def test_keep_alive_argument(capture):
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
with capture:
|
|
p = m.Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.addChild(m.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 = m.Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.addChildKeepAlive(m.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 == """
|
|
Releasing parent.
|
|
Releasing child.
|
|
"""
|
|
|
|
|
|
def test_keep_alive_return_value(capture):
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
with capture:
|
|
p = m.Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.returnChild()
|
|
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 = m.Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.returnChildKeepAlive()
|
|
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 == """
|
|
Releasing parent.
|
|
Releasing child.
|
|
"""
|
|
|
|
|
|
# https://bitbucket.org/pypy/pypy/issues/2447
|
|
@pytest.unsupported_on_pypy
|
|
def test_alive_gc(capture):
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
p = m.ParentGC()
|
|
p.addChildKeepAlive(m.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 == """
|
|
Releasing parent.
|
|
Releasing child.
|
|
"""
|
|
|
|
|
|
def test_alive_gc_derived(capture):
|
|
class Derived(m.Parent):
|
|
pass
|
|
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
p = Derived()
|
|
p.addChildKeepAlive(m.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 == """
|
|
Releasing parent.
|
|
Releasing child.
|
|
"""
|
|
|
|
|
|
def test_alive_gc_multi_derived(capture):
|
|
class Derived(m.Parent, m.Child):
|
|
def __init__(self):
|
|
m.Parent.__init__(self)
|
|
m.Child.__init__(self)
|
|
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
p = Derived()
|
|
p.addChildKeepAlive(m.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 == """
|
|
Releasing parent.
|
|
Releasing child.
|
|
Releasing child.
|
|
"""
|
|
|
|
|
|
def test_return_none(capture):
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
with capture:
|
|
p = m.Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.returnNullChildKeepAliveChild()
|
|
assert ConstructorStats.detail_reg_inst() == n_inst + 1
|
|
assert capture == ""
|
|
with capture:
|
|
del p
|
|
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
assert capture == "Releasing parent."
|
|
|
|
with capture:
|
|
p = m.Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.returnNullChildKeepAliveParent()
|
|
assert ConstructorStats.detail_reg_inst() == n_inst + 1
|
|
assert capture == ""
|
|
with capture:
|
|
del p
|
|
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
assert capture == "Releasing parent."
|
|
|
|
|
|
def test_call_guard():
|
|
assert m.unguarded_call() == "unguarded"
|
|
assert m.guarded_call() == "guarded"
|
|
|
|
assert m.multiple_guards_correct_order() == "guarded & guarded"
|
|
assert m.multiple_guards_wrong_order() == "unguarded & guarded"
|
|
|
|
if hasattr(m, "with_gil"):
|
|
assert m.with_gil() == "GIL held"
|
|
assert m.without_gil() == "GIL released"
|