Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39441fc

Browse files
bpo-32338: OrderedDict import is no longer needed in re. (pythonGH-4891)
(cherry picked from commit b931bd0) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 720a4b6 commit 39441fc

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

Lib/re.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,6 @@
128128
except ImportError:
129129
_locale = None
130130

131-
# try _collections first to reduce startup cost
132-
try:
133-
from _collections import OrderedDict
134-
except ImportError:
135-
from collections import OrderedDict
136-
137131

138132
# public symbols
139133
__all__ = [
@@ -271,7 +265,7 @@ def escape(pattern):
271265
# --------------------------------------------------------------------
272266
# internals
273267

274-
_cache = OrderedDict()
268+
_cache = {} # ordered!
275269

276270
_MAXCACHE = 512
277271
def _compile(pattern, flags):
@@ -292,9 +286,10 @@ def _compile(pattern, flags):
292286
p = sre_compile.compile(pattern, flags)
293287
if not (flags & DEBUG):
294288
if len(_cache) >= _MAXCACHE:
289+
# Drop the oldest item
295290
try:
296-
_cache.popitem(last=False)
297-
except KeyError:
291+
del _cache[next(iter(_cache))]
292+
except (StopIteration, RuntimeError, KeyError):
298293
pass
299294
_cache[type(pattern), pattern, flags] = p
300295
return p

0 commit comments

Comments
 (0)