Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(5)

Side by Side Diff: Lib/fnmatch.py

Issue 3055: combined patches from http://bugs.python.org/issue3187 (Closed) SVN Base: http://svn.python.org/view/*checkout*/python/branches/py3k/
Patch Set: One more tweak (fold some long lines) Created 1 year, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 """Filename matching with shell patterns. 1 """Filename matching with shell patterns.
2 2
3 fnmatch(FILENAME, PATTERN) matches according to the local convention. 3 fnmatch(FILENAME, PATTERN) matches according to the local convention.
4 fnmatchcase(FILENAME, PATTERN) always takes case in account. 4 fnmatchcase(FILENAME, PATTERN) always takes case in account.
5 5
6 The functions operate by translating the pattern into a regular 6 The functions operate by translating the pattern into a regular
7 expression. They cache the compiled regular expressions for speed. 7 expression. They cache the compiled regular expressions for speed.
8 8
9 The function translate(PATTERN) returns a regular expression 9 The function translate(PATTERN) returns a regular expression
10 corresponding to PATTERN. (It does not compile it.) 10 corresponding to PATTERN. (It does not compile it.)
(...skipping 18 matching lines...) Expand all
29 An initial period in FILENAME is not special. 29 An initial period in FILENAME is not special.
30 Both FILENAME and PATTERN are first case-normalized 30 Both FILENAME and PATTERN are first case-normalized
31 if the operating system requires it. 31 if the operating system requires it.
32 If you don't want this, use fnmatchcase(FILENAME, PATTERN). 32 If you don't want this, use fnmatchcase(FILENAME, PATTERN).
33 """ 33 """
34 34
35 import os 35 import os
36 name = os.path.normcase(name) 36 name = os.path.normcase(name)
37 pat = os.path.normcase(pat) 37 pat = os.path.normcase(pat)
38 return fnmatchcase(name, pat) 38 return fnmatchcase(name, pat)
39
40 def _compile_pattern(pat):
41 regex = _cache.get(pat)
42 if regex is None:
43 if isinstance(pat, bytes):
44 pat_str = str(pat, 'ISO-8859-1')
45 res_str = translate(pat_str)
46 res = bytes(res_str, 'ISO-8859-1')
47 else:
48 res = translate(pat)
49 _cache[pat] = regex = re.compile(res)
50 return regex.match
39 51
40 def filter(names, pat): 52 def filter(names, pat):
41 """Return the subset of the list NAMES that match PAT""" 53 """Return the subset of the list NAMES that match PAT"""
42 import os,posixpath 54 import os,posixpath
43 result=[] 55 result = []
44 pat=os.path.normcase(pat) 56 pat = os.path.normcase(pat)
45 if not pat in _cache: 57 match = _compile_pattern(pat)
46 res = translate(pat)
47 _cache[pat] = re.compile(res)
48 match=_cache[pat].match
49 if os.path is posixpath: 58 if os.path is posixpath:
50 # normcase on posix is NOP. Optimize it away from the loop. 59 # normcase on posix is NOP. Optimize it away from the loop.
51 for name in names: 60 for name in names:
52 if match(name): 61 if match(name):
53 result.append(name) 62 result.append(name)
54 else: 63 else:
55 for name in names: 64 for name in names:
56 if match(os.path.normcase(name)): 65 if match(os.path.normcase(name)):
57 result.append(name) 66 result.append(name)
58 return result 67 return result
59 68
60 def fnmatchcase(name, pat): 69 def fnmatchcase(name, pat):
61 """Test whether FILENAME matches PATTERN, including case. 70 """Test whether FILENAME matches PATTERN, including case.
62 71
63 This is a version of fnmatch() which doesn't case-normalize 72 This is a version of fnmatch() which doesn't case-normalize
64 its arguments. 73 its arguments.
65 """ 74 """
66 75
67 if not pat in _cache: 76 match = _compile_pattern(pat)
68 res = translate(pat) 77 return match(name) is not None
69 _cache[pat] = re.compile(res)
70 return _cache[pat].match(name) is not None
71 78
72 def translate(pat): 79 def translate(pat):
73 """Translate a shell PATTERN to a regular expression. 80 """Translate a shell PATTERN to a regular expression.
74 81
75 There is no way to quote meta-characters. 82 There is no way to quote meta-characters.
76 """ 83 """
77 84
78 i, n = 0, len(pat) 85 i, n = 0, len(pat)
79 res = '' 86 res = ''
80 while i < n: 87 while i < n:
(...skipping 17 matching lines...) Expand all
98 stuff = pat[i:j].replace('\\','\\\\') 105 stuff = pat[i:j].replace('\\','\\\\')
99 i = j+1 106 i = j+1
100 if stuff[0] == '!': 107 if stuff[0] == '!':
101 stuff = '^' + stuff[1:] 108 stuff = '^' + stuff[1:]
102 elif stuff[0] == '^': 109 elif stuff[0] == '^':
103 stuff = '\\' + stuff 110 stuff = '\\' + stuff
104 res = '%s[%s]' % (res, stuff) 111 res = '%s[%s]' % (res, stuff)
105 else: 112 else:
106 res = res + re.escape(c) 113 res = res + re.escape(c)
107 return res + "$" 114 return res + "$"
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r497