MCPcopy Index your code
hub / github.com/python/cpython / test_cache_invalidation

Method test_cache_invalidation

Lib/test/test_functools.py:2666–2766  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2664 # Sized in the MRO
2665
2666 def test_cache_invalidation(self):
2667 from collections import UserDict
2668 import weakref
2669
2670 class TracingDict(UserDict):
2671 def __init__(self, *args, **kwargs):
2672 super(TracingDict, self).__init__(*args, **kwargs)
2673 self.set_ops = []
2674 self.get_ops = []
2675 def __getitem__(self, key):
2676 result = self.data[key]
2677 self.get_ops.append(key)
2678 return result
2679 def __setitem__(self, key, value):
2680 self.set_ops.append(key)
2681 self.data[key] = value
2682 def clear(self):
2683 self.data.clear()
2684
2685 td = TracingDict()
2686 with support.swap_attr(weakref, "WeakKeyDictionary", lambda: td):
2687 c = collections.abc
2688 @functools.singledispatch
2689 def g(arg):
2690 return "base"
2691 d = {}
2692 l = []
2693 self.assertEqual(len(td), 0)
2694 self.assertEqual(g(d), "base")
2695 self.assertEqual(len(td), 1)
2696 self.assertEqual(td.get_ops, [])
2697 self.assertEqual(td.set_ops, [dict])
2698 self.assertEqual(td.data[dict], g.registry[object])
2699 self.assertEqual(g(l), "base")
2700 self.assertEqual(len(td), 2)
2701 self.assertEqual(td.get_ops, [])
2702 self.assertEqual(td.set_ops, [dict, list])
2703 self.assertEqual(td.data[dict], g.registry[object])
2704 self.assertEqual(td.data[list], g.registry[object])
2705 self.assertEqual(td.data[dict], td.data[list])
2706 self.assertEqual(g(l), "base")
2707 self.assertEqual(g(d), "base")
2708 self.assertEqual(td.get_ops, [list, dict])
2709 self.assertEqual(td.set_ops, [dict, list])
2710 g.register(list, lambda arg: "list")
2711 self.assertEqual(td.get_ops, [list, dict])
2712 self.assertEqual(len(td), 0)
2713 self.assertEqual(g(d), "base")
2714 self.assertEqual(len(td), 1)
2715 self.assertEqual(td.get_ops, [list, dict])
2716 self.assertEqual(td.set_ops, [dict, list, dict])
2717 self.assertEqual(td.data[dict],
2718 functools._find_impl(dict, g.registry))
2719 self.assertEqual(g(l), "list")
2720 self.assertEqual(len(td), 2)
2721 self.assertEqual(td.get_ops, [list, dict])
2722 self.assertEqual(td.set_ops, [dict, list, dict, list])
2723 self.assertEqual(td.data[list],

Callers

nothing calls this directly

Calls 6

TracingDictClass · 0.85
_clear_cacheMethod · 0.80
gFunction · 0.70
assertEqualMethod · 0.45
registerMethod · 0.45
dispatchMethod · 0.45

Tested by

no test coverage detected