Fix octal escapes in re module

Merged
RO
2mo ago
+101-29

In the re module, fix the handling of specific cases of \number as explained in CPython documentation:

If the first digit of number is 0, or number is 3 octal digits long, it will not be interpreted as a group match, but as the character with octal value number.

Three-digit octal escapes above 0o377 (255) raise PatternError like CPython does. Getting the position right in the error message was the difficult part.

AI disclosure: LLMs have been used to detect the issue and help prepare this PR.


Summary by cubic

Fixes octal escape handling in re replacement templates so \0 and three-digit octal escapes like \123 are translated as characters, and escapes above 0o377 raise PatternError with the correct position. Validation now also runs when a negative count skips substitution, matching CPython.