]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patch #1001604: glob.glob() now returns unicode filenames if it was
authorGeorg Brandl <georg@python.org>
Wed, 7 Mar 2007 08:31:51 +0000 (08:31 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 7 Mar 2007 08:31:51 +0000 (08:31 +0000)
given a unicode argument and os.listdir() returns unicode filenames.

Lib/glob.py
Lib/test/test_glob.py
Misc/NEWS

index 95656cc1a6dd3657de543d6ec5c479b2d4e10370..a92b11f431bd88a20cbec5239bf22aae0e75bba0 100644 (file)
@@ -1,8 +1,9 @@
 """Filename globbing utility."""
 
+import sys
 import os
-import fnmatch
 import re
+import fnmatch
 
 __all__ = ["glob", "iglob"]
 
@@ -48,13 +49,15 @@ 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())
     try:
         names = os.listdir(dirname)
     except os.error:
         return []
-    if pattern[0]!='.':
-        names=filter(lambda x: x[0]!='.',names)
-    return fnmatch.filter(names,pattern)
+    if pattern[0] != '.':
+        names = filter(lambda x: x[0] != '.', names)
+    return fnmatch.filter(names, pattern)
 
 def glob0(dirname, basename):
     if basename == '':
index 5ce09f9d10d3390f25d15dba7f5542b398e38653..f1993aba311ccd3228b5bf022340877b272a2d3d 100644 (file)
@@ -52,6 +52,16 @@ class GlobTests(unittest.TestCase):
         eq(self.glob('aab'), [self.norm('aab')])
         eq(self.glob('zymurgy'), [])
 
+        # test return types are unicode, but only if os.listdir
+        # returns unicode filenames
+        uniset = set([unicode])
+        tmp = os.listdir(u'.')
+        if set(type(x) for x in tmp) == uniset:
+            u1 = glob.glob(u'*')
+            u2 = glob.glob(u'./*')
+            self.assertEquals(set(type(r) for r in u1), uniset)
+            self.assertEquals(set(type(r) for r in u2), uniset)
+
     def test_glob_one_directory(self):
         eq = self.assertSequencesEqual_noorder
         eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa']))
index bb0290905713db51198e8f824075ca932e1a954c..362bd5ba8cd8227bbfe1c43d174a3cc2bfedc986 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -152,6 +152,9 @@ Core and builtins
 Library
 -------
 
+- Patch #1001604: glob.glob() now returns unicode filenames if it was
+  given a unicode argument and os.listdir() returns unicode filenames.
+
 - Patch #1673619: setup.py identifies extension modules it doesn't know how
   to build and those it knows how to build but that fail to build.