python / python/cpython

Attempting to assign an infinte generator to an extended slice hangs indefinitely instead of raising ValueError

Đang mở
#146,268 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:

For builtin types which support assignment to extended slices (i.e. slices with a step other than 1), the length of the iterable being assigned from must be equal to the length of the slice being assigned to. However, Python appears to determine the length of the iterable by repeatedly calling next() until StopIteration is raised.

In the event that an object never raises StopIteration, this creates an infinite evaluation loop, even though it should be apparent that the two sizes are not equal once the size of the slice has been exceeded. See the example below:

class MyGen:
    def __iter__(self):
        while True:
            yield "foo"

l = [0,1,2,3]

l[::2] = range(10)  # raises ValueError: attempt to assign sequence of size 10 to extended slice of size 2
l[::2] = MyGen()  # hangs indefinitely despite returning more than 2 values
CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs
  • gh-146272

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

Tái hiện tình trạng treo bằng các ví dụ về phép gán extended-slice trong issue, sau đó lần theo đường dẫn gán extended-slice builtin sử dụng iterable. Thêm một bài kiểm thử hồi quy bao quát một trình tạo vô hạn và xác minh rằng phép gán phát sinh ValueError ngay khi độ dài của slice bị vượt quá.

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ó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá 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.