Add support for PEP572 in ast_unparse.c (GH-13337)
authorBatuhan Taşkaya <47358913+isidentical@users.noreply.github.com>
Sat, 18 May 2019 22:10:20 +0000 (01:10 +0300)
committerPablo Galindo <Pablogsal@gmail.com>
Sat, 18 May 2019 22:10:20 +0000 (23:10 +0100)
Lib/test/test_future.py
Misc/NEWS.d/next/Core and Builtins/2019-05-15-14-01-09.bpo-36826.GLrO3W.rst [new file with mode: 0644]
Python/ast_unparse.c

index 38de3dfdafcdb15333195378b20a7fa486962da8..cd320a266a8741a6d9a8d223eed7446f5053ba0c 100644 (file)
@@ -275,6 +275,8 @@ class AnnotationsFutureTestCase(unittest.TestCase):
         eq('f((x for x in a), 2)')
         eq('(((a)))', 'a')
         eq('(((a, b)))', '(a, b)')
+        eq("(x:=10)")
+        eq("f'{(x:=10):=10}'")
 
 
 if __name__ == "__main__":
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-05-15-14-01-09.bpo-36826.GLrO3W.rst b/Misc/NEWS.d/next/Core and Builtins/2019-05-15-14-01-09.bpo-36826.GLrO3W.rst
new file mode 100644 (file)
index 0000000..5a1b519
--- /dev/null
@@ -0,0 +1 @@
+Add NamedExpression kind support to ast_unparse.c
index 25a5c698a1db9ed4bc4ed94fcdf76b7e40465eb7..5f366a188b36e79660598143f73586c98b30c26e 100644 (file)
@@ -809,6 +809,17 @@ append_ast_await(_PyUnicodeWriter *writer, expr_ty e, int level)
     return 0;
 }
 
+static int
+append_named_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
+{
+    APPEND_STR_IF(level > PR_TUPLE, "(");
+    APPEND_EXPR(e->v.NamedExpr.target, PR_ATOM);
+    APPEND_STR(":=");
+    APPEND_EXPR(e->v.NamedExpr.value, PR_ATOM);
+    APPEND_STR_IF(level > PR_TUPLE, ")");
+    return 0;
+}
+
 static int
 append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
 {
@@ -867,6 +878,8 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
         return append_ast_list(writer, e);
     case Tuple_kind:
         return append_ast_tuple(writer, e, level);
+    case NamedExpr_kind:
+        return append_named_expr(writer, e, level);
     default:
         PyErr_SetString(PyExc_SystemError,
                         "unknown expression kind");