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