[Python-checkins] cpython (merge 3.2 -> default): Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3.

mark.dickinson python-checkins at python.org
Sun May 6 18:35:44 CEST 2012


http://hg.python.org/cpython/rev/89e928048903
changeset:   76803:89e928048903
parent:      76801:5c60c142f099
parent:      76802:c80576303892
user:        Mark Dickinson <mdickinson at enthought.com>
date:        Sun May 06 17:35:19 2012 +0100
summary:
  Issue #14965:  Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.

files:
  Misc/NEWS                    |   6 +++
  Tools/parser/test_unparse.py |  31 +++++++++++++++++
  Tools/parser/unparse.py      |  42 +++++++++++++----------
  3 files changed, 60 insertions(+), 19 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -28,6 +28,12 @@
 - Issue #14127 and #10148: shutil.copystat now preserves exact mtime and atime
   on filesystems providing nanosecond resolution.
 
+Tools/Demos
+-----------
+
+- Issue #14965: Bring Tools/parser/unparse.py support up to date with
+  the Python 3.3 Grammar.
+
 
 What's New in Python 3.3.0 Alpha 3?
 ===================================
diff --git a/Tools/parser/test_unparse.py b/Tools/parser/test_unparse.py
--- a/Tools/parser/test_unparse.py
+++ b/Tools/parser/test_unparse.py
@@ -93,6 +93,21 @@
     suite5
 """
 
+with_simple = """\
+with f():
+    suite1
+"""
+
+with_as = """\
+with f() as x:
+    suite1
+"""
+
+with_two_items = """\
+with f() as x, g() as y:
+    suite1
+"""
+
 class ASTTestCase(unittest.TestCase):
     def assertASTEqual(self, ast1, ast2):
         self.assertEqual(ast.dump(ast1), ast.dump(ast2))
@@ -209,6 +224,22 @@
     def test_try_except_finally(self):
         self.check_roundtrip(try_except_finally)
 
+    def test_starred_assignment(self):
+        self.check_roundtrip("a, *b, c = seq")
+        self.check_roundtrip("a, (*b, c) = seq")
+        self.check_roundtrip("a, *b[0], c = seq")
+        self.check_roundtrip("a, *(b, c) = seq")
+
+    def test_with_simple(self):
+        self.check_roundtrip(with_simple)
+
+    def test_with_as(self):
+        self.check_roundtrip(with_as)
+
+    def test_with_two_items(self):
+        self.check_roundtrip(with_two_items)
+
+
 class DirectoryTestCase(ASTTestCase):
     """Test roundtrip behaviour on all files in Lib and Lib/test."""
 
diff --git a/Tools/parser/unparse.py b/Tools/parser/unparse.py
--- a/Tools/parser/unparse.py
+++ b/Tools/parser/unparse.py
@@ -147,6 +147,14 @@
             self.dispatch(t.value)
         self.write(")")
 
+    def _YieldFrom(self, t):
+        self.write("(")
+        self.write("yield from")
+        if t.value:
+            self.write(" ")
+            self.dispatch(t.value)
+        self.write(")")
+
     def _Raise(self, t):
         self.fill("raise")
         if not t.exc:
@@ -158,12 +166,11 @@
             self.write(" from ")
             self.dispatch(t.cause)
 
-    def _TryExcept(self, t):
+    def _Try(self, t):
         self.fill("try")
         self.enter()
         self.dispatch(t.body)
         self.leave()
-
         for ex in t.handlers:
             self.dispatch(ex)
         if t.orelse:
@@ -171,22 +178,12 @@
             self.enter()
             self.dispatch(t.orelse)
             self.leave()
-
-    def _TryFinally(self, t):
-        if len(t.body) == 1 and isinstance(t.body[0], ast.TryExcept):
-            # try-except-finally
-            self.dispatch(t.body)
-        else:
-            self.fill("try")
+        if t.finalbody:
+            self.fill("finally")
             self.enter()
-            self.dispatch(t.body)
+            self.dispatch(t.finalbody)
             self.leave()
 
-        self.fill("finally")
-        self.enter()
-        self.dispatch(t.finalbody)
-        self.leave()
-
     def _ExceptHandler(self, t):
         self.fill("except")
         if t.type:
@@ -296,10 +293,7 @@
 
     def _With(self, t):
         self.fill("with ")
-        self.dispatch(t.context_expr)
-        if t.optional_vars:
-            self.write(" as ")
-            self.dispatch(t.optional_vars)
+        interleave(lambda: self.write(", "), self.dispatch, t.items)
         self.enter()
         self.dispatch(t.body)
         self.leave()
@@ -472,6 +466,10 @@
         self.dispatch(t.slice)
         self.write("]")
 
+    def _Starred(self, t):
+        self.write("*")
+        self.dispatch(t.value)
+
     # slice
     def _Ellipsis(self, t):
         self.write("...")
@@ -560,6 +558,12 @@
         if t.asname:
             self.write(" as "+t.asname)
 
+    def _withitem(self, t):
+        self.dispatch(t.context_expr)
+        if t.optional_vars:
+            self.write(" as ")
+            self.dispatch(t.optional_vars)
+
 def roundtrip(filename, output=sys.stdout):
     with open(filename, "rb") as pyfile:
         encoding = tokenize.detect_encoding(pyfile.readline)[0]

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list