o
    :ګe                     @   s  d dl m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 G dd deZG d	d
 d
eZG dd deZG dd deZG dd de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 Zd!Zd"Zd#Z d$Z!d%Z"d&Z#d'Z$d(Z%dS ))    )absolute_importN)Thread)
format_exc)
namedtuple)Futurec                       s"   e Zd ZdZd fdd	Z  ZS )ProcessExpiredz&Raised when process dies unexpectedly.r   c                    s   t t| | || _d S N)superr   __init__exitcode)selfmsgcode	__class__ d/vol/project/2023/60021/g236002117/DeepSeek-Coder/venv/lib/python3.10/site-packages/pebble/common.pyr
   !   s   
zProcessExpired.__init__)r   )__name__
__module____qualname____doc__r
   __classcell__r   r   r   r   r      s    r   c                   @      e Zd Zdd ZdS )PebbleFuturec                 C   s~   | j 2 | jtkr t| _| jD ]}||  q	 W d   dS | jtkr1t| _	 W d   dS td1 s8w   Y  dS )a  Mark the future as running or process any cancel notifications.

        Should only be used by Executor implementations and unit tests.

        If the future has been cancelled (cancel() was called and returned
        True) then any threads waiting on the future completing (though calls
        to as_completed() or wait()) are notified and False is returned.

        If the future was not cancelled then it is put in the running state
        (future calls to running() will return True) and True is returned.

        This method should be called by Executor implementations before
        executing the work associated with this future. If this method returns
        False then the work should not be executed.

        Returns:
            False if the Future was cancelled, True otherwise.

        Raises:
            RuntimeError: if set_result() or set_exception() was called.
        NFTzFuture in unexpected state)	
_condition_state	CANCELLEDCANCELLED_AND_NOTIFIED_waitersadd_cancelledPENDINGRUNNINGRuntimeError)r   waiterr   r   r   set_running_or_notify_cancel(   s   


z)PebbleFuture.set_running_or_notify_cancelN)r   r   r   r$   r   r   r   r   r   &   s    r   c                   @   r   )ProcessFuturec                 C   s~   | j . | jtkr	 W d   dS | jttfv r"	 W d   dS t| _| j   W d   n1 s4w   Y  |   dS )zCancel the future.

        Returns True if the future was cancelled, False otherwise. A future
        cannot be cancelled if it has already completed.
        NFT)r   r   FINISHEDr   r   
notify_all_invoke_callbacksr   r   r   r   cancelN   s   

zProcessFuture.cancelN)r   r   r   r*   r   r   r   r   r%   M   s    r%   c                   @       e Zd ZdZdd Zdd ZdS )RemoteTracebackzuTraceback wrapper for exceptions in remote process.

    Exception.__cause__ requires a BaseException subclass.

    c                 C   s
   || _ d S r   	traceback)r   r.   r   r   r   r
   j   s   
zRemoteTraceback.__init__c                 C   s   | j S r   r-   r)   r   r   r   __str__m   s   zRemoteTraceback.__str__N)r   r   r   r   r
   r/   r   r   r   r   r,   c   s    r,   c                   @   r+   )RemoteExceptionz2Pickling wrapper for exceptions in remote process.c                 C   s   || _ || _d S r   	exceptionr.   )r   r2   r.   r   r   r   r
   t   s   
zRemoteException.__init__c                 C   s   t | j| jffS r   )rebuild_exceptionr2   r.   r)   r   r   r   
__reduce__x   s   zRemoteException.__reduce__N)r   r   r   r   r
   r4   r   r   r   r   r0   q   s    r0   c                 C   s   t || _| S r   )r,   	__cause__r1   r   r   r   r3   |   s   
r3   c                 O   s"   t || ||d}||_|  |S N)targetnameargskwargs)r   daemonstart)r8   functionr;   r9   r:   threadr   r   r   launch_thread   s   r?   c                 O   s$   |j || ||d}||_|  |S r6   )Processr;   r<   )r8   r=   r;   
mp_contextr9   r:   processr   r   r   launch_process   s   rC   c                 C   sr   |    | d |  r+tjdkr+zt| jtj |   W n
 t	y*   Y dS w |  r7t
dt  dS )z"Does its best to stop the process.   ntNzUnable to terminate PID %d)	terminatejoinis_aliveosr8   killpidsignalSIGKILLOSErrorr"   getpid)rB   r   r   r   stop_process   s   
rP   c              
   O   sN   zt t| |i |W S  ty& } zt |_t t|W  Y d}~S d}~ww z;Runs the given function returning its results or exception.N)ResultSUCCESSBaseExceptionr   r.   FAILUREr=   r9   r:   errorr   r   r   execute   s   rX   c              
   O   sV   zt t| |i |W S  ty* } zt |_t tt||jW  Y d}~S d}~ww rQ   )rR   rS   rT   r   r.   rU   r0   rV   r   r   r   process_execute   s   rY   c              
   C   s^   z|  | W dS  tjtfy. } zt |_|  ttt||j W Y d}~dS d}~ww )z7Send result handling pickling and communication errors.N)	sendpicklePicklingError	TypeErrorr   r.   rR   ERRORr0   )pipedatarW   r   r   r   send_result   s   &ra   rR   )statusvalue      g?r    r!   r&   r   r   )&
__future__r   rI   r[   rL   	threadingr   r.   r   collectionsr   concurrent.futuresr   rN   r   r   r%   	Exceptionr,   objectr0   r3   r?   rC   rP   rX   rY   ra   rR   rS   rU   r^   
SLEEP_UNITr    r!   r&   r   r   r   r   r   r   <module>   s<   '			
	