2 from pybind11_tests
import methods_and_attributes
as m
3 from pybind11_tests
import ConstructorStats
7 instance1 = m.ExampleMandA()
8 instance2 = m.ExampleMandA(32)
10 instance1.add1(instance2)
11 instance1.add2(instance2)
12 instance1.add3(instance2)
13 instance1.add4(instance2)
14 instance1.add5(instance2)
21 assert str(instance1) ==
"ExampleMandA[value=320]" 22 assert str(instance2) ==
"ExampleMandA[value=32]" 23 assert str(instance1.self1()) ==
"ExampleMandA[value=320]" 24 assert str(instance1.self2()) ==
"ExampleMandA[value=320]" 25 assert str(instance1.self3()) ==
"ExampleMandA[value=320]" 26 assert str(instance1.self4()) ==
"ExampleMandA[value=320]" 27 assert str(instance1.self5()) ==
"ExampleMandA[value=320]" 29 assert instance1.internal1() == 320
30 assert instance1.internal2() == 320
31 assert instance1.internal3() == 320
32 assert instance1.internal4() == 320
33 assert instance1.internal5() == 320
35 assert instance1.overloaded() ==
"()" 36 assert instance1.overloaded(0) ==
"(int)" 37 assert instance1.overloaded(1, 1.0) ==
"(int, float)" 38 assert instance1.overloaded(2.0, 2) ==
"(float, int)" 39 assert instance1.overloaded(3, 3) ==
"(int, int)" 40 assert instance1.overloaded(4., 4.) ==
"(float, float)" 41 assert instance1.overloaded_const(-3) ==
"(int) const" 42 assert instance1.overloaded_const(5, 5.0) ==
"(int, float) const" 43 assert instance1.overloaded_const(6.0, 6) ==
"(float, int) const" 44 assert instance1.overloaded_const(7, 7) ==
"(int, int) const" 45 assert instance1.overloaded_const(8., 8.) ==
"(float, float) const" 46 assert instance1.overloaded_float(1, 1) ==
"(float, float)" 47 assert instance1.overloaded_float(1, 1.) ==
"(float, float)" 48 assert instance1.overloaded_float(1., 1) ==
"(float, float)" 49 assert instance1.overloaded_float(1., 1.) ==
"(float, float)" 51 assert instance1.value == 320
53 assert str(instance1) ==
"ExampleMandA[value=100]" 56 assert cstats.alive() == 2
57 del instance1, instance2
58 assert cstats.alive() == 0
59 assert cstats.values() == [
"32"]
60 assert cstats.default_constructions == 1
61 assert cstats.copy_constructions == 3
62 assert cstats.move_constructions >= 1
63 assert cstats.copy_assignments == 0
64 assert cstats.move_assignments == 0
68 """Issue #443: calling copied methods fails in Python 3""" 70 m.ExampleMandA.add2c = m.ExampleMandA.add2
71 m.ExampleMandA.add2d = m.ExampleMandA.add2b
72 a = m.ExampleMandA(123)
74 a.add2(m.ExampleMandA(-100))
76 a.add2b(m.ExampleMandA(20))
78 a.add2c(m.ExampleMandA(6))
80 a.add2d(m.ExampleMandA(-7))
85 instance = m.TestProperties()
87 assert instance.def_readonly == 1
88 with pytest.raises(AttributeError):
89 instance.def_readonly = 2
91 instance.def_readwrite = 2
92 assert instance.def_readwrite == 2
94 assert instance.def_property_readonly == 2
95 with pytest.raises(AttributeError):
96 instance.def_property_readonly = 3
98 instance.def_property = 3
99 assert instance.def_property == 3
101 with pytest.raises(AttributeError)
as excinfo:
102 dummy = instance.def_property_writeonly
103 assert "unreadable attribute" in str(excinfo.value)
105 instance.def_property_writeonly = 4
106 assert instance.def_property_readonly == 4
108 with pytest.raises(AttributeError)
as excinfo:
109 dummy = instance.def_property_impossible
110 assert "unreadable attribute" in str(excinfo.value)
112 with pytest.raises(AttributeError)
as excinfo:
113 instance.def_property_impossible = 5
114 assert "can't set attribute" in str(excinfo.value)
118 assert m.TestProperties.def_readonly_static == 1
119 with pytest.raises(AttributeError)
as excinfo:
120 m.TestProperties.def_readonly_static = 2
121 assert "can't set attribute" in str(excinfo.value)
123 m.TestProperties.def_readwrite_static = 2
124 assert m.TestProperties.def_readwrite_static == 2
126 with pytest.raises(AttributeError)
as excinfo:
127 dummy = m.TestProperties.def_writeonly_static
128 assert "unreadable attribute" in str(excinfo.value)
130 m.TestProperties.def_writeonly_static = 3
131 assert m.TestProperties.def_readonly_static == 3
133 assert m.TestProperties.def_property_readonly_static == 3
134 with pytest.raises(AttributeError)
as excinfo:
135 m.TestProperties.def_property_readonly_static = 99
136 assert "can't set attribute" in str(excinfo.value)
138 m.TestProperties.def_property_static = 4
139 assert m.TestProperties.def_property_static == 4
141 with pytest.raises(AttributeError)
as excinfo:
142 dummy = m.TestProperties.def_property_writeonly_static
143 assert "unreadable attribute" in str(excinfo.value)
145 m.TestProperties.def_property_writeonly_static = 5
146 assert m.TestProperties.def_property_static == 5
149 instance = m.TestProperties()
151 m.TestProperties.def_readwrite_static = 0
152 assert m.TestProperties.def_readwrite_static == 0
153 assert instance.def_readwrite_static == 0
155 instance.def_readwrite_static = 2
156 assert m.TestProperties.def_readwrite_static == 2
157 assert instance.def_readwrite_static == 2
159 with pytest.raises(AttributeError)
as excinfo:
160 dummy = instance.def_property_writeonly_static
161 assert "unreadable attribute" in str(excinfo.value)
163 instance.def_property_writeonly_static = 4
164 assert instance.def_property_static == 4
167 assert m.TestPropertiesOverride().def_readonly == 99
168 assert m.TestPropertiesOverride.def_readonly_static == 99
172 """Static property getter and setters expect the type object as the their only argument""" 174 instance = m.TestProperties()
175 assert m.TestProperties.static_cls
is m.TestProperties
176 assert instance.static_cls
is m.TestProperties
178 def check_self(self):
179 assert self
is m.TestProperties
181 m.TestProperties.static_cls = check_self
182 instance.static_cls = check_self
186 """Overriding pybind11's default metaclass changes the behavior of `static_property`""" 188 assert type(m.ExampleMandA).__name__ ==
"pybind11_type" 189 assert type(m.MetaclassOverride).__name__ ==
"type" 191 assert m.MetaclassOverride.readonly == 1
192 assert type(m.MetaclassOverride.__dict__[
"readonly"]).__name__ ==
"pybind11_static_property" 195 m.MetaclassOverride.readonly = 2
196 assert m.MetaclassOverride.readonly == 2
197 assert isinstance(m.MetaclassOverride.__dict__[
"readonly"], int)
201 from pybind11_tests
import debug_enabled
203 with pytest.raises(RuntimeError)
as excinfo:
204 m.ExampleMandA.add_mixed_overloads1()
205 assert (
str(excinfo.value) ==
206 "overloading a method with both static and instance methods is not supported; " +
207 (
"compile in debug mode for more details" if not debug_enabled
else 208 "error while attempting to bind static method ExampleMandA.overload_mixed1" 209 "(arg0: float) -> str")
212 with pytest.raises(RuntimeError)
as excinfo:
213 m.ExampleMandA.add_mixed_overloads2()
214 assert (
str(excinfo.value) ==
215 "overloading a method with both static and instance methods is not supported; " +
216 (
"compile in debug mode for more details" if not debug_enabled
else 217 "error while attempting to bind instance method ExampleMandA.overload_mixed2" 218 "(self: pybind11_tests.methods_and_attributes.ExampleMandA, arg0: int, arg1: int)" 223 @pytest.mark.parametrize(
"access", [
"ro",
"rw",
"static_ro",
"static_rw"])
225 if not access.startswith(
"static"):
226 obj = m.TestPropRVP()
230 ref =
getattr(obj, access +
"_ref")
231 assert ref.value == 1
233 assert getattr(obj, access +
"_ref").value == 2
236 copy =
getattr(obj, access +
"_copy")
237 assert copy.value == 1
239 assert getattr(obj, access +
"_copy").value == 1
241 copy =
getattr(obj, access +
"_func")
242 assert copy.value == 1
244 assert getattr(obj, access +
"_func").value == 1
248 """When returning an rvalue, the return value policy is automatically changed from 249 `reference(_internal)` to `move`. The following would not work otherwise.""" 251 instance = m.TestPropRVP()
255 os = m.TestPropRVP.static_rvalue
260 @pytest.unsupported_on_pypy
262 instance = m.DynamicClass()
263 assert not hasattr(instance,
"foo")
264 assert "foo" not in dir(instance)
268 assert hasattr(instance,
"foo")
269 assert instance.foo == 42
270 assert "foo" in dir(instance)
273 assert "foo" in instance.__dict__
274 instance.__dict__ = {
"bar":
True}
275 assert not hasattr(instance,
"foo")
276 assert hasattr(instance,
"bar")
278 with pytest.raises(TypeError)
as excinfo:
279 instance.__dict__ = []
280 assert str(excinfo.value) ==
"__dict__ must be set to a dictionary, not a 'list'" 283 assert cstats.alive() == 1
285 assert cstats.alive() == 0
288 class PythonDerivedDynamicClass(m.DynamicClass):
291 for cls
in m.CppDerivedDynamicClass, PythonDerivedDynamicClass:
294 assert derived.foobar == 100
296 assert cstats.alive() == 1
298 assert cstats.alive() == 0
302 @pytest.unsupported_on_pypy
305 instance = m.DynamicClass()
306 instance.circular_reference = instance
309 assert cstats.alive() == 1
311 assert cstats.alive() == 0
314 i1 = m.DynamicClass()
315 i2 = m.DynamicClass()
319 assert cstats.alive() == 2
321 assert cstats.alive() == 0
326 assert msg(a.f(
"hi")) ==
""" 327 loading ArgInspector1 argument WITH conversion allowed. Argument value = hi 329 assert msg(a.g(
"this is a",
"this is b")) ==
""" 330 loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a 331 loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b 333 loading ArgInspector2 argument WITH conversion allowed. Argument value = (default arg inspector 2) 335 assert msg(a.g(
"this is a",
"this is b", 42)) ==
""" 336 loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a 337 loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b 339 loading ArgInspector2 argument WITH conversion allowed. Argument value = (default arg inspector 2) 341 assert msg(a.g(
"this is a",
"this is b", 42,
"this is d")) ==
""" 342 loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a 343 loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b 345 loading ArgInspector2 argument WITH conversion allowed. Argument value = this is d 347 assert (a.h(
"arg 1") ==
348 "loading ArgInspector2 argument WITHOUT conversion allowed. Argument value = arg 1")
349 assert msg(m.arg_inspect_func(
"A1",
"A2")) ==
""" 350 loading ArgInspector2 argument WITH conversion allowed. Argument value = A1 351 loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = A2 354 assert m.floats_preferred(4) == 2.0
355 assert m.floats_only(4.0) == 2.0
356 with pytest.raises(TypeError)
as excinfo:
358 assert msg(excinfo.value) ==
""" 359 floats_only(): incompatible function arguments. The following argument types are supported: 360 1. (f: float) -> float 365 assert m.ints_preferred(4) == 2
366 assert m.ints_preferred(
True) == 0
367 with pytest.raises(TypeError)
as excinfo:
368 m.ints_preferred(4.0)
369 assert msg(excinfo.value) ==
""" 370 ints_preferred(): incompatible function arguments. The following argument types are supported: 376 assert m.ints_only(4) == 2
377 with pytest.raises(TypeError)
as excinfo:
379 assert msg(excinfo.value) ==
""" 380 ints_only(): incompatible function arguments. The following argument types are supported: 388 from pybind11_tests
import debug_enabled
390 with pytest.raises(RuntimeError)
as excinfo:
391 m.bad_arg_def_named()
392 assert msg(excinfo.value) == (
393 "arg(): could not convert default argument 'a: UnregisteredType' in function " 394 "'should_fail' into a Python object (type not registered yet?)" 395 if debug_enabled
else 396 "arg(): could not convert default argument into a Python object (type not registered " 397 "yet?). Compile in debug mode for more information." 400 with pytest.raises(RuntimeError)
as excinfo:
401 m.bad_arg_def_unnamed()
402 assert msg(excinfo.value) == (
403 "arg(): could not convert default argument 'UnregisteredType' in function " 404 "'should_fail' into a Python object (type not registered yet?)" 405 if debug_enabled
else 406 "arg(): could not convert default argument into a Python object (type not registered " 407 "yet?). Compile in debug mode for more information." 413 assert m.no_none1(a) == 42
414 assert m.no_none2(a) == 42
415 assert m.no_none3(a) == 42
416 assert m.no_none4(a) == 42
417 assert m.no_none5(a) == 42
418 assert m.ok_none1(a) == 42
419 assert m.ok_none2(a) == 42
420 assert m.ok_none3(a) == 42
421 assert m.ok_none4(a) == 42
422 assert m.ok_none5(a) == 42
424 with pytest.raises(TypeError)
as excinfo:
426 assert "incompatible function arguments" in str(excinfo.value)
427 with pytest.raises(TypeError)
as excinfo:
429 assert "incompatible function arguments" in str(excinfo.value)
430 with pytest.raises(TypeError)
as excinfo:
432 assert "incompatible function arguments" in str(excinfo.value)
433 with pytest.raises(TypeError)
as excinfo:
435 assert "incompatible function arguments" in str(excinfo.value)
436 with pytest.raises(TypeError)
as excinfo:
438 assert "incompatible function arguments" in str(excinfo.value)
441 with pytest.raises(TypeError)
as excinfo:
442 assert m.ok_none1(
None) == -1
443 assert msg(excinfo.value) ==
""" 444 ok_none1(): incompatible function arguments. The following argument types are supported: 445 1. (arg0: m.methods_and_attributes.NoneTester) -> int 451 assert m.ok_none2(
None) == -1
452 assert m.ok_none3(
None) == -1
453 assert m.ok_none4(
None) == -1
454 assert m.ok_none5(
None) == -1
458 """#283: __str__ called on uninitialized instance when constructor arguments invalid""" 460 assert str(m.StrIssue(3)) ==
"StrIssue[3]" 462 with pytest.raises(TypeError)
as excinfo:
463 str(m.StrIssue(
"no",
"such",
"constructor"))
464 assert msg(excinfo.value) ==
""" 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' 474 a = m.RegisteredDerived()
476 assert a.rw_value == 42
477 assert a.ro_value == 1.25
479 assert a.sum() == 48.25
481 assert a.rw_value == 48
482 assert a.ro_value == 1.5
483 assert a.sum() == 49.5
484 assert a.rw_value_prop == 48
486 assert a.rw_value_prop == 49
488 assert a.ro_value_prop == 1.75
492 """Tests that returning a pointer to a type that gets converted with a custom type caster gets 493 destroyed when the function has py::return_value_policy::take_ownership policy applied.""" 495 cstats = m.destruction_tester_cstats()
497 z = m.custom_caster_no_destroy()
498 assert cstats.alive() == 1
and cstats.default_constructions == 1
502 z = m.custom_caster_destroy()
504 assert cstats.default_constructions == 2
507 z = m.custom_caster_destroy_const()
509 assert cstats.default_constructions == 3
512 assert cstats.alive() == 1
object getattr(handle obj, handle name)
static ConstructorStats & get(std::type_index type)
def test_custom_caster_destruction()
def test_methods_and_attributes()
def test_unregistered_base_implementations()
def test_static_properties()
def test_dynamic_attributes()
def test_bad_arg_default(msg)
return isinstance(obj, type)
def test_property_return_value_policies(access)
def test_metaclass_override()
const detail::type_info * type
def test_property_rvalue_policy()
def test_noconvert_args(msg)
def test_no_mixed_overloads()
def test_accepts_none(msg)
bool hasattr(handle obj, handle name)