1 | n/a | """Fixer for it.next() -> next(it), per PEP 3114.""" |
---|
2 | n/a | # Author: Collin Winter |
---|
3 | n/a | |
---|
4 | n/a | # Things that currently aren't covered: |
---|
5 | n/a | # - listcomp "next" names aren't warned |
---|
6 | n/a | # - "with" statement targets aren't checked |
---|
7 | n/a | |
---|
8 | n/a | # Local imports |
---|
9 | n/a | from ..pgen2 import token |
---|
10 | n/a | from ..pygram import python_symbols as syms |
---|
11 | n/a | from .. import fixer_base |
---|
12 | n/a | from ..fixer_util import Name, Call, find_binding |
---|
13 | n/a | |
---|
14 | n/a | bind_warning = "Calls to builtin next() possibly shadowed by global binding" |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | class FixNext(fixer_base.BaseFix): |
---|
18 | n/a | BM_compatible = True |
---|
19 | n/a | PATTERN = """ |
---|
20 | n/a | power< base=any+ trailer< '.' attr='next' > trailer< '(' ')' > > |
---|
21 | n/a | | |
---|
22 | n/a | power< head=any+ trailer< '.' attr='next' > not trailer< '(' ')' > > |
---|
23 | n/a | | |
---|
24 | n/a | classdef< 'class' any+ ':' |
---|
25 | n/a | suite< any* |
---|
26 | n/a | funcdef< 'def' |
---|
27 | n/a | name='next' |
---|
28 | n/a | parameters< '(' NAME ')' > any+ > |
---|
29 | n/a | any* > > |
---|
30 | n/a | | |
---|
31 | n/a | global=global_stmt< 'global' any* 'next' any* > |
---|
32 | n/a | """ |
---|
33 | n/a | |
---|
34 | n/a | order = "pre" # Pre-order tree traversal |
---|
35 | n/a | |
---|
36 | n/a | def start_tree(self, tree, filename): |
---|
37 | n/a | super(FixNext, self).start_tree(tree, filename) |
---|
38 | n/a | |
---|
39 | n/a | n = find_binding('next', tree) |
---|
40 | n/a | if n: |
---|
41 | n/a | self.warning(n, bind_warning) |
---|
42 | n/a | self.shadowed_next = True |
---|
43 | n/a | else: |
---|
44 | n/a | self.shadowed_next = False |
---|
45 | n/a | |
---|
46 | n/a | def transform(self, node, results): |
---|
47 | n/a | assert results |
---|
48 | n/a | |
---|
49 | n/a | base = results.get("base") |
---|
50 | n/a | attr = results.get("attr") |
---|
51 | n/a | name = results.get("name") |
---|
52 | n/a | |
---|
53 | n/a | if base: |
---|
54 | n/a | if self.shadowed_next: |
---|
55 | n/a | attr.replace(Name("__next__", prefix=attr.prefix)) |
---|
56 | n/a | else: |
---|
57 | n/a | base = [n.clone() for n in base] |
---|
58 | n/a | base[0].prefix = "" |
---|
59 | n/a | node.replace(Call(Name("next", prefix=node.prefix), base)) |
---|
60 | n/a | elif name: |
---|
61 | n/a | n = Name("__next__", prefix=name.prefix) |
---|
62 | n/a | name.replace(n) |
---|
63 | n/a | elif attr: |
---|
64 | n/a | # We don't do this transformation if we're assigning to "x.next". |
---|
65 | n/a | # Unfortunately, it doesn't seem possible to do this in PATTERN, |
---|
66 | n/a | # so it's being done here. |
---|
67 | n/a | if is_assign_target(node): |
---|
68 | n/a | head = results["head"] |
---|
69 | n/a | if "".join([str(n) for n in head]).strip() == '__builtin__': |
---|
70 | n/a | self.warning(node, bind_warning) |
---|
71 | n/a | return |
---|
72 | n/a | attr.replace(Name("__next__")) |
---|
73 | n/a | elif "global" in results: |
---|
74 | n/a | self.warning(node, bind_warning) |
---|
75 | n/a | self.shadowed_next = True |
---|
76 | n/a | |
---|
77 | n/a | |
---|
78 | n/a | ### The following functions help test if node is part of an assignment |
---|
79 | n/a | ### target. |
---|
80 | n/a | |
---|
81 | n/a | def is_assign_target(node): |
---|
82 | n/a | assign = find_assign(node) |
---|
83 | n/a | if assign is None: |
---|
84 | n/a | return False |
---|
85 | n/a | |
---|
86 | n/a | for child in assign.children: |
---|
87 | n/a | if child.type == token.EQUAL: |
---|
88 | n/a | return False |
---|
89 | n/a | elif is_subtree(child, node): |
---|
90 | n/a | return True |
---|
91 | n/a | return False |
---|
92 | n/a | |
---|
93 | n/a | def find_assign(node): |
---|
94 | n/a | if node.type == syms.expr_stmt: |
---|
95 | n/a | return node |
---|
96 | n/a | if node.type == syms.simple_stmt or node.parent is None: |
---|
97 | n/a | return None |
---|
98 | n/a | return find_assign(node.parent) |
---|
99 | n/a | |
---|
100 | n/a | def is_subtree(root, node): |
---|
101 | n/a | if root == node: |
---|
102 | n/a | return True |
---|
103 | n/a | return any(is_subtree(c, node) for c in root.children) |
---|