Skip to content

gh-129093: Fix f-string debug text sometimes getting cut off when expression contains ! #129159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1757,5 +1757,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
Loading