Skip to content

Commit 27d786e

Browse files
committed
Add test_private_esft_tolerance() in test_smart_ptr.cpp,py
1 parent e7e5d6e commit 27d786e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

tests/test_smart_ptr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,13 @@ TEST_SUBMODULE(smart_ptr, m) {
473473
}
474474
return list;
475475
});
476+
477+
class PrivateESFT : /* implicit private */ std::enable_shared_from_this<PrivateESFT> {};
478+
struct ContainerUsingPrivateESFT {
479+
std::shared_ptr<PrivateESFT> ptr;
480+
};
481+
py::class_<ContainerUsingPrivateESFT>(m, "ContainerUsingPrivateESFT")
482+
.def(py::init<>())
483+
.def_readwrite("ptr",
484+
&ContainerUsingPrivateESFT::ptr); // <- access ESFT through shared_ptr
476485
}

tests/test_smart_ptr.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,15 @@ def test_shared_ptr_gc():
326326
pytest.gc_collect()
327327
for i, v in enumerate(el.get()):
328328
assert i == v.value()
329+
330+
331+
def test_private_esft_tolerance():
332+
# Regression test: binding a shared_ptr<T> member where T privately inherits
333+
# enable_shared_from_this<T> must not cause a C++ compile error.
334+
c = m.ContainerUsingPrivateESFT()
335+
# The ptr member is not actually usable in any way, but this is how the
336+
# pybind11 v2 release series worked.
337+
with pytest.raises(TypeError):
338+
_ = c.ptr # getattr
339+
with pytest.raises(TypeError):
340+
c.ptr = None # setattr

0 commit comments

Comments
 (0)