Skip to content

Preserve externally compiled regex flags during backend selection - #151

Open
Master-Norna wants to merge 1 commit into
cpburnz:masterfrom
Master-Norna:fix/preserve-compiled-regex-flags
Open

Master-Norna wants to merge 1 commit into
cpburnz:masterfrom
Master-Norna:fix/preserve-compiled-regex-flags

Conversation

@Master-Norna

Copy link
Copy Markdown

Both native backends rebuild RegexPattern.regex.pattern without its compile-time flags. As a result, installing an optional backend can silently change matching results for precompiled Python regexes.

This patch falls back to simple for None/best when an active standard-library re.Pattern has flags that cannot be reconstructed from its expression. Explicit re2/hyperscan selection raises ValueError with guidance to use backend='simple'. Comparing against the expression's own flags preserves existing inline-flag behavior; ordinary gitignore patterns keep their current native backend. This does not attempt to make all Python and native regex syntax or Unicode semantics equivalent.

It also makes += leave the spec unchanged if rebuilding the backend is rejected.

Regression tests cover I/M/S/X/A and combinations, Unicode case folding, verbose comments with invalid bare regex syntax, warning-as-error handling without leaking probe warnings, mixed inclusion/exclusion and indices, inline flags, no-op rules, GitIgnoreSpec directory precedence, and rejected in-place additions.

Validation on Windows / CPython 3.12.14:

  • Full unittest discovery with both native libraries: 231 tests run, 14 existing Windows symlink skips, no failures/errors.
  • tox -e ci-base,ci-re2,ci-hyperscan: all three isolated environments passed.
  • Installed-wheel smoke tests in each tox environment: passed.
  • Full discovery with -W error::FutureWarning, strict Sphinx build, and sdist/wheel build: passed.
  • Mypy and Pyright each report the same two pre-existing _typing.py errors on the unmodified base and the patch, with no additional diagnostics.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant