Skip to content

Commit 515d99c

Browse files
LiuYinCarlnedbat
LiuYinCarl
authored andcommitted
fix: unusual cases of backslash in f-string #1836
1 parent c6b3901 commit 515d99c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

coverage/phystokens.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ def _phys_tokens(toks: TokenInfos) -> TokenInfos:
5757
if last_ttext.endswith("\\"):
5858
inject_backslash = False
5959
elif ttype == token.STRING:
60-
if last_line.endswith(last_ttext + "\\\n"):
60+
if (last_line.endswith("\\\n") and
61+
last_line.rstrip(" \\\n").endswith(last_ttext)):
6162
# Deal with special cases like such code::
6263
#
63-
# a = ["aaa",\
64+
# a = ["aaa",\ # there may be zero or more blanks between "," and "\".
6465
# "bbb \
6566
# ccc"]
6667
#
@@ -69,6 +70,9 @@ def _phys_tokens(toks: TokenInfos) -> TokenInfos:
6970
# It's a multi-line string and the first line ends with
7071
# a backslash, so we don't need to inject another.
7172
inject_backslash = False
73+
elif sys.version_info >= (3, 12) and ttype == token.FSTRING_MIDDLE:
74+
if ttext.split("\n", 1)[0][-1] == "\\":
75+
inject_backslash = False
7276
if inject_backslash:
7377
# Figure out what column the backslash is in.
7478
ccol = len(last_line.split("\n")[-2]) - 1

0 commit comments

Comments
 (0)