python / python/cpython

sum() deallocates float subclass instances as exact floats in the complex fast path

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

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

interpreter-core 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ả

Bug report

Bug description:

Bug report

Description

In builtin_sum_impl() (Python/bltinmodule.c), the complex-number fast
path tests items with PyFloat_Check() but releases them with
_Py_DECREF_SPECIALIZED(item, _PyFloat_ExactDealloc).

PyFloat_Check() also matches subclasses of float, whereas
_PyFloat_ExactDealloc is only valid for exact float objects. When a
float subclass instance reaches refcount 0 through this path, it is
deallocated as an exact float, bypassing its real tp_dealloc
(instance __dict__, weakref list, and GC bookkeeping). This can leak
memory and leave the cyclic GC in an inconsistent state.

The adjacent float fast path in the same function already guards the
identical specialized dealloc with PyFloat_CheckExact(); only the
complex branch uses the broader PyFloat_Check().

Reproducer

class F(float):
    pass

# complex start value selects the complex fast path
sum([F(1.0), F(2.0)], 0j)

Proposed fix

In the complex branch, use PyFloat_CheckExact(item) instead of
PyFloat_Check(item), so subclass instances fall through to the generic
PyNumber_Add path with a normal Py_DECREF. Numeric behavior is
unchanged.

Environment

  • CPython: main branch (please confirm the line is still present)
CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-150869
  • gh-151063

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 Python/bltinmodule.c tại builtin_sum_impl() và so sánh fast path của complex với fast path float liền kề. Xác minh kiểm tra giải phóng chuyên biệt dành cho các lớp con của float và xác nhận rằng reproducer hoàn tất mà lớp con không bị giải phóng không đúng cách; các PR được liên kết gh-150869 và gh-151063 cho thấy công việc có thể đã được tiến hành.

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
backend
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
35/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.