2022-01-25 21:29:54 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pybind11_tests import class_sh_void_ptr_capsule as m
|
|
|
|
|
|
|
|
|
2022-03-18 18:08:20 +00:00
|
|
|
class Valid:
|
|
|
|
def __init__(self):
|
|
|
|
self.capsule_generated = False
|
|
|
|
|
2023-02-22 20:46:12 +00:00
|
|
|
def as_pybind11_tests_class_sh_void_ptr_capsule_Valid(self):
|
2022-03-18 18:08:20 +00:00
|
|
|
self.capsule_generated = True
|
|
|
|
return m.create_test_void_ptr_capsule()
|
|
|
|
|
|
|
|
|
|
|
|
class NoConversion:
|
|
|
|
def __init__(self):
|
|
|
|
self.capsule_generated = False
|
|
|
|
|
|
|
|
|
|
|
|
class NoCapsuleReturned:
|
|
|
|
def __init__(self):
|
|
|
|
self.capsule_generated = False
|
|
|
|
|
2023-02-22 20:46:12 +00:00
|
|
|
def as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned(
|
2022-03-18 18:08:20 +00:00
|
|
|
self,
|
|
|
|
):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class AsAnotherObject:
|
|
|
|
def __init__(self):
|
|
|
|
self.capsule_generated = False
|
|
|
|
|
2023-02-22 20:46:12 +00:00
|
|
|
def as_pybind11_tests_class_sh_void_ptr_capsule_Valid(self):
|
2022-03-18 18:08:20 +00:00
|
|
|
self.capsule_generated = True
|
|
|
|
return m.create_test_void_ptr_capsule()
|
|
|
|
|
|
|
|
|
2022-01-25 21:29:54 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-22 21:01:46 +00:00
|
|
|
("ctor", "caller", "expected"),
|
2022-01-25 21:29:54 +00:00
|
|
|
[
|
2023-02-22 21:01:46 +00:00
|
|
|
(Valid, m.get_from_valid_capsule, 1),
|
|
|
|
(AsAnotherObject, m.get_from_valid_capsule, 1),
|
2022-01-25 21:29:54 +00:00
|
|
|
],
|
|
|
|
)
|
2023-02-22 21:01:46 +00:00
|
|
|
def test_valid_as_void_ptr_capsule_function(ctor, caller, expected):
|
2022-01-25 21:29:54 +00:00
|
|
|
obj = ctor()
|
|
|
|
assert caller(obj) == expected
|
2023-02-22 21:01:46 +00:00
|
|
|
assert obj.capsule_generated
|
2022-01-25 21:29:54 +00:00
|
|
|
|
|
|
|
|
2022-03-18 18:08:20 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-22 21:01:46 +00:00
|
|
|
("ctor", "caller"),
|
2022-03-18 18:08:20 +00:00
|
|
|
[
|
2023-02-22 21:01:46 +00:00
|
|
|
(NoConversion, m.get_from_no_conversion_capsule),
|
|
|
|
(NoCapsuleReturned, m.get_from_no_capsule_returned),
|
2022-03-18 18:08:20 +00:00
|
|
|
],
|
|
|
|
)
|
2023-02-22 21:01:46 +00:00
|
|
|
def test_invalid_as_void_ptr_capsule_function(ctor, caller):
|
2022-03-18 18:08:20 +00:00
|
|
|
obj = ctor()
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
caller(obj)
|
2023-02-22 21:01:46 +00:00
|
|
|
assert not obj.capsule_generated
|
2022-03-18 18:08:20 +00:00
|
|
|
|
|
|
|
|
2022-01-25 21:29:54 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-22 20:46:12 +00:00
|
|
|
("ctor", "caller", "pointer_type", "capsule_generated"),
|
2022-01-25 21:29:54 +00:00
|
|
|
[
|
2022-03-18 18:08:20 +00:00
|
|
|
(AsAnotherObject, m.get_from_shared_ptr_valid_capsule, "shared_ptr", True),
|
|
|
|
(AsAnotherObject, m.get_from_unique_ptr_valid_capsule, "unique_ptr", True),
|
2022-01-25 21:29:54 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
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
|
2022-03-11 00:18:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_type_with_getattr():
|
|
|
|
obj = m.TypeWithGetattr()
|
|
|
|
assert obj.get_42() == 42
|
|
|
|
assert obj.something == "GetAttr: something"
|
2022-03-18 18:08:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_multiple_inheritance_getattr():
|
|
|
|
d1 = m.Derived1()
|
|
|
|
assert d1.foo() == 0
|
|
|
|
assert d1.bar() == 1
|
|
|
|
assert d1.prop1 == "Base GetAttr: prop1"
|
|
|
|
|
|
|
|
d2 = m.Derived2()
|
|
|
|
assert d2.foo() == 0
|
|
|
|
assert d2.bar() == 2
|
|
|
|
assert d2.prop2 == "Base GetAttr: prop2"
|
2023-04-08 01:24:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_pass_unspecified_base():
|
|
|
|
assert m.PassUnspecBase(m.UnspecDerived()) == 230
|