Home | History | Annotate | Download | only in fixes
      1 # Copyright 2007 Google, Inc. All Rights Reserved.
      2 # Licensed to PSF under a Contributor Agreement.
      3 
      4 """Fixer that changes xrange(...) into range(...)."""
      5 
      6 # Local imports
      7 from .. import fixer_base
      8 from ..fixer_util import Name, Call, consuming_calls
      9 from .. import patcomp
     10 
     11 
     12 class FixXrange(fixer_base.BaseFix):
     13     BM_compatible = True
     14     PATTERN = """
     15               power<
     16                  (name='range'|name='xrange') trailer< '(' args=any ')' >
     17               rest=any* >
     18               """
     19 
     20     def start_tree(self, tree, filename):
     21         super(FixXrange, self).start_tree(tree, filename)
     22         self.transformed_xranges = set()
     23 
     24     def finish_tree(self, tree, filename):
     25         self.transformed_xranges = None
     26 
     27     def transform(self, node, results):
     28         name = results["name"]
     29         if name.value == u"xrange":
     30             return self.transform_xrange(node, results)
     31         elif name.value == u"range":
     32             return self.transform_range(node, results)
     33         else:
     34             raise ValueError(repr(name))
     35 
     36     def transform_xrange(self, node, results):
     37         name = results["name"]
     38         name.replace(Name(u"range", prefix=name.prefix))
     39         # This prevents the new range call from being wrapped in a list later.
     40         self.transformed_xranges.add(id(node))
     41 
     42     def transform_range(self, node, results):
     43         if (id(node) not in self.transformed_xranges and
     44             not self.in_special_context(node)):
     45             range_call = Call(Name(u"range"), [results["args"].clone()])
     46             # Encase the range call in list().
     47             list_call = Call(Name(u"list"), [range_call],
     48                              prefix=node.prefix)
     49             # Put things that were after the range() call after the list call.
     50             for n in results["rest"]:
     51                 list_call.append_child(n)
     52             return list_call
     53 
     54     P1 = "power< func=NAME trailer< '(' node=any ')' > any* >"
     55     p1 = patcomp.compile_pattern(P1)
     56 
     57     P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
     58             | comp_for< 'for' any 'in' node=any any* >
     59             | comparison< any 'in' node=any any*>
     60          """
     61     p2 = patcomp.compile_pattern(P2)
     62 
     63     def in_special_context(self, node):
     64         if node.parent is None:
     65             return False
     66         results = {}
     67         if (node.parent.parent is not None and
     68                self.p1.match(node.parent.parent, results) and
     69                results["node"] is node):
     70             # list(d.keys()) -> list(d.keys()), etc.
     71             return results["func"].value in consuming_calls
     72         # for ... in d.iterkeys() -> for ... in d.keys(), etc.
     73         return self.p2.match(node.parent, results) and results["node"] is node
     74