pybind11/tests/test_class_sh_property_non_owning.py
Ralf W. Grosse-Kunstleve 37d0bf4289
[smart_holder] Enable properties for non-owning holders (#4586)
* Add test_class_sh_property_non_owning.cpp,py

Failing:

```
__________________________________________________________ test_persistent_holder __________________________________________________________

    def test_persistent_holder():
        h = m.DataFieldsHolder(2)
>       c = h.vec_at(0).core_fld
E       RuntimeError: Non-owning holder (loaded_as_shared_ptr).

h          = <pybind11_tests.class_sh_property_non_owning.DataFieldsHolder object at 0x7fabab516470>

test_class_sh_property_non_owning.py:6: RuntimeError
__________________________________________________________ test_temporary_holder ___________________________________________________________

    def test_temporary_holder():
        d = m.DataFieldsHolder(2).vec_at(1)
>       c = d.core_fld
E       RuntimeError: Non-owning holder (loaded_as_shared_ptr).

d          = <pybind11_tests.class_sh_property_non_owning.DataField object at 0x7fabab548770>

test_class_sh_property_non_owning.py:13: RuntimeError
```

* Introduce `shared_ptr_from_python(responsible_parent)` and use in all `property_cpp_function`s with `const shared_ptr<T> &` arguments.

Tests are incomplete.

* Complete tests.

* Add comment for `smart_holder_type_caster_load<T>::shared_ptr_from_python`
2023-03-23 17:21:41 -07:00

29 lines
927 B
Python

import pytest
from pybind11_tests import class_sh_property_non_owning as m
@pytest.mark.parametrize("persistent_holder", [True, False])
@pytest.mark.parametrize(
("core_fld", "expected"),
[
("core_fld_value_ro", (13, 24)),
("core_fld_value_rw", (13, 24)),
("core_fld_shared_ptr_ro", (14, 25)),
("core_fld_shared_ptr_rw", (14, 25)),
("core_fld_raw_ptr_ro", (14, 25)),
("core_fld_raw_ptr_rw", (14, 25)),
("core_fld_unique_ptr_rw", (15, 26)),
],
)
def test_core_fld_common(core_fld, expected, persistent_holder):
if persistent_holder:
h = m.DataFieldsHolder(2)
for i, exp in enumerate(expected):
c = getattr(h.vec_at(i), core_fld)
assert c.int_value == exp
else:
for i, exp in enumerate(expected):
c = getattr(m.DataFieldsHolder(2).vec_at(i), core_fld)
assert c.int_value == exp