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