
    kj
                     R   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
  e       Zdddeded	ed
edz  de j                   f
dZddde j                   ded
edz  ddfdZddded
edz  ddfdZde j                   defdZeded   fd       Zeded   fd       Zy)    N)	Coroutine)Iterator)contextmanager)human)clientcoronamekeep_refr   returnc                    t        j                  |       }t        |||       |rD|j                         s4t        j                  |       |j                  t        j                         |S )a  
    Wrapper around `asyncio.create_task`.

    - Use `keep_ref` to keep an internal reference.
      This ensures that the task is not garbage collected mid-execution if no other reference is kept.
    - Use `client` to pass the client address as additional debug info on the task.
    r	   r   )asynciocreate_taskset_task_debug_infodone_KEEP_ALIVEaddadd_done_callbackdiscard)r   r	   r
   r   ts        f/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/mitmproxy/utils/asyncio_utils.pyr   r      sR     	D!AV4 		K//0H    taskc                    t        j                          | _        ddu r*t        j                  j	                  dd      x}r| d| d}| j                  |       |r|| _        yy)z.Set debug info for an externally-spawned task.TPYTEST_CURRENT_TESTNz [created in ])timecreatedosenvirongetset_namer   )r   r	   r   tests       r   r   r   &   s`     99;DLDbjjnn5JD&QQdQ}TF!,MM$ r   c                 P    t        j                         }|sJ t        || |       y)z$Set debug info for the current task.r   N)r   current_taskr   )r	   r   r   s      r   set_current_task_debug_infor&   5   s%     !DK447r   c                     | j                         }t        | dd      }|rdt        j                         |z
  dd}nd}t        | dd      }|rt        j                  |       d}| | | S )	z*Get a task representation with debug info.r   r   z (age: z.0fzs) r   z: )get_namegetattrr   r   format_address)r   r	   aager   s        r   	task_reprr.   @   sz    ==?DtY*A		a,B/T8R(F((014XdVC5!!r   c              #      K   t        j                         }|j                         }|j                  |        	 d  |j                  |       y # |j                  |       w xY ww)N)r   get_running_loopget_exception_handlerset_exception_handler)handlerloopexistings      r   install_exception_handlerr6   N   sT     ##%D))+Hw'-""8,""8,s   6A%A A%A""A%c               #     K   t        j                         } t        j                  dk  rd  y | j	                         }| j                  t         j                         	 d  | j                  |       y # | j                  |       w xY ww)N)      )r   r0   sysversion_infoget_task_factoryset_task_factoryeager_task_factory)r4   r5   s     r   set_eager_task_factoryr?   Y   sk     ##%D
'!((*g889	,!!(+D!!(+s   ABA5 #B5BB)r   r   r:   r   collections.abcr   r   
contextlibr   mitmproxy.utilsr   setr   strbooltupleTaskr   r   r&   r.   r6   r?    r   r   <module>rI      s    	 
  % $ % !e  
  	
 DL \\8  	
,,  DL	
 
$  8
8 DL8 
	8"GLL "S " -(4. - - 
, 
, 
,r   