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
分支
36k
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 摘要。