Skip to content

Commit d2e3ac5

Browse files
committed
Cleaned up a bit of dead code in CodeUnit and commented a lot of code
1 parent 5e5ca7e commit d2e3ac5

File tree

3 files changed

+21
-47
lines changed

3 files changed

+21
-47
lines changed

cls/TestCoverage/Data/CodeUnit.cls

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,20 @@ ClassMethod GetCurrentByName(pInternalName As %String, pSourceNamespace As %Stri
133133

134134

135135
If (tType = "PY") {
136+
// fill in the Lines property of this CodeUnit
136137
set tPointer = 0
137138
While $ListNext(pDocumentText, tPointer, tCurLine) {
138139
do pCodeUnit.Lines.Insert(tCurLine)
139140
}
140141

141142
do pCodeUnit.Lines.Insert("")
142143

144+
// Filling in the MethodMap and LineToMethodMap properties
143145
Set ClassName = $Piece(tName,".", *)
144146
Set tMethodInfo = ##class(TestCoverage.Utils).GetPythonMethodMapping(pDocumentText, ClassName)
145-
Set tLineToMethodInfo = tMethodInfo."__getitem__"(0)
146-
Set tMethodMapInfo = tMethodInfo."__getitem__"(1)
147+
// tMethodInfo is a python tuple of (line to method info, method map info)
148+
Set tLineToMethodInfo = tMethodInfo."__getitem__"(0) // a python builtins list where the item at index i is the name of the method that line i is a part of
149+
Set tMethodMapInfo = tMethodInfo."__getitem__"(1) // a python builtins dict with key = method name, value = the line number of its definition
147150
for i=1:1:$listlength(pDocumentText) {
148151
Set tMethod = tLineToMethodInfo."__getitem__"(i)
149152
Do pCodeUnit.LineToMethodMap.SetAt(tMethod,i)
@@ -157,12 +160,6 @@ ClassMethod GetCurrentByName(pInternalName As %String, pSourceNamespace As %Stri
157160
Do pCodeUnit.MethodMap.SetAt(tStartLine,tMethod)
158161
Set tExecutableFlags(tStartLine) = 0
159162
Do pCodeUnit.MethodEndMap.SetAt(tEndLine, tMethod)
160-
161-
Set tMethodMask = ""
162-
for j = tStartLine:1:tEndLine {
163-
Set $Bit(tMethodMask,j) = 1
164-
}
165-
Set tMethodSignature = $list(pDocumentText, tStartLine)
166163
}
167164
}
168165
Else {
@@ -252,10 +249,8 @@ Method UpdatePyExecutableLines(pName As %String, ByRef pPyCodeUnit) As %Status
252249
Set tBitString = ""
253250
If (##class(TestCoverage.Manager).HasPython(pName)) {
254251

255-
// snapshot the Python CodeUnit for it
256252
Set tFromHash = pPyCodeUnit.Hash
257253
Set tToHash = ..Hash
258-
// now bring over the executable lines
259254

260255
&sql(
261256
DECLARE C1 CURSOR FOR
@@ -352,12 +347,15 @@ Method UpdateSourceMap(pSourceNamespace As %String, ByRef pCache) As %Status
352347

353348
set tClass = ..Name
354349
Set tSourceUnits(tClass_".CLS") = ""
350+
// we'll need the MethodMap from the .CLS CodeUnit to figure out the line mappings
355351
$$$ThrowOnError(..GetCurrentByName(tClass _ ".CLS", pSourceNamespace, .pCLSCodeUnit, .pCLSCache))
356352
// we'll do the mappings from the .py to the .cls direction, so that we don't iterate over objectscript lines
357353
Set tMethod = ""
358354
Do ..MethodMap.GetNext(.tMethod)
359355
while (tMethod '= "")
360356
{
357+
// for each method in the .py file, we'll find the line number of the corresponding method (guaranteed to be unique) in the .cls file
358+
// and then map each line in the .py file to each line in the .cls file by just going 1 by 1 down the lines
361359
Set tCLSMethodNum = pCLSCodeUnit.MethodMap.GetAt(tMethod)
362360
Set tMethodStart = ..MethodMap.GetAt(tMethod)
363361
Set tMethodEnd = ..MethodEndMap.GetAt(tMethod)
@@ -506,7 +504,7 @@ Method UpdateComplexity() As %Status
506504

507505
// python methods
508506
If (##class(TestCoverage.Manager).HasPython(..Name)) {
509-
do ##class(TestCoverage.Data.CodeUnit).GetCurrentByName(..Name _ ".PY", , .pPyCodeUnit, )
507+
do ##class(TestCoverage.Data.CodeUnit).GetCurrentByName(..Name _ ".PY", , .pPyCodeUnit, ) // need the source code for the python
510508
set tDocumentText = pPyCodeUnit.Lines.Serialize()
511509
set tMethodComplexities = ..GetPythonComplexities(tDocumentText)
512510
}
@@ -533,6 +531,7 @@ Method UpdateComplexity() As %Status
533531
Quit tSC
534532
}
535533

534+
/// returns a python dict with (key, value) = (method name, complexity) for each python method
536535
ClassMethod GetPythonComplexities(pDocumentText) [ Language = python ]
537536
{
538537
from radon.complexity import cc_visit
@@ -578,8 +577,8 @@ ClassMethod GetCurrentHash(pName As %String, pType As %String, Output pHash As %
578577
// includes the class compilation signature.
579578
Set pHash = ..HashArrayRange(.pCodeArray,5,pName_"."_pType,.tSizeHint)
580579
} ElseIf (pType = "PY") {
581-
Merge pCodeArray = ^ROUTINE(pName_".py",0)
582-
set tSizeHint = ^ROUTINE(pName_".py",0,0)
580+
Merge pCodeArray = ^ROUTINE(pName_".py",0) // the python source code
581+
set tSizeHint = ^ROUTINE(pName_".py",0,0) // the number of lines in the python code
583582
set pHash = ..HashArrayRange(.pCodeArray, ,pName_".py", .pSizeHint)
584583
} Else {
585584
// Give standard descriptive error about the type being invalid.

cls/TestCoverage/Data/Coverage.cls

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ ClassMethod StoreIntCoverage(pRun As %Integer, pTestPath As %String, pName As %S
100100
$$$ThrowOnError(tSC)
101101
}
102102
Else { // If pType = "PY"
103+
//^IRIS.TEMP.TestCoveragePy(ClassName) contains the number of covered lines in this class
104+
//^IRIS.TEMP.TestCoveragePy(ClassName, i) in increasing order contain the line numbers for the covered lines
103105
if $Data(^IRIS.TEMP.TestCoveragePY(pName)) {
104106
for i = 1:1:(^IRIS.TEMP.TestCoveragePY(pName)-1) {
105107
Set tLineNumber = ^IRIS.TEMP.TestCoveragePY(pName, i)

cls/TestCoverage/Utils.cls

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ ClassMethod Snapshot(pIntRoutines As %List, Output pRelevantRoutines As %List =
8282
{
8383
Set tSC = $$$OK
8484
Try {
85-
// For debugging use only: no parallelization
86-
#; Set tPointer = 0
87-
#; While $ListNext(pIntRoutines, tPointer, tIntRoutine) {
88-
#; do ##class(TestCoverage.Data.CodeUnit).GetCurrentByName(tIntRoutine_".INT")
89-
#; }
90-
9185
#dim tSnapshotQueue As %SYSTEM.WorkMgr
9286
Set tSnapshotQueue = $System.WorkMgr.Initialize(,.tSC)
9387
$$$ThrowOnError(tSC)
@@ -133,30 +127,6 @@ ClassMethod Snapshot(pIntRoutines As %List, Output pRelevantRoutines As %List =
133127
Set pRelevantRoutines = pRelevantRoutines _ $ListBuild(tIntRoutine)
134128
}
135129

136-
// snapshot all the compiled python CodeUnits
137-
#; Set tSnapshotQueue = $System.WorkMgr.Initialize(,.tSC)
138-
#; $$$ThrowOnError(tSC)
139-
#; Set tPointer = 0
140-
#; While $ListNext(pPyRelevantRoutines,tPointer,tPyRoutine) {
141-
#; Set tSC = tSnapshotQueue.Queue("##class(TestCoverage.Data.CodeUnit).GetCurrentByName",tPyRoutine_".PY")
142-
#; $$$ThrowOnError(tSC)
143-
#; }
144-
145-
#; Set tSC = tSnapshotQueue.WaitForComplete()
146-
#; $$$ThrowOnError(tSC)
147-
148-
// update the executable lines for the .cls files that have python -- TODO: make the UpdatePyExecutableLines method not rely on knowing the .cls and .py CodeUnits beforehand
149-
#; Set tSnapshotQueue = $System.WorkMgr.Initialize(,.tSC)
150-
#; $$$ThrowOnError(tSC)
151-
#; Set tPointer = 0
152-
#; While $ListNext(pPyRelevantRoutines,tPointer,tClass) {
153-
#; Set tSC = tSnapshotQueue.Queue("##class(TestCoverage.Data.CodeUnit).UpdatePyExecutableLines",tClass)
154-
#; $$$ThrowOnError(tSC)
155-
#; }
156-
157-
#; Set tSC = tSnapshotQueue.WaitForComplete()
158-
#; $$$ThrowOnError(tSC)
159-
160130
Write !
161131
} Catch e {
162132
Set tSC = e.AsStatus()
@@ -443,6 +413,9 @@ ClassMethod CodeArrayToList(ByRef pCodeArray, Output pDocumentText As %List)
443413
quit
444414
}
445415

416+
/// returns a python tuple of (line to method info, method map info)
417+
/// linetomethodinfo: a python builtins list where the item at index i is the name of the method that line i is a part of
418+
/// methodmapinfo: a python builtins dict with key = method name, value = the line number of its definition
446419
ClassMethod GetPythonMethodMapping(pDocumentText, ClassName) [ Language = python ]
447420
{
448421
import iris
@@ -454,13 +427,13 @@ ClassMethod GetPythonMethodMapping(pDocumentText, ClassName) [ Language = python
454427
source = ''.join(source_lines)
455428
tree = ast.parse(source)
456429
line_function_map = [None] * (len(source_lines)+2)
457-
method_map = {}
430+
method_map = {} # dictionary from the method name to its start and ending line number
458431

459432
class FunctionMapper(ast.NodeVisitor):
460433
def __init__(self):
461434
self.current_class = None
462435
self.current_function = None
463-
self.outermost_function = None
436+
self.outermost_function = None # for objectscript purposes, we only care about the outer level functions/methods
464437

465438
def visit_ClassDef(self, node):
466439
prev_class = self.current_class
@@ -471,7 +444,7 @@ ClassMethod GetPythonMethodMapping(pDocumentText, ClassName) [ Language = python
471444
def visit_FunctionDef(self, node):
472445
if self.outermost_function is None:
473446
self.outermost_function = node.name
474-
method_map[node.name] = (node.lineno-1, node.end_lineno-1)
447+
method_map[node.name] = (node.lineno-1, node.end_lineno-1)
475448

476449
self.current_function = node.name
477450
for lineno in range(node.lineno, node.end_lineno + 1):
@@ -526,7 +499,7 @@ ClassMethod GetPythonLineExecutableFlags(pDocumentText) [ Language = python ]
526499
ast.AsyncWith, ast.Raise, ast.Try, ast.Assert,
527500
ast.Import, ast.ImportFrom, ast.Pass,
528501
ast.Break, ast.Continue, ast.Delete, ast.Yield,
529-
ast.YieldFrom, ast.Await, ast.Nonlocal)): # all executable
502+
ast.YieldFrom, ast.Await, ast.Nonlocal)): # all executable (determined manually)
530503
executable_lines.add(node.lineno)
531504
elif isinstance(node, ast.ExceptHandler): # except (but not finally) is executable
532505
executable_lines.add(node.lineno)

0 commit comments

Comments
 (0)