6 from pybind11_tests
import methods_and_attributes
as m
7 from pybind11_tests
import ConstructorStats
11 instance1 = m.ExampleMandA()
12 instance2 = m.ExampleMandA(32)
14 instance1.add1(instance2)
15 instance1.add2(instance2)
16 instance1.add3(instance2)
17 instance1.add4(instance2)
18 instance1.add5(instance2)
25 assert str(instance1) ==
"ExampleMandA[value=320]" 26 assert str(instance2) ==
"ExampleMandA[value=32]" 27 assert str(instance1.self1()) ==
"ExampleMandA[value=320]" 28 assert str(instance1.self2()) ==
"ExampleMandA[value=320]" 29 assert str(instance1.self3()) ==
"ExampleMandA[value=320]" 30 assert str(instance1.self4()) ==
"ExampleMandA[value=320]" 31 assert str(instance1.self5()) ==
"ExampleMandA[value=320]" 33 assert instance1.internal1() == 320
34 assert instance1.internal2() == 320
35 assert instance1.internal3() == 320
36 assert instance1.internal4() == 320
37 assert instance1.internal5() == 320
39 assert instance1.overloaded() ==
"()" 40 assert instance1.overloaded(0) ==
"(int)" 41 assert instance1.overloaded(1, 1.0) ==
"(int, float)" 42 assert instance1.overloaded(2.0, 2) ==
"(float, int)" 43 assert instance1.overloaded(3, 3) ==
"(int, int)" 44 assert instance1.overloaded(4., 4.) ==
"(float, float)" 45 assert instance1.overloaded_const(-3) ==
"(int) const" 46 assert instance1.overloaded_const(5, 5.0) ==
"(int, float) const" 47 assert instance1.overloaded_const(6.0, 6) ==
"(float, int) const" 48 assert instance1.overloaded_const(7, 7) ==
"(int, int) const" 49 assert instance1.overloaded_const(8., 8.) ==
"(float, float) const" 50 assert instance1.overloaded_float(1, 1) ==
"(float, float)" 51 assert instance1.overloaded_float(1, 1.) ==
"(float, float)" 52 assert instance1.overloaded_float(1., 1) ==
"(float, float)" 53 assert instance1.overloaded_float(1., 1.) ==
"(float, float)" 55 assert instance1.value == 320
57 assert str(instance1) ==
"ExampleMandA[value=100]" 60 assert cstats.alive() == 2
61 del instance1, instance2
62 assert cstats.alive() == 0
63 assert cstats.values() == [
"32"]
64 assert cstats.default_constructions == 1
65 assert cstats.copy_constructions == 2
66 assert cstats.move_constructions >= 2
67 assert cstats.copy_assignments == 0
68 assert cstats.move_assignments == 0
72 """Issue #443: calling copied methods fails in Python 3""" 74 m.ExampleMandA.add2c = m.ExampleMandA.add2
75 m.ExampleMandA.add2d = m.ExampleMandA.add2b
76 a = m.ExampleMandA(123)
78 a.add2(m.ExampleMandA(-100))
80 a.add2b(m.ExampleMandA(20))
82 a.add2c(m.ExampleMandA(6))
84 a.add2d(m.ExampleMandA(-7))
89 instance = m.TestProperties()
91 assert instance.def_readonly == 1
92 with pytest.raises(AttributeError):
93 instance.def_readonly = 2
95 instance.def_readwrite = 2
96 assert instance.def_readwrite == 2
98 assert instance.def_property_readonly == 2
99 with pytest.raises(AttributeError):
100 instance.def_property_readonly = 3
102 instance.def_property = 3
103 assert instance.def_property == 3
105 with pytest.raises(AttributeError)
as excinfo:
106 dummy = instance.def_property_writeonly
107 assert "unreadable attribute" in str(excinfo.value)
109 instance.def_property_writeonly = 4
110 assert instance.def_property_readonly == 4
112 with pytest.raises(AttributeError)
as excinfo:
113 dummy = instance.def_property_impossible
114 assert "unreadable attribute" in str(excinfo.value)
116 with pytest.raises(AttributeError)
as excinfo:
117 instance.def_property_impossible = 5
118 assert "can't set attribute" in str(excinfo.value)
122 assert m.TestProperties.def_readonly_static == 1
123 with pytest.raises(AttributeError)
as excinfo:
124 m.TestProperties.def_readonly_static = 2
125 assert "can't set attribute" in str(excinfo.value)
127 m.TestProperties.def_readwrite_static = 2
128 assert m.TestProperties.def_readwrite_static == 2
130 with pytest.raises(AttributeError)
as excinfo:
131 dummy = m.TestProperties.def_writeonly_static
132 assert "unreadable attribute" in str(excinfo.value)
134 m.TestProperties.def_writeonly_static = 3
135 assert m.TestProperties.def_readonly_static == 3
137 assert m.TestProperties.def_property_readonly_static == 3
138 with pytest.raises(AttributeError)
as excinfo:
139 m.TestProperties.def_property_readonly_static = 99
140 assert "can't set attribute" in str(excinfo.value)
142 m.TestProperties.def_property_static = 4
143 assert m.TestProperties.def_property_static == 4
145 with pytest.raises(AttributeError)
as excinfo:
146 dummy = m.TestProperties.def_property_writeonly_static
147 assert "unreadable attribute" in str(excinfo.value)
149 m.TestProperties.def_property_writeonly_static = 5
150 assert m.TestProperties.def_property_static == 5
153 instance = m.TestProperties()
155 m.TestProperties.def_readwrite_static = 0
156 assert m.TestProperties.def_readwrite_static == 0
157 assert instance.def_readwrite_static == 0
159 instance.def_readwrite_static = 2
160 assert m.TestProperties.def_readwrite_static == 2
161 assert instance.def_readwrite_static == 2
163 with pytest.raises(AttributeError)
as excinfo:
164 dummy = instance.def_property_writeonly_static
165 assert "unreadable attribute" in str(excinfo.value)
167 instance.def_property_writeonly_static = 4
168 assert instance.def_property_static == 4
171 assert m.TestPropertiesOverride().def_readonly == 99
172 assert m.TestPropertiesOverride.def_readonly_static == 99
176 """Static property getter and setters expect the type object as the their only argument""" 178 instance = m.TestProperties()
179 assert m.TestProperties.static_cls
is m.TestProperties
180 assert instance.static_cls
is m.TestProperties
182 def check_self(self):
183 assert self
is m.TestProperties
185 m.TestProperties.static_cls = check_self
186 instance.static_cls = check_self
190 """Overriding pybind11's default metaclass changes the behavior of `static_property`""" 192 assert type(m.ExampleMandA).__name__ ==
"pybind11_type" 193 assert type(m.MetaclassOverride).__name__ ==
"type" 195 assert m.MetaclassOverride.readonly == 1
196 assert type(m.MetaclassOverride.__dict__[
"readonly"]).__name__ ==
"pybind11_static_property" 199 m.MetaclassOverride.readonly = 2
200 assert m.MetaclassOverride.readonly == 2
201 assert isinstance(m.MetaclassOverride.__dict__[
"readonly"], int)
205 from pybind11_tests
import debug_enabled
207 with pytest.raises(RuntimeError)
as excinfo:
208 m.ExampleMandA.add_mixed_overloads1()
209 assert (
str(excinfo.value) ==
210 "overloading a method with both static and instance methods is not supported; " +
211 (
"compile in debug mode for more details" if not debug_enabled
else 212 "error while attempting to bind static method ExampleMandA.overload_mixed1" 213 "(arg0: float) -> str")
216 with pytest.raises(RuntimeError)
as excinfo:
217 m.ExampleMandA.add_mixed_overloads2()
218 assert (
str(excinfo.value) ==
219 "overloading a method with both static and instance methods is not supported; " +
220 (
"compile in debug mode for more details" if not debug_enabled
else 221 "error while attempting to bind instance method ExampleMandA.overload_mixed2" 222 "(self: pybind11_tests.methods_and_attributes.ExampleMandA, arg0: int, arg1: int)" 227 @pytest.mark.parametrize(
"access", [
"ro",
"rw",
"static_ro",
"static_rw"])
229 if not access.startswith(
"static"):
230 obj = m.TestPropRVP()
234 ref =
getattr(obj, access +
"_ref")
235 assert ref.value == 1
237 assert getattr(obj, access +
"_ref").value == 2
240 copy =
getattr(obj, access +
"_copy")
241 assert copy.value == 1
243 assert getattr(obj, access +
"_copy").value == 1
245 copy =
getattr(obj, access +
"_func")
246 assert copy.value == 1
248 assert getattr(obj, access +
"_func").value == 1
252 """When returning an rvalue, the return value policy is automatically changed from 253 `reference(_internal)` to `move`. The following would not work otherwise.""" 255 instance = m.TestPropRVP()
259 os = m.TestPropRVP.static_rvalue
264 @pytest.mark.xfail(
"env.PYPY")
266 instance = m.DynamicClass()
267 assert not hasattr(instance,
"foo")
268 assert "foo" not in dir(instance)
272 assert hasattr(instance,
"foo")
273 assert instance.foo == 42
274 assert "foo" in dir(instance)
277 assert "foo" in instance.__dict__
278 instance.__dict__ = {
"bar":
True}
279 assert not hasattr(instance,
"foo")
280 assert hasattr(instance,
"bar")
282 with pytest.raises(TypeError)
as excinfo:
283 instance.__dict__ = []
284 assert str(excinfo.value) ==
"__dict__ must be set to a dictionary, not a 'list'" 287 assert cstats.alive() == 1
289 assert cstats.alive() == 0
292 class PythonDerivedDynamicClass(m.DynamicClass):
295 for cls
in m.CppDerivedDynamicClass, PythonDerivedDynamicClass:
298 assert derived.foobar == 100
300 assert cstats.alive() == 1
302 assert cstats.alive() == 0
306 @pytest.mark.xfail(
"env.PYPY")
309 instance = m.DynamicClass()
310 instance.circular_reference = instance
313 assert cstats.alive() == 1
315 assert cstats.alive() == 0
318 i1 = m.DynamicClass()
319 i2 = m.DynamicClass()
323 assert cstats.alive() == 2
325 assert cstats.alive() == 0
329 from pybind11_tests
import debug_enabled
331 with pytest.raises(RuntimeError)
as excinfo:
332 m.bad_arg_def_named()
333 assert msg(excinfo.value) == (
334 "arg(): could not convert default argument 'a: UnregisteredType' in function " 335 "'should_fail' into a Python object (type not registered yet?)" 336 if debug_enabled
else 337 "arg(): could not convert default argument into a Python object (type not registered " 338 "yet?). Compile in debug mode for more information." 341 with pytest.raises(RuntimeError)
as excinfo:
342 m.bad_arg_def_unnamed()
343 assert msg(excinfo.value) == (
344 "arg(): could not convert default argument 'UnregisteredType' in function " 345 "'should_fail' into a Python object (type not registered yet?)" 346 if debug_enabled
else 347 "arg(): could not convert default argument into a Python object (type not registered " 348 "yet?). Compile in debug mode for more information." 354 assert m.no_none1(a) == 42
355 assert m.no_none2(a) == 42
356 assert m.no_none3(a) == 42
357 assert m.no_none4(a) == 42
358 assert m.no_none5(a) == 42
359 assert m.ok_none1(a) == 42
360 assert m.ok_none2(a) == 42
361 assert m.ok_none3(a) == 42
362 assert m.ok_none4(a) == 42
363 assert m.ok_none5(a) == 42
365 with pytest.raises(TypeError)
as excinfo:
367 assert "incompatible function arguments" in str(excinfo.value)
368 with pytest.raises(TypeError)
as excinfo:
370 assert "incompatible function arguments" in str(excinfo.value)
371 with pytest.raises(TypeError)
as excinfo:
373 assert "incompatible function arguments" in str(excinfo.value)
374 with pytest.raises(TypeError)
as excinfo:
376 assert "incompatible function arguments" in str(excinfo.value)
377 with pytest.raises(TypeError)
as excinfo:
379 assert "incompatible function arguments" in str(excinfo.value)
382 with pytest.raises(TypeError)
as excinfo:
383 assert m.ok_none1(
None) == -1
384 assert msg(excinfo.value) ==
""" 385 ok_none1(): incompatible function arguments. The following argument types are supported: 386 1. (arg0: m.methods_and_attributes.NoneTester) -> int 392 assert m.ok_none2(
None) == -1
393 assert m.ok_none3(
None) == -1
394 assert m.ok_none4(
None) == -1
395 assert m.ok_none5(
None) == -1
399 """#283: __str__ called on uninitialized instance when constructor arguments invalid""" 401 assert str(m.StrIssue(3)) ==
"StrIssue[3]" 403 with pytest.raises(TypeError)
as excinfo:
404 str(m.StrIssue(
"no",
"such",
"constructor"))
405 assert msg(excinfo.value) ==
""" 406 __init__(): incompatible constructor arguments. The following argument types are supported: 407 1. m.methods_and_attributes.StrIssue(arg0: int) 408 2. m.methods_and_attributes.StrIssue() 410 Invoked with: 'no', 'such', 'constructor' 415 a = m.RegisteredDerived()
417 assert a.rw_value == 42
418 assert a.ro_value == 1.25
420 assert a.sum() == 48.25
422 assert a.rw_value == 48
423 assert a.ro_value == 1.5
424 assert a.sum() == 49.5
425 assert a.rw_value_prop == 48
427 assert a.rw_value_prop == 49
429 assert a.ro_value_prop == 1.75
433 """Tests that explicit lvalue ref-qualified methods can be called just like their 434 non ref-qualified counterparts.""" 440 assert r.constRefQualified(23) == 40
bool hasattr(handle obj, handle name)
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)