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

Skip to content

Commit 5461558

Browse files
committed
Issue #16696: fix comparison between bytes and string. Also, improve glob tests.
1 parent 3d068b2 commit 5461558

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

Lib/glob.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def glob1(dirname, pattern):
6363
return fnmatch.filter(names, pattern)
6464

6565
def glob0(dirname, basename):
66-
if basename == '':
66+
if not basename:
6767
# `os.path.split()` returns an empty basename for paths ending with a
6868
# directory separator. 'q*x/' should match only directories.
6969
if os.path.isdir(dirname):

Lib/test/test_glob.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,35 @@ def test_glob_directory_names(self):
9797
os.path.join('aab', 'F')]))
9898

9999
def test_glob_directory_with_trailing_slash(self):
100-
# We are verifying that when there is wildcard pattern which
101-
# ends with os.sep doesn't blow up.
102-
res = glob.glob(self.tempdir + '*' + os.sep)
103-
self.assertEqual(len(res), 1)
100+
# Patterns ending with a slash shouldn't match non-dirs
101+
res = glob.glob(os.path.join(self.tempdir, 'Z*Z') + os.sep)
102+
self.assertEqual(res, [])
103+
res = glob.glob(os.path.join(self.tempdir, 'ZZZ') + os.sep)
104+
self.assertEqual(res, [])
105+
# When there is wildcard pattern which ends with os.sep, glob()
106+
# doesn't blow up.
107+
res = glob.glob(os.path.join(self.tempdir, 'aa*') + os.sep)
108+
self.assertEqual(len(res), 2)
104109
# either of these results are reasonable
105-
self.assertIn(res[0], [self.tempdir, self.tempdir + os.sep])
110+
self.assertIn(set(res), [
111+
{self.norm('aaa'), self.norm('aab')},
112+
{self.norm('aaa') + os.sep, self.norm('aab') + os.sep},
113+
])
114+
115+
def test_glob_bytes_directory_with_trailing_slash(self):
116+
# Same as test_glob_directory_with_trailing_slash, but with a
117+
# bytes argument.
118+
res = glob.glob(os.fsencode(os.path.join(self.tempdir, 'Z*Z') + os.sep))
119+
self.assertEqual(res, [])
120+
res = glob.glob(os.fsencode(os.path.join(self.tempdir, 'ZZZ') + os.sep))
121+
self.assertEqual(res, [])
122+
res = glob.glob(os.fsencode(os.path.join(self.tempdir, 'aa*') + os.sep))
123+
self.assertEqual(len(res), 2)
124+
# either of these results are reasonable
125+
self.assertIn({os.fsdecode(x) for x in res}, [
126+
{self.norm('aaa'), self.norm('aab')},
127+
{self.norm('aaa') + os.sep, self.norm('aab') + os.sep},
128+
])
106129

107130
@skip_unless_symlink
108131
def test_glob_broken_symlinks(self):

0 commit comments

Comments
 (0)