a
    ø£Beƒd  ã                   @   sü   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ZddlZddlZddl	m
Z
 ddlmZ ddlmZmZ ddlmZ ddlmZmZmZmZmZ ddlZddlmZ dd	lmZ dd
lmZ dZdZdZG dd„ dƒZ G dd„ dƒZ!G dd„ deƒZ"dS )z.Wrappers for forwarding stdout/stderr over zmqé    N)Úb2a_hex)Údeque)ÚStringIOÚ
TextIOBase)Úlocal)ÚAnyÚCallableÚDequeÚDictÚOptional)Úextract_header)ÚIOLoop)Ú	ZMQStreamé   iè  c                   @   s²   e Zd ZdZd*dd„Zdd„ Zdd„ Zd	d
„ Zdd„ Ze	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"„ ƒZd#d$„ Zd%d&„ Zd'd(„ Zd)S )+ÚIOPubThreada   An object for sending IOPub messages in a background thread

    Prevents a blocking main thread from delaying output from threads.

    IOPubThread(pub_socket).background_socket is a Socket-API-providing object
    whose IO is always run in a thread.
    Fc                 C   sª   || _ d| _t| ƒ| _t ¡ | _|| _tdd�| _	|r>|  
¡  t ¡ | _tƒ | _i | _t ¡ | _d| _d| _|  ¡  tj| jdd�| _d| j_d| j_d| j_d| j_dS )a  Create IOPub thread

        Parameters
        ----------
        socket : zmq.PUB Socket
            the socket on which messages will be sent.
        pipe : bool
            Whether this process should listen for IOPub messages
            piped from subprocesses.
        F)Zmake_currenté
   NÚIOPub)ÚtargetÚnameT)ÚsocketÚ_stoppedÚBackgroundSocketZbackground_socketÚosÚgetpidÚ_master_pidÚ
_pipe_flagr   Úio_loopÚ_setup_pipe_inÚ	threadingr   Ú_localr   Ú_eventsÚ_event_pipesÚLockÚ_event_pipe_gc_lockÚ_event_pipe_gc_secondsÚ_event_pipe_gc_taskÚ_setup_event_pipeÚThreadÚ_thread_mainÚthreadÚdaemonZpydev_do_not_traceZis_pydev_daemon_threadr   )Úselfr   Úpipe© r-   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/ipykernel/iostream.pyÚ__init__0   s(    



zIOPubThread.__init__c                    sn   ‡ fdd„}ˆ j  |¡ ˆ js(ˆ j  ¡  ˆ jdur\‡ fdd„}ˆ jsRˆ j  |¡ n
ˆ j ¡  ˆ j jdd� dS )z.The inner loop that's actually run in a threadc                      s   t  ˆ  ¡ ¡ˆ _d S ©N)ÚasyncioÚensure_futureÚ_run_event_pipe_gcr%   r-   ©r+   r-   r.   Ú_start_event_gcS   s    z1IOPubThread._thread_main.<locals>._start_event_gcNc                   “   s   ˆ j  ¡  d S r0   )r%   Úcancelr-   r4   r-   r.   Ú_cancel_   s    z)IOPubThread._thread_main.<locals>._cancelT)Zall_fds)r   Zrun_syncr   Ústartr%   r6   Úclose)r+   r5   r7   r-   r4   r.   r(   P   s    


zIOPubThread._thread_mainc                 C   sf   | j j}|  tj¡}d|_tt d¡ƒ d¡}d|  }| _	| 
|¡ t|| jƒ| _| j | j¡ dS )zLCreate the PULL socket listening for events that should fire in this thread.r   é   Úasciizinproc://%sN)r   ÚcontextÚzmqÚPULLÚlingerr   r   ÚurandomÚdecodeÚ_event_interfaceÚbindr   r   Z_event_pullerÚon_recvÚ_handle_event)r+   ÚctxÚpipe_inÚ_uuidZifacer-   r-   r.   r&   i   s    
zIOPubThread._setup_event_pipec              
   Ã   sd   t  | j¡I dH  z|  ¡ I dH  W q  ty\ } z td|› �tjd� W Y d}~q d}~0 0 q dS )z&Task to run event pipe gc continuouslyNz)Exception in IOPubThread._event_pipe_gc: ©Úfile)r1   Úsleepr$   Ú_event_pipe_gcÚ	ExceptionÚprintÚsysÚ
__stderr__)r+   Úer-   r-   r.   r3   u   s
    zIOPubThread._run_event_pipe_gcc                 Ã   sd   | j s
dS | j�@ t| j  ¡ ƒD ] \}}| ¡ s | ¡  | j |= q W d  ƒ n1 sV0    Y  dS )z.run a single garbage collection on event pipesN)r!   r#   ÚlistÚitemsÚis_aliver9   )r+   r)   r   r-   r-   r.   rL   ~   s    zIOPubThread._event_pipe_gcc                 C   s†   z| j j}W nt ty€   | jj}| tj¡}d|_| | j	¡ || j _| j
� || jt ¡ < W d  ƒ n1 sr0    Y  Y n0 |S )zSthread-local event pipe for signaling events that should be processed in the threadr   N)r   Ú
event_pipeÚAttributeErrorr   r<   r=   ÚPUSHr?   ÚconnectrB   r#   r!   r   Úcurrent_thread)r+   rU   rF   r-   r-   r.   Ú_event_pipe‰   s    2zIOPubThread._event_pipec                 C   s,   t | jƒ}t|ƒD ]}| j ¡ }|ƒ  qdS )zÅHandle an event on the event pipe

        Content of the message is ignored.

        Whenever *an* event arrives on the event stream,
        *all* waiting events are processed in order.
        N)Úlenr    ÚrangeÚpopleft)r+   ÚmsgZn_eventsÚ_Zevent_fr-   r-   r.   rE   œ   s    


zIOPubThread._handle_eventc              
   C   s¤   | j j}t d¡| _|  tj¡}d|_z| d¡| _	W nL tj
y‚ } z2tjd| d dd� d| _| ¡  W Y d	}~d	S d	}~0 0 t|| jƒ| _| j | j¡ d	S )
z7setup listening pipe for IOPub from forked subprocessesr:   r   ztcp://127.0.0.1z)Couldn't bind IOPub Pipe to 127.0.0.1: %sz'
subprocess output will be unavailable.é   ©Ú
stacklevelFN)r   r<   r   r@   Ú
_pipe_uuidr=   r>   r?   Zbind_to_random_portÚ
_pipe_portZZMQErrorÚwarningsÚwarnr   r9   r   r   Z_pipe_inrD   Ú_handle_pipe_msg)r+   rF   rG   rQ   r-   r-   r.   r   «   s$    ÿýzIOPubThread._setup_pipe_inc                 C   sJ   | j r|  ¡ sdS |d | jkr4td|tjd� dS |  |dd… ¡ dS )z'handle a pipe message from a subprocessNr   zBad pipe message: %srI   r   )r   Ú_is_master_processrc   rN   rO   rP   Úsend_multipart©r+   r^   r-   r-   r.   rg   Ã   s    zIOPubThread._handle_pipe_msgc                 C   s2   t  ¡ }| t j¡}d|_| d| j ¡ ||fS )Ni¸  ztcp://127.0.0.1:%i)r=   ÚContextr   rW   r?   rX   rd   )r+   rF   Úpipe_outr-   r-   r.   Ú_setup_pipe_outÌ   s
    zIOPubThread._setup_pipe_outc                 C   s   t  ¡ | jkS r0   ©r   r   r   r4   r-   r-   r.   rh   Ô   s    zIOPubThread._is_master_processc                 C   s   | j r|  ¡ rtS tS dS )z8check for forks, and switch to zmq pipeline if necessaryN)r   rh   ÚMASTERÚCHILDr4   r-   r-   r.   Ú_check_mp_mode×   s    zIOPubThread._check_mp_modec                 C   s"   d| j _| j  ¡  t | j¡ dS )zStart the IOPub threadr   N)r)   r   r8   ÚatexitÚregisterÚstopr4   r-   r-   r.   r8   Þ   s    
zIOPubThread.startc                 C   sh   d| _ | j ¡ sdS | j | jj¡ | jjdd� | j ¡ rHd}t|ƒ‚| j 	¡ D ]\}}| 
¡  qRdS )zStop the IOPub threadTNé   )Útimeoutz,IOPub thread did not terminate in 30 seconds)r   r)   rT   r   Zadd_callbackrt   ÚjoinÚTimeoutErrorr!   rS   r9   )r+   r^   Ú_threadrU   r-   r-   r.   rt   æ   s    

zIOPubThread.stopc                 C   s   | j r
dS | j ¡  d| _dS )zClose the IOPub thread.N)Úclosedr   r9   r4   r-   r-   r.   r9   ø   s    
zIOPubThread.closec                 C   s
   | j d u S r0   )r   r4   r-   r-   r.   rz   ÿ   s    zIOPubThread.closedc                 C   s.   | j  ¡ r$| j |¡ | j d¡ n|ƒ  dS )ztSchedule a function to be called in our IO thread.

        If the thread is not running, call immediately.
        ó    N)r)   rT   r    ÚappendrZ   Úsend)r+   Úfr-   r-   r.   Úschedule  s    
zIOPubThread.schedulec                    s   ˆ  ‡ ‡‡fdd„¡ dS )zŒsend_multipart schedules actual zmq send in my thread.

        If my thread isn't running (e.g. forked process), send immediately.
        c                      s   ˆj ˆ i ˆ¤ŽS r0   )Ú_really_sendr-   ©ÚargsÚkwargsr+   r-   r.   Ú<lambda>  r{   z,IOPubThread.send_multipart.<locals>.<lambda>N)r   ©r+   r‚   rƒ   r-   r�   r.   ri     s    zIOPubThread.send_multipartc                 O   sv   | j r
dS |  ¡ }|tkr6| jj|g|¢R i |¤Ž n<|  ¡ \}}|j| jg|¢g|¢R i |¤Ž | ¡  | ¡  dS )z)The callback that actually sends messagesN)	rz   rq   rp   r   ri   rm   rc   r9   Úterm)r+   r^   r‚   rƒ   Zmp_moderF   rl   r-   r-   r.   r€     s     zIOPubThread._really_sendN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   r(   r&   r3   rL   ÚpropertyrZ   rE   r   rg   rm   rh   rq   r8   rt   r9   rz   r   ri   r€   r-   r-   r-   r.   r   '   s,   
 	
	
r   c                       sH   e Zd ZdZdZdd„ Z‡ fdd„Z‡ fdd„Zd	d
„ Zdd„ Z	‡  Z
S )r   z>Wrapper around IOPub thread that provides zmq send[_multipart]Nc                 C   s
   || _ dS )zInitialize the socket.N)Ú	io_thread)r+   rŒ   r-   r-   r.   r/   /  s    zBackgroundSocket.__init__c                    sv   |  d¡r | d¡r tƒ  |¡ | jdus.J ‚t| jj|ƒrftjd|› d|› �t	dd� t
| jj|ƒS tƒ  |¡ dS )z2Wrap socket attr access for backward-compatibilityÚ__NzAccessing zmq Socket attribute úO on BackgroundSocket is deprecated since ipykernel 4.3.0 use .io_thread.socket.r`   ra   )Ú
startswithÚendswithÚsuperÚ__getattr__rŒ   Úhasattrr   re   rf   ÚDeprecationWarningÚgetattr)r+   Úattr©Ú	__class__r-   r.   r’   3  s    þûzBackgroundSocket.__getattr__c                    sj   |dks|  d¡r,| d¡r,tƒ  ||¡ n:tjd|› d|› �tdd� | jdusVJ ‚t| jj	||ƒ dS )zSet an attribute on the socket.rŒ   r�   zSetting zmq Socket attribute rŽ   r`   ra   N)
r�   r�   r‘   Ú__setattr__re   rf   r”   rŒ   Úsetattrr   )r+   r–   Úvaluer—   r-   r.   r™   D  s    þûzBackgroundSocket.__setattr__c                 O   s   | j |gg|¢R i |¤ŽS )zSend a message to the socket.)ri   )r+   r^   r‚   rƒ   r-   r-   r.   r}   S  s    zBackgroundSocket.sendc                 O   s    | j dusJ ‚| j j|i |¤ŽS )zSchedule send in IO threadN)rŒ   ri   r…   r-   r-   r.   ri   W  s    zBackgroundSocket.send_multipart)r‡   rˆ   r‰   rŠ   rŒ   r/   r’   r™   r}   ri   Ú__classcell__r-   r-   r—   r.   r   *  s   r   c                   @   sð   e Zd ZU dZdZdZdZdZdZe	e
 ed< dd„ Zd	d
„ Zd3dddœdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdd„ Zdd„ Zd d!„ Zee	e d"œd#d$„Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zed-d.„ ƒZ d/d0„ Z!d1d2„ Z"dS )4Ú	OutStreamzpA file like object that publishes the stream to a 0MQ PUB socket.

    Output is handed off to an IO Thread
    r   gš™™™™™É?NzUTF-8Ú_excc                 C   s(   t | ddƒdur| jS d}t |¡‚dS )z^
        Things like subprocess will peak and write to the fileno() of stderr/stdout.
        Ú_original_stdstream_copyNÚfileno)r•   rŸ   ÚioÚUnsupportedOperationrj   r-   r-   r.   r    l  s    zOutStream.filenoc                 C   sn   zLt  | jt¡}|rJ| jrJ|  |jdd�¡ t  | j|¡ t  | jt¡}qW n tyh   t	 
¡ | _Y n0 dS )aj  
        We've redirected standards streams 0 and 1 into a pipe.

        We need to watch in a thread and redirect them to the right places.

        1) the ZMQ channels to show in notebook interfaces,
        2) the original stdout/err, to capture errors in terminals.

        We cannot schedule this on the ioloop thread, as this might be blocking.

        Úreplace)ÚerrorsN)r   ÚreadÚ_fidÚPIPE_BUFFER_SIZEÚ_should_watchÚwriterA   rŸ   rM   rO   Úexc_inforž   )r+   Zbtsr-   r-   r.   Ú_watch_pipe_fdv  s    
zOutStream._watch_pipe_fdTF)ÚwatchfdÚisattyc          
      C   s†  |durt jdtdd� || _t|tƒsLt jd| tdd� t|ƒ}| ¡  || _|| _d| 	¡  | _
i | _t ¡ | _d| _d| _|j| _t ¡ | _tƒ | _d| _t|ƒ| _d| _tƒ | _|ràtj  d¡sÖtj  d	¡ràd
tj!vsè|dkrød| _|  "|¡ |�r‚t#|dƒ�rvt#|dƒ�rv| j�rnz| $¡ }W n t%�yB   d}Y n0 |du�rn|| j&k�rnt' (t' )| j*d¡¡}|| _nd}	t+|	ƒ‚dS )aœ  
        Parameters
        ----------
        session : object
            the session object
        pub_thread : threading.Thread
            the publication thread
        name : str {'stderr', 'stdout'}
            the name of the standard stream to replace
        pipe : object
            the pip object
        echo : bool
            whether to echo output
        watchfd : bool (default, True)
            Watch the file descriptor corresponding to the replaced stream.
            This is useful if you know some underlying code will write directly
            the file descriptor by its number. It will spawn a watching thread,
            that will swap the give file descriptor for a pipe, read from the
            pipe, and insert this into the current Stream.
        isatty : bool (default, False)
            Indication of whether this stream has terminal capabilities (e.g. can handle colors)

        NzKpipe argument to OutStream is deprecated and ignored since ipykernel 4.2.3.r`   ra   zISince IPykernel 4.3, OutStream should be created with IOPubThread, not %rs   stream.FÚlinuxÚdarwinZPYTEST_CURRENT_TESTÚforceTr¥   r©   Úwz(echo argument must be a file-like object),re   rf   r”   ÚsessionÚ
isinstancer   r8   Ú
pub_threadr   ÚencodeÚtopicÚparent_headerr   r   r   Ú_flush_pendingÚ_subprocess_flush_pendingr   Ú_io_loopr   ÚRLockÚ_buffer_lockr   Ú_bufferÚechoÚboolÚ_isattyr¨   r   r   rO   Úplatformr�   ÚenvironÚ_setup_stream_redirectsr“   r    rM   Ú_original_stdstream_fdr¡   ÚTextIOWrapperÚFileIOrŸ   Ú
ValueError)
r+   r²   r´   r   r,   r¾   r¬   r­   Zecho_fdr^   r-   r-   r.   r/   Œ  sx    "ý
ÿü


ÿ
ý
ýûø


þÿzOutStream.__init__c                 C   s   | j S )zpReturn a bool indicating whether this is an 'interactive' stream.

        Returns:
            Boolean
        )rÀ   r4   r-   r-   r.   r­   ô  s    zOutStream.isattyc                 C   sj   t  ¡ \}}tt|ƒ ¡  }| _t  |¡| _t  ||¡ || _	d | _
tj| jd�| _d| j_| j ¡  d S )N)r   T)r   r,   r•   rO   r    rÄ   ÚduprŸ   Údup2r¦   rž   r   r'   r«   Úwatch_fd_threadr*   r8   )r+   r   ÚprÚpwZfnor-   r-   r.   rÃ   ü  s    z!OutStream._setup_stream_redirectsc                 C   s   t  ¡ | jkS r0   rn   r4   r-   r-   r.   rh   	  s    zOutStream._is_master_processc                 C   s   t |ƒ| _dS )zSet the parent header.N)r   r·   )r+   Úparentr-   r-   r.   Ú
set_parent  s    zOutStream.set_parentc                 C   sj   | j r@d| _ t | jd¡ | j ¡  t | j| j¡ t | j¡ | j	r`| j	\}}}t
 |||¡ d| _dS )zClose the stream.Fó    N)r¨   r   r©   rÄ   rÊ   rw   rÉ   rŸ   r9   rž   Ú	tracebackÚprint_exceptionr´   )r+   Úetyper›   Útbr-   r-   r.   r9     s    
zOutStream.closec                 C   s
   | j d u S r0   )r´   r4   r-   r-   r.   rz      s    zOutStream.closedc                    s,   ˆ j r
dS dˆ _ ‡ fdd„}ˆ j |¡ dS )zuschedule a flush in the IO thread

        call this on write, to indicate that flush should be called soon.
        NTc                      s   ˆ j  ˆ jˆ j¡ d S r0   )rº   Ú
call_laterÚflush_intervalÚ_flushr-   r4   r-   r.   Ú_schedule_in_thread.  s    z6OutStream._schedule_flush.<locals>._schedule_in_thread)r¸   r´   r   )r+   r×   r-   r4   r.   Ú_schedule_flush$  s
    zOutStream._schedule_flushc                 C   s~   | j rr| j jdurr| j j ¡ rr| j jjt ¡ jkrr| j  | j¡ t ¡ }| j  |j	¡ | 
| j¡sztdtjd� n|  ¡  dS )zStrigger actual zmq send

        send will happen in the background thread
        NzIOStream.flush timed outrI   )r´   r)   rT   Úidentr   rY   r   rÖ   ÚEventÚsetÚwaitÚflush_timeoutrN   rO   rP   )r+   Zevtr-   r-   r.   Úflush3  s    ÿ
þ
ýüzOutStream.flushc              
   C   sÞ   d| _ d| _| jdurjz| j ¡  W nD tyh } z,| jtjurTtd|› �tjd� W Y d}~n
d}~0 0 |  ¡ }|rÚt	 
¡ | j_| j|dœ}| jjd|| jd�}| jD ]}||ƒ}|du r¨ dS q¨| jj| j|| jd� dS )	z³This is where the actual send happens.

        _flush should generally be called in the IO thread,
        unless the thread has been destroyed (e.g. forked subprocess).
        FNzFlush failed: rI   )r   ÚtextÚstream)rÍ   )rÙ   )r¸   r¹   r¾   rÞ   ÚOSErrorrO   rP   rN   Ú_flush_bufferr   r   r²   Úpidr   r^   r·   Ú_hooksr}   r´   r¶   )r+   rQ   ÚdataÚcontentr^   Úhookr-   r-   r.   rÖ   K  s,    
*
ýzOutStream._flush)ÚstringÚreturnc              
   C   s  t |tƒs dt|ƒ› �}t|ƒ‚| jdur€z| j |¡ W nD ty~ } z,| jtjurjt	d|› �tjd� W Y d}~n
d}~0 0 | j
du r˜d}t|ƒ‚nh|  ¡  }| j� | j |¡ W d  ƒ n1 sÊ0    Y  |rø| jrâdS d| _| j
 | j¡ n|  ¡  t|ƒS )z´Write to current stream after encoding if necessary

        Returns
        -------
        len : int
            number of items from input parameter written to stream.

        z"write() argument must be str, not NzWrite failed: rI   úI/O operation on closed fileT)r³   ÚstrÚtypeÚ	TypeErrorr¾   r©   rá   rO   rP   rN   r´   rÇ   rh   r¼   r½   r¹   r   rÖ   rØ   r[   )r+   rè   r^   rQ   Zis_childr-   r-   r.   r©   r  s,    


*


*zOutStream.writec                 C   s0   | j du rd}t|ƒ‚n|D ]}|  |¡ qdS )zWrite lines to the stream.Nrê   )r´   rÇ   r©   )r+   Úsequencer^   rè   r-   r-   r.   Ú
writelinesœ  s
    

zOutStream.writelinesc                 C   s   dS )z$Test whether the stream is writable.Tr-   r4   r-   r-   r.   Úwritable¥  s    zOutStream.writablec                 C   s   |   ¡ }| ¡ }| ¡  |S )z<clear the current buffer and return the current buffer data.)Ú_rotate_bufferÚgetvaluer9   )r+   Úbufrå   r-   r-   r.   râ   ©  s    zOutStream._flush_bufferc                 C   s8   | j � | j}tƒ | _W d  ƒ n1 s*0    Y  |S )z@Returns the current buffer and replaces it with an empty buffer.N)r¼   r½   r   )r+   Z
old_bufferr-   r-   r.   rñ   °  s    &zOutStream._rotate_bufferc                 C   s   t | jdƒsg | j_| jjS )NÚhooks)r“   r   rô   r4   r-   r-   r.   rä   ·  s    zOutStream._hooksc                 C   s   | j  |¡ dS )aî  
        Registers a hook with the thread-local storage.

        Parameters
        ----------
        hook : Any callable object

        Returns
        -------
        Either a publishable message, or `None`.
        The hook callable must return a message from
        the __call__ method if they still require the
        `session.send` method to be called after transformation.
        Returning `None` will halt that execution path, and
        session.send will not be called.
        N)rä   r|   ©r+   rç   r-   r-   r.   Úregister_hook¾  s    zOutStream.register_hookc                 C   s,   z| j  |¡ W dS  ty&   Y dS 0 dS )aB  
        Un-registers a hook with the thread-local storage.

        Parameters
        ----------
        hook : Any callable object which has previously been
            registered as a hook.

        Returns
        -------
        bool - `True` if the hook was removed, `False` if it wasn't
            found.
        TFN)rä   ÚremoverÇ   rõ   r-   r-   r.   Úunregister_hookÑ  s
    zOutStream.unregister_hook)NN)#r‡   rˆ   r‰   rŠ   rÝ   rÕ   r¶   Úencodingrž   r   r   Ú__annotations__r    r«   r/   r­   rÃ   rh   rÎ   r9   r‹   rz   rØ   rÞ   rÖ   rë   Úintr©   rï   rð   râ   rñ   rä   rö   rø   r-   r-   r-   r.   r�   ]  sB   

  ú÷h
'*	
r�   )#rŠ   r1   rr   r¡   r   rO   r   rÐ   re   Úbinasciir   Úcollectionsr   r   r   r   Útypingr   r   r	   r
   r   r=   Zjupyter_client.sessionr   Ztornado.ioloopr   Zzmq.eventloop.zmqstreamr   ro   rp   r§   r   r   r�   r-   r-   r-   r.   Ú<module>   s2     3