
    Rh                         d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	m
Z
 g dZdddZd	 Zd
 Zd Z G d d      Zd Zd Z G d d      Z e       Z G d de      Z G d de      Zy)z"Subinterpreters High Level Module.    N)is_shareableRunFailedError)ChannelErrorChannelNotFoundErrorChannelEmptyError)Interpreterget_currentget_maincreatelist_allSendChannelRecvChannelcreate_channellist_all_channelsr   r   r   r   Tisolatedc                 H    t        j                  |       }t        ||       S )z'Return a new (idle) Python interpreter.r   )_interpretersr   r   )r   ids     0/usr/lib/python3.12/test/support/interpreters.pyr   r      s    			x	0BrH--    c                  b    t        j                         D  cg c]  } t        |        c} S c c} w )z!Return all existing interpreters.)r   r   r   r   s    r   r   r      s#    &3&<&<&>?KO???s   ,c                  @    t        j                         } t        |       S )z)Return the currently running interpreter.)r   r	   r   r   s    r   r	   r	   "   s    		"	"	$Br?r   c                  @    t        j                         } t        |       S )zReturn the main interpreter.)r   r
   r   r   s    r   r
   r
   (   s    				!Br?r   c                   f    e Zd ZdZdddZd Zd Zd Zed        Z	ed	        Z
d
 Zd ZdddZy)r   zA single Python interpreter.Nr   c                |    t        |t        t        j                  f      st	        d|      || _        || _        y Nzid must be an int, got )
isinstanceintr   InterpreterID	TypeError_id	_isolated)selfr   r   s      r   __init__zInterpreter.__init__1   s8    "sM$?$?@A5bV<==!r   c                     t        t        | j                        | j                        }d |j	                         D        }t        |       j                   ddj                  |       dS )N)r   r   c              3   0   K   | ]  \  }}| d |  yw)=N ).0kvs      r   	<genexpr>z'Interpreter.__repr__.<locals>.<genexpr>9   s     841aQCq,8s   (z, ))dictr    r#   r$   itemstype__name__join)r%   datakwargss      r   __repr__zInterpreter.__repr__7   sR    s488}t~~>84::<8t*%%&a		&(9':!<<r   c                 ,    t        | j                        S Nhashr#   r%   s    r   __hash__zInterpreter.__hash__<       DHH~r   c                 `    t        |t              st        S |j                  | j                  k(  S r:   )r   r   NotImplementedr#   r%   others     r   __eq__zInterpreter.__eq__?   s%    %-!!99((r   c                     | j                   S r:   r#   r=   s    r   r   zInterpreter.idE       xxr   c                 z    | j                   $t        j                  | j                        | _         | j                   S r:   )r$   r   is_isolatedr#   r=   s    r   r   zInterpreter.isolatedI   s.    >>! +66txx@DN~~r   c                 @    t        j                  | j                        S )z<Return whether or not the identified interpreter is running.)r   
is_runningr#   r=   s    r   rK   zInterpreter.is_runningQ   s    ''11r   c                 @    t        j                  | j                        S )zFinalize and destroy the interpreter.

        Attempting to destroy the current interpreter results
        in a RuntimeError.
        )r   destroyr#   r=   s    r   closezInterpreter.closeU   s     $$TXX..r   )channelsc               F    t        j                  | j                  ||       y)zqRun the given source code in the interpreter.

        This blocks the current Python thread until done.
        N)r   
run_stringr#   )r%   src_strrO   s      r   runzInterpreter.run]   s    
 	  7H=r   )r4   
__module____qualname____doc__r&   r8   r>   rD   propertyr   r   rK   rN   rS   r*   r   r   r   r   .   sX    &'+ "=
)    2/ +/ >r   r   c                  ^    t        j                         } t        |       t        |       }}||fS )zReturn (recv, send) for a new cross-interpreter channel.

    The channel may be used to pass data safely between interpreters.
    )	_channelsr   r   r   )cidrecvsends      r   r   r   e   s/    
 


CS!;s#3$D:r   c                  x    t        j                         D  cg c]  } t        |       t        |       f c} S c c} w )z4Return a list of (recv, send) for all open channels.)rY   r   r   r   )rZ   s    r   r   r   o   s<     !))+- {3/0 - - -s   7c                   8    e Zd ZdZd Zd Zd Zd Zed        Z	y)_ChannelEndz/The base class for RecvChannel and SendChannel.c                 n    t        |t        t        j                  f      st	        d|      || _        y r   )r   r    rY   	ChannelIDr"   r#   )r%   r   s     r   r&   z_ChannelEnd.__init__x   s1    "sI$7$7895bV<==r   c                 ^    t        |       j                   dt        | j                         dS )Nz(id=r0   )r3   r4   r    r#   r=   s    r   r8   z_ChannelEnd.__repr__}   s(    t*%%&d3txx=/;;r   c                 ,    t        | j                        S r:   r;   r=   s    r   r>   z_ChannelEnd.__hash__   r?   r   c                     t        | t              rt        |t              st        S t        |t              st        S |j                  | j                  k(  S r:   )r   r   rA   r   r#   rB   s     r   rD   z_ChannelEnd.__eq__   s?    dK(e[1%%E;/!!yyDHH$$r   c                     | j                   S r:   rF   r=   s    r   r   z_ChannelEnd.id   rG   r   N)
r4   rT   rU   rV   r&   r8   r>   rD   rW   r   r*   r   r   r_   r_   u   s-    9
<%  r   r_   c                   2    e Zd ZdZ e       dddZefdZy)r   z1The receiving end of a cross-interpreter channel.g{Gz?)	_sentinel_delayc                    t        j                  | j                  |      }||u r:t        j                  |       t        j                  | j                  |      }||u r:|S )zReturn the next object from the channel.

        This blocks until an object has been sent, if none have been
        sent already.
        )rY   r[   r#   timesleep)r%   rg   rh   objs       r   r[   zRecvChannel.recv   sQ     nnTXXy1YJJv..95C Y 
r   c                     |t         u rt        j                  | j                        S t        j                  | j                  |      S )zReturn the next object from the channel.

        If none have been sent then return the default if one
        is provided or fail with ChannelEmptyError.  Otherwise this
        is the same as recv().
        )_NOT_SETrY   r[   r#   )r%   defaults     r   recv_nowaitzRecvChannel.recv_nowait   s4     h>>$((++>>$((G44r   N)r4   rT   rU   rV   objectr[   rn   rp   r*   r   r   r   r      s    ; & 
 #+ 
5r   r   c                       e Zd ZdZd Zd Zy)r   z/The sending end of a cross-interpreter channel.c                 n    t        j                  | j                  |       t        j                  d       y)z{Send the object (i.e. its data) to the channel's receiving end.

        This blocks until the object is received.
           N)rY   r\   r#   rj   rk   r%   rl   s     r   r\   zSendChannel.send   s#    
 	txx% 	

1r   c                 B    t        j                  | j                  |      S )zSend the object to the channel's receiving end.

        If the object is immediately received then return True
        (else False).  Otherwise this is the same as send().
        )rY   r\   r#   ru   s     r   send_nowaitzSendChannel.send_nowait   s     ~~dhh,,r   N)r4   rT   rU   rV   r\   rw   r*   r   r   r   r      s    9		-r   r   )rV   rj   _xxsubinterpretersr   _xxinterpchannelsrY   r   r   r   r   r   __all__r   r   r	   r
   r   r   r   r_   rq   rn   r   r   r*   r   r   <module>r{      s    (  * % < 
  .@
4> 4>n- 6 85+ 58-+ -r   