[Python-checkins] r53756 - in sandbox/trunk/2to3: fixer_tests.py fixes/fix_raise.py fixes/macros.py

collin.winter python-checkins at python.org
Mon Feb 12 17:22:30 CET 2007


Author: collin.winter
Date: Mon Feb 12 17:22:29 2007
New Revision: 53756

Modified:
   sandbox/trunk/2to3/fixer_tests.py
   sandbox/trunk/2to3/fixes/fix_raise.py
   sandbox/trunk/2to3/fixes/macros.py
Log:
Make fix_raise smarter about dealing with parenthesized expressions

Modified: sandbox/trunk/2to3/fixer_tests.py
==============================================================================
--- sandbox/trunk/2to3/fixer_tests.py	(original)
+++ sandbox/trunk/2to3/fixer_tests.py	Mon Feb 12 17:22:29 2007
@@ -643,20 +643,37 @@
 
     def test_3(self):
         b = """raise Exception, (5, 6, 7)"""
-        a = """raise Exception((5, 6, 7))"""
+        a = """raise Exception(5, 6, 7)"""
         self.check(b, a)
-
-    # These should not be touched
-
+        
     def test_4(self):
-        b = """raise Exception"""
-        a = """raise Exception"""
+        b = """raise E, (5, 6) % (a, b)"""
+        a = """raise E((5, 6) % (a, b))"""
         self.check(b, a)
-
+        
     def test_5(self):
-        b = """raise Exception(5, 6)"""
-        a = """raise Exception(5, 6)"""
+        b = """raise (((E1, E2), E3), E4), V"""
+        a = """raise E1(V)"""
+        self.check(b, a)
+        
+    def test_6(self):
+        b = """raise (E1, (E2, E3), E4), V"""
+        a = """raise E1(V)"""
         self.check(b, a)
+        
+    # These should produce a warning
+    
+    def test_warn_1(self):
+        s = """raise 'foo'"""
+        self.warns(s, s, "Python 3 does not support string exceptions")
+    
+    def test_warn_2(self):
+        s = """raise "foo", 5"""
+        self.warns(s, s, "Python 3 does not support string exceptions")
+    
+    def test_warn_3(self):
+        s = """raise "foo", 5, 6"""
+        self.warns(s, s, "Python 3 does not support string exceptions")
 
     # These should result in traceback-assignment
 
@@ -708,7 +725,7 @@
         b = """def foo():
                     raise Exception, (5, 6, 7), 6"""
         a = """def foo():
-                    xxx_todo_changeme9 = Exception((5, 6, 7))
+                    xxx_todo_changeme9 = Exception(5, 6, 7)
                     xxx_todo_changeme9.__traceback__ = 6
                     raise xxx_todo_changeme9"""
         self.check(b, a)
@@ -720,7 +737,7 @@
                     b = 6"""
         a = """def foo():
                     a = 5
-                    xxx_todo_changeme10 = Exception((5, 6, 7))
+                    xxx_todo_changeme10 = Exception(5, 6, 7)
                     xxx_todo_changeme10.__traceback__ = 6
                     raise xxx_todo_changeme10
                     b = 6"""

Modified: sandbox/trunk/2to3/fixes/fix_raise.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_raise.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_raise.py	Mon Feb 12 17:22:29 2007
@@ -7,22 +7,48 @@
 # Local imports
 import pytree
 from fixes import basefix
-from fixes.macros import Name, Call, Assign, Newline, Attr
+from fixes.macros import Name, Call, Assign, Newline, Attr, is_tuple
 
 class FixRaise(basefix.BaseFix):
 
     PATTERN = """
-    raise_stmt< 'raise' exc=any ',' val=any [',' tb=any] >
+    raise_stmt< 'raise' exc=any [',' val=any [',' tb=any]] >
     """
 
     def transform(self, node):
         syms = self.syms
         results = self.match(node)
         assert results
-
+        
         exc = results["exc"].clone()
-        args = [results["val"].clone()]
-        args[0].set_prefix("")
+        if exc.type is token.STRING:
+            self.cannot_convert(node, "Python 3 does not support string exceptions")
+            return
+
+        # Python 2 supports
+        #  raise ((((E1, E2), E3), E4), E5), V
+        # as a synonym for
+        #  raise E1, V
+        # Since Python 3 will not support this, we recurse down any tuple
+        # literals, always taking the first element.
+        while is_tuple(exc):
+            # exc.children[1:-1] is the unparenthesized tuple
+            # exc.children[1].children[0] is the first element of the tuple
+            exc = exc.children[1].children[0].clone()
+        exc.set_prefix(" ")
+
+        if "val" not in results:
+            # One-argument raise
+            new = pytree.Node(syms.raise_stmt, [Name("raise"), exc])
+            new.set_prefix(node.get_prefix())
+            return new
+        
+        val = results["val"].clone()
+        if is_tuple(val):
+            args = [c.clone() for c in val.children[1:-1]]
+        else:
+            val.set_prefix("")
+            args = [val]
 
         if "tb" in results:
             tb = results["tb"].clone()
@@ -42,8 +68,7 @@
 
             # Assign the traceback
             set_tb = pytree.Node(syms.simple_stmt,
-                                 [Assign(Attr(name.clone(),
-                                              Name("__traceback__")), tb),
+                                 [Assign(Attr(name.clone(), Name("__traceback__")), tb),
                                   Newline()])
             set_tb.set_prefix(indent)
             set_tb.parent = node.parent.parent
@@ -57,7 +82,6 @@
             new.set_prefix(indent)
             return new
         else:
-            new = pytree.Node(syms.raise_stmt,
-                              [Name("raise"), Call(exc, args)])
+            new = pytree.Node(syms.raise_stmt, [Name("raise"), Call(exc, args)])
             new.set_prefix(node.get_prefix())
             return new

Modified: sandbox/trunk/2to3/fixes/macros.py
==============================================================================
--- sandbox/trunk/2to3/fixes/macros.py	(original)
+++ sandbox/trunk/2to3/fixes/macros.py	Mon Feb 12 17:22:29 2007
@@ -55,3 +55,13 @@
 def Newline():
     """A newline literal"""
     return Leaf(token.NEWLINE, "\n")
+
+def is_tuple(node):
+    """Does the node represent a tuple literal?"""
+    
+    return (isinstance(node, Node)
+           and len(node.children) > 1
+           and isinstance(node.children[0], Leaf)
+           and isinstance(node.children[-1], Leaf)
+           and node.children[0].value == "("
+           and node.children[-1].value == ")")


More information about the Python-checkins mailing list