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

Skip to content

Commit c2ef5c2

Browse files
committed
Never return a non-existing pathname.
Rewrote has_magic using a regular expression match.
1 parent 05e5219 commit c2ef5c2

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

Lib/glob.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import os
44
import fnmatch
5+
import regex
56

67

78
def glob(pathname):
8-
if not has_magic(pathname): return [pathname]
9+
if not has_magic(pathname):
10+
if os.path.exists(pathname):
11+
return [pathname]
12+
else:
13+
return []
914
dirname, basename = os.path.split(pathname)
1015
if has_magic(dirname):
1116
list = glob(dirname)
@@ -34,9 +39,13 @@ def glob1(dirname, pattern):
3439
return []
3540
result = []
3641
for name in names:
37-
if name[0] <> '.' or pattern[0] == '.':
38-
if fnmatch.fnmatch(name, pattern): result.append(name)
42+
if name[0] != '.' or pattern[0] == '.':
43+
if fnmatch.fnmatch(name, pattern):
44+
result.append(name)
3945
return result
4046

47+
48+
magic_check = regex.compile('[*?[]')
49+
4150
def has_magic(s):
42-
return '*' in s or '?' in s or '[' in s
51+
return magic_check.search(s) >= 0

0 commit comments

Comments
 (0)