
    ^h                        S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SKJr  S SK	J
r
JrJrJrJr  / SQr\" S5      rSS.       SS	 jjr  S       SS
 jjrSS jrg)    )annotationsN)get_running_loop)TracebackType)Any	AwaitableCallableTypeVarcast)run_in_executor_with_contextcall_soon_threadsafeget_traceback_from_context_T)loopc                   U=(       d
    [        5       n[        R                  " 5       nUR                  " SUR                  U /UQ76 $ )z
Run a function in an executor, but make sure it uses the same contextvars.
This is required so that the function will see the right application.

See also: https://bugs.python.org/issue34014
N)r   contextvarscopy_contextrun_in_executorrun)funcr   argsctxs       X/home/james-whalen/.local/lib/python3.13/site-packages/prompt_toolkit/eventloop/utils.pyr   r      s>     %#%D*779Ccggt;d;;    c                   ^ ^^^ U=(       d
    [        5       mUc  TR                  T 5        g[        R                  " 5       U-   mSU UUU4S jjmTR                  T5        g)a  
Wrapper around asyncio's `call_soon_threadsafe`.

This takes a `max_postpone_time` which can be used to tune the urgency of
the method.

Asyncio runs tasks in first-in-first-out. However, this is not what we
want for the render function of the prompt_toolkit UI. Rendering is
expensive, but since the UI is invalidated very often, in some situations
we render the UI too often, so much that the rendering CPU usage slows down
the rest of the processing of the application.  (Pymux is an example where
we have to balance the CPU time spend on rendering the UI, and parsing
process output.)
However, we want to set a deadline value, for when the rendering should
happen. (The UI should stay responsive).
Nc                    > [        TS/ 5      (       d  T " 5         g [        R                  " 5       T:  a  T " 5         g TR                  T5        g )N_ready)getattrtimer   )r   loop2max_postpone_untilschedules   r   r!   &call_soon_threadsafe.<locals>.scheduleC   sE    
 uh++F 99;++F 	""8,r   )returnNone)r   r   r   )r   max_postpone_timer   r   r    r!   s   `  @@@r   r   r   %   sX    * &$&E  ""4('88- -" 
x(r   c                    U R                  S5      nU(       aC  [        US5      (       a  [        [        UR                  5      $ [
        R                  " 5       S   $ g)z,
Get the traceback object from the context.
	exception__traceback__   N)gethasattrr
   r   r(   sysexc_info)contextr'   s     r   r   r   W   sK     K(I9o..y'>'>??
 <<>!$$r   )r   zCallable[..., _T]r   r   r    asyncio.AbstractEventLoop | Noner#   zAwaitable[_T])NN)r   zCallable[[], None]r%   zfloat | Noner   r/   r#   r$   )r.   zdict[str, Any]r#   zTracebackType | None)
__future__r   asyncior   r,   r   r   typesr   typingr   r   r   r	   r
   __all__r   r   r   r    r   r   <module>r6      s    "   
  $  : : T] .2<
<< +< 	<& '+-1/)
/)#/) +/) 
	/)dr   