Fix RecentlyUsedContainer.__setitem__() when maxsize=0
MODIFY
src/urllib3/_collections.py
src/urllib3/_collections.py
@@ -139,16 +139,17 @@ class RecentlyUsedContainer(Generic[_KT, _VT], MutableMapping[_KT, _VT]):
# size of the pool. Because accessing a key should move it to
# the end of the eviction line, we pop it out first.
evicted_item = key, self._container.pop(key)
+ self._container[key] = value
except KeyError:
- if len(self._container) >= self._maxsize:
+ # When the key does not exist, we insert the value first so that
+ # evicting works in all cases, including when self._maxsize is 0
+ self._container[key] = value
+ if len(self._container) > self._maxsize:
# If we didn't evict an existing value, and we've hit our maximum
# size, then we have to evict the least recently used item from
# the beginning of the container.
evicted_item = self._container.popitem(last=False)
- # Finally, insert the new value.
- self._container[key] = value
-
# After releasing the lock on the pool, dispose of any evicted value.
if evicted_item is not None and self.dispose_func:
_, evicted_value = evicted_item

MODIFY
test/test_collections.py
test/test_collections.py
@@ -22,6 +22,11 @@ class TestLRUContainer:
assert 0 not in d
assert (i + 1) in d
+ def test_maxsize_0(self):
+ d = Container(0)
+ d[1] = 1
+ assert len(d) == 0
+
def test_expire(self):
d = Container(5)

Fix RecentlyUsedContainer.__setitem__() when maxsize=0

Co-authored-by: Seth Michael Larson <sethmichaellarson@gmail.com>