Debian Codegraph @

Lib/test/test_frame.py

raw
1 import copy
2 import operator
3 import re
4 import sys
5 import textwrap
6 import threading
7 import unittest
8 import weakref
9 try:
10 import _testcapi
11 except ImportError:
12 _testcapi = None
13
14 from collections.abc import Mapping
15 from test import support
16 from test.support import import_helper, threading_helper
17 from test.support.script_helper import assert_python_ok
18 from test import mapping_tests
19
20
21 class ClearTest(unittest.TestCase):
22 """
23 Tests for frame.clear().
24 """
25
26 def inner(self, x=5, **kwargs):
27 1/0
28
29 def outer(self, **kwargs):
30 try:
31 self.inner(**kwargs)
32 except ZeroDivisionError as e:
33 exc = e
34 return exc
35
36 def clear_traceback_frames(self, tb):
37 """
38 Clear all frames in a traceback.
39 """
40 while tb is not None:
41 tb.tb_frame.clear()
42 tb = tb.tb_next
43
44 def test_clear_locals(self):
45 class C:
46 pass
47 c = C()
48 wr = weakref.ref(c)
49 exc = self.outer(c=c)
50 del c
51 support.gc_collect()
52 # A reference to c is held through the frames
53 self.assertIsNot(None, wr())
54 self.clear_traceback_frames(exc.__traceback__)
55 support.gc_collect()
56 # The reference was released by .clear()
57 self.assertIs(None, wr())
58
59 def test_clear_locals_after_f_locals_access(self):
60 # see gh-113939
61 class C:
62 pass
63
64 wr = None
65 def inner():
66 nonlocal wr
67 c = C()
68 wr = weakref.ref(c)
69 1/0
70
71 try:
72 inner()
73 except ZeroDivisionError as exc:
74 support.gc_collect()
75 self.assertIsNotNone(wr())
76 exc.__traceback__.tb_next.tb_frame.clear()
77 support.gc_collect()
78 self.assertIsNone(wr())
79
80 def test_clear_does_not_clear_specials(self):
81 class C:
82 pass
83 c = C()
84 exc = self.outer(c=c)
85 del c
86 f = exc.__traceback__.tb_frame
87 f.clear()
88 self.assertIsNot(f.f_code, None)
89 self.assertIsNot(f.f_locals, None)
90 self.assertIsNot(f.f_builtins, None)
91 self.assertIsNot(f.f_globals, None)
92
93 def test_clear_generator(self):
94 endly = False
95 def g():
96 nonlocal endly
97 try:
98 yield
99 self.inner()
100 finally:
101 endly = True
102 gen = g()
103 next(gen)
104 self.assertFalse(endly)
105
106 # Cannot clear a suspended frame
107 with self.assertRaisesRegex(RuntimeError, r'suspended frame'):
108 gen.gi_frame.clear()
109 self.assertFalse(endly)
110
111 def test_clear_executing(self):
112 # Attempting to clear an executing frame is forbidden.
113 try:
114 1/0
115 except ZeroDivisionError as e:
116 f = e.__traceback__.tb_frame
117 with self.assertRaises(RuntimeError):
118 f.clear()
119 with self.assertRaises(RuntimeError):
120 f.f_back.clear()
121
122 def test_clear_executing_generator(self):
123 # Attempting to clear an executing generator frame is forbidden.
124 endly = False
125 def g():
126 nonlocal endly
127 try:
128 1/0
129 except ZeroDivisionError as e:
130 f = e.__traceback__.tb_frame
131 with self.assertRaises(RuntimeError):
132 f.clear()
133 with self.assertRaises(RuntimeError):
134 f.f_back.clear()
135 yield f
136 finally:
137 endly = True
138 gen = g()
139 f = next(gen)
140 self.assertFalse(endly)
141 # Cannot clear a suspended frame
142 with self.assertRaisesRegex(RuntimeError, 'suspended frame'):
143 f.clear()
144 self.assertFalse(endly)
145
146 def test_lineno_with_tracing(self):
147 def record_line():
148 f = sys._getframe(1)
149 lines.append(f.f_lineno-f.f_code.co_firstlineno)
150
151 def test(trace):
152 record_line()
153 if trace:
154 sys._getframe(0).f_trace = True
155 record_line()
156 record_line()
157
158 expected_lines = [1, 4, 5]
159 lines = []
160 test(False)
161 self.assertEqual(lines, expected_lines)
162 lines = []
163 test(True)
164 self.assertEqual(lines, expected_lines)
165
166 @support.cpython_only
167 def test_clear_refcycles(self):
168 # .clear() doesn't leave any refcycle behind
169 with support.disable_gc():
170 class C:
171 pass
172 c = C()
173 wr = weakref.ref(c)
174 exc = self.outer(c=c)
175 del c
176 self.assertIsNot(None, wr())
177 self.clear_traceback_frames(exc.__traceback__)
178 self.assertIs(None, wr())
179
180
181 class FrameAttrsTest(unittest.TestCase):
182
183 def make_frames(self):
184 def outer():
185 x = 5
186 y = 6
187 def inner():
188 z = x + 2
189 1/0
190 t = 9
191 return inner()
192 try:
193 outer()
194 except ZeroDivisionError as e:
195 tb = e.__traceback__
196 frames = []
197 while tb:
198 frames.append(tb.tb_frame)
199 tb = tb.tb_next
200 return frames
201
202 def test_clear_locals(self):
203 # Test f_locals after clear() (issue #21897)
204 f, outer, inner = self.make_frames()
205 outer.clear()
206 inner.clear()
207 self.assertEqual(outer.f_locals, {})
208 self.assertEqual(inner.f_locals, {})
209
210 def test_locals_clear_locals(self):
211 # Test f_locals before and after clear() (to exercise caching)
212 f, outer, inner = self.make_frames()
213 self.assertNotEqual(outer.f_locals, {})
214 self.assertNotEqual(inner.f_locals, {})
215 outer.clear()
216 inner.clear()
217 self.assertEqual(outer.f_locals, {})
218 self.assertEqual(inner.f_locals, {})
219
220 def test_f_lineno_del_segfault(self):
221 f, _, _ = self.make_frames()
222 with self.assertRaises(AttributeError):
223 del f.f_lineno
224
225 def test_f_generator(self):
226 # Test f_generator in different contexts.
227
228 def t0():
229 def nested():
230 frame = sys._getframe()
231 return frame.f_generator
232
233 def gen():
234 yield nested()
235
236 g = gen()
237 try:
238 return next(g)
239 finally:
240 g.close()
241
242 def t1():
243 frame = sys._getframe()
244 return frame.f_generator
245
246 def t2():
247 frame = sys._getframe()
248 yield frame.f_generator
249
250 async def t3():
251 frame = sys._getframe()
252 return frame.f_generator
253
254 # For regular functions f_generator is None
255 self.assertIsNone(t0())
256 self.assertIsNone(t1())
257
258 # For generators f_generator is equal to self
259 g = t2()
260 try:
261 frame_g = next(g)
262 self.assertIs(g, frame_g)
263 finally:
264 g.close()
265
266 # Ditto for coroutines
267 c = t3()
268 try:
269 c.send(None)
270 except StopIteration as ex:
271 self.assertIs(ex.value, c)
272 else:
273 raise AssertionError('coroutine did not exit')
274
275
276 class ReprTest(unittest.TestCase):
277 """
278 Tests for repr(frame).
279 """
280
281 def test_repr(self):
282 def outer():
283 x = 5
284 y = 6
285 def inner():
286 z = x + 2
287 1/0
288 t = 9
289 return inner()
290
291 offset = outer.__code__.co_firstlineno
292 try:
293 outer()
294 except ZeroDivisionError as e:
295 tb = e.__traceback__
296 frames = []
297 while tb:
298 frames.append(tb.tb_frame)
299 tb = tb.tb_next
300 else:
301 self.fail("should have raised")
302
303 f_this, f_outer, f_inner = frames
304 file_repr = re.escape(repr(__file__))
305 self.assertRegex(repr(f_this),
306 r"^<frame at 0x[0-9a-fA-F]+, file %s, line %d, code test_repr>$"
307 % (file_repr, offset + 23))
308 self.assertRegex(repr(f_outer),
309 r"^<frame at 0x[0-9a-fA-F]+, file %s, line %d, code outer>$"
310 % (file_repr, offset + 7))
311 self.assertRegex(repr(f_inner),
312 r"^<frame at 0x[0-9a-fA-F]+, file %s, line %d, code inner>$"
313 % (file_repr, offset + 5))
314
315 class TestFrameLocals(unittest.TestCase):
316 def test_scope(self):
317 class A:
318 x = 1
319 sys._getframe().f_locals['x'] = 2
320 sys._getframe().f_locals['y'] = 2
321
322 self.assertEqual(A.x, 2)
323 self.assertEqual(A.y, 2)
324
325 def f():
326 x = 1
327 sys._getframe().f_locals['x'] = 2
328 sys._getframe().f_locals['y'] = 2
329 self.assertEqual(x, 2)
330 self.assertEqual(locals()['y'], 2)
331 f()
332
333 def test_closure(self):
334 x = 1
335 y = 2
336
337 def f():
338 z = x + y
339 d = sys._getframe().f_locals
340 self.assertEqual(d['x'], 1)
341 self.assertEqual(d['y'], 2)
342 d['x'] = 2
343 d['y'] = 3
344
345 f()
346 self.assertEqual(x, 2)
347 self.assertEqual(y, 3)
348
349 def test_closure_with_inline_comprehension(self):
350 lambda: k
351 k = 1
352 lst = [locals() for k in [0]]
353 self.assertEqual(lst[0]['k'], 0)
354
355 def test_as_dict(self):
356 x = 1
357 y = 2
358 d = sys._getframe().f_locals
359 # self, x, y, d
360 self.assertEqual(len(d), 4)
361 self.assertIs(d['d'], d)
362 self.assertEqual(set(d.keys()), set(['x', 'y', 'd', 'self']))
363 self.assertEqual(len(d.values()), 4)
364 self.assertIn(1, d.values())
365 self.assertEqual(len(d.items()), 4)
366 self.assertIn(('x', 1), d.items())
367 self.assertEqual(d.__getitem__('x'), 1)
368 d.__setitem__('x', 2)
369 self.assertEqual(d['x'], 2)
370 self.assertEqual(d.get('x'), 2)
371 self.assertIs(d.get('non_exist', None), None)
372 self.assertEqual(d.__len__(), 4)
373 self.assertEqual(set([key for key in d]), set(['x', 'y', 'd', 'self']))
374 self.assertIn('x', d)
375 self.assertTrue(d.__contains__('x'))
376
377 self.assertEqual(reversed(d), list(reversed(d.keys())))
378
379 d.update({'x': 3, 'z': 4})
380 self.assertEqual(d['x'], 3)
381 self.assertEqual(d['z'], 4)
382
383 with self.assertRaises(TypeError):
384 d.update([1, 2])
385
386 self.assertEqual(d.setdefault('x', 5), 3)
387 self.assertEqual(d.setdefault('new', 5), 5)
388 self.assertEqual(d['new'], 5)
389
390 with self.assertRaises(KeyError):
391 d['non_exist']
392
393 def test_as_number(self):
394 x = 1
395 y = 2
396 d = sys._getframe().f_locals
397 self.assertIn('z', d | {'z': 3})
398 d |= {'z': 3}
399 self.assertEqual(d['z'], 3)
400 d |= {'y': 3}
401 self.assertEqual(d['y'], 3)
402 with self.assertRaises(TypeError):
403 d |= 3
404 with self.assertRaises(TypeError):
405 _ = d | [3]
406
407 def test_non_string_key(self):
408 d = sys._getframe().f_locals
409 d[1] = 2
410 self.assertEqual(d[1], 2)
411
412 def test_write_with_hidden(self):
413 def f():
414 f_locals = [sys._getframe().f_locals for b in [0]][0]
415 f_locals['b'] = 2
416 f_locals['c'] = 3
417 self.assertEqual(b, 2)
418 self.assertEqual(c, 3)
419 b = 0
420 c = 0
421 f()
422
423 def test_local_objects(self):
424 o = object()
425 k = '.'.join(['a', 'b', 'c'])
426 f_locals = sys._getframe().f_locals
427 f_locals['o'] = f_locals['k']
428 self.assertEqual(o, 'a.b.c')
429
430 def test_copy(self):
431 x = 0
432 d = sys._getframe().f_locals
433 d_copy = d.copy()
434 self.assertIsInstance(d_copy, dict)
435 self.assertEqual(d_copy['x'], 0)
436 d_copy['x'] = 1
437 self.assertEqual(x, 0)
438
439 def test_update_with_self(self):
440 def f():
441 f_locals = sys._getframe().f_locals
442 f_locals.update(f_locals)
443 f_locals.update(f_locals)
444 f_locals.update(f_locals)
445 f()
446
447 def test_repr(self):
448 x = 1
449 # Introduce a reference cycle
450 frame = sys._getframe()
451 self.assertEqual(repr(frame.f_locals), repr(dict(frame.f_locals)))
452
453 def test_delete(self):
454 x = 1
455 d = sys._getframe().f_locals
456
457 # This needs to be tested before f_extra_locals is created
458 with self.assertRaisesRegex(KeyError, 'non_exist'):
459 del d['non_exist']
460
461 with self.assertRaises(KeyError):
462 d.pop('non_exist')
463
464 with self.assertRaisesRegex(ValueError, 'local variables'):
465 del d['x']
466
467 with self.assertRaises(AttributeError):
468 d.clear()
469
470 with self.assertRaises(ValueError):
471 d.pop('x')
472
473 with self.assertRaises(ValueError):
474 d.pop('x', None)
475
476 # 'm', 'n' is stored in f_extra_locals
477 d['m'] = 1
478 d['n'] = 1
479
480 with self.assertRaises(KeyError):
481 d.pop('non_exist')
482
483 del d['m']
484 self.assertEqual(d.pop('n'), 1)
485
486 self.assertNotIn('m', d)
487 self.assertNotIn('n', d)
488
489 self.assertEqual(d.pop('n', 2), 2)
490
491 @support.cpython_only
492 def test_sizeof(self):
493 proxy = sys._getframe().f_locals
494 support.check_sizeof(self, proxy, support.calcobjsize("P"))
495
496 def test_unsupport(self):
497 x = 1
498 d = sys._getframe().f_locals
499 with self.assertRaises(TypeError):
500 copy.copy(d)
501
502 with self.assertRaises(TypeError):
503 copy.deepcopy(d)
504
505 def test_is_mapping(self):
506 x = 1
507 d = sys._getframe().f_locals
508 self.assertIsInstance(d, Mapping)
509 match d:
510 case {"x": value}:
511 self.assertEqual(value, 1)
512 kind = "mapping"
513 case _:
514 kind = "other"
515 self.assertEqual(kind, "mapping")
516
517 def _x_stringlikes(self):
518 class StringSubclass(str):
519 pass
520
521 class ImpostorX:
522 def __hash__(self):
523 return hash('x')
524
525 def __eq__(self, other):
526 return other == 'x'
527
528 return StringSubclass('x'), ImpostorX(), 'x'
529
530 def test_proxy_key_stringlikes_overwrite(self):
531 def f(obj):
532 x = 1
533 proxy = sys._getframe().f_locals
534 proxy[obj] = 2
535 return (
536 list(proxy.keys()),
537 dict(proxy),
538 proxy
539 )
540
541 for obj in self._x_stringlikes():
542 with self.subTest(cls=type(obj).__name__):
543
544 keys_snapshot, proxy_snapshot, proxy = f(obj)
545 expected_keys = ['obj', 'x', 'proxy']
546 expected_dict = {'obj': 'x', 'x': 2, 'proxy': proxy}
547 self.assertEqual(proxy.keys(), expected_keys)
548 self.assertEqual(proxy, expected_dict)
549 self.assertEqual(keys_snapshot, expected_keys)
550 self.assertEqual(proxy_snapshot, expected_dict)
551
552 def test_proxy_key_stringlikes_ftrst_write(self):
553 def f(obj):
554 proxy = sys._getframe().f_locals
555 proxy[obj] = 2
556 self.assertEqual(x, 2)
557 x = 1
558
559 for obj in self._x_stringlikes():
560 with self.subTest(cls=type(obj).__name__):
561 f(obj)
562
563 def test_proxy_key_unhashables(self):
564 class StringSubclass(str):
565 __hash__ = None
566
567 class ObjectSubclass:
568 __hash__ = None
569
570 proxy = sys._getframe().f_locals
571
572 for obj in StringSubclass('x'), ObjectSubclass():
573 with self.subTest(cls=type(obj).__name__):
574 with self.assertRaises(TypeError):
575 proxy[obj]
576 with self.assertRaises(TypeError):
577 proxy[obj] = 0
578
579 def test_constructor(self):
580 FrameLocalsProxy = type([sys._getframe().f_locals
581 for x in range(1)][0])
582 self.assertEqual(FrameLocalsProxy.__name__, 'FrameLocalsProxy')
583
584 def make_frame():
585 x = 1
586 y = 2
587 return sys._getframe()
588
589 proxy = FrameLocalsProxy(make_frame())
590 self.assertEqual(proxy, {'x': 1, 'y': 2})
591
592 # constructor expects 1 frame argument
593 with self.assertRaises(TypeError):
594 FrameLocalsProxy() # no arguments
595 with self.assertRaises(TypeError):
596 FrameLocalsProxy(123) # wrong type
597 with self.assertRaises(TypeError):
598 FrameLocalsProxy(frame=sys._getframe()) # no keyword arguments
599
600 def test_overwrite_locals(self):
601 # Verify we do not crash if we overwrite a local passed as an argument
602 # from an ancestor in the call stack.
603 def f():
604 xs = [1, 2, 3]
605 ys = [4, 5, 6]
606 return g(xs)
607
608 def g(xs):
609 f = sys._getframe()
610 f.f_back.f_locals["xs"] = None
611 f.f_back.f_locals["ys"] = None
612 return xs[1]
613
614 self.assertEqual(f(), 2)
615
616
617 class FrameLocalsProxyMappingTests(mapping_tests.TestHashMappingProtocol):
618 """Test that FrameLocalsProxy behaves like a Mapping (with exceptions)"""
619
620 def _f(*args, **kwargs):
621 def _f():
622 return sys._getframe().f_locals
623 return _f()
624 type2test = _f
625
626 @unittest.skipIf(True, 'Locals proxies for different frames never compare as equal')
627 def test_constructor(self):
628 pass
629
630 @unittest.skipIf(True, 'Unlike a mapping: del proxy[key] fails')
631 def test_write(self):
632 pass
633
634 @unittest.skipIf(True, 'Unlike a mapping: no proxy.popitem')
635 def test_popitem(self):
636 pass
637
638 @unittest.skipIf(True, 'Unlike a mapping: no proxy.pop')
639 def test_pop(self):
640 pass
641
642 @unittest.skipIf(True, 'Unlike a mapping: no proxy.clear')
643 def test_clear(self):
644 pass
645
646 @unittest.skipIf(True, 'Unlike a mapping: no proxy.fromkeys')
647 def test_fromkeys(self):
648 pass
649
650 # no del
651 def test_getitem(self):
652 mapping_tests.BasicTestMappingProtocol.test_getitem(self)
653 d = self._full_mapping({'a': 1, 'b': 2})
654 self.assertEqual(d['a'], 1)
655 self.assertEqual(d['b'], 2)
656 d['c'] = 3
657 d['a'] = 4
658 self.assertEqual(d['c'], 3)
659 self.assertEqual(d['a'], 4)
660
661 @unittest.skipIf(True, 'Unlike a mapping: no proxy.update')
662 def test_update(self):
663 pass
664
665 # proxy.copy returns a regular dict
666 def test_copy(self):
667 d = self._full_mapping({1:1, 2:2, 3:3})
668 self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
669 d = self._empty_mapping()
670 self.assertEqual(d.copy(), d)
671 self.assertRaises(TypeError, d.copy, None)
672
673 self.assertIsInstance(d.copy(), dict)
674
675 @unittest.skipIf(True, 'Locals proxies for different frames never compare as equal')
676 def test_eq(self):
677 pass
678
679
680 class TestFrameCApi(unittest.TestCase):
681 def test_basic(self):
682 x = 1
683 ctypes = import_helper.import_module('ctypes')
684 PyEval_GetFrameLocals = ctypes.pythonapi.PyEval_GetFrameLocals
685 PyEval_GetFrameLocals.restype = ctypes.py_object
686 frame_locals = PyEval_GetFrameLocals()
687 self.assertTrue(type(frame_locals), dict)
688 self.assertEqual(frame_locals['x'], 1)
689 frame_locals['x'] = 2
690 self.assertEqual(x, 1)
691
692 PyEval_GetFrameGlobals = ctypes.pythonapi.PyEval_GetFrameGlobals
693 PyEval_GetFrameGlobals.restype = ctypes.py_object
694 frame_globals = PyEval_GetFrameGlobals()
695 self.assertTrue(type(frame_globals), dict)
696 self.assertIs(frame_globals, globals())
697
698 PyEval_GetFrameBuiltins = ctypes.pythonapi.PyEval_GetFrameBuiltins
699 PyEval_GetFrameBuiltins.restype = ctypes.py_object
700 frame_builtins = PyEval_GetFrameBuiltins()
701 self.assertEqual(frame_builtins, __builtins__)
702
703 PyFrame_GetLocals = ctypes.pythonapi.PyFrame_GetLocals
704 PyFrame_GetLocals.argtypes = [ctypes.py_object]
705 PyFrame_GetLocals.restype = ctypes.py_object
706 frame = sys._getframe()
707 f_locals = PyFrame_GetLocals(frame)
708 self.assertTrue(f_locals['x'], 1)
709 f_locals['x'] = 2
710 self.assertEqual(x, 2)
711
712
713 class TestIncompleteFrameAreInvisible(unittest.TestCase):
714
715 def test_issue95818(self):
716 # See GH-95818 for details
717 code = textwrap.dedent(f"""
718 import gc
719
720 gc.set_threshold(1,1,1)
721 class GCHello:
722 def __del__(self):
723 print("Destroyed from gc")
724
725 def gen():
726 yield
727
728 fd = open({__file__!r})
729 l = [fd, GCHello()]
730 l.append(l)
731 del fd
732 del l
733 gen()
734 """)
735 assert_python_ok("-c", code)
736
737 @support.cpython_only
738 @threading_helper.requires_working_threading()
739 def test_sneaky_frame_object_teardown(self):
740
741 class SneakyDel:
742 def __del__(self):
743 """
744 Stash a reference to the entire stack for walking later.
745
746 It may look crazy, but you'd be surprised how common this is
747 when using a test runner (like pytest). The typical recipe is:
748 ResourceWarning + -Werror + a custom sys.unraisablehook.
749 """
750 nonlocal sneaky_frame_object
751 sneaky_frame_object = sys._getframe()
752
753 class SneakyThread(threading.Thread):
754 """
755 A separate thread isn't needed to make this code crash, but it does
756 make crashes more consistent, since it means sneaky_frame_object is
757 backed by freed memory after the thread completes!
758 """
759
760 def run(self):
761 """Run SneakyDel.__del__ as this frame is popped."""
762 ref = SneakyDel()
763
764 sneaky_frame_object = None
765 t = SneakyThread()
766 t.start()
767 t.join()
768 # sneaky_frame_object can be anything, really, but it's crucial that
769 # SneakyThread.run's frame isn't anywhere on the stack while it's being
770 # torn down:
771 self.assertIsNotNone(sneaky_frame_object)
772 while sneaky_frame_object is not None:
773 self.assertIsNot(
774 sneaky_frame_object.f_code, SneakyThread.run.__code__
775 )
776 sneaky_frame_object = sneaky_frame_object.f_back
777
778 def test_entry_frames_are_invisible_during_teardown(self):
779 class C:
780 """A weakref'able class."""
781
782 def f():
783 """Try to find globals and locals as this frame is being cleared."""
784 ref = C()
785 # Ignore the fact that exec(C()) is a nonsense callback. We're only
786 # using exec here because it tries to access the current frame's
787 # globals and locals. If it's trying to get those from a shim frame,
788 # we'll crash before raising:
789 return weakref.ref(ref, exec)
790
791 with support.catch_unraisable_exception() as catcher:
792 # Call from C, so there is a shim frame directly above f:
793 weak = operator.call(f) # BOOM!
794 # Cool, we didn't crash. Check that the callback actually happened:
795 self.assertIs(catcher.unraisable.exc_type, TypeError)
796 self.assertIsNone(weak())
797
798
799 if __name__ == "__main__":
800 unittest.main()