[Python-checkins] r56393 - in sandbox/trunk/2to3: fixes/basefix.py fixes/fix_print.py fixes/fix_raise.py fixes/fix_raw_input.py fixes/fix_throw.py fixes/fix_tuple_params.py fixes/fix_unicode.py fixes/fix_xrange.py fixes/util.py

collin.winter python-checkins at python.org
Sun Jul 15 01:04:54 CEST 2007


Author: collin.winter
Date: Sun Jul 15 01:04:54 2007
New Revision: 56393

Modified:
   sandbox/trunk/2to3/   (props changed)
   sandbox/trunk/2to3/fixes/basefix.py
   sandbox/trunk/2to3/fixes/fix_print.py
   sandbox/trunk/2to3/fixes/fix_raise.py
   sandbox/trunk/2to3/fixes/fix_raw_input.py
   sandbox/trunk/2to3/fixes/fix_throw.py
   sandbox/trunk/2to3/fixes/fix_tuple_params.py
   sandbox/trunk/2to3/fixes/fix_unicode.py
   sandbox/trunk/2to3/fixes/fix_xrange.py
   sandbox/trunk/2to3/fixes/util.py
Log:
General fixer clean-up (whitespace, imports, use new utils, etc).


Modified: sandbox/trunk/2to3/fixes/basefix.py
==============================================================================
--- sandbox/trunk/2to3/fixes/basefix.py	(original)
+++ sandbox/trunk/2to3/fixes/basefix.py	Sun Jul 15 01:04:54 2007
@@ -116,7 +116,7 @@
         self.logger.warning(msg % (lineno, for_output))
         if reason:
             self.logger.warning(reason)
-            
+
     def warning(self, node, reason):
         """Used for warning the user about possible uncertainty in the
         translation.

Modified: sandbox/trunk/2to3/fixes/fix_print.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_print.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_print.py	Sun Jul 15 01:04:54 2007
@@ -20,8 +20,8 @@
 class FixPrint(basefix.BaseFix):
 
     PATTERN = """
-    'print' | print_stmt
-    """
+              'print' | print_stmt
+              """
 
     def match(self, node):
         # Override

Modified: sandbox/trunk/2to3/fixes/fix_raise.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_raise.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_raise.py	Sun Jul 15 01:04:54 2007
@@ -78,6 +78,6 @@
             new.set_prefix(node.get_prefix())
             return new
         else:
-            new = pytree.Node(syms.raise_stmt, [Name("raise"), Call(exc, args)])
-            new.set_prefix(node.get_prefix())
-            return new
+            return pytree.Node(syms.raise_stmt,
+                               [Name("raise"), Call(exc, args)],
+                               prefix=node.get_prefix())

Modified: sandbox/trunk/2to3/fixes/fix_raw_input.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_raw_input.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_raw_input.py	Sun Jul 15 01:04:54 2007
@@ -2,19 +2,15 @@
 # Author: Andre Roberge
 
 # Local imports
-import pytree
 from fixes import basefix
 from fixes.util import Name
 
 class FixRawInput(basefix.BaseFix):
 
     PATTERN = """
-              power< 'raw_input' args=trailer< '(' [any] ')' > >
+              power< name='raw_input' trailer< '(' [any] ')' > >
               """
 
     def transform(self, node, results):
-        args = results["args"]
-
-        new = pytree.Node(self.syms.power, [Name("input"), args.clone()])
-        new.set_prefix(node.get_prefix())
-        return new
+        name = results["name"]
+        name.replace(Name("input", prefix=name.get_prefix()))

Modified: sandbox/trunk/2to3/fixes/fix_throw.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_throw.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_throw.py	Sun Jul 15 01:04:54 2007
@@ -30,11 +30,11 @@
         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 
+            return
 
         val = val.clone()
         if is_tuple(val):
@@ -48,7 +48,7 @@
         if "tb" in results:
             tb = results["tb"].clone()
             tb.set_prefix("")
-            
+
             e = Call(exc, args)
             with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])]
             throw_args.replace(pytree.Node(syms.power, with_tb))

Modified: sandbox/trunk/2to3/fixes/fix_tuple_params.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_tuple_params.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_tuple_params.py	Sun Jul 15 01:04:54 2007
@@ -33,11 +33,9 @@
               lambda=lambdef< 'lambda' args=vfpdef< any+ > ':' body=any >"""
 
     def transform(self, node, results):
-        assert results
-        
         if "lambda" in results:
-            return self.transform_lambda(node)
-        
+            return self.transform_lambda(node, results)
+
         new_lines = []
         suite = results["suite"]
         args = results["args"]
@@ -51,7 +49,7 @@
             start = 0
             indent = "; "
             end = pytree.Leaf(token.INDENT, "")
-        
+
         # We need access to self for new_name(), and making this a method
         #  doesn't feel right. Closing over self and new_lines makes the
         #  code below cleaner.
@@ -63,8 +61,9 @@
             if add_prefix:
                 n.set_prefix(" ")
             tuple_arg.replace(n)
-            new_lines.append(pytree.Node(syms.simple_stmt, [stmt, end.clone()]))
-        
+            new_lines.append(pytree.Node(syms.simple_stmt,
+                                         [stmt, end.clone()]))
+
         if args.type == syms.tfpdef:
             handle_tuple(args)
         elif args.type == syms.typedargslist:
@@ -73,15 +72,15 @@
                     # Without add_prefix, the emitted code is correct,
                     #  just ugly.
                     handle_tuple(arg, add_prefix=(i > 0))
-                    
+
         if not new_lines:
             return node
-        
+
         # This isn't strictly necessary, but it plays nicely with other fixers.
         # TODO(cwinter) get rid of this when children becomes a smart list
         for line in new_lines:
             line.parent = suite[0]
-            
+
         # TODO(cwinter) suite-cleanup
         after = start
         if start == 0:
@@ -89,22 +88,20 @@
         elif is_docstring(suite[0].children[start]):
             new_lines[0].set_prefix(indent)
             after = start + 1
-            
+
         suite[0].children[after:after] = new_lines
         for i in range(after+1, after+len(new_lines)+1):
             suite[0].children[i].set_prefix(indent)
         suite[0].changed()
-        
-    def transform_lambda(self, node):
-        results = self.match(node)
-        assert results
+
+    def transform_lambda(self, node, results):
         args = results["args"]
         body = results["body"]
 
         params = find_params(args)
         to_index = map_to_index(params)
         tup_name = self.new_name(tuple_name(params))
-        
+
         new_param = Name(tup_name)
         new_param.set_prefix(args.get_prefix())
         args.replace(new_param.clone())

Modified: sandbox/trunk/2to3/fixes/fix_unicode.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_unicode.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_unicode.py	Sun Jul 15 01:04:54 2007
@@ -3,7 +3,6 @@
 """
 
 import re
-import pytree
 from pgen2 import token
 from fixes import basefix
 
@@ -27,4 +26,3 @@
         new = node.clone()
         new.value = new.value[1:]
         return new
-    return None

Modified: sandbox/trunk/2to3/fixes/fix_xrange.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_xrange.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_xrange.py	Sun Jul 15 01:04:54 2007
@@ -4,23 +4,15 @@
 """Fixer that changes xrange(...) into range(...)."""
 
 # Local imports
-import pytree
-from pgen2 import token
 from fixes import basefix
 from fixes.util import Name
 
 class FixXrange(basefix.BaseFix):
 
     PATTERN = """
-    power<
-        'xrange'
-        args=trailer< '(' [any] ')' >
-    >
-    """
+              power< name='xrange' trailer< '(' [any] ')' > >
+              """
 
     def transform(self, node, results):
-        args = results["args"]
-        new = pytree.Node(self.syms.power,
-                          [Name("range"), args.clone()])
-        new.set_prefix(node.get_prefix())
-        return new
+        name = results["name"]
+        name.replace(Name("range", prefix=name.get_prefix()))

Modified: sandbox/trunk/2to3/fixes/util.py
==============================================================================
--- sandbox/trunk/2to3/fixes/util.py	(original)
+++ sandbox/trunk/2to3/fixes/util.py	Sun Jul 15 01:04:54 2007
@@ -7,10 +7,6 @@
 from pygram import python_symbols as syms
 
 
-### Constant nodes
-ass_leaf = Leaf(token.EQUAL, "=")
-ass_leaf.set_prefix(" ")
-
 ###########################################################
 ### Common node-construction "macros"
 ###########################################################
@@ -33,7 +29,8 @@
         source.set_prefix(" ")
         source = [source]
 
-    return Node(syms.atom, target + [ass_leaf.clone()] + source)
+    return Node(syms.atom,
+                target + [Leaf(token.EQUAL, "=", prefix=" ")] + source)
 
 def Name(name, prefix=None):
     """Return a NAME leaf"""
@@ -102,7 +99,7 @@
                        [Leaf(token.LBRACE, "["),
                         inner,
                         Leaf(token.RBRACE, "]")])
-    
+
 ###########################################################
 ### Determine whether a node represents a given literal
 ###########################################################


More information about the Python-checkins mailing list