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).

@python-cla-bot
Copy link

python-cla-bot bot commented Mar 22, 2026

All commit authors signed the Contributor License Agreement.

CLA 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.

@csm10495 csm10495 force-pushed the copilot/fix-bug-146268 branch from 3b29ec0 to 26fd46e Compare March 22, 2026 14:18
@csm10495
Copy link
Contributor Author

@johnslavik updated based off your feedback. Please check again.

@csm10495 csm10495 marked this pull request as ready for review March 22, 2026 14:58
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does bytearray and array.array handle those cases? likewise what about assigning with itself? does it change the current behavior? a[::2] = a should not be behave differently or if so we should test it

}
else if (step != 1 &&
!PyList_CheckExact(value) &&
!PyTuple_CheckExact(value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have an FastSequence check for that? (fast sequences are lists or tuples). Also we should not hit this path if we have a known length.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we have a way to check if it is a fast seq vs trying to create a new one. Agreed on the second part.

@bedevere-app
Copy link

bedevere-app bot commented Mar 22, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@csm10495
Copy link
Contributor Author

csm10495 commented Mar 22, 2026

How does bytearray and array.array handle those cases? likewise what about assigning with itself? does it change the current behavior? a[::2] = a should not be behave differently or if so we should test it

For me on 3.14.3 and on the latest head of this branch this scenario works the same:

In [1]: a = list(range(10))

In [2]: a
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [3]: a[::2] = a
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 a[::2] = a

ValueError: attempt to assign sequence of size 10 to extended slice of size 5

In [4]:

tbd on bytearray and arrray.array

@csm10495 csm10495 force-pushed the copilot/fix-bug-146268 branch from 8f1056c to c1ad027 Compare March 22, 2026 17:06
@csm10495 csm10495 requested a review from picnixz March 22, 2026 17:07
@csm10495
Copy link
Contributor Author

@picnixz I think i addressed your points. Thanks!

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