[Python-porting] Fw: lib2to3 and matching of fixers

Vinay Sajip vinay_sajip at yahoo.co.uk
Wed Dec 14 21:38:35 CET 2011


Benjamin Peterson <benjamin at ...> writes:

> 
> You have to account for the newline.
> 

Thanks for the response, but it's not clear how or why. As to the "why": the
portion of the tree I get by looking at

tree.children[0].children[0].children[2]

is

Node(atom, [
    Leaf(7, '('),
    Node(testlist_gexp, [
        Leaf(1, 'int'),
        Leaf(12, ','),
        Leaf(1, 'long')
    ]),
    Leaf(8, ')')
])

which is the bit I expect to match. It does not contain the newline (that's
present as the last child the enclosing grandparent node (simple_stmt) obtained
by looking at tree.children[0]:

Node(simple_stmt, [
    Node(expr_stmt, [
        Leaf(1, 'x'),
        Leaf(22, '='),
        Node(atom, [
            Leaf(7, '('),
            Node(testlist_gexp, [
                Leaf(1, 'int'),
                Leaf(12, ','),
                Leaf(1, 'long')
            ]),
            Leaf(8, ')')
        ])
    ]),
    Leaf(4, '\n')
])

The pattern I used appears consistent with the subset of the pattern used in
the is_instance fixer to match a tuple in the isinstance call, other than
whitespace in the pattern string itself:

    power<
        'isinstance'
        trailer< '(' arglist< any ',' atom< '('
            args=testlist_gexp< any+ >
        ')' > > ')' >
    >

in the is_instance fixer, as against my fixer's

    atom< '(' args=testlist_gexp< any+ > ')' >

So I can't see why the newline needs to be accounted for, nor how to do it.
Arranging the whitespace in my pattern to exactly match that segment in the
is_instance fixer's pattern made no difference, as I would have expected.

So, how do I account for the newline, and why do I need to when it's two levels
up in the tree?

Thanks for your patience with lib2to3 newbie.

Regards,

Vinay Sajip




More information about the Python-porting mailing list