
    dafu%                     P   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 d dlmZmZ d dlmZmZmZ d dl
mZmZ  ej*                  d       d	 Zd
ddddZ G d d      Z G d d      Z G d dej4                        Zd Zedk(  r ej<                          yy)    N)mock)verbose)forgetmock_register_at_fork)TESTFNunlinkrmtree)script_helperthreading_helperT)modulec                    	 t        |      dz  r	dd l}dd l}ndd l}dd l}|j                  dd      }|j                  t        j                                t        |      | k(  }|r|j                          y y # t        $ r*}|j                  |j                  d              Y d }~sd }~ww xY w# |j                  t        j                                t        |      | k(  }|r|j                          w w xY w)N   r         )
lenmodulefinderrandom	randrange	Exceptionappendwith_traceback	threading	get_identset)	Ndone
done_taskserrorsr   r   xefinisheds	            C/root/Python-3.12.4/Lib/test/test_importlib/test_threaded_import.pytaskr#      s     z?QQ" 	)--/0z?a'HHJ   .a&&t,--. 	)--/0z?a'HHJ s*   1A8 8	B+ B&!B. &B++B. .AC4zaif 1:
        import time
        time.sleep(%(delay)s)
        x = 'a'
        import C
        zaif 1:
        import time
        time.sleep(%(delay)s)
        x = 'b'
        import D
        zimport Bzimport A)ABCDc                       e Zd ZdZd ZddZy)FinderzIA dummy finder to detect concurrent access to its find_spec()
    method.c                 R    d| _         d| _        t        j                         | _        y Nr   )numcallsr   r   Locklockselfs    r"   __init__zFinder.__init__D   s    NN$	    Nc                     t        j                         sJ | j                  5  | xj                  dz  c_        d d d        | j                  }t        j                  d       |dz   | _        y # 1 sw Y   5xY w)Nr   g{Gz?)imp	lock_heldr.   r,   r   timesleep)r0   namepathtargetr   s        r"   	find_speczFinder.find_specI   s\     }}YY 	MMQM	FF

4Q		 	s   A--A6NN)__name__
__module____qualname____doc__r1   r;    r2   r"   r)   r)   @   s    %
	r2   r)   c                       e Zd ZdZddZy)FlushingFinderzMA dummy finder which flushes sys.path_importer_cache when it gets
    called.Nc                 @    t         j                  j                          y N)syspath_importer_cacheclear)r0   r8   r9   r:   s       r"   r;   zFlushingFinder.find_specX   s    %%'r2   r<   )r=   r>   r?   r@   r;   rA   r2   r"   rC   rC   T   s    (r2   rC   c                   b    e Zd Zd Zd Zed        Zd Zd Zd Z	d Z
d Zed	        Zd
 Zd Zy)ThreadedImportTestsc                 N    t         j                  j                  dd       | _        y Nr   )rF   modulespop
old_randomr/   s    r"   setUpzThreadedImportTests.setUp^   s    ++//(D9r2   c                 X    | j                   | j                   t        j                  d<   y y rL   )rO   rF   rM   r/   s    r"   tearDownzThreadedImportTests.tearDowna   s%     ??&$(OOCKK! 'r2   c                   	
 t        j                         rt        j                  d      t	        j
                         dD ]  t        rt        ddd       dD ]  }	 t        j                  |=  g 
g 	j                          t        j                         }t        j                  	
fdt!              D              5  	 d d d        j#                  d	      }t        j                         |z
  }t        rt        d
|dz  z  dd       dt%        	      d}| j'                  
|       | j)                  ||       t        st        d        y # t        $ r Y w xY w# 1 sw Y   xY w)Nz"can't run when import lock is held)   2   rT   rU   rT   rU   Tryingzthreads ... )end)r   r   c              3   \   K   | ]#  }t        j                  t        f        % yw))r:   argsN)r   Threadr#   ).0ir   r   r   r   s     r"   	<genexpr>zAThreadedImportTests.check_parallel_module_init.<locals>.<genexpr>|   s2      0' $$D4V7UVV0's   ),iX  z%.1f msg     @@T)flushrX   zdone: /zOK.)r4   r5   unittestSkipTestr   Eventr   printrF   rM   KeyErrorrH   r6   	monotonicr   start_threadsrangewaitr   assertFalse
assertTrue)r0   mock_osmodnamet0	completeddtdbg_infor   r   r   r   s          @@@@r"   check_parallel_module_initz.ThreadedImportTests.check_parallel_module_inith   sH   ==?##$HII  	Ah=c:5 G,
 FJJJL!B!// 0'"1X0' '   		'*I!B&Bi2c6*$C@(+J;HVX.OOIx0e3	    s   #E E0 	E-,E-0E9	c                 $    | j                          y rE   )rr   r/   s    r"   test_parallel_module_initz-ThreadedImportTests.test_parallel_module_init   s    '')r2   c                    t               }t        j                  j                  d|       	 | j	                          | j                  |j                  d       | j                  |j                  |j                         t        j                  j                  |       y # t        j                  j                  |       w xY wr+   )
r)   rF   	meta_pathinsertrr   assertGreaterr,   assertEqualr   remove)r0   finders     r"   test_parallel_meta_pathz+ThreadedImportTests.test_parallel_meta_path   s    Q'	)++-v2VXXv7MM  (CMM  (s   AB !B?c                    t               t               }fd}t        j                  j	                  d|       t        j
                  j                  |       	 |j                  d       | j                         }| j                  j                  d       | j                  j                  j                         t        j
                  j                  |       t        j                  j                  |       y # t        j
                  j                  |       t        j                  j                  |       w xY w)Nc                 2    j                  d       t        )N )r;   ImportError)r9   r{   s    r"   	path_hookz?ThreadedImportTests.test_parallel_path_hooks.<locals>.path_hook   s    R r2   r   r   )r)   rC   rF   
path_hooksrw   rv   r   r;   rr   rx   r,   ry   r   rz   )r0   flushing_finderr   numtestsr{   s       @r"   test_parallel_path_hooksz,ThreadedImportTests.test_parallel_path_hooks   s      )*	 	a+_-	-%%b)668Hv2VXXv7MM  1NN!!), MM  1NN!!),s   A#C= =A D=c                     	 t         j                  d= dd l}| j	                  |j
                  j                  j                         y # t        $ r Y ?w xY w)Nz+test.test_importlib.threaded_import_hangersr   )rF   rM   re   +test.test_importlib.threaded_import_hangersrj   test_importlibthreaded_import_hangersr   )r0   tests     r"   test_import_hangersz'ThreadedImportTests.test_import_hangers   sM    	IJ 	;,,DDKKL  		s   A 	AAc                   	 d}t        j                  t               | j                  t        j
                  t               t        j                  j                  dt               | j                  t        j                  j                  t               t        j                         D ]|  \  }}|d|iz  }t        t         j                  j                  t        |dz         d      5 }|j                  |j                  d             d d d        | j                  t         |       ~ t#        j$                          g 		fd}	fd}t'        j(                  |	      }t'        j(                  |	      }|j+                          |j+                          |j                          |j                          | j-                  t/        	      d
dh       y # 1 sw Y   xY w)Ng      ?r   delay.pywbutf-8c                  F    dd l } j                  t        | dd              y Nr   r   )r$   r   getattr)r$   resultss    r"   	import_abz<ThreadedImportTests.test_circular_imports.<locals>.import_ab       NN71c401r2   c                  F    dd l } j                  t        | dd              y r   )r%   r   r   )r%   r   s    r"   	import_baz<ThreadedImportTests.test_circular_imports.<locals>.import_ba   r   r2   )r:   ab)osmkdirr   
addCleanupshutilr	   rF   r9   rw   rz   circular_imports_modulesitemsopenjoinwriteencoder   	importlibinvalidate_cachesr   r[   startry   r   )
r0   r   r8   contentsfr   r   t1t2r   s
            @r"   test_circular_importsz)ThreadedImportTests.test_circular_imports   sM    
v.6"06<<> 	*ND(7E"22Hbggll64%<8$? 21012OOFD)		* 	##%	2	2 Y/Y/




	
	WSz2%2 2s   %!GG	c                    d}t         j                  j                  dt        j                         | j                  t         j                  j                  t        j                         t        dz   }t        |d      5 }|j                  |j                  d             d d d        | j                  t        |       | j                  t        t               | j                  t        d       t        j                          t!        j"                         5  t%        t               d d d        t         j&                  t        = y # 1 sw Y   xY w# 1 sw Y   +xY w)Nzif 1:
            import threading
            def target():
                import random
            t = threading.Thread(target=target)
            t.start()
            t.join()
            t = Noner   r   r   r   __pycache__)rF   r9   rw   r   curdirr   rz   r   r   r   r   r   r   r	   r   r   r   wait_threads_exit
__import__rM   )r0   rl   codefilenamer   s        r"   test_side_effect_importz+ThreadedImportTests.test_side_effect_import   s     	299%3E>(D! 	*QGGDKK()	*)'.##%//1 	v	KK	* 	*	 	s   >!EEEEc                     t         j                  j                  t         j                  j                  t              dd      }t        j                  |       y )Npartialzcfimport.pyr   r9   r   dirname__file__r
   assert_python_okr0   fns     r"   'test_concurrent_futures_circular_importz;ThreadedImportTests.test_concurrent_futures_circular_import   s6    WW\\"''//(3#]4&&r*r2   c                     t         j                  j                  t         j                  j                  t              dd      }t        j                  |       y )Nr   zpool_in_threads.pyr   r   s     r"   )test_multiprocessing_pool_circular_importz=ThreadedImportTests.test_multiprocessing_pool_circular_import   s7    WW\\"''//(3#%9;&&r*r2   N)r=   r>   r?   rP   rR   r   rr   rt   r|   r   r   r   r   r   r   rA   r2   r"   rJ   rJ   \   sZ    :4  B*)-.M$3L    ,++r2   rJ   c                  .   t        j                         } t        j                  t         j                  g|   	 t        j                         }t        j                  t
        j                  |       t        j                  d       y # t        $ r Y y w xY w)Ngh㈵>)	r   threading_setupra   addModuleCleanupthreading_cleanuprF   getswitchintervalsetswitchintervalAttributeError)thread_infoold_switchintervals     r"   setUpModuler     su    "224K.@@O;O 224!!#"7"79KLd# s   AB 	BB__main__)_impr4   r   r   rF   r6   r   r   ra   r   test.supportr   test.support.import_helperr   r   test.support.os_helperr   r   r	   r
   r   requires_working_threadingr#   r   r)   rC   TestCaserJ   r   r=   mainrA   r2   r"   <module>r      s     	  
        D ; ; 8 +  + +4 80

 
	 " (( (c+(++ c+L zHMMO r2   