U
    pdb3                     @   s   d Z d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 ejZejZejZejZejd ZG d	d
 d
ZeZdd Zdd Zdd Zdd Zdd Zdd ZddddZzddlZW n ek
r   Y nX ej ZZdS )z.A Future class similar to the one in PEP 3148.)Futurewrap_futureisfuture    N   )base_futures)events)
exceptions)format_helpersc                   @   s   e Zd ZdZeZdZdZdZdZ	dZ
dZddddZejZdd Zd	d
 Zedd Zej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ddZdd  Zd!d" Zd#d$ Zd%d& Z e Z!dS )'r   a,  This class is *almost* compatible with concurrent.futures.Future.

    Differences:

    - This class is not thread-safe.

    - result() and exception() do not take a timeout argument and
      raise an exception when the future isn't done yet.

    - Callbacks registered with add_done_callback() are always called
      via the event loop's call_soon().

    - This class is not compatible with the wait() and as_completed()
      methods in the concurrent.futures package.

    (In Python 3.4 or later we may be able to unify the implementations.)
    NFloopc                C   s@   |dkrt  | _n|| _g | _| j r<ttd| _	dS )zInitialize the future.

        The optional event_loop argument allows explicitly setting the event
        loop object used by the future. If it's not provided, the future uses
        the default event loop.
        Nr   )
r   get_event_loop_loop
_callbacksZ	get_debugr	   extract_stacksys	_getframe_source_tracebackselfr    r   %/usr/lib/python3.8/asyncio/futures.py__init__D   s    
zFuture.__init__c                 C   s   d | jjd|  S )Nz<{} {}> )format	__class____name__join
_repr_infor   r   r   r   __repr__V   s    
zFuture.__repr__c                 C   sF   | j s
d S | j}| jj d|| d}| jr6| j|d< | j| d S )Nz exception was never retrieved)message	exceptionfutureZsource_traceback)_Future__log_traceback
_exceptionr   r   r   r   Zcall_exception_handler)r   exccontextr   r   r   __del__Z   s    
zFuture.__del__c                 C   s   | j S N)r#   r   r   r   r   _log_tracebackj   s    zFuture._log_tracebackc                 C   s   t |rtdd| _d S )Nz'_log_traceback can only be set to FalseF)bool
ValueErrorr#   )r   valr   r   r   r)   n   s    c                 C   s   | j }|dkrtd|S )z-Return the event loop the Future is bound to.Nz!Future object is not initialized.)r   RuntimeErrorr   r   r   r   get_loopt   s    zFuture.get_loopc                 C   s&   d| _ | jtkrdS t| _|   dS )zCancel the future and schedule callbacks.

        If the future is already done or cancelled, return False.  Otherwise,
        change the future's state to cancelled, schedule the callbacks and
        return True.
        FT)r#   _state_PENDING
_CANCELLED_Future__schedule_callbacksr   r   r   r   cancel{   s    
zFuture.cancelc                 C   sH   | j dd }|sdS g | j dd< |D ]\}}| jj|| |d q(dS )zInternal: Ask the event loop to call all callbacks.

        The callbacks are scheduled to be called as soon as possible. Also
        clears the callback list.
        Nr&   )r   r   	call_soon)r   Z	callbackscallbackctxr   r   r   Z__schedule_callbacks   s    zFuture.__schedule_callbacksc                 C   s
   | j tkS )z(Return True if the future was cancelled.)r/   r1   r   r   r   r   	cancelled   s    zFuture.cancelledc                 C   s
   | j tkS )zReturn True if the future is done.

        Done means either that a result / exception are available, or that the
        future was cancelled.
        )r/   r0   r   r   r   r   done   s    zFuture.donec                 C   s@   | j tkrtj| j tkr$tdd| _| jdk	r:| j| jS )a  Return the result this future represents.

        If the future has been cancelled, raises CancelledError.  If the
        future's result isn't yet available, raises InvalidStateError.  If
        the future is done and has an exception set, this exception is raised.
        zResult is not ready.FN)	r/   r1   r   CancelledError	_FINISHEDInvalidStateErrorr#   r$   _resultr   r   r   r   result   s    



zFuture.resultc                 C   s0   | j tkrtj| j tkr$tdd| _| jS )a&  Return the exception that was set on this future.

        The exception (or None if no exception was set) is returned only if
        the future is done.  If the future has been cancelled, raises
        CancelledError.  If the future isn't done yet, raises
        InvalidStateError.
        zException is not set.F)r/   r1   r   r:   r;   r<   r#   r$   r   r   r   r   r!      s    


zFuture.exceptionr4   c                C   sB   | j tkr| jj|| |d n |dkr.t }| j||f dS )zAdd a callback to be run when the future becomes done.

        The callback is called with a single argument - the future object. If
        the future is already done when this is called, the callback is
        scheduled with call_soon.
        r4   N)r/   r0   r   r5   contextvarsZcopy_contextr   append)r   fnr&   r   r   r   add_done_callback   s
    
zFuture.add_done_callbackc                    s<    fdd| j D }t| j t| }|r8|| j dd< |S )z}Remove all instances of a callback from the "call when done" list.

        Returns the number of callbacks removed.
        c                    s    g | ]\}}| kr||fqS r   r   ).0fr7   rA   r   r   
<listcomp>   s   z/Future.remove_done_callback.<locals>.<listcomp>N)r   len)r   rA   Zfiltered_callbacksZremoved_countr   rE   r   remove_done_callback   s    
zFuture.remove_done_callbackc                 C   s8   | j tkr t| j  d| || _t| _ |   dS )zMark the future done and set its result.

        If the future is already done when this method is called, raises
        InvalidStateError.
        : N)r/   r0   r   r<   r=   r;   r2   )r   r>   r   r   r   
set_result   s
    
zFuture.set_resultc                 C   sb   | j tkr t| j  d| t|tr0| }t|tkrDtd|| _t	| _ | 
  d| _dS )zMark the future done and set an exception.

        If the future is already done when this method is called, raises
        InvalidStateError.
        rI   zPStopIteration interacts badly with generators and cannot be raised into a FutureTN)r/   r0   r   r<   
isinstancetypeStopIteration	TypeErrorr$   r;   r2   r#   )r   r!   r   r   r   set_exception   s    

zFuture.set_exceptionc                 c   s,   |   sd| _| V  |   s$td|  S )NTzawait wasn't used with future)r9   _asyncio_future_blockingr-   r>   r   r   r   r   	__await__  s    zFuture.__await__)"r   
__module____qualname____doc__r0   r/   r=   r$   r   r   rP   r#   r   r   Z_future_repr_infor   r   r'   propertyr)   setterr.   r3   r2   r8   r9   r>   r!   rB   rH   rJ   rO   rQ   __iter__r   r   r   r   r      s:   

r   c                 C   s,   z
| j }W n tk
r   Y nX | S | jS r(   )r.   AttributeErrorr   )futr.   r   r   r   	_get_loop  s    
rZ   c                 C   s   |   rdS | | dS )z?Helper setting the result only if the future was not cancelled.N)r8   rJ   )rY   r>   r   r   r   _set_result_unless_cancelled  s    r[   c                 C   sX   t | }|tjjkr tj| j S |tjjkr8tj| j S |tjjkrPtj| j S | S d S r(   )rL   
concurrentfuturesr:   r   argsTimeoutErrorr<   )r%   Z	exc_classr   r   r   _convert_future_exc#  s    r`   c                 C   s^   |  st| r|   |  s(dS | }|dk	rH| t| n| }| 	| dS )z8Copy state from a future to a concurrent.futures.Future.N)
r9   AssertionErrorr8   r3   Zset_running_or_notify_cancelr!   rO   r`   r>   rJ   )r\   sourcer!   r>   r   r   r   _set_concurrent_future_state/  s    rc   c                 C   sl   |   st| rdS |  r$t|  r6|  n2|  }|dk	rV|t| n|  }|| dS )zqInternal helper to copy state from another Future.

    The other Future may be a concurrent.futures.Future.
    N)	r9   ra   r8   r3   r!   rO   r`   r>   rJ   )rb   destr!   r>   r   r   r   _copy_future_state>  s    
re   c                    s   t sttjjstdt s<ttjjs<tdt rLtndt r`tnddd  fdd} fdd	}| | dS )
a  Chain two futures so that when one completes, so does the other.

    The result (or exception) of source will be copied to destination.
    If destination is cancelled, source gets cancelled too.
    Compatible with both asyncio.Future and concurrent.futures.Future.
    z(A future is required for source argumentz-A future is required for destination argumentNc                 S   s"   t | rt||  n
t| | d S r(   )r   re   rc   )r"   otherr   r   r   
_set_stateb  s    z!_chain_future.<locals>._set_statec                    s2   |   r.d ks kr"  nj d S r(   )r8   r3   call_soon_threadsafe)destination)	dest_looprb   source_loopr   r   _call_check_cancelh  s    
z)_chain_future.<locals>._call_check_cancelc                    sJ     rd k	r rd S d ks,kr8 |  n |  d S r(   )r8   Z	is_closedrh   )rb   )rg   rj   ri   rk   r   r   _call_set_stateo  s    z&_chain_future.<locals>._call_set_state)r   rK   r\   r]   r   rN   rZ   rB   )rb   ri   rl   rm   r   )rg   rj   ri   rb   rk   r   _chain_futureR  s    	
rn   r
   c                C   sN   t | r| S t| tjjs(td| |dkr8t }| }t	| | |S )z&Wrap concurrent.futures.Future object.z+concurrent.futures.Future is expected, got N)
r   rK   r\   r]   r   ra   r   r   Zcreate_futurern   )r"   r   Z
new_futurer   r   r   r   |  s    
r   )rT   __all__Zconcurrent.futuresr\   r?   Zloggingr    r   r   r   r	   r   r0   r1   r;   DEBUGZSTACK_DEBUGr   Z	_PyFuturerZ   r[   r`   rc   re   rn   r   Z_asyncioImportErrorZ_CFuturer   r   r   r   <module>   s:   
 q*