From 84a0b8d4b6459009862ba7e8ca01079d325dadbf Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 7 Mar 2007 08:32:24 +0000 Subject: Patch #1001604: glob.glob() now returns unicode filenames if it was given a unicode argument and os.listdir() returns unicode filenames. (backport from rev. 54197) --- Lib/glob.py | 11 +++++++---- Lib/test/test_glob.py | 10 ++++++++++ Misc/NEWS | 3 +++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Lib/glob.py b/Lib/glob.py index 95656cc..a92b11f 100644 --- a/Lib/glob.py +++ b/Lib/glob.py @@ -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 == '': diff --git a/Lib/test/test_glob.py b/Lib/test/test_glob.py index 5ce09f9..f1993ab 100644 --- a/Lib/test/test_glob.py +++ b/Lib/test/test_glob.py @@ -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'])) diff --git a/Misc/NEWS b/Misc/NEWS index 81ea485..f39a95a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -197,6 +197,9 @@ Extension Modules Library ------- +- Patch #1001604: glob.glob() now returns unicode filenames if it was + given a unicode argument and os.listdir() returns unicode filenames. + - Patch #685268: Consider a package's __path__ in imputil. - Patch 1463026: Support default namespace in XMLGenerator. -- cgit v0.12