python / python/cpython

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

Open
#146,268 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the hang with the extended-slice assignment examples in the issue, then trace the builtin extended-slice assignment path that consumes the iterable. Add a regression test covering an infinite generator and verify that assignment raises ValueError once the slice length is exceeded.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.