3 from pybind11_tests
import class_
as m
4 from pybind11_tests
import UserType, ConstructorStats
9 assert "pybind11_type" in repr(
type(UserType))
10 assert "UserType" in repr(UserType)
14 with pytest.raises(TypeError)
as excinfo:
16 assert msg(excinfo.value) ==
"m.class_.NoConstructor: No constructor defined!" 18 instance = m.NoConstructor.new_instance()
21 assert cstats.alive() == 1
23 assert cstats.alive() == 0
27 assert doc(UserType) ==
"A `py::class_` type for testing" 28 assert UserType.__name__ ==
"UserType" 29 assert UserType.__module__ ==
"pybind11_tests" 30 assert UserType.get_value.__name__ ==
"get_value" 31 assert UserType.get_value.__module__ ==
"pybind11_tests" 33 assert doc(UserType.get_value) ==
""" 34 get_value(self: m.UserType) -> int 36 Get value using a method 38 assert doc(UserType.value) ==
"Get/set value using a property" 40 assert doc(m.NoConstructor.new_instance) ==
""" 41 new_instance() -> m.class_.NoConstructor 48 """Tests that a properly qualified name is set in __qualname__ (even in pre-3.3, where we 49 backport the attribute) and that generated docstrings properly use it and the module name""" 50 assert m.NestBase.__qualname__ ==
"NestBase" 51 assert m.NestBase.Nested.__qualname__ ==
"NestBase.Nested" 53 assert doc(m.NestBase.__init__) ==
""" 54 __init__(self: m.class_.NestBase) -> None 56 assert doc(m.NestBase.g) ==
""" 57 g(self: m.class_.NestBase, arg0: m.class_.NestBase.Nested) -> None 59 assert doc(m.NestBase.Nested.__init__) ==
""" 60 __init__(self: m.class_.NestBase.Nested) -> None 62 assert doc(m.NestBase.Nested.fn) ==
""" 63 fn(self: m.class_.NestBase.Nested, arg0: int, arg1: m.class_.NestBase, arg2: m.class_.NestBase.Nested) -> None 65 assert doc(m.NestBase.Nested.fa) ==
""" 66 fa(self: m.class_.NestBase.Nested, a: int, b: m.class_.NestBase, c: m.class_.NestBase.Nested) -> None 68 assert m.NestBase.__module__ ==
"pybind11_tests.class_" 69 assert m.NestBase.Nested.__module__ ==
"pybind11_tests.class_" 73 roger = m.Rabbit(
'Rabbit')
74 assert roger.name() +
" is a " + roger.species() ==
"Rabbit is a parrot" 75 assert m.pet_name_species(roger) ==
"Rabbit is a parrot" 77 polly = m.Pet(
'Polly',
'parrot')
78 assert polly.name() +
" is a " + polly.species() ==
"Polly is a parrot" 79 assert m.pet_name_species(polly) ==
"Polly is a parrot" 81 molly = m.Dog(
'Molly')
82 assert molly.name() +
" is a " + molly.species() ==
"Molly is a dog" 83 assert m.pet_name_species(molly) ==
"Molly is a dog" 85 fred = m.Hamster(
'Fred')
86 assert fred.name() +
" is a " + fred.species() ==
"Fred is a rodent" 88 assert m.dog_bark(molly) ==
"Woof!" 90 with pytest.raises(TypeError)
as excinfo:
92 assert msg(excinfo.value) ==
""" 93 dog_bark(): incompatible function arguments. The following argument types are supported: 94 1. (arg0: m.class_.Dog) -> str 96 Invoked with: <m.class_.Pet object at 0> 99 with pytest.raises(TypeError)
as excinfo:
100 m.Chimera(
"lion",
"goat")
101 assert "No constructor defined!" in str(excinfo.value)
105 assert type(m.return_class_1()).__name__ ==
"DerivedClass1" 106 assert type(m.return_class_2()).__name__ ==
"DerivedClass2" 107 assert type(m.return_none()).__name__ ==
"NoneType" 109 assert type(m.return_class_n(1)).__name__ ==
"DerivedClass1" 110 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2" 111 assert type(m.return_class_n(0)).__name__ ==
"BaseClass" 112 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2" 113 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2" 114 assert type(m.return_class_n(0)).__name__ ==
"BaseClass" 115 assert type(m.return_class_n(1)).__name__ ==
"DerivedClass1" 119 objects = [tuple(), dict(), m.Pet(
"Polly",
"parrot")] + [m.Dog(
"Molly")] * 4
120 expected = (
True,
True,
True,
True,
True,
False,
False)
121 assert m.check_instances(objects) == expected
127 with pytest.raises(RuntimeError)
as excinfo:
128 m.mismatched_holder_1()
129 assert re.match(
'generic_type: type ".*MismatchDerived1" does not have a non-default ' 130 'holder type while its base ".*MismatchBase1" does',
str(excinfo.value))
132 with pytest.raises(RuntimeError)
as excinfo:
133 m.mismatched_holder_2()
134 assert re.match(
'generic_type: type ".*MismatchDerived2" has a non-default holder type ' 135 'while its base ".*MismatchBase2" does not',
str(excinfo.value))
139 """#511: problem with inheritance + overwritten def_static""" 141 d1 = m.MyDerived.make2()
142 d2 = m.MyDerived.make()
150 """Ensure the lifetime of temporary objects created for implicit conversions""" 151 assert m.implicitly_convert_argument(UserType(5)) == 5
152 assert m.implicitly_convert_variable(UserType(5)) == 5
154 assert "outside a bound function" in m.implicitly_convert_variable_fail(UserType(5))
158 """Tests that class-specific operator new/delete functions are invoked""" 160 class SubAliased(m.AliasedHasOpNewDelSize):
165 b = m.HasOpNewDelSize()
166 d = m.HasOpNewDelBoth()
167 assert capture ==
""" 172 sz_alias =
str(m.AliasedHasOpNewDelSize.size_alias)
173 sz_noalias =
str(m.AliasedHasOpNewDelSize.size_noalias)
175 c = m.AliasedHasOpNewDelSize()
178 "C new " + sz_noalias +
"\n" +
179 "C new " + sz_alias +
"\n" 189 assert capture ==
""" 201 "C delete " + sz_noalias +
"\n" +
202 "C delete " + sz_alias +
"\n" 207 """Expose protected member functions to Python using a helper class""" 214 class C(m.ProtectedB):
216 m.ProtectedB.__init__(self)
226 """ Tests that simple POD classes can be constructed using C++11 brace initialization """ 227 a = m.BraceInitialization(123,
"test")
228 assert a.field1 == 123
229 assert a.field2 ==
"test" 234 b = m.NoBraceInitialization([123, 456])
235 assert b.vec == [123, 456]
238 @pytest.unsupported_on_pypy
240 """Instances must correctly increase/decrease the reference count of their types (#1029)""" 241 from sys
import getrefcount
246 for cls
in m.Dog, PyDog:
247 refcount_1 = getrefcount(cls)
248 molly = [cls(
"Molly")
for _
in range(10)]
249 refcount_2 = getrefcount(cls)
253 refcount_3 = getrefcount(cls)
255 assert refcount_1 == refcount_3
256 assert refcount_2 > refcount_1
261 with pytest.raises(TypeError)
as excinfo:
262 m.BogusImplicitConversion(0)
263 assert msg(excinfo.value) ==
''' 264 __init__(): incompatible constructor arguments. The following argument types are supported: 265 1. m.class_.BogusImplicitConversion(arg0: m.class_.BogusImplicitConversion) 272 with pytest.raises(TypeError)
as exc_info:
273 m.test_error_after_conversions(
"hello")
274 assert str(exc_info.value).startswith(
275 "Unable to convert function return value to a Python type!")
280 p = m.Aligned().ptr()
def test_brace_initialization()
static ConstructorStats & get(std::type_index type)
def test_inheritance(msg)
def test_automatic_upcasting()
def test_operator_new_delete(capture)
return isinstance(obj, type)
def test_reentrant_implicit_conversion_failure(msg)
def test_mismatched_holder()
const detail::type_info * type
def test_class_refcount()
def test_bind_protected_functions()
def test_implicit_conversion_life_support()
def test_override_static()
bool hasattr(handle obj, handle name)
def test_error_after_conversions()