\d ddZddlmZddlmZmZmZmZmZm Z dZ dZ Gddej Z dS) aAdjust some old Python 2 idioms to their modern counterparts. * Change some type comparisons to isinstance() calls: type(x) == T -> isinstance(x, T) type(x) is T -> isinstance(x, T) type(x) != T -> not isinstance(x, T) type(x) is not T -> not isinstance(x, T) * Change "while 1:" into "while True:". * Change both v = list(EXPR) v.sort() foo(v) and the more general v = EXPR v.sort() foo(v) into v = sorted(EXPR) foo(v) ) fixer_base)CallCommaNameNode BlankLinesymsz0(n='!=' | '==' | 'is' | n=comp_op< 'is' 'not' >)z(power< 'type' trailer< '(' x=any ')' > >c XeZdZdZdedededed ZfdZdZdZ d Z d Z xZ S) FixIdiomsTz isinstance=comparison<  z8 T=any > | isinstance=comparison< T=any aX > | while_stmt< 'while' while='1' ':' any+ > | sorted=any< any* simple_stmt< expr_stmt< id1=any '=' power< list='list' trailer< '(' (not arglist) any ')' > > > '\n' > sort= simple_stmt< power< id2=any trailer< '.' 'sort' > trailer< '(' ')' > > '\n' > next=any* > | sorted=any< any* simple_stmt< expr_stmt< id1=any '=' expr=any > '\n' > sort= simple_stmt< power< id2=any trailer< '.' 'sort' > trailer< '(' ')' > > '\n' > next=any* > ctt||}|rd|vr|d|dkr|SdS|S)Nsortedid1id2)superr match)selfnoder __class__s )..\python\lib\lib2to3\fixes\fix_idioms.pyrzFixIdioms.matchOsT )T " " ( ( . .  Qx1U8##4cd|vr|||Sd|vr|||Sd|vr|||Std)N isinstancewhilerz Invalid match)transform_isinstancetransform_whiletransform_sort RuntimeError)rrresultss r transformzFixIdioms.transformZss 7 " ",,T7;; ;   ''g66 6  &&tW55 5// /rcb|d}|d}d|_d|_ttd|t |g}d|vr0d|_t t jtd|g}|j|_|S)NxTr rnnot)cloneprefixrrrrr not_test)rrr r#r$tests rrzFixIdioms.transform_isinstanceds CL    CL   D&&EGGQ88 '>>DK U T':;;Dk  rch|d}|td|jdS)NrTruer))replacerr))rrr ones rrzFixIdioms.transform_whileps3g D 33344444rc@|d}|d}|d}|d}|r*|td|jne|rT|}d|_|t td|g|jnt d||j}d |vr|rJ|d d |d jf} d | |d _dS|j sJ|j Jt} |j | |j | usJ|d d | _dSdS) Nsortnextlistexprrr.r%zshould not have reached here )getr/rr)r(rrremove rpartitionjoinparent next_siblingr append_child) rrr sort_stmt next_stmt list_call simple_exprnewbtwn prefix_linesend_lines rrzFixIdioms.transform_sorttsFO FO KK'' kk&))  ?   d8I4DEEE F F F F  ?##%%CCJ   T(^^cU,7,>!@!@!@ A A A A=>> > 4<< ;!% 5 5a 8)A,:MN &*ii &=&= ! ### '''' -555$;; --h777 -9999#'//$"7"7":! rSs<AAAAAAAAAAAAAAAA81s;s;s;s;s; "s;s;s;s;s;r