mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Allow references to objects held by smart pointers (#533)
This commit is contained in:
committed by
Wenzel Jakob
parent
8c85a85747
commit
ab90ec6ce9
@@ -1,3 +1,4 @@
|
||||
import pytest
|
||||
from pybind11_tests import ConstructorStats
|
||||
|
||||
|
||||
@@ -124,3 +125,74 @@ def test_unique_nodelete():
|
||||
del o
|
||||
cstats = ConstructorStats.get(MyObject4)
|
||||
assert cstats.alive() == 1 # Leak, but that's intentional
|
||||
|
||||
|
||||
def test_shared_ptr_and_references():
|
||||
from pybind11_tests.smart_ptr import SharedPtrRef, A
|
||||
|
||||
s = SharedPtrRef()
|
||||
stats = ConstructorStats.get(A)
|
||||
assert stats.alive() == 2
|
||||
|
||||
ref = s.ref # init_holder_helper(holder_ptr=false, owned=false)
|
||||
assert stats.alive() == 2
|
||||
assert s.set_ref(ref)
|
||||
with pytest.raises(RuntimeError) as excinfo:
|
||||
assert s.set_holder(ref)
|
||||
assert "Unable to cast from non-held to held instance" in str(excinfo.value)
|
||||
|
||||
copy = s.copy # init_holder_helper(holder_ptr=false, owned=true)
|
||||
assert stats.alive() == 3
|
||||
assert s.set_ref(copy)
|
||||
assert s.set_holder(copy)
|
||||
|
||||
holder_ref = s.holder_ref # init_holder_helper(holder_ptr=true, owned=false)
|
||||
assert stats.alive() == 3
|
||||
assert s.set_ref(holder_ref)
|
||||
assert s.set_holder(holder_ref)
|
||||
|
||||
holder_copy = s.holder_copy # init_holder_helper(holder_ptr=true, owned=true)
|
||||
assert stats.alive() == 3
|
||||
assert s.set_ref(holder_copy)
|
||||
assert s.set_holder(holder_copy)
|
||||
|
||||
del ref, copy, holder_ref, holder_copy, s
|
||||
assert stats.alive() == 0
|
||||
|
||||
|
||||
def test_shared_ptr_from_this_and_references():
|
||||
from pybind11_tests.smart_ptr import SharedFromThisRef, B
|
||||
|
||||
s = SharedFromThisRef()
|
||||
stats = ConstructorStats.get(B)
|
||||
assert stats.alive() == 2
|
||||
|
||||
ref = s.ref # init_holder_helper(holder_ptr=false, owned=false, bad_wp=false)
|
||||
assert stats.alive() == 2
|
||||
assert s.set_ref(ref)
|
||||
assert s.set_holder(ref) # std::enable_shared_from_this can create a holder from a reference
|
||||
|
||||
bad_wp = s.bad_wp # init_holder_helper(holder_ptr=false, owned=false, bad_wp=true)
|
||||
assert stats.alive() == 2
|
||||
assert s.set_ref(bad_wp)
|
||||
with pytest.raises(RuntimeError) as excinfo:
|
||||
assert s.set_holder(bad_wp)
|
||||
assert "Unable to cast from non-held to held instance" in str(excinfo.value)
|
||||
|
||||
copy = s.copy # init_holder_helper(holder_ptr=false, owned=true, bad_wp=false)
|
||||
assert stats.alive() == 3
|
||||
assert s.set_ref(copy)
|
||||
assert s.set_holder(copy)
|
||||
|
||||
holder_ref = s.holder_ref # init_holder_helper(holder_ptr=true, owned=false, bad_wp=false)
|
||||
assert stats.alive() == 3
|
||||
assert s.set_ref(holder_ref)
|
||||
assert s.set_holder(holder_ref)
|
||||
|
||||
holder_copy = s.holder_copy # init_holder_helper(holder_ptr=true, owned=true, bad_wp=false)
|
||||
assert stats.alive() == 3
|
||||
assert s.set_ref(holder_copy)
|
||||
assert s.set_holder(holder_copy)
|
||||
|
||||
del ref, bad_wp, copy, holder_ref, holder_copy, s
|
||||
assert stats.alive() == 0
|
||||
|
||||
Reference in New Issue
Block a user