6 from pybind11_tests
import class_
as m
7 from pybind11_tests
import UserType, ConstructorStats
12 assert "pybind11_type" in repr(
type(UserType))
13 assert "UserType" in repr(UserType)
17 with pytest.raises(TypeError)
as excinfo:
19 assert msg(excinfo.value) ==
"m.class_.NoConstructor: No constructor defined!" 21 instance = m.NoConstructor.new_instance()
24 assert cstats.alive() == 1
26 assert cstats.alive() == 0
30 assert m.check_type(1) == m.DerivedClass1
31 with pytest.raises(RuntimeError)
as execinfo:
34 assert 'pybind11::detail::get_type_info: unable to find type info' in str(execinfo.value)
35 assert 'Invalid' in str(execinfo.value)
43 assert m.get_type_of(1) == int
44 assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
45 assert m.get_type_of(int) == type
50 assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
54 assert m.as_type(int) == int
56 with pytest.raises(RuntimeError):
57 assert m.as_type(1) == int
59 with pytest.raises(RuntimeError):
60 assert m.as_type(m.DerivedClass1()) == m.DerivedClass1
64 assert doc(UserType) ==
"A `py::class_` type for testing" 65 assert UserType.__name__ ==
"UserType" 66 assert UserType.__module__ ==
"pybind11_tests" 67 assert UserType.get_value.__name__ ==
"get_value" 68 assert UserType.get_value.__module__ ==
"pybind11_tests" 70 assert doc(UserType.get_value) ==
""" 71 get_value(self: m.UserType) -> int 73 Get value using a method 75 assert doc(UserType.value) ==
"Get/set value using a property" 77 assert doc(m.NoConstructor.new_instance) ==
""" 78 new_instance() -> m.class_.NoConstructor 85 """Tests that a properly qualified name is set in __qualname__ (even in pre-3.3, where we 86 backport the attribute) and that generated docstrings properly use it and the module name""" 87 assert m.NestBase.__qualname__ ==
"NestBase" 88 assert m.NestBase.Nested.__qualname__ ==
"NestBase.Nested" 90 assert doc(m.NestBase.__init__) ==
""" 91 __init__(self: m.class_.NestBase) -> None 93 assert doc(m.NestBase.g) ==
""" 94 g(self: m.class_.NestBase, arg0: m.class_.NestBase.Nested) -> None 96 assert doc(m.NestBase.Nested.__init__) ==
""" 97 __init__(self: m.class_.NestBase.Nested) -> None 99 assert doc(m.NestBase.Nested.fn) ==
""" 100 fn(self: m.class_.NestBase.Nested, arg0: int, arg1: m.class_.NestBase, arg2: m.class_.NestBase.Nested) -> None 102 assert doc(m.NestBase.Nested.fa) ==
""" 103 fa(self: m.class_.NestBase.Nested, a: int, b: m.class_.NestBase, c: m.class_.NestBase.Nested) -> None 105 assert m.NestBase.__module__ ==
"pybind11_tests.class_" 106 assert m.NestBase.Nested.__module__ ==
"pybind11_tests.class_" 110 roger = m.Rabbit(
'Rabbit')
111 assert roger.name() +
" is a " + roger.species() ==
"Rabbit is a parrot" 112 assert m.pet_name_species(roger) ==
"Rabbit is a parrot" 114 polly = m.Pet(
'Polly',
'parrot')
115 assert polly.name() +
" is a " + polly.species() ==
"Polly is a parrot" 116 assert m.pet_name_species(polly) ==
"Polly is a parrot" 118 molly = m.Dog(
'Molly')
119 assert molly.name() +
" is a " + molly.species() ==
"Molly is a dog" 120 assert m.pet_name_species(molly) ==
"Molly is a dog" 122 fred = m.Hamster(
'Fred')
123 assert fred.name() +
" is a " + fred.species() ==
"Fred is a rodent" 125 assert m.dog_bark(molly) ==
"Woof!" 127 with pytest.raises(TypeError)
as excinfo:
129 assert msg(excinfo.value) ==
""" 130 dog_bark(): incompatible function arguments. The following argument types are supported: 131 1. (arg0: m.class_.Dog) -> str 133 Invoked with: <m.class_.Pet object at 0> 136 with pytest.raises(TypeError)
as excinfo:
137 m.Chimera(
"lion",
"goat")
138 assert "No constructor defined!" in str(excinfo.value)
147 with pytest.raises(TypeError)
as exc_info:
149 expected = [
"m.class_.Pet.__init__() must be called when overriding __init__",
150 "Pet.__init__() must be called when overriding __init__"]
152 assert msg(exc_info.value)
in expected
155 class RabbitHamster(m.Rabbit, m.Hamster):
157 m.Rabbit.__init__(self,
"RabbitHamster")
159 with pytest.raises(TypeError)
as exc_info:
161 expected = [
"m.class_.Hamster.__init__() must be called when overriding __init__",
162 "Hamster.__init__() must be called when overriding __init__"]
163 assert msg(exc_info.value)
in expected
167 assert type(m.return_class_1()).__name__ ==
"DerivedClass1" 168 assert type(m.return_class_2()).__name__ ==
"DerivedClass2" 169 assert type(m.return_none()).__name__ ==
"NoneType" 171 assert type(m.return_class_n(1)).__name__ ==
"DerivedClass1" 172 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2" 173 assert type(m.return_class_n(0)).__name__ ==
"BaseClass" 174 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2" 175 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2" 176 assert type(m.return_class_n(0)).__name__ ==
"BaseClass" 177 assert type(m.return_class_n(1)).__name__ ==
"DerivedClass1" 181 objects = [
tuple(),
dict(), m.Pet(
"Polly",
"parrot")] + [m.Dog(
"Molly")] * 4
182 expected = (
True,
True,
True,
True,
True,
False,
False)
183 assert m.check_instances(objects) == expected
189 with pytest.raises(RuntimeError)
as excinfo:
190 m.mismatched_holder_1()
191 assert re.match(
'generic_type: type ".*MismatchDerived1" does not have a non-default ' 192 'holder type while its base ".*MismatchBase1" does',
str(excinfo.value))
194 with pytest.raises(RuntimeError)
as excinfo:
195 m.mismatched_holder_2()
196 assert re.match(
'generic_type: type ".*MismatchDerived2" has a non-default holder type ' 197 'while its base ".*MismatchBase2" does not',
str(excinfo.value))
201 """#511: problem with inheritance + overwritten def_static""" 203 d1 = m.MyDerived.make2()
204 d2 = m.MyDerived.make()
212 """Ensure the lifetime of temporary objects created for implicit conversions""" 213 assert m.implicitly_convert_argument(UserType(5)) == 5
214 assert m.implicitly_convert_variable(UserType(5)) == 5
216 assert "outside a bound function" in m.implicitly_convert_variable_fail(UserType(5))
220 """Tests that class-specific operator new/delete functions are invoked""" 222 class SubAliased(m.AliasedHasOpNewDelSize):
227 b = m.HasOpNewDelSize()
228 d = m.HasOpNewDelBoth()
229 assert capture ==
""" 234 sz_alias =
str(m.AliasedHasOpNewDelSize.size_alias)
235 sz_noalias =
str(m.AliasedHasOpNewDelSize.size_noalias)
237 c = m.AliasedHasOpNewDelSize()
240 "C new " + sz_noalias +
"\n" +
241 "C new " + sz_alias +
"\n" 251 assert capture ==
""" 263 "C delete " + sz_noalias +
"\n" +
264 "C delete " + sz_alias +
"\n" 269 """Expose protected member functions to Python using a helper class""" 276 class C(m.ProtectedB):
278 m.ProtectedB.__init__(self)
288 """ Tests that simple POD classes can be constructed using C++11 brace initialization """ 289 a = m.BraceInitialization(123,
"test")
290 assert a.field1 == 123
291 assert a.field2 ==
"test" 296 b = m.NoBraceInitialization([123, 456])
297 assert b.vec == [123, 456]
300 @pytest.mark.xfail(
"env.PYPY")
302 """Instances must correctly increase/decrease the reference count of their types (#1029)""" 303 from sys
import getrefcount
308 for cls
in m.Dog, PyDog:
309 refcount_1 = getrefcount(cls)
310 molly = [cls(
"Molly")
for _
in range(10)]
311 refcount_2 = getrefcount(cls)
315 refcount_3 = getrefcount(cls)
317 assert refcount_1 == refcount_3
318 assert refcount_2 > refcount_1
323 with pytest.raises(TypeError)
as excinfo:
324 m.BogusImplicitConversion(0)
325 assert msg(excinfo.value) ==
''' 326 __init__(): incompatible constructor arguments. The following argument types are supported: 327 1. m.class_.BogusImplicitConversion(arg0: m.class_.BogusImplicitConversion) 334 with pytest.raises(TypeError)
as exc_info:
335 m.test_error_after_conversions(
"hello")
336 assert str(exc_info.value).startswith(
337 "Unable to convert function return value to a Python type!")
342 p = m.Aligned().
ptr()
347 @pytest.mark.xfail(
"env.PYPY")
349 with pytest.raises(TypeError)
as exc_info:
350 class PyFinalChild(m.IsFinal):
352 assert str(exc_info.value).endswith(
"is not an acceptable base type")
356 @pytest.mark.xfail(
"env.PYPY")
358 with pytest.raises(TypeError)
as exc_info:
359 class PyNonFinalFinalChild(m.IsNonFinalFinal):
361 assert str(exc_info.value).endswith(
"is not an acceptable base type")
366 with pytest.raises(RuntimeError):
367 m.PyPrintDestructor().throw_something()
def test_brace_initialization()
bool hasattr(handle obj, handle name)
def test_inheritance(msg)
Annotation for documentation.
bool isinstance(handle obj)
def test_automatic_upcasting()
def test_type_of_py_nodelete()
void foo(CV_QUALIFIER Matrix3d &m)
def test_operator_new_delete(capture)
static ConstructorStats & get(std::type_index type)
def test_reentrant_implicit_conversion_failure(msg)
def test_exception_rvalue_abort()
def test_non_final_final()
def test_inheritance_init(msg)
def test_mismatched_holder()
def test_class_refcount()
def test_bind_protected_functions()
def test_implicit_conversion_life_support()
def test_override_static()
def test_error_after_conversions()