Skip to content

Fix null escape at the end of a replace template - #220

Merged
facelessuser merged 1 commit into
facelessuser:masterfrom
abhishekcpp007:fix/null-escape-at-template-end
Nov 16, 2026
Merged

facelessuser merged 1 commit into
facelessuser:masterfrom
abhishekcpp007:fix/null-escape-at-template-end

Conversation

@abhishekcpp007

Copy link
Copy Markdown
Contributor

A null escape at the very end of a replace template raises instead of giving \x00:

>>> import re
>>> from backrefs import bre
>>> p = re.compile('(Test)')
>>> re.sub(p, r'\1\0', 'Test')
'Test\x00'
>>> bre.compile_replace(p, r'\1\0')(p.match('Test'))
backrefs._bre_parse.PatternError: Unmatched '<' at 4!

In get_octal, zero_count = len(value) sits after the leading-zero loop, so it's skipped when next(i) hits the end of the template. \0 and \00 then aren't treated as octal. The count now goes up inside the loop. Same change in bregex.

@gir-bot gir-bot added S: needs-review Needs to be reviewed and/or approved. C: bre Related to backrefs in re library. C: bregex Related to backrefs in regex library. C: docs Related to documentation. C: tests Related to testing. labels Nov 15, 2026
@facelessuser

Copy link
Copy Markdown
Owner

@gir-bot lgtm

@gir-bot gir-bot added S: approved The pull request is ready to be merged. and removed S: needs-review Needs to be reviewed and/or approved. labels Nov 16, 2026
@facelessuser
facelessuser merged commit 9a02917 into facelessuser:master Nov 16, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C: bre Related to backrefs in re library. C: bregex Related to backrefs in regex library. C: docs Related to documentation. C: tests Related to testing. S: approved The pull request is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants