B
    v9aF                 @   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mZmZmZ d dl	m
Z
 d dlZd dlZd dlZd dlmZmZ d dlm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 e Zde_de_d	ejkrd	e_d
ejkrd
e_e adt_ dt_!dt_"dt_#dt_$dd Z%e%  dd Z&d@ddZ'dAddZ(dBddZ)dZ*ej+dkre,- Z.e.e,krte,/ Z.ej 0e.sdZ*e&d
d Z1e2ej3e2e1j3dZ3dd Z4dCddZ5ej6d d! Z7ej6dDdd"d#d$Z8ej6d%d& Z9G d'd( d(Z:G d)d* d*e:Z;G d+d, d,e:Z<d-d. Z=d/d0 Z>ej6d1d2 Z?d3d4 Z@G d5d6 d6ZAdEd8d9ZBG d:d; d;e jCZDG d<d= d=ZEG d>d? d?eEZFdS )F    N)	machineryutilinvalidate_caches)ResourceReader)PathPurePath)support   )data01)	zipdata01errno	importlibZ	_testcapic              C   sd   x^t jD ]T} xNtjD ]D}tj| }tj| |}tj|r| t_|t_	|t_
|t_d S qW qW d S )N)syspathr   EXTENSION_SUFFIXES
EXTENSIONSnameosjoinexistsextfilename	file_path)r   r   r   r    r   /usr/lib/python3.7/util.py_extension_details&   s    
r   c             C   s4   d| krdnd}t | }t j| |dd}||dS )zAImport a module from importlib both w/ and w/o _frozen_importlib..)r   r   )_frozen_importlib_frozen_importlib_external)freshZblocked)FrozenSource)r   Zimport_fresh_module)Zmodule_namer   frozensourcer   r   r   import_importlib6   s
    
r$   c       
      K   s   |d krt j}nt|ts"|| }d|| j}| |f}t||}| j|_| j|_	||_
x(| D ]\}}|| }	t|||	 qdW |S )Nz{}_{})unittestZTestCase
isinstancetypeformat__name__types	new_class
__module__Z_NAMEZ_KINDitemssetattr)
clskindbasekwargsr   basesZspecializedattrvaluesvaluer   r   r   specialize_class?   s    
r7   c             K   s(   t | d|f|}t | d|f|}||fS )Nr    r!   )r7   )r/   r1   r2   r"   r#   r   r   r   split_frozenR   s    r8   c             K   s   t | |f|S )N)r8   )Z
test_classr1   r2   r   r   r   	test_bothX   s    r9   T)win32cygwinFr!   )r    r!   c             C   s   t t d| S )zRClass decorator that nullifies tests requiring a case-insensitive
    file system.z&requires a case-insensitive filesystem)r%   skipIfCASE_INSENSITIVE_FS)testr   r   r   case_insensitive_testsk   s    r?    c          	   C   sB   t j||d }t|d}|| W d Q R X d| ||fS )Nz.pywz{}.{})r   r   r   openwriter(   )parentr   Zpkg_dircontentr   Zsubfiler   r   r   	submoduler   s    rF   c              g   s   xD| D ]<}|dkr t d|ytj|= W q tk
r@   Y qX qW z
dV  W dx.| D ]&}ytj|= W qX tk
r|   Y qXX qXW X dS )zUncache a module from sys.modules.

    A basic sanity check is performed to prevent uncaching modules that either
    cannot/shouldn't be uncached.

    )r   marshalZimpzcannot uncache {0}N)
ValueErrorr(   r   modulesKeyError)namesr   r   r   r   uncachey   s    



rL   )pkgc            #   s    fddt jD }td }t f|  t|z t  tj	| }|rntj	|d}t
  n|d }|d krd}|d k	rt|d}|| W d Q R X |V  W d Q R X W d Q R X W d Q R X d S )Nc                s"   g | ]}| d d  kr|qS )r   r   )	partition).0n)r   r   r   
<listcomp>   s    ztemp_module.<locals>.<listcomp>z__init__.pyz.pyr@   rA   )r   rI   r   Ztemp_cwdrL   ZDirsOnSysPathr   r   r   r   mkdirrB   rC   )r   rE   rM   	conflictscwdlocationZmodpathZmodfiler   )r   r   temp_module   s     rV   c              k   s   i }zx^dg fdg fdg fdi ffD ]>\}}t t|||< || krR| | }| |= n|}tt|| q$W t| rtd|  dV  W dx | D ]\}}tt|| qW X dS )a,  Context manager to manage the various importers and stored state in the
    sys module.

    The 'modules' attribute is not supported as the interpreter state stores a
    pointer to the dict that the interpreter uses internally;
    reassigning to sys.modules does not have the desired effect.

    	meta_pathr   
path_hookspath_importer_cachezunrecognized arguments: {0}N)getattrr   r.   lenrH   r(   keysr-   )r2   Z	originalsr4   defaultZ	new_valuer6   r   r   r   import_state   s"    

r^   c               @   s6   e Zd ZdZi dddZdd Zdd Zd	d
 ZdS )_ImporterMockz0Base class to help with creating importer mocks.)module_codec            G   s   i | _ i | _x|D ]}|ds&|}n|d td  }d|krFd }n||kr`|ddd }n|}t|}| |_d|_||_	||_
||krdg|_|| j |< ||kr|| | j|< qW d S )Nz	.__init__r   r	   r   z<mock __file__>z<mock __path__>)rI   r`   endswithr[   rsplitr*   
ModuleType
__loader____file____package__r4   __path__)selfr`   rK   r   import_namepackagemoduler   r   r   __init__   s*    



z_ImporterMock.__init__c             C   s
   | j | S )N)rI   )rh   r   r   r   r   __getitem__   s    z_ImporterMock.__getitem__c             C   s   t | j  | _| j  | S )N)rL   rI   r\   _uncache	__enter__)rh   r   r   r   ro      s    
z_ImporterMock.__enter__c             G   s   | j d d d  d S )N)rn   __exit__)rh   exc_infor   r   r   rp      s    z_ImporterMock.__exit__N)r)   r,   __qualname____doc__rl   rm   ro   rp   r   r   r   r   r_      s
   r_   c               @   s"   e Zd ZdZdddZdd ZdS )mock_modulesz!Importer mock using PEP 302 APIs.Nc             C   s   || j krd S | S d S )N)rI   )rh   fullnamer   r   r   r   find_module   s    
zmock_modules.find_modulec             C   sh   || j krtnT| j | tj |< || jkrZy| j|   W n tk
rX   tj |=  Y nX | j | S d S )N)rI   ImportErrorr   r`   	Exception)rh   ru   r   r   r   load_module   s    

zmock_modules.load_module)N)r)   r,   rr   rs   rv   ry   r   r   r   r   rt      s   
rt   c               @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
	mock_specz!Importer mock using PEP 451 APIs.Nc             C   sD   y| j | }W n tk
r"   d S X tj||j| t|dd d}|S )Nrg   )loadersubmodule_search_locations)rI   rJ   r   spec_from_file_locationre   rZ   )rh   ru   r   rD   rk   specr   r   r   	find_spec  s    zmock_spec.find_specc             C   s   |j | jkrt| j|j  S )N)r   rI   rw   )rh   r~   r   r   r   create_module  s    zmock_spec.create_modulec             C   s.   y| j |jj   W n tk
r(   Y nX d S )N)r`   __spec__r   rJ   )rh   rk   r   r   r   exec_module  s    zmock_spec.exec_module)NN)r)   r,   rr   rs   r   r   r   r   r   r   r   rz     s   

rz   c                s(   t jrdd S t  fdd}|S )ztDecorator to protect sys.dont_write_bytecode from mutation and to skip
    tests that require it to be set to False.c              _   s   d S )Nr   )argsr2   r   r   r   <lambda>)      z'writes_bytecode_files.<locals>.<lambda>c                 s(   t j}dt _z | |}W d |t _X |S )NF)r   dont_write_bytecode)r   r2   ZoriginalZ	to_return)fxnr   r   wrapper*  s    z&writes_bytecode_files.<locals>.wrapper)r   r   	functoolswraps)r   r   r   )r   r   writes_bytecode_files%  s    	r   c          
   C   sL   yt t j|  W n0 tk
rF } z|jtjkr6 W dd}~X Y nX dS )zEnsure that the __pycache__ directory for PEP 3147 pyc file exists.

    :param bytecode_path: File system path to PEP 3147 pyc file.
    N)r   rR   r   dirnameOSErrorr   ZEEXIST)bytecode_patherrorr   r   r   ensure_bytecode_path6  s
    r   c              g   s  d}g }i }d}d}z0t  }||d< t }x| D ]}|dsH|}	n|dtd  }	||	 |	tjkrvtj|	= |d}
|}xB|
dd D ]2}t	j
||}t	j
|st	| || qW t	j
||
d d }t|d	}||| W dQ R X || |||< q4W t| }|  t|gd
}|  |V  W d|dk	r`|ddd |dk	rx|ddd t| X dS )aR  Temporarily create each named module with an attribute (named 'attr')
    that contains the name passed into the context manager that caused the
    creation of the module.

    All files are created in a temporary directory returned by
    tempfile.mkdtemp(). This directory is inserted at the beginning of
    sys.path. When the context manager exits all created files (source and
    bytecode) are explicitly deleted.

    No magic is performed when creating packages! This means that if you create
    a module within a package you must also create the package's __init__ as
    well.

    zattr = {0!r}Nz.rootrl   z	.__init__r   z.pyrA   )r   )tempfileZmkdtempsetra   r[   addr   rI   splitr   r   r   r   rR   appendrB   rC   r(   rL   ro   r^   rp   r   Zrmtree)rK   r#   Zcreated_pathsmappingZstate_managerZuncache_managerZtemp_dirZimport_namesr   ri   Z
name_partsr   Z	directoryfiler   r   r   create_modulesB  sL    









r   c                s    fdd}|S )zA mock sys.path_hooks entry.c                s   |  krt S )N)rw   )entry)entriesimporterr   r   hook~  s    zmock_path_hook.<locals>.hookr   )r   r   r   r   )r   r   r   mock_path_hook|  s    r   c               @   s   e Zd Zdd ZdS )CASEOKTestBasec               s,   d}t  fdd|D |kr( d d S )N)s   PYTHONCASEOKPYTHONCASEOKc             3   s   | ]}| j jjjkV  qd S )N)r   _bootstrap_external_osenviron)rO   x)rh   r   r   	<genexpr>  s   z4CASEOKTestBase.caseok_env_changed.<locals>.<genexpr>z/os.environ changes not reflected in _os.environ)anyZskipTest)rh   Zshould_existZpossibilitiesr   )rh   r   caseok_env_changed  s    z!CASEOKTestBase.caseok_env_changedN)r)   r,   rr   r   r   r   r   r   r     s   r   r   c       	         sN   G  fdddt }d}t|}| }tj||d|d}||_||_|S )Nc                   sH   e Zd Zdd ZfddZfddZ fddZ fd	d
ZdS )zcreate_package.<locals>.Readerc             S   s   | S )Nr   )rh   rj   r   r   r   get_resource_reader  s    z2create_package.<locals>.Reader.get_resource_readerc                s   || _ t tr n S d S )N)_pathr&   rx   )rh   r   )r   r   r   open_resource  s    
z,create_package.<locals>.Reader.open_resourcec                s   || _ t tr n S d S )N)r   r&   rx   )rh   path_)r   r   r   resource_path  s    
z,create_package.<locals>.Reader.resource_pathc                sL   || _ ttrx2 D ]*}|d}t|dkr|d |krdS qW dS )N/r	   r   TF)r   r&   rx   r   r[   )rh   r   r   parts)contentsr   r   r   is_resource  s    


z*create_package.<locals>.Reader.is_resourcec             3   s&   t trx D ]
}|V  qW d S )N)r&   rx   )rh   r   )r   r   r   r   r     s    

z'create_package.<locals>.Reader.contentsN)r)   r,   rr   r   r   r   r   r   r   )r   r   r   r   r   Reader  s
   
r   Ztestingpackagezdoes-not-exist)origin
is_package)r   r*   rc   r   
ModuleSpecr   rd   )	r   r   r   r   r   r   rk   r{   r~   r   )r   r   r   r   create_package  s    #
r   c               @   s   e Zd Zejdd Zdd Zdd Zdd Ze	
ejd	k d
dd Zdd Zdd Zdd Zdd Zdd Ze	
ejdk ddd Ze	
ejdk ddd Zdd ZdS ) CommonResourceTestsc             C   s   t d S )N)NotImplementedError)rh   rj   r   r   r   r   execute  s    zCommonResourceTests.executec             C   s   |  tjd d S )Nz
utf-8.file)r   r
   r)   )rh   r   r   r   test_package_name  s    z%CommonResourceTests.test_package_namec             C   s   |  td d S )Nz
utf-8.file)r   r
   )rh   r   r   r   test_package_object  s    z'CommonResourceTests.test_package_objectc             C   s   d}|  t| d S )Nz
utf-8.file)r   r
   )rh   r   r   r   r   test_string_path  s    z$CommonResourceTests.test_string_path)      zrequires os.PathLike supportc             C   s   t d}| t| d S )Nz
utf-8.file)r   r   r
   )rh   r   r   r   r   test_pathlib_path  s    z%CommonResourceTests.test_pathlib_pathc          	   C   s8   t t}|jd }| t | t| W d Q R X d S )Nz
utf-8.file)r   re   rD   assertRaisesrH   r   r
   )rh   r   	full_pathr   r   r   test_absolute_path  s    
z&CommonResourceTests.test_absolute_pathc          	   C   s&   |  t | td W d Q R X d S )Nz../data01/utf-8.file)r   rH   r   r
   )rh   r   r   r   test_relative_path  s    z&CommonResourceTests.test_relative_pathc             C   s   t jtj= | tjd d S )Nz
utf-8.file)r   rI   r
   r)   r   )rh   r   r   r   $test_importing_module_as_side_effect  s    
z8CommonResourceTests.test_importing_module_as_side_effectc          	   C   s&   |  t | td W d Q R X d S )Nz
utf-8.file)r   	TypeErrorr   r)   )rh   r   r   r   test_non_package_by_name  s    z,CommonResourceTests.test_non_package_by_namec          	   C   s0   |  t tjd }| |d W d Q R X d S )Nztest.test_importlib.utilz
utf-8.file)r   r   r   rI   r   )rh   rk   r   r   r   test_non_package_by_package  s    
z/CommonResourceTests.test_non_package_by_package)r   zNo ResourceReader in Python 2c             C   s8   t d}t|t d}| |d | |jjd d S )Ns   Hello, world!)r   r   z
utf-8.file)ioBytesIOr   FileNotFoundErrorr   assertEqualrd   r   )rh   
bytes_datarj   r   r   r   test_resource_opener  s    
z(CommonResourceTests.test_resource_openerc             C   s:   t d}t}t||d}| |d | |jjd d S )Ns   Hello, world!)r   r   z
utf-8.file)r   r   re   r   r   r   rd   r   )rh   r   r   rj   r   r   r   test_resource_path  s
    
z&CommonResourceTests.test_resource_pathc          	   C   s6   t t t d}| t | |d W d Q R X d S )N)r   r   z
utf-8.file)r   r   r   r   )rh   rj   r   r   r   test_useless_loader  s    
z'CommonResourceTests.test_useless_loaderN)r)   r,   rr   abcabstractmethodr   r   r   r   r%   r<   r   version_infor   r   r   r   r   r   r   r   r   r   r   r   r   r     s   r   c               @   s0   e Zd ZdZedd Zedd Zdd ZdS )ZipSetupBaseNc             C   s>   t | jj}|j}t|d | _tj| j t	
d| _d S )Nzziptestdata.zipZziptestdata)r   
ZIP_MODULEre   rD   str	_zip_pathr   r   r   r   import_moduledata)r/   Z	data_pathZdata_dirr   r   r   
setUpClass  s
    zZipSetupBase.setUpClassc             C   s~   yt j| j W n tk
r&   Y nX yt j| j= t j| jj= W n t	k
rV   Y nX y| `| `W n t
k
rx   Y nX d S )N)r   r   remover   rH   rY   rI   r   r)   rJ   AttributeError)r/   r   r   r   tearDownClass  s    
zZipSetupBase.tearDownClassc             C   s   t  }| jt jf|  d S )N)r   Zmodules_setupZ
addCleanupZmodules_cleanup)rh   rI   r   r   r   setUp(  s    zZipSetupBase.setUp)r)   r,   rr   r   classmethodr   r   r   r   r   r   r   r   
  s   r   c               @   s   e Zd ZeZdS )ZipSetupN)r)   r,   rr   r   r   r   r   r   r   r   -  s   r   )N)N)N)r@   )r@   )Tr   )Gr   builtins
contextlibr   r   r   r   r   r   Zimportlib.abcr   r   r   Zos.pathZpathlibr   r   r>   r   r%   r   r   r*   r@   r
   r   SimpleNamespaceZBUILTINSZ	good_nameZbad_namebuiltin_module_namesr   r   r   r   r   r   r   r$   r7   r8   r9   r=   platformre   upperZchanged_namelowerr   Zsource_importlibstaticmethod
__import__r?   rF   contextmanagerrL   rV   r^   r_   rt   rz   r   r   r   r   r   r   ABCr   r   r   r   r   r   r   <module>   s   

	




):		
2J#