]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/lib2to3/fixes/fix_has_key.py
d7bf0718051a3933f8ee8a3ccf95de5b39f5733f
1 # Copyright 2006 Google, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement.
4 """Fixer for has_key().
6 Calls to .has_key() methods are expressed in terms of the 'in'
12 1) While the primary target of this fixer is dict.has_key(), the
13 fixer will change any has_key() method call, regardless of its
16 2) Cases like this will not be converted:
22 Only *calls* to has_key() are converted. While it is possible to
23 convert the above to something like
29 this is currently not done.
34 from ..pgen2
import token
35 from .. import fixer_base
36 from ..fixer_util
import Name
, parenthesize
39 class FixHasKey(fixer_base
.BaseFix
):
45 trailer< '.' 'has_key' >
48 ( not(arglist | argument<any '=' any>) arg=any
49 | arglist<(not argument<any '=' any>) arg=any ','>
60 trailer< '.' 'has_key' >
63 ( not(arglist | argument<any '=' any>) arg=any
64 | arglist<(not argument<any '=' any>) arg=any ','>
72 def transform(self
, node
, results
):
75 if (node
.parent
.type == syms
.not_test
and
76 self
.pattern
.match(node
.parent
)):
77 # Don't transform a node matching the first alternative of the
78 # pattern when its parent matches the second alternative
80 negation
= results
.get("negation")
81 anchor
= results
["anchor"]
83 before
= [n
.clone() for n
in results
["before"]]
84 arg
= results
["arg"].clone()
85 after
= results
.get("after")
87 after
= [n
.clone() for n
in after
]
88 if arg
.type in (syms
.comparison
, syms
.not_test
, syms
.and_test
,
89 syms
.or_test
, syms
.test
, syms
.lambdef
, syms
.argument
):
90 arg
= parenthesize(arg
)
94 before
= pytree
.Node(syms
.power
, before
)
96 n_op
= Name(u
"in", prefix
=u
" ")
98 n_not
= Name(u
"not", prefix
=u
" ")
99 n_op
= pytree
.Node(syms
.comp_op
, (n_not
, n_op
))
100 new
= pytree
.Node(syms
.comparison
, (arg
, n_op
, before
))
102 new
= parenthesize(new
)
103 new
= pytree
.Node(syms
.power
, (new
,) + tuple(after
))
104 if node
.parent
.type in (syms
.comparison
, syms
.expr
, syms
.xor_expr
,
105 syms
.and_expr
, syms
.shift_expr
,
106 syms
.arith_expr
, syms
.term
,
107 syms
.factor
, syms
.power
):
108 new
= parenthesize(new
)