î j fÔ?ã@s?dZdddddgZddlZddlZddlZddlZddlZdd lm Z dd lm Z d Z d Z d 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.pyrs c@sLeZdZdZdZdd„Zdd „Zd d „Zd d „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 r r r Ú __slots__rrrr(rrrrr!s 0   rc@s*eZdZdZeZdZdZdZdZ dZ dZ dZ dddd„Z dd„Zd d „Zd d „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..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|j rÎ|j d}|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__Ás zFuture.__repr__cCsh|js dS|j}id|jjd6|d6|d6}|jrT|j|d:s z/Future.remove_done_callback..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 r r r 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_cancelledns r\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.._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.._call_check_cancelcs?ˆdksˆˆkr(ˆˆ|ƒnˆjˆˆ|ƒdS)N)rd)r^)rcrfrergrrÚ_call_set_state³sz&_chain_future.._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ÚDEBUGZ STACK_DEBUGrrrr\r_rarlrrrrrÚs2         Pý    %