Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3..../lib2to3/fixes
File: fix_dict.py
# Copyright 2007 Google, Inc. All Rights Reserved.
[0] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[1] Fix | Delete
[2] Fix | Delete
"""Fixer for dict methods.
[3] Fix | Delete
[4] Fix | Delete
d.keys() -> list(d.keys())
[5] Fix | Delete
d.items() -> list(d.items())
[6] Fix | Delete
d.values() -> list(d.values())
[7] Fix | Delete
[8] Fix | Delete
d.iterkeys() -> iter(d.keys())
[9] Fix | Delete
d.iteritems() -> iter(d.items())
[10] Fix | Delete
d.itervalues() -> iter(d.values())
[11] Fix | Delete
[12] Fix | Delete
d.viewkeys() -> d.keys()
[13] Fix | Delete
d.viewitems() -> d.items()
[14] Fix | Delete
d.viewvalues() -> d.values()
[15] Fix | Delete
[16] Fix | Delete
Except in certain very specific contexts: the iter() can be dropped
[17] Fix | Delete
when the context is list(), sorted(), iter() or for...in; the list()
[18] Fix | Delete
can be dropped when the context is list() or sorted() (but not iter()
[19] Fix | Delete
or for...in!). Special contexts that apply to both: list(), sorted(), tuple()
[20] Fix | Delete
set(), any(), all(), sum().
[21] Fix | Delete
[22] Fix | Delete
Note: iter(d.keys()) could be written as iter(d) but since the
[23] Fix | Delete
original d.iterkeys() was also redundant we don't fix this. And there
[24] Fix | Delete
are (rare) contexts where it makes a difference (e.g. when passing it
[25] Fix | Delete
as an argument to a function that introspects the argument).
[26] Fix | Delete
"""
[27] Fix | Delete
[28] Fix | Delete
# Local imports
[29] Fix | Delete
from .. import pytree
[30] Fix | Delete
from .. import patcomp
[31] Fix | Delete
from .. import fixer_base
[32] Fix | Delete
from ..fixer_util import Name, Call, Dot
[33] Fix | Delete
from .. import fixer_util
[34] Fix | Delete
[35] Fix | Delete
[36] Fix | Delete
iter_exempt = fixer_util.consuming_calls | {"iter"}
[37] Fix | Delete
[38] Fix | Delete
[39] Fix | Delete
class FixDict(fixer_base.BaseFix):
[40] Fix | Delete
BM_compatible = True
[41] Fix | Delete
[42] Fix | Delete
PATTERN = """
[43] Fix | Delete
power< head=any+
[44] Fix | Delete
trailer< '.' method=('keys'|'items'|'values'|
[45] Fix | Delete
'iterkeys'|'iteritems'|'itervalues'|
[46] Fix | Delete
'viewkeys'|'viewitems'|'viewvalues') >
[47] Fix | Delete
parens=trailer< '(' ')' >
[48] Fix | Delete
tail=any*
[49] Fix | Delete
>
[50] Fix | Delete
"""
[51] Fix | Delete
[52] Fix | Delete
def transform(self, node, results):
[53] Fix | Delete
head = results["head"]
[54] Fix | Delete
method = results["method"][0] # Extract node for method name
[55] Fix | Delete
tail = results["tail"]
[56] Fix | Delete
syms = self.syms
[57] Fix | Delete
method_name = method.value
[58] Fix | Delete
isiter = method_name.startswith("iter")
[59] Fix | Delete
isview = method_name.startswith("view")
[60] Fix | Delete
if isiter or isview:
[61] Fix | Delete
method_name = method_name[4:]
[62] Fix | Delete
assert method_name in ("keys", "items", "values"), repr(method)
[63] Fix | Delete
head = [n.clone() for n in head]
[64] Fix | Delete
tail = [n.clone() for n in tail]
[65] Fix | Delete
special = not tail and self.in_special_context(node, isiter)
[66] Fix | Delete
args = head + [pytree.Node(syms.trailer,
[67] Fix | Delete
[Dot(),
[68] Fix | Delete
Name(method_name,
[69] Fix | Delete
prefix=method.prefix)]),
[70] Fix | Delete
results["parens"].clone()]
[71] Fix | Delete
new = pytree.Node(syms.power, args)
[72] Fix | Delete
if not (special or isview):
[73] Fix | Delete
new.prefix = ""
[74] Fix | Delete
new = Call(Name("iter" if isiter else "list"), [new])
[75] Fix | Delete
if tail:
[76] Fix | Delete
new = pytree.Node(syms.power, [new] + tail)
[77] Fix | Delete
new.prefix = node.prefix
[78] Fix | Delete
return new
[79] Fix | Delete
[80] Fix | Delete
P1 = "power< func=NAME trailer< '(' node=any ')' > any* >"
[81] Fix | Delete
p1 = patcomp.compile_pattern(P1)
[82] Fix | Delete
[83] Fix | Delete
P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
[84] Fix | Delete
| comp_for< 'for' any 'in' node=any any* >
[85] Fix | Delete
"""
[86] Fix | Delete
p2 = patcomp.compile_pattern(P2)
[87] Fix | Delete
[88] Fix | Delete
def in_special_context(self, node, isiter):
[89] Fix | Delete
if node.parent is None:
[90] Fix | Delete
return False
[91] Fix | Delete
results = {}
[92] Fix | Delete
if (node.parent.parent is not None and
[93] Fix | Delete
self.p1.match(node.parent.parent, results) and
[94] Fix | Delete
results["node"] is node):
[95] Fix | Delete
if isiter:
[96] Fix | Delete
# iter(d.iterkeys()) -> iter(d.keys()), etc.
[97] Fix | Delete
return results["func"].value in iter_exempt
[98] Fix | Delete
else:
[99] Fix | Delete
# list(d.keys()) -> list(d.keys()), etc.
[100] Fix | Delete
return results["func"].value in fixer_util.consuming_calls
[101] Fix | Delete
if not isiter:
[102] Fix | Delete
return False
[103] Fix | Delete
# for ... in d.iterkeys() -> for ... in d.keys(), etc.
[104] Fix | Delete
return self.p2.match(node.parent, results) and results["node"] is node
[105] Fix | Delete
[106] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function