diff options
author | Guido van Rossum <guido@python.org> | 2008-10-02 18:55:37 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2008-10-02 18:55:37 (GMT) |
commit | f0af3e30db9475ab68bcb1f1ce0b5581e214df76 (patch) | |
tree | 71efbc67686d96e8c8a81dd97c75c419adf36657 /Lib/fnmatch.py | |
parent | fefeca53eebe8665c08ac0c041639ada3c9f9446 (diff) | |
download | cpython-f0af3e30db9475ab68bcb1f1ce0b5581e214df76.zip cpython-f0af3e30db9475ab68bcb1f1ce0b5581e214df76.tar.gz cpython-f0af3e30db9475ab68bcb1f1ce0b5581e214df76.tar.bz2 |
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Stinner, with small tweaks by GvR.
Diffstat (limited to 'Lib/fnmatch.py')
-rw-r--r-- | Lib/fnmatch.py | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/Lib/fnmatch.py b/Lib/fnmatch.py index 3bf2463..dd01205 100644 --- a/Lib/fnmatch.py +++ b/Lib/fnmatch.py @@ -37,15 +37,24 @@ def fnmatch(name, pat): pat = os.path.normcase(pat) return fnmatchcase(name, pat) +def _compile_pattern(pat): + regex = _cache.get(pat) + if regex is None: + if isinstance(pat, bytes): + pat_str = str(pat, 'ISO-8859-1') + res_str = translate(pat_str) + res = bytes(res_str, 'ISO-8859-1') + else: + res = translate(pat) + _cache[pat] = regex = re.compile(res) + return regex.match + def filter(names, pat): """Return the subset of the list NAMES that match PAT""" import os,posixpath - result=[] - pat=os.path.normcase(pat) - if not pat in _cache: - res = translate(pat) - _cache[pat] = re.compile(res) - match=_cache[pat].match + result = [] + pat = os.path.normcase(pat) + match = _compile_pattern(pat) if os.path is posixpath: # normcase on posix is NOP. Optimize it away from the loop. for name in names: @@ -64,10 +73,8 @@ def fnmatchcase(name, pat): its arguments. """ - if not pat in _cache: - res = translate(pat) - _cache[pat] = re.compile(res) - return _cache[pat].match(name) is not None + match = _compile_pattern(pat) + return match(name) is not None def translate(pat): """Translate a shell PATTERN to a regular expression. |