From f6516a740a63571674a3b77c36d60619110d2951 Mon Sep 17 00:00:00 2001 From: anticipate218 <3481402509@qq.com> Date: Thu, 12 Nov 2026 15:57:10 +0800 Subject: [PATCH] Preserve custom regex matching when selecting native backends Keep custom RegexPattern matching on the simple backend during automatic selection. Reject it through existing explicit native compatibility checks, including instance delegation, while preserving native execution for standard and inactive patterns. Assisted-by: OpenAI Codex --- CHANGES.rst | 1 + CHANGES_1.in.rst | 1 + README-dist.rst | 4 + README.rst | 3 + pathspec/_backends/_utils.py | 8 ++ tests/test_10_custom_regex_matching.py | 119 +++++++++++++++++++++++++ 6 files changed, 136 insertions(+) create mode 100644 tests/test_10_custom_regex_matching.py diff --git a/CHANGES.rst b/CHANGES.rst index aa26a67..d12bb2b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -24,6 +24,7 @@ New features: Bug fixes: +- Preserve overridden ``RegexPattern.match_file()`` implementations when selecting a backend. - Hyperscan backend no longer fails on multibyte UTF-8 characters. - `Pull #123`_: Ignore invalid gitignore bracket ranges for `GitIgnoreSpec`. - `Pull #128`_: Support POSIX character classes (e.g. `[[:alpha:]]`) in gitignore bracket expressions. diff --git a/CHANGES_1.in.rst b/CHANGES_1.in.rst index 657c12e..75a64c3 100644 --- a/CHANGES_1.in.rst +++ b/CHANGES_1.in.rst @@ -21,6 +21,7 @@ New features: Bug fixes: +- Preserve overridden ``RegexPattern.match_file()`` implementations when selecting a backend. - Hyperscan backend no longer fails on multibyte UTF-8 characters. - `Pull #123`_: Ignore invalid gitignore bracket ranges for `GitIgnoreSpec`. - `Pull #128`_: Support POSIX character classes (e.g. `[[:alpha:]]`) in gitignore bracket expressions. diff --git a/README-dist.rst b/README-dist.rst index f567c38..ce346dc 100644 --- a/README-dist.rst +++ b/README-dist.rst @@ -130,6 +130,9 @@ the patterns they generate themselves. Precompiled expressions from other regex libraries keep their original engine when using "best" or "simple". Explicit native backends reject these expressions because their compile options cannot be preserved when rebuilding the pattern. +Patterns overriding ``match_file()`` also use "simple" during automatic backend +selection and are rejected by explicit native backends, preserving their custom +matching behavior. See `benchmarks_backends.md`_ for comparisons between native Python regular expressions and the optional backends. @@ -251,6 +254,7 @@ Major changes: Bug fixes: +- Preserve overridden ``RegexPattern.match_file()`` implementations when selecting a backend. - Avoid following symbolic link targets when checking file types in ``iter_tree_files(follow_links=False)``. - `Pull #123`_: Ignore invalid gitignore bracket ranges for `GitIgnoreSpec`. diff --git a/README.rst b/README.rst index 9b81118..27f9f57 100644 --- a/README.rst +++ b/README.rst @@ -125,6 +125,9 @@ ignored. Precompiled expressions from other regex libraries keep their original engine when using "best" or "simple". Explicit native backends reject these expressions because their compile options cannot be preserved when rebuilding the pattern. +Patterns overriding ``match_file()`` also use "simple" during automatic backend +selection and are rejected by explicit native backends, preserving their custom +matching behavior. See `benchmarks_backends.md`_ for comparisons between native Python regular expressions and the optional backends. diff --git a/pathspec/_backends/_utils.py b/pathspec/_backends/_utils.py index 66fd046..0e79575 100644 --- a/pathspec/_backends/_utils.py +++ b/pathspec/_backends/_utils.py @@ -122,6 +122,14 @@ def is_native_compatible(pattern: Pattern) -> bool: # Custom patterns can only be matched by calling them. return False + match_file = pattern.match_file + if ( + getattr(match_file, '__func__', None) is not RegexPattern.match_file + or getattr(match_file, '__self__', None) is not pattern + ): + # Recompiling the regex would bypass a custom matching implementation. + return False + regex = pattern.regex if not isinstance(regex, re.Pattern): # The pattern was not compiled by Python's *re* module. Its flags and diff --git a/tests/test_10_custom_regex_matching.py b/tests/test_10_custom_regex_matching.py new file mode 100644 index 0000000..2286d08 --- /dev/null +++ b/tests/test_10_custom_regex_matching.py @@ -0,0 +1,119 @@ +"""Custom regex matching must not change when a native backend is installed.""" + +import re +import unittest +from unittest.mock import patch + +from pathspec import GitIgnoreSpec, PathSpec, RegexPattern +from pathspec.patterns.gitignore.spec import GitIgnoreSpecPattern + +from .util import require_backend + +SPEC_CLASSES: tuple[type[PathSpec], ...] = (PathSpec, GitIgnoreSpec) + + +class PublicPythonPattern(RegexPattern): + def match_file(self, file): + if file.startswith('private/'): + return None + return super().match_file(file) + + +class InheritedPublicPythonPattern(PublicPythonPattern): + pass + + +class OrdinaryRegexPattern(RegexPattern): + pass + + +class CustomRegexMatchingTest(unittest.TestCase): + def test_automatic_backend_preserves_override(self): + for backend in ('re2', 'hyperscan'): + for spec_class in SPEC_CLASSES: + for pattern_class in (PublicPythonPattern, InheritedPublicPythonPattern): + for selection in (None, 'best'): + with self.subTest(backend=backend, spec_class=spec_class, + pattern_class=pattern_class, selection=selection): + require_backend(backend) + pattern = pattern_class(re.compile(r'.*\.py$'), include=True) + with patch('pathspec._backends.agg._BEST_BACKEND', backend): + spec = spec_class([pattern], backend=selection) + self.assertEqual( + list(spec.match_files(['public/tool.py', 'private/secret.py'])), + ['public/tool.py'], + ) + self.assertEqual(spec.check_file('public/tool.py').index, 0) + self.assertFalse(spec.match_file('private/secret.py')) + + def test_explicit_native_backend_rejects_override(self): + for backend in ('re2', 'hyperscan'): + for spec_class in SPEC_CLASSES: + with self.subTest(backend=backend, spec_class=spec_class): + require_backend(backend) + pattern = PublicPythonPattern(re.compile(r'.*\.py$'), include=True) + with self.assertRaisesRegex(ValueError, 'Use the simple backend instead'): + spec_class([pattern], backend=backend) + + def test_combination_preserves_custom_exclusion(self): + for backend in ('re2', 'hyperscan'): + for spec_class in SPEC_CLASSES: + with self.subTest(backend=backend, spec_class=spec_class): + require_backend(backend) + with patch('pathspec._backends.agg._BEST_BACKEND', backend): + original = spec_class([RegexPattern(re.compile(r'.*\.py$'), True)]) + exclusion = spec_class([PublicPythonPattern(re.compile(r'.*\.py$'), False)]) + combined = original + exclusion + original += exclusion + for spec in (combined, original): + self.assertFalse(spec.match_file('public/tool.py')) + self.assertTrue(spec.match_file('private/secret.py')) + self.assertEqual(spec.check_file('public/tool.py').index, 1) + self.assertEqual(spec.check_file('private/secret.py').index, 0) + + def test_inactive_override_keeps_native_backend(self): + for backend in ('re2', 'hyperscan'): + for spec_class in SPEC_CLASSES: + with self.subTest(backend=backend, spec_class=spec_class): + require_backend(backend) + patterns = [PublicPythonPattern(None), RegexPattern(re.compile(r'.*\.py$'), True)] + with patch('pathspec._backends.agg._BEST_BACKEND', backend): + spec = spec_class(patterns) + self.assertFalse(type(spec._backend).__name__.startswith('Simple')) + self.assertTrue(spec.match_file('private/secret.py')) + self.assertEqual(spec.check_file('private/secret.py').index, 1) + + def test_inherited_default_matching_keeps_native_backend(self): + for backend in ('re2', 'hyperscan'): + for spec_class in SPEC_CLASSES: + for pattern_class in (OrdinaryRegexPattern, GitIgnoreSpecPattern): + with self.subTest(backend=backend, spec_class=spec_class, pattern_class=pattern_class): + require_backend(backend) + pattern = pattern_class(re.compile(r'.*\.py$'), include=True) + with patch('pathspec._backends.agg._BEST_BACKEND', backend): + spec = spec_class([pattern]) + self.assertFalse(type(spec._backend).__name__.startswith('Simple')) + self.assertTrue(spec.match_file('public/tool.py')) + + def test_instance_override(self): + for backend in ('re2', 'hyperscan'): + with self.subTest(backend=backend): + require_backend(backend) + pattern = OrdinaryRegexPattern(re.compile(r'.*\.py$'), include=True) + pattern.match_file = lambda file: None + with patch('pathspec._backends.agg._BEST_BACKEND', backend): + spec = PathSpec([pattern]) + self.assertFalse(spec.match_file('public/tool.py')) + + def test_instance_delegating_to_another_pattern(self): + for backend in ('re2', 'hyperscan'): + for spec_class in SPEC_CLASSES: + with self.subTest(backend=backend, spec_class=spec_class): + require_backend(backend) + pattern = OrdinaryRegexPattern(re.compile(r'.*\.py$'), include=True) + other = RegexPattern(re.compile(r'.*\.txt$'), include=True) + pattern.match_file = other.match_file + with patch('pathspec._backends.agg._BEST_BACKEND', backend): + spec = spec_class([pattern]) + self.assertTrue(spec.match_file('readme.txt')) + self.assertFalse(spec.match_file('tool.py'))