B
    't\                 @   sp   d Z ddlmZmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
Z
dd	gZG d
d	 d	eZG dd deZdS )z9
Future implementation for the prompt_toolkit eventloop.
    )unicode_literalsprint_function   )	EventLoop)get_context_idcontext)get_event_loopNFutureInvalidStateErrorc               @   s   e Zd ZdZdS )r
   z- The operation is not allowed in this state. N)__name__
__module____qualname____doc__ r   r   >lib/python3.7/site-packages/prompt_toolkit/eventloop/future.pyr
      s   c               @   s   e Zd ZdZd ddZejdkr(dd Ze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ed!ddZdd ZeZdS )"r	   z
    `Future` object for use with the prompt_toolkit event loops.  (Not by
    accident very similar to asyncio -- but much more limited in functionality.
    They are however not meant to be used interchangeable.)
    Nc             C   sL   |d kst |tst|pt | _g | _d | _d | _d| _d| _	t
 | _d S )NF)
isinstancer   AssertionErrorr   loopdone_callbacks_result
_exception_done_retrieved_resultr   _ctx_id)selfr   r   r   r   __init__   s    zFuture.__init__)      c             C   s6   | j r2| js2| j }d| jj || d}| j| d S )Nz %s exception was never retrieved)message	exceptionZfuture)r   r   	__class__r   r   Zcall_exception_handler)r   excr   r   r   r   __del__/   s    zFuture.__del__c             C   s   |  }| | |S )z
        Returns a Future for which the result has been set to the given result.
        Similar to Twisted's `Deferred.succeed()`.
        )
set_result)clsresultfr   r   r   succeed:   s    
zFuture.succeedc             C   s   |  }| | |S )z
        Returns a Future for which the error has been set to the given result.
        Similar to Twisted's `Deferred.fail()`.
        )set_exception)r$   r%   r&   r   r   r   failD   s    
zFuture.failc                s0   j   jr, fdd}j| dS )z
        Add a callback to be run when the future becomes done.  (This
        callback will be called with one argument only: this future
        object.)
        c                  s   d_   d S )NT)r   r   )callbackr   r   r   call_cbX   s    z)Future.add_done_callback.<locals>.call_cbN)r   appendr   r   call_from_executor)r   r*   r+   r   )r*   r   r   add_done_callbackN   s    zFuture.add_done_callbackc             C   s&   | j rtd|| _d| _ |   dS )z* Mark the future done and set its result. z#Future result has been set already.TN)r   r
   r   _call_callbacks)r   r%   r   r   r   r#   ^   s
    zFuture.set_resultc             C   s.   | j rtd|| _d| _ | jr*|   n dS )z, Mark the future done and set an exception. z#Future result has been set already.TN)r   r
   r   r   r/   )r   r   r   r   r   r(   g   s    
	zFuture.set_exceptionc                s6   j d d    rd_ fdd}j| d S )NTc           	      s0   t j x D ]} |  qW W d Q R X d S )N)r   r   )cb)r   r   r   r   call_them_all   s    
z-Future._call_callbacks.<locals>.call_them_all)r   r   r   r-   )r   r1   r   )r   r   r   r/   {   s
    zFuture._call_callbacksc             C   s(   | j s
td| _| jr| jn| jS dS )z+ Return the result this future represents. TN)r   r
   r   r   r   )r   r   r   r   r%      s    zFuture.resultc             C   s   | j s
td| _| jS )z3 Return the exception that was set on this future. T)r   r
   r   r   )r   r   r   r   r      s    zFuture.exceptionc             C   s   | j S )z
        Return True if the future is done. Done means either that a result /
        exception are available, or that the future was cancelled.
        )r   )r   r   r   r   done   s    zFuture.donec                s(   ddl m} |  | j fdd} S )zE
        Turn this `Future` into an asyncio `Future` object.
        r   )r	   c                s*   |   r |    n |   d S )N)r   r(   r#   r%   )r&   )	asyncio_fr   r   _   s    z#Future.to_asyncio_future.<locals>._)Zasyncior	   r.   )r   r	   r4   r   )r3   r   to_asyncio_future   s    zFuture.to_asyncio_futurec                s    | |d |j  fdd} S )zQ
        Return a prompt_toolkit `Future` from the given asyncio Future.
        )r   c                s*   |   r |    n |   d S )N)r   r(   r#   r%   )r3   )r&   r   r   r4      s    z%Future.from_asyncio_future.<locals>._)r.   )r$   r3   r   r4   r   )r&   r   from_asyncio_future   s    
zFuture.from_asyncio_futurec             C   s   |    S )z! For compatibility with asyncio. )r5   __iter__)r   r   r   r   r7      s    zFuture.__iter__)N)N)r   r   r   r   r   sysversion_infor"   classmethodr'   r)   r.   r#   r(   r/   r%   r   r2   r5   r6   r7   	__await__r   r   r   r   r	      s$   



	)r   Z
__future__r   r   baser   r   r   defaultsr   r8   __all__	Exceptionr
   objectr	   r   r   r   r   <module>   s   