a
    &ñ»jg  ã                   @  sô   d Z ddlm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m	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ G dd„ dejƒZG dd„ dƒZeZddœdd„Zddddœdd„Zdddœdd„ZdS ) zhGateway code for initiating popen, socket and ssh connections.

(c) 2004-2013, Holger Krekel and others
é    )ÚannotationsN)ÚTYPE_CHECKING)ÚAny)ÚCallableé   )Úgateway_base)ÚIO)ÚChannel)ÚMessage)ÚGroup)ÚXSpecc                      sÈ   e Zd ZU dZded< ddddœ‡ fdd	„Zed
dœdd„ƒZd
dœdd„Zddœdd„Z	d,ddddœdd„Z
d-dddœdd„Zddœdd„Zddœdd „Zd!d"d#d$œd%d&„Zd.d(dd)œd*d+„Z‡  ZS )/ÚGatewayz0Gateway to a local or remote Python Interpreter.r   Ú_groupr   r   ÚNone)ÚioÚspecÚreturnc                   s&   t ƒ j||jdd� || _|  ¡  dS )z	:private:r   )r   ÚidZ_startcountN)ÚsuperÚ__init__r   r   Z_initreceive)Úselfr   r   ©Ú	__class__© ú5E:\soccer\ml_env\Lib\site-packages\execnet/gateway.pyr      s    zGateway.__init__Ústr©r   c                 C  s   | j jS ©N)Ú_ioÚremoteaddress©r   r   r   r   r   #   s    zGateway.remoteaddressc                 C  sp   z&|   ¡ rdpd}tt| j ¡ ƒƒ}W n ty@   d}d}Y n0 d| jj› d| j›d|› d| j	j
› d	|› d
�S )z.A string representing gateway type and status.zreceive-liveznot-receivingZuninitializedÚnoú<z id=ú ú, z model, z active channels>)Úhasreceiverr   ÚlenÚ_channelfactoryZchannelsÚAttributeErrorr   Ú__name__r   Z	execmodelÚbackend)r   ÚrÚir   r   r   Ú__repr__(   s    
zGateway.__repr__c              
   C  s¤   |   d¡ | | jvr"|   d¡ dS | j | ¡ z.|   d¡ |  tj¡ |   d¡ | j ¡  W nB tt	t
fyž } z$|   d¡ |   d| ¡ W Y d}~n
d}~0 0 dS )z›Trigger gateway exit.

        Defer waiting for finishing of receiver-thread and subprocess activity
        to when group.terminate() is called.
        zgateway.exit() calledz'gateway already unregistered with groupNz--> sending GATEWAY_TERMINATEz--> io.close_writez-io-error: could not send termination sequencez exception: %r)Z_tracer   Z_unregisterÚ_sendr
   ZGATEWAY_TERMINATEr   Zclose_writeÚ
ValueErrorÚEOFErrorÚOSError)r   Úexcr   r   r   Úexit2   s    





zGateway.exitTFÚbool)Úpy2str_as_py3strÚpy3str_as_py2strr   c                 C  s*   ||f| _ t | j ¡}| jtj|d� dS )z§Set the string coercion for this gateway.

        The default is to try to convert py2 str as py3 str, but not to try and
        convert py3 str to py2 str.
        )ÚdataN)Z
_strconfigr   Údumps_internalr.   r
   ZRECONFIGURE)r   r5   r6   r7   r   r   r   ÚreconfigureF   s    
zGateway.reconfigureÚRInfo)Úupdater   c                 C  sD   |st | dƒs>|  t¡}zt| ¡ ƒ| _W | ¡  n
| ¡  0 | jS )z,Return some sys/env information from remote.Ú_cache_rinfo)ÚhasattrÚremote_execÚrinfo_sourcer:   Úreceiver<   Z	waitclose)r   r;   Úchr   r   r   Ú_rinfoR   s    
zGateway._rinfoc                 C  s   | j  ¡ dkS )z(Whether gateway is able to receive data.r   )Z_receivepoolÚactive_countr    r   r   r   r%   \   s    zGateway.hasreceiverÚRemoteStatusc                 C  s6   |   ¡ }|  tj|j¡ | ¡ }| j |j¡ t|ƒS )z5Obtain information about the remote execution status.)	Ú
newchannelr.   r
   ZSTATUSr   r@   r'   Z_local_closerD   )r   ÚchannelZ
statusdictr   r   r   Úremote_status`   s
    zGateway.remote_statuszCstr | types.FunctionType | Callable[..., object] | types.ModuleTypeÚobjectr	   )ÚsourceÚkwargsr   c              
   K  s¤   d}d}t |tjƒr4t |¡}t |¡ t |¡}n4t |tjƒrZ|j	}t |¡}t
|ƒ}nt t|ƒ¡}|sx|rxtdƒ‚|  ¡ }|  tj|jt ||||f¡¡ |S )a¯  Return channel object and connect it to a remote
        execution thread where the given ``source`` executes.

        * ``source`` is a string: execute source string remotely
          with a ``channel`` put into the global namespace.
        * ``source`` is a pure function: serialize source and
          call function with ``**kwargs``, adding a
          ``channel`` object to the keyword arguments.
        * ``source`` is a pure module: execute source of module
          with a ``channel`` in its global namespace.

        In all cases the binding ``__name__='__channelexec__'``
        will be available in the global namespace of the remotely
        executing code.
        Nz-can't pass kwargs to non-function remote_exec)Ú
isinstanceÚtypesÚ
ModuleTypeÚinspectÚgetsourcefileÚ	linecacheÚupdatecacheÚ	getsourceÚFunctionTyper)   Ú_source_of_functionÚtextwrapÚdedentr   Ú	TypeErrorrE   r.   r
   ZCHANNEL_EXECr   r   r8   )r   rI   rJ   Z	call_nameÚ	file_namerF   r   r   r   r>   j   s(    



ýzGateway.remote_execNz
int | None)Únumr   c                 C  s   t dƒ dS )z1DEPRECATED.  Is currently a NO-OPERATION already.z?WARNING: remote_init_threads() is a no-operation in execnet-1.2N)Úprint)r   rY   r   r   r   Úremote_init_threads–   s    zGateway.remote_init_threads)TF)F)N)r)   Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   Úpropertyr   r-   r3   r9   rB   r%   rG   r>   r[   Ú__classcell__r   r   r   r   r      s   

 ÿ

,r   c                   @  s<   e Zd Zddœdd„Zddœdd„Zer8ddd	œd
d„ZdS )r:   r   r   c                 C  s   | j  |¡ d S r   )Ú__dict__r;   )r   rJ   r   r   r   r   œ   s    zRInfo.__init__r   c                 C  s&   d  dd„ t| j ¡ ƒD ƒ¡}d| S )Nr$   c                 s  s    | ]\}}|› d |› �V  qdS )ú=Nr   )Ú.0ÚkÚvr   r   r   Ú	<genexpr>    ó    z!RInfo.__repr__.<locals>.<genexpr>z
<RInfo %r>)ÚjoinÚsortedrb   Úitems)r   Úinfor   r   r   r-   Ÿ   s    zRInfo.__repr__r   )Únamer   c                 C  s   d S r   r   )r   rm   r   r   r   Ú__getattr__¥   rh   zRInfo.__getattr__N)r)   r\   r]   r   r-   r   rn   r   r   r   r   r:   ›   s   r:   r   r   c              	   C  sB   dd l }dd l}|  t|j|jd d… |j| ¡ | ¡ d�¡ d S )Nr   é   )Ú
executableÚversion_infoÚplatformÚcwdÚpid)	ÚosÚsysÚsendÚdictrp   rq   rr   ÚgetcwdÚgetpid)rF   ru   rv   r   r   r   r?   «   s    ûÿr?   r   ztypes.CodeTypez	list[str])rI   Úcodeobjr   c                   s>   dd l ‰ dd l‰t |j¡‰‡ ‡‡fdd„ˆ  ˆ  | ¡¡D ƒS )Nr   c                   s4   g | ],}t |ˆ jƒr|jˆvr|jˆjvr|j‘qS r   )rK   ÚNamer   rb   )rd   Únode©ÚastÚbuiltinsÚvarsr   r   Ú
<listcomp>¿   s
   
üz-_find_non_builtin_globals.<locals>.<listcomp>)r   r€   rx   ÚfromkeysÚco_varnamesÚwalkÚparse)rI   r{   r   r~   r   Ú_find_non_builtin_globalsº   s    þr‡   z*types.FunctionType | Callable[..., object])Úfunctionr   c           	   
   C  sö   | j dkrtdƒ‚zt | ¡}W n  ty@   t | ¡d }Y n0 |j}|rX|d dkr`tdƒ‚| j}| j}|d ur|tdƒ‚zt 	| ¡}W n2 t
y¼ } ztd|  ƒ|‚W Y d }~n
d }~0 0 t |¡}t||ƒ}|ràtd|ƒ‚d	|jd
  }|| S )Nz<lambda>z can't evaluate lambda functions'r   rF   z0expected first function argument to be `channel`z'functions with closures can't be passedzcan't find source file for %sz.the use of non-builtin globals isn't supportedÚ
r   )r)   r/   rN   Úgetfullargspecr(   Ú
getargspecÚargsÚ__closure__Ú__code__rR   r1   rU   rV   r‡   Úco_firstlineno)	rˆ   ÚsigrŒ   Úclosurer{   rI   ÚeZused_globalsZ
leading_wsr   r   r   rT   È   s.    
$


rT   )r^   Ú
__future__r   rN   rP   rU   rL   Útypingr   r   r   Ú r   r   r	   r
   Úmultir   Zxspecr   ZBaseGatewayr   r:   rD   r?   r‡   rT   r   r   r   r   Ú<module>   s*    