cachetools 7.3.0__tar.gz → 7.3.2__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.
- {cachetools-7.3.0 → cachetools-7.3.2}/CHANGELOG.rst +19 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/MANIFEST.in +1 -0
- {cachetools-7.3.0/src/cachetools.egg-info → cachetools-7.3.2}/PKG-INFO +1 -1
- {cachetools-7.3.0 → cachetools-7.3.2}/docs/index.rst +10 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools/__init__.py +98 -7
- {cachetools-7.3.0 → cachetools-7.3.2/src/cachetools.egg-info}/PKG-INFO +1 -1
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/__init__.py +148 -4
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_fifo.py +4 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_lfu.py +28 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_lru.py +4 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_rr.py +15 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_tlru.py +27 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_ttl.py +30 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/LICENSE +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/README.rst +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/docs/conf.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/pyproject.toml +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/setup.cfg +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools/__init__.pyi +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools/_cached.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools/_cachedmethod.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools/_descriptor.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools/func.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools/func.pyi +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools/keys.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools/keys.pyi +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools/py.typed +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools.egg-info/SOURCES.txt +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools.egg-info/dependency_links.txt +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/src/cachetools.egg-info/top_level.txt +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_cache.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_cached.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_cachedmethod.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_classmethod.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_func.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_keys.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tests/test_threading.py +0 -0
- {cachetools-7.3.0 → cachetools-7.3.2}/tox.ini +0 -0
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
v7.3.2 (2026-11-14)
|
|
2
|
+
===================
|
|
3
|
+
|
|
4
|
+
- Properly support shallow copies of caches, including subclasses
|
|
5
|
+
with additional attributes (broken since v7.3.1).
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
v7.3.1 (2026-11-12)
|
|
9
|
+
===================
|
|
10
|
+
|
|
11
|
+
- Fix deep-copying and pickling ``LFUCache`` instances with many
|
|
12
|
+
different use counts (broken since v6.1.0).
|
|
13
|
+
|
|
14
|
+
- Fix shallow copies of caches sharing internal state with the
|
|
15
|
+
original.
|
|
16
|
+
|
|
17
|
+
- Minor documentation improvements.
|
|
18
|
+
|
|
19
|
+
|
|
1
20
|
v7.3.0 (2026-11-05)
|
|
2
21
|
===================
|
|
3
22
|
|
|
@@ -631,6 +631,16 @@ often called with the same arguments:
|
|
|
631
631
|
e.g. :func:`cachetools.keys.hashkey`, if you need `self` to be part
|
|
632
632
|
of the cache key.
|
|
633
633
|
|
|
634
|
+
.. note::
|
|
635
|
+
|
|
636
|
+
Once a decorated method has been accessed on an instance, a
|
|
637
|
+
*shallow* copy of that instance, e.g. one made with
|
|
638
|
+
:func:`copy.copy`, shares the method with the original, and it
|
|
639
|
+
stays bound to the instance that accessed it first. To avoid
|
|
640
|
+
this, make shallow copies before any decorated method is
|
|
641
|
+
accessed, use :func:`copy.deepcopy`, or define a
|
|
642
|
+
:meth:`__copy__` method that creates the copy from scratch.
|
|
643
|
+
|
|
634
644
|
When using a shared cache for multiple methods, be aware that
|
|
635
645
|
different cache keys must be created for each method even when
|
|
636
646
|
function arguments are the same, just as with the :func:`cached`
|
|
@@ -12,7 +12,7 @@ __all__ = (
|
|
|
12
12
|
"cachedmethod",
|
|
13
13
|
)
|
|
14
14
|
|
|
15
|
-
__version__ = "7.3.
|
|
15
|
+
__version__ = "7.3.2"
|
|
16
16
|
|
|
17
17
|
import collections
|
|
18
18
|
import collections.abc
|
|
@@ -112,6 +112,15 @@ class Cache(collections.abc.MutableMapping):
|
|
|
112
112
|
def __len__(self):
|
|
113
113
|
return len(self.__data)
|
|
114
114
|
|
|
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
|
|
123
|
+
|
|
115
124
|
# Note that we cannot simply inherit get(), pop() and setdefault()
|
|
116
125
|
# from MutableMapping, since these rely on __getitem__ throwing a
|
|
117
126
|
# KeyError on cache miss. This is not the case if __missing__ is
|
|
@@ -188,6 +197,11 @@ class FIFOCache(Cache):
|
|
|
188
197
|
cache_delitem(self, key)
|
|
189
198
|
del self.__order[key]
|
|
190
199
|
|
|
200
|
+
def __copy__(self):
|
|
201
|
+
copy = super().__copy__()
|
|
202
|
+
copy.__order = self.__order.copy()
|
|
203
|
+
return copy
|
|
204
|
+
|
|
191
205
|
def popitem(self):
|
|
192
206
|
"""Remove and return the `(key, value)` pair first inserted."""
|
|
193
207
|
try:
|
|
@@ -214,6 +228,14 @@ class LFUCache(Cache):
|
|
|
214
228
|
self.count = count
|
|
215
229
|
self.keys = collections.OrderedDict()
|
|
216
230
|
|
|
231
|
+
def __reduce__(self):
|
|
232
|
+
return LFUCache._Link, (self.count,), (None, {"keys": self.keys})
|
|
233
|
+
|
|
234
|
+
def insert(self, next):
|
|
235
|
+
self.next = next
|
|
236
|
+
self.prev = prev = next.prev
|
|
237
|
+
prev.next = next.prev = self
|
|
238
|
+
|
|
217
239
|
def unlink(self):
|
|
218
240
|
next = self.next
|
|
219
241
|
prev = self.prev
|
|
@@ -254,6 +276,28 @@ class LFUCache(Cache):
|
|
|
254
276
|
if not link.keys:
|
|
255
277
|
link.unlink()
|
|
256
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
|
+
|
|
293
|
+
def __setstate__(self, state):
|
|
294
|
+
self.__dict__.update(state)
|
|
295
|
+
root = self.__root
|
|
296
|
+
root.prev = root.next = root
|
|
297
|
+
links = set(self.__links.values())
|
|
298
|
+
for link in sorted(links, key=lambda obj: obj.count):
|
|
299
|
+
link.insert(root)
|
|
300
|
+
|
|
257
301
|
def popitem(self):
|
|
258
302
|
"""Remove and return the `(key, value)` pair least frequently used."""
|
|
259
303
|
root = self.__root
|
|
@@ -313,6 +357,11 @@ class LRUCache(Cache):
|
|
|
313
357
|
cache_delitem(self, key)
|
|
314
358
|
del self.__order[key]
|
|
315
359
|
|
|
360
|
+
def __copy__(self):
|
|
361
|
+
copy = super().__copy__()
|
|
362
|
+
copy.__order = self.__order.copy()
|
|
363
|
+
return copy
|
|
364
|
+
|
|
316
365
|
def popitem(self):
|
|
317
366
|
"""Remove and return the `(key, value)` pair least recently used."""
|
|
318
367
|
try:
|
|
@@ -345,6 +394,12 @@ class RRCache(Cache):
|
|
|
345
394
|
self.__index = {}
|
|
346
395
|
self.__keys = []
|
|
347
396
|
|
|
397
|
+
def __copy__(self):
|
|
398
|
+
copy = super().__copy__()
|
|
399
|
+
copy.__index = self.__index.copy()
|
|
400
|
+
copy.__keys = self.__keys.copy()
|
|
401
|
+
return copy
|
|
402
|
+
|
|
348
403
|
@property
|
|
349
404
|
def choice(self):
|
|
350
405
|
"""The `choice` function used by the cache."""
|
|
@@ -406,6 +461,9 @@ class _TimedCache(Cache):
|
|
|
406
461
|
def __exit__(self, *exc):
|
|
407
462
|
self.__nesting -= 1
|
|
408
463
|
|
|
464
|
+
def __copy__(self):
|
|
465
|
+
return _TimedCache._Timer(self.__timer)
|
|
466
|
+
|
|
409
467
|
def __reduce__(self):
|
|
410
468
|
return _TimedCache._Timer, (self.__timer,)
|
|
411
469
|
|
|
@@ -416,6 +474,11 @@ class _TimedCache(Cache):
|
|
|
416
474
|
Cache.__init__(self, maxsize, getsizeof)
|
|
417
475
|
self.__timer = _TimedCache._Timer(timer)
|
|
418
476
|
|
|
477
|
+
def __copy__(self):
|
|
478
|
+
copy = super().__copy__()
|
|
479
|
+
copy.__timer = self.__timer.__copy__()
|
|
480
|
+
return copy
|
|
481
|
+
|
|
419
482
|
def __repr__(self, cache_repr=Cache.__repr__):
|
|
420
483
|
with self.__timer as time:
|
|
421
484
|
self.expire(time)
|
|
@@ -472,6 +535,11 @@ class TTLCache(_TimedCache):
|
|
|
472
535
|
def __reduce__(self):
|
|
473
536
|
return TTLCache._Link, (self.key, self.expires)
|
|
474
537
|
|
|
538
|
+
def insert(self, next):
|
|
539
|
+
self.next = next
|
|
540
|
+
self.prev = prev = next.prev
|
|
541
|
+
prev.next = next.prev = self
|
|
542
|
+
|
|
475
543
|
def unlink(self):
|
|
476
544
|
next = self.next
|
|
477
545
|
prev = self.prev
|
|
@@ -518,9 +586,7 @@ class TTLCache(_TimedCache):
|
|
|
518
586
|
links.move_to_end(key)
|
|
519
587
|
link.unlink()
|
|
520
588
|
link.expires = expires
|
|
521
|
-
link.
|
|
522
|
-
link.prev = prev = root.prev
|
|
523
|
-
prev.next = root.prev = link
|
|
589
|
+
link.insert(self.__root)
|
|
524
590
|
|
|
525
591
|
def __delitem__(self, key, cache_delitem=Cache.__delitem__):
|
|
526
592
|
cache_delitem(self, key)
|
|
@@ -539,14 +605,27 @@ class TTLCache(_TimedCache):
|
|
|
539
605
|
yield curr.key
|
|
540
606
|
curr = curr.next
|
|
541
607
|
|
|
608
|
+
def __copy__(self):
|
|
609
|
+
copy = super().__copy__()
|
|
610
|
+
copy.__root = root = TTLCache._Link()
|
|
611
|
+
root.prev = root.next = root
|
|
612
|
+
links = {}
|
|
613
|
+
curr = self.__root.next
|
|
614
|
+
while curr is not self.__root:
|
|
615
|
+
links[curr.key] = link = TTLCache._Link(curr.key, curr.expires)
|
|
616
|
+
link.insert(root)
|
|
617
|
+
curr = curr.next
|
|
618
|
+
copy.__links = collections.OrderedDict(
|
|
619
|
+
(key, links[key]) for key in self.__links
|
|
620
|
+
)
|
|
621
|
+
return copy
|
|
622
|
+
|
|
542
623
|
def __setstate__(self, state):
|
|
543
624
|
self.__dict__.update(state)
|
|
544
625
|
root = self.__root
|
|
545
626
|
root.prev = root.next = root
|
|
546
627
|
for link in sorted(self.__links.values(), key=lambda obj: obj.expires):
|
|
547
|
-
link.
|
|
548
|
-
link.prev = prev = root.prev
|
|
549
|
-
prev.next = root.prev = link
|
|
628
|
+
link.insert(root)
|
|
550
629
|
self.expire(self.timer())
|
|
551
630
|
|
|
552
631
|
@property
|
|
@@ -679,6 +758,18 @@ class TLRUCache(_TimedCache):
|
|
|
679
758
|
if time < curr.expires and not curr.removed:
|
|
680
759
|
yield curr.key
|
|
681
760
|
|
|
761
|
+
def __copy__(self):
|
|
762
|
+
copy = super().__copy__()
|
|
763
|
+
items = {}
|
|
764
|
+
for item in self.__order:
|
|
765
|
+
items[item] = new = TLRUCache._Item(item.key, item.expires)
|
|
766
|
+
new.removed = item.removed
|
|
767
|
+
copy.__order = [items[item] for item in self.__order]
|
|
768
|
+
copy.__items = collections.OrderedDict(
|
|
769
|
+
(key, items[item]) for key, item in self.__items.items()
|
|
770
|
+
)
|
|
771
|
+
return copy
|
|
772
|
+
|
|
682
773
|
@property
|
|
683
774
|
def ttu(self):
|
|
684
775
|
"""The time-to-use function used by the cache."""
|
|
@@ -464,7 +464,10 @@ class CacheTestMixin(SimplifiedTestCaseProtocol):
|
|
|
464
464
|
source.update({1: 1, 2: 2})
|
|
465
465
|
|
|
466
466
|
cache = pickle.loads(pickle.dumps(source))
|
|
467
|
+
self.assertIsNot(source, cache)
|
|
467
468
|
self.assertEqual(source, cache)
|
|
469
|
+
self.assertEqual(source.maxsize, cache.maxsize)
|
|
470
|
+
self.assertEqual(source.currsize, cache.currsize)
|
|
468
471
|
|
|
469
472
|
self.assertEqual(2, len(cache))
|
|
470
473
|
self.assertEqual(1, cache[1])
|
|
@@ -472,13 +475,15 @@ class CacheTestMixin(SimplifiedTestCaseProtocol):
|
|
|
472
475
|
|
|
473
476
|
cache[3] = 3
|
|
474
477
|
self.assertEqual(2, len(cache))
|
|
478
|
+
self.assertEqual(2, cache.currsize)
|
|
475
479
|
self.assertEqual(3, cache[3])
|
|
476
480
|
self.assertTrue(1 in cache or 2 in cache)
|
|
477
481
|
|
|
478
|
-
|
|
479
|
-
self.assertEqual(2,
|
|
480
|
-
self.
|
|
481
|
-
self.
|
|
482
|
+
self.assertEqual(2, len(source))
|
|
483
|
+
self.assertEqual(2, source.currsize)
|
|
484
|
+
self.assertIn(1, source)
|
|
485
|
+
self.assertIn(2, source)
|
|
486
|
+
self.assertNotIn(3, source)
|
|
482
487
|
|
|
483
488
|
self.assertEqual(cache, pickle.loads(pickle.dumps(cache)))
|
|
484
489
|
|
|
@@ -493,3 +498,142 @@ class CacheTestMixin(SimplifiedTestCaseProtocol):
|
|
|
493
498
|
cache = pickle.loads(pickle.dumps(source))
|
|
494
499
|
self.assertEqual(n, len(cache))
|
|
495
500
|
self.assertEqual(source, cache)
|
|
501
|
+
|
|
502
|
+
def test_copy(self):
|
|
503
|
+
import copy
|
|
504
|
+
|
|
505
|
+
source = self.Cache(maxsize=2)
|
|
506
|
+
source.update({1: 1, 2: 2})
|
|
507
|
+
|
|
508
|
+
cache = copy.copy(source)
|
|
509
|
+
self.assertIsNot(source, cache)
|
|
510
|
+
self.assertEqual(source, cache)
|
|
511
|
+
self.assertEqual(source.maxsize, cache.maxsize)
|
|
512
|
+
self.assertEqual(source.currsize, cache.currsize)
|
|
513
|
+
|
|
514
|
+
self.assertEqual(2, len(cache))
|
|
515
|
+
self.assertEqual(1, cache[1])
|
|
516
|
+
self.assertEqual(2, cache[2])
|
|
517
|
+
|
|
518
|
+
cache[3] = 3
|
|
519
|
+
self.assertEqual(2, len(cache))
|
|
520
|
+
self.assertEqual(2, cache.currsize)
|
|
521
|
+
self.assertEqual(3, cache[3])
|
|
522
|
+
self.assertTrue(1 in cache or 2 in cache)
|
|
523
|
+
|
|
524
|
+
self.assertEqual(2, len(source))
|
|
525
|
+
self.assertEqual(2, source.currsize)
|
|
526
|
+
self.assertIn(1, source)
|
|
527
|
+
self.assertIn(2, source)
|
|
528
|
+
self.assertNotIn(3, source)
|
|
529
|
+
|
|
530
|
+
self.assertEqual(cache, copy.copy(cache))
|
|
531
|
+
|
|
532
|
+
def test_copy_maxsize(self):
|
|
533
|
+
import copy
|
|
534
|
+
import sys
|
|
535
|
+
|
|
536
|
+
# test empty cache, single element, large cache (recursion limit)
|
|
537
|
+
for n in [0, 1, sys.getrecursionlimit() * 2]:
|
|
538
|
+
source = self.Cache(maxsize=n)
|
|
539
|
+
source.update((i, i) for i in range(n))
|
|
540
|
+
cache = copy.copy(source)
|
|
541
|
+
self.assertEqual(n, len(cache))
|
|
542
|
+
self.assertEqual(source, cache)
|
|
543
|
+
|
|
544
|
+
def test_copy_attributes(self):
|
|
545
|
+
import copy
|
|
546
|
+
import threading
|
|
547
|
+
|
|
548
|
+
class Cache(self.Cache):
|
|
549
|
+
def __init__(self, maxsize, **kwargs):
|
|
550
|
+
super().__init__(maxsize, **kwargs)
|
|
551
|
+
self.lock = threading.Lock() # cannot be deep-copied
|
|
552
|
+
self.stats = {"hits": 0}
|
|
553
|
+
|
|
554
|
+
source = Cache(maxsize=2)
|
|
555
|
+
source.update({1: [1], 2: [2]})
|
|
556
|
+
|
|
557
|
+
cache = copy.copy(source)
|
|
558
|
+
self.assertIs(type(source), type(cache))
|
|
559
|
+
# a shallow copy shares attributes and values with its source
|
|
560
|
+
self.assertIs(source.lock, cache.lock)
|
|
561
|
+
self.assertIs(source.stats, cache.stats)
|
|
562
|
+
self.assertIs(source[1], cache[1])
|
|
563
|
+
self.assertIs(source[2], cache[2])
|
|
564
|
+
|
|
565
|
+
# but none of the cache's own state
|
|
566
|
+
cache[3] = [3]
|
|
567
|
+
del cache[next(iter(cache))]
|
|
568
|
+
self.assertEqual(1, len(cache))
|
|
569
|
+
self.assertEqual(2, len(source))
|
|
570
|
+
self.assertEqual(2, source.currsize)
|
|
571
|
+
self.assertIn(1, source)
|
|
572
|
+
self.assertIn(2, source)
|
|
573
|
+
self.assertNotIn(3, source)
|
|
574
|
+
|
|
575
|
+
def test_copy_getsizeof(self):
|
|
576
|
+
import copy
|
|
577
|
+
|
|
578
|
+
class GetSizeOf:
|
|
579
|
+
def __call__(self, value):
|
|
580
|
+
return value
|
|
581
|
+
|
|
582
|
+
source = self.Cache(maxsize=3, getsizeof=GetSizeOf())
|
|
583
|
+
source.update({1: 1, 2: 2})
|
|
584
|
+
|
|
585
|
+
cache = copy.copy(source)
|
|
586
|
+
self.assertIs(source.getsizeof, cache.getsizeof)
|
|
587
|
+
self.assertEqual(3, cache.currsize)
|
|
588
|
+
|
|
589
|
+
# item sizes are kept separately for source and copy
|
|
590
|
+
cache[2] = 1
|
|
591
|
+
self.assertEqual(2, cache.currsize)
|
|
592
|
+
self.assertEqual(3, source.currsize)
|
|
593
|
+
del source[2]
|
|
594
|
+
self.assertEqual(1, source.currsize)
|
|
595
|
+
self.assertEqual(2, cache.currsize)
|
|
596
|
+
del cache[2]
|
|
597
|
+
self.assertEqual(1, cache.currsize)
|
|
598
|
+
|
|
599
|
+
def test_deepcopy(self):
|
|
600
|
+
import copy
|
|
601
|
+
|
|
602
|
+
source = self.Cache(maxsize=2)
|
|
603
|
+
source.update({1: 1, 2: 2})
|
|
604
|
+
|
|
605
|
+
cache = copy.deepcopy(source)
|
|
606
|
+
self.assertIsNot(source, cache)
|
|
607
|
+
self.assertEqual(source, cache)
|
|
608
|
+
self.assertEqual(source.maxsize, cache.maxsize)
|
|
609
|
+
self.assertEqual(source.currsize, cache.currsize)
|
|
610
|
+
|
|
611
|
+
self.assertEqual(2, len(cache))
|
|
612
|
+
self.assertEqual(1, cache[1])
|
|
613
|
+
self.assertEqual(2, cache[2])
|
|
614
|
+
|
|
615
|
+
cache[3] = 3
|
|
616
|
+
self.assertEqual(2, len(cache))
|
|
617
|
+
self.assertEqual(2, cache.currsize)
|
|
618
|
+
self.assertEqual(3, cache[3])
|
|
619
|
+
self.assertTrue(1 in cache or 2 in cache)
|
|
620
|
+
|
|
621
|
+
self.assertEqual(2, len(source))
|
|
622
|
+
self.assertEqual(2, source.currsize)
|
|
623
|
+
self.assertIn(1, source)
|
|
624
|
+
self.assertIn(2, source)
|
|
625
|
+
self.assertNotIn(3, source)
|
|
626
|
+
|
|
627
|
+
self.assertEqual(cache, copy.deepcopy(cache))
|
|
628
|
+
|
|
629
|
+
def test_deepcopy_maxsize(self):
|
|
630
|
+
import copy
|
|
631
|
+
import sys
|
|
632
|
+
|
|
633
|
+
# test empty cache, single element, large cache (recursion limit)
|
|
634
|
+
for n in [0, 1, sys.getrecursionlimit() * 2]:
|
|
635
|
+
source = self.Cache(maxsize=n)
|
|
636
|
+
source.update((i, i) for i in range(n))
|
|
637
|
+
cache = copy.deepcopy(source)
|
|
638
|
+
self.assertEqual(n, len(cache))
|
|
639
|
+
self.assertEqual(source, cache)
|
|
@@ -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)
|
|
@@ -137,3 +137,31 @@ class LFUCacheTest(unittest.TestCase, CacheTestMixin):
|
|
|
137
137
|
self.assertIn(3, cache)
|
|
138
138
|
self.assertIn(5, cache)
|
|
139
139
|
self.assertNotIn(4, cache)
|
|
140
|
+
|
|
141
|
+
def test_lfu_copy_use_counts(self):
|
|
142
|
+
"""Regression test for recursion errors from copying and
|
|
143
|
+
pickling LFUCache with many use counts."""
|
|
144
|
+
import copy
|
|
145
|
+
import pickle
|
|
146
|
+
import sys
|
|
147
|
+
|
|
148
|
+
n = sys.getrecursionlimit()
|
|
149
|
+
source = LFUCache[int, int](maxsize=n + 1)
|
|
150
|
+
for key in range(n):
|
|
151
|
+
source[key] = key
|
|
152
|
+
for _ in range(key):
|
|
153
|
+
source[key] # key ends up with use count key + 1
|
|
154
|
+
source[n] = n # tied with key 0 at use count 1
|
|
155
|
+
|
|
156
|
+
for cache in (
|
|
157
|
+
copy.copy(source),
|
|
158
|
+
copy.deepcopy(source),
|
|
159
|
+
pickle.loads(pickle.dumps(source)),
|
|
160
|
+
):
|
|
161
|
+
# compare keys only, since items() would count as a use
|
|
162
|
+
self.assertEqual(list(source), list(cache))
|
|
163
|
+
# the copy evicts by use count, then by age
|
|
164
|
+
order = [cache.popitem()[0] for _ in range(len(cache))]
|
|
165
|
+
self.assertEqual([0, n, *range(1, n)], order)
|
|
166
|
+
|
|
167
|
+
self.assertEqual(n + 1, len(source))
|
|
@@ -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,30 @@ 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))
|
|
@@ -287,3 +287,33 @@ 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))
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|