static_assert in from_raw_ptr_take_ownership, to be tested.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-06-01 17:20:31 -07:00 committed by Ralf W. Grosse-Kunstleve
parent 7a74bfede7
commit 24c223ad8e
3 changed files with 12 additions and 5 deletions

View File

@ -235,6 +235,8 @@ struct smart_holder {
template <typename T>
static smart_holder from_raw_ptr_take_ownership(T *raw_ptr) {
static_assert(!std::is_base_of<std::enable_shared_from_this<T>, T>::value,
"Ownership must not be transferred via a raw pointer.");
ensure_pointee_is_destructible<T>("from_raw_ptr_take_ownership");
smart_holder hld;
hld.vptr.reset(raw_ptr, make_guarded_builtin_delete<T>(true));

View File

@ -45,8 +45,8 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(SharedFromThisRef)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SharedFromThisVirt)
TEST_SUBMODULE(class_sh_shared_from_this, m) {
py::classh<MyObject3>(m, "MyObject3")
.def(py::init<int>());
// py::classh<MyObject3>(m, "MyObject3")
// .def(py::init<int>());
m.def("make_myobject3_1", []() { return new MyObject3(8); });
m.def("make_myobject3_2", []() { return std::make_shared<MyObject3>(9); });
m.def("print_myobject3_1", [](const MyObject3 *obj) { py::print(obj->toString()); });
@ -55,7 +55,7 @@ TEST_SUBMODULE(class_sh_shared_from_this, m) {
//m.def("print_myobject3_4", [](const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); });
using B = SharedFromThisRef::B;
py::classh<B>(m, "B");
// py::classh<B>(m, "B");
py::classh<SharedFromThisRef>(m, "SharedFromThisRef")
.def(py::init<>())
.def_readonly("bad_wp", &SharedFromThisRef::value)
@ -70,5 +70,5 @@ TEST_SUBMODULE(class_sh_shared_from_this, m) {
static std::shared_ptr<SharedFromThisVirt> sft(new SharedFromThisVirt());
py::classh<SharedFromThisVirt>(m, "SharedFromThisVirt")
.def_static("get", []() { return sft.get(); });
.def_static("get", []() { return sft.get(); }, py::return_value_policy::reference);
}

View File

@ -6,6 +6,7 @@ from pybind11_tests import ConstructorStats
def test_smart_ptr(capture):
pytest.skip("WIP")
# Object3
for i, o in zip(
[9, 8, 9], [m.MyObject3(9), m.make_myobject3_1(), m.make_myobject3_2()]
@ -31,6 +32,7 @@ def test_smart_ptr(capture):
def test_shared_from_this_ref():
pytest.skip("WIP")
s = m.SharedFromThisRef()
stats = ConstructorStats.get(m.B)
assert stats.alive() == 2
@ -46,6 +48,7 @@ def test_shared_from_this_ref():
def test_shared_from_this_bad_wp():
pytest.skip("WIP")
s = m.SharedFromThisRef()
stats = ConstructorStats.get(m.B)
assert stats.alive() == 2
@ -62,6 +65,7 @@ def test_shared_from_this_bad_wp():
def test_shared_from_this_copy():
pytest.skip("WIP")
s = m.SharedFromThisRef()
stats = ConstructorStats.get(m.B)
assert stats.alive() == 2
@ -76,6 +80,7 @@ def test_shared_from_this_copy():
def test_shared_from_this_holder_ref():
pytest.skip("WIP")
s = m.SharedFromThisRef()
stats = ConstructorStats.get(m.B)
assert stats.alive() == 2
@ -91,6 +96,7 @@ def test_shared_from_this_holder_ref():
def test_shared_from_this_holder_copy():
pytest.skip("WIP")
s = m.SharedFromThisRef()
stats = ConstructorStats.get(m.B)
assert stats.alive() == 2
@ -107,7 +113,6 @@ def test_shared_from_this_holder_copy():
def test_shared_from_this_virt():
pytest.skip("Pre-empting ASAN heap-use-after-free in next line.")
z = m.SharedFromThisVirt.get()
y = m.SharedFromThisVirt.get()
assert y is z