[Python-checkins] r54964 - in sandbox/trunk/2to3: example.py fixes/fix_ws_comma.py pytree.py refactor.py

guido.van.rossum python-checkins at python.org
Wed Apr 25 21:47:58 CEST 2007


Author: guido.van.rossum
Date: Wed Apr 25 21:47:49 2007
New Revision: 54964

Added:
   sandbox/trunk/2to3/fixes/fix_ws_comma.py   (contents, props changed)
Modified:
   sandbox/trunk/2to3/example.py
   sandbox/trunk/2to3/pytree.py
   sandbox/trunk/2to3/refactor.py
Log:
Example fixer that normalizes the whitespace around commas (and around colons
in dict displays).  This required a few minor tweaks elsewhere.


Modified: sandbox/trunk/2to3/example.py
==============================================================================
--- sandbox/trunk/2to3/example.py	(original)
+++ sandbox/trunk/2to3/example.py	Wed Apr 25 21:47:49 2007
@@ -65,6 +65,13 @@
 def foo():
 	pass # body indented by tab
 
+def test_ws_comma():
+    yield 1,2 ,3
+    f(1,2 ,3)
+    `a ,b`
+    def f(a,b ,c): pass
+    { a:b,c:d , e : f }
+
 def apply_examples():
     x = apply(f, g + h)
     y = apply(f, g, h)

Added: sandbox/trunk/2to3/fixes/fix_ws_comma.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_ws_comma.py	Wed Apr 25 21:47:49 2007
@@ -0,0 +1,37 @@
+"""Fixer that changes 'a ,b' into 'a, b'.
+
+This also changes '{a :b}' into '{a: b}', but does not touch other
+uses of colons.  It does not touch other uses of whitespace.
+
+"""
+
+import pytree
+from pgen2 import token
+from fixes import basefix
+
+class FixWsComma(basefix.BaseFix):
+
+  PATTERN = """
+  any<(not(',') any)+ ',' ((not(',') any)+ ',')* [not(',') any]>
+  """
+
+  COMMA = pytree.Leaf(token.COMMA, ",")
+  COLON = pytree.Leaf(token.COLON, ":")
+  SEPS = (COMMA, COLON)
+
+  def transform(self, node):
+    new = node.clone()
+    comma = False
+    for child in new.children:
+      if child in self.SEPS:
+        prefix = child.get_prefix()
+        if prefix.isspace() and "\n" not in prefix:
+          child.set_prefix("")
+        comma = True
+      else:
+        if comma:
+          prefix = child.get_prefix()
+          if not prefix:
+            child.set_prefix(" ")
+        comma = False
+    return new

Modified: sandbox/trunk/2to3/pytree.py
==============================================================================
--- sandbox/trunk/2to3/pytree.py	(original)
+++ sandbox/trunk/2to3/pytree.py	Wed Apr 25 21:47:49 2007
@@ -460,8 +460,9 @@
         """Initializer.  Takes optional type, content, and name.
 
         The type, if given, must be a symbol type (>= 256).  If the
-        type is None, the content must be None, and this matches *any*
-        single node (leaf or not).
+        type is None this matches *any* single node (leaf or not),
+        except if content is not None, in which it only matches
+        non-leaf nodes that also match the content pattern.
 
         The content, if not None, must be a sequence of Patterns that
         must match the node's children exactly.  If the content is
@@ -472,8 +473,6 @@
         """
         if type is not None:
             assert type >= 256, type
-        else:
-            assert content is None, repr(content)
         if content is not None:
             assert not isinstance(content, basestring), repr(content)
             content = list(content)

Modified: sandbox/trunk/2to3/refactor.py
==============================================================================
--- sandbox/trunk/2to3/refactor.py	(original)
+++ sandbox/trunk/2to3/refactor.py	Wed Apr 25 21:47:49 2007
@@ -256,7 +256,8 @@
             for fixer in self.fixers:
                 if fixer.match(node):
                     new = fixer.transform(node)
-                    if new is not None and new != node:
+                    if new is not None and (new != node or
+                                            str(new) != str(node)):
                         node.replace(new)
                         changes += 1
                     elif tree.was_changed:


More information about the Python-checkins mailing list