Kaydet (Commit) 114454e9 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka Kaydeden (comit) GitHub

bpo-28293: Don't completely dump the regex cache when full. (#3768)

üst 0e950dd2
...@@ -128,6 +128,13 @@ try: ...@@ -128,6 +128,13 @@ try:
except ImportError: except ImportError:
_locale = None _locale = None
# try _collections first to reduce startup cost
try:
from _collections import OrderedDict
except ImportError:
from collections import OrderedDict
# public symbols # public symbols
__all__ = [ __all__ = [
"match", "fullmatch", "search", "sub", "subn", "split", "match", "fullmatch", "search", "sub", "subn", "split",
...@@ -260,7 +267,7 @@ def escape(pattern): ...@@ -260,7 +267,7 @@ def escape(pattern):
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# internals # internals
_cache = {} _cache = OrderedDict()
_pattern_type = type(sre_compile.compile("", 0)) _pattern_type = type(sre_compile.compile("", 0))
...@@ -281,7 +288,10 @@ def _compile(pattern, flags): ...@@ -281,7 +288,10 @@ def _compile(pattern, flags):
p = sre_compile.compile(pattern, flags) p = sre_compile.compile(pattern, flags)
if not (flags & DEBUG): if not (flags & DEBUG):
if len(_cache) >= _MAXCACHE: if len(_cache) >= _MAXCACHE:
_cache.clear() try:
_cache.popitem(False)
except KeyError:
pass
_cache[type(pattern), pattern, flags] = p _cache[type(pattern), pattern, flags] = p
return p return p
......
The regular expression cache is no longer completely dumped when it is full.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment