Your IP : 18.216.150.124
�
j f�?�@s?dZdddddgZddlZddlZddlZddlZddlZdd lm Z dd
lm
Z
dZdZd
Z
ejjjZejjZejjZejdZGdd�de�ZGdd�d�ZGdd�d�Zdd�Zdd�Zdd�Zdd�Zdddd�ZdS)z.A Future class similar to the one in PEP 3148.�CancelledError�TimeoutError�InvalidStateError�Future�wrap_future�N�)�compat)�eventsZPENDINGZ CANCELLEDZFINISHEDc@seZdZdZdS)rz+The operation is not allowed in this state.N)�__name__�
__module__�__qualname__�__doc__�rr�4/opt/alt/python34/lib64/python3.4/asyncio/futures.pyrsc@sLeZdZdZdZdd�Zdd �Zd
d�Zdd
�ZdS)�_TracebackLoggera
Helper to log a traceback upon destruction if not cleared.
This solves a nasty problem with Futures and Tasks that have an
exception set: if nobody asks for the exception, the exception is
never logged. This violates the Zen of Python: 'Errors should
never pass silently. Unless explicitly silenced.'
However, we don't want to log the exception as soon as
set_exception() is called: if the calling code is written
properly, it will get the exception and handle it properly. But
we *do* want to log it if result() or exception() was never called
-- otherwise developers waste a lot of time wondering why their
buggy code fails silently.
An earlier attempt added a __del__() method to the Future class
itself, but this backfired because the presence of __del__()
prevents garbage collection from breaking cycles. A way out of
this catch-22 is to avoid having a __del__() method on the Future
class itself, but instead to have a reference to a helper object
with a __del__() method that logs the traceback, where we ensure
that the helper object doesn't participate in cycles, and only the
Future has a reference to it.
The helper object is added when set_exception() is called. When
the Future is collected, and the helper is present, the helper
object is also collected, and its __del__() method will log the
traceback. When the Future's result() or exception() method is
called (and a helper object is present), it removes the helper
object, after calling its clear() method to prevent it from
logging.
One downside is that we do a fair amount of work to extract the
traceback from the exception, even when it is never logged. It
would seem cheaper to just store the exception object, but that
references the traceback, which references stack frames, which may
reference the Future, which references the _TracebackLogger, and
then the _TracebackLogger would be included in a cycle, which is
what we're trying to avoid! As an optimization, we don't
immediately format the exception; we only do the work when
activate() is called, which call is delayed until after all the
Future's callbacks have run. Since usually a Future has at least
one callback (typically set by 'yield from') and usually that
callback extracts the callback, thereby removing the need to
format the exception.
PS. I don't claim credit for this solution. I first heard of it
in a discussion about closing files when they are collected.
�loop�source_traceback�exc�tbcCs.|j|_|j|_||_d|_dS)N)�_loopr�_source_tracebackrrr)�self�futurerrrr�__init__Us z_TracebackLogger.__init__cCsC|j}|dk r?d|_tj|j||j�|_ndS)N)r� traceback�format_exception� __class__�
__traceback__r)rrrrr�activate[s
z_TracebackLogger.activatecCsd|_d|_dS)N)rr)rrrr�clearbs z_TracebackLogger.clearcCs�|jr�d}|jrTdjtj|j��}|d7}|d|j�7}n|dj|j�j�7}|jji|d6�ndS)Nz*Future/Task exception was never retrieved
�z0Future/Task created at (most recent call last):
z%s
�message)rr�joinr�format_list�rstripr�call_exception_handler)r�msg�srcrrr�__del__fs
z_TracebackLogger.__del__N)zloopzsource_tracebackzexcztb) r
rrr
� __slots__rrrr(rrrrr!s0rc@s*eZdZdZeZdZdZdZdZ dZ
dZdZdddd�Z
dd�Zd d
�Zdd�Zejr�d
d�Zndd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zejr&eZ ndS)%raThis class is *almost* compatible with concurrent.futures.Future.
Differences:
- 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_threadsafe().
- 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.)
NFrcCsa|dkrtj�|_n ||_g|_|jj�r]tjtjd��|_ ndS)z�Initialize the future.
The optional event_loop argument allows to explicitly set the event
loop object used by the future. If it's not provided, the future uses
the default event loop.
Nr)
r Zget_event_loopr�
_callbacksZ get_debugr�
extract_stack�sys� _getframer)rrrrrr�s zFuture.__init__cCs�|j}t|�}|s$d}ndd�}|dkrO||d�}nq|dkr�dj||d�||d��}n<|dkr�dj||d�|d||d
��}nd |S)Nr cSstj|f�S)N)r Z_format_callback_source)�callbackrrr� format_cb�sz,Future.__format_callbacks.<locals>.format_cbrr�z{}, {}z{}, <{} more>, {}zcb=[%s]���)r*�len�format)r�cb�sizer/rrrZ__format_callbacks�s )zFuture.__format_callbackscCs�|jj�g}|jtkrw|jdk rL|jdj|j��qwtj|j�}|jdj|��n|j r�|j|j
��n|jr�|jd}|jd|d|df�n|S)Nzexception={!r}z result={}rzcreated at %s:%srr1)�_state�lower� _FINISHED�
_exception�appendr3�reprlib�repr�_resultr*�_Future__format_callbacksr)r�info�resultZframerrr�
_repr_info�s
"zFuture._repr_infocCs)|j�}d|jjdj|�fS)Nz<%s %s>� )rArr
r")rr?rrr�__repr__�szFuture.__repr__cCsh|js
dS|j}id|jjd6|d6|d6}|jrT|j|d<n|jj|�dS)Nz %s exception was never retrievedr!� exceptionrr)�_log_tracebackr9rr
rrr%)rr�contextrrrr(�s
zFuture.__del__cCs*|jtkrdSt|_|j�dS)z�Cancel 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)r6�_PENDING�
_CANCELLED�_schedule_callbacks)rrrr�cancel�s
z
Future.cancelcCsX|jdd�}|sdSg|jdd�<x!|D]}|jj||�q7WdS)z�Internal: 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� call_soon)rZ callbacksr.rrrrI�s
zFuture._schedule_callbackscCs
|jtkS)z(Return True if the future was cancelled.)r6rH)rrrr� cancelled�szFuture.cancelledcCs
|jtkS)z�Return True if the future is done.
Done means either that a result / exception are available, or that the
future was cancelled.
)r6rG)rrrr�done�szFuture.donecCs�|jtkrt�n|jtkr6td��nd|_|jdk rg|jj�d|_n|jdk r�|j�n|j S)aReturn 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.
zResult is not ready.FN)
r6rHrr8rrE�
_tb_loggerrr9r=)rrrrr@s
z
Future.resultcCsn|jtkrt�n|jtkr6td��nd|_|jdk rg|jj�d|_n|jS)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.
zException is not set.FN) r6rHrr8rrErNrr9)rrrrrDs
zFuture.exceptioncCs9|jtkr%|jj||�n|jj|�dS)z�Add 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.
N)r6rGrrKr*r:)r�fnrrr�add_done_callback'szFuture.add_done_callbackcsU�fdd�|jD�}t|j�t|�}|rQ||jdd�<n|S)z}Remove all instances of a callback from the "call when done" list.
Returns the number of callbacks removed.
cs"g|]}|�kr|�qSrr)�.0�f)rOrr�
<listcomp>:s z/Future.remove_done_callback.<locals>.<listcomp>N)r*r2)rrOZfiltered_callbacksZ
removed_countr)rOr�remove_done_callback5s
zFuture.remove_done_callbackcCsM|jtkr-tdj|j|���n||_t|_|j�dS)z�Mark the future done and set its result.
If the future is already done when this method is called, raises
InvalidStateError.
z{}: {!r}N)r6rGrr3r=r8rI)rr@rrr�
set_resultBs
zFuture.set_resultcCs�|jtkr-tdj|j|���nt|t�rH|�}n||_t|_|j�t j
ryd|_n(t||�|_
|jj|j
j�dS)z�Mark the future done and set an exception.
If the future is already done when this method is called, raises
InvalidStateError.
z{}: {!r}TN)r6rGrr3�
isinstance�typer9r8rIr�PY34rErrNrrKr)rrDrrr�
set_exceptionNs
zFuture.set_exceptionccs'|j�sd|_|Vn|j�S)NT)rM� _blockingr@)rrrr�__iter__cs zFuture.__iter__)!r
rrr
rGr6r=r9rrrZrErNrr>rArCrrXr(rJrIrLrMr@rDrPrTrUrYr[ZPY35Z __await__rrrrrqs8
cCs!|j�rdS|j|�dS)z?Helper setting the result only if the future was not cancelled.N)rLrU)Zfutr@rrr�_set_result_unless_cancellednsr\cCsn|j�r|j�n|j�s)dS|j�}|dk rQ|j|�n|j�}|j|�dS)z8Copy state from a future to a concurrent.futures.Future.N)rLrJZset_running_or_notify_cancelrDrYr@rU)�
concurrent�sourcerDr@rrr�_set_concurrent_future_stateus
r_cCsn|j�rdS|j�r)|j�nA|j�}|dk rQ|j|�n|j�}|j|�dS)zqInternal helper to copy state from another Future.
The other Future may be a concurrent.futures.Future.
N)rLrJrDrYr@rU)r^�destrDr@rrr�_copy_future_state�s
racs�t�ttjjf�s*td��nt�ttjjf�sTtd��nt�t�rl�jnd�t�t�r��jnd�dd�����fdd�}����fdd �}�j|��j|�dS)
aChain 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 argumentNcSs0t|t�rt||�n
t||�dS)N)rVrrar_)r�otherrrr�
_set_state�sz!_chain_future.<locals>._set_statecsH|j�rD�dks$��kr1�j�qD�j�j�ndS)N)rLrJ�call_soon_threadsafe)�destination)� dest_loopr^�source_looprr�_call_check_cancel�s
z)_chain_future.<locals>._call_check_cancelcs?�dks��kr(��|�n�j��|�dS)N)rd)r^)rcrfrergrr�_call_set_state�sz&_chain_future.<locals>._call_set_state)rVrr]�futures� TypeErrorrrP)r^rerhrir)rcrfrer^rgr�
_chain_future�s
rlrcCs3t|t�r|Std|�}t||�|S)z&Wrap concurrent.futures.Future object.r)rVrrl)rrZ
new_futurerrrr�s
)r
�__all__Zconcurrent.futures._baser]Zloggingr;r,rr rr rGrHr8rjZ_base�Errorrr�DEBUGZSTACK_DEBUGrrrr\r_rarlrrrrr�<module>s2
P�%
?>