Skip to content

Commit

Permalink
Fix bug when setting existing key in full dict
Browse files Browse the repository at this point in the history
Minimal version of PR mailgun#32 without adding a counter.
  • Loading branch information
Nick Timkovich committed Jan 21, 2020
1 parent cfbeacc commit 34bbc4e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 7 additions & 4 deletions expiringdict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ def __setitem__(self, key, value, set_time=None):
""" Set d[key] to value. """
with self.lock:
if len(self) == self.max_len:
try:
self.popitem(last=False)
except KeyError:
pass
if key in self:
del self[key]
else:
try:
self.popitem(last=False)
except KeyError:
pass
if set_time is None:
set_time = time.time()
OrderedDict.__setitem__(self, key, (value, set_time))
Expand Down
11 changes: 11 additions & 0 deletions tests/expiringdict_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,14 @@ def test_not_implemented():
assert_raises(NotImplementedError, d.viewitems)
assert_raises(NotImplementedError, d.viewkeys)
assert_raises(NotImplementedError, d.viewvalues)


def test_reset_of_key_no_trim():
"""Re-setting an existing key should not cause a non-expired key to be dropped"""
d = ExpiringDict(max_len=2, max_age_seconds=10)
d["a"] = "A"
d["b"] = "B"

d["b"] = "B"

assert "a" in d

0 comments on commit 34bbc4e

Please sign in to comment.