mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Make unique_ptr's with non-default deleters work
Currently pybind11 only supports std::unique_ptr<T> holders by default (other holders can, of course, be declared using the macro). PR #368 added a `py::nodelete` that is intended to be used as: py::class_<Type, std::unique_ptr<Type, py::nodelete>> c("Type"); but this doesn't work out of the box. (You could add an explicit holder type declaration, but this doesn't appear to have been the intention of the commit). This commit fixes it by generalizing the unique_ptr type_caster to take both the type and deleter as template arguments, so that *any* unique_ptr instances are now automatically handled by pybind. It also adds a test to test_smart_ptr, testing both that py::nodelete (now) works, and that the object is indeed not deleted as intended.
This commit is contained in:
@@ -113,3 +113,13 @@ def test_smart_ptr(capture):
|
||||
# assert cstats.move_constructions >= 0 # Doesn't invoke any
|
||||
assert cstats.copy_assignments == 30
|
||||
assert cstats.move_assignments == 0
|
||||
|
||||
def test_unique_nodelete(capture):
|
||||
from pybind11_tests import MyObject4
|
||||
o = MyObject4(23)
|
||||
assert o.value == 23
|
||||
cstats = ConstructorStats.get(MyObject4)
|
||||
assert cstats.alive() == 1
|
||||
del o
|
||||
cstats = ConstructorStats.get(MyObject4)
|
||||
assert cstats.alive() == 1 # Leak, but that's intentional
|
||||
|
||||
Reference in New Issue
Block a user