Skip to content

Commit

Permalink
fix: unusual cases of backslash in f-string nedbat#1836
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYinCarl authored and nedbat committed Sep 4, 2024
1 parent c6b3901 commit 515d99c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions coverage/phystokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ def _phys_tokens(toks: TokenInfos) -> TokenInfos:
if last_ttext.endswith("\\"):
inject_backslash = False
elif ttype == token.STRING:
if last_line.endswith(last_ttext + "\\\n"):
if (last_line.endswith("\\\n") and
last_line.rstrip(" \\\n").endswith(last_ttext)):
# Deal with special cases like such code::
#
# a = ["aaa",\
# a = ["aaa",\ # there may be zero or more blanks between "," and "\".
# "bbb \
# ccc"]
#
Expand All @@ -69,6 +70,9 @@ def _phys_tokens(toks: TokenInfos) -> TokenInfos:
# It's a multi-line string and the first line ends with
# a backslash, so we don't need to inject another.
inject_backslash = False
elif sys.version_info >= (3, 12) and ttype == token.FSTRING_MIDDLE:
if ttext.split("\n", 1)[0][-1] == "\\":
inject_backslash = False
if inject_backslash:
# Figure out what column the backslash is in.
ccol = len(last_line.split("\n")[-2]) - 1
Expand Down

0 comments on commit 515d99c

Please sign in to comment.