Skip to content

Commit 4620807

Browse files
committed
SH, update shared_ptr copy tests
1 parent 51dd458 commit 4620807

File tree

5 files changed

+63
-29
lines changed

5 files changed

+63
-29
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ set(PYBIND11_TEST_FILES
106106
test_class_sh_disowning_mi.cpp
107107
test_class_sh_factory_constructors.cpp
108108
test_class_sh_inheritance.cpp
109+
test_class_sh_shared_ptr_copy.cpp
109110
test_class_sh_trampoline_basic.cpp
110111
test_class_sh_trampoline_self_life_support.cpp
111112
test_class_sh_trampoline_shared_ptr_cpp_arg.cpp
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "pybind11_tests.h"
2+
3+
#include <pybind11/smart_holder.h>
4+
5+
#include <memory>
6+
#include <string>
7+
#include <vector>
8+
9+
namespace pybind11_tests {
10+
namespace class_sh_shared_ptr_copy {
11+
12+
template<int SerNo>
13+
struct Foo {
14+
std::string mtxt;
15+
Foo() : mtxt("DefaultConstructor") {}
16+
Foo(const std::string &mtxt_) : mtxt(mtxt_) {}
17+
Foo(const Foo &other) { mtxt = other.mtxt + "_CpCtor"; }
18+
Foo(Foo &&other) { mtxt = other.mtxt + "_MvCtor"; }
19+
};
20+
21+
using FooAVL = Foo<0>;
22+
using FooDEF = Foo<1>;
23+
24+
} // namespace class_sh_shared_ptr_copy
25+
} // namespace pybind11_tests
26+
27+
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_shared_ptr_copy::FooAVL)
28+
29+
namespace pybind11_tests {
30+
namespace class_sh_shared_ptr_copy {
31+
32+
TEST_SUBMODULE(class_sh_shared_ptr_copy, m) {
33+
namespace py = pybind11;
34+
35+
py::class_<FooAVL, PYBIND11_SH_AVL(FooAVL)>(m, "FooAVL");
36+
py::class_<FooDEF, std::shared_ptr<FooDEF>>(m, "FooDEF");
37+
38+
m.def("test_avl", []() {
39+
auto o = std::make_shared<FooAVL>("AVL");
40+
auto l = py::list();
41+
l.append(o);
42+
});
43+
m.def("test_def", []() {
44+
auto o = std::make_shared<FooDEF>("DEF");
45+
auto l = py::list();
46+
l.append(o);
47+
});
48+
}
49+
50+
} // namespace class_sh_shared_ptr_copy
51+
} // namespace pybind11_tests
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from pybind11_tests import class_sh_shared_ptr_copy as m
4+
5+
6+
def test_avl():
7+
m.test_avl()
8+
9+
10+
def test_def():
11+
m.test_def()

tests/test_smart_ptr.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,6 @@ class MyObject3 : public std::enable_shared_from_this<MyObject3> {
9898
int value;
9999
};
100100

101-
// an uncopyable object managed by a std::shared_ptr<>
102-
class MyObject3a {
103-
public:
104-
MyObject3a(int value) : value(value) { print_created(this, toString()); }
105-
std::string toString() const { return "MyObject3a[" + std::to_string(value) + "]"; }
106-
virtual ~MyObject3a() { print_destroyed(this); }
107-
private:
108-
int value;
109-
};
110-
111101
// test_unique_nodelete
112102
// Object with a private destructor
113103
class MyObject4;
@@ -363,15 +353,6 @@ TEST_SUBMODULE(smart_ptr, m) {
363353
m.def("print_myobject3_3", [](const std::shared_ptr<MyObject3> &obj) { py::print(obj->toString()); });
364354
m.def("print_myobject3_4", [](const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); });
365355

366-
py::class_<MyObject3a>(m, "MyObject3a");
367-
m.def("make_myobject3_1", []() { return new MyObject3a(8); });
368-
m.def("make_myobject3_2", []() { return std::make_shared<MyObject3a>(9); });
369-
m.def("print_myobject3a_1", [](const MyObject3a *obj) { py::print(obj->toString()); });
370-
m.def("print_myobject3a_2", [](std::shared_ptr<MyObject3a> obj) { py::print(obj->toString()); });
371-
m.def("print_myobject3a_3", [](const std::shared_ptr<MyObject3a> &obj) { py::print(obj->toString()); });
372-
// this doesn't compile, should it?
373-
//m.def("print_myobject3a_4", [](const std::shared_ptr<MyObject3a> *obj) { py::print((*obj)->toString()); });
374-
375356
// test_smart_ptr_refcounting
376357
m.def("test_object1_refcounting", []() {
377358
ref<MyObject1> o = new MyObject1(0);
@@ -481,10 +462,4 @@ TEST_SUBMODULE(smart_ptr, m) {
481462
list.append(py::cast(e));
482463
return list;
483464
});
484-
485-
m.def("test_3011_shared_ptr", []() {
486-
auto o = std::make_shared<MyObject3a>(42);
487-
auto l = py::list();
488-
l.append(o);
489-
});
490465
}

tests/test_smart_ptr.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,3 @@ 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)