python / python/cpython

Attempting to assign an infinte generator to an extended slice hangs indefinitely instead of raising ValueError

Aperta
#146,268 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

interpreter-core type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug report

Bug description:

For builtin types which support assignment to extended slices (i.e. slices with a step other than 1), the length of the iterable being assigned from must be equal to the length of the slice being assigned to. However, Python appears to determine the length of the iterable by repeatedly calling next() until StopIteration is raised.

In the event that an object never raises StopIteration, this creates an infinite evaluation loop, even though it should be apparent that the two sizes are not equal once the size of the slice has been exceeded. See the example below:

class MyGen:
    def __iter__(self):
        while True:
            yield "foo"

l = [0,1,2,3]

l[::2] = range(10)  # raises ValueError: attempt to assign sequence of size 10 to extended slice of size 2
l[::2] = MyGen()  # hangs indefinitely despite returning more than 2 values
CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs
  • gh-146272

Guida per i contributori

Apri la guida per i contributori

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.

Direzione di ricerca

Riproduci il blocco con gli esempi di assegnazione a slice estesi nell’issue, quindi segui il percorso di assegnazione builtin a slice estesi che consuma l’iterabile. Aggiungi un test di regressione che copra un generatore infinito e verifica che l’assegnazione sollevi ValueError una volta superata la lunghezza dello slice.

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

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.