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

Side by Side Diff: Lib/glob.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 globbing utility.""" 1 """Filename globbing utility."""
2 2
3 import sys 3 import sys
4 import os 4 import os
5 import re 5 import re
6 import fnmatch 6 import fnmatch
7 7
8 __all__ = ["glob", "iglob"] 8 __all__ = ["glob", "iglob"]
9 9
10 def glob(pathname): 10 def glob(pathname):
11 """Return a list of paths matching a pathname pattern. 11 """Return a list of paths matching a pathname pattern.
12 12
13 The pattern may contain simple shell-style wildcards a la fnmatch. 13 The pattern may contain simple shell-style wildcards a la fnmatch.
14 14
15 """ 15 """
16 return list(iglob(pathname)) 16 return list(iglob(pathname))
17 17
18 def iglob(pathname): 18 def iglob(pathname):
19 """Return a list of paths matching a pathname pattern. 19 """Return a list of paths matching a pathname pattern.
20 20
21 The pattern may contain simple shell-style wildcards a la fnmatch. 21 The pattern may contain simple shell-style wildcards a la fnmatch.
22 22
23 """ 23 """
24 if not has_magic(pathname): 24 if not has_magic(pathname):
25 if os.path.lexists(pathname): 25 if os.path.lexists(pathname):
26 yield pathname 26 yield pathname
27 return 27 return
28 dirname, basename = os.path.split(pathname) 28 dirname, basename = os.path.split(pathname)
29 if not dirname: 29 if not dirname:
30 for name in glob1(os.curdir, basename): 30 for name in glob1(None, basename):
31 yield name 31 yield name
32 return 32 return
33 if has_magic(dirname): 33 if has_magic(dirname):
34 dirs = iglob(dirname) 34 dirs = iglob(dirname)
35 else: 35 else:
36 dirs = [dirname] 36 dirs = [dirname]
37 if has_magic(basename): 37 if has_magic(basename):
38 glob_in_dir = glob1 38 glob_in_dir = glob1
39 else: 39 else:
40 glob_in_dir = glob0 40 glob_in_dir = glob0
41 for dirname in dirs: 41 for dirname in dirs:
42 for name in glob_in_dir(dirname, basename): 42 for name in glob_in_dir(dirname, basename):
43 yield os.path.join(dirname, name) 43 yield os.path.join(dirname, name)
44 44
45 # These 2 helper functions non-recursively glob inside a literal directory. 45 # These 2 helper functions non-recursively glob inside a literal directory.
46 # They return a list of basenames. `glob1` accepts a pattern while `glob0` 46 # They return a list of basenames. `glob1` accepts a pattern while `glob0`
47 # takes a literal basename (so it only has to check for its existence). 47 # takes a literal basename (so it only has to check for its existence).
48 48
49 def glob1(dirname, pattern): 49 def glob1(dirname, pattern):
50 if not dirname: 50 if not dirname:
51 dirname = os.curdir 51 if isinstance(pattern, bytes):
52 if isinstance(pattern, str) and not isinstance(dirname, str): 52 dirname = bytes(os.curdir, 'ASCII')
53 dirname = str(dirname, sys.getfilesystemencoding() or 53 else:
54 sys.getdefaultencoding()) 54 dirname = os.curdir
55 try: 55 try:
56 names = os.listdir(dirname) 56 names = os.listdir(dirname)
57 except os.error: 57 except os.error:
58 return [] 58 return []
59 if pattern[0] != '.': 59 if pattern[0] != '.':
60 names = [x for x in names if x[0] != '.'] 60 names = [x for x in names if x[0] != '.']
61 return fnmatch.filter(names, pattern) 61 return fnmatch.filter(names, pattern)
62 62
63 def glob0(dirname, basename): 63 def glob0(dirname, basename):
64 if basename == '': 64 if basename == '':
65 # `os.path.split()` returns an empty basename for paths ending with a 65 # `os.path.split()` returns an empty basename for paths ending with a
66 # directory separator. 'q*x/' should match only directories. 66 # directory separator. 'q*x/' should match only directories.
67 if os.path.isdir(dirname): 67 if os.path.isdir(dirname):
68 return [basename] 68 return [basename]
69 else: 69 else:
70 if os.path.lexists(os.path.join(dirname, basename)): 70 if os.path.lexists(os.path.join(dirname, basename)):
71 return [basename] 71 return [basename]
72 return [] 72 return []
73 73
74 74
75 magic_check = re.compile('[*?[]') 75 magic_check = re.compile('[*?[]')
76 magic_check_bytes = re.compile(b'[*?[]')
76 77
77 def has_magic(s): 78 def has_magic(s):
78 return magic_check.search(s) is not None 79 if isinstance(s, bytes):
80 match = magic_check_bytes.search(s)
81 else:
82 match = magic_check.search(s)
83 return match is not None
OLDNEW

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