
    ;e                     d    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 G 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            	       X     e Zd ZdZdedededed	Z fdZd Zd Z	d	 Z
d
 Z xZ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+>) 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                     t          t          |                               |          }|rd|v r|d         |d         k    r|S d S |S )Nsortedid1id2)superr   match)selfnoder	__class__s      //usr/lib/python3.11/lib2to3/fixes/fix_idioms.pyr   zFixIdioms.matchO   sT    )T""((..  	Qx1U8##4    c                     d|v r|                      ||          S d|v r|                     ||          S d|v r|                     ||          S t          d          )N
isinstancewhiler   zInvalid match)transform_isinstancetransform_whiletransform_sortRuntimeError)r   r   resultss      r   	transformzFixIdioms.transformZ   ss    7"",,T7;;;''g666  &&tW555///r   c                 b   |d                                          }|d                                          }d|_        d|_        t          t          d          |t	                      |g          }d|v r0d|_        t          t          j        t          d          |g          }|j        |_        |S )NxT r   r   nnot)cloneprefixr   r   r   r   r	   not_test)r   r   r    r#   r$   tests         r   r   zFixIdioms.transform_isinstanced   s    CL  CL  D&&EGGQ88'>>DKUT':;;Dkr   c                 h    |d         }|                     t          d|j                             d S )Nr   Truer)   )replacer   r)   )r   r   r    ones       r   r   zFixIdioms.transform_whilep   s3    gD
33344444r   c                    |d         }|d         }|                     d          }|                     d          }|r*|                    t          d|j                             ne|rT|                                }d|_        |                    t          t          d          |g|j                             nt          d          |                                 |j        }d	|v r|rJ|                    d	          d
         |d
         j        f}	d		                    |	          |d
         _        d S t                      }
|j                            |
           |                    d	          d
         |
_        d S d S )Nsortnextlistexprr   r.   r%   zshould not have reached here
    )getr/   r   r)   r(   r   r   remove
rpartitionjoinr   parentappend_child)r   r   r    	sort_stmt	next_stmt	list_callsimple_exprnewbtwnprefix_linesend_lines              r   r   zFixIdioms.transform_sortt   s   FO	FO	KK''	kk&)) 	?d8I4DEEEFFFF 	?##%%CCJT(^^cU,7,>!@ !@ !@ A A A A =>>> 4<< ; !% 5 5a 8)A,:MN&*ii&=&=	!###
 %;; --h777 #'//$"7"7":! <r   )__name__
__module____qualname__explicitTYPECMPPATTERNr   r!   r   r   r   __classcell__)r   s   @r   r   r   %   s        HHN 44ccc444K%!GN	 	 	 	 	0 0 0
 
 
5 5 5$; $; $; $; $; $; $;r   r   N)__doc__r%   r   
fixer_utilr   r   r   r   r   r	   rK   rJ   BaseFixr    r   r   <module>rR      s    <       A A A A A A A A A A A A A A A A81s; s; s; s; s;
" s; s; s; s; s;r   