On 8/24/07, Collin Winter <[EMAIL PROTECTED]> wrote:
> Adrian and I talked about this this morning, and he said he's going to
> go ahead with an implementation. The original warning messages were a
> good idea, but they tend to get lost when converting large projects.

(I assume this is the place to post patches for the 2to3 utility, but
please set me straight if I should use bugs.python.org instead...)

I've attached two patches that implement the 2to3 change discussed in
this thread. In 2to3_insert_comment.diff --

* fixes/util.py gets an insert_comment() function. Give it a Node/Leaf
and a comment message, and it will insert a Python comment before the
given Node/Leaf. This takes indentation into account, such that the
comment will be indented to fix the indentation of the line it is
commenting. For example:

    if foo:
        # comment about bar()
        bar()

It also handles existing comments gracefully. If a line already has a
comment above it, the new comment will be added on a new line under
the old one.

* pytree.Base gets two new methods: get_previous_sibling() and
get_previous_in_tree(). These just made it easier and clearer to
implement insert_comment().

* tests/test_util.py has unit tests for insert_comment(), and
tests/test_pytree.py has tests for the two new pytree.Base methods.

The other patch, 2to3_comment_warnings.diff, is an example of how we
could integrate this new insert_comment() method to replace the
current functionality of fixes.basefix.BaseFix.warning(). To see this
in action, apply these two patches and run the 2to3 script
(refactor.py) on the following input:

    foo()
    map(f, x)

The resulting output should display a Python comment above the map()
call instead of outputting a warning to stdout, which was the previous
behavior.

If these patches are accepted, the next steps would be to change the
behavior of warns() and warns_unchanged() in tests/test_fixers.py, so
that the tests can catch the new behavior.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

Attachment: 2to3_comment_warnings.diff
Description: Binary data

Attachment: 2to3_insert_comment.diff
Description: Binary data

_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to