Skip to content

Commit 73708c5

Browse files
committed
fix: Accounted for fact that compiled Python doesn't include empty lines at the top of a .CLS version of a Python method, added unit test for that; also fixed error handling on the Python to CLS line mapping
1 parent 4a58476 commit 73708c5

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

cls/TestCoverage/Data/CodeUnit.cls

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,17 +404,27 @@ Method UpdateSourceMap(pSourceNamespace As %String, ByRef pCache) As %Status
404404
Set tMethodStart = ..MethodMap.GetAt(tMethod)
405405
Set tMethodEnd = ..MethodEndMap.GetAt(tMethod)
406406
Set tMethodName = tMethod
407+
408+
// tFullMap(py/int Line Number, absolute) = $lb("CLS", class name, method name, CLS/mac start line (relative to method), CLS/mac end line (relative to method))
407409
Set tFullMap(tMethodStart) = $lb("CLS", tClass,tMethodName, -1, -1) ; -1 because the class
408410
; definition doesn't have the +1 offset from the {
411+
412+
// there's a strange edge case where if the python method in the .CLS file starts with spaces, that's not captured in the Python compiled code
413+
// so we have to find how many empty lines there are at the beginning
414+
Set tEmptyLines = 0
415+
while ($zstrip( pCLSCodeUnit.Lines.GetAt(tCLSMethodNum + 1 + tEmptyLines + 1), "<>W") = "") {
416+
Set tEmptyLines = tEmptyLines + 1
417+
}
418+
409419
For i = tMethodStart+1:1:tMethodEnd {
410420
Set tClassLineNum = i-tMethodStart
411-
Set tFullMap(i) = $lb("CLS", tClass,tMethodName, tClassLineNum, tClassLineNum)
421+
Set tFullMap(i) = $lb("CLS", tClass,tMethodName, tClassLineNum+tEmptyLines, tClassLineNum+tEmptyLines)
412422

413423
// extra check to make sure that the lines we're mapping between are the same as expected
414-
Set tClassLineCode = $zstrip(pCLSCodeUnit.Lines.GetAt(tCLSMethodNum + tClassLineNum + 1), "<>W")
424+
Set tClassLineCode = $zstrip(pCLSCodeUnit.Lines.GetAt(tCLSMethodNum + 1 + tEmptyLines + tClassLineNum), "<>W")
415425
Set tPyLineCode = $zstrip(..Lines.GetAt(i), "<>W")
416426
if (tPyLineCode '= tClassLineCode) {
417-
Set tSC = $$$ERROR($$$GeneralError,"Compiled .py code doesn't match .CLS python code at line " _ $char(10,13) _ tPyLineCode)
427+
$$$ThrowStatus($$$ERROR($$$GeneralError,"Compiled .py code doesn't match .CLS python code at line " _ $char(10,13) _ tPyLineCode))
418428
}
419429
}
420430
Do ..MethodMap.GetNext(.tMethod)
@@ -595,7 +605,7 @@ Method GetMethodOffset(pAbsoluteLine As %Integer, Output pMethod As %String, Out
595605
{
596606
}
597607

598-
ClassMethod GetCurrentHash(pName As %String, pType As %String, Output pHash As %String, Output pCodeArray As %String, Output pCache) As %Status [ Private ]
608+
ClassMethod GetCurrentHash(pName As %String, pType As %String, Output pHash As %String, Output pCodeArray As %String, Output pCache) As %Status
599609
{
600610
Set tSC = $$$OK
601611
Try {

internal/testing/unit_tests/UnitTest/TestCoverage/Unit/CodeUnit.cls

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ Method TestCodeUnitCreation()
4949
Set tCodeGeneratorLine = tClsCodeUnit.MethodMap.GetAt("SampleCodeGenerator")
5050
Set tNormalMethodLine = tClsCodeUnit.MethodMap.GetAt("SampleNormalMethod")
5151
Set tPythonMethodLine = tClsCodeUnit.MethodMap.GetAt("SamplePythonMethod")
52+
set tPythonWeirdSpacingMethodLine = tClsCodeUnit.MethodMap.GetAt("PythonWeirdSpacing")
5253

5354
Do $$$AssertNotEquals(tConstantReturnValueLine,"")
5455
Do $$$AssertNotEquals(tCodeGeneratorLine,"")
5556
Do $$$AssertNotEquals(tNormalMethodLine,"")
5657
Do $$$AssertNotEquals(tPythonMethodLine,"")
58+
Do $$$AssertNotEquals(tPythonWeirdSpacingMethodLine,"")
5759

5860
// test if LineIsPython is working properly
5961
Do $$$AssertEquals(tClsCodeUnit.LineIsPython.GetAt(tPythonMethodLine+2), 1)
@@ -69,6 +71,10 @@ Method TestCodeUnitCreation()
6971
Set tTestLines(tNormalMethodLine+3) = $ListBuild("SampleNormalMethod+2",,,tIntCodeUnit.Hash,tIntCodeUnit.MethodMap.GetAt(methodLabel)+2, "INT")
7072
Set tTestLines(tPythonMethodLine+2) = $ListBuild("SamplePythonMethod+1",,,tPyCodeUnit.Hash,tPyCodeUnit.MethodMap.GetAt("SamplePythonMethod")+1, "PY")
7173
Set tTestLines(tPythonMethodLine+3) = $ListBuild("SamplePythonMethod+2",,,tPyCodeUnit.Hash,tPyCodeUnit.MethodMap.GetAt("SamplePythonMethod")+2, "PY")
74+
Set tTestLines(tPythonWeirdSpacingMethodLine+4) = $ListBuild("PythonWeirdSpacing+1",,,tPyCodeUnit.Hash,tPyCodeUnit.MethodMap.GetAt("PythonWeirdSpacing")+1, "PY")
75+
Set tTestLines(tPythonWeirdSpacingMethodLine+5) = $ListBuild("PythonWeirdSpacing+2",,,tPyCodeUnit.Hash,tPyCodeUnit.MethodMap.GetAt("PythonWeirdSpacing")+2, "PY")
76+
Set tTestLines(tPythonWeirdSpacingMethodLine+6) = $ListBuild("PythonWeirdSpacing+3",,,tPyCodeUnit.Hash,tPyCodeUnit.MethodMap.GetAt("PythonWeirdSpacing")+3, "PY")
77+
7278
Set tLine = ""
7379
For {
7480
Set tLine = $Order(tTestLines(tLine),1,tInfo)
@@ -149,4 +155,13 @@ ClassMethod SamplePythonMethod() [ Language = python ]
149155
return 50
150156
}
151157

158+
ClassMethod PythonWeirdSpacing() [ Language = python ]
159+
{
160+
161+
162+
x = [0] * 10
163+
x.append([50])
164+
return [element * 2 for element in x]
165+
}
166+
152167
}

0 commit comments

Comments
 (0)