mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
bpo-42645: Make sure that return/break/continue are only traced once when exiting via a finally block. (GH-23780)
* Make sure that return/break/continue are only traced once when exiting via a finally block. * Add test for return in try-finally. * Update importlib
This commit is contained in:
parent
c71581c7a4
commit
5274b682bc
4 changed files with 1791 additions and 1677 deletions
|
|
@ -678,6 +678,119 @@ class TraceTestCase(unittest.TestCase):
|
|||
(4, 'line'),
|
||||
(4, 'return')])
|
||||
|
||||
def test_if_break(self):
|
||||
|
||||
def func():
|
||||
seq = [1, 0]
|
||||
while seq:
|
||||
n = seq.pop()
|
||||
if n:
|
||||
break # line 5
|
||||
else:
|
||||
n = 99
|
||||
return n # line 8
|
||||
|
||||
self.run_and_compare(func,
|
||||
[(0, 'call'),
|
||||
(1, 'line'),
|
||||
(2, 'line'),
|
||||
(3, 'line'),
|
||||
(4, 'line'),
|
||||
(2, 'line'),
|
||||
(3, 'line'),
|
||||
(4, 'line'),
|
||||
(5, 'line'),
|
||||
(8, 'line'),
|
||||
(8, 'return')])
|
||||
|
||||
def test_break_through_finally(self):
|
||||
|
||||
def func():
|
||||
a, c, d, i = 1, 1, 1, 99
|
||||
try:
|
||||
for i in range(3):
|
||||
try:
|
||||
a = 5
|
||||
if i > 0:
|
||||
break # line 7
|
||||
a = 8
|
||||
finally:
|
||||
c = 10
|
||||
except:
|
||||
d = 12 # line 12
|
||||
assert a == 5 and c == 10 and d == 1 # line 13
|
||||
|
||||
self.run_and_compare(func,
|
||||
[(0, 'call'),
|
||||
(1, 'line'),
|
||||
(2, 'line'),
|
||||
(3, 'line'),
|
||||
(4, 'line'),
|
||||
(5, 'line'),
|
||||
(6, 'line'),
|
||||
(8, 'line'),
|
||||
(10, 'line'),
|
||||
(3, 'line'),
|
||||
(4, 'line'),
|
||||
(5, 'line'),
|
||||
(6, 'line'),
|
||||
(7, 'line'),
|
||||
(10, 'line'),
|
||||
(13, 'line'),
|
||||
(13, 'return')])
|
||||
|
||||
def test_continue_through_finally(self):
|
||||
|
||||
def func():
|
||||
a, b, c, d, i = 1, 1, 1, 1, 99
|
||||
try:
|
||||
for i in range(2):
|
||||
try:
|
||||
a = 5
|
||||
if i > 0:
|
||||
continue # line 7
|
||||
b = 8
|
||||
finally:
|
||||
c = 10
|
||||
except:
|
||||
d = 12 # line 12
|
||||
assert (a, b, c, d) == (5, 8, 10, 1) # line 13
|
||||
|
||||
self.run_and_compare(func,
|
||||
[(0, 'call'),
|
||||
(1, 'line'),
|
||||
(2, 'line'),
|
||||
(3, 'line'),
|
||||
(4, 'line'),
|
||||
(5, 'line'),
|
||||
(6, 'line'),
|
||||
(8, 'line'),
|
||||
(10, 'line'),
|
||||
(3, 'line'),
|
||||
(4, 'line'),
|
||||
(5, 'line'),
|
||||
(6, 'line'),
|
||||
(7, 'line'),
|
||||
(10, 'line'),
|
||||
(3, 'line'),
|
||||
(13, 'line'),
|
||||
(13, 'return')])
|
||||
|
||||
def test_return_through_finally(self):
|
||||
|
||||
def func():
|
||||
try:
|
||||
return 2
|
||||
finally:
|
||||
4
|
||||
|
||||
self.run_and_compare(func,
|
||||
[(0, 'call'),
|
||||
(1, 'line'),
|
||||
(2, 'line'),
|
||||
(4, 'line'),
|
||||
(4, 'return')])
|
||||
|
||||
|
||||
class SkipLineEventsTraceTestCase(TraceTestCase):
|
||||
"""Repeat the trace tests, but with per-line events skipped"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue