]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patch #409973: Speedup glob.glob, add fnmatch.filter.
authorMartin v. Löwis <martin@v.loewis.de>
Wed, 6 Jun 2001 06:24:38 +0000 (06:24 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Wed, 6 Jun 2001 06:24:38 +0000 (06:24 +0000)
Lib/fnmatch.py
Lib/glob.py

index 182a9ef923f0338e4c0ac39a1df8095af600966c..da0bb34c781af19ed6e09f7a53fa321596ae84a1 100644 (file)
@@ -37,6 +37,26 @@ def fnmatch(name, pat):
     pat = os.path.normcase(pat)
     return fnmatchcase(name, pat)
 
+def filter(names, pat):
+    """Return the subset of the list NAMES that match PAT"""
+    import os,posixpath
+    result=[]
+    pat=os.path.normcase(pat)
+    if not _cache.has_key(pat):
+        res = translate(pat)
+        _cache[pat] = re.compile(res)
+    match=_cache[pat].match
+    if os.path is posixpath:
+        # normcase on posix is NOP. Optimize it away from the loop.
+        for name in names:
+            if match(name):
+                result.append(name)
+    else:
+        for name in names:
+            if match(os.path.normcase(name)):
+                result.append(name)
+    return result
+
 def fnmatchcase(name, pat):
     """Test whether FILENAME matches PATTERN, including case.
 
index eeb6bdd6468aeaaa0a20fdaa6850eb2a9099b5c6..d5e508abab9d27d2a5707d4b93673eb4d215952d 100644 (file)
@@ -18,7 +18,9 @@ def glob(pathname):
         else:
             return []
     dirname, basename = os.path.split(pathname)
-    if has_magic(dirname):
+    if not dirname:
+        return glob1(os.curdir, basename)
+    elif has_magic(dirname):
         list = glob(dirname)
     else:
         list = [dirname]
@@ -43,12 +45,9 @@ def glob1(dirname, pattern):
         names = os.listdir(dirname)
     except os.error:
         return []
-    result = []
-    for name in names:
-        if name[0] != '.' or pattern[0] == '.':
-            if fnmatch.fnmatch(name, pattern):
-                result.append(name)
-    return result
+    if pattern[0]!='.':
+        names=filter(lambda x: x[0]!='.',names)
+    return fnmatch.filter(names,pattern)
 
 
 magic_check = re.compile('[*?[]')