test_methods_and_attributes.py (11986:c12e4625ab56) test_methods_and_attributes.py (12037:d28054ac6ec9)
1import pytest
2from pybind11_tests import ExampleMandA, ConstructorStats
3
4
5def test_methods_and_attributes():
6 instance1 = ExampleMandA()
7 instance2 = ExampleMandA(32)
8
9 instance1.add1(instance2)
10 instance1.add2(instance2)
11 instance1.add3(instance2)
12 instance1.add4(instance2)
13 instance1.add5(instance2)
14 instance1.add6(32)
15 instance1.add7(32)
16 instance1.add8(32)
17 instance1.add9(32)
18 instance1.add10(32)
19
20 assert str(instance1) == "ExampleMandA[value=320]"
21 assert str(instance2) == "ExampleMandA[value=32]"
22 assert str(instance1.self1()) == "ExampleMandA[value=320]"
23 assert str(instance1.self2()) == "ExampleMandA[value=320]"
24 assert str(instance1.self3()) == "ExampleMandA[value=320]"
25 assert str(instance1.self4()) == "ExampleMandA[value=320]"
26 assert str(instance1.self5()) == "ExampleMandA[value=320]"
27
28 assert instance1.internal1() == 320
29 assert instance1.internal2() == 320
30 assert instance1.internal3() == 320
31 assert instance1.internal4() == 320
32 assert instance1.internal5() == 320
33
34 assert instance1.overloaded(1, 1.0) == "(int, float)"
35 assert instance1.overloaded(2.0, 2) == "(float, int)"
1import pytest
2from pybind11_tests import ExampleMandA, ConstructorStats
3
4
5def test_methods_and_attributes():
6 instance1 = ExampleMandA()
7 instance2 = ExampleMandA(32)
8
9 instance1.add1(instance2)
10 instance1.add2(instance2)
11 instance1.add3(instance2)
12 instance1.add4(instance2)
13 instance1.add5(instance2)
14 instance1.add6(32)
15 instance1.add7(32)
16 instance1.add8(32)
17 instance1.add9(32)
18 instance1.add10(32)
19
20 assert str(instance1) == "ExampleMandA[value=320]"
21 assert str(instance2) == "ExampleMandA[value=32]"
22 assert str(instance1.self1()) == "ExampleMandA[value=320]"
23 assert str(instance1.self2()) == "ExampleMandA[value=320]"
24 assert str(instance1.self3()) == "ExampleMandA[value=320]"
25 assert str(instance1.self4()) == "ExampleMandA[value=320]"
26 assert str(instance1.self5()) == "ExampleMandA[value=320]"
27
28 assert instance1.internal1() == 320
29 assert instance1.internal2() == 320
30 assert instance1.internal3() == 320
31 assert instance1.internal4() == 320
32 assert instance1.internal5() == 320
33
34 assert instance1.overloaded(1, 1.0) == "(int, float)"
35 assert instance1.overloaded(2.0, 2) == "(float, int)"
36 assert instance1.overloaded_const(3, 3.0) == "(int, float) const"
37 assert instance1.overloaded_const(4.0, 4) == "(float, int) const"
36 assert instance1.overloaded(3, 3) == "(int, int)"
37 assert instance1.overloaded(4., 4.) == "(float, float)"
38 assert instance1.overloaded_const(5, 5.0) == "(int, float) const"
39 assert instance1.overloaded_const(6.0, 6) == "(float, int) const"
40 assert instance1.overloaded_const(7, 7) == "(int, int) const"
41 assert instance1.overloaded_const(8., 8.) == "(float, float) const"
42 assert instance1.overloaded_float(1, 1) == "(float, float)"
43 assert instance1.overloaded_float(1, 1.) == "(float, float)"
44 assert instance1.overloaded_float(1., 1) == "(float, float)"
45 assert instance1.overloaded_float(1., 1.) == "(float, float)"
38
39 assert instance1.value == 320
40 instance1.value = 100
41 assert str(instance1) == "ExampleMandA[value=100]"
42
43 cstats = ConstructorStats.get(ExampleMandA)
44 assert cstats.alive() == 2
45 del instance1, instance2
46 assert cstats.alive() == 0
47 assert cstats.values() == ["32"]
48 assert cstats.default_constructions == 1
49 assert cstats.copy_constructions == 3
50 assert cstats.move_constructions >= 1
51 assert cstats.copy_assignments == 0
52 assert cstats.move_assignments == 0
53
54
55def test_properties():
56 from pybind11_tests import TestProperties
57
58 instance = TestProperties()
59
60 assert instance.def_readonly == 1
61 with pytest.raises(AttributeError):
62 instance.def_readonly = 2
63
64 instance.def_readwrite = 2
65 assert instance.def_readwrite == 2
66
67 assert instance.def_property_readonly == 2
68 with pytest.raises(AttributeError):
69 instance.def_property_readonly = 3
70
71 instance.def_property = 3
72 assert instance.def_property == 3
73
74
75def test_static_properties():
76 from pybind11_tests import TestProperties as Type
77
78 assert Type.def_readonly_static == 1
46
47 assert instance1.value == 320
48 instance1.value = 100
49 assert str(instance1) == "ExampleMandA[value=100]"
50
51 cstats = ConstructorStats.get(ExampleMandA)
52 assert cstats.alive() == 2
53 del instance1, instance2
54 assert cstats.alive() == 0
55 assert cstats.values() == ["32"]
56 assert cstats.default_constructions == 1
57 assert cstats.copy_constructions == 3
58 assert cstats.move_constructions >= 1
59 assert cstats.copy_assignments == 0
60 assert cstats.move_assignments == 0
61
62
63def test_properties():
64 from pybind11_tests import TestProperties
65
66 instance = TestProperties()
67
68 assert instance.def_readonly == 1
69 with pytest.raises(AttributeError):
70 instance.def_readonly = 2
71
72 instance.def_readwrite = 2
73 assert instance.def_readwrite == 2
74
75 assert instance.def_property_readonly == 2
76 with pytest.raises(AttributeError):
77 instance.def_property_readonly = 3
78
79 instance.def_property = 3
80 assert instance.def_property == 3
81
82
83def test_static_properties():
84 from pybind11_tests import TestProperties as Type
85
86 assert Type.def_readonly_static == 1
79 with pytest.raises(AttributeError):
87 with pytest.raises(AttributeError) as excinfo:
80 Type.def_readonly_static = 2
88 Type.def_readonly_static = 2
89 assert "can't set attribute" in str(excinfo)
81
82 Type.def_readwrite_static = 2
83 assert Type.def_readwrite_static == 2
84
85 assert Type.def_property_readonly_static == 2
90
91 Type.def_readwrite_static = 2
92 assert Type.def_readwrite_static == 2
93
94 assert Type.def_property_readonly_static == 2
86 with pytest.raises(AttributeError):
95 with pytest.raises(AttributeError) as excinfo:
87 Type.def_property_readonly_static = 3
96 Type.def_property_readonly_static = 3
97 assert "can't set attribute" in str(excinfo)
88
89 Type.def_property_static = 3
90 assert Type.def_property_static == 3
91
98
99 Type.def_property_static = 3
100 assert Type.def_property_static == 3
101
102 # Static property read and write via instance
103 instance = Type()
92
104
105 Type.def_readwrite_static = 0
106 assert Type.def_readwrite_static == 0
107 assert instance.def_readwrite_static == 0
108
109 instance.def_readwrite_static = 2
110 assert Type.def_readwrite_static == 2
111 assert instance.def_readwrite_static == 2
112
113
114def test_static_cls():
115 """Static property getter and setters expect the type object as the their only argument"""
116 from pybind11_tests import TestProperties as Type
117
118 instance = Type()
119 assert Type.static_cls is Type
120 assert instance.static_cls is Type
121
122 def check_self(self):
123 assert self is Type
124
125 Type.static_cls = check_self
126 instance.static_cls = check_self
127
128
129def test_metaclass_override():
130 """Overriding pybind11's default metaclass changes the behavior of `static_property`"""
131 from pybind11_tests import MetaclassOverride
132
133 assert type(ExampleMandA).__name__ == "pybind11_type"
134 assert type(MetaclassOverride).__name__ == "type"
135
136 assert MetaclassOverride.readonly == 1
137 assert type(MetaclassOverride.__dict__["readonly"]).__name__ == "pybind11_static_property"
138
139 # Regular `type` replaces the property instead of calling `__set__()`
140 MetaclassOverride.readonly = 2
141 assert MetaclassOverride.readonly == 2
142 assert isinstance(MetaclassOverride.__dict__["readonly"], int)
143
144
93@pytest.mark.parametrize("access", ["ro", "rw", "static_ro", "static_rw"])
94def test_property_return_value_policies(access):
95 from pybind11_tests import TestPropRVP
96
97 if not access.startswith("static"):
98 obj = TestPropRVP()
99 else:
100 obj = TestPropRVP
101
102 ref = getattr(obj, access + "_ref")
103 assert ref.value == 1
104 ref.value = 2
105 assert getattr(obj, access + "_ref").value == 2
106 ref.value = 1 # restore original value for static properties
107
108 copy = getattr(obj, access + "_copy")
109 assert copy.value == 1
110 copy.value = 2
111 assert getattr(obj, access + "_copy").value == 1
112
113 copy = getattr(obj, access + "_func")
114 assert copy.value == 1
115 copy.value = 2
116 assert getattr(obj, access + "_func").value == 1
117
118
119def test_property_rvalue_policy():
120 """When returning an rvalue, the return value policy is automatically changed from
121 `reference(_internal)` to `move`. The following would not work otherwise.
122 """
123 from pybind11_tests import TestPropRVP
124
125 instance = TestPropRVP()
126 o = instance.rvalue
127 assert o.value == 1
145@pytest.mark.parametrize("access", ["ro", "rw", "static_ro", "static_rw"])
146def test_property_return_value_policies(access):
147 from pybind11_tests import TestPropRVP
148
149 if not access.startswith("static"):
150 obj = TestPropRVP()
151 else:
152 obj = TestPropRVP
153
154 ref = getattr(obj, access + "_ref")
155 assert ref.value == 1
156 ref.value = 2
157 assert getattr(obj, access + "_ref").value == 2
158 ref.value = 1 # restore original value for static properties
159
160 copy = getattr(obj, access + "_copy")
161 assert copy.value == 1
162 copy.value = 2
163 assert getattr(obj, access + "_copy").value == 1
164
165 copy = getattr(obj, access + "_func")
166 assert copy.value == 1
167 copy.value = 2
168 assert getattr(obj, access + "_func").value == 1
169
170
171def test_property_rvalue_policy():
172 """When returning an rvalue, the return value policy is automatically changed from
173 `reference(_internal)` to `move`. The following would not work otherwise.
174 """
175 from pybind11_tests import TestPropRVP
176
177 instance = TestPropRVP()
178 o = instance.rvalue
179 assert o.value == 1
180
181
182def test_property_rvalue_policy_static():
183 """When returning an rvalue, the return value policy is automatically changed from
184 `reference(_internal)` to `move`. The following would not work otherwise.
185 """
186 from pybind11_tests import TestPropRVP
128 o = TestPropRVP.static_rvalue
129 assert o.value == 1
130
131
187 o = TestPropRVP.static_rvalue
188 assert o.value == 1
189
190
191# https://bitbucket.org/pypy/pypy/issues/2447
192@pytest.unsupported_on_pypy
132def test_dynamic_attributes():
133 from pybind11_tests import DynamicClass, CppDerivedDynamicClass
134
135 instance = DynamicClass()
136 assert not hasattr(instance, "foo")
137 assert "foo" not in dir(instance)
138
139 # Dynamically add attribute
140 instance.foo = 42
141 assert hasattr(instance, "foo")
142 assert instance.foo == 42
143 assert "foo" in dir(instance)
144
145 # __dict__ should be accessible and replaceable
146 assert "foo" in instance.__dict__
147 instance.__dict__ = {"bar": True}
148 assert not hasattr(instance, "foo")
149 assert hasattr(instance, "bar")
150
151 with pytest.raises(TypeError) as excinfo:
152 instance.__dict__ = []
153 assert str(excinfo.value) == "__dict__ must be set to a dictionary, not a 'list'"
154
155 cstats = ConstructorStats.get(DynamicClass)
156 assert cstats.alive() == 1
157 del instance
158 assert cstats.alive() == 0
159
160 # Derived classes should work as well
161 class PythonDerivedDynamicClass(DynamicClass):
162 pass
163
164 for cls in CppDerivedDynamicClass, PythonDerivedDynamicClass:
165 derived = cls()
166 derived.foobar = 100
167 assert derived.foobar == 100
168
169 assert cstats.alive() == 1
170 del derived
171 assert cstats.alive() == 0
172
173
193def test_dynamic_attributes():
194 from pybind11_tests import DynamicClass, CppDerivedDynamicClass
195
196 instance = DynamicClass()
197 assert not hasattr(instance, "foo")
198 assert "foo" not in dir(instance)
199
200 # Dynamically add attribute
201 instance.foo = 42
202 assert hasattr(instance, "foo")
203 assert instance.foo == 42
204 assert "foo" in dir(instance)
205
206 # __dict__ should be accessible and replaceable
207 assert "foo" in instance.__dict__
208 instance.__dict__ = {"bar": True}
209 assert not hasattr(instance, "foo")
210 assert hasattr(instance, "bar")
211
212 with pytest.raises(TypeError) as excinfo:
213 instance.__dict__ = []
214 assert str(excinfo.value) == "__dict__ must be set to a dictionary, not a 'list'"
215
216 cstats = ConstructorStats.get(DynamicClass)
217 assert cstats.alive() == 1
218 del instance
219 assert cstats.alive() == 0
220
221 # Derived classes should work as well
222 class PythonDerivedDynamicClass(DynamicClass):
223 pass
224
225 for cls in CppDerivedDynamicClass, PythonDerivedDynamicClass:
226 derived = cls()
227 derived.foobar = 100
228 assert derived.foobar == 100
229
230 assert cstats.alive() == 1
231 del derived
232 assert cstats.alive() == 0
233
234
235# https://bitbucket.org/pypy/pypy/issues/2447
236@pytest.unsupported_on_pypy
174def test_cyclic_gc():
175 from pybind11_tests import DynamicClass
176
177 # One object references itself
178 instance = DynamicClass()
179 instance.circular_reference = instance
180
181 cstats = ConstructorStats.get(DynamicClass)
182 assert cstats.alive() == 1
183 del instance
184 assert cstats.alive() == 0
185
186 # Two object reference each other
187 i1 = DynamicClass()
188 i2 = DynamicClass()
189 i1.cycle = i2
190 i2.cycle = i1
191
192 assert cstats.alive() == 2
193 del i1, i2
194 assert cstats.alive() == 0
237def test_cyclic_gc():
238 from pybind11_tests import DynamicClass
239
240 # One object references itself
241 instance = DynamicClass()
242 instance.circular_reference = instance
243
244 cstats = ConstructorStats.get(DynamicClass)
245 assert cstats.alive() == 1
246 del instance
247 assert cstats.alive() == 0
248
249 # Two object reference each other
250 i1 = DynamicClass()
251 i2 = DynamicClass()
252 i1.cycle = i2
253 i2.cycle = i1
254
255 assert cstats.alive() == 2
256 del i1, i2
257 assert cstats.alive() == 0
258
259
260def test_noconvert_args(msg):
261 from pybind11_tests import ArgInspector, arg_inspect_func, floats_only, floats_preferred
262
263 a = ArgInspector()
264 assert msg(a.f("hi")) == """
265 loading ArgInspector1 argument WITH conversion allowed. Argument value = hi
266 """
267 assert msg(a.g("this is a", "this is b")) == """
268 loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a
269 loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b
270 13
271 loading ArgInspector2 argument WITH conversion allowed. Argument value = (default arg inspector 2)
272 """ # noqa: E501 line too long
273 assert msg(a.g("this is a", "this is b", 42)) == """
274 loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a
275 loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b
276 42
277 loading ArgInspector2 argument WITH conversion allowed. Argument value = (default arg inspector 2)
278 """ # noqa: E501 line too long
279 assert msg(a.g("this is a", "this is b", 42, "this is d")) == """
280 loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a
281 loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b
282 42
283 loading ArgInspector2 argument WITH conversion allowed. Argument value = this is d
284 """
285 assert (a.h("arg 1") ==
286 "loading ArgInspector2 argument WITHOUT conversion allowed. Argument value = arg 1")
287 assert msg(arg_inspect_func("A1", "A2")) == """
288 loading ArgInspector2 argument WITH conversion allowed. Argument value = A1
289 loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = A2
290 """
291
292 assert floats_preferred(4) == 2.0
293 assert floats_only(4.0) == 2.0
294 with pytest.raises(TypeError) as excinfo:
295 floats_only(4)
296 assert msg(excinfo.value) == """
297 floats_only(): incompatible function arguments. The following argument types are supported:
298 1. (f: float) -> float
299
300 Invoked with: 4
301 """
302
303
304def test_bad_arg_default(msg):
305 from pybind11_tests import debug_enabled, bad_arg_def_named, bad_arg_def_unnamed
306
307 with pytest.raises(RuntimeError) as excinfo:
308 bad_arg_def_named()
309 assert msg(excinfo.value) == (
310 "arg(): could not convert default argument 'a: NotRegistered' in function 'should_fail' "
311 "into a Python object (type not registered yet?)"
312 if debug_enabled else
313 "arg(): could not convert default argument into a Python object (type not registered "
314 "yet?). Compile in debug mode for more information."
315 )
316
317 with pytest.raises(RuntimeError) as excinfo:
318 bad_arg_def_unnamed()
319 assert msg(excinfo.value) == (
320 "arg(): could not convert default argument 'NotRegistered' in function 'should_fail' "
321 "into a Python object (type not registered yet?)"
322 if debug_enabled else
323 "arg(): could not convert default argument into a Python object (type not registered "
324 "yet?). Compile in debug mode for more information."
325 )