python / python/cpython

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

未关闭
#146,268 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

使用 issue 中扩展切片赋值的示例复现挂起问题,然后跟踪会消耗 iterable 的内置扩展切片赋值路径。添加一个涵盖无限生成器的回归测试,并验证切片长度超出后赋值会引发 ValueError。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。