cachetools 7.3.1__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.1 → cachetools-7.3.3}/CHANGELOG.rst +19 -0
  2. {cachetools-7.3.1 → cachetools-7.3.3}/MANIFEST.in +1 -0
  3. {cachetools-7.3.1/src/cachetools.egg-info → cachetools-7.3.3}/PKG-INFO +1 -1
  4. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools/__init__.py +109 -39
  5. {cachetools-7.3.1 → cachetools-7.3.3/src/cachetools.egg-info}/PKG-INFO +1 -1
  6. {cachetools-7.3.1 → cachetools-7.3.3}/tests/__init__.py +67 -0
  7. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_fifo.py +4 -0
  8. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_lru.py +4 -0
  9. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_rr.py +15 -0
  10. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_tlru.py +70 -0
  11. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_ttl.py +69 -0
  12. {cachetools-7.3.1 → cachetools-7.3.3}/LICENSE +0 -0
  13. {cachetools-7.3.1 → cachetools-7.3.3}/README.rst +0 -0
  14. {cachetools-7.3.1 → cachetools-7.3.3}/docs/conf.py +0 -0
  15. {cachetools-7.3.1 → cachetools-7.3.3}/docs/index.rst +0 -0
  16. {cachetools-7.3.1 → cachetools-7.3.3}/pyproject.toml +0 -0
  17. {cachetools-7.3.1 → cachetools-7.3.3}/setup.cfg +0 -0
  18. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools/__init__.pyi +0 -0
  19. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools/_cached.py +0 -0
  20. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools/_cachedmethod.py +0 -0
  21. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools/_descriptor.py +0 -0
  22. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools/func.py +0 -0
  23. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools/func.pyi +0 -0
  24. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools/keys.py +0 -0
  25. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools/keys.pyi +0 -0
  26. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools/py.typed +0 -0
  27. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools.egg-info/SOURCES.txt +0 -0
  28. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools.egg-info/dependency_links.txt +0 -0
  29. {cachetools-7.3.1 → cachetools-7.3.3}/src/cachetools.egg-info/top_level.txt +0 -0
  30. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_cache.py +0 -0
  31. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_cached.py +0 -0
  32. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_cachedmethod.py +0 -0
  33. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_classmethod.py +0 -0
  34. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_func.py +0 -0
  35. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_keys.py +0 -0
  36. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_lfu.py +0 -0
  37. {cachetools-7.3.1 → cachetools-7.3.3}/tests/test_threading.py +0 -0
  38. {cachetools-7.3.1 → cachetools-7.3.3}/tox.ini +0 -0
@@ -1,3 +1,22 @@
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
+
13
+ v7.3.2 (2026-11-14)
14
+ ===================
15
+
16
+ - Properly support shallow copies of caches, including subclasses
17
+ with additional attributes (broken since v7.3.1).
18
+
19
+
1
20
  v7.3.1 (2026-11-12)
2
21
  ===================
3
22
 
@@ -1,3 +1,4 @@
1
1
  prune .github/
2
2
  exclude .gitignore
3
3
  exclude .readthedocs.yaml
4
+ exclude AGENTS.md
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cachetools
3
- Version: 7.3.1
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.1"
15
+ __version__ = "7.3.3"
16
16
 
17
17
  import collections
18
18
  import collections.abc
@@ -112,11 +112,14 @@ class Cache(collections.abc.MutableMapping):
112
112
  def __len__(self):
113
113
  return len(self.__data)
114
114
 
115
- def __copy__(self, *shared):
116
- import copy
117
-
118
- shared = (self.getsizeof, *shared, *self.__data, *self.__data.values())
119
- return copy.deepcopy(self, {id(obj): obj for obj in shared})
115
+ def __copy__(self):
116
+ cls = type(self)
117
+ copy = cls.__new__(cls)
118
+ copy.__dict__.update(self.__dict__)
119
+ copy.__data = self.__data.copy()
120
+ if self.__size is not None:
121
+ copy.__size = self.__size.copy()
122
+ return copy
120
123
 
121
124
  # Note that we cannot simply inherit get(), pop() and setdefault()
122
125
  # from MutableMapping, since these rely on __getitem__ throwing a
@@ -194,6 +197,11 @@ class FIFOCache(Cache):
194
197
  cache_delitem(self, key)
195
198
  del self.__order[key]
196
199
 
200
+ def __copy__(self):
201
+ copy = super().__copy__()
202
+ copy.__order = self.__order.copy()
203
+ return copy
204
+
197
205
  def popitem(self):
198
206
  """Remove and return the `(key, value)` pair first inserted."""
199
207
  try:
@@ -223,6 +231,11 @@ class LFUCache(Cache):
223
231
  def __reduce__(self):
224
232
  return LFUCache._Link, (self.count,), (None, {"keys": self.keys})
225
233
 
234
+ def insert(self, next):
235
+ self.next = next
236
+ self.prev = prev = next.prev
237
+ prev.next = next.prev = self
238
+
226
239
  def unlink(self):
227
240
  next = self.next
228
241
  prev = self.prev
@@ -263,15 +276,27 @@ class LFUCache(Cache):
263
276
  if not link.keys:
264
277
  link.unlink()
265
278
 
279
+ def __copy__(self):
280
+ copy = super().__copy__()
281
+ copy.__root = root = LFUCache._Link(0)
282
+ root.prev = root.next = root
283
+ copy.__links = links = {}
284
+ curr = self.__root.next
285
+ while curr is not self.__root:
286
+ link = LFUCache._Link(curr.count)
287
+ link.keys = curr.keys.copy()
288
+ link.insert(root)
289
+ links.update(dict.fromkeys(link.keys, link))
290
+ curr = curr.next
291
+ return copy
292
+
266
293
  def __setstate__(self, state):
267
294
  self.__dict__.update(state)
268
295
  root = self.__root
269
296
  root.prev = root.next = root
270
297
  links = set(self.__links.values())
271
298
  for link in sorted(links, key=lambda obj: obj.count):
272
- link.next = root
273
- link.prev = prev = root.prev
274
- prev.next = root.prev = link
299
+ link.insert(root)
275
300
 
276
301
  def popitem(self):
277
302
  """Remove and return the `(key, value)` pair least frequently used."""
@@ -326,12 +351,21 @@ class LRUCache(Cache):
326
351
 
327
352
  def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
328
353
  cache_setitem(self, key, value)
329
- self.__touch(key)
354
+ order = self.__order
355
+ if key in order:
356
+ order.move_to_end(key)
357
+ else:
358
+ order[key] = None
330
359
 
331
360
  def __delitem__(self, key, cache_delitem=Cache.__delitem__):
332
361
  cache_delitem(self, key)
333
362
  del self.__order[key]
334
363
 
364
+ def __copy__(self):
365
+ copy = super().__copy__()
366
+ copy.__order = self.__order.copy()
367
+ return copy
368
+
335
369
  def popitem(self):
336
370
  """Remove and return the `(key, value)` pair least recently used."""
337
371
  try:
@@ -347,13 +381,6 @@ class LRUCache(Cache):
347
381
  Cache.clear(self)
348
382
  self.__order.clear()
349
383
 
350
- def __touch(self, key):
351
- """Mark as recently used."""
352
- try:
353
- self.__order.move_to_end(key)
354
- except KeyError:
355
- self.__order[key] = None
356
-
357
384
 
358
385
  class RRCache(Cache):
359
386
  """Random Replacement (RR) cache implementation."""
@@ -364,8 +391,11 @@ class RRCache(Cache):
364
391
  self.__index = {}
365
392
  self.__keys = []
366
393
 
367
- def __copy__(self, *shared):
368
- return Cache.__copy__(self, self.__choice, *shared)
394
+ def __copy__(self):
395
+ copy = super().__copy__()
396
+ copy.__index = self.__index.copy()
397
+ copy.__keys = self.__keys.copy()
398
+ return copy
369
399
 
370
400
  @property
371
401
  def choice(self):
@@ -428,6 +458,15 @@ class _TimedCache(Cache):
428
458
  def __exit__(self, *exc):
429
459
  self.__nesting -= 1
430
460
 
461
+ def __copy__(self):
462
+ return _TimedCache._Timer(self.__timer)
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
+
431
470
  def __reduce__(self):
432
471
  return _TimedCache._Timer, (self.__timer,)
433
472
 
@@ -438,9 +477,10 @@ class _TimedCache(Cache):
438
477
  Cache.__init__(self, maxsize, getsizeof)
439
478
  self.__timer = _TimedCache._Timer(timer)
440
479
 
441
- def __copy__(self, *shared):
442
- # share the timer function, which is all a _Timer reduces to
443
- return Cache.__copy__(self, *self.__timer.__reduce__()[1], *shared)
480
+ def __copy__(self):
481
+ copy = super().__copy__()
482
+ copy.__timer = self.__timer.__copy__()
483
+ return copy
444
484
 
445
485
  def __repr__(self, cache_repr=Cache.__repr__):
446
486
  with self.__timer as time:
@@ -498,6 +538,11 @@ class TTLCache(_TimedCache):
498
538
  def __reduce__(self):
499
539
  return TTLCache._Link, (self.key, self.expires)
500
540
 
541
+ def insert(self, next):
542
+ self.next = next
543
+ self.prev = prev = next.prev
544
+ prev.next = next.prev = self
545
+
501
546
  def unlink(self):
502
547
  next = self.next
503
548
  prev = self.prev
@@ -544,9 +589,7 @@ class TTLCache(_TimedCache):
544
589
  links.move_to_end(key)
545
590
  link.unlink()
546
591
  link.expires = expires
547
- link.next = root = self.__root
548
- link.prev = prev = root.prev
549
- prev.next = root.prev = link
592
+ link.insert(self.__root)
550
593
 
551
594
  def __delitem__(self, key, cache_delitem=Cache.__delitem__):
552
595
  cache_delitem(self, key)
@@ -556,23 +599,37 @@ class TTLCache(_TimedCache):
556
599
  raise KeyError(key)
557
600
 
558
601
  def __iter__(self):
559
- root = self.__root
560
- curr = root.next
561
- 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)):
562
606
  # "freeze" time for iterator access
563
607
  with self.timer as time:
564
- if time < curr.expires:
565
- yield curr.key
608
+ link = self.__links.get(key)
609
+ if link is not None and time < link.expires:
610
+ yield key
611
+
612
+ def __copy__(self):
613
+ copy = super().__copy__()
614
+ copy.__root = root = TTLCache._Link()
615
+ root.prev = root.next = root
616
+ links = {}
617
+ curr = self.__root.next
618
+ while curr is not self.__root:
619
+ links[curr.key] = link = TTLCache._Link(curr.key, curr.expires)
620
+ link.insert(root)
566
621
  curr = curr.next
622
+ copy.__links = collections.OrderedDict(
623
+ (key, links[key]) for key in self.__links
624
+ )
625
+ return copy
567
626
 
568
627
  def __setstate__(self, state):
569
628
  self.__dict__.update(state)
570
629
  root = self.__root
571
630
  root.prev = root.next = root
572
631
  for link in sorted(self.__links.values(), key=lambda obj: obj.expires):
573
- link.next = root
574
- link.prev = prev = root.prev
575
- prev.next = root.prev = link
632
+ link.insert(root)
576
633
  self.expire(self.timer())
577
634
 
578
635
  @property
@@ -699,14 +756,27 @@ class TLRUCache(_TimedCache):
699
756
  raise KeyError(key)
700
757
 
701
758
  def __iter__(self):
702
- 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)):
703
763
  # "freeze" time for iterator access
704
764
  with self.timer as time:
705
- if time < curr.expires and not curr.removed:
706
- yield curr.key
707
-
708
- def __copy__(self, *shared):
709
- return _TimedCache.__copy__(self, self.__ttu, *shared)
765
+ item = self.__items.get(key)
766
+ if item is not None and time < item.expires:
767
+ yield key
768
+
769
+ def __copy__(self):
770
+ copy = super().__copy__()
771
+ items = {}
772
+ for item in self.__order:
773
+ items[item] = new = TLRUCache._Item(item.key, item.expires)
774
+ new.removed = item.removed
775
+ copy.__order = [items[item] for item in self.__order]
776
+ copy.__items = collections.OrderedDict(
777
+ (key, items[item]) for key, item in self.__items.items()
778
+ )
779
+ return copy
710
780
 
711
781
  @property
712
782
  def ttu(self):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cachetools
3
- Version: 7.3.1
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
 
@@ -541,6 +553,61 @@ class CacheTestMixin(SimplifiedTestCaseProtocol):
541
553
  self.assertEqual(n, len(cache))
542
554
  self.assertEqual(source, cache)
543
555
 
556
+ def test_copy_attributes(self):
557
+ import copy
558
+ import threading
559
+
560
+ class Cache(self.Cache):
561
+ def __init__(self, maxsize, **kwargs):
562
+ super().__init__(maxsize, **kwargs)
563
+ self.lock = threading.Lock() # cannot be deep-copied
564
+ self.stats = {"hits": 0}
565
+
566
+ source = Cache(maxsize=2)
567
+ source.update({1: [1], 2: [2]})
568
+
569
+ cache = copy.copy(source)
570
+ self.assertIs(type(source), type(cache))
571
+ # a shallow copy shares attributes and values with its source
572
+ self.assertIs(source.lock, cache.lock)
573
+ self.assertIs(source.stats, cache.stats)
574
+ self.assertIs(source[1], cache[1])
575
+ self.assertIs(source[2], cache[2])
576
+
577
+ # but none of the cache's own state
578
+ cache[3] = [3]
579
+ del cache[next(iter(cache))]
580
+ self.assertEqual(1, len(cache))
581
+ self.assertEqual(2, len(source))
582
+ self.assertEqual(2, source.currsize)
583
+ self.assertIn(1, source)
584
+ self.assertIn(2, source)
585
+ self.assertNotIn(3, source)
586
+
587
+ def test_copy_getsizeof(self):
588
+ import copy
589
+
590
+ class GetSizeOf:
591
+ def __call__(self, value):
592
+ return value
593
+
594
+ source = self.Cache(maxsize=3, getsizeof=GetSizeOf())
595
+ source.update({1: 1, 2: 2})
596
+
597
+ cache = copy.copy(source)
598
+ self.assertIs(source.getsizeof, cache.getsizeof)
599
+ self.assertEqual(3, cache.currsize)
600
+
601
+ # item sizes are kept separately for source and copy
602
+ cache[2] = 1
603
+ self.assertEqual(2, cache.currsize)
604
+ self.assertEqual(3, source.currsize)
605
+ del source[2]
606
+ self.assertEqual(1, source.currsize)
607
+ self.assertEqual(2, cache.currsize)
608
+ del cache[2]
609
+ self.assertEqual(1, cache.currsize)
610
+
544
611
  def test_deepcopy(self):
545
612
  import copy
546
613
 
@@ -9,6 +9,8 @@ class FIFOCacheTest(unittest.TestCase, CacheTestMixin):
9
9
  Cache = FIFOCache
10
10
 
11
11
  def test_fifo(self):
12
+ import copy
13
+
12
14
  cache = FIFOCache[int, int](maxsize=2)
13
15
 
14
16
  cache[1] = 1
@@ -21,6 +23,8 @@ class FIFOCacheTest(unittest.TestCase, CacheTestMixin):
21
23
  self.assertNotIn(1, cache)
22
24
 
23
25
  cache[2]
26
+ # a shallow copy evicts in the same order, on its own
27
+ self.assertEqual((2, 2), copy.copy(cache).popitem())
24
28
  cache[4] = 4
25
29
  self.assertEqual(len(cache), 2)
26
30
  self.assertEqual(cache[3], 3)
@@ -9,6 +9,8 @@ class LRUCacheTest(unittest.TestCase, CacheTestMixin):
9
9
  Cache = LRUCache
10
10
 
11
11
  def test_lru(self):
12
+ import copy
13
+
12
14
  cache = LRUCache[int, int](maxsize=2)
13
15
 
14
16
  cache[1] = 1
@@ -21,6 +23,8 @@ class LRUCacheTest(unittest.TestCase, CacheTestMixin):
21
23
  self.assertNotIn(1, cache)
22
24
 
23
25
  cache[2]
26
+ # a shallow copy evicts in the same order, on its own
27
+ self.assertEqual((3, 3), copy.copy(cache).popitem())
24
28
  cache[4] = 4
25
29
  self.assertEqual(len(cache), 2)
26
30
  self.assertEqual(cache[2], 2)
@@ -101,3 +101,18 @@ class RRCacheTest(unittest.TestCase, CacheTestMixin):
101
101
 
102
102
  with self.assertRaises(KeyError):
103
103
  cache.popitem()
104
+
105
+ def test_rr_copy(self):
106
+ import copy
107
+
108
+ source = RRCache[int, int](maxsize=3, choice=min)
109
+ source.update({1: 1, 2: 2, 3: 3})
110
+
111
+ # the copy has its own keys to choose from
112
+ cache = copy.copy(source)
113
+ del cache[1]
114
+ self.assertEqual((2, 2), cache.popitem())
115
+ self.assertEqual((1, 1), source.popitem())
116
+ self.assertEqual((2, 2), source.popitem())
117
+ self.assertEqual((3, 3), source.popitem())
118
+ self.assertEqual((3, 3), cache.popitem())
@@ -420,3 +420,73 @@ class TLRUCacheTest(unittest.TestCase, CacheTestMixin):
420
420
  sized["abc"] = "abc"
421
421
  self.assertEqual("abc", sized["abc"])
422
422
  self.assertEqual(3, sized.currsize)
423
+
424
+ def test_tlru_copy(self):
425
+ import copy
426
+
427
+ source = TLRUCache[int, int, int](
428
+ maxsize=3, ttu=lambda _key, value, time: time + value, timer=Timer()
429
+ )
430
+ source[1] = 3
431
+ source[2] = 1
432
+ source[3] = 2
433
+ source[1] = 2 # leaves a removed item behind, least recently used is 2
434
+
435
+ cache = copy.copy(source)
436
+ self.assertEqual(list(source), list(cache))
437
+
438
+ # the copy evicts as its source would, on its own
439
+ cache[4] = 5
440
+ self.assertEqual([1, 3, 4], sorted(cache))
441
+ self.assertEqual([1, 2, 3], sorted(source))
442
+
443
+ # items expire in the copy as in its source
444
+ cache.timer.tick()
445
+ self.assertEqual([1, 3], sorted(source))
446
+ self.assertEqual([1, 3, 4], sorted(cache))
447
+ source.timer.tick()
448
+ self.assertEqual([], list(source))
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))
@@ -287,3 +287,72 @@ class TTLCacheTest(unittest.TestCase, CacheTestMixin):
287
287
  sized["abc"] = "abc"
288
288
  self.assertEqual("abc", sized["abc"])
289
289
  self.assertEqual(3, sized.currsize)
290
+
291
+ def test_ttl_copy(self):
292
+ import copy
293
+
294
+ source = TTLCache[int, int, int](maxsize=3, ttl=2, timer=Timer())
295
+ source[1] = 1
296
+ source[2] = 2
297
+ source.timer.tick()
298
+ source[3] = 3
299
+ self.assertEqual(1, source[1]) # least recently used is now 2
300
+
301
+ cache = copy.copy(source)
302
+ self.assertEqual([1, 2, 3], list(cache))
303
+
304
+ # the copy evicts as its source would, on its own
305
+ cache[4] = 4
306
+ self.assertEqual([1, 3, 4], list(cache))
307
+ self.assertEqual([1, 2, 3], list(source))
308
+
309
+ # the timer is shared, but not the caches' use of it
310
+ self.assertIsNot(source.timer, cache.timer)
311
+ with source.timer:
312
+ cache.timer.tick()
313
+ self.assertEqual(1, source.timer())
314
+ self.assertEqual(2, cache.timer())
315
+ self.assertEqual(2, source.timer())
316
+
317
+ # items expire in the copy as in its source
318
+ self.assertEqual([3, 4], list(cache))
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