-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Attach python lifetime to shared_ptr passed to C++ #2839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) 2021 The Pybind Development Team. | ||
// All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
#include "pybind11_tests.h" | ||
|
||
namespace { | ||
|
||
// For testing whether a python subclass of a C++ object dies when the | ||
// last python reference is lost | ||
struct SpBase { | ||
// returns true if the base virtual function is called | ||
virtual bool is_base_used() { return true; } | ||
|
||
// returns true if there's an associated python instance | ||
bool has_python_instance() { | ||
auto tinfo = py::detail::get_type_info(typeid(SpBase)); | ||
return (bool)py::detail::get_object_handle(this, tinfo); | ||
} | ||
|
||
SpBase() = default; | ||
SpBase(const SpBase &) = delete; | ||
virtual ~SpBase() = default; | ||
}; | ||
|
||
struct PySpBase : SpBase { | ||
bool is_base_used() override { PYBIND11_OVERRIDE(bool, SpBase, is_base_used); } | ||
}; | ||
|
||
struct SpBaseTester { | ||
std::shared_ptr<SpBase> get_object() const { return m_obj; } | ||
void set_object(std::shared_ptr<SpBase> obj) { m_obj = std::move(obj); } | ||
bool is_base_used() { return m_obj->is_base_used(); } | ||
bool has_instance() { return (bool)m_obj; } | ||
bool has_python_instance() { return m_obj && m_obj->has_python_instance(); } | ||
void set_nonpython_instance() { | ||
m_obj = std::make_shared<SpBase>(); | ||
} | ||
std::shared_ptr<SpBase> m_obj; | ||
}; | ||
|
||
// For testing that a C++ class without an alias does not retain the python | ||
// portion of the object | ||
struct SpGoAway {}; | ||
|
||
struct SpGoAwayTester { | ||
std::shared_ptr<SpGoAway> m_obj; | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST_SUBMODULE(trampoline_shared_ptr_cpp_arg, m) { | ||
// For testing whether a python subclass of a C++ object dies when the | ||
// last python reference is lost | ||
|
||
py::class_<SpBase, std::shared_ptr<SpBase>, PySpBase>(m, "SpBase") | ||
.def(py::init<>()) | ||
.def("is_base_used", &SpBase::is_base_used) | ||
.def("has_python_instance", &SpBase::has_python_instance); | ||
|
||
py::class_<SpBaseTester>(m, "SpBaseTester") | ||
.def(py::init<>()) | ||
.def("get_object", &SpBaseTester::get_object) | ||
.def("set_object", &SpBaseTester::set_object) | ||
.def("is_base_used", &SpBaseTester::is_base_used) | ||
.def("has_instance", &SpBaseTester::has_instance) | ||
.def("has_python_instance", &SpBaseTester::has_python_instance) | ||
.def("set_nonpython_instance", &SpBaseTester::set_nonpython_instance) | ||
.def_readwrite("obj", &SpBaseTester::m_obj); | ||
|
||
// For testing that a C++ class without an alias does not retain the python | ||
// portion of the object | ||
|
||
py::class_<SpGoAway, std::shared_ptr<SpGoAway>>(m, "SpGoAway").def(py::init<>()); | ||
|
||
py::class_<SpGoAwayTester>(m, "SpGoAwayTester") | ||
.def(py::init<>()) | ||
.def_readwrite("obj", &SpGoAwayTester::m_obj); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
import pybind11_tests.trampoline_shared_ptr_cpp_arg as m | ||
|
||
|
||
def test_shared_ptr_cpp_arg(): | ||
import weakref | ||
|
||
class PyChild(m.SpBase): | ||
def is_base_used(self): | ||
return False | ||
|
||
tester = m.SpBaseTester() | ||
|
||
obj = PyChild() | ||
objref = weakref.ref(obj) | ||
|
||
# Pass the last python reference to the C++ function | ||
tester.set_object(obj) | ||
del obj | ||
pytest.gc_collect() | ||
|
||
# python reference is still around since C++ has it now | ||
assert objref() is not None | ||
assert tester.is_base_used() is False | ||
assert tester.obj.is_base_used() is False | ||
assert tester.get_object() is objref() | ||
|
||
|
||
def test_shared_ptr_cpp_prop(): | ||
class PyChild(m.SpBase): | ||
def is_base_used(self): | ||
return False | ||
|
||
tester = m.SpBaseTester() | ||
|
||
# Set the last python reference as a property of the C++ object | ||
tester.obj = PyChild() | ||
pytest.gc_collect() | ||
|
||
# python reference is still around since C++ has it now | ||
assert tester.is_base_used() is False | ||
assert tester.has_python_instance() is True | ||
assert tester.obj.is_base_used() is False | ||
assert tester.obj.has_python_instance() is True | ||
|
||
|
||
def test_shared_ptr_arg_identity(): | ||
import weakref | ||
|
||
tester = m.SpBaseTester() | ||
|
||
obj = m.SpBase() | ||
objref = weakref.ref(obj) | ||
|
||
tester.set_object(obj) | ||
del obj | ||
pytest.gc_collect() | ||
|
||
# python reference is still around since C++ has it | ||
assert objref() is not None | ||
assert tester.get_object() is objref() | ||
assert tester.has_python_instance() is True | ||
|
||
# python reference disappears once the C++ object releases it | ||
tester.set_object(None) | ||
pytest.gc_collect() | ||
assert objref() is None | ||
|
||
|
||
def test_shared_ptr_alias_nonpython(): | ||
tester = m.SpBaseTester() | ||
|
||
# C++ creates the object, a python instance shouldn't exist | ||
tester.set_nonpython_instance() | ||
assert tester.is_base_used() is True | ||
assert tester.has_instance() is True | ||
assert tester.has_python_instance() is False | ||
|
||
# Now a python instance exists | ||
cobj = tester.get_object() | ||
assert cobj.has_python_instance() | ||
assert tester.has_instance() is True | ||
assert tester.has_python_instance() is True | ||
|
||
# Now it's gone | ||
del cobj | ||
pytest.gc_collect() | ||
assert tester.has_instance() is True | ||
assert tester.has_python_instance() is False | ||
|
||
# When we pass it as an arg to a new tester the python instance should | ||
# disappear because it wasn't created with an alias | ||
new_tester = m.SpBaseTester() | ||
|
||
cobj = tester.get_object() | ||
assert cobj.has_python_instance() | ||
|
||
new_tester.set_object(cobj) | ||
assert tester.has_python_instance() is True | ||
assert new_tester.has_python_instance() is True | ||
|
||
del cobj | ||
pytest.gc_collect() | ||
|
||
# Gone! | ||
assert tester.has_instance() is True | ||
assert tester.has_python_instance() is False | ||
assert new_tester.has_instance() is True | ||
assert new_tester.has_python_instance() is False | ||
|
||
|
||
def test_shared_ptr_goaway(): | ||
import weakref | ||
|
||
tester = m.SpGoAwayTester() | ||
|
||
obj = m.SpGoAway() | ||
objref = weakref.ref(obj) | ||
|
||
assert tester.obj is None | ||
|
||
tester.obj = obj | ||
del obj | ||
pytest.gc_collect() | ||
|
||
# python reference is no longer around | ||
assert objref() is None | ||
# C++ reference is still around | ||
assert tester.obj is not None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.