o
    yAfR                     @  s0  d dl mZ 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mZmZ ddlmZ dZd	Zd
ZdZdd ZefddZG dd dZdDddZG dd dejZejjZejjZe ejddZ!ej"ej#u rej$ej_$ejej_ejej_ee_"e ej"ddd v rej$ej_$ejej_ejej_d d!l	m%Z% d dl&Z&ej"e&j'u sJ e%d"Z(e(j)*ej) ee(_#e(e&_edEd%d&Zej+dFd(d)Z,e		*dGdHd/d0Z-e-j+		*dGdId2d)Z,e			*dJdKd5d6Z.e.j+			*dJdLd7d)Z,			*dJdMd9d:Z/d;Z0d<Z1d=Z2dZ3e4 Z5d>d? Z6d@dA Z7dBdC Z8dS )N    )annotationsN)singledispatch)TracebackType)AnyListOptional   )BaseExceptionGroup   
   zG
The above exception was the direct cause of the following exception:

zF
During handling of the above exception, another exception occurred:

c                 C  s8   t |d}|d u s|s|  d}|S |  d| d}|S )N	exception
z: )_safe_string)etypevaluevaluestrline r   ?/usr/lib/python3.10/site-packages/exceptiongroup/_formatting.py_format_final_exc_line   s   

r   c                 C  s2   z|| W S  t y   d| d|j d Y S w )N< z
() failed>)BaseException__name__)r   whatfuncr   r   r   r   &   s
   
r   c                   @  s&   e Zd Zdd Zdd ZdddZdS )	_ExceptionPrintContextc                 C  s   t  | _d| _d| _d S )Nr   F)setseenexception_group_depth
need_closeselfr   r   r   __init__.   s   
z_ExceptionPrintContext.__init__c                 C  s   dd| j   S )Nr      )r   r!   r   r   r   indent3   s   z_ExceptionPrintContext.indentNc                 c  sl    |d u rd}|   }| jr||d 7 }t|tr%t ||dd V  d S |D ]}t ||dd V  q'd S )N|r   c                 S     dS NTr   r   r   r   r   <lambda>>       z-_ExceptionPrintContext.emit.<locals>.<lambda>c                 S  r'   r(   r   r)   r   r   r   r*   A   r+   )r%   r   
isinstancestrtextwrap)r"   Ztext_genmargin_charZ
indent_strtextr   r   r   emit6   s   
z_ExceptionPrintContext.emitN)r   
__module____qualname__r#   r%   r1   r   r   r   r   r   -   s    r   r   type[BaseException]r   r   tbTracebackType | NonereturnNonec              	   C  s    t jdt| || d S )N )sysstderrwritejoin	tracebackformat_exception)r   r   r6   r   r   r   exceptiongroup_excepthookD   s    rA   c                   @  s<   e Zd ZdddddddddZdddddZdd ZdS )PatchedTracebackExceptionNTF)limitlookup_linescapture_localscompact_seenexc_typer5   	exc_valuer   exc_tracebackr7   rC   
int | NonerD   boolrE   rF   rG   set[int] | Noner8   r9   c             	   C  sR  i }	t jdkr||	d< |d u}
|d u rt }|t| tjjt||||d| _	|| _
t|d| _z	t|dd | _W n' tyd   tt jdd dd}t jd d	 d
krat||rad | _n Y nw |rt|tr|j| _|j}|d ur{t|nd | _|j| _|j| _|j| _t jdkr|j}|d urt|nd | _|j| _n#|rt|ttfrt|dd d urt||}|r|  jd| d7  _|r| j	D ]}|j  q|d ur|j!nd| _!|
s| |fg}|r|" \}}|r|j#d urt|j#|vrt$t%|j#|j#|j#j&||||d}nd }|r"|d u o |d uo |j! }nd}|rJ|j'd urJ|rJt|j'|vrJt$t%|j'|j'|j'j&||||d}nd }|rqt|t(rqg }|j)D ]}t$t%|||j&|||d}|*| qZnd }||_#||_'||_)|r|*|j#|j#f |r|*|j'|j'f |r|+t,|j)|j) |sd S d S d S )N)   r   rF   )rC   rD   rE   r   	__notes__zurllib.error	HTTPErrorr   r$   )rN      namez. Did you mean: 'z'?F)rC   rD   rE   rG   T)rD   rE   rG   )-r;   version_infor   addidr?   StackSummaryextractwalk_tbstackrH   r   _strgetattrrO   KeyErrormodulesgetr,   
issubclassSyntaxErrorfilenamelinenor-   r0   offsetmsg
end_lineno
end_offset	NameErrorAttributeError_compute_suggestion_errorr   __suppress_context__pop	__cause__rB   type__traceback____context__r	   
exceptionsappendextendzip)r"   rH   rI   rJ   rC   rD   rE   rF   rG   kwargsis_recursive_callrP   lnoend_lno
suggestionframequeueteecauseneed_contextcontextrp   excZtexcr   r   r   r#   K   s   
	



"


z"PatchedTracebackException.__init__chain_ctxc             	   c  s   |d u rt  }g }| }|r9|r8|jd urt}|j}n|jd ur)|js)t}|j}nd }d }|||f |}|sn|d |f t|D ]#\}}|d urU||E d H  |j	d u r{|j
rp|dE d H  ||j
 E d H  || E d H  qD|jtkr|dt dE d H  qD|jdk}|r| jd7  _|j
r|jd|rdnd dE d H  ||j
 E d H  || E d H  t|j	}	|	tkr|	}
ntd }
d	|_t|
D ]}||
d k}|rd
|_td ur|tk}nd	}|s|d  nd}| |dkrdnd d| d V  | jd7  _|s%|j	| j||dE d H  n|	t }|dkr0dnd}|d| d| dE d H  |rR|jrR| d V  d	|_| jd8  _q|rh|jdkseJ d|_qDd S )Nz#Traceback (most recent call last):
z... (max_group_depth is z)
r   r   z3Exception Group Traceback (most recent call last):
+)r/   FTz...z+-z  z+---------------- z ----------------
r   sr:   zand z more exceptionr   z&+------------------------------------
)r   rl   _cause_messagero   rj   _context_messagerq   reversedr1   rp   rY   formatformat_exception_onlyr   max_group_depthlenmax_group_widthr    ranger%   )r"   r   r   outputr   chained_msgchained_excrd   Zis_toplevelZnum_excsnilast_exc	truncatedtitle	remainingpluralr   r   r   r      s   








z PatchedTracebackException.formatc                 c  s    | j du rtd| jV  dS | j j}| j j}|dvr)t|ts#d}|d | }t| j t	s7t|| jV  nt
durDt
| |E dH  nt| E dH  t| jtjjrm| jD ]}t|d}dd |dD E dH  qVdS | jdur}t| jd	td
V  dS dS )a  Format the exception part of the traceback.
        The return value is a generator of strings, each ending in a newline.
        Normally, the generator emits a single string; however, for
        SyntaxError exceptions, it emits several lines that (when
        printed) display detailed information about where the syntax
        error occurred.
        The message indicating which exception occurred is always the last
        string in the output.
        N)__main__builtinsz	<unknown>.notec                 S  s   g | ]}|d  qS )r   r   ).0r   r   r   r   
<listcomp>R  s    zCPatchedTracebackException.format_exception_only.<locals>.<listcomp>r   rO   )r   )rH   r?   r   rZ   r4   r3   r,   r-   r_   r`   'traceback_exception_format_syntax_error2traceback_exception_original_format_exception_onlyrO   collectionsabcSequencer   splitrepr)r"   stypesmodr   r   r   r   r   3  s.   





z/PatchedTracebackException.format_exception_only)rH   r5   rI   r   rJ   r7   rC   rK   rD   rL   rE   rL   rF   rL   rG   rM   r8   r9   )r   r3   r4   r#   r   r   r   r   r   r   rB   J   s     YrB   _format_syntax_errorr   )apport_excepthookZpartial_apport_excepthook)
ModuleTypeZexceptiongroup_fake_sys__exc	List[str]c                 C  s   t tt| | d dd S )NT)rF   )listrB   rm   r   )r   r   r   r   r     s   r   rm   c                 C  s   t |S r2   )r   )r   r   r   r   r   _  s   r   TrC   Optional[int]r   rL   c                 C  s$   t tt| | | j|ddj|dS )NT)rC   rF   r   )r   rB   rm   rn   r   )r   rC   r   r   r   r   r@     s   r@   r   c                 C  s   t |||S r2   )r@   )r   r   r6   rC   r   r   r   r   r     s   filer   c                 C  sD   |d u rt j}tt| | | j|dj|dD ]	}t||dd qd S )N)rC   r   r:   )r   end)r;   r<   rB   rm   rn   r   print)r   rC   r   r   r   r   r   r   print_exception  s   
r   c                 C  s   t |||| d S r2   )r   )r   r   r6   rC   r   r   r   r   r   r     s   	
Any | Nonec                 C  s   t  d }t|| || d S )Nr   )r;   exc_infor   )rC   r   r   r   r   r   r   	print_exc  s   r   i  (   r$   c                 C  s$   | |krdS |   |  krtS tS )Nr   )lower
_CASE_COST
_MOVE_COST)Zch_aZch_br   r   r   _substitution_cost  s
   r   c                 C  sX  t | dd }|d u st|tsd S t| tr6t | dt}|tu r"d S | j}zt|}W n6 ty5   Y d S w t| ts=J |d u rCd S |j	d urP|j	}|j	d usH|j
}t|jt|j t|j }t|tkrjd S t|}|tkrtd S |}d }|D ]/}	|	|krqzt|	| d t d }
t|
|d }
t||	|
}||
krqz|r||k r|	}|}qz|S )NrR   objrN      r   )r[   r,   r-   rh   	_SENTINELr   dir	Exceptionrg   tb_nexttb_framer   f_locals	f_globals
f_builtinsr   _MAX_CANDIDATE_ITEMS_MAX_STRING_SIZEr   min_levenshtein_distance)rI   r6   Z
wrong_namer   dry   Zwrong_name_lenZbest_distancerx   Zpossible_nameZmax_distanceZcurrent_distancer   r   r   ri     sV   


ri   c                 C  sR  | |krdS d}| |d  r4||d  r4| | || kr4|d7 }| |d  r4||d  r4| | || ks| |d  } ||d  }d}| d |pGd  r~|d |pOd  r~| |d  ||d  kr~|d8 }| d |pgd  r~|d |pod  r~| |d  ||d  ks^| d |pd  } |d |pd  }| r|st t| t|  S t| tkst|tkr|d S t|t| k r|| } }t|t|  t  |kr|d S ttt t t| d  t }d}tt|D ]G}|| }|t   }	}tj}
tt| D ]&}|	t|| |  }|| }	t||	t  }t||}|||< ||
k r|}
q|
|kr&|d   S q|S )Nr   r   )	r   r   r   r   r   r;   maxsizer   r   )abZmax_costprepostrowresultZbindexZbcharZdistanceminimumindex
substituteZinsert_deleter   r   r   r   !  sR   ((88



r   )r   r5   r   r   r6   r7   r8   r9   )r   r   r8   r   )r   rm   r   r   r8   r   r(   )r   r   rC   r   r   rL   r8   r   )r   rm   r   r   r6   r   rC   r   r   rL   r8   r   )NNT)
r   r   rC   r   r   r   r   rL   r8   r9   )r   rm   r   r   r6   r   rC   r   r   r   r   rL   r8   r9   )rC   r   r   r   r   rL   r8   r9   )9
__future__r   collections.abcr   r;   r.   r?   	functoolsr   typesr   typingr   r   r   _exceptionsr	   r   r   r   r   r   r-   r   r   rA   TracebackExceptionrB   r   Z#traceback_exception_original_formatr   r   r[   r   
excepthook__excepthook__r#   r   Zapport_python_hookr   Zfake_sys__dict__updateregisterr   r@   r   r   r   r   r   r   objectr   r   ri   r   r   r   r   r   <module>   s   

  
1