
    
dZ+                         d 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m	Z	 ddl
mZ ddlZddlmZmZ ddlmZmZ d Zdd	Z G d
 de          Z G d de          ZdS )z}
In addition to the remote_call mechanism implemented in CommBase:
 - Implements _wait_reply, so blocking calls can be made.
    N)get_ipython)	localhost)ioloop)CommBase	CommError)TimeoutErrorPY2c                  $   t          j                     } |                     t           j        t           j        d           |                     t                      df           |                                 d         }|                                  |S )z&Find a free port on the local machine.s           r      )socket
setsockopt
SOL_SOCKET	SO_LINGERbindr   getsocknameclose)sockports     Alib/python3.11/site-packages/spyder_kernels/comms/frontendcomm.pyget_free_portr      sl    =??DOOF%v'7CCCIIy{{Aa DJJLLLK    c                     t                      j        j                                        st	          d          t                      j                            | d|          S )zf
    Send a request to the frontend.

    If blocking is True, The return value will be returned.
    z%Can't make a request to a closed commF)blocking	broadcasttimeout)r   kernelfrontend_commis_openr   frontend_call)r   r   s     r   frontend_requestr    %   s_     ==-5577 A?@@@==-- .   r   c                        e Zd ZdZ fdZd fd	Z fdZd Zd Zd Z		 	 d fd
	Z
ddZddZd Z fdZd Zd Zd Z fdZ fdZ xZS )FrontendCommz(Mixin to implement the spyder_shell_api.c                     t          t                                                      | _         j        j                             j         j                   d  _         	                    d j
                   t          j                     _         j        j        r1t          j                     _         j                            t          j                   _        d j        _        t-                       _         j        j                             j         j                   _        t1          t          d          rd j        _        t          j                     _        t          j         j                   _         j                                         t@          s, j        j        j!         fd}| j        j        _!        d S d S d S )N_send_comm_configi  ROUTER_HANDOVERr   )targetc                  D                                                     dS )zClose comm_socket_thread.N)close_thread)parent_closeselfs   r   r   z$FrontendComm.__init__.<locals>.close^   s$    %%''' LNNNNNr   )"superr"   __init__r   comm_managerregister_target
_comm_name
_comm_open	comm_portregister_call_handlerr$   	threadingRLock	comm_lockparentzmqContextcontextr   ROUTERcomm_socketlingerr   _bind_sockethasattrrouter_handoverEventcomm_thread_closeThreadpoll_threadcomm_socket_threadstartr	   r   )r*   r   r   r)   	__class__s   `  @r   r,   zFrontendComm.__init__7   s   lD!!**,,,  00OT_	. 	. 	. ""#6#'#9	; 	; 	; #** ; 	1;==DL#|223:>>D&*D#*__DN![/<< $.2 2DNs-.. 5 45 0%._%6%6D"&/&6d>N&O&O&OD##))+++  1#{17# # # # # #
 ,1"(((;	1 	1*1 1r   Nc                     | j         5  t          t          |                               |          cddd           S # 1 swxY w Y   dS )z)Close the comm and notify the other side.N)r5   r+   r"   r   )r*   comm_idrF   s     r   r   zFrontendComm.closee   s    ^ 	< 	<t,,227;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<s   (>AAc                     | j         5   t          t          |           j        |i |cddd           S # 1 swxY w Y   dS )z*Publish custom messages to the other side.N)r5   r+   r"   _send_message)r*   argskwargsrF   s      r   rJ   zFrontendComm._send_messagej   s    ^ 	L 	L:5t,,:DKFKK	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	Ls    6::c                     | j                                          | j                                         | j                                         | j                                         dS )Close comm.N)rA   setr;   r   r9   termrD   joinr*   s    r   r(   zFrontendComm.close_threado   s[    ""$$$   $$&&&&&r   c                     t           s%t          j                                                     | j                                        s/|                                  | j                                        -dS dS )z"Receive messages from comm socket.N)r	   r   IOLoop
initializerA   is_setpoll_onerR   s    r   rC   zFrontendComm.poll_threadv   ss     	)MOO&&((((//11 	MMOOO (//11 	 	 	 	 	r   c                    d}| j         j        r| j         j        d         }	 | j         j                            | j        d          \  }}nH# t
          j        j        $ r Y dS t          $ r% | j         j	        
                    dd           Y dS w xY w|d         d         }|dk    r0| j                                         |                     |           dS | j         j                            |d          }	 || j         j	        
                    d	|           	 t           j                                         t           j                                         |r!|                    t
          j                   dS dS t*          ro ||||           	 t           j                                         t           j                                         |r!|                    t
          j                   dS dS ddl}t/          |d
d          r6|                    |          r!|                     ||||                     n ||||           n1# t          $ r$ | j         j	                            dd           Y nw xY wt           j                                         t           j                                         |r!|                    t
          j                   dS dS # t           j                                         t           j                                         |r |                    t
          j                   w w xY w)z%Receive one message from comm socket.Nr   zInvalid Message:T)exc_infoheadermsg_typeshutdown_requestzUnknown message type: %rrunFzException in message handler:)r   shell_streamssessionrecvr;   r7   errorContextTerminated	ExceptionlogwarningrA   rO   _comm_closeshell_handlersgetsysstdoutflushstderrPOLLOUTr	   asynciogetattriscoroutinefunctionr]   )r*   
out_streamidentmsgr[   handlerrn   s          r   rW   zFrontendComm.poll_one~   sy   
;$ 	6 215J	,11$2BAFFJE33y* 	 	 	FF 	 	 	KO##$6#FFFFF	 x=,)))"&&(((S!!!F+,004@@	.''(BHMMM* JJ .  -----. ./  
E3///$ JJ .  -----. .' NNN.. 	0//88	0 GGJs;;<<<< 
E3/// 	@ 	@ 	@KO!!/$ " @ @ @ @ @	@ JJ .  -----. . JJ .  ----.sO   (A B"*BB8"I <I 2AI 
K +I96K 8I99K A L=Fc                 \    t          t          |                               ||||          S )zGet a handler for remote calls.)r   rH   callbackr   )r+   r"   remote_call)r*   rH   r   rv   r   rF   s        r   rw   zFrontendComm.remote_call   s8     \4((44	 5   	r   c                 .    |            rdS t          j                     } |            sk|t          j                     ||z   k    rdS t          j                    | j        u r|                                  nt          j        d            |            kdS )z6Wait until condition is met. Returns False if timeout.TNFg{Gz?)timer3   current_threadrD   rW   sleep)r*   	conditionr   t_starts       r   
wait_untilzFrontendComm.wait_until   s    9;; 	4)++)++ 	!"ty{{Ww5F'F'Fu'))T-DDD 
4    )++ 	! tr   Tc                       fd}                      |          s>|r                     ||d           dS t          d                    |                    dS )z-Wait until the frontend replies to a request.c                       j         v S )zThe reply is there!)_reply_inbox)call_idr*   s   r   reply_receivedz0FrontendComm._wait_reply.<locals>.reply_received   s    d///r   FNz%Timeout while waiting for '{}' reply.)r~   _wait_replyr   format)r*   r   	call_namer   retryr   s   ``    r   r   zFrontendComm._wait_reply   s    	0 	0 	0 	0 	0 	0 ~.. 	    )WeDDD7>>      		  	 r   c                     |j         | _        |                     |           |                     |d         d         d                    |                                  dS )z%
        A new comm is open!
        contentdatapickle_protocolN)rH   calling_comm_id_register_comm_set_pickle_protocolr$   )r*   commrs   s      r   r0   zFrontendComm._comm_open   s]      $|D!!!!!#i."89J"KLLL     r   c                 h    | j         |d<   t          t          |                               |          S )zA message is about to be sentr1   )r1   r+   r"   on_outgoing_call)r*   	call_dictrF   s     r   r   zFrontendComm.on_outgoing_call   s-    !%	+\4((99)DDDr   c                     |                                                      | j                   |                                                      t          j                   dS )z%Send the comm config to the frontend.N)rw   _set_comm_portr1   r   pickleHIGHEST_PROTOCOLrR   s    r   r$   zFrontendComm._send_comm_config   sM    ))$.999//0GHHHHHr   c                 L    |d         d         }|                      |           dS )rN   r   rH   N)r   )r*   rs   rH   s      r   rf   zFrontendComm._comm_close   s*    i.+ 	

7r   c                 T    |                                                      |           dS )zK
        Send an async error back to the frontend to be displayed.
        N)rw   _async_error)r*   error_wrappers     r   r   zFrontendComm._async_error   s*     	''66666r   c                 r    fd}|_         t          t          |                                          dS )z3
        Remove side effect ipykernel has.
        c                 D    j         r                     |            dS dS )zHandle a comm_msg messageN)_msg_callback)rs   r   s    r   
handle_msgz/FrontendComm._register_comm.<locals>.handle_msg   s2    ! (""3'''''( (r   N)r   r+   r"   r   )r*   r   r   rF   s    ` r   r   zFrontendComm._register_comm   sJ    	( 	( 	( 	( 	( %lD!!0066666r   c                    | j         5  t          j        }t          j        }|j        }|j        }t          j                    }t          |||          |_        t          |||          |_        	 t          t          |           
                    |||          ||_        ||_        cddd           S # ||_        ||_        w xY w# 1 swxY w Y   dS )z/Call the callback function for the remote call.N)r5   ri   rj   rl   writer3   	get_identWriteWrapperr+   r"   _remote_callback)
r*   r   	call_argscall_kwargscurrent_stdoutcurrent_stderrsaved_stdout_writesaved_stderr_write	thread_idrF   s
            r   r   zFrontendComm._remote_callback  s)   ^ 	: 	: ZN ZN!/!5!/!5!+--I#/"Iy$: $:N #/"Iy$: $:N :\400AAy+7 7 (:$'9$	: 	: 	: 	: 	: 	: 	: 	: (:$'9$9999	: 	: 	: 	: 	: 	: 	: 	: 	: 	:s*   A&C0)B4C4CCCCN)NFNN)T)__name__
__module____qualname____doc__r,   r   rJ   r(   rC   rW   rw   r~   r   r0   r   r$   rf   r   r   r   __classcell__)rF   s   @r   r"   r"   4   s       22,1 ,1 ,1 ,1 ,1\< < < < < <
L L L L L
' ' '  2. 2. 2.h BF         "       ! ! !E E E E E
I I I
  7 7 7	7 	7 	7 	7 	7: : : : : : : : :r   r"   c                   $    e Zd ZdZd Zd Zd ZdS )r   z*Wrapper to warn user when text is printed.c                 >    || _         || _        || _        d| _        d S )NF)_write_name
_thread_id_warning_shown)r*   r   namer   s       r   r,   zWriteWrapper.__init__  s%    
##r   c                 B    ddg}t          fd|D                       S )z7Determine if a message is benign in order to filter it.DeprecationWarningzIOStream.flush timed outc                     g | ]}|v S  r   ).0rs   messages     r   
<listcomp>z2WriteWrapper.is_benign_message.<locals>.<listcomp>+  s    >>>sC7N>>>r   )any)r*   r   benign_messagess    ` r   is_benign_messagezWriteWrapper.is_benign_message!  s7    
 !&
 >>>>o>>>???r   c                 b   | j         t          j                    k    r|                     |          S |                     |          sh| j        sLd| _        t          | j                  dk    r-|                     dt          | j                  z   dz              |                     |          S dS )zPrint warning once.Tz'show_mpl_backend_errors'z
Output from spyder call z:
N)r   r3   r   r   r   r   reprr   )r*   strings     r   __call__zWriteWrapper.__call__-  s    ?i13333;;v&&&%%f-- 	'& &*# 
##'BBBKK4tDJ7G7GG%O   ;;v&&&	' 	'r   N)r   r   r   r   r,   r   r   r   r   r   r   r     sJ        44$ $ $
@ 
@ 
@' ' ' ' 'r   r   r   )r   r   r   ri   r3   ry   IPython.core.getipythonr   jupyter_client.localinterfacesr   tornador   r7   spyder_kernels.comms.commbaser   r   spyder_kernels.py3compatr   r	   r   r    r"   objectr   r   r   r   <module>r      s:   
   



      / / / / / / 4 4 4 4 4 4       



 = = = = = = = = 6 6 6 6 6 6 6 6     a: a: a: a: a:8 a: a: a:H%' %' %' %' %'6 %' %' %' %' %'r   