o ?Ogk7@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 _callbacksZ get_debugr extract_stacksys _getframe_source_tracebackselfr r6/opt/alt/python310/lib64/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 exceptionfutureZsource_traceback)_Future__log_traceback _exceptionrrrrZcall_exception_handler)rexccontextrrr__del__^s  zFuture.__del__cCs|jSN)r$rrrr_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 _CANCELLEDr/_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)rZ callbackscallbackctxrrrZ__schedule_callbackss zFuture.__schedule_callbackscCs |jtkS)z(Return True if the future was cancelled.)r5r7rrrr 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. )r5r6rrrrdones 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) r5r7r4 _FINISHEDr InvalidStateErrorr$r%with_traceback _exception_tb_resultr3rrrresults    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)r5r7r4rAr rBr$r%r3rrrr"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)r5r6rr< contextvarsZ 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).0fr>rIrr s z/Future.remove_done_callback..N)rlen)rrIZfiltered_callbacksZ removed_countrrMrremove_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)r5r6r rBrErAr8)rrFrrr 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. rQzPStopIteration interacts badly with generators and cannot be raised into a FutureTN)r5r6r rB isinstancetype StopIteration TypeErrorr% __traceback__rDrAr8r$)rr"rrr set_exceptions    zFuture.set_exceptionccs.|s d|_|V|std|S)NTzawait wasn't used with future)r@_asyncio_future_blockingr-rFrrrr __await__szFuture.__await__r))(r __module__ __qualname____doc__r6r5rEr%rrr/r1rYr$rrZ_future_repr_inforr r( classmethodr__class_getitem__propertyr*setterr.r4r:r8r?r@rFr"rJrPrRrXrZ__iter__rrrrrsD     rcCs&z|j}W|StyY|jSwr))r.AttributeErrorr)futr.rrr _get_loop)s recCs|rdS||dS)z?Helper setting the result only if the future was not cancelled.N)r?rR)rdrFrrr_set_result_unless_cancelled5srfcCsTt|}|tjjurtj|jS|tjjurtj|jS|tjjur(tj|jS|Sr))rT concurrentfuturesr0r args TimeoutErrorrB)r&Z exc_classrrr_convert_future_exc<s      rkcCs`|sJ|r||sdS|}|dur%|t|dS|}||dS)z8Copy state from a future to a concurrent.futures.Future.N) r@r?r:Zset_running_or_notify_cancelr"rXrkrFrR)rgsourcer"rFrrr_set_concurrent_future_stateHs rmcCsp|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)r@r?r:r"rXrkrFrR)rldestr"rFrrr_copy_future_stateWs   rocststtjjstdtsttjjstdtr&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))rrorm)r#otherrrr _set_state{sz!_chain_future.._set_statecs8|rdus urdSjdSdSr))r?r:call_soon_threadsafe) destination) dest_looprl source_looprr_call_check_cancels  z)_chain_future.._call_check_cancelcsXrdurrdSdusur|dSr#dS|dSr))r?Z is_closedrr)rl)rqrtrsrurr_call_set_statesz&_chain_future.._call_set_state)rrSrgrhrrVrerJ)rlrsrvrwr)rqrtrsrlrur _chain_futureks   rxr cCsNt|r|St|tjjsJd||durt}|}t|||S)z&Wrap concurrent.futures.Future object.z+concurrent.futures.Future is expected, got N) rrSrgrhrrr Z create_futurerx)r#r Z new_futurerrrrs r) r]__all__Zconcurrent.futuresrgrGZloggingrtypesrrrr r rr6r7rADEBUGZ STACK_DEBUGrZ _PyFuturererfrkrmrorxrZ_asyncio ImportErrorZ_CFuturerrrrsB         ,