a
    ze<                    @   sZ  d dl m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	Z	d dl
Z
d dlZd dlmZ d dlmZ edZdd ZdKd	d
ZdLddZejdd Zdd Zi fddZG dd deddZejdd ZG dd deddZG dd deddZddd d!Zd"d# Zd$d% 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"Z%G d0d1 d1e"Z&G d2d3 d3e"Z'G d4d5 d5e"Z(G d6d7 d7e"Z)G d8d9 d9e"Z*G d:d; d;e"Z+G d<d= d=e"Z,G d>d? d?e"Z-G d@dA dAe"Z.G dBdC dCe"Z/G dDdE dEedEdFZ0e1dGG dHdI dIe"Z2e3dJkrVe4  dS )M    )
namedtupleN)dedent)support)script_helperZ_xxsubinterpretersc                 C   s:   t  \}}| dd}td| d| d}|t|fS )N
z
                z-
        import contextlib
        with open(zU, 'w') as spipe:
            with contextlib.redirect_stdout(spipe):
                z	
        )ospipereplacer   open)scriptrwZindentedwrapped r   2/usr/lib/python3.9/test/test__xxsubinterpreters.py_captured_script   s    
r   c                 C   sJ   t |\}}|& t| || | W  d    S 1 s<0    Y  d S N)r   interpreters
run_stringread)interpZrequestsharedr   Zrpiper   r   r   _run_output"   s    r   c                 C   sL   |d u rt j}t }|| }t| sHt |kr<tdtd qd S )Nzinterp is not runningg{Gz?)r   ZSHORT_TIMEOUTtime	monotonicr   
is_runningRuntimeErrorsleep)r   timeoutZ
start_timedeadliner   r   r   _wait_for_interp_to_run)   s    
r    c                 #   s|   t  \} fdd}tj|d}|  t  d V  t|d}|d W d    n1 sf0    Y  |  d S )Nc                      s   t  td d d S )Nz7
            # wait for "signal"
            with open(z5) as rpipe:
                rpipe.read()
            )r   r   r   r   r   r   r   r   run:   s    
z_running.<locals>.runtargetr   done)	r   r   	threadingThreadstartr    r
   writejoin)r   r   r"   tZspiper   r!   r   _running7   s    (r,   c                 K   s   t | || d S r   )_run_interp)idsourcer   r   r   r   
run_interpV   s    r0   c                 C   sF   t |}t }|| kr4t |kr(tt|| nt| || d S r   )r   r   get_mainget_currentr   execr   )r.   r/   r   Z_mainnsmainr   r   r   r-   Z   s    r-   c                       s*   e Zd Zedd Zd fdd	Z  ZS )Interpreterc                 C   s(   t || r|S t |tr | |S td S r   )
isinstancestrNotImplementedError)clsrawr   r   r   from_rawh   s
    

zInterpreter.from_rawNc                    s   t  }||kr6|sd}n|dkr0td||}nX|d urp|sHd}n|dkrXtdt|t jst |}n|r||dkrd}|}nt  }t | ||}|S )Nr4   z)name mismatch (expected "main", got "{}")r   z!name mismatch (unexpected "main"))	r   r1   
ValueErrorformatr6   InterpreterIDcreatesuper__new__)r9   namer.   r4   self	__class__r   r   rA   q   s,    zInterpreter.__new__)NN)__name__
__module____qualname__classmethodr;   rA   __classcell__r   r   rD   r   r5   f   s   
r5   zname idc                   c   s$   z
d V  W n t jy   Y n0 d S r   )r   ChannelClosedErrorr   r   r   r   expect_channel_closed   s
    
rL   c                       s6   e Zd Zd
 fdd	Zdd Zdd Zdd	 Z  ZS )ChannelActionNc                    s&   |sd}|sd}t  | |||}|S )Nbothr4   r@   rA   )r9   actionendr   rC   rD   r   r   rA      s    zChannelAction.__new__c                 O   sb   | j dkr | jdvrJt| jn*| j dv r@| jdvrJt| jn
t| j | jdvr^t| jd S )Nuse)sameoppositesendrecvcloseforce-close)rN   rS   rT   rU   rV   )r4   rS   otherextra)rP   rQ   r<   r   )rC   argskwargsr   r   r   __init__   s    





zChannelAction.__init__c                 C   s2   | j dkr|S | j dkr(|dkr$dS dS | j S d S )NrS   rT   rU   rV   rQ   rC   rQ   r   r   r   resolve_end   s
    

zChannelAction.resolve_endc                 C   st   | j dkr|S | j dkr(|d u r$t|S | j dkrB|d u r>t|S | j dkrp|jdkrZ|S |rl|jdkrl|S td S )NrS   rZ   r[   r4   )r   r   rB   )rC   r   rZ   r[   r   r   r   resolve_interp   s     




zChannelAction.resolve_interp)NN)rF   rG   rH   rA   r^   ra   rb   rJ   r   r   rD   r   rM      s   rM   zaction end interpc                       sB   e Zd Zddd fddZdd Zdd	 Zd
dddZ  ZS )ChannelStater   Fclosedc                   s   t  | ||}|S r   rO   )r9   pendingre   rC   rD   r   r   rA      s    zChannelState.__new__c                 C   s   t | | jd | jdS N   rd   typerf   re   rC   r   r   r   incr   s    zChannelState.incrc                 C   s   t | | jd | jdS rg   ri   rk   r   r   r   decr   s    zChannelState.decrTforcec                C   s2   | j r|r| jdkr| S t| |r&dn| jddS )Nr   Trd   )re   rf   rj   )rC   ro   r   r   r   rX      s    zChannelState.close)r   )rF   rG   rH   rA   rl   rm   rX   rJ   r   r   rD   r   rc      s   rc   zpending closedT
hideclosedc                C   sr   |j r(|dkr"|dkr"|jr"d}q,d}nd}zt| |||}W n& tjyd   |sX|sX | }Y n
0 |rnd|S )NrR   rV   FT.)re   rf   _run_actionr   rK   rX   )cidrP   rQ   staterq   Z
expectfailresultr   r   r   
run_action   s    rv   c                 C   s  |dkr|dkr$t | d | S |dkrx|jsdzt |  W n t jyX   | Y S 0 tdqt |  | S nt|n~|dkri }|dv rd||< t j	| fi | |
 S |d	k rd
di}|dv rd||< t j	| fi | |j
ddS t|d S )NrR   rU      spamrV   zexpected ChannelEmptyErrorrX   rV   rU   TrY   ro   rn   )r   channel_sendrl   rf   channel_recvChannelEmptyError	Exceptionrm   r<   channel_closerX   )rs   rP   rQ   rt   r]   r   r   r   rr      s8    





rr   c               	   C   s>   t  D ]0} | dkrqzt |  W q ty6   Y q0 qd S )Nr   )r   list_alldestroyr   r.   r   r   r   clean_up_interpreters  s    r   c               	   C   s6   t  D ](} zt |  W q t jy.   Y q0 qd S r   )r   channel_list_allchannel_destroyChannelNotFoundErrorrs   r   r   r   clean_up_channels  s
    r   c                   @   s   e Zd Zdd ZdS )TestBasec                 C   s   t   t  d S r   r   r   rk   r   r   r   tearDown)  s    zTestBase.tearDownN)rF   rG   rH   r   r   r   r   r   r   '  s   r   c                   @   s   e Zd Zdd Zdd ZdS )IsShareableTestsc              	   C   sP   g d}|D ]>}|  |  | t| W d    q1 s@0    Y  qd S )N)Nrw   spam
   i)subTest
assertTruer   is_shareable)rC   Z
shareablesobjr   r   r   test_default_shareables3  s    	z(IsShareableTests.test_default_shareablesc                 C   s   G dd d}G dd dt }ddtdttt t d||d	|d
g}|D ]B}| t|  | t	| W d    qJ1 s0    Y  qJd S )Nc                   @   s   e Zd Zdd Zdd ZdS )z3IsShareableTests.test_not_shareable.<locals>.Cheesec                 S   s
   || _ d S r   rB   )rC   rB   r   r   r   r^   D  s    z<IsShareableTests.test_not_shareable.<locals>.Cheese.__init__c                 S   s   | j S r   r   rk   r   r   r   __str__F  s    z;IsShareableTests.test_not_shareable.<locals>.Cheese.__str__N)rF   rG   rH   r^   r   r   r   r   r   CheeseC  s   r   c                   @   s   e Zd ZdZdS )z5IsShareableTests.test_not_shareable.<locals>.SubByteszA subclass of a shareable type.N)rF   rG   rH   __doc__r   r   r   r   SubBytesI  s   r   TF.g      Y@ZWensleydalerw   )
bytesNotImplementedrj   objectr|   r   reprassertFalser   r   )rC   r   r   Znot_shareablesr   r   r   r   test_not_shareableB  s(    z#IsShareableTests.test_not_shareableN)rF   rG   rH   r   r   r   r   r   r   r   1  s   r   c                       s`   e Zd Z fddZ fddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Z  ZS )ShareableTypeTestsc                    s   t    t | _d S r   )r@   setUpr   channel_creaters   rk   rD   r   r   r   e  s    
zShareableTypeTests.setUpc                    s   t | j t   d S r   )r   r   rs   r@   r   rk   rD   r   r   r   i  s    zShareableTypeTests.tearDownc              	   C   sr   |D ]h}|  |J t| j| t| j}| || | t|t| W d    q1 sb0    Y  qd S r   )r   r   ry   rs   rz   assertEqualassertIsrj   )rC   valuesr   gotr   r   r   _assert_valuesm  s    z!ShareableTypeTests._assert_valuesc              	   C   s^   dD ]T}|  |6 t| j| t| j}| || W d    q1 sN0    Y  qd S )Nr   )r   r   ry   rs   rz   r   )rC   r   r   r   r   r   test_singletonsx  s
    z"ShareableTypeTests.test_singletonsc                 C   s   |  dd| jg d S )Nrw   i'  )r   rs   rk   r   r   r   
test_types  s
    zShareableTypeTests.test_typesc                 C   s   |  dd tddD  d S )Nc                 s   s   | ]}|j d dddV  qdS )   littleT)signedN)to_bytes).0ir   r   r   	<genexpr>  s   z0ShareableTypeTests.test_bytes.<locals>.<genexpr>  )r   rangerk   r   r   r   
test_bytes  s    
zShareableTypeTests.test_bytesc                 C   s   |  g d d S )N)zhello worldu   你好世界 )r   rk   r   r   r   	test_strs  s    zShareableTypeTests.test_strsc                 C   s*   |  ttddtjtj d g d S )Nr   r   rh   )r   	itertoolschainr   sysmaxsizerk   r   r   r   test_int  s    zShareableTypeTests.test_intc              
   C   s   t jd t j d dd g}|D ]f}| |H | t t| j| W d    n1 s^0    Y  W d    q 1 s|0    Y  q d S )Nrh   r     )r   r   r   assertRaisesOverflowErrorr   ry   rs   )rC   Zintsr   r   r   r   test_non_shareable_int  s    
z)ShareableTypeTests.test_non_shareable_int)rF   rG   rH   r   r   r   r   r   r   r   r   r   rJ   r   r   rD   r   r   c  s   	r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ListAllTestsc                 C   s"   t  }t  }| ||g d S r   )r   r1   r~   r   )rC   r4   idsr   r   r   test_initial  s    zListAllTests.test_initialc                 C   s6   t  }t  }t  }t  }| ||||g d S r   )r   r1   r?   r~   r   rC   r4   firstsecondr   r   r   r   test_after_creating  s
    z ListAllTests.test_after_creatingc                 C   s>   t  }t  }t  }t | t  }| |||g d S r   )r   r1   r?   r   r~   r   r   r   r   r   test_after_destroying  s    
z"ListAllTests.test_after_destroyingN)rF   rG   rH   r   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd Zdd ZdS )GetCurrentTestsc                 C   s.   t  }t  }| || | |t j d S r   )r   r1   r2   r   assertIsInstancer>   )rC   r4   curr   r   r   	test_main  s    zGetCurrentTests.test_mainc                 C   sR   t  }t  }t|td}t| }t  \}}| || | 	|| d S )Nz
            import _xxsubinterpreters as _interpreters
            cur = _interpreters.get_current()
            print(cur)
            assert isinstance(cur, _interpreters.InterpreterID)
            )
r   r1   r?   r   r   intstripr~   r   ZassertNotEqual)rC   r4   r   outr   _expectedr   r   r   test_subinterpreter  s    z#GetCurrentTests.test_subinterpreterN)rF   rG   rH   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd Zdd ZdS )GetMainTestsc                 C   s0   t  \}t  }| || | |t j d S r   )r   r~   r1   r   r   r>   )rC   r   r4   r   r   r   test_from_main  s    
zGetMainTests.test_from_mainc                 C   s<   t  \}t  }t|td}t| }| || d S )Nz
            import _xxsubinterpreters as _interpreters
            main = _interpreters.get_main()
            print(main)
            assert isinstance(main, _interpreters.InterpreterID)
            )r   r~   r?   r   r   r   r   r   )rC   r   r   r   r4   r   r   r   test_from_subinterpreter  s
    
z%GetMainTests.test_from_subinterpreterN)rF   rG   rH   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S )IsRunningTestsc                 C   s   t  }| t | d S r   )r   r1   r   r   )rC   r4   r   r   r   r     s    zIsRunningTests.test_mainc                 C   sd   t  }| t | t|  | t | W d    n1 sF0    Y  | t | d S r   )r   r?   r   r   r,   r   rC   r   r   r   r   r     s
    
.z"IsRunningTests.test_subinterpreterc                 C   s2   t  }t|td| d}| | d d S )Nz`
            import _xxsubinterpreters as _interpreters
            if _interpreters.is_running(zZ):
                print(True)
            else:
                print(False)
            True)r   r?   r   r   r   r   )rC   r   r   r   r   r   r     s
    z'IsRunningTests.test_from_subinterpreterc                 C   sJ   t  }t | | t t | W d    n1 s<0    Y  d S r   )r   r?   r   r   r   r   r   r   r   r   test_already_destroyed  s    
z%IsRunningTests.test_already_destroyedc                 C   s8   |  t td W d    n1 s*0    Y  d S Ni@B )r   r   r   r   rk   r   r   r   test_does_not_exist  s    z"IsRunningTests.test_does_not_existc                 C   s8   |  t td W d    n1 s*0    Y  d S Nr   )r   r<   r   r   rk   r   r   r   test_bad_id  s    zIsRunningTests.test_bad_idN)	rF   rG   rH   r   r   r   r   r   r   r   r   r   r   r     s   r   c                   @   sD   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S )InterpreterIDTestsc                 C   s"   t jddd}| t|d d S )Nr   Trn   )r   r>   r   r   rC   r.   r   r   r   test_with_int  s    z InterpreterIDTests.test_with_intc                 C   s4   G dd dt }tj| dd}| t|d d S )Nc                   @   s   e Zd Zdd ZdS )z.InterpreterIDTests.test_coerce_id.<locals>.Intc                 S   s   dS Nr   r   rk   r   r   r   	__index__  s    z8InterpreterIDTests.test_coerce_id.<locals>.Int.__index__NrF   rG   rH   r   r   r   r   r   Int  s   r   Trn   r   )r7   r   r>   r   r   )rC   r   r.   r   r   r   test_coerce_id  s    z!InterpreterIDTests.test_coerce_idc                 C   sf   |  ttjt  |  ttjd |  ttjd |  ttjd |  ttjd |  ttjd d S Ng      $@10s   10r   l            )r   	TypeErrorr   r>   r   r<   r   rk   r   r   r   r     s    zInterpreterIDTests.test_bad_idc                 C   sH   t  }| t" t t|d  W d    n1 s:0    Y  d S Nrh   )r   r   r   r   r>   r   r   r   r   r   r   $  s    z&InterpreterIDTests.test_does_not_existc                 C   s"   t jddd}| t|d d S Nr   Trn   r   )r   r>   r   r7   r   r   r   r   test_str)  s    zInterpreterIDTests.test_strc                 C   s"   t jddd}| t|d d S )Nr   Trn   zInterpreterID(10))r   r>   r   r   r   r   r   r   	test_repr-  s    zInterpreterIDTests.test_reprc                 C   s(  t  }t t|}t  }| ||k | ||k | |t|k | t||k | |tt|k | tt||k | |tt|d k | |tt|k | |dd k | |tdk | |dk | ||k | ||k | ||k | ||k d S N皙?r   r   infr   )r   r?   r>   r   r   floatr   r7   rC   id1id2id3r   r   r   test_equality1  s$    z InterpreterIDTests.test_equalityN)
rF   rG   rH   r   r   r   r   r   r   r   r   r   r   r   r     s   r   c                   @   sN   e Zd Zdd Zeddd Zdd Zdd	 Zd
d Z	dd Z
dd ZdS )CreateTestsc                 C   s*   t  }| |t j | |t   d S r   )r   r?   r   r>   assertInr~   r   r   r   r   test_in_mainJ  s    zCreateTests.test_in_mainz*enable this test when working on pystate.cc                 C   sD   t  }tdD ] }t }t| || q| t|d d S )Nd   )setr   r   r?   r   addr   len)rC   seenr   r.   r   r   r   test_unique_idP  s    
zCreateTests.test_unique_idc                    sn   t  d   fdd}t j|d} |  W d    n1 sH0    Y  |  |  t  d S )Nc                      s   t       d S r   )r   r?   acquirereleaser   r.   lockr   r   f]  s    z%CreateTests.test_in_thread.<locals>.fr#   )r&   Lockr'   r(   r*   r   r   r~   rC   r   r+   r   r   r   test_in_threadZ  s    &zCreateTests.test_in_threadc                 C   sJ   t  \}t  }t|td}t| }| tt  |||h d S )Nz
            import _xxsubinterpreters as _interpreters
            id = _interpreters.create()
            print(id)
            assert isinstance(id, _interpreters.InterpreterID)
            )	r   r~   r?   r   r   r   r   r   r   )rC   r4   r   r   r   r   r   r   test_in_subinterpreteri  s
    
z"CreateTests.test_in_subinterpreterc                    s^   t  \}t   d  fdd}tj|d}|  |  | tt  | h d S )Nc                     s   t  td} t|  d S )Nz
                import _xxsubinterpreters as _interpreters
                id = _interpreters.create()
                print(id)
                )r   r   r   r   )r   r   r   r   r   r   z  s    z6CreateTests.test_in_threaded_subinterpreter.<locals>.fr#   )	r   r~   r?   r&   r'   r(   r*   r   r   )rC   r4   r   r+   r   r  r   test_in_threaded_subinterpreterv  s    
	z+CreateTests.test_in_threaded_subinterpreterc                 C   sj   t t }g }tdD ]}t }|| q|D ]}t| q4t }| t t ||hB  d S N   )r   r   r~   r   r?   appendr   r   rC   beforer   r   r.   r   r   r   test_after_destroy_all  s    z"CreateTests.test_after_destroy_allc                 C   s`   t t }t }t }t }t| t| t }| t t |||hB  d S r   )r   r   r~   r?   r   r   )rC   r
  r   r   r   r.   r   r   r   test_after_destroy_some  s    

z#CreateTests.test_after_destroy_someN)rF   rG   rH   r   unittestskipr   r  r  r  r  r  r   r   r   r   r   H  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
dd Zdd ZdS )DestroyTestsc                 C   sf   t  }t  }t  }| |t   t | | |t   | |t   | |t   d S r   )r   r?   r   r~   r   assertNotInr   r   r   r   test_one  s    
zDestroyTests.test_onec                 C   sv   t t }t  }tdD ]}t }|| q| t t ||B  |D ]}t| qN| t t | d S r  )r   r   r~   r   r?   r   r   r   r	  r   r   r   test_all  s    zDestroyTests.test_allc                    sl   t  \ t t   W d    n1 s40    Y   fdd}tj|d}|  |  d S )Nc                      s8    t t  W d    n1 s*0    Y  d S r   r   r   r   r   r   r4   rC   r   r   r     s    z!DestroyTests.test_main.<locals>.fr#   )	r   r~   r   r   r   r&   r'   r(   r*   r  r   r  r   r     s    
(zDestroyTests.test_mainc                 C   sJ   t  }t | | t t | W d    n1 s<0    Y  d S r   )r   r?   r   r   r   r   r   r   r   r     s    
z#DestroyTests.test_already_destroyedc                 C   s8   |  t td W d    n1 s*0    Y  d S r   r  rk   r   r   r   r     s    z DestroyTests.test_does_not_existc                 C   s8   |  t td W d    n1 s*0    Y  d S r   )r   r<   r   r   rk   r   r   r   r     s    zDestroyTests.test_bad_idc                 C   sJ   t  \}t  }td| d}t || | tt  ||h d S )Nzo
            import _xxsubinterpreters as _interpreters
            try:
                _interpreters.destroy(zD)
            except RuntimeError:
                pass
            r   r~   r?   r   r   r   r   )rC   r4   r.   r   r   r   r   test_from_current  s    

zDestroyTests.test_from_currentc                 C   sR   t  \}t  }t  }td| d}t || | tt  ||h d S )NzZ
            import _xxsubinterpreters as _interpreters
            _interpreters.destroy()
            r  )rC   r4   r   r   r   r   r   r   test_from_sibling  s    

zDestroyTests.test_from_siblingc                    s4   t    fdd}tj|d}|  |  d S )Nc                      s   t   d S r   )r   r   r   r   r   r   r     s    z.DestroyTests.test_from_other_thread.<locals>.fr#   )r   r?   r&   r'   r(   r*   r  r   r   r   test_from_other_thread  s
    z#DestroyTests.test_from_other_threadc              	   C   s   t  \}t  }t|| | jt |d| dd | jtd| dd t | W d    n1 sn0    Y  | t | W d    n1 s0    Y  d S )NzInterp z& should be running before destruction.)msgz%Should not be able to destroy interp z while it's still running.)	r   r~   r?   r,   r   r   r   r   r   )rC   r4   r   r   r   r   test_still_running  s    



(zDestroyTests.test_still_runningN)rF   rG   rH   r  r  r   r   r   r   r  r  r  r  r   r   r   r   r    s   
	r  c                       s   e Zd ZedZdZ fddZ fddZdd Zd	d
 Z	dd Z
eeedddd Zdd Zdd Zdd Zdd Zdd Zdd Zejd5d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 d0d1 Z!e"d2d3d4 Z#  Z$S )6RunStringTestszJ
        with open('{}', 'w') as out:
            out.write('{}')
        r   c                    s   t    t | _d | _d S r   )r@   r   r   r?   r.   _fsrk   rD   r   r   r     s    

zRunStringTests.setUpc                    s"   | j d ur| j   t   d S r   )r  rX   r@   r   rk   rD   r   r   r     s    

zRunStringTests.tearDownc                 C   sV   t d\}}|& t| j| | }W d    n1 s<0    Y  | |d d S )Nprint("it worked!", end="")
it worked!)r   r   r   r.   r   r   )rC   r   filer   r   r   r   test_success  s
    &zRunStringTests.test_successc                    sr   t d\ }|B  fdd}tj|d}|  |  | }W d    n1 sX0    Y  |d d S )Nr  c                      s   t j  d S r   )r   r   r.   r   r   rC   r   r   r   $  s    z(RunStringTests.test_in_thread.<locals>.fr#   r  )r   r&   r'   r(   r*   r   r   )rC   r   r   r+   r   r   r"  r   r  !  s    &zRunStringTests.test_in_threadc                 C   s`   t jdd}td\}}|$ t || | }W d    n1 sF0    Y  | |d d S )NF)isolatedz
            import threading
            def f():
                print('it worked!', end='')

            t = threading.Thread(target=f)
            t.start()
            t.join()
            r  )r   r?   r   r   r   r   )rC   Z	subinterpr   r   r   r   r   r   test_create_thread.  s    	&z!RunStringTests.test_create_threadforkztest needs os.fork()c                 C   s   dd l }|dj}|d |  d}td|j d| d}t| j| |	d |
 }| || W d    n1 s0    Y  d S )Nr   zw+r   zspam spam spam spam spamz
                import os
                try:
                    os.fork()
                except RuntimeError:
                    with open('z3', 'w') as out:
                        out.write('z')
                )tempfileNamedTemporaryFiler)   flushr   rB   r   r   r.   seekr   r   )rC   r&  r   r   r   Zcontentr   r   r   	test_fork?  s    


zRunStringTests.test_forkc              	   C   sf   t | jH | t t| jd W d    n1 s:0    Y  W d    n1 sX0    Y  d S )Nprint("spam"))r,   r.   r   r   r   r   rk   r   r   r   test_already_runningU  s    z#RunStringTests.test_already_runningc                 C   sT   d}|t  v r|d7 }q| t t |d W d    n1 sF0    Y  d S )Nr   rh   r+  )r   r~   r   r   r   r   r   r   r   r   Z  s
    
z"RunStringTests.test_does_not_existc                 C   s:   |  t tdd W d    n1 s,0    Y  d S )Nr   r+  )r   r<   r   r   rk   r   r   r   test_error_ida  s    zRunStringTests.test_error_idc                 C   s:   |  t tdd W d    n1 s,0    Y  d S )Nr   r+  )r   r   r   r   rk   r   r   r   r   e  s    zRunStringTests.test_bad_idc                 C   s<   |  t t| jd W d    n1 s.0    Y  d S r   r   r   r   r   r.   rk   r   r   r   test_bad_scripti  s    zRunStringTests.test_bad_scriptc                 C   s<   |  t t| jd W d    n1 s.0    Y  d S )Ns   print("spam")r.  rk   r   r   r   test_bytes_for_scriptm  s    z$RunStringTests.test_bytes_for_scriptNc                 c   sz   |  tj}d V  W d    n1 s(0    Y  |d u r\| t|jdd t| n| t|jd|| d S )N:r   z{}: {})r   r   RunFailedErrorr   r7   	exceptionsplitr=   )rC   exctyper  Zcaughtr   r   r   assert_run_failedq  s    $
z RunStringTests.assert_run_failedc                 C   s<   |  t t| jd W d    n1 s.0    Y  d S )Nzprint("spam")r6  SyntaxErrorr   r   r.   rk   r   r   r   test_invalid_syntax|  s    z"RunStringTests.test_invalid_syntaxc                 C   s>   |  td t| jd W d    n1 s00    Y  d S )Nr   zraise Exception("spam"))r6  r|   r   r   r.   rk   r   r   r   test_failure  s    zRunStringTests.test_failurec                 C   s>   |  td t| jd W d    n1 s00    Y  d S )N42zraise SystemExit(42))r6  
SystemExitr   r   r.   rk   r   r   r   test_SystemExit  s    zRunStringTests.test_SystemExitc                 C   s~   |  t" t| jtd W d    n1 s20    Y  |  td" t| jtd W d    n1 sp0    Y  d S )NzG
                import sys
                sys.exit()
                r:  zI
                import sys
                sys.exit(42)
                )r6  r;  r   r   r.   r   rk   r   r   r   test_sys_exit  s    0zRunStringTests.test_sys_exitc                 C   s   t  \}}ddd d}td| d}t| j|| t|d}t|}W d    n1 sb0    Y  | 	|d d | 	|d	 d
 | 	|d d | 
|d  d S )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   *   r?  r   ru   )   r@  )r   r   r   r   r   r.   r
   pickleloadr   ZassertIsNonerC   r   r   r   r   channsr   r   r   test_with_shared  s    
(zRunStringTests.test_with_sharedc                 C   s   t | jtd ddi}td}t | j|| t \}}td| d}t | j| t|d}t|}W d    n1 s0    Y  | 	|d d d	 | 	|d
 d d | 	|d d d S )Nzj
            spam = 'eggs'
            ns1 = dict(vars())
            del ns1['__builtins__']
            r   r>  zL
            ns2 = dict(vars())
            del ns2['__builtins__']
        zr
            ns = dict(vars())
            del ns['__builtins__']
            import pickle
            with open(rA  rB  ns1r?  ns2)
r   r   r.   r   r   r   r
   rE  rF  r   )rC   r   r   r   r   rH  rI  r   r   r   test_shared_overwrites  s    
(z%RunStringTests.test_shared_overwritesc                 C   s|   t  \}}ddi}td| d}t| j|| t|d}t|}W d    n1 s^0    Y  | 	|d d d S )NrF   s   not __main__z
            spam = 42

            ns = dict(vars())
            del ns['__builtins__']
            import pickle
            with open(rA  rB  )
r   r   r   r   r   r.   r
   rE  rF  r   rG  r   r   r   #test_shared_overwrites_default_vars  s    
	(z2RunStringTests.test_shared_overwrites_default_varsc                 C   s   t  \}}t| jtd| d t|d}t|}W d    n1 sP0    Y  t  \}}t| jtd| d t|d}t|}W d    n1 s0    Y  | 	d| | 
d| | 	d| | 	d| d S )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
            rB  z
            eggs = False

            ns = dict(vars())
            del ns['__builtins__']
            import pickle
            with open(rA  r   r?  )r   r   r   r   r.   r   r
   rE  rF  r   r  )rC   r   r   rH  rK  rL  r   r   r   test_main_reused  s     
(	(zRunStringTests.test_main_reusedc              
   C   s   t  \}}td| d}t| j| t|d}t|}W d    n1 sT0    Y  |	d |	d | 
|di d d d dd d S )	Nz
            spam = 42

            ns = dict(vars())
            ns['__builtins__'] = str(ns['__builtins__'])
            import pickle
            with open(rA  rB  __builtins__
__loader____main__rC  )rF   __annotations__r   __package____spec__r   )r   r   r   r   r   r.   r
   rE  rF  popr   )rC   r   r   r   rH  rI  r   r   r    test_execution_namespace_is_main  s"    
	(

z/RunStringTests.test_execution_namespace_is_mainzblocking foreverc              	   C   s   t d}t P}t|d|}t|}| }W d    n1 sH0    Y  W d    n1 sf0    Y  | |d d S )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()
        r   r   )r   r   Ztemp_dirr   Zmake_scriptZspawn_pythonwaitr   )rC   r   dirnamefilenameprocretcoder   r   r   test_still_running_at_exit   s    
Dz)RunStringTests.test_still_running_at_exit)N)%rF   rG   rH   r   ZSCRIPTZFILENAMEr   r   r!  r  r$  r  Z
skipUnlesshasattrr   r*  r,  r   r-  r   r/  r0  
contextlibcontextmanagerr6  r8  r9  r<  r=  rJ  rM  rN  rO  rW  r  r]  rJ   r   r   rD   r   r    s8   

!r  c                   @   sT   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
dd ZdS )ChannelIDTestsc                 C   s0   t jddd}| t|d | |jd d S )Nr   Trn   rN   )r   _channel_idr   r   rQ   rC   rs   r   r   r   test_default_kwargs>  s    z"ChannelIDTests.test_default_kwargsc                 C   s   t jdddd}| |jd t jddddd}| |jd t jdddd}| |jd t jddddd	}| |jd t jddddd}| |jd
 d S )Nr   TrU   ro   rU   FrU   rV   ro   rV   ro   rV   )rV   rU   ro   rN   )r   rb  r   rQ   rc  r   r   r   test_with_kwargsD  s    zChannelIDTests.test_with_kwargsc                 C   s4   G dd dt }tj| dd}| t|d d S )Nc                   @   s   e Zd Zdd ZdS )z*ChannelIDTests.test_coerce_id.<locals>.Intc                 S   s   dS r   r   rk   r   r   r   r   V  s    z4ChannelIDTests.test_coerce_id.<locals>.Int.__index__Nr   r   r   r   r   r   U  s   r   Trn   r   )r7   r   rb  r   r   )rC   r   rs   r   r   r   r   T  s    zChannelIDTests.test_coerce_idc                 C   sf   |  ttjt  |  ttjd |  ttjd |  ttjd |  ttjd |  ttjd d S r   )r   r   r   rb  r   r<   r   rk   r   r   r   r   \  s    zChannelIDTests.test_bad_idc                 C   s>   |  t  tjdddd W d    n1 s00    Y  d S )Nr   FrU   rV   )r   r<   r   rb  rk   r   r   r   test_bad_kwargsd  s    zChannelIDTests.test_bad_kwargsc                 C   sJ   t  }| t j" t t|d  W d    n1 s<0    Y  d S r   )r   r   r   r   rb  r   rc  r   r   r   r   h  s    z"ChannelIDTests.test_does_not_existc                 C   s"   t jddd}| t|d d S r   )r   rb  r   r7   rc  r   r   r   r   m  s    zChannelIDTests.test_strc                 C   s   t jddd}| t|d t jdddd}| t|d t jdddd}| t|d t jddddd	}| t|d d S )
Nr   Trn   zChannelID(10)re  zChannelID(10, send=True)rg  zChannelID(10, recv=True)rf  )r   rb  r   r   rc  r   r   r   r   q  s    zChannelIDTests.test_reprc                 C   s(  t  }t t|}t  }| ||k | ||k | |t|k | t||k | |tt|k | tt||k | |tt|d k | |tt|k | |dd k | |tdk | |dk | ||k | ||k | ||k | ||k d S r   )r   r   rb  r   r   r   r   r7   )rC   cid1cid2Zcid3r   r   r   r   ~  s$    zChannelIDTests.test_equalityN)rF   rG   rH   rd  rh  r   r   rj  r   r   r   r   r   r   r   r   ra  <  s   ra  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
d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d#d$ Zd%d& Zd'd( Zed)d*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 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ChannelTestsc                 C   s   t  }| |t j d S r   )r   r   r   Z	ChannelIDrc  r   r   r   test_create_cid  s    zChannelTests.test_create_cidc                 C   sr   t  }t  }t  }t  }t  }| |t|d  | |t|d  | t|t| |||h d S r   )r   r   r   r   r   r   )rC   r
  r   r   r   Zafterr   r   r   test_sequential_ids  s    z ChannelTests.test_sequential_idsc                 C   s\   t  }t|td}t| }t  }t|td}t| }| |t|d  d S )Nz
            import _xxsubinterpreters as _interpreters
            cid = _interpreters.channel_create()
            print(cid)
            rh   )r   r?   r   r   r   r   r   )rC   r   r   rk  r   rl  r   r   r   test_ids_global  s    zChannelTests.test_ids_globalc                 C   s@   t  }t j|dd}t j|dd}| |g  | |g  dS )z=Test listing interpreters for a channel with no associations.TrU   FN)r   r   channel_list_interpretersr   )rC   rs   send_interpsrecv_interpsr   r   r   #test_channel_list_interpreters_none  s
    z0ChannelTests.test_channel_list_interpreters_nonec                 C   s   t  }t  }t |d t j|dd}t j|dd}| ||g | |g  t  }t|td| d t j|dd}t j|dd}| ||g | ||g dS )z(Test basic listing channel interpreters.rU   Trq  Fe
            import _xxsubinterpreters as _interpreters
            obj = _interpreters.channel_recv(r  N)	r   r1   r   ry   rr  r   r?   r   r   )rC   interp0rs   rs  rt  interp1r   r   r   $test_channel_list_interpreters_basic  s    z1ChannelTests.test_channel_list_interpreters_basicc                 C   s   t  }t  }t  }t  }t  }t |d t|td| d t|td| d t|td| d t j|dd}t j|dd}| t	|||h | t	|||h d	S )
z?Test listing interpreters for a channel with many associations.rU   _
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_send(z, "send")
            rv  r  Trq  FN)
r   r1   r?   r   ry   r   r   rr  r   r   )rC   rw  rx  interp2interp3rs   rs  rt  r   r   r   'test_channel_list_interpreters_multiple  s&    z4ChannelTests.test_channel_list_interpreters_multiplec                 C   s   t  }t  }t  }t |d t|td| d t j|dd}t j|dd}| ||g | ||g t 	| t j|dd}t j|dd}| ||g | |g  dS )z?Test listing channel interpreters with a destroyed interpreter.rU   rv  r  Trq  FN)
r   r1   r?   r   ry   r   r   rr  r   r   rC   rw  rx  rs   rs  rt  r   r   r   (test_channel_list_interpreters_destroyed  s     
z5ChannelTests.test_channel_list_interpreters_destroyedc                 C   s>  t  }t  }t  }t  }t |d t|td| d t |d t|td| d t j|dd}t j|dd}| t	|d | t	|d t j
|dd t j|dd}t j|dd}| t	|d	 | t	|d t|td
| d t j|dd}t j|dd}| t	|d	 | ||g dS )z:Test listing channel interpreters with a released channel.datarv  r  Trq  Frh   r   r   b
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_release(N)r   r1   r?   r   ry   r   r   rr  r   r   channel_release)rC   rw  rx  r{  rs   rs  rt  r   r   r   'test_channel_list_interpreters_released	  s8    z4ChannelTests.test_channel_list_interpreters_releasedc                 C   s   t  }t  }t  }t |d t j|dd}t j|dd}| t|d | t|d t j|dd | 	t j
 t j|dd W d   n1 s0    Y  | 	t j
 t j|dd W d   n1 s0    Y  dS )	z8Test listing channel interpreters with a closed channel.rU   Trq  Frh   r   rn   N)r   r1   r?   r   ry   rr  r   r   r}   r   rK   r~  r   r   r   %test_channel_list_interpreters_closed4  s    ,z2ChannelTests.test_channel_list_interpreters_closedc                 C   sX  t  }t  }t  }t |d t j|dd}t j|dd}| t|d | t|d t j|dd | 	t j
 t j|dd W d   n1 s0    Y  t j|dd}| t|d t|td| d	 | 	t j
 t j|dd W d   n1 s0    Y  | 	t j
 t j|dd W d   n1 sJ0    Y  dS )
zCTest listing channel interpreters with a channel's send end closed.rU   Trq  Frh   r   N`
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_close(, force=True)
            )r   r1   r?   r   ry   rr  r   r   r}   r   rK   r   r   r~  r   r   r   .test_channel_list_interpreters_closed_send_endJ  s(    ,.z;ChannelTests.test_channel_list_interpreters_closed_send_endc                 C   s>   t  }d}t || t |}| || | || d S Nrw   )r   r   ry   rz   r   ZassertIsNot)rC   rs   Zorigr   r   r   r   test_send_recv_mainn  s    
z ChannelTests.test_send_recv_mainc                 C   s   t  }t|td}d S )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
            )r   r?   r   r   )rC   r   r   r   r   r   test_send_recv_same_interpreterw  s    z,ChannelTests.test_send_recv_same_interpreterc                 C   s@   t  }t  }t|td| d}t |}| |d d S )Nrz  , b'spam')
            rw   )r   r   r?   r   r   rz   r   )rC   rs   r   r   r   r   r   r   %test_send_recv_different_interpreters  s    
z2ChannelTests.test_send_recv_different_interpretersc                    sV   t    fdd}tj|d}|  t  d |  t  }| |d d S )Nc                     sD   zt  } W q4W q  t jy0   td Y q 0 q t  |  d S )Nr   )r   rz   r{   r   r   ry   )r   r   r   r   r     s    
z8ChannelTests.test_send_recv_different_threads.<locals>.fr#   rw   )	r   r   r&   r'   r(   ry   r*   rz   r   rC   r   r+   r   r   r   r    test_send_recv_different_threads  s    
z-ChannelTests.test_send_recv_different_threadsc                    sf   t   t  d  fdd}tj|d}|  t  d |  t  }| 	|d d S )Nc                      s    t td  d  dd S )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')
                )r   r   r   rs   r   r   r   r   r     s
    
zIChannelTests.test_send_recv_different_interpreters_and_threads.<locals>.fr#   rw      eggs)
r   r   r?   r&   r'   r(   ry   r*   rz   r   r  r   r  r   1test_send_recv_different_interpreters_and_threads  s    
z>ChannelTests.test_send_recv_different_interpreters_and_threadsc                 C   s<   |  tj tdd W d    n1 s.0    Y  d S )Nr   rw   )r   r   r   ry   rk   r   r   r   test_send_not_found  s    z ChannelTests.test_send_not_foundc                 C   s:   |  tj td W d    n1 s,0    Y  d S r   )r   r   r   rz   rk   r   r   r   test_recv_not_found  s    z ChannelTests.test_recv_not_foundc                 C   sB   t  }| t j t | W d    n1 s40    Y  d S r   )r   r   r   r{   rz   rc  r   r   r   test_recv_empty  s    zChannelTests.test_recv_emptyc           	      C   s   t  }t }t||}t|d  t|d t|d t|d t||}t||}t|}t||}t||}| || | |d  | |d | |d | |d | || d S )Nrh   rw   r  )r   r   r   rz   ry   r   r   )	rC   defaultrs   Zobj1obj2Zobj3Zobj4Zobj5Zobj6r   r   r   test_recv_default  s$    
zChannelTests.test_recv_defaultc                 C   sR   t  }t  }t|tdt|jd}t |}| |d | |	 d d S )Nz
            import _xxsubinterpreters as _interpreters
            print(cid.end)
            _interpreters.channel_send(cid, b'spam')
            r   rw   rU   )
r   r   r?   r   r   dictrU   rz   r   r   rC   rs   r   r   r   r   r   r   test_run_string_arg_unresolved  s    


z+ChannelTests.test_run_string_arg_unresolvedz(disabled until high-level channels existc                 C   s`   t  }t j|dd}t  }t|tdt|jd}t |}| 	|d | 	|
 d d S )NT)_resolvez
            import _xxsubinterpreters as _interpreters
            print(chan.id.end)
            _interpreters.channel_send(chan.id, b'spam')
            )rH  rw   rU   )r   r   rb  r?   r   r   r  rU   rz   r   r   r  r   r   r   test_run_string_arg_resolved  s    


z)ChannelTests.test_run_string_arg_resolvedc                 C   s   t  }t |d t | t | | t j t |d W d    n1 sV0    Y  | t j t | W d    n1 s0    Y  d S Nrw   r  r   r   ry   rz   r}   r   rK   rc  r   r   r   test_close_single_user  s    

*z#ChannelTests.test_close_single_userc                 C   s  t  }t  }t  }t |td| d t |td| d t | | t j(}t |td| d W d    n1 s0    Y  | dt	|j
 | t j(}t |td| d W d    n1 s0    Y  | dt	|j
 d S )Nrz  r  z_
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_recv(r  z,
                _interpreters.channel_send(z, b'spam')
                rK   )r   r   r?   r   r   r}   r   r2  r   r7   r3  )rC   rs   r   r   cmr   r   r   test_close_multiple_users  s(    



*
*z&ChannelTests.test_close_multiple_usersc                 C   sb   t  }t |d t | t | | t j t | W d    n1 sT0    Y  d S r  r  rc  r   r   r   test_close_multiple_times(  s    

z&ChannelTests.test_close_multiple_timesc              
   C   s   g d}|D ]\}}|  ||f t }t|d t| tj|||d | tj t|d W d    n1 s0    Y  | tj t| W d    n1 s0    Y  W d    q1 s0    Y  qd S )N))FFTF)FT)TTrw   ri  r  )r   r   r   ry   rz   r}   r   rK   )rC   ZtestsrU   rV   rs   r   r   r   test_close_empty1  s    
*zChannelTests.test_close_emptyc                 C   sp   t  }t |d t |d | t j t | W d    n1 sL0    Y  t | t |d d S )Nrw   r>  r  r   r   ry   r   ZChannelNotEmptyErrorr}   rz   rc  r   r   r   %test_close_defaults_with_unused_itemsD  s    (
z2ChannelTests.test_close_defaults_with_unused_itemsc                 C   s   t  }t |d t |d | t j t j|dd W d    n1 sP0    Y  t | t |d t | t | t j|dd d S )Nrw   r>  TrV   r  r  rc  r   r   r   *test_close_recv_with_unused_items_unforcedN  s    ,


z7ChannelTests.test_close_recv_with_unused_items_unforcedc                 C   s   t  }t |d t |d t j|dd | t j t |d W d    n1 s\0    Y  t | t | | t j t | W d    n1 s0    Y  d S )Nrw   r>  Trq  r  r   r   ry   r}   r   rK   rz   rc  r   r   r   *test_close_send_with_unused_items_unforced[  s    *

z7ChannelTests.test_close_send_with_unused_items_unforcedc                 C   s   t  }t |d t |d | t j  t j|ddd W d    n1 sR0    Y  t | t |d t | t | t j|dd d S )Nrw   r>  Trx   r  r  r  rc  r   r   r   *test_close_both_with_unused_items_unforcedh  s    .


z7ChannelTests.test_close_both_with_unused_items_unforcedc                 C   s   t  }t |d t |d t j|ddd | t j t |d W d    n1 s^0    Y  | t j t | W d    n1 s0    Y  d S )Nrw   r>  Trg  r  r  rc  r   r   r   (test_close_recv_with_unused_items_forcedu  s    *z5ChannelTests.test_close_recv_with_unused_items_forcedc                 C   s   t  }t |d t |d t j|ddd | t j t |d W d    n1 s^0    Y  | t j t | W d    n1 s0    Y  d S )Nrw   r>  Tre  r  r  rc  r   r   r   (test_close_send_with_unused_items_forced  s    *z5ChannelTests.test_close_send_with_unused_items_forcedc                 C   s   t  }t |d t |d t j|dddd | t j t |d W d    n1 s`0    Y  | t j t | W d    n1 s0    Y  d S )Nrw   r>  Trf  r  r  rc  r   r   r   (test_close_both_with_unused_items_forced  s    *z5ChannelTests.test_close_both_with_unused_items_forcedc                 C   s   t  }t | | t j t |d W d    n1 s@0    Y  | t j t | W d    n1 sv0    Y  d S r  )r   r   r}   r   rK   ry   rz   rc  r   r   r   test_close_never_used  s    
*z"ChannelTests.test_close_never_usedc                 C   s   t  }t |d t  }t |td| d | t j t | W d    n1 s`0    Y  | t j t 	| W d    n1 s0    Y  d S )Nrw   r  r  )
r   r   ry   r?   r   r   r   rK   rz   r}   rC   rs   r   r   r   r   !test_close_by_unassociated_interp  s    
(z.ChannelTests.test_close_by_unassociated_interpc                 C   s   t  }t |d t |d t |d t | t j|dd | t j t |d W d    n1 sr0    Y  | t j t | W d    n1 s0    Y  d S )Nrw   Trn   r  r  rc  r   r   r   -test_close_used_multiple_times_by_single_user  s    
*z:ChannelTests.test_close_used_multiple_times_by_single_userc                 C   s   t  }| t j t jddd W d    n1 s80    Y  t | | t j t j|dd W d    n1 s|0    Y  d S )Nr   Trq  )r   r   r   r   rr  r}   rK   rc  r   r   r   .test_channel_list_interpreters_invalid_channel  s    ,
z;ChannelTests.test_channel_list_interpreters_invalid_channelc                 C   s@   t  }| t t | W d    n1 s20    Y  d S r   )r   r   r   r   rr  rc  r   r   r   +test_channel_list_interpreters_invalid_args  s    z8ChannelTests.test_channel_list_interpreters_invalid_argsN)*rF   rG   rH   rn  ro  rp  ru  ry  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   rm    sL   	+$	
	
	rm  c                   @   s`   e Zd 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dd Zdd ZdS )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
    c                 C   s   t  }t |d t | t j|ddd | t j t |d W d    n1 s\0    Y  | t j t | W d    n1 s0    Y  d S Nrw   Tri  r  r   r   ry   rz   r  r   rK   rc  r   r   r   test_single_user  s    
*z$ChannelReleaseTests.test_single_userc                 C   sx   t  }t  }t  }t |td| d t|td| d| d}t |td| d | | d d S )	Nrz  r  rv  z,)
            _interpreters.channel_release(z+)
            print(repr(obj))
            z+
            _interpreters.channel_release(r  zb'spam')r   r   r?   r   r   r   r   r   )rC   rs   r   r   r   r   r   r   test_multiple_users  s    

z'ChannelReleaseTests.test_multiple_usersc                 C   s   t  }t |d t | t | | t j t |d W d    n1 sV0    Y  | t j t | W d    n1 s0    Y  d S r  r  rc  r   r   r   test_no_kwargs  s    

*z"ChannelReleaseTests.test_no_kwargsc                 C   sn   t  }t |d t | t j|ddd | t j  t j|ddd W d    n1 s`0    Y  d S )Nrw   Tri  r  rc  r   r   r   test_multiple_times   s    
z'ChannelReleaseTests.test_multiple_timesc                 C   sj   t  }t |d t |d t j|ddd | t j t | W d    n1 s\0    Y  d S )Nrw   r>  Tri  )r   r   ry   r  r   rK   rz   rc  r   r   r   test_with_unused_items)  s    z*ChannelReleaseTests.test_with_unused_itemsc                 C   s   t  }t | | t j t |d W d    n1 s@0    Y  | t j t | W d    n1 sv0    Y  d S r  )r   r   r  r   rK   ry   rz   rc  r   r   r   test_never_used2  s    
*z#ChannelReleaseTests.test_never_usedc                 C   s   t  }t |d t  }t |td| d t |}t | | t j	 t |d W d    n1 sv0    Y  | 
|d d S )Nrw   r  r  r  )r   r   ry   r?   r   r   rz   r  r   rK   r   )rC   rs   r   r   r   r   r   test_by_unassociated_interp;  s    


*z/ChannelReleaseTests.test_by_unassociated_interpc              	   C   sh   t  }t  }t |td| d| d | t j t | W d    n1 sZ0    Y  d S )Nze
            import _xxsubinterpreters as _interpreters
            obj = _interpreters.channel_send(z5, b'spam')
            _interpreters.channel_release(r  )r   r   r?   r   r   r   rK   rz   r  r   r   r   test_close_if_unassociatedJ  s    
z.ChannelReleaseTests.test_close_if_unassociatedc                 C   sR   t  }t |d  t | t |d t j|dd t |}| |d d S )Nrw   Trq  )r   r   ry   rz   r  r   )rC   rs   r   r   r   r   test_partiallyW  s    

z"ChannelReleaseTests.test_partiallyc                 C   s   t  }t |d t |d t |d t | t j|ddd | t j t |d W d    n1 st0    Y  | t j t | W d    n1 s0    Y  d S r  r  rc  r   r   r   'test_used_multiple_times_by_single_userb  s    
*z;ChannelReleaseTests.test_used_multiple_times_by_single_userN)rF   rG   rH   r   r  r  r  r  r  r  r  r  r  r  r   r   r   r   r    s   '			r  c                       sz   e Zd ZdZ fddZedd Zedd Zdd	 ZdddZ	dd Z
dd Zdd Zdd Zdd Zdd Z  ZS )ChannelCloseFixtureFc                    sx   | j ri }n4t|}t|}t|}|j||j||j|i}|sHd}t | |||||}t |_t |_	||_
|S )NrS   )QUICKr5   r;   rB   r@   rA   r   _preppedrc   _state_known)r9   rQ   r   rZ   r[   creatorZknownrC   rD   r   r   rA   w  s     


zChannelCloseFixture.__new__c                 C   s   | j S r   r  rk   r   r   r   rt     s    zChannelCloseFixture.statec                 C   s>   z| j W S  ty8   | | j}| || _ | j  Y S 0 d S r   )_cidAttributeError_get_interpreterr  _new_channel)rC   r  r   r   r   rs     s    zChannelCloseFixture.cidc                 C   s   |  |}| | |S r   )r  _prep_interpreterr   r   r   r   get_interpreter  s    

z#ChannelCloseFixture.get_interpreterNc                 C   s2   |d u r| j }|dkr&| jjdkr&dS t| jjS )NrV   rU   F)rQ   rt   re   boolr`   r   r   r   expect_closed_error  s
    z'ChannelCloseFixture.expect_closed_errorc                 C   s   |  | d S r   )r  r   r   r   r   prep_interpreter  s    z$ChannelCloseFixture.prep_interpreterc                 C   s
   || _ d S r   r  )rC   rP   ru   r   r   r   record_action  s    z!ChannelCloseFixture.record_actionc                 C   s   t   t  d S r   r   rk   r   r   r   clean_up  s    zChannelCloseFixture.clean_upc                 C   s@   |j dkrt S t }t|jd| d t|| _| jS )Nr4   a  
                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
                )rB   r   r   r0   r.   rz   r  )rC   r  Zchr   r   r   r    s    

z ChannelCloseFixture._new_channelc                 C   sj   |dv r| j S |dkr| jS |dkr*| jS |}z| j| }W n$ ty`   t| }| j|< Y n0 |S d S )N)rS   r   rZ   r[   )r   rZ   r[   r  KeyErrorr5   )rC   r   rB   r   r   r   r    s    z$ChannelCloseFixture._get_interpreterc                 C   sF   |j | jv rd S | j|j  |jdkr,d S t|j d| j d d S )Nr4   a  
            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   rB   r0   rs   r   r   r   r   r    s    
z%ChannelCloseFixture._prep_interpreter)N)rF   rG   rH   r  rA   propertyrt   rs   r  r  r  r  r  r  r  r  rJ   r   r   rD   r   r  p  s   


r  zend interp other extra creatorz%these tests take several hours to runc                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZddddZ	dd Z
dd Zd"ddZdd Zd#ddZdd Zdd Zd d! ZdS )$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
    c                 c   s  g V  |  ddD ]"}|V  |  D ]}|| V  q$q|  ddD ]"}|V  |  D ]}|| V  qTqB| ddD ]z}|V  |  ddD ]*}|| }|V  |  D ]}|| V  qq|  ddD ]*}|| }|V  |  D ]}|| V  qqqr| ddD ]}|V  |  ddD ].}|| }|V  |  D ]}|| V  q*q|  ddD ].}|| }|V  |  D ]}|| V  qfqLqd S )NrS   rZ   r[   )_iter_close_action_sets_iter_post_close_action_sets_iter_use_action_sets)rC   ZcloseactionsZpostactionsZ
useactionsactionsr   r   r   iter_action_sets  sB    z'ExhaustiveChannelTests.iter_action_setsc                 c   s  ||f}t dd|gV  t dd|gV  t dd|t dd|gV  t dd|gV  t dd|gV  t dd|t dd|gV  |D ]<}|D ]2}|D ](}t dd|t dd|t dd|gV  qqq|D ]P}|D ]F}|D ]<}|D ]2}t dd|t dd|t dd|t dd|gV  qqqqd S )NrR   rV   rU   rM   )rC   rx  r{  interpsr|  Zinterp4r   r   r   r  9  s>    














z,ExhaustiveChannelTests._iter_use_action_setsc              	   c   s   d}||f}dD ]4}|rdnd}|D ]}|D ]}t |||gV  q,q$qdD ]<}	dD ]2}
|D ](}|D ]}t |	d|t |
d|gV  qbqZqRqJd S )Nrx   r  rY   rX   rW   rV   rU   r  )rC   rx  r{  Zendsr  ro   opr   rQ   ZrecvopZsendoprV   rU   r   r   r   r  j  s    


z.ExhaustiveChannelTests._iter_close_action_setsc                 c   s.   dD ]$}t dd|gV  t dd|gV  qd S )N)rS   r[   rZ   rR   rV   rU   r  r   r   r   r   r  }  s
    

z3ExhaustiveChannelTests._iter_post_close_action_setsc                 C   s   |D ]}|  || qd S r   )rv   )rC   fixr  rP   r   r   r   run_actions  s    z"ExhaustiveChannelTests.run_actionsTrp   c                C   s   | |j}||j|j|j}|| |jdkrZt|j	|j
||j|d}||| nt }t|jd|j	 dt|j
 dt| dt|j d| d| d| d ttt|d	tt|d
}||| d S )Nr4   rp   zB
                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   )rf   re   )ra   rQ   rb   r   rZ   r[   r  rB   rv   rs   rP   rt   r  r   r   r0   r.   r   rc   r   
from_bytesrz   r  )rC   r  rP   rq   rQ   r   ru   r  r   r   r   rv     sB    

	
z!ExhaustiveChannelTests.run_actionc              	   c   sB   g d}|D ]0\}}}dD ] }dD ]}t |||||V  q"qqd S )N))r4   r   r[   )r   r4   r[   )rx  r{  r[   )rx  r{  r4   )rS   rZ   r  ri  )r  )rC   r   r   rZ   r[   r  rQ   r   r   r   iter_fixtures  s
    z$ExhaustiveChannelTests.iter_fixturesc                C   st   |rdnd}t ||jd}| s4| j||dd n<| tj  | j||dd W d    n1 sf0    Y  d S )NrY   rX   rS   Frp   )rM   rQ   r  rv   r   r   rK   )rC   r  ro   r  rX   r   r   r   _close  s    zExhaustiveChannelTests._closeNc                 C   s.  |d u s|j dkr| tj t|j W d    n1 s@0    Y  | tj t|jd W d    n1 sz0    Y  | tj t|j W d    n1 s0    Y  | tj  tj|jdd W d    n1 s0    Y  n0t|j	d t|j	d t|j	d t|j	d d S )	Nr4   rw   Trn   zz
                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)
                )
rB   r   r   rK   rz   rs   ry   r}   r0   r.   )rC   r  r   r   r   r   _assert_closed_in_interp  s    *,*0z/ExhaustiveChannelTests._assert_closed_in_interpc                 C   s|   |  |jj t|jjD ]}t|j q| | dD ]&}|	|}|j
dkrTq:| || q:|	d}| || d S )N)rS   rZ   r4   Zfresh)r   rt   re   r   rf   r   rz   rs   r  r  rB   )rC   r  r   r   r   r   r   _assert_closed  s    



z%ExhaustiveChannelTests._assert_closedFc              	   c   s   d}|   D ]}t  |  D ]}|d7 }|dkr:  d S |rl|d d dkrTt  t||dt| n2|d d dkrtddd td	dd tj  |||fV  q|rtd
 qt  d S )Nr   rh   r      z({} actions) r   r_   .z---)r  printr  r=   r   r   stdoutr(  )rC   verboser   r  r  r   r   r   _iter_close_tests  s$    
z(ExhaustiveChannelTests._iter_close_testsc                 C   s   dt _|  D ]
\}}}qd S )NT)r  r  r  rC   r   r  r  r   r   r   _skim_close_tests	  s    z(ExhaustiveChannelTests._skim_close_testsc              	   C   s   |   D ]v\}}}| d|||@ ||j | || | j|dd | | W d    n1 sl0    Y  |  qd S )N	{} {}  {}Frn   	r  r   r=   r  r   r  r  r  r  r  r   r   r   
test_close	  s    (z!ExhaustiveChannelTests.test_closec              	   C   s   |   D ]v\}}}| d|||@ ||j | || | j|dd | | W d    n1 sl0    Y  |  qd S )Nr  Trn   r  r  r   r   r   test_force_close	  s    (z'ExhaustiveChannelTests.test_force_close)N)F)rF   rG   rH   r   r  r  r  r  r  rv   r  r  r  r  r  r  r  r  r   r   r   r   r    s   (-1	 	

r  rR  )N)N)5collectionsr   r_  r   r   rE  r   textwrapr   r&   r   r  testr   Ztest.supportr   import_moduler   r   r   r    r`  r,   r0   r-   r5   rL   rM   rc   rv   rr   r   r   ZTestCaser   r   r   r   r   r   r   r   r   r  r  ra  rm  r  r  r  r  rF   r4   r   r   r   r   <module>   st   



&
	3$

2?(;]b  7Y    : &q  @
