Skip to content

Commit

Permalink
[3.13] gh-129093: Fix f-string debug text sometimes getting cut off w…
Browse files Browse the repository at this point in the history
…hen 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 767cf70)

Co-authored-by: Tomas R <tomas.roun8@gmail.com>
  • Loading branch information
miss-islington and tomasr8 authored Jan 22, 2025
1 parent 0ddcb61 commit a379749
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
18 changes: 18 additions & 0 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix f-strings such as ``f'{expr=}'`` sometimes not displaying the full
expression when the expression contains ``!=``.
4 changes: 1 addition & 3 deletions Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a379749

Please sign in to comment.