Skip to content

Commit 78a81d8

Browse files
committed
gh-145865: Fix CoverageResults.__init__ to copy the counts dict
Fix typo where self.counter was assigned instead of self.counts, causing the counts dict to not be copied. This made CoverageResults.update() mutate the caller's original dict. The fix changes self.counter = self.counts.copy() to self.counts = self.counts.copy(), matching the pattern used for calledfuncs and callers on the following lines.
1 parent d5b2681 commit 78a81d8

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

Lib/test/test_trace.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,5 +590,32 @@ def test_no_source_file(self):
590590
self.assertIn(f"{filename}({firstlineno + 4})", out[4])
591591

592592

593+
class TestCoverageResultsInit(unittest.TestCase):
594+
def test_counts_dict_is_copied(self):
595+
# gh-145865: CoverageResults.__init__ should copy the counts dict
596+
from trace import CoverageResults
597+
598+
counts = {}
599+
cr = CoverageResults(counts=counts)
600+
cr.update(CoverageResults(counts={("file.py", 1): 5}))
601+
self.assertEqual(counts, {})
602+
603+
def test_calledfuncs_dict_is_copied(self):
604+
from trace import CoverageResults
605+
606+
calledfuncs = {}
607+
cr = CoverageResults(calledfuncs=calledfuncs)
608+
cr.update(CoverageResults(calledfuncs={("file.py", "mod", "func"): 1}))
609+
self.assertEqual(calledfuncs, {})
610+
611+
def test_callers_dict_is_copied(self):
612+
from trace import CoverageResults
613+
614+
callers = {}
615+
cr = CoverageResults(callers=callers)
616+
cr.update(CoverageResults(callers={(("a.py", "m", "f"), ("b.py", "m", "g")): 1}))
617+
self.assertEqual(callers, {})
618+
619+
593620
if __name__ == '__main__':
594621
unittest.main()

Lib/trace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def __init__(self, counts=None, calledfuncs=None, infile=None,
155155
self.counts = counts
156156
if self.counts is None:
157157
self.counts = {}
158-
self.counter = self.counts.copy() # map (filename, lineno) to count
158+
self.counts = self.counts.copy() # map (filename, lineno) to count
159159
self.calledfuncs = calledfuncs
160160
if self.calledfuncs is None:
161161
self.calledfuncs = {}

0 commit comments

Comments
 (0)