Skip to content

gh-146268: Fix case where assigning an infinite generator to an extend slice hangs instead of raising ValueError#146272

Open
csm10495 wants to merge 5 commits intopython:mainfrom
csm10495:copilot/fix-bug-146268
Open

gh-146268: Fix case where assigning an infinite generator to an extend slice hangs instead of raising ValueError#146272
csm10495 wants to merge 5 commits intopython:mainfrom
csm10495:copilot/fix-bug-146268

Conversation

@csm10495
Copy link
Contributor

gh-146268: Fix case where assigning an infinite generator to an extend slice hangs instead of raising ValueError

Also add in some unit tests and a blurb entry.

step != 1 is special since we will only perform that assignment if the size of the right matches that of the left. An infinitely sized right hand side can never fill in a (non infinite) left hand side. So we calculate out the left hand side's slice length, then use that to determine if we went over (+1 the left hand's length).

Copilot AI and others added 5 commits March 22, 2026 06:51
…-146268)

When assigning an arbitrary iterable to an extended slice (step != 1),
use bounded iteration to collect at most slicelength + 1 items instead
of eagerly consuming the entire iterable via PySequence_Fast. This
prevents infinite iterators from hanging indefinitely.

The slice length is recomputed after consuming the iterable to handle
pathological iterators that mutate the list during iteration.

Co-authored-by: csm10495 <5749838+csm10495@users.noreply.github.com>
Agent-Logs-Url: https://github.com/csm10495/cpython/sessions/996cabbe-7dc6-4faa-b95b-31907ef41b20
@python-cla-bot
Copy link

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@johnslavik
Copy link
Member

johnslavik commented Mar 22, 2026

This isn't ready yet.

❯ py t.py
Traceback (most recent call last):
  File "/Users/bartosz.slawecki/Python/cpython/t.py", line 10, in <module>
    l[::2] = MyGen()
    ~^^^^^
ValueError: attempt to assign sequence of size 3 to extended slice of size 2

The error message from this approach leaks internal implementation details. MyGen() (from the PR) is not a sequence of size 3, it's a generator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants