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)
This commit is contained in:
Georg Brandl 2007-03-07 08:32:24 +00:00
parent fd13ef9c9c
commit 84a0b8d4b6
3 changed files with 20 additions and 4 deletions

View file

@ -1,8 +1,9 @@
"""Filename globbing utility.""" """Filename globbing utility."""
import sys
import os import os
import fnmatch
import re import re
import fnmatch
__all__ = ["glob", "iglob"] __all__ = ["glob", "iglob"]
@ -48,13 +49,15 @@ def iglob(pathname):
def glob1(dirname, pattern): def glob1(dirname, pattern):
if not dirname: if not dirname:
dirname = os.curdir dirname = os.curdir
if isinstance(pattern, unicode) and not isinstance(dirname, unicode):
dirname = unicode(dirname, sys.getfilesystemencoding())
try: try:
names = os.listdir(dirname) names = os.listdir(dirname)
except os.error: except os.error:
return [] return []
if pattern[0]!='.': if pattern[0] != '.':
names=filter(lambda x: x[0]!='.',names) names = filter(lambda x: x[0] != '.', names)
return fnmatch.filter(names,pattern) return fnmatch.filter(names, pattern)
def glob0(dirname, basename): def glob0(dirname, basename):
if basename == '': if basename == '':

View file

@ -52,6 +52,16 @@ class GlobTests(unittest.TestCase):
eq(self.glob('aab'), [self.norm('aab')]) eq(self.glob('aab'), [self.norm('aab')])
eq(self.glob('zymurgy'), []) 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): def test_glob_one_directory(self):
eq = self.assertSequencesEqual_noorder eq = self.assertSequencesEqual_noorder
eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa'])) eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa']))

View file

@ -197,6 +197,9 @@ Extension Modules
Library 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 #685268: Consider a package's __path__ in imputil.
- Patch 1463026: Support default namespace in XMLGenerator. - Patch 1463026: Support default namespace in XMLGenerator.