
\[\                 @   sm   d  Z  d d l m Z d d l m Z m Z m Z m Z m Z m	 Z	 d Z
 d Z Gd d   d e j  Z d S)	a  Adjust 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)CallCommaNameNode	BlankLinesymsz0(n='!=' | '==' | 'is' | n=comp_op< 'is' 'not' >)z(power< 'type' trailer< '(' x=any ')' > >c                   sn   e  Z d  Z d Z d e e e e f Z   f d d   Z d d   Z d d   Z	 d	 d
   Z
 d d   Z   S)	FixIdiomsTa  
        isinstance=comparison< %s %s T=any >
        |
        isinstance=comparison< T=any %s %s >
        |
        while_stmt< 'while' while='1' ':' any+ >
        |
        sorted=any<
            any*
            simple_stmt<
              expr_stmt< id1=any '='
                         power< list='list' trailer< '(' (not arglist<any+>) 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*
        >
    c                sJ   t  t |   j |  } | rF d | k rF | d | d k rB | Sd  S| S)NsortedZid1Zid2)superr	   match)selfnoder)	__class__ ./usr/lib/python3.4/lib2to3/fixes/fix_idioms.pyr   O   s    zFixIdioms.matchc             C   sd   d | k r |  j  | |  Sd | k r8 |  j | |  Sd | k rT |  j | |  St d   d  S)N
isinstancewhiler
   zInvalid match)transform_isinstancetransform_whiletransform_sortRuntimeError)r   r   resultsr   r   r   	transformZ   s    zFixIdioms.transformc             C   s   | d j    } | d j    } d | _ d | _ t t d  | t   | g  } d | k r d | _ t t j t d  | g  } n  | j | _ | S)NxT  r   nnot)cloneprefixr   r   r   r   r   Znot_test)r   r   r   r   r   Ztestr   r   r   r   d   s    		!	!zFixIdioms.transform_isinstancec             C   s*   | d } | j  t d d | j  d  S)Nr   Truer"   )replacer   r"   )r   r   r   Zoner   r   r   r   p   s    
zFixIdioms.transform_whilec             C   sv  | d } | d } | j  d  } | j  d  } | rW | j t d d | j  nR | r | j   } d | _ | j t t d  | g d | j  n t d   | j   | j } d	 | k rr| r| j d	  d
 | d
 j f }	 d	 j	 |	  | d
 _ qr| j
 st  | j d  k s+t  t   }
 | j
 j |
  | j |
 k sYt  | j d	  d
 |
 _ n  d  S)Nsortnextlistexprr
   r"   r   zshould not have reached here
    )getr$   r   r"   r!   r   r   remove
rpartitionjoinparentAssertionErrorZnext_siblingr   Zappend_child)r   r   r   Z	sort_stmtZ	next_stmtZ	list_callZsimple_exprnewZbtwnZprefix_linesZend_liner   r   r   r   t   s0    

	
	 	zFixIdioms.transform_sort)__name__
__module____qualname__ZexplicitTYPECMPZPATTERNr   r   r   r   r   r   r   )r   r   r	   %   s   '
r	   N)__doc__r   r   Z
fixer_utilr   r   r   r   r   r   r6   r5   ZBaseFixr	   r   r   r   r   <module>   s
   .