
    f                     b   d Z 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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c 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  ddlm!Z" ddlm#Z# ddl$m%Z% dZ&dZ'dZ( ejR                         de*de+fd       Z,dejZ                  de+fdZ.de/fdZ0d Z1dddZ2 G d dejf                        Z4 G d de4      Z5 G d  d!      Z6 G d" d#e4      Z7 G d$ d%e7e5      Z8 G d& d'e8      Z9 G d( d)e8      Z: G d* d+e5      Z;d, Z<e<fd-Z=e<fd.Z>y)/a]  
Verbose and colourful traceback formatting.

**ColorTB**

I've always found it a bit hard to visually parse tracebacks in Python.  The
ColorTB class is a solution to that problem.  It colors the different parts of a
traceback in a manner similar to what you would expect from a syntax-highlighting
text editor.

Installation instructions for ColorTB::

    import sys,ultratb
    sys.excepthook = ultratb.ColorTB()

**VerboseTB**

I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
of useful info when a traceback occurs.  Ping originally had it spit out HTML
and intended it for CGI programmers, but why should they have all the fun?  I
altered it to spit out colored text to the terminal.  It's a bit overwhelming,
but kind of neat, and maybe useful for long-running programs that you believe
are bug-free.  If a crash *does* occur in that type of program you want details.
Give it a shot--you'll love it or you'll hate it.

.. note::

  The Verbose mode prints the variables currently visible where the exception
  happened (shortening their strings if too long). This can potentially be
  very slow, if you happen to have a huge data structure whose string
  representation is complex to compute. Your computer may appear to freeze for
  a while with cpu usage at 100%. If this occurs, you can cancel the traceback
  with Ctrl-C (maybe hitting it more than once).

  If you encounter this kind of situation often, you may want to use the
  Verbose_novars mode instead of the regular Verbose, which avoids formatting
  variables (but otherwise includes the information and context given by
  Verbose).

.. note::

  The verbose mode print all variables in the stack, which means it can
  potentially leak sensitive information like access keys, or unencrypted
  password.

Installation instructions for VerboseTB::

    import sys,ultratb
    sys.excepthook = ultratb.VerboseTB()

Note:  Much of the code in this module was lifted verbatim from the standard
library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.

Color schemes
-------------

The colors are defined in the class TBTools through the use of the
ColorSchemeTable class. Currently the following exist:

  - NoColor: allows all of this module to be used in any terminal (the color
    escapes are just dummy blank strings).

  - Linux: is meant to look good in a terminal like the Linux console (black
    or very dark background).

  - LightBG: similar to Linux but swaps dark/light colors to be more readable
    in light background terminals.

  - Neutral: a neutral color scheme that should be readable on both light and
    dark background

You can implement other color schemes easily, the syntax is fairly
self-explanatory. Please send back new schemes you develop to the author for
possible inclusion in future releases.

Inheritance diagram:

.. inheritance-diagram:: IPython.core.ultratb
   :parts: 3
    N)TracebackType)AnyListOptionalTuple)Terminal256Formatter)get_style_by_name)get_ipython)debugger)DisplayTrap)exception_colors)
PyColorize)path)	py3compat)get_terminal_size   NoColori'  filenamereturnc                     | j                  d      sy	 t        | d      5 }t        d |D              }ddd       |S # 1 sw Y   S xY w# t        $ r Y yw xY w)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   yw)   N ).0lines     O/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/core/ultratb.py	<genexpr>z)count_lines_in_py_file.<locals>.<genexpr>   s     +d+s   N)endswithopensumUnicodeError)r   filess      r   count_lines_in_py_filer%      sg     U#	h$ ,+d++, H	, H  		s+   A
 =A
 AA
 A
 
	AAframec                     | j                   j                  }|1t        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.
    zNo file....)f_codeco_filenameprintinspectgetsourcelineslenr%   )r&   r   linesfirsts       r   get_line_number_of_framer0      sO    $ ||''Hm--e4us5z!!!(++    
has_colorsc                 l   t         dz
  }g }| D ]"  }|t        j                  u r,|j                  |j                  d|j
                  d       B|j                  |      j                  d      dz   }|j                  }|j                  rF|t        t        |            z
  }	t        j                  |	      t        |      }
|j                  }nd||fz  }
|j                  }||
|j
                  d|}|j                  |       |s|j                  s|j                  |dz          % |S )a  
    Format tracebacks lines with pointing arrow, leading numbers...

    Parameters
    ----------
    lines : list[Line]
    Colors
        ColorScheme used.
    lvals : str
        Values of local variables, already colored, to inject just after the error line.
    r   z   (...)
)	pygmented%*s )INDENT_SIZE
stack_dataLINE_GAPappendlinenoEmNormalrenderrstriplineno
is_currentr-   strr   
make_arrow)r.   Colorsr2   lvalsnumbers_widthres
stack_liner   r@   padnumstart_colors               r   _format_traceback_linesrL      s     !OM
C %
,,,JJ6??FMMJK  : 6==dCdJ""  #c&k"22C$//4c&kBC //K=&11C --K)3tD

4Z**JJut|$)%* Jr1   c                    t         dz
  }g }t        || |z
        D ]  \  }}	t        j                  |	      }	 ||	d      \  }
}|s|
}	|| k(  re|t	        t        |            z
  }t        j                  |      t        |       }|j                  ||j                  d|	|j                  }	n&d||fz  }|j                  ||j                  d|	}	|j                  |	       |s|| k(  s|j                  |dz           |S )a  
    Format tracebacks lines with pointing arrow, leading numbers...

    Parameters
    ==========

    lnum: int
        number of the target line of code.
    index: int
        which line in the list should be highlighted.
    lines: list[string]
    Colors:
        ColorScheme used.
    lvals: bytes
        Values of local variables, already colored, to inject just after the error line.
    _line_format: f (str) -> (str, bool)
        return (colorized version of str, failure to do so)
    r   rB   r7   r6   r4   )r8   	enumerater   cast_unicoder-   rB   r   rC   r<   r   r=   r@   r;   )lnumindexr.   rD   rE   _line_formatrF   rG   ir   new_lineerrrI   rJ   s                 r   _simple_format_traceback_linesrV      s    &  !OM
CUD5L1 %4%%d+$T51#D9#c!f+-C$//4c$i@CD =!,,C"(--fmmTJD

4Q$YJJut|$3%4 Jr1   r@   c                F   t               }|6|j                  j                  |       x}|\  }}|	d| d| }nUd| d| }nLd}t        j                  t        j                  | t        j                              }|	d| d| }nd| d| }|j                  |||      S )a  
    Format filename lines with custom formatting from caching compiler or `File *.py` by default

    Parameters
    ----------
    file : str
    ColorFilename
        ColorScheme's filename coloring to be used.
    ColorNormal
        ColorScheme's normal coloring to be used.
    z{label} z{name}z{name}, line {lineno}Filez{name}:{lineno})labelnamer@   )	r
   compileformat_code_name	util_pathcompress_userr   rO   fs_encodingformat)	r#   ColorFilenameColorNormalr@   ipinstdatarZ   r[   tpl_links	            r   _format_filenamerg     s     ]F^^44T::TGt>#M?(;-HH ]O+D[MR  &&""4)>)>?
 >#M?(;-HH $M?2Ek]SH??T&?AAr1   c                       e Zd ZdZdZ	 	 	 	 	 ddd fdZd Zd Z eee      Z	e
d        Zd	eeeeef      fd
Zd	ee   fdZed	efd       Zd Zd Zd Zddee   fdZ	 	 	 ddedee   dee   dee   def
dZ xZS )TBToolsz2Basic tools used by all traceback printer classes.r   N)debugger_clsc                
   t         t        |   ||       || _        || _        t               | _        | j                  |       || _        |xs t        j                  | _        |r| j                         | _        y d | _        y N)parentconfig)superri   __init__call_pdb_ostreamr   color_scheme_table
set_colors
old_schemer   Pdbrj   pdb)selfcolor_schemerq   ostreamrm   rn   rj   	__class__s          r   rp   zTBTools.__init__?  st     	gt%VF%C    #3"4%&(8HLL((*DHDHr1   c                 R    | j                   t        j                  S | 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.
        )rr   sysstdoutrx   s    r   _get_ostreamzTBTools._get_ostreamc  s      "]]2szzEEr1   c                 J    |t        |d      rt        |d      sJ || _        y )Nwriteflush)hasattrrr   )rx   vals     r   _set_ostreamzTBTools._set_ostreamp  s&    {wsG4g9NOOr1   c                 Z    t        | dd       }|r|S t        | dd      ry t        | dd       S )N	__cause____suppress_context__F__context__getattr)exception_valuecauses     r   _get_chained_exceptionzTBTools._get_chained_exceptionv  s7    d;L?$:EBt<<r1   r   c                 \    | j                  |      }|r|j                  ||j                  fS y N)r   r{   __traceback__)rx   evaluechained_evalues      r   get_parts_of_chained_exceptionz&TBTools.get_parts_of_chained_exception  s3     44V<!++^^=Y=YYYr1   c                 &    d}d}|r|gg}|S |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   )rx   r   direct_causeexception_during_handlingmessages        r   !prepare_chained_exception_messagez)TBTools.prepare_chained_exception_message  s3    a$m!$~&G  223Gr1   c                 P    | j                   j                  j                         dk7  S )Nnocolor)rs   active_scheme_namelowerr   s    r   r2   zTBTools.has_colors  s"    &&99??AYNNr1   c                      | j                   j                  |i | | j                   j                  | _        t	        | d      r*| j
                   | j
                  j                  |i | yyy)z;Shorthand access to the color table scheme selector method.rw   N)rs   set_active_schemeactive_colorsrD   r   rw   rt   )rx   argskws      r   rt   zTBTools.set_colors  sf     	2114>2>--;;4DHH$8DHH,, %9r1   c                 Z   | j                   j                  dk(  rA| j                   j                  | j                         | j                   j                  | _        y| j                   j                  | _        | j                   j                  d       | j                   j                  | _        y)z=Toggle between the currently active color scheme and NoColor.r   N)rs   r   r   ru   r   rD   r   s    r   color_togglezTBTools.color_toggle  s}     ""55B##55dooF11??DK"55HHDO##55i@11??DKr1   c                 $    dj                  |      S )4Convert a structured traceback (a list) to a string.r4   joinrx   stbs     r   stb2textzTBTools.stb2text  s    yy~r1   	tb_offsetc                 N    | j                  |||||      }| j                  |      S )zgReturn formatted traceback.

        Subclasses may override this if they add extra arguments.
        )structured_tracebackr   )rx   etypevaluetbr   contexttb_lists          r   textzTBTools.text  s/    
 ++E5",5w@}}W%%r1   r   r   etbnumber_of_lines_of_contextc                     t               )zWReturn a list of traceback frames.

        Must be implemented by each class.
        )NotImplementedErrorrx   r   r   r   r   r   s         r   r   zTBTools.structured_traceback  s     "##r1   )r   FNNNN   NNr   )__name__
__module____qualname____doc__r   rp   r   r   propertyrz   staticmethodr   r   r   typeBaseExceptionr   r   r   r   r   boolr2   rt   r   r   intr   r   __classcell__r{   s   @r   ri   ri   9  s   < I " "HF |\2G= =	%m]:;	<$s)  OD O O	-	@& & (,#'*+$$ '$ m$	$
 C=$ %($r1   ri   c            
       l    e Zd ZdZd Zd Z	 	 	 ddedee   dee	   dee
   fd	Zd
 Zd Zd Zd Zd Zy)ListTBaj  Print traceback information from a traceback list, with optional color.

    Calling requires 3 arguments: (etype, evalue, elist)
    as would be obtained by::

      etype, evalue, tb = sys.exc_info()
      if tb:
        elist = traceback.extract_tb(tb)
      else:
        elist = None

    It can thus be used by programs which need to process the traceback before
    printing (such as console replacements based on the code module from the
    standard library).

    Because they are meant to be called without a full traceback (only a
    list), instances of this class can't call the interactive pdb debugger.c                     | j                   j                          | j                   j                  | j                  |||             | j                   j                  d       y Nr4   )rz   r   r   r   rx   r   r   elists       r   __call__zListTB.__call__  sC    499UE59:4 r1   c                 2    |rt        j                  |      S y r   )	traceback
extract_tb)rx   r   s     r   _extract_tbzListTB._extract_tb  s    ''++r1   Nr   r   r   r   c                 6   t        |t              r|\  }}n
t               }t        |t              r|}n|| j	                  |      }ng }|| j
                  n|}t        |t              sJ | j                  }g }	|rd|rt        |      |kD  r||d }|	j                  d|j                  d|j                  ddz          |	j                  | j                  |             dj                  | j                  ||            }
|	j                  |
       | j!                  |      }|rtt#        |d         |vrd|| j%                  |j&                        d   nd}|\  }}}|j)                  t#        |d                d}| j+                  ||||f||      |z   |	z   }	|	S )	a  Return a color formatted string with the traceback info.

        Parameters
        ----------
        etype : exception type
            Type of the exception raised.
        evalue : object
            Data stored in the exception
        etb : list | TracebackType | None
            If list: List of frames, see class docstring for details.
            If Traceback: Traceback of the exception.
        tb_offset : int, optional
            Number of frames in the traceback to skip.  If not given, the
            instance evalue is used (set in constructor).
        context : int, optional
            Number of lines of context information to print.

        Returns
        -------
        String with formatted exception.
        Nz
Traceback z(most recent call last):r4    r   r   )
isinstancetuplesetlistr   r   r   rD   r-   r;   normalEmr=   extend_format_listr   _format_exception_onlyr   idr   r   addr   )rx   r   r   r   r   r   chained_exc_idsr   rD   out_listr.   	exceptionchained_exception_messagechained_exceptions_tb_offsets                 r   r   zListTB.structured_traceback  s   > c5!#& C!eOc4 E_$$S)EE&/&7DNNY	)S)))SZ)3ij)OO#__fmm=?CD EOOD--e4533E6BC77?	"Yq\*/A % 66v7G7GHK &
 "+E639Q< 01+,())/*0 ,,   r1   c                    | j                   }g }t        |      D ]  \  }\  }}}}|t        |      dz
  k(  r.|j                  |j                  |j
                  |j                  fn#|j                  |j                  |j                  df\  }	}
}}t        |||	|      }|	 d| }|dk7  r|d|
 | |	 dz  }n|dz  }|r|| d|j                          |	 dz  }|j                  |        |S )	a   Format a list of traceback entry tuples for printing.

        Given a list of tuples as returned by extract_tb() or
        extract_stack(), return a list of strings ready for printing.
        Each string in the resulting list corresponds to the item with the
        same index in the argument list.  Each string ends in a newline;
        the strings may contain internal newlines as well, for those items
        whose source text line is not None.

        Lifted almost verbatim from traceback.py
        r   r   rW     <module>z in r4       )rD   rN   r-   r   nameEm
filenameEmr   r=   r[   r   rg   stripr;   )rx   extracted_listrD   r   indr   r@   r[   r   	normalColnameColfileCollineColfnsitems                  r   r   zListTB._format_list;  s    3<^3L 	/C/(FD$ #n-11 &--1B1BFKKPmmV[[&//2F	 1Iw #8WiOC[3%(Dz!$wivi[;;7)4

~i[CCKK#	& r1   c                    d}| j                   }g }t        j                  |j                  |j                  z   |j
                  z         }||j                  |dz          n9t        |t              rd}|j                  sd|_	        |j                  r7|j                  }t        j                  |j                  |j                        }nd}d}|j                  |j                  dt        |j                  |j                  |j                  |dk(  rdn|	      |j
                  d       |dk(  r t        j                  |j                   d
      }|d}	|	t#        |      k  r:||	   j%                         r'|	dz  }	|	t#        |      k  r||	   j%                         r'|j                  |j&                  d|j)                         |j
                  d       |j*                  ad}
||	|j*                  dz
   D ]  }|j%                         r|
|z  }
|
dz  }
 |j                  |j,                  |
d|j
                  d       	 |j.                  }
|
r1|j                  ||j                  d|j
                  d|
d       n|j                  d|z         |r=t5               }|1|j6                  j9                  |j                  |j                  d       |S # t0        $ r | j3                  |      }
Y w xY w)aT  Format the exception part of a traceback.

        The arguments are the exception type and value such as given by
        sys.exc_info()[:2]. The return value is a list of strings, each ending
        in a newline.  Normally, the list contains a single string; however,
        for SyntaxError exceptions, it contains several lines that (when
        printed) display detailed information about where the syntax error
        occurred.  The message indicating which exception occurred is the
        always last string in the list.

        Also lifted nearly verbatim from traceback.py
        FNr4   Tz<string>unknownr   r   rW   zutf-8r   r   r   r7   ^r   z%s
)rD   r   rO   excNamer   r=   r;   
issubclassSyntaxErrorr   r@   	linecachegetliner   rg   r   r   r-   isspacer   r   offsetcaretmsg	Exception	_some_strr
   hookssynchronize_with_editor)rx   r   r   have_filedatarD   r   styper@   textlinerS   r$   crd   s                r   r   zListTB._format_exception_only_  s    &&v~~'F'VW=KK%%- $~~
u~<<"\\F(00NH&F!H (!NN"--"OO,2i,?DV	 
 r>(55ejj'JH'Ac(m+0C0C0EQ c(m+0C0C0EKK&++2:..2B28--!A B ||/"!)!ELL1,<!= )A yy{ !Q !S	)
 6<<39==%B C*II eV^^.4mmQ@ A FUN+  ]F!44U^^U\\STU  *NN5)*s   K K98K9c                 0    t         j                  | ||      S )zOnly print the exception type and message, without a traceback.

        Parameters
        ----------
        etype : exception type
        value : exception value
        )r   r   )rx   r   r   s      r   get_exception_onlyzListTB.get_exception_only  s     **4>>r1   c                     | j                   }|j                          |j                  dj                  | j	                  ||                   |j                          y)zOnly print the exception type and message, without a traceback.

        Parameters
        ----------
        etype : exception type
        evalue : exception value
        r4   N)rz   r   r   r   r  )rx   r   r   rz   s       r   show_exception_onlyzListTB.show_exception_only  sA     ,,dii 7 7v FGHr1   c                 ~    	 t        j                  t        |            S #  dt        |      j                  z  cY S xY w)Nz<unprintable %s object>)r   rO   rB   r   r   )rx   r   s     r   r  zListTB._some_str  s:    	E))#e*55	E-U0D0DDDs     <r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r1   r   r   r     sy    O&!
 (,#'QQ 'Q m$	Q
 C=Qf"HOb?Er1   r   c                       e Zd ZU dZee   ed<   ee   ed<   ee   ed<   ee   ed<   e	d        Z
ddddee   dedee   fd	Zed
        Zed        Zed        Zy)	FrameInfozm
    Mirror of stack data's FrameInfo, but so that we can bypass highlighting on
    really long frames.
    descriptionr   r@   r   c                      | t        |dd       t        |dd       t        |dd       t        |dd       t        |dd       |d       S )Nr  r   r@   r&   codesdr   r   )cls
frame_infos     r   _from_stack_data_FrameInfoz$FrameInfo._from_stack_data_FrameInfo  sQ    Jt4J
D1J$/J.J-
 	
r1   Nr  c                    || _         || _        || _        || _        || _        || _        || _        | t        j                  |      }|d   | _	        y y Nr   )
r  r   r@   r&   r  _sdr   r+   r,   	raw_lines)	rx   r  r   r@   r&   r  r  r   ixs	            r   rp   zFrameInfo.__init__  s]     ' 
	 :''.BUDN r1   c                 J    | j                   r| j                   j                  S g S r   )r  variables_in_executing_piecer   s    r   r  z&FrameInfo.variables_in_executing_piece  s    8888888Ir1   c                 .    | j                   j                  S r   )r  r.   r   s    r   r.   zFrameInfo.lines  s    xx~~r1   c                 H    | j                   r| j                   j                  S y r   )r  	executingr   s    r   r"  zFrameInfo.executing  s    8888%%%r1   )r   r   r   r   r   rB   __annotations__r   r   classmethodr  rp   r   r  r.   r"  r   r1   r   r  r    s    
 #sm#Jc]	
 	
& #c]# # c
	#0      r1   r  c                       e Zd ZdZdZ	 	 	 	 	 	 	 	 	 	 ddededededef
d	Zd
e	fdZ
ddedefdZd Zdedee   dee   dee   fdZdededefdZ	 	 	 ddedee   dee   dee   def
dZddefdZddZddZy)	VerboseTBa;  A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
    of HTML.  Requires inspect and pydoc.  Crazy, man.

    Modified version which optionally strips the topmost entries from the
    traceback, to be used with alternate interpreters (because their own code
    would appear in the traceback).zbg:ansiyellowNry   rq   r   long_headerinclude_varsc           	          t         j                  | ||||	|
|       || _        || _        || _        |t
        j                  }|| _        d| _        y)aW  Specify traceback offset, headers and color scheme.

        Define how many frames to drop from the tracebacks. Calling it with
        tb_offset=1 allows use of this handler in interpreters which will have
        their own code at the top of the traceback (VerboseTB will first
        remove that frame before printing the traceback info).)ry   rq   rz   rm   rn   rj   NT)	ri   rp   r   r'  r(  r   
checkcachecheck_cacheskip_hidden)rx   ry   rq   rz   r   r'  r(  r+  rj   rm   rn   s              r   rp   zVerboseTB.__init__  sh    & 	%% 	 	
 #&( #..K&r1   r  c                 v   t        |t              sJ | j                  }|j                  }t        |j                  t
        j                        r d|j                  d|j                  d|dS dt        z  }|j                  d||}d|j                   d|j                   d| }d|j                  d	|j                  d
|}d|j                  d|}t        |j                  |j                  ||j                        }	t!        j"                  |j$                        \  }
}}}|j&                  |j&                  j)                         }nd}|dk(  rd}nF| j*                  rt,        nt.        }	 t!        j0                  |
||||      }|j3                  ||      }d}g }| j*                  rH	 |j6                  }|D ]5  }|j9                  ||j:                  t=        |j>                        fz         7 	 |r||jC                  |      }|	 |rdnd | d}|j                  #|j                  d	|}|tE        jF                  |j                        z  }	|	d|d}tI        jJ                  | jL                  jN                  |       jP                  }|jR                  jT                  }|j                  d   }|jV                  }||z
  }||jX                  k\  r5t[        ||jX                  z
  d      }||jX                  z   }|jX                  }nd}||jX                  z   }||| }|djC                  t]        ||||||            S |djC                  t_        |j`                  || jb                  |            z  }|S # t4        $ r	 ||z  }Y w xY w# t@        $ r |j9                  d       Y w xY w)zFormat a single stack framer   z[... skipping similar frames: ]r4   r7   zin z{file}z{scope}z%sz"(***failed resolving arguments***)z%s z= %srW   ?r   r   )formatvalue)r#   scopez<Exception trying to inspect frame. No more locals available.z, )stylerm   r   )2r   r  rD   r=   r  r9   RepeatedFramesr   r  r8   valEmvNamerg   r   r   r@   r+   getargvaluesr&   r"  code_qualnamer(  eqreprnullreprformatargvaluesra   KeyErrorr  r;   r[   reprr   r  r   r^   r_   r   Parserrs   r   format2r  co_firstlinenor  r   maxrV   rL   r.   r2   ) rx   r  rD   ColorsNormalindent	em_normaltpl_calltpl_call_failtpl_name_vallinkr   varargsvarkwlocals_funccallvar_reprr1  rE   
lvals_listfibpvarresultrf   levelrR   
first_linecurrent_liner  rQ   startstops                                    r   format_recordzVerboseTB.format_recordG  s   *i000}}jnnj&?&?@
 6 6F F {""(,,E	hv||nIl^TLLLL

 +1,,E$$	
 )0(<(<Z=M=M(N%gug+''557DD:D "&!2!2vH,//'5'x  D>( 
	 ">> RC%%lchhSYY5P&PQR $innZ&@AE6$$B/vR8>>!$*$5$5|DHi55j6I6IJJD!%t,E%,,--@@g  $77J%,,Q/L",,I :-E
***EJ$6$66:z111"**z111!%-I 2$!$	  bgg'$$fdoou F
 ]  ," %t+#,8  !!Rs%   6-N 4AN NNN87N8r   long_versionc                 ,   | j                   }|j                  }|j                  ||}t        dt	               d         }|rdt
        j                  j                         d   z   dz   t
        j                  z   }t        j                  t        j                               }|j                  d|z  |d|d|t        |      z
  t        |      z
  z  |d|j                  |      	}	|	dz  }	|	S |d	j                  |t        |      z
        }	|	S )
NK   r   zPython : -r4   r7   z
A problem occurred executing Python code.  Here is the sequence of function
calls leading up to the error, with the most recent (innermost) call last.z!Traceback (most recent call last))rD   r=   r   minr   r}   versionsplit
executabletimectimetopliner-   rjust)
rx   r   rX  colorscolorsnormalexcwidthpyverdateheads
             r   prepare_headerzVerboseTB.prepare_header  s   }} ..%>B)+A./ 1 1 3A 66=NE::diik*D eus5z)CJ67

5!D _D 	 399%#e*:LMD
 r1   c                 "   | j                   }|j                  }	 t        t        ||f      \  }}|j                  ||dt        j                  |      gS #  t        t	        j
                         d d }}t        t        ||f      \  }}Y _xY w)N   r[  )	rD   r=   maprB   r}   exc_infor   r   rO   )rx   r   r   re  rf  	etype_str
evalue_strs          r   format_exceptionzVerboseTB.format_exception  s    }}	>$'eV_$=!Iz !'	 ,i.D.DZ.PR S 	S	>!36E$'eV_$=!Izs   A 4Br   r   c           
      .   |}	 |j                   }|| j                  n|}t        |t              sJ | j                  t        |      | j                        }|r| j                  |||      ng }g }	d}
t        |      dz
  }t        |      D ]  \  }}t        |j                  t        j                        s=| j                  r1|j                  j                   j#                  dd      r||k7  r|
dz  }
g|
r?| j$                  }|j&                  }|	j)                  d|j*                  d|
d|d       d}
|	j)                  | j-                  |              |
r=| j$                  }|j&                  }|	j)                  d|j*                  d|
d|d       | j/                  ||      }|rB|d   }t1               }|1|j2                  j5                  |j6                  |j8                  d       |g|	z   d	j;                  |d         gz   gS # t        $ r Y w xY w)
zFormats the header, traceback and exception message for a single exception.

        This may be called multiple times by Python 3 exception chaining
        (PEP 3134).
        r   r   __tracebackhide__r   z[... skipping hidden z frame]r4   r   )r   AttributeErrorr   r   r   rl  rB   r'  get_recordsr-   rN   r  r9   r3  r,  r&   f_localsgetrD   r=   r;   r   rW  rs  r
   r  r  r   r@   r   )rx   r   r   r   r   r   
orig_etyperk  recordsframesskipped
lastrecordrS   recordrD   rA  formatted_exceptionr  rd   s                      r   format_exception_as_a_wholez%VerboseTB.format_exception_as_a_whole  s    
	NNE '0&7DNNY	)S)))""3u:t/?/?@LODS"<iHUW 	 \A%
"7+ 	6IAvvzz:+D+DE$$ LL))--.A1EZqLG%}}~~w> MM$,,V45'	6( [[F!==LMM>>7L:
 #33E6B J ]F!44Z5H5H*J[J[]^_277+>q+A#B"CCDDa  		s   H 	HHr   c                    |J |dz
  }|dz  }||z
  }| j                   r8t        d      }t        j                  || j                        }t        |      }nd }t        j                  |||      }	|}
d}g }|
	 t        j                  |
j                        }|1|j                  }|j                  d      ^}}|dk(  r|
j                  }
Ut        |
j                        }t        ||      }|j!                  |
       t#        |
d	d       }
|
|t$        kD  r\g }|D ]S  }|j                  }|j&                  f}|j(                  }|j*                  }|j!                  t-        d
|||||             U |S t/        t        j,                  j                  ||	            |d  }|D cg c]  }t,        j1                  |       }}|S # t        $ r d}Y w xY wc c}w )Nr   rn  default)r2  )beforeafterpygments_formatterr   .IPythontb_nextz	Raw frame)r   )options)r2   r	   r9   style_with_executing_node_tb_highlightr   Optionsr+   	getmoduletb_framer   r_  r  r0   OSErrorr@  r;   r   FAST_THRESHOLDf_linenor(   r)   r  r   r  )rx   r   r   r   r   r  r  r2  	formatterr  cfmax_lentbsmodmod_name	root_name_FIsr   r&   r@   r  r   rG   r   s                            r   rx  zVerboseTB.get_records1  s    ,q015??%i0E88@R@RSE,59II$$(
 '*n''4?"||H$,NN3$7MI I-ZZ 22;;? '7+GJJrNY-B n" ^#C 
..*||++

#XvudG
 J:''2232HI)*U@CD1y33A6DD
-  * Es   ;AG G %GGGc                    | j                  |||||      }| j                  }|j                  }|j                  dt	        dt               d         z  |}	|	g}
d}d}|}| j                  |      }|r)|J || j                  |j                        z  }|\  }}}nd}t               }|r|| j                  |||||      z  }| j                  |      }|rRt        |d         |vrB|j                  t        |d                || j                  |j                        z  }|\  }}}nd}|rt        |      D ]  }|
|z  }
	 |
S )z5Return a nice text document describing the traceback.r\  rZ  r      Nr   )r  rD   r=   rc  r]  r   r   r   r   r   r   r   reversed)rx   r   r   r   r   r   r  re  rf  rk  structured_traceback_partsr   lines_of_contextformatted_exceptionsr   r   s                   r   r   zVerboseTB.structured_tracebackk  s    #>>ufcSm?HJ }}!>>3R9J9LQ9O1P+PR^_&*V"'($277?	%%% D$J$J6K[K[$\\ !*E63F% D$D$DUFTWYiEa%c c ;;FCIIaL!1_!D##By|$45$(N(NvO_O_(``$%."vs  $,,@#A 	>&*==&	> *)r1   forcec                 "   |s| j                   re| j                  | j                         | _        t        t        j
                        }|5  | j                  j                          t        | d      r| j                  | j                  }nt        j                  x}| _        | j                  l| j                  j                  V| j                  j                  J | j                  j                  | _        | j                  | j                  j                  V|r|j                  r|j                  }|j                  | j                  _        | j                  j                  d|       ddd       t        | d      r| `yy# 1 sw Y   xY w)a4  Call up the pdb debugger if desired, always clean up the tb
        reference.

        Keywords:

          - force(False): by default, this routine checks the instance call_pdb
            flag and does not actually invoke the debugger if the flag is false.
            The 'force' option forces the debugger to activate even if the flag
            is false.

        If the call_pdb flag is set, the pdb interactive debugger is
        invoked. In all cases, the self.tb reference to the current traceback
        is deleted to prevent lingering references which hamper memory
        management.

        Note that each call to pdb() does an 'import readline', so if your app
        requires a special setup for the readline completers, you'll have to
        fix that by hand after invoking the exception handler.N)hookr   )rq   rw   rj   r   r}   __displayhook__resetr   r   last_tracebackr  r  botframeinteraction)rx   r  display_trapr   s       r   r   zVerboseTB.debugger  s.   ( DMMxx,,. 'C,?,?@L 0 4&477+>''C$'$6$66C$'gg)dggoo.I77??666"ggooDG gg)dggoo.I 3;;++C$'LL!$$T3/0 4 0 0s   CFAFFc                    |xs t        j                         \  }}}|| _        | j                  }|j	                          |j                  | j                  |||             |j                  d       |j	                          y r   )r}   rp  r   rz   r   r   r   )rx   infor   r   r   rz   s         r   handlerzVerboseTB.handler  s`    #5s||~,,diivs34dr1   c                     || j                          n| j                  |||f       	 | j                          y# t        $ r t        d       Y yw xY w)z@This hook can replace sys.excepthook (for Python 2.1 or higher).N
KeyboardInterrupt)r  r   KeyboardInterruptr*   )rx   r   r   r   s       r   r   zVerboseTB.__call__  sI    ;LLNLL%-.	)MMO  	)'(	)s   : AA)
LinuxFNr   FTNNNN)Fr   r   )NNN)r   r   r   r   r  rB   r   r   rp   r  rW  rl  rs  r   r   r   r   r  rx  r   r   r  r   r   r1   r   r&  r&    sU   ' $M $!!& &  & 
 &  &  & Pw	 wrC t @SAEAE 'AE m$	AE C=AEF8 8>A8NQ8| (,#'*+,*,* ',* m$	,*
 C=,* %(,*\*d *X	)r1   r&  c                   h    e Zd ZU dZeed<   	 	 	 	 	 ddZddZd Zdde	e   fdZ
d Zd	 Zd
 Zd Zy)FormattedTBa  Subclass ListTB but allow calling with a traceback.

    It can thus be used as a sys.excepthook for Python > 2.1.

    Also adds 'Context' and 'Verbose' modes, not available in ListTB.

    Allows a tb_offset to be specified. This is useful for situations where
    one needs to remove a number of topmost frames from the traceback (such as
    occurs with python programs that themselves execute other python code,
    like Python shells).  modeNc                     g d| _         | j                   dd | _        t        j                  | ||||||||	|
|       t	        dddd      | _        | j                  |       y )N)PlainContextVerboseMinimalr   r  )
ry   rq   rz   r   r'  r(  r+  rj   rm   rn   r   r4   )valid_modesverbose_modesr&  rp   dict_join_charsset_mode)rx   r  ry   rq   rz   r   r'  r(  r+  rj   rm   rn   s               r   rp   zFormattedTB.__init__  su     F!--a24lX#*i'2'2"(	 	 	9  b$(*, 	dr1   c                    || j                   n|}| j                  }|| j                  v rt        j	                  | |||||      S |dk(  rt
        j                  | ||      S | j                          t
        j	                  | |||||      S )Nr  )r   r  r  r&  r   r   r  r+  )rx   r   r   r   r   r   r  s          r   r   z FormattedTB.structured_traceback  s    &/&7DNNY	yy4%%%11eUB	3M  Y,,T5%@@ ..eUB	3M r1   c                 8    | j                   j                  |      S )r   )tb_join_charr   r   s     r   r   zFormattedTB.stb2text  s      %%c**r1   c                    |sS| j                   j                  | j                        dz   t        | j                         z  }| j                   |   | _        nN|| j                   vr't	        d|z   dz   t        | j                         z         t        |t
              sJ || _        | j                  | j                   d   k(  | _        | j                  | j                     | _	        y)zbSwitch to the desired mode.

        If mode is not specified, cycles through the available modes.r   z#Unrecognized mode in FormattedTB: <z>
Valid modes: rn  N)
r  rQ   r  r-   
ValueErrorrB   r   r(  r  r  )rx   r  new_idxs      r   r  zFormattedTB.set_mode  s    
 ''--dii81<$**+,G((1DI)))5< @   "%d&6&6"78 
 dC(((DI!YY$*:*:1*== ,,TYY7r1   c                 @    | j                  | j                  d          y r  r  r  r   s    r   plainzFormattedTB.plain0      d&&q)*r1   c                 @    | j                  | j                  d          y )Nr   r  r   s    r   r   zFormattedTB.context3  r  r1   c                 @    | j                  | j                  d          y )Nrn  r  r   s    r   verbosezFormattedTB.verbose6  r  r1   c                 @    | j                  | j                  d          y )Nr  r  r   s    r   minimalzFormattedTB.minimal9  r  r1   )r  r  FNr   FFNNNNr   r   )r   r   r   r   rB   r#  rp   r   r   r   r  r  r   r  r  r   r1   r   r  r    sP    	 IDI>C04%)	.&+8Xc] 8.++++r1   r  c                   R    e Zd ZdZ	 	 d
dZ	 	 	 ddedee   dee   dee	   de	f
d	Z
y)AutoFormattedTBaG  A traceback printer which can be called on the fly.

    It will find out about exceptions by itself.

    A brief example::

        AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
        try:
          ...
        except:
          AutoTB()  # or AutoTB(out=logfile) where logfile is an open file object
    Nc                    || j                   }|j                          |j                  | j                  ||||             |j                  d       |j                          	 | j	                          y# t
        $ r t        d       Y yw xY w)aI  Print out a formatted exception traceback.

        Optional arguments:
          - out: an open file-like object to direct output to.

          - tb_offset: the number of frames to skip over in the stack, on a
          per-call basis (this overrides temporarily the instance's tb_offset
          given at initialization time.Nr4   r  )rz   r   r   r   r   r  r*   )rx   r   r   r   outr   s         r   r   zAutoFormattedTB.__call__L  sp     ;,,C				$))E63	:;		$			)MMO  	)'(	)s   $A5 5BBr   r   r   r   r   c                     |t        j                         \  }}}t        |t              r|d   | _        n|| _        t
        j                  | |||||      S r  )r}   rp  r   r   r   r  r   r   s         r   r   z$AutoFormattedTB.structured_tracebackd  sX     =!$E63c5!!fDGDG//%i1K
 	
r1   )NNNNNr   )r   r   r   r   r   r   r   r   r   r   r   r   r1   r   r  r  >  sb     59%))8 (,#'*+

 '
 m$	

 C=
 %(
r1   r  c                       e Zd ZdZddZy)ColorTBz;Shorthand to initialize a FormattedTB in Linux colors mode.c                 6    t        j                  | f||d| y )N)ry   rq   )r  rp   )rx   ry   rq   kwargss       r   rp   zColorTB.__init__  s$    T 	:&.	:28	:r1   N)r  r   )r   r   r   r   rp   r   r1   r   r  r  |  s
    E:r1   r  c                   B     e Zd ZdZddZd Z	 	 d fd	Zd Zd Z xZ	S )	SyntaxTBz:Extension which holds some state: the last exception valuec                 D    t         j                  | |||       d | _        y rl   )r   rp   last_syntax_error)rx   ry   rm   rn   s       r   rp   zSyntaxTB.__init__  s    l6&I!%r1   c                 B    || _         t        j                  | |||       y r   )r  r   r   r   s       r   r   zSyntaxTB.__call__  s    !&eUE2r1   c                 n   t        |t              rt        |j                  t              rlt        |j                  t
              rRt        j                  |j                         t        j                  |j                  |j                        }|r||_	        || _
        t        t        | 7  |||||      S )N)r   r   )r   r   r   rB   r@   r   r   r*  r   r   r  ro   r  r   )rx   r   r   r   r   r   newtextr{   s          r   r   zSyntaxTB.structured_traceback  s    
 e[)u~~s3u||S1  0''EG$
!&Xt9%DMW^ : ` 	`r1   c                 ,    | j                   }d| _         |S )z+Return the current error state and clear itN)r  )rx   es     r   clear_err_statezSyntaxTB.clear_err_state  s    ""!%r1   c                 $    dj                  |      S )r   r   r   r   s     r   r   zSyntaxTB.stb2text  s    wws|r1   )r   NNr   )
r   r   r   r   rp   r   r   r  r   r   r   s   @r   r  r    s*    D&3
 CG%&` r1   r  c                 B   	 t         j                  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 n# t        $ r   Y Y Y yxY wY Y yxY wxY w)z(Hopefully pretty robust repr equivalent.r   Nr{   z%s instancezUNRECOVERABLE REPR FAILURE)pydocr   r<  r  r   	text_repr)r   r[   klasss      r   r  r    s    4zzu%% 4	4;  		44 uj$7$T?*{D9(9U+;;; $ 43 se   ! B
:BBBB B$B BBBBBBBBBc                     d ||       z  S )Nz=%sr   r   r<  s     r   r8  r8    s    4;r1   c                      y)Nr   r   r  s     r   r9  r9    s    r1   )?r   	functoolsr+   r   r  r}   ra  r   typesr   typingr   r   r   r   r9   pygments.formatters.terminal256r   pygments.stylesr	   IPython.utils.colorableutils	colorabler  r
   IPython.corer   IPython.core.display_trapr   IPython.core.excolorsr   IPython.utilsr   r   r^   r   IPython.utils.terminalr   r8   DEFAULT_SCHEMEr  	lru_cacherB   r   r%   	FrameTyper0   r   rL   rV   rg   	Colorableri   r   r  r&  r  r  r  r  r  r8  r9  r   r1   r   <module>r     s`  Ot     
     - -  @ - + +  ! 1 2 $ + # 4   S S  *,EOO , ,4$t $L/d BF #BNL$i!! L$`EEW EEP@ @HD) D)P]+)V ]+B8
k 8
|:k :$v $P4: !  # r1   