[3.12] gh-129093: Fix f-string debug text sometimes getting cut off when expression contains ! (#129164)
https://github.com/python/cpython/commit/e577ff4ce47034f5637a048af57145e03bf... commit: e577ff4ce47034f5637a048af57145e03bf1f64c branch: 3.12 author: Pablo Galindo Salgado <Pablogsal@gmail.com> committer: pablogsal <Pablogsal@gmail.com> date: 2025-01-22T00:47:20Z summary: [3.12] gh-129093: Fix f-string debug text sometimes getting cut off when expression contains ! (#129164) files: A Misc/NEWS.d/next/Core_and_Builtins/2025-01-21-23-35-41.gh-issue-129093.0rvETC.rst M Lib/test/test_fstring.py M Parser/tokenizer.c diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index da1ab4ef5853e6..6cba2e8616eb72 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -1875,6 +1875,24 @@ def test_syntax_warning_infinite_recursion_in_file(self): self.assertIn(rb"\1", stdout) self.assertEqual(len(stderr.strip().splitlines()), 2) + def test_gh129093(self): + self.assertEqual(f'{1==2=}', '1==2=False') + self.assertEqual(f'{1 == 2=}', '1 == 2=False') + self.assertEqual(f'{1!=2=}', '1!=2=True') + self.assertEqual(f'{1 != 2=}', '1 != 2=True') + + self.assertEqual(f'{(1) != 2=}', '(1) != 2=True') + self.assertEqual(f'{(1*2) != (3)=}', '(1*2) != (3)=True') + + self.assertEqual(f'{1 != 2 == 3 != 4=}', '1 != 2 == 3 != 4=False') + self.assertEqual(f'{1 == 2 != 3 == 4=}', '1 == 2 != 3 == 4=False') + + self.assertEqual(f'{f'{1==2=}'=}', "f'{1==2=}'='1==2=False'") + self.assertEqual(f'{f'{1 == 2=}'=}', "f'{1 == 2=}'='1 == 2=False'") + self.assertEqual(f'{f'{1!=2=}'=}', "f'{1!=2=}'='1!=2=True'") + self.assertEqual(f'{f'{1 != 2=}'=}', "f'{1 != 2=}'='1 != 2=True'") + if __name__ == "__main__": + unittest.main() diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-01-21-23-35-41.gh-issue-129093.0rvETC.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-01-21-23-35-41.gh-issue-129093.0rvETC.rst new file mode 100644 index 00000000000000..067d52eff2da1e --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-01-21-23-35-41.gh-issue-129093.0rvETC.rst @@ -0,0 +1,2 @@ +Fix f-strings such as ``f'{expr=}'`` sometimes not displaying the full +expression when the expression contains ``!=``. diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 9e0dee8cc383d6..7ba717d909124e 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -480,9 +480,7 @@ static int update_fstring_expr(struct tok_state *tok, char cur) { case '}': case '!': case ':': - if (tok_mode->last_expr_end == -1) { - tok_mode->last_expr_end = strlen(tok->start); - } + tok_mode->last_expr_end = strlen(tok->start); break; default: Py_UNREACHABLE();
participants (1)
-
pablogsal