pybind11/tests/test_class_sh_virtual_py_cpp_mix.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

70 lines
1.5 KiB
Python

from __future__ import annotations
import pytest
from pybind11_tests import class_sh_virtual_py_cpp_mix as m
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
pytest.skip("smart_holder not available.", allow_module_level=True)
class PyBase(m.Base): # Avoiding name PyDerived, for more systematic naming.
def __init__(self):
m.Base.__init__(self)
def get(self):
return 323
class PyCppDerived(m.CppDerived):
def __init__(self):
m.CppDerived.__init__(self)
def get(self):
return 434
@pytest.mark.parametrize(
("ctor", "expected"),
[
(m.Base, 101),
(PyBase, 323),
(m.CppDerivedPlain, 202),
(m.CppDerived, 212),
(PyCppDerived, 434),
],
)
def test_base_get(ctor, expected):
obj = ctor()
assert obj.get() == expected
@pytest.mark.parametrize(
("ctor", "expected"),
[
(m.Base, 4101),
(PyBase, 4323),
(m.CppDerivedPlain, 4202),
(m.CppDerived, 4212),
(PyCppDerived, 4434),
],
)
def test_get_from_cpp_plainc_ptr(ctor, expected):
obj = ctor()
assert m.get_from_cpp_plainc_ptr(obj) == expected
@pytest.mark.parametrize(
("ctor", "expected"),
[
(m.Base, 5101),
(PyBase, 5323),
(m.CppDerivedPlain, 5202),
(m.CppDerived, 5212),
(PyCppDerived, 5434),
],
)
def test_get_from_cpp_unique_ptr(ctor, expected):
obj = ctor()
assert m.get_from_cpp_unique_ptr(obj) == expected