Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python2..../lib2to3/fixes
File: fix_has_key.py
# Copyright 2006 Google, Inc. All Rights Reserved.
[0] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[1] Fix | Delete
[2] Fix | Delete
"""Fixer for has_key().
[3] Fix | Delete
[4] Fix | Delete
Calls to .has_key() methods are expressed in terms of the 'in'
[5] Fix | Delete
operator:
[6] Fix | Delete
[7] Fix | Delete
d.has_key(k) -> k in d
[8] Fix | Delete
[9] Fix | Delete
CAVEATS:
[10] Fix | Delete
1) While the primary target of this fixer is dict.has_key(), the
[11] Fix | Delete
fixer will change any has_key() method call, regardless of its
[12] Fix | Delete
class.
[13] Fix | Delete
[14] Fix | Delete
2) Cases like this will not be converted:
[15] Fix | Delete
[16] Fix | Delete
m = d.has_key
[17] Fix | Delete
if m(k):
[18] Fix | Delete
...
[19] Fix | Delete
[20] Fix | Delete
Only *calls* to has_key() are converted. While it is possible to
[21] Fix | Delete
convert the above to something like
[22] Fix | Delete
[23] Fix | Delete
m = d.__contains__
[24] Fix | Delete
if m(k):
[25] Fix | Delete
...
[26] Fix | Delete
[27] Fix | Delete
this is currently not done.
[28] Fix | Delete
"""
[29] Fix | Delete
[30] Fix | Delete
# Local imports
[31] Fix | Delete
from .. import pytree
[32] Fix | Delete
from ..pgen2 import token
[33] Fix | Delete
from .. import fixer_base
[34] Fix | Delete
from ..fixer_util import Name, parenthesize
[35] Fix | Delete
[36] Fix | Delete
[37] Fix | Delete
class FixHasKey(fixer_base.BaseFix):
[38] Fix | Delete
BM_compatible = True
[39] Fix | Delete
[40] Fix | Delete
PATTERN = """
[41] Fix | Delete
anchor=power<
[42] Fix | Delete
before=any+
[43] Fix | Delete
trailer< '.' 'has_key' >
[44] Fix | Delete
trailer<
[45] Fix | Delete
'('
[46] Fix | Delete
( not(arglist | argument<any '=' any>) arg=any
[47] Fix | Delete
| arglist<(not argument<any '=' any>) arg=any ','>
[48] Fix | Delete
)
[49] Fix | Delete
')'
[50] Fix | Delete
>
[51] Fix | Delete
after=any*
[52] Fix | Delete
>
[53] Fix | Delete
|
[54] Fix | Delete
negation=not_test<
[55] Fix | Delete
'not'
[56] Fix | Delete
anchor=power<
[57] Fix | Delete
before=any+
[58] Fix | Delete
trailer< '.' 'has_key' >
[59] Fix | Delete
trailer<
[60] Fix | Delete
'('
[61] Fix | Delete
( not(arglist | argument<any '=' any>) arg=any
[62] Fix | Delete
| arglist<(not argument<any '=' any>) arg=any ','>
[63] Fix | Delete
)
[64] Fix | Delete
')'
[65] Fix | Delete
>
[66] Fix | Delete
>
[67] Fix | Delete
>
[68] Fix | Delete
"""
[69] Fix | Delete
[70] Fix | Delete
def transform(self, node, results):
[71] Fix | Delete
assert results
[72] Fix | Delete
syms = self.syms
[73] Fix | Delete
if (node.parent.type == syms.not_test and
[74] Fix | Delete
self.pattern.match(node.parent)):
[75] Fix | Delete
# Don't transform a node matching the first alternative of the
[76] Fix | Delete
# pattern when its parent matches the second alternative
[77] Fix | Delete
return None
[78] Fix | Delete
negation = results.get("negation")
[79] Fix | Delete
anchor = results["anchor"]
[80] Fix | Delete
prefix = node.prefix
[81] Fix | Delete
before = [n.clone() for n in results["before"]]
[82] Fix | Delete
arg = results["arg"].clone()
[83] Fix | Delete
after = results.get("after")
[84] Fix | Delete
if after:
[85] Fix | Delete
after = [n.clone() for n in after]
[86] Fix | Delete
if arg.type in (syms.comparison, syms.not_test, syms.and_test,
[87] Fix | Delete
syms.or_test, syms.test, syms.lambdef, syms.argument):
[88] Fix | Delete
arg = parenthesize(arg)
[89] Fix | Delete
if len(before) == 1:
[90] Fix | Delete
before = before[0]
[91] Fix | Delete
else:
[92] Fix | Delete
before = pytree.Node(syms.power, before)
[93] Fix | Delete
before.prefix = u" "
[94] Fix | Delete
n_op = Name(u"in", prefix=u" ")
[95] Fix | Delete
if negation:
[96] Fix | Delete
n_not = Name(u"not", prefix=u" ")
[97] Fix | Delete
n_op = pytree.Node(syms.comp_op, (n_not, n_op))
[98] Fix | Delete
new = pytree.Node(syms.comparison, (arg, n_op, before))
[99] Fix | Delete
if after:
[100] Fix | Delete
new = parenthesize(new)
[101] Fix | Delete
new = pytree.Node(syms.power, (new,) + tuple(after))
[102] Fix | Delete
if node.parent.type in (syms.comparison, syms.expr, syms.xor_expr,
[103] Fix | Delete
syms.and_expr, syms.shift_expr,
[104] Fix | Delete
syms.arith_expr, syms.term,
[105] Fix | Delete
syms.factor, syms.power):
[106] Fix | Delete
new = parenthesize(new)
[107] Fix | Delete
new.prefix = prefix
[108] Fix | Delete
return new
[109] Fix | Delete
[110] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function