Skip to content

AST for Older Pythons #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.0.3] - Unreleased

### Changed
- #52: Method mapping code now doesn't use AST's endline_no property to support older python versions

## [4.0.2] - 2024-08-16

### Fixed
Expand Down
23 changes: 14 additions & 9 deletions cls/TestCoverage/Utils.cls
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,8 @@ ClassMethod GetPythonMethodMapping(pDocumentText) [ Language = python ]
import iris
import ast
source_lines = iris.cls('%SYS.Python').ToList(pDocumentText)
source_lines = [line + "\n" for line in source_lines] # contains a list of each line of the source code

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

self.current_function = node.name
for lineno in range(node.lineno, node.end_lineno + 1):
Expand All @@ -483,14 +484,18 @@ ClassMethod GetPythonMethodMapping(pDocumentText) [ Language = python ]
self.current_function = None
if self.outermost_function == node.name:
self.outermost_function = None

# preprocessing the ending line number for each function
tree_with_line_numbers = ast.increment_lineno(tree, n=1)
for node in ast.walk(tree_with_line_numbers):
if isinstance(node, ast.FunctionDef):
node.end_lineno = node.body[-1].end_lineno
@staticmethod
def get_end_line(node):
return max(child.lineno for child in ast.walk(node) if hasattr(child, 'lineno'))
#; # preprocessing the ending line number for each function
#; tree_with_line_numbers = ast.increment_lineno(tree, n=1)
#; for node in ast.walk(tree_with_line_numbers):
#; if isinstance(node, ast.FunctionDef):
#; node.end_lineno = node.body[-1].end_lineno

FunctionMapper().visit(tree)
print(source_lines)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like debug code?

print(method_map)
return (line_function_map, method_map)
}

Expand Down
2 changes: 1 addition & 1 deletion module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Export generator="Cache" version="25">
<Document name="TestCoverage.ZPM"><Module>
<Name>TestCoverage</Name>
<Version>4.0.2</Version>
<Version>4.0.3</Version>
<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>
<Packaging>module</Packaging>
<Resource Name="TestCoverage.PKG" Directory="cls" />
Expand Down
Loading