Skip to content

Commit 0249160

Browse files
committed
fix: ast code now doesn't use endline_no to support older python
1 parent 5e85d48 commit 0249160

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [4.0.3] - 2024-08-16
9+
10+
### Changed
11+
- #53: Method mapping code now doesn't use AST's endline_no property to support older python versions
12+
813
## [4.0.2] - 2024-08-16
914

1015
### Fixed

cls/TestCoverage/Utils.cls

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,8 @@ ClassMethod GetPythonMethodMapping(pDocumentText) [ Language = python ]
451451
import iris
452452
import ast
453453
source_lines = iris.cls('%SYS.Python').ToList(pDocumentText)
454-
source_lines = [line + "\n" for line in source_lines] # contains a list of each line of the source code
455454

456-
source = ''.join(source_lines)
455+
source = '\n'.join(source_lines)
457456
tree = ast.parse(source)
458457
line_function_map = [None] * (len(source_lines)+2)
459458
method_map = {} # dictionary from the method name to its start and ending line number
@@ -473,7 +472,9 @@ ClassMethod GetPythonMethodMapping(pDocumentText) [ Language = python ]
473472
def visit_FunctionDef(self, node):
474473
if self.outermost_function is None:
475474
self.outermost_function = node.name
476-
method_map[node.name] = (node.lineno-1, node.end_lineno-1)
475+
start_line = node.lineno
476+
end_line = self.get_end_line(node)
477+
method_map[node.name] = (start_line, end_line)
477478

478479
self.current_function = node.name
479480
for lineno in range(node.lineno, node.end_lineno + 1):
@@ -483,14 +484,18 @@ ClassMethod GetPythonMethodMapping(pDocumentText) [ Language = python ]
483484
self.current_function = None
484485
if self.outermost_function == node.name:
485486
self.outermost_function = None
486-
487-
# preprocessing the ending line number for each function
488-
tree_with_line_numbers = ast.increment_lineno(tree, n=1)
489-
for node in ast.walk(tree_with_line_numbers):
490-
if isinstance(node, ast.FunctionDef):
491-
node.end_lineno = node.body[-1].end_lineno
487+
@staticmethod
488+
def get_end_line(node):
489+
return max(child.lineno for child in ast.walk(node) if hasattr(child, 'lineno'))
490+
#; # preprocessing the ending line number for each function
491+
#; tree_with_line_numbers = ast.increment_lineno(tree, n=1)
492+
#; for node in ast.walk(tree_with_line_numbers):
493+
#; if isinstance(node, ast.FunctionDef):
494+
#; node.end_lineno = node.body[-1].end_lineno
492495

493496
FunctionMapper().visit(tree)
497+
print(source_lines)
498+
print(method_map)
494499
return (line_function_map, method_map)
495500
}
496501

module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Export generator="Cache" version="25">
33
<Document name="TestCoverage.ZPM"><Module>
44
<Name>TestCoverage</Name>
5-
<Version>4.0.2</Version>
5+
<Version>4.0.3</Version>
66
<Description>Run your typical ObjectScript %UnitTest tests and see which lines of your code are executed. Includes Cobertura-style reporting for use in continuous integration tools.</Description>
77
<Packaging>module</Packaging>
88
<Resource Name="TestCoverage.PKG" Directory="cls" />

0 commit comments

Comments
 (0)