1 | n/a | """Unit tests for zero-argument super() & related machinery.""" |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | import warnings |
---|
5 | n/a | from test.support import check_warnings |
---|
6 | n/a | |
---|
7 | n/a | |
---|
8 | n/a | class A: |
---|
9 | n/a | def f(self): |
---|
10 | n/a | return 'A' |
---|
11 | n/a | @classmethod |
---|
12 | n/a | def cm(cls): |
---|
13 | n/a | return (cls, 'A') |
---|
14 | n/a | |
---|
15 | n/a | class B(A): |
---|
16 | n/a | def f(self): |
---|
17 | n/a | return super().f() + 'B' |
---|
18 | n/a | @classmethod |
---|
19 | n/a | def cm(cls): |
---|
20 | n/a | return (cls, super().cm(), 'B') |
---|
21 | n/a | |
---|
22 | n/a | class C(A): |
---|
23 | n/a | def f(self): |
---|
24 | n/a | return super().f() + 'C' |
---|
25 | n/a | @classmethod |
---|
26 | n/a | def cm(cls): |
---|
27 | n/a | return (cls, super().cm(), 'C') |
---|
28 | n/a | |
---|
29 | n/a | class D(C, B): |
---|
30 | n/a | def f(self): |
---|
31 | n/a | return super().f() + 'D' |
---|
32 | n/a | def cm(cls): |
---|
33 | n/a | return (cls, super().cm(), 'D') |
---|
34 | n/a | |
---|
35 | n/a | class E(D): |
---|
36 | n/a | pass |
---|
37 | n/a | |
---|
38 | n/a | class F(E): |
---|
39 | n/a | f = E.f |
---|
40 | n/a | |
---|
41 | n/a | class G(A): |
---|
42 | n/a | pass |
---|
43 | n/a | |
---|
44 | n/a | |
---|
45 | n/a | class TestSuper(unittest.TestCase): |
---|
46 | n/a | |
---|
47 | n/a | def tearDown(self): |
---|
48 | n/a | # This fixes the damage that test_various___class___pathologies does. |
---|
49 | n/a | nonlocal __class__ |
---|
50 | n/a | __class__ = TestSuper |
---|
51 | n/a | |
---|
52 | n/a | def test_basics_working(self): |
---|
53 | n/a | self.assertEqual(D().f(), 'ABCD') |
---|
54 | n/a | |
---|
55 | n/a | def test_class_getattr_working(self): |
---|
56 | n/a | self.assertEqual(D.f(D()), 'ABCD') |
---|
57 | n/a | |
---|
58 | n/a | def test_subclass_no_override_working(self): |
---|
59 | n/a | self.assertEqual(E().f(), 'ABCD') |
---|
60 | n/a | self.assertEqual(E.f(E()), 'ABCD') |
---|
61 | n/a | |
---|
62 | n/a | def test_unbound_method_transfer_working(self): |
---|
63 | n/a | self.assertEqual(F().f(), 'ABCD') |
---|
64 | n/a | self.assertEqual(F.f(F()), 'ABCD') |
---|
65 | n/a | |
---|
66 | n/a | def test_class_methods_still_working(self): |
---|
67 | n/a | self.assertEqual(A.cm(), (A, 'A')) |
---|
68 | n/a | self.assertEqual(A().cm(), (A, 'A')) |
---|
69 | n/a | self.assertEqual(G.cm(), (G, 'A')) |
---|
70 | n/a | self.assertEqual(G().cm(), (G, 'A')) |
---|
71 | n/a | |
---|
72 | n/a | def test_super_in_class_methods_working(self): |
---|
73 | n/a | d = D() |
---|
74 | n/a | self.assertEqual(d.cm(), (d, (D, (D, (D, 'A'), 'B'), 'C'), 'D')) |
---|
75 | n/a | e = E() |
---|
76 | n/a | self.assertEqual(e.cm(), (e, (E, (E, (E, 'A'), 'B'), 'C'), 'D')) |
---|
77 | n/a | |
---|
78 | n/a | def test_super_with_closure(self): |
---|
79 | n/a | # Issue4360: super() did not work in a function that |
---|
80 | n/a | # contains a closure |
---|
81 | n/a | class E(A): |
---|
82 | n/a | def f(self): |
---|
83 | n/a | def nested(): |
---|
84 | n/a | self |
---|
85 | n/a | return super().f() + 'E' |
---|
86 | n/a | |
---|
87 | n/a | self.assertEqual(E().f(), 'AE') |
---|
88 | n/a | |
---|
89 | n/a | def test_various___class___pathologies(self): |
---|
90 | n/a | # See issue #12370 |
---|
91 | n/a | class X(A): |
---|
92 | n/a | def f(self): |
---|
93 | n/a | return super().f() |
---|
94 | n/a | __class__ = 413 |
---|
95 | n/a | x = X() |
---|
96 | n/a | self.assertEqual(x.f(), 'A') |
---|
97 | n/a | self.assertEqual(x.__class__, 413) |
---|
98 | n/a | class X: |
---|
99 | n/a | x = __class__ |
---|
100 | n/a | def f(): |
---|
101 | n/a | __class__ |
---|
102 | n/a | self.assertIs(X.x, type(self)) |
---|
103 | n/a | with self.assertRaises(NameError) as e: |
---|
104 | n/a | exec("""class X: |
---|
105 | n/a | __class__ |
---|
106 | n/a | def f(): |
---|
107 | n/a | __class__""", globals(), {}) |
---|
108 | n/a | self.assertIs(type(e.exception), NameError) # Not UnboundLocalError |
---|
109 | n/a | class X: |
---|
110 | n/a | global __class__ |
---|
111 | n/a | __class__ = 42 |
---|
112 | n/a | def f(): |
---|
113 | n/a | __class__ |
---|
114 | n/a | self.assertEqual(globals()["__class__"], 42) |
---|
115 | n/a | del globals()["__class__"] |
---|
116 | n/a | self.assertNotIn("__class__", X.__dict__) |
---|
117 | n/a | class X: |
---|
118 | n/a | nonlocal __class__ |
---|
119 | n/a | __class__ = 42 |
---|
120 | n/a | def f(): |
---|
121 | n/a | __class__ |
---|
122 | n/a | self.assertEqual(__class__, 42) |
---|
123 | n/a | |
---|
124 | n/a | def test___class___instancemethod(self): |
---|
125 | n/a | # See issue #14857 |
---|
126 | n/a | class X: |
---|
127 | n/a | def f(self): |
---|
128 | n/a | return __class__ |
---|
129 | n/a | self.assertIs(X().f(), X) |
---|
130 | n/a | |
---|
131 | n/a | def test___class___classmethod(self): |
---|
132 | n/a | # See issue #14857 |
---|
133 | n/a | class X: |
---|
134 | n/a | @classmethod |
---|
135 | n/a | def f(cls): |
---|
136 | n/a | return __class__ |
---|
137 | n/a | self.assertIs(X.f(), X) |
---|
138 | n/a | |
---|
139 | n/a | def test___class___staticmethod(self): |
---|
140 | n/a | # See issue #14857 |
---|
141 | n/a | class X: |
---|
142 | n/a | @staticmethod |
---|
143 | n/a | def f(): |
---|
144 | n/a | return __class__ |
---|
145 | n/a | self.assertIs(X.f(), X) |
---|
146 | n/a | |
---|
147 | n/a | def test___class___new(self): |
---|
148 | n/a | # See issue #23722 |
---|
149 | n/a | # Ensure zero-arg super() works as soon as type.__new__() is completed |
---|
150 | n/a | test_class = None |
---|
151 | n/a | |
---|
152 | n/a | class Meta(type): |
---|
153 | n/a | def __new__(cls, name, bases, namespace): |
---|
154 | n/a | nonlocal test_class |
---|
155 | n/a | self = super().__new__(cls, name, bases, namespace) |
---|
156 | n/a | test_class = self.f() |
---|
157 | n/a | return self |
---|
158 | n/a | |
---|
159 | n/a | class A(metaclass=Meta): |
---|
160 | n/a | @staticmethod |
---|
161 | n/a | def f(): |
---|
162 | n/a | return __class__ |
---|
163 | n/a | |
---|
164 | n/a | self.assertIs(test_class, A) |
---|
165 | n/a | |
---|
166 | n/a | def test___class___delayed(self): |
---|
167 | n/a | # See issue #23722 |
---|
168 | n/a | test_namespace = None |
---|
169 | n/a | |
---|
170 | n/a | class Meta(type): |
---|
171 | n/a | def __new__(cls, name, bases, namespace): |
---|
172 | n/a | nonlocal test_namespace |
---|
173 | n/a | test_namespace = namespace |
---|
174 | n/a | return None |
---|
175 | n/a | |
---|
176 | n/a | # This case shouldn't trigger the __classcell__ deprecation warning |
---|
177 | n/a | with check_warnings() as w: |
---|
178 | n/a | warnings.simplefilter("always", DeprecationWarning) |
---|
179 | n/a | class A(metaclass=Meta): |
---|
180 | n/a | @staticmethod |
---|
181 | n/a | def f(): |
---|
182 | n/a | return __class__ |
---|
183 | n/a | self.assertEqual(w.warnings, []) |
---|
184 | n/a | |
---|
185 | n/a | self.assertIs(A, None) |
---|
186 | n/a | |
---|
187 | n/a | B = type("B", (), test_namespace) |
---|
188 | n/a | self.assertIs(B.f(), B) |
---|
189 | n/a | |
---|
190 | n/a | def test___class___mro(self): |
---|
191 | n/a | # See issue #23722 |
---|
192 | n/a | test_class = None |
---|
193 | n/a | |
---|
194 | n/a | class Meta(type): |
---|
195 | n/a | def mro(self): |
---|
196 | n/a | # self.f() doesn't work yet... |
---|
197 | n/a | self.__dict__["f"]() |
---|
198 | n/a | return super().mro() |
---|
199 | n/a | |
---|
200 | n/a | class A(metaclass=Meta): |
---|
201 | n/a | def f(): |
---|
202 | n/a | nonlocal test_class |
---|
203 | n/a | test_class = __class__ |
---|
204 | n/a | |
---|
205 | n/a | self.assertIs(test_class, A) |
---|
206 | n/a | |
---|
207 | n/a | def test___classcell___expected_behaviour(self): |
---|
208 | n/a | # See issue #23722 |
---|
209 | n/a | class Meta(type): |
---|
210 | n/a | def __new__(cls, name, bases, namespace): |
---|
211 | n/a | nonlocal namespace_snapshot |
---|
212 | n/a | namespace_snapshot = namespace.copy() |
---|
213 | n/a | return super().__new__(cls, name, bases, namespace) |
---|
214 | n/a | |
---|
215 | n/a | # __classcell__ is injected into the class namespace by the compiler |
---|
216 | n/a | # when at least one method needs it, and should be omitted otherwise |
---|
217 | n/a | namespace_snapshot = None |
---|
218 | n/a | class WithoutClassRef(metaclass=Meta): |
---|
219 | n/a | pass |
---|
220 | n/a | self.assertNotIn("__classcell__", namespace_snapshot) |
---|
221 | n/a | |
---|
222 | n/a | # With zero-arg super() or an explicit __class__ reference, |
---|
223 | n/a | # __classcell__ is the exact cell reference to be populated by |
---|
224 | n/a | # type.__new__ |
---|
225 | n/a | namespace_snapshot = None |
---|
226 | n/a | class WithClassRef(metaclass=Meta): |
---|
227 | n/a | def f(self): |
---|
228 | n/a | return __class__ |
---|
229 | n/a | |
---|
230 | n/a | class_cell = namespace_snapshot["__classcell__"] |
---|
231 | n/a | method_closure = WithClassRef.f.__closure__ |
---|
232 | n/a | self.assertEqual(len(method_closure), 1) |
---|
233 | n/a | self.assertIs(class_cell, method_closure[0]) |
---|
234 | n/a | # Ensure the cell reference *doesn't* get turned into an attribute |
---|
235 | n/a | with self.assertRaises(AttributeError): |
---|
236 | n/a | WithClassRef.__classcell__ |
---|
237 | n/a | |
---|
238 | n/a | def test___classcell___missing(self): |
---|
239 | n/a | # See issue #23722 |
---|
240 | n/a | # Some metaclasses may not pass the original namespace to type.__new__ |
---|
241 | n/a | # We test that case here by forcibly deleting __classcell__ |
---|
242 | n/a | class Meta(type): |
---|
243 | n/a | def __new__(cls, name, bases, namespace): |
---|
244 | n/a | namespace.pop('__classcell__', None) |
---|
245 | n/a | return super().__new__(cls, name, bases, namespace) |
---|
246 | n/a | |
---|
247 | n/a | # The default case should continue to work without any warnings |
---|
248 | n/a | with check_warnings() as w: |
---|
249 | n/a | warnings.simplefilter("always", DeprecationWarning) |
---|
250 | n/a | class WithoutClassRef(metaclass=Meta): |
---|
251 | n/a | pass |
---|
252 | n/a | self.assertEqual(w.warnings, []) |
---|
253 | n/a | |
---|
254 | n/a | # With zero-arg super() or an explicit __class__ reference, we expect |
---|
255 | n/a | # __build_class__ to emit a DeprecationWarning complaining that |
---|
256 | n/a | # __class__ was not set, and asking if __classcell__ was propagated |
---|
257 | n/a | # to type.__new__. |
---|
258 | n/a | # In Python 3.7, that warning will become a RuntimeError. |
---|
259 | n/a | expected_warning = ( |
---|
260 | n/a | '__class__ not set.*__classcell__ propagated', |
---|
261 | n/a | DeprecationWarning |
---|
262 | n/a | ) |
---|
263 | n/a | with check_warnings(expected_warning): |
---|
264 | n/a | warnings.simplefilter("always", DeprecationWarning) |
---|
265 | n/a | class WithClassRef(metaclass=Meta): |
---|
266 | n/a | def f(self): |
---|
267 | n/a | return __class__ |
---|
268 | n/a | # Check __class__ still gets set despite the warning |
---|
269 | n/a | self.assertIs(WithClassRef().f(), WithClassRef) |
---|
270 | n/a | |
---|
271 | n/a | # Check the warning is turned into an error as expected |
---|
272 | n/a | with warnings.catch_warnings(): |
---|
273 | n/a | warnings.simplefilter("error", DeprecationWarning) |
---|
274 | n/a | with self.assertRaises(DeprecationWarning): |
---|
275 | n/a | class WithClassRef(metaclass=Meta): |
---|
276 | n/a | def f(self): |
---|
277 | n/a | return __class__ |
---|
278 | n/a | |
---|
279 | n/a | def test___classcell___overwrite(self): |
---|
280 | n/a | # See issue #23722 |
---|
281 | n/a | # Overwriting __classcell__ with nonsense is explicitly prohibited |
---|
282 | n/a | class Meta(type): |
---|
283 | n/a | def __new__(cls, name, bases, namespace, cell): |
---|
284 | n/a | namespace['__classcell__'] = cell |
---|
285 | n/a | return super().__new__(cls, name, bases, namespace) |
---|
286 | n/a | |
---|
287 | n/a | for bad_cell in (None, 0, "", object()): |
---|
288 | n/a | with self.subTest(bad_cell=bad_cell): |
---|
289 | n/a | with self.assertRaises(TypeError): |
---|
290 | n/a | class A(metaclass=Meta, cell=bad_cell): |
---|
291 | n/a | pass |
---|
292 | n/a | |
---|
293 | n/a | def test___classcell___wrong_cell(self): |
---|
294 | n/a | # See issue #23722 |
---|
295 | n/a | # Pointing the cell reference at the wrong class is also prohibited |
---|
296 | n/a | class Meta(type): |
---|
297 | n/a | def __new__(cls, name, bases, namespace): |
---|
298 | n/a | cls = super().__new__(cls, name, bases, namespace) |
---|
299 | n/a | B = type("B", (), namespace) |
---|
300 | n/a | return cls |
---|
301 | n/a | |
---|
302 | n/a | with self.assertRaises(TypeError): |
---|
303 | n/a | class A(metaclass=Meta): |
---|
304 | n/a | def f(self): |
---|
305 | n/a | return __class__ |
---|
306 | n/a | |
---|
307 | n/a | def test_obscure_super_errors(self): |
---|
308 | n/a | def f(): |
---|
309 | n/a | super() |
---|
310 | n/a | self.assertRaises(RuntimeError, f) |
---|
311 | n/a | def f(x): |
---|
312 | n/a | del x |
---|
313 | n/a | super() |
---|
314 | n/a | self.assertRaises(RuntimeError, f, None) |
---|
315 | n/a | class X: |
---|
316 | n/a | def f(x): |
---|
317 | n/a | nonlocal __class__ |
---|
318 | n/a | del __class__ |
---|
319 | n/a | super() |
---|
320 | n/a | self.assertRaises(RuntimeError, X().f) |
---|
321 | n/a | |
---|
322 | n/a | def test_cell_as_self(self): |
---|
323 | n/a | class X: |
---|
324 | n/a | def meth(self): |
---|
325 | n/a | super() |
---|
326 | n/a | |
---|
327 | n/a | def f(): |
---|
328 | n/a | k = X() |
---|
329 | n/a | def g(): |
---|
330 | n/a | return k |
---|
331 | n/a | return g |
---|
332 | n/a | c = f().__closure__[0] |
---|
333 | n/a | self.assertRaises(TypeError, X.meth, c) |
---|
334 | n/a | |
---|
335 | n/a | def test_super_init_leaks(self): |
---|
336 | n/a | # Issue #26718: super.__init__ leaked memory if called multiple times. |
---|
337 | n/a | # This will be caught by regrtest.py -R if this leak. |
---|
338 | n/a | # NOTE: Despite the use in the test a direct call of super.__init__ |
---|
339 | n/a | # is not endorsed. |
---|
340 | n/a | sp = super(float, 1.0) |
---|
341 | n/a | for i in range(1000): |
---|
342 | n/a | super.__init__(sp, int, i) |
---|
343 | n/a | |
---|
344 | n/a | |
---|
345 | n/a | if __name__ == "__main__": |
---|
346 | n/a | unittest.main() |
---|