Your IP : 216.73.216.52


Current Path : /opt/alt/python35/lib64/python3.5/test/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/test/__pycache__/test_threaded_import.cpython-35.pyc



��Yf"�@s2ddlZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
mZmZm
Z
mZmZmZmZe
d�Zdd�Zdddd	d
ddd
iZGdd�d�ZGdd�d�ZGdd�dej�Ze
dd��Zedkr.e�dS)�N)	�verbose�
import_module�run_unittest�TESTFN�reap_threads�forget�unlink�rmtree�
start_threads�	threadingc	Cs�z�yYt|�dr1ddl}ddl}nddl}ddl}|jdd�}Wn;tk
r�}z|j|jd��WYdd}~XnXWd|jtj��t|�|k}|r�|j	�XdS)N�r��)
�len�modulefinder�randomZ	randrange�	Exception�append�with_tracebackr�	get_ident�set)	�N�done�
done_tasks�errorsrr�x�eZfinished�r�>/opt/alt/python35/lib64/python3.5/test/test_threaded_import.py�tasks-r�Azaif 1:
        import time
        time.sleep(%(delay)s)
        x = 'a'
        import C
        �Bzaif 1:
        import time
        time.sleep(%(delay)s)
        x = 'b'
        import D
        �Czimport B�Dzimport Ac@s4eZdZdZdd�Zdddd�ZdS)�FinderzIA dummy finder to detect concurrent access to its find_spec()
    method.cCs%d|_d|_tj�|_dS)Nr)�numcallsrrZLock�lock)�selfrrr�__init__@s		zFinder.__init__Nc
CsYtj�st�|j�|jd7_WdQRX|j}tjd�|d|_dS)Nr
g{�G�z�?)�imp�	lock_held�AssertionErrorr&r%r�timeZsleep)r'�name�path�targetrrrr�	find_specEs
	
zFinder.find_spec)�__name__�
__module__�__qualname__�__doc__r(r0rrrrr$<sr$c@s(eZdZdZdddd�ZdS)�FlushingFinderzMA dummy finder which flushes sys.path_importer_cache when it gets
    called.NcCstjj�dS)N)�sys�path_importer_cache�clear)r'r-r.r/rrrr0TszFlushingFinder.find_spec)r1r2r3r4r0rrrrr5Psr5c@s|eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�ThreadedImportTestscCstjjdd�|_dS)Nr)r6�modules�pop�
old_random)r'rrr�setUpZszThreadedImportTests.setUpcCs#|jdk	r|jtjd<dS)Nr)r<r6r:)r'rrr�tearDown]szThreadedImportTests.tearDowncsstj�rtjd��tj��xEdD]=�trPtd�ddd�x7d	d
gD])}ytj	|=Wq]t
k
r�Yq]Xq]Wg�g��j�tj
�}t����fdd�t��D���WdQRX�jd�}tj
�|}tr%td|ddddd�dt���f}|j�|�|j||�tr.td�q.WdS)Nz"can't run when import lock is held��2rZTryingzthreads ...�end� rrc	3s3|])}tjdtd����f�VqdS)r/�argsN)r�Threadr)�.0�i)rrrrrr�	<genexpr>wszAThreadedImportTests.check_parallel_module_init.<locals>.<genexpr>�
�<z%.1f msg@�@�flushTzdone: %s/%szOK.)r?r@)r?r@r?r@r?r@iX)r)r*�unittestZSkipTestrZEventr�printr6r:�KeyErrorr8r,Z	monotonicr
�range�waitr�assertFalseZ
assertTrue)r'�modnameZt0Z	completedZdtZdbg_infor)rrrrr�check_parallel_module_initds6

	
z.ThreadedImportTests.check_parallel_module_initcCs|j�dS)N)rR)r'rrr�test_parallel_module_init�sz-ThreadedImportTests.test_parallel_module_initcCskt�}tjjd|�z7|j�|j|jd�|j|j|j�Wdtjj	|�XdS)Nr)
r$r6�	meta_path�insertrR�
assertGreaterr%�assertEqualr�remove)r'�finderrrr�test_parallel_meta_path�s	
z+ThreadedImportTests.test_parallel_meta_pathcs�t��t�}�fdd�}tjjd|�tjj|�zF|jd�|j�}|j	�j
d�|j�j�j
�Wdtjj
|�tjj
|�XdS)Ncs�jd�t�dS)N�)r0�ImportError)r.)rYrr�	path_hook�s
z?ThreadedImportTests.test_parallel_path_hooks.<locals>.path_hookrr[)r$r5r6�
path_hooksrUrTrr0rRrVr%rWrrX)r'Zflushing_finderr]Znumtestsr)rYr�test_parallel_path_hooks�s		
z,ThreadedImportTests.test_parallel_path_hookscCsFytjd=Wntk
r"YnXddl}|j|jj�dS)Nztest.threaded_import_hangersr)r6r:rMZtest.threaded_import_hangersrPZthreaded_import_hangersr)r'Ztestrrr�test_import_hangers�s
z'ThreadedImportTests.test_import_hangersc	snd}tjt�|jtjt�tjjdt�|jtjj	t�x|t
j�D]n\}}|d|i}ttjj
t|d�d��}|j|jd��WdQRX|jt|�q\Wtj�g��fdd�}�fd	d
�}tjd|�}tjd|�}|j�|j�|j
�|j
�|jt��dd
h�dS)Ng�?r�delayz.py�wbzutf-8cs)ddl}�jt|dd��dS)Nrr)r r�getattr)r )�resultsrr�	import_ab�sz<ThreadedImportTests.test_circular_imports.<locals>.import_abcs)ddl}�jt|dd��dS)Nrr)r!rrc)r!)rdrr�	import_ba�sz<ThreadedImportTests.test_circular_imports.<locals>.import_bar/�a�b)�os�mkdirr�
addCleanup�shutilr	r6r.rUrX�circular_imports_modules�items�open�join�write�encoder�	importlib�invalidate_cachesrrD�startrWr)	r'rar-�contents�frerfZt1Zt2r)rdr�test_circular_imports�s*
%




z)ThreadedImportTests.test_circular_importsc
Cs�d}tjjdtj�|jtjjtj�td}t|d��}|j	|j
d��WdQRX|jt|�|jtt�|jt
d�tj�tt�dS)Nz�if 1:
            import threading
            def target():
                import random
            t = threading.Thread(target=target)
            t.start()
            t.join()rz.pyrbzutf-8�__pycache__)r6r.rUri�curdirrkrXrrorqrrrrr	rsrt�
__import__)r'�code�filenamerwrrr�test_side_effect_import�s

z+ThreadedImportTests.test_side_effect_importN)r1r2r3r=r>rRrSrZr_r`rxr~rrrrr9Xs!

&r9cCsgd}ytj�}tjd�Wntk
r7YnXztt�Wd|dk	rbtj|�XdS)Ng�h㈵��>)r6�getswitchinterval�setswitchinterval�AttributeErrorrr9)Zold_switchintervalrrr�	test_main�s
r��__main__)�_impr)rirsr6r,rlrKZtest.supportrrrrrrrr	r
rrrmr$r5ZTestCaser9r�r1rrrr�<module>s*@�