
    X-Ph{.                    \   U 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ZddlZddl	m
Z
 ddlmZmZ ej        dk    r/ddlZddlZeZej        j        Zej        Zded<   ej        Zded	<   nddlZej        Z G d
 de          Z G d d          Z G d de          Z G d de          ZdS )zCross platform abstractions for inter-process communication

On Unix, this uses AF_UNIX sockets.
On Windows, this uses NamedPipes.
    )annotationsN)TracebackType)CallableFinalwin32zCallable[[_IPCHandle], int]DisconnectNamedPipeFlushFileBuffersc                      e Zd ZdZdS )IPCExceptionzException for IPC issues.N)__name__
__module____qualname____doc__     H/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypy/ipc.pyr   r   "   s        ####r   r   c                  H    e Zd ZU dZded<   dd
ZddZdddZddZddZ	dS )IPCBasea  Base class for communication between the dmypy client and server.

    This contains logic shared between the client and server, such as reading
    and writing.
    We want to be able to send multiple "messages" over a single connection and
    to be able to separate the messages. We do this by encoding the messages
    in an alphabet that does not contain spaces, then adding a space for
    separation. The last framed message is also followed by a space.
    
_IPCHandle
connectionnamestrtimeoutfloat | NonereturnNonec                H    || _         || _        t                      | _        d S N)r   r   	bytearraybuffer)selfr   r   s      r   __init__zIPCBase.__init__3   s    	kkr   bytearray | Nonec                    | j                             d          }|dk    rdS | j         d|         }| j         |dz   d         | _         |S )z9Return a full frame from the bytes we have in the buffer.    N   )r    find)r!   	space_posbdatas      r   frame_from_bufferzIPCBase.frame_from_buffer8   sP    K$$T**	??4JYJ'k)a-//2r   順 sizeintc                   t                      }t          j        dk    rm	 |                                 }|nSt	          j        | j        |d          \  }}	 |t          j        k    rf| j        rt          | j        dz            nt          j
        }t	          j        |j        |          }|t          j        k    rt          d|           n## t          $ r |                                  w xY w|                    d          \  }}|                                }|r1| j                            |           |                                 }|n:|dk    rn3|t          j        k    rK|t          j        k    rt          d          knP	 |                                 }|n8| j                            |          }|sn| j                            |           O|sd	S t1          j        |d
                              d          S )z=Read bytes from an IPC connection until we have a full frame.r   TN
overlapped  Bad result from I/O wait: r   zReadFile operation aborted. base64utf8)r   sysplatformr+   _winapiReadFiler   ERROR_IO_PENDINGr   r.   INFINITEWaitForSingleObjecteventWAIT_OBJECT_0r   BaseExceptioncancelGetOverlappedResult	getbufferr    extendERROR_MORE_DATAERROR_OPERATION_ABORTEDrecvcodecsdecode)	r!   r-   r*   overrr   res_mores	            r   readzIPCBase.readB   s%   "+++<7"" F ..00$ "*4?DTRRRCg666>Bl"`#dlT&9":":":PWP`%9"(GLL'"777"./QC/Q/Q"R"RR$   IIKKK //553||~~ K&&t,,, 2244E(!88G333G;;;&'DEEEA FD) ..00$ ++D11 ""4((()  	 2}UH--44V<<<s   A6C  C0datac                ,   t          j        |                    d          d          dz   }t          j        dk    r=	 t	          j        | j        |d          \  }}	 |t          j        k    rg| j        rt          | j        dz            nt          j
        }t	          j        |j        |          }|t          j        k    rt          d|           n|d	k    rt          d
|           n## t          $ r |                                  w xY w|                    d          \  }}|d	k    s
J |            |t%          |          k    sJ dS # t&          $ r}t          d|j                   |d}~ww xY w| j                            |           dS )zWrite to an IPC connection.r6   r5   r%   r   Tr0   r2   r3   r   z#Failed writing to pipe with error: zFailed to write with error: N)rH   encoder7   r8   r9   	WriteFiler   r;   r   r.   r<   r=   r>   r?   r   r@   rA   rB   lenOSErrorwinerrorsendall)	r!   rP   encoded_datarJ   rK   r   rL   bytes_writtenes	            r   writezIPCBase.write|   s    }T[[%8%8(CCdJ<7""W!+DO\VZ[[[C
g666>Bl"`#dlT&9":":":PWP`%9"(GLL'"777"./QC/Q/Q"R"RR 8*+VQT+V+VWWW$   IIKKK &(%;%;D%A%A"saxxxxxx$L(9(9999999 W W W"#N!*#N#NOOUVVW O##L11111s1   E BC. -E . DA E 
E7E22E7c                    t           j        dk    r2| j        t          j        k    rt          j        | j                   d S d S | j                                         d S Nr   )r7   r8   r   r9   NULLCloseHandlecloser!   s    r   r`   zIPCBase.close   s[    <7""',..#DO44444 /. O!!#####r   Nr   r   r   r   r   r   )r   r#   )r,   )r-   r.   r   r   )rP   r   r   r   r   r   )
r   r   r   r   __annotations__r"   r+   rO   r[   r`   r   r   r   r   r   &   s           " " " "
   8= 8= 8= 8= 8=t2 2 2 28$ $ $ $ $ $r   r   c                  <     e Zd ZdZd fdZdd	Z	 	 	 dddZ xZS )	IPCClientz%The client side of an IPC connection.r   r   r   r   r   r   c           	        t                                          ||           t          j        dk    ri| j        rt          | j        dz            nt          j        }	 t          j        | j	        |           n^# t          $ r}t          d| j	         d          |d }~wt          $ r+}|j        t          j        k    rt          d          | d }~ww xY w	 t          j        | j	        t          j        t          j        z  dt          j        t          j        t          j        t          j                  | _        n8# t          $ r+}|j        t          j        k    rt          d          | d }~ww xY wt          j        | j        t          j        d d            d S t3          j        t2          j                  | _        | j                            |           | j                            |           d S )Nr   r2   zThe NamedPipe at z was not found.z!Timed out waiting for connection.r   zThe connection is busy.)superr"   r7   r8   r   r.   r9   NMPWAIT_WAIT_FOREVERWaitNamedPiper   FileNotFoundErrorr   rU   rV   ERROR_SEM_TIMEOUT
CreateFileGENERIC_READGENERIC_WRITEr^   OPEN_EXISTINGFILE_FLAG_OVERLAPPEDr   ERROR_PIPE_BUSYSetNamedPipeHandleStatePIPE_READMODE_MESSAGEsocketAF_UNIX
settimeoutconnect)r!   r   r   rZ   	__class__s       r   r"   zIPCClient.__init__   s   w'''<7""26,`c$,-...GD`G%di9999$ Z Z Z"#Qty#Q#Q#QRRXYY   :!:::&'JKKQRR	
")"4I(7+@@L)0L# #    :!888&'@AAqH	
 +!>d     %mFN;;DOO&&w///O##D)))))s=    A; ;
CBC+&CCA#D> >
E3&E..E3c                    | S r   r   ra   s    r   	__enter__zIPCClient.__enter__   s    r   Nexc_tytype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | Nonec                .    |                                   d S r   )r`   r!   r|   r~   r   s       r   __exit__zIPCClient.__exit__   s     	

r   rb   )r   rf   NNNr|   r}   r~   r   r   r   r   r   )r   r   r   r   r"   r{   r   __classcell__ry   s   @r   rf   rf      s|        //"* "* "* "* "* "*H   
 .2(,'+	        r   rf   c                  j     e Zd ZU dZded<   dd fdZddZ	 	 	 dddZddZe	dd            Z
 xZS )	IPCServeri   r   BUFFER_SIZENr   r   r   r   r   r   c           
     
   t           j        dk    rMd                    |t          j        t          j        d                                                              }n| d}t                      	                    ||           t           j        dk    rt          j        | j        t          j        t          j        z  t          j        z  t          j        t          j        z  t          j        z  dz  d| j        | j        t          j        d          | _        | j        dk    r%t          j                    }t/          d	|           d S t1          j                    | _        t
          j                            | j        | j                  }t;          j        t:          j                  | _        | j                             |           | j        !                    d           || j        "                    |           d S d S )
Nr   z\\.\pipe\{}-{}.pipe   z.sock   r'   r   r&   zInvalid handle to pipe: )#r7   r8   formatr5   urlsafe_b64encodeosurandomrI   rh   r"   r9   CreateNamedPiper   PIPE_ACCESS_DUPLEXFILE_FLAG_FIRST_PIPE_INSTANCErq   rt   PIPE_TYPE_MESSAGE	PIPE_WAITr   ri   r   GetLastErrorr   tempfilemkdtempsock_directorypathjoinru   rv   sockbindlistenrw   )r!   r   r   rK   sockfilery   s        r   r"   zIPCServer.__init__   s   <7"")00f.rz!}}==DDFF DD >>>Dw'''<7""%5	*78./ -+,#$    , DO "$$*,,"#Cc#C#CDDD %$ #+"2"4"4Dw||D$7CCHfn55DIINN8$$$IQ"	$$W----- #"r   c                   t           j        dk    r	 t          j        | j        d          }	 | j        rt          | j        dz            nt          j        }t          j        |j	        |          }|t          j
        k    sJ n<# t          $ r/ |                                 t          j        | j                    w xY w|                    d          \  }}|dk    sJ n# t          $ r*}|j        t          j        t          j        fvr Y d }~nRd }~ww xY w	 | j                                        \  | _        }n'# t*          j        $ r}t-          d          |d }~ww xY w| S )Nr   Tr0   r2   r   zThe socket timed out)r7   r8   r9   ConnectNamedPiper   r   r.   r<   r=   r>   r?   r@   rA   r_   rB   rU   rV   ERROR_PIPE_CONNECTEDERROR_NO_DATAr   acceptru   r   )r!   rJ   r   rL   rM   rK   rZ   s          r   r{   zIPCServer.__enter__   s   <7"" -do$OOO:>,\c$,"5666GL\G!5bhHHC'"777777$   IIKKK'888 //553axxxxx    :g&BGDY%ZZZ [ZZZZ B%)Y%5%5%7%7"> B B B"#9::ABs;   C# AB 9B?#
D- DD!D= =E!EE!r|   r}   r~   r   r   r   c                    t           j        dk    rT	 t          | j                  st	          d          	 t          | j                   d S # t          | j                   w xY w|                                  d S )Nr   z;Failed to flush NamedPipe buffer, maybe the client hung up?)r7   r8   r	   r   r   r   r`   r   s       r   r   zIPCServer.__exit__  s     <7""5'88 &U  
 $DO44444#DO4444JJLLLLLs   #A A"c                    t           j        dk    r|                                  d S t          j        | j                   d S r]   )r7   r8   r`   shutilrmtreer   ra   s    r   cleanupzIPCServer.cleanup(  s9    <7""JJLLLLLM$-.....r   c                   t           j        dk    r| j        S t           j        dk    r*t          j                            | j        | j                  S | j                                        }t          |t                    sJ |S )Nr   gnu0)r7   r8   r   r   r   r   r   r   getsockname
isinstancer   )r!   r   s     r   connection_namezIPCServer.connection_name.  so    <7""9\V## 7<< 3TY???9((**DdC(((((Kr   r   rb   )r   r   r   r   rc   )r   r   )r   r   r   r   rd   r"   r{   r   r   propertyr   r   r   s   @r   r   r      s         K". ". ". ". ". ". ".H   < .2(,'+	    $/ / / / 
 
 
 X
 
 
 
 
r   r   )r   
__future__r   r5   rH   r   r   r7   r   typesr   typingr   r   r8   r9   ctypesr.   r   windllkernel32r   rd   r	   ru   	Exceptionr   r   rf   r   r   r   r   <module>r      s     # " " " " "   				  



        " " " " " " " "<7NNNMMMJ}%H7?7SSSSS4<4MMMMMMMMMJ$ $ $ $ $9 $ $ $w$ w$ w$ w$ w$ w$ w$ w$t0 0 0 0 0 0 0 0ff f f f f f f f f fr   