tkem/cachetools

Improve popitem() exception context handling

Closed this issue · 0 comments

tkem commented

As pointed out by @cool-RR in #171, exceptions raised from popitem() are shown with more context information than expected:

>>> import cachetools
>>> d = cachetools.LRUCache(10)
>>> d.popitem()
Traceback (most recent call last):
  File "/home/tkem/src/cachetools/cachetools/lru.py", line 29, in popitem
    key = next(iter(self.__order))
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tkem/src/cachetools/cachetools/lru.py", line 31, in popitem
    raise KeyError('%s is empty' % self.__class__.__name__)
KeyError: 'LRUCache is empty'

In this case, the StopIteration exception is an implementation details of LRUCache.popitem() and should not be displayed. The same issue applies to the other cache types as well. IMHO, only the KeyError should be displayed, which is part of the MutableMapping.popitem() API.