|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pybind11_tests.class_sh_trampoline_shared_ptr_cpp_arg as m |
| 5 | + |
| 6 | + |
| 7 | +def test_shared_ptr_cpp_arg(): |
| 8 | + import weakref |
| 9 | + |
| 10 | + class PyChild(m.SpBase): |
| 11 | + def is_base_used(self): |
| 12 | + return False |
| 13 | + |
| 14 | + tester = m.SpBaseTester() |
| 15 | + |
| 16 | + obj = PyChild() |
| 17 | + objref = weakref.ref(obj) |
| 18 | + |
| 19 | + # Pass the last python reference to the C++ function |
| 20 | + tester.set_object(obj) |
| 21 | + del obj |
| 22 | + pytest.gc_collect() |
| 23 | + |
| 24 | + # python reference is still around since C++ has it now |
| 25 | + assert objref() is not None |
| 26 | + assert tester.is_base_used() is False |
| 27 | + assert tester.obj.is_base_used() is False |
| 28 | + assert tester.get_object() is objref() |
| 29 | + |
| 30 | + |
| 31 | +def test_shared_ptr_cpp_prop(): |
| 32 | + class PyChild(m.SpBase): |
| 33 | + def is_base_used(self): |
| 34 | + return False |
| 35 | + |
| 36 | + tester = m.SpBaseTester() |
| 37 | + |
| 38 | + # Set the last python reference as a property of the C++ object |
| 39 | + tester.obj = PyChild() |
| 40 | + pytest.gc_collect() |
| 41 | + |
| 42 | + # python reference is still around since C++ has it now |
| 43 | + assert tester.is_base_used() is False |
| 44 | + assert tester.has_python_instance() is True |
| 45 | + assert tester.obj.is_base_used() is False |
| 46 | + assert tester.obj.has_python_instance() is True |
| 47 | + |
| 48 | + |
| 49 | +def test_shared_ptr_arg_identity(): |
| 50 | + import weakref |
| 51 | + |
| 52 | + tester = m.SpBaseTester() |
| 53 | + |
| 54 | + obj = m.SpBase() |
| 55 | + objref = weakref.ref(obj) |
| 56 | + |
| 57 | + tester.set_object(obj) |
| 58 | + del obj |
| 59 | + pytest.gc_collect() |
| 60 | + |
| 61 | + # python reference is still around since C++ has it |
| 62 | + assert objref() is not None |
| 63 | + assert tester.get_object() is objref() |
| 64 | + assert tester.has_python_instance() is True |
| 65 | + |
| 66 | + # python reference disappears once the C++ object releases it |
| 67 | + tester.set_object(None) |
| 68 | + pytest.gc_collect() |
| 69 | + assert objref() is None |
| 70 | + |
| 71 | + |
| 72 | +def test_shared_ptr_alias_nonpython(): |
| 73 | + tester = m.SpBaseTester() |
| 74 | + |
| 75 | + # C++ creates the object, a python instance shouldn't exist |
| 76 | + tester.set_nonpython_instance() |
| 77 | + assert tester.is_base_used() is True |
| 78 | + assert tester.has_instance() is True |
| 79 | + assert tester.has_python_instance() is False |
| 80 | + |
| 81 | + # Now a python instance exists |
| 82 | + cobj = tester.get_object() |
| 83 | + assert cobj.has_python_instance() |
| 84 | + assert tester.has_instance() is True |
| 85 | + assert tester.has_python_instance() is True |
| 86 | + |
| 87 | + # Now it's gone |
| 88 | + del cobj |
| 89 | + pytest.gc_collect() |
| 90 | + assert tester.has_instance() is True |
| 91 | + assert tester.has_python_instance() is False |
| 92 | + |
| 93 | + # SMART_HOLDER_WIP: the behavior below is DIFFERENT from PR #2839 |
| 94 | + # When we pass it as an arg to a new tester the python instance should |
| 95 | + # disappear because it wasn't created with an alias |
| 96 | + new_tester = m.SpBaseTester() |
| 97 | + |
| 98 | + cobj = tester.get_object() |
| 99 | + assert cobj.has_python_instance() |
| 100 | + |
| 101 | + new_tester.set_object(cobj) |
| 102 | + assert tester.has_python_instance() is True |
| 103 | + assert new_tester.has_python_instance() is True |
| 104 | + |
| 105 | + del cobj |
| 106 | + pytest.gc_collect() |
| 107 | + |
| 108 | + # Gone! |
| 109 | + assert tester.has_instance() is True |
| 110 | + assert tester.has_python_instance() is True # False in PR #2839 |
| 111 | + assert new_tester.has_instance() is True |
| 112 | + assert new_tester.has_python_instance() is True # False in PR #2839 |
| 113 | + |
| 114 | + |
| 115 | +def test_shared_ptr_goaway(): |
| 116 | + import weakref |
| 117 | + |
| 118 | + tester = m.SpGoAwayTester() |
| 119 | + |
| 120 | + obj = m.SpGoAway() |
| 121 | + objref = weakref.ref(obj) |
| 122 | + |
| 123 | + assert tester.obj is None |
| 124 | + |
| 125 | + tester.obj = obj |
| 126 | + del obj |
| 127 | + pytest.gc_collect() |
| 128 | + |
| 129 | + # python reference is no longer around |
| 130 | + assert objref() is None |
| 131 | + # C++ reference is still around |
| 132 | + assert tester.obj is not None |
| 133 | + |
| 134 | + |
| 135 | +def test_infinite(): |
| 136 | + tester = m.SpBaseTester() |
| 137 | + while True: |
| 138 | + tester.set_object(m.SpBase()) |
| 139 | + return # Comment out for manual leak checking (use `top` command). |
0 commit comments