
    ^h                        S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSK
Jr  SSKJrJrJrJr  SSKrSSKJr  SSKJr  SSKJs  Jr  SSKJr  SS	KJr  SS
KJr  SSKJ r   SSK!J"r"  SSK!J#r$  SSK!J%r%  SSK&J'r'  Sr(Sr)Sr*\RV                  " 5       S\,S\-4S j5       r.S\
R^                  S\-4S jr0\,4S jr1S\24S jr3S r4SS.S jr5 " S S\Rl                  5      r7 " S  S!\75      r8 " S" S#5      r9 " S$ S%\75      r: " S& S'\:\85      r; " S( S)\;5      r< " S* S+\;5      r= " S, S-\85      r>S. r?\?4S/ jr@\?4S0 jrAg)1a]  
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
    )SequenceN)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                     U R                  S5      (       d  g [        U S5       n[        S U 5       5      nSSS5        U$ ! , (       d  f       W$ = f! [         a     gf = f)zv
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   &   #    U  H  nS v   M	     g7f)   N ).0lines     N/home/james-whalen/.local/lib/python3.13/site-packages/IPython/core/ultratb.py	<genexpr>)count_lines_in_py_file.<locals>.<genexpr>   s     +ddds   N)endswithopensumUnicodeError)r   filess      r   count_lines_in_py_filer'      sh     U##	h$+d++ % H	 %$ H  		s-   A AA 
AA A 
A! A!framec                     U R                   R                  nUc1  [        S5        [        R                  " U 5      u  p#U[        U5      -   $ [        U5      $ )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_framer2      sM    $ ||''Hm--e4s5z!!!(++    c                 H     U" U 5      $ !   SU SUR                    S3s $ = f)N< z
() failed>)__name__)valuewhatfuncs      r   _safe_stringr;      s1    4E{44&$--
33s   
 !
has_colorsc                    [         S-
  n/ nU  GH;  nU[        R                  L a/  UR                  UR                  < SUR
                  < S35        MF  UR                  US9R                  S5      S-   nUR                  nUR                  (       aI  U[        [        U5      5      -
  n	[        R                  " U	5      < [        U5      < 3n
UR                  nOSXH4-  n
UR                  nU< U
< UR
                  < SU< 3nUR                  U5        U(       d  GM  UR                  (       d  GM'  UR                  US-   5        GM>     U$ )z
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%*sr6   )INDENT_SIZE
stack_dataLINE_GAPappendlinenoEmNormalrenderrstriplineno
is_currentr/   strr   
make_arrow)r0   Colorsr<   lvalsnumbers_widthres
stack_liner   rI   padnumstart_colors               r   _format_traceback_linesrU      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5Z***JJut|$) * Jr3   c                 B   [         S-
  n/ n[        X U-
  5       GH   u  p[        R                  " U	5      n	U" U	S5      u  pU(       d  U
n	X:X  am  U[	        [        U5      5      -
  n[        R                  " U5      < [        U 5      < 3nUR                  < U< UR                  < SU	< UR                  < 3n	O)SXh4-  nUR                  < U< UR                  < SU	< 3n	UR                  U	5        U(       d  M  X:X  d  M  UR                  US-   5        GM     U$ )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   rK   r6   r@   r>   )rA   	enumerater   cast_unicoder/   rK   r   rL   rE   r   rF   rI   rD   )lnumindexr0   rM   rN   _line_formatrO   rP   ir   new_lineerrrR   rS   s                 r   _simple_format_traceback_linesr_      s    &  !OM
CU5L1%%d+$T51D9#c!f+-C$//4c$i@CD =,,C"(--fmmTJD

45QYJJut|$3 24 Jr3   rI   c                H   [        5       nUb7  UR                  R                  U 5      =nb  Uu  pgUc	  SU SU 3nOXSU SU 3nOOSn[        R                  " [
        R                  " U [        R                  5      5      nUc	  SU SU 3nOSU SU 3nUR                  XgUS9$ )z
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namerI   )	r   compileformat_code_name	util_pathcompress_userr   rX   fs_encodingformat)	r%   ColorFilenameColorNormalrI   ipinstdatarc   rd   tpl_links	            r   _format_filenamerp     s     ]F^^44T::TG>#M?(;-HH ]O+D[MR  &&""4)>)>?
 >#M?(;-HH $M?2Ek]SH??&?AAr3   c                   $  ^  \ rS rSrSrSr     SSS.U 4S jjjrS rS r\	" \\5      r
\S	 5       rS
\\\\\4      4S jrS
\\   4S jr\	S
\4S j5       rS rS rS rSS\\   4S jjr   SS\S\\   S\\   S\\   S\4
S jjrSrU =r$ )TBToolsiB  z2Basic tools used by all traceback printer classes.r   N)debugger_clsc                  > [         [        U ]  XES9  X l        X0l        [        5       U l        U R                  U5        Xl        U=(       d    [        R                  U l        U(       a  U R                  5       U l        g S U l        g N)parentconfig)superrr   __init__call_pdb_ostreamr   color_scheme_table
set_colors
old_schemer   Pdbrs   pdb)selfcolor_schemerz   ostreamrv   rw   rs   	__class__s          r   ry   TBTools.__init__H  sl     	gt%V%C    #3"4%&(8HLL((*DHDHr3   c                 T    U R                   c  [        R                  $ U R                   $ )aC  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stdoutr   s    r   _get_ostreamTBTools._get_ostreaml  s      "]]2szzEEr3   c                 ^    Ub$  [        US5      (       a  [        US5      (       d   eXl        g )Nwriteflush)hasattrr{   )r   vals     r   _set_ostreamTBTools._set_ostreamy  s*    {wsG44g9N9NOOr3   c                 n    [        U SS 5      nU(       a  U$ [        U SS5      (       a  g [        U SS 5      $ )N	__cause____suppress_context__F__context__getattr)exception_valuecauses     r   _get_chained_exceptionTBTools._get_chained_exception  s:    d;L?$:EBBt<<r3   r   c                 d    U R                  U5      nU(       a  UR                  X"R                  4$ g N)r   r   __traceback__)r   evaluechained_evalues      r   get_parts_of_chained_exception&TBTools.get_parts_of_chained_exception  s1     44V<!++^=Y=YYYr3   c                 0    SnSnU(       a  U//nU$ U//nU$ )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_message)TBTools.prepare_chained_exception_message  s3    a$m!$~&G  223Gr3   c                 P    U R                   R                  R                  5       S:g  $ )Nnocolor)r|   active_scheme_namelowerr   s    r   r<   TBTools.has_colors  s"    &&99??AYNNr3   c                     U R                   R                  " U0 UD6  U R                   R                  U l        [	        U S5      (       a+  U R
                  b  U R
                  R                  " U0 UD6  ggg)z;Shorthand access to the color table scheme selector method.r   N)r|   set_active_schemeactive_colorsrM   r   r   r}   )r   argskws      r   r}   TBTools.set_colors  se     	114>2>--;;4DHH$8HH,, %9r3   c                 \   U R                   R                  S:X  aA  U R                   R                  U R                  5        U R                   R                  U l        gU R                   R                  U l        U R                   R                  S5        U R                   R                  U l        g)z=Toggle between the currently active color scheme and NoColor.r   N)r|   r   r   r~   r   rM   r   s    r   color_toggleTBTools.color_toggle  s}     ""55B##55dooF11??DK"55HHDO##55i@11??DKr3   c                 $    SR                  U5      $ )4Convert a structured traceback (a list) to a string.r>   joinr   stbs     r   stb2textTBTools.stb2text  s    yy~r3   	tb_offsetc                 J    U R                  XUXE5      nU R                  U5      $ )zWReturn formatted traceback.

Subclasses may override this if they add extra arguments.
)structured_tracebackr   )r   etyper8   tbr   contexttb_lists          r   textTBTools.text  s+    
 ++E",5@}}W%%r3   r   r   etbnumber_of_lines_of_contextc                     [        5       e)zGReturn a list of traceback frames.

Must be implemented by each class.
)NotImplementedErrorr   r   r   r   r   r   s         r   r   TBTools.structured_traceback  s     "##r3   )rM   r{   rz   r|   rs   r~   r   )r   FNNNN   NNr   ) r7   
__module____qualname____firstlineno____doc__r   ry   r   r   propertyr   staticmethodr   r   r   typeBaseExceptionr   r   r   r   r   boolr<   r}   r   r   intr   r   __static_attributes____classcell__r   s   @r   rr   rr   B  s   < I " " "HF |\2G= =	%m]:;	<$s)  OD O O	-	@& & (,#'*+$$ '$ m$	$
 C=$ %($ $r3   rr   c            
       x    \ rS rSrSrS rS r   SS\S\\	   S\\
   S	\\   4S
 jjrS rS rS rS rS rSrg)ListTBi  a:  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                     U R                   R                  5         U R                   R                  U R                  XU5      5        U R                   R                  S5        g Nr>   )r   r   r   r   r   r   r8   elists       r   __call__ListTB.__call__  sA    499U59:4 r3   c                 >    U(       a  [         R                  " U5      $ g r   )	traceback
extract_tb)r   r   s     r   _extract_tbListTB._extract_tb  s    ''++r3   Nr   r   r   r   c                 x   [        U[        5      (       a  Uu  p6O
[        5       n[        U[        5      (       a  UnOUb  U R	                  U5      nO/ nUc  U R
                  OUn[        U[        5      (       d   eU R                  n/ n	U(       ak  U(       a  [        U5      U:  a  XtS nU	R                  SUR                  < SUR                  < S3S-   5        U	R                  U R                  U5      5        SR                  U R                  X5      5      n
U	R                  U
5        U R!                  U5      nU(       au  [#        US   5      U;  ac  Ub  U R%                  UR&                  5      S   OSnUu  pnUR)                  [#        US   5      5        SnU R+                  UUX64UU5      U-   U	-   n	U	$ )	aU  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):r>    r   r   )
isinstancetuplesetlistr   r   r   rM   r/   rD   normalEmrF   extend_format_listr   _format_exception_onlyr   idr   r   addr   )r   r   r   r   r   r   chained_exc_idsr   rM   out_listr0   	exceptionchained_exception_messagechained_exceptions_tb_offsets                 r   r   ListTB.structured_traceback  s   > c5!!#& C!eOc4  E_$$S)EE&/&7DNNY	)S))))SZ)3j)OO#__fmm=?CD EOOD--e4533EBC77?	"Yq\*/A % 66v7G7GHK &
 "+E39Q< 01+,())*0 ,,   r3   c                    U R                   n/ n[        U5       H  u  nu  pVpxU[        U5      S-
  :X  a.  UR                  UR                  UR
                  UR                  4O#UR                  UR                  UR                  S4u  pp[        X[XS9nU	 SU 3nUS:w  a  USU
 U U	 S3-  nOUS-  nU(       a  X SUR                  5        U	 S3-  nUR                  U5        M     U$ )	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   r`     <module>z in r>       )rM   rW   r/   r   nameEm
filenameEmr   rF   rd   r   rp   striprD   )r   extracted_listrM   output_listindr   rI   rd   r   	normalColnameColfileCollineColfnsitems                  r   r   ListTB._format_listD  s    3<^3L/C/(D #n-11 &--1B1BFKKPmmV[[&//2F	 1I #8iOC[3%(Dz!$wivi[;;)4

~i[CCt$# 4M& r3   c                    SnU R                   n/ n[        R                  " UR                  UR                  -   UR
                  -   5      nUc  UR                  US-   5        GO[        U[        5      (       Ga  SnUR                  (       d  SUl	        UR                  (       a8  UR                  n[        R                  " UR                  UR                  5      nOSnSnUR                  UR                  < S[        UR                  UR                  UR                  US:X  a  SOUS	9< UR
                  < S35        US:X  a!  [        R                  " UR                   S
5      nUGb
  Sn	U	[#        U5      :  aD  X   R%                  5       (       a-  U	S-  n	U	[#        U5      :  a  X   R%                  5       (       a  M-  UR                  UR&                  < SUR)                  5       < UR
                  < S35        UR*                  bj  Sn
XUR*                  S-
    H#  nUR%                  5       (       a  X-  n
M  U
S-  n
M%     UR                  UR,                  < U
< SUR
                  < S35         UR.                  n
U
(       a5  UR                  U< UR                  < SUR
                  < SU
< S35        OUR                  SU-  5        UR5                  S [7        US/ 5       5       5        U(       a>  [9        5       nUb1  UR:                  R=                  UR                  UR                  S5        U$ ! [0         a    U R3                  U5      n
 Nf = f)a  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
FNr>   Tz<string>unknownr   r   r`   zutf-8r   r   r  r6   ^r   z%s
c              3   *   #    U  H	  o S 3v   M     g7f)r>   Nr   )r   xs     r   r   0ListTB._format_exception_only.<locals>.<genexpr>  s     Q1PABx1Ps   	__notes__)rM   r   rX   excNamer7   rF   rD   
issubclassSyntaxErrorr   rI   	linecachegetliner   rp   r  r   r/   isspacer   r  offsetcaretmsg	Exception	_some_strr   r   r   hookssynchronize_with_editor)r   r   r8   have_filedatarM   r  styperI   textliner\   r&   crm   s                r   r   ListTB._format_exception_onlyh  s    &&v~~'F'VW=ut|,%-- $~~
u~<<"\\F(00NH&F!H"" (!NN"--"OO,2i,?DV	 
 r>(55ejj'JH'Ac(m+0C0C0E0EQ c(m+0C0C0E0E&&*0++x~~7GW ||/"!)ELL1,<!=A yy{{ ! !S	 ">
 $**+1<<FMMJ*II ""&+V^^V]]AN ""6E>2 QR1PQQ  ]F!44U^^U\\STU%  *NN5)*s   7L= =MMc                 .    [         R                  XU5      $ )zOnly print the exception type and message, without a traceback.

Parameters
----------
etype : exception type
value : exception value
)r   r   )r   r   r8   s      r   get_exception_onlyListTB.get_exception_only  s     **4>>r3   c                     U R                   nUR                  5         UR                  SR                  U R	                  X5      5      5        UR                  5         g)zOnly print the exception type and message, without a traceback.

Parameters
----------
etype : exception type
evalue : exception value
r>   N)r   r   r   r   r*  )r   r   r   r   s       r   show_exception_onlyListTB.show_exception_only  s?     ,,dii 7 7 FGHr3   c                      [         R                  " [        U5      5      $ !   S[        U5      R                  -  s $ = f)Nz<unprintable %s object>)r   rX   rK   r   r7   )r   r8   s     r   r!  ListTB._some_str  s:    	E))#e*55	E-U0D0DDDs   ! =r   r   )r7   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r*  r-  r!  r   r   r3   r   r   r     sy    O&!
 (,#'QQ 'Q m$	Q
 C=Qf"HTl?Er3   r   c                       \ rS rSr% Sr\\   \S'   \\   \S'   \\	   \S'   \\	   \S'   \
S 5       rSSS	.S\\   S\S\\	   4S
 jjr\S 5       r\S 5       r\S 5       rSrg)	FrameInfoi  za
Mirror of stack data's FrameInfo, but so that we can bypass highlighting on
really long frames.
descriptionr   rI   r   c                     U " [        USS 5      [        USS 5      [        USS 5      [        USS 5      [        USS 5      US S9$ )Nr3  r   rI   r(   codesdr   r   )cls
frame_infos     r   _from_stack_data_FrameInfo$FrameInfo._from_stack_data_FrameInfo  sQ    Jt4J
D1J$/J.J-
 	
r3   Nr6  c                    Xl         X l        X0l        X@l        XPl        X`l        Xpl        Uc!  [        R                  " U5      nUS   U l	        g g Nr   )
r3  r   rI   r(   r5  _sdr   r-   r.   	raw_lines)	r   r3  r   rI   r(   r5  r7  r   ixs	            r   ry   FrameInfo.__init__  sO     ' 
	 :''.BUDN r3   c                 T    U R                   (       a  U R                   R                  $ / $ r   )r>  variables_in_executing_piecer   s    r   rC  &FrameInfo.variables_in_executing_piece  s    8888888Ir3   c                 .    U R                   R                  $ r   )r>  r0   r   s    r   r0   FrameInfo.lines  s    xx~~r3   c                 R    U R                   (       a  U R                   R                  $ g r   )r>  	executingr   s    r   rH  FrameInfo.executing  s    8888%%%r3   )r>  r5  r   r3  r   r(   rI   r?  )r7   r   r   r   r   r   rK   __annotations__r   r   classmethodr:  ry   r   rC  r0   rH  r   r   r3   r   r2  r2    s    
 #sm#Jc]	
 	
& #c]# # c
	#0      r3   r2  c                   "   \ rS rSrSrSrSr          SS\S\S\	S	\S
\4
S jjr
S\4S jrSS\S\4S jjrS rS\S\\   S\\   S\\	   4S jrS\S\	S\	4S jr   SS\S\\   S\\   S\\	   S\	4
S jjrSS\4S jjrS S jrS!S jrSrg)"	VerboseTBi#  a+  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:ansiyellowdefaultNr   rz   r   long_headerinclude_varsc           
          [         R                  U UUUU	U
US9  X@l        XPl        X`l        Uc  [
        R                  nXpl        SU l        g)a7  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).)r   rz   r   rv   rw   rs   NT)	rr   ry   r   rO  rP  r  
checkcachecheck_cacheskip_hidden)r   r   rz   r   r   rO  rP  rS  rs   rv   rw   s              r   ry   VerboseTB.__init__.  s`    & 	%% 	 	
 #&( #..K&r3   r9  c                    [        U[        5      (       d   eU R                  nUR                  n[        UR                  [
        R                  5      (       a#  SUR                  < SUR                  < SU< S3$ S[        -  nUR                  < SU< U< 3nSUR                   SUR                   SU 3nSUR                  < S	UR                  < S
U< 3nSUR                  < SU< 3n[        UR                  UR                  UUR                  S9n	[         R"                  " UR$                  5      u  ppUR&                  b  UR&                  R)                  5       nOSnUS:X  a  SnODU R*                  (       a  [,        O[.        n [         R0                  " XXUS9nUR3                  UUS9nSn/ nU R*                  (       aI   UR6                  nU H6  nUR9                  UUR:                  [=        UR>                  5      4-  5        M8     U(       a  U< URC                  U5      < 3nU	 U(       a  SOS U S3nUR                  Gc)  UR                  < S	U< 3nU[D        RF                  " UR                  5      -  n	U	< SU< S3n[H        RJ                  " U RL                  RN                  U S9RP                  nURR                  RT                  nUR                  S   nURV                  nUU-
  nUURX                  :  a5  [[        UURX                  -
  S5      nUURX                  -   nURX                  nOSnUURX                  -   nUUU nU< SRC                  []        UUUUUU5      5      < 3$ USRC                  [_        UR`                  X Rb                  U5      5      -  nU$ ! [4         a    X~-  n GNf = f! [@         a    UR9                  S5         GNf = f)zFormat a single stack framer  z[... skipping similar frames: ]r>   r6   zin z{file}z{scope}z%sz"(***failed resolving arguments***)z%s z= %sr`   ?r  r   )formatvalue)r%   scopez<Exception trying to inspect frame. No more locals available.z, )stylerv   r   )2r   r2  rM   rF   r>  rB   RepeatedFramesr  r3  rA   valEmvNamerp   r   r  rI   r-   getargvaluesr(   rH  code_qualnamerP  eqreprnullreprformatargvaluesrj   KeyErrorrC  rD   rd   reprr8   r   r   rg   rh   r   Parserr|   r   format2r5  co_firstlinenor?  r   maxr_   rU   r0   r<   ) r   r9  rM   ColorsNormalindent	em_normaltpl_calltpl_call_failtpl_name_vallinkr   varargsvarkwlocals_r:   callvar_reprrZ  rN   
lvals_listfibpvarresultro   levelr[   
first_linecurrent_liner?  rZ   startstops                                    r   format_recordVerboseTB.format_recordV  s   *i0000}}jnnj&?&?@@
 6 6F F {""(,,E	hv||nIl^TLLLL

 +1,,E$$	
 )0(<(<Z=M=M(N%u+''557DD:D "&!2!2vH,//5x  D>( 
	 ">>C%%lchhSYY5P&PQ   $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'$$foou F
 ]  ," %+#,8  !!Rs%   &N. 
AO .O ?O O"!O"r   long_versionc                 R   U R                   nUR                  nUR                  < U< U< 3n[        S[	        5       S   5      nU(       a  S[
        R                  R                  5       S   -   S-   [
        R                  -   n[        R                  " [        R                  " 5       5      nUR                  < SU-  < U< SU< SU[        U5      -
  [        U5      -
  -  < U< SUR                  U5      < 3	n	U	S-  n	U	$ U< S	R                  U[        U5      -
  5      < 3n	U	$ )
NK   r   zPython z: -r>   r6   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))rM   rF   r  minr   r   versionsplit
executabletimectimetopliner/   rjust)
r   r   r  colorscolorsnormalexcwidthpyverdateheads
             r   prepare_header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
 r3   c                   ^ U R                   nUR                  m [        [        X45      u  pE[        US/ 5      n[        U[        5      (       a  [        U[        [        45      (       a  [        US[        S9/nSR                  UR                  UT[        R                  " U5      5      /U4S jU 5       Q$ !   [        [        R
                  " 5       S S p![        [        X45      u  pE N= f)N   r  )r:   z
{}{}{}: {}c           	   3      >#    U  H5  nS R                  T[        [        R                  " U5      S5      5      v   M7     g7f)z{}{}noteN)rj   r;   r   rX   )r   nr  s     r   r   -VerboseTB.format_exception.<locals>.<genexpr>  sA       A  ,y/E/Ea/H&"Q  s   =A )rM   rF   maprK   r   exc_infor   r   r   bytesr;   re  rj   r  r   rX   )r   r   r   r  	etype_str
evalue_strnotesr  s          @r   format_exceptionVerboseTB.format_exception  s    }}	>$'e_$=!I R0%**je.M.M!%4@AE &&z2	
 	
 	
	>!36$'e_$=!Izs   B: :2C.r   r   c           
      j   Un UR                   nUc  U R                  OUn[        U[        5      (       d   eU R                  [        U5      U R                  5      nU(       a  U R                  X4U5      O/ n/ n	Sn
[        U5      S-
  n[        U5       H  u  p[        UR                  [        R                  5      (       dH  U R                  (       a7  UR                  R                   R#                  SS5      (       a  X:w  a  U
S-  n
Mv  U
(       aB  U R$                  nUR&                  nU	R)                  SUR*                  < SU
< SU< S35        Sn
U	R)                  U R-                  U5      5        M     U
(       a@  U R$                  nUR&                  nU	R)                  SUR*                  < SU
< SU< S35        U R/                  X5      nU(       aC  US   n[1        5       nUb1  UR2                  R5                  UR6                  UR8                  S5        U/U	-   U-   /$ ! [         a     GN!f = f)	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]r>   )r7   AttributeErrorr   r   r   r  rK   rO  get_recordsr/   rW   r>  rB   r\  rT  r(   f_localsgetrM   rF   rD   r  r  r  r   r"  r#  r   rI   )r   r   r   r   r   r   
orig_etyper  recordsframesskipped
lastrecordr\   recordrM   rj  formatted_exceptionr9  rm   s                      r   format_exception_as_a_whole%VerboseTB.format_exception_as_a_whole  s    
	NNE '0&7DNNY	)S))))""3u:t/?/?@LODSiHUW 	 \A%
"7+IAvzz:+D+DEE$$ LL))--.A1EEqLG%}}~~w> MM$,,V45' ,( [[F!==LMM>>7L:
 #33EB J ]F!44Z5H5H*J[J[]^_"5566a  		s   H$ $
H21H2r   c                    Uc   eUS-
  nUS-  nXE-
  nU R                   (       a?  [        U R                  5      n[        R                  " XpR
                  5      n[        US9nOS n[        R                  " UUUS9n	Un
Sn/ nU
b   [        R                  " U
R                  5      nUb3  UR                  nUR                  S5      tnnUS:X  a  U
R                  n
MZ  [        U
R                  5      n[!        X5      nUR#                  U
5        [%        U
SS 5      n
U
b  M  U[&        :  a[  / nU HQ  nUR                  nUR(                  4nUR*                  nUR,                  nUR#                  [/        S	UUUUUS
95        MS     U$ [1        [        R.                  R                  XS95      US  nU Vs/ s H  n[.        R3                  U5      PM     nnU$ ! [         a    Sn Nf = fs  snf )Nr   r  )r[  )beforeafterpygments_formatterr   .IPythontb_nextz	Raw frame)r   )options)r<   r
   _tb_highlight_stylerB   style_with_executing_node_tb_highlightr	   Optionsr-   	getmoduletb_framer7   r  r  r2   OSErrorri  rD   r   FAST_THRESHOLDf_linenor*   r+   r2  r   r:  )r   r   r   r   r   r  r  r[  	formatterr  cfmax_lentbsmodmod_name	root_name_FIsr   r(   rI   r5  r   rP   r   s                            r   r  VerboseTB.get_recordsR  s    ,q01??%d&>&>?E88@R@RSE,59II$$(
 '*n''4?"||H$,NN3$7MI I-ZZ 22;;? '+GJJrNY-B n" ^#C..*||++

#XvudG  J:''2232HI)*U@CD1y33A6D
-  * Es   AG G 1G&G#"G#c                    U R                  XX5U5      nU R                  nUR                  nUR                  < S[	        S[        5       S   5      -  < U< 3n	U	/n
SnSnUnU R                  U5      nU(       a(  Uc   eXR                  UR                  5      -  nUu  pnOSn[        5       nU(       a  XR                  XX<U5      -  nU R                  U5      nU(       aR  [        US   5      U;  a@  UR                  [        US   5      5        XR                  UR                  5      -  nUu  pnOSnU(       a  M  [        U5       H  nX-  n
M	     U
$ )z5Return a nice text document describing the traceback.r  r  r      Nr   )r  rM   rF   r  r  r   r   r   r   r   r   r   reversed)r   r   r   r   r   r   r  r  r  r  structured_traceback_partsr   lines_of_contextformatted_exceptionsr   r   s                   r   r   VerboseTB.structured_traceback  sr    #>>uc?HJ }}!>>3R9J9LQ9O1P+PR^_&*V"'($277?	%%% $J$J6K[K[$\\ !*E3F% $D$DUTWEa%c c ;;FCIIaL!1_!D##By|$45$(N(NvO_O_(``$%."s f $,,@#A&=& $B *)r3   forcec                 >   U(       d  U R                   (       Ga  U R                  c  U R                  5       U l        [        [        R
                  S9nU   U R                  R                  5         [        U S5      (       a  U R                  b  U R                  nO[        R                  =o0l        U R                  bq  U R                  R                  bZ  U R                  R                  c   eU R                  R                  U l        U R                  b  U R                  R                  b  MZ  U(       a  UR                  (       a  UR                  nUR                  U R                  l        [        R                  S:  a  [        R                  O[        [        S[        R                  5      nU(       a  U R                  R!                  SU5        OU R                  R!                  SU5        SSS5        [        U S5      (       a  U ?gg! , (       d  f       N#= f)a  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   )r     last_exc)rz   r   rs   r   r   __displayhook__resetr   r   last_tracebackr  r  botframeversion_info
last_valuer   interaction)r   r  display_trapr   r  s        r   r   VerboseTB.debugger  so   ( DMMMxx,,. 'C,?,?@L 4&&477+>''C$'$6$66C'gg)dggoo.I77??666"ggooDG gg)dggoo.I 3;;++C$'LL! ),(8(87(BcnnPSU_adaoaoHpHH((s3HH((s3' * 4 + s   CH/CH
Hc                    U=(       d    [         R                  " 5       u  p#nX@l        U R                  nUR	                  5         UR                  U R                  X#U5      5        UR                  S5        UR	                  5         g r   )r   r  r   r   r   r   r   )r   infor   r   r   r   s         r   handlerVerboseTB.handler  sZ    #5s||~,,diis34dr3   c                     Uc  U R                  5         OU R                  XU45         U R                  5         g! [         a    [        S5         gf = f)z@This hook can replace sys.excepthook (for Python 2.1 or higher).N
KeyboardInterrupt)r  r   KeyboardInterruptr,   )r   r   r   r   s       r   r   VerboseTB.__call__  sG    ;LLNLL%-.	)MMO  	)'(	)s   : AA)rS  rP  rO  r   rT  r   r   )
LinuxFNr   FTNNNN)Fr   r   )NNN)r7   r   r   r   r   r  r  rK   r   r   ry   r2  r  r  r  r   r   r   r   r  r  r   r   r  r   r   r   r3   r   rM  rM  #  sV   ' $M# $!!& &  & 
 &  &  & Pw	 wrC t @
@A7A7 'A7 m$	A7 C=A7F8 8>A8NQ8| (,#'*+,*,* ',* m$	,*
 C=,* %(,*\0d 0d	)r3   rM  c                   x    \ rS rSr% Sr\\S'        SS jrSS jrS r	SS\
\   4S jjrS	 rS
 rS rS rSrg)FormattedTBi  a  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                     / SQU l         U R                   SS U l        [        R                  XUXEXgXXS9  [	        SSSSS9U l        U R                  U5        g )N)PlainContextVerboseMinimalr   r  )
r   rz   r   r   rO  rP  rS  rs   rv   rw   r   r>   )valid_modesverbose_modesrM  ry   dict_join_charsset_mode)r   r  r   rz   r   r   rO  rP  rS  rs   rv   rw   s               r   ry   FormattedTB.__init__  sk     F!--a24X#*'2'2"(	 	 	9  b$(*, 	dr3   c                    Uc  U R                   OUnU R                  nX`R                  ;   a  [        R	                  XX#XE5      $ US:X  a  [
        R                  XU5      $ U R                  5         [
        R	                  XX#XE5      $ )Nr  )r   r  r  rM  r   r   r*  rS  )r   r   r8   r   r   r   r  s          r   r    FormattedTB.structured_traceback)  s    &/&7DNNY	yy%%%11U	  Y,,T%@@ ..U	 r3   c                 8    U R                   R                  U5      $ )r   )tb_join_charr   r   s     r   r   FormattedTB.stb2text<  s      %%c**r3   c                    U(       dS  U R                   R                  U R                  5      S-   [        U R                   5      -  nU R                   U   U l        OSXR                   ;  a'  [	        SU-   S-   [        U R                   5      -   5      e[        U[
        5      (       d   eXl        U R                  U R                   S   :H  U l        U R                  U R                     U l	        g)zZSwitch to the desired mode.

If mode is not specified, cycles through the available modes.r   z#Unrecognized mode in FormattedTB: <z>
Valid modes: r  N)
r  rZ   r  r/   
ValueErrorrK   r   rP  r  r  )r   r  new_idxs      r   r  FormattedTB.set_mode@  s    
 ''--dii81<$**+,G((1DI)))5< @   "%d&6&6"78 
 dC((((I!YY$*:*:1*== ,,TYY7r3   c                 @    U R                  U R                  S   5        g r=  r  r  r   s    r   plainFormattedTB.plainW      d&&q)*r3   c                 @    U R                  U R                  S   5        g )Nr   r  r   s    r   r   FormattedTB.contextZ  r  r3   c                 @    U R                  U R                  S   5        g )Nr  r  r   s    r   verboseFormattedTB.verbose]  r  r3   c                 @    U R                  U R                  S   5        g )Nr  r  r   s    r   minimalFormattedTB.minimal`  r  r3   )r  rP  r  r  r  r  )r  r  FNr   FFNNNNr   r   )r7   r   r   r   r   rK   rJ  ry   r   r   r   r  r  r   r  r  r   r   r3   r   r  r    sP    	 IDI>C04%)	.&+8Xc] 8.++++r3   r  c                   `    \ rS rSrSr  SS jr   SS\S\\   S\\	   S\\
   S	\
4
S
 jjrSrg)AutoFormattedTBie  a'  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                    Uc  U R                   nUR                  5         UR                  U R                  XX55      5        UR                  S5        UR                  5          U R	                  5         g! [
         a    [        S5         gf = f)a!  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.Nr>   r  )r   r   r   r   r   r  r,   )r   r   r   r   outr   s         r   r   AutoFormattedTB.__call__s  sl     ;,,C				$))E3:;		$			)MMO  	)'(	)s   #A4 4BBr   r   r   r   r   c                     Uc  [         R                  " 5       u  pn[        U[        5      (       a  US   U l        OX0l        [
        R                  XX#XE5      $ r=  )r   r  r   r   r   r  r   r   s         r   r   $AutoFormattedTB.structured_traceback  sP     =!$E3c5!!!fDGG//i
 	
r3   )r   )NNNNNr   )r7   r   r   r   r   r   r   r   r   r   r   r   r   r   r3   r   r  r  e  sg     59%))8 (,#'*+

 '
 m$	

 C=
 %(
 
r3   r  c                   "    \ rS rSrSrSS jrSrg)ColorTBi  z;Shorthand to initialize a FormattedTB in Linux colors mode.c                 8    [         R                  " U 4UUS.UD6  g )N)r   rz   )r  ry   )r   r   rz   kwargss       r   ry   ColorTB.__init__  s$    T 	:&.	:28	:r3   r   N)r  r   )r7   r   r   r   r   ry   r   r   r3   r   r  r    s
    E:r3   r  c                   P   ^  \ rS rSrSrS	S jrS r  S
U 4S jjrS rS r	Sr
U =r$ )SyntaxTBi  z:Extension which holds some state: the last exception valuec                 :    [         R                  XX#S9  S U l        g ru   )r   ry   last_syntax_error)r   r   rv   rw   s       r   ry   SyntaxTB.__init__  s    6I!%r3   c                 <    X l         [        R                  XX#5        g r   )r   r   r   r   s       r   r   SyntaxTB.__call__  s    !&U2r3   c                   > [        U[        5      (       a  [        UR                  [        5      (       aw  [        UR                  [
        5      (       aX  [        R                  " UR                  5        [        R                  " UR                  UR                  5      nU(       a  Xbl	        X l
        [        [        U ]7  XUXES9$ )N)r   r   )r   r  r   rK   rI   r   r  rR  r  r   r   rx   r  r   )r   r   r8   r   r   r   newtextr   s          r   r   SyntaxTB.structured_traceback  s    
 e[))u~~s33u||S11  0''EG$
!&Xt9%DM : ` 	`r3   c                 ,    U R                   nSU l         U$ )z+Return the current error state and clear itNr   )r   es     r   clear_err_stateSyntaxTB.clear_err_state  s    ""!%r3   c                 $    SR                  U5      $ )r   r   r   r   s     r   r   SyntaxTB.stb2text  s    wws|r3   r(  )r   NNr   )r7   r   r   r   r   ry   r   r   r*  r   r   r   r   s   @r   r  r    s/    D&3
 CG%&`  r3   r  c                 Z    [         R                  R                  U 5      $ ! [         a    e    [        U 5      s $ ! [         a    e    [	        U SS5      nU(       a  [        U5      s s $ [	        U SS5      nU(       a  S[        U5      -  s s $   g! [         a    e      g= f= f= f)z(Hopefully pretty robust repr equivalent.r7   Nr   z%s instancezUNRECOVERABLE REPR FAILURE)pydocr   re  r  r   	text_repr)r8   rd   klasss      r   r0  r0    s    4zzu%% 4	4;  		44 uj$7$T?*{D9(9U+;;; $ 43s_   ! B*
;B*B'B&B''B*+!BB'B*B*B$ B'!B*$B''B*c                     SU" U 5      -  $ )Nz=%sr   r8   re  s     r   ra  ra    s    4;r3   c                     g)Nr   r   r3  s     r   rb  rb    s    r3   )Br   collections.abcr   	functoolsr-   r  r/  r   r  r   typesr   typingr   r   r   r   rB   pygments.formatters.terminal256r	   pygments.stylesr
   IPython.utils.colorableutils	colorabler  r   IPython.corer   IPython.core.display_trapr   IPython.core.excolorsr   IPython.utilsr   r   rg   r   IPython.utils.terminalr   rA   DEFAULT_SCHEMEr  	lru_cacherK   r   r'   	FrameTyper2   r;   r   rU   r_   rp   	Colorablerr   r   r2  rM  r  r  r  r  r0  ra  rb  r   r3   r   <module>rG     sk  Ot %     
     - -  @ - + +  ! 1 2 $ + # 4   S S  *,EOO , ,4 $' 4$t $L/d BF #BNL$i!! L$`JEW JEZ@ @H]) ])B]+)V ]+B8
k 8
|:k :$v $P4: !  # r3   