Fix: is_fully_escaped does not handle consecutive backslashes correctly#14393
Open
EternalRights wants to merge 3 commits intopytest-dev:mainfrom
Open
Fix: is_fully_escaped does not handle consecutive backslashes correctly#14393EternalRights wants to merge 3 commits intopytest-dev:mainfrom
EternalRights wants to merge 3 commits intopytest-dev:mainfrom
Conversation
The function only checked if the character immediately before a regex metacharacter was a backslash, but did not count how many consecutive backslashes preceded it. When two backslashes appear before a metacharacter (e.g. r'\\.'), the first escapes the second, leaving the metacharacter unescaped. The function incorrectly reported such strings as fully escaped. Fix by counting consecutive backslashes: an even count means the metacharacter is not escaped, an odd count means it is. Closes pytest-dev#14392
4 backslashes + pipe: even count means pipe is not escaped, so is_fully_escaped should return False, not True.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #14392
Description
The
is_fully_escapedfunction inraises.pyonly checked whether the character immediately before a regex metacharacter was a backslash. It did not count how many consecutive backslashes preceded the metacharacter.In regex, backslashes escape each other in pairs. So
\\.(two backslashes then a dot) means an escaped backslash followed by an unescaped dot. Butis_fully_escaped(r\\.)was incorrectly returningTruebecause it only checked ifs[i-1]was\.This affects
pytest.raises(match=...)error messages: when the match pattern contains escaped backslashes before a metacharacter, the function incorrectly considers the pattern fully escaped and skips showing a regex diff on match failure.Fix: Count consecutive backslashes before each metacharacter. Even count means the metacharacter is not escaped; odd count means it is.
Type of Change
Checklist