File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ Fixed a bug in :func: `pytest.raises(match=...) <pytest.raises> ` "fully escaped" detection, causing the regex diff display to be shown in some instances when the raw string diff display should be shown instead.
Original file line number Diff line number Diff line change @@ -345,9 +345,10 @@ def _check_raw_type(
345345def is_fully_escaped (s : str ) -> bool :
346346 # we know we won't compile with re.VERBOSE, so whitespace doesn't need to be escaped
347347 metacharacters = "{}()+.*?^$[]|"
348- return not any (
349- c in metacharacters and (i == 0 or s [i - 1 ] != "\\ " ) for (i , c ) in enumerate (s )
350- )
348+ # Strip all escape sequences (backslash + any char), then check if any
349+ # metacharacter remains unescaped in the resulting string.
350+ stripped = re .sub (r"\\." , "" , s )
351+ return not any (c in metacharacters for c in stripped )
351352
352353
353354def unescape (s : str ) -> str :
Original file line number Diff line number Diff line change @@ -444,3 +444,19 @@ def test_pipe_is_treated_as_regex_metacharacter(self) -> None:
444444 assert not is_fully_escaped ("foo|bar" )
445445 assert is_fully_escaped (r"foo\|bar" )
446446 assert unescape (r"foo\|bar" ) == "foo|bar"
447+
448+ def test_consecutive_backslashes_in_escape_check (self ) -> None :
449+ """Consecutive backslashes escape each other, leaving the metachar unescaped."""
450+ from _pytest .raises import is_fully_escaped
451+
452+ # r"\." -> one backslash escapes the dot -> fully escaped
453+ assert is_fully_escaped (r"\." )
454+ # r"\\." -> two backslashes: the first escapes the second, dot is unescaped
455+ assert not is_fully_escaped (r"\\." )
456+ # r"\\\." -> three backslashes: pair escapes pair, last escapes dot -> fully escaped
457+ assert is_fully_escaped (r"\\\." )
458+ # Same idea with pipe metachar
459+ # "\\\\|" is the string \\| (2 backslashes + pipe): even count, pipe is unescaped
460+ assert not is_fully_escaped ("\\ \\ |" )
461+ # r"\\\\|" is the string \\\\| (4 backslashes + pipe): even count, pipe is unescaped
462+ assert not is_fully_escaped (r"\\\\|" )
You can’t perform that action at this time.
0 commit comments