From 01779db1fea8d98acb0a15ccf229768c5851b64a Mon Sep 17 00:00:00 2001 From: LiuYinCarl Date: Mon, 26 Aug 2024 05:18:51 +0000 Subject: [PATCH 1/6] fix backslash in f-string --- coverage/phystokens.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/coverage/phystokens.py b/coverage/phystokens.py index 9fc36ecda..7f113a9d1 100644 --- a/coverage/phystokens.py +++ b/coverage/phystokens.py @@ -69,6 +69,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 From a1aa6b8b79ec9c3bd639c2bf20f3148327d229a7 Mon Sep 17 00:00:00 2001 From: Kenshin <1427518212@qq.com> Date: Tue, 27 Aug 2024 11:48:53 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20phystokens.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix https://github.com/nedbat/coveragepy/pull/1838#issuecomment-2311174029 --- coverage/phystokens.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coverage/phystokens.py b/coverage/phystokens.py index 7f113a9d1..bfc86b570 100644 --- a/coverage/phystokens.py +++ b/coverage/phystokens.py @@ -57,10 +57,10 @@ 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_text): # Deal with special cases like such code:: # - # a = ["aaa",\ + # a = ["aaa",\ # there may be zero or more blanks between "," and "\". # "bbb \ # ccc"] # From b7542923e4b00876f3bcc5af0d7b79f481f4518b Mon Sep 17 00:00:00 2001 From: Kenshin <1427518212@qq.com> Date: Tue, 27 Aug 2024 11:55:14 +0800 Subject: [PATCH 3/6] update phystokens.py fix typo. --- coverage/phystokens.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coverage/phystokens.py b/coverage/phystokens.py index bfc86b570..dd660552e 100644 --- a/coverage/phystokens.py +++ b/coverage/phystokens.py @@ -57,7 +57,7 @@ def _phys_tokens(toks: TokenInfos) -> TokenInfos: if last_ttext.endswith("\\"): inject_backslash = False elif ttype == token.STRING: - if last_line.endswith("\\\n") and last_line.rstrip(" \\\n").endswith(last_text): + if last_line.endswith("\\\n") and last_line.rstrip(" \\\n").endswith(last_ttext): # Deal with special cases like such code:: # # a = ["aaa",\ # there may be zero or more blanks between "," and "\". From 54835a59289e1dcf7d2c0febc4c0e4ce8890018a Mon Sep 17 00:00:00 2001 From: Kenshin <1427518212@qq.com> Date: Tue, 27 Aug 2024 12:06:26 +0800 Subject: [PATCH 4/6] update phystokens.py fix code lint. --- coverage/phystokens.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/coverage/phystokens.py b/coverage/phystokens.py index dd660552e..0c344b0af 100644 --- a/coverage/phystokens.py +++ b/coverage/phystokens.py @@ -57,7 +57,8 @@ def _phys_tokens(toks: TokenInfos) -> TokenInfos: if last_ttext.endswith("\\"): inject_backslash = False elif ttype == token.STRING: - if last_line.endswith("\\\n") and last_line.rstrip(" \\\n").endswith(last_ttext): + if (last_line.endswith("\\\n") and + last_line.rstrip(" \\\n").endswith(last_ttext)): # Deal with special cases like such code:: # # a = ["aaa",\ # there may be zero or more blanks between "," and "\". From 1b252849bda78bd47f1936a786dee0ad8497bb57 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 4 Sep 2024 08:44:19 -0400 Subject: [PATCH 5/6] test: add a test for #1836, by Marco Ricci --- tests/test_html.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_html.py b/tests/test_html.py index d5cc0f301..01b81f623 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -1203,6 +1203,36 @@ def test_bug_1828(self) -> None: '3 ccc"]', ] + @pytest.mark.parametrize( + "leader", ["", "f", "r", "fr", "rf"], + ids=["string", "f-string", "raw_string", "f-raw_string", "raw_f-string"] + ) + def test_bug_1836(self, leader) -> None: + # https://github.com/nedbat/coveragepy/issues/1836 + self.make_file("py312_fstrings.py", f"""\ + prog_name = 'bug.py' + err_msg = {leader}'''\\ + {{prog_name}}: ERROR: This is the first line of the error. + {{prog_name}}: ERROR: This is the second line of the error. + \\ + {{prog_name}}: ERROR: This is the third line of the error. + ''' + """) + + cov = coverage.Coverage() + py312_fstrings = self.start_import_stop(cov, "py312_fstrings") + cov.html_report(py312_fstrings) + + assert self.get_html_report_text_lines("py312_fstrings.py") == [ + "1" + "prog_name = 'bug.py'", + "2" + f"err_msg = {leader}'''\\", + "3" + "{prog_name}: ERROR: This is the first line of the error.", + "4" + "{prog_name}: ERROR: This is the second line of the error.", + "5" + "\\", + "6" + "{prog_name}: ERROR: This is the third line of the error.", + "7" + "'''", + ] + def test_unicode(self) -> None: surrogate = "\U000e0100" From 2ee47df9d7cfc89422dce4da94c87dcc75b2fbb9 Mon Sep 17 00:00:00 2001 From: LiuYinCarl Date: Wed, 4 Sep 2024 12:56:30 +0000 Subject: [PATCH 6/6] fix type warnning --- tests/test_html.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_html.py b/tests/test_html.py index 01b81f623..5b5e3b86c 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -1207,7 +1207,7 @@ def test_bug_1828(self) -> None: "leader", ["", "f", "r", "fr", "rf"], ids=["string", "f-string", "raw_string", "f-raw_string", "raw_f-string"] ) - def test_bug_1836(self, leader) -> None: + def test_bug_1836(self, leader: str) -> None: # https://github.com/nedbat/coveragepy/issues/1836 self.make_file("py312_fstrings.py", f"""\ prog_name = 'bug.py'