python / python/cpython

`deque.index` result changed in 3.14.7 when a bound's `__index__` mutates the deque

未关闭
#156,743 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.14 3.15 3.16 docs
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

I'm Samuel the human, and I endorse this message.


(written by claude)

Bug description

gh-150750 (merged in 3.14.7 via #150921) changed the result of deque.index when a bound's __index__ mutates the deque.
The change is not mentioned in the NEWS entry, which only describes the free-threading race, and is not covered by a test.

from collections import deque

d = deque([1, 2, 3])

class Grow:
    def __index__(self):
        d.extend([2, 2, 2])
        return -1

print(d.index(2, Grow()))

3.14.6 and earlier raise ValueError: deque.index(x): x not in deque.
3.14.7 prints 5.

Before #150779, stop defaulted to Py_SIZE(deque) read during argument parsing, before the start converter called __index__.
In the example, stop was captured as 3, start=-1 then resolved against the live size 6 to give 5, and start > stop clamped the search to an empty window.
After #150779, deque_index_impl reads the size once, after argument conversion, so both bounds resolve against the mutated size and the search finds the element.

The new behavior is self-consistent (one size snapshot, taken after both converters run) and matches what list.index does, so this is probably fine to keep.
Reporting it because the change is user-visible in a patch release: it may deserve a changelog note and a regression test, or reverting if the old behavior was intended.

Your environment

  • CPython versions tested on: 3.14.0, 3.14.2, 3.14.4 (old behavior); 3.14.7, 3.15.0rc1 (new behavior)
  • Operating system and architecture: macOS arm64
Linked PRs
  • gh-157784

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先重现 deque.index 示例,并查看 deque.index 的实现、NEWS 条目和链接的 PR #157784。完成的标准是确定是否应保留 3.14.7 的行为;如果应保留,则记录该更改并补充缺失的回归测试覆盖。

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

评估

技术栈
python
领域
documentation, testing
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
35/100

把新 issue 发到你的邮箱

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