Skip to content

Commit f786790

Browse files
committed
added testcase
1 parent b9979b6 commit f786790

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

tests/test_smart_ptr.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ test_initializer smart_ptr([](py::module &m) {
129129
py::class_<MyObject1, ref<MyObject1>>(m, "MyObject1", obj)
130130
.def(py::init<int>());
131131

132+
m.def("test_object1_refcounting",
133+
[]() -> bool {
134+
ref<MyObject1> o = new MyObject1(0);
135+
bool good = o->getRefCount() == 1;
136+
py::object o2 = py::cast(o, py::return_value_policy::reference);
137+
// always request (partial) ownership for objects with intrusive
138+
// reference counting even when using the 'reference' RVP
139+
good &= o->getRefCount() == 2;
140+
return good;
141+
}
142+
);
143+
132144
m.def("make_object_1", &make_object_1);
133145
m.def("make_object_2", &make_object_2);
134146
m.def("make_myobject1_1", &make_myobject1_1);

tests/test_smart_ptr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ def test_smart_ptr(capture):
116116
assert cstats.move_assignments == 0
117117

118118

119+
def test_smart_ptr_refcounting():
120+
from pybind11_tests import test_object1_refcounting
121+
assert test_object1_refcounting()
122+
123+
119124
def test_unique_nodelete():
120125
from pybind11_tests import MyObject4
121126
o = MyObject4(23)

0 commit comments

Comments
 (0)