`trace.CoverageResults.__init__` does not copy the counts dict
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu trong Lib/trace.py tại CoverageResults.init và Trace.results(), nơi issue cho biết từ điển counts được truyền qua. Kiểm tra hành vi cập nhật hiện có và bổ sung coverage để giữ nguyên từ điển của bên gọi; hoàn thành có nghĩa là việc cập nhật một CoverageResults không còn làm thay đổi từ điển được constructor cung cấp hoặc trạng thái nội bộ của đối tượng Trace.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- testing
- Loại issue
- Lỗi
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 25/100