U e5db3ã@súdZdZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ej Z ej Z ejZejZejdZGd d „d ƒZeZd d „Zd d„Zdd„Zdd„Zdd„Zdd„Zddœdd„Zz ddlZWnek rêYn XejZZdS)z.A Future class similar to the one in PEP 3148.)ÚFutureÚ wrap_futureÚisfutureéNé)Ú base_futures)Úevents)Ú exceptions)Úformat_helpersc@sÔeZdZdZeZdZdZdZdZ dZ dZ ddœdd„Z e jZdd„Zd d „Zed d „ƒZejd d „ƒZdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zddœdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Z e Z!dS)'ra,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©ÚloopcCs@|dkrt ¡|_n||_g|_|j ¡rú )ÚformatÚ __class__Ú__name__ÚjoinÚ _repr_info©rrrrÚ__repr__Vs  ÿzFuture.__repr__cCsF|js dS|j}|jj›d�||dœ}|jr6|j|d<|j |¡dS)Nz exception was never retrieved)ÚmessageÚ exceptionÚfutureZsource_traceback)Ú_Future__log_tracebackÚ _exceptionrrrr Zcall_exception_handler)rÚexcÚcontextrrrÚ__del__Zs ü zFuture.__del__cCs|jS©N)r#rrrrÚ_log_tracebackjszFuture._log_tracebackcCst|ƒrtdƒ‚d|_dS)Nz'_log_traceback can only be set to FalseF)ÚboolÚ ValueErrorr#)rÚvalrrrr)nscCs|j}|dkrtdƒ‚|S)z-Return the event loop the Future is bound to.Nz!Future object is not initialized.)r Ú RuntimeErrorrrrrÚget_looptszFuture.get_loopcCs&d|_|jtkrdSt|_| ¡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)r#Ú_stateÚ_PENDINGÚ _CANCELLEDÚ_Future__schedule_callbacksrrrrÚcancel{s  z Future.cancelcCsH|jdd…}|sdSg|jdd…<|D]\}}|jj|||d�q(dS)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&)rr Ú call_soon)rZ callbacksÚcallbackÚctxrrrZ__schedule_callbacks‰s  zFuture.__schedule_callbackscCs |jtkS)z(Return True if the future was cancelled.)r/r1rrrrÚ 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. )r/r0rrrrÚdone�sz Future.donecCs@|jtkrtj‚|jtkr$t d¡‚d|_|jdk r:|j‚|jS)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) r/r1rÚCancelledErrorÚ _FINISHEDÚInvalidStateErrorr#r$Ú_resultrrrrÚresult¥s    z Future.resultcCs0|jtkrtj‚|jtkr$t d¡‚d|_|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.F)r/r1rr:r;r<r#r$rrrrr!µs    zFuture.exceptionr4cCsB|jtkr|jj|||d�n |dkr.t ¡}|j ||f¡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. r4N)r/r0r r5Ú contextvarsZ copy_contextrÚappend)rÚfnr&rrrÚadd_done_callbackÄs  zFuture.add_done_callbackcs<‡fdd„|jDƒ}t|jƒt|ƒ}|r8||jdd…<|S)z}Remove all instances of a callback from the "call when done" list. Returns the number of callbacks removed. cs g|]\}}|ˆkr||f‘qSrr)Ú.0Úfr7©rArrÚ Ùsþz/Future.remove_done_callback..N)rÚlen)rrAZfiltered_callbacksZ removed_countrrErÚremove_done_callbackÔs ÿzFuture.remove_done_callbackcCs8|jtkr t |j›d|›�¡‚||_t|_| ¡dS)z–Mark the future done and set its result. If the future is already done when this method is called, raises InvalidStateError. ú: N)r/r0rr<r=r;r2)rr>rrrÚ set_resultãs  zFuture.set_resultcCsb|jtkr t |j›d|›�¡‚t|tƒr0|ƒ}t|ƒtkrDtdƒ‚||_t |_|  ¡d|_ dS)z˜Mark the future done and set an exception. If the future is already done when this method is called, raises InvalidStateError. rIzPStopIteration interacts badly with generators and cannot be raised into a FutureTN) r/r0rr<Ú isinstanceÚtypeÚ StopIterationÚ TypeErrorr$r;r2r#)rr!rrrÚ set_exceptionïs   zFuture.set_exceptionccs,| ¡sd|_|V| ¡s$tdƒ‚| ¡S)NTzawait wasn't used with future)r9Ú_asyncio_future_blockingr-r>rrrrÚ __await__s zFuture.__await__)"rÚ __module__Ú __qualname__Ú__doc__r0r/r=r$r rrPr#rrZ_future_repr_inforrr'Úpropertyr)Úsetterr.r3r2r8r9r>r!rBrHrJrOrQÚ__iter__rrrrrs:    rcCs,z |j}Wntk rYnX|ƒS|jSr()r.ÚAttributeErrorr )Úfutr.rrrÚ _get_loops  rZcCs| ¡r dS| |¡dS)z?Helper setting the result only if the future was not cancelled.N)r8rJ)rYr>rrrÚ_set_result_unless_cancelledsr[cCsXt|ƒ}|tjjkr tj|jŽS|tjjkr8tj|jŽS|tjjkrPtj|jŽS|SdSr()rLÚ concurrentÚfuturesr:rÚargsÚ TimeoutErrorr<)r%Z exc_classrrrÚ_convert_future_exc#s      r`cCs^| ¡s t‚| ¡r| ¡| ¡s(dS| ¡}|dk rH| t|ƒ¡n| ¡}|  |¡dS)z8Copy state from a future to a concurrent.futures.Future.N) r9ÚAssertionErrorr8r3Zset_running_or_notify_cancelr!rOr`r>rJ)r\Úsourcer!r>rrrÚ_set_concurrent_future_state/s rccCsl| ¡s t‚| ¡rdS| ¡r$t‚| ¡r6| ¡n2| ¡}|dk rV| t|ƒ¡n| ¡}| |¡dS)zqInternal helper to copy state from another Future. The other Future may be a concurrent.futures.Future. N) r9rar8r3r!rOr`r>rJ)rbÚdestr!r>rrrÚ_copy_future_state>s   recs¦tˆƒstˆtjjƒstdƒ‚tˆƒs._set_statecs2| ¡r.ˆdksˆˆkr"ˆ ¡n ˆ ˆj¡dSr()r8r3Úcall_soon_threadsafe)Ú destination)Ú dest_looprbÚ source_looprrÚ_call_check_cancelhs z)_chain_future.._call_check_cancelcsJˆ ¡rˆdk rˆ ¡rdSˆdks,ˆˆkr8ˆˆ|ƒnˆ ˆˆ|¡dSr()r8Z is_closedrh)rb)rgrjrirkrrÚ_call_set_stateosÿÿ z&_chain_future.._call_set_state)rrKr\r]rrNrZrB)rbrirlrmr)rgrjrirbrkrÚ _chain_futureRs ÿ ÿ rnr cCsNt|ƒr |St|tjjƒs(td|›�ƒ‚|dkr8t ¡}| ¡}t ||ƒ|S)z&Wrap concurrent.futures.Future object.z+concurrent.futures.Future is expected, got N) rrKr\r]rrarr Z create_futurern)r"r Z new_futurerrrr|sÿ r)rTÚ__all__Zconcurrent.futuresr\r?ZloggingrÚrrrr rr0r1r;ÚDEBUGZ STACK_DEBUGrZ _PyFuturerZr[r`rcrernrZ_asyncioÚ ImportErrorZ_CFuturerrrrÚs:     q  *