Item assignment and deletion raise `IndexError` instead of `TypeError` on types without them, for a huge index
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 62/100
Research direction
The bug is in PyObject_SetItem and PyObject_DelItem in Objects/abstract.c, where an index key is converted with PyNumber_AsSsize_t(key, PyExc_IndexError) before checking that sq_ass_item exists. Start by reading those two functions and the non-index branch, which already checks sq_ass_item. Done when tuple, set, str and range item assignment or deletion with a huge int raises TypeError, and a regression test in Lib/test covering the four cases from the report passes.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- Python
- Stars
- 78.4k
- Forks
- 36.6k
- Avg merge
- 23h 34m
- Merged PRs (30d)
- 610
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/cpython
-
stdlib topic-subprocess type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
corner case: base64.b32decode ignorechars=b'=' miscounts paddingPossibly taken @ElDShell claimed this 1 day ago. Open3.15 easy type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
python/cpython#162353 · 1 comment ·
Maintainers usually reply within 1 day
-
TracebackException.lineno changes from str to int after format() when a keyword typo suggestion is foundPossibly taken @Sudhakar7J claimed this 2 days ago. Openstdlib type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
python/cpython#162296 · 1 comment ·
Maintainers usually reply within 1 day
-
[HACL] The `HACL_CAN_COMPILE_VEC128` macro is not defined when building Python with `-Wundef`Possibly taken @tamaki0575-cloud claimed this 1 day ago. Openbuild extension-modules topic-C-API type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
python/cpython#162219 · 2 comments ·
Maintainers usually reply within 1 day
-
zipfile: ZipInfo.comment exceeding 65535 bytes causes struct.error in ZipFile.close() without proper validationPossibly taken @1cbyc claimed this 6 days ago. Openstdlib type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
Similar issues
-
bot-triaged module: correctness (silent) module: fft triage review
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
conda-forge/zarpaint-feedstock#8 · 1 comment · 1 reaction ·
-
plot_prediction colorbar shows a 0-1 range instead of the data rangePossibly taken @nikhil3495 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
mllam/neural-lam#902 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
didi/helixharness#28 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 1/5 Under an hour Newbie friendliness 62/100
DaoCloud/DaoCloud-docs#7839 ·
Maintainers usually reply within 1 day