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(
37 assert "Invalid" in str(execinfo.value)
45 assert m.get_type_of(1) == int
46 assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
47 assert m.get_type_of(int) == type
51 assert m.get_type_classic(1) == int
52 assert m.get_type_classic(m.DerivedClass1()) == m.DerivedClass1
53 assert m.get_type_classic(int) == type
58 assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
62 assert m.as_type(int) == int
64 with pytest.raises(TypeError):
65 assert m.as_type(1) == int
67 with pytest.raises(TypeError):
68 assert m.as_type(m.DerivedClass1()) == m.DerivedClass1
72 assert doc(UserType) ==
"A `py::class_` type for testing"
73 assert UserType.__name__ ==
"UserType"
74 assert UserType.__module__ ==
"pybind11_tests"
75 assert UserType.get_value.__name__ ==
"get_value"
76 assert UserType.get_value.__module__ ==
"pybind11_tests"
79 doc(UserType.get_value)
81 get_value(self: m.UserType) -> int
83 Get value using a method
86 assert doc(UserType.value) ==
"Get/set value using a property"
89 doc(m.NoConstructor.new_instance)
91 new_instance() -> m.class_.NoConstructor
99 """Tests that a properly qualified name is set in __qualname__ (even in pre-3.3, where we
100 backport the attribute) and that generated docstrings properly use it and the module name"""
101 assert m.NestBase.__qualname__ ==
"NestBase"
102 assert m.NestBase.Nested.__qualname__ ==
"NestBase.Nested"
105 doc(m.NestBase.__init__)
107 __init__(self: m.class_.NestBase) -> None
113 g(self: m.class_.NestBase, arg0: m.class_.NestBase.Nested) -> None
117 doc(m.NestBase.Nested.__init__)
119 __init__(self: m.class_.NestBase.Nested) -> None
123 doc(m.NestBase.Nested.fn)
125 fn(self: m.class_.NestBase.Nested, arg0: int, arg1: m.class_.NestBase, arg2: m.class_.NestBase.Nested) -> None
129 doc(m.NestBase.Nested.fa)
131 fa(self: m.class_.NestBase.Nested, a: int, b: m.class_.NestBase, c: m.class_.NestBase.Nested) -> None
134 assert m.NestBase.__module__ ==
"pybind11_tests.class_"
135 assert m.NestBase.Nested.__module__ ==
"pybind11_tests.class_"
139 roger = m.Rabbit(
"Rabbit")
140 assert roger.name() +
" is a " + roger.species() ==
"Rabbit is a parrot"
141 assert m.pet_name_species(roger) ==
"Rabbit is a parrot"
143 polly = m.Pet(
"Polly",
"parrot")
144 assert polly.name() +
" is a " + polly.species() ==
"Polly is a parrot"
145 assert m.pet_name_species(polly) ==
"Polly is a parrot"
147 molly = m.Dog(
"Molly")
148 assert molly.name() +
" is a " + molly.species() ==
"Molly is a dog"
149 assert m.pet_name_species(molly) ==
"Molly is a dog"
151 fred = m.Hamster(
"Fred")
152 assert fred.name() +
" is a " + fred.species() ==
"Fred is a rodent"
154 assert m.dog_bark(molly) ==
"Woof!"
156 with pytest.raises(TypeError)
as excinfo:
161 dog_bark(): incompatible function arguments. The following argument types are supported:
162 1. (arg0: m.class_.Dog) -> str
164 Invoked with: <m.class_.Pet object at 0>
168 with pytest.raises(TypeError)
as excinfo:
169 m.Chimera(
"lion",
"goat")
170 assert "No constructor defined!" in str(excinfo.value)
180 with pytest.raises(TypeError)
as exc_info:
182 expected =
"m.class_.Pet.__init__() must be called when overriding __init__"
183 assert msg(exc_info.value) == expected
186 class RabbitHamster(m.Rabbit, m.Hamster):
188 m.Rabbit.__init__(self,
"RabbitHamster")
190 with pytest.raises(TypeError)
as exc_info:
192 expected =
"m.class_.Hamster.__init__() must be called when overriding __init__"
193 assert msg(exc_info.value) == expected
197 assert type(m.return_class_1()).__name__ ==
"DerivedClass1"
198 assert type(m.return_class_2()).__name__ ==
"DerivedClass2"
199 assert type(m.return_none()).__name__ ==
"NoneType"
201 assert type(m.return_class_n(1)).__name__ ==
"DerivedClass1"
202 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2"
203 assert type(m.return_class_n(0)).__name__ ==
"BaseClass"
204 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2"
205 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2"
206 assert type(m.return_class_n(0)).__name__ ==
"BaseClass"
207 assert type(m.return_class_n(1)).__name__ ==
"DerivedClass1"
211 objects = [
tuple(),
dict(), m.Pet(
"Polly",
"parrot")] + [m.Dog(
"Molly")] * 4
212 expected = (
True,
True,
True,
True,
True,
False,
False)
213 assert m.check_instances(objects) == expected
219 with pytest.raises(RuntimeError)
as excinfo:
220 m.mismatched_holder_1()
222 'generic_type: type ".*MismatchDerived1" does not have a non-default '
223 'holder type while its base ".*MismatchBase1" does',
227 with pytest.raises(RuntimeError)
as excinfo:
228 m.mismatched_holder_2()
230 'generic_type: type ".*MismatchDerived2" has a non-default holder type '
231 'while its base ".*MismatchBase2" does not',
237 """#511: problem with inheritance + overwritten def_static"""
239 d1 = m.MyDerived.make2()
240 d2 = m.MyDerived.make()
248 """Ensure the lifetime of temporary objects created for implicit conversions"""
249 assert m.implicitly_convert_argument(UserType(5)) == 5
250 assert m.implicitly_convert_variable(UserType(5)) == 5
252 assert "outside a bound function" in m.implicitly_convert_variable_fail(UserType(5))
256 """Tests that class-specific operator new/delete functions are invoked"""
258 class SubAliased(m.AliasedHasOpNewDelSize):
263 b = m.HasOpNewDelSize()
264 d = m.HasOpNewDelBoth()
273 sz_alias =
str(m.AliasedHasOpNewDelSize.size_alias)
274 sz_noalias =
str(m.AliasedHasOpNewDelSize.size_noalias)
276 c = m.AliasedHasOpNewDelSize()
278 assert capture == (
"C new " + sz_noalias +
"\n" +
"C new " + sz_alias +
"\n")
301 assert capture == (
"C delete " + sz_noalias +
"\n" +
"C delete " + sz_alias +
"\n")
305 """Expose protected member functions to Python using a helper class"""
312 class C(m.ProtectedB):
314 m.ProtectedB.__init__(self)
324 """ Tests that simple POD classes can be constructed using C++11 brace initialization """
325 a = m.BraceInitialization(123,
"test")
326 assert a.field1 == 123
327 assert a.field2 ==
"test"
332 b = m.NoBraceInitialization([123, 456])
333 assert b.vec == [123, 456]
336 @pytest.mark.xfail(
"env.PYPY")
338 """Instances must correctly increase/decrease the reference count of their types (#1029)"""
339 from sys
import getrefcount
344 for cls
in m.Dog, PyDog:
345 refcount_1 = getrefcount(cls)
346 molly = [cls(
"Molly")
for _
in range(10)]
347 refcount_2 = getrefcount(cls)
351 refcount_3 = getrefcount(cls)
353 assert refcount_1 == refcount_3
354 assert refcount_2 > refcount_1
359 with pytest.raises(TypeError)
as excinfo:
360 m.BogusImplicitConversion(0)
364 __init__(): incompatible constructor arguments. The following argument types are supported:
365 1. m.class_.BogusImplicitConversion(arg0: m.class_.BogusImplicitConversion)
373 with pytest.raises(TypeError)
as exc_info:
374 m.test_error_after_conversions(
"hello")
375 assert str(exc_info.value).startswith(
376 "Unable to convert function return value to a Python type!"
382 p = m.Aligned().ptr()
387 @pytest.mark.xfail(
"env.PYPY")
389 with pytest.raises(TypeError)
as exc_info:
391 class PyFinalChild(m.IsFinal):
394 assert str(exc_info.value).endswith(
"is not an acceptable base type")
398 @pytest.mark.xfail(
"env.PYPY")
400 with pytest.raises(TypeError)
as exc_info:
402 class PyNonFinalFinalChild(m.IsNonFinalFinal):
405 assert str(exc_info.value).endswith(
"is not an acceptable base type")
410 with pytest.raises(RuntimeError):
411 m.PyPrintDestructor().throw_something()
417 instances = [m.SamePointer()
for _
in range(n)]
423 instances[i] = m.Empty()
431 assert issubclass(m.DerivedWithNested, m.BaseWithNested)
432 assert m.BaseWithNested.Nested != m.DerivedWithNested.Nested
433 assert m.BaseWithNested.Nested.get_name() ==
"BaseWithNested::Nested"
434 assert m.DerivedWithNested.Nested.get_name() ==
"DerivedWithNested::Nested"
440 module_scope = types.ModuleType(
"module_scope")
441 with pytest.raises(RuntimeError)
as exc_info:
442 m.register_duplicate_class_name(module_scope)
444 'generic_type: cannot initialize type "Duplicate": '
445 "an object with that name is already defined"
447 assert str(exc_info.value) == expected
448 with pytest.raises(RuntimeError)
as exc_info:
449 m.register_duplicate_class_type(module_scope)
450 expected =
'generic_type: type "YetAnotherDuplicate" is already registered!'
451 assert str(exc_info.value) == expected
456 with pytest.raises(RuntimeError)
as exc_info:
457 m.register_duplicate_nested_class_name(ClassScope)
459 'generic_type: cannot initialize type "DuplicateNested": '
460 "an object with that name is already defined"
462 assert str(exc_info.value) == expected
463 with pytest.raises(RuntimeError)
as exc_info:
464 m.register_duplicate_nested_class_type(ClassScope)
465 expected =
'generic_type: type "YetAnotherDuplicateNested" is already registered!'
466 assert str(exc_info.value) == expected