summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorloic <loic@dachary.org>2016-12-18 01:56:31 +0100
committerloic <loic@dachary.org>2016-12-18 01:56:31 +0100
commitff3b97a0b4f7ca264f7b7897cc101521485cdff8 (patch)
treefc74e399e39134aa8af83c61c8ea663fcf04e95a
parentb33628b4918f47694e89526955cf83b9764f6562 (diff)
downloadpython-coveragepy-issue-493-2.tar.gz
finally happens before return in a try #493issue-493-2
In a try block such as: if expr: try: return finally print pass the print happens before the return and cannot be followed by pass. The general case is that when the body/else/handlers in a try block all have return, break etc., the code behind finally: has no arc to the statement following the try block. close #493
-rw-r--r--coverage/parser.py12
-rw-r--r--tests/test_arcs.py18
2 files changed, 25 insertions, 5 deletions
diff --git a/coverage/parser.py b/coverage/parser.py
index 71334b6..3d46bfa 100644
--- a/coverage/parser.py
+++ b/coverage/parser.py
@@ -842,19 +842,21 @@ class AstArcAnalyzer(object):
try_block.return_from # or a `return`.
)
- exits = self.add_body_arcs(node.finalbody, prev_starts=final_from)
+ final_exits = self.add_body_arcs(node.finalbody, prev_starts=final_from)
if try_block.break_from:
- break_exits = self._combine_finally_starts(try_block.break_from, exits)
+ break_exits = self._combine_finally_starts(try_block.break_from, final_exits)
self.process_break_exits(break_exits)
if try_block.continue_from:
- continue_exits = self._combine_finally_starts(try_block.continue_from, exits)
+ continue_exits = self._combine_finally_starts(try_block.continue_from, final_exits)
self.process_continue_exits(continue_exits)
if try_block.raise_from:
- raise_exits = self._combine_finally_starts(try_block.raise_from, exits)
+ raise_exits = self._combine_finally_starts(try_block.raise_from, final_exits)
self.process_raise_exits(raise_exits)
if try_block.return_from:
- return_exits = self._combine_finally_starts(try_block.return_from, exits)
+ return_exits = self._combine_finally_starts(try_block.return_from, final_exits)
self.process_return_exits(return_exits)
+ if exits:
+ exits = final_exits
return exits
diff --git a/tests/test_arcs.py b/tests/test_arcs.py
index b03ac53..a245e21 100644
--- a/tests/test_arcs.py
+++ b/tests/test_arcs.py
@@ -774,6 +774,24 @@ class ExceptionArcTest(CoverageTest):
arcz=".1 12 28 89 9. -23 34 46 6-2",
)
+ def test_return_finally_before_return(self):
+ self.check_coverage("""\
+ a = []
+ def check_token(data):
+ if data:
+ try:
+ return 1
+ finally:
+ a.append(1)
+ return 2
+ assert 2 == check_token(False)
+ assert [] == a
+ assert 1 == check_token(True)
+ assert [1] == a
+ """,
+ arcz=".1 12 29 9A AB BC C-1 -23 34 45 57 7-2 38 8-2",
+ )
+
def test_except_jump_finally(self):
self.check_coverage("""\
def func(x):