o
    ^i!6                     @  s  U d dl mZ dg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	 d dlm
Z
 d dlZd dlZd d	lmZmZ d
dlmZ d
dlm	Z d dlmZ d dlmZmZ d dlmZmZmZmZ d dlZejdkrvd dlmZm Z m!Z! n
d dlmZm Z m!Z! edZ"edddZ#eddddZ$eddddZ%edddZ&edddZ'edZ"edddZ(dZ)de*d< ejd kre eee(f Z+de*d!< n ejdkreee)de(f e eee(f f Z+de*d!< nd"Z+de*d!< G d#d$ d$Z,G d%d de,Z-dS )&    )annotations	TaskGroupN)TracebackType)events)
exceptions)tasks)futures)OptionalType   )install)BaseExceptionGroup)AnyUnion)Self	TypeAliasLiteralTypeVar)   	   )	Generator	Coroutine	Awaitable_T
_YieldT_coT)	covariant_SendT_contra)contravariantdefault_ReturnT_co)r   r   _SendT_contra_nd)r   _ReturnT_co_nd_T_cozfutures.Future[object] | Noner   _TaskYieldType)r      _TaskCompatibleCoroz9Generator[_TaskYieldType, None, _T_co] | Awaitable[_T_co]c                   @  sr   e Zd ZdZd,ddZd-ddZd.d
dZd/ddZd0ddZdddd1d d!Z	d2d$d%Z
d,d&d'Zd3d*d+ZdS )4
_TaskGroupa9  Asynchronous context manager for managing groups of tasks.

    Example use:

        async with asyncio.TaskGroup() as group:
            task1 = group.create_task(some_coroutine(...))
            task2 = group.create_task(other_coroutine(...))
        print("Both tasks have completed now.")

    All tasks are awaited when the context manager exits.

    Any exceptions other than `asyncio.CancelledError` raised within
    a task will cancel all remaining tasks and wait for them to exit.
    The exceptions are then combined and raised as an `ExceptionGroup`.
    returnNonec                 C  sB   d| _ d| _d| _d | _d | _d| _t | _g | _d | _	d | _
d S )NF)_entered_exiting	_aborting_loop_parent_task_parent_cancel_requestedset_tasks_errors_base_error_on_completed_futself r6   Y/var/www/html/stock_analysis/be/venv/lib/python3.10/site-packages/taskgroup/taskgroups.py__init__J   s   
z_TaskGroup.__init__strc                 C  sv   dg}| j r|dt| j   | jr|dt| j  | jr(|d n| jr0|d d|}d| dS )	N ztasks=zerrors=
cancellingentered z
<TaskGroup>)r0   appendlenr1   r+   r)   join)r5   infoinfo_strr6   r6   r7   __repr__V   s   

z_TaskGroup.__repr__r   c                   s^   | j rtd| d| jd u rt | _t| j| _| jd u r*td| dd| _ | S )N
TaskGroup z has already been enteredz! cannot determine the parent taskT)r)   RuntimeErrorr,   r   get_running_loopr   current_taskr-   r4   r6   r6   r7   
__aenter__d   s   


z_TaskGroup.__aenter__etOptional[Type[BaseException]]excOptional[BaseException]tbOptional[TracebackType]Optional[bool]c                   sJ   d }z|  ||I d H W d | _d | _d | _d }S d | _d | _d | _d }w N)_aexitr-   r1   r2   )r5   rJ   rL   rN   r6   r6   r7   	__aexit__p   s   z_TaskGroup.__aexit__c              
     s  | j d usJ | jd usJ | jd usJ d| _|d ur*| |r*| jd u r*|| _|tju r1|nd }| jr?| j 	 dkr?d }|d urJ| j
sJ|   | jr| jd u rX| j | _z| jI d H  W n tjy| } z| j
sr|}|   W Y d }~nd }~ww d | _| jsM| jrJ | jd urz| jd }w z|r| jsz|d }w W d }nd }w |d ur|tjur|d usJ | j| | jrztd| jd d }w d S )NTr   zunhandled errors in a TaskGroup)r-   r,   r1   r*   _is_base_errorr2   r   CancelledErrorr.   uncancelr+   _abortr0   r3   create_futurer?   r   )r5   rJ   rL   propagate_cancellation_errorexr6   r6   r7   rR      sh   
	


z_TaskGroup._aexitNnamecontextcoro_TaskCompatibleCoro[_T_co]r\   
str | Noner]   contextvars.Context | Nonetasks.Task[_T_co]c                C  s   | j std| d| jr| jstd| d| jr$td| d| jdus+J |du r6| j|}n| jj||d}t|| |	 rO| 
| |S | j| || j
 |S zbCreate a new task in this group and return it.

        Similar to `asyncio.create_task`.
        rE   z has not been enteredz is finishedz is shutting downN)r]   )r)   rF   r*   r0   r+   r,   create_taskr   _set_task_namedone_on_task_doneaddadd_done_callbackr5   r^   r\   r]   taskr6   r6   r7   rd      s"   
z_TaskGroup.create_taskBaseExceptionboolc                 C  s   t |tsJ t |ttfS rQ   )
isinstancerl   
SystemExitKeyboardInterrupt)r5   rL   r6   r6   r7   rT     s   z_TaskGroup._is_base_errorc                 C  s&   d| _ | jD ]
}| s|  qd S )NT)r+   r0   rf   cancel)r5   tr6   r6   r7   rW   
  s   
z_TaskGroup._abortrk   tasks.Task[object]c                 C  s  | j d usJ | jd usJ | jd usJ | j| | jd ur.| js.| j s.| jd | r4d S |	 }|d u r>d S | j 
| | |rQ| jd u rQ|| _| j rj| jd|d| j d||d d S | js~| js|   d| _| j  d S d S d S )NTzTask z% has errored out but its parent task z is already completed)message	exceptionrk   )r1   r-   r,   r0   discardr3   rf   
set_result	cancelledru   r?   rT   r2   call_exception_handlerr+   r.   rW   rq   )r5   rk   rL   r6   r6   r7   rg     s<   

z_TaskGroup._on_task_done)r'   r(   )r'   r9   r'   r   )rJ   rK   rL   rM   rN   rO   r'   rP   )rJ   rK   rL   rM   r'   rP   )r^   r_   r\   r`   r]   ra   r'   rb   )rL   rl   r'   rm   )rk   rs   r'   r(   )__name__
__module____qualname____doc__r8   rD   rI   rS   rR   rd   rT   rW   rg   r6   r6   r6   r7   r&   9   s    




b
%
r&   c                      sD   e Zd ZU ded< ddddddZd fddZdddZ  ZS )r   zcontextlib.AsyncExitStack_TaskGroup__stackNr[   r^   r_   r\   r`   r]   ra   r'   _tasks.Task[_T_co]c                C  s   | j std| d| jr| jstd| d| jr$td| d| jdus+J |du r7t| j|}n	tj| j||d}t|| |	 rQ| 
| |S | j| || j
 |S rc   )r)   rF   r*   r0   r+   r,   task_factoryr   re   rf   rg   rh   ri   rj   r6   r6   r7   rd   N  s"   
zTaskGroup.create_taskr   c              	     s   t  4 I d H *}|t I d H  t  I d H  |t j |	 | _
W d   I d H  | S 1 I d H s9w   Y  | S rQ   )
contextlibAsyncExitStackenter_async_context_installinstall_uncancelsuperrI   push_async_exitrS   pop_allr   )r5   stack	__class__r6   r7   rI   o  s   zTaskGroup.__aenter__exc_typerK   exc_valrM   exc_tbrO   rP   c                   s   | j |||I d H S rQ   )r   rS   )r5   r   r   r   r6   r6   r7   rS   w  s   zTaskGroup.__aexit__)r^   r_   r\   r`   r]   ra   r'   r   rz   )r   rK   r   rM   r   rO   r'   rP   )r{   r|   r}   __annotations__rd   rI   rS   __classcell__r6   r6   r   r7   r   K  s   
 !).
__future__r   __all__systypesr   asyncior   r   r   r   contextvarstypingr	   r
   r:   r   r   r0   exceptiongroupr   r   r   typing_extensionsr   r   r   r   r   version_infocollections.abcr   r   r   r   r   r   r   r    r!   r"   r#   r   r%   r&   r   r6   r6   r6   r7   <module>   sV   



  