lib2to3 pattern creation with unexpected results

Benjamin Peterson benjamin at python.org
Tue Dec 22 18:21:49 EST 2009


Zac Burns <zac256 <at> gmail.com> writes:

> 
> 
> Greetings,I'm trying to re-purpose the lib2to3 module and along the way came
up with this pattern:"funcdef<'def' name=NAME parameters ['->' test] ':'
suite=suite>"It seems to have 2 problems:
> 
> Single-line defs are not matched. Eg: "def singleLineFunc(): return 1 + 2" is
not matched, but "def multiLineFunc():\n   a = 1 + 2\n   return a" is matched.

The pattern for that is funcdef< 'def' 'singleLineFunc' parameters< '(' ')' >
':' simple_stmt< return_stmt< 'return' arith_expr< '1' '+' '2' > > '\n' > >. No
suite.

> 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?

> 
> Why are these happening? If anyone could point me to a general discussion
about creating these would be most helpful. I haven't found a lot of resources
for these and investigating has been more of a struggle than usual.

Yes, 2to3 pattern matching is poorly documented. scripts/find_pattern.py is your
friend.






More information about the Python-list mailing list