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 天内回复