summaryrefslogtreecommitdiffstats
path: root/Lib/re.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-09-26 16:47:36 (GMT)
committerGitHub <noreply@github.com>2017-09-26 16:47:36 (GMT)
commit114454e9f6addbcb364e9a37102c8131ae2da1dd (patch)
treef356c57b2a928ac2e581c0b18f0ab8c8e16cdc3e /Lib/re.py
parent0e950dd22b075b4809c84afda8aede02b76ac0fa (diff)
downloadcpython-114454e9f6addbcb364e9a37102c8131ae2da1dd.zip
cpython-114454e9f6addbcb364e9a37102c8131ae2da1dd.tar.gz
cpython-114454e9f6addbcb364e9a37102c8131ae2da1dd.tar.bz2
bpo-28293: Don't completely dump the regex cache when full. (#3768)
Diffstat (limited to 'Lib/re.py')
-rw-r--r--Lib/re.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/re.py b/Lib/re.py
index d0ee5db..657a4f6 100644
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -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