summaryrefslogtreecommitdiff
path: root/Python/ast_unparse.c
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-09-30 11:19:15 -0700
committerGitHub <noreply@github.com>2018-09-30 11:19:15 -0700
commit0f161b307969f86b4f8f31baf38f53f5462a9874 (patch)
treed3022e1d7d2630a3af06ac69eef1f0a8abe046d3 /Python/ast_unparse.c
parentb0b8f9bd4e6f78ac7383b4e56cfb6cbacc77da89 (diff)
downloadcpython-git-0f161b307969f86b4f8f31baf38f53f5462a9874.tar.gz
bpo-34854: Fix compiling string annotations containing lambdas. (GH-9645)
* Compiling a string annotation containing a lambda with keyword-only argument without default value caused a crash. * Remove the final "*" (it is incorrect syntax) in the representation of lambda without *args and keyword-only arguments when compile from AST. * Improve the representation of lambda without arguments. (cherry picked from commit 2a2940e5c3e6d92f4fac5e9d361a1e224bb2f12e) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Python/ast_unparse.c')
-rw-r--r--Python/ast_unparse.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/Python/ast_unparse.c b/Python/ast_unparse.c
index 725ce31fe3..43453f567b 100644
--- a/Python/ast_unparse.c
+++ b/Python/ast_unparse.c
@@ -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) {
@@ -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);
+ }
}
}
@@ -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);