Debian Codegraph @

Lib/test/test_super.py

raw
1 """Unit tests for zero-argument super() & related machinery."""
2
3 import copy
4 import pickle
5 import textwrap
6 import threading
7 import unittest
8 from unittest.mock import patch
9 from test.support import import_helper, threading_helper
10
11
12 class A:
13 def f(self):
14 return 'A'
15 @classmethod
16 def cm(cls):
17 return (cls, 'A')
18
19 class B(A):
20 def f(self):
21 return super().f() + 'B'
22 @classmethod
23 def cm(cls):
24 return (cls, super().cm(), 'B')
25
26 class C(A):
27 def f(self):
28 return super().f() + 'C'
29 @classmethod
30 def cm(cls):
31 return (cls, super().cm(), 'C')
32
33 class D(C, B):
34 def f(self):
35 return super().f() + 'D'
36 def cm(cls):
37 return (cls, super().cm(), 'D')
38
39 class E(D):
40 pass
41
42 class F(E):
43 f = E.f
44
45 class G(A):
46 pass
47
48
49 class TestSuper(unittest.TestCase):
50
51 def tearDown(self):
52 # This fixes the damage that test_various___class___pathologies does.
53 nonlocal __class__
54 __class__ = TestSuper
55
56 def test_basics_working(self):
57 self.assertEqual(D().f(), 'ABCD')
58
59 def test_class_getattr_working(self):
60 self.assertEqual(D.f(D()), 'ABCD')
61
62 def test_subclass_no_override_working(self):
63 self.assertEqual(E().f(), 'ABCD')
64 self.assertEqual(E.f(E()), 'ABCD')
65
66 def test_unbound_method_transfer_working(self):
67 self.assertEqual(F().f(), 'ABCD')
68 self.assertEqual(F.f(F()), 'ABCD')
69
70 def test_class_methods_still_working(self):
71 self.assertEqual(A.cm(), (A, 'A'))
72 self.assertEqual(A().cm(), (A, 'A'))
73 self.assertEqual(G.cm(), (G, 'A'))
74 self.assertEqual(G().cm(), (G, 'A'))
75
76 def test_super_in_class_methods_working(self):
77 d = D()
78 self.assertEqual(d.cm(), (d, (D, (D, (D, 'A'), 'B'), 'C'), 'D'))
79 e = E()
80 self.assertEqual(e.cm(), (e, (E, (E, (E, 'A'), 'B'), 'C'), 'D'))
81
82 def test_super_with_closure(self):
83 # Issue4360: super() did not work in a function that
84 # contains a closure
85 class E(A):
86 def f(self):
87 def nested():
88 self
89 return super().f() + 'E'
90
91 self.assertEqual(E().f(), 'AE')
92
93 def test_various___class___pathologies(self):
94 # See issue #12370
95 class X(A):
96 def f(self):
97 return super().f()
98 __class__ = 413
99 x = X()
100 self.assertEqual(x.f(), 'A')
101 self.assertEqual(x.__class__, 413)
102 class X:
103 x = __class__
104 def f():
105 __class__
106 self.assertIs(X.x, type(self))
107 with self.assertRaises(NameError) as e:
108 exec("""class X:
109 __class__
110 def f():
111 __class__""", globals(), {})
112 self.assertIs(type(e.exception), NameError) # Not UnboundLocalError
113 class X:
114 global __class__
115 __class__ = 42
116 def f():
117 __class__
118 self.assertEqual(globals()["__class__"], 42)
119 del globals()["__class__"]
120 self.assertNotIn("__class__", X.__dict__)
121 class X:
122 nonlocal __class__
123 __class__ = 42
124 def f():
125 __class__
126 self.assertEqual(__class__, 42)
127
128 def test___class___instancemethod(self):
129 # See issue #14857
130 class X:
131 def f(self):
132 return __class__
133 self.assertIs(X().f(), X)
134
135 def test___class___classmethod(self):
136 # See issue #14857
137 class X:
138 @classmethod
139 def f(cls):
140 return __class__
141 self.assertIs(X.f(), X)
142
143 def test___class___staticmethod(self):
144 # See issue #14857
145 class X:
146 @staticmethod
147 def f():
148 return __class__
149 self.assertIs(X.f(), X)
150
151 def test___class___new(self):
152 # See issue #23722
153 # Ensure zero-arg super() works as soon as type.__new__() is completed
154 test_class = None
155
156 class Meta(type):
157 def __new__(cls, name, bases, namespace):
158 nonlocal test_class
159 self = super().__new__(cls, name, bases, namespace)
160 test_class = self.f()
161 return self
162
163 class A(metaclass=Meta):
164 @staticmethod
165 def f():
166 return __class__
167
168 self.assertIs(test_class, A)
169
170 def test___class___delayed(self):
171 # See issue #23722
172 test_namespace = None
173
174 class Meta(type):
175 def __new__(cls, name, bases, namespace):
176 nonlocal test_namespace
177 test_namespace = namespace
178 return None
179
180 class A(metaclass=Meta):
181 @staticmethod
182 def f():
183 return __class__
184
185 self.assertIs(A, None)
186
187 B = type("B", (), test_namespace)
188 self.assertIs(B.f(), B)
189
190 def test___class___mro(self):
191 # See issue #23722
192 test_class = None
193
194 class Meta(type):
195 def mro(self):
196 # self.f() doesn't work yet...
197 self.__dict__["f"]()
198 return super().mro()
199
200 class A(metaclass=Meta):
201 def f():
202 nonlocal test_class
203 test_class = __class__
204
205 self.assertIs(test_class, A)
206
207 def test___classcell___expected_behaviour(self):
208 # See issue #23722
209 class Meta(type):
210 def __new__(cls, name, bases, namespace):
211 nonlocal namespace_snapshot
212 namespace_snapshot = namespace.copy()
213 return super().__new__(cls, name, bases, namespace)
214
215 # __classcell__ is injected into the class namespace by the compiler
216 # when at least one method needs it, and should be omitted otherwise
217 namespace_snapshot = None
218 class WithoutClassRef(metaclass=Meta):
219 pass
220 self.assertNotIn("__classcell__", namespace_snapshot)
221
222 # With zero-arg super() or an explicit __class__ reference,
223 # __classcell__ is the exact cell reference to be populated by
224 # type.__new__
225 namespace_snapshot = None
226 class WithClassRef(metaclass=Meta):
227 def f(self):
228 return __class__
229
230 class_cell = namespace_snapshot["__classcell__"]
231 method_closure = WithClassRef.f.__closure__
232 self.assertEqual(len(method_closure), 1)
233 self.assertIs(class_cell, method_closure[0])
234 # Ensure the cell reference *doesn't* get turned into an attribute
235 with self.assertRaises(AttributeError):
236 WithClassRef.__classcell__
237
238 def test___classcell___missing(self):
239 # See issue #23722
240 # Some metaclasses may not pass the original namespace to type.__new__
241 # We test that case here by forcibly deleting __classcell__
242 class Meta(type):
243 def __new__(cls, name, bases, namespace):
244 namespace.pop('__classcell__', None)
245 return super().__new__(cls, name, bases, namespace)
246
247 # The default case should continue to work without any errors
248 class WithoutClassRef(metaclass=Meta):
249 pass
250
251 # With zero-arg super() or an explicit __class__ reference, we expect
252 # __build_class__ to raise a RuntimeError complaining that
253 # __class__ was not set, and asking if __classcell__ was propagated
254 # to type.__new__.
255 expected_error = '__class__ not set.*__classcell__ propagated'
256 with self.assertRaisesRegex(RuntimeError, expected_error):
257 class WithClassRef(metaclass=Meta):
258 def f(self):
259 return __class__
260
261 def test___classcell___overwrite(self):
262 # See issue #23722
263 # Overwriting __classcell__ with nonsense is explicitly prohibited
264 class Meta(type):
265 def __new__(cls, name, bases, namespace, cell):
266 namespace['__classcell__'] = cell
267 return super().__new__(cls, name, bases, namespace)
268
269 for bad_cell in (None, 0, "", object()):
270 with self.subTest(bad_cell=bad_cell):
271 with self.assertRaises(TypeError):
272 class A(metaclass=Meta, cell=bad_cell):
273 pass
274
275 def test___classcell___wrong_cell(self):
276 # See issue #23722
277 # Pointing the cell reference at the wrong class is also prohibited
278 class Meta(type):
279 def __new__(cls, name, bases, namespace):
280 cls = super().__new__(cls, name, bases, namespace)
281 B = type("B", (), namespace)
282 return cls
283
284 with self.assertRaises(TypeError):
285 class A(metaclass=Meta):
286 def f(self):
287 return __class__
288
289 def test_obscure_super_errors(self):
290 def f():
291 super()
292 with self.assertRaisesRegex(RuntimeError, r"no arguments"):
293 f()
294
295 class C:
296 def f():
297 super()
298 with self.assertRaisesRegex(RuntimeError, r"no arguments"):
299 C.f()
300
301 def f(x):
302 del x
303 super()
304 with self.assertRaisesRegex(RuntimeError, r"arg\[0\] deleted"):
305 f(None)
306
307 class X:
308 def f(x):
309 nonlocal __class__
310 del __class__
311 super()
312 with self.assertRaisesRegex(RuntimeError, r"empty __class__ cell"):
313 X().f()
314
315 def test_cell_as_self(self):
316 class X:
317 def meth(self):
318 super()
319
320 def f():
321 k = X()
322 def g():
323 return k
324 return g
325 c = f().__closure__[0]
326 self.assertRaises(TypeError, X.meth, c)
327
328 def test_super_init_leaks(self):
329 # Issue #26718: super.__init__ leaked memory if called multiple times.
330 # This will be caught by regrtest.py -R if this leak.
331 # NOTE: Despite the use in the test a direct call of super.__init__
332 # is not endorsed.
333 sp = super(float, 1.0)
334 for i in range(1000):
335 super.__init__(sp, int, i)
336
337 def test_super_argcount(self):
338 with self.assertRaisesRegex(TypeError, "expected at most"):
339 super(int, int, int)
340
341 def test_super_argtype(self):
342 with self.assertRaisesRegex(TypeError, "argument 1 must be a type"):
343 super(1, int)
344
345 def test_shadowed_global(self):
346 source = textwrap.dedent(
347 """
348 class super:
349 msg = "truly super"
350
351 class C:
352 def method(self):
353 return super().msg
354 """,
355 )
356 with import_helper.ready_to_import(name="shadowed_super", source=source):
357 import shadowed_super
358 self.assertEqual(shadowed_super.C().method(), "truly super")
359 import_helper.unload("shadowed_super")
360
361 def test_shadowed_local(self):
362 class super:
363 msg = "quite super"
364
365 class C:
366 def method(self):
367 return super().msg
368
369 self.assertEqual(C().method(), "quite super")
370
371 def test_shadowed_dynamic(self):
372 class MySuper:
373 msg = "super super"
374
375 class C:
376 def method(self):
377 return super().msg
378
379 with patch(f"{__name__}.super", MySuper) as m:
380 self.assertEqual(C().method(), "super super")
381
382 def test_shadowed_dynamic_two_arg(self):
383 call_args = []
384 class MySuper:
385 def __init__(self, *args):
386 call_args.append(args)
387 msg = "super super"
388
389 class C:
390 def method(self):
391 return super(1, 2).msg
392
393 with patch(f"{__name__}.super", MySuper) as m:
394 self.assertEqual(C().method(), "super super")
395 self.assertEqual(call_args, [(1, 2)])
396
397 def test_attribute_error(self):
398 class C:
399 def method(self):
400 return super().msg
401
402 with self.assertRaisesRegex(AttributeError, "'super' object has no attribute 'msg'"):
403 C().method()
404
405 def test_bad_first_arg(self):
406 class C:
407 def method(self):
408 return super(1, self).method()
409
410 with self.assertRaisesRegex(TypeError, "argument 1 must be a type"):
411 C().method()
412
413 def test_supercheck_fail(self):
414 class C:
415 def method(self, type_, obj):
416 return super(type_, obj).method()
417
418 c = C()
419 err_msg = (
420 r"super\(type, obj\): obj \({} {}\) is not "
421 r"an instance or subtype of type \({}\)."
422 )
423
424 cases = (
425 (int, c, int.__name__, C.__name__, "instance of"),
426 # obj is instance of type
427 (C, list(), C.__name__, list.__name__, "instance of"),
428 # obj is type itself
429 (C, list, C.__name__, list.__name__, "type"),
430 )
431
432 for case in cases:
433 with self.subTest(case=case):
434 type_, obj, type_str, obj_str, instance_or_type = case
435 regex = err_msg.format(instance_or_type, obj_str, type_str)
436
437 with self.assertRaisesRegex(TypeError, regex):
438 c.method(type_, obj)
439
440 def test_super___class__(self):
441 class C:
442 def method(self):
443 return super().__class__
444
445 self.assertEqual(C().method(), super)
446
447 def test_super_subclass___class__(self):
448 class mysuper(super):
449 pass
450
451 class C:
452 def method(self):
453 return mysuper(C, self).__class__
454
455 self.assertEqual(C().method(), mysuper)
456
457 def test_unusual_getattro(self):
458 class MyType(type):
459 pass
460
461 def test(name):
462 mytype = MyType(name, (MyType,), {})
463 super(MyType, type(mytype)).__setattr__(mytype, "bar", 1)
464 self.assertEqual(mytype.bar, 1)
465
466 _testinternalcapi = import_helper.import_module("_testinternalcapi")
467 for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
468 test("foo1")
469
470 def test_reassigned_new(self):
471 class A:
472 def __new__(cls):
473 pass
474
475 def __init_subclass__(cls):
476 if "__new__" not in cls.__dict__:
477 cls.__new__ = cls.__new__
478
479 class B(A):
480 pass
481
482 class C(B):
483 def __new__(cls):
484 return super().__new__(cls)
485
486 _testinternalcapi = import_helper.import_module("_testinternalcapi")
487 for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
488 C()
489
490 def test_mixed_staticmethod_hierarchy(self):
491 # This test is just a desugared version of `test_reassigned_new`
492 class A:
493 @staticmethod
494 def some(cls, *args, **kwargs):
495 self.assertFalse(args)
496 self.assertFalse(kwargs)
497
498 class B(A):
499 def some(cls, *args, **kwargs):
500 return super().some(cls, *args, **kwargs)
501
502 class C(B):
503 @staticmethod
504 def some(cls):
505 return super().some(cls)
506
507 _testinternalcapi = import_helper.import_module("_testinternalcapi")
508 for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
509 C.some(C)
510
511 @threading_helper.requires_working_threading()
512 def test___class___modification_multithreaded(self):
513 """ Note: this test isn't actually testing anything on its own.
514 It requires a sys audithook to be set to crash on older Python.
515 This should be the case anyways as our test suite sets
516 an audit hook.
517 """
518
519 class Foo:
520 pass
521
522 class Bar:
523 pass
524
525 thing = Foo()
526 def work():
527 foo = thing
528 for _ in range(200):
529 foo.__class__ = Bar
530 type(foo)
531 foo.__class__ = Foo
532 type(foo)
533
534
535 threads = []
536 for _ in range(6):
537 thread = threading.Thread(target=work)
538 thread.start()
539 threads.append(thread)
540
541 for thread in threads:
542 thread.join()
543
544 def test_special_methods(self):
545 for e in E(), E:
546 s = super(C, e)
547 self.assertEqual(s.__reduce__, e.__reduce__)
548 self.assertEqual(s.__reduce_ex__, e.__reduce_ex__)
549 self.assertEqual(s.__getstate__, e.__getstate__)
550 self.assertNotHasAttr(s, '__getnewargs__')
551 self.assertNotHasAttr(s, '__getnewargs_ex__')
552 self.assertNotHasAttr(s, '__setstate__')
553 self.assertNotHasAttr(s, '__copy__')
554 self.assertNotHasAttr(s, '__deepcopy__')
555
556 def test_pickling(self):
557 e = E()
558 e.x = 1
559 s = super(C, e)
560 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
561 with self.subTest(proto=proto):
562 u = pickle.loads(pickle.dumps(s, proto))
563 self.assertEqual(u.f(), s.f())
564 self.assertIs(type(u), type(s))
565 self.assertIs(type(u.__self__), E)
566 self.assertEqual(u.__self__.x, 1)
567 self.assertIs(u.__thisclass__, C)
568 self.assertIs(u.__self_class__, E)
569
570 s = super(C, E)
571 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
572 with self.subTest(proto=proto):
573 u = pickle.loads(pickle.dumps(s, proto))
574 self.assertEqual(u.cm(), s.cm())
575 self.assertEqual(u.f, s.f)
576 self.assertIs(type(u), type(s))
577 self.assertIs(u.__self__, E)
578 self.assertIs(u.__thisclass__, C)
579 self.assertIs(u.__self_class__, E)
580
581 def test_shallow_copying(self):
582 s = super(C, E())
583 self.assertIs(copy.copy(s), s)
584 s = super(C, E)
585 self.assertIs(copy.copy(s), s)
586
587 def test_deep_copying(self):
588 e = E()
589 e.x = [1]
590 s = super(C, e)
591 u = copy.deepcopy(s)
592 self.assertEqual(u.f(), s.f())
593 self.assertIs(type(u), type(s))
594 self.assertIsNot(u, s)
595 self.assertIs(type(u.__self__), E)
596 self.assertIsNot(u.__self__, e)
597 self.assertIsNot(u.__self__.x, e.x)
598 self.assertEqual(u.__self__.x, [1])
599 self.assertIs(u.__thisclass__, C)
600 self.assertIs(u.__self_class__, E)
601
602 s = super(C, E)
603 u = copy.deepcopy(s)
604 self.assertEqual(u.cm(), s.cm())
605 self.assertEqual(u.f, s.f)
606 self.assertIsNot(u, s)
607 self.assertIs(type(u), type(s))
608 self.assertIs(u.__self__, E)
609 self.assertIs(u.__thisclass__, C)
610 self.assertIs(u.__self_class__, E)
611
612
613 if __name__ == "__main__":
614 unittest.main()