B -_1@sdZdZddlZddlZddlZddlZddlmZddlm Z ddlm Z ej Z ej Z ej Z ejZejZejZejZejdZGdd d ZeZd d Zd d ZddZddZddZddddZy ddlZWnek rYn XejZZdS)z.A Future class similar to the one in PEP 3148.)CancelledError TimeoutErrorInvalidStateErrorFuture wrap_futureisfutureN) base_futures)events)format_helpersc@seZdZdZeZdZdZdZdZ dZ dZ ddddZ e jZddZd d Zed d Zejd d ZddZddZddZddZddZddZddZddddZdd 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|_|jr )format __class____name__join _repr_info)rrrr__repr__Ys zFuture.__repr__cCsF|js dS|j}|jjd||d}|jr6|j|d<|j|dS)Nz exception was never retrieved)message exceptionfutureZsource_traceback)_Future__log_traceback _exceptionrrrrZcall_exception_handler)rexccontextrrr__del__]s  zFuture.__del__cCs|jS)N)r")rrrr_log_tracebackmszFuture._log_tracebackcCst|rtdd|_dS)Nz'_log_traceback can only be set to FalseF)bool ValueErrorr")rvalrrrr'qscCs|jS)z-Return the event loop the Future is bound to.)r)rrrrget_loopwszFuture.get_loopcCs&d|_|jtkrdSt|_|dS)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_callbacks)rrrrcancel{s  z Future.cancelcCsL|jdd}|sdSg|jdd<x"|D]\}}|jj|||dq*WdS)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%)rr call_soon)rZ callbackscallbackctxrrrZ__schedule_callbackss zFuture.__schedule_callbackscCs |jtkS)z(Return True if the future was cancelled.)r,r.)rrrr cancelledszFuture.cancelledcCs |jtkS)zReturn True if the future is done. Done means either that a result / exception are available, or that the future was cancelled. )r,r-)rrrrdonesz Future.donecCs<|jtkrt|jtkr tdd|_|jdk r6|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,r.r _FINISHEDrr"r#_result)rrrrresults   z Future.resultcCs,|jtkrt|jtkr tdd|_|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,r.rr6rr"r#)rrrrr s   zFuture.exception)r%cCsB|jtkr|jj|||dn |dkr.t}|j||fdS)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. )r%N)r,r-rr1 contextvarsZ copy_contextrappend)rfnr%rrradd_done_callbacks  zFuture.add_done_callbackcs<fdd|jD}t|jt|}|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||fqSrr).0fr3)r;rr sz/Future.remove_done_callback..N)rlen)rr;Zfiltered_callbacksZ removed_countr)r;rremove_done_callbacks   zFuture.remove_done_callbackcCs4|jtkrtd|j|||_t|_|dS)zMark the future done and set its result. If the future is already done when this method is called, raises InvalidStateError. z{}: {!r}N)r,r-rrr7r6r/)rr8rrr set_results  zFuture.set_resultcCs^|jtkrtd|j|t|tr,|}t|tkr@td||_t |_| d|_ dS)zMark the future done and set an exception. If the future is already done when this method is called, raises InvalidStateError. z{}: {!r}zPStopIteration interacts badly with generators and cannot be raised into a FutureTN) r,r-rr isinstancetype StopIteration TypeErrorr#r6r/r")rr rrr set_exceptions   zFuture.set_exceptionccs,|sd|_|V|s$td|S)NTzawait wasn't used with future)r5_asyncio_future_blocking RuntimeErrorr8)rrrr __await__s zFuture.__await__)"r __module__ __qualname____doc__r-r,r7r#rrrHr"rr Z_future_repr_inforrr&propertyr'setterr+r0r/r4r5r8r r<rArBrGrJ__iter__rrrrr s6   rcCs,y |j}Wntk rYnX|S|jS)N)r+AttributeErrorr)futr+rrr _get_loops  rScCs|r dS||dS)z?Helper setting the result only if the future was not cancelled.N)r4rB)rRr8rrr_set_result_unless_cancelledsrTcCsZ|s t|r||s(dS|}|dk rD||n|}||dS)z8Copy state from a future to a concurrent.futures.Future.N) r5AssertionErrorr4r0Zset_running_or_notify_cancelr rGr8rB) concurrentsourcer r8rrr_set_concurrent_future_state#s  rXcCsh|s t|rdS|r$t|r6|n.|}|dk rR||n|}||dS)zqInternal helper to copy state from another Future. The other Future may be a concurrent.futures.Future. N)r5rUr4r0r rGr8rB)rWdestr r8rrr_copy_future_state2s    rZcststtjjstdts._set_statecs2|r.dkskr"n jdS)N)r4r0call_soon_threadsafe) destination) dest_looprW source_looprr_call_check_cancel\s z)_chain_future.._call_check_cancelcsJrdk rrdSdks,kr8|n|dS)N)r4Z is_closedr])rW)r\r_r^r`rr_call_set_statecs  z&_chain_future.._call_set_state)rrCrVfuturesrrFrSr<)rWr^rarbr)r\r_r^rWr`r _chain_futureFs     rd)r cCsNt|r |St|tjjs(td||dkr8t}|}t |||S)z&Wrap concurrent.futures.Future object.z+concurrent.futures.Future is expected, got N) rrCrVrcrrUr r Z create_futurerd)r!r Z new_futurerrrrps  r)rM__all__Zconcurrent.futuresrVr9Zloggingrr r r rrrrr-r.r6DEBUGZ STACK_DEBUGrZ _PyFuturerSrTrXrZrdrZ_asyncio ImportErrorZ_CFuturerrrrs<    n *