-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-125038: redundant GET_ITER instructions are removed from genexpr code #126408
base: main
Are you sure you want to change the base?
gh-125038: redundant GET_ITER instructions are removed from genexpr code #126408
Conversation
I'll do the initial review on this later today. In the meantime, you can rebase against main to fix the JIT failures (see gh-126465). |
…nexpr code Adds CHECK_ITERABLE instruction. It checks that TOS is iterable or async iterable. Redundant GET_ITER and GET_AITER instructions are removed from generator expression code.
9bbdf16
to
affb1f8
Compare
For future reference, don't force push. Everything is squashed onto main anyway. |
I've used the Github button "rebase", it made force push. |
I like to use the "Update branch" button, which merges in main without a need for local actions. We try to avoid force-pushes since they make it harder to track the history of the PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, this is looking pretty good! Several comments, though, as this is a pretty big PR.
Misc/NEWS.d/next/Core_and_Builtins/2024-11-04-21-58-20.gh-issue-125038.kGhWOS.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I think this is ready for a core dev to review it. @JelleZijlstra, would you mind taking a look?
I thought the idea was to entirely remove the I realize that would be a slight change, but it makes generator expressions behave the same as generators: >>> def make_gen(x):
... for i in x:
... yield i
...
>>> gen = make_gen(1) # No error here
>>> next(gen)
Traceback (most recent call last):
File "<python-input-28>", line 1, in <module>
next(gen)
~~~~^^^^^
File "<python-input-26>", line 2, in make_gen
for i in x:
^
TypeError: 'int' object is not iterable Creating a generator with a non-iterable is fine. It is only we try to iterate over it, that an error occurs. |
It's not possible to check arguments of an arbitrary generators, since it can be of any type. def make_range_gen(n, m):
for i in range(n + m * 5):
yield i But generator expressions can have only one parameter, which should be Let's look at the solution with
Cons:
I'm not sure about the balance of this pros and cons for the typical user of Python. @ZeroIntensity, @JelleZijlstra, what do you think? |
Can we please discuss this a bit more? I don't think that changing where the error happens is safe in terms of backward compatibility. I am 100% sure that people expect error to happen when the gen is created and I am also sure that the code reflects that (validation, error handling, etc). If we are going to break this - we need to issue a deprecation period. I also don't fully understand why we are doing this. |
Sorry, there's enough contention here that I'd like to have my check removed from the PR (it was only supposed to denote "ready for core review," anyway).
Adds
CHECK_ITERABLE
instruction. It checks thatTOS
is iterable or async iterable.IMO, new simple instruction is a good way to prevent behavior changes.
Redundant
GET_ITER
instructions are removed from generator expression code.Those were added during previous fix.
📚 Documentation preview 📚: https://cpython-previews--126408.org.readthedocs.build/