Skip to content

bpo-40651: Bugfix of the LRU implementation based on OrderedDict in the collections document #20139

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

Merged
merged 1 commit into from
May 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Doc/library/collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,8 @@ variants of :func:`functools.lru_cache`::
return value

def __setitem__(self, key, value):
if key in self:
self.move_to_end(key)
super().__setitem__(key, value)
if len(self) > self.maxsize:
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 this part could be replaced by self.popitem(last=False) now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right.
It is cleaner to remove the oldest entry by self.popitem(last=False).

At the same time, It is working correctly.

Do you suggest I update it in this pull request?
Not sure about the conventions here.
Normally we are only supposed to change the code directly related to the issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know, I think popitem() was added in Python3 so this block of code needed to be compatible with Python 2 until this year.

Let's wait for @rhettinger review, I can change it in another PR if he thinks it's needed.

oldest = next(iter(self))
Expand Down