pybind11/tests/test_class_sh_trampoline_shared_from_this.py

79 lines
2.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import pybind11_tests.class_sh_trampoline_shared_from_this as m
import gc
import weakref
class PySft(m.Sft):
pass
def test_pass_shared_ptr():
obj = PySft("PySft")
assert obj.history == "PySft"
assert obj.use_count() == 2
m.pass_shared_ptr(obj)
assert obj.history == "PySft_PassSharedPtr"
assert obj.use_count() == 2
m.pass_shared_ptr(obj)
assert obj.history == "PySft_PassSharedPtr_PassSharedPtr"
assert obj.use_count() == 2
def test_pass_shared_ptr_while_stashed():
obj = PySft("PySft")
obj_wr = weakref.ref(obj)
stash1 = m.SftSharedPtrStash(1)
stash1.Add(obj)
assert obj.history == "PySft_Stash1Add"
assert obj.use_count() == 2
m.pass_shared_ptr(obj)
assert obj.history == "PySft_Stash1Add_PassSharedPtr"
assert obj.use_count() == 2
stash2 = m.SftSharedPtrStash(2)
stash2.Add(obj)
assert obj.history == "PySft_Stash1Add_PassSharedPtr_Stash2Add"
assert obj.use_count() == 2
assert stash2.history(0) == "PySft_Stash1Add_PassSharedPtr_Stash2Add"
assert stash2.use_count(0) == 1 # TODO: this is not great.
stash2.Add(obj)
assert obj.history == "PySft_Stash1Add_PassSharedPtr_Stash2Add_Stash2Add"
assert obj.use_count() == 2
assert stash1.use_count(0) == 1
assert stash1.history(0) == "PySft_Stash1Add_PassSharedPtr_Stash2Add_Stash2Add"
assert stash2.use_count(0) == 1
assert stash2.history(0) == "PySft_Stash1Add_PassSharedPtr_Stash2Add_Stash2Add"
assert stash2.use_count(1) == 1
assert stash2.history(1) == "PySft_Stash1Add_PassSharedPtr_Stash2Add_Stash2Add"
del obj
assert stash2.use_count(0) == 1
assert stash2.history(0) == "PySft_Stash1Add_PassSharedPtr_Stash2Add_Stash2Add"
assert stash2.use_count(1) == 1
assert stash2.history(1) == "PySft_Stash1Add_PassSharedPtr_Stash2Add_Stash2Add"
del stash2
gc.collect()
assert obj_wr() is not None
assert stash1.history(0) == "PySft_Stash1Add_PassSharedPtr_Stash2Add_Stash2Add"
del stash1
gc.collect()
assert obj_wr() is None
def test_pass_shared_ptr_while_stashed_with_shared_from_this():
obj = PySft("PySft")
obj_wr = weakref.ref(obj)
stash1 = m.SftSharedPtrStash(1)
stash1.AddSharedFromThis(obj)
assert obj.history == "PySft_Stash1AddSharedFromThis"
assert stash1.use_count(0) == 2
stash1.AddSharedFromThis(obj)
assert obj.history == "PySft_Stash1AddSharedFromThis_Stash1AddSharedFromThis"
assert stash1.use_count(0) == 3
assert stash1.use_count(1) == 3
del obj
del stash1
gc.collect()
assert obj_wr() is None