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

collin.winter python-checkins at python.org
Sat Feb 24 21:54:44 CET 2007


Author: collin.winter
Date: Sat Feb 24 21:54:41 2007
New Revision: 53893

Modified:
   sandbox/trunk/2to3/fixer_tests.py
   sandbox/trunk/2to3/fixes/fix_raise.py
   sandbox/trunk/2to3/fixes/fix_throw.py
Log:
Change fix_raise and fix_throw to use the with_traceback() method

Modified: sandbox/trunk/2to3/fixer_tests.py
==============================================================================
--- sandbox/trunk/2to3/fixer_tests.py	(original)
+++ sandbox/trunk/2to3/fixer_tests.py	Sat Feb 24 21:54:41 2007
@@ -681,9 +681,7 @@
         b = """def foo():
                     raise Exception, 5, 6"""
         a = """def foo():
-                    xxx_todo_changeme5 = Exception(5)
-                    xxx_todo_changeme5.__traceback__ = 6
-                    raise xxx_todo_changeme5"""
+                    raise Exception(5).with_traceback(6)"""
         self.check(b, a)
 
     def test_tb_2(self):
@@ -693,9 +691,7 @@
                     b = 6"""
         a = """def foo():
                     a = 5
-                    xxx_todo_changeme6 = Exception(5)
-                    xxx_todo_changeme6.__traceback__ = 6
-                    raise xxx_todo_changeme6
+                    raise Exception(5).with_traceback(6)
                     b = 6"""
         self.check(b, a)
 
@@ -703,9 +699,7 @@
         b = """def foo():
                     raise Exception,5,6"""
         a = """def foo():
-                    xxx_todo_changeme7 = Exception(5)
-                    xxx_todo_changeme7.__traceback__ = 6
-                    raise xxx_todo_changeme7"""
+                    raise Exception(5).with_traceback(6)"""
         self.check(b, a)
 
     def test_tb_4(self):
@@ -715,9 +709,7 @@
                     b = 6"""
         a = """def foo():
                     a = 5
-                    xxx_todo_changeme8 = Exception(5)
-                    xxx_todo_changeme8.__traceback__ = 6
-                    raise xxx_todo_changeme8
+                    raise Exception(5).with_traceback(6)
                     b = 6"""
         self.check(b, a)
 
@@ -725,9 +717,7 @@
         b = """def foo():
                     raise Exception, (5, 6, 7), 6"""
         a = """def foo():
-                    xxx_todo_changeme9 = Exception(5, 6, 7)
-                    xxx_todo_changeme9.__traceback__ = 6
-                    raise xxx_todo_changeme9"""
+                    raise Exception(5, 6, 7).with_traceback(6)"""
         self.check(b, a)
 
     def test_tb_6(self):
@@ -737,9 +727,7 @@
                     b = 6"""
         a = """def foo():
                     a = 5
-                    xxx_todo_changeme10 = Exception(5, 6, 7)
-                    xxx_todo_changeme10.__traceback__ = 6
-                    raise xxx_todo_changeme10
+                    raise Exception(5, 6, 7).with_traceback(6)
                     b = 6"""
         self.check(b, a)
 
@@ -759,27 +747,41 @@
 
     def test_3(self):
         b = """g.throw(Exception, (5, 6, 7))"""
-        a = """g.throw(Exception((5, 6, 7)))"""
+        a = """g.throw(Exception(5, 6, 7))"""
         self.check(b, a)
 
     def test_4(self):
         b = """5 + g.throw(Exception, 5)"""
         a = """5 + g.throw(Exception(5))"""
         self.check(b, a)
+        
+    # These should produce warnings
+
+    def test_warn_1(self):
+        s = """g.throw("foo")"""
+        self.warns(s, s, "Python 3 does not support string exceptions")
+
+    def test_warn_2(self):
+        s = """g.throw("foo", 5)"""
+        self.warns(s, s, "Python 3 does not support string exceptions")
+
+    def test_warn_3(self):
+        s = """g.throw("foo", 5, 6)"""
+        self.warns(s, s, "Python 3 does not support string exceptions")
 
     # These should not be touched
 
-    def test_5(self):
+    def test_untouched_1(self):
         b = """g.throw(Exception)"""
         a = """g.throw(Exception)"""
         self.check(b, a)
 
-    def test_6(self):
+    def test_untouched_2(self):
         b = """g.throw(Exception(5, 6))"""
         a = """g.throw(Exception(5, 6))"""
         self.check(b, a)
 
-    def test_7(self):
+    def test_untouched_3(self):
         b = """5 + g.throw(Exception(5, 6))"""
         a = """5 + g.throw(Exception(5, 6))"""
         self.check(b, a)
@@ -790,9 +792,7 @@
         b = """def foo():
                     g.throw(Exception, 5, 6)"""
         a = """def foo():
-                    xxx_todo_changeme11 = Exception(5)
-                    xxx_todo_changeme11.__traceback__ = 6
-                    g.throw(xxx_todo_changeme11)"""
+                    g.throw(Exception(5).with_traceback(6))"""
         self.check(b, a)
 
     def test_tb_2(self):
@@ -802,9 +802,7 @@
                     b = 6"""
         a = """def foo():
                     a = 5
-                    xxx_todo_changeme12 = Exception(5)
-                    xxx_todo_changeme12.__traceback__ = 6
-                    g.throw(xxx_todo_changeme12)
+                    g.throw(Exception(5).with_traceback(6))
                     b = 6"""
         self.check(b, a)
 
@@ -812,9 +810,7 @@
         b = """def foo():
                     g.throw(Exception,5,6)"""
         a = """def foo():
-                    xxx_todo_changeme13 = Exception(5)
-                    xxx_todo_changeme13.__traceback__ = 6
-                    g.throw(xxx_todo_changeme13)"""
+                    g.throw(Exception(5).with_traceback(6))"""
         self.check(b, a)
 
     def test_tb_4(self):
@@ -824,9 +820,7 @@
                     b = 6"""
         a = """def foo():
                     a = 5
-                    xxx_todo_changeme14 = Exception(5)
-                    xxx_todo_changeme14.__traceback__ = 6
-                    g.throw(xxx_todo_changeme14)
+                    g.throw(Exception(5).with_traceback(6))
                     b = 6"""
         self.check(b, a)
 
@@ -834,9 +828,7 @@
         b = """def foo():
                     g.throw(Exception, (5, 6, 7), 6)"""
         a = """def foo():
-                    xxx_todo_changeme15 = Exception((5, 6, 7))
-                    xxx_todo_changeme15.__traceback__ = 6
-                    g.throw(xxx_todo_changeme15)"""
+                    g.throw(Exception(5, 6, 7).with_traceback(6))"""
         self.check(b, a)
 
     def test_tb_6(self):
@@ -846,9 +838,7 @@
                     b = 6"""
         a = """def foo():
                     a = 5
-                    xxx_todo_changeme16 = Exception((5, 6, 7))
-                    xxx_todo_changeme16.__traceback__ = 6
-                    g.throw(xxx_todo_changeme16)
+                    g.throw(Exception(5, 6, 7).with_traceback(6))
                     b = 6"""
         self.check(b, a)
 
@@ -856,9 +846,7 @@
         b = """def foo():
                     a + g.throw(Exception, 5, 6)"""
         a = """def foo():
-                    xxx_todo_changeme17 = Exception(5)
-                    xxx_todo_changeme17.__traceback__ = 6
-                    a + g.throw(xxx_todo_changeme17)"""
+                    a + g.throw(Exception(5).with_traceback(6))"""
         self.check(b, a)
 
     def test_tb_8(self):
@@ -868,9 +856,7 @@
                     b = 6"""
         a = """def foo():
                     a = 5
-                    xxx_todo_changeme18 = Exception(5)
-                    xxx_todo_changeme18.__traceback__ = 6
-                    a + g.throw(xxx_todo_changeme18)
+                    a + g.throw(Exception(5).with_traceback(6))
                     b = 6"""
         self.check(b, a)
 

Modified: sandbox/trunk/2to3/fixes/fix_raise.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_raise.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_raise.py	Sat Feb 24 21:54:41 2007
@@ -5,7 +5,7 @@
 import pytree
 from pgen2 import token
 from fixes import basefix
-from fixes.macros import Name, Call, Assign, Newline, Attr, is_tuple
+from fixes.macros import Name, Call, Attr, ArgList, is_tuple
 
 class FixRaise(basefix.BaseFix):
 
@@ -50,34 +50,14 @@
 
         if "tb" in results:
             tb = results["tb"].clone()
-            name = Name(self.new_name())
-            children = list(node.parent.parent.children)
-            i = children.index(node.parent)
-            indent = children[1].value
-
-            # Instance the exception
-            build_e = pytree.Node(syms.simple_stmt,
-                                  [Assign(name.clone(), Call(exc, args)),
-                                   Newline()])
-            build_e.parent = node.parent.parent
-            if node.get_prefix():
-                # Over-indents otherwise
-                build_e.set_prefix(indent)
-
-            # Assign the traceback
-            set_tb = pytree.Node(syms.simple_stmt,
-                                 [Assign(Attr(name.clone(), Name("__traceback__")), tb),
-                                  Newline()])
-            set_tb.set_prefix(indent)
-            set_tb.parent = node.parent.parent
-
-            # Insert into the suite
-            children[i:i] = [build_e, set_tb]
-            node.parent.parent.children = tuple(children)
-
-            name.set_prefix(" ")
-            new = pytree.Node(syms.simple_stmt, [Name("raise"), name])
-            new.set_prefix(indent)
+            tb.set_prefix("")
+            
+            e = Call(exc, args)
+            with_tb = Attr(e, Name('with_traceback'))
+            call_wtb = list(with_tb + (ArgList([tb]),))            
+
+            new = pytree.Node(syms.simple_stmt, [Name("raise")] + call_wtb)
+            new.set_prefix(node.get_prefix())
             return new
         else:
             new = pytree.Node(syms.raise_stmt, [Name("raise"), Call(exc, args)])

Modified: sandbox/trunk/2to3/fixes/fix_throw.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_throw.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_throw.py	Sat Feb 24 21:54:41 2007
@@ -5,7 +5,7 @@
 import pytree
 from pgen2 import token
 from fixes import basefix
-from fixes.macros import Name, Call, Assign, Newline, Attr
+from fixes.macros import Name, Call, ArgList, Attr, is_tuple
 
 class FixThrow(basefix.BaseFix):
 
@@ -13,6 +13,8 @@
     power< any trailer< '.' 'throw' >
            trailer< '(' args=arglist< exc=any ',' val=any [',' tb=any] > ')' >
     >
+    |
+    power< any trailer< '.' 'throw' > trailer< '(' exc=any ')' > >
     """
 
     def transform(self, node):
@@ -20,59 +22,35 @@
         results = self.match(node)
         assert results
 
-        throw_args = results["args"]
         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
+            
+        # Leave "g.throw(E)" alone
+        val = results.get("val")
+        if val is None:
+            return 
+
+        val = val.clone()
+        if is_tuple(val):
+            args = [c.clone() for c in val.children[1:-1]]
+        else:
+            val.set_prefix("")
+            args = [val]
+
+        throw_args = results["args"]
 
         if "tb" in results:
             tb = results["tb"].clone()
-            name = Name(self.new_name())
-            suite = find_parent_suite(node)
-            stmts = list(suite.children)
-            node_stmt = find_stmt(stmts, node)
-            i = stmts.index(node_stmt)
-            indent = stmts[1].value
-
-            # Instance the exception
-            build_e = pytree.Node(syms.simple_stmt,
-                                  [Assign(name.clone(), Call(exc, args)),
-                                   Newline()])
-            build_e.parent = node.parent.parent
-            if node_stmt.get_prefix():
-                # Over-indents otherwise
-                build_e.set_prefix(indent)
-
-            # Assign the traceback
-            tb.set_prefix(" ")
-            set_tb = pytree.Node(syms.simple_stmt,
-                                 [Assign(Attr(name.clone(),
-                                              Name("__traceback__")), tb),
-                                  Newline()])
-            set_tb.set_prefix(indent)
-            set_tb.parent = node.parent.parent
-
-            # Insert into the suite
-            stmts[i:i] = [build_e, set_tb]
-            suite.children = tuple(stmts)
-
-            throw_args.replace(name)
-            if not node_stmt.get_prefix():
-                node_stmt.set_prefix(indent)
+            tb.set_prefix("")
+            
+            e = Call(exc, args)
+            with_tb = Attr(e, Name('with_traceback'))
+            call_wtb = list(with_tb + (ArgList([tb]),))
+            
+            throw_args.replace(pytree.Node(syms.power, call_wtb))
             # No return
         else:
             throw_args.replace(Call(exc, args))
             # No return
-
-
-def find_parent_suite(node):
-    parent = node.parent
-    while parent:
-        if len(parent.children) > 2 and parent.children[0].value == "\n":
-            return parent
-        parent = parent.parent
-
-def find_stmt(stmts, node):
-    while node not in stmts:
-        node = node.parent
-    return node


More information about the Python-checkins mailing list