77from sphinxlint import rst
88
99
10+ PER_FILE_CACHES = []
11+
12+
13+ def per_file_cache (func ):
14+ memoized_func = lru_cache (maxsize = None )(func )
15+ PER_FILE_CACHES .append (memoized_func )
16+ return memoized_func
17+
18+
1019def match_size (re_match ):
1120 return re_match .end () - re_match .start ()
1221
@@ -29,7 +38,7 @@ def _clean_heuristic(paragraph, regex):
2938 paragraph = paragraph [: candidate .start ()] + paragraph [candidate .end () :]
3039
3140
32- @lru_cache ()
41+ @per_file_cache
3342def clean_paragraph (paragraph ):
3443 """Removes all good constructs, so detectors can focus on bad ones.
3544
@@ -45,7 +54,7 @@ def clean_paragraph(paragraph):
4554 return paragraph .replace ("\x00 " , "\\ " )
4655
4756
48- @lru_cache ()
57+ @per_file_cache
4958def escape2null (text ):
5059 r"""Return a string with escape-backslashes converted to nulls.
5160
@@ -79,7 +88,7 @@ def escape2null(text):
7988 start = found + 2 # skip character after escape
8089
8190
82- @lru_cache ()
91+ @per_file_cache
8392def paragraphs (lines ):
8493 """Yield (paragraph_line_no, paragraph_text) pairs describing
8594 paragraphs of the given lines.
@@ -150,7 +159,6 @@ def is_multiline_non_rst_block(line):
150159 return False
151160
152161
153- _NON_RST_BLOCKS_CACHE = {}
154162_ZERO_OR_MORE_SPACES_RE = re .compile (" *" )
155163
156164
@@ -159,14 +167,7 @@ def hide_non_rst_blocks(lines, hidden_block_cb=None):
159167
160168 The filter actually replace "removed" lines by empty lines, so the
161169 line numbering still make sense.
162-
163- This function is quite hot, so we cache the returned value where possible.
164- The function is only "pure" when hidden_block_cb is None, however,
165- so we can only safely cache the output when hidden_block_cb=None.
166170 """
167- lines = tuple (lines )
168- if hidden_block_cb is None and lines in _NON_RST_BLOCKS_CACHE :
169- return _NON_RST_BLOCKS_CACHE [lines ]
170171 in_literal = None
171172 excluded_lines = []
172173 block_line_start = None
@@ -194,10 +195,7 @@ def hide_non_rst_blocks(lines, hidden_block_cb=None):
194195 output .append (line )
195196 if excluded_lines and hidden_block_cb :
196197 hidden_block_cb (block_line_start , "" .join (excluded_lines ))
197- output = tuple (output )
198- if hidden_block_cb is None :
199- _NON_RST_BLOCKS_CACHE [lines ] = output
200- return output
198+ return tuple (output )
201199
202200
203201_starts_with_directive_marker = re .compile (rf"\.\. { rst .ALL_DIRECTIVES } ::" ).match
@@ -207,7 +205,7 @@ def hide_non_rst_blocks(lines, hidden_block_cb=None):
207205_starts_with_substitution_definition = re .compile (r"\.\. \|[^\|]*\| " ).match
208206
209207
210- @lru_cache ()
208+ @per_file_cache
211209def type_of_explicit_markup (line ):
212210 """Tell apart various explicit markup blocks."""
213211 line = line .lstrip ()
0 commit comments