Skip to content

Commit 41a87e2

Browse files
authored
Merge pull request #47 from intersystems/PythonMethodSpacing
Embedded Python bug fixes
2 parents 77981bc + 7e73f1d commit 41a87e2

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [4.0.1] - 2024-08-09
99

10+
### Fixed
1011
- #45: Fixed Python line 0 tracking for 2024.2
12+
- #46: Fix for bug caused by UpdateComplexity calling GetCurrentByName unnecessarily and causing dependency issues
13+
- #47: Fixed mapping issue caused by empty lines at top of Python method not showing up in compiled Python
1114

1215
## [4.0.0] - 2024-08-01
1316

cls/TestCoverage/Data/CodeUnit.cls

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ Property LineIsPython As array Of %Boolean;
4444
/// Set to true if this class/routine is generated
4545
Property Generated As %Boolean [ InitialExpression = 0 ];
4646

47-
///
47+
/// If the CodeUnit has changed since we last updated it, used to see if we need to call UpdateComplexity
48+
Property OutdatedComplexity As %Boolean [ InitialExpression = 1 ];
49+
4850
/// Methods, branches, etc. within this unit of code.
4951
Relationship SubUnits As TestCoverage.Data.CodeSubUnit [ Cardinality = children, Inverse = Parent ];
5052

@@ -87,6 +89,9 @@ ClassMethod GetCurrentByName(pInternalName As %String, pSourceNamespace As %Stri
8789
If (tUpdatedHash '= tKnownHash) {
8890
//Clear out old data and flag the need for an update.
8991
Set tNeedsUpdate = 1
92+
If $IsObject($Get(tMapToUnit)) {
93+
set tMapToUnit.OutdatedComplexity = 1
94+
}
9095
&sql(delete from TestCoverage_Data.CodeUnitMap where ToHash = :tKnownHash)
9196
If (SQLCODE < 0) {
9297
Throw ##class(%Exception.SQL).CreateFromSQLCODE(SQLCODE,%msg)
@@ -399,17 +404,27 @@ Method UpdateSourceMap(pSourceNamespace As %String, ByRef pCache) As %Status
399404
Set tMethodStart = ..MethodMap.GetAt(tMethod)
400405
Set tMethodEnd = ..MethodEndMap.GetAt(tMethod)
401406
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))
402409
Set tFullMap(tMethodStart) = $lb("CLS", tClass,tMethodName, -1, -1) ; -1 because the class
403410
; 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+
404419
For i = tMethodStart+1:1:tMethodEnd {
405420
Set tClassLineNum = i-tMethodStart
406-
Set tFullMap(i) = $lb("CLS", tClass,tMethodName, tClassLineNum, tClassLineNum)
421+
Set tFullMap(i) = $lb("CLS", tClass,tMethodName, tClassLineNum+tEmptyLines, tClassLineNum+tEmptyLines)
407422

408423
// extra check to make sure that the lines we're mapping between are the same as expected
409-
Set tClassLineCode = $zstrip(pCLSCodeUnit.Lines.GetAt(tCLSMethodNum + tClassLineNum + 1), "<>W")
424+
Set tClassLineCode = $zstrip(pCLSCodeUnit.Lines.GetAt(tCLSMethodNum + 1 + tEmptyLines + tClassLineNum), "<>W")
410425
Set tPyLineCode = $zstrip(..Lines.GetAt(i), "<>W")
411426
if (tPyLineCode '= tClassLineCode) {
412-
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))
413428
}
414429
}
415430
Do ..MethodMap.GetNext(.tMethod)
@@ -521,9 +536,10 @@ Method UpdateSourceMap(pSourceNamespace As %String, ByRef pCache) As %Status
521536
}
522537
}
523538

524-
// Update cyclomatic complexity for methods in the linked class
539+
// Update cyclomatic complexity for methods in the linked class if we don't already have the newest version
525540
Set tClass = $Order(tCodeUnits("CLS",""),1,tClassCodeUnit)
526-
If $IsObject($Get(tClassCodeUnit)) {
541+
If ($IsObject($Get(tClassCodeUnit)) && (tClassCodeUnit.OutdatedComplexity)){
542+
set tClassCodeUnit.OutdatedComplexity = 0
527543
$$$ThrowOnError(tClassCodeUnit.UpdateComplexity())
528544
}
529545
} Catch e {
@@ -589,7 +605,7 @@ Method GetMethodOffset(pAbsoluteLine As %Integer, Output pMethod As %String, Out
589605
{
590606
}
591607

592-
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
593609
{
594610
Set tSC = $$$OK
595611
Try {
@@ -690,6 +706,9 @@ Storage Default
690706
<Value name="5">
691707
<Value>Generated</Value>
692708
</Value>
709+
<Value name="6">
710+
<Value>OutdatedComplexity</Value>
711+
</Value>
693712
</Data>
694713
<Data name="LineIsPython">
695714
<Attribute>LineIsPython</Attribute>

cls/TestCoverage/Manager.cls

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,11 @@ ClassMethod OnBeforeAllTests(manager As TestCoverage.Manager, dir As %String, By
683683
Set tSC = manager.UpdateCoverageTargetsForTestDirectory(dir)
684684
$$$ThrowOnError(tSC)
685685
}
686-
686+
if (manager.ListenerManager) {
687+
set tObj = {"message": "Starting tests"}
688+
Do manager.ListenerManager.BroadCastToAll(tObj)
689+
}
687690
If (manager.CoverageDetail = 0) {
688-
if (manager.ListenerManager) {
689-
set tObj = {"message": "Starting tests"}
690-
Do manager.ListenerManager.BroadCastToAll(tObj)
691-
}
692691
Set tSC = manager.StartCoverageTracking()
693692
$$$ThrowOnError(tSC)
694693
}

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)