[Python-checkins] r84213 - in python/branches/release31-maint: Python/ast.c

amaury.forgeotdarc python-checkins at python.org
Thu Aug 19 22:26:00 CEST 2010


Author: amaury.forgeotdarc
Date: Thu Aug 19 22:26:00 2010
New Revision: 84213

Log:
Merged revisions 84209 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r84209 | amaury.forgeotdarc | 2010-08-19 19:43:15 +0200 (jeu., 19 août 2010) | 5 lines
  
  Check the return values for all functions returning an ast node.
  Failure to do it may result in strange error messages or even crashes,
  in admittedly convoluted cases that are normally syntax errors, like:
      def f(*xx, __debug__): pass
........


Modified:
   python/branches/release31-maint/   (props changed)
   python/branches/release31-maint/Python/ast.c

Modified: python/branches/release31-maint/Python/ast.c
==============================================================================
--- python/branches/release31-maint/Python/ast.c	(original)
+++ python/branches/release31-maint/Python/ast.c	Thu Aug 19 22:26:00 2010
@@ -673,6 +673,8 @@
             case tfpdef:
                 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
                     expression = ast_for_expr(c, CHILD(n, i + 2));
+                    if (!expression)
+		      goto error;
                     asdl_seq_SET(kwdefaults, j, expression);
                     i += 2; /* '=' and test */
                 }
@@ -682,10 +684,8 @@
                 if (NCH(ch) == 3) {
                     /* ch is NAME ':' test */
                     annotation = ast_for_expr(c, CHILD(ch, 2));
-                    if (!annotation) {
-                        ast_error(ch, "expected expression");
+                    if (!annotation)
                         goto error;
-                    }
                 }
                 else {
                     annotation = NULL;
@@ -777,22 +777,22 @@
     }
     posargs = (nposargs ? asdl_seq_new(nposargs, c->c_arena) : NULL);
     if (!posargs && nposargs)
-        goto error;
+        return NULL;
     kwonlyargs = (nkwonlyargs ?
                    asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
     if (!kwonlyargs && nkwonlyargs)
-        goto error;
+        return NULL;
     posdefaults = (nposdefaults ?
                     asdl_seq_new(nposdefaults, c->c_arena) : NULL);
     if (!posdefaults && nposdefaults)
-        goto error;
+        return NULL;
     /* The length of kwonlyargs and kwdefaults are same
        since we set NULL as default for keyword only argument w/o default
        - we have sequence data structure, but no dictionary */
     kwdefaults = (nkwonlyargs ?
                    asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
     if (!kwdefaults && nkwonlyargs)
-        goto error;
+        return NULL;
 
     if (nposargs + nkwonlyargs > 255) {
         ast_error(n, "more than 255 arguments");
@@ -816,7 +816,7 @@
                 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
                     expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
                     if (!expression)
-                        goto error;
+                        return NULL;
                     assert(posdefaults != NULL);
                     asdl_seq_SET(posdefaults, j++, expression);
                     i += 2;
@@ -825,11 +825,11 @@
                 else if (found_default) {
                     ast_error(n,
                              "non-default argument follows default argument");
-                    goto error;
+                    return NULL;
                 }
                 arg = compiler_arg(c, ch);
                 if (!arg)
-                    goto error;
+                    return NULL;
                 asdl_seq_SET(posargs, k++, arg);
                 i += 2; /* the name and the comma */
                 break;
@@ -837,7 +837,7 @@
                 if (i+1 >= NCH(n)) {
                     ast_error(CHILD(n, i),
                         "named arguments must follow bare *");
-                    goto error;
+                    return NULL;
                 }
                 ch = CHILD(n, i+1);  /* tfpdef or COMMA */
                 if (TYPE(ch) == COMMA) {
@@ -845,7 +845,7 @@
                     i += 2; /* now follows keyword only arguments */
                     res = handle_keywordonly_args(c, n, i,
                                                   kwonlyargs, kwdefaults);
-                    if (res == -1) goto error;
+                    if (res == -1) return NULL;
                     i = res; /* res has new position to process */
                 }
                 else {
@@ -855,6 +855,8 @@
                     if (NCH(ch) > 1) {
                         /* there is an annotation on the vararg */
                         varargannotation = ast_for_expr(c, CHILD(ch, 2));
+                        if (!varargannotation)
+                            return NULL;
                     }
                     i += 3;
                     if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
@@ -862,7 +864,7 @@
                         int res = 0;
                         res = handle_keywordonly_args(c, n, i,
                                                       kwonlyargs, kwdefaults);
-                        if (res == -1) goto error;
+                        if (res == -1) return NULL;
                         i = res; /* res has new position to process */
                     }
                 }
@@ -874,24 +876,22 @@
                 if (NCH(ch) > 1) {
                     /* there is an annotation on the kwarg */
                     kwargannotation = ast_for_expr(c, CHILD(ch, 2));
+                    if (!kwargannotation)
+                        return NULL;
                 }
                 if (!kwarg)
-                    goto error;
+                    return NULL;
                 i += 3;
                 break;
             default:
                 PyErr_Format(PyExc_SystemError,
                              "unexpected node in varargslist: %d @ %d",
                              TYPE(ch), i);
-                goto error;
+                return NULL;
         }
     }
     return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg,
                     kwargannotation, posdefaults, kwdefaults, c->c_arena);
- error:
-    Py_XDECREF(vararg);
-    Py_XDECREF(kwarg);
-    return NULL;
 }
 
 static expr_ty
@@ -976,9 +976,9 @@
 
     for (i = 0; i < NCH(n); i++) {
         d = ast_for_decorator(c, CHILD(n, i));
-            if (!d)
-                return NULL;
-            asdl_seq_SET(decorator_seq, i, d);
+        if (!d)
+            return NULL;
+        asdl_seq_SET(decorator_seq, i, d);
     }
     return decorator_seq;
 }
@@ -1004,7 +1004,7 @@
     if (TYPE(CHILD(n, name_i+2)) == RARROW) {
         returns = ast_for_expr(c, CHILD(n, name_i + 3));
         if (!returns)
-                return NULL;
+            return NULL;
         name_i += 2;
     }
     body = ast_for_suite(c, CHILD(n, name_i + 3));
@@ -2152,11 +2152,10 @@
                 return NULL;
             }
             e = ast_for_testlist(c, ch);
-
-            /* set context to assign */
             if (!e)
               return NULL;
 
+            /* set context to assign */
             if (!set_context(c, e, Store, CHILD(n, i)))
               return NULL;
 
@@ -2957,6 +2956,8 @@
 
     REQ(n, with_item);
     context_expr = ast_for_expr(c, CHILD(n, 0));
+    if (!context_expr)
+        return NULL;
     if (NCH(n) == 3) {
         optional_vars = ast_for_expr(c, CHILD(n, 2));
 


More information about the Python-checkins mailing list