diff --git a/tests/test_class_sh_shared_from_this.py b/tests/test_class_sh_shared_from_this.py index 112aa1810..42ff3e20e 100644 --- a/tests/test_class_sh_shared_from_this.py +++ b/tests/test_class_sh_shared_from_this.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# import pytest +import pytest from pybind11_tests import class_sh_shared_from_this as m from pybind11_tests import ConstructorStats @@ -30,7 +30,7 @@ def test_smart_ptr(capture): assert cstats.move_assignments == 0 -def test_shared_ptr_from_this_and_references(): +def test_shared_from_this_ref(): s = m.SharedFromThisRef() stats = ConstructorStats.get(m.B) assert stats.alive() == 2 @@ -41,6 +41,14 @@ def test_shared_ptr_from_this_and_references(): assert s.set_holder( ref ) # std::enable_shared_from_this can create a holder from a reference + del ref, s + assert stats.alive() == 0 + + +def test_shared_from_this_bad_wp(): + s = m.SharedFromThisRef() + stats = ConstructorStats.get(m.B) + assert stats.alive() == 2 bad_wp = s.bad_wp # init_holder_helper(holder_ptr=false, owned=false, bad_wp=true) assert stats.alive() == 2 @@ -49,31 +57,57 @@ def test_shared_ptr_from_this_and_references(): if 1: assert s.set_holder(bad_wp) # assert "Unable to cast from non-held to held instance" in str(excinfo.value) + del bad_wp, s + assert stats.alive() == 0 + + +def test_shared_from_this_copy(): + s = m.SharedFromThisRef() + stats = ConstructorStats.get(m.B) + assert stats.alive() == 2 copy = s.copy # init_holder_helper(holder_ptr=false, owned=true, bad_wp=false) # RuntimeError: Invalid return_value_policy for shared_ptr. assert stats.alive() == 3 assert s.set_ref(copy) assert s.set_holder(copy) + del copy, s + assert stats.alive() == 0 + + +def test_shared_from_this_holder_ref(): + s = m.SharedFromThisRef() + stats = ConstructorStats.get(m.B) + assert stats.alive() == 2 holder_ref = ( s.holder_ref ) # init_holder_helper(holder_ptr=true, owned=false, bad_wp=false) - assert stats.alive() == 3 + assert stats.alive() == 2 assert s.set_ref(holder_ref) assert s.set_holder(holder_ref) + del holder_ref, s + assert stats.alive() == 0 + + +def test_shared_from_this_holder_copy(): + s = m.SharedFromThisRef() + stats = ConstructorStats.get(m.B) + assert stats.alive() == 2 holder_copy = ( # RuntimeError: Invalid return_value_policy for shared_ptr. s.holder_copy ) # init_holder_helper(holder_ptr=true, owned=true, bad_wp=false) - assert stats.alive() == 3 + assert stats.alive() == 2 assert s.set_ref(holder_copy) assert s.set_holder(holder_copy) - - del ref, bad_wp, copy, holder_ref, holder_copy, s + del holder_copy, s assert stats.alive() == 0 + +def test_shared_from_this_virt(): + pytest.skip("Pre-empting ASAN heap-use-after-free in next line.") z = m.SharedFromThisVirt.get() y = m.SharedFromThisVirt.get() assert y is z