-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test_keep_alive: Show that keep_alive cycle escapes GC
- Loading branch information
1 parent
8a9aec7
commit bd3e1a7
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,12 @@ | ||
#include "pybind11_tests.h" | ||
|
||
class SimpleClass {}; | ||
|
||
TEST_SUBMODULE(keep_alive, m) { | ||
m.def("keep_alive_impl", [](py::handle nurse, py::handle patient) { | ||
py::detail::keep_alive_impl(nurse, patient); | ||
}); | ||
|
||
py::class_<SimpleClass>(m, "SimpleClass") | ||
.def(py::init()); | ||
} |
This file contains 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,29 @@ | ||
# -*- coding: utf-8 -*- | ||
# N.B. This is only focused on CPython, so using gc directly, rather than | ||
# `pytest.gc_collect()`. | ||
import gc | ||
import weakref | ||
|
||
from pybind11_tests import keep_alive as m | ||
|
||
|
||
def test_keep_alive_cycle(): | ||
# See #2761. | ||
o1 = m.SimpleClass() | ||
wr1 = weakref.ref(o1) | ||
o2 = m.SimpleClass() | ||
wr2 = weakref.ref(o2) | ||
assert wr1() is not None | ||
assert wr2() is not None | ||
|
||
# Add a direct cycle. | ||
m.keep_alive_impl(o1, o2) | ||
m.keep_alive_impl(o2, o1) | ||
|
||
del o1 | ||
del o2 | ||
gc.collect() | ||
|
||
# This shows that py::keep_alive will leak objects :( | ||
assert wr1() is not None | ||
assert wr2() is not None |