Skip to content

Commit

Permalink
fix: Accounted for fact that compiled Python doesn't include empty li…
Browse files Browse the repository at this point in the history
…nes 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
  • Loading branch information
isc-cge committed Aug 14, 2024
1 parent 4a58476 commit 73708c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
18 changes: 14 additions & 4 deletions cls/TestCoverage/Data/CodeUnit.cls
Original file line number Diff line number Diff line change
Expand Up @@ -404,17 +404,27 @@ Method UpdateSourceMap(pSourceNamespace As %String, ByRef pCache) As %Status
Set tMethodStart = ..MethodMap.GetAt(tMethod)
Set tMethodEnd = ..MethodEndMap.GetAt(tMethod)
Set tMethodName = tMethod

// 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))
Set tFullMap(tMethodStart) = $lb("CLS", tClass,tMethodName, -1, -1) ; -1 because the class
; definition doesn't have the +1 offset from the {

// 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
// so we have to find how many empty lines there are at the beginning
Set tEmptyLines = 0
while ($zstrip( pCLSCodeUnit.Lines.GetAt(tCLSMethodNum + 1 + tEmptyLines + 1), "<>W") = "") {
Set tEmptyLines = tEmptyLines + 1
}

For i = tMethodStart+1:1:tMethodEnd {
Set tClassLineNum = i-tMethodStart
Set tFullMap(i) = $lb("CLS", tClass,tMethodName, tClassLineNum, tClassLineNum)
Set tFullMap(i) = $lb("CLS", tClass,tMethodName, tClassLineNum+tEmptyLines, tClassLineNum+tEmptyLines)

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

ClassMethod GetCurrentHash(pName As %String, pType As %String, Output pHash As %String, Output pCodeArray As %String, Output pCache) As %Status [ Private ]
ClassMethod GetCurrentHash(pName As %String, pType As %String, Output pHash As %String, Output pCodeArray As %String, Output pCache) As %Status
{
Set tSC = $$$OK
Try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ Method TestCodeUnitCreation()
Set tCodeGeneratorLine = tClsCodeUnit.MethodMap.GetAt("SampleCodeGenerator")
Set tNormalMethodLine = tClsCodeUnit.MethodMap.GetAt("SampleNormalMethod")
Set tPythonMethodLine = tClsCodeUnit.MethodMap.GetAt("SamplePythonMethod")
set tPythonWeirdSpacingMethodLine = tClsCodeUnit.MethodMap.GetAt("PythonWeirdSpacing")

Do $$$AssertNotEquals(tConstantReturnValueLine,"")
Do $$$AssertNotEquals(tCodeGeneratorLine,"")
Do $$$AssertNotEquals(tNormalMethodLine,"")
Do $$$AssertNotEquals(tPythonMethodLine,"")
Do $$$AssertNotEquals(tPythonWeirdSpacingMethodLine,"")

// test if LineIsPython is working properly
Do $$$AssertEquals(tClsCodeUnit.LineIsPython.GetAt(tPythonMethodLine+2), 1)
Expand All @@ -69,6 +71,10 @@ Method TestCodeUnitCreation()
Set tTestLines(tNormalMethodLine+3) = $ListBuild("SampleNormalMethod+2",,,tIntCodeUnit.Hash,tIntCodeUnit.MethodMap.GetAt(methodLabel)+2, "INT")
Set tTestLines(tPythonMethodLine+2) = $ListBuild("SamplePythonMethod+1",,,tPyCodeUnit.Hash,tPyCodeUnit.MethodMap.GetAt("SamplePythonMethod")+1, "PY")
Set tTestLines(tPythonMethodLine+3) = $ListBuild("SamplePythonMethod+2",,,tPyCodeUnit.Hash,tPyCodeUnit.MethodMap.GetAt("SamplePythonMethod")+2, "PY")
Set tTestLines(tPythonWeirdSpacingMethodLine+4) = $ListBuild("PythonWeirdSpacing+1",,,tPyCodeUnit.Hash,tPyCodeUnit.MethodMap.GetAt("PythonWeirdSpacing")+1, "PY")
Set tTestLines(tPythonWeirdSpacingMethodLine+5) = $ListBuild("PythonWeirdSpacing+2",,,tPyCodeUnit.Hash,tPyCodeUnit.MethodMap.GetAt("PythonWeirdSpacing")+2, "PY")
Set tTestLines(tPythonWeirdSpacingMethodLine+6) = $ListBuild("PythonWeirdSpacing+3",,,tPyCodeUnit.Hash,tPyCodeUnit.MethodMap.GetAt("PythonWeirdSpacing")+3, "PY")

Set tLine = ""
For {
Set tLine = $Order(tTestLines(tLine),1,tInfo)
Expand Down Expand Up @@ -149,4 +155,13 @@ ClassMethod SamplePythonMethod() [ Language = python ]
return 50
}

ClassMethod PythonWeirdSpacing() [ Language = python ]
{


x = [0] * 10
x.append([50])
return [element * 2 for element in x]
}

}

0 comments on commit 73708c5

Please sign in to comment.