2021-06-20 01:13:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-06-20 16:45:39 +00:00
|
|
|
import env # noqa: F401
|
|
|
|
|
2021-06-20 01:13:27 +00:00
|
|
|
import pybind11_tests.class_sh_trampoline_shared_from_this as m
|
|
|
|
|
2021-06-20 15:35:51 +00:00
|
|
|
import gc
|
|
|
|
import weakref
|
2021-06-20 01:13:27 +00:00
|
|
|
|
2021-06-20 15:35:51 +00:00
|
|
|
|
|
|
|
class PySft(m.Sft):
|
2021-06-20 01:13:27 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_pass_shared_ptr():
|
2021-06-20 15:35:51 +00:00
|
|
|
obj = PySft("PySft")
|
|
|
|
assert obj.history == "PySft"
|
2021-06-20 16:45:39 +00:00
|
|
|
assert obj.use_count() in [2, -1] # TODO: Be smarter/stricter.
|
2021-06-20 15:35:51 +00:00
|
|
|
m.pass_shared_ptr(obj)
|
|
|
|
assert obj.history == "PySft_PassSharedPtr"
|
2021-06-20 16:45:39 +00:00
|
|
|
assert obj.use_count() in [2, -1]
|
2021-06-20 01:13:27 +00:00
|
|
|
m.pass_shared_ptr(obj)
|
2021-06-20 15:35:51 +00:00
|
|
|
assert obj.history == "PySft_PassSharedPtr_PassSharedPtr"
|
2021-06-20 16:45:39 +00:00
|
|
|
assert obj.use_count() in [2, -1]
|
2021-06-20 15:35:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
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"
|
2021-06-20 16:45:39 +00:00
|
|
|
assert obj.use_count() in [2, -1]
|
2021-06-20 01:13:27 +00:00
|
|
|
m.pass_shared_ptr(obj)
|
2021-06-20 15:35:51 +00:00
|
|
|
assert obj.history == "PySft_Stash1Add_PassSharedPtr"
|
2021-06-20 16:45:39 +00:00
|
|
|
assert obj.use_count() in [2, -1]
|
2021-06-20 15:35:51 +00:00
|
|
|
stash2 = m.SftSharedPtrStash(2)
|
|
|
|
stash2.Add(obj)
|
|
|
|
assert obj.history == "PySft_Stash1Add_PassSharedPtr_Stash2Add"
|
2021-06-20 16:45:39 +00:00
|
|
|
assert obj.use_count() in [2, -1]
|
2021-06-20 15:35:51 +00:00
|
|
|
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"
|
2021-06-20 16:45:39 +00:00
|
|
|
assert obj.use_count() in [2, -1]
|
2021-06-20 15:35:51 +00:00
|
|
|
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()
|
2021-06-20 16:45:39 +00:00
|
|
|
if not env.PYPY:
|
|
|
|
assert obj_wr() is None
|
2021-06-20 15:35:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2021-06-20 01:13:27 +00:00
|
|
|
del obj
|
2021-06-20 15:35:51 +00:00
|
|
|
del stash1
|
|
|
|
gc.collect()
|
2021-06-20 16:45:39 +00:00
|
|
|
if not env.PYPY:
|
|
|
|
assert obj_wr() is None
|