This commit is contained in:
Raymond Hettinger 2016-09-18 20:17:51 -07:00
commit 07064c409c
1 changed files with 4 additions and 11 deletions

View File

@ -122,6 +122,7 @@ This module also defines an exception 'error'.
import enum import enum
import sre_compile import sre_compile
import sre_parse import sre_parse
import functools
try: try:
import _locale import _locale
except ImportError: except ImportError:
@ -234,7 +235,7 @@ def compile(pattern, flags=0):
def purge(): def purge():
"Clear the regular expression caches" "Clear the regular expression caches"
_cache.clear() _cache.clear()
_cache_repl.clear() _compile_repl.cache_clear()
def template(pattern, flags=0): def template(pattern, flags=0):
"Compile a template pattern, returning a pattern object" "Compile a template pattern, returning a pattern object"
@ -278,7 +279,6 @@ def escape(pattern):
# internals # internals
_cache = {} _cache = {}
_cache_repl = {}
_pattern_type = type(sre_compile.compile("", 0)) _pattern_type = type(sre_compile.compile("", 0))
@ -311,17 +311,10 @@ def _compile(pattern, flags):
_cache[type(pattern), pattern, flags] = p, loc _cache[type(pattern), pattern, flags] = p, loc
return p return p
@functools.lru_cache(_MAXCACHE)
def _compile_repl(repl, pattern): def _compile_repl(repl, pattern):
# internal: compile replacement pattern # internal: compile replacement pattern
try: return sre_parse.parse_template(repl, pattern)
return _cache_repl[repl, pattern]
except KeyError:
pass
p = sre_parse.parse_template(repl, pattern)
if len(_cache_repl) >= _MAXCACHE:
_cache_repl.clear()
_cache_repl[repl, pattern] = p
return p
def _expand(pattern, match, template): def _expand(pattern, match, template):
# internal: match.expand implementation hook # internal: match.expand implementation hook