o
    GXh;                    @   sj  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 d dlmZ edZdd	 ZdLd
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/d0 d0e#Z&G d1d2 d2e#Z'G d3d4 d4e#Z(G d5d6 d6e#Z)G d7d8 d8e#Z*G d9d: d:e#Z+G d;d< d<e#Z,G d=d> d>e#Z-G d?d@ d@e#Z.G dAdB dBe#Z/G dCdD dDe#Z0G dEdF dFedFdGZ1e2dHG dIdJ dJe#Z3e4dKkr3e5  dS dS )M    )
namedtupleN)dedent)support)import_helper)script_helperZ_xxsubinterpretersc                 C   s>   t  \}}| dd}td| d| d}|t|ddfS )N
z
                z-
        import contextlib
        with open(zg, 'w', encoding="utf-8") as spipe:
            with contextlib.redirect_stdout(spipe):
                z	
        utf-8encoding)ospipereplacer   open)scriptrwZindentedwrapped r   F/opt/python-3.10.19/usr/lib/python3.10/test/test__xxsubinterpreters.py_captured_script   s   
r   c                 C   sJ   t |\}}| t| || | W  d    S 1 sw   Y  d S N)r   interpreters
run_stringread)interpZrequestsharedr   Zrpiper   r   r   _run_output#   s
   $r   c                 C   sX   |d u rt j}t }|| }t| s*t |krtdtd t| rd S 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d}|d W d    n1 s6w   Y  |  d S )Nc                      s   t  td d d S )Nz7
            # wait for "signal"
            with open(zG, encoding="utf-8") as rpipe:
                rpipe.read()
            )r   r   r   r   r   r   r   r   run;   s   
z_running.<locals>.runtargetr   r   r	   done)	r   r   	threadingThreadstartr$   r   writejoin)r   r   r&   tZspiper   r%   r   _running8   s   r0   c                 K   s   t | || d S r   )_run_interp)idsourcer   r   r   r   
run_interpW   s   r4   c                 C   sH   t |}t }|| krt |krtt|| d S t| || d S r   )r   r   get_mainget_currentr    execr   )r2   r3   r   Z_mainnsmainr   r   r   r1   [   s   r1   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r   )
isinstancestrNotImplementedError)clsrawr   r   r   from_rawi   s
   

zInterpreter.from_rawNc                    s   t  }||kr|sd}n|dkrtd||}n,|d ur8|s$d}n|dkr,tdt|t js7t |}n|r>|dkrCd}|}nt  }t | ||}|S )Nr8   z)name mismatch (expected "main", got "{}")r   z!name mismatch (unexpected "main"))	r   r5   
ValueErrorformatr:   InterpreterIDcreatesuper__new__)r=   namer2   r8   self	__class__r   r   rE   r   s.   
zInterpreter.__new__NN)__name__
__module____qualname__classmethodr?   rE   __classcell__r   r   rH   r   r9   g   s    
r9   zname idc                   c   s&    zd V  W d S  t jy   Y d S w r   )r   ChannelClosedErrorr   r   r   r   expect_channel_closed   s   rQ   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bothr8   rD   rE   )r=   actionendr   rG   rH   r   r   rE      s   zChannelAction.__new__c                 O   sb   | j dkr| jdvrt| jn| j dv r | jdvrt| jnt| j | jdvr/t| jd S )Nuse)sameoppositesendrecvcloseforce-close)rS   rX   rY   rZ   r[   )r8   rX   otherextra)rU   rV   r@   r   )rG   argskwargsr   r   r   __init__   s   








zChannelAction.__init__c                 C   s.   | j dkr|S | j dkr|dkrdS dS | j S )NrX   rY   rZ   r[   rV   rG   rV   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r!|d u rt|S | j dkr8|jdkr-|S |r6|jdkr6|S td S )NrX   r_   r`   r8   )r   r    rF   )rG   r   r_   r`   r   r   r   resolve_interp   s"   




zChannelAction.resolve_interprJ   )rK   rL   rM   rE   rc   rf   rg   rO   r   r   rH   r   rR      s
    rR   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   rT   )r=   pendingrj   rG   rH   r   r   rE      s   zChannelState.__new__c                 C   s   t | | jd | jdS N   ri   typerk   rj   rG   r   r   r   incr      zChannelState.incrc                 C   s   t | | jd | jdS rl   rn   rp   r   r   r   decr   rr   zChannelState.decrTforcec                C   s8   | j r|r
| jdkr| S t| |rdddS | jddS )Nr   Tri   )rj   rk   ro   )rG   ru   r   r   r   r]      s    zChannelState.close)r   )rK   rL   rM   rE   rq   rs   r]   rO   r   r   rH   r   rh      s
    rh   zpending closedT
hideclosedc                C   st   |j r|dkr|dkr|jrd}nd}nd}z	t| |||}W n tjy3   |s,|s, | }Y |S w |r8d|S )NrW   r[   FT.)rj   rk   _run_actionr   rP   r]   )cidrU   rV   staterw   Z
expectfailresultr   r   r   
run_action   s    
r|   c                 C   s   |dkr=|dkrt | d | S |dkr9|js0z
t |  W td t jy/   | Y S w t |  | S t||dkrXi }|dv rKd||< t j	| fi | |
 S |d	krwd
di}|dv rhd||< t j	| fi | |j
ddS t|)NrW   rZ      spamr[   zexpected ChannelEmptyErrorr]   r[   rZ   Tr^   ru   rt   )r   channel_sendrq   rk   channel_recvChannelEmptyError	Exceptionrs   r@   channel_closer]   )ry   rU   rV   rz   rb   r   r   r   rx      s:   
rx   c               	   C   s<   t  D ]} | dkrqzt |  W q ty   Y qw d S )Nr   )r   list_alldestroyr    r2   r   r   r   clean_up_interpreters  s   r   c               	   C   s4   t  D ]} zt |  W q t jy   Y qw d S r   )r   channel_list_allchannel_destroyChannelNotFoundErrorry   r   r   r   clean_up_channels   s   r   c                   @      e Zd Zdd ZdS )TestBasec                 C      t   t  d S r   r   r   rp   r   r   r   tearDown*     
zTestBase.tearDownN)rK   rL   rM   r   r   r   r   r   r   (  s    r   c                   @      e Zd Zdd Zdd ZdS )IsShareableTestsc              	   C   sP   g d}|D ]}|  | | t| W d    n1 s w   Y  qd S )N)Nr}   spam
   i)subTest
assertTruer   is_shareable)rG   Z
shareablesobjr   r   r   test_default_shareables4  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 ]!}| t| | t	| W d    n1 sAw   Y  q%d S )Nc                   @   r   )z3IsShareableTests.test_not_shareable.<locals>.Cheesec                 S   s
   || _ d S r   rF   )rG   rF   r   r   r   rc   E     
z<IsShareableTests.test_not_shareable.<locals>.Cheese.__init__c                 S      | j S r   r   rp   r   r   r   __str__G  s   z;IsShareableTests.test_not_shareable.<locals>.Cheese.__str__N)rK   rL   rM   rc   r   r   r   r   r   CheeseD  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)rK   rL   rM   __doc__r   r   r   r   SubBytesJ  s    r   TF.g      Y@ZWensleydaler}   )
bytesNotImplementedro   objectr   r   reprassertFalser   r   )rG   r   r   Znot_shareablesr   r   r   r   test_not_shareableC  s.   z#IsShareableTests.test_not_shareableN)rK   rL   rM   r   r   r   r   r   r   r   2  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                       t    t | _d S r   )rD   setUpr   channel_createry   rp   rH   r   r   r   f     
zShareableTypeTests.setUpc                    s   t | j t   d S r   )r   r   ry   rD   r   rp   rH   r   r   r   j  s   zShareableTypeTests.tearDownc              	   C   sr   |D ]4}|  |% t| j| t| j}| || | t|t| W d    n1 s1w   Y  qd S r   )r   r   r   ry   r   assertEqualassertIsro   )rG   valuesr   gotr   r   r   _assert_valuesn  s   z!ShareableTypeTests._assert_valuesc              	   C   s^   dD ]*}|  | t| j| t| j}| || W d    n1 s'w   Y  qd S )Nr   )r   r   r   ry   r   r   )rG   r   r   r   r   r   test_singletonsy  s   z"ShareableTypeTests.test_singletonsc                 C   s   |  dd| jg d S )Nr}   i'  )r   ry   rp   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   rangerp   r   r   r   
test_bytes  s   
zShareableTypeTests.test_bytesc                 C   s   |  g d d S )N)zhello worldu   你好世界 )r   rp   r   r   r   	test_strs     zShareableTypeTests.test_strsc                 C   s*   |  ttddtjtj d g d S )Nr   r   rm   )r   	itertoolschainr   sysmaxsizerp   r   r   r   test_int  s   
zShareableTypeTests.test_intc              
   C   s   t jd t j d dd g}|D ]3}| |$ | t t| j| W d    n1 s/w   Y  W d    n1 s>w   Y  qd S )Nrm   r     )r   r   r   assertRaisesOverflowErrorr   r   ry   )rG   Zintsr   r   r   r   test_non_shareable_int  s   
z)ShareableTypeTests.test_non_shareable_int)rK   rL   rM   r   r   r   r   r   r   r   r   r   rO   r   r   rH   r   r   d  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   r5   r   r   )rG   r8   idsr   r   r   test_initial  s   zListAllTests.test_initialc                 C   s6   t  }t  }t  }t  }| ||||g d S r   )r   r5   rC   r   r   rG   r8   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   r5   rC   r   r   r   r   r   r   r   test_after_destroying  s   
z"ListAllTests.test_after_destroyingN)rK   rL   rM   r   r   r   r   r   r   r   r     s    r   c                   @   r   )GetCurrentTestsc                 C   s.   t  }t  }| || | |t j d S r   )r   r5   r6   r   assertIsInstancerB   )rG   r8   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   r5   rC   r   r   intstripr   r   ZassertNotEqual)rG   r8   r   outr   _expectedr   r   r   test_subinterpreter  s   z#GetCurrentTests.test_subinterpreterN)rK   rL   rM   r   r   r   r   r   r   r         r   c                   @   r   )GetMainTestsc                 C   s0   t  \}t  }| || | |t j d S r   )r   r   r5   r   r   rB   )rG   r   r8   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   rC   r   r   r   r   r   )rG   r   r   r   r8   r   r   r   test_from_subinterpreter  s
   
z%GetMainTests.test_from_subinterpreterN)rK   rL   rM   r   r   r   r   r   r   r     r   r   c                   @   sF   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S )IsRunningTestsc                 C   s   t  }| t | d S r   )r   r5   r   r   )rG   r8   r   r   r   r     s   zIsRunningTests.test_mainzFails on FreeBSDc                 C   sd   t  }| t | t| | t | W d    n1 s#w   Y  | t | d S r   )r   rC   r   r   r0   r   rG   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   rC   r   r   r   r   )rG   r   r   r   r   r   r     s
   z'IsRunningTests.test_from_subinterpreterc                 C   sL   t  }t | | t t | W d    d S 1 sw   Y  d S r   )r   rC   r   r   r    r   r   r   r   r   test_already_destroyed   
   
"z%IsRunningTests.test_already_destroyedc                 C   :   |  t td W d    d S 1 sw   Y  d S Ni@B )r   r    r   r   rp   r   r   r   test_does_not_exist     "z"IsRunningTests.test_does_not_existc                 C   r   Nr   )r   r@   r   r   rp   r   r   r   test_bad_id
  r   zIsRunningTests.test_bad_idN)rK   rL   rM   r   unittestskipr   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   Trt   )r   rB   r   r   rG   r2   r   r   r   test_with_int  s   z InterpreterIDTests.test_with_intc                 C   4   G dd dt }tj| dd}| t|d d S )Nc                   @   r   )z.InterpreterIDTests.test_coerce_id.<locals>.Intc                 S      dS Nr   r   rp   r   r   r   	__index__     z8InterpreterIDTests.test_coerce_id.<locals>.Int.__index__NrK   rL   rM   r   r   r   r   r   Int      r   Trt   r   )r;   r   rB   r   r   )rG   r   r2   r   r   r   test_coerce_id     z!InterpreterIDTests.test_coerce_idc                 C   f   |  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   rB   r   r@   r   rp   r   r   r   r        zInterpreterIDTests.test_bad_idc                 C   sJ   t  }| t t t|d  W d    d S 1 sw   Y  d S Nrm   )r   r   r   r    rB   r   r   r   r   r   r   &  s   "z&InterpreterIDTests.test_does_not_existc                 C   "   t jddd}| t|d d S Nr   Trt   r   )r   rB   r   r;   r   r   r   r   test_str+     zInterpreterIDTests.test_strc                 C   r  )Nr   Trt   zInterpreterID(10))r   rB   r   r   r   r   r   r   	test_repr/  r  zInterpreterIDTests.test_reprc                 C   (  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   rC   rB   r   r   floatr   r;   rG   id1id2id3r   r   r   test_equality3  $   z InterpreterIDTests.test_equalityN)
rK   rL   rM   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   rC   r   rB   assertInr   r   r   r   r   test_in_mainL  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   rC   r   addr   len)rG   seenr   r2   r   r   r   test_unique_idR  s   
zCreateTests.test_unique_idc                    sn   t  d   fdd}t j|d} |  W d    n1 s$w   Y  |  |  t  d S )Nc                      s   t       d S r   )r   rC   acquirereleaser   r2   lockr   r   f_  s   z%CreateTests.test_in_thread.<locals>.fr'   )r*   Lockr+   r,   r.   r  r   r   rG   r  r/   r   r  r   test_in_thread\  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   rC   r   r   r   r   r   r  )rG   r8   r  r   r  r   r   r   test_in_subinterpreterk  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  |  s   z6CreateTests.test_in_threaded_subinterpreter.<locals>.fr'   )	r   r   rC   r*   r+   r,   r.   r   r  )rG   r8   r  r/   r   r$  r   test_in_threaded_subinterpreterx  s   
	z+CreateTests.test_in_threaded_subinterpreterc                 C   sj   t t }g }tdD ]}t }|| q|D ]}t| qt }| t t ||hB  d S N   )r  r   r   r   rC   appendr   r   rG   beforer   r   r2   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   rC   r   r   )rG   r*  r  r  r  r2   r   r   r   test_after_destroy_some  s   

 z#CreateTests.test_after_destroy_someN)rK   rL   rM   r  r   r   r  r"  r#  r%  r+  r,  r   r   r   r   r  J  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   rC   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| q'| t t | d S r&  )r  r   r   r   rC   r  r   r   r)  r   r   r   test_all  s   zDestroyTests.test_allc                    sl   t  \ t t   W d    n1 sw   Y   fdd}tj|d}|  |  d S )Nc                      s:    t t  W d    d S 1 sw   Y  d S r   r   r    r   r   r   r8   rG   r   r   r    r   z!DestroyTests.test_main.<locals>.fr'   )	r   r   r   r    r   r*   r+   r,   r.   r!  r   r2  r   r     s   
zDestroyTests.test_mainc                 C   sL   t  }t | | t t | W d    d S 1 sw   Y  d S r   )r   rC   r   r   r    r   r   r   r   r     r   z#DestroyTests.test_already_destroyedc                 C   r   r   r1  rp   r   r   r   r     r   z DestroyTests.test_does_not_existc                 C   r   r   )r   r@   r   r   rp   r   r   r   r     r   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   rC   r   r   r   r  )rG   r8   r2   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()
            r3  )rG   r8   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       z.DestroyTests.test_from_other_thread.<locals>.fr'   )r   rC   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 s7w   Y  | t | W d    d S 1 sOw   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   rC   r0   r   r   r   r    r   )rG   r8   r   r   r   r   test_still_running  s   



"zDestroyTests.test_still_runningN)rK   rL   rM   r/  r0  r   r   r   r   r4  r6  r8  r:  r   r   r   r   r-    s    
	r-  c                       s   e Z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d1d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/d0 Z  Z S )2RunStringTestsc                    r   r   )rD   r   r   rC   r2   rp   rH   r   r   r     r   zRunStringTests.setUpc                 C   sV   t d\}}| t| j| | }W d    n1 sw   Y  | |d d S )Nprint("it worked!", end="")
it worked!)r   r   r   r2   r   r   )rG   r   filer   r   r   r   test_success  s   
zRunStringTests.test_successc                    sr   t d\ }|!  fdd}tj|d}|  |  | }W d    n1 s,w   Y  |d d S )Nr<  c                      s   t j  d S r   )r   r   r2   r   r   rG   r   r   r    r   z(RunStringTests.test_in_thread.<locals>.fr'   r=  )r   r*   r+   r,   r.   r   r   )rG   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 s#w   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   rC   r   r   r   r   )rG   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 }|jddd6}|d |  d}td|j d| d	}t| j| |	d |
 }| || W d    d S 1 sDw   Y  d S )
Nr   zw+r   r	   r   zspam spam spam spam spamz
                import os
                try:
                    os.fork()
                except RuntimeError:
                    with open('zE', 'w', encoding='utf-8') as out:
                        out.write('z')
                )tempfileNamedTemporaryFiler-   flushr   rF   r   r   r2   seekr   r   )rG   rD  r>  r   r   Zcontentr   r   r   	test_fork5  s   


"zRunStringTests.test_forkc              	   C   sx   t | j- | t t| jd W d    n1 sw   Y  W d    d S W d    d S 1 s5w   Y  d S )Nprint("spam"))r0   r2   r   r    r   r   rp   r   r   r   test_already_runningK  s   "z#RunStringTests.test_already_runningc                 C   s`   d}|t  v r|d7 }|t  v s| t t |d W d    d S 1 s)w   Y  d S )Nr   rm   rI  )r   r   r   r    r   r   r   r   r   r   P  s   "z"RunStringTests.test_does_not_existc                 C   <   |  t tdd W d    d S 1 sw   Y  d S )Nr   rI  )r   r@   r   r   rp   r   r   r   test_error_idW     "zRunStringTests.test_error_idc                 C   rK  )Nr   rI  )r   r   r   r   rp   r   r   r   r   [  rM  zRunStringTests.test_bad_idc                 C   >   |  t t| jd W d    d S 1 sw   Y  d S r   r   r   r   r   r2   rp   r   r   r   test_bad_script_     "zRunStringTests.test_bad_scriptc                 C   rN  )Ns   print("spam")rO  rp   r   r   r   test_bytes_for_scriptc  rQ  z$RunStringTests.test_bytes_for_scriptNc                 c   s~    |  tj}d V  W d    n1 sw   Y  |d u r0| t|jdd t| d S | t|jd|| d S )N:r   z{}: {})r   r   RunFailedErrorr   r;   	exceptionsplitrA   )rG   exctyper9  Zcaughtr   r   r   assert_run_failedg  s   
z RunStringTests.assert_run_failedc                 C   rN  )Nzprint("spam")rX  SyntaxErrorr   r   r2   rp   r   r   r   test_invalid_syntaxr  s   "z"RunStringTests.test_invalid_syntaxc                 C   @   |  td t| jd W d    d S 1 sw   Y  d S )Nr   zraise Exception("spam"))rX  r   r   r   r2   rp   r   r   r   test_failurew     "zRunStringTests.test_failurec                 C   r[  )N42zraise SystemExit(42))rX  
SystemExitr   r   r2   rp   r   r   r   test_SystemExit{  r]  zRunStringTests.test_SystemExitc                 C   s   |  t t| jtd W d    n1 sw   Y  |  td t| jtd W d    d S 1 s9w   Y  d S )NzG
                import sys
                sys.exit()
                r^  zI
                import sys
                sys.exit(42)
                )rX  r_  r   r   r2   r   rp   r   r   r   test_sys_exit  s   "zRunStringTests.test_sys_exitc                 C   s   t  \}}ddd d}td| d}t| j|| t|d}t|}W d    n1 s1w   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   *   rc  r   r{   )   rd  )r   r   r   r   r   r2   r   pickleloadr   ZassertIsNonerG   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 sCw   Y  | 	|d d d	 | 	|d
 d d | 	|d d d S )Nzj
            spam = 'eggs'
            ns1 = dict(vars())
            del ns1['__builtins__']
            r   rb  zL
            ns2 = dict(vars())
            del ns2['__builtins__']
        zr
            ns = dict(vars())
            del ns['__builtins__']
            import pickle
            with open(re  rf  ns1rc  ns2)
r   r   r2   r   r   r   r   ri  rj  r   )rG   r   r   r   r   rl  rm  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/w   Y  | 	|d d d S )NrK   s   not __main__z
            spam = 42

            ns = dict(vars())
            del ns['__builtins__']
            import pickle
            with open(re  rf  )
r   r   r   r   r   r2   r   ri  rj  r   rk  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 s(w   Y  t  \}}t| jtd| d t|d}t|}W d    n1 sUw   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
            rf  z
            eggs = False

            ns = dict(vars())
            del ns['__builtins__']
            import pickle
            with open(re  r   rc  )r   r   r   r   r2   r   r   ri  rj  r  r.  )rG   r   r   rl  ro  rp  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 s*w   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(re  rf  __builtins__
__loader____main__rg  )rK   __annotations__r   __package____spec__r   )r   r   r   r   r   r2   r   ri  rj  popr   )rG   r   r   r   rl  rm  r   r   r    test_execution_namespace_is_main  s$   
	

z/RunStringTests.test_execution_namespace_is_mainzblocking foreverc              	   C   s   t d}t (}t|d|}t|}| }W d    n1 s$w   Y  W d    n1 s3w   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   )rG   r   dirnamefilenameprocretcoder   r   r   test_still_running_at_exit  s   

z)RunStringTests.test_still_running_at_exitr   )!rK   rL   rM   r   r?  r"  rB  r   Z
skipUnlesshasattrr   rH  rJ  r   rL  r   rP  rR  
contextlibcontextmanagerrX  rZ  r\  r`  ra  rn  rq  rr  rs  r{  r   r  rO   r   r   rH   r   r;  	  s4    

!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   Trt   rS   )r   _channel_idr   r   rV   rG   ry   r   r   r   test_default_kwargs4  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rZ   ru   rZ   FrZ   r[   ru   r[   ru   r[   )r[   rZ   ru   rS   )r   r  r   rV   r  r   r   r   test_with_kwargs:  s   zChannelIDTests.test_with_kwargsc                 C   r   )Nc                   @   r   )z*ChannelIDTests.test_coerce_id.<locals>.Intc                 S   r   r   r   rp   r   r   r   r   L  r   z4ChannelIDTests.test_coerce_id.<locals>.Int.__index__Nr   r   r   r   r   r   K  r   r   Trt   r   )r;   r   r  r   r   )rG   r   ry   r   r   r   r   J  r   zChannelIDTests.test_coerce_idc                 C   r   r   )r   r   r   r  r   r@   r   rp   r   r   r   r   R  r   zChannelIDTests.test_bad_idc                 C   s@   |  t tjdddd W d    d S 1 sw   Y  d S )Nr   FrZ   r[   )r   r@   r   r  rp   r   r   r   test_bad_kwargsZ  s   "zChannelIDTests.test_bad_kwargsc                 C   sL   t  }| t j t t|d  W d    d S 1 sw   Y  d S r  )r   r   r   r   r  r   r  r   r   r   r   ^  s   "z"ChannelIDTests.test_does_not_existc                 C   r  r  )r   r  r   r;   r  r   r   r   r  c  r  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   Trt   zChannelID(10)r  zChannelID(10, send=True)r  zChannelID(10, recv=True)r  )r   r  r   r   r  r   r   r   r  g  s   zChannelIDTests.test_reprc                 C   r  r  )r   r   r  r   r   r  r   r;   )rG   cid1cid2Zcid3r   r   r   r  t  r  zChannelIDTests.test_equalityN)rK   rL   rM   r  r  r   r   r  r   r  r  r  r   r   r   r   r  2  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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	ChannelIDr  r   r   r   test_create_cid     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  )rG   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)
            rm   )r   rC   r   r   r   r   r   )rG   r  r   r  r  r  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rZ   FN)r   r   channel_list_interpretersr   )rG   ry   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.rZ   Tr  Fe
            import _xxsubinterpreters as _interpreters
            obj = _interpreters.channel_recv(r5  N)	r   r5   r   r   r  r   rC   r   r   )rG   interp0ry   r  r  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.rZ   _
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_send(z, "send")
            r  r5  Tr  FN)
r   r5   rC   r   r   r   r   r  r   r  )rG   r  r  interp2interp3ry   r  r  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.rZ   r  r5  Tr  FN)
r   r5   rC   r   r   r   r   r  r   r   rG   r  r  ry   r  r  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.datar  r5  Tr  Frm   r   r   b
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_release(N)r   r5   rC   r   r   r   r   r  r   r  channel_release)rG   r  r  r  ry   r  r  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 sOw   Y  | 	t j
 t j|dd W d   dS 1 smw   Y  dS )	z8Test listing channel interpreters with a closed channel.rZ   Tr  Frm   r   rt   N)r   r5   rC   r   r   r  r   r  r   r   rP   r  r   r   r   %test_channel_list_interpreters_closed*  s   "z2ChannelTests.test_channel_list_interpreters_closedc                 C   sV  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 sOw   Y  t j|dd}| t|d t|td| d	 | 	t j
 t j|dd W d   n1 sw   Y  | 	t j
 t j|dd W d   dS 1 sw   Y  dS )
zCTest listing channel interpreters with a channel's send end closed.rZ   Tr  Frm   r   N`
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_close(, force=True)
            )r   r5   rC   r   r   r  r   r  r   r   rP   r   r   r  r   r   r   .test_channel_list_interpreters_closed_send_end@  s.   "z;ChannelTests.test_channel_list_interpreters_closed_send_endc                 C   s>   t  }d}t || t |}| || | || d S Nr}   )r   r   r   r   r   ZassertIsNot)rG   ry   Zorigr   r   r   r   test_send_recv_maind  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   rC   r   r   )rG   r  r   r   r   r   test_send_recv_same_interpreterm  r  z,ChannelTests.test_send_recv_same_interpreterc                 C   s@   t  }t  }t|td| d}t |}| |d d S )Nr  , b'spam')
            r}   )r   r   rC   r   r   r   r   )rG   ry   r  r   r   r   r   r   %test_send_recv_different_interpretersy  s   
z2ChannelTests.test_send_recv_different_interpretersc                    sV   t    fdd}tj|d}|  t  d |  t  }| |d d S )Nc                     sB   	 zt  } W n t jy   td Y nw qt  |  d S )NTr	  )r   r   r   r   r!   r   )r   r   r   r   r    s   
z8ChannelTests.test_send_recv_different_threads.<locals>.fr'   r}   )	r   r   r*   r+   r,   r   r.   r   r   rG   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   ry   r  r   r   r   r    s
   
zIChannelTests.test_send_recv_different_interpreters_and_threads.<locals>.fr'   r}      eggs)
r   r   rC   r*   r+   r,   r   r.   r   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    d S 1 sw   Y  d S )Nr   r}   )r   r   r   r   rp   r   r   r   test_send_not_found  s   "z ChannelTests.test_send_not_foundc                 C   s<   |  tj td W d    d S 1 sw   Y  d S r   )r   r   r   r   rp   r   r   r   test_recv_not_found  s   "z ChannelTests.test_recv_not_foundc                 C   sD   t  }| t j t | W d    d S 1 sw   Y  d S r   )r   r   r   r   r   r  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 )Nrm   r}   r  )r   r   r   r   r   r   r   )	rG   defaultry   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   r}   rZ   )
r   r   rC   r   r   dictrZ   r   r   r   rG   ry   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)Z_resolvez
            import _xxsubinterpreters as _interpreters
            print(chan.id.end)
            _interpreters.channel_send(chan.id, b'spam')
            )rl  r}   rZ   )r   r   r  rC   r   r   r  rZ   r   r   r   r  r   r   r   test_run_string_arg_resolved  s   


z)ChannelTests.test_run_string_arg_resolvedc                 C      t  }t |d t | t | | t j t |d W d    n1 s+w   Y  | t j t | W d    d S 1 sGw   Y  d S Nr}   r  r   r   r   r   r   r   rP   r  r   r   r   test_close_single_user     

"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 sFw   Y  | dt	|j
 | t j}t |td| d W d    n1 sqw   Y  | dt	|j
 d S )Nr  r  z_
            import _xxsubinterpreters as _interpreters
            _interpreters.channel_recv(r5  z,
                _interpreters.channel_send(z, b'spam')
                rP   )r   r   rC   r   r   r   r   rT  r  r;   rU  )rG   ry   r  r  cmr   r   r   test_close_multiple_users  s,   




z&ChannelTests.test_close_multiple_usersc                 C   sd   t  }t |d t | t | | t j t | W d    d S 1 s+w   Y  d S r  r  r  r   r   r   test_close_multiple_times  s   

"z&ChannelTests.test_close_multiple_timesc              
   C   s   g d}|D ]i\}}|  ||fV t }t|d t| tj|||d | tj t|d W d    n1 s@w   Y  | tj t| W d    n1 s[w   Y  W d    n1 sjw   Y  qd S )N))FFTF)FT)TTr}   r  r  )r   r   r   r   r   r   r   rP   )rG   ZtestsrZ   r[   ry   r   r   r   test_close_empty'  s"   
zChannelTests.test_close_emptyc                 C   sp   t  }t |d t |d | t j t | W d    n1 s&w   Y  t | t |d d S )Nr}   rb  r  r   r   r   r   ZChannelNotEmptyErrorr   r   r  r   r   r   %test_close_defaults_with_unused_items:  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 s(w   Y  t | t |d t | t | t j|dd d S )Nr}   rb  Tr[   r  r  r  r   r   r   *test_close_recv_with_unused_items_unforcedD  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.w   Y  t | t | | t j t | W d    d S 1 sTw   Y  d S )Nr}   rb  Tr  r  r   r   r   r   r   rP   r   r  r   r   r   *test_close_send_with_unused_items_unforcedQ  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 s)w   Y  t | t |d t | t | t j|dd d S )Nr}   rb  Tr~   r  r  r  r  r   r   r   *test_close_both_with_unused_items_unforced^  s   


z7ChannelTests.test_close_both_with_unused_items_unforcedc                 C      t  }t |d t |d t j|ddd | t j t |d W d    n1 s/w   Y  | t j t | W d    d S 1 sKw   Y  d S )Nr}   rb  Tr  r  r  r  r   r   r   (test_close_recv_with_unused_items_forcedk     "z5ChannelTests.test_close_recv_with_unused_items_forcedc                 C   r  )Nr}   rb  Tr  r  r  r  r   r   r   (test_close_send_with_unused_items_forcedv  r  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 s0w   Y  | t j t | W d    d S 1 sLw   Y  d S )Nr}   rb  Tr  r  r  r  r   r   r   (test_close_both_with_unused_items_forced  s   "z5ChannelTests.test_close_both_with_unused_items_forcedc                 C      t  }t | | t j t |d W d    n1 s w   Y  | t j t | W d    d S 1 s<w   Y  d S r  )r   r   r   r   rP   r   r   r  r   r   r   test_close_never_used     
"z"ChannelTests.test_close_never_usedc                 C   s   t  }t |d t  }t |td| d | t j t | W d    n1 s0w   Y  | t j t 	| W d    d S 1 sLw   Y  d S )Nr}   r  r  )
r   r   r   rC   r   r   r   rP   r   r   rG   ry   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 s9w   Y  | t j t | W d    d S 1 sUw   Y  d S )Nr}   Trt   r  r  r  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 sw   Y  t | | t j t j|dd W d    d S 1 s?w   Y  d S )Nr   Tr  )r   r   r   r   r  r   rP   r  r   r   r   .test_channel_list_interpreters_invalid_channel  s   
"z;ChannelTests.test_channel_list_interpreters_invalid_channelc                 C   sB   t  }| t t | W d    d S 1 sw   Y  d S r   )r   r   r   r   r  r  r   r   r   +test_channel_list_interpreters_invalid_args  s   "z8ChannelTests.test_channel_list_interpreters_invalid_argsN)*rK   rL   rM   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  r  r   r   r   r   r    sN    	+$	
	
	r  c                   @   sd   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.w   Y  | t j t | W d    d S 1 sJw   Y  d S Nr}   Tr  r  r   r   r   r   r  r   rP   r  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 )	Nr  r  r  z,)
            _interpreters.channel_release(z+)
            print(repr(obj))
            z+
            _interpreters.channel_release(r5  zb'spam')r   r   rC   r   r   r   r   r   )rG   ry   r  r  r   r   r   r   test_multiple_users  s   

z'ChannelReleaseTests.test_multiple_usersc                 C   r  r  r  r  r   r   r   test_no_kwargs  r  z"ChannelReleaseTests.test_no_kwargsc                 C   sp   t  }t |d t | t j|ddd | t j t j|ddd W d    d S 1 s1w   Y  d S )Nr}   Tr  r  r  r   r   r   test_multiple_times  s   
"z'ChannelReleaseTests.test_multiple_timesc                 C   sl   t  }t |d t |d t j|ddd | t j t | W d    d S 1 s/w   Y  d S )Nr}   rb  Tr  )r   r   r   r  r   rP   r   r  r   r   r   test_with_unused_items  s   "z*ChannelReleaseTests.test_with_unused_itemsc                 C   r  r  )r   r   r  r   rP   r   r   r  r   r   r   test_never_used(  r  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 s;w   Y  | 
|d d S )Nr}   r  r5  r  )r   r   r   rC   r   r   r   r  r   rP   r   )rG   ry   r   r   r   r   r   test_by_unassociated_interp1  s   


z/ChannelReleaseTests.test_by_unassociated_interpc              	   C   sj   t  }t  }t |td| d| d | t j t | W d    d S 1 s.w   Y  d S )Nze
            import _xxsubinterpreters as _interpreters
            obj = _interpreters.channel_send(z5, b'spam')
            _interpreters.channel_release(r5  )r   r   rC   r   r   r   rP   r   r  r   r   r   test_close_if_unassociated@  s   
"z.ChannelReleaseTests.test_close_if_unassociatedc                 C   sR   t  }t |d  t | t |d t j|dd t |}| |d d S )Nr}   Tr  )r   r   r   r   r  r   )rG   ry   r   r   r   r   test_partiallyM  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 s:w   Y  | t j t | W d    d S 1 sVw   Y  d S r  r  r  r   r   r   'test_used_multiple_times_by_single_userX  s   
"z;ChannelReleaseTests.test_used_multiple_times_by_single_userN)rK   rL   rM   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 }nt|}t|}t|}|j||j||j|i}|s$d}t | |||||}t |_t |_	||_
|S )NrX   )QUICKr9   r?   rF   rD   rE   r  _preppedrh   _state_known)r=   rV   r   r_   r`   creatorZknownrG   rH   r   r   rE   m  s    


zChannelCloseFixture.__new__c                 C   r   r   r  rp   r   r   r   rz     s   zChannelCloseFixture.statec                 C   s:   z| j W S  ty   | | j}| || _ | j  Y S w r   )_cidAttributeError_get_interpreterr  _new_channel)rG   r  r   r   r   ry     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 )Nr[   rZ   F)rV   rz   rj   boolre   r   r   r   expect_closed_error  s
   z'ChannelCloseFixture.expect_closed_errorc                 C   s   |  | d S r   )r  r   r   r   r   prep_interpreter  r7  z$ChannelCloseFixture.prep_interpreterc                 C   s
   || _ d S r   r  )rG   rU   r{   r   r   r   record_action  r   z!ChannelCloseFixture.record_actionc                 C   r   r   r   rp   r   r   r   clean_up  r   zChannelCloseFixture.clean_upc                 C   s@   |j dkr	t S t }t|jd| d t|| _| jS )Nr8   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
                )rF   r   r   r4   r2   r   r  )rG   r  Zchr   r   r   r    s   

z ChannelCloseFixture._new_channelc                 C   sf   |dv r| j S |dkr| jS |dkr| jS |}z| j| }W |S  ty2   t| }| j|< Y |S w )N)rX   r   r_   r`   )r   r_   r`   r  KeyErrorr9   )rG   r   rF   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 )Nr8   a  
            import _xxsubinterpreters as interpreters
            import test.test__xxsubinterpreters as helpers
            ChannelState = helpers.ChannelState
            try:
                cid
            except NameError:
                cid = interpreters._channel_id(r5  )r2   r  r  rF   r4   ry   r   r   r   r   r    s   
z%ChannelCloseFixture._prep_interpreterr   )rK   rL   rM   r  rE   propertyrz   ry   r  r  r  r  r  r  r  r  rO   r   r   rH   r   r  f  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  q+q"| ddD ]=}|V  |  ddD ]}|| }|V  |  D ]}|| V  qRqE|  ddD ]}|| }|V  |  D ]}|| V  qnqaq:| ddD ]=}|V  |  ddD ]}|| }|V  |  D ]}|| V  qq|  ddD ]}|| }|V  |  D ]}|| V  qqq~d S )NrX   r_   r`   )_iter_close_action_sets_iter_post_close_action_sets_iter_use_action_sets)rG   ZcloseactionsZpostactionsZ
useactionsactionsr   r   r   iter_action_sets  sV   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 ]}|D ]}t dd|t dd|t dd|gV  qIqEqA|D ](}|D ]#}|D ]}|D ]}t dd|t dd|t dd|t dd|gV  qnqjqfqbd S )NrW   r[   rZ   rR   )rG   r  r  interpsr  Zinterp4r   r   r   r  /  sL   










	



z,ExhaustiveChannelTests._iter_use_action_setsc              	   c   s    d}||f}dD ]}|rdnd}|D ]}|D ]
}t |||gV  qqq	dD ]}	dD ]}
|D ]}|D ]}t |	d|t |
d|gV  q2q.q*q&d S )Nr~   r  r^   r]   r\   r[   rZ   r  )rG   r  r  Zendsr  ru   opr   rV   ZrecvopZsendopr[   rZ   r   r   r   r  `  s,   


z.ExhaustiveChannelTests._iter_close_action_setsc                 c   s0    dD ]}t dd|gV  t dd|gV  qd S )N)rX   r`   r_   rW   r[   rZ   r  r   r   r   r   r  s  s   

z3ExhaustiveChannelTests._iter_post_close_action_setsc                 C   s   |D ]}|  || qd S r   )r|   )rG   fixr  rU   r   r   r   run_actions|  s   z"ExhaustiveChannelTests.run_actionsTrv   c                C   s   | |j}||j|j|j}|| |jdkr.t|j	|j
||j|d}||| d S t }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 )Nr8   rv   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   )rk   rj   )rf   rV   rg   r   r_   r`   r  rF   r|   ry   rU   rz   r  r   r   r4   r2   r   rh   r   
from_bytesr   r  )rG   r	  rU   rw   rV   r   r{   r  r   r   r   r|     sB   

	
z!ExhaustiveChannelTests.run_actionc              	   c   sD    g d}|D ]\}}}dD ]}dD ]}t |||||V  qqqd S )N))r8   r   r`   )r   r8   r`   )r  r  r`   )r  r  r8   )rX   r_   r  r  )r  )rG   r   r   r_   r`   r  rV   r   r   r   iter_fixtures  s   z$ExhaustiveChannelTests.iter_fixturesc                C   sx   |rdnd}t ||jd}| s| j||dd d S | tj | j||dd W d    d S 1 s5w   Y  d S )Nr^   r]   rX   Frv   )rR   rV   r  r|   r   r   rP   )rG   r	  ru   r  r]   r   r   r   _close  s   "zExhaustiveChannelTests._closeNc                 C   s2  |d u s	|j dkr| tj t|j W d    n1 s w   Y  | tj t|jd W d    n1 s=w   Y  | tj t|j W d    n1 sYw   Y  | tj tj|jdd W d    d S 1 sxw   Y  d S t|j	d t|j	d t|j	d t|j	d d S )	Nr8   r}   Trt   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)
                )
rF   r   r   rP   r   ry   r   r   r4   r2   )rG   r	  r   r   r   r   _assert_closed_in_interp  s"   "z/ExhaustiveChannelTests._assert_closed_in_interpc                 C   s|   |  |jj t|jjD ]}t|j q| | dD ]}|	|}|j
dkr*q| || q|	d}| || d S )N)rX   r_   r8   Zfresh)r   rz   rj   r   rk   r   r   ry   r  r  rF   )rG   r	  r   r   r   r   r   _assert_closed  s   



z%ExhaustiveChannelTests._assert_closedFc              	   c   s    d}|   D ]V}t  |  D ]F}|d7 }|dkr  d S |r7|d d dkr+t  t||dt| n|d d dkrEtddd td	dd tj  |||fV  q|r]td
 qt  d S )Nr   rm   r      z({} actions) r   rd   .z---)r  printr  rA   r  r   stdoutrF  )rG   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  rG   r   r	  r  r   r   r   _skim_close_tests  s   z(ExhaustiveChannelTests._skim_close_testsc              	   C      |   D ];\}}}| d|||  ||j | || | j|dd | | W d    n1 s6w   Y  |  qd S )N	{} {}  {}Frt   	r  r   rA   r  r   r
  r  r  r  r  r   r   r   
test_close     
z!ExhaustiveChannelTests.test_closec              	   C   r  )Nr  Trt   r  r  r   r   r   test_force_close		  r  z'ExhaustiveChannelTests.test_force_closer   )F)rK   rL   rM   r   r  r  r  r  r
  r|   r  r  r  r  r  r  r  r  r   r   r   r   r     s$    -1	 
	
r   rv  r   )6collectionsr   r  r   r   ri  r   textwrapr   r*   r   r   testr   Ztest.supportr   r   import_moduler   r   r   r$   r  r0   r4   r1   r9   rQ   rR   rh   r|   rx   r   r   ZTestCaser   r   r   r   r   r   r   r   r  r-  r;  r  r  r  r  r   r   rK   r8   r   r   r   r   <module>   sz    



&
	3$

2?);]b  +Y    : &q  
@