← Back
Editing: ultratb.cpython-311.pyc
� �@�c�� � �� � d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl m Z mZmZm Z ddlZddlmZ ddlmZ ddlmZ ddlmZ dd lmZ dd lmZ ddlmZ ddlmZ dd lm Z ddl!m"c m#Z# dZ$dZ%de&fd�Z'dd�d�Z( G d� de#j) � � Z* G d� de*� � Z+ G d� de*� � Z, G d� de,e+� � Z- G d� de-� � Z. G d� de-� � Z/ G d � d!e+� � Z0d"� Z1e1fd#�Z2e1fd$�Z3dS )%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)�Tuple�List�Any�Optional)�Terminal256Formatter)�get_style_by_name)�get_ipython)�debugger)�DisplayTrap)�exception_colors)�path)� py3compat)�get_terminal_size� �NoColor� has_colorsc �H � t dz }g }| D �]}|t j u r&|� |j �d|j �d�� � �7|� |�� � � d� � dz }|j }|j rL|t t |� � � � z } t j | � � �t |� � ��} |j }nd||fz } |j }|�| �|j �d|��}|� |� � |r|j r|� |dz � � ��|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. � z (...)� )� pygmentedz%*s� )�INDENT_SIZE� stack_data�LINE_GAP�append�linenoEm�Normal�render�rstrip�lineno� is_current�len�strr � make_arrow)�lines�Colorsr �lvals� numbers_width�res� stack_line�liner! �pad�num�start_colors �6/usr/lib/python3/dist-packages/IPython/core/ultratb.py�_format_traceback_linesr1 � sB � � �!�O�M� �C�� %� %� ���,�,�,��J�J�6�?�?�?�F�M�M�M�J�K�K�K��� � �:� �6�6�=�=�d�C�C�d�J���"��� � (��#�c�&�k�k�"2�"2�2�C�$�/��4�4�4�c�&�k�k�k�B�C� �/�K�K��=�&�1�1�C� �-�K�)�k�3�3�� � � �t�t�D��� � �4����� %�Z�*� %��J�J�u�t�|�$�$�$���J� �r! c �F � t � � }|�7| |j j v r)d|j j | z } |� d|� d|� �}nMd|� d|� �}nDt j t j | t j � � � � } |� d|� d|� �}nd|� d|� �}|� | |� � � S ) a Format filename lines with `In [n]` if it's the nth code cell or `File *.py` if it's a module. Parameters ---------- file : str ColorFilename ColorScheme's filename coloring to be used. ColorNormal ColorScheme's normal coloring to be used. Nz[%s]zCell z In {file}zIn {file}, line {lineno}zFile �{file}z{file}:{lineno})�filer! ) r �compile� _filename_map� util_path� compress_userr �cast_unicode�fs_encoding�format)r6 � ColorFilename�ColorNormalr! �ipinst�tpl_links r0 �_format_filenamerB � s� � � �]�]�F� ��d�f�n�&B�B�B����4�T�:�:���>�F�}�F�F��F�F�H�H�W�}�W�W�+�W�W�H�H��&��"�4��)>�?�?� � �� �>�C�}�C�C�k�C�C�H�H�N�}�N�N��N�N�H��?�?��V�?�4�4�4r2 c � � � e Zd ZdZdZ ddd�� fd�Zd� Zd � Z eee� � Z e d � � � Zdee eeef fd�Zdee fd �Zedefd�� � Zd� Zd� Zd� Zddee fd�Z ddee fd�Z� xZS )�TBToolsz2Basic tools used by all traceback printer classes.r r FN)�debugger_clsc �D �� t t | � � � ||�� � || _ || _ t � � | _ | � |� � || _ |pt j | _ |r| � � � | _ d S d | _ d S �N)�parent�config) �superrD �__init__�call_pdb�_ostreamr �color_scheme_table� set_colors� old_schemer �PdbrE �pdb)�self�color_schemerL �ostreamrH rI rE � __class__s �r0 rK zTBTools.__init__� s� �� � �g�t���%�%�V�F�%�C�C�C� �� � �� � #3�"4�"4�������%�%�%�&���(�8�H�L���� ��(�(�*�*�D�H�H�H��D�H�H�Hr2 c �6 � | j �t j n| j S )as Output stream that exceptions are written to. Valid values are: - None: the default, which means that IPython will dynamically resolve to sys.stdout. This ensures compatibility with most tools, including Windows (where plain stdout doesn't recognize ANSI escapes). - Any object with 'write' and 'flush' attributes. )rM �sys�stdout�rS s r0 �_get_ostreamzTBTools._get_ostream� s � � "�]�2�s�z�z�� �Er2 c �\ � |�"t |d� � rt |d� � sJ �|| _ d S )N�write�flush)�hasattrrM )rS �vals r0 �_set_ostreamzTBTools._set_ostream s2 � ��{�w�s�G�4�4�{���g�9N�9N�{�{�{��� � � r2 c �t � t | dd � � }|r|S t | dd� � rd S t | dd � � S )N� __cause__�__suppress_context__F�__context__)�getattr)�exception_value�causes r0 �_get_chained_exceptionzTBTools._get_chained_exception sL � ����d�;�;��� ��L��?�$:�E�B�B� ��4��� �t�<�<�<r2 �returnc �R � | � |� � }|r|j ||j fS d S �N)ri rV � __traceback__)rS �evalue�chained_evalues r0 �get_parts_of_chained_exceptionz&TBTools.get_parts_of_chained_exception s8 � � �4�4�V�<�<��� Z�!�+�^�^�=Y�Y�Y��tr2 c �$ � d}d}|r|gg}n|gg}|S )NzF The above exception was the direct cause of the following exception: zE During handling of the above exception, another exception occurred: � )rS rh �direct_cause�exception_during_handling�messages r0 �!prepare_chained_exception_messagez)TBTools.prepare_chained_exception_message s1 � �a��$m�!�� 4�$�~�&�G�G�1�2�3�G��r2 c �F � | j j � � � dk S )N�nocolor)rN �active_scheme_name�lowerrZ s r0 r zTBTools.has_colors) s � ��&�9�?�?�A�A�Y�N�Nr2 c � � | j j |i |�� | j j | _ t | d� � r| j � | j j |i |�� dS dS dS )z;Shorthand access to the color table scheme selector method.rR N)rN �set_active_scheme� active_colorsr'