Your IP : 216.73.216.213


Current Path : /opt/alt/python39/lib64/python3.9/test/__pycache__/
Upload File :
Current File : //opt/alt/python39/lib64/python3.9/test/__pycache__/test__xxsubinterpreters.cpython-39.opt-1.pyc

a

XC?h<�@sZddlmZddlZddlZddlZddlZddlZddlmZddl	Z	ddl
Z
ddlZddlm
Z
ddlmZe
�d�Zdd�ZdKd	d
�ZdLdd�Zejd
d��Zdd�Zifdd�ZGdd�dedd��Zejdd��ZGdd�dedd��ZGdd�dedd��Zdd�d d!�Zd"d#�Zd$d%�Zd&d'�Z Gd(d)�d)ej!�Z"Gd*d+�d+ej!�Z#Gd,d-�d-ej!�Z$Gd.d/�d/e"�Z%Gd0d1�d1e"�Z&Gd2d3�d3e"�Z'Gd4d5�d5e"�Z(Gd6d7�d7e"�Z)Gd8d9�d9e"�Z*Gd:d;�d;e"�Z+Gd<d=�d=e"�Z,Gd>d?�d?e"�Z-Gd@dA�dAe"�Z.GdBdC�dCe"�Z/GdDdE�dEedEdF��Z0e�1dG�GdHdI�dIe"��Z2e3dJk�rVe�4�dS)M�)�
namedtupleN)�dedent)�support)�
script_helperZ_xxsubinterpreterscCs:t��\}}|�dd�}td|�d|�d��}|t|�fS)N�
z
                z-
        import contextlib
        with open(zU, 'w') as spipe:
            with contextlib.redirect_stdout(spipe):
                z	
        )�os�pipe�replacer�open)�script�r�wZindented�wrapped�r�A/opt/alt/python39/lib64/python3.9/test/test__xxsubinterpreters.py�_captured_scripts��
rcCsJt|�\}}|�&t�|||�|��Wd�S1s<0YdS�N)r�interpreters�
run_string�read)�interpZrequest�sharedrZrpiperrr�_run_output"srcCsL|durtj}t��}||}t�|�sHt��|kr<td��t�d�qdS)Nzinterp is not runningg{�G�z�?)rZ
SHORT_TIMEOUT�time�	monotonicr�
is_running�RuntimeError�sleep)rZtimeoutZ
start_timeZdeadlinerrr�_wait_for_interp_to_run)s
rc#s|t��\�}��fdd�}tj|d�}|��t��dVt|d��}|�d�Wd�n1sf0Y|��dS)Ncst��td��d���dS)Nz7
            # wait for "signal"
            with open(z5) as rpipe:
                rpipe.read()
            )rrrr�rrrr�run:s
�z_running.<locals>.run��targetr
Zdone)	rr�	threading�Thread�startrr
�write�join)rr
r �tZspiperrr�_running7s(r)cKst|||�dSr)�_run_interp)�id�sourcerrrr�
run_interpVsr-cCsFt|�}t��}||kr4t��|kr(t�t||�nt�|||�dSr)rr�get_main�get_currentr�execr)r+r,rZ_mainns�mainrrrr*Zsr*cs*eZdZedd��Zd�fdd�	Z�ZS)�InterpretercCs(t||�r|St|t�r ||�St�dSr)�
isinstance�str�NotImplementedError)�cls�rawrrr�from_rawhs


zInterpreter.from_rawNcs�t��}||kr6|sd}n|dkr0td�|���|}nX|durp|sHd}n|dkrXtd��t|tj�s�t�|�}n|r||dkr�d}|}nt��}t��|||�}|S)Nr1z)name mismatch (expected "main", got "{}")rz!name mismatch (unexpected "main"))	rr.�
ValueError�formatr3�
InterpreterID�create�super�__new__)r6�namer+r1�self��	__class__rrr>qs,�zInterpreter.__new__)NN)�__name__�
__module__�__qualname__�classmethodr8r>�
__classcell__rrrArr2fs
r2zname idccs$z
dVWntjyYn0dSr)r�ChannelClosedErrorrrrr�expect_channel_closed�s

rIcs6eZdZd
�fdd�	Zdd�Zdd�Zdd	�Z�ZS)�
ChannelActionNcs&|sd}|sd}t��||||�}|S)N�bothr1�r=r>)r6�action�endrr@rArrr>�szChannelAction.__new__cOsb|jdkr |jdvrJt|j��n*|jdvr@|jdvrJt|j��n
t|j��|jdvr^t|j��dS)N�use)�same�opposite�send�recv��close�force-close)rKrPrQrRrS)r1rP�other�extra)rMrNr9r)r@�args�kwargsrrr�__init__�s





zChannelAction.__init__cCs2|jdkr|S|jdkr(|dkr$dSdS|jSdS)NrPrQrRrS�rN�r@rNrrr�resolve_end�s


zChannelAction.resolve_endcCst|jdkr|S|jdkr(|dur$t�|S|jdkrB|dur>t�|S|jdkrp|jdkrZ|S|rl|jdkrl|St�dS)NrPrWrXr1)rrr?)r@rrWrXrrr�resolve_interp�s 




zChannelAction.resolve_interp)NN)rCrDrEr>r[r^r_rGrrrArrJ�srJzaction end interpcsBeZdZddd��fdd�Zdd�Zdd	�Zd
d�dd
�Z�ZS)�ChannelStaterF��closedcst��|||�}|SrrL)r6�pendingrbr@rArrr>�szChannelState.__new__cCst|�|jd|jd�S�N�ra��typercrb�r@rrr�incr�szChannelState.incrcCst|�|jd|jd�Srdrfrhrrr�decr�szChannelState.decrT��forcecCs2|jr|r|jdkr|St|�|r&dn|jdd�S)NrTra)rbrcrg)r@rlrrrrU�szChannelState.close)r)rCrDrEr>rirjrUrGrrrArr`�sr`zpending closedT��
hideclosedcCsr|jr(|dkr"|dkr"|jr"d}q,d}nd}zt||||�}Wn&tjyd|sX|sX�|��}Yn
0|rnd�|S)NrOrSFT.)rbrc�_run_actionrrHrU)�cidrMrN�staternZ
expectfail�resultrrr�
run_action�srscCs|dkr�|dkr$t�|d�|��S|dkrx|jsdzt�|�WntjyX|YS0td��q�t�|�|��Snt|��n~|dkr�i}|dvr�d||<tj	|fi|��|�
�S|d	k�r�d
di}|dvr�d||<tj	|fi|��|j
dd�St|��dS)NrOrR�spamrSzexpected ChannelEmptyErrorrU�rSrRTrVrlrk)r�channel_sendrirc�channel_recv�ChannelEmptyError�	Exceptionrjr9�
channel_closerU)rprMrNrqrZrrrro�s8





�roc	Cs>t��D]0}|dkrqzt�|�Wqty6Yq0qdS)Nr)r�list_all�destroyr�r+rrr�clean_up_interpreterssr~c	Cs6t��D](}zt�|�Wqtjy.Yq0qdSr)r�channel_list_all�channel_destroy�ChannelNotFoundError�rprrr�clean_up_channelss
r�c@seZdZdd�ZdS)�TestBasecCst�t�dSr�r~r�rhrrr�tearDown)szTestBase.tearDownN)rCrDrEr�rrrrr�'sr�c@seZdZdd�Zdd�ZdS)�IsShareableTestsc	CsPgd�}|D]>}|�|�� |�t�|��Wd�q1s@0YqdS)N)Nrt�spam�
i����)�subTest�
assertTruer�is_shareable)r@Z
shareables�objrrr�test_default_shareables3s	�z(IsShareableTests.test_default_shareablesc
Cs�Gdd�d�}Gdd�dt�}ddtdttt�t�d||d	�|d
�g}|D]B}|�t|��� |�t�	|��Wd�qJ1s�0YqJdS)Nc@seZdZdd�Zdd�ZdS)z3IsShareableTests.test_not_shareable.<locals>.CheesecSs
||_dSr�r?)r@r?rrrr[Dsz<IsShareableTests.test_not_shareable.<locals>.Cheese.__init__cSs|jSrr�rhrrr�__str__Fsz;IsShareableTests.test_not_shareable.<locals>.Cheese.__str__N)rCrDrEr[r�rrrr�CheeseCsr�c@seZdZdZdS)z5IsShareableTests.test_not_shareable.<locals>.SubByteszA subclass of a shareable type.N)rCrDrE�__doc__rrrr�SubBytesIsr�TF.gY@ZWensleydalert)
�bytes�NotImplementedrg�objectryr��repr�assertFalserr�)r@r�r�Znot_shareablesr�rrr�test_not_shareableBs(��z#IsShareableTests.test_not_shareableN)rCrDrEr�r�rrrrr�1sr�cs`eZdZ�fdd�Z�fdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Z�ZS)�ShareableTypeTestscst���t��|_dSr)r=�setUpr�channel_createrprhrArrr�es
zShareableTypeTests.setUpcst�|j�t���dSr)rr�rpr=r�rhrArrr�iszShareableTypeTests.tearDownc	Csr|D]h}|�|��Jt�|j|�t�|j�}|�||�|�t|�t|��Wd�q1sb0YqdSr)r�rrvrprw�assertEqual�assertIsrg)r@�valuesr��gotrrr�_assert_valuesmsz!ShareableTypeTests._assert_valuesc	Cs^dD]T}|�|��6t�|j|�t�|j�}|�||�Wd�q1sN0YqdS)Nr)r�rrvrprwr�)r@r�r�rrr�test_singletonsxs
z"ShareableTypeTests.test_singletonscCs|�dd|jg�dS)Nrti')r�rprhrrr�
test_types�s
�zShareableTypeTests.test_typescCs|�dd�tdd�D��dS)Ncss|]}|jdddd�VqdS)��littleT)ZsignedN)�to_bytes)�.0�irrr�	<genexpr>�s�z0ShareableTypeTests.test_bytes.<locals>.<genexpr>����)r��rangerhrrr�
test_bytes�s
�zShareableTypeTests.test_bytescCs|�gd��dS)N)zhello worldu你好世界�)r�rhrrr�	test_strs�szShareableTypeTests.test_strscCs*|�t�tdd�tjtjdg��dS)Nr�r�re)r��	itertools�chainr��sys�maxsizerhrrr�test_int�s�zShareableTypeTests.test_intc
Cs�tjdtjdddg}|D]f}|�|��H|�t��t�|j|�Wd�n1s^0YWd�q 1s|0Yq dS)Nrer���)r�r�r��assertRaises�
OverflowErrorrrvrp)r@Zintsr�rrr�test_non_shareable_int�s
�z)ShareableTypeTests.test_non_shareable_int)
rCrDrEr�r�r�r�r�r�r�r�r�rGrrrArr�cs	r�c@s$eZdZdd�Zdd�Zdd�ZdS)�ListAllTestscCs"t��}t��}|�||g�dSr)rr.r{r�)r@r1�idsrrr�test_initial�szListAllTests.test_initialcCs6t��}t��}t��}t��}|�||||g�dSr)rr.r<r{r��r@r1�first�secondr�rrr�test_after_creating�s
z ListAllTests.test_after_creatingcCs>t��}t��}t��}t�|�t��}|�|||g�dSr)rr.r<r|r{r�r�rrr�test_after_destroying�s
z"ListAllTests.test_after_destroyingN)rCrDrEr�r�r�rrrrr��sr�c@seZdZdd�Zdd�ZdS)�GetCurrentTestscCs.t��}t��}|�||�|�|tj�dSr)rr.r/r��assertIsInstancer;)r@r1�currrr�	test_main�szGetCurrentTests.test_maincCsRt��}t��}t|td��}t|���}t��\}}|�||�|�	||�dS)Nz�
            import _xxsubinterpreters as _interpreters
            cur = _interpreters.get_current()
            print(cur)
            assert isinstance(cur, _interpreters.InterpreterID)
            )
rr.r<rr�int�stripr{r�ZassertNotEqual)r@r1r�outr��_�expectedrrr�test_subinterpreter�sz#GetCurrentTests.test_subinterpreterN)rCrDrEr�r�rrrrr��sr�c@seZdZdd�Zdd�ZdS)�GetMainTestscCs0t��\}t��}|�||�|�|tj�dSr)rr{r.r�r�r;)r@r�r1rrr�test_from_main�s
zGetMainTests.test_from_maincCs<t��\}t��}t|td��}t|���}|�||�dS)Nz�
            import _xxsubinterpreters as _interpreters
            main = _interpreters.get_main()
            print(main)
            assert isinstance(main, _interpreters.InterpreterID)
            )rr{r<rrr�r�r�)r@r�rr�r1rrr�test_from_subinterpreter�s

z%GetMainTests.test_from_subinterpreterN)rCrDrEr�r�rrrrr��sr�c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�IsRunningTestscCst��}|�t�|��dSr)rr.r�r)r@r1rrrr��szIsRunningTests.test_maincCsdt��}|�t�|��t|�� |�t�|��Wd�n1sF0Y|�t�|��dSr)rr<r�rr)r��r@rrrrr��s

.z"IsRunningTests.test_subinterpretercCs2t��}t|td|�d���}|�|��d�dS)Nz`
            import _xxsubinterpreters as _interpreters
            if _interpreters.is_running(zZ):
                print(True)
            else:
                print(False)
            �True)rr<rrr�r�)r@rr�rrrr��s
�z'IsRunningTests.test_from_subinterpretercCsJt��}t�|�|�t��t�|�Wd�n1s<0YdSr)rr<r|r�rrr�rrr�test_already_destroyed�s
z%IsRunningTests.test_already_destroyedcCs8|�t��t�d�Wd�n1s*0YdS�Ni@B)r�rrrrhrrr�test_does_not_existsz"IsRunningTests.test_does_not_existcCs8|�t��t�d�Wd�n1s*0YdS�Nr�)r�r9rrrhrrr�test_bad_idszIsRunningTests.test_bad_idN)	rCrDrEr�r�r�r�r�r�rrrrr��sr�c@sDeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�InterpreterIDTestscCs"tjddd�}|�t|�d�dS)Nr�Trk)rr;r�r��r@r+rrr�
test_with_intsz InterpreterIDTests.test_with_intcCs4Gdd�dt�}tj|�dd�}|�t|�d�dS)Nc@seZdZdd�ZdS)z.InterpreterIDTests.test_coerce_id.<locals>.IntcSsdS�Nr�rrhrrr�	__index__sz8InterpreterIDTests.test_coerce_id.<locals>.Int.__index__N�rCrDrEr�rrrr�Intsr�Trkr�)r4rr;r�r�)r@r�r+rrr�test_coerce_idsz!InterpreterIDTests.test_coerce_idcCsf|�ttjt��|�ttjd�|�ttjd�|�ttjd�|�ttjd�|�ttjd�dS�Ng$@�10s10r�l)r��	TypeErrorrr;r�r9r�rhrrrr�szInterpreterIDTests.test_bad_idcCsHt��}|�t��"t�t|�d�Wd�n1s:0YdS�Nre)rr�r�rr;r�r�rrrr�$sz&InterpreterIDTests.test_does_not_existcCs"tjddd�}|�t|�d�dS�Nr�Trkr�)rr;r�r4r�rrr�test_str)szInterpreterIDTests.test_strcCs"tjddd�}|�t|�d�dS)Nr�TrkzInterpreterID(10))rr;r�r�r�rrr�	test_repr-szInterpreterIDTests.test_reprcCs(t��}t�t|��}t��}|�||k�|�||k�|�|t|�k�|�t|�|k�|�|tt|��k�|�tt|��|k�|�|tt|��dk�|�|tt|��k�|�|ddk�|�|td�k�|�|dk�|�||k�|�||k�|�||k�|�||k�dS�N皙�����?r�r��infr�)rr<r;r�r��floatr�r4�r@�id1�id2�id3rrr�
test_equality1s$z InterpreterIDTests.test_equalityN)
rCrDrEr�r�r�r�r�r�r�rrrrr�
sr�c@sNeZdZdd�Ze�d�dd��Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�ZdS)�CreateTestscCs*t��}|�|tj�|�|t���dSr)rr<r�r;�assertInr{r�rrr�test_in_mainJszCreateTests.test_in_mainz*enable this test when working on pystate.ccCsDt�}td�D] }t��}t�|�|�|�q|�t|�d�dS)N�d)�setr�rr<r|�addr��len)r@�seenr�r+rrr�test_unique_idPs
zCreateTests.test_unique_idcsnt���d���fdd�}tj|d�}��|��Wd�n1sH0Y|��|��t���dS)Ncst���������dSr)rr<�acquire�releaser�r+�lockrr�f]sz%CreateTests.test_in_thread.<locals>.fr!)r#ZLockr$r%r'r�rr{�r@r�r(rr�r�test_in_threadZs&zCreateTests.test_in_threadcCsJt��\}t��}t|td��}t|���}|�tt���|||h�dS)Nz�
            import _xxsubinterpreters as _interpreters
            id = _interpreters.create()
            print(id)
            assert isinstance(id, _interpreters.InterpreterID)
            )	rr{r<rrr�r�r�r�)r@r1r�r�r�rrr�test_in_subinterpreteris

z"CreateTests.test_in_subinterpretercs^t��\}t���d���fdd�}tj|d�}|��|��|�tt���|��h�dS)Ncst�td��}t|����dS)Nz�
                import _xxsubinterpreters as _interpreters
                id = _interpreters.create()
                print(id)
                )rrr�r�)r��r�r�rrr�zsz6CreateTests.test_in_threaded_subinterpreter.<locals>.fr!)	rr{r<r#r$r%r'r�r�)r@r1r�r(rr�r�test_in_threaded_subinterpretervs
	z+CreateTests.test_in_threaded_subinterpretercCsjtt���}g}td�D]}t��}|�|�q|D]}t�|�q4t��}|�tt���||hB�dS�N�)r�rr{r�r<�appendr|r��r@�beforer�r�r+rrr�test_after_destroy_all�sz"CreateTests.test_after_destroy_allcCs`tt���}t��}t��}t��}t�|�t�|�t��}|�tt���|||hB�dSr)r�rr{r<r|r�)r@rr�r�r�r+rrr�test_after_destroy_some�s

z#CreateTests.test_after_destroy_someN)rCrDrEr��unittest�skipr�r�r�rrrrrrrr�Hs
	
r�c@s\eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�ZdS)�DestroyTestscCsft��}t��}t��}|�|t���t�|�|�|t���|�|t���|�|t���dSr)rr<r�r{r|�assertNotInr�rrr�test_one�s
zDestroyTests.test_onecCsvtt���}t�}td�D]}t��}|�|�q|�tt���||B�|D]}t�|�qN|�tt���|�dSr)r�rr{r�r<r�r�r|rrrr�test_all�szDestroyTests.test_allcslt��\���t��t���Wd�n1s40Y��fdd�}tj|d�}|��|��dS)Ncs8��t��t���Wd�n1s*0YdSr�r�rrr|r�r1r@rrr��sz!DestroyTests.test_main.<locals>.fr!)	rr{r�rr|r#r$r%r'r�rrrr��s
(zDestroyTests.test_maincCsJt��}t�|�|�t��t�|�Wd�n1s<0YdSr)rr<r|r�rr�rrrr��s
z#DestroyTests.test_already_destroyedcCs8|�t��t�d�Wd�n1s*0YdSr�rrhrrrr��sz DestroyTests.test_does_not_existcCs8|�t��t�d�Wd�n1s*0YdSr�)r�r9rr|rhrrrr��szDestroyTests.test_bad_idcCsJt��\}t��}td|�d��}t�||�|�tt���||h�dS)Nzo
            import _xxsubinterpreters as _interpreters
            try:
                _interpreters.destroy(zD)
            except RuntimeError:
                pass
            �rr{r<rrr�r�)r@r1r+rrrr�test_from_current�s
�
zDestroyTests.test_from_currentcCsRt��\}t��}t��}td|�d��}t�||�|�tt���||h�dS)NzZ
            import _xxsubinterpreters as _interpreters
            _interpreters.destroy(�)
            r)r@r1r�r�rrrr�test_from_sibling�s
�
zDestroyTests.test_from_siblingcs4t����fdd�}tj|d�}|��|��dS)Ncst���dSr)rr|rr}rrr��sz.DestroyTests.test_from_other_thread.<locals>.fr!)rr<r#r$r%r'r�rr}r�test_from_other_thread�s
z#DestroyTests.test_from_other_threadc	Cs�t��\}t��}t|��||jt�|�d|�d�d�|jtd|�d�d��t�|�Wd�n1sn0Y|�t�|��Wd�n1s�0YdS)NzInterp z& should be running before destruction.)�msgz%Should not be able to destroy interp z while it's still running.)	rr{r<r)r�rr�rr|)r@r1rrrr�test_still_running�s


�
�(zDestroyTests.test_still_runningN)
rCrDrErr
r�r�r�r�rrrrrrrrr
�s

	r
cseZdZed�ZdZ�fdd�Z�fdd�Zdd�Zd	d
�Z	dd�Z
e�e
ed
�d�dd��Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zejd5dd��Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Z d0d1�Z!e�"d2�d3d4��Z#�Z$S)6�RunStringTestszJ
        with open('{}', 'w') as out:
            out.write('{}')
        r�cst���t��|_d|_dSr)r=r�rr<r+�_fsrhrArrr�s

zRunStringTests.setUpcs"|jdur|j��t���dSr)rrUr=r�rhrArrr�s

zRunStringTests.tearDowncCsVtd�\}}|�&t�|j|�|��}Wd�n1s<0Y|�|d�dS)N�print("it worked!", end="")�
it worked!)rrrr+rr�)r@r�filer�rrr�test_successs
&zRunStringTests.test_successcsrtd�\�}|�B��fdd�}tj|d�}|��|��|��}Wd�n1sX0Y��|d�dS)Nrcst��j��dSr)rrr+r�rr@rrr�$sz(RunStringTests.test_in_thread.<locals>.fr!r)rr#r$r%r'rr�)r@rr�r(r�rrrr�!s&zRunStringTests.test_in_threadcCs`tjdd�}td�\}}|�$t�||�|��}Wd�n1sF0Y|�|d�dS)NF)�isolatedz�
            import threading
            def f():
                print('it worked!', end='')

            t = threading.Thread(target=f)
            t.start()
            t.join()
            r)rr<rrrr�)r@Z	subinterprrr�rrr�test_create_thread.s	&z!RunStringTests.test_create_thread�forkztest needs os.fork()cCs�ddl}|�d��j}|�d�|��d}td|j�d|�d��}t�|j|�|�	d�|�
�}|�||�Wd�n1s�0YdS)Nrzw+r�zspam spam spam spam spamz�
                import os
                try:
                    os.fork()
                except RuntimeError:
                    with open('z3', 'w') as out:
                        out.write('z')
                )�tempfileZNamedTemporaryFiler&�flushrr?rrr+�seekrr�)r@r!rr�rZcontentrrr�	test_fork?s
��

zRunStringTests.test_forkc	Csft|j��H|�t��t�|jd�Wd�n1s:0YWd�n1sX0YdS)N�
print("spam"))r)r+r�rrrrhrrr�test_already_runningUsz#RunStringTests.test_already_runningcCsTd}|t��vr|d7}q|�t��t�|d�Wd�n1sF0YdS)Nrrer%)rr{r�rrr�rrrr�Zs

z"RunStringTests.test_does_not_existcCs:|�t��t�dd�Wd�n1s,0YdS)Nr�r%)r�r9rrrhrrr�
test_error_idaszRunStringTests.test_error_idcCs:|�t��t�dd�Wd�n1s,0YdS)Nr�r%)r�r�rrrhrrrr�eszRunStringTests.test_bad_idcCs<|�t��t�|jd�Wd�n1s.0YdSr��r�r�rrr+rhrrr�test_bad_scriptiszRunStringTests.test_bad_scriptcCs<|�t��t�|jd�Wd�n1s.0YdS)Ns
print("spam")r(rhrrr�test_bytes_for_scriptmsz$RunStringTests.test_bytes_for_scriptNccsz|�tj��}dVWd�n1s(0Y|dur\|�t|j��d�dt|��n|�t|j�d�||��dS)N�:rz{}: {})r�r�RunFailedErrorr�r4�	exception�splitr:)r@�exctyperZcaughtrrr�assert_run_failedqs$�
�z RunStringTests.assert_run_failedcCs<|�t��t�|jd�Wd�n1s.0YdS)Nzprint("spam")r0�SyntaxErrorrrr+rhrrr�test_invalid_syntax|sz"RunStringTests.test_invalid_syntaxcCs>|�td��t�|jd�Wd�n1s00YdS)Nr�zraise Exception("spam"))r0ryrrr+rhrrr�test_failure�szRunStringTests.test_failurecCs>|�td��t�|jd�Wd�n1s00YdS)N�42zraise SystemExit(42))r0�
SystemExitrrr+rhrrr�test_SystemExit�szRunStringTests.test_SystemExitcCs~|�t��"t�|jtd��Wd�n1s20Y|�td��"t�|jtd��Wd�n1sp0YdS)NzG
                import sys
                sys.exit()
                r4zI
                import sys
                sys.exit(42)
                )r0r5rrr+rrhrrr�
test_sys_exit�s0zRunStringTests.test_sys_exitcCs�t��\}}dddd�}td|�d��}t�|j||�t|d��}t�|�}Wd�n1sb0Y|�	|dd�|�	|d	d
�|�	|dd�|�
|d
�dS)N�hams-1)r��eggs�cheddarz�
            eggs = int(eggs)
            spam = 42
            result = spam + eggs

            ns = dict(vars())
            del ns['__builtins__']
            import pickle
            with open(�C, 'wb') as chan:
                pickle.dump(ns, chan)
            �rbr��*r9r�rr�)r:)rrrrrr+r
�pickle�loadr�ZassertIsNone�r@rr
rr�chan�nsrrr�test_with_shared�s��
(zRunStringTests.test_with_sharedcCs�t�|jtd��ddi}td�}t�|j||�t��\}}td|�d��}t�|j|�t|d��}t�|�}Wd�n1s�0Y|�	|ddd	�|�	|d
dd�|�	|dd�dS)Nzj
            spam = 'eggs'
            ns1 = dict(vars())
            del ns1['__builtins__']
            r�r8zL
            ns2 = dict(vars())
            del ns2['__builtins__']
        zr
            ns = dict(vars())
            del ns['__builtins__']
            import pickle
            with open(r;r<�ns1r9�ns2)
rrr+rrrr
r?r@r�)r@rrrr
rBrCrrr�test_shared_overwrites�s�
(z%RunStringTests.test_shared_overwritescCs|t��\}}ddi}td|�d��}t�|j||�t|d��}t�|�}Wd�n1s^0Y|�	|dd�dS)NrCsnot __main__z�
            spam = 42

            ns = dict(vars())
            del ns['__builtins__']
            import pickle
            with open(r;r<)
rrrrrr+r
r?r@r�rArrr�#test_shared_overwrites_default_vars�s�
	(z2RunStringTests.test_shared_overwrites_default_varscCs�t��\}}t�|jtd|�d���t|d��}t�|�}Wd�n1sP0Yt��\}}t�|jtd|�d���t|d��}t�|�}Wd�n1s�0Y|�	d|�|�
d|�|�	d|�|�	d|�dS)Nz�
            spam = True

            ns = dict(vars())
            del ns['__builtins__']
            import pickle
            with open(zd, 'wb') as chan:
                pickle.dump(ns, chan)
            del ns, pickle, chan
            r<z�
            eggs = False

            ns = dict(vars())
            del ns['__builtins__']
            import pickle
            with open(r;r�r9)rrrrr+rr
r?r@r�r)r@rr
rBrErFrrr�test_main_reused�s �
(�	(zRunStringTests.test_main_reusedc
Cs�t��\}}td|�d��}t�|j|�t|d��}t�|�}Wd�n1sT0Y|�	d�|�	d�|�
|diddddd��dS)	Nz�
            spam = 42

            ns = dict(vars())
            ns['__builtins__'] = str(ns['__builtins__'])
            import pickle
            with open(r;r<�__builtins__�
__loader__�__main__r=)rC�__annotations__r��__package__�__spec__r�)rrrrrr+r
r?r@�popr�)r@rr
rrBrCrrr� test_execution_namespace_is_mains"�
	(

�z/RunStringTests.test_execution_namespace_is_mainzblocking foreverc	Cs�td�}t���P}t�|d|�}t�|��}|��}Wd�n1sH0YWd�n1sf0Y|�|d�dS)Na�
        from textwrap import dedent
        import threading
        import _xxsubinterpreters as _interpreters
        id = _interpreters.create()
        def f():
            _interpreters.run_string(id, dedent('''
                import time
                # Give plenty of time for the main interpreter to finish.
                time.sleep(1_000_000)
                '''))

        t = threading.Thread(target=f)
        t.start()
        rr)rrZtemp_dirrZmake_scriptZspawn_python�waitr�)r@r�dirname�filename�procZretcoderrr�test_still_running_at_exit s
Dz)RunStringTests.test_still_running_at_exit)N)%rCrDrErZSCRIPTZFILENAMEr�r�rr�rrZ
skipUnless�hasattrrr$r&r�r'r�r)r*�
contextlib�contextmanagerr0r2r3r6r7rDrGrHrIrQr	rVrGrrrArrs8



!rc@sTeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�ChannelIDTestscCs0tjddd�}|�t|�d�|�|jd�dS)Nr�TrkrK)r�_channel_idr�r�rN�r@rprrr�test_default_kwargs>sz"ChannelIDTests.test_default_kwargscCs�tjdddd�}|�|jd�tjddddd�}|�|jd�tjdddd�}|�|jd�tjddddd	�}|�|jd�tjddddd�}|�|jd
�dS)Nr�T�rRrlrRF�rRrSrl�rSrlrS)rSrRrlrK)rr[r�rNr\rrr�test_with_kwargsDszChannelIDTests.test_with_kwargscCs4Gdd�dt�}tj|�dd�}|�t|�d�dS)Nc@seZdZdd�ZdS)z*ChannelIDTests.test_coerce_id.<locals>.IntcSsdSr�rrhrrrr�Vsz4ChannelIDTests.test_coerce_id.<locals>.Int.__index__Nr�rrrrr�Usr�Trkr�)r4rr[r�r�)r@r�rprrrr�TszChannelIDTests.test_coerce_idcCsf|�ttjt��|�ttjd�|�ttjd�|�ttjd�|�ttjd�|�ttjd�dSr�)r�r�rr[r�r9r�rhrrrr�\szChannelIDTests.test_bad_idcCs>|�t�� tjdddd�Wd�n1s00YdS)Nr�F�rRrS)r�r9rr[rhrrr�test_bad_kwargsdszChannelIDTests.test_bad_kwargscCsJt��}|�tj��"t�t|�d�Wd�n1s<0YdSr�)rr�r�r�r[r�r\rrrr�hsz"ChannelIDTests.test_does_not_existcCs"tjddd�}|�t|�d�dSr�)rr[r�r4r\rrrr�mszChannelIDTests.test_strcCs�tjddd�}|�t|�d�tjdddd�}|�t|�d�tjdddd�}|�t|�d�tjddddd	�}|�t|�d�dS)
Nr�Trkz
ChannelID(10)r^zChannelID(10, send=True)r`zChannelID(10, recv=True)r_)rr[r�r�r\rrrr�qszChannelIDTests.test_reprcCs(t��}t�t|��}t��}|�||k�|�||k�|�|t|�k�|�t|�|k�|�|tt|��k�|�tt|��|k�|�|tt|��dk�|�|tt|��k�|�|ddk�|�|td�k�|�|dk�|�||k�|�||k�|�||k�|�||k�dSr�)rr�r[r�r�r�r�r4)r@�cid1�cid2Zcid3rrrr�~s$zChannelIDTests.test_equalityN)rCrDrEr]rar�r�rcr�r�r�r�rrrrrZ<s
rZc@s>eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Ze�d)�d*d+��Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Z d:d;�Z!d<d=�Z"d>d?�Z#d@dA�Z$dBdC�Z%dDdE�Z&dFdG�Z'dHdI�Z(dJdK�Z)dLS)M�ChannelTestscCst��}|�|tj�dSr)rr�r�Z	ChannelIDr\rrr�test_create_cid�szChannelTests.test_create_cidcCsrt��}t��}t��}t��}t��}|�|t|�d�|�|t|�d�|�t|�t|�|||h�dSr�)rrr�r�r�r�)r@rr�r�r�Zafterrrr�test_sequential_ids�sz ChannelTests.test_sequential_idscCs\t��}t|td��}t|���}t��}t|td��}t|���}|�|t|�d�dS)Nz�
            import _xxsubinterpreters as _interpreters
            cid = _interpreters.channel_create()
            print(cid)
            re)rr<rrr�r�r�)r@r�r�rdr�rerrr�test_ids_global�szChannelTests.test_ids_globalcCs@t��}tj|dd�}tj|dd�}|�|g�|�|g�dS)z=Test listing interpreters for a channel with no associations.T�rRFN)rr��channel_list_interpretersr�)r@rp�send_interps�recv_interpsrrr�#test_channel_list_interpreters_none�s
z0ChannelTests.test_channel_list_interpreters_nonecCs�t��}t��}t�|d�tj|dd�}tj|dd�}|�||g�|�|g�t��}t|td|�d���tj|dd�}tj|dd�}|�||g�|�||g�dS)z(Test basic listing channel interpreters.rRTrjF�e
            import _xxsubinterpreters as _interpreters
            obj = _interpreters.channel_recv(rN)	rr.r�rvrkr�r<rr)r@�interp0rprlrm�interp1rrr�$test_channel_list_interpreters_basic�s�z1ChannelTests.test_channel_list_interpreters_basiccCs�t��}t��}t��}t��}t��}t�|d�t|td|�d���t|td|�d���t|td|�d���tj|dd�}tj|dd�}|�t	|�||h�|�t	|�||h�d	S)
z?Test listing interpreters for a channel with many associations.rR�_
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_send(z, "send")
            rorTrjFN)
rr.r<r�rvrrrkr�r�)r@rprq�interp2�interp3rprlrmrrr�'test_channel_list_interpreters_multiple�s&���z4ChannelTests.test_channel_list_interpreters_multiplecCs�t��}t��}t��}t�|d�t|td|�d���tj|dd�}tj|dd�}|�||g�|�||g�t�	|�tj|dd�}tj|dd�}|�||g�|�|g�dS)z?Test listing channel interpreters with a destroyed interpreter.rRrorTrjFN)
rr.r<r�rvrrrkr�r|�r@rprqrprlrmrrr�(test_channel_list_interpreters_destroyed�s �
z5ChannelTests.test_channel_list_interpreters_destroyedcCs>t��}t��}t��}t��}t�|d�t|td|�d���t�|d�t|td|�d���tj|dd�}tj|dd�}|�t	|�d�|�t	|�d�tj
|dd�tj|dd�}tj|dd�}|�t	|�d	�|�t	|�d�t|td
|�d���tj|dd�}tj|dd�}|�t	|�d	�|�||g�dS)z:Test listing channel interpreters with a released channel.�datarorTrjFrer�r�b
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_release(N)rr.r<r�rvrrrkr�r��channel_release)r@rprqrtrprlrmrrr�'test_channel_list_interpreters_released	s8���z4ChannelTests.test_channel_list_interpreters_releasedcCs�t��}t��}t��}t�|d�tj|dd�}tj|dd�}|�t|�d�|�t|�d�tj|dd�|�	tj
��tj|dd�Wd�n1s�0Y|�	tj
��tj|dd�Wd�n1s�0YdS)	z8Test listing channel interpreters with a closed channel.rRTrjFrerrkN)rr.r<r�rvrkr�r�rzr�rHrwrrr�%test_channel_list_interpreters_closed4s,z2ChannelTests.test_channel_list_interpreters_closedcCsXt��}t��}t��}t�|d�tj|dd�}tj|dd�}|�t|�d�|�t|�d�tj|dd�|�	tj
��tj|dd�Wd�n1s�0Ytj|dd�}|�t|�d�t|td|�d	���|�	tj
��tj|dd�Wd�n1�s0Y|�	tj
��tj|dd�Wd�n1�sJ0YdS)
zCTest listing channel interpreters with a channel's send end closed.rRTrjFrerN�`
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_close(�, force=True)
            )
rr.r<r�rvrkr�r�rzr�rHrrrwrrr�.test_channel_list_interpreters_closed_send_endJs(,�.z;ChannelTests.test_channel_list_interpreters_closed_send_endcCs>t��}d}t�||�t�|�}|�||�|�||�dS�Nrt)rr�rvrwr�ZassertIsNot)r@rpZorigr�rrr�test_send_recv_mainns
z ChannelTests.test_send_recv_maincCst��}t|td��}dS)Na6
            import _xxsubinterpreters as _interpreters
            cid = _interpreters.channel_create()
            orig = b'spam'
            _interpreters.channel_send(cid, orig)
            obj = _interpreters.channel_recv(cid)
            assert obj is not orig
            assert obj == orig
            )rr<rr)r@r�r�rrr�test_send_recv_same_interpreterwsz,ChannelTests.test_send_recv_same_interpretercCs@t��}t��}t|td|�d���}t�|�}|�|d�dS)Nrs�, b'spam')
            rt)rr�r<rrrwr�)r@rpr�r�r�rrr�%test_send_recv_different_interpreters�s�
z2ChannelTests.test_send_recv_different_interpreterscsVt����fdd�}tj|d�}|��t��d�|��t���}|�|d�dS)NcsDzt���}Wq4Wqtjy0t�d�Yq0qt��|�dS)Nr�)rrwrxrrrv)r�r�rrr��s
z8ChannelTests.test_send_recv_different_threads.<locals>.fr!rt)	rr�r#r$r%rvr'rwr��r@r�r(r�rr�r� test_send_recv_different_threads�s
z-ChannelTests.test_send_recv_different_threadscsft���t���d����fdd�}tj|d�}|��t��d�|��t���}|�	|d�dS)Ncs t�td��d��d����dS)Nz�
                import time
                import _xxsubinterpreters as _interpreters
                while True:
                    try:
                        obj = _interpreters.channel_recv(z�)
                        break
                    except _interpreters.ChannelEmptyError:
                        time.sleep(0.1)
                assert(obj == b'spam')
                _interpreters.channel_send(z, b'eggs')
                )rrr�rpr�r�rrr��s
�
�zIChannelTests.test_send_recv_different_interpreters_and_threads.<locals>.fr!rt�eggs)
rr�r<r#r$r%rvr'rwr�r�rr�r�1test_send_recv_different_interpreters_and_threads�s
z>ChannelTests.test_send_recv_different_interpreters_and_threadscCs<|�tj��t�dd�Wd�n1s.0YdS)Nr�rt)r�rr�rvrhrrr�test_send_not_found�sz ChannelTests.test_send_not_foundcCs:|�tj��t�d�Wd�n1s,0YdSr�)r�rr�rwrhrrr�test_recv_not_found�sz ChannelTests.test_recv_not_foundcCsBt��}|�tj��t�|�Wd�n1s40YdSr)rr�r�rxrwr\rrr�test_recv_empty�szChannelTests.test_recv_emptyc	Cs�t�}t��}t�||�}t�|d�t�|d�t�|d�t�|d�t�||�}t�||�}t�|�}t�||�}t�||�}|�||�|�|d�|�|d�|�|d�|�|d�|�||�dS)Nrertr�)r�rr�rwrvr�r�)	r@�defaultrpZobj1Zobj2Zobj3Zobj4Zobj5Zobj6rrr�test_recv_default�s$
zChannelTests.test_recv_defaultcCsRt��}t��}t|td�t|jd��}t�|�}|�|d�|�|�	�d�dS)Nz�
            import _xxsubinterpreters as _interpreters
            print(cid.end)
            _interpreters.channel_send(cid, b'spam')
            r�rtrR)
rr�r<rr�dictrRrwr�r��r@rprr�r�rrr�test_run_string_arg_unresolved�s

�
z+ChannelTests.test_run_string_arg_unresolvedz(disabled until high-level channels existcCs`t��}tj|dd�}t��}t|td�t|jd��}t�|�}|�	|d�|�	|�
�d�dS)NT)Z_resolvez�
            import _xxsubinterpreters as _interpreters
            print(chan.id.end)
            _interpreters.channel_send(chan.id, b'spam')
            )rBrtrR)rr�r[r<rrr�rRrwr�r�r�rrr�test_run_string_arg_resolved�s

�
z)ChannelTests.test_run_string_arg_resolvedcCs�t��}t�|d�t�|�t�|�|�tj��t�|d�Wd�n1sV0Y|�tj��t�|�Wd�n1s�0YdS�Nrtr��rr�rvrwrzr�rHr\rrr�test_close_single_users

*z#ChannelTests.test_close_single_usercCst��}t��}t��}t�|td|�d���t�|td|�d���t�|�|�tj��(}t�|td|�d���Wd�n1s�0Y|�dt	|j
��|�tj��(}t�|td|�d���Wd�n1s�0Y|�dt	|j
��dS)Nrsr�z_
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_recv(rz,
                _interpreters.channel_send(z, b'spam')
                rH)rr�r<rrrzr�r,r�r4r-)r@rpr�r��cmrrr�test_close_multiple_userss(
�
�

�*
�*z&ChannelTests.test_close_multiple_userscCsbt��}t�|d�t�|�t�|�|�tj��t�|�Wd�n1sT0YdSr�r�r\rrr�test_close_multiple_times(s

z&ChannelTests.test_close_multiple_timesc
Cs�gd�}|D]�\}}|�||f���t��}t�|d�t�|�tj|||d�|�tj��t�|d�Wd�n1s�0Y|�tj��t�|�Wd�n1s�0YWd�q1s�0YqdS)N))FF�TF)FT)TTrtrbr�)r�rr�rvrwrzr�rH)r@ZtestsrRrSrprrr�test_close_empty1s
*zChannelTests.test_close_emptycCspt��}t�|d�t�|d�|�tj��t�|�Wd�n1sL0Yt�|�t�|d�dS)Nrtr8r��rr�rvr�ZChannelNotEmptyErrorrzrwr\rrr�%test_close_defaults_with_unused_itemsDs(
z2ChannelTests.test_close_defaults_with_unused_itemscCs�t��}t�|d�t�|d�|�tj��tj|dd�Wd�n1sP0Yt�|�t�|d�t�|�t�|�tj|dd�dS)Nrtr8T�rSr�r�r\rrr�*test_close_recv_with_unused_items_unforcedNs,


z7ChannelTests.test_close_recv_with_unused_items_unforcedcCs�t��}t�|d�t�|d�tj|dd�|�tj��t�|d�Wd�n1s\0Yt�|�t�|�|�tj��t�|�Wd�n1s�0YdS)Nrtr8Trjr��rr�rvrzr�rHrwr\rrr�*test_close_send_with_unused_items_unforced[s*

z7ChannelTests.test_close_send_with_unused_items_unforcedcCs�t��}t�|d�t�|d�|�tj�� tj|ddd�Wd�n1sR0Yt�|�t�|d�t�|�t�|�tj|dd�dS)Nrtr8Trur�r�r�r\rrr�*test_close_both_with_unused_items_unforcedhs.


z7ChannelTests.test_close_both_with_unused_items_unforcedcCs�t��}t�|d�t�|d�tj|ddd�|�tj��t�|d�Wd�n1s^0Y|�tj��t�|�Wd�n1s�0YdS)Nrtr8Tr`r�r�r\rrr�(test_close_recv_with_unused_items_forcedus*z5ChannelTests.test_close_recv_with_unused_items_forcedcCs�t��}t�|d�t�|d�tj|ddd�|�tj��t�|d�Wd�n1s^0Y|�tj��t�|�Wd�n1s�0YdS)Nrtr8Tr^r�r�r\rrr�(test_close_send_with_unused_items_forced�s*z5ChannelTests.test_close_send_with_unused_items_forcedcCs�t��}t�|d�t�|d�tj|dddd�|�tj��t�|d�Wd�n1s`0Y|�tj��t�|�Wd�n1s�0YdS)Nrtr8Tr_r�r�r\rrr�(test_close_both_with_unused_items_forced�s*z5ChannelTests.test_close_both_with_unused_items_forcedcCs�t��}t�|�|�tj��t�|d�Wd�n1s@0Y|�tj��t�|�Wd�n1sv0YdSr�)rr�rzr�rHrvrwr\rrr�test_close_never_used�s
*z"ChannelTests.test_close_never_usedcCs�t��}t�|d�t��}t�|td|�d���|�tj��t�|�Wd�n1s`0Y|�tj��t�	|�Wd�n1s�0YdS)Nrtr~r)
rr�rvr<rrr�rHrwrz�r@rprrrr�!test_close_by_unassociated_interp�s
�(z.ChannelTests.test_close_by_unassociated_interpcCs�t��}t�|d�t�|d�t�|d�t�|�tj|dd�|�tj��t�|d�Wd�n1sr0Y|�tj��t�|�Wd�n1s�0YdS)NrtTrkr�r�r\rrr�-test_close_used_multiple_times_by_single_user�s
*z:ChannelTests.test_close_used_multiple_times_by_single_usercCs�t��}|�tj��tjddd�Wd�n1s80Yt�|�|�tj��tj|dd�Wd�n1s|0YdS)Nr�Trj)rr�r�r�rkrzrHr\rrr�.test_channel_list_interpreters_invalid_channel�s,
z;ChannelTests.test_channel_list_interpreters_invalid_channelcCs@t��}|�t��t�|�Wd�n1s20YdSr)rr�r�r�rkr\rrr�+test_channel_list_interpreters_invalid_args�sz8ChannelTests.test_channel_list_interpreters_invalid_argsN)*rCrDrErgrhrirnrrrvrxr|r}r�r�r�r�r�r�r�r�r�r�r�rr	r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrf�sL	+$	
	



	

rfc@s`eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�ChannelReleaseTestsa�
    - main / interp / other
    - run in: current thread / new thread / other thread / different threads
    - end / opposite
    - force / no force
    - used / not used  (associated / not associated)
    - empty / emptied / never emptied / partly emptied
    - closed / not closed
    - released / not released
    - creator (interp) / other
    - associated interpreter not running
    - associated interpreter destroyed
    cCs�t��}t�|d�t�|�tj|ddd�|�tj��t�|d�Wd�n1s\0Y|�tj��t�|�Wd�n1s�0YdS�NrtTrbr��rr�rvrwr{r�rHr\rrr�test_single_user�s
*z$ChannelReleaseTests.test_single_usercCsxt��}t��}t��}t�|td|�d���t|td|�d|�d���}t�|td|�d���|�|��d�dS)	Nrsr�roz,)
            _interpreters.channel_release(z+)
            print(repr(obj))
            z+
            _interpreters.channel_release(rzb'spam')rr�r<rrrr�r�)r@rpr�r�r�rrr�test_multiple_userss
���
�z'ChannelReleaseTests.test_multiple_userscCs�t��}t�|d�t�|�t�|�|�tj��t�|d�Wd�n1sV0Y|�tj��t�|�Wd�n1s�0YdSr�r�r\rrr�test_no_kwargss

*z"ChannelReleaseTests.test_no_kwargscCsnt��}t�|d�t�|�tj|ddd�|�tj�� tj|ddd�Wd�n1s`0YdS)NrtTrbr�r\rrr�test_multiple_times s
z'ChannelReleaseTests.test_multiple_timescCsjt��}t�|d�t�|d�tj|ddd�|�tj��t�|�Wd�n1s\0YdS)Nrtr8Trb)rr�rvr{r�rHrwr\rrr�test_with_unused_items)sz*ChannelReleaseTests.test_with_unused_itemscCs�t��}t�|�|�tj��t�|d�Wd�n1s@0Y|�tj��t�|�Wd�n1sv0YdSr�)rr�r{r�rHrvrwr\rrr�test_never_used2s
*z#ChannelReleaseTests.test_never_usedcCs�t��}t�|d�t��}t�|td|�d���t�|�}t�|�|�tj	��t�|d�Wd�n1sv0Y|�
|d�dS)Nrtrzrr�)rr�rvr<rrrwr{r�rHr�)r@rprr�rrr�test_by_unassociated_interp;s
�

*z/ChannelReleaseTests.test_by_unassociated_interpc	Csht��}t��}t�|td|�d|�d���|�tj��t�|�Wd�n1sZ0YdS)Nze
            import _xxsubinterpreters as _interpreters
            obj = _interpreters.channel_send(z5, b'spam')
            _interpreters.channel_release(r)rr�r<rrr�rHrwr�rrr�test_close_if_unassociatedJs
��z.ChannelReleaseTests.test_close_if_unassociatedcCsRt��}t�|d�t�|�t�|d�tj|dd�t�|�}|�|d�dS)NrtTrj)rr�rvrwr{r�)r@rpr�rrr�test_partiallyWs

z"ChannelReleaseTests.test_partiallycCs�t��}t�|d�t�|d�t�|d�t�|�tj|ddd�|�tj��t�|d�Wd�n1st0Y|�tj��t�|�Wd�n1s�0YdSr�r�r\rrr�'test_used_multiple_times_by_single_userbs
*z;ChannelReleaseTests.test_used_multiple_times_by_single_userN)rCrDrEr�r�r�r�r�r�r�r�r�r�r�rrrrr��s'			
r�cszeZdZdZ�fdd�Zedd��Zedd��Zdd	�Zddd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Z�ZS)�ChannelCloseFixtureFcsx|jri}n4t�|�}t�|�}t�|�}|j||j||j|i}|sHd}t��||||||�}t�|_t�|_	||_
|S)NrP)�QUICKr2r8r?r=r>r��_preppedr`�_state�_known)r6rNrrWrX�creatorZknownr@rArrr>ws 


�zChannelCloseFixture.__new__cCs|jSr�r�rhrrrrq�szChannelCloseFixture.statecCs>z|jWSty8|�|j�}|�|�|_|jYS0dSr)�_cid�AttributeError�_get_interpreterr��_new_channel)r@r�rrrrp�szChannelCloseFixture.cidcCs|�|�}|�|�|Sr)r��_prep_interpreterr�rrr�get_interpreter�s

z#ChannelCloseFixture.get_interpreterNcCs2|dur|j}|dkr&|jjdkr&dSt|jj�S)NrSrRF)rNrqrb�boolr]rrr�expect_closed_error�s
z'ChannelCloseFixture.expect_closed_errorcCs|�|�dSr)r�r�rrr�prep_interpreter�sz$ChannelCloseFixture.prep_interpretercCs
||_dSrr�)r@rMrrrrr�
record_action�sz!ChannelCloseFixture.record_actioncCst�t�dSrr�rhrrr�clean_up�szChannelCloseFixture.clean_upcCs@|jdkrt��St��}t|jd|�d��t�|�|_|jS)Nr1a
                import _xxsubinterpreters
                cid = _xxsubinterpreters.channel_create()
                # We purposefully send back an int to avoid tying the
                # channel to the other interpreter.
                _xxsubinterpreters.channel_send(zC, int(cid))
                del _xxsubinterpreters
                )r?rr�r-r+rwr�)r@r�Zchrrrr��s
�
z ChannelCloseFixture._new_channelcCsj|dvr|jS|dkr|jS|dkr*|jS|}z|j|}Wn$ty`t|�}|j|<Yn0|SdS)N)rPrrWrX)rrWrXr��KeyErrorr2)r@rr?rrrr��sz$ChannelCloseFixture._get_interpretercCsF|j|jvrdS|j�|j�|jdkr,dSt|jd|j�d��dS)Nr1a
            import _xxsubinterpreters as interpreters
            import test.test__xxsubinterpreters as helpers
            ChannelState = helpers.ChannelState
            try:
                cid
            except NameError:
                cid = interpreters._channel_id(r)r+r�r�r?r-rpr�rrrr��s
�z%ChannelCloseFixture._prep_interpreter)N)rCrDrEr�r>�propertyrqrpr�r�r�r�r�r�r�r�rGrrrArr�ps


r�zend interp other extra creatorz%these tests take several hours to runc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�dd�Z	dd�Z
dd�Zd"dd�Zdd�Z
d#dd�Zdd�Zdd�Zd d!�ZdS)$�ExhaustiveChannelTestsa�
    - main / interp / other
    - run in: current thread / new thread / other thread / different threads
    - end / opposite
    - force / no force
    - used / not used  (associated / not associated)
    - empty / emptied / never emptied / partly emptied
    - closed / not closed
    - released / not released
    - creator (interp) / other
    - associated interpreter not running
    - associated interpreter destroyed

    - close after unbound
    ccs�gV|�dd�D]"}|V|��D]}||Vq$q|�dd�D]"}|V|��D]}||VqTqB|�dd�D]z}|V|�dd�D]*}||}|V|��D]}||Vq�q�|�dd�D]*}||}|V|��D]}||Vq�q�qr|�dd�D]�}|V|�dd�D].}||}|V|��D]}||V�q*�q|�dd�D].}||}|V|��D]}||V�qf�qLq�dS)NrPrWrX)�_iter_close_action_sets�_iter_post_close_action_sets�_iter_use_action_sets)r@ZcloseactionsZpostactionsZ
useactions�actionsrrr�iter_action_setssBz'ExhaustiveChannelTests.iter_action_setsccs||f}tdd|�gVtdd|�gVtdd|�tdd|�gVtdd|�gVtdd|�gVtdd|�tdd|�gV|D]<}|D]2}|D](}tdd|�tdd|�tdd|�gVq�q�q�|D]P}|D]F}|D]<}|D]2}tdd|�tdd|�tdd|�tdd|�gVq�q�q�q�dS)NrOrSrR�rJ)r@rqrt�interpsruZinterp4rrrr�9s>
�
�

�
�
�

�


�



�z,ExhaustiveChannelTests._iter_use_action_setsc
	cs�d}||f}dD]4}|rdnd}|D]}|D]}t|||�gVq,q$qdD]<}	dD]2}
|D](}|D]}t|	d|�t|
d|�gVqbqZqRqJdS)Nrur�rVrUrTrSrRr�)
r@rqrtZendsr�rl�oprrNZrecvopZsendoprSrRrrrr�js
�

�z.ExhaustiveChannelTests._iter_close_action_setsccs.dD]$}tdd|�gVtdd|�gVqdS)N)rPrXrWrOrSrRr�r�rrrr�}s

�
�z3ExhaustiveChannelTests._iter_post_close_action_setscCs|D]}|�||�qdSr)rs)r@�fixr�rMrrr�run_actions�sz"ExhaustiveChannelTests.run_actionsTrmcCs�|�|j�}|�|j|j|j�}|�|�|jdkrZt|j	|j
||j|d�}|�||�n�t
��}t|jd|j	�dt|j
��dt|��dt|j��d|�d|�d|�d��tt�t
�|�d	�tt
�|��d
�}|�||�dS)Nr1rmzB
                result = helpers.run_action(
                    z,
                    z!,
                    hideclosed=zB,
                    )
                interpreters.channel_send(zR, result.pending.to_bytes(1, 'little'))
                interpreters.channel_send(z2, b'X' if result.closed else b'')
                r�)rcrb)r^rNr_rrWrXr�r?rsrprMrqr�rr�r-r+r�r`r��
from_bytesrwr�)r@r�rMrnrNrrrr�rrrrs�sB

�������	�
�z!ExhaustiveChannelTests.run_actionc	csBgd�}|D]0\}}}dD] }dD]}t|||||�Vq"qqdS)N))r1rrX)rr1rX)rqrtrX)rqrtr1)rPrWr�rb)r�)r@rrrWrXr�rNrrr�
iter_fixtures�s
z$ExhaustiveChannelTests.iter_fixturescCst|rdnd}t||jd�}|��s4|j||dd�n<|�tj�� |j||dd�Wd�n1sf0YdS)NrVrUrPFrm)rJrNr�rsr�rrH)r@r�rlr�rUrrr�_close�szExhaustiveChannelTests._closeNcCs.|dus|jdkr�|�tj��t�|j�Wd�n1s@0Y|�tj��t�|jd�Wd�n1sz0Y|�tj��t�|j�Wd�n1s�0Y|�tj�� tj|jdd�Wd�n1s�0Yn0t|j	d�t|j	d�t|j	d�t|j	d�dS)	Nr1rtTrkzz
                with helpers.expect_channel_closed():
                    interpreters.channel_recv(cid)
                z�
                with helpers.expect_channel_closed():
                    interpreters.channel_send(cid, b'spam')
                z{
                with helpers.expect_channel_closed():
                    interpreters.channel_close(cid)
                z�
                with helpers.expect_channel_closed():
                    interpreters.channel_close(cid, force=True)
                )
r?r�rrHrwrprvrzr-r+)r@r�rrrr�_assert_closed_in_interp�s*,*0z/ExhaustiveChannelTests._assert_closed_in_interpcCs||�|jj�t|jj�D]}t�|j�q|�|�dD]&}|�	|�}|j
dkrTq:|�||�q:|�	d�}|�||�dS)N)rPrWr1Zfresh)r�rqrbr�rcrrwrpr�r�r?)r@r�r�rrrr�_assert_closed�s



z%ExhaustiveChannelTests._assert_closedFc	cs�d}|��D]�}t�|��D]�}|d7}|dkr:dS|rl|dddkrTt�t||d�t|���n2|dddkr�tddd�td	dd�tj��|||fVq|rtd
�qt�dS)Nrrer��z({} actions)� r�r\�.z---)r��printr�r:r�r��stdoutr")r@�verboser�r�r�rrr�_iter_close_tests�s$
z(ExhaustiveChannelTests._iter_close_testscCsdt_|��D]
\}}}qdS)NT)r�r�r��r@r�r�r�rrr�_skim_close_tests	sz(ExhaustiveChannelTests._skim_close_testsc	Cs�|��D]v\}}}|�d�|||���@|�|j�|�||�|j|dd�|�|�Wd�n1sl0Y|��qdS)N�	{} {}  {}Frk�	r�r�r:r�rr�r�r�r�r�rrr�
test_close	s(z!ExhaustiveChannelTests.test_closec	Cs�|��D]v\}}}|�d�|||���@|�|j�|�||�|j|dd�|�|�Wd�n1sl0Y|��qdS)Nr�Trkr�r�rrr�test_force_close	s(z'ExhaustiveChannelTests.test_force_close)N)F)rCrDrEr�r�r�r�r�r�rsr�r�r�r�r�r�r�r�rrrrr��s(-1	 
	

r�rL)N)N)5�collectionsrrXr�rr?r��textwraprr#rr�testrZtest.supportr�
import_modulerrrrrYr)r-r*r2rIrJr`rsror~r�ZTestCaser�r�r�r�r�r�r�r�r�r
rrZrfr�r�r	r�rCr1rrrr�<module>st



&
	3$

2?(;]b7Y:&�q@