[Python-checkins] r83797 - in sandbox/trunk/2to3/lib2to3: fixer_util.py tests/test_util.py
benjamin.peterson
python-checkins at python.org
Sun Aug 8 01:54:51 CEST 2010
Author: benjamin.peterson
Date: Sun Aug 8 01:54:51 2010
New Revision: 83797
Log:
add a function to find how a node is indented
Modified:
sandbox/trunk/2to3/lib2to3/fixer_util.py
sandbox/trunk/2to3/lib2to3/tests/test_util.py
Modified: sandbox/trunk/2to3/lib2to3/fixer_util.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixer_util.py (original)
+++ sandbox/trunk/2to3/lib2to3/fixer_util.py Sun Aug 8 01:54:51 2010
@@ -1,6 +1,8 @@
"""Utility functions, node construction macros, etc."""
# Author: Collin Winter
+from itertools import islice
+
# Local imports
from .pgen2 import token
from .pytree import Leaf, Node
@@ -245,6 +247,16 @@
return False
return True
+def find_indentation(node):
+ """Find the indentation of *node*."""
+ while node is not None:
+ if node.type == syms.suite and len(node.children) > 2:
+ indent = node.children[1]
+ if indent.type == token.INDENT:
+ return indent.value
+ node = node.parent
+ return u""
+
###########################################################
### The following functions are to find bindings in a suite
###########################################################
Modified: sandbox/trunk/2to3/lib2to3/tests/test_util.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/tests/test_util.py (original)
+++ sandbox/trunk/2to3/lib2to3/tests/test_util.py Sun Aug 8 01:54:51 2010
@@ -575,3 +575,20 @@
node = parse('bar()')
fixer_util.touch_import(None, "cgi", node)
self.assertEqual(str(node), 'import cgi\nbar()\n\n')
+
+class Test_find_indentation(support.TestCase):
+
+ def test_nothing(self):
+ fi = fixer_util.find_indentation
+ node = parse("node()")
+ self.assertEqual(fi(node), u"")
+ node = parse("")
+ self.assertEqual(fi(node), u"")
+
+ def test_simple(self):
+ fi = fixer_util.find_indentation
+ node = parse("def f():\n x()")
+ self.assertEqual(fi(node), u"")
+ self.assertEqual(fi(node.children[0].children[4].children[2]), u" ")
+ node = parse("def f():\n x()\n y()")
+ self.assertEqual(fi(node.children[0].children[4].children[4]), u" ")
More information about the Python-checkins
mailing list