Terry J. Reedy added the comment:

I am reopening this as a doc bug because RewriteName is a copy (with 'ast.' 
prefixes added) of a buggy example in the doc. The bug is that the new .value 
Name and Str attributes do not get the required 'lineno' and 'col_offset' 
attributes.  As Armin said, copy_location is not recursive and does not fix 
children of the node it fixes.  Also, the recursive .visit method does not 
recurse into children of replacement nodes (and if it did, the new Str node 
would still not be fixed).

The fix could be to reuse the Name node and add another copy_location call: the 
following works.

    def visit_Name(self, node):
        return ast.copy_location(
            ast.Subscript(
                value=node,
                slice=ast.Index(value=ast.copy_location(
                    ast.Str(s=node.id), node)),
                ctx=node.ctx),
            node)

but I think this illustrates that comment in the fix_missing_locations() entry 
that locations are "tedious to fill in for generated nodes".  So I think the 
doc fix should use Armin's version of RewriteName and say to call 
fix_missing_locations on the result of .visit if new nodes are added.  (I 
checked that his code still works in 3.5).

The entry for NodeTransformer might mention that .visit does not recurse into 
replacement nodes.

The missing lineno error came up in this python-list thread:
https://mail.python.org/pipermail/python-list/2015-June/693316.html

----------
assignee:  -> docs@python
components: +Documentation
nosy: +benjamin.peterson, docs@python, terry.reedy
resolution: not a bug -> 
stage:  -> needs patch
status: closed -> open
title: ast.NodeTransformer bug -> ast.NodeTransformer doc bug
type:  -> behavior
versions: +Python 2.7, Python 3.4, Python 3.5, Python 3.6 -Python 2.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue3530>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to