python / python/cpython

`trace.CoverageResults.__init__` does not copy the counts dict

Đang mở
#145,865 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-bug
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

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. 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

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.