cpython icon indicating copy to clipboard operation
cpython copied to clipboard

bpo-45975: Simplify some while-loops with walrus operator

Open nickdrozd opened this issue 3 years ago • 19 comments

The following pattern occurs a few times in the codebase:

while 1:
    data = conn.recv(blocksize)
    if not data:
        break
    ...

There is an unbounded while loop in which some kind of input is read. If the input is there, it is processed and the loop is continued; otherwise the loop is broken.

This can be expressed more cleanly with the walrus operator:

while data := conn.recv(blocksize):
   ...

Some of this code goes back to the days of Python 1. I assume that the original authors would have used the walrus idiom if it had been available, which it wasn't. But now it is.

Rewriting the instances of this pattern shaves off 148 lines from the codebase. Removing the manual break statements makes the logic more straightforward.

This should not change the behavior of anything. I'm assuming that this code is all under test and that any deviations from the existing behavior will be caught. Anything that isn't tested shouldn't be changed (and should be tested).

https://bugs.python.org/issue45975

nickdrozd avatar Oct 31 '21 16:10 nickdrozd

A code change this extensive must have a bpo issue where the advisability of this change can be debated. I intentionally did not click the [Approve and run] pending that. The opening 'comment' above can be copied as the opening post. It is far too long for a merge comment but great as an issue opener. Connect this PR to the issue by adding 'bpo-#####' to the title. Some issues:

Backports: Code reformats are often not backported. In the other hand, I would want idlelib changes backported after reviewing them. Make a separate PR for idlelib changes. I will likely approve and merge it but have little opinion re other modules.

Deprecated modules (distutils) are generally not patched.

There might be other reasons to oppose changes other modules.

terryjreedy avatar Oct 31 '21 16:10 terryjreedy

This PR is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Dec 02 '21 00:12 github-actions[bot]

You really need to:

  1. Create an issue on bpo (bugs.python.org)
  2. Look up its url (it looks like https://bugs.python.org/issue6778) and copy a number after /issue part
  3. Prepend a title of this pull request with bpo-NNNN: where NNNN is the copied number so the bot will link everything necessary.

The bpo is used since pre-Github times (Python repository migrated many times between hostings), so all development discussions go there.

arhadthedev avatar Dec 02 '21 08:12 arhadthedev

This will make backports to 3.6 and 3.7 more complicated.

iritkatriel avatar Dec 03 '21 23:12 iritkatriel

@iritkatriel Are backports necessary here? This is neither a bugfix nor a new feature.

arhadthedev avatar Dec 04 '21 07:12 arhadthedev

@iritkatriel Are backports necessary here? This is neither a bugfix nor a new feature.

No but if there will be a need in the future to backport a security fix in the same code, it won’t work cleanly because of this PR.

So that’s an argument against this sweeping change. What is the argument in its favour?

iritkatriel avatar Dec 04 '21 09:12 iritkatriel

Rebased on main. Reverted changes to distutils (deprecated) and idlelib (split off to https://github.com/python/cpython/pull/31083).

nickdrozd avatar Feb 02 '22 17:02 nickdrozd

For future PRs to CPython, please don’t use force pushes, they make poor experience for reviewers. All PRs use squash merges so it doesn’t matter if branches are messy. Thanks!

merwok avatar Feb 03 '22 20:02 merwok

I saw that aifc is among the "dead batteries" to be removed, so I undid the change there.

nickdrozd avatar Mar 27 '22 14:03 nickdrozd

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.

bedevere-bot avatar Mar 27 '22 15:03 bedevere-bot

I have made the requested changes; please review again.

nickdrozd avatar Mar 28 '22 22:03 nickdrozd

Thanks for making the requested changes!

@ethanfurman: please review the changes made to this pull request.

bedevere-bot avatar Mar 28 '22 22:03 bedevere-bot

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.

bedevere-bot avatar Mar 29 '22 03:03 bedevere-bot

I have made the requested changes; please review again.

nickdrozd avatar Mar 29 '22 13:03 nickdrozd

Thanks for making the requested changes!

@ethanfurman: please review the changes made to this pull request.

bedevere-bot avatar Mar 29 '22 13:03 bedevere-bot

:robot: New build scheduled with the buildbot fleet by @ethanfurman for commit 1583049e754a2e31c9959c3fc40fa4f3e3842a17 :robot:

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

bedevere-bot avatar Mar 29 '22 15:03 bedevere-bot

Hello, just checking in on this. I suppose it needs to be updated. I can do that, or someone else can.

nickdrozd avatar Oct 26 '22 17:10 nickdrozd

Hello, just checking in on this. I suppose it needs to be updated. I can do that, or someone else can.

@nickdrozd Can you resolve the merge conflicts? I will then review.

eendebakpt avatar Nov 09 '22 20:11 eendebakpt

@eendebakpt Updated!

nickdrozd avatar Nov 11 '22 05:11 nickdrozd

PR looks good to me. The status is awaiting merge. I guess @ethanfurman will have to do that.

eendebakpt avatar Nov 26 '22 22:11 eendebakpt

Thanks to all the reviewers! Your work is greatly appreciated :heart:

nickdrozd avatar Nov 27 '22 13:11 nickdrozd