Item assignment and deletion raise `IndexError` instead of `TypeError` on types without them, for a huge index

Aperta Adatta ai principianti
#162,466 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c, python
Ambito
backend

Direzione di ricerca

Il bug si trova in PyObject_SetItem e PyObject_DelItem in Objects/abstract.c, dove una chiave di indice viene convertita con PyNumber_AsSsize_t(key, PyExc_IndexError) prima di verificare che sq_ass_item esista. Inizia leggendo queste due funzioni e il ramo per le chiavi non di indice, che controlla già sq_ass_item. È completato quando l'assegnazione o la cancellazione di un elemento di tuple, set, str e range con un int enorme solleva TypeError e un test di regressione in Lib/test che copra i quattro casi del report viene superato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

interpreter-core type-bug

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

Lingua principale
Python
Stelle
78.4k
Fork
36.6k
Merge medio
23h 34m
PR unite (30g)
610

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python/cpython

Tutte le issue di python/cpython

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.