1 from __future__
import annotations
8 from pybind11_tests
import ConstructorStats
9 from pybind11_tests
import methods_and_attributes
as m
12 "unreadable attribute" if sys.version_info < (3, 11)
else "object has no getter"
15 "can't set attribute" if sys.version_info < (3, 11)
else "object has no setter"
18 "can't delete attribute" if sys.version_info < (3, 11)
else "object has no deleter"
23 instance1 = m.ExampleMandA()
24 instance2 = m.ExampleMandA(32)
26 instance1.add1(instance2)
27 instance1.add2(instance2)
28 instance1.add3(instance2)
29 instance1.add4(instance2)
30 instance1.add5(instance2)
37 assert str(instance1) ==
"ExampleMandA[value=320]"
38 assert str(instance2) ==
"ExampleMandA[value=32]"
39 assert str(instance1.self1()) ==
"ExampleMandA[value=320]"
40 assert str(instance1.self2()) ==
"ExampleMandA[value=320]"
41 assert str(instance1.self3()) ==
"ExampleMandA[value=320]"
42 assert str(instance1.self4()) ==
"ExampleMandA[value=320]"
43 assert str(instance1.self5()) ==
"ExampleMandA[value=320]"
45 assert instance1.internal1() == 320
46 assert instance1.internal2() == 320
47 assert instance1.internal3() == 320
48 assert instance1.internal4() == 320
49 assert instance1.internal5() == 320
51 assert instance1.overloaded() ==
"()"
52 assert instance1.overloaded(0) ==
"(int)"
53 assert instance1.overloaded(1, 1.0) ==
"(int, float)"
54 assert instance1.overloaded(2.0, 2) ==
"(float, int)"
55 assert instance1.overloaded(3, 3) ==
"(int, int)"
56 assert instance1.overloaded(4.0, 4.0) ==
"(float, float)"
57 assert instance1.overloaded_const(-3) ==
"(int) const"
58 assert instance1.overloaded_const(5, 5.0) ==
"(int, float) const"
59 assert instance1.overloaded_const(6.0, 6) ==
"(float, int) const"
60 assert instance1.overloaded_const(7, 7) ==
"(int, int) const"
61 assert instance1.overloaded_const(8.0, 8.0) ==
"(float, float) const"
62 assert instance1.overloaded_float(1, 1) ==
"(float, float)"
63 assert instance1.overloaded_float(1, 1.0) ==
"(float, float)"
64 assert instance1.overloaded_float(1.0, 1) ==
"(float, float)"
65 assert instance1.overloaded_float(1.0, 1.0) ==
"(float, float)"
67 assert instance1.value == 320
69 assert str(instance1) ==
"ExampleMandA[value=100]"
72 assert cstats.alive() == 2
73 del instance1, instance2
74 assert cstats.alive() == 0
75 assert cstats.values() == [
"32"]
76 assert cstats.default_constructions == 1
77 assert cstats.copy_constructions == 2
78 assert cstats.move_constructions >= 2
79 assert cstats.copy_assignments == 0
80 assert cstats.move_assignments == 0
84 """Issue #443: calling copied methods fails in Python 3"""
86 m.ExampleMandA.add2c = m.ExampleMandA.add2
87 m.ExampleMandA.add2d = m.ExampleMandA.add2b
88 a = m.ExampleMandA(123)
90 a.add2(m.ExampleMandA(-100))
92 a.add2b(m.ExampleMandA(20))
94 a.add2c(m.ExampleMandA(6))
96 a.add2d(m.ExampleMandA(-7))
101 instance = m.TestProperties()
103 assert instance.def_readonly == 1
104 with pytest.raises(AttributeError):
105 instance.def_readonly = 2
107 instance.def_readwrite = 2
108 assert instance.def_readwrite == 2
110 assert instance.def_property_readonly == 2
111 with pytest.raises(AttributeError):
112 instance.def_property_readonly = 3
114 instance.def_property = 3
115 assert instance.def_property == 3
117 with pytest.raises(AttributeError)
as excinfo:
118 dummy = instance.def_property_writeonly
119 assert NO_GETTER_MSG
in str(excinfo.value)
121 instance.def_property_writeonly = 4
122 assert instance.def_property_readonly == 4
124 with pytest.raises(AttributeError)
as excinfo:
125 dummy = instance.def_property_impossible
126 assert NO_GETTER_MSG
in str(excinfo.value)
128 with pytest.raises(AttributeError)
as excinfo:
129 instance.def_property_impossible = 5
130 assert NO_SETTER_MSG
in str(excinfo.value)
134 assert m.TestProperties.def_readonly_static == 1
135 with pytest.raises(AttributeError)
as excinfo:
136 m.TestProperties.def_readonly_static = 2
137 assert NO_SETTER_MSG
in str(excinfo.value)
139 m.TestProperties.def_readwrite_static = 2
140 assert m.TestProperties.def_readwrite_static == 2
142 with pytest.raises(AttributeError)
as excinfo:
143 dummy = m.TestProperties.def_writeonly_static
144 assert NO_GETTER_MSG
in str(excinfo.value)
146 m.TestProperties.def_writeonly_static = 3
147 assert m.TestProperties.def_readonly_static == 3
149 assert m.TestProperties.def_property_readonly_static == 3
150 with pytest.raises(AttributeError)
as excinfo:
151 m.TestProperties.def_property_readonly_static = 99
152 assert NO_SETTER_MSG
in str(excinfo.value)
154 m.TestProperties.def_property_static = 4
155 assert m.TestProperties.def_property_static == 4
157 with pytest.raises(AttributeError)
as excinfo:
158 dummy = m.TestProperties.def_property_writeonly_static
159 assert NO_GETTER_MSG
in str(excinfo.value)
161 m.TestProperties.def_property_writeonly_static = 5
162 assert m.TestProperties.def_property_static == 5
165 instance = m.TestProperties()
167 m.TestProperties.def_readwrite_static = 0
168 assert m.TestProperties.def_readwrite_static == 0
169 assert instance.def_readwrite_static == 0
171 instance.def_readwrite_static = 2
172 assert m.TestProperties.def_readwrite_static == 2
173 assert instance.def_readwrite_static == 2
175 with pytest.raises(AttributeError)
as excinfo:
176 dummy = instance.def_property_writeonly_static
177 assert NO_GETTER_MSG
in str(excinfo.value)
179 instance.def_property_writeonly_static = 4
180 assert instance.def_property_static == 4
183 assert m.TestPropertiesOverride().def_readonly == 99
184 assert m.TestPropertiesOverride.def_readonly_static == 99
187 del m.TestPropertiesOverride.def_readonly_static
188 assert hasattr(m.TestPropertiesOverride,
"def_readonly_static")
190 m.TestPropertiesOverride.def_readonly_static
191 is m.TestProperties.def_readonly_static
193 assert "def_readonly_static" not in m.TestPropertiesOverride.__dict__
194 properties_override = m.TestPropertiesOverride()
195 with pytest.raises(AttributeError)
as excinfo:
196 del properties_override.def_readonly
197 assert NO_DELETER_MSG
in str(excinfo.value)
201 """Static property getter and setters expect the type object as the their only argument"""
203 instance = m.TestProperties()
204 assert m.TestProperties.static_cls
is m.TestProperties
205 assert instance.static_cls
is m.TestProperties
207 def check_self(self):
208 assert self
is m.TestProperties
210 m.TestProperties.static_cls = check_self
211 instance.static_cls = check_self
215 """Overriding pybind11's default metaclass changes the behavior of `static_property`"""
217 assert type(m.ExampleMandA).__name__ ==
"pybind11_type"
218 assert type(m.MetaclassOverride).__name__ ==
"type"
220 assert m.MetaclassOverride.readonly == 1
222 type(m.MetaclassOverride.__dict__[
"readonly"]).__name__
223 ==
"pybind11_static_property"
227 m.MetaclassOverride.readonly = 2
228 assert m.MetaclassOverride.readonly == 2
229 assert isinstance(m.MetaclassOverride.__dict__[
"readonly"], int)
233 from pybind11_tests
import detailed_error_messages_enabled
235 with pytest.raises(RuntimeError)
as excinfo:
236 m.ExampleMandA.add_mixed_overloads1()
239 ==
"overloading a method with both static and instance methods is not supported; "
241 "#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more details"
242 if not detailed_error_messages_enabled
243 else "error while attempting to bind static method ExampleMandA.overload_mixed1"
244 "(arg0: float) -> str"
248 with pytest.raises(RuntimeError)
as excinfo:
249 m.ExampleMandA.add_mixed_overloads2()
252 ==
"overloading a method with both static and instance methods is not supported; "
254 "#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more details"
255 if not detailed_error_messages_enabled
256 else "error while attempting to bind instance method ExampleMandA.overload_mixed2"
257 "(self: pybind11_tests.methods_and_attributes.ExampleMandA, arg0: int, arg1: int)"
263 @pytest.mark.parametrize(
"access", [
"ro",
"rw",
"static_ro",
"static_rw"])
265 obj = m.TestPropRVP()
if not access.startswith(
"static")
else m.TestPropRVP
267 ref =
getattr(obj, access +
"_ref")
268 assert ref.value == 1
270 assert getattr(obj, access +
"_ref").value == 2
273 copy =
getattr(obj, access +
"_copy")
274 assert copy.value == 1
276 assert getattr(obj, access +
"_copy").value == 1
278 copy =
getattr(obj, access +
"_func")
279 assert copy.value == 1
281 assert getattr(obj, access +
"_func").value == 1
285 """When returning an rvalue, the return value policy is automatically changed from
286 `reference(_internal)` to `move`. The following would not work otherwise."""
288 instance = m.TestPropRVP()
292 os = m.TestPropRVP.static_rvalue
297 @pytest.mark.xfail(
"env.PYPY")
299 instance = m.DynamicClass()
300 assert not hasattr(instance,
"foo")
301 assert "foo" not in dir(instance)
305 assert hasattr(instance,
"foo")
306 assert instance.foo == 42
307 assert "foo" in dir(instance)
310 assert "foo" in instance.__dict__
311 instance.__dict__ = {
"bar":
True}
312 assert not hasattr(instance,
"foo")
313 assert hasattr(instance,
"bar")
315 with pytest.raises(TypeError)
as excinfo:
316 instance.__dict__ = []
317 assert str(excinfo.value) ==
"__dict__ must be set to a dictionary, not a 'list'"
320 assert cstats.alive() == 1
322 assert cstats.alive() == 0
325 class PythonDerivedDynamicClass(m.DynamicClass):
328 for cls
in m.CppDerivedDynamicClass, PythonDerivedDynamicClass:
331 assert derived.foobar == 100
333 assert cstats.alive() == 1
335 assert cstats.alive() == 0
339 @pytest.mark.xfail(
"env.PYPY")
342 instance = m.DynamicClass()
343 instance.circular_reference = instance
346 assert cstats.alive() == 1
348 assert cstats.alive() == 0
351 i1 = m.DynamicClass()
352 i2 = m.DynamicClass()
356 assert cstats.alive() == 2
358 assert cstats.alive() == 0
362 from pybind11_tests
import detailed_error_messages_enabled
364 with pytest.raises(RuntimeError)
as excinfo:
365 m.bad_arg_def_named()
366 assert msg(excinfo.value) == (
367 "arg(): could not convert default argument 'a: UnregisteredType' in function "
368 "'should_fail' into a Python object (type not registered yet?)"
369 if detailed_error_messages_enabled
370 else "arg(): could not convert default argument into a Python object (type not registered "
371 "yet?). #define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more information."
374 with pytest.raises(RuntimeError)
as excinfo:
375 m.bad_arg_def_unnamed()
376 assert msg(excinfo.value) == (
377 "arg(): could not convert default argument 'UnregisteredType' in function "
378 "'should_fail' into a Python object (type not registered yet?)"
379 if detailed_error_messages_enabled
380 else "arg(): could not convert default argument into a Python object (type not registered "
381 "yet?). #define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more information."
387 assert m.no_none1(a) == 42
388 assert m.no_none2(a) == 42
389 assert m.no_none3(a) == 42
390 assert m.no_none4(a) == 42
391 assert m.no_none5(a) == 42
392 assert m.ok_none1(a) == 42
393 assert m.ok_none2(a) == 42
394 assert m.ok_none3(a) == 42
395 assert m.ok_none4(a) == 42
396 assert m.ok_none5(a) == 42
398 with pytest.raises(TypeError)
as excinfo:
400 assert "incompatible function arguments" in str(excinfo.value)
401 with pytest.raises(TypeError)
as excinfo:
403 assert "incompatible function arguments" in str(excinfo.value)
404 with pytest.raises(TypeError)
as excinfo:
406 assert "incompatible function arguments" in str(excinfo.value)
407 with pytest.raises(TypeError)
as excinfo:
409 assert "incompatible function arguments" in str(excinfo.value)
410 with pytest.raises(TypeError)
as excinfo:
412 assert "incompatible function arguments" in str(excinfo.value)
415 with pytest.raises(TypeError)
as excinfo:
416 assert m.ok_none1(
None) == -1
420 ok_none1(): incompatible function arguments. The following argument types are supported:
421 1. (arg0: m.methods_and_attributes.NoneTester) -> int
428 assert m.ok_none2(
None) == -1
429 assert m.ok_none3(
None) == -1
430 assert m.ok_none4(
None) == -1
431 assert m.ok_none5(
None) == -1
433 with pytest.raises(TypeError)
as excinfo:
434 m.no_none_kwarg(
None)
435 assert "incompatible function arguments" in str(excinfo.value)
436 with pytest.raises(TypeError)
as excinfo:
437 m.no_none_kwarg(a=
None)
438 assert "incompatible function arguments" in str(excinfo.value)
439 with pytest.raises(TypeError)
as excinfo:
440 m.no_none_kwarg_kw_only(
None)
441 assert "incompatible function arguments" in str(excinfo.value)
442 with pytest.raises(TypeError)
as excinfo:
443 m.no_none_kwarg_kw_only(a=
None)
444 assert "incompatible function arguments" in str(excinfo.value)
448 """#2778: implicit casting from None to object (not pointer)"""
449 a = m.NoneCastTester()
450 assert m.ok_obj_or_none(a) == -1
451 a = m.NoneCastTester(4)
452 assert m.ok_obj_or_none(a) == 4
453 a = m.NoneCastTester(
None)
454 assert m.ok_obj_or_none(a) == -1
455 assert m.ok_obj_or_none(
None) == -1
459 """#283: __str__ called on uninitialized instance when constructor arguments invalid"""
461 assert str(m.StrIssue(3)) ==
"StrIssue[3]"
463 with pytest.raises(TypeError)
as excinfo:
464 str(m.StrIssue(
"no",
"such",
"constructor"))
468 __init__(): incompatible constructor arguments. The following argument types are supported:
469 1. m.methods_and_attributes.StrIssue(arg0: int)
470 2. m.methods_and_attributes.StrIssue()
472 Invoked with: 'no', 'such', 'constructor'
478 a = m.RegisteredDerived()
480 assert a.rw_value == 42
481 assert a.ro_value == 1.25
483 assert a.sum() == 48.25
485 assert a.rw_value == 48
486 assert a.ro_value == 1.5
487 assert a.sum() == 49.5
488 assert a.rw_value_prop == 48
490 assert a.rw_value_prop == 49
492 assert a.ro_value_prop == 1.75
496 """Tests that explicit lvalue ref-qualified methods can be called just like their
497 non ref-qualified counterparts."""
503 assert r.constRefQualified(23) == 40
507 "Check to see if the normal overload order (first defined) and prepend overload order works"
508 assert m.overload_order(
"string") == 1
509 assert m.overload_order(0) == 4
511 assert "1. overload_order(arg0: int) -> int" in m.overload_order.__doc__
512 assert "2. overload_order(arg0: str) -> int" in m.overload_order.__doc__
513 assert "3. overload_order(arg0: str) -> int" in m.overload_order.__doc__
514 assert "4. overload_order(arg0: int) -> int" in m.overload_order.__doc__
516 with pytest.raises(TypeError)
as err:
517 m.overload_order(1.1)
519 assert "1. (arg0: int) -> int" in str(err.value)
520 assert "2. (arg0: str) -> int" in str(err.value)
521 assert "3. (arg0: str) -> int" in str(err.value)
522 assert "4. (arg0: int) -> int" in str(err.value)
526 r = m.RValueRefParam()
527 assert r.func1(
"123") == 3
528 assert r.func2(
"1234") == 4
529 assert r.func3(
"12345") == 5
530 assert r.func4(
"123456") == 6
534 fld = m.exercise_is_setter.Field()
535 assert fld.int_value == -99
536 setter_return = fld.int_value = 100
538 assert setter_return == 100
539 assert fld.int_value == 100