diff Python/ast.c @ 57367:ed30bcffa146 legacy-trunk

improve several corner cases related with argument names in parenthesis - Fix #7362: give a good error message for parenthesized arguments with defaults. - Add a py3k warning for any parenthesized arguments since those are not allowed in Py3. This warning is not given in tuple unpacking, since that incurs the tuple unpacking warning.
author Benjamin Peterson <benjamin@python.org>
date Thu, 19 Nov 2009 22:54:57 +0000
parents 844180b7e6ff
children c2fdf25329ff
line wrap: on
line diff
--- a/Python/ast.c	Thu Nov 19 18:41:49 2009 +0000
+++ b/Python/ast.c	Thu Nov 19 22:54:57 2009 +0000
@@ -692,7 +692,8 @@
     while (i < NCH(n)) {
         ch = CHILD(n, i);
         switch (TYPE(ch)) {
-            case fpdef:
+            case fpdef: {
+                int complex_args = 0, parenthesized = 0;
             handle_fpdef:
                 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
                    anything other than EQUAL or a comma? */
@@ -707,6 +708,12 @@
                     found_default = 1;
                 }
                 else if (found_default) {
+                    /* def f((x)=4): pass should raise an error.
+                       def f((x, (y))): pass will just incur the tuple unpacking warning. */
+                    if (parenthesized && !complex_args) {
+                        ast_error(n, "parenthesized arg with default");
+                        goto error;
+                    }
                     ast_error(n, 
                              "non-default argument follows default argument");
                     goto error;
@@ -719,6 +726,7 @@
                         if (Py_Py3kWarningFlag && !ast_warn(c, ch,
                             "tuple parameter unpacking has been removed in 3.x"))
                             goto error;
+                        complex_args = 1;
                         asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
                         if (!asdl_seq_GET(args, k-1))
                                 goto error;
@@ -726,6 +734,7 @@
                         /* def foo((x)): setup for checking NAME below. */
                         /* Loop because there can be many parens and tuple
                            unpacking mixed in. */
+                        parenthesized = 1;
                         ch = CHILD(ch, 0);
                         assert(TYPE(ch) == fpdef);
                         goto handle_fpdef;
@@ -747,7 +756,13 @@
                                          
                 }
                 i += 2; /* the name and the comma */
+                if (parenthesized && Py_Py3kWarningFlag &&
+                    !ast_warn(c, ch, "parenthesized argument names "
+                              "are invalid in 3.x"))
+                    goto error;
+
                 break;
+            }
             case STAR:
                 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
                     goto error;