Debian Codegraph @

pypy/interpreter/test/apptest_exctable_dis.py

raw symbols SCIP
1 """
2 Bytecode inspection for exception table Phase 7 work.
3 Run with:
4 ~/oss/pypy2.7-v7.3.20-linux64/bin/pypy pytest.py pypy/interpreter/test/apptest_exctable_dis.py -s -v > /tmp/dis_pypy.txt 2>&1 &
5 Compare against CPython:
6 python3.11 pytest.py -D pypy/interpreter/test/apptest_exctable_dis.py -s -v > /tmp/dis_cpython.txt 2>&1 &
7 """
8 import dis
9
10
11 def _try_except_as():
12 try:
13 x = 1
14 except ValueError as e:
15 pass
16
17
18 def _try_except_no_name():
19 try:
20 x = 1
21 except ValueError:
22 pass
23
24
25 def _try_except_multi_handler():
26 try:
27 x = 1
28 except TypeError as e:
29 pass
30 except ValueError as e:
31 pass
32
33
34 def _try_finally():
35 try:
36 x = 1
37 finally:
38 pass
39
40
41 def _nested_try_except_in_except():
42 try:
43 x = 1
44 except OSError:
45 try:
46 y = 2
47 except ValueError:
48 pass
49
50
51 def test_dis_try_except_as():
52 print()
53 print("=== try/except T as name ===")
54 dis.dis(_try_except_as)
55
56
57 def test_dis_try_except_no_name():
58 print()
59 print("=== try/except T (no name) ===")
60 dis.dis(_try_except_no_name)
61
62
63 def test_dis_try_except_multi():
64 print()
65 print("=== try/except with two handlers (both as name) ===")
66 dis.dis(_try_except_multi_handler)
67
68
69 def _try_finally_inner():
70 x = 1
71
72 def test_dis_try_finally():
73 print()
74 print("=== try/finally ===")
75 dis.dis(_try_finally)
76 print()
77 print("=== plain function (no try) ===")
78 dis.dis(_try_finally_inner)
79
80
81 def test_dis_nested():
82 print()
83 print("=== nested try/except inside except handler ===")
84 dis.dis(_nested_try_except_in_except)
85
86
87 def _try_in_try():
88 try:
89 try:
90 pass
91 except Exception as ex:
92 pass
93 except Exception:
94 pass
95
96
97 def test_dis_try_in_try():
98 print()
99 print("=== nested try-in-try (settrace lineno test) ===")
100 dis.dis(_try_in_try)
101
102
103 def _try_finally_with_in_body():
104 # try/finally where the finally body contains a 'with' statement.
105 # Regression: assemble.py used to emit a spurious exception table entry
106 # that extended the outer handler into the finally body, causing the
107 # inner with-block's handler to be bypassed at runtime.
108 log = []
109
110 class CM:
111 def __enter__(self):
112 log.append('enter')
113 return self
114 def __exit__(self, *a):
115 log.append(('exit', bool(a[0])))
116 return True # suppress any exception from within the with block
117
118 try:
119 log.append('try')
120 finally:
121 with CM():
122 log.append('in with')
123
124 return log
125
126
127 def test_try_finally_with_in_body():
128 log = _try_finally_with_in_body()
129 assert log == ['try', 'enter', 'in with', ('exit', False)], log
130
131
132 def _try_finally_with_raises_in_body(exc_class):
133 # Variant: with-block inside finally raises; must be handled by the
134 # with-block's __exit__, not by the outer exception-path handler.
135 log = []
136
137 class CM:
138 def __enter__(self):
139 return self
140 def __exit__(self, tp, val, tb):
141 log.append(('exit', tp is exc_class))
142 return True # suppress
143
144 try:
145 pass
146 finally:
147 with CM():
148 raise exc_class("inside with")
149
150 return log
151
152
153 def test_try_finally_with_raises_in_body():
154 log = _try_finally_with_raises_in_body(ValueError)
155 assert log == [('exit', True)], log
156
157
158 def _except_star_all_caught():
159 try:
160 raise KeyError
161 except* Exception as e:
162 pass
163
164
165 def test_except_star_cleanup_lineno():
166 # GH-101517 / pdb test: after the except* body runs, the RETURN_VALUE for
167 # the "all exceptions caught" path must be attributed to the except* clause
168 # line, not to the body line. pdb relies on this to display the right line.
169 import sys
170
171 print()
172 print("=== except* cleanup lineno dis ===")
173 dis.dis(_except_star_all_caught)
174
175 events = []
176 base = _except_star_all_caught.__code__.co_firstlineno
177
178 def tracer(frame, event, arg):
179 if frame.f_code is _except_star_all_caught.__code__:
180 events.append((event, frame.f_lineno - base + 1))
181 return tracer
182
183 sys.settrace(tracer)
184 _except_star_all_caught()
185 sys.settrace(None)
186
187 print("trace events (relative line):", events)
188 return_linenos = [ln for evt, ln in events if evt == 'return']
189 # except* clause is relative line 4; body (pass) is relative line 5.
190 # CPython fires the return event at line 4 (the except* clause).
191 assert return_linenos == [4], (
192 "expected return event at except* clause (line 4), got: {}".format(return_linenos)
193 )