`trace.CoverageResults.__init__` does not copy the counts dict
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
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
Linked PRs
- gh-146176
- gh-146183
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 Lib/trace.py 中的 CoverageResults.init 和 Trace.results() 開始,issue 指出 counts 字典會被直接傳遞。檢查現有的更新行為,並新增用於保留呼叫端字典的覆蓋測試;完成標準是,更新 CoverageResults 不再修改建構函式提供的字典或 Trace 物件的內部狀態。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- testing
- Issue 類型
- 缺陷
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100