This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients aronacher, benjamin.peterson, daishiharada, docs@python, georg.brandl, terry.reedy
Date 2015-06-29.20:44:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1435610683.78.0.840867406363.issue3530@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2015-06-29 20:44:43terry.reedysetrecipients: + terry.reedy, georg.brandl, daishiharada, benjamin.peterson, aronacher, docs@python
2015-06-29 20:44:43terry.reedysetmessageid: <1435610683.78.0.840867406363.issue3530@psf.upfronthosting.co.za>
2015-06-29 20:44:43terry.reedylinkissue3530 messages
2015-06-29 20:44:43terry.reedycreate