From 71255f175726259f1a6492ff49a6c7393fde8741 Mon Sep 17 00:00:00 2001 From: Taylor Date: Fri, 8 Apr 2022 13:08:44 -0400 Subject: [PATCH] Fixes an issue I had with a TextNode for which the token had a lineno but not a position. Unsure whether it should be considered a bug within Django or within django_coverage_plugin (or my project), but either way, this fixed it for me. --- django_coverage_plugin/plugin.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/django_coverage_plugin/plugin.py b/django_coverage_plugin/plugin.py index 41a091c..54e35af 100644 --- a/django_coverage_plugin/plugin.py +++ b/django_coverage_plugin/plugin.py @@ -112,6 +112,14 @@ def filename_for_frame(frame): except (KeyError, AttributeError): return None + def lines_for_node(n): + try: + startLine = n.token.lineno + count = len(n.s.splitlines(True)) if isinstance(n, TextNode) else 0 + return startLine, startLine + count + except AttributeError: + return None + def position_for_node(node): try: return node.token.position @@ -127,6 +135,9 @@ def filename_for_frame(frame): except (KeyError, AttributeError, IndexError): return None + def lines_for_node(node): + return None + def position_for_node(node): return node.source[1] @@ -249,7 +260,8 @@ def line_number_range(self, frame): position = position_for_node(render_self) if position is None: - return -1, -1 + lines = lines_for_node(render_self) + return lines if lines else (-1, -1) if SHOW_TRACING: print("{!r}: {}".format(render_self, position))