pybind11/tests/test_class_sh_void_ptr_capsule.py
Xiaofei Wang 9d4b4dffce
Fixes issue #3788 (#3796)
* Reproducer for https://github.com/pybind/pybind11/issues/3788

Expected to build & run as-is. Uncommenting reproduces the infinite recursion.

* Moving try_as_void_ptr_capsule() to the end of load_impl()

* Moving new test into the existing test_class_sh_void_ptr_capsule

* Experiment

* Remove comments and simplify the test cases.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-03-10 16:18:24 -08:00

40 lines
1.3 KiB
Python

import pytest
from pybind11_tests import class_sh_void_ptr_capsule as m
@pytest.mark.parametrize(
"ctor, caller, expected, capsule_generated",
[
(m.Valid, m.get_from_valid_capsule, 101, False),
(m.NoConversion, m.get_from_no_conversion_capsule, 102, False),
(m.NoCapsuleReturned, m.get_from_no_capsule_returned, 103, False),
(m.AsAnotherObject, m.get_from_valid_capsule, 104, True),
],
)
def test_as_void_ptr_capsule(ctor, caller, expected, capsule_generated):
obj = ctor()
assert caller(obj) == expected
assert obj.capsule_generated == capsule_generated
@pytest.mark.parametrize(
"ctor, caller, pointer_type, capsule_generated",
[
(m.AsAnotherObject, m.get_from_shared_ptr_valid_capsule, "shared_ptr", True),
(m.AsAnotherObject, m.get_from_unique_ptr_valid_capsule, "unique_ptr", True),
],
)
def test_as_void_ptr_capsule_unsupported(ctor, caller, pointer_type, capsule_generated):
obj = ctor()
with pytest.raises(RuntimeError) as excinfo:
caller(obj)
assert pointer_type in str(excinfo.value)
assert obj.capsule_generated == capsule_generated
def test_type_with_getattr():
obj = m.TypeWithGetattr()
assert obj.get_42() == 42
assert obj.something == "GetAttr: something"