[Python-checkins] bpo-34854: Fix compiling string annotations containing lambdas. (GH-9645)

Miss Islington (bot) webhook-mailer at python.org
Sun Sep 30 14:19:20 EDT 2018


https://github.com/python/cpython/commit/0f161b307969f86b4f8f31baf38f53f5462a9874
commit: 0f161b307969f86b4f8f31baf38f53f5462a9874
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-09-30T11:19:15-07:00
summary:

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 at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2018-09-30-19-27-13.bpo-34854.6TKTcB.rst
M Lib/test/test_future.py
M Python/ast_unparse.c

diff --git a/Lib/test/test_future.py b/Lib/test/test_future.py
index 61cd63479d85..70da0cf57040 100644
--- a/Lib/test/test_future.py
+++ b/Lib/test/test_future.py
@@ -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')
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-09-30-19-27-13.bpo-34854.6TKTcB.rst b/Misc/NEWS.d/next/Core and Builtins/2018-09-30-19-27-13.bpo-34854.6TKTcB.rst
new file mode 100644
index 000000000000..4e04e1f157cd
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-09-30-19-27-13.bpo-34854.6TKTcB.rst	
@@ -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.
diff --git a/Python/ast_unparse.c b/Python/ast_unparse.c
index 725ce31fe3c0..43453f567b05 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);



More information about the Python-checkins mailing list