summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-03-11 08:38:13 +0200
committerGitHub <noreply@github.com>2018-03-11 08:38:13 +0200
commitb931bd0a2fe7e9293339019352baf3317166b769 (patch)
tree67989fed9c5653d62ecdb82b5ea49bfe1de1dcfd
parent26c9f565d016db21257a60d29ab2c99383dd5ac7 (diff)
downloadcpython-git-b931bd0a2fe7e9293339019352baf3317166b769.tar.gz
bpo-32338: OrderedDict import is no longer needed in re. (#4891)
-rw-r--r--Lib/re.py13
1 files changed, 4 insertions, 9 deletions
diff --git a/Lib/re.py b/Lib/re.py
index a8b6753d39..94d486579e 100644
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -128,12 +128,6 @@ 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__ = [
@@ -271,7 +265,7 @@ Match = type(sre_compile.compile('', 0).match(''))
# --------------------------------------------------------------------
# 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