Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/_pytest/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,14 @@ def _check_raw_type(

def is_fully_escaped(s: str) -> bool:
# we know we won't compile with re.VERBOSE, so whitespace doesn't need to be escaped
metacharacters = "{}()+.*?^$[]"
metacharacters = "{}()+.*?^$[]|"
return not any(
c in metacharacters and (i == 0 or s[i - 1] != "\\") for (i, c) in enumerate(s)
)


def unescape(s: str) -> str:
return re.sub(r"\\([{}()+-.*?^$\[\]\s\\])", r"\1", s)
return re.sub(r"\\([{}()+-.*?^$\[\]\s\\|])", r"\1", s)


# These classes conceptually differ from ExceptionInfo in that ExceptionInfo is tied, and
Expand Down
19 changes: 19 additions & 0 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,22 @@ def test_raises_match_compiled_regex(self) -> None:
pattern_with_flags = re.compile(r"INVALID LITERAL", re.IGNORECASE)
with pytest.raises(ValueError, match=pattern_with_flags):
int("asdf")

def test_pipe_metacharacter_not_treated_as_literal(self) -> None:
"""Regression test: | (pipe) must be recognized as a regex metacharacter.

When match='^foo|bar$' is used, is_fully_escaped should recognize this
as a real regex (not a fully-escaped literal), so that pytest does not
attempt an exact-string diff against it.
"""
from _pytest.raises import is_fully_escaped
from _pytest.raises import unescape

# Pipe is a metacharacter and should not be treated as escaped
assert not is_fully_escaped("foo|bar")

# Escaped pipe should be treated as a literal
assert is_fully_escaped(r"foo\|bar")

# unescape should remove the backslash from an escaped pipe
assert unescape(r"foo\|bar") == "foo|bar"