[Python-checkins] cpython (2.7): Issue #23191: fnmatch functions that use caching are now threadsafe.

serhiy.storchaka python-checkins at python.org
Tue Jan 27 10:41:49 CET 2015


https://hg.python.org/cpython/rev/fe12c34c39eb
changeset:   94335:fe12c34c39eb
branch:      2.7
parent:      94311:ae42c4576438
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Tue Jan 27 11:40:51 2015 +0200
summary:
  Issue #23191: fnmatch functions that use caching are now threadsafe.

files:
  Lib/fnmatch.py |  16 ++++++++++------
  Misc/NEWS      |   2 ++
  2 files changed, 12 insertions(+), 6 deletions(-)


diff --git a/Lib/fnmatch.py b/Lib/fnmatch.py
--- a/Lib/fnmatch.py
+++ b/Lib/fnmatch.py
@@ -47,12 +47,14 @@
     import os,posixpath
     result=[]
     pat=os.path.normcase(pat)
-    if not pat in _cache:
+    try:
+        re_pat = _cache[pat]
+    except KeyError:
         res = translate(pat)
         if len(_cache) >= _MAXCACHE:
             _cache.clear()
-        _cache[pat] = re.compile(res)
-    match=_cache[pat].match
+        _cache[pat] = re_pat = re.compile(res)
+    match = re_pat.match
     if os.path is posixpath:
         # normcase on posix is NOP. Optimize it away from the loop.
         for name in names:
@@ -71,12 +73,14 @@
     its arguments.
     """
 
-    if not pat in _cache:
+    try:
+        re_pat = _cache[pat]
+    except KeyError:
         res = translate(pat)
         if len(_cache) >= _MAXCACHE:
             _cache.clear()
-        _cache[pat] = re.compile(res)
-    return _cache[pat].match(name) is not None
+        _cache[pat] = re_pat = re.compile(res)
+    return re_pat.match(name) is not None
 
 def translate(pat):
     """Translate a shell PATTERN to a regular expression.
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,8 @@
 Library
 -------
 
+- Issue #23191: fnmatch functions that use caching are now threadsafe.
+
 - Issue #18518: timeit now rejects statements which can't be compiled outside
   a function or a loop (e.g. "return" or "break").
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list