o
    HXhK                     @   s6  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
 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 sUw   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re  dS dS )    N)support)import_helper)	os_helper)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   7/opt/python-3.10.19/usr/lib/python3.10/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| | |sEzt   W n
 ty=   Y d S w | d d S 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ej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_moduleD   s   z&ImportTests.test_import_encoded_modulec              	   C   s   | j D ]'\}}}td| | jd }| |j| W d    n1 s%w   Y  qtjt	g}| 
t td| W d    d S 1 sIw   Y  d S )Nmodule_r   badsyntax_pep3120)r%   r   find_moduler'   r   r,   ospathdirname__file__assertRaisesSyntaxError)r   r(   r,   _fdr2   r   r   r   test_find_module_encodingJ   s   "z%ImportTests.test_find_module_encodingc              	   C   s   | j D ]B\}}}td| | j\}}}|' | |d  | |j| | | d | | d|  W d    n1 s@w   Y  qtd\}}}|& | |d  | |jd | | d | | d W d    d S 1 syw   Y  d S )Nr.   r   z# test %s encoding
tokenizeutf-8z*"""Tokenization help for Python programs.
)	r%   r   r0   r'   ZassertNotEqualr   r,   tellreadline)r   r(   r,   r7   fpfilenameinfor   r   r   test_issue1267S   s,   


"zImportTests.test_issue1267c              
   C   s   d}t jdd zFt|d ddd}|d W d    n1 s#w   Y  t|\}}}|  | |j	d	 W t jd= t
|d  t
|d
  d S t jd= t
|d  t
|d
  w )NZtest_imp_helperr   ..pywzlatin-1r,   z%# coding: cp1252
u = 'test.test_imp'
cp1252.pyc)sysr2   insertopenwriter   r0   closer   r,   r   unlink)r   temp_mod_namefiler?   r@   r   r   r   test_issue3594f   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||}|s-| d
|  ||}d| }d| }tj	|d}zEt j
dtj t|d ddd}|d W d    n1 sdw   Y  t|\}}	}
|A | | | |	d d | | |
d d | |
d d | |
d tj t|||	|
}| |jd W d    n1 sw   Y  t  td t||d }W d    n1 sw   Y  | |jd t  td t jst|t|d }W d    n	1 sw   Y  | |jd tj|st| t|ddd}|d W d    n	1 s7w   Y  t  td t||}W d    n	1 sWw   Y  | |j d W t jd= dD ]}t!"||  t!"||  qjt!#| t!#d d S t jd= dD ]}t!"||  t!"||  qt!#| t!#d w )Ns   ä            )
r;   cp1250cp1251rF   cp1253cp1254cp1255cp1256cp1257cp1258darwinr;   s   äz2can't run this test with %s as filesystem encodingZtest_imp_helper_Ztest_imp_helper_package_z__init__.pyr   rC   rD   rE   za = 1
   r   r   zb = 2
)rC   rG   __pycache__)$rH   getfilesystemencodingplatformr   getskipTestdecoder1   r2   joinrI   curdirrJ   rK   r   r0   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   rM   rmtree)r   Zfs_encodingZknown_localesZspecial_charZdecoded_charrN   Ztest_package_nameZinit_file_namerO   r?   r@   r(   packageextr   r   r   test_issue5604t   s   













zImportTests.test_issue5604c                 C   s$   t jt}| ttjd|g d S )Nr/   )r1   r2   r3   r4   r5   r6   r   r0   )r   r2   r   r   r   test_issue9319   s   
zImportTests.test_issue9319c                 C   s   t j}t j}t 3 td}| |d j tj	dg|R  }| 
t | | 
||j | ||j W d    d S 1 s@w   Y  d S )Nr1   r   )r1   r2   getenvr   EnvironmentVarGuardr   r0   
addCleanuprL   rk   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   r0   r}   rL   rk   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    d S 1 sLw   Y  d S )N_testimportmultipleZ_testimportmultiple_fooZ_testimportmultiple_barZnonexistent)r   r0   rL   r   r   r   r5   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 sw   Y  | ||jj | ||jj d S )Nzbogus file path	extension)	r5   r   r   r   ZassertIn	exceptionr2   r   name)r   r2   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r!| d t j|d g|dd  R   d S )Nr   r   ra   z/found module doesn't appear to be a C extensionr_   )r   r0   rL   ZC_EXTENSIONrf   rk   )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w zUt|}tjd}t||j	}| 
|j| | 
|jj| | 
|jj	|j	 | tt|d | 
|jd | t j| | W zt j|= W d S  tyg   Y d S w zt j|= W w  tyw   Y w w )Nztest.imp_dummyZ_testmultiphaseZ
dummy_namei  )rH   modulesKeyErrorr#   r$   util	find_specr   r   originr   r   __spec__r   r5   AttributeErrorgetattrZ	int_constr~   )r   r   modulespecr   r   r   3test_issue24748_load_module_skips_sys_modules_check  s4   
z?ImportTests.test_issue24748_load_module_skips_sys_modules_checkz*test meaningful only when writing bytecodec                 C   sZ   t   t jd }t| | tdtjt jdg W d    d S 1 s&w   Y  d S )NrC   z
^No modulerB   )	r   temp_cwdZTESTFNr1   rt   assertRaisesRegexr   r   r0   )r   sourcer   r   r   test_bug7732.  s   


"zImportTests.test_bug7732c                 C   s6   t dt jtt jdd}|t j |t j d S )Nr   r;   rE   )r   Z_LoadSourceCompatibilityr4   rJ   get_data)r   loaderr   r   r   test_multiple_calls_to_get_data7  s
   
z+ImportTests.test_multiple_calls_to_get_datac              	   C   s   dt  }ttjt  |}ttj||/ | td t	|t
d  W d    n1 s0w   Y  W d    d S W d    d S 1 sHw   Y  d S )Ntmpzembedded null )r   typerH   r   r   Z	swap_itemr   
ValueErrorr   rp   r4   )r   r+   r(   r   r   r   test_load_source>  s   
"zImportTests.test_load_sourcec                 C   sT   t td}G dd d}| t ||  W d    d S 1 s#w   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   BadSpecL  s    r   )r   get_attributer   r5   	TypeError)r   r   r   r   r   r   test_issue31315G  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_35321R  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_hash]  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 ]\}}|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)r;   )r   Zassert_python_okr   outstriprg   )r   casesZinterp_argsr   argsresr   r   r   'test_pyc_invalidation_mode_from_cmdlinea  s   



z3ImportTests.test_pyc_invalidation_mode_from_cmdlinec              	   C   s   t  ? tdd}|d W d    n1 sw   Y  tjddtjjd tj	ddgd\}}}t
d|||}W d    n1 sFw   Y  | |jd	 d S )
Nzmymod.pywbs   x = 42
T)doraiseinvalidation_modeZmymodrB   )r2   r   )r   r   rJ   rK   
py_compilecompilePycInvalidationModeCHECKED_HASHr   r0   rk   r   r   )r   r>   rO   r2   r   r(   r   r   r   test_find_and_load_checked_pycp  s   

z*ImportTests.test_find_and_load_checked_pycc                    s^   dd l  ttd}G dd d}|  fdd}|  td}|d |  d S )Nr   create_builtinc                   @   s   e Zd ZdZdS )z7ImportTests.test_create_builtin_subinterp.<locals>.SpecbuiltinsN)r   r   r    r   r   r   r   r   Spec  s    r   c                     sB   t  } t}|  t  | d  t  d S )Nr_   )rH   getrefcountr   r   r~   r   gcZcollect)Zrefcntr(   r   r   r   r   r   check_get_builtins  s
   

zEImportTests.test_create_builtin_subinterp.<locals>.check_get_builtinszimport builtins)r   r   r   r   Zrun_in_subinterpr   )r   r   r   r   retr   r   r   test_create_builtin_subinterp  s   

z)ImportTests.test_create_builtin_subinterpN) r   r   r    r)   r-   r9   rA   rP   ry   rz   r   r   r   r   r   r   r   r	   r
   rH   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   >   sB    	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    d S 1 sw   Y  d S )Nr   )r   r|   r1   r   reload)r   r1   r   r   r   test_source  s   
"zReloadTests.test_sourcec                 C   B   t d dd l}t| W d    d S 1 sw   Y  d S )Ntimer   )r   CleanImportr   r   r   )r   r   r   r   r   test_extension     "zReloadTests.test_extensionc                 C   r   )Nmarshalr   )r   r   r   r   r   )r   r   r   r   r   test_builtin  r   zReloadTests.test_builtinc                    sj   ddl m} tjd  fdd}| | | td t	| W d    d S 1 s.w   Y  d S )Nr   )parserhtmlc                      s    t jd< d S )Nr   )rH   r   r   r   r   r   cleanup  s   z5ReloadTests.test_with_deleted_parent.<locals>.cleanup)
r   r   rH   r   popr}   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.pyrb   
qux.{}.pycT)r1   r2   rh   formattagr   r   rr   r   r2   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   rb   r   r   )r1   r2   rh   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
skipUnlessrH   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| d S t| w )N)	r   TESTFN_UNENCODABLEr1   rt   r5   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   r#   importlib.utilr1   Zos.pathr   rH   r*   r   Ztest.supportr   r   r   r	   rm   rn   ro   DeprecationWarningr   r   r   ZTestCaser   r"   r   r   r   r   mainr   r   r   r   <module>   s:    

"  `&