summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2016-02-13 17:41:51 -0500
committerNed Batchelder <ned@nedbatchelder.com>2016-02-13 17:41:51 -0500
commita72ac98c5132ddb4a801f2562893bc201a9b8dd2 (patch)
treea7dcc4eeb4e5e56e6a10ad2bbc866251a5e761ed
parent76691876eb8c4ca406dbe10613d0a217d3fcb47d (diff)
downloadpython-coveragepy-git-a72ac98c5132ddb4a801f2562893bc201a9b8dd2.tar.gz
Add missing branch descriptions for handling exceptions
-rw-r--r--coverage/parser.py5
-rw-r--r--tests/test_parser.py20
2 files changed, 23 insertions, 2 deletions
diff --git a/coverage/parser.py b/coverage/parser.py
index 715ab156..aa7693f8 100644
--- a/coverage/parser.py
+++ b/coverage/parser.py
@@ -786,14 +786,15 @@ class AstArcAnalyzer(object):
handler_exits = set()
- last_handler_start = None
if node.handlers:
+ last_handler_start = None
for handler_node in node.handlers:
handler_start = self.line_for_node(handler_node)
if last_handler_start is not None:
self.arcs.add((last_handler_start, handler_start))
last_handler_start = handler_start
- handler_exits |= self.add_body_arcs(handler_node.body, from_start=ArcStart(handler_start))
+ from_start = ArcStart(handler_start, cause="the exception caught by line {lineno} didn't happen")
+ handler_exits |= self.add_body_arcs(handler_node.body, from_start=from_start)
if node.orelse:
exits = self.add_body_arcs(node.orelse, prev_starts=exits)
diff --git a/tests/test_parser.py b/tests/test_parser.py
index 53a514e0..d9781343 100644
--- a/tests/test_parser.py
+++ b/tests/test_parser.py
@@ -250,6 +250,26 @@ class ParserMissingArcDescriptionTest(CoverageTest):
"line 5 didn't run the set comprehension on line 5"
)
+ def test_missing_arc_descriptions_for_exceptions(self):
+ text = textwrap.dedent(u"""\
+ try:
+ pass
+ except ZeroDivideError:
+ print("whoops")
+ except ValueError:
+ print("yikes")
+ """)
+ parser = PythonParser(text=text)
+ parser.parse_source()
+ self.assertEqual(
+ parser.missing_arc_description(3, 4),
+ "line 3 didn't jump to line 4, because the exception caught by line 3 didn't happen"
+ )
+ self.assertEqual(
+ parser.missing_arc_description(5, 6),
+ "line 5 didn't jump to line 6, because the exception caught by line 5 didn't happen"
+ )
+
class ParserFileTest(CoverageTest):
"""Tests for coverage.py's code parsing from files."""