diff options
author | Guido van Rossum <guido@python.org> | 1992-01-12 23:32:11 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1992-01-12 23:32:11 (GMT) |
commit | c2ef5c2dede30e147a5a82f285d2510c9ca853a0 (patch) | |
tree | a0deeea5e8e4f2e01a8d61913c02e1f3442f5289 | |
parent | 05e5219f53996f606b93fe42a56771819090e994 (diff) | |
download | cpython-c2ef5c2dede30e147a5a82f285d2510c9ca853a0.zip cpython-c2ef5c2dede30e147a5a82f285d2510c9ca853a0.tar.gz cpython-c2ef5c2dede30e147a5a82f285d2510c9ca853a0.tar.bz2 |
Never return a non-existing pathname.
Rewrote has_magic using a regular expression match.
-rw-r--r-- | Lib/glob.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/Lib/glob.py b/Lib/glob.py index 354af39..bacaf18 100644 --- a/Lib/glob.py +++ b/Lib/glob.py @@ -2,10 +2,15 @@ import os import fnmatch +import regex def glob(pathname): - if not has_magic(pathname): return [pathname] + if not has_magic(pathname): + if os.path.exists(pathname): + return [pathname] + else: + return [] dirname, basename = os.path.split(pathname) if has_magic(dirname): list = glob(dirname) @@ -34,9 +39,13 @@ def glob1(dirname, pattern): return [] result = [] for name in names: - if name[0] <> '.' or pattern[0] == '.': - if fnmatch.fnmatch(name, pattern): result.append(name) + if name[0] != '.' or pattern[0] == '.': + if fnmatch.fnmatch(name, pattern): + result.append(name) return result + +magic_check = regex.compile('[*?[]') + def has_magic(s): - return '*' in s or '?' in s or '[' in s + return magic_check.search(s) >= 0 |