Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-125038: redundant GET_ITER instructions are removed from genexpr code#126408

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

efimov-mikhail
Copy link
Contributor

@efimov-mikhailefimov-mikhail commentedNov 4, 2024
edited by github-actionsbot
Loading

AddsCHECK_ITERABLE instruction. It checks thatTOS is iterable or async iterable.
IMO, new simple instruction is a good way to prevent behavior changes.

RedundantGET_ITER instructions are removed from generator expression code.
Those were added during previous fix.


📚 Documentation preview 📚:https://cpython-previews--126408.org.readthedocs.build/

@efimov-mikhail
Copy link
ContributorAuthor

@JelleZijlstraJelleZijlstra self-requested a reviewNovember 4, 2024 19:14
@ZeroIntensityZeroIntensity self-requested a reviewNovember 6, 2024 13:55
@ZeroIntensity
Copy link
Member

I'll do the initial review on this later today. In the meantime, you can rebase against main to fix the JIT failures (seegh-126465).

efimov-mikhail reacted with thumbs up emoji

…nexpr codeAdds CHECK_ITERABLE instruction. It checks that TOS is iterable orasync iterable. Redundant GET_ITER and GET_AITER instructions areremoved from generator expression code.
@efimov-mikhailefimov-mikhailforce-pushed theissue_125038_get_iter_rework branch from9bbdf16 toaffb1f8CompareNovember 6, 2024 15:36
@ZeroIntensity
Copy link
Member

For future reference, don't force push. Everything is squashed onto main anyway.

@efimov-mikhail
Copy link
ContributorAuthor

For future reference, don't force push. Everything is squashed onto main anyway.

I've used the Github button "rebase", it made force push.
But local merging with main and pushing to this branch is preffered, isn't it?

@JelleZijlstra
Copy link
Member

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.

efimov-mikhail reacted with thumbs up emoji

Copy link
Member

@ZeroIntensityZeroIntensity left a 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.

efimov-mikhailand others added3 commitsNovember 7, 2024 08:47
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
@efimov-mikhailefimov-mikhail marked this pull request as draftNovember 7, 2024 06:28
@efimov-mikhailefimov-mikhail marked this pull request as ready for reviewNovember 7, 2024 19:51
ZeroIntensity
ZeroIntensity previously approved these changesNov 7, 2024
Copy link
Member

@ZeroIntensityZeroIntensity left a 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?

efimov-mikhail reacted with heart emoji
@markshannon
Copy link
Member

I thought the idea was to entirely remove theGET_ITER, now that the generator expression handles the conversion from iterable to iterator.

I realize that would be a slight change, but it makes generator expressions behave the same as generators:

>>>defmake_gen(x):...foriinx:...yieldi...>>>gen=make_gen(1)# No error here>>>next(gen)Traceback (mostrecentcalllast):File"<python-input-28>",line1,in<module>next(gen)~~~~^^^^^File"<python-input-26>",line2,inmake_genforiinx:^TypeError:'int'objectisnotiterable

Creating a generator with a non-iterable is fine. It is only we try to iterate over it, that an error occurs.

@efimov-mikhail
Copy link
ContributorAuthor

efimov-mikhail commentedNov 12, 2024
edited
Loading

It's not possible to check arguments of an arbitrary generators, since it can be of any type.
For example, this generator is correct:

defmake_range_gen(n,m):foriinrange(n+m*5):yieldi

But generator expressions can have only one parameter, which should beIterable.
So we can guarantee that argument is reallyIterable.

Let's look at the solution withCHECK_ITERABLE bytecode.
Pros:

  1. Backward compatibility in all details.
  2. Early detection of incorrect code in genexprs.
  3. Very small impact on perfomance, almost zero, since implementation ofCHECK_ITERABLE is very simple.

Cons:

  1. Small impact on memory size of compiled object (+2 bytes on each creation of genexpr).
  2. New bytecode with little purpose, which uses one of byte values in range(256), and we want to keep those values for important bytecodes.

I'm not sure about the balance of this pros and cons for the typical user of Python.
Backward compatibility seems to be essential advantage.
But, of course, I can removeCHECK_ITERABLE, if you sure that solution without it is better.

@ZeroIntensity,@JelleZijlstra, what do you think?

@sobolevn
Copy link
Member

Creating a generator with a non-iterable is fine. It is only we try to iterate over it, that an error occurs.

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.

efimov-mikhail reacted with thumbs up emoji

@ZeroIntensityZeroIntensity dismissed theirstale reviewNovember 13, 2024 11:46

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

@efimov-mikhail
Copy link
ContributorAuthor

efimov-mikhail commentedApr 12, 2025
edited
Loading

It seems that this PR can be closed because other PR on this topic (#132351) is already merged.

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

@ZeroIntensityZeroIntensityZeroIntensity left review comments

@ericsnowcurrentlyericsnowcurrentlyAwaiting requested review from ericsnowcurrentlyericsnowcurrently is a code owner

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

@iritkatrieliritkatrielAwaiting requested review from iritkatrieliritkatriel is a code owner

@JelleZijlstraJelleZijlstraAwaiting requested review from JelleZijlstra

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

5 participants
@efimov-mikhail@ZeroIntensity@JelleZijlstra@markshannon@sobolevn

[8]ページ先頭

©2009-2025 Movatter.jp