o
     JAf¿	  ã                   @   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__© ú1/usr/lib/python3.10/lib2to3/fixes/fix_exitfunc.pyr      s   zFixExitfunc.__init__c                    s   t t| ƒ ||¡ d | _d S r   )r   r
   Ú
start_treeÚ
sys_import)r   ZtreeÚfilenamer   r   r   r   !   s   
zFixExitfunc.start_treec                 C   s(  d|v r| j d u r|d | _ d S |d  ¡ }d|_t tjttdƒtdƒƒ¡}t	||g|jƒ}| 
|¡ | j d u rA|  |d¡ d S | j jd }|jtjkr]| tƒ ¡ | tddƒ¡ d S | 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   ÚreplaceÚwarningÚchildrenÚtypeZdotted_as_namesZappend_childr   ÚparentÚindexZimport_nameZsimple_stmtZinsert_childr   )r   ZnodeÚresultsr   r   ÚcallÚnamesZcontaining_stmtZpositionZstmt_containerZ
new_importÚnewr   r   r   Ú	transform%   s6   

ÿ

ÿ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     