lib2to3 pattern creation with unexpected results

Zac Burns zac256 at gmail.com
Tue Dec 22 19:24:35 EST 2009


On Tue, Dec 22, 2009 at 3:21 PM, Benjamin Peterson <benjamin at python.org>wrote:

>
> The pattern for that is funcdef< 'def' 'singleLineFunc' parameters< '(' ')'
> >
> ':' simple_stmt< return_stmt< 'return' arith_expr< '1' '+' '2' > > '\n' >
> >. No
> suite.
>
>
I'm trying to match any function block, the two examples were just a part of
my unit tests.



> > The first multi-line function in a file is matched twice (eg, the same
> node,
> results pair is passed to the transform method.
>
> That is odd and maybe a 2to3 bug. Could you post an example?
>
>
Ah, found the problem here - I was running a touch_import which probably
caused the node iterator to doubly-visit. Without the touch_import this
works fine.


>
>
> Yes, 2to3 pattern matching is poorly documented. scripts/find_pattern.py is
> your
> friend.
>
>
This does not appear to come with python (just checked 2.6.4x86) but did
find it in the trunk. Useful for sure, but it seems to only return results
like the one you gave - very specific and you have to know what's going on
to generalize.

I eventually arrived on this pattern:
"""
    funcdef<'def' name=NAME parameters ['->' test] ':' suite=suite>
    |
    funcdef<'def' name=NAME parameters ['->' test] ':' suite=simple_stmt>
"""

Which, though it works I'm still curious why because I'll be writing some
more patterns here on out.
The problem I have is that the grammar defines suite to be this:
"simple_stmt | NEWLINE INDENT stmt+ DEDENT" So, why should my change have
any effect?

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091222/8eb68cb4/attachment-0001.html>


More information about the Python-list mailing list