summaryrefslogtreecommitdiff
path: root/ext/pybind11/tests/test_smart_ptr.py
diff options
context:
space:
mode:
Diffstat (limited to 'ext/pybind11/tests/test_smart_ptr.py')
-rw-r--r--ext/pybind11/tests/test_smart_ptr.py136
1 files changed, 67 insertions, 69 deletions
diff --git a/ext/pybind11/tests/test_smart_ptr.py b/ext/pybind11/tests/test_smart_ptr.py
index b5af3bd38..4dfe0036f 100644
--- a/ext/pybind11/tests/test_smart_ptr.py
+++ b/ext/pybind11/tests/test_smart_ptr.py
@@ -1,40 +1,35 @@
import pytest
+from pybind11_tests import smart_ptr as m
from pybind11_tests import ConstructorStats
def test_smart_ptr(capture):
# Object1
- from pybind11_tests import (MyObject1, make_object_1, make_object_2,
- print_object_1, print_object_2, print_object_3, print_object_4)
-
- for i, o in enumerate([make_object_1(), make_object_2(), MyObject1(3)], start=1):
+ for i, o in enumerate([m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1):
assert o.getRefCount() == 1
with capture:
- print_object_1(o)
- print_object_2(o)
- print_object_3(o)
- print_object_4(o)
+ m.print_object_1(o)
+ m.print_object_2(o)
+ m.print_object_3(o)
+ m.print_object_4(o)
assert capture == "MyObject1[{i}]\n".format(i=i) * 4
- from pybind11_tests import (make_myobject1_1, make_myobject1_2,
- print_myobject1_1, print_myobject1_2,
- print_myobject1_3, print_myobject1_4)
-
- for i, o in enumerate([make_myobject1_1(), make_myobject1_2(), MyObject1(6), 7], start=4):
+ for i, o in enumerate([m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7],
+ start=4):
print(o)
with capture:
if not isinstance(o, int):
- print_object_1(o)
- print_object_2(o)
- print_object_3(o)
- print_object_4(o)
- print_myobject1_1(o)
- print_myobject1_2(o)
- print_myobject1_3(o)
- print_myobject1_4(o)
+ m.print_object_1(o)
+ m.print_object_2(o)
+ m.print_object_3(o)
+ m.print_object_4(o)
+ m.print_myobject1_1(o)
+ m.print_myobject1_2(o)
+ m.print_myobject1_3(o)
+ m.print_myobject1_4(o)
assert capture == "MyObject1[{i}]\n".format(i=i) * (4 if isinstance(o, int) else 8)
- cstats = ConstructorStats.get(MyObject1)
+ cstats = ConstructorStats.get(m.MyObject1)
assert cstats.alive() == 0
expected_values = ['MyObject1[{}]'.format(i) for i in range(1, 7)] + ['MyObject1[7]'] * 4
assert cstats.values() == expected_values
@@ -45,21 +40,16 @@ def test_smart_ptr(capture):
assert cstats.move_assignments == 0
# Object2
- from pybind11_tests import (MyObject2, make_myobject2_1, make_myobject2_2,
- make_myobject3_1, make_myobject3_2,
- print_myobject2_1, print_myobject2_2,
- print_myobject2_3, print_myobject2_4)
-
- for i, o in zip([8, 6, 7], [MyObject2(8), make_myobject2_1(), make_myobject2_2()]):
+ for i, o in zip([8, 6, 7], [m.MyObject2(8), m.make_myobject2_1(), m.make_myobject2_2()]):
print(o)
with capture:
- print_myobject2_1(o)
- print_myobject2_2(o)
- print_myobject2_3(o)
- print_myobject2_4(o)
+ m.print_myobject2_1(o)
+ m.print_myobject2_2(o)
+ m.print_myobject2_3(o)
+ m.print_myobject2_4(o)
assert capture == "MyObject2[{i}]\n".format(i=i) * 4
- cstats = ConstructorStats.get(MyObject2)
+ cstats = ConstructorStats.get(m.MyObject2)
assert cstats.alive() == 1
o = None
assert cstats.alive() == 0
@@ -71,19 +61,16 @@ def test_smart_ptr(capture):
assert cstats.move_assignments == 0
# Object3
- from pybind11_tests import (MyObject3, print_myobject3_1, print_myobject3_2,
- print_myobject3_3, print_myobject3_4)
-
- for i, o in zip([9, 8, 9], [MyObject3(9), make_myobject3_1(), make_myobject3_2()]):
+ for i, o in zip([9, 8, 9], [m.MyObject3(9), m.make_myobject3_1(), m.make_myobject3_2()]):
print(o)
with capture:
- print_myobject3_1(o)
- print_myobject3_2(o)
- print_myobject3_3(o)
- print_myobject3_4(o)
+ m.print_myobject3_1(o)
+ m.print_myobject3_2(o)
+ m.print_myobject3_3(o)
+ m.print_myobject3_4(o)
assert capture == "MyObject3[{i}]\n".format(i=i) * 4
- cstats = ConstructorStats.get(MyObject3)
+ cstats = ConstructorStats.get(m.MyObject3)
assert cstats.alive() == 1
o = None
assert cstats.alive() == 0
@@ -94,10 +81,8 @@ def test_smart_ptr(capture):
assert cstats.copy_assignments == 0
assert cstats.move_assignments == 0
- # Object and ref
- from pybind11_tests import Object, cstats_ref
-
- cstats = ConstructorStats.get(Object)
+ # Object
+ cstats = ConstructorStats.get(m.Object)
assert cstats.alive() == 0
assert cstats.values() == []
assert cstats.default_constructions == 10
@@ -106,7 +91,8 @@ def test_smart_ptr(capture):
assert cstats.copy_assignments == 0
assert cstats.move_assignments == 0
- cstats = cstats_ref()
+ # ref<>
+ cstats = m.cstats_ref()
assert cstats.alive() == 0
assert cstats.values() == ['from pointer'] * 10
assert cstats.default_constructions == 30
@@ -117,26 +103,30 @@ def test_smart_ptr(capture):
def test_smart_ptr_refcounting():
- from pybind11_tests import test_object1_refcounting
- assert test_object1_refcounting()
+ assert m.test_object1_refcounting()
def test_unique_nodelete():
- from pybind11_tests import MyObject4
- o = MyObject4(23)
+ o = m.MyObject4(23)
assert o.value == 23
- cstats = ConstructorStats.get(MyObject4)
+ cstats = ConstructorStats.get(m.MyObject4)
assert cstats.alive() == 1
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
+def test_large_holder():
+ o = m.MyObject5(5)
+ assert o.value == 5
+ cstats = ConstructorStats.get(m.MyObject5)
+ assert cstats.alive() == 1
+ del o
+ assert cstats.alive() == 0
- s = SharedPtrRef()
- stats = ConstructorStats.get(A)
+
+def test_shared_ptr_and_references():
+ s = m.SharedPtrRef()
+ stats = ConstructorStats.get(m.A)
assert stats.alive() == 2
ref = s.ref # init_holder_helper(holder_ptr=false, owned=false)
@@ -166,10 +156,8 @@ def test_shared_ptr_and_references():
def test_shared_ptr_from_this_and_references():
- from pybind11_tests.smart_ptr import SharedFromThisRef, B
-
- s = SharedFromThisRef()
- stats = ConstructorStats.get(B)
+ s = m.SharedFromThisRef()
+ stats = ConstructorStats.get(m.B)
assert stats.alive() == 2
ref = s.ref # init_holder_helper(holder_ptr=false, owned=false, bad_wp=false)
@@ -202,21 +190,31 @@ def test_shared_ptr_from_this_and_references():
del ref, bad_wp, copy, holder_ref, holder_copy, s
assert stats.alive() == 0
+ z = m.SharedFromThisVirt.get()
+ y = m.SharedFromThisVirt.get()
+ assert y is z
-def test_move_only_holder():
- from pybind11_tests.smart_ptr import TypeWithMoveOnlyHolder
- a = TypeWithMoveOnlyHolder.make()
- stats = ConstructorStats.get(TypeWithMoveOnlyHolder)
+def test_move_only_holder():
+ a = m.TypeWithMoveOnlyHolder.make()
+ stats = ConstructorStats.get(m.TypeWithMoveOnlyHolder)
assert stats.alive() == 1
del a
assert stats.alive() == 0
def test_smart_ptr_from_default():
- from pybind11_tests.smart_ptr import HeldByDefaultHolder
-
- instance = HeldByDefaultHolder()
+ instance = m.HeldByDefaultHolder()
with pytest.raises(RuntimeError) as excinfo:
- HeldByDefaultHolder.load_shared_ptr(instance)
+ m.HeldByDefaultHolder.load_shared_ptr(instance)
assert "Unable to load a custom holder type from a default-holder instance" in str(excinfo)
+
+
+def test_shared_ptr_gc():
+ """#187: issue involving std::shared_ptr<> return value policy & garbage collection"""
+ el = m.ElementList()
+ for i in range(10):
+ el.add(m.ElementA(i))
+ pytest.gc_collect()
+ for i, v in enumerate(el.get()):
+ assert i == v.value()