Item assignment and deletion raise `IndexError` instead of `TypeError` on types without them, for a huge index
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
缺陷位於 Objects/abstract.c 中的 PyObject_SetItem 和 PyObject_DelItem。這裡在檢查 sq_ass_item 是否存在之前,先用 PyNumber_AsSsize_t(key, PyExc_IndexError) 轉換索引鍵。請先閱讀這兩個函式,以及已經檢查 sq_ass_item 的非索引分支。當 tuple、set、str 和 range 使用超大 int 進行元素賦值或刪除時拋出 TypeError,並且 Lib/test 中涵蓋報告中四種情況的回歸測試通過,即為完成。
由索引模型根據 Issue 內容生成。
描述
Bug report
Bug description:
Assigning to or deleting an item of a tuple, str, bytes, range, set or frozenset raises TypeError, unless the index is an int that doesn't fit in a Py_ssize_t. Then it raises IndexError, although the type supports no item assignment or deletion at all:
for stmt in ["s = {1}; s[0] = 0", "s = {1}; s[2**63] = 0", "t = (1, 2); del t[0]", "t = (1, 2); del t[2**63]"]:
try:
exec(stmt)
except Exception as e:
print(f"{stmt:26} {type(e).__name__}: {e}")
s = {1}; s[0] = 0 TypeError: 'set' object does not support item assignment
s = {1}; s[2**63] = 0 IndexError: cannot fit 'int' into an index-sized integer
t = (1, 2); del t[0] TypeError: 'tuple' object doesn't support item deletion
t = (1, 2); del t[2**63] IndexError: cannot fit 'int' into an index-sized integer
A set isn't even subscriptable, and s[2**63] (a read) does give TypeError: 'set' object is not subscriptable. The cause is in PyObject_SetItem and PyObject_DelItem (Objects/abstract.c): when the type has tp_as_sequence, an index key is converted with PyNumber_AsSsize_t(key, PyExc_IndexError) before anyone checks that sq_ass_item exists. The branch for a non-index key already checks sq_ass_item; checking it first in both branches would give the TypeError. PyPy 3.12 raises TypeError in all four cases. I found it while writing a Lean spec of Python.
CPython versions tested on:
3.12, 3.15, CPython main branch
Operating systems tested on:
Linux
- 主要語言
- Python
- 星號
- 78.4k
- 分支
- 36.6k
- 平均合併
- 23 小時 34 分鐘
- 30 天內合併 PR
- 610
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/cpython 的其他 Issue
-
stdlib topic-subprocess type-bug
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
corner case: base64.b32decode ignorechars=b'=' miscounts padding可能已有人在做 @ElDShell 今天認領。 未關閉3.15 easy type-bug
難度 2/5 1-3 小時 新手友好度 62/100
python/cpython#162353 · 1 則留言 ·
維護者通常 1 天內回覆
-
TracebackException.lineno changes from str to int after format() when a keyword typo suggestion is found可能已有人在做 @Sudhakar7J 於 2 天前認領。 未關閉stdlib type-bug
難度 2/5 1-3 小時 新手友好度 67/100
python/cpython#162296 · 1 則留言 ·
維護者通常 1 天內回覆
-
[HACL] The `HACL_CAN_COMPILE_VEC128` macro is not defined when building Python with `-Wundef`可能已有人在做 @tamaki0575-cloud 於 1 天前認領。 未關閉build extension-modules topic-C-API type-bug
難度 2/5 1-3 小時 新手友好度 64/100
python/cpython#162219 · 2 則留言 ·
維護者通常 1 天內回覆
-
zipfile: ZipInfo.comment exceeding 65535 bytes causes struct.error in ZipFile.close() without proper validation可能已有人在做 @1cbyc 於 5 天前認領。 未關閉stdlib type-bug
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
vllm-project/vllm#69304 · 1 則留言 ·
維護者通常 1 天內回覆
-
pydanty:is-working
難度 2/5 1-3 小時 新手友好度 68/100
pydantic/pydantic-ai#14187 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 62/100
維護者通常 1 天內回覆
-
test
難度 2/5 1-3 小時 新手友好度 62/100
prebid/salesagent#2676 ·
維護者通常 1 天內回覆
-
難度 1/5 1-3 小時 新手友好度 62/100
sunososobro-hub/cloudball#339 ·
維護者通常 1 天內回覆