Skip to content

trace.CoverageResults.__init__ does not copy the counts dict #145865

@stefanzetzsche

Description

@stefanzetzsche

Issue

CoverageResults.__init__ copies calledfuncs and callers via .copy() but assigns counts directly. This means self.counts is an alias to the caller's dict, and calling update() mutates the original dict passed to the constructor.

Reproducer

from trace import CoverageResults

# counts is NOT copied — original is mutated:
counts = {}
cr = CoverageResults(counts=counts)
cr.update(CoverageResults(counts={('file.py', 1): 5}))
print(counts)  # {('file.py', 1): 5} — mutated

# calledfuncs IS copied — original is not mutated:
calledfuncs = {}
cr = CoverageResults(calledfuncs=calledfuncs)
cr.update(CoverageResults(calledfuncs={('file.py', 'mod', 'func'): 1}))
print(calledfuncs)  # {} — not mutated

Impact

The documentation states that CoverageResults "should not be created directly by the user," so direct external impact is limited. However, the inconsistency also affects internal usage: Trace.results() passes its internal self.counts directly to the constructor, so calling update() on the returned CoverageResults will mutate the Trace object's internal state.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions