Kaydet (Commit) 3ce465ab authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #23191: fnmatch functions that use caching are now threadsafe.

üst c328d11e
...@@ -47,12 +47,14 @@ def filter(names, pat): ...@@ -47,12 +47,14 @@ def filter(names, pat):
import os,posixpath import os,posixpath
result=[] result=[]
pat=os.path.normcase(pat) pat=os.path.normcase(pat)
if not pat in _cache: try:
re_pat = _cache[pat]
except KeyError:
res = translate(pat) res = translate(pat)
if len(_cache) >= _MAXCACHE: if len(_cache) >= _MAXCACHE:
_cache.clear() _cache.clear()
_cache[pat] = re.compile(res) _cache[pat] = re_pat = re.compile(res)
match=_cache[pat].match match = re_pat.match
if os.path is posixpath: if os.path is posixpath:
# normcase on posix is NOP. Optimize it away from the loop. # normcase on posix is NOP. Optimize it away from the loop.
for name in names: for name in names:
...@@ -71,12 +73,14 @@ def fnmatchcase(name, pat): ...@@ -71,12 +73,14 @@ def fnmatchcase(name, pat):
its arguments. its arguments.
""" """
if not pat in _cache: try:
re_pat = _cache[pat]
except KeyError:
res = translate(pat) res = translate(pat)
if len(_cache) >= _MAXCACHE: if len(_cache) >= _MAXCACHE:
_cache.clear() _cache.clear()
_cache[pat] = re.compile(res) _cache[pat] = re_pat = re.compile(res)
return _cache[pat].match(name) is not None return re_pat.match(name) is not None
def translate(pat): def translate(pat):
"""Translate a shell PATTERN to a regular expression. """Translate a shell PATTERN to a regular expression.
......
...@@ -15,6 +15,8 @@ Core and Builtins ...@@ -15,6 +15,8 @@ Core and Builtins
Library Library
------- -------
- Issue #23191: fnmatch functions that use caching are now threadsafe.
- Issue #18518: timeit now rejects statements which can't be compiled outside - Issue #18518: timeit now rejects statements which can't be compiled outside
a function or a loop (e.g. "return" or "break"). a function or a loop (e.g. "return" or "break").
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment