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

Skip to content

Commit b5d4d2a

Browse files
committed
Patch #409973: Speedup glob.glob, add fnmatch.filter.
1 parent 3d10b34 commit b5d4d2a

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

Lib/fnmatch.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ def fnmatch(name, pat):
3737
pat = os.path.normcase(pat)
3838
return fnmatchcase(name, pat)
3939

40+
def filter(names, pat):
41+
"""Return the subset of the list NAMES that match PAT"""
42+
import os,posixpath
43+
result=[]
44+
pat=os.path.normcase(pat)
45+
if not _cache.has_key(pat):
46+
res = translate(pat)
47+
_cache[pat] = re.compile(res)
48+
match=_cache[pat].match
49+
if os.path is posixpath:
50+
# normcase on posix is NOP. Optimize it away from the loop.
51+
for name in names:
52+
if match(name):
53+
result.append(name)
54+
else:
55+
for name in names:
56+
if match(os.path.normcase(name)):
57+
result.append(name)
58+
return result
59+
4060
def fnmatchcase(name, pat):
4161
"""Test whether FILENAME matches PATTERN, including case.
4262

Lib/glob.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ def glob(pathname):
1818
else:
1919
return []
2020
dirname, basename = os.path.split(pathname)
21-
if has_magic(dirname):
21+
if not dirname:
22+
return glob1(os.curdir, basename)
23+
elif has_magic(dirname):
2224
list = glob(dirname)
2325
else:
2426
list = [dirname]
@@ -43,12 +45,9 @@ def glob1(dirname, pattern):
4345
names = os.listdir(dirname)
4446
except os.error:
4547
return []
46-
result = []
47-
for name in names:
48-
if name[0] != '.' or pattern[0] == '.':
49-
if fnmatch.fnmatch(name, pattern):
50-
result.append(name)
51-
return result
48+
if pattern[0]!='.':
49+
names=filter(lambda x: x[0]!='.',names)
50+
return fnmatch.filter(names,pattern)
5251

5352

5453
magic_check = re.compile('[*?[]')

0 commit comments

Comments
 (0)