1 """Fix "for x in f.xreadlines()" -> "for x in f". 2 3 This fixer will also convert g(f.xreadlines) into g(f.__iter__).""" 4 # Author: Collin Winter 5 6 # Local imports 7 from .. import fixer_base 8 from ..fixer_util import Name 9 10 11 class 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("__iter__", prefix=no_call.prefix)) 24 else: 25 node.replace([x.clone() for x in results["call"]]) 26