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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ New features:

Bug fixes:

- Preserve subclass state when shallow-copying regular expression patterns, including subclasses without an include parameter.

- Preserve slashes inside gitignore bracket expressions while excluding path separators from their matches.
- Hyperscan backend no longer fails on multibyte UTF-8 characters.
- `Pull #123`_: Ignore invalid gitignore bracket ranges for `GitIgnoreSpec`.
Expand Down
2 changes: 2 additions & 0 deletions CHANGES_1.in.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ New features:

Bug fixes:

- Preserve subclass state when shallow-copying regular expression patterns, including subclasses without an include parameter.

- Preserve slashes inside gitignore bracket expressions while excluding path separators from their matches.
- Hyperscan backend no longer fails on multibyte UTF-8 characters.
- `Pull #123`_: Ignore invalid gitignore bracket ranges for `GitIgnoreSpec`.
Expand Down
17 changes: 0 additions & 17 deletions pathspec/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@
from typing import (
Any,
Optional, # Replaced by `X | None` in 3.10.
TypeVar,
Union) # Replaced by `X | Y` in 3.10.

from ._typing import (
AnyStr, # Removed in 3.18.
deprecated, # Added in 3.13.
override) # Added in 3.12.

RegexPatternSelf = TypeVar("RegexPatternSelf", bound='RegexPattern')
"""
:class:`.RegexPattern` self type hint to support Python v<3.11 using PEP 673
recommendation.
"""

class Pattern(object):
"""
The :class:`Pattern` class is the abstract definition of a pattern.
Expand Down Expand Up @@ -179,16 +172,6 @@ def __str__(self) -> str:
"""
return str(self.pattern or "")

def __copy__(self: RegexPatternSelf) -> RegexPatternSelf:
"""
Performa a shallow copy of the pattern.

Returns the copy (:class:`RegexPattern`).
"""
other = self.__class__(self.regex, self.include)
other.pattern = self.pattern
return other

def __eq__(self, other: object) -> bool:
"""
Tests the equality of this regex pattern with *other* (:class:`RegexPattern`)
Expand Down
78 changes: 78 additions & 0 deletions tests/test_11_pattern_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""Test shallow copying of regular expression patterns and their subclasses."""

import re
from copy import copy
from unittest import TestCase

from pathspec import RegexPattern
from pathspec.patterns.gitignore.basic import GitIgnoreBasicPattern
from pathspec.patterns.gitignore.spec import GitIgnoreSpecPattern


class SingleArgumentPattern(RegexPattern):
def __init__(self, pattern):
super().__init__(pattern)


class StatefulPattern(RegexPattern):
def __init__(self, pattern, include=None):
super().__init__(pattern, include)
self.metadata = []


class SlottedPattern(RegexPattern):
__slots__ = ('metadata', '__private', 'unset')

def __init__(self, pattern, include=None):
super().__init__(pattern, include)
self.metadata = []
self.__private = []


class PatternCopyTest(TestCase):

def assert_pattern_copy(self, pattern):
other = copy(pattern)
self.assertIsNot(other, pattern)
self.assertIs(type(other), type(pattern))
self.assertIs(other.pattern, pattern.pattern)
self.assertIs(other.regex, pattern.regex)
self.assertIs(other.include, pattern.include)
self.assertEqual(
bool(other.match_file('abc.txt')), bool(pattern.match_file('abc.txt')),
)
return other

def test_builtin_patterns(self):
for factory in (RegexPattern, GitIgnoreBasicPattern, GitIgnoreSpecPattern):
for text in ('abc.txt', None):
with self.subTest(factory=factory, text=text):
self.assert_pattern_copy(factory(text))

def test_compiled_pattern_and_include(self):
for include in (True, False, None):
with self.subTest(include=include):
pattern = RegexPattern(re.compile('abc.txt', re.IGNORECASE), include)
other = self.assert_pattern_copy(pattern)
self.assertIsNotNone(other.match_file('ABC.TXT'))

def test_subclass_without_include_parameter(self):
self.assert_pattern_copy(SingleArgumentPattern('abc.txt'))

def test_subclass_dictionary_state(self):
pattern = StatefulPattern('abc.txt')
pattern.metadata.append('retained')
other = self.assert_pattern_copy(pattern)
self.assertIs(other.metadata, pattern.metadata)
self.assertIsNot(other.__dict__, pattern.__dict__)

def test_subclass_slot_state(self):
pattern = SlottedPattern('abc.txt')
pattern.metadata.append('retained')
other = self.assert_pattern_copy(pattern)
self.assertIs(other.metadata, pattern.metadata)
self.assertIs(
getattr(other, '_SlottedPattern__private'),
getattr(pattern, '_SlottedPattern__private'),
)
self.assertFalse(hasattr(other, 'unset'))
Loading