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