5from pybind11_tests
import ConstructorStats, UserType
6from pybind11_tests
import class_
as m
11 assert "pybind11_type" in repr(
type(UserType))
12 assert "UserType" in repr(UserType)
16 with pytest.raises(TypeError)
as excinfo:
18 assert msg(excinfo.value) ==
"m.class_.NoConstructor: No constructor defined!"
20 instance = m.NoConstructor.new_instance()
23 assert cstats.alive() == 1
25 assert cstats.alive() == 0
29 instance = m.NoConstructorNew()
31 assert cstats.alive() == 1
33 assert cstats.alive() == 0
37 assert m.check_type(1) == m.DerivedClass1
38 with pytest.raises(RuntimeError)
as execinfo:
41 assert "pybind11::detail::get_type_info: unable to find type info" in str(
44 assert "Invalid" in str(execinfo.value)
52 assert m.get_type_of(1) == int
53 assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
54 assert m.get_type_of(int) == type
58 assert m.get_type_classic(1) == int
59 assert m.get_type_classic(m.DerivedClass1()) == m.DerivedClass1
60 assert m.get_type_classic(int) == type
65 assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
69 assert m.as_type(int) == int
71 with pytest.raises(TypeError):
72 assert m.as_type(1) == int
74 with pytest.raises(TypeError):
75 assert m.as_type(m.DerivedClass1()) == m.DerivedClass1
79 assert doc(UserType) ==
"A `py::class_` type for testing"
80 assert UserType.__name__ ==
"UserType"
81 assert UserType.__module__ ==
"pybind11_tests"
82 assert UserType.get_value.__name__ ==
"get_value"
83 assert UserType.get_value.__module__ ==
"pybind11_tests"
86 doc(UserType.get_value)
88 get_value(self: m.UserType) -> int
90 Get value using a method
93 assert doc(UserType.value) ==
"Get/set value using a property"
96 doc(m.NoConstructor.new_instance)
98 new_instance() -> m.class_.NoConstructor
106 """Tests that a properly qualified name is set in __qualname__ (even in pre-3.3, where we
107 backport the attribute) and that generated docstrings properly use it
and the module name
"""
108 assert m.NestBase.__qualname__ ==
"NestBase"
109 assert m.NestBase.Nested.__qualname__ ==
"NestBase.Nested"
112 doc(m.NestBase.__init__)
114 __init__(self: m.class_.NestBase) -> None
120 g(self: m.class_.NestBase, arg0: m.class_.NestBase.Nested) -> None
124 doc(m.NestBase.Nested.__init__)
126 __init__(self: m.class_.NestBase.Nested) -> None
130 doc(m.NestBase.Nested.fn)
132 fn(self: m.class_.NestBase.Nested, arg0: int, arg1: m.class_.NestBase, arg2: m.class_.NestBase.Nested) -> None
136 doc(m.NestBase.Nested.fa)
138 fa(self: m.class_.NestBase.Nested, a: int, b: m.class_.NestBase, c: m.class_.NestBase.Nested) -> None
141 assert m.NestBase.__module__ ==
"pybind11_tests.class_"
142 assert m.NestBase.Nested.__module__ ==
"pybind11_tests.class_"
146 roger = m.Rabbit(
"Rabbit")
147 assert roger.name() +
" is a " + roger.species() ==
"Rabbit is a parrot"
148 assert m.pet_name_species(roger) ==
"Rabbit is a parrot"
150 polly = m.Pet(
"Polly",
"parrot")
151 assert polly.name() +
" is a " + polly.species() ==
"Polly is a parrot"
152 assert m.pet_name_species(polly) ==
"Polly is a parrot"
154 molly = m.Dog(
"Molly")
155 assert molly.name() +
" is a " + molly.species() ==
"Molly is a dog"
156 assert m.pet_name_species(molly) ==
"Molly is a dog"
158 fred = m.Hamster(
"Fred")
159 assert fred.name() +
" is a " + fred.species() ==
"Fred is a rodent"
161 assert m.dog_bark(molly) ==
"Woof!"
163 with pytest.raises(TypeError)
as excinfo:
168 dog_bark(): incompatible function arguments. The following argument types are supported:
169 1. (arg0: m.class_.Dog) -> str
171 Invoked with: <m.class_.Pet object at 0>
175 with pytest.raises(TypeError)
as excinfo:
176 m.Chimera(
"lion",
"goat")
177 assert "No constructor defined!" in str(excinfo.value)
187 with pytest.raises(TypeError)
as exc_info:
189 expected =
"m.class_.Pet.__init__() must be called when overriding __init__"
190 assert msg(exc_info.value) == expected
193 class RabbitHamster(m.Rabbit, m.Hamster):
195 m.Rabbit.__init__(self,
"RabbitHamster")
197 with pytest.raises(TypeError)
as exc_info:
199 expected =
"m.class_.Hamster.__init__() must be called when overriding __init__"
200 assert msg(exc_info.value) == expected
204 assert type(m.return_class_1()).__name__ ==
"DerivedClass1"
205 assert type(m.return_class_2()).__name__ ==
"DerivedClass2"
206 assert type(m.return_none()).__name__ ==
"NoneType"
208 assert type(m.return_class_n(1)).__name__ ==
"DerivedClass1"
209 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2"
210 assert type(m.return_class_n(0)).__name__ ==
"BaseClass"
211 assert type(m.return_class_n(2)).__name__ ==
"DerivedClass2"
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(1)).__name__ ==
"DerivedClass1"
218 objects = [
tuple(),
dict(), m.Pet(
"Polly",
"parrot")] + [m.Dog(
"Molly")] * 4
219 expected = (
True,
True,
True,
True,
True,
False,
False)
220 assert m.check_instances(objects) == expected
226 with pytest.raises(RuntimeError)
as excinfo:
227 m.mismatched_holder_1()
229 'generic_type: type ".*MismatchDerived1" does not have a non-default '
230 'holder type while its base ".*MismatchBase1" does',
234 with pytest.raises(RuntimeError)
as excinfo:
235 m.mismatched_holder_2()
237 'generic_type: type ".*MismatchDerived2" has a non-default holder type '
238 'while its base ".*MismatchBase2" does not',
244 """#511: problem with inheritance + overwritten def_static"""
246 d1 = m.MyDerived.make2()
247 d2 = m.MyDerived.make()
255 """Ensure the lifetime of temporary objects created for implicit conversions"""
256 assert m.implicitly_convert_argument(UserType(5)) == 5
257 assert m.implicitly_convert_variable(UserType(5)) == 5
259 assert "outside a bound function" in m.implicitly_convert_variable_fail(UserType(5))
263 """Tests that class-specific operator new/delete functions are invoked"""
265 class SubAliased(m.AliasedHasOpNewDelSize):
270 b = m.HasOpNewDelSize()
271 d = m.HasOpNewDelBoth()
280 sz_alias = str(m.AliasedHasOpNewDelSize.size_alias)
281 sz_noalias = str(m.AliasedHasOpNewDelSize.size_noalias)
283 c = m.AliasedHasOpNewDelSize()
285 assert capture == (
"C new " + sz_noalias +
"\n" +
"C new " + sz_alias +
"\n")
308 assert capture == (
"C delete " + sz_noalias +
"\n" +
"C delete " + sz_alias +
"\n")
312 """Expose protected member functions to Python using a helper class"""
319 class C(m.ProtectedB):
321 m.ProtectedB.__init__(self)
331 """Tests that simple POD classes can be constructed using C++11 brace initialization"""
332 a = m.BraceInitialization(123,
"test")
333 assert a.field1 == 123
334 assert a.field2 ==
"test"
339 b = m.NoBraceInitialization([123, 456])
340 assert b.vec == [123, 456]
343@pytest.mark.xfail("env.PYPY")
345 """Instances must correctly increase/decrease the reference count of their types (#1029)"""
346 from sys
import getrefcount
351 for cls
in m.Dog, PyDog:
352 refcount_1 = getrefcount(cls)
353 molly = [cls(
"Molly")
for _
in range(10)]
354 refcount_2 = getrefcount(cls)
358 refcount_3 = getrefcount(cls)
360 assert refcount_1 == refcount_3
361 assert refcount_2 > refcount_1
366 with pytest.raises(TypeError)
as excinfo:
367 m.BogusImplicitConversion(0)
371 __init__(): incompatible constructor arguments. The following argument types are supported:
372 1. m.class_.BogusImplicitConversion(arg0: m.class_.BogusImplicitConversion)
380 with pytest.raises(TypeError)
as exc_info:
381 m.test_error_after_conversions(
"hello")
382 assert str(exc_info.value).startswith(
383 "Unable to convert function return value to a Python type!"
389 p = m.Aligned().ptr()
394@pytest.mark.xfail("env.PYPY")
396 with pytest.raises(TypeError)
as exc_info:
398 class PyFinalChild(m.IsFinal):
401 assert str(exc_info.value).endswith(
"is not an acceptable base type")
405@pytest.mark.xfail("env.PYPY")
407 with pytest.raises(TypeError)
as exc_info:
409 class PyNonFinalFinalChild(m.IsNonFinalFinal):
412 assert str(exc_info.value).endswith(
"is not an acceptable base type")
417 with pytest.raises(RuntimeError):
418 m.PyPrintDestructor().throw_something()
424 instances = [m.SamePointer()
for _
in range(n)]
430 instances[i] = m.Empty()
438 assert issubclass(m.DerivedWithNested, m.BaseWithNested)
439 assert m.BaseWithNested.Nested != m.DerivedWithNested.Nested
440 assert m.BaseWithNested.Nested.get_name() ==
"BaseWithNested::Nested"
441 assert m.DerivedWithNested.Nested.get_name() ==
"DerivedWithNested::Nested"
447 module_scope = types.ModuleType(
"module_scope")
448 with pytest.raises(RuntimeError)
as exc_info:
449 m.register_duplicate_class_name(module_scope)
451 'generic_type: cannot initialize type "Duplicate": '
452 "an object with that name is already defined"
454 assert str(exc_info.value) == expected
455 with pytest.raises(RuntimeError)
as exc_info:
456 m.register_duplicate_class_type(module_scope)
457 expected =
'generic_type: type "YetAnotherDuplicate" is already registered!'
458 assert str(exc_info.value) == expected
463 with pytest.raises(RuntimeError)
as exc_info:
464 m.register_duplicate_nested_class_name(ClassScope)
466 'generic_type: cannot initialize type "DuplicateNested": '
467 "an object with that name is already defined"
469 assert str(exc_info.value) == expected
470 with pytest.raises(RuntimeError)
as exc_info:
471 m.register_duplicate_nested_class_type(ClassScope)
472 expected =
'generic_type: type "YetAnotherDuplicateNested" is already registered!'
473 assert str(exc_info.value) == expected
static ConstructorStats & get(std::type_index type)
bool hasattr(handle obj, handle name)
bool isinstance(handle obj)
\rst Return true if obj is an instance of T.
def test_implicit_conversion_life_support()
def test_inheritance_init(msg)
def test_exception_rvalue_abort()
def test_reentrant_implicit_conversion_failure(msg)
def test_register_duplicate_class()
def test_brace_initialization()
def test_operator_new_delete(capture)
def test_override_static()
def test_non_final_final()
def test_instance_new(msg)
def test_type_of_classic()
def test_base_and_derived_nested_scope()
def test_error_after_conversions()
def test_type_of_py_nodelete()
def test_mismatched_holder()
def test_inheritance(msg)
def test_class_refcount()
def test_automatic_upcasting()
def test_bind_protected_functions()
def test_multiple_instances_with_same_pointer(capture)
Annotation for documentation.