[Python-checkins] cpython (merge 3.3 -> default): Issue #16696: fix comparison between bytes and string. Also, improve glob tests.

antoine.pitrou python-checkins at python.org
Sun Dec 16 16:05:48 CET 2012


http://hg.python.org/cpython/rev/9e898ee68388
changeset:   80885:9e898ee68388
parent:      80881:bfb39fb93af7
parent:      80884:ed8134df30e4
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sun Dec 16 16:03:57 2012 +0100
summary:
  Issue #16696: fix comparison between bytes and string. Also, improve glob tests.

files:
  Lib/glob.py           |   2 +-
  Lib/test/test_glob.py |  33 ++++++++++++++++++++++++++----
  2 files changed, 29 insertions(+), 6 deletions(-)


diff --git a/Lib/glob.py b/Lib/glob.py
--- a/Lib/glob.py
+++ b/Lib/glob.py
@@ -62,7 +62,7 @@
     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):
diff --git a/Lib/test/test_glob.py b/Lib/test/test_glob.py
--- a/Lib/test/test_glob.py
+++ b/Lib/test/test_glob.py
@@ -97,12 +97,35 @@
                                                    os.path.join('aab', 'F')]))
 
     def test_glob_directory_with_trailing_slash(self):
-        # We are verifying that when there is wildcard pattern which
-        # ends with os.sep doesn't blow up.
-        res = glob.glob(self.tempdir + '*' + os.sep)
-        self.assertEqual(len(res), 1)
+        # Patterns ending with a slash shouldn't match non-dirs
+        res = glob.glob(os.path.join(self.tempdir, 'Z*Z') + os.sep)
+        self.assertEqual(res, [])
+        res = glob.glob(os.path.join(self.tempdir, 'ZZZ') + os.sep)
+        self.assertEqual(res, [])
+        # When there is wildcard pattern which ends with os.sep, glob()
+        # doesn't blow up.
+        res = glob.glob(os.path.join(self.tempdir, 'aa*') + os.sep)
+        self.assertEqual(len(res), 2)
         # either of these results are reasonable
-        self.assertIn(res[0], [self.tempdir, self.tempdir + os.sep])
+        self.assertIn(set(res), [
+                      {self.norm('aaa'), self.norm('aab')},
+                      {self.norm('aaa') + os.sep, self.norm('aab') + os.sep},
+                      ])
+
+    def test_glob_bytes_directory_with_trailing_slash(self):
+        # Same as test_glob_directory_with_trailing_slash, but with a
+        # bytes argument.
+        res = glob.glob(os.fsencode(os.path.join(self.tempdir, 'Z*Z') + os.sep))
+        self.assertEqual(res, [])
+        res = glob.glob(os.fsencode(os.path.join(self.tempdir, 'ZZZ') + os.sep))
+        self.assertEqual(res, [])
+        res = glob.glob(os.fsencode(os.path.join(self.tempdir, 'aa*') + os.sep))
+        self.assertEqual(len(res), 2)
+        # either of these results are reasonable
+        self.assertIn({os.fsdecode(x) for x in res}, [
+                      {self.norm('aaa'), self.norm('aab')},
+                      {self.norm('aaa') + os.sep, self.norm('aab') + os.sep},
+                      ])
 
     @skip_unless_symlink
     def test_glob_broken_symlinks(self):

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list