Skip to content

Fix extended slice assignment hanging on infinite iterators#4

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/fix-bug-146268
Draft

Fix extended slice assignment hanging on infinite iterators#4
Copilot wants to merge 5 commits intomainfrom
copilot/fix-bug-146268

Conversation

Copy link

Copilot AI commented Mar 22, 2026

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.

Changes Made

  • Objects/listobject.c: Added bounded iteration path for non-list/tuple iterables on extended slice assignment, with distinct error messages depending on the case:
    • Iterator yielded too many items: "attempt to assign iterable that yielded more items than extended slice of size N"
    • Iterator yielded too few items: "attempt to assign sequence of size N to extended slice of larger size"
    • List/tuple size mismatch (unchanged): "attempt to assign sequence of size X to extended slice of size Y"
  • Lib/test/list_tests.py: Added tests for extended slice assignment with infinite generators, finite iterators (correct length, too few, too many), and error message validation
  • NEWS entry: Added via blurb add for Attempting to assign an infinte generator to an extended slice hangs indefinitely instead of raising ValueError python/cpython#146268 attributed to Charles Machalow

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI requested a review from csm10495 March 22, 2026 07:42
Copilot stopped work on behalf of csm10495 due to an error March 22, 2026 07:42
@csm10495
Copy link
Owner

@copilot see python#146272 (comment)

Fix that. The gist is that in this case the size is undetermined because it's an iterable it shouldn't say the size that we've gotten to this far but rather something along the lines of

ValueError: attempt to assign sequence of size 3 to extended slice of larger size.

Make minimal changes and update unit tests. Make sure at least one test ensures the correct exception wording for the situation.

Copy link
Author

Copilot AI commented Mar 22, 2026

@copilot see python#146272 (comment)

Fix that. The gist is that in this case the size is undetermined because it's an iterable it shouldn't say the size ...

Updated the error messages in c4c841f. The bounded iteration path now uses distinct wording for each case:

  • Too many items (including infinite): "attempt to assign iterable that yielded more items than extended slice of size %zd"
  • Too few items: "attempt to assign sequence of size %zd to extended slice of larger size"
  • List/tuple (exact sizes known): unchanged original message

Tests verify the correct exception wording for all three cases.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/python/cpython/issues/comments/4106180648
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (http block)

If you need me to access, download, or install something from one of these locations, you can either:

@csm10495 csm10495 force-pushed the copilot/fix-bug-146268 branch from 3b29ec0 to 26fd46e Compare March 22, 2026 14:18
@csm10495 csm10495 force-pushed the copilot/fix-bug-146268 branch from 8f1056c to c1ad027 Compare March 22, 2026 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants