[Python-checkins] r54420 - in sandbox/trunk/2to3: HACKING fixes/fix_tuple_params.py pytree.py refactor.py tests/test_pytree.py
collin.winter
python-checkins at python.org
Sat Mar 17 08:02:51 CET 2007
Author: collin.winter
Date: Sat Mar 17 08:02:46 2007
New Revision: 54420
Added:
sandbox/trunk/2to3/HACKING (contents, props changed)
Modified:
sandbox/trunk/2to3/fixes/fix_tuple_params.py
sandbox/trunk/2to3/pytree.py
sandbox/trunk/2to3/refactor.py
sandbox/trunk/2to3/tests/test_pytree.py
Log:
Add a mechanism to explicitly flag a tree as changed. This comes in handy when a fixer works by modifying a node's children or a leaf's value.
Added: sandbox/trunk/2to3/HACKING
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/HACKING Sat Mar 17 08:02:46 2007
@@ -0,0 +1,8 @@
+Tips/tricks/hints for writing new fixers:
+
+ * Don't write your own PATTERN from scratch; that's what find_pattern.py
+ is for.
+
+ * If your fixer works by changing a node's children list or a leaf's
+ value, be sure to call the node/leaf's changed() method. This to
+ be sure refactor.py will recognize that the tree has changed.
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 Sat Mar 17 08:02:46 2007
@@ -84,3 +84,4 @@
for i in range(after+1, after+len(new_lines)+1):
children[i].set_prefix(indent)
suite[0].children = tuple(children)
+ suite[0].changed()
Modified: sandbox/trunk/2to3/pytree.py
==============================================================================
--- sandbox/trunk/2to3/pytree.py (original)
+++ sandbox/trunk/2to3/pytree.py Sat Mar 17 08:02:46 2007
@@ -29,6 +29,7 @@
type = None # int: token number (< 256) or symbol number (>= 256)
parent = None # Parent node pointer, or None
children = () # Tuple of subnodes
+ was_changed = False
def __new__(cls, *args, **kwds):
"""Constructor that prevents Base from being instantiated."""
@@ -111,6 +112,7 @@
else:
l_children.append(ch)
assert found, (self.children, self, new)
+ self.changed()
self.parent.children = tuple(l_children)
if new is not None:
new.parent = self.parent
@@ -124,6 +126,11 @@
return
node = node.children[0]
return node.lineno
+
+ def changed(self):
+ if self.parent:
+ self.parent.changed()
+ self.was_changed = True
class Node(Base):
Modified: sandbox/trunk/2to3/refactor.py
==============================================================================
--- sandbox/trunk/2to3/refactor.py (original)
+++ sandbox/trunk/2to3/refactor.py Sat Mar 17 08:02:46 2007
@@ -231,6 +231,8 @@
if new is not None and new != node:
node.replace(new)
changes += 1
+ elif tree.was_changed:
+ changes += 1
for fixer in self.fixers:
fixer.finish_tree(tree, filename)
return changes
Modified: sandbox/trunk/2to3/tests/test_pytree.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_pytree.py (original)
+++ sandbox/trunk/2to3/tests/test_pytree.py Sat Mar 17 08:02:46 2007
@@ -117,13 +117,45 @@
l3 = pytree.Leaf(100, "bar")
n1 = pytree.Node(1000, [l1, l2, l3])
self.assertEqual(n1.children, (l1, l2, l3))
+ self.failIf(n1.was_changed)
l2new = pytree.Leaf(100, "-")
l2.replace(l2new)
self.assertEqual(n1.children, (l1, l2new, l3))
+ self.failUnless(n1.was_changed)
def testConvert(self):
# XXX
pass
+
+ def testChangedLeaf(self):
+ l1 = pytree.Leaf(100, "f")
+ self.failIf(l1.was_changed)
+
+ l1.changed()
+ self.failUnless(l1.was_changed)
+
+ def testChangedNode(self):
+ l1 = pytree.Leaf(100, "f")
+ n1 = pytree.Node(1000, [l1])
+ self.failIf(n1.was_changed)
+
+ n1.changed()
+ self.failUnless(n1.was_changed)
+
+ def testChangedRecursive(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "+")
+ l3 = pytree.Leaf(100, "bar")
+ n1 = pytree.Node(1000, [l1, l2, l3])
+ n2 = pytree.Node(1000, [n1])
+ self.failIf(l1.was_changed)
+ self.failIf(n1.was_changed)
+ self.failIf(n2.was_changed)
+
+ n1.changed()
+ self.failUnless(n1.was_changed)
+ self.failUnless(n2.was_changed)
+ self.failIf(l1.was_changed)
class TestPatterns(support.TestCase):
More information about the Python-checkins
mailing list