f7jdZdZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl m Z dd l m Z e jZe jZe j Ze j"Zej$dz ZGd d ZeZd Zd ZdZdZdZdZdddZ ddlZej(xZZy#e$rYywxYw)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 dddZdZdZeeZedZej,d Zd Zd Zdd Zd ZdZdZdZdZdddZdZ dZ!dZ"dZ#e#Z$y)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|_|jj r.t j tjd|_ yy)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 &/usr/lib/python3.12/asyncio/futures.py__init__zFuture.__init__Hs[ <..0DJDJ ::   !%3%A%A a &"D " "c,tj|SN)r _future_reprrs r__repr__zFuture.__repr__Xs((..rc|jsy|j}|jjd||d}|jr|j|d<|j j |y)Nz exception was never retrieved)message exceptionfuturesource_traceback)_Future__log_traceback _exception __class____name__rrcall_exception_handler)rexccontexts r__del__zFuture.__del__[sl## oo>>**++IJ    ! !*.*@*@G& ' ))'2rc|jSr)r'r s r_log_tracebackzFuture._log_tracebackms###rc,|r tdd|_y)Nz'_log_traceback can only be set to FalseF) ValueErrorr')rvals rr0zFuture._log_tracebackqs FG G$rc8|j}| td|S)z-Return the event loop the Future is bound to.z!Future object is not initialized.)r RuntimeErrorrs rget_loopzFuture.get_loopws!zz <BC 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~sr    *%%C"&D J    '++-C++D,@,@AC--" rc~d|_|jtk7ryt|_||_|j y)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.cancels9 % ;;( "  " !!#rc|jdd}|syg|jdd|D]#\}}|jj|||%y)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_callbackssR OOA&  & >MHc JJ 4 = >rc(|jtk(S)z(Return True if the future was cancelled.)r?rAr s r cancelledzFuture.cancelleds{{j((rc(|jtk7S)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$|jtk(r|j}||jtk7rt j dd|_|j%|jj|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.F) r?rAr= _FINISHEDr InvalidStateErrorr'r(with_traceback _exception_tb_resultr<s rresultz Future.resultsx ;;* $,,.CI ;;) #../EF F$ ?? &//001C1CD D||rc|jtk(r|j}||jtk7rt j dd|_|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.exceptionsS ;;* $,,.CI ;;) #../FG G$rrFc|jtk7r|jj|||y|t j }|j j||fy)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_callbacksR ;;( " JJ T7 ;%224 OO " "B= 1rc|jDcgc]\}}||k7r||f}}}t|jt|z }|r||jdd|Scc}}w)z}Remove all instances of a callback from the "call when done" list. Returns the number of callbacks removed. N)rlen)rr\frJfiltered_callbacks removed_counts rremove_done_callbackzFuture.remove_done_callbacksl /3oo*"*1c!"b !#h**DOO,s3E/FF !3DOOA  *sAc|jtk7r$tj|jd|||_t |_|j y)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_resultsJ ;;( "..$++b/IJ J   !!#rcL|jtk7r$tj|jd|t |t r|}t |t ur td||_|j|_ t|_|jd|_ y)zMark the future done and set an exception. If the future is already done when this method is called, raises InvalidStateError. rezPStopIteration 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 ;;( "..$++b/IJ J i &! I  ?m +AB B#&44  !!##rc#K|js d|_||js td|jSw)NTzawait wasn't used with future)rO_asyncio_future_blockingr5rVr s r __await__zFuture.__await__s=yy{,0D )Jyy{>? ?{{}sAA r)%r* __module__ __qualname____doc__r@r?rUr(rrr9r8ror'rr!r. classmethodr__class_getitem__propertyr0setterr6r=rDrBrMrOrVr$r]rcrfrmrp__iter__rrrrs&FGJ EON %O#" /3 $L1 $$%% (  >) '" 04 2  $$&Hrrc^ |j}|S#t$rY|jSwxYwr)r6AttributeErrorr)futr6s r _get_loopr}+s:<<z    99  s  ,,cH|jry|j|y)z?Helper setting the result only if the future was not cancelled.N)rMrf)r|rVs r_set_result_unless_cancelledr7s }}NN6rclt|}|tjjurt j|j S|tjj urt j |j S|tjjurt j|j S|Sr)ri concurrentfuturesr:r args TimeoutErrorrR)r, exc_classs r_convert_future_excr>sS IJ&&555((#((33 j((55 5&&11 j((:: :++SXX66 rc.|jsJ|jr|j|jsy|j }||j t |y|j}|j|y)z8Copy state from a future to a concurrent.futures.Future.N) rOrMrDset_running_or_notify_cancelr$rmrrVrf)rsourcer$rVs r_set_concurrent_future_staterJs ;;==   2: 2 2 4  "I   !4Y!?@ f%rcL|jsJ|jry|jrJ|jr|jy|j}||j t |y|j }|j|y)zqInternal helper to copy state from another Future. The other Future may be a concurrent.futures.Future. N)rOrMrDr$rmrrVrf)rdestr$rVs r_copy_future_staterYs ;;== ~~yy{?  $$&    29= >]]_F OOF #rcts/ttjjs t dts/ttjjs t dtr t ndtr t nddfd}fd}j|j|y)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 argumentNcLt|r t||yt||yr)rrr)r%others r _set_statez!_chain_future.._set_state}s F  uf - ( 7rc|jr3urjyjjyyr)rMrDcall_soon_threadsafe) destination dest_loopr source_loops r_call_check_cancelz)_chain_future.._call_check_cancels<  ""kY&> 00? #rcjrjryur |yjryj|yr)rM is_closedr)rrrrrs r_call_set_statez&_chain_future.._call_set_states[  ! ! #%)*=*=*?    [ 8 {F +""$  * *:{F Kr)rrhrrrrkr}r])rrrrrrrs`` @@@r _chain_futurerms F Jv/9/A/A/H/H%JBCC K K4>4F4F4M4M*OGHH'/'7)F#TK*2;*? +&TI8 @ L!!"45 _-rr ct|r|St|tjjs Jd||t j }|j}t|||S)z&Wrap concurrent.futures.Future object.z+concurrent.futures.Future is expected, got ) rrhrrrr r create_futurer)r%r new_futures rrrso fj0077 8A 5fZ@A 8 |$$&##%J&*% r) rs__all__concurrent.futuresrrYloggingrtypesrrr r r rr@rArQDEBUG STACK_DEBUGr _PyFuturer}rrrrrr_asyncio_CFuture ImportErrorryrrrs4        $ $  " " mma FFT     &$().X!% ( !'FX   sB**B21B2