Visitar URL original
bpo-46310: simplify `for` loop in `asyncio/windows_events` by sobolevn · Pull Request #30334 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20260131033426/https://github.com/python/cpython/pull/30334
Skip to content
/ cpython Public

Conversation

@sobolevn
Copy link
Member

@sobolevn sobolevn commented Jan 2, 2022 •

It used to have two problems:

  1. Extra list() call on results, not sure why it was needed
  2. .items() with unused key, .values() simplifies that

I don't think we need NEWS or issue for that.

https://bugs.python.org/issue46310


# Cancel remaining registered operations.
for address, (fut, ov, obj, callback) in list(self._cache.items()):
for fut, ov, obj, callback in self._cache.values():
Copy link
Member Author

Choose a reason for hiding this comment

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

ov, obj, callback are unused as well, but I've decided to keep them, because that's how other places do.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think list was necessary here to make a copy of the cache before iteration so that if any thread makes any change to the cache then it won't cause a RuntimeError. Moreover IIRC list on dict items is an atomic operation as it calls a c function.

Copy link
Contributor

Choose a reason for hiding this comment

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

fut.cancel() can have a registered callback with a side-effect that modifies self._cache dict.
The unrecoverable failure in loop.close() is not desirable.
self._cache.items() can be replaced with self._cache.values() safely, sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! TIL 🙂

@merwok
Copy link
Member

merwok commented Jan 2, 2022

Can you open a ticket? It should be rare that a change is made without a ticket, for example fixing a typo or doing some gardening in github config files. Changing code however should be done with a good reason: fixing a problem, improving behaviour or performance, or adding a new feature, therefore it should have a ticket (which can be seen by many people using different channels, contrary to github PRs).

Minor sidenote: I would not call this small change a refactoring! The word is used for everything now, when it should mean: changing internal organization of code without affecting its visible effects. We can have reworks, changes, simplifications, optimizations, removal of unneeded code, etc 🙂

Copy link
Contributor

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

Please revert back to iteration over the view's copy.


# Cancel remaining registered operations.
for address, (fut, ov, obj, callback) in list(self._cache.items()):
for fut, ov, obj, callback in self._cache.values():
Copy link
Contributor

Choose a reason for hiding this comment

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

fut.cancel() can have a registered callback with a side-effect that modifies self._cache dict.
The unrecoverable failure in loop.close() is not desirable.
self._cache.items() can be replaced with self._cache.values() safely, sure.

@bedevere-bot
Copy link

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.

@sobolevn
Copy link
Member Author

sobolevn commented Jan 4, 2022

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

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

@bedevere-bot bedevere-bot requested a review from asvetlov January 4, 2022 09:16
@merwok
Copy link
Member

merwok commented Jan 4, 2022

A ticket is needed, please see my previous comment

@sobolevn sobolevn changed the title Microrefactoring: simplify for loop in asyncio/windows_events bpo-46310: simplify for loop in asyncio/windows_events Jan 9, 2022
@sobolevn
Copy link
Member Author

sobolevn commented Jan 9, 2022

Done!

@merwok thanks a lot for guiding me! 👍
@asvetlov thanks a lot for the review.

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment •

Choose a reason for hiding this comment

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

This is a minor refactoring which does not affect behavior. Please remove the NEWS entry. It only overwhelm users with useless information.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

@asvetlov asvetlov merged commit fc75bfb into python:main Jan 11, 2022
@asvetlov
Copy link
Contributor

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.

7 participants