4 from pybind11_tests
import ConstructorStats, UserType
5 from pybind11_tests
import class_
as m
9 expected_name =
"UserType" if env.PYPY
else "pybind11_tests.UserType"
10 assert m.obj_class_name(UserType(1)) == expected_name
11 assert m.obj_class_name(UserType) == expected_name
15 assert "pybind11_type" in repr(
type(UserType))
16 assert "UserType" in repr(UserType)
20 with pytest.raises(TypeError)
as excinfo:
22 assert msg(excinfo.value) ==
"m.class_.NoConstructor: No constructor defined!"
24 instance = m.NoConstructor.new_instance()
27 assert cstats.alive() == 1
29 assert cstats.alive() == 0
33 instance = m.NoConstructorNew()
35 assert cstats.alive() == 1
37 assert cstats.alive() == 0
41 assert m.check_type(1) == m.DerivedClass1
42 with pytest.raises(RuntimeError)
as execinfo:
45 assert "pybind11::detail::get_type_info: unable to find type info" in str(
48 assert "Invalid" in str(execinfo.value)
56 assert m.get_type_of(1) == int
57 assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
58 assert m.get_type_of(int) == type
62 assert m.get_type_classic(1) == int
63 assert m.get_type_classic(m.DerivedClass1()) == m.DerivedClass1
64 assert m.get_type_classic(int) == type
69 assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
73 assert m.as_type(int) == int
75 with pytest.raises(TypeError):
76 assert m.as_type(1) == int
78 with pytest.raises(TypeError):
79 assert m.as_type(m.DerivedClass1()) == m.DerivedClass1
83 assert doc(UserType) ==
"A `py::class_` type for testing"
84 assert UserType.__name__ ==
"UserType"
85 assert UserType.__module__ ==
"pybind11_tests"
86 assert UserType.get_value.__name__ ==
"get_value"
87 assert UserType.get_value.__module__ ==
"pybind11_tests"
90 doc(UserType.get_value)
92 get_value(self: m.UserType) -> int
94 Get value using a method
97 assert doc(UserType.value) ==
"Get/set value using a property"
100 doc(m.NoConstructor.new_instance)
102 new_instance() -> m.class_.NoConstructor
110 """Tests that a properly qualified name is set in __qualname__ and that
111 generated docstrings properly use it and the module name"""
112 assert m.NestBase.__qualname__ ==
"NestBase"
113 assert m.NestBase.Nested.__qualname__ ==
"NestBase.Nested"
116 doc(m.NestBase.__init__)
118 __init__(self: m.class_.NestBase) -> None
124 g(self: m.class_.NestBase, arg0: m.class_.NestBase.Nested) -> None
128 doc(m.NestBase.Nested.__init__)
130 __init__(self: m.class_.NestBase.Nested) -> None
134 doc(m.NestBase.Nested.fn)
136 fn(self: m.class_.NestBase.Nested, arg0: int, arg1: m.class_.NestBase, arg2: m.class_.NestBase.Nested) -> None
140 doc(m.NestBase.Nested.fa)
142 fa(self: m.class_.NestBase.Nested, a: int, b: m.class_.NestBase, c: m.class_.NestBase.Nested) -> None
145 assert m.NestBase.__module__ ==
"pybind11_tests.class_"
146 assert m.NestBase.Nested.__module__ ==
"pybind11_tests.class_"
150 roger = m.Rabbit(
"Rabbit")
151 assert roger.name() +
" is a " + roger.species() ==
"Rabbit is a parrot"
152 assert m.pet_name_species(roger) ==
"Rabbit is a parrot"
154 polly = m.Pet(
"Polly",
"parrot")
155 assert polly.name() +
" is a " + polly.species() ==
"Polly is a parrot"
156 assert m.pet_name_species(polly) ==
"Polly is a parrot"
158 molly = m.Dog(
"Molly")
159 assert molly.name() +
" is a " + molly.species() ==
"Molly is a dog"
160 assert m.pet_name_species(molly) ==
"Molly is a dog"
162 fred = m.Hamster(
"Fred")
163 assert fred.name() +
" is a " + fred.species() ==
"Fred is a rodent"
165 assert m.dog_bark(molly) ==
"Woof!"
167 with pytest.raises(TypeError)
as excinfo:
172 dog_bark(): incompatible function arguments. The following argument types are supported:
173 1. (arg0: m.class_.Dog) -> str
175 Invoked with: <m.class_.Pet object at 0>
179 with pytest.raises(TypeError)
as excinfo:
180 m.Chimera(
"lion",
"goat")
181 assert "No constructor defined!" in str(excinfo.value)
190 with pytest.raises(TypeError)
as exc_info:
192 expected =
"m.class_.Pet.__init__() must be called when overriding __init__"
193 assert msg(exc_info.value) == expected
196 class RabbitHamster(m.Rabbit, m.Hamster):
198 m.Rabbit.__init__(self,
"RabbitHamster")
200 with pytest.raises(TypeError)
as exc_info:
202 expected =
"m.class_.Hamster.__init__() must be called when overriding __init__"
203 assert msg(exc_info.value) == expected
207 assert type(m.return_class_1()).__name__ ==
"DerivedClass1"
208 assert type(m.return_class_2()).__name__ ==
"DerivedClass2"
209 assert type(m.return_none()).__name__ ==
"NoneType"
211 assert type(m.return_class_n(1)).__name__ ==
"DerivedClass1"
212 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2"
213 assert type(m.return_class_n(0)).__name__ ==
"BaseClass"
214 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2"
215 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2"
216 assert type(m.return_class_n(0)).__name__ ==
"BaseClass"
217 assert type(m.return_class_n(1)).__name__ ==
"DerivedClass1"
221 objects = [(), {}, m.Pet(
"Polly",
"parrot")] + [m.Dog(
"Molly")] * 4
222 expected = (
True,
True,
True,
True,
True,
False,
False)
223 assert m.check_instances(objects) == expected
229 with pytest.raises(RuntimeError)
as excinfo:
230 m.mismatched_holder_1()
232 'generic_type: type ".*MismatchDerived1" does not have a non-default '
233 'holder type while its base ".*MismatchBase1" does',
237 with pytest.raises(RuntimeError)
as excinfo:
238 m.mismatched_holder_2()
240 'generic_type: type ".*MismatchDerived2" has a non-default holder type '
241 'while its base ".*MismatchBase2" does not',
247 """#511: problem with inheritance + overwritten def_static"""
249 d1 = m.MyDerived.make2()
250 d2 = m.MyDerived.make()
258 """Ensure the lifetime of temporary objects created for implicit conversions"""
259 assert m.implicitly_convert_argument(UserType(5)) == 5
260 assert m.implicitly_convert_variable(UserType(5)) == 5
262 assert "outside a bound function" in m.implicitly_convert_variable_fail(UserType(5))
266 """Tests that class-specific operator new/delete functions are invoked"""
268 class SubAliased(m.AliasedHasOpNewDelSize):
273 b = m.HasOpNewDelSize()
274 d = m.HasOpNewDelBoth()
283 sz_alias =
str(m.AliasedHasOpNewDelSize.size_alias)
284 sz_noalias =
str(m.AliasedHasOpNewDelSize.size_noalias)
286 c = m.AliasedHasOpNewDelSize()
288 assert capture == (
"C new " + sz_noalias +
"\n" +
"C new " + sz_alias +
"\n")
311 assert capture == (
"C delete " + sz_noalias +
"\n" +
"C delete " + sz_alias +
"\n")
315 """Expose protected member functions to Python using a helper class"""
321 assert m.read_foo(b.void_foo()) == 42
322 assert m.pointers_equal(b.get_self(), b)
324 class C(m.ProtectedB):
326 m.ProtectedB.__init__(self)
336 """Tests that simple POD classes can be constructed using C++11 brace initialization"""
337 a = m.BraceInitialization(123,
"test")
338 assert a.field1 == 123
339 assert a.field2 ==
"test"
344 b = m.NoBraceInitialization([123, 456])
345 assert b.vec == [123, 456]
348 @pytest.mark.xfail(
"env.PYPY")
350 """Instances must correctly increase/decrease the reference count of their types (#1029)"""
351 from sys
import getrefcount
356 for cls
in m.Dog, PyDog:
357 refcount_1 = getrefcount(cls)
358 molly = [cls(
"Molly")
for _
in range(10)]
359 refcount_2 = getrefcount(cls)
363 refcount_3 = getrefcount(cls)
365 assert refcount_1 == refcount_3
366 assert refcount_2 > refcount_1
371 with pytest.raises(TypeError)
as excinfo:
372 m.BogusImplicitConversion(0)
376 __init__(): incompatible constructor arguments. The following argument types are supported:
377 1. m.class_.BogusImplicitConversion(arg0: m.class_.BogusImplicitConversion)
385 with pytest.raises(TypeError)
as exc_info:
386 m.test_error_after_conversions(
"hello")
387 assert str(exc_info.value).startswith(
388 "Unable to convert function return value to a Python type!"
394 p = m.Aligned().ptr()
399 @pytest.mark.xfail(
"env.PYPY")
401 with pytest.raises(TypeError)
as exc_info:
403 class PyFinalChild(m.IsFinal):
406 assert str(exc_info.value).endswith(
"is not an acceptable base type")
410 @pytest.mark.xfail(
"env.PYPY")
412 with pytest.raises(TypeError)
as exc_info:
414 class PyNonFinalFinalChild(m.IsNonFinalFinal):
417 assert str(exc_info.value).endswith(
"is not an acceptable base type")
422 with pytest.raises(RuntimeError):
423 m.PyPrintDestructor().throw_something()
429 instances = [m.SamePointer()
for _
in range(n)]
435 instances[i] = m.Empty()
443 assert issubclass(m.DerivedWithNested, m.BaseWithNested)
444 assert m.BaseWithNested.Nested != m.DerivedWithNested.Nested
445 assert m.BaseWithNested.Nested.get_name() ==
"BaseWithNested::Nested"
446 assert m.DerivedWithNested.Nested.get_name() ==
"DerivedWithNested::Nested"
452 module_scope = types.ModuleType(
"module_scope")
453 with pytest.raises(RuntimeError)
as exc_info:
454 m.register_duplicate_class_name(module_scope)
456 'generic_type: cannot initialize type "Duplicate": '
457 "an object with that name is already defined"
459 assert str(exc_info.value) == expected
460 with pytest.raises(RuntimeError)
as exc_info:
461 m.register_duplicate_class_type(module_scope)
462 expected =
'generic_type: type "YetAnotherDuplicate" is already registered!'
463 assert str(exc_info.value) == expected
468 with pytest.raises(RuntimeError)
as exc_info:
469 m.register_duplicate_nested_class_name(ClassScope)
471 'generic_type: cannot initialize type "DuplicateNested": '
472 "an object with that name is already defined"
474 assert str(exc_info.value) == expected
475 with pytest.raises(RuntimeError)
as exc_info:
476 m.register_duplicate_nested_class_type(ClassScope)
477 expected =
'generic_type: type "YetAnotherDuplicateNested" is already registered!'
478 assert str(exc_info.value) == expected
484 ==
"This is really only meant to exercise successful compilation."