Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_limit_glob(self):
"""Test expansion limit of `glob`."""
with self.assertRaises(_wcparse.PatternLimitException):
glob.glob('{1..11}', flags=glob.BRACE, limit=10)
def test_limit_globmatch(self):
"""Test expansion limit of `globmatch`."""
with self.assertRaises(_wcparse.PatternLimitException):
pathlib.PurePath('name').globmatch('{1..11}', flags=pathlib.BRACE, limit=10)
def test_limit_translate(self):
"""Test expansion limit of `translate`."""
with self.assertRaises(_wcparse.PatternLimitException):
fnmatch.translate('{1..11}', flags=fnmatch.BRACE, limit=10)
"""
matrix:
- name: glob
default_encoding: utf-8
glob_pattern_limit: 10
sources:
- '{}/**/test-{{1..11}}.txt'
aspell:
lang: en
hunspell:
d: en_US
pipeline: null
"""
).format(self.tempdir)
self.mktemp('.glob.yml', config, 'utf-8')
with self.assertRaises(PatternLimitException):
self.assert_spellcheck('.glob.yml', [])
def test_limit_iglob(self):
"""Test expansion limit of `iglob`."""
with self.assertRaises(_wcparse.PatternLimitException):
list(glob.iglob('{1..11}', flags=glob.BRACE, limit=10))
def _iter_patterns(self, patterns):
"""Iterate expanded patterns."""
seen = set()
for p in patterns:
p = util.norm_pattern(p, not self.unix, self.raw_chars)
for count, expanded in enumerate(_wcparse.expand(p, self.flags), 1):
if 0 < self.limit < count:
raise _wcparse.PatternLimitException(
"Pattern limit exceeded the limit of {:d}".format(self.limit)
)
# Filter out duplicate patterns. If `NOUNIQUE` is enabled,
# we only want to filter on negative patterns as they are
# only filters.
is_neg = _wcparse.is_negative(expanded, self.flags)
if not self.nounique or is_neg:
if expanded in seen:
continue
seen.add(expanded)
yield is_neg, expanded
def compile(patterns, flags, limit=PATTERN_LIMIT): # noqa A001
"""Compile patterns."""
positive = []
negative = []
if isinstance(patterns, (str, bytes)):
patterns = [patterns]
is_unix = is_unix_style(flags)
seen = set()
for pattern in patterns:
pattern = util.norm_pattern(pattern, not is_unix, flags & RAWCHARS)
for count, expanded in enumerate(expand(pattern, flags), 1):
if 0 < limit < count:
raise PatternLimitException("Pattern limit exceeded the limit of {:d}".format(limit))
if expanded not in seen:
seen.add(expanded)
(negative if is_negative(expanded, flags) else positive).append(_compile(expanded, flags))
if patterns and negative and not positive:
if flags & NEGATEALL:
default = '**'
if isinstance(patterns[0], bytes):
default = os.fsencode(default)
positive.append(_compile(default, flags | (GLOBSTAR if flags & PATHNAME else 0)))
if patterns and flags & NODIR:
ptype = BYTES if isinstance(patterns[0], bytes) else UNICODE
negative.append(RE_NO_DIR[ptype] if is_unix else RE_WIN_NO_DIR[ptype])
return WcRegexp(tuple(positive), tuple(negative), flags & REALPATH, flags & PATHNAME, flags & FOLLOW)
"""Translate patterns."""
positive = []
negative = []
if isinstance(patterns, (str, bytes)):
patterns = [patterns]
flags = (flags | _TRANSLATE) & FLAG_MASK
is_unix = is_unix_style(flags)
seen = set()
for pattern in patterns:
pattern = util.norm_pattern(pattern, not is_unix, flags & RAWCHARS)
for count, expanded in enumerate(expand(pattern, flags), 1):
if 0 < limit < count:
raise PatternLimitException("Pattern limit exceeded the limit of {:d}".format(limit))
if expanded not in seen:
seen.add(expanded)
(negative if is_negative(expanded, flags) else positive).append(WcParse(expanded, flags).parse())
if patterns and negative and not positive:
if flags & NEGATEALL:
default = b'**' if isinstance(patterns[0], bytes) else '**'
positive.append(WcParse(default, flags | (GLOBSTAR if flags & PATHNAME else 0)).parse())
if patterns and flags & NODIR:
index = BYTES if isinstance(patterns[0], bytes) else UNICODE
exclude = _NO_NIX_DIR[index] if is_unix else _NO_WIN_DIR[index]
negative.append(exclude)
return positive, negative