cpython
cpython copied to clipboard
bpo-45975: Simplify some while-loops with walrus operator
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
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.
This PR is stale because it has been open for 30 days with no activity.
You really need to:
- Create an issue on bpo (bugs.python.org)
- Look up its url (it looks like
https://bugs.python.org/issue6778
) and copy a number after/issue
part - 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.
This will make backports to 3.6 and 3.7 more complicated.
@iritkatriel Are backports necessary here? This is neither a bugfix nor a new feature.
@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?
Rebased on main. Reverted changes to distutils (deprecated) and idlelib (split off to https://github.com/python/cpython/pull/31083).
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!
I saw that aifc is among the "dead batteries" to be removed, so I undid the change there.
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.
I have made the requested changes; please review again.
Thanks for making the requested changes!
@ethanfurman: please review the changes made to this pull request.
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.
I have made the requested changes; please review again.
Thanks for making the requested changes!
@ethanfurman: please review the changes made to this pull request.
: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.
Hello, just checking in on this. I suppose it needs to be updated. I can do that, or someone else can.
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 Updated!
PR looks good to me. The status is awaiting merge. I guess @ethanfurman will have to do that.
Thanks to all the reviewers! Your work is greatly appreciated :heart: