Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[3.13] gh-130618: Fix parser error when using lambdas inside f-strings (GH-130638) #130642

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 1 commit into from
Mar 1, 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
[3.13] gh-130618: Fix parser error when using lambdas inside f-strings (
GH-130638)

(cherry picked from commit e06bebb)

Co-authored-by: Pablo Galindo Salgado <[email protected]>
Signed-off-by: Pablo Galindo <[email protected]>
  • Loading branch information
pablogsal committed Feb 27, 2025
commit 91ab8b26d9c366b065ff7864f05e4b4c3c356f0e
12 changes: 12 additions & 0 deletions Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,18 @@ async def foo():
with self.assertRaises(Done):
foo().send(None)

def test_complex_lambda(self):
def test1(foo, bar):
return ""

def test2():
return f"{test1(
foo=lambda: '、、、、、、、、、、、、、、、、、',
bar=lambda: 'abcdefghijklmnopqrstuvwxyz 123456789 123456789',
)}"

self.assertEqual(test2(), "")


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a bug that was causing ``UnicodeDecodeError`` or ``SystemError`` to be
raised when using f-strings with ``lambda`` expressions with non-ASCII
characters. Patch by Pablo Galindo
6 changes: 5 additions & 1 deletion Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,13 @@ _PyLexer_update_fstring_expr(struct tok_state *tok, char cur)
break;
case '}':
case '!':
case ':':
tok_mode->last_expr_end = strlen(tok->start);
break;
case ':':
if (tok_mode->last_expr_end == -1) {
tok_mode->last_expr_end = strlen(tok->start);
}
break;
default:
Py_UNREACHABLE();
}
Expand Down
Loading