Skip to content

bpo-34854: Fix a crash in string annotations containing specific lambda. #9645

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
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 changes: 2 additions & 1 deletion Lib/test/test_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,12 @@ def test_annotations(self):
eq('-1')
eq('~int and not v1 ^ 123 + v2 | True')
eq('a + (not b)')
eq('lambda: None')
eq('lambda arg: None')
eq('lambda a=True: a')
eq('lambda a, b, c=True: a')
eq("lambda a, b, c=True, *, d=1 << v2, e='str': a")
eq("lambda a, b, c=True, *vararg, d=v1 << 2, e='str', **kwargs: a + b")
eq("lambda a, b, c=True, *vararg, d, e='str', **kwargs: a + b")
eq('lambda x: lambda y: x + y')
eq('1 if True else 2')
eq('str or None if int or True else str or bytes or None')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a crash in compiling string annotations containing a lambda with a
keyword-only argument that doesn't have a default value.
11 changes: 7 additions & 4 deletions Python/ast_unparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ append_ast_args(_PyUnicodeWriter *writer, arguments_ty args)
}

/* vararg, or bare '*' if no varargs but keyword-only arguments present */
if (args->vararg || args->kwonlyargs) {
if (args->vararg || asdl_seq_LEN(args->kwonlyargs)) {
APPEND_STR_IF_NOT_FIRST(", ");
APPEND_STR("*");
if (args->vararg) {
Expand All @@ -229,8 +229,11 @@ append_ast_args(_PyUnicodeWriter *writer, arguments_ty args)

di = i - arg_count + default_count;
if (di >= 0) {
APPEND_STR("=");
APPEND_EXPR((expr_ty)asdl_seq_GET(args->kw_defaults, di), PR_TEST);
expr_ty default_ = (expr_ty)asdl_seq_GET(args->kw_defaults, di);
if (default_) {
APPEND_STR("=");
APPEND_EXPR(default_, PR_TEST);
}
}
}

Expand All @@ -248,7 +251,7 @@ static int
append_ast_lambda(_PyUnicodeWriter *writer, expr_ty e, int level)
{
APPEND_STR_IF(level > PR_TEST, "(");
APPEND_STR("lambda ");
APPEND_STR(asdl_seq_LEN(e->v.Lambda.args->args) ? "lambda " : "lambda");
APPEND(args, e->v.Lambda.args);
APPEND_STR(": ");
APPEND_EXPR(e->v.Lambda.body, PR_TEST);
Expand Down