Re-apply r83871.
This commit is contained in:
parent
e81c806be7
commit
6fdb74f0ae
|
@ -84,13 +84,6 @@ patterns.
|
||||||
<_sre.SRE_Match object at 0x...>
|
<_sre.SRE_Match object at 0x...>
|
||||||
|
|
||||||
|
|
||||||
.. function:: purge()
|
|
||||||
|
|
||||||
Clear the internal pattern cache.
|
|
||||||
|
|
||||||
.. versionadded:: 3.2
|
|
||||||
|
|
||||||
|
|
||||||
.. seealso::
|
.. seealso::
|
||||||
|
|
||||||
Module :mod:`glob`
|
Module :mod:`glob`
|
||||||
|
|
|
@ -12,19 +12,9 @@ corresponding to PATTERN. (It does not compile it.)
|
||||||
import os
|
import os
|
||||||
import posixpath
|
import posixpath
|
||||||
import re
|
import re
|
||||||
|
import functools
|
||||||
|
|
||||||
__all__ = ["filter", "fnmatch", "fnmatchcase", "purge", "translate"]
|
__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]
|
||||||
|
|
||||||
_cache = {} # Maps text patterns to compiled regexen.
|
|
||||||
_cacheb = {} # Ditto for bytes patterns.
|
|
||||||
_MAXCACHE = 100 # Maximum size of caches.
|
|
||||||
|
|
||||||
|
|
||||||
def purge():
|
|
||||||
"""Clear the pattern cache."""
|
|
||||||
_cache.clear()
|
|
||||||
_cacheb.clear()
|
|
||||||
|
|
||||||
|
|
||||||
def fnmatch(name, pat):
|
def fnmatch(name, pat):
|
||||||
"""Test whether FILENAME matches PATTERN.
|
"""Test whether FILENAME matches PATTERN.
|
||||||
|
@ -45,28 +35,21 @@ def fnmatch(name, pat):
|
||||||
pat = os.path.normcase(pat)
|
pat = os.path.normcase(pat)
|
||||||
return fnmatchcase(name, pat)
|
return fnmatchcase(name, pat)
|
||||||
|
|
||||||
|
@functools.lru_cache(maxsize=250)
|
||||||
def _compile_pattern(pat):
|
def _compile_pattern(pat, is_bytes=False):
|
||||||
cache = _cacheb if isinstance(pat, bytes) else _cache
|
if is_bytes:
|
||||||
regex = cache.get(pat)
|
pat_str = str(pat, 'ISO-8859-1')
|
||||||
if regex is None:
|
res_str = translate(pat_str)
|
||||||
if isinstance(pat, bytes):
|
res = bytes(res_str, 'ISO-8859-1')
|
||||||
pat_str = str(pat, 'ISO-8859-1')
|
else:
|
||||||
res_str = translate(pat_str)
|
res = translate(pat)
|
||||||
res = bytes(res_str, 'ISO-8859-1')
|
return re.compile(res).match
|
||||||
else:
|
|
||||||
res = translate(pat)
|
|
||||||
if len(cache) >= _MAXCACHE:
|
|
||||||
cache.clear()
|
|
||||||
cache[pat] = regex = re.compile(res)
|
|
||||||
return regex.match
|
|
||||||
|
|
||||||
|
|
||||||
def filter(names, pat):
|
def filter(names, pat):
|
||||||
"""Return the subset of the list NAMES that match PAT."""
|
"""Return the subset of the list NAMES that match PAT."""
|
||||||
result = []
|
result = []
|
||||||
pat = os.path.normcase(pat)
|
pat = os.path.normcase(pat)
|
||||||
match = _compile_pattern(pat)
|
match = _compile_pattern(pat, isinstance(pat, bytes))
|
||||||
if os.path is posixpath:
|
if os.path is posixpath:
|
||||||
# normcase on posix is NOP. Optimize it away from the loop.
|
# normcase on posix is NOP. Optimize it away from the loop.
|
||||||
for name in names:
|
for name in names:
|
||||||
|
@ -78,14 +61,13 @@ def filter(names, pat):
|
||||||
result.append(name)
|
result.append(name)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def fnmatchcase(name, pat):
|
def fnmatchcase(name, pat):
|
||||||
"""Test whether FILENAME matches PATTERN, including case.
|
"""Test whether FILENAME matches PATTERN, including case.
|
||||||
|
|
||||||
This is a version of fnmatch() which doesn't case-normalize
|
This is a version of fnmatch() which doesn't case-normalize
|
||||||
its arguments.
|
its arguments.
|
||||||
"""
|
"""
|
||||||
match = _compile_pattern(pat)
|
match = _compile_pattern(pat, isinstance(pat, bytes))
|
||||||
return match(name) is not None
|
return match(name) is not None
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,15 +3,10 @@
|
||||||
from test import support
|
from test import support
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from fnmatch import (fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb, purge,
|
from fnmatch import fnmatch, fnmatchcase, translate, filter
|
||||||
translate, filter)
|
|
||||||
|
|
||||||
|
|
||||||
class FnmatchTestCase(unittest.TestCase):
|
class FnmatchTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
purge()
|
|
||||||
|
|
||||||
def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
|
def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
|
||||||
if should_match:
|
if should_match:
|
||||||
self.assertTrue(fn(filename, pattern),
|
self.assertTrue(fn(filename, pattern),
|
||||||
|
@ -65,22 +60,6 @@ class FnmatchTestCase(unittest.TestCase):
|
||||||
self.check_match(b'test\xff', b'te*\xff')
|
self.check_match(b'test\xff', b'te*\xff')
|
||||||
self.check_match(b'foo\nbar', b'foo*')
|
self.check_match(b'foo\nbar', b'foo*')
|
||||||
|
|
||||||
def test_cache_clearing(self):
|
|
||||||
# check that caches do not grow too large
|
|
||||||
# http://bugs.python.org/issue7846
|
|
||||||
|
|
||||||
# string pattern cache
|
|
||||||
for i in range(_MAXCACHE + 1):
|
|
||||||
fnmatch('foo', '?' * i)
|
|
||||||
|
|
||||||
self.assertLessEqual(len(_cache), _MAXCACHE)
|
|
||||||
|
|
||||||
# bytes pattern cache
|
|
||||||
for i in range(_MAXCACHE + 1):
|
|
||||||
fnmatch(b'foo', b'?' * i)
|
|
||||||
self.assertLessEqual(len(_cacheb), _MAXCACHE)
|
|
||||||
|
|
||||||
|
|
||||||
class TranslateTestCase(unittest.TestCase):
|
class TranslateTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def test_translate(self):
|
def test_translate(self):
|
||||||
|
|
Loading…
Reference in New Issue