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