setdefault was acquiring write_lock, then calling getitem and also
potentially setitem, which also both try to acquire the write lock.
2.56e-3. :issue:`912`, :pr:`922`
- Int and float literals can be written with the '_' separator for
legibility, like 12_345. :pr:`923`
+- Fix a bug causing deadlocks in ``LRUCache.setdefault``. :pr:`1000`
Version 2.10.2
"""Set `default` if the key is not in the cache otherwise
leave unchanged. Return the value of this key.
"""
- self._wlock.acquire()
try:
- try:
- return self[key]
- except KeyError:
- self[key] = default
- return default
- finally:
- self._wlock.release()
+ return self[key]
+ except KeyError:
+ self[key] = default
+ return default
def clear(self):
"""Clear the cache."""
try:
self._remove(key)
except ValueError:
- # __getitem__ is not locked, it might happen
pass
finally:
self._wlock.release()
__iter__ = iterkeys
def __reversed__(self):
- """Iterate over the values in the cache dict, oldest items
+ """Iterate over the keys in the cache dict, oldest items
coming first.
"""
return iter(tuple(self._queue))