
    MhA                        d dl Z d dl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m	Z	m
Z
m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mZmZmZ  e            Zd
Ze j        dede fd            Z!dej"        de fdZ#efdedededefdZ$de%ej&                 dede'de%e         def
dZ(dedefdZ)e)fdede	egef         defdZ*e)fdede	egef         defdZ+ddde'dedz  d e dz  defd!Z,d"e d#e de%e-ee-ee'f         f                  de%e         dedefd$Z. G d% d&          Z/ G d' d(          Z0dS ))    N)TracebackType)AnyCallableOptionalTuple)Token)get_ipython)debugger)path)	py3compat)ThemeTokenStreamtheme_table   filenamereturnc                     |                      d          sdS 	 t          | d          5 }t          d |D                       }ddd           n# 1 swxY w Y   n# t          $ r Y dS w xY w|S )z
    Given a filename, returns the number of lines in the file
    if it ends with the extension ".py". Otherwise, returns 0.
    z.pyr   rc              3      K   | ]}d V  dS )   N ).0lines     T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/IPython/core/tbtools.py	<genexpr>z)count_lines_in_py_file.<locals>.<genexpr>"   s"      ++d++++++    N)endswithopensumUnicodeError)r   filess      r   count_lines_in_py_filer#      s     U## q	h$$ ,++d+++++, , , , , , , , , , , , , , , 	 	 	11	Hs4   A AA AA AA 
A)(A)framec                     | j         j        }|8t          d           t          j        |           \  }}|t          |          z   S t          |          S )a  
    Given a frame object, returns the total number of lines in the file
    containing the frame's code object, or the number of lines in the
    frame's source code if the file is not available.

    Parameters
    ----------
    frame : FrameType
        The frame object whose line number is to be determined.

    Returns
    -------
    int
        The total number of lines in the file containing the frame's
        code object, or the number of lines in the frame's source code
        if the file is not available.
    NzNo file....)f_codeco_filenameprintinspectgetsourcelineslenr#   )r$   r   linesfirsts       r   get_line_number_of_framer.   (   sU    $ |'Hm-e44us5zz!!!(+++r   valuewhatfuncc                 D    	  ||           S #  d| d|j          dcY S xY w)N< z
() failed>)__name__)r/   r0   r1   s      r   _safe_stringr6   B   s?    4tE{{43433$-333333s   
 r,   theme
has_colors
lvals_toksc                 H   t           dz
  }g }| D ]}|t          j        u r%t          j        dfg}|                    |           6|j        }|                    |                              d          dz   }	|j	        rl|t          t          |                    z
  }
t          j        |                    |
          ft          j        t          |          ft          dft          |	fg}n3d||fz  }t          j        t          |          ft          dft          |	fg}|                    |           |rk|j	        rd|D ]W}|                    t          dt           z  f           |                    |           |                    t          df           X|dd         }|S )	z
    Format tracebacks lines with pointing arrow, leading numbers,
    this assumes the stack have been extracted using stackdata.


    Parameters
    ----------
    lines : list[Line]
    r   z   (...))	pygmented
r4   %*sN)INDENT_SIZE
stack_dataLINE_GAPr   LinenoEmextendlinenorenderrstrip
is_currentr+   str
make_arrowappend)r,   r7   r8   r9   numbers_widthtokens
stack_linetoksrD   r   padnumlvs                r   _format_traceback_linesrR   J   s     !OMF  !  !
,,,^Z01DMM$"  : 66==dCCdJ  	#c&kk"2"22C!1!1#!6!67V-	DD =&11CS*D 	d 	!*/ 	!  - -ucK&78999b!!!udm,,,,CRC[FMr   c                 r   	 t           j                            |           S # t          $ r   	 t          |           cY S # t          $ r   	 t	          | dd          }|rt          |          cY cY S t	          | dd          }|rdt          |          z  cY cY S Y Y dS # t          $ r   Y Y Y dS xY wxY wxY w)z(Hopefully pretty robust repr equivalent.r5   N	__class__z%s instancezUNRECOVERABLE REPR FAILURE)pydoctextreprKeyboardInterruptgetattr	text_repr)r/   nameklasss      r   rZ   rZ      s   4zu%%%   4	4;;  	 	 		44 uj$77 +$T??*****{D99 <(9U+;+;;;;;;;3333$   433333s_   ! B6?B6B3!B-B3.B62$BB3B6B6B0+B3,B60B33B6rW   c                     d ||           z  S )Nz=%sr   r/   rW   s     r   eqreprr_      s    44;;r   c                     dS )N r   r^   s     r   nullreprrb      s    2r   )rD   emr!   rD   c                   | rt           j        nt           j        }| rt           j        nt           j        }t                      }|?|j                            |          x}#|\  }}|||f|df||fgS ||f|df||f|d| fgS t          j	        t          j        |t          j                            }||df||fgS |df|| d| fgS )z
    Format filename lines with custom formatting from caching compiler or `File *.py` by default

    Parameters
    ----------
    em: wether bold or not
    file : str
    Nr4   z, line zFile :)r   NormalEmNormal
FilenameEmFilenamer	   compileformat_code_name	util_pathcompress_userr   cast_unicodefs_encoding)	rc   r!   rD   rg   ri   ipinstdatalabelr[   s	            r   _tokens_filenamers      s'     "3U^^u|F#%9u5>H]]F^44T:::TGt>4   4 -V--.	  &"4)>??
 
 >!4   !d--V--. r   lnumindexc                    |D ];}t          |t                    sJ |D ]}t          |d         t                    sJ  <t          dz
  }g }t	          || |z
            D ]\  }	\  }
\  }}|s|}
|
}|	| k    rl|t          t          |	                    z
  }t          j        |                    |          ft          j        t          |           ft          dft          |fg}n&d||	fz  }t          j        |ft          dft          |fg}|	                    |           |r |	| k    r|D ]}|	                    |           |S )aL  
    Format tracebacks lines with pointing arrow, leading numbers

    This should be equivalent to _format_traceback_lines, but does not rely on stackdata
    to format the lines

    This is due to the fact that stackdata may be slow on super long and complex files.

    Parameters
    ==========

    lnum: int
        number of the target line of code.
    index: int
        which line in the list should be highlighted.
    lines: list[string]
    lvals_toks: pairs of token type and str
        Values of local variables, already colored, to inject just after the error line.
    r   r4   r=   )

isinstancelistrH   r?   	enumerater+   r   rB   rI   rC   )rt   ru   r,   r9   r7   itemsubitrK   res_toksir   new_lineerrcolored_linerO   	line_tokspadding_numrQ   s                     r   _simple_format_traceback_linesr      s   4  - -$%%%%% 	- 	-EeAh,,,,,,	-  !OMH&/te|&D&D $ $""D/8S 	D99#c!ff++-C!1!1#!6!67T+%	II  =!"44K -%I
 		""" 	$!t))  $ $####Or   c                      e Zd ZU dZee         ed<   ee         ed<   eed<   ee         ed<   ee         ed<   e	j
        j        ed<   eed<   ed	e	j
        j        e	j
        j        z  d
d fd            Zddddee         dedededeej                 dededz  fdZed
ee         fd            Zed
ee         fd            Zed
efd            ZdS )	FrameInfozm
    Mirror of stack data's FrameInfo, but so that we can bypass highlighting on
    really long frames.
    descriptionr   rD   context	raw_lines_sdr$   
frame_infor   c                      | t          |dd           t          |dd           t          |dd           t          |dd           t          |dd           |d           S )Nr   r   rD   r$   codesdr   rY   )clsr   s     r   _from_stack_data_FrameInfoz$FrameInfo._from_stack_data_FrameInfo*  sn     sJt44J
D11J$//J..J--
 
 
 	
r   Nr   r   r   c                >   t          |t          t          d           f          s
J |            || _        || _        || _        || _        || _        || _        || _	        |;	 t          j        |          d         | _        d S # t          $ r dg| _        Y d S w xY wd S )Nr   zG'Could not get source, probably due dynamically evaluated source code.')rw   inttyper   r   rD   r$   r   r   r   r)   r*   r   OSError)selfr   r   rD   r$   r   r   r   s           r   __init__zFrameInfo.__init__8  s     &3T

"344<<f<<<& 
	 :!(!7!>!>q!A   ]"	 :s   #B BBc                 ,    | j         | j         j        S g S N)r   variables_in_executing_piecer   s    r   r   z&FrameInfo.variables_in_executing_pieceV  s    8888Ir   c                     ddl m} | j        J 	 | j        j        S # |$ r  G d d          } |            gcY S w xY w)Nr   )NotOneValueFoundc                   &    e Zd ZdZdZdedefdZdS )FrameInfo.lines.<locals>.Dummyr   Fr;   r   c                    dS )NzH<Error retrieving source code with stack_data see ipython/ipython#13598>r   )r   r;   s     r   rE   z%FrameInfo.lines.<locals>.Dummy.renderj  s    eer   N)r5   
__module____qualname__rD   rG   boolrH   rE   r   r   r   Dummyr   f  sH        "
ft f f f f f f fr   r   )executing.executingr   r   r,   )r   r   r   s      r   r,   zFrameInfo.lines]  s    888888x###	8>! 		 		 		f f f f f f f f EGG9		s    ==c                 ,    | j         | j         j        S d S r   )r   	executingr   s    r   r   zFrameInfo.executingo  s    88%%4r   )r5   r   r   __doc__r   rH   __annotations__r   rx   r@   corer   r   classmethodRepeatedFramesr   typesCodeTyper   propertyr   r,   r   r   r   r   r   r     s         
 #smKKKc]Cy		""""JJJ
#2Z_5SS
	
 
 
 [
* "  c]  	
  u~&  t   < d3i    X tCy    X" 3    X  r   r   c                   .    e Zd ZU dZdZeed<   eed<   eed<   eed<   eed<   eed<   e	d	d
fd
dddededede
d
z  def
 fdZdefdZd)dZ eee          Zededefd            Zded
z  deee
eef                  fdZded
z  deee                  fdZedefd            Zdedd
fdZdedd
fdZd)dZdee         defdZ	 	 d*d!e
d"ed
z  d#ed
z  d$ee         d%edefd&Z	 	 	 d+d!e
ded
z  d'ee         d$ee         d%edee         fd(Z  xZ!S ),TBToolsz2Basic tools used by all traceback printer classes.r   _theme_name_old_theme_namecall_pdbostreamdebugger_clspdbFNnocolor)r   
theme_namecolor_schemer   c                   |t           ur=t          |t                    s
J |            t          j        dt
          d           |}|dv rFt          j        d|                                 dt
          d           |                                }t                                                       || _	        || _
        |                     |           |pt          j        | _        |r|                                 | _        d S d | _        d S )NzScolor_scheme is deprecated since IPython 9.0, use theme_name instead, all lowercase   
stacklevel)LinuxLightBGNeutralNoColorz?Theme names and color schemes are lowercase in IPython 9.0 use z instead)	_sentinelrw   rH   warningswarnDeprecationWarninglowersuperr   r   _ostreamset_theme_namer
   Pdbr   r   )r   r   r   r   r   r   rT   s         r   r   zTBTools.__init__  s'    y((lC00>>,>>>Me"   
 &JCCCMnR\RbRbRdRdnnn"   
 $))++J 	    	J'''(8HL 	((**DHHHDHHHr   r   c                 6    | j         t          j        n| j         S )as  Output stream that exceptions are written to.

        Valid values are:

        - None: the default, which means that IPython will dynamically resolve
          to sys.stdout.  This ensures compatibility with most tools, including
          Windows (where plain stdout doesn't recognize ANSI escapes).

        - Any object with 'write' and 'flush' attributes.
        )r   sysstdoutr   s    r   _get_ostreamzTBTools._get_ostream  s     "]2szzEr   c                 \    |"t          |d          rt          |d          sJ || _        d S )Nwriteflush)hasattrr   )r   vals     r   _set_ostreamzTBTools._set_ostream  s2    {wsG44{g9N9N{{{r   exception_valuec                 t    t          | dd           }|r|S t          | dd          rd S t          | dd           S )N	__cause____suppress_context__F__context__r   )r   causes     r   _get_chained_exceptionzTBTools._get_chained_exception  sL    d;; 	L?$:EBB 	4t<<<r   evaluec                 R    |                      |          }|r|j        ||j        fS d S r   )r   rT   __traceback__)r   r   chained_evalues      r   get_parts_of_chained_exceptionz&TBTools.get_parts_of_chained_exception  s?     44V<< 	(, 
 tr   r   c                 $    d}d}|r|gg}n|gg}|S )NzF
The above exception was the direct cause of the following exception:
zE
During handling of the above exception, another exception occurred:
r   )r   r   direct_causeexception_during_handlingmessages        r   !prepare_chained_exception_messagez)TBTools.prepare_chained_exception_message  s>     W 	 V 	"  	4$~&GG123Gr   c                 `    | j         | j                                         k    sJ | j         dk    S )Nr   )r   r   r   s    r   r8   zTBTools.has_colors  s3    4#3#9#9#;#;;;;;9,,r   r[   c                     |t           v sJ |                                |k    sJ || _        t          | d          r#| j        | j                            |           d S d S d S )Nr   )r   r   r   r   r   r   r   r[   s     r   r   zTBTools.set_theme_name  sy    {""""zz||t####4 	*DH$8H##D)))))	* 	*$8$8r   c                 h    t          j        dt          d           |                     |           dS )z;Shorthand access to the color table scheme selector method.zFset_colors is deprecated since IPython 9.0, use set_theme_name insteadr   r   N)r   r   r   r   r   s     r   
set_colorszTBTools.set_colors  sB     	T	
 	
 	
 	

 	D!!!!!r   c                 ^    | j         dk    r| j        | _         dS | j         | _        d| _         dS )z=Toggle between the currently active color scheme and nocolor.r   N)r   r   r   s    r   color_togglezTBTools.color_toggle  s:    y((#3D#'#3D (Dr   stbc                 ,    d                     |          S )z4Convert a structured traceback (a list) to a string.r<   )join)r   r   s     r   stb2textzTBTools.stb2text  s    yy~~r      etyper/   tb	tb_offsetr   c                 ^    |                      |||||          }|                     |          S )zgReturn formatted traceback.

        Subclasses may override this if they add extra arguments.
        )structured_tracebackr   )r   r   r/   r   r   r   tb_lists          r   rV   zTBTools.text  s1     ++E5"iQQ}}W%%%r   etbc                     t                      )zWReturn a list of traceback frames.

        Must be implemented by each class.
        )NotImplementedError)r   r   r   r   r   r   s         r   r   zTBTools.structured_traceback  s     "###r   )r   N)Nr   )NNr   )"r5   r   r   r   r   rH   r   r   r   r   r   r   r   r   r   r   staticmethodr   BaseExceptionr   r   r   r   rx   r   r8   r   r   r   r   r   rV   r   __classcell__)rT   s   @r   r   r   w  s>        << INNNLLL	HHH &	- %)#- - -- - 	- Tk- - - - - - -^Fc F F F F    h|\22G= = = = = \=#d*	%m]:;	<   "T)	d3i     -D - - - X-*3 *4 * * * *	"s 	"t 	" 	" 	" 	") ) ) )DI #     $(& && t#& D 	&
 C=& & 
& & & && (,#'$ $$ $$ m$	$
 C=$ $ 
c$ $ $ $ $ $ $ $r   r   )1	functoolsr)   rU   r   r   r   r   typingr   r   r   r   r@   pygments.tokenr   IPythonr	   IPython.corer
   IPython.utilsr   rl   r   IPython.utils.PyColorizer   r   r   objectr   r?   	lru_cacherH   r   r#   	FrameTyper.   r6   rx   Liner   rR   rZ   r_   rb   rs   tupler   r   r   r   r   r   <module>r     s         



         1 1 1 1 1 1 1 1 1 1 1 1                       ! ! ! ! ! ! + + + + + + # # # # # # D D D D D D D D D DFHH	 S S     ,EO , , , , ,4 58 4 4 43 4c 4C 4 4 4 44
 44 4 [!	4
 4 4 4 4p4S 4S 4 4 4 4< 5>  # XseSj1 #     7@  C xs
3 C     	0 0 00
*0 $J	0
 0 0 0 0f=
== c5d++,-= [!	=
 = = = = =@Y Y Y Y Y Y Y Yxs$ s$ s$ s$ s$ s$ s$ s$ s$ s$r   