6 from pybind11_tests
import ConstructorStats
7 from pybind11_tests
import methods_and_attributes
as m
10 "unreadable attribute" if sys.version_info < (3, 11)
else "object has no getter" 13 "can't set attribute" if sys.version_info < (3, 11)
else "object has no setter" 16 "can't delete attribute" if sys.version_info < (3, 11)
else "object has no deleter" 21 instance1 = m.ExampleMandA()
22 instance2 = m.ExampleMandA(32)
24 instance1.add1(instance2)
25 instance1.add2(instance2)
26 instance1.add3(instance2)
27 instance1.add4(instance2)
28 instance1.add5(instance2)
35 assert str(instance1) ==
"ExampleMandA[value=320]" 36 assert str(instance2) ==
"ExampleMandA[value=32]" 37 assert str(instance1.self1()) ==
"ExampleMandA[value=320]" 38 assert str(instance1.self2()) ==
"ExampleMandA[value=320]" 39 assert str(instance1.self3()) ==
"ExampleMandA[value=320]" 40 assert str(instance1.self4()) ==
"ExampleMandA[value=320]" 41 assert str(instance1.self5()) ==
"ExampleMandA[value=320]" 43 assert instance1.internal1() == 320
44 assert instance1.internal2() == 320
45 assert instance1.internal3() == 320
46 assert instance1.internal4() == 320
47 assert instance1.internal5() == 320
49 assert instance1.overloaded() ==
"()" 50 assert instance1.overloaded(0) ==
"(int)" 51 assert instance1.overloaded(1, 1.0) ==
"(int, float)" 52 assert instance1.overloaded(2.0, 2) ==
"(float, int)" 53 assert instance1.overloaded(3, 3) ==
"(int, int)" 54 assert instance1.overloaded(4.0, 4.0) ==
"(float, float)" 55 assert instance1.overloaded_const(-3) ==
"(int) const" 56 assert instance1.overloaded_const(5, 5.0) ==
"(int, float) const" 57 assert instance1.overloaded_const(6.0, 6) ==
"(float, int) const" 58 assert instance1.overloaded_const(7, 7) ==
"(int, int) const" 59 assert instance1.overloaded_const(8.0, 8.0) ==
"(float, float) const" 60 assert instance1.overloaded_float(1, 1) ==
"(float, float)" 61 assert instance1.overloaded_float(1, 1.0) ==
"(float, float)" 62 assert instance1.overloaded_float(1.0, 1) ==
"(float, float)" 63 assert instance1.overloaded_float(1.0, 1.0) ==
"(float, float)" 65 assert instance1.value == 320
67 assert str(instance1) ==
"ExampleMandA[value=100]" 70 assert cstats.alive() == 2
71 del instance1, instance2
72 assert cstats.alive() == 0
73 assert cstats.values() == [
"32"]
74 assert cstats.default_constructions == 1
75 assert cstats.copy_constructions == 2
76 assert cstats.move_constructions >= 2
77 assert cstats.copy_assignments == 0
78 assert cstats.move_assignments == 0
82 """Issue #443: calling copied methods fails in Python 3""" 84 m.ExampleMandA.add2c = m.ExampleMandA.add2
85 m.ExampleMandA.add2d = m.ExampleMandA.add2b
86 a = m.ExampleMandA(123)
88 a.add2(m.ExampleMandA(-100))
90 a.add2b(m.ExampleMandA(20))
92 a.add2c(m.ExampleMandA(6))
94 a.add2d(m.ExampleMandA(-7))
99 instance = m.TestProperties()
101 assert instance.def_readonly == 1
102 with pytest.raises(AttributeError):
103 instance.def_readonly = 2
105 instance.def_readwrite = 2
106 assert instance.def_readwrite == 2
108 assert instance.def_property_readonly == 2
109 with pytest.raises(AttributeError):
110 instance.def_property_readonly = 3
112 instance.def_property = 3
113 assert instance.def_property == 3
115 with pytest.raises(AttributeError)
as excinfo:
116 dummy = instance.def_property_writeonly
117 assert NO_GETTER_MSG
in str(excinfo.value)
119 instance.def_property_writeonly = 4
120 assert instance.def_property_readonly == 4
122 with pytest.raises(AttributeError)
as excinfo:
123 dummy = instance.def_property_impossible
124 assert NO_GETTER_MSG
in str(excinfo.value)
126 with pytest.raises(AttributeError)
as excinfo:
127 instance.def_property_impossible = 5
128 assert NO_SETTER_MSG
in str(excinfo.value)
132 assert m.TestProperties.def_readonly_static == 1
133 with pytest.raises(AttributeError)
as excinfo:
134 m.TestProperties.def_readonly_static = 2
135 assert NO_SETTER_MSG
in str(excinfo.value)
137 m.TestProperties.def_readwrite_static = 2
138 assert m.TestProperties.def_readwrite_static == 2
140 with pytest.raises(AttributeError)
as excinfo:
141 dummy = m.TestProperties.def_writeonly_static
142 assert NO_GETTER_MSG
in str(excinfo.value)
144 m.TestProperties.def_writeonly_static = 3
145 assert m.TestProperties.def_readonly_static == 3
147 assert m.TestProperties.def_property_readonly_static == 3
148 with pytest.raises(AttributeError)
as excinfo:
149 m.TestProperties.def_property_readonly_static = 99
150 assert NO_SETTER_MSG
in str(excinfo.value)
152 m.TestProperties.def_property_static = 4
153 assert m.TestProperties.def_property_static == 4
155 with pytest.raises(AttributeError)
as excinfo:
156 dummy = m.TestProperties.def_property_writeonly_static
157 assert NO_GETTER_MSG
in str(excinfo.value)
159 m.TestProperties.def_property_writeonly_static = 5
160 assert m.TestProperties.def_property_static == 5
163 instance = m.TestProperties()
165 m.TestProperties.def_readwrite_static = 0
166 assert m.TestProperties.def_readwrite_static == 0
167 assert instance.def_readwrite_static == 0
169 instance.def_readwrite_static = 2
170 assert m.TestProperties.def_readwrite_static == 2
171 assert instance.def_readwrite_static == 2
173 with pytest.raises(AttributeError)
as excinfo:
174 dummy = instance.def_property_writeonly_static
175 assert NO_GETTER_MSG
in str(excinfo.value)
177 instance.def_property_writeonly_static = 4
178 assert instance.def_property_static == 4
181 assert m.TestPropertiesOverride().def_readonly == 99
182 assert m.TestPropertiesOverride.def_readonly_static == 99
185 del m.TestPropertiesOverride.def_readonly_static
187 hasattr(m.TestPropertiesOverride,
"def_readonly_static")
188 and m.TestPropertiesOverride.def_readonly_static
189 is m.TestProperties.def_readonly_static
191 assert "def_readonly_static" not in m.TestPropertiesOverride.__dict__
192 properties_override = m.TestPropertiesOverride()
193 with pytest.raises(AttributeError)
as excinfo:
194 del properties_override.def_readonly
195 assert NO_DELETER_MSG
in str(excinfo.value)
199 """Static property getter and setters expect the type object as the their only argument""" 201 instance = m.TestProperties()
202 assert m.TestProperties.static_cls
is m.TestProperties
203 assert instance.static_cls
is m.TestProperties
205 def check_self(self):
206 assert self
is m.TestProperties
208 m.TestProperties.static_cls = check_self
209 instance.static_cls = check_self
213 """Overriding pybind11's default metaclass changes the behavior of `static_property`""" 215 assert type(m.ExampleMandA).__name__ ==
"pybind11_type" 216 assert type(m.MetaclassOverride).__name__ ==
"type" 218 assert m.MetaclassOverride.readonly == 1
220 type(m.MetaclassOverride.__dict__[
"readonly"]).__name__
221 ==
"pybind11_static_property" 225 m.MetaclassOverride.readonly = 2
226 assert m.MetaclassOverride.readonly == 2
227 assert isinstance(m.MetaclassOverride.__dict__[
"readonly"], int)
231 from pybind11_tests
import detailed_error_messages_enabled
233 with pytest.raises(RuntimeError)
as excinfo:
234 m.ExampleMandA.add_mixed_overloads1()
237 ) ==
"overloading a method with both static and instance methods is not supported; " + (
238 "#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more details" 239 if not detailed_error_messages_enabled
240 else "error while attempting to bind static method ExampleMandA.overload_mixed1" 241 "(arg0: float) -> str" 244 with pytest.raises(RuntimeError)
as excinfo:
245 m.ExampleMandA.add_mixed_overloads2()
248 ) ==
"overloading a method with both static and instance methods is not supported; " + (
249 "#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more details" 250 if not detailed_error_messages_enabled
251 else "error while attempting to bind instance method ExampleMandA.overload_mixed2" 252 "(self: pybind11_tests.methods_and_attributes.ExampleMandA, arg0: int, arg1: int)" 257 @pytest.mark.parametrize(
"access", [
"ro",
"rw",
"static_ro",
"static_rw"])
259 if not access.startswith(
"static"):
260 obj = m.TestPropRVP()
264 ref =
getattr(obj, access +
"_ref")
265 assert ref.value == 1
267 assert getattr(obj, access +
"_ref").value == 2
270 copy =
getattr(obj, access +
"_copy")
271 assert copy.value == 1
273 assert getattr(obj, access +
"_copy").value == 1
275 copy =
getattr(obj, access +
"_func")
276 assert copy.value == 1
278 assert getattr(obj, access +
"_func").value == 1
282 """When returning an rvalue, the return value policy is automatically changed from 283 `reference(_internal)` to `move`. The following would not work otherwise.""" 285 instance = m.TestPropRVP()
289 os = m.TestPropRVP.static_rvalue
294 @pytest.mark.xfail(
"env.PYPY")
296 instance = m.DynamicClass()
297 assert not hasattr(instance,
"foo")
298 assert "foo" not in dir(instance)
302 assert hasattr(instance,
"foo")
303 assert instance.foo == 42
304 assert "foo" in dir(instance)
307 assert "foo" in instance.__dict__
308 instance.__dict__ = {
"bar":
True}
309 assert not hasattr(instance,
"foo")
310 assert hasattr(instance,
"bar")
312 with pytest.raises(TypeError)
as excinfo:
313 instance.__dict__ = []
314 assert str(excinfo.value) ==
"__dict__ must be set to a dictionary, not a 'list'" 317 assert cstats.alive() == 1
319 assert cstats.alive() == 0
322 class PythonDerivedDynamicClass(m.DynamicClass):
325 for cls
in m.CppDerivedDynamicClass, PythonDerivedDynamicClass:
328 assert derived.foobar == 100
330 assert cstats.alive() == 1
332 assert cstats.alive() == 0
336 @pytest.mark.xfail(
"env.PYPY")
339 instance = m.DynamicClass()
340 instance.circular_reference = instance
343 assert cstats.alive() == 1
345 assert cstats.alive() == 0
348 i1 = m.DynamicClass()
349 i2 = m.DynamicClass()
353 assert cstats.alive() == 2
355 assert cstats.alive() == 0
359 from pybind11_tests
import detailed_error_messages_enabled
361 with pytest.raises(RuntimeError)
as excinfo:
362 m.bad_arg_def_named()
363 assert msg(excinfo.value) == (
364 "arg(): could not convert default argument 'a: UnregisteredType' in function " 365 "'should_fail' into a Python object (type not registered yet?)" 366 if detailed_error_messages_enabled
367 else "arg(): could not convert default argument into a Python object (type not registered " 368 "yet?). #define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more information." 371 with pytest.raises(RuntimeError)
as excinfo:
372 m.bad_arg_def_unnamed()
373 assert msg(excinfo.value) == (
374 "arg(): could not convert default argument 'UnregisteredType' in function " 375 "'should_fail' into a Python object (type not registered yet?)" 376 if detailed_error_messages_enabled
377 else "arg(): could not convert default argument into a Python object (type not registered " 378 "yet?). #define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more information." 384 assert m.no_none1(a) == 42
385 assert m.no_none2(a) == 42
386 assert m.no_none3(a) == 42
387 assert m.no_none4(a) == 42
388 assert m.no_none5(a) == 42
389 assert m.ok_none1(a) == 42
390 assert m.ok_none2(a) == 42
391 assert m.ok_none3(a) == 42
392 assert m.ok_none4(a) == 42
393 assert m.ok_none5(a) == 42
395 with pytest.raises(TypeError)
as excinfo:
397 assert "incompatible function arguments" in str(excinfo.value)
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)
412 with pytest.raises(TypeError)
as excinfo:
413 assert m.ok_none1(
None) == -1
417 ok_none1(): incompatible function arguments. The following argument types are supported: 418 1. (arg0: m.methods_and_attributes.NoneTester) -> int 425 assert m.ok_none2(
None) == -1
426 assert m.ok_none3(
None) == -1
427 assert m.ok_none4(
None) == -1
428 assert m.ok_none5(
None) == -1
430 with pytest.raises(TypeError)
as excinfo:
431 m.no_none_kwarg(
None)
432 assert "incompatible function arguments" in str(excinfo.value)
433 with pytest.raises(TypeError)
as excinfo:
434 m.no_none_kwarg(a=
None)
435 assert "incompatible function arguments" in str(excinfo.value)
436 with pytest.raises(TypeError)
as excinfo:
437 m.no_none_kwarg_kw_only(
None)
438 assert "incompatible function arguments" in str(excinfo.value)
439 with pytest.raises(TypeError)
as excinfo:
440 m.no_none_kwarg_kw_only(a=
None)
441 assert "incompatible function arguments" in str(excinfo.value)
445 """#2778: implicit casting from None to object (not pointer)""" 446 a = m.NoneCastTester()
447 assert m.ok_obj_or_none(a) == -1
448 a = m.NoneCastTester(4)
449 assert m.ok_obj_or_none(a) == 4
450 a = m.NoneCastTester(
None)
451 assert m.ok_obj_or_none(a) == -1
452 assert m.ok_obj_or_none(
None) == -1
456 """#283: __str__ called on uninitialized instance when constructor arguments invalid""" 458 assert str(m.StrIssue(3)) ==
"StrIssue[3]" 460 with pytest.raises(TypeError)
as excinfo:
461 str(m.StrIssue(
"no",
"such",
"constructor"))
465 __init__(): incompatible constructor arguments. The following argument types are supported: 466 1. m.methods_and_attributes.StrIssue(arg0: int) 467 2. m.methods_and_attributes.StrIssue() 469 Invoked with: 'no', 'such', 'constructor' 475 a = m.RegisteredDerived()
477 assert a.rw_value == 42
478 assert a.ro_value == 1.25
480 assert a.sum() == 48.25
482 assert a.rw_value == 48
483 assert a.ro_value == 1.5
484 assert a.sum() == 49.5
485 assert a.rw_value_prop == 48
487 assert a.rw_value_prop == 49
489 assert a.ro_value_prop == 1.75
493 """Tests that explicit lvalue ref-qualified methods can be called just like their 494 non ref-qualified counterparts.""" 500 assert r.constRefQualified(23) == 40
504 "Check to see if the normal overload order (first defined) and prepend overload order works" 505 assert m.overload_order(
"string") == 1
506 assert m.overload_order(0) == 4
508 assert "1. overload_order(arg0: int) -> int" in m.overload_order.__doc__
509 assert "2. overload_order(arg0: str) -> int" in m.overload_order.__doc__
510 assert "3. overload_order(arg0: str) -> int" in m.overload_order.__doc__
511 assert "4. overload_order(arg0: int) -> int" in m.overload_order.__doc__
513 with pytest.raises(TypeError)
as err:
514 m.overload_order(1.1)
516 assert "1. (arg0: int) -> int" in str(err.value)
517 assert "2. (arg0: str) -> int" in str(err.value)
518 assert "3. (arg0: str) -> int" in str(err.value)
519 assert "4. (arg0: int) -> int" in str(err.value)
523 r = m.RValueRefParam()
524 assert r.func1(
"123") == 3
525 assert r.func2(
"1234") == 4
526 assert r.func3(
"12345") == 5
527 assert r.func4(
"123456") == 6
bool hasattr(handle obj, handle name)
def test_rvalue_ref_param()
def test_overload_ordering()
def test_methods_and_attributes()
def test_unregistered_base_implementations()
bool isinstance(handle obj)
def test_static_properties()
def test_dynamic_attributes()
def test_bad_arg_default(msg)
static ConstructorStats & get(std::type_index type)
def test_property_return_value_policies(access)
def test_metaclass_override()
object getattr(handle obj, handle name)
def test_property_rvalue_policy()
def test_no_mixed_overloads()
def test_accepts_none(msg)