cachetools 7.3.2__tar.gz → 7.3.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. {cachetools-7.3.2 → cachetools-7.3.3}/CHANGELOG.rst +12 -0
  2. {cachetools-7.3.2/src/cachetools.egg-info → cachetools-7.3.3}/PKG-INFO +1 -1
  3. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools/__init__.py +26 -18
  4. {cachetools-7.3.2 → cachetools-7.3.3/src/cachetools.egg-info}/PKG-INFO +1 -1
  5. {cachetools-7.3.2 → cachetools-7.3.3}/tests/__init__.py +12 -0
  6. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_tlru.py +43 -0
  7. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_ttl.py +39 -0
  8. {cachetools-7.3.2 → cachetools-7.3.3}/LICENSE +0 -0
  9. {cachetools-7.3.2 → cachetools-7.3.3}/MANIFEST.in +0 -0
  10. {cachetools-7.3.2 → cachetools-7.3.3}/README.rst +0 -0
  11. {cachetools-7.3.2 → cachetools-7.3.3}/docs/conf.py +0 -0
  12. {cachetools-7.3.2 → cachetools-7.3.3}/docs/index.rst +0 -0
  13. {cachetools-7.3.2 → cachetools-7.3.3}/pyproject.toml +0 -0
  14. {cachetools-7.3.2 → cachetools-7.3.3}/setup.cfg +0 -0
  15. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools/__init__.pyi +0 -0
  16. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools/_cached.py +0 -0
  17. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools/_cachedmethod.py +0 -0
  18. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools/_descriptor.py +0 -0
  19. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools/func.py +0 -0
  20. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools/func.pyi +0 -0
  21. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools/keys.py +0 -0
  22. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools/keys.pyi +0 -0
  23. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools/py.typed +0 -0
  24. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools.egg-info/SOURCES.txt +0 -0
  25. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools.egg-info/dependency_links.txt +0 -0
  26. {cachetools-7.3.2 → cachetools-7.3.3}/src/cachetools.egg-info/top_level.txt +0 -0
  27. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_cache.py +0 -0
  28. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_cached.py +0 -0
  29. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_cachedmethod.py +0 -0
  30. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_classmethod.py +0 -0
  31. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_fifo.py +0 -0
  32. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_func.py +0 -0
  33. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_keys.py +0 -0
  34. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_lfu.py +0 -0
  35. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_lru.py +0 -0
  36. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_rr.py +0 -0
  37. {cachetools-7.3.2 → cachetools-7.3.3}/tests/test_threading.py +0 -0
  38. {cachetools-7.3.2 → cachetools-7.3.3}/tox.ini +0 -0
@@ -1,3 +1,15 @@
1
+ v7.3.3 (2026-11-18)
2
+ ===================
3
+
4
+ - Fix deep-copying ``TTLCache`` and ``TLRUCache`` instances whose
5
+ timer defines ``__deepcopy__``.
6
+
7
+ - Fix updating ``TTLCache`` and ``TLRUCache`` items while iterating,
8
+ which skipped or repeated keys.
9
+
10
+ - Minor ``LRUCache`` performance improvements.
11
+
12
+
1
13
  v7.3.2 (2026-11-14)
2
14
  ===================
3
15
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cachetools
3
- Version: 7.3.2
3
+ Version: 7.3.3
4
4
  Summary: Extensible memoizing collections and decorators
5
5
  Author-email: Thomas Kemmer <tkemmer@computer.org>
6
6
  Maintainer-email: Thomas Kemmer <tkemmer@computer.org>
@@ -12,7 +12,7 @@ __all__ = (
12
12
  "cachedmethod",
13
13
  )
14
14
 
15
- __version__ = "7.3.2"
15
+ __version__ = "7.3.3"
16
16
 
17
17
  import collections
18
18
  import collections.abc
@@ -351,7 +351,11 @@ class LRUCache(Cache):
351
351
 
352
352
  def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
353
353
  cache_setitem(self, key, value)
354
- self.__touch(key)
354
+ order = self.__order
355
+ if key in order:
356
+ order.move_to_end(key)
357
+ else:
358
+ order[key] = None
355
359
 
356
360
  def __delitem__(self, key, cache_delitem=Cache.__delitem__):
357
361
  cache_delitem(self, key)
@@ -377,13 +381,6 @@ class LRUCache(Cache):
377
381
  Cache.clear(self)
378
382
  self.__order.clear()
379
383
 
380
- def __touch(self, key):
381
- """Mark as recently used."""
382
- try:
383
- self.__order.move_to_end(key)
384
- except KeyError:
385
- self.__order[key] = None
386
-
387
384
 
388
385
  class RRCache(Cache):
389
386
  """Random Replacement (RR) cache implementation."""
@@ -464,6 +461,12 @@ class _TimedCache(Cache):
464
461
  def __copy__(self):
465
462
  return _TimedCache._Timer(self.__timer)
466
463
 
464
+ def __deepcopy__(self, memo):
465
+ # otherwise __getattr__ forwards to the timer's own __deepcopy__
466
+ import copy
467
+
468
+ return _TimedCache._Timer(copy.deepcopy(self.__timer, memo))
469
+
467
470
  def __reduce__(self):
468
471
  return _TimedCache._Timer, (self.__timer,)
469
472
 
@@ -596,14 +599,15 @@ class TTLCache(_TimedCache):
596
599
  raise KeyError(key)
597
600
 
598
601
  def __iter__(self):
599
- root = self.__root
600
- curr = root.next
601
- while curr is not root:
602
+ # Iterate over a snapshot of keys, in insertion order: Note that
603
+ # changes/updates while iterating do not raise RuntimeError, since
604
+ # items may also expire, so this seems to be the easier way.
605
+ for key in list(Cache.__iter__(self)):
602
606
  # "freeze" time for iterator access
603
607
  with self.timer as time:
604
- if time < curr.expires:
605
- yield curr.key
606
- curr = curr.next
608
+ link = self.__links.get(key)
609
+ if link is not None and time < link.expires:
610
+ yield key
607
611
 
608
612
  def __copy__(self):
609
613
  copy = super().__copy__()
@@ -752,11 +756,15 @@ class TLRUCache(_TimedCache):
752
756
  raise KeyError(key)
753
757
 
754
758
  def __iter__(self):
755
- for curr in self.__order:
759
+ # Iterate over a snapshot of keys, in insertion order: Note that
760
+ # changes/updates while iterating do not raise RuntimeError, since
761
+ # items may also expire, so this seems to be the easier way.
762
+ for key in list(Cache.__iter__(self)):
756
763
  # "freeze" time for iterator access
757
764
  with self.timer as time:
758
- if time < curr.expires and not curr.removed:
759
- yield curr.key
765
+ item = self.__items.get(key)
766
+ if item is not None and time < item.expires:
767
+ yield key
760
768
 
761
769
  def __copy__(self):
762
770
  copy = super().__copy__()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cachetools
3
- Version: 7.3.2
3
+ Version: 7.3.3
4
4
  Summary: Extensible memoizing collections and decorators
5
5
  Author-email: Thomas Kemmer <tkemmer@computer.org>
6
6
  Maintainer-email: Thomas Kemmer <tkemmer@computer.org>
@@ -457,6 +457,18 @@ class CacheTestMixin(SimplifiedTestCaseProtocol):
457
457
  self.assertEqual(1, len(cache))
458
458
  self.assertEqual(4, cache.currsize)
459
459
 
460
+ def test_iter_update(self):
461
+ cache = self.Cache(maxsize=3)
462
+ cache.update({1: 1, 2: 2, 3: 3})
463
+
464
+ # updating items while iterating visits each key once, in order
465
+ keys = []
466
+ for key in cache:
467
+ keys.append(key)
468
+ cache[key] = key * 10
469
+ self.assertEqual([1, 2, 3], keys)
470
+ self.assertEqual([10, 20, 30], list(cache.values()))
471
+
460
472
  def test_pickle(self):
461
473
  import pickle
462
474
 
@@ -447,3 +447,46 @@ class TLRUCacheTest(unittest.TestCase, CacheTestMixin):
447
447
  source.timer.tick()
448
448
  self.assertEqual([], list(source))
449
449
  self.assertEqual([4], list(cache))
450
+
451
+ def test_tlru_deepcopy_timer(self):
452
+ import copy
453
+
454
+ class DeepCopyTimer(Timer):
455
+ def __deepcopy__(self, memo):
456
+ return copy.copy(self)
457
+
458
+ source = TLRUCache[int, int, int](
459
+ maxsize=2, ttu=lambda _, v, t: t + v, timer=DeepCopyTimer()
460
+ )
461
+ source[1] = 1
462
+
463
+ # a timer may define __deepcopy__ itself
464
+ cache = copy.deepcopy(source)
465
+ self.assertEqual([1], list(cache))
466
+
467
+ # the copy has a timer of its own
468
+ cache.timer.tick()
469
+ self.assertEqual([], list(cache))
470
+ self.assertEqual([1], list(source))
471
+
472
+ def test_tlru_iter_modify(self):
473
+ cache = TLRUCache[int, int, int](
474
+ maxsize=6, ttu=lambda _, v, t: t + 2, timer=Timer()
475
+ )
476
+ cache.update({1: 1, 2: 2, 3: 3})
477
+
478
+ # items inserted while iterating are not visited
479
+ keys = []
480
+ for key in cache:
481
+ keys.append(key)
482
+ cache[key + 10] = key
483
+ self.assertEqual([1, 2, 3], keys)
484
+ self.assertEqual([1, 2, 3, 11, 12, 13], list(cache))
485
+
486
+ # items removed while iterating are skipped
487
+ keys = []
488
+ for key in cache:
489
+ keys.append(key)
490
+ cache.clear()
491
+ self.assertEqual([1], keys)
492
+ self.assertEqual([], list(cache))
@@ -317,3 +317,42 @@ class TTLCacheTest(unittest.TestCase, CacheTestMixin):
317
317
  # items expire in the copy as in its source
318
318
  self.assertEqual([3, 4], list(cache))
319
319
  self.assertEqual([3], list(source))
320
+
321
+ def test_ttl_deepcopy_timer(self):
322
+ import copy
323
+
324
+ class DeepCopyTimer(Timer):
325
+ def __deepcopy__(self, memo):
326
+ return copy.copy(self)
327
+
328
+ source = TTLCache[int, int, int](maxsize=2, ttl=1, timer=DeepCopyTimer())
329
+ source[1] = 1
330
+
331
+ # a timer may define __deepcopy__ itself
332
+ cache = copy.deepcopy(source)
333
+ self.assertEqual([1], list(cache))
334
+
335
+ # the copy has a timer of its own
336
+ cache.timer.tick()
337
+ self.assertEqual([], list(cache))
338
+ self.assertEqual([1], list(source))
339
+
340
+ def test_ttl_iter_modify(self):
341
+ cache = TTLCache[int, int, int](maxsize=6, ttl=2, timer=Timer())
342
+ cache.update({1: 1, 2: 2, 3: 3})
343
+
344
+ # items inserted while iterating are not visited
345
+ keys = []
346
+ for key in cache:
347
+ keys.append(key)
348
+ cache[key + 10] = key
349
+ self.assertEqual([1, 2, 3], keys)
350
+ self.assertEqual([1, 2, 3, 11, 12, 13], list(cache))
351
+
352
+ # items removed while iterating are skipped
353
+ keys = []
354
+ for key in cache:
355
+ keys.append(key)
356
+ cache.clear()
357
+ self.assertEqual([1], keys)
358
+ self.assertEqual([], list(cache))
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes