o w[ek7@sdZdZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl m Z dd l m Z e jZe jZe jZe jZejdZGd d d ZeZd d ZddZddZddZddZddZddddZzddlZWn ey{YdSwejZZdS)z.A Future class similar to the one in PEP 3148.)Future wrap_futureisfutureN) GenericAlias) base_futures)events) exceptions)format_helpersc@seZdZdZeZdZdZdZdZ dZ dZ dZ dZ ddddZejZddZd d ZeeZed d Zejd d ZddZddZd)ddZddZddZddZddZ ddZ!dddd Z"d!d"Z#d#d$Z$d%d&Z%d'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.) NFloopcCsD|dur t|_n||_g|_|jr ttd|_ 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_tracebackselfr r&/usr/lib/python3.10/asyncio/futures.py__init__Hs   zFuture.__init__cCsd|jjd|S)Nz<{} {}> )format __class____name__join _repr_inforrrr__repr__Zs  zFuture.__repr__cCsF|jsdS|j}|jjd||d}|jr|j|d<|j|dS)Nz exception was never retrieved)message exceptionfuturesource_traceback)_Future__log_traceback _exceptionrrrrcall_exception_handler)rexccontextrrr__del__^s  zFuture.__del__cCs|jSN)r&r rrr_log_tracebackpszFuture._log_tracebackcCs|rtdd|_dS)Nz'_log_traceback can only be set to FalseF) ValueErrorr&)rvalrrrr-ts cCs|j}|dur td|S)z-Return the event loop the Future is bound to.Nz!Future object is not initialized.)r RuntimeErrorrrrrget_loopzszFuture.get_loopcCs2|jdur t}nt|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)_cancel_messager CancelledError_cancelled_exc __context__rr)rrr_make_cancelled_errors   zFuture._make_cancelled_errorcCs,d|_|jtkr dSt|_||_|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 _CANCELLEDr2_Future__schedule_callbacks)rmsgrrrcancels z Future.cancelcCsH|jdd}|s dSg|jdd<|D] \}}|jj|||dqdS)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 callbackscallbackctxrrr__schedule_callbackss zFuture.__schedule_callbackscCs |jtkS)z(Return True if the future was cancelled.)r8r:r rrr cancelleds zFuture.cancelledcCs |jtkS)zReturn True if the future is done. Done means either that a result / exception are available, or that the future was cancelled. )r8r9r rrrdones z Future.donecCsN|jtkr |}||jtkrtdd|_|jdur$|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.FN) r8r:r7 _FINISHEDr InvalidStateErrorr&r'with_traceback _exception_tb_resultr6rrrresults    z Future.resultcCs6|jtkr |}||jtkrtdd|_|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)r8r:r7rFr rGr&r'r6rrrr#s   zFuture.exceptionr>cCsD|jtkr|jj|||ddS|durt}|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)r8r9rr? contextvars copy_contextrappend)rfnr*rrradd_done_callbacks zFuture.add_done_callbackcs<fdd|jD}t|jt|}|r||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).0frBrOrr s z/Future.remove_done_callback..N)rlen)rrOfiltered_callbacks removed_countrrSrremove_done_callbacks zFuture.remove_done_callbackcCs8|jtkrt|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)r8r9r rGrJrFr;)rrKrrr set_results  zFuture.set_resultcCsj|jtkrt|jd|t|tr|}t|tur"td||_|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. rYzPStopIteration interacts badly with generators and cannot be raised into a FutureTN)r8r9r rG isinstancetype StopIteration TypeErrorr' __traceback__rIrFr;r&)rr#rrr set_exceptions    zFuture.set_exceptionccs.|s d|_|V|std|S)NTzawait wasn't used with future)rE_asyncio_future_blockingr0rKr rrr __await__szFuture.__await__r,)(r __module__ __qualname____doc__r9r8rJr'rrr2r4rar&rr_future_repr_inforr!r+ classmethodr__class_getitem__propertyr-setterr1r7r=r;rDrErKr#rPrXrZr`rb__iter__rrrrrsD     rcCs&z|j}W|StyY|jSwr,)r1AttributeErrorr)futr1rrr _get_loop)s rncCs|rdS||dS)z?Helper setting the result only if the future was not cancelled.N)rDrZ)rmrKrrr_set_result_unless_cancelled5srocCsTt|}|tjjurtj|jS|tjjurtj|jS|tjjur(tj|jS|Sr,)r\ concurrentfuturesr3r args TimeoutErrorrG)r) exc_classrrr_convert_future_exc<s      rucCs`|sJ|r||sdS|}|dur%|t|dS|}||dS)z8Copy state from a future to a concurrent.futures.Future.N) rErDr=set_running_or_notify_cancelr#r`rurKrZ)rpsourcer#rKrrr_set_concurrent_future_stateHs rxcCsp|sJ|r dS|rJ|r|dS|}|dur-|t|dS|}||dS)zqInternal helper to copy state from another Future. The other Future may be a concurrent.futures.Future. N)rErDr=r#r`rurKrZ)rwdestr#rKrrr_copy_future_stateWs   rzcststtjjstdtsttjjstdtr&tndtr0tndddfdd}fdd }||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 argumentNcSs$t|r t||dSt||dSr,)rrzrx)r$otherrrr _set_state{sz!_chain_future.._set_statecs8|rdus urdSjdSdSr,)rDr=call_soon_threadsafe) destination) dest_looprw source_looprr_call_check_cancels  z)_chain_future.._call_check_cancelcsXrdurrdSdusur|dSr#dS|dSr,)rD is_closedr})rw)r|rr~rrr_call_set_statesz&_chain_future.._call_set_state)rr[rprqrr^rnrP)rwr~rrr)r|rr~rwrr _chain_futureks   rr cCsNt|r|St|tjjsJd||durt}|}t|||S)z&Wrap concurrent.futures.Future object.z+concurrent.futures.Future is expected, got N) rr[rprqrrr create_futurer)r$r new_futurerrrrs r) re__all__concurrent.futuresrprLloggingrtypesrrrr r rr9r:rFDEBUG STACK_DEBUGr _PyFuturernrorurxrzrr_asyncio ImportError_CFuturerrrrsB         ,