Skip to content

fix: align Pattern pos/endpos signatures with re and add sliced-search fallback - #106

Merged
Qubitium merged 1 commit into
mainfrom
fix/re-positional-parity
Aug 24, 2026
Merged

fix: align Pattern pos/endpos signatures with re and add sliced-search fallback#106
Qubitium merged 1 commit into
mainfrom
fix/re-positional-parity

Conversation

@Qubitium

Copy link
Copy Markdown
Contributor

Problem

re.Pattern methods accept pos/endpos positionally, but pypcre's Pattern.match/search/fullmatch/finditer/findall declared them keyword-only. Drop-in re-style code therefore failed:

pcre.compile("b").search(s, 2)
# TypeError: Pattern.search() takes 2 positional arguments but 3 were given

Fix

  • Accept pos and endpos positionally on Pattern.match, search, fullmatch, finditer, and findall, matching the stdlib re.Pattern signature order; the pypcre-specific options argument remains keyword-only.
  • For minimal non-C backends whose search() lacks pos/endpos support, Pattern.search() now falls back to searching a slice (subject[pos:endpos]) and returns an internal _OffsetMatch wrapper that shifts start()/end()/span()/regs back by pos, so reported spans stay absolute in the original subject while .string, .pos, .endpos, and .re still reference the original subject/pattern.

The C backend keeps forwarding pos natively: slicing alone would change semantics for anchors (^, $), \b, and lookbehinds relative to re, since they would evaluate against the slice boundary rather than the full string.

Tests

  • New parity tests compare positional search/match/fullmatch/finditer/findall results (spans and pos) against stdlib re, including negative cases.
  • New fallback test exercises a minimal backend without pos support and asserts absolute spans, regs, pos, string, and re on the returned match.

Full suite: 832 passed, 19 skipped (test_cache_strategy_benchmark failure is pre-existing on main and timing-related).

…h fallback

Pattern.match/search/fullmatch/finditer/findall required keyword-only
pos/endpos, breaking drop-in re-style positional calls such as
pattern.search(s, 2). Accept them positionally (options stays
keyword-only) and, for minimal non-C backends whose search() lacks
pos support, fall back to searching a slice with offset tracking so
reported spans stay absolute in the original subject.
@Qubitium
Qubitium merged commit 267c059 into main Aug 24, 2026
19 checks passed
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