]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129093: Fix f-string debug text sometimes getting cut off when expression contains...
authorTomas R. <tomas.roun8@gmail.com>
Wed, 22 Jan 2025 00:26:37 +0000 (01:26 +0100)
committerGitHub <noreply@github.com>
Wed, 22 Jan 2025 00:26:37 +0000 (00:26 +0000)
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 c359f2ecce01f121bda6a0b8a782b479f098a957..31d91215889460c92d3899836097285b6c879238 100644 (file)
@@ -1757,5 +1757,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 dbbb94a407c81d8186aa29189e9a37aab6e22af4..8c5ae37fa9086055b0d1f6428f013d2c2ffe1b1c 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();