]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-28293: Don't completely dump the regex cache when full. (#3768)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 26 Sep 2017 16:47:36 +0000 (19:47 +0300)
committerGitHub <noreply@github.com>
Tue, 26 Sep 2017 16:47:36 +0000 (19:47 +0300)
Lib/re.py
Misc/NEWS.d/next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst [new file with mode: 0644]

index d0ee5db175b5faeae16ebebf0c467d9370b9f016..657a4f6721ed59e494dd059a70af0669c6170e5b 100644 (file)
--- 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
 
diff --git a/Misc/NEWS.d/next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst b/Misc/NEWS.d/next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst
new file mode 100644 (file)
index 0000000..16b92b0
--- /dev/null
@@ -0,0 +1 @@
+The regular expression cache is no longer completely dumped when it is full.