]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/lib2to3/fixes/fix_next.py
bd0cfe9579e3d87c64c7e45a27055728adc163a0
1 """Fixer for it.next() -> next(it), per PEP 3114."""
2 # Author: Collin Winter
4 # Things that currently aren't covered:
5 # - listcomp "next" names aren't warned
6 # - "with" statement targets aren't checked
9 from ..pgen2
import token
10 from ..pygram
import python_symbols
as syms
11 from .. import fixer_base
12 from ..fixer_util
import Name
, Call
, find_binding
14 bind_warning
= "Calls to builtin next() possibly shadowed by global binding"
17 class FixNext(fixer_base
.BaseFix
):
20 power< base=any+ trailer< '.' attr='next' > trailer< '(' ')' > >
22 power< head=any+ trailer< '.' attr='next' > not trailer< '(' ')' > >
24 classdef< 'class' any+ ':'
28 parameters< '(' NAME ')' > any+ >
31 global=global_stmt< 'global' any* 'next' any* >
34 order
= "pre" # Pre-order tree traversal
36 def start_tree(self
, tree
, filename
):
37 super(FixNext
, self
).start_tree(tree
, filename
)
39 n
= find_binding(u
'next', tree
)
41 self
.warning(n
, bind_warning
)
42 self
.shadowed_next
= True
44 self
.shadowed_next
= False
46 def transform(self
, node
, results
):
49 base
= results
.get("base")
50 attr
= results
.get("attr")
51 name
= results
.get("name")
54 if self
.shadowed_next
:
55 attr
.replace(Name(u
"__next__", prefix
=attr
.prefix
))
57 base
= [n
.clone() for n
in base
]
59 node
.replace(Call(Name(u
"next", prefix
=node
.prefix
), base
))
61 n
= Name(u
"__next__", prefix
=name
.prefix
)
64 # We don't do this transformation if we're assigning to "x.next".
65 # Unfortunately, it doesn't seem possible to do this in PATTERN,
66 # so it's being done here.
67 if is_assign_target(node
):
68 head
= results
["head"]
69 if "".join([str(n
) for n
in head
]).strip() == u
'__builtin__':
70 self
.warning(node
, bind_warning
)
72 attr
.replace(Name(u
"__next__"))
73 elif "global" in results
:
74 self
.warning(node
, bind_warning
)
75 self
.shadowed_next
= True
78 ### The following functions help test if node is part of an assignment
81 def is_assign_target(node
):
82 assign
= find_assign(node
)
86 for child
in assign
.children
:
87 if child
.type == token
.EQUAL
:
89 elif is_subtree(child
, node
):
93 def find_assign(node
):
94 if node
.type == syms
.expr_stmt
:
96 if node
.type == syms
.simple_stmt
or node
.parent
is None:
98 return find_assign(node
.parent
)
100 def is_subtree(root
, node
):
103 return any(is_subtree(c
, node
) for c
in root
.children
)