U
    gH                     @  s   d dl mZ d dlZd dlZd dlmZmZmZ d dlm	Z	 d dl
mZ d dlmZmZ ejdk rndd	lmZ ereee gef ZG d
d dZdddddZdS )    )annotationsN)CallableIterableMapping)AbstractContextManager)TracebackType)TYPE_CHECKINGAny)         )BaseExceptionGroupc                   @  sL   e Zd ZddddZddddZd	d
dddddZdd
dddZdS )_Catcherz2Mapping[tuple[type[BaseException], ...], _Handler])handler_mapc                 C  s
   || _ d S N)_handler_map)selfr    r   9/tmp/pip-unpacked-wheel-9cx8664k/exceptiongroup/_catch.py__init__   s    z_Catcher.__init__None)returnc                 C  s   d S r   r   )r   r   r   r   	__enter__   s    z_Catcher.__enter__ztype[BaseException] | NonezBaseException | NonezTracebackType | Nonebool)etypeexctbr   c                 C  sj   |d k	rf|  |}||krdS |d kr*dS t|tr`z||jW n tk
r^   |j|_ Y nX ||dS )NFT)handle_exception
isinstancer   	__cause____context__)r   r   r   r   Z	unhandledr   r   r   __exit__   s    

z_Catcher.__exit__BaseException)r   r   c           	      C  sb  t |tr|}ntd|g}g }| j D ]\}}||\}}|rz*z|W n tk
rj   ||}Y nX W nl tk
r } z$||kr|| n||j W 5 d }~X Y nN tk
r } z|| W 5 d }~X Y n$X t	
|rtd|d|d||s* q
q*|r0t|dkr&|d S td|S |rZt|jdkrZ|jd |krZ|S |S d S )N zError trying to handle z with z,. Exception handler must be a sync function.r   r   )r   r   r   itemssplitappendextend
exceptionsr"   inspectiscoroutine	TypeErrorlen)	r   r   ZexcgroupZnew_exceptionsZ	exc_typeshandlermatchedresultnew_excr   r   r   r   2   sL    


z_Catcher.handle_exceptionN)__name__
__module____qualname__r   r   r!   r   r   r   r   r   r      s   r   zFMapping[type[BaseException] | Iterable[type[BaseException]], _Handler]zAbstractContextManager[None])
__handlersr   c                 C  s   t | tstdi }|  D ]\}}t |trBt|trB|f}nt |trVt|}ntdt	|sntd|D ]2}t |trt|tstdt|t
rrtdqr|||< qt|S )Nzthe argument must be a mappingzCeach key must be either an exception classes or an iterable thereofzhandlers must be callablezHcatching ExceptionGroup with catch() is not allowed. Use except instead.)r   r   r+   r$   type
issubclassr"   r   tuplecallabler   r   )r4   r   Ztype_or_iterabler-   iterableexc_typer   r   r   catch`   s>    
 

 

r;   )
__future__r   r)   syscollections.abcr   r   r   
contextlibr   typesr   typingr   r	   version_info_exceptionsr   Z_Handlerr   r;   r   r   r   r   <module>   s   
O