Skip to content

Fix: is_fully_escaped does not handle consecutive backslashes correctly#14393

Open
EternalRights wants to merge 4 commits intopytest-dev:mainfrom
EternalRights:fix-is-fully-escaped-consecutive-backslashes
Open

Fix: is_fully_escaped does not handle consecutive backslashes correctly#14393
EternalRights wants to merge 4 commits intopytest-dev:mainfrom
EternalRights:fix-is-fully-escaped-consecutive-backslashes

Conversation

@EternalRights
Copy link
Copy Markdown

Closes #14392

Description

The is_fully_escaped function in raises.py only 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. But is_fully_escaped(r\\.) was incorrectly returning True because it only checked if s[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

  • Bug fix

Checklist

  • Added tests for changed functionality
  • Added changelog entry
  • No documentation update needed (internal helper function)

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
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Apr 18, 2026
4 backslashes + pipe: even count means pipe is not escaped, so is_fully_escaped should return False, not True.
Copy link
Copy Markdown

@alokshukla631 alokshukla631 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Counting the run of preceding backslashes matches regex escaping rules much better than the old single-character check, and the added cases make the intended behavior easy to follow.

@EternalRights
Copy link
Copy Markdown
Author

@alokshukla631
Thank you for your review and feedback!
I've updated the PR branch to stay in sync with the main branch, and I'm now awaiting approval from a maintainer with write access.
If you have any further suggestions, feel free to let me know.

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

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix: is_fully_escaped does not handle consecutive backslashes correctly

2 participants