[Python-checkins] r53174 - sandbox/trunk/2to3/fixes/basefix.py sandbox/trunk/2to3/fixes/fix_exec.py sandbox/trunk/2to3/fixes/fix_has_key.py sandbox/trunk/2to3/fixes/fix_intern.py sandbox/trunk/2to3/fixes/fix_ne.py sandbox/trunk/2to3/fixes/fix_null.py sandbox/trunk/2to3/fixes/fix_print.py

guido.van.rossum python-checkins at python.org
Thu Dec 28 22:50:14 CET 2006


Author: guido.van.rossum
Date: Thu Dec 28 22:50:13 2006
New Revision: 53174

Removed:
   sandbox/trunk/2to3/fixes/fix_null.py
Modified:
   sandbox/trunk/2to3/fixes/basefix.py
   sandbox/trunk/2to3/fixes/fix_exec.py
   sandbox/trunk/2to3/fixes/fix_has_key.py
   sandbox/trunk/2to3/fixes/fix_intern.py
   sandbox/trunk/2to3/fixes/fix_ne.py
   sandbox/trunk/2to3/fixes/fix_print.py
Log:
Finish refactoring the remaining fixes.
The null fix is no longer needed; the class docs are in basefix.py.


Modified: sandbox/trunk/2to3/fixes/basefix.py
==============================================================================
--- sandbox/trunk/2to3/fixes/basefix.py	(original)
+++ sandbox/trunk/2to3/fixes/basefix.py	Thu Dec 28 22:50:13 2006
@@ -10,7 +10,13 @@
 
 class BaseFix(object):
 
-    """Optional base class for fixers."""
+    """Optional base class for fixers.
+
+    The subclass name must be FixFooBar where FooBar is the result of
+    removing underscores and capitalizing the words of the fix name.
+    For example, the class name for a fixer named 'has_key' should be
+    FixHasKey.
+    """
 
     PATTERN = None  # Subclass *must* override with a string literal
     pattern = None  # Compiled pattern, set by compile_pattern()
@@ -20,7 +26,11 @@
     syms = pygram.python_symbols
 
     def __init__(self, options):
-        """Initializer.  Subclass may override."""
+        """Initializer.  Subclass may override.
+o
+        The argument is an optparse.Values instance which can be used
+        to inspect the command line options.
+        """
         self.options = options
         self.compile_pattern()
 
@@ -35,6 +45,10 @@
     def match(self, node):
         """Returns match for a given parse tree node.
 
+        Should return a true or false object (not necessarily a bool).
+        It may return a non-empty dict of matching sub-nodes as
+        returned by a matching pattern.
+
         Subclass may override.
         """
         results = {}
@@ -43,7 +57,10 @@
     def transform(self, node):
         """Returns the transformation for a given parse tree node.
 
-        Subclass must override.
+        Should return None, or a node that is a modified copy of the
+        argument node.  The argument should not be modified in place.
+
+        Subclass *must* override.
         """
         return None
 

Modified: sandbox/trunk/2to3/fixes/fix_exec.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_exec.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_exec.py	Thu Dec 28 22:50:13 2006
@@ -8,30 +8,19 @@
 
 # Local imports
 import pytree
-import patcomp
-import pygram
+from fixes import basefix
 
-syms = pygram.python_symbols
-pat_compile = patcomp.PatternCompiler().compile_pattern
 
-PATTERN = """
-exec_stmt< 'exec' a=any 'in' b=any [',' c=any] >
-|
-exec_stmt< 'exec' (not atom<'(' [any] ')'>) a=any >
-"""
+class FixExec(basefix.BaseFix):
 
-
-class FixExec(object):
-
-    def __init__(self, options):
-        self.options = options
-        self.pattern = pat_compile(PATTERN)
-
-    def match(self, node):
-        results = {}
-        return self.pattern.match(node, results) and results
+    PATTERN = """
+    exec_stmt< 'exec' a=any 'in' b=any [',' c=any] >
+    |
+    exec_stmt< 'exec' (not atom<'(' [any] ')'>) a=any >
+    """
 
     def transform(self, node):
+        syms = self.syms
         results = self.match(node)
         assert results
         a = results["a"]
@@ -51,4 +40,3 @@
                                         pytree.Leaf(token.RPAR, ")")])])
         new.set_prefix(node.get_prefix())
         return new
-                

Modified: sandbox/trunk/2to3/fixes/fix_has_key.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_has_key.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_has_key.py	Thu Dec 28 22:50:13 2006
@@ -8,28 +8,12 @@
 
 # Local imports
 import pytree
-import patcomp
-import pygram
+from fixes import basefix
 
-syms = pygram.python_symbols
-pat_compile = patcomp.PatternCompiler().compile_pattern
 
-PATTERN = """
-anchor=power<
-    before=any+
-    trailer< '.' 'has_key' >
-    trailer<
-        '('
-        ( not(arglist | argument<any '=' any>) arg=any
-        | arglist<(not argument<any '=' any>) arg=any ','>
-        )
-        ')'
-    >
-    after=any*
->
-|
-negation=not_test<
-    'not'
+class FixHasKey(basefix.BaseFix):
+
+    PATTERN = """
     anchor=power<
         before=any+
         trailer< '.' 'has_key' >
@@ -40,22 +24,27 @@
             )
             ')'
         >
+        after=any*
     >
->
-"""
-
-
-class FixHasKey(object):
-
-    def __init__(self, options):
-        self.options = options
-        self.pattern = pat_compile(PATTERN)
-
-    def match(self, node):
-        results = {}
-        return self.pattern.match(node, results) and results
+    |
+    negation=not_test<
+        'not'
+        anchor=power<
+            before=any+
+            trailer< '.' 'has_key' >
+            trailer<
+                '('
+                ( not(arglist | argument<any '=' any>) arg=any
+                | arglist<(not argument<any '=' any>) arg=any ','>
+                )
+                ')'
+            >
+        >
+    >
+    """
 
     def transform(self, node):
+        syms = self.syms
         results = self.match(node)
         assert results
         if (node.parent.type == syms.not_test and
@@ -73,7 +62,7 @@
             after = [n.clone() for n in after]
         if arg.type in (syms.comparison, syms.not_test, syms.and_test,
                         syms.or_test, syms.test, syms.lambdef, syms.argument):
-            arg = pygram.parenthesize(arg)
+            arg = self.parenthesize(arg)
         if len(before) == 1:
             before = before[0]
         else:
@@ -87,12 +76,12 @@
             n_op = pytree.Node(syms.comp_op, (n_not, n_op))
         new = pytree.Node(syms.comparison, (arg, n_op, before))
         if after:
-            new = pygram.parenthesize(new)
+            new = self.parenthesize(new)
             new = pytree.Node(syms.power, (new,) + tuple(after))
         if node.parent.type in (syms.comparison, syms.expr, syms.xor_expr,
                                 syms.and_expr, syms.shift_expr,
                                 syms.arith_expr, syms.term,
                                 syms.factor, syms.power):
-            new = pygram.parenthesize(new)
+            new = self.parenthesize(new)
         new.set_prefix(prefix)
         return new

Modified: sandbox/trunk/2to3/fixes/fix_intern.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_intern.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_intern.py	Thu Dec 28 22:50:13 2006
@@ -8,34 +8,23 @@
 
 # Local imports
 import pytree
-import patcomp
-import pygram
+from fixes import basefix
 
-syms = pygram.python_symbols
-pat_compile = patcomp.PatternCompiler().compile_pattern
 
-PATTERN = """
-power< 'intern'
-       trailer< lpar='('
-                ( not(arglist | argument<any '=' any>) obj=any
-                  | obj=arglist<(not argument<any '=' any>) any ','> )
-                rpar=')' >
-       after=any*
->
-"""
+class FixIntern(basefix.BaseFix):
 
-
-class FixIntern(object):
-
-    def __init__(self, options):
-        self.options = options
-        self.pattern = pat_compile(PATTERN)
-
-    def match(self, node):
-        results = {}
-        return self.pattern.match(node, results) and results
+    PATTERN = """
+    power< 'intern'
+           trailer< lpar='('
+                    ( not(arglist | argument<any '=' any>) obj=any
+                      | obj=arglist<(not argument<any '=' any>) any ','> )
+                    rpar=')' >
+           after=any*
+    >
+    """
 
     def transform(self, node):
+        syms = self.syms
         results = self.match(node)
         assert results
         obj = results["obj"].clone()
@@ -58,4 +47,3 @@
                           + after)
         new.set_prefix(node.get_prefix())
         return new
-                

Modified: sandbox/trunk/2to3/fixes/fix_ne.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_ne.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_ne.py	Thu Dec 28 22:50:13 2006
@@ -11,17 +11,17 @@
 
 # Local imports
 import pytree
-import pygram
+from fixes import basefix
 
-syms = pygram.python_symbols
 
+class FixNe(basefix.BaseFix):
 
-class FixNe(object):
-
-    def __init__(self, options):
-        self.options = options
+    def compile_pattern(self):
+        # Override
+        pass
 
     def match(self, node):
+        # Override
         return node.type == token.NOTEQUAL and node.value == "<>"
 
     def transform(self, node):

Deleted: /sandbox/trunk/2to3/fixes/fix_null.py
==============================================================================
--- /sandbox/trunk/2to3/fixes/fix_null.py	Thu Dec 28 22:50:13 2006
+++ (empty file)
@@ -1,40 +0,0 @@
-# Copyright 2006 Google, Inc. All Rights Reserved.
-# Licensed to PSF under a Contributor Agreement.
-
-"""Null fixer.  Use as a template."""
-
-
-class FixNull(object):
-
-    """Fixer class.
-
-    The class name must be FixFooBar where FooBar is the result of
-    removing underscores and capitalizing the words of the fix name.
-    For example, the class name for a fixer named 'has_key' should be
-    FixHasKey.
-    """
-
-    def __init__(self, options):
-        """Initializer.
-
-        The argument is an optparse.Values instance which can be used
-        to inspect the command line options.
-        """
-        self.options = options
-
-    def match(self, node):
-        """Matcher.
-
-        Should return a true or false object (not necessarily a bool).
-        It may return a non-empty dict of matching sub-nodes as
-        returned by a matching pattern.
-        """
-        return None
-
-    def transform(self, node):
-        """Transformer.
-
-        Should return None, or a node that is a modified copy of the
-        argument node.  The argument should not be modified in place.
-        """
-        return None

Modified: sandbox/trunk/2to3/fixes/fix_print.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_print.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_print.py	Thu Dec 28 22:50:13 2006
@@ -15,30 +15,24 @@
 
 # Local imports
 import pytree
-import patcomp
-import pygram
+from fixes import basefix
 
-syms = pygram.python_symbols
-pat_compile = patcomp.PatternCompiler().compile_pattern
 
-PATTERN = """
-'print' | print_stmt
-"""
+class FixPrint(basefix.BaseFix):
 
-
-class FixPrint(object):
-
-    def __init__(self, options):
-        self.options = options
-        self.pattern = pat_compile(PATTERN)
+    PATTERN = """
+    'print' | print_stmt
+    """
 
     def match(self, node):
-        if node.parent is not None and node.parent.type == syms.print_stmt:
+        # Override
+        if node.parent is not None and node.parent.type == self.syms.print_stmt:
             # Avoid matching 'print' as part of a print_stmt
             return None
         return self.pattern.match(node)
 
     def transform(self, node):
+        syms = self.syms
         results = self.match(node)
         assert results
 
@@ -68,13 +62,13 @@
             l_args[0].set_prefix("")
         if sep is not None or end is not None or file is not None:
             if sep is not None:
-                add_kwarg(l_args, "sep",
-                          pytree.Leaf(token.STRING, repr(sep)))
+                self.add_kwarg(l_args, "sep",
+                               pytree.Leaf(token.STRING, repr(sep)))
             if end is not None:
-                add_kwarg(l_args, "end",
-                          pytree.Leaf(token.STRING, repr(end)))
+                self.add_kwarg(l_args, "end",
+                               pytree.Leaf(token.STRING, repr(end)))
             if file is not None:
-                add_kwarg(l_args, "file", file)
+                self.add_kwarg(l_args, "file", file)
         if l_args:
             n_arglist = pytree.Node(syms.arglist, l_args)
         else:
@@ -87,14 +81,14 @@
         n_stmt.set_prefix(node.get_prefix())
         return n_stmt
 
-def add_kwarg(l_nodes, s_kwd, n_expr):
-    # XXX All this prefix-setting may lose comments (though rarely)
-    n_expr.set_prefix("")
-    n_argument = pytree.Node(syms.argument,
-                             (pytree.Leaf(token.NAME, s_kwd),
-                              pytree.Leaf(token.EQUAL, "="),
-                              n_expr))
-    if l_nodes:
-        l_nodes.append(pytree.Leaf(token.COMMA, ","))
-        n_argument.set_prefix(" ")
-    l_nodes.append(n_argument)
+    def add_kwarg(self, l_nodes, s_kwd, n_expr):
+        # XXX All this prefix-setting may lose comments (though rarely)
+        n_expr.set_prefix("")
+        n_argument = pytree.Node(self.syms.argument,
+                                 (pytree.Leaf(token.NAME, s_kwd),
+                                  pytree.Leaf(token.EQUAL, "="),
+                                  n_expr))
+        if l_nodes:
+            l_nodes.append(pytree.Leaf(token.COMMA, ","))
+            n_argument.set_prefix(" ")
+        l_nodes.append(n_argument)


More information about the Python-checkins mailing list