]> granicus.if.org Git - python/commitdiff
Issue #14701: Add missing support for 'raise ... from' in parser module.
authorMark Dickinson <mdickinson@enthought.com>
Mon, 7 May 2012 11:01:27 +0000 (12:01 +0100)
committerMark Dickinson <mdickinson@enthought.com>
Mon, 7 May 2012 11:01:27 +0000 (12:01 +0100)
Lib/test/test_parser.py
Misc/NEWS
Modules/parsermodule.c

index f6105fc8184326a59bb7a7ac34e604806133172c..edd1a0947570f60fe8855423c3ba7f2d55a5e660 100644 (file)
@@ -297,6 +297,14 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase):
         self.check_suite("[*a, *b] = y")
         self.check_suite("for [*x, b] in x: pass")
 
+    def test_raise_statement(self):
+        self.check_suite("raise\n")
+        self.check_suite("raise e\n")
+        self.check_suite("try:\n"
+                         "    suite\n"
+                         "except Exception as e:\n"
+                         "    raise ValueError from e\n")
+
 
 #
 #  Second, we take *invalid* trees and make sure we get ParserError
index fe86680b3d46441d53de3b5eed5301ffa0f9c343..e777cac1a5e7cd8cb6299d1d8e5e772b08286b13 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -61,6 +61,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #14701: Fix missing support for 'raise ... from' in parser module.
+
 - Issue #13183: Fix pdb skipping frames after hitting a breakpoint and running
   step.  Patch by Xavier de Gaye.
 
index 99e476d762c702e0ac9b8b2457d2e6caebaa78c3..89ad9789bae4f5d050b428c21724ee09ef001ad8 100644 (file)
@@ -1608,31 +1608,30 @@ validate_return_stmt(node *tree)
 }
 
 
+/*
+ *  raise_stmt:
+ *
+ *  'raise' [test ['from' test]]
+ */
 static int
 validate_raise_stmt(node *tree)
 {
     int nch = NCH(tree);
     int res = (validate_ntype(tree, raise_stmt)
-               && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
+               && ((nch == 1) || (nch == 2) || (nch == 4)));
+
+    if (!res && !PyErr_Occurred())
+        (void) validate_numnodes(tree, 2, "raise");
 
     if (res) {
         res = validate_name(CHILD(tree, 0), "raise");
         if (res && (nch >= 2))
             res = validate_test(CHILD(tree, 1));
-        if (res && nch > 2) {
-            res = (validate_comma(CHILD(tree, 2))
+        if (res && (nch == 4)) {
+            res = (validate_name(CHILD(tree, 2), "from")
                    && validate_test(CHILD(tree, 3)));
-            if (res && (nch > 4))
-                res = (validate_comma(CHILD(tree, 4))
-                       && validate_test(CHILD(tree, 5)));
         }
     }
-    else
-        (void) validate_numnodes(tree, 2, "raise");
-    if (res && (nch == 4))
-        res = (validate_comma(CHILD(tree, 2))
-               && validate_test(CHILD(tree, 3)));
-
     return (res);
 }