Skip to content

Fix: is_fully_escaped does not handle consecutive backslashes correctly #14392

@EternalRights

Description

@EternalRights

Bug

The function \is_fully_escaped\ in \src/_pytest/raises.py\ only checks whether the character immediately before a regex metacharacter is a backslash. It does not count how many consecutive backslashes precede the metacharacter.

When two backslashes appear before a metacharacter (e.g.
'\\.'), the first backslash escapes the second, so the metacharacter is left unescaped. But \is_fully_escaped\ sees a backslash before the dot and incorrectly reports the string as fully escaped.

Impact

This affects \pytest.raises(match=...)\ when the match pattern contains escaped backslashes followed by a regex metacharacter. The match failure message may skip showing a regex diff and instead show a less helpful message.

Reproduction

\\python
from _pytest.raises import is_fully_escaped

Should be False: two backslashes escape each other, leaving . unescaped

is_fully_escaped(r'\\.') # Returns True (wrong)

Should be False: four backslashes = two escaped pairs, | unescaped

is_fully_escaped('\\\\|') # Returns True (wrong)
\\

Expected behavior

Count consecutive backslashes. If the count is even, the metacharacter is not escaped. If odd, it is escaped.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions