a
    "hF                     @   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 d dlm	Z	 d dl
Z
d dlZe $ ede d dlZW d   n1 s0    Y  d dlZdd ZG dd de
jZG d	d
 d
e
jZG dd de
jZG dd de
jZG dd de
jZedkre
  dS )    N)support)script_helperignorec                 C   s"   t | } tttd d| S )zXDecorator to skip a test if not running under CPython or lacking
    imp.load_dynamic().load_dynamiczimp.load_dynamic() required)r   cpython_onlyunittestskipIfhasattrimp)meth r   5/opt/python-3.9.21/usr/lib/python3.9/test/test_imp.pyrequires_load_dynamic   s    
r   c                   @   s    e Zd ZdZdd Zdd ZdS )	LockTestsz)Very basic test of import lock functions.c                 C   s   |  t |d|  d S )Nz!expected imp.lock_held() to be %r)assertEqualr
   	lock_held)selfexpectedr   r   r   verify_lock_state   s    zLockTests.verify_lock_statec                 C   s   d}t  }| | t|D ]}t   | d qt|D ]}t   q>| | |szt   W n tyx   Y n0 | d d S )N2   Tz5release_lock() without lock should raise RuntimeError)r
   r   r   rangeacquire_lockrelease_lockRuntimeErrorZfail)r   ZLOOPSZlock_held_at_startir   r   r   testLock    s    


zLockTests.testLockN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   r   c                   @   s   e 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
edd Zedd Zedd Zedd Zedd Zeejddd Zdd Zd d! Zejd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,S )-ImportTestsc                 C   s   t d}|j| _|j| _d S )Nztest.encoded_modules)	importlibimport_moduletest_strings__path__	test_path)r   modr   r   r   setUp<   s    
zImportTests.setUpc                 C   s2   | j D ]&\}}}td| }| ||j qd S )Nztest.encoded_modules.module_)r#   r!   r"   r   test)r   modnameencodingZteststrr&   r   r   r   test_import_encoded_moduleA   s
    z&ImportTests.test_import_encoded_modulec              	   C   s   | j D ]N\}}}td| | jd }| |j| W d    q1 sJ0    Y  qtjt	g}| 
t td| W d    n1 s0    Y  d S )Nmodule_r   badsyntax_pep3120)r#   r
   find_moduler%   r   r*   ospathdirname__file__assertRaisesSyntaxError)r   r&   r*   _fdr0   r   r   r   test_find_module_encodingG   s    .z%ImportTests.test_find_module_encodingc              	   C   s   | j D ]\}}}td| | j\}}}|N | |d  | |j| | | d | | d|  W d    q1 s0    Y  qtd\}}}|J | |d  | |jd | | d | | d W d    n1 s0    Y  d S )Nr,   r   z# test %s encoding
tokenizeutf-8z*"""Tokenization help for Python programs.
)	r#   r
   r.   r%   ZassertNotEqualr   r*   tellreadline)r   r&   r*   r5   fpfilenameinfor   r   r   test_issue1267P   s&    

&
zImportTests.test_issue1267c              
   C   s   d}t jdd zt|d d}|d W d    n1 sB0    Y  t|\}}}|  | |j	d W t jd= t
|d  t
|d  n&t jd= t
|d  t
|d  0 d S )	NZtest_imp_helperr   ..pywz%# coding: cp1252
u = 'test.test_imp'
cp1252.pyc)sysr0   insertopenwriter
   r.   closer   r*   r   unlink)r   temp_mod_namefiler=   r>   r   r   r   test_issue3594c   s    (zImportTests.test_issue3594c                 C   sV  t  }ddddddddddd
}t jdkr>| |d d	}n
||}|sZ| d
|  ||}d| }d| }tj	|d}zt j
dtj t|d d}|d W d    n1 s0    Y  t|\}}	}
| | | | |	d d | | |
d d | |
d d | |
d tj t|||	|
}| |jd W d    n1 sl0    Y  t * td t||d }W d    n1 s0    Y  | |jd t 8 td t jst|t|d }W d    n1 s0    Y  | |jd tj|s@t| t|d}|d W d    n1 sl0    Y  t & td t||}W d    n1 s0    Y  | |j d W t jd= dD ]"}t!"||  t!"||  qt!#| t!#d nFt jd= dD ]"}t!"||  t!"||  qt!#| t!#d 0 d S )Ns   ä            )
r9   cp1250cp1251rC   cp1253cp1254cp1255cp1256cp1257cp1258darwinr9   s   äz2can't run this test with %s as filesystem encodingZtest_imp_helper_Ztest_imp_helper_package_z__init__.pyr   rA   rB   za = 1
   r   r   zb = 2
)rA   rD   __pycache__)$rE   getfilesystemencodingplatformr   getskipTestdecoder/   r0   joinrF   curdirrG   rH   r
   r.   ZassertIsNotNoneZ
assertTrueendswithZ	PY_SOURCEload_moduleawarningscatch_warningssimplefilterload_sourcedont_write_bytecodeZload_compiledcache_from_sourceexistsmkdirZload_packagebr   rJ   rmtree)r   Zfs_encodingZknown_localesZspecial_charZdecoded_charrK   Ztest_package_nameZinit_file_namerL   r=   r>   r&   packageextr   r   r   test_issue5604q   s    


(
.

0

$
*

,

zImportTests.test_issue5604c                 C   s$   t jt}| ttjd|g d S )Nr-   )r/   r0   r1   r2   r3   r4   r
   r.   )r   r0   r   r   r   test_issue9319   s    
zImportTests.test_issue9319c                 C   s   t j}t j}t d td}| |d j tj	dg|R  }| 
t | | 
||j | ||j W d    n1 s~0    Y  d S )Nr/   r   )r/   r0   getenvr   EnvironmentVarGuardr
   r.   
addCleanuprI   rh   assertIsZassertIsNot)r   	orig_pathZorig_getenvxZnew_osr   r   r   test_load_from_source   s    	

z!ImportTests.test_load_from_sourcec                 C   sN   d}t |}|d }|d ur*| |j t j|g|R  }| |j| d S )N_heapqr   )r
   r.   rz   rI   rh   r   r   )r   Zexampler}   Zfile_r&   r   r   r   test_issue15828_load_extensions   s    
z+ImportTests.test_issue15828_load_extensionsc                 C   s   d}t |\}}}|  t ||}t d|}t d|}| |j| | |jd | |jd | t t d| W d    n1 s0    Y  d S )N_testimportmultipleZ_testimportmultiple_fooZ_testimportmultiple_barZnonexistent)r
   r.   rI   r   r   r   r3   ImportError)r   mfileobjpathnamedescriptionZmod0Zmod1Zmod2r   r   r   +test_issue16421_multiple_modules_in_one_dll   s    z7ImportTests.test_issue16421_multiple_modules_in_one_dllc                 C   sb   d}d}|  t}t|| W d    n1 s40    Y  | ||jj | ||jj d S )Nzbogus file path	extension)	r3   r   r
   r   ZassertIn	exceptionr0   r   name)r   r0   r   errr   r   r   "test_load_dynamic_ImportError_path   s    *z.ImportTests.test_load_dynamic_ImportError_pathc                 C   sb   d}t |}|d d ur&|d   |d d t jkrB| d t j|d g|dd  R   d S )Nr   r   r^   z/found module doesn't appear to be a C extensionr\   )r
   r.   rI   ZC_EXTENSIONrc   rh   )r   r   foundr   r   r   'test_load_module_extension_file_is_None  s    

z3ImportTests.test_load_module_extension_file_is_Nonec                 C   s   d}zt j|= W n ty"   Y n0 zt|}tjd}t||j	}| 
|j| | 
|jj| | 
|jj	|j	 | tt|d | 
|jd | t j| | W zt j|= W q ty   Y q0 n"zt j|= W n ty   Y n0 0 d S )Nztest.imp_dummyZ_testmultiphaseZ
dummy_namei  )rE   modulesKeyErrorr!   r"   util	find_specr
   r   originr   r   __spec__r   r3   AttributeErrorgetattrZ	int_constr{   )r   r   modulespecr   r   r   3test_issue24748_load_module_skips_sys_modules_check  s.    
z?ImportTests.test_issue24748_load_module_skips_sys_modules_checkz*test meaningful only when writing bytecodec                 C   sX   t  < t jd }t| | tdtjt jdg W d    n1 sJ0    Y  d S )NrA   z
^No moduler@   )	r   temp_cwdZTESTFNr/   rq   assertRaisesRegexr   r
   r.   )r   sourcer   r   r   test_bug7732+  s    


zImportTests.test_bug7732c                 C   s2   t dt jtt j}|t j |t j d S )Nr
   )r
   Z_LoadSourceCompatibilityr2   rG   get_data)r   loaderr   r   r   test_multiple_calls_to_get_data4  s
    
z+ImportTests.test_multiple_calls_to_get_datac              	   C   s   dt  }ttjt  |}ttj||L | td  t	|t
d  W d    n1 s`0    Y  W d    n1 s~0    Y  d S )Ntmpzembedded null )r   typerE   r   r   Z	swap_itemr   
ValueErrorr
   rm   r2   )r   r)   r&   r   r   r   test_load_source;  s
    
zImportTests.test_load_sourcec                 C   sR   t td}G dd d}| t ||  W d    n1 sD0    Y  d S )Ncreate_dynamicc                   @   s   e Zd ZdZdZdS )z,ImportTests.test_issue31315.<locals>.BadSpecNfoo)r   r   r   r   r   r   r   r   r   BadSpecI  s   r   )r   Zget_attributer
   r3   	TypeError)r   r   r   r   r   r   test_issue31315D  s    zImportTests.test_issue31315c                 C   s4   dd l }| |jjd dd l}| |jjd d S )Nr   frozen)_frozen_importlib_externalr   r   r   _frozen_importlib)r   r   r   r   r   r   test_issue_35321O  s    zImportTests.test_issue_35321c                 C   s,   |  tddd |  tddd d S )N*   s   his   Z:}+   s   659)r   _impsource_hash)r   r   r   r   test_source_hashZ  s    zImportTests.test_source_hashc                 C   sh   g dfddgdfddgdfddgdfg}|D ]6\}}|ddg }t j| }| |j d| q,d S )Ndefaultz--check-hash-based-pycsalwaysneverz-cz.import _imp; print(_imp.check_hash_based_pycs)r9   )r   Zassert_python_okr   outstriprd   )r   ZcasesZinterp_argsr   argsresr   r   r   'test_pyc_invalidation_mode_from_cmdline^  s    



z3ImportTests.test_pyc_invalidation_mode_from_cmdlinec              	   C   s   t  ~ tdd}|d W d    n1 s40    Y  tjddtjjd tj	ddgd\}}}t
d|||}W d    n1 s0    Y  | |jd	 d S )
Nzmymod.pywbs   x = 42
T)doraiseinvalidation_modeZmymodr@   )r0   r   )r   r   rG   rH   
py_compilecompilePycInvalidationModeCHECKED_HASHr
   r.   rh   r   r}   )r   r<   rL   r0   r   r&   r   r   r   test_find_and_load_checked_pycm  s    
(.z*ImportTests.test_find_and_load_checked_pycN)r   r   r   r'   r+   r7   r?   rM   rv   rw   r~   r   r   r   r   r   r   r   r   rE   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    ;   s<   	W





	

r    c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ReloadTestszPVery basic tests to make sure that imp.reload() operates just like
    reload().c                 C   s>   t  " dd l}t| W d    n1 s00    Y  d S )Nr   )r   ry   r/   r
   reload)r   r/   r   r   r   test_source  s    
zReloadTests.test_sourcec                 C   s@   t d" dd l}t| W d    n1 s20    Y  d S )Ntimer   )r   CleanImportr   r
   r   )r   r   r   r   r   test_extension  s    zReloadTests.test_extensionc                 C   s@   t d" dd l}t| W d    n1 s20    Y  d S )Nmarshalr   )r   r   r   r
   r   )r   r   r   r   r   test_builtin  s    zReloadTests.test_builtinc                    sh   ddl m} tjd  fdd}| | | td t	| W d    n1 sZ0    Y  d S )Nr   )parserhtmlc                      s    t jd< d S )Nr   )rE   r   r   r   r   r   cleanup  s    z5ReloadTests.test_with_deleted_parent.<locals>.cleanup)
r   r   rE   r   poprz   r   r   r
   r   )r   r   r   r   r   r   test_with_deleted_parent  s    
z$ReloadTests.test_with_deleted_parentN)r   r   r   r   r   r   r   r   r   r   r   r   r   |  s
   r   c                   @   sP   e Zd ZdZe Zee	j
jduddd Zee	j
jduddd ZdS )	PEP3147TestszTests of PEP 3147.Nz1requires sys.implementation.cache_tag not be Nonec              	   C   sF   t jdddd}t jddddd| j}| t|d| d S )Nr   barbazqux.pyr_   
qux.{}.pycT)r/   r0   re   formattagr   r
   ro   r   r0   expectr   r   r   test_cache_from_source  s
    
z#PEP3147Tests.test_cache_from_sourcez4requires sys.implementation.cache_tag to not be Nonec              	   C   sD   t jddddd| j}t jdddd}| t|| d S )Nr   r   r   r_   r   r   )r/   r0   re   r   r   r   r
   source_from_cacher   r   r   r   test_source_from_cache  s
    
z#PEP3147Tests.test_source_from_cache)r   r   r   r   r
   Zget_tagr   r   Z
skipUnlessrE   implementation	cache_tagr   r   r   r   r   r   r     s   
r   c                   @   s&   e Zd Zeejdu ddd ZdS )NullImporterTestsNzNeed an undecodeable filenamec              	   C   s@   t j}t| z| ttj| W t| nt| 0 d S )N)	r   TESTFN_UNENCODABLEr/   rq   r3   r   r
   ZNullImporterrmdir)r   r   r   r   r   test_unencodeable  s
    
z#NullImporterTests.test_unencodeable)r   r   r   r   r   r   r   r   r   r   r   r   r     s   r   __main__)r!   importlib.utilr/   Zos.pathr   rE   r(   r   Ztest.supportr   r   rj   rk   rl   DeprecationWarningr
   r   r   ZTestCaser   r    r   r   r   r   mainr   r   r   r   <module>   s.   
&"  C&
