]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-129093: Fix f-string debug text sometimes getting cut off when expression...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 22 Jan 2025 00:50:22 +0000 (01:50 +0100)
committerGitHub <noreply@github.com>
Wed, 22 Jan 2025 00:50:22 +0000 (00:50 +0000)
gh-129093: Fix f-string debug text sometimes getting cut off when expression contains `!` (GH-129159)
(cherry picked from commit 767cf708449fbf13826d379ecef64af97d779510)

Co-authored-by: Tomas R <tomas.roun8@gmail.com>
Lib/test/test_fstring.py
Misc/NEWS.d/next/Core_and_Builtins/2025-01-21-23-35-41.gh-issue-129093.0rvETC.rst [new file with mode: 0644]
Parser/lexer/lexer.c

index c747bbedc6dba88f603a2926959a6c715883965b..e77f76fc55b44ad0e65879143f270513e2ec691a 100644 (file)
@@ -1758,5 +1758,23 @@ print(f'''{{
         for s in ["", "some string"]:
             self.assertEqual(get_code(f"'{s}'"), get_code(f"f'{s}'"))
 
+    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 (file)
index 0000000..067d52e
--- /dev/null
@@ -0,0 +1,2 @@
+Fix f-strings such as ``f'{expr=}'`` sometimes not displaying the full
+expression when the expression contains ``!=``.
index 8c868593f944c8cb4bdc7d624b1056d2cad56a2b..3ced1be0b003b822fe3cca419152cf9c247b9273 100644 (file)
@@ -212,9 +212,7 @@ _PyLexer_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();