a
    "h-H                     @   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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v rd	e_d
ejvrd
e_e a dt _!dt _"dt _#dt _$dt _%dd Z&e&  dd Z'dDddZ(dEddZ)dFddZ*dZ+ej,dvre-. Z/e/e-kr|e-0 Z/ej!1e/sdZ+e'd
d Z2e3ej4e3e2j4dZ4dd Z5dGddZ6d d! Z7ej8d"d# Z9ej8dHdd$d%d&Z:ej8d'd( Z;G d)d* d*Z<G d+d, d,e<Z=G d-d. d.e<Z>d/d0 Z?d1d2 Z@ej8d3d4 ZAej8d5d6 ZBd7d8 ZCG d9d: d:ZDdId<d=ZEG d>d? d?e jFZGG d@dA dAZHG dBdC dCeHZIdS )J    N)	machineryutilinvalidate_caches)ResourceReader)PathPurePath)support   )data01)	zipdata01errno	importlibZ	_testcapic                  C   s`   t jD ]T} tjD ]H}tj| }tj| |}tj|r| t_|t_	|t_
|t_  d S qqd S N)syspathr   EXTENSION_SUFFIXES
EXTENSIONSnameosjoinexistsextfilename	file_path)r   r   r   r    r   @/opt/python-3.9.21/usr/lib/python3.9/test/test_importlib/util.py_extension_details'   s    


r   c                 C   s4   d| v rdnd}t | }t j| |dd}||dS )N.)r   r   )_frozen_importlib_frozen_importlib_external)freshZblockedFrozenSource)r   Zimport_fresh_module)module_namer    frozensourcer   r   r   import_importlib7   s    
r'   c           
      K   s   |d u rt j}nt|ts"|| }d|| j}| |f}t||}| j|_| j|_	||_
| D ]\}}|| }	t|||	 qb|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    
r:   c                 K   s0   t | d|fi |}t | d|fi |}||fS )Nr"   r#   )r:   )r2   r4   r5   r%   r&   r   r   r   split_frozenS   s    r;   c                 K   s   t | |fi |S r   )r;   )Z
test_classr4   r5   r   r   r   	test_bothY   s    r<   T)win32cygwinFr#   r!   c                 C   s   t t d| S )Nz&requires a case-insensitive filesystem)r(   skipIfCASE_INSENSITIVE_FS)testr   r   r   case_insensitive_testsl   s
    rB    c                 C   sV   t j||d }t|d}|| W d    n1 s<0    Y  d| ||fS )N.pywz{}.{})r   r   r   openwriter+   )parentr   Zpkg_dircontentr   Zsubfiler   r   r   	submodules   s    (rJ   c                 C   sB   t | d$}|d t|W  d    S 1 s40    Y  d S )Nrb   )rF   seekmarshalload)Zpyc_pathZpyc_fr   r   r   get_code_from_pycz   s    
rP   c                  g   s   | D ]:}|dv rt d|ztj|= W q ty<   Y q0 qz4d V  W | D ]$}ztj|= W qN typ   Y qN0 qNn,| D ]$}ztj|= W qz ty   Y qz0 qz0 d S )N)r   rN   Zimpzcannot uncache {0})
ValueErrorr+   r   modulesKeyError)namesr   r   r   r   uncache   s*    
rU   )pkgc                #   s&   fddt jD }td }t g|R   t| t  tj	| }|rptj	|d}t
  n|d }|d u rd}|d urt|d}|| W d    n1 s0    Y  |V  W d    n1 s0    Y  W d    n1 s0    Y  W d    n1 s0    Y  d S )Nc                    s"   g | ]}| d d  kr|qS )r   r   )	partition).0nr   r   r   
<listcomp>       ztemp_module.<locals>.<listcomp>z__init__.pyrD   rC   rE   )r   rR   r   Ztemp_cwdrU   ZDirsOnSysPathr   r   r   r   mkdirrF   rG   )r   rI   rV   	conflictscwdlocationmodpathZmodfiler   rZ   r   temp_module   s     (rb   c                  k   s   i }zdg fdg fdg fdi ffD ]>\}}t t|||< || v rP| | }| |= n|}tt|| q"t| r|td|  d V  W | D ]\}}tt|| qn | D ]\}}tt|| q0 d S )N	meta_pathr   
path_hookspath_importer_cachezunrecognized arguments: {0})getattrr   r1   lenrQ   r+   keysr0   )r5   Z	originalsr7   defaultZ	new_valuer9   r   r   r   import_state   s*    
rj   c                   @   s2   e Zd Zi dddZdd Zdd Zdd	 Zd
S )_ImporterMock)module_codec                G   s   i | _ i | _|D ]}|ds$|}n|d td  }d|vrDd }n||kr^|ddd }n|}t|}| |_d|_||_	||_
||krdg|_|| j |< ||v r|| | j|< qd S )N	.__init__r   r	   r   z<mock __file__>z<mock __path__>)rR   rl   endswithrg   rsplitr-   
ModuleType
__loader____file____package__r7   __path__)selfrl   rT   r   import_namepackagemoduler   r   r   __init__   s*    


z_ImporterMock.__init__c                 C   s
   | j | S r   rR   )ru   r   r   r   r   __getitem__   s    z_ImporterMock.__getitem__c                 C   s   t | j  | _| j  | S r   )rU   rR   rh   _uncache	__enter__ru   r   r   r   r}      s    
z_ImporterMock.__enter__c                 G   s   | j d d d  d S r   )r|   __exit__)ru   exc_infor   r   r   r      s    z_ImporterMock.__exit__N)r,   r/   __qualname__ry   r{   r}   r   r   r   r   r   rk      s   rk   c                   @   s   e Zd ZdddZdd ZdS )mock_modulesNc                 C   s   || j vrd S | S d S r   rz   )ru   fullnamer   r   r   r   find_module  s    
zmock_modules.find_modulec                 C   sf   || j vrtnR| j | tj |< || jv rXz| j|   W n tyV   tj |=  Y n0 | j | S d S r   )rR   ImportErrorr   rl   	Exception)ru   r   r   r   r   load_module  s    

zmock_modules.load_module)N)r,   r/   r   r   r   r   r   r   r   r      s   
r   c                   @   s&   e Zd ZdddZdd Zdd ZdS )		mock_specNc                 C   sD   z| j | }W n ty"   Y d S 0 tj||j| t|dd d}|S )Nrt   )loadersubmodule_search_locations)rR   rS   r   spec_from_file_locationrr   rf   )ru   r   r   rH   rx   specr   r   r   	find_spec  s    
zmock_spec.find_specc                 C   s   |j | jvrt| j|j  S r   )r   rR   r   )ru   r   r   r   r   create_module$  s    zmock_spec.create_modulec                 C   s,   z| j |jj   W n ty&   Y n0 d S r   )rl   __spec__r   rS   ru   rx   r   r   r   exec_module)  s    zmock_spec.exec_module)NN)r,   r/   r   r   r   r   r   r   r   r   r     s   

r   c                    s(   t jrdd S t  fdd}|S )Nc                  _   s   d S r   r   )argsr5   r   r   r   <lambda>4  r\   z'writes_bytecode_files.<locals>.<lambda>c                     s2   t j}dt _z | i |}W |t _n|t _0 |S )NF)r   dont_write_bytecode)r   r5   ZoriginalZ	to_returnfxnr   r   wrapper5  s    z&writes_bytecode_files.<locals>.wrapper)r   r   	functoolswraps)r   r   r   r   r   writes_bytecode_files0  s
    r   c              
   C   sN   zt t j|  W n2 tyH } z|jtjkr4 W Y d }~n
d }~0 0 d S r   )r   r]   r   dirnameOSErrorr   EEXIST)bytecode_patherrorr   r   r   ensure_bytecode_pathA  s
    r   c                 c   s*   t j}| t _zd V  W |t _n|t _0 d S r   )r   pycache_prefix)prefixZ_orig_prefixr   r   r   temporary_pycache_prefixM  s
    r   c                  g   s  d}g }i }d }d }zxt  }||d< t }| D ]}|dsF|}	n|d td  }	||	 |	tjv rttj|	= |d}
|}|
d d D ]2}t	j
||}t	j
|st	| || qt	j
||
d d }t|d }||| W d    n1 s
0    Y  || |||< q2t| }|  t|gd	}|  |V  W |d url|d d d  |d ur|d d d  t| n<|d ur|d d d  |d ur|d d d  t| 0 d S )
Nzattr = {0!r}z.rootry   rm   r   rD   rE   r   )tempfilemkdtempsetrn   rg   addr   rR   splitr   r   r   r   r]   appendrF   rG   r+   rU   r}   rj   r   r   rmtree)rT   r&   Zcreated_pathsmappingZstate_managerZuncache_managerZtemp_dirZimport_namesr   rv   Z
name_partsr   Z	directoryfiler   r   r   create_modulesX  sV    




0





r   c                    s    fdd}|S )Nc                    s   |  vrt S r   )r   )entryentriesimporterr   r   hook  s    zmock_path_hook.<locals>.hookr   )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v V  qd S r   )r   _bootstrap_external_osenviron)rX   xr~   r   r   	<genexpr>  s   z4CASEOKTestBase.caseok_env_changed.<locals>.<genexpr>z/os.environ changes not reflected in _os.environ)anyZskipTest)ru   Zshould_existZpossibilitiesr   r~   r   caseok_env_changed  s    z!CASEOKTestBase.caseok_env_changedN)r,   r/   r   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 r   r   ru   rw   r   r   r   get_resource_reader  s    z2create_package.<locals>.Reader.get_resource_readerc                    s   || _ t tr n S d S r   _pathr)   r   ru   r   )r   r   r   open_resource  s    
z,create_package.<locals>.Reader.open_resourcec                    s   || _ t tr n S d S r   r   )ru   path_r   r   r   resource_path  s    
z,create_package.<locals>.Reader.resource_pathc                    sJ   || _ ttr D ],}|d}t|dkr|d |kr dS qdS )N/r	   r   TF)r   r)   r   r   rg   )ru   r   r   partscontentsr   r   r   is_resource  s    

z*create_package.<locals>.Reader.is_resourcec                 3   s"   t tr D ]
}|V  qd S r   )r)   r   )ru   r   r   r   r   r     s    
z'create_package.<locals>.Reader.contentsN)r,   r/   r   r   r   r   r   r   r   r   r   r   r   r   Reader  s
   
r   Ztestingpackagezdoes-not-exist)origin
is_package)r   r-   rp   r   
ModuleSpecr   rq   )	r   r   r   r   r   r   rx   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 r   )NotImplementedError)ru   rw   r   r   r   r   execute  s    zCommonResourceTests.executec                 C   s   |  tjd d S N
utf-8.file)r   r
   r,   r~   r   r   r   test_package_name  s    z%CommonResourceTests.test_package_namec                 C   s   |  td d S r   r   r
   r~   r   r   r   test_package_object  s    z'CommonResourceTests.test_package_objectc                 C   s   d}|  t| d S r   r   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 r   )r   r   r
   r   r   r   r   test_pathlib_path  s    z%CommonResourceTests.test_pathlib_pathc                 C   sL   t t}|jd }| t | t| W d    n1 s>0    Y  d S r   )r   rr   rH   assertRaisesrQ   r   r
   )ru   r   	full_pathr   r   r   test_absolute_path  s    
z&CommonResourceTests.test_absolute_pathc                 C   s:   |  t | td W d    n1 s,0    Y  d S )Nz../data01/utf-8.file)r   rQ   r   r
   r~   r   r   r   test_relative_path  s    z&CommonResourceTests.test_relative_pathc                 C   s   t jtj= | tjd d S r   )r   rR   r
   r,   r   r~   r   r   r   $test_importing_module_as_side_effect  s    
z8CommonResourceTests.test_importing_module_as_side_effectc                 C   s:   |  t | td W d    n1 s,0    Y  d S r   )r   	TypeErrorr   r,   r~   r   r   r   test_non_package_by_name  s    z,CommonResourceTests.test_non_package_by_namec                 C   sD   |  t& tjd }| |d W d    n1 s60    Y  d S )Nztest.test_importlib.utilr   )r   r   r   rR   r   r   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   r   )ioBytesIOr   FileNotFoundErrorr   assertEqualrq   r   )ru   
bytes_datarw   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 r   )r   r   rr   r   r   r   rq   r   )ru   r   r   rw   r   r   r   test_resource_path  s
    
z&CommonResourceTests.test_resource_pathc                 C   sJ   t t t d}| t | |d W d    n1 s<0    Y  d S )Nr   r   )r   r   r   r   r   r   r   r   test_useless_loader  s
    z'CommonResourceTests.test_useless_loaderN)r,   r/   r   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_MODULErr   rH   str	_zip_pathr   r   r   r   import_moduledata)r2   Z	data_pathZdata_dirr   r   r   
setUpClass#  s
    zZipSetupBase.setUpClassc                 C   sx   zt j| j W n ty$   Y n0 zt j| j= t j| jj= W n t	yR   Y n0 z| `| `W n t
yr   Y n0 d S r   )r   r   remover   rQ   re   rR   r   r,   rS   AttributeError)r2   r   r   r   tearDownClass+  s    
zZipSetupBase.tearDownClassc                 C   s    t  }| jt jg|R   d S r   )r   Zmodules_setupZ
addCleanupZmodules_cleanup)ru   rR   r   r   r   setUp>  s    zZipSetupBase.setUp)r,   r/   r   r   classmethodr   r   r   r   r   r   r   r      s   

r   c                   @   s   e Zd ZeZdS )ZipSetupN)r,   r/   r   r   r   r   r   r   r   r   C  s   r   )N)N)N)rC   )rC   )Tr   )Jr   builtins
contextlibr   r   r   r   r   r   importlib.abcr   r   rN   r   Zos.pathpathlibr   r   rA   r   r(   r   r   r-   rC   r
   r   SimpleNamespaceZBUILTINSZ	good_nameZbad_namebuiltin_module_namesr   r   r   r   r   r   r   r'   r:   r;   r<   r@   platformrr   upperZchanged_namelowerr   Zsource_importlibstaticmethod
__import__rB   rJ   rP   contextmanagerrU   rb   rj   rk   r   r   r   r   r   r   r   r   r   ABCr   r   r   r   r   r   r   <module>   s   

	







)


9		
2J#