B
    Ç S]¿	  ã               @   sJ   d Z ddlmZmZ ddlmZmZmZmZm	Z	m
Z
 G dd„ dejƒZdS )z7
Convert use of sys.exitfunc to use the atexit module.
é    )ÚpytreeÚ
fixer_base)ÚNameÚAttrÚCallÚCommaÚNewlineÚsymsc                   s<   e Zd ZdZdZdZ‡ fdd„Z‡ fdd„Zdd„ Z‡  Z	S )	ÚFixExitfuncTa”  
              (
                  sys_import=import_name<'import'
                      ('sys'
                      |
                      dotted_as_names< (any ',')* 'sys' (',' any)* >
                      )
                  >
              |
                  expr_stmt<
                      power< 'sys' trailer< '.' 'exitfunc' > >
                  '=' func=any >
              )
              c                s   t t| ƒj|Ž  d S )N)Úsuperr
   Ú__init__)ÚselfÚargs)Ú	__class__© ú+lib/python3.7/lib2to3/fixes/fix_exitfunc.pyr      s    zFixExitfunc.__init__c                s   t t| ƒ ||¡ d | _d S )N)r   r
   Ú
start_treeÚ
sys_import)r   ZtreeÚfilename)r   r   r   r   !   s    zFixExitfunc.start_treec             C   s&  d|kr | j d kr|d | _ d S |d  ¡ }d|_t tjttdƒtdƒƒ¡}t	||g|jƒ}| 
|¡ | j d kr‚|  |d¡ d S | j jd }|jtjkr¸| tƒ ¡ | tddƒ¡ nj| j j}|j | j ¡}|j}	t tjtd	ƒtddƒg¡}
t tj|
g¡}| |d tƒ ¡ | |d
 |¡ d S )Nr   ÚfuncÚ ÚatexitÚregisterzKCan't find sys import; Please add an atexit import at the top of your file.é   ú Úimporté   )r   ZcloneÚprefixr   ZNoder	   Zpowerr   r   r   ÚreplaceZwarningZchildrenÚtypeZdotted_as_namesZappend_childr   ÚparentÚindexZimport_nameZsimple_stmtZinsert_childr   )r   ZnodeZresultsr   r   ZcallÚnamesZcontaining_stmtZpositionZstmt_containerZ
new_importÚnewr   r   r   Ú	transform%   s2    



zFixExitfunc.transform)
Ú__name__Ú
__module__Ú__qualname__Zkeep_line_orderZBM_compatibleZPATTERNr   r   r$   Ú__classcell__r   r   )r   r   r
      s   r
   N)Ú__doc__Zlib2to3r   r   Zlib2to3.fixer_utilr   r   r   r   r   r	   ZBaseFixr
   r   r   r   r   Ú<module>   s    