o
    vi                  '   @   s4  d dl Z d dl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mZ d dlmZmZmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZmZmZm Z m!Z!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*m+Z+m,Z, ddl-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZCmDZD dd	lEm#Z# ddlFmGZG ejHdkZIdZJdZKdeDdeDdeeeLeLeLf  fddZMdd d!d"dd#d#eJeKdd$ddd$d%d d&d'ee0 d(eeL d)eeL d*eLd+eeN d,eOd-eOd.eLd/eLd0eeL d1eOd2eeO d3ee3 d4eOd5eeeNef  d6eLdeeeP ePee gef f"d7d8ZQeG d9d: d:ZReG d;d< d<ZSeG d=d> d>ZTeG d?d@ d@ZUG dAdB dBe:ZVG dCdD dDZWeXdEkreQd$dF d dlZdGeddfdHdIZYdGeddfdJdKZZdNdLdMZ[e[  dS dS )O    N)	dataclassfield)islice)walk_tb)
ModuleTypeTracebackType)AnyCallableDictIterableListOptionalSequenceSetTupleTypeUnion)guess_lexer_for_filename)CommentKeywordNameNumberOperatorString)Text)Token)ClassNotFound   )pretty)loop_first_last	loop_last)Columns)ConsoleConsoleOptionsConsoleRenderableOverflowMethodGroupRenderResultgroup)	Constrain)RegexHighlighterReprHighlighter)Panel)render_scope)Style)SyntaxSyntaxPosition)Themewin32
   P   startendreturnc           	      c   s|    | \}}|\}}||kr|||fV  dS t t||d D ]\}}}|r,||dfV  q|r5|d|fV  q|ddfV  qdS )zYield start and end positions per line.

    Args:
        start: Start position.
        end: End position.

    Returns:
        Iterable of (LINE, COLUMN1, COLUMN2).
    r   r   N)r   range)	r5   r6   line1column1line2column2firstlastline_no rA   M/home/air/biblejyuku/back/venv/lib/python3.10/site-packages/rich/traceback.py_iter_syntax_lines8   s   rC   d   X      FTrA   )consolewidth
code_widthextra_linestheme	word_wrapshow_localslocals_max_lengthlocals_max_stringlocals_max_depthlocals_hide_dunderlocals_hide_sunderlocals_overflowindent_guidessuppress
max_framesrG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   c                    s   | du r	t ddn| jrdu rdndtt dtdtt ddf 	
fdd	d
tddffdd}zt }|| tj	W S  t
y_   tj	}t_	| Y S w )a  Install a rich traceback handler.

    Once installed, any tracebacks will be printed with syntax highlighting and rich formatting.


    Args:
        console (Optional[Console], optional): Console to write exception to. Default uses internal Console instance.
        width (Optional[int], optional): Width (in characters) of traceback. Defaults to 100.
        code_width (Optional[int], optional): Code width (in characters) of traceback. Defaults to 88.
        extra_lines (int, optional): Extra lines of code. Defaults to 3.
        theme (Optional[str], optional): Pygments theme to use in traceback. Defaults to ``None`` which will pick
            a theme appropriate for the platform.
        word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False.
        show_locals (bool, optional): Enable display of local variables. Defaults to False.
        locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
            Defaults to 10.
        locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80.
        locals_max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None.
        locals_hide_dunder (bool, optional): Hide locals prefixed with double underscore. Defaults to True.
        locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False.
        locals_overflow (OverflowMethod, optional): How to handle overflowing locals, or None to disable. Defaults to None.
        indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True.
        suppress (Sequence[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback.

    Returns:
        Callable: The previous exception handler that was replaced.

    NT)stderrtype_value	tracebackr7   c                    sD   t j| ||f 
t	d}| d S )NrH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   )	Tracebackfrom_exceptionboolprint)rX   rY   rZ   exception_traceback)rI   rJ   rT   rQ   rR   rP   rN   rO   rS   rV   rM   rU   rK   traceback_consolerH   rL   rA   rB   
excepthook   s,   zinstall.<locals>.excepthookipc              	      sl   i j  dtdtdd f fdd}dddtdtdtdd ffd	d
_|_ fdd_d S )Nargskwargsr7   c                     s   | | i | dS )zEwrap the default ip.showtraceback to store info for ip._showtracebackNrA   rd   re   )default_showtracebacktb_datarA   rB   ipy_show_traceback   s   zCinstall.<locals>.ipy_excepthook_closure.<locals>.ipy_show_tracebackF)	is_syntaxrj   c                    s~     }| rdn|d }dd}d}|du r!|rdnd}t|D ]}|du r- n|j}q% |d |d | i dS )z2Internally called traceback from ip._showtracebackN   running_compiled_codeF	tb_offsetr   r   )_get_exc_infogetr9   tb_next)rj   rd   re   	exc_tupletbcompiledrm   _)rb   rc   rh   rA   rB   ipy_display_traceback   s   
zFinstall.<locals>.ipy_excepthook_closure.<locals>.ipy_display_tracebackc                     s    | ddi|S )Nrj   TrA   rf   )ru   rA   rB   <lambda>   s    z9install.<locals>.ipy_excepthook_closure.<locals>.<lambda>)showtracebackr   r^   _showtracebackshowsyntaxerror)rc   ri   )rb   )rg   rc   ru   rh   rB   ipy_excepthook_closure   s    z'install.<locals>.ipy_excepthook_closure)r"   
is_jupyterr   BaseExceptionr   r   r   get_ipythonsysrb   	Exception)rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rz   rc   old_excepthookrA   )rI   rb   rJ   rT   rQ   rR   rP   rN   rO   rS   rV   rM   rU   rK   ra   rH   rL   rB   installT   s4   /,+r   c                   @   st   e Zd ZU eed< eed< eed< dZeed< dZee	ee
jf  ed< dZeeeeef eeef f  ed< dS )	Framefilenamelinenoname lineNlocalslast_instruction)__name__
__module____qualname__str__annotations__intr   r   r   r
   r   Noder   r   rA   rA   rA   rB   r      s   
 ,r   c                   @   sL   e Zd ZU eed< eed< eed< eed< eed< eedZe	e ed< dS )	_SyntaxErroroffsetr   r   r   msgdefault_factorynotesN)
r   r   r   r   r   r   r   listr   r   rA   rA   rA   rB   r      s   
 r   c                   @   s   e Zd ZU eed< eed< dZee ed< dZe	ed< e
edZee ed< e
edZee ed	< dZe	ed
< e
edZed ed< dS )Stackexc_type	exc_valueNsyntax_errorFis_causer   framesr   is_groupTrace
exceptions)r   r   r   r   r   r   r   r   r   r^   r   r   r   r   r   r   r   r   rA   rA   rA   rB   r      s   
 r   c                   @   s   e Zd ZU ee ed< dS )r   stacksN)r   r   r   r   r   r   rA   rA   rA   rB   r      s   
 r   c                   @   s   e Zd ZdgZdS )PathHighlighterz(?P<dim>.*/)(?P<bold>.+)N)r   r   r   
highlightsrA   rA   rA   rB   r     s    
r   c                (   @   s  e Zd ZdZddddddZ	d;dd	d
dddeedddddddddee dee	 dee	 de	dee
 dedede	de	dee	 dededee dedeee
ef  de	f dd Zedd	d
dddeedddddddd!d"ee d#ed$ee dee	 dee	 de	dee
 dedede	de	dee	 deded%ee dedeee
ef  de	d&d f&d'd(Zedeeddddd)d"ee d#ed$ee dede	de	dee	 deded*eee  d&efd+d,Zd-ed.ed&efd/d0Ze d1ed&efd2d3Zed4e
d5e
d&e
fd6d7Z e d8e!d&efd9d:Z"dS )<r\   a  A Console renderable that renders a traceback.

    Args:
        trace (Trace, optional): A `Trace` object produced from `extract`. Defaults to None, which uses
            the last exception.
        width (Optional[int], optional): Number of characters used to traceback. Defaults to 100.
        code_width (Optional[int], optional): Number of code characters used to traceback. Defaults to 88.
        extra_lines (int, optional): Additional lines of code to render. Defaults to 3.
        theme (str, optional): Override pygments theme used in traceback.
        word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False.
        show_locals (bool, optional): Enable display of local variables. Defaults to False.
        indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True.
        locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
            Defaults to 10.
        locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80.
        locals_max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None.
        locals_hide_dunder (bool, optional): Hide locals prefixed with double underscore. Defaults to True.
        locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False.
        locals_overflow (OverflowMethod, optional): How to handle overflowing locals, or None to disable. Defaults to None.
        suppress (Sequence[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback.
        max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100.

    textpythoncythonpyrex)r   z.pyz.pxdz.pyxz.pxiNrD   rE   rF   FTrA   )rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   locals_overlowrT   rU   rV   tracerH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   r   rT   rU   rV   c                C   s.  |d u r$t  \}}}|d u s|d u s|d u rtd| j||||d}|| _|| _|| _|| _t	|p5d| _
|| _|| _|| _|| _|	| _|
| _|| _|| _|| _g | _|D ]-}t|tss|jd uskJ |dtj|j}n|}tjtj|}| j| qX|dkrtd|| _d S d| _d S )Nz9Value for 'trace' required if not called in except: blockrM   	ansi_darkz+ must be a module with '__file__' attributer      ) r~   exc_info
ValueErrorextractr   rH   rI   rJ   r/   	get_themerK   rL   rM   rT   rN   rO   rP   rQ   rR   rS   rU   
isinstancer   __file__ospathdirnamenormpathabspathappendmaxrV   )selfr   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   r   rT   rU   rV   r   r   rZ   suppress_entityr   rA   rA   rB   __init__'  sD   
"zTraceback.__init__r[   r   r   rZ   rS   r7   c                C   sH   | j ||||	|
||||d	}| |f||||||	||
|||||||dS )a  Create a traceback from exception info

        Args:
            exc_type (Type[BaseException]): Exception type.
            exc_value (BaseException): Exception value.
            traceback (TracebackType): Python Traceback object.
            width (Optional[int], optional): Number of characters used to traceback. Defaults to 100.
            code_width (Optional[int], optional): Number of code characters used to traceback. Defaults to 88.
            extra_lines (int, optional): Additional lines of code to render. Defaults to 3.
            theme (str, optional): Override pygments theme used in traceback.
            word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False.
            show_locals (bool, optional): Enable display of local variables. Defaults to False.
            indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True.
            locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
                Defaults to 10.
            locals_max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None.
            locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80.
            locals_hide_dunder (bool, optional): Hide locals prefixed with double underscore. Defaults to True.
            locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False.
            locals_overflow (OverflowMethod, optional): How to handle overflowing locals, or None to disable. Defaults to None.
            suppress (Iterable[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback.
            max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100.

        Returns:
            Traceback: A Traceback instance that may be printed.
        )rM   rN   rO   rP   rQ   rR   )rH   rI   rJ   rK   rL   rM   rT   rN   rO   rP   rQ   rR   r   rU   rV   )r   )clsr   r   rZ   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rich_tracebackrA   rA   rB   r]   `  s<   1zTraceback.from_exception)rM   rN   rO   rP   rQ   rR   _visited_exceptionsr   c          "         s  g }d}ddl m} t|ddpg }|
du rt n|
}dtdtfdd	}	 t||j||||d}tj	dkret
|ttfred
|_|jD ] }||v rKqD|| |jtjt|||j| |d qDt
|trt|jpod|jpsd|jpwd|jp{d|j|d|_|| |jj}dtttt f  dtttt f  f fdd}t!|D ]\}}|j"j#}d}tj	dkr|j$d }t%t&|j"' ||d }|\}}}}|dur|dur|dur|dur||f||ff}|r|(dst)j*+|st)j*,||}|j-.ddrqt/|pd||j"j0|rfdd||j-1 D nd|d}|| |j-.ddr6|jdd= q|sot|dd} | durU| |urU| j2}| }| j}d
}q$|j3} | durot|ddso| j2}| }| j}d}q$	 t4|d}!|!S )a,  Extract traceback information.

        Args:
            exc_type (Type[BaseException]): Exception type.
            exc_value (BaseException): Exception value.
            traceback (TracebackType): Python Traceback object.
            show_locals (bool, optional): Enable display of local variables. Defaults to False.
            locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
                Defaults to 10.
            locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80.
            locals_max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None.
            locals_hide_dunder (bool, optional): Hide locals prefixed with double underscore. Defaults to True.
            locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False.

        Returns:
            Trace: A Trace instance which you can use to construct a `Traceback`.
        Fr   )_IMPORT_CWD	__notes__N_objectr7   c                 S   s    zt | W S  ty   Y dS w )z1Don't allow exceptions from __str__ to propagate.z<exception str() failed>)r   r   )r   rA   rA   rB   safe_str  s
   
z#Traceback.extract.<locals>.safe_strT)r   r   r   r   )rF      )rM   rN   rQ   rR   r   ?r   )r   r   r   r   r   r   iter_localsc                 3   sT     ss| E dH  dS | D ]\}} r| drqr"| dr"q||fV  qdS )z-Extract locals from an iterator of key pairs.N__rt   
startswith)r   keyrY   )rQ   rR   rA   rB   
get_locals  s   
z%Traceback.extract.<locals>.get_localsrk   r   <_rich_traceback_omitc              	      s8   i | ]\}}t |st |s|tj| d qS ))
max_length
max_string	max_depth)inspect
isfunctionisclassr   traverse).0r   rY   )rP   rN   rO   rA   rB   
<dictcomp>D  s    z%Traceback.extract.<locals>.<dictcomp>)r   r   r   r   r   _rich_traceback_guard	__cause____suppress_context__)r   )5richr   getattrsetr   r   r   r   r~   version_infor   BaseExceptionGroupExceptionGroupr   r   addr   r\   r   type__traceback__SyntaxErrorr   r   r   r   r   r   r   r   r   r   objectr   f_codeco_filenamef_lastinextr   co_positionsr   r   r   isabsjoinf_localsro   r   co_nameitems	__class____context__r   )"r   r   r   rZ   rM   rN   rO   rP   rQ   rR   r   r   r   r   r   grouped_exceptionsr   stack	exceptionr   r   frame_summaryr@   r   r   instruction_indexinstruction_position
start_lineend_linestart_column
end_columnframecauser   rA   )rQ   rR   rP   rN   rO   rB   r     s   !




	


zTraceback.extractrG   optionsc                 #   sF   j }|  |j}ti d|td|td|td|tjd|t	d|t
tdd d	|td
|ttdd d|t	d|tjd|tjd|tjd|tjd|td|td|tjtdd ddt t dtdtdtf fddttjjD ]
\}}||V  qd S )Nr   pygments.textpygments.stringpygments.functionpygments.numberzrepr.indentT)dimzrepr.strz
repr.brace)boldzrepr.numberzrepr.bool_truezrepr.bool_falsez	repr.nonezscope.borderzscope.equalsz	scope.keyzscope.key.specialF)inheritr   r?   r7   c           	   
   3   s   | j r/t| d dddd}t|j} |V  W d    n1 s*w   Y  | jd uro tt| j dddjdjV  W d    n1 sXw   Y  t	| j
 dd	f| jjV  n| jrt	| j
 dd	f| jV  n
t	| j
 d	fV  | jD ]}t	d
|V  q| jrt| jdD ],\}}g }t|jD ]\}}||| qdV  ttt| d| ddjV  q|s| jrtdV  d S tdV  d S d S )Nz7[traceback.title]Traceback [dim](most recent call last)ztraceback.borderT)r   r   )titlestyleborder_styleexpandpaddingztraceback.border.syntax_error)r  r  r  r	  rH   z: ztraceback.exc_type)z[NOTE] ztraceback.noter   r   zSub-exception #ztraceback.group.border)r  r  zI
[i]The above exception was the direct cause of the following exception:
zH
[i]During handling of the above exception, another exception occurred:
)r   r,   _render_stackr)   rH   	use_themer   _render_syntax_errorr   assembler   r   r   r   r   	enumerater   r    r   r   r&   r   from_markup)	r   r?   stack_renderablenotegroup_nogroup_exceptionr   
group_lastgroup_stackbackground_stylerG   highlighterrender_stackr   traceback_themerA   rB   r    s~   



	

z0Traceback.__rich_console__.<locals>.render_stack)rK   get_background_styleget_style_for_tokenr1   	TextTokenr   r   r   Functionr   r   r.   r   Constant	Delimiterr   r+   r(   r   r^   r'   r    reversedr   r   )r   rG   r   rK   token_styler?   r   rA   r  rB   __rich_console__r  s\   
	




&CzTraceback.__rich_console__r   c                 c   s    t  }t }|jdkr,tj|jr,tjd|j dfdt|j	dfdd}||V  ||j
 }d|_t|jd	 t|}|d
|| |tjdd|  d dd7 }|V  d S )Nz<stdin> r   :r   r  r   r  Tr   zbold underline
u   [traceback.offset]▲[/])r+   r   r   r   r   existsr   r  r   r   r   rstripno_wrapminr   lenstylizer  )r   r   r  path_highlighterr   syntax_error_textr   rA   rA   rB   r    s*   


zTraceback._render_syntax_errorr   codec                 C   s   t j|d }|s(|d}|dkr|d | n|}|dr(d| v r(dS z| j|p4t||j	W S  t
y?   Y dS w )Nr8   r(  z#!r   r   )r   r   splitextindexr   lowerLEXERSro   r   r   r   )r   r   r1  extnew_line_index
first_linerA   rA   rB   _guess_lexer  s   
zTraceback._guess_lexerr   c                 #   s   t  }j}dtdtt ffdd}d }jdkr+tjd t|jjd  }d}t	|jD ]G\}}|r@||v r@d}q2|rX|d usHJ t
d	t| d
dddV  d}|dk}	|j t fddjD }
tj|jrt
j|t
|jdddt|jdfd|jdfdd}nt
jd|jdfdt|jdfdd}|jds|	sdV  |V  |jdr||E d H  q2|
szz8t|j}d|}|sW q2|j|}t|||d|jj |jj f|jhjjjdd
}dV  W n ty } zt
d| dfV  W Y d }~q2d }~ww |j d urh|j \}}t!||D ]B\}}}z$|dkr@||d  }t|t|"  }|dkrMt||d  }W n t#yY   Y q%w |j$d||f||fd q%|j%rwt&|g||dd n|V  q2d S )!Nr   r7   c              	   3   s4    | j rt| j d j j j j jdV  d S d S )Nr   )r  rT   r   r   r   overflow)r   r-   rT   rN   rO   rP   rS   )r   )r   rA   rB   render_locals  s   z.Traceback._render_stack.<locals>.render_localsr   rk   FTz
... z frames hidden ...centerztraceback.error)justifyr  c                 3   s    | ]}  |V  qd S Nr   )r   r   )frame_filenamerA   rB   	<genexpr>(  s    z*Traceback._render_stack.<locals>.<genexpr>r   r'  r%  r  z in r   r   zin r   r   )rK   line_numbers
line_rangehighlight_linesrL   rI   rT   dedentr(  r   r8   ztraceback.error_range)r  r5   r6   )r	  )'r   rK   r   r   r$   rV   r9   r-  r   r  r   r   anyrU   r   r   r)  r  r   r   r   r   	linecachegetlinesr   r9  r/   rJ   rL   rI   rT   r   r   rC   lstrip
IndexErrorstylize_ranger   r!   )r   r   r/  rK   r;  exclude_framesexcludedframe_indexr   r>   
suppressedr   
code_linesr1  
lexer_namesyntaxerrorr5   r6   r:   r;   r=   r   rA   )r?  r   rB   r
    s   
	







zTraceback._render_stackr>  )#r   r   r   __doc__r5  LOCALS_MAX_LENGTHLOCALS_MAX_STRINGr   r   r   r   r^   r%   r   r   r   r   classmethodr   r   r|   r   r]   r   r   r"   r#   r'   r#  r(   r   r  r9  r   r
  rA   rA   rA   rB   r\     s<   
	

9	
O	

 B
fr\   __main__r   ac                 C   s   d}t ||   d S )Nr   )r_   )rX  onerA   rA   rB   bar  s   rZ  c                 C   s   d}h ddd}t |  d S )NT>   Duncan IdahoThufir HawatPaul AtreidesVladimir Harkonnen)NFT)
charactersatomic_types)rZ  )rX  r   zedrA   rA   rB   foo  s
   	rb  c                   C   s   t d d S )Nr   )rb  rA   rA   rA   rB   rR    s   rR  )r7   N)\r   rF  r   r~   dataclassesr   r   	itertoolsr   rZ   r   typesr   r   typingr   r	   r
   r   r   r   r   r   r   r   r   pygments.lexersr   pygments.tokenr   r   r   r   r   r   r   r  r   pygments.utilr   r   r   _loopr   r    columnsr!   rG   r"   r#   r$   r%   r&   r'   r(   	constrainr)   r  r*   r+   panelr,   scoper-   r  r.   rQ  r/   r0   r   rK   r1   platformWINDOWSrT  rU  r   rC   r   r^   r|   r   r   r   r   r   r   r\   r   rZ  rb  rR  rA   rA   rA   rB   <module>   s    4 $	

	

 
		     




