ยปCore Development>Code coverage>Lib/lib2to3/fixes/fix_next.py

Python code coverage for Lib/lib2to3/fixes/fix_next.py

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