Format: application/x-python-code
⬇ Download File (37.8 KB)
   1  o
   2  
   3  ��h\��@sdZddlmZddlZddlZddlZddlZddlZddlZddl	Z	ddl
   4  Z
   5  ddl
   6  mZddlm
   7  Z
   8  mZmZmZddlZddlmZddlmZddlmmZddlmZdd	lmZdd
   9  lmZddlm Z ddl!m"Z"dd
  10  l!m#Z$ddl!m%Z%ddl&m'Z'dZ(dZ)dZ*e�+�de,de-fdd��Z.de
  11  j/de-fdd�Z0e,fdd�Z1de2fdd�Z3dd �Z4dd!�d"d#�Z5Gd$d%�d%ej6�Z7Gd&d'�d'e7�Z8Gd(d)�d)�Z9Gd*d+�d+e7�Z:Gd,d-�d-e:e8�Z;Gd.d/�d/e;�Z<Gd0d1�d1e;�Z=Gd2d3�d3e8�Z>d4d5�Z?e?fd6d7�Z@e?fd8d9�ZAdS):a]
  12  Verbose and colourful traceback formatting.
  13  
  14  **ColorTB**
  15  
  16  I've always found it a bit hard to visually parse tracebacks in Python.  The
  17  ColorTB class is a solution to that problem.  It colors the different parts of a
  18  traceback in a manner similar to what you would expect from a syntax-highlighting
  19  text editor.
  20  
  21  Installation instructions for ColorTB::
  22  
  23      import sys,ultratb
  24      sys.excepthook = ultratb.ColorTB()
  25  
  26  **VerboseTB**
  27  
  28  I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
  29  of useful info when a traceback occurs.  Ping originally had it spit out HTML
  30  and intended it for CGI programmers, but why should they have all the fun?  I
  31  altered it to spit out colored text to the terminal.  It's a bit overwhelming,
  32  but kind of neat, and maybe useful for long-running programs that you believe
  33  are bug-free.  If a crash *does* occur in that type of program you want details.
  34  Give it a shot--you'll love it or you'll hate it.
  35  
  36  .. note::
  37  
  38    The Verbose mode prints the variables currently visible where the exception
  39    happened (shortening their strings if too long). This can potentially be
  40    very slow, if you happen to have a huge data structure whose string
  41    representation is complex to compute. Your computer may appear to freeze for
  42    a while with cpu usage at 100%. If this occurs, you can cancel the traceback
  43    with Ctrl-C (maybe hitting it more than once).
  44  
  45    If you encounter this kind of situation often, you may want to use the
  46    Verbose_novars mode instead of the regular Verbose, which avoids formatting
  47    variables (but otherwise includes the information and context given by
  48    Verbose).
  49  
  50  .. note::
  51  
  52    The verbose mode print all variables in the stack, which means it can
  53    potentially leak sensitive information like access keys, or unencrypted
  54    password.
  55  
  56  Installation instructions for VerboseTB::
  57  
  58      import sys,ultratb
  59      sys.excepthook = ultratb.VerboseTB()
  60  
  61  Note:  Much of the code in this module was lifted verbatim from the standard
  62  library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
  63  
  64  Color schemes
  65  -------------
  66  
  67  The colors are defined in the class TBTools through the use of the
  68  ColorSchemeTable class. Currently the following exist:
  69  
  70    - NoColor: allows all of this module to be used in any terminal (the color
  71      escapes are just dummy blank strings).
  72  
  73    - Linux: is meant to look good in a terminal like the Linux console (black
  74      or very dark background).
  75  
  76    - LightBG: similar to Linux but swaps dark/light colors to be more readable
  77      in light background terminals.
  78  
  79    - Neutral: a neutral color scheme that should be readable on both light and
  80      dark background
  81  
  82  You can implement other color schemes easily, the syntax is fairly
  83  self-explanatory. Please send back new schemes you develop to the author for
  84  possible inclusion in future releases.
  85  
  86  Inheritance diagram:
  87  
  88  .. inheritance-diagram:: IPython.core.ultratb
  89     :parts: 3
  90  �)�SequenceN)�
  91  TracebackType)�Any�List�Optional�Tuple)�Terminal256Formatter)�get_style_by_name)�get_ipython)�debugger)�DisplayTrap)�exception_colors)�
  92  PyColorize)�path)�	py3compat)�get_terminal_size��NoColori'�filename�returncCsj|�d�sdSz#t|d��}tdd�|D��}Wd�W|S1s#wYW|Sty4YdSw)z�
  93      Given a filename, returns the number of lines in the file
  94      if it ends with the extension ".py". Otherwise, returns 0.
  95      z.pyr�rcss�|]}dVqdS)�N�)�.0�linerr�Jc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/ultratb.py�	<genexpr>�s�z)count_lines_in_py_file.<locals>.<genexpr>N)�endswith�open�sum�UnicodeError)r�file�srrr�count_lines_in_py_file�s
  96  ����r#�framecCs:|jj}|durtd�t�|�\}}|t|�St|�S)a
  97      Given a frame object, returns the total number of lines in the file
  98      containing the frame's code object, or the number of lines in the
  99      frame's source code if the file is not available.
 100  
 101      Parameters
 102      ----------
 103      frame : FrameType
 104          The frame object whose line number is to be determined.
 105  
 106      Returns
 107      -------
 108      int
 109          The total number of lines in the file containing the frame's
 110          code object, or the number of lines in the frame's source code
 111          if the file is not available.
 112      NzNo file....)�f_code�co_filename�print�inspect�getsourcelines�lenr#)r$r�lines�firstrrr�get_line_number_of_frame�sr-cCs*z||�WSd|�d|j�d�YS)N�<� z
 113  () failed>)�__name__)�value�what�funcrrr�_safe_string�s
 114  r4�
 115  has_colorscCs�td}g}|D]^}|tjur|�d|j|jf�q|j|d��d�d}|j}|j	rC|t
 116  t|��}	dt�
 117  |	�t|�f}
 118  |j}n	d||f}
 119  |j}d||
 120  |j|f}|�|�|rf|j	rf|�|d�q|S)a
 121  
 122      Format tracebacks lines with pointing arrow, leading numbers...
 123  
 124      Parameters
 125      ----------
 126      lines : list[Line]
 127      Colors
 128          ColorScheme used.
 129      lvals : str
 130          Values of local variables, already colored, to inject just after the error line.
 131      rz
 132  %s   (...)%s
 133  )�	pygmented�
 134  �%s%s�%*s�	%s%s%s %s)�INDENT_SIZE�
 135  stack_dataZLINE_GAP�append�linenoEm�Normal�render�rstrip�lineno�
 136  is_currentr*�strr�
 137  make_arrow)r+�Colorsr5�lvals�
 138  numbers_width�resZ
 139  stack_linerrB�pad�numZstart_colorrrr�_format_traceback_lines�s(
 140  
 141  
 142  �rLcCs�td}g}t|||�D]Z\}}	t�|	�}	||	d�\}
 143  }|s!|
 144  }	||krE|tt|��}dt�|�t|�f}
 145  d|j|
 146  |j	|	|j
 147  f}	nd||f}
 148  d|j|
 149  |j
 150  |	f}	|�|	�|rg||krg|�|d�q
 151  |S)a�
 152      Format tracebacks lines with pointing arrow, leading numbers...
 153  
 154      Parameters
 155      ==========
 156  
 157      lnum: int
 158          number of the target line of code.
 159      index: int
 160          which line in the list should be highlighted.
 161      lines: list[string]
 162      Colors:
 163          ColorScheme used.
 164      lvals: bytes
 165          Values of local variables, already colored, to inject just after the error line.
 166      _line_format: f (str) -> (str, bool)
 167          return (colorized version of str, failure to do so)
 168      rrDr8z%s%s%s %s%sr9r:r7)
 169  r;�	enumerater�cast_unicoder*rDrrEr>rr?rBr=)�lnum�indexr+rFrG�_line_formatrHrI�ir�new_line�errrJrKrrr�_simple_format_traceback_lines�s0
 170  �
 171  �rU�rBc	Cs�t�}|dur+|j�|�}dur+|\}}|dur"d|�d|��}n*d|�d|��}n!d}t�t�|tj��}|durDd|�d|��}nd|�d|��}|j|||d�S)a
 172      Format filename lines with custom formatting from caching compiler or `File *.py` by default
 173  
 174      Parameters
 175      ----------
 176      file : str
 177      ColorFilename
 178          ColorScheme's filename coloring to be used.
 179      ColorNormal
 180          ColorScheme's normal coloring to be used.
 181      Nz{label} z{name}z{name}, line {lineno}�Filez{name}:{lineno})�label�namerB)	r
 182  �compileZformat_code_name�	util_path�
 183  compress_userrrNZfs_encoding�format)	r!Z
 184  ColorFilenameZColorNormalrB�ipinst�datarXrY�tpl_linkrrr�_format_filenames ��racs�eZdZdZdZ					d&dd��fdd�Zd	d
 185  �Zdd�Zeee�Z	e
 186  d
 187  d��Zdee
 188  eeeffdd�Zdeefdd�Zedefdd��Zdd�Zdd�Zdd�Zd'deefdd�Z			d(d ed!eed"eedeed#ef
 189  d$d%�Z�ZS))�TBToolsz2Basic tools used by all traceback printer classes.rrFN)�debugger_clscs`tt|�j||d�||_||_t�|_|�|�||_|p t	j
 190  |_|r+|��|_dSd|_dS�N)�parent�config)
 191  �superrb�__init__�call_pdb�_ostreamr
 192  �color_scheme_table�
 193  set_colors�
 194  old_schemerZPdbrc�pdb)�self�color_schemeri�ostreamrerfrc��	__class__rrrhHs	
 195  
 196  zTBTools.__init__cCs|jdurtjS|jS)asOutput stream that exceptions are written to.
 197  
 198          Valid values are:
 199  
 200          - None: the default, which means that IPython will dynamically resolve
 201            to sys.stdout.  This ensures compatibility with most tools, including
 202            Windows (where plain stdout doesn't recognize ANSI escapes).
 203  
 204          - Any object with 'write' and 'flush' attributes.
 205          N)rj�sys�stdout�rorrr�_get_ostreamlszTBTools._get_ostreamcCs*|dust|d�rt|d�sJ�||_dS)N�write�flush)�hasattrrj)ro�valrrr�_set_ostreamys 
 206  zTBTools._set_ostreamcCs0t|dd�}|r
 207  |St|dd�rdSt|dd�S)N�	__cause__�__suppress_context__F�__context__��getattr)Zexception_value�causerrr�_get_chained_exceptionszTBTools._get_chained_exceptionrcCs |�|�}|r|j||jfSdS�N)r�rs�
 208  __traceback__)ro�evalueZchained_evaluerrr�get_parts_of_chained_exception�s
 209  z&TBTools.get_parts_of_chained_exceptioncCs$d}d}|r|gg}|S|gg}|S)NzF
 210  The above exception was the direct cause of the following exception:
 211  zE
 212  During handling of the above exception, another exception occurred:
 213  r)ror�Zdirect_causeZexception_during_handling�messagerrr�!prepare_chained_exception_message�s�z)TBTools.prepare_chained_exception_messagecCs|jj��dkS)NZnocolor)rk�active_scheme_name�lowerrvrrrr5�szTBTools.has_colorscOsN|jj|i|��|jj|_t|d�r#|jdur%|jj|i|��dSdSdS)z;Shorthand access to the color table scheme selector method.rnN)rk�set_active_scheme�
 214  active_colorsrFrzrnrl)ro�args�kwrrrrl�s
 215  
 216  �zTBTools.set_colorscCsL|jjdkr|j�|j�|jj|_dS|jj|_|j�d�|jj|_dS)z=Toggle between the currently active color scheme and NoColor.rN)rkr�r�rmr�rFrvrrr�color_toggle�s
 217  zTBTools.color_togglecC�
 218  d�|�S)�4Convert a structured traceback (a list) to a string.r7��join�roZstbrrr�stb2text��
 219  zTBTools.stb2text��	tb_offsetcCs|�|||||�}|�|�S)zgReturn formatted traceback.
 220  
 221          Subclasses may override this if they add extra arguments.
 222          )�structured_tracebackr�)ro�etyper1�tbr��contextZtb_listrrr�text�s
 223  �
 224  zTBTools.textr�r��etb�number_of_lines_of_contextcCst��)zWReturn a list of traceback frames.
 225  
 226          Must be implemented by each class.
 227          )�NotImplementedError�ror�r�r�r�r�rrrr��szTBTools.structured_traceback)rFNNN�Nr��NNr�)r0�
 228  __module__�__qualname__�__doc__r�rhrwr|�propertyrq�staticmethodr�rr�type�
 229  BaseExceptionrr�rrr��boolr5rlr�r��intr�r��
 230  __classcell__rrrrrrbBsP��$
 231  
 232  
 233  �	
 234  
 235  ������rbc
 236  @sveZdZdZdd�Zdd�Z			dded	eed
 237  ee	dee
 238  fdd
 239  �Zdd�Zdd�Z
 240  dd�Zdd�Zdd�ZdS)�ListTBajPrint traceback information from a traceback list, with optional color.
 241  
 242      Calling requires 3 arguments: (etype, evalue, elist)
 243      as would be obtained by::
 244  
 245        etype, evalue, tb = sys.exc_info()
 246        if tb:
 247          elist = traceback.extract_tb(tb)
 248        else:
 249          elist = None
 250  
 251      It can thus be used by programs which need to process the traceback before
 252      printing (such as console replacements based on the code module from the
 253      standard library).
 254  
 255      Because they are meant to be called without a full traceback (only a
 256      list), instances of this class can't call the interactive pdb debugger.cCs0|j��|j�|�|||��|j�d�dS�Nr7)rqryrxr��ror�r1�elistrrr�__call__�s
 257  zListTB.__call__cCs|rt�|�SdSr�)�	traceback�
 258  extract_tb)ror�rrr�_extract_tb�s
 259  zListTB._extract_tbNr�r�r�r�r�cCsVt|t�r
 260  |\}}nt�}t|t�r|}n|dur|�|�}ng}|dur(|jn|}t|t�s1J�|j}g}	|r[|rFt|�|krF||d�}|	�	d|j
 261  |jfd�|	�|�
 262  |��d�|�||��}
 263  |	�	|
 264  �|dur�|�|�}|r�t|d�|vr�|dur�|�|j�dnd}|\}}}|�t|d��d}
 265  |�||||f|
 266  |�||	}	|	S)a�Return a color formatted string with the traceback info.
 267  
 268          Parameters
 269          ----------
 270          etype : exception type
 271              Type of the exception raised.
 272          evalue : object
 273              Data stored in the exception
 274          etb : list | TracebackType | None
 275              If list: List of frames, see class docstring for details.
 276              If Traceback: Traceback of the exception.
 277          tb_offset : int, optional
 278              Number of frames in the traceback to skip.  If not given, the
 279              instance evalue is used (set in constructor).
 280          context : int, optional
 281              Number of lines of context information to print.
 282  
 283          Returns
 284          -------
 285          String with formatted exception.
 286          Nz&Traceback %s(most recent call last)%s:r7�rr)�
 287  isinstance�tuple�set�listr�r�r�rFr*r=�normalEmr?�extend�_format_listr��_format_exception_onlyr��idr�r}�addr�)ror�r�r�r�r��chained_exc_idsr�rFZout_listr+�	exceptionZchained_exception_message�chained_exceptions_tb_offsetrrrr��s\
 288  
 289  
 290  
 291  ��
 292  
 293  ��
 294  ����zListTB.structured_tracebackcCs�|j}g}t|�D]`\}\}}}}|t|�dkr#|j|j|j|jfn|j|j|j	df\}	}
 295  }}t
 296  |||	|d�}
 297  |	�d|
 298  ��}|dkrP|d|
 299  �|�|	�d�7}n|d7}|rd||�d|���|	�d�7}|�|�q	|S)	aFormat a list of traceback entry tuples for printing.
 300  
 301          Given a list of tuples as returned by extract_tb() or
 302          extract_stack(), return a list of strings ready for printing.
 303          Each string in the resulting list corresponds to the item with the
 304          same index in the argument list.  Each string ends in a newline;
 305          the strings may contain internal newlines as well, for those items
 306          whose source text line is not None.
 307  
 308          Lifted almost verbatim from traceback.py
 309          rr�rVz  �<module>z in r7�    )
 310  rFrMr*r�ZnameEm�
 311  filenameEmrr?rYrra�stripr=)ro�extracted_listrF�output_list�indrrBrYrZ	normalColZnameColZfileColZlineCol�fns�itemrrrr�Gs 
 312  �
 313  �zListTB._format_listc
 314  
 315  Cs"d}|j}g}t�|j|j|j�}|dur|�|d�n�t|t�r�d}|j	s,d|_	|j
 316  r;|j
 317  }t�|j	|j
 318  �}nd}d}|�d|j
 319  t|j	|j|j
 320  |dkrQdn|d	�|jf�|dkret�|jd
 321  �}|dur�d}	|	t|�kr�||	��r�|	d7}	|	t|�kr�||	��sw|�d
 322  |j|��|jf�|jdur�d}
 323  ||	|jd�D]}|��r�|
 324  |7}
 325  q�|
 326  d7}
 327  q�|�d|j|
 328  |jf�z|j}
 329  Wnty�|�|�}
 330  Ynw|
 331  r�|�d||j|j|
 332  f�n|�d|�|�dd�t|dg�D��|�rt�}|du�r|j�|j	|j
 333  d�|S)aTFormat the exception part of a traceback.
 334  
 335          The arguments are the exception type and value such as given by
 336          sys.exc_info()[:2]. The return value is a list of strings, each ending
 337          in a newline.  Normally, the list contains a single string; however,
 338          for SyntaxError exceptions, it contains several lines that (when
 339          printed) display detailed information about where the syntax error
 340          occurred.  The message indicating which exception occurred is the
 341          always last string in the list.
 342  
 343          Also lifted nearly verbatim from traceback.py
 344          FNr7Tz<string>�unknownr�z	%s  %s%s
 345  rVzutf-8rrz%s    %s%s
 346  r�r/z%s%s^%s
 347  z%s%s:%s %s
 348  z%s
 349  css�|]}|�d�VqdS)r7Nr)r�xrrrr�s�z0ListTB._format_exception_only.<locals>.<genexpr>�	__notes__)rFrrN�excNamer0r?r=�
 350  issubclass�SyntaxErrorrrB�	linecache�getliner�rar�r�r*�isspacerr��offsetZcaret�msg�	Exception�	_some_strr�r�r
 351  �hooks�synchronize_with_editor)
 352  ror�r1Z
 353  have_filedatarFr��styperBZtextlinerRr"�cr^rrrr�ks|
 354  
 355  ����
 356  ��
 357  
 358  
 359  �
 360  ��
 361  zListTB._format_exception_onlycCst�|||�S)z�Only print the exception type and message, without a traceback.
 362  
 363          Parameters
 364          ----------
 365          etype : exception type
 366          value : exception value
 367          )r�r�)ror�r1rrr�get_exception_only�szListTB.get_exception_onlycCs2|j}|��|�d�|�||���|��dS)z�Only print the exception type and message, without a traceback.
 368  
 369          Parameters
 370          ----------
 371          etype : exception type
 372          evalue : exception value
 373          r7N)rqryrxr�r�)ror�r�rqrrr�show_exception_only�s
 374  zListTB.show_exception_onlycCs*zt�t|��WSdt|�jYS)Nz<unprintable %s object>)rrNrDr�r0)ror1rrrr��szListTB._some_strr�)r0r�r�r�r�r�r�rr�rr�r�r�r�r�r�r�rrrrr��s*
 375  ����
 376  �V$V
 377  r�c@s�eZdZUdZeeed<eeed<eeed<eeed<e	eed<e
 378  dd��Zd	d	d
 379  �deededeefdd�Ze
 380  d
 381  d��Ze
 382  dd��Ze
 383  dd��Zd	S)�	FrameInfozm
 384      Mirror of stack data's FrameInfo, but so that we can bypass highlighting on
 385      really long frames.
 386      �descriptionrrBr��	raw_linesc	Cs>|t|dd�t|dd�t|dd�t|dd�t|dd�|dd�S)Nr�rrBr$�code��sdr�r�)�cls�
 387  frame_inforrr�_from_stack_data_FrameInfo�s
 388  
 389  
 390  
 391  
 392  �z$FrameInfo._from_stack_data_FrameInfoNr�cCsj||_||_||_||_||_||_||_|dur3zt�|�d|_	WdSt
 393  y2dg|_	YdSwdS)NrzG'Could not get source, probably due dynamically evaluated source code.')r�rrBr$r��_sdr�r(r)r��OSError)ror�rrBr$r�r�r�rrrrh�s���zFrameInfo.__init__cCs|jr|jjSgSr�)r��variables_in_executing_piecervrrrr��z&FrameInfo.variables_in_executing_piececCs@ddlm}z|jjWS|yGdd�d�}|�gYSw)Nr)�NotOneValueFoundc@seZdZdZdZdd�ZdS)zFrameInfo.lines.<locals>.DummyrFcS�dS)NzH<Error retrieving source code with stack_data see ipython/ipython#13598>r)ror6rrrr@+�z%FrameInfo.lines.<locals>.Dummy.renderN)r0r�r�rBrCr@rrrr�Dummy'sr�)Zexecuting.executingr�r�r+)ror�r�rrrr+s
 394  �zFrameInfo.linescCs|jr|jjSdSr�)r��	executingrvrrrr�0r�zFrameInfo.executing)r0r�r�r�rrD�__annotations__rr�r�classmethodr�rhr�r�r+r�rrrrr��s0
 395  
 396  ���
 397  �
 398  
 399  r�c@seZdZdZdZdZ										d*d	ed
 400  ededed
 401  ef
 402  dd�Z	de
 403  fdd�Zd+dedefdd�Zdd�Z
 404  dedeedeedeefdd�Zdededefdd�Z			 d,dedeedeedeedef
 405  d!d"�Zd+d#efd$d%�Zd-d&d'�Zd.d(d)�ZdS)/�	VerboseTBa;A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
 406      of HTML.  Requires inspect and pydoc.  Crazy, man.
 407  
 408      Modified version which optionally strips the topmost entries from the
 409      traceback, to be used with alternate interpreters (because their own code
 410      would appear in the traceback).z
 411  bg:ansiyellow�default�LinuxFNrTrprir��long_header�include_varsc	CsHtj|||||	|
 412  |d�||_||_||_|durtj}||_d|_dS)aWSpecify traceback offset, headers and color scheme.
 413  
 414          Define how many frames to drop from the tracebacks. Calling it with
 415          tb_offset=1 allows use of this handler in interpreters which will have
 416          their own code at the top of the traceback (VerboseTB will first
 417          remove that frame before printing the traceback info).)rprirqrerfrcNT)	rbrhr�r�r�r��
 418  checkcache�check_cache�skip_hidden)rorprirqr�r�r�r�rcrerfrrrrhDs �	
 419  zVerboseTB.__init__r�c Cs�t|t�sJ�|j}|j}t|jtj�rd|j|j|fSdt	}d|j
 420  ||f}d|j�d|j
 421  �d|��}d|j|j
 422  |f}d|j
 423  |f}t|j
 424  |j||jd	�}	t�|j�\}
 425  }}}
 426  |jd
 427  urf|j��}nd}|dkrod
 428  }n(|jrttnt}ztj|
 429  |||
 430  |d�}|j||d�}Wn
 431  ty�||}Ynwd
 432  }g}|jr�z|j}|D]}|�||jt|j�f�q�Wnt y�|�d�Ynw|r�d||�!|�f}|	�|r�dnd
 433  �|�d�}|jd
 434  u�rEd|j|f}|t"�#|j
 435  �}	d|	|f}t$j%|j&j'|d�j(}|j)j*}|jd}|j+}||}||j,k�r(t-||j,d�}||j,}|j,}nd}||j,}|||�}d|d
 436  �!t.||||||��fS|d
 437  �!t/|j0||j1|��7}|S)zFormat a single stack framez*    %s[... skipping similar frames: %s]%s
 438  r/z%s
 439  %s%szin z{file}z{scope}z.in %s%%s%s(***failed resolving arguments***)%sz
 440  %%s %s= %%s%srVN�?r�r�)�formatvalue)r!�scopez<Exception trying to inspect frame. No more locals available.r8z, r7z%s%%s%sz%s %s
 441  )�stylerer)2r�r�rFr?r�r<�RepeatedFramesr�r�r;ZvalEmZvNamerarr�rBr(�getargvaluesr$r�Z
 442  code_qualnamer��eqrepr�nullrepr�formatargvaluesr]�KeyErrorr�r=rY�reprr1r�r�r[r\r�Parserrkr�Zformat2r��co_firstlinenor�r��maxrUrLr+r5) ror�rF�ColorsNormal�indentZ	em_normalZtpl_callZ
 443  tpl_call_failZtpl_name_val�linkr��varargs�varkwZlocals_r3�callZvar_reprr�rGZ
 444  lvals_listZfibp�var�resultr`�levelrQ�
 445  first_line�current_liner�rP�start�stoprrr�
 446  format_recordls�
 447  ���
 448  
 449  �������
 450  
 451  
 452  �����zVerboseTB.format_recordr��long_versionc
 453  
 454  Cs�|j}|j}d|j||f}tdt�d�}|rNdtj��ddtj}t	�
 455  t	�	��}d|jd|||d|t|�t|�||�
 456  |�f}	|	d	7}	|	Sd
 457  |d�
 458  |t|��f}	|	S)N�%s%s%s�KrzPython z: z%s%s%s
 459  %s%s%s
 460  %s�-r/z�
 461  A problem occurred executing Python code.  Here is the sequence of function
 462  calls leading up to the error, with the most recent (innermost) call last.r8z!Traceback (most recent call last))rFr?r��minrrt�version�split�
 463  executable�time�ctime�topliner*�rjust)
 464  ror�r�colors�colorsnormal�exc�width�pyver�date�headrrr�prepare_header�s2�	���zVerboseTB.prepare_headercs�|j}|j�ztt||f�\}}Wntt��dd�}}tt||f�\}}Yt|dg�}t|t�r<t|tt	f�rDt
 465  |dtd�g}d�|j
 466  |�t�|��g�fdd�|D��S)N�r�)r3z
 467  {}{}{}: {}c3s(�|]}d��tt�|�d��VqdS)z{}{}ZnoteN)r]r4rrN)r�n�r&rrrs���
 468  �z-VerboseTB.format_exception.<locals>.<genexpr>)rFr?�maprDrt�exc_infor�r�r�bytesr4rr]r�rrN)ror�r�r%Z	etype_strZ
 469  evalue_str�notesrr/r�format_exceptions*��
 470  ��zVerboseTB.format_exceptionr�r�cCsp|}z|j}Wn	tyYnw|dur|jn|}t|t�s!J�|�t|�|j�}|r3|�|||�ng}g}	d}
 471  t	|�d}t
 472  |�D]<\}}
 473  t|
 474  jtj
 475  �sb|jrb|
 476  jj�dd�rb||krb|
 477  d7}
 478  qC|
 479  rw|j}|j}|	�d|j|
 480  |f�d}
 481  |	�|�|
 482  ��qC|
 483  r�|j}|j}|	�d|j|
 484  |f�|�||�}|r�|d}t�}|dur�|j�|j|jd�|g|	|gS)z�Formats the header, traceback and exception message for a single exception.
 485  
 486          This may be called multiple times by Python 3 exception chaining
 487          (PEP 3134).
 488          NrrZ__tracebackhide__z'    %s[... skipping hidden %s frame]%s
 489  �����)r0�AttributeErrorr�r�r�r,rDr��get_recordsr*rMr�r<rr�r$�f_locals�getrFr?r=r�rr4r
 490  r�r�rrB)ror�r�r�r�r�Z
 491  orig_etyper+�records�framesZskippedZ
 492  lastrecordrR�recordrFr�formatted_exceptionr�r^rrr�format_exception_as_a_whole%sb
 493  �����
 494  ��
 495  ��z%VerboseTB.format_exception_as_a_wholer�cCsz|dusJ�|d}|d}||}|jr't|j�}t�||j�}t|d�}nd}tj|||d�}	|}
 496  d}g}|
 497  durz$t�	|
 498  j
 499  �}
 500  |
 501  durY|
 502  j}|�d�^}}|dkrY|
 503  j
 504  }
 505  Wq7t|
 506  j
 507  �}Wntyjd}Ynwt||�}|�|
 508  �t|
 509  dd�}
 510  |
 511  dus;|tkr�g}|D]}|j
 512  }|jf}|j}|j}|�td	|||||d
 513  ��q�|Sttjj||	d��|d�}dd
 514  �|D�}|S)Nrr-)r)�before�afterZpygments_formatterr�.�IPython�tb_nextz	Raw frame)r�)�optionscSsg|]}t�|��qSr)r�r�)rrrrr�
 515  <listcomp>�sz)VerboseTB.get_records.<locals>.<listcomp>)r5r	�tb_highlight_styler<Zstyle_with_executing_node�tb_highlightr�Optionsr(�	getmodule�tb_framer0rrCr-r�r
 516  r=r��FAST_THRESHOLD�f_linenor%r&r�r�)ror�r�r�r�r@r?r�	formatterrD�cf�max_lenZtbs�mod�mod_nameZ	root_name�_ZFIsr�r$rBr�rrIrrrr7hsd
 517  ��
 518  
 519  ���zVerboseTB.get_recordsr�cCs|�|||||�}|j}|j}d|jdtdt�d�|f}	|	g}
 520  d}d}|}
 521  |�|�}|rC|dus5J�|
 522  |�|j�7}
 523  |\}}}nd}t	�}|r|
 524  |�|||||�7}
 525  |�|�}|r{t
 526  |d�|vr{|�t
 527  |d��|
 528  |�|j�7}
 529  |\}}}nd}|sJt|
 530  �D]}|
 531  |7}
 532  q�|
 533  S)z5Return a nice text document describing the traceback.rrrr�Nr)
 534  r>rFr?r#rrr�r�r}r�r�r��reversed)ror�r�r�r�r�r=r%r&r+Zstructured_traceback_partsr�Zlines_of_contextZformatted_exceptionsr�r�rrrr��s>	� 
 535  �
 536  �
 537  zVerboseTB.structured_traceback�forcecCs6|s|jr�|jdur|��|_ttjd�}|�q|j��t|d�r+|jdur+|j}ntj	}|_|jdurT|jj
 538  durT|jj
 539  dusDJ�|jj
 540  |_|jdurT|jj
 541  dus<|r\|j
 542  r\|j
 543  }|j|j_tj
 544  dkritjnttdtj�}|rz|j�d|�n|j�d|�Wd�n1s�wYt|d�r�|`dSdS)a4Call up the pdb debugger if desired, always clean up the tb
 545          reference.
 546  
 547          Keywords:
 548  
 549            - force(False): by default, this routine checks the instance call_pdb
 550              flag and does not actually invoke the debugger if the flag is false.
 551              The 'force' option forces the debugger to activate even if the flag
 552              is false.
 553  
 554          If the call_pdb flag is set, the pdb interactive debugger is
 555          invoked. In all cases, the self.tb reference to the current traceback
 556          is deleted to prevent lingering references which hamper memory
 557          management.
 558  
 559          Note that each call to pdb() does an 'import readline', so if your app
 560          requires a special setup for the readline completers, you'll have to
 561          fix that by hand after invoking the exception handler.N)�hookr�)rS��last_exc)rirnrcrrt�__displayhook__�resetrzr��last_tracebackrCrJZbotframe�version_info�
 562  last_valuer�Zinteraction)rorUZdisplay_trapr�r'rrrr�s2
 563  
 564  
 565  
 566  
 567  �
 568  
 569  ��
 570  �zVerboseTB.debuggercCsP|pt��\}}}||_|j}|��|�|�|||��|�d�|��dSr�)rtr1r�rqryrxr�)ro�infor�r�r�rqrrr�handlers
 571  zVerboseTB.handlercCsN|dur	|��n|�|||f�z|��WdSty&td�YdSw)z@This hook can replace sys.excepthook (for Python 2.1 or higher).N�
 572  KeyboardInterrupt)r_r�KeyboardInterruptr')ror�r�r�rrrr�
 573  s
 574  �zVerboseTB.__call__)
 575  r�FNrFTNNNN)Fr�r�)NNN)r0r�r�r�rGrFrDr�r�rhr�rr,r4r�rr�rr>r7r�rr_r�rrrrr�9sz�����
 576  �(y  ���
 577  �C��
 578  �>�����
 579  �.
 580  2r�c@steZdZUdZeed<					ddd	�Zddd�Zd
 581  d�Zdde	efdd�Z
 582  dd�Zdd�Zdd�Z
 583  dd�ZdS)�FormattedTBa�Subclass ListTB but allow calling with a traceback.
 584  
 585      It can thus be used as a sys.excepthook for Python > 2.1.
 586  
 587      Also adds 'Context' and 'Verbose' modes, not available in ListTB.
 588  
 589      Allows a tb_offset to be specified. This is useful for situations where
 590      one needs to remove a number of topmost frames from the traceback (such as
 591      occurs with python programs that themselves execute other python code,
 592      like Python shells).  �mode�Plainr�FNrc
 593  CsZgd�|_|jdd�|_tj|||||||||	|
 594  |d�tddddd�|_|�|�dS)N)rd�Context�Verbose�MinimalrrS)
 595  rprirqr�r�r�r�rcrerfr�r7)�valid_modes�
 596  verbose_modesr�rh�dict�_join_chars�set_mode)rorcrprirqr�r�r�r�rcrerfrrrrh(s
 597  
 598  ��zFormattedTB.__init__r�cCsh|dur|jn|}|j}||jvrt�||||||�S|dkr&t�|||�S|��t�||||||�S)Nrg)r�rcrir�r�r�r�r�)ror�r1r�r�r�rcrrrr�?s
 599  ��z FormattedTB.structured_tracebackcCs|j�|�S)r�)�tb_join_charr�r�rrrr�RszFormattedTB.stb2textcCs�|s|j�|j�dt|j�}|j||_n||jvr)td|dt|j���t|t�s0J�||_|j|jdk|_|j|j|_	dS)zbSwitch to the desired mode.
 600  
 601          If mode is not specified, cycles through the available modes.rz#Unrecognized mode in FormattedTB: <z>
 602  Valid modes: r-N)
 603  rhrPrcr*�
 604  ValueErrorrDr�r�rkrm)rorcZnew_idxrrrrlVs�
 605  
 606  ��zFormattedTB.set_modecC�|�|jd�dS�Nr�rlrhrvrrr�plainm�zFormattedTB.plaincCro)Nrrqrvrrrr�prszFormattedTB.contextcCro)Nr-rqrvrrr�verbosesrszFormattedTB.verbosecCro)NrSrqrvrrr�minimalvrszFormattedTB.minimal)rdr�FNrFFNNNNr�r�)r0r�r�r�rDr�rhr�r�rrlrrr�rtrurrrrrbs 
 607  
 608  �
 609  rbc@sPeZdZdZ		d
 610  dd�Z			ddedeedeed	ee	d
 611  e	f
 612  dd�Z
 613  dS)�AutoFormattedTBaGA traceback printer which can be called on the fly.
 614  
 615      It will find out about exceptions by itself.
 616  
 617      A brief example::
 618  
 619          AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
 620          try:
 621            ...
 622          except:
 623            AutoTB()  # or AutoTB(out=logfile) where logfile is an open file object
 624      NcCsj|dur|j}|��|�|�||||��|�d�|��z|��WdSty4td�YdSw)aIPrint out a formatted exception traceback.
 625  
 626          Optional arguments:
 627            - out: an open file-like object to direct output to.
 628  
 629            - tb_offset: the number of frames to skip over in the stack, on a
 630            per-call basis (this overrides temporarily the instance's tb_offset
 631            given at initialization time.Nr7r`)rqryrxr�rrar')ror�r�r��outr�rrrr��s
 632  �zAutoFormattedTB.__call__r�r�r�r�r�r�cCsF|durt��\}}}t|t�r|d|_n||_t�||||||�Srp)rtr1r�r�r�rbr�r�rrrr��s	
 633  �z$AutoFormattedTB.structured_traceback)NNNNNr�)r0r�r�r�r�r�rr�rr�r�rrrrrv{s&
 634  
 635  �������rvc@seZdZdZddd�ZdS)�ColorTBz;Shorthand to initialize a FormattedTB in Linux colors mode.r�rcKstj|f||d�|��dS)N)rpri)rbrh)rorpri�kwargsrrrrh�s
 636  
 637  �
 638  �zColorTB.__init__N)r�r)r0r�r�r�rhrrrrrx�srxcsHeZdZdZddd�Zdd�Z		d�fd	d
 639  �	Zdd�Zd
 640  d�Z�Z	S)�SyntaxTBz:Extension which holds some state: the last exception valuerNcCstj||||d�d|_dSrd)r�rh�last_syntax_error)rorprerfrrrrh�s
 641  zSyntaxTB.__init__cCs||_t�||||�dSr�)r{r�r�r�rrrr��szSyntaxTB.__call__r�csht|t�r$t|jt�r$t|jt�r$t�|j�t�|j|j�}|r$||_	||_
 642  tt|�j
 643  |||||d�S)N)r�r�)r�r�rrDrBr�r�r�r�r�r{rgrzr�)ror�r1r�r�r�Znewtextrrrrr��s
 644  
 645  �
 646  ��zSyntaxTB.structured_tracebackcCs|j}d|_|S)z+Return the current error state and clear itN)r{)ro�errr�clear_err_state�szSyntaxTB.clear_err_statecCr�)r�r�r�r�rrrr��r�zSyntaxTB.stb2text)rNNr�)
 647  r0r�r�r�rhr�r�r}r�r�rrrrrrz�s
 648  �rzcCs�ztj�|�WSty�zt|�WYSty!�z&t|dd�}|r6t|�WYYSt|dd�}|rIdt|�WYYSWntyR�YYYdSYYdS)z(Hopefully pretty robust repr equivalent.r0Nrsz%s instancezUNRECOVERABLE REPR FAILURE)�pydocr�rrar��	text_repr)r1rY�klassrrrr�s.�
 649  �rcCsd||�S)Nz=%sr�r1rrrrrsrcCr�)Nr�rr�rrrr
 650  r�r)Br��collections.abcr�	functoolsr(r�r~rtr!r��typesr�typingrrrrr<Zpygments.formatters.terminal256rZpygments.stylesr	ZIPython.utils.colorable�utilsZ	colorablerBr
 651  ZIPython.corerZIPython.core.display_traprZIPython.core.excolorsr
 652  Z
 653  IPython.utilsrrr[rZIPython.utils.terminalrr;ZDEFAULT_SCHEMErK�	lru_cacherDr�r#�	FrameTyper-r4r�rLrUraZ	Colorablerbr�r�r�rbrvrxrzrrrrrrr�<module>sfZ&2'Wda>(