4 from pybind11_tests
import ConstructorStats, UserType
5 from pybind11_tests
import class_
as m
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 instance = m.NoConstructorNew()
29 assert cstats.alive() == 1
31 assert cstats.alive() == 0
35 assert m.check_type(1) == m.DerivedClass1
36 with pytest.raises(RuntimeError)
as execinfo:
39 assert "pybind11::detail::get_type_info: unable to find type info" in str(
42 assert "Invalid" in str(execinfo.value)
50 assert m.get_type_of(1) == int
51 assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
52 assert m.get_type_of(int) == type
56 assert m.get_type_classic(1) == int
57 assert m.get_type_classic(m.DerivedClass1()) == m.DerivedClass1
58 assert m.get_type_classic(int) == type
63 assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
67 assert m.as_type(int) == int
69 with pytest.raises(TypeError):
70 assert m.as_type(1) == int
72 with pytest.raises(TypeError):
73 assert m.as_type(m.DerivedClass1()) == m.DerivedClass1
77 assert doc(UserType) ==
"A `py::class_` type for testing" 78 assert UserType.__name__ ==
"UserType" 79 assert UserType.__module__ ==
"pybind11_tests" 80 assert UserType.get_value.__name__ ==
"get_value" 81 assert UserType.get_value.__module__ ==
"pybind11_tests" 84 doc(UserType.get_value)
86 get_value(self: m.UserType) -> int 88 Get value using a method 91 assert doc(UserType.value) ==
"Get/set value using a property" 94 doc(m.NoConstructor.new_instance)
96 new_instance() -> m.class_.NoConstructor 104 """Tests that a properly qualified name is set in __qualname__ and that 105 generated docstrings properly use it and the module name""" 106 assert m.NestBase.__qualname__ ==
"NestBase" 107 assert m.NestBase.Nested.__qualname__ ==
"NestBase.Nested" 110 doc(m.NestBase.__init__)
112 __init__(self: m.class_.NestBase) -> None 118 g(self: m.class_.NestBase, arg0: m.class_.NestBase.Nested) -> None 122 doc(m.NestBase.Nested.__init__)
124 __init__(self: m.class_.NestBase.Nested) -> None 128 doc(m.NestBase.Nested.fn)
130 fn(self: m.class_.NestBase.Nested, arg0: int, arg1: m.class_.NestBase, arg2: m.class_.NestBase.Nested) -> None 134 doc(m.NestBase.Nested.fa)
136 fa(self: m.class_.NestBase.Nested, a: int, b: m.class_.NestBase, c: m.class_.NestBase.Nested) -> None 139 assert m.NestBase.__module__ ==
"pybind11_tests.class_" 140 assert m.NestBase.Nested.__module__ ==
"pybind11_tests.class_" 144 roger = m.Rabbit(
"Rabbit")
145 assert roger.name() +
" is a " + roger.species() ==
"Rabbit is a parrot" 146 assert m.pet_name_species(roger) ==
"Rabbit is a parrot" 148 polly = m.Pet(
"Polly",
"parrot")
149 assert polly.name() +
" is a " + polly.species() ==
"Polly is a parrot" 150 assert m.pet_name_species(polly) ==
"Polly is a parrot" 152 molly = m.Dog(
"Molly")
153 assert molly.name() +
" is a " + molly.species() ==
"Molly is a dog" 154 assert m.pet_name_species(molly) ==
"Molly is a dog" 156 fred = m.Hamster(
"Fred")
157 assert fred.name() +
" is a " + fred.species() ==
"Fred is a rodent" 159 assert m.dog_bark(molly) ==
"Woof!" 161 with pytest.raises(TypeError)
as excinfo:
166 dog_bark(): incompatible function arguments. The following argument types are supported: 167 1. (arg0: m.class_.Dog) -> str 169 Invoked with: <m.class_.Pet object at 0> 173 with pytest.raises(TypeError)
as excinfo:
174 m.Chimera(
"lion",
"goat")
175 assert "No constructor defined!" in str(excinfo.value)
185 with pytest.raises(TypeError)
as exc_info:
187 expected =
"m.class_.Pet.__init__() must be called when overriding __init__" 188 assert msg(exc_info.value) == expected
191 class RabbitHamster(m.Rabbit, m.Hamster):
193 m.Rabbit.__init__(self,
"RabbitHamster")
195 with pytest.raises(TypeError)
as exc_info:
197 expected =
"m.class_.Hamster.__init__() must be called when overriding __init__" 198 assert msg(exc_info.value) == expected
202 assert type(m.return_class_1()).__name__ ==
"DerivedClass1" 203 assert type(m.return_class_2()).__name__ ==
"DerivedClass2" 204 assert type(m.return_none()).__name__ ==
"NoneType" 206 assert type(m.return_class_n(1)).__name__ ==
"DerivedClass1" 207 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2" 208 assert type(m.return_class_n(0)).__name__ ==
"BaseClass" 209 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2" 210 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2" 211 assert type(m.return_class_n(0)).__name__ ==
"BaseClass" 212 assert type(m.return_class_n(1)).__name__ ==
"DerivedClass1" 216 objects = [
tuple(),
dict(), m.Pet(
"Polly",
"parrot")] + [m.Dog(
"Molly")] * 4
217 expected = (
True,
True,
True,
True,
True,
False,
False)
218 assert m.check_instances(objects) == expected
224 with pytest.raises(RuntimeError)
as excinfo:
225 m.mismatched_holder_1()
227 'generic_type: type ".*MismatchDerived1" does not have a non-default ' 228 'holder type while its base ".*MismatchBase1" does',
232 with pytest.raises(RuntimeError)
as excinfo:
233 m.mismatched_holder_2()
235 'generic_type: type ".*MismatchDerived2" has a non-default holder type ' 236 'while its base ".*MismatchBase2" does not',
242 """#511: problem with inheritance + overwritten def_static""" 244 d1 = m.MyDerived.make2()
245 d2 = m.MyDerived.make()
253 """Ensure the lifetime of temporary objects created for implicit conversions""" 254 assert m.implicitly_convert_argument(UserType(5)) == 5
255 assert m.implicitly_convert_variable(UserType(5)) == 5
257 assert "outside a bound function" in m.implicitly_convert_variable_fail(UserType(5))
261 """Tests that class-specific operator new/delete functions are invoked""" 263 class SubAliased(m.AliasedHasOpNewDelSize):
268 b = m.HasOpNewDelSize()
269 d = m.HasOpNewDelBoth()
278 sz_alias =
str(m.AliasedHasOpNewDelSize.size_alias)
279 sz_noalias =
str(m.AliasedHasOpNewDelSize.size_noalias)
281 c = m.AliasedHasOpNewDelSize()
283 assert capture == (
"C new " + sz_noalias +
"\n" +
"C new " + sz_alias +
"\n")
306 assert capture == (
"C delete " + sz_noalias +
"\n" +
"C delete " + sz_alias +
"\n")
310 """Expose protected member functions to Python using a helper class""" 317 class C(m.ProtectedB):
319 m.ProtectedB.__init__(self)
329 """Tests that simple POD classes can be constructed using C++11 brace initialization""" 330 a = m.BraceInitialization(123,
"test")
331 assert a.field1 == 123
332 assert a.field2 ==
"test" 337 b = m.NoBraceInitialization([123, 456])
338 assert b.vec == [123, 456]
341 @pytest.mark.xfail(
"env.PYPY")
343 """Instances must correctly increase/decrease the reference count of their types (#1029)""" 344 from sys
import getrefcount
349 for cls
in m.Dog, PyDog:
350 refcount_1 = getrefcount(cls)
351 molly = [cls(
"Molly")
for _
in range(10)]
352 refcount_2 = getrefcount(cls)
356 refcount_3 = getrefcount(cls)
358 assert refcount_1 == refcount_3
359 assert refcount_2 > refcount_1
364 with pytest.raises(TypeError)
as excinfo:
365 m.BogusImplicitConversion(0)
369 __init__(): incompatible constructor arguments. The following argument types are supported: 370 1. m.class_.BogusImplicitConversion(arg0: m.class_.BogusImplicitConversion) 378 with pytest.raises(TypeError)
as exc_info:
379 m.test_error_after_conversions(
"hello")
380 assert str(exc_info.value).startswith(
381 "Unable to convert function return value to a Python type!" 387 p = m.Aligned().ptr()
392 @pytest.mark.xfail(
"env.PYPY")
394 with pytest.raises(TypeError)
as exc_info:
396 class PyFinalChild(m.IsFinal):
399 assert str(exc_info.value).endswith(
"is not an acceptable base type")
403 @pytest.mark.xfail(
"env.PYPY")
405 with pytest.raises(TypeError)
as exc_info:
407 class PyNonFinalFinalChild(m.IsNonFinalFinal):
410 assert str(exc_info.value).endswith(
"is not an acceptable base type")
415 with pytest.raises(RuntimeError):
416 m.PyPrintDestructor().throw_something()
422 instances = [m.SamePointer()
for _
in range(n)]
428 instances[i] = m.Empty()
436 assert issubclass(m.DerivedWithNested, m.BaseWithNested)
437 assert m.BaseWithNested.Nested != m.DerivedWithNested.Nested
438 assert m.BaseWithNested.Nested.get_name() ==
"BaseWithNested::Nested" 439 assert m.DerivedWithNested.Nested.get_name() ==
"DerivedWithNested::Nested" 445 module_scope = types.ModuleType(
"module_scope")
446 with pytest.raises(RuntimeError)
as exc_info:
447 m.register_duplicate_class_name(module_scope)
449 'generic_type: cannot initialize type "Duplicate": ' 450 "an object with that name is already defined" 452 assert str(exc_info.value) == expected
453 with pytest.raises(RuntimeError)
as exc_info:
454 m.register_duplicate_class_type(module_scope)
455 expected =
'generic_type: type "YetAnotherDuplicate" is already registered!' 456 assert str(exc_info.value) == expected
461 with pytest.raises(RuntimeError)
as exc_info:
462 m.register_duplicate_nested_class_name(ClassScope)
464 'generic_type: cannot initialize type "DuplicateNested": ' 465 "an object with that name is already defined" 467 assert str(exc_info.value) == expected
468 with pytest.raises(RuntimeError)
as exc_info:
469 m.register_duplicate_nested_class_type(ClassScope)
470 expected =
'generic_type: type "YetAnotherDuplicateNested" is already registered!' 471 assert str(exc_info.value) == expected
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_multiple_instances_with_same_pointer(capture)
def test_type_of_py_nodelete()
def test_type_of_classic()
void foo(CV_QUALIFIER Matrix3d &m)
def test_operator_new_delete(capture)
def test_register_duplicate_class()
static ConstructorStats & get(std::type_index type)
def test_reentrant_implicit_conversion_failure(msg)
def test_exception_rvalue_abort()
def test_non_final_final()
Matrix< Scalar, Dynamic, Dynamic > C
def test_inheritance_init(msg)
def test_instance_new(msg)
def test_mismatched_holder()
Double_ range(const Point2_ &p, const Point2_ &q)
def test_class_refcount()
def test_base_and_derived_nested_scope()
def test_bind_protected_functions()
def test_implicit_conversion_life_support()
def test_override_static()
def test_error_after_conversions()