diff options
Diffstat (limited to 'Lib/glob.py')
| -rw-r--r-- | Lib/glob.py | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/Lib/glob.py b/Lib/glob.py index 0aee605..f16e8e1 100644 --- a/Lib/glob.py +++ b/Lib/glob.py @@ -1,18 +1,9 @@ """Filename globbing utility.""" -import sys import os import re import fnmatch -try: - _unicode = unicode -except NameError: - # If Python is built without Unicode support, the unicode type - # will not exist. Fake one. - class _unicode(object): - pass - __all__ = ["glob", "iglob"] def glob(pathname): @@ -35,7 +26,7 @@ def iglob(pathname): return dirname, basename = os.path.split(pathname) if not dirname: - for name in glob1(os.curdir, basename): + for name in glob1(None, basename): yield name return # `os.path.split()` returns the argument itself as a dirname if it is a @@ -59,20 +50,20 @@ def iglob(pathname): def glob1(dirname, pattern): if not dirname: - dirname = os.curdir - if isinstance(pattern, _unicode) and not isinstance(dirname, unicode): - dirname = unicode(dirname, sys.getfilesystemencoding() or - sys.getdefaultencoding()) + if isinstance(pattern, bytes): + dirname = bytes(os.curdir, 'ASCII') + else: + dirname = os.curdir try: names = os.listdir(dirname) except os.error: return [] - if pattern[0] != '.': - names = filter(lambda x: x[0] != '.', names) + if not _ishidden(pattern): + names = [x for x in names if not _ishidden(x)] return fnmatch.filter(names, pattern) def glob0(dirname, basename): - if basename == '': + if not basename: # `os.path.split()` returns an empty basename for paths ending with a # directory separator. 'q*x/' should match only directories. if os.path.isdir(dirname): @@ -84,6 +75,14 @@ def glob0(dirname, basename): magic_check = re.compile('[*?[]') +magic_check_bytes = re.compile(b'[*?[]') def has_magic(s): - return magic_check.search(s) is not None + if isinstance(s, bytes): + match = magic_check_bytes.search(s) + else: + match = magic_check.search(s) + return match is not None + +def _ishidden(path): + return path[0] in ('.', b'.'[0]) |
