Item assignment and deletion raise `IndexError` instead of `TypeError` on types without them, for a huge index
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
バグは Objects/abstract.c の PyObject_SetItem と PyObject_DelItem にあります。そこでは、sq_ass_item の存在を確認する前に、インデックスキーを PyNumber_AsSsize_t(key, PyExc_IndexError) で変換しています。まずこの2つの関数と、sq_ass_item をすでに確認している非インデックス側の分岐を読んでください。tuple、set、str、range に巨大な int を使って要素の代入または削除を行うと TypeError が発生するようになり、報告にある4つのケースを対象とした 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分
- マージ済み PR(30日)
- 610
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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 日以内に返信