
\[\                 @   s   d  Z  d d l m Z d d l m Z d d l m Z d d l m Z m Z m	 Z	 m
 Z
 m Z m Z d d   Z Gd d	   d	 e j  Z d
 S)a  Fixer for except statements with named exceptions.

The following cases will be converted:

- "except E, T:" where T is a name:

    except E as T:

- "except E, T:" where T is not a name, tuple or list:

        except E as t:
            T = t

    This is done because the target of an "except" clause must be a
    name.

- "except E, T:" where T is a tuple or list literal:

        except E as t:
            T = t.args
   )pytree)token)
fixer_base)AssignAttrNameis_tupleis_listsymsc             c   sb   x[ t  |   D]M \ } } | j t j k r | j d j d k rZ | |  | d f VqZ q q Wd  S)N    exceptr   )	enumeratetyper
   except_clausechildrenvalue)Znodesin r   ./usr/lib/python3.4/lib2to3/fixes/fix_except.pyfind_excepts   s    r   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)	FixExceptTa1  
    try_stmt< 'try' ':' (simple_stmt | suite)
                  cleanup=(except_clause ':' (simple_stmt | suite))+
                  tail=(['except' ':' (simple_stmt | suite)]
                        ['else' ':' (simple_stmt | suite)]
                        ['finally' ':' (simple_stmt | suite)]) >
    c             C   s  |  j  } d d   | d D } d d   | d D } xt |  D]\ } } t | j  d k rD | j d d  \ } }	 }
 |	 j t d d	 d
  |
 j t j k rt |  j	   d	 d
 } |
 j
   } d | _ |
 j |  | j
   } | j } x0 t |  D]" \ } } t | t j  rPqqWt |
  sFt |
  rgt | t | t d    } n t | |  } x. t | d  |   D] } | j d |  qW| j | |  q|
 j d k rd
 |
 _ qqD qD Wd d   | j d  d  D | | } t j | j |  S)Nc             S   s   g  |  ] } | j     q Sr   )clone).0r   r   r   r   
<listcomp>2   s   	 z'FixExcept.transform.<locals>.<listcomp>tailc             S   s   g  |  ] } | j     q Sr   )r   )r   Zchr   r   r   r   4   s   	 Zcleanup      asprefix  argsr   c             S   s   g  |  ] } | j     q Sr   )r   )r   cr   r   r   r   \   s   	    )r
   r   lenr   replacer   r   r   NAMEnew_namer   r   r   
isinstancer   ZNoder   r	   r   r   reversedZinsert_child)selfZnodeZresultsr
   r   Ztry_cleanupr   Ze_suiteEZcommaNZnew_NtargetZsuite_stmtsr   ZstmtZassignZchildr   r   r   r   	transform/   s6    			!(zFixExcept.transformN)__name__
__module____qualname__ZBM_compatibleZPATTERNr/   r   r   r   r   r   $   s   r   N)__doc__r!   r   Zpgen2r   r   Z
fixer_utilr   r   r   r   r	   r
   r   ZBaseFixr   r   r   r   r   <module>   s   .