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

Function memoize_wrapper

Tools/peg_generator/pegen/parser.py:44–71  ·  view source on GitHub ↗
(self: "Parser", *args: object)

Source from the content-addressed store, hash-verified

42 method_name = method.__name__
43
44 def memoize_wrapper(self: "Parser", *args: object) -> Any:
45 mark = self._mark()
46 key = mark, method_name, args
47 # Fast path: cache hit, and not verbose.
48 if key in self._cache and not self._verbose:
49 tree, endmark = self._cache[key]
50 self._reset(endmark)
51 return tree
52 # Slow path: no cache hit, or verbose.
53 verbose = self._verbose
54 argsr = ",".join(repr(arg) for arg in args)
55 fill = " " * self._level
56 if key not in self._cache:
57 if verbose:
58 print(f"{fill}{method_name}({argsr}) ... (looking at {self.showpeek()})")
59 self._level += 1
60 tree = method(self, *args)
61 self._level -= 1
62 if verbose:
63 print(f"{fill}... {method_name}({argsr}) -> {tree!s:.200}")
64 endmark = self._mark()
65 self._cache[key] = tree, endmark
66 else:
67 tree, endmark = self._cache[key]
68 if verbose:
69 print(f"{fill}{method_name}({argsr}) -> {tree!s:.200}")
70 self._reset(endmark)
71 return tree
72
73 memoize_wrapper.__wrapped__ = method # type: ignore[attr-defined]
74 return cast(F, memoize_wrapper)

Callers

nothing calls this directly

Calls 4

methodFunction · 0.85
showpeekMethod · 0.80
_resetMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…