[Python-checkins] r73972 - in sandbox/trunk/2to3/lib2to3: fixer_base.py fixes/fix_ne.py fixes/fix_numliterals.py patcomp.py refactor.py

benjamin.peterson python-checkins at python.org
Sun Jul 12 04:25:45 CEST 2009


Author: benjamin.peterson
Date: Sun Jul 12 04:25:45 2009
New Revision: 73972

Log:
try to make the head node dict as sparse as possible

Modified:
   sandbox/trunk/2to3/lib2to3/fixer_base.py
   sandbox/trunk/2to3/lib2to3/fixes/fix_ne.py
   sandbox/trunk/2to3/lib2to3/fixes/fix_numliterals.py
   sandbox/trunk/2to3/lib2to3/patcomp.py
   sandbox/trunk/2to3/lib2to3/refactor.py

Modified: sandbox/trunk/2to3/lib2to3/fixer_base.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixer_base.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixer_base.py	Sun Jul 12 04:25:45 2009
@@ -33,6 +33,8 @@
     explicit = False # Is this ignored by refactor.py -f all?
     run_order = 5   # Fixers will be sorted by run order before execution
                     # Lower numbers will be run first.
+    _accept_type = None # [Advanced and not public] This tells RefactoringTool
+                        # which node type to accept when there's not a pattern.
 
     # Shortcut for access to Python grammar symbols
     syms = pygram.python_symbols

Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_ne.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_ne.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_ne.py	Sun Jul 12 04:25:45 2009
@@ -12,9 +12,11 @@
 class FixNe(fixer_base.BaseFix):
     # This is so simple that we don't need the pattern compiler.
 
+    _accept_type = token.NOTEQUAL
+
     def match(self, node):
         # Override
-        return node.type == token.NOTEQUAL and node.value == u"<>"
+        return node.value == u"<>"
 
     def transform(self, node, results):
         new = pytree.Leaf(token.NOTEQUAL, u"!=", prefix=node.prefix)

Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_numliterals.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_numliterals.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_numliterals.py	Sun Jul 12 04:25:45 2009
@@ -12,10 +12,11 @@
 class FixNumliterals(fixer_base.BaseFix):
     # This is so simple that we don't need the pattern compiler.
 
+    _accept_type = token.NUMBER
+
     def match(self, node):
         # Override
-        return (node.type == token.NUMBER and
-                (node.value.startswith(u"0") or node.value[-1] in u"Ll"))
+        return (node.value.startswith(u"0") or node.value[-1] in u"Ll")
 
     def transform(self, node, results):
         val = node.value

Modified: sandbox/trunk/2to3/lib2to3/patcomp.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/patcomp.py	(original)
+++ sandbox/trunk/2to3/lib2to3/patcomp.py	Sun Jul 12 04:25:45 2009
@@ -14,7 +14,7 @@
 import os
 
 # Fairly local imports
-from .pgen2 import driver, literals, token, tokenize, parse
+from .pgen2 import driver, literals, token, tokenize, parse, grammar
 
 # Really local imports
 from . import pytree
@@ -138,7 +138,7 @@
         node = nodes[0]
         if node.type == token.STRING:
             value = unicode(literals.evalString(node.value))
-            return pytree.LeafPattern(content=value)
+            return pytree.LeafPattern(_type_of_literal(value), value)
         elif node.type == token.NAME:
             value = node.value
             if value.isupper():
@@ -179,6 +179,15 @@
              "TOKEN": None}
 
 
+def _type_of_literal(value):
+    if value[0].isalpha():
+        return token.NAME
+    elif value in grammar.opmap:
+        return grammar.opmap[value]
+    else:
+        return None
+
+
 def pattern_convert(grammar, raw_node_info):
     """Converts raw node information to a Node or Leaf instance."""
     type, value, context, children = raw_node_info

Modified: sandbox/trunk/2to3/lib2to3/refactor.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/refactor.py	(original)
+++ sandbox/trunk/2to3/lib2to3/refactor.py	Sun Jul 12 04:25:45 2009
@@ -84,7 +84,10 @@
                 for node_type in heads:
                     head_nodes[node_type].append(fixer)
         else:
-            every.append(fixer)
+            if fixer._accept_type is not None:
+                head_nodes[fixer._accept_type].append(fixer)
+            else:
+                every.append(fixer)
     for node_type in chain(pygram.python_grammar.symbol2number.itervalues(),
                            pygram.python_grammar.tokens):
         head_nodes[node_type].extend(every)


More information about the Python-checkins mailing list