Skip to content

Commit 1ca44cb

Browse files
committed
Initial test
1 parent 8f6ca71 commit 1ca44cb

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

tests/test_smart_ptr.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
BSD-style license that can be found in the LICENSE file.
99
*/
1010

11+
#define PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
12+
1113
#if defined(_MSC_VER) && _MSC_VER < 1910 // VS 2015's MSVC
1214
# pragma warning(disable: 4702) // unreachable code in system header (xatomic.h(382))
1315
#endif
@@ -98,6 +100,16 @@ class MyObject3 : public std::enable_shared_from_this<MyObject3> {
98100
int value;
99101
};
100102

103+
// an uncopyable object managed by a std::shared_ptr<>
104+
class MyObject3a {
105+
public:
106+
MyObject3a(int value) : value(value) { print_created(this, toString()); }
107+
std::string toString() const { return "MyObject3a[" + std::to_string(value) + "]"; }
108+
virtual ~MyObject3a() { print_destroyed(this); }
109+
private:
110+
int value;
111+
};
112+
101113
// test_unique_nodelete
102114
// Object with a private destructor
103115
class MyObject4;
@@ -353,6 +365,15 @@ TEST_SUBMODULE(smart_ptr, m) {
353365
m.def("print_myobject3_3", [](const std::shared_ptr<MyObject3> &obj) { py::print(obj->toString()); });
354366
m.def("print_myobject3_4", [](const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); });
355367

368+
py::class_<MyObject3a>(m, "MyObject3a");
369+
m.def("make_myobject3_1", []() { return new MyObject3a(8); });
370+
m.def("make_myobject3_2", []() { return std::make_shared<MyObject3a>(9); });
371+
m.def("print_myobject3a_1", [](const MyObject3a *obj) { py::print(obj->toString()); });
372+
m.def("print_myobject3a_2", [](std::shared_ptr<MyObject3a> obj) { py::print(obj->toString()); });
373+
m.def("print_myobject3a_3", [](const std::shared_ptr<MyObject3a> &obj) { py::print(obj->toString()); });
374+
// this doesn't compile, should it?
375+
//m.def("print_myobject3a_4", [](const std::shared_ptr<MyObject3a> *obj) { py::print((*obj)->toString()); });
376+
356377
// test_smart_ptr_refcounting
357378
m.def("test_object1_refcounting", []() {
358379
ref<MyObject1> o = new MyObject1(0);
@@ -462,4 +483,10 @@ TEST_SUBMODULE(smart_ptr, m) {
462483
list.append(py::cast(e));
463484
return list;
464485
});
486+
487+
m.def("test_3011_shared_ptr", []() {
488+
auto o = std::make_shared<MyObject3a>(42);
489+
auto l = py::list();
490+
l.append(o);
491+
});
465492
}

tests/test_smart_ptr.py

+4
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,7 @@ def test_shared_ptr_gc():
316316
pytest.gc_collect()
317317
for i, v in enumerate(el.get()):
318318
assert i == v.value()
319+
320+
321+
def test_3011_shared_ptr():
322+
m.test_3011_shared_ptr()

0 commit comments

Comments
 (0)