]> granicus.if.org Git - python/commitdiff
Merged revisions 87876-87877 via svnmerge from
authorGeorg Brandl <georg@python.org>
Sun, 9 Jan 2011 07:53:14 +0000 (07:53 +0000)
committerGeorg Brandl <georg@python.org>
Sun, 9 Jan 2011 07:53:14 +0000 (07:53 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r87876 | georg.brandl | 2011-01-09 08:38:51 +0100 (So, 09 Jan 2011) | 1 line

  #10869: do not visit root node twice in ast.increment_lineno().
........
  r87877 | georg.brandl | 2011-01-09 08:50:48 +0100 (So, 09 Jan 2011) | 1 line

  Add missing line.
........

Doc/library/ast.rst
Lib/ast.py
Lib/test/test_ast.py
Misc/ACKS
Misc/NEWS

index c62094517171b744936ac309a153b07b1e665ca6..039fe41f1890ee645f412a08611b8c2046eb943c 100644 (file)
@@ -187,9 +187,9 @@ and classes for traversing abstract syntax trees:
 
 .. function:: walk(node)
 
-   Recursively yield all child nodes of *node*, in no specified order.  This is
-   useful if you only want to modify nodes in place and don't care about the
-   context.
+   Recursively yield all descendant nodes in the tree starting at *node*
+   (including *node* itself), in no specified order.  This is useful if you only
+   want to modify nodes in place and don't care about the context.
 
 
 .. class:: NodeVisitor()
index d778a859ad79d237026bcfbd2762f9e4019b6dd1..7f35f182e9887bcfdfd90c434eeb8f100837bee5 100644 (file)
@@ -152,8 +152,6 @@ def increment_lineno(node, n=1):
     Increment the line number of each node in the tree starting at *node* by *n*.
     This is useful to "move code" to a different location in a file.
     """
-    if 'lineno' in node._attributes:
-        node.lineno = getattr(node, 'lineno', 0) + n
     for child in walk(node):
         if 'lineno' in child._attributes:
             child.lineno = getattr(child, 'lineno', 0) + n
@@ -204,9 +202,9 @@ def get_docstring(node, clean=True):
 
 def walk(node):
     """
-    Recursively yield all child nodes of *node*, in no specified order.  This is
-    useful if you only want to modify nodes in place and don't care about the
-    context.
+    Recursively yield all descendant nodes in the tree starting at *node*
+    (including *node* itself), in no specified order.  This is useful if you
+    only want to modify nodes in place and don't care about the context.
     """
     from collections import deque
     todo = deque([node])
index 05ce09e3ca722f865e0430ce04b8c2d6860b0ba7..c1574ec76b212d711d0531e0ee79a23f434e66dd 100644 (file)
@@ -264,6 +264,14 @@ class ASTHelpers_Test(unittest.TestCase):
             'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
             'col_offset=0))'
         )
+        # issue10869: do not increment lineno of root twice
+        src = ast.parse('1 + 1', mode='eval')
+        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
+        self.assertEqual(ast.dump(src, include_attributes=True),
+            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
+            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
+            'col_offset=0))'
+        )
 
     def test_iter_fields(self):
         node = ast.parse('foo()', mode='eval')
index 8f55c5dbd52f9f3c233248f4a0ff5670343fd01a..e84040d95f57794d06ed57e70c80269aa565a48e 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -848,6 +848,7 @@ Cliff Wells
 Rickard Westman
 Jeff Wheeler
 Christopher White
+David White
 Mats Wichmann
 Truida Wiedijk
 Felix Wiemann
index e4362789709675bf3dea16ae02a47370db76ef7b..d5b36a1b5cbde6a6837acd266a121bc1c6495f91 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@ Library
   dependent, but ``time.mktime`` will now accept full range supported
   by the OS.  Conversion of 2-digit years to 4-digit is deprecated.
 
+- Issue #10869: Fixed bug where ast.increment_lineno modified the root
+  node twice.
+
 - Issue #7858: Raise an error properly when os.utime() fails under Windows
   on an existing file.