Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python2..../lib2to3/fixes
File: fix_tuple_params.py
"""Fixer for function definitions with tuple parameters.
[0] Fix | Delete
[1] Fix | Delete
def func(((a, b), c), d):
[2] Fix | Delete
...
[3] Fix | Delete
[4] Fix | Delete
->
[5] Fix | Delete
[6] Fix | Delete
def func(x, d):
[7] Fix | Delete
((a, b), c) = x
[8] Fix | Delete
...
[9] Fix | Delete
[10] Fix | Delete
It will also support lambdas:
[11] Fix | Delete
[12] Fix | Delete
lambda (x, y): x + y -> lambda t: t[0] + t[1]
[13] Fix | Delete
[14] Fix | Delete
# The parens are a syntax error in Python 3
[15] Fix | Delete
lambda (x): x + y -> lambda x: x + y
[16] Fix | Delete
"""
[17] Fix | Delete
# Author: Collin Winter
[18] Fix | Delete
[19] Fix | Delete
# Local imports
[20] Fix | Delete
from .. import pytree
[21] Fix | Delete
from ..pgen2 import token
[22] Fix | Delete
from .. import fixer_base
[23] Fix | Delete
from ..fixer_util import Assign, Name, Newline, Number, Subscript, syms
[24] Fix | Delete
[25] Fix | Delete
def is_docstring(stmt):
[26] Fix | Delete
return isinstance(stmt, pytree.Node) and \
[27] Fix | Delete
stmt.children[0].type == token.STRING
[28] Fix | Delete
[29] Fix | Delete
class FixTupleParams(fixer_base.BaseFix):
[30] Fix | Delete
run_order = 4 #use a lower order since lambda is part of other
[31] Fix | Delete
#patterns
[32] Fix | Delete
BM_compatible = True
[33] Fix | Delete
[34] Fix | Delete
PATTERN = """
[35] Fix | Delete
funcdef< 'def' any parameters< '(' args=any ')' >
[36] Fix | Delete
['->' any] ':' suite=any+ >
[37] Fix | Delete
|
[38] Fix | Delete
lambda=
[39] Fix | Delete
lambdef< 'lambda' args=vfpdef< '(' inner=any ')' >
[40] Fix | Delete
':' body=any
[41] Fix | Delete
>
[42] Fix | Delete
"""
[43] Fix | Delete
[44] Fix | Delete
def transform(self, node, results):
[45] Fix | Delete
if "lambda" in results:
[46] Fix | Delete
return self.transform_lambda(node, results)
[47] Fix | Delete
[48] Fix | Delete
new_lines = []
[49] Fix | Delete
suite = results["suite"]
[50] Fix | Delete
args = results["args"]
[51] Fix | Delete
# This crap is so "def foo(...): x = 5; y = 7" is handled correctly.
[52] Fix | Delete
# TODO(cwinter): suite-cleanup
[53] Fix | Delete
if suite[0].children[1].type == token.INDENT:
[54] Fix | Delete
start = 2
[55] Fix | Delete
indent = suite[0].children[1].value
[56] Fix | Delete
end = Newline()
[57] Fix | Delete
else:
[58] Fix | Delete
start = 0
[59] Fix | Delete
indent = u"; "
[60] Fix | Delete
end = pytree.Leaf(token.INDENT, u"")
[61] Fix | Delete
[62] Fix | Delete
# We need access to self for new_name(), and making this a method
[63] Fix | Delete
# doesn't feel right. Closing over self and new_lines makes the
[64] Fix | Delete
# code below cleaner.
[65] Fix | Delete
def handle_tuple(tuple_arg, add_prefix=False):
[66] Fix | Delete
n = Name(self.new_name())
[67] Fix | Delete
arg = tuple_arg.clone()
[68] Fix | Delete
arg.prefix = u""
[69] Fix | Delete
stmt = Assign(arg, n.clone())
[70] Fix | Delete
if add_prefix:
[71] Fix | Delete
n.prefix = u" "
[72] Fix | Delete
tuple_arg.replace(n)
[73] Fix | Delete
new_lines.append(pytree.Node(syms.simple_stmt,
[74] Fix | Delete
[stmt, end.clone()]))
[75] Fix | Delete
[76] Fix | Delete
if args.type == syms.tfpdef:
[77] Fix | Delete
handle_tuple(args)
[78] Fix | Delete
elif args.type == syms.typedargslist:
[79] Fix | Delete
for i, arg in enumerate(args.children):
[80] Fix | Delete
if arg.type == syms.tfpdef:
[81] Fix | Delete
# Without add_prefix, the emitted code is correct,
[82] Fix | Delete
# just ugly.
[83] Fix | Delete
handle_tuple(arg, add_prefix=(i > 0))
[84] Fix | Delete
[85] Fix | Delete
if not new_lines:
[86] Fix | Delete
return
[87] Fix | Delete
[88] Fix | Delete
# This isn't strictly necessary, but it plays nicely with other fixers.
[89] Fix | Delete
# TODO(cwinter) get rid of this when children becomes a smart list
[90] Fix | Delete
for line in new_lines:
[91] Fix | Delete
line.parent = suite[0]
[92] Fix | Delete
[93] Fix | Delete
# TODO(cwinter) suite-cleanup
[94] Fix | Delete
after = start
[95] Fix | Delete
if start == 0:
[96] Fix | Delete
new_lines[0].prefix = u" "
[97] Fix | Delete
elif is_docstring(suite[0].children[start]):
[98] Fix | Delete
new_lines[0].prefix = indent
[99] Fix | Delete
after = start + 1
[100] Fix | Delete
[101] Fix | Delete
for line in new_lines:
[102] Fix | Delete
line.parent = suite[0]
[103] Fix | Delete
suite[0].children[after:after] = new_lines
[104] Fix | Delete
for i in range(after+1, after+len(new_lines)+1):
[105] Fix | Delete
suite[0].children[i].prefix = indent
[106] Fix | Delete
suite[0].changed()
[107] Fix | Delete
[108] Fix | Delete
def transform_lambda(self, node, results):
[109] Fix | Delete
args = results["args"]
[110] Fix | Delete
body = results["body"]
[111] Fix | Delete
inner = simplify_args(results["inner"])
[112] Fix | Delete
[113] Fix | Delete
# Replace lambda ((((x)))): x with lambda x: x
[114] Fix | Delete
if inner.type == token.NAME:
[115] Fix | Delete
inner = inner.clone()
[116] Fix | Delete
inner.prefix = u" "
[117] Fix | Delete
args.replace(inner)
[118] Fix | Delete
return
[119] Fix | Delete
[120] Fix | Delete
params = find_params(args)
[121] Fix | Delete
to_index = map_to_index(params)
[122] Fix | Delete
tup_name = self.new_name(tuple_name(params))
[123] Fix | Delete
[124] Fix | Delete
new_param = Name(tup_name, prefix=u" ")
[125] Fix | Delete
args.replace(new_param.clone())
[126] Fix | Delete
for n in body.post_order():
[127] Fix | Delete
if n.type == token.NAME and n.value in to_index:
[128] Fix | Delete
subscripts = [c.clone() for c in to_index[n.value]]
[129] Fix | Delete
new = pytree.Node(syms.power,
[130] Fix | Delete
[new_param.clone()] + subscripts)
[131] Fix | Delete
new.prefix = n.prefix
[132] Fix | Delete
n.replace(new)
[133] Fix | Delete
[134] Fix | Delete
[135] Fix | Delete
### Helper functions for transform_lambda()
[136] Fix | Delete
[137] Fix | Delete
def simplify_args(node):
[138] Fix | Delete
if node.type in (syms.vfplist, token.NAME):
[139] Fix | Delete
return node
[140] Fix | Delete
elif node.type == syms.vfpdef:
[141] Fix | Delete
# These look like vfpdef< '(' x ')' > where x is NAME
[142] Fix | Delete
# or another vfpdef instance (leading to recursion).
[143] Fix | Delete
while node.type == syms.vfpdef:
[144] Fix | Delete
node = node.children[1]
[145] Fix | Delete
return node
[146] Fix | Delete
raise RuntimeError("Received unexpected node %s" % node)
[147] Fix | Delete
[148] Fix | Delete
def find_params(node):
[149] Fix | Delete
if node.type == syms.vfpdef:
[150] Fix | Delete
return find_params(node.children[1])
[151] Fix | Delete
elif node.type == token.NAME:
[152] Fix | Delete
return node.value
[153] Fix | Delete
return [find_params(c) for c in node.children if c.type != token.COMMA]
[154] Fix | Delete
[155] Fix | Delete
def map_to_index(param_list, prefix=[], d=None):
[156] Fix | Delete
if d is None:
[157] Fix | Delete
d = {}
[158] Fix | Delete
for i, obj in enumerate(param_list):
[159] Fix | Delete
trailer = [Subscript(Number(unicode(i)))]
[160] Fix | Delete
if isinstance(obj, list):
[161] Fix | Delete
map_to_index(obj, trailer, d=d)
[162] Fix | Delete
else:
[163] Fix | Delete
d[obj] = prefix + trailer
[164] Fix | Delete
return d
[165] Fix | Delete
[166] Fix | Delete
def tuple_name(param_list):
[167] Fix | Delete
l = []
[168] Fix | Delete
for obj in param_list:
[169] Fix | Delete
if isinstance(obj, list):
[170] Fix | Delete
l.append(tuple_name(obj))
[171] Fix | Delete
else:
[172] Fix | Delete
l.append(obj)
[173] Fix | Delete
return u"_".join(l)
[174] Fix | Delete
[175] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function