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

This commit is contained in:
Serhiy Storchaka 2017-09-26 19:47:36 +03:00 committed by GitHub
parent 0e950dd22b
commit 114454e9f6
2 changed files with 13 additions and 2 deletions

View File

@ -128,6 +128,13 @@ try:
except ImportError:
_locale = None
# try _collections first to reduce startup cost
try:
from _collections import OrderedDict
except ImportError:
from collections import OrderedDict
# public symbols
__all__ = [
"match", "fullmatch", "search", "sub", "subn", "split",
@ -260,7 +267,7 @@ def escape(pattern):
# --------------------------------------------------------------------
# internals
_cache = {}
_cache = OrderedDict()
_pattern_type = type(sre_compile.compile("", 0))
@ -281,7 +288,10 @@ def _compile(pattern, flags):
p = sre_compile.compile(pattern, flags)
if not (flags & DEBUG):
if len(_cache) >= _MAXCACHE:
_cache.clear()
try:
_cache.popitem(False)
except KeyError:
pass
_cache[type(pattern), pattern, flags] = p
return p

View File

@ -0,0 +1 @@
The regular expression cache is no longer completely dumped when it is full.