1 | n/a | """Fix "for x in f.xreadlines()" -> "for x in f". |
---|
2 | n/a | |
---|
3 | n/a | This fixer will also convert g(f.xreadlines) into g(f.__iter__).""" |
---|
4 | n/a | # Author: Collin Winter |
---|
5 | n/a | |
---|
6 | n/a | # Local imports |
---|
7 | n/a | from .. import fixer_base |
---|
8 | n/a | from ..fixer_util import Name |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | class FixXreadlines(fixer_base.BaseFix): |
---|
12 | n/a | BM_compatible = True |
---|
13 | n/a | PATTERN = """ |
---|
14 | n/a | power< call=any+ trailer< '.' 'xreadlines' > trailer< '(' ')' > > |
---|
15 | n/a | | |
---|
16 | n/a | power< any+ trailer< '.' no_call='xreadlines' > > |
---|
17 | n/a | """ |
---|
18 | n/a | |
---|
19 | n/a | def transform(self, node, results): |
---|
20 | n/a | no_call = results.get("no_call") |
---|
21 | n/a | |
---|
22 | n/a | if no_call: |
---|
23 | n/a | no_call.replace(Name("__iter__", prefix=no_call.prefix)) |
---|
24 | n/a | else: |
---|
25 | n/a | node.replace([x.clone() for x in results["call"]]) |
---|