Change fnmatch.py to use separate caches for str and bytes keys.

This is necessary to pass the tests with -bb.
This commit is contained in:
Guido van Rossum 2008-10-03 16:38:30 +00:00
parent 7ed519603f
commit 3f2291f802
1 changed files with 5 additions and 3 deletions

View File

@ -14,7 +14,8 @@ import re
__all__ = ["filter", "fnmatch","fnmatchcase","translate"] __all__ = ["filter", "fnmatch","fnmatchcase","translate"]
_cache = {} _cache = {} # Maps text patterns to compiled regexen.
_cacheb = {} # Ditto for bytes patterns.
def fnmatch(name, pat): def fnmatch(name, pat):
"""Test whether FILENAME matches PATTERN. """Test whether FILENAME matches PATTERN.
@ -38,7 +39,8 @@ def fnmatch(name, pat):
return fnmatchcase(name, pat) return fnmatchcase(name, pat)
def _compile_pattern(pat): def _compile_pattern(pat):
regex = _cache.get(pat) cache = _cacheb if isinstance(pat, bytes) else _cache
regex = cache.get(pat)
if regex is None: if regex is None:
if isinstance(pat, bytes): if isinstance(pat, bytes):
pat_str = str(pat, 'ISO-8859-1') pat_str = str(pat, 'ISO-8859-1')
@ -46,7 +48,7 @@ def _compile_pattern(pat):
res = bytes(res_str, 'ISO-8859-1') res = bytes(res_str, 'ISO-8859-1')
else: else:
res = translate(pat) res = translate(pat)
_cache[pat] = regex = re.compile(res) cache[pat] = regex = re.compile(res)
return regex.match return regex.match
def filter(names, pat): def filter(names, pat):