B
    u9a+                 @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZ yd dlZd dlZW n ek
r   dZY nX G dd dZdS )    N)support)print_warningc               @   s  e Zd ZdZdZdqddddZdZdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+dRdS Z,dTdU Z-dVdW Z.dXdY Z/dZd[ Z0d\d] Z1d^d_ Z2d`da e3e4D Z5dbdc Z6ddde Z7dfdg Z8dhdi Z9djdk Z:dldm Z;dndo Z<dpS )rsaved_test_environmenta  Save bits of the test environment and restore them at block exit.

        with saved_test_environment(testname, verbose, quiet):
            #stuff

    Unless quiet is True, a warning is printed to stderr if any of
    the saved items was changed by the test.  The attribute 'changed'
    is initially False, but is set to True if a change is detected.

    If verbose is more than 1, the before and after state of changed
    items is also printed.
    Fr   )pgoc            C   s   || _ || _|| _|| _d S )N)testnameverbosequietr   )selfr   r   r   r    r
   /usr/lib/python3.7/save_env.py__init__*   s    zsaved_test_environment.__init__)zsys.argvcwdz	sys.stdinz
sys.stdoutz
sys.stderrz
os.environzsys.pathzsys.path_hooks
__import__zwarnings.filterszasyncore.socket_mapzlogging._handlerszlogging._handlerListzsys.gettracezsys.warnoptionsz!multiprocessing.process._danglingzthreading._danglingzsysconfig._CONFIG_VARSzsysconfig._INSTALL_SCHEMESfileslocalezwarnings.showwarningZshutil_archive_formatsZshutil_unpack_formatsc             C   s   t tjtjtjd d  fS )N)idsysargv)r	   r
   r
   r   get_sys_argvG   s    z#saved_test_environment.get_sys_argvc             C   s    |d t _|d t jd d < d S )N      )r   r   )r	   Z
saved_argvr
   r
   r   restore_sys_argvI   s    
z'saved_test_environment.restore_sys_argvc             C   s   t  S )N)osgetcwd)r	   r
   r
   r   get_cwdM   s    zsaved_test_environment.get_cwdc             C   s   t | d S )N)r   chdir)r	   Z	saved_cwdr
   r
   r   restore_cwdO   s    z"saved_test_environment.restore_cwdc             C   s   t jS )N)r   stdout)r	   r
   r
   r   get_sys_stdoutR   s    z%saved_test_environment.get_sys_stdoutc             C   s
   |t _d S )N)r   r   )r	   Zsaved_stdoutr
   r
   r   restore_sys_stdoutT   s    z)saved_test_environment.restore_sys_stdoutc             C   s   t jS )N)r   stderr)r	   r
   r
   r   get_sys_stderrW   s    z%saved_test_environment.get_sys_stderrc             C   s
   |t _d S )N)r   r    )r	   Zsaved_stderrr
   r
   r   restore_sys_stderrY   s    z)saved_test_environment.restore_sys_stderrc             C   s   t jS )N)r   stdin)r	   r
   r
   r   get_sys_stdin\   s    z$saved_test_environment.get_sys_stdinc             C   s
   |t _d S )N)r   r#   )r	   Zsaved_stdinr
   r
   r   restore_sys_stdin^   s    z(saved_test_environment.restore_sys_stdinc             C   s   t tjtjttjfS )N)r   r   environdict)r	   r
   r
   r   get_os_environa   s    z%saved_test_environment.get_os_environc             C   s(   |d t _t j  t j|d  d S )Nr   r   )r   r&   clearupdate)r	   Zsaved_environr
   r
   r   restore_os_environc   s    

z)saved_test_environment.restore_os_environc             C   s   t tjtjtjd d  fS )N)r   r   path)r	   r
   r
   r   get_sys_pathh   s    z#saved_test_environment.get_sys_pathc             C   s    |d t _|d t jd d < d S )Nr   r   )r   r,   )r	   Z
saved_pathr
   r
   r   restore_sys_pathj   s    
z'saved_test_environment.restore_sys_pathc             C   s   t tjtjtjd d  fS )N)r   r   
path_hooks)r	   r
   r
   r   get_sys_path_hooksn   s    z)saved_test_environment.get_sys_path_hooksc             C   s    |d t _|d t jd d < d S )Nr   r   )r   r/   )r	   Zsaved_hooksr
   r
   r   restore_sys_path_hooksp   s    
z-saved_test_environment.restore_sys_path_hooksc             C   s   t  S )N)r   gettrace)r	   r
   r
   r   get_sys_gettracet   s    z'saved_test_environment.get_sys_gettracec             C   s   t | d S )N)r   settrace)r	   Z	trace_fxnr
   r
   r   restore_sys_gettracev   s    z+saved_test_environment.restore_sys_gettracec             C   s   t jS )N)builtinsr   )r	   r
   r
   r   get___import__y   s    z%saved_test_environment.get___import__c             C   s
   |t _d S )N)r6   r   )r	   import_r
   r
   r   restore___import__{   s    z)saved_test_environment.restore___import__c             C   s   t tjtjtjd d  fS )N)r   warningsfilters)r	   r
   r
   r   get_warnings_filters~   s    z+saved_test_environment.get_warnings_filtersc             C   s    |d t _|d t jd d < d S )Nr   r   )r:   r;   )r	   Zsaved_filtersr
   r
   r   restore_warnings_filters   s    
z/saved_test_environment.restore_warnings_filtersc             C   s   t jd}|r|j pi S )Nasyncore)r   modulesget
socket_mapcopy)r	   r>   r
   r
   r   get_asyncore_socket_map   s    z.saved_test_environment.get_asyncore_socket_mapc             C   s0   t jd}|d k	r,|jdd |j| d S )Nr>   T)Z
ignore_all)r   r?   r@   Z	close_allrA   r*   )r	   Z	saved_mapr>   r
   r
   r   restore_asyncore_socket_map   s    z2saved_test_environment.restore_asyncore_socket_mapc             C   s   t jt j fS )N)shutil_ARCHIVE_FORMATSrB   )r	   r
   r
   r   get_shutil_archive_formats   s    z1saved_test_environment.get_shutil_archive_formatsc             C   s(   |d t _t j  t j|d  d S )Nr   r   )rE   rF   r)   r*   )r	   savedr
   r
   r   restore_shutil_archive_formats   s    

z5saved_test_environment.restore_shutil_archive_formatsc             C   s   t jt j fS )N)rE   _UNPACK_FORMATSrB   )r	   r
   r
   r   get_shutil_unpack_formats   s    z0saved_test_environment.get_shutil_unpack_formatsc             C   s(   |d t _t j  t j|d  d S )Nr   r   )rE   rJ   r)   r*   )r	   rH   r
   r
   r   restore_shutil_unpack_formats   s    

z4saved_test_environment.restore_shutil_unpack_formatsc             C   s   t tjtjtj fS )N)r   loggingZ	_handlersrB   )r	   r
   r
   r   get_logging__handlers   s    z,saved_test_environment.get_logging__handlersc             C   s   d S )Nr
   )r	   Zsaved_handlersr
   r
   r   restore_logging__handlers   s    z0saved_test_environment.restore_logging__handlersc             C   s   t tjtjtjd d  fS )N)r   rM   Z_handlerList)r	   r
   r
   r   get_logging__handlerList   s    z/saved_test_environment.get_logging__handlerListc             C   s   d S )Nr
   )r	   Zsaved_handlerListr
   r
   r   restore_logging__handlerList   s    z3saved_test_environment.restore_logging__handlerListc             C   s   t tjtjtjd d  fS )N)r   r   warnoptions)r	   r
   r
   r   get_sys_warnoptions   s    z*saved_test_environment.get_sys_warnoptionsc             C   s    |d t _|d t jd d < d S )Nr   r   )r   rR   )r	   Zsaved_optionsr
   r
   r   restore_sys_warnoptions   s    
z.saved_test_environment.restore_sys_warnoptionsc             C   s
   t j S )N)	threading	_danglingrB   )r	   r
   r
   r   get_threading__dangling   s    z.saved_test_environment.get_threading__danglingc             C   s   t j  t j| d S )N)rU   rV   r)   r*   )r	   rH   r
   r
   r   restore_threading__dangling   s    
z2saved_test_environment.restore_threading__danglingc             C   s   t sd S t j  t jj S )N)multiprocessingprocessZ_cleanuprV   rB   )r	   r
   r
   r   %get_multiprocessing_process__dangling   s    
z<saved_test_environment.get_multiprocessing_process__danglingc             C   s&   t sd S t jj  t jj| d S )N)rY   rZ   rV   r)   r*   )r	   rH   r
   r
   r   )restore_multiprocessing_process__dangling   s    z@saved_test_environment.restore_multiprocessing_process__danglingc             C   s"   t d tt jt jtt jfS )Nprefix)	sysconfigZget_config_varr   _CONFIG_VARSr'   )r	   r
   r
   r   get_sysconfig__CONFIG_VARS   s    
z1saved_test_environment.get_sysconfig__CONFIG_VARSc             C   s(   |d t _t j  t j|d  d S )Nr   r   )r^   r_   r)   r*   )r	   rH   r
   r
   r   restore_sysconfig__CONFIG_VARS   s    

z5saved_test_environment.restore_sysconfig__CONFIG_VARSc             C   s   t tjtjtj fS )N)r   r^   _INSTALL_SCHEMESrB   )r	   r
   r
   r   get_sysconfig__INSTALL_SCHEMES   s    z5saved_test_environment.get_sysconfig__INSTALL_SCHEMESc             C   s(   |d t _t j  t j|d  d S )Nr   r   )r^   rb   r)   r*   )r	   rH   r
   r
   r   "restore_sysconfig__INSTALL_SCHEMES   s    

z9saved_test_environment.restore_sysconfig__INSTALL_SCHEMESc             C   s   t dd t D S )Nc             s   s&   | ]}|t j|rd nd V  qdS )/ N)r   r,   isdir).0fnr
   r
   r   	<genexpr>   s   z3saved_test_environment.get_files.<locals>.<genexpr>)sortedr   listdir)r	   r
   r
   r   	get_files   s    z saved_test_environment.get_filesc             C   sL   t j}||krH|d |krHtj|r2t | ntj|rHt | d S )Nre   )r   ZTESTFNr   r,   isfileunlinkrg   Zrmtree)r	   Zsaved_valueri   r
   r
   r   restore_files   s    z$saved_test_environment.restore_filesc             C   s    g | ]}| d rtt|qS )LC_)
startswithgetattrr   )rh   lcr
   r
   r   
<listcomp>   s    z!saved_test_environment.<listcomp>c          
   C   sN   g }xD| j D ]:}y||t|d f W q ttfk
rD   wY qX qW |S )N)_lcappendr   	setlocale	TypeError
ValueError)r	   Zpairingsrt   r
   r
   r   
get_locale   s    z!saved_test_environment.get_localec             C   s"   x|D ]\}}t || qW d S )N)r   rx   )r	   rH   rt   Zsettingr
   r
   r   restore_locale   s    z%saved_test_environment.restore_localec             C   s   t jS )N)r:   showwarning)r	   r
   r
   r   get_warnings_showwarning   s    z/saved_test_environment.get_warnings_showwarningc             C   s
   |t _d S )N)r:   r}   )r	   fxnr
   r
   r   restore_warnings_showwarning   s    z3saved_test_environment.restore_warnings_showwarningc             c   sH   xB| j D ]8}|dd}d| }d| }|t| |t| |fV  qW d S )N._Zget_Zrestore_)	resourcesreplacers   )r	   nameZmethod_suffixZget_nameZrestore_namer
   r
   r   resource_info   s
    z$saved_test_environment.resource_infoc             C   s   t dd |  D | _| S )Nc             s   s   | ]\}}}|| fV  qd S )Nr
   )rh   r   r@   restorer
   r
   r   rj     s    z3saved_test_environment.__enter__.<locals>.<genexpr>)r'   r   saved_values)r	   r
   r
   r   	__enter__  s    z saved_test_environment.__enter__c       
      C   s   | j }| ` t  |  jtjO  _xz|  D ]n\}}}| }||}	||	kr,d| _||	 | js,| js,t	| d| j
  td|	 d| dtjdd q,W dS )NTz was modified by z
  Before: z
  After:   )fileflushF)r   r   Z
gc_collectchangedZenvironment_alteredr   popr   r   r   r   printr   r    )
r	   exc_typeZexc_valZexc_tbr   r   r@   r   ZcurrentZoriginalr
   r
   r   __exit__  s    
zsaved_test_environment.__exit__N)r   F)=__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r!   r"   r$   r%   r(   r+   r-   r.   r0   r1   r3   r5   r7   r9   r<   r=   rC   rD   rG   rI   rK   rL   rN   rO   rP   rQ   rS   rT   rW   rX   r[   r\   r`   ra   rc   rd   rm   rp   dirr   rv   r{   r|   r~   r   r   r   r   r
   r
   r
   r   r      sp   r   )r6   r   rM   r   rE   r   r^   rU   r:   Ztestr   Ztest.libregrtest.utilsr   Z_multiprocessingZmultiprocessing.processrY   ImportErrorr   r
   r
   r
   r   <module>   s   
