pybind11/tests/test_class_sh_shared_ptr_copy_move.py
Ralf W. Grosse-Kunstleve bd8985aa0f
[smart_holder] Introduce PYBIND11_SMART_HOLDER_DISABLE option. (#5348)
* Step 1: Establish new `PYBIND11_SMART_HOLDER_ENABLED` macro, but only under include/pybind11/

At the stage `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` and `PYBIND11_SMART_HOLDER_ENABLED` are still equivalent.

* Systematically replace all `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` with `PYBIND11_SMART_HOLDER_ENABLED` under tests/ and ubench/

As at the previous stage, `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` and `PYBIND11_SMART_HOLDER_ENABLED` are still equivalent.

* Introduce `PYBIND11_SMART_HOLDER_DISABLE` option.

* `#ifdef` out entire `wrap()` function to avoid `unused-parameter` warning-as-error under macos-13

```
/Users/runner/work/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:67:23: error: unused parameter 'm' [-Werror,-Wunused-parameter]
void wrap(py::module_ m, const char *py_class_name) {
                      ^
/Users/runner/work/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:67:38: error: unused parameter 'py_class_name' [-Werror,-Wunused-parameter]
void wrap(py::module_ m, const char *py_class_name) {
                                     ^
2 errors generated.
```
2024-09-01 14:34:36 -07:00

47 lines
1.2 KiB
Python

from __future__ import annotations
import pytest
from pybind11_tests import class_sh_shared_ptr_copy_move as m
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_shptr_copy():
txt = m.test_ShPtr_copy()[0].get_history()
assert txt == "FooShPtr_copy"
def test_smhld_copy():
txt = m.test_SmHld_copy()[0].get_history()
assert txt == "FooSmHld_copy"
def test_shptr_move():
txt = m.test_ShPtr_move()[0].get_history()
assert txt == "FooShPtr_move"
def test_smhld_move():
txt = m.test_SmHld_move()[0].get_history()
assert txt == "FooSmHld_move"
def _check_property(foo_typ, prop_typ, policy):
o = m.Outer()
name = f"{foo_typ}_{prop_typ}_{policy}"
history = f"Foo{foo_typ}_Outer"
f = getattr(o, name)
assert f.get_history() == history
# and try again to check that o did not get changed
f = getattr(o, name)
assert f.get_history() == history
def test_properties():
for prop_typ in ("readonly", "readwrite", "property_readonly"):
for foo_typ in ("ShPtr", "SmHld"):
for policy in ("default", "copy", "move"):
_check_property(foo_typ, prop_typ, policy)