[Python-checkins] bpo-32338: OrderedDict import is no longer needed in re. (GH-4891)

Miss Islington (bot) webhook-mailer at python.org
Sun Mar 11 03:02:01 EDT 2018


https://github.com/python/cpython/commit/39441fce0218a3f51a80cf17aa179a32651a02f6
commit: 39441fce0218a3f51a80cf17aa179a32651a02f6
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-03-10T23:01:58-08:00
summary:

bpo-32338: OrderedDict import is no longer needed in re. (GH-4891)

(cherry picked from commit b931bd0a2fe7e9293339019352baf3317166b769)

Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>

files:
M Lib/re.py

diff --git a/Lib/re.py b/Lib/re.py
index a8b6753d3909..94d486579e08 100644
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -128,12 +128,6 @@
 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__ = [
@@ -271,7 +265,7 @@ def escape(pattern):
 # --------------------------------------------------------------------
 # internals
 
-_cache = OrderedDict()
+_cache = {}  # ordered!
 
 _MAXCACHE = 512
 def _compile(pattern, flags):
@@ -292,9 +286,10 @@ def _compile(pattern, flags):
     p = sre_compile.compile(pattern, flags)
     if not (flags & DEBUG):
         if len(_cache) >= _MAXCACHE:
+            # Drop the oldest item
             try:
-                _cache.popitem(last=False)
-            except KeyError:
+                del _cache[next(iter(_cache))]
+            except (StopIteration, RuntimeError, KeyError):
                 pass
         _cache[type(pattern), pattern, flags] = p
     return p



More information about the Python-checkins mailing list