close
Skip to content
Merged
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
9 changes: 9 additions & 0 deletions pathspec/patterns/gitignore/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ def _translate_segment_glob(
# as literal slashes by regex as defined by POSIX.
expr += pattern[i:j].replace('\\', '\\\\')

if range_error == 'raise':
try:
re.compile(expr)
except re.error as e:
raise _RangeError((
f"Invalid range notation={pattern[i:j]!r} found in "
f"pattern={pattern!r}."
)) from e

# Add regex bracket expression to regex result.
regex += expr

Expand Down
8 changes: 3 additions & 5 deletions tests/test_04_gitignore_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,16 +944,14 @@ def test_15_issue_93_c_2_invalid(self):
self.assertIs(pattern.include, None)
self.assertIs(pattern.regex, None)

# The `re` module fails to compile these.
# - NOTE: Technically, these should result in null patterns rather than
# exceptions to fully replicate Git's behavior.
for raw_pattern in [
'[z-a]',
'a[z-a]',
]:
with self.subTest(f"p={raw_pattern!r}"):
with self.assertRaises(re_PatternError):
GitIgnoreSpecPattern(raw_pattern)
pattern = GitIgnoreSpecPattern(raw_pattern)
self.assertIs(pattern.include, None)
self.assertIs(pattern.regex, None)

def test_15_issue_93_c_3_unclosed(self):
"""
Expand Down
Loading