https://github.com/python/cpython/commit/a3797492179c249417a06d2499a7d535d453ac2c
commit: a3797492179c249417a06d2499a7d535d453ac2c
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: pablogsal <[email protected]>
date: 2025-01-22T00:50:22Z
summary:

[3.13] gh-129093: Fix f-string debug text sometimes getting cut off when 
expression contains `!` (GH-129159) (#129163)

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 <[email protected]>

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/lexer/lexer.c

diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index c747bbedc6dba8..e77f76fc55b44a 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -1758,5 +1758,23 @@ def get_code(s):
         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
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/lexer/lexer.c b/Parser/lexer/lexer.c
index 8c868593f944c8..3ced1be0b003b8 100644
--- a/Parser/lexer/lexer.c
+++ b/Parser/lexer/lexer.c
@@ -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();

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to