Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pathspec/patterns/gitignore/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,19 @@ def _translate_segment_glob(

expr += body + ']'

if errors == 'raise':
try:
re.compile(expr)
except re.error as e: # Renamed to `re.PatternError` in 3.13.
try:
re.compile(expr)
except re.error as e: # Renamed to `re.PatternError` in 3.13.
if errors == 'raise':
raise _RangeNotationError((
f"Invalid range notation={pattern[i:j]!r} found in "
f"pattern={pattern!r}."
)) from e
else:
# Treat the whole bracket expression as a literal.
regex += re.escape(pattern[bracket_start:j])
i = j
continue

# Add regex bracket expression to regex result.
regex += expr
Expand Down
15 changes: 2 additions & 13 deletions tests/test_03_gitignore_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import re
from unittest import (
TestCase)
try:
from re import PatternError as re_PatternError # Added in 3.13.
except ImportError:
from re import error as re_PatternError

from pathspec.patterns.gitignore.base import (
GitIgnorePatternError,
Expand Down Expand Up @@ -1052,21 +1048,14 @@ def test_c_2_invalid(self):
('[^]', f'^(?s:.+/)?\\[\\^\\]{_DIR_OPT}'),
('a[!]', f'^(?s:.+/)?a\\[!\\]{_DIR_OPT}'),
('a[^]', f'^(?s:.+/)?a\\[\\^\\]{_DIR_OPT}'),
('[z-a]', f'^(?s:.+/)?\\[z\\-a\\]{_DIR_OPT}'),
('a[z-a]', f'^(?s:.+/)?a\\[z\\-a\\]{_DIR_OPT}'),
]:
with self.subTest(f"p={raw_pattern!r}"):
pattern = GitIgnoreBasicPattern(raw_pattern)
self.assertIs(pattern.include, True)
self.assertEqual(pattern.regex.pattern, regex)

# The `re` module fails to compile these.
for raw_pattern in [
'[z-a]',
'a[z-a]',
]:
with self.subTest(f"p={raw_pattern!r}"):
with self.assertRaises(re_PatternError):
GitIgnoreBasicPattern(raw_pattern)

def test_c_3_unclosed(self):
"""
Test patterns with unclosed range notation.
Expand Down
16 changes: 16 additions & 0 deletions tests/test_08_gitignore_bracket_slash.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ def test_invalid_posix_after_range(self):
with self.assertRaises(GitIgnorePatternError):
factory(line, errors='raise')

def test_reversed_range(self):
for factory in (GitIgnoreBasicPattern, GitIgnoreSpecPattern):
for line in ('[z-a]', 'a[z-a]'):
with self.subTest(factory=factory.__name__, line=line):
pattern = factory(line, errors='literal')
self.assertIsNotNone(pattern.match_file(line))
self.assertIsNone(pattern.match_file(line.replace('[z-a]', 'z')))
self.assertIsNone(factory(line, errors='null').regex)
with self.assertRaises(GitIgnorePatternError):
factory(line, errors='raise')

spec = PathSpec.from_lines('gitignore', ['[z-a]'])
self.assertFalse(spec.match_file('a'))
self.assertFalse(spec.match_file('m'))
self.assertTrue(spec.match_file('[z-a]'))

def test_pattern_separator(self):
for factory in (GitIgnoreBasicPattern, GitIgnoreSpecPattern, GitWildMatchPattern):
for bracket, matched in (
Expand Down
Loading