3
\| ã @ sB d Z ddlmZ ddlmZ ddlmZmZ G dd„ dejƒZdS )a& Fixer for has_key().
Calls to .has_key() methods are expressed in terms of the 'in'
operator:
d.has_key(k) -> k in d
CAVEATS:
1) While the primary target of this fixer is dict.has_key(), the
fixer will change any has_key() method call, regardless of its
class.
2) Cases like this will not be converted:
m = d.has_key
if m(k):
...
Only *calls* to has_key() are converted. While it is possible to
convert the above to something like
m = d.__contains__
if m(k):
...
this is currently not done.
é )Úpytree)Ú
fixer_base)ÚNameÚparenthesizec @ s e Zd ZdZdZdd„ ZdS )Ú FixHasKeyTa•
anchor=power<
before=any+
trailer< '.' 'has_key' >
trailer<
'('
( not(arglist | argument) arg=any
| arglist<(not argument) arg=any ','>
)
')'
>
after=any*
>
|
negation=not_test<
'not'
anchor=power<
before=any+
trailer< '.' 'has_key' >
trailer<
'('
( not(arglist | argument) arg=any
| arglist<(not argument) arg=any ','>
)
')'
>
>
>
c
C s† |st ‚| j}|jj|jkr.| jj|jƒr.d S |jdƒ}|d }|j}dd„ |d D ƒ}|d j ƒ }|jdƒ} | r€dd„ | D ƒ} |j|j
|j|j|j|j
|j|jfkr®t|ƒ}t|ƒd krÄ|d
}ntj|j|ƒ}d|_tddd
�}
|�rtddd
�}tj|j||
fƒ}
tj|j
||
|fƒ}| �rBt|ƒ}tj|j|ft| ƒ ƒ}|jj|j
|j|j|j|j|j|j|j|jf k�r|t|ƒ}||_|S )NÚnegationÚanchorc S s g | ]}|j ƒ ‘qS © )Úclone)Ú.0Únr r ú1/usr/lib64/python3.6/lib2to3/fixes/fix_has_key.pyú
R s z'FixHasKey.transform..ÚbeforeÚargÚafterc S s g | ]}|j ƒ ‘qS r )r
)r r r r r
r V s é é ú Úin)ÚprefixÚnot)ÚAssertionErrorÚsymsÚparentÚtypeZnot_testÚpatternÚmatchÚgetr r
Z
comparisonZand_testZor_testZtestZlambdefZargumentr Úlenr ZNodeZpowerr Zcomp_opÚtupleÚexprZxor_exprZand_exprZ
shift_exprZ
arith_exprZtermZfactor)
ÚselfZnodeZresultsr r r r r r r Zn_opZn_notÚnewr r r
Ú transformG sF
zFixHasKey.transformN)Ú__name__Ú
__module__Ú__qualname__Z
BM_compatibleZPATTERNr$ r r r r
r &