test_class_sh_unique_ptr_member.py 825 Bytes
Newer Older
1
2
3
# -*- coding: utf-8 -*-
import pytest

4
from pybind11_tests import class_sh_unique_ptr_member as m
5
6


7
8
9
def test_make_unique_pointee():
    obj = m.make_unique_pointee()
    assert obj.get_int() == 213
10
11


12
13
14
15
16
@pytest.mark.parametrize(
    "give_up_ownership_via",
    ["give_up_ownership_via_unique_ptr", "give_up_ownership_via_shared_ptr"],
)
def test_pointee_and_ptr_owner(give_up_ownership_via):
17
18
    obj = m.pointee()
    assert obj.get_int() == 213
19
20
    owner = m.ptr_owner(obj)
    with pytest.raises(RuntimeError) as exc_info:
21
        obj.get_int()
22
23
    assert (
        str(exc_info.value)
24
        == "Missing value for wrapped C++ type: Python instance was disowned."
25
    )
26
    assert owner.is_owner()
27
    reclaimed = getattr(owner, give_up_ownership_via)()
28
    assert not owner.is_owner()
29
    assert reclaimed.get_int() == 213