Index: Lib/fnmatch.py =================================================================== --- Lib/fnmatch.py (révision 65973) +++ Lib/fnmatch.py (copie de travail) @@ -11,6 +11,7 @@ """ import re +import sys __all__ = ["filter", "fnmatch","fnmatchcase","translate"] @@ -45,15 +46,25 @@ if not pat in _cache: res = translate(pat) _cache[pat] = re.compile(res) + else: + res = None match=_cache[pat].match - if os.path is posixpath: - # normcase on posix is NOP. Optimize it away from the loop. - for name in names: - if match(name): + match_bytes = None + for name in names: + if isinstance(name, bytes): + if not match_bytes: + # create match regex for bytes string + charset = sys.getfilesystemencoding() + pat = pat.encode(charset) + if not pat in _cache: + if res is None: + res = translate(pat) + res = res.encode(charset) + _cache[pat] = re.compile(res) + match_bytes = _cache[pat].match + if match_bytes(os.path.normcase(name)): result.append(name) - else: - for name in names: - if match(os.path.normcase(name)): + elif match(os.path.normcase(name)): result.append(name) return result Index: Lib/test/test_fnmatch.py =================================================================== --- Lib/test/test_fnmatch.py (révision 65973) +++ Lib/test/test_fnmatch.py (copie de travail) @@ -2,8 +2,9 @@ from test import support import unittest +import sys -from fnmatch import fnmatch, fnmatchcase +from fnmatch import fnmatch, fnmatchcase, filter class FnmatchTestCase(unittest.TestCase): @@ -37,6 +38,11 @@ check('a', r'[!\]') check('\\', r'[!\]', 0) + def test_filter_bytes(self): + charset = sys.getfilesystemencoding() + abc = "abc".encode(charset) + names = ["abc", abc] + self.assertEqual(filter(names, "*"), names) def test_main(): support.run_unittest(FnmatchTestCase)