\d78dZdZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl m Z dd l m Z e jZe jZe jZe jZejdz ZGd d ZeZd Zd ZdZdZdZdZdddZ ddlZejxZZdS#e$rYdSwxYw)z.A Future class similar to the one in PEP 3148.)Future wrap_futureisfutureN) GenericAlias) base_futures)events) exceptions)format_helpersceZdZdZeZdZdZdZdZ dZ dZ dZ dZ dddZdZdZeeZedZejd Zd Zd Zdd Zd ZdZdZdZdZdddZdZ dZ!dZ"dZ#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.) NFloopc|tj|_n||_g|_|jr-t jtjd|_ dSdS)zInitialize the future. The optional event_loop argument allows explicitly setting the event loop object used by the future. If it's not provided, the future uses the default event loop. Nr) r _get_event_loop_loop _callbacks get_debugr extract_stacksys _getframe_source_tracebackselfrs ..\python\lib\asyncio\futures.py__init__zFuture.__init__Hsq </11DJJDJ :   ! ! "%3%A a  &"&"D " " " " "c*tj|SN)r _future_reprrs r__repr__zFuture.__repr__Xs(...rc|jsdS|j}|jjd||d}|jr |j|d<|j|dS)Nz exception was never retrieved)message exceptionfuturesource_traceback)_Future__log_traceback _exception __class____name__rrcall_exception_handler)rexccontexts r__del__zFuture.__del__[sx#  Fo>*JJJ     ! A*.*@G& ' ))'22222rc|jSr)r'r s r_log_tracebackzFuture._log_tracebackms ##rc6|rtdd|_dS)Nz'_log_traceback can only be set to FalseF) ValueErrorr')rvals rr0zFuture._log_tracebackqs(  HFGG G$rc6|j}|td|S)z-Return the event loop the Future is bound to.Nz!Future object is not initialized.)r RuntimeErrorrs rget_loopzFuture.get_loopws"z <BCC C rc|j|j}d|_|S|jtj}ntj|j}|j|_d|_|S)zCreate the CancelledError to raise if the Future is cancelled. This should only be called once when handling a cancellation since it erases the saved context exception value. N)_cancelled_exc_cancel_messager CancelledError __context__rr,s r_make_cancelled_errorzFuture._make_cancelled_error~se   *%C"&D J   '+--CC+D,@AAC-" rcd|_|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 _CANCELLEDr9_Future__schedule_callbacks)rmsgs rcancelz Future.cancelsD % ;( " "5  " !!###trc|jdd}|sdSg|jdd<|D]"\}}|j|||#dS)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. Nr-)rr call_soon)r callbackscallbackctxs r__schedule_callbackszFuture.__schedule_callbackssn OAAA&   F& > >MHc J 4 = = = = > >rc"|jtkS)z(Return True if the future was cancelled.)r?rAr s r cancelledzFuture.cancelleds{j((rc"|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@r s rdonez Future.dones {h&&rc|jtkr|}||jtkrt jdd|_|j|j|j |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.F) r?rAr= _FINISHEDr InvalidStateErrorr'r(with_traceback _exception_tb_resultr<s rresultz Future.resultsw ;* $ $,,..CI ;) # #./EFF F$ ? &/001CDD D|rc|jtkr|}||jtkrt jdd|_|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?rAr=rQr rRr'r(r<s rr$zFuture.exceptionsV ;* $ $,,..CI ;) # #./FGG G$rrFc|jtkr|j|||dS|t j}|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. rFN)r?r@rrG contextvars copy_contextrappend)rfnr-s radd_done_callbackzFuture.add_done_callbacksg ;( " " J T7 ; ; ; ; ;%244 O " "B= 1 1 1 1 1rcfd|jD}t|jt|z }|r ||jdd<|S)z}Remove all instances of a callback from the "call when done" list. Returns the number of callbacks removed. c*g|]\}}|k ||fSr`).0frJr\s r z/Future.remove_done_callback..s2***"*1c!"b !#h!(rN)rlen)rr\filtered_callbacks removed_counts ` rremove_done_callbackzFuture.remove_done_callbacksk ****.2o***DO,,s3E/F/FF  4!3DOAAA rc|jtkrtj|jd|||_t |_|dS)zMark the future done and set its result. If the future is already done when this method is called, raises InvalidStateError. : N)r?r@r rRrUrQrB)rrVs r set_resultzFuture.set_resultsX ;( " ".$+/I/I/I/IJJ J   !!#####rc^|jtkrtj|jd|t |t r |}t |t urtd||_|j |_ t|_| d|_ dS)zMark 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?r@r rR isinstancetype StopIteration TypeErrorr( __traceback__rTrQrBr')rr$s r set_exceptionzFuture.set_exception s ;( " ".$+/I/I/I/IJJ J i & & $! I  ??m + +ABB B#&4  !!####rc#K|s d|_|V|std|S)NTzawait wasn't used with future)rO_asyncio_future_blockingr5rVr s r __await__zFuture.__await__sUyy{{ ,0D )JJJyy{{ @>?? ?{{}}rr)%r* __module__ __qualname____doc__r@r?rUr(rrr9r8rsr'rr!r. classmethodr__class_getitem__propertyr0setterr6r=rDrBrMrOrVr$r]rgrjrqrt__iter__r`rrrrs&FGJ EON %O#""""" ///333 $ L11 $$X$%%% (     > > >))) '''" 04 2 2 2 2 2    $ $ $$$$&HHHrrcT |j}|S#t$rYnwxYw|jSr)r6AttributeErrorr)futr6s r _get_loopr+sH<xzz       9s   c\|rdS||dS)z?Helper setting the result only if the future was not cancelled.N)rMrj)rrVs r_set_result_unless_cancelledr7s/ }}NN6rct|}|tjjurt j|jS|tjjurt j|jS|tjjurt j|jS|Sr)rm concurrentfuturesr:r args TimeoutErrorrR)r, exc_classs r_convert_future_excr>suS IJ&555(#(33 j(5 5 5&11 j(: : :+SX66 rcL|sJ|r|j|jsdS|}||jt |dS|}|j|dS)z8Copy state from a future to a concurrent.futures.Future.N) rOrMrDset_running_or_notify_cancelr$rqrrVrj)rsourcer$rVs r_set_concurrent_future_staterJs ;;==   2: 2 4 4  ""I  !4Y!?!?@@@@@ f%%%%%rc|sJ|rdS|rJ|r|dS|}|$|t |dS|}||dS)zqInternal helper to copy state from another Future. The other Future may be a concurrent.futures.Future. N)rOrMrDr$rqrrVrj)rdestr$rVs r_copy_future_staterYs ;;== ~~yy{{ $ $$&&    29== > > > > >]]__F OOF # # # # #rcts.ttjjst dts.ttjjst dtrt ndtrt nddfd}fd}||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 argumentNcht|rt||dSt||dSr)rrr)r%others r _set_statez!_chain_future.._set_state}s> F   8 uf - - - - - ( 7 7 7 7 7rc|r8urdSjdSdSr)rMrDcall_soon_threadsafe) destination dest_loopr source_loops r_call_check_cancelz)_chain_future.._call_check_cancelsa  " " @"kY&>&> 00?????  @ @rcrrdSur|dSrdS|dSr)rM is_closedr)rrrrrs r_call_set_statez&_chain_future.._call_set_states  ! ! # # %)*=*=*?*?% F   [ 8 8 J{F + + + + +""$$   * *:{F K K K K Kr)rrlrrrrorr])rrrrrrrs`` @@@r _chain_futurerms_ F  DJv/9/A/H%J%JDBCCC K IK4>4F4M*O*OIGHHH'/'7'7A)F###TK*2;*?*?I +&&&TI888 @@@@@@@ L L L L L L L L!!"4555 _-----rr ct|r|St|tjjs Jd||t j}|}t|||S)z&Wrap concurrent.futures.Future object.z+concurrent.futures.Future is expected, got ) rrlrrrr r create_futurer)r%r new_futures rrrs fj07 8 8AA@f@@AAA |%''##%%J&*%%% r) rw__all__concurrent.futuresrrYloggingrtypesrrr r r rr@rArQDEBUG STACK_DEBUGr _PyFuturerrrrrrr_asyncio_CFuture ImportErrorr`rrrs44        $  " ma FFFFFFFFT         & & &$$$().).).X!%     (OOO !'FXXX    DD sBBB