o 7]hOe@sddlmZddlZddlZddlZddlmZmZddlm Z ddl m Z m Z ddl mZmZmZmZmZmZmZmZmZddlmZddlmZmZmZmZmZmZdd lm Z!dd lm"Z"d d l#m$Z$d d l%m&Z&m'Z'd dl(m)Z)d dl*m+Z+m,Z,m-Z-m.Z.m/Z/d dl0m1Z1d dl2m3Z3m4Z4d dl5m6Z6d dl7m8Z8d dl9m:Z:d dl;mm?Z?e@dkZAdZBdZCdddddddddd d ee+d!eeDd"eDd#eeEd$eFd%eFd&eFd'eeeEe fd(eDd)eeeGeGee geffd*d+ZHeGd,d-d-ZIeGd.d/d/ZJeGd0d1d1ZKeGd2d3d3ZLGd4d5d5e3ZMGd6d7d7ZNeOd8krZd d9l*m+Z+e+Z*ddlZd:ed)dfd;d<ZPd:ed)dfd=d>ZQdAd?d@ZReRdSdS)B)absolute_importN) dataclassfield)walk_tb) ModuleType TracebackType) AnyCallableDictIterableListOptionalSequenceTypeUnion)guess_lexer_for_filename)CommentKeywordNameNumberOperatorString)Text)Token)pretty) loop_first loop_last)Columns)ConsoleConsoleOptionsConsoleRenderable RenderResultgroup) Constrain)RegexHighlighterReprHighlighter)Panel) render_scope)Style)Syntax)ThemeWindows PdFT) consolewidth extra_linestheme word_wrap show_locals indent_guidessuppress max_framesr2r3r4r5r6r7r8r9r:returnc s|dur ttjdn|dttdtdttddf f dd d tddffd d } z t} | | tj WSt yNtj } t_ | YSw) a8Install a rich traceback handler. Once installed, any tracebacks will be printed with syntax highlighting and rich formatting. Args: console (Optional[Console], optional): Console to write exception to. Default uses internal Console instance. width (Optional[int], optional): Width (in characters) of traceback. Defaults to 100. extra_lines (int, optional): Extra lines of code. Defaults to 3. theme (Optional[str], optional): Pygments theme to use in traceback. Defaults to ``None`` which will pick a theme appropriate for the platform. word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False. show_locals (bool, optional): Enable display of local variables. Defaults to False. indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True. suppress (Sequence[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback. Returns: Callable: The previous exception handler that was replaced. N)filetype_value tracebackr;cs*tj|||d dS)N)r3r4r5r6r7r8r9r:)print Tracebackfrom_exception)r=r>r?) r4r8r:r7r9r5traceback_consoler3r6r1.excepthookipc slijdtdtddffdd }dddtdtdtddffd d _|_fd d _dS) Nargskwargsr;cs||i|dS)zEwrap the default ip.showtraceback to store info for ip._showtracebackNr1rGrH)default_showtracebacktb_datar1rDipy_show_traceback^szCinstall..ipy_excepthook_closure..ipy_show_tracebackF) is_syntaxrMcst}|rdn|d}dd}d|rdnd}t|D] }|dur(n|j}q |d|d|idS)z2Internally called traceback from ip._showtracebackNrunning_compiled_codeF tb_offsetrr) _get_exc_infogetrangetb_next)rMrGrH exc_tupletbcompiledrP_)rErFrKr1rDipy_display_tracebackds  zFinstall..ipy_excepthook_closure..ipy_display_tracebackcs|ddi|S)NrMTr1rI)rYr1rDsz9install..ipy_excepthook_closure..) showtracebackrbool_showtracebackshowsyntaxerror)rFrL)rE)rJrFrYrKrDipy_excepthook_closureZs z'install..ipy_excepthook_closure) rsysstderrr BaseExceptionr rr get_ipythonrE Exception) r2r3r4r5r6r7r8r9r:r_rFold_excepthookr1) rEr4r8r:r7r9r5rCr3r6rDinstall#s( ) rfc@sLeZdZUeed<eed<eed<dZeed<dZee ee j fed<dS)FramefilenamelinenonamelineNlocals) __name__ __module__ __qualname__str__annotations__intrlrmr r rNoder1r1r1rDrgs  rgc@s6eZdZUeed<eed<eed<eed<eed<dS) _SyntaxErroroffsetrhrlrimsgN)rnrorprsrrrqr1r1r1rDrus  ruc@sPeZdZUeed<eed<dZeeed<dZe ed<e e dZ e eed<dS) Stackexc_type exc_valueN syntax_errorFis_cause)default_factoryframes)rnrorprqrrr{r rur|r\rlistr~r rgr1r1r1rDrxs  rxc@seZdZUeeed<dS)TracestacksN)rnrorpr rxrrr1r1r1rDrs rc@seZdZdgZdS)PathHighlighterz(?P.*/)(?P.+)N)rnrorp highlightsr1r1r1rDrs rc@seZdZdZddddddZddd dd d d eed df d eedee de dee de de de de de de e e efde fddZedd dd d d eed df deededeedee de dee de de de de de de e e efde ddfddZed eefdeededeede de de defd d!Zd"ed#edefd$d%Zed&edefd'd(Zed)e d*e de fd+d,Zed-edefd.d/Z dS)0rAaA Console renderable that renders a traceback. Args: trace (Trace, optional): A `Trace` object produced from `extract`. Defaults to None, which uses the last exception. width (Optional[int], optional): Number of characters used to traceback. Defaults to 100. extra_lines (int, optional): Additional lines of code to render. Defaults to 3. theme (str, optional): Override pygments theme used in traceback. word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False. show_locals (bool, optional): Enable display of local variables. Defaults to False. indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True. locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to 10. locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80. suppress (Sequence[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback. max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100. textpythoncythonpyrex)rkz.pyz.pxdz.pyxz.pxiNr/r0FTr1tracer3r4r5r6r7r8locals_max_lengthlocals_max_stringr9r:c Cs|dur$t\} } }| dus| dus|durtd|j| | ||d}||_||_||_t|p2d|_ ||_ ||_ ||_ ||_ | |_g|_| D]-}t|tsd|jdus\J|dtj|j}n|}tjtj|}|j|qI| dkrtd| |_dSd|_dS)Nz9Value for 'trace' required if not called in except: blockr7 ansi_darkz+ must be a module with '__file__' attributer)r`exc_info ValueErrorextractrr3r4r* get_themer5r6r7r8rrr9 isinstancerq__file__ospathdirnamenormpathabspathappendmaxr:)selfrr3r4r5r6r7r8rrr9r:ryrzr?suppress_entityrr1r1rD__init__s:  "zTraceback.__init__ryrzr?r;c Cs0|j||||d}|||||||| | | | | d S)aCreate a traceback from exception info Args: exc_type (Type[BaseException]): Exception type. exc_value (BaseException): Exception value. traceback (TracebackType): Python Traceback object. width (Optional[int], optional): Number of characters used to traceback. Defaults to 100. extra_lines (int, optional): Additional lines of code to render. Defaults to 3. theme (str, optional): Override pygments theme used in traceback. word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False. show_locals (bool, optional): Enable display of local variables. Defaults to False. indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True. locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to 10. locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80. suppress (Iterable[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback. max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100. Returns: Traceback: A Traceback instance that may be printed. r) r3r4r5r6r7r8rrr9r:)r)clsryrzr?r3r4r5r6r7r8rrr9r:rich_tracebackr1r1rDrBs &zTraceback.from_exceptioncsg}d}ddlm} dtdtfdd} t| |j| ||d } t|tr)rqrd)rr1r1rDsafe_strRs   z#Traceback.extract..safe_strT)ryrzr|?rk)rvrhrirlrwrrr1rD usz%Traceback.extract..N)rhrirjrm_rich_traceback_guard __cause____suppress_context__)r)"pip._vendor.richrrrqrxrnr SyntaxErrorrurvrhrirrwr{rr~rf_code co_filename startswithrrisabsjoinrgco_namef_localsitemsgetattr __traceback__ __class__ __context__r)rryrzr?r7rrrr|rrstackr frame_summaryline_norhframecauserr1rrDr4s~            zTraceback.extractr2optionsc csR|j}|}|j}tid|td|td|td|tjd|t d|t t ddd |td |tt dd d |t d |t j d|t j d|t j d|tjd|td|td|tj t dddd}t}tt|jjD]\}} | jrt|| d|dddd} t| |j} || | VWdn1swY| jdur||tt|| j|ddd|jd|jVWdn1swYt| jddf|| jj Vn| j!rt| jddf|| j!Vn t| jdfV|s&| j"r t#dVqt#dVqdS) Nr pygments.textpygments.stringpygments.functionpygments.numberz repr.indentT)dimzrepr.strz repr.brace)boldz repr.numberzrepr.bool_truezrepr.bool_falsez repr.nonez scope.borderz scope.equalsz scope.keyzscope.key.specialF)inheritz7[traceback.title]Traceback [dim](most recent call last)ztraceback.border)rr)titlestyle border_styleexpandpaddingztraceback.border.syntax_error)rrrrr3z: ztraceback.exc_typezI [i]The above exception was the direct cause of the following exception: zH [i]During handling of the above exception, another exception occurred: )$r5get_background_styleget_style_for_tokenr+ TextTokenrrrFunctionrrr)rConstant Delimiterrr&rreversedrrr~r' _render_stackr$r3 use_themer{_render_syntax_errorrassembleryrwrzr| from_markup) rr2rr5background_style token_styletraceback_theme highlighterlastrstack_renderabler1r1rD__rich_console__s             zTraceback.__rich_console__r{ccst}t}|jdkr%tjd|jdfdt|jdfdd}||V||j}d|_ t |j d t |}| d |||tjd d|d dd7}|VdS) Nz r:rrrrTrzbold underline u[traceback.offset]▲[/])r&rrhrrrqrirlrstripno_wrapminrvlenstylizer)rr{rpath_highlighterrsyntax_error_textrvr1r1rDrs(    zTraceback._render_syntax_errorrhcodecCsltj|d}|s(|d}|dkr|d|n|}|dr(d|vr(dS|j|p3t||j }|S)Nrz#!r) rrsplitextindexrlowerLEXERSrRrrj)rrhrextnew_line_index first_line lexer_namer1r1rD _guess_lexers zTraceback._guess_lexerrc #s t}j}idtdtffdd }dtdttffdd }d}jdkr8tjd t|j jd }d }t |j D]\}} |rL||vrLd }q?|rd|dusTJt d t|d dddVd }|dk} | j t fddjD} t j|t | j dddt| jdfd| jdfdd} | j ds| sdV| V| j dr|| EdHq?| s z-|| j } | j | }t| ||d | jj| jjf| jhjdjd d }dVWnty}zt d|dfVWYd}~q?d}~ww| jr t|g|| dd n|Vq?dS)!Nrhr;csV|}|dur)t|dddd }|}Wdn1s wY||<|S)zRead files, and cache results on filename. Args: filename (str): Filename to read Returns: str: Contents of file Nrtzutf-8replace)encodingerrors)rRopenread)rhr code_file) code_cacher1rD read_codes  z*Traceback._render_stack..read_coderc3s,|jrt|jdjjjdVdSdS)Nrm)rr8rr)rmr(r8rr)r)rr1rD render_locals+s z.Traceback._render_stack..render_localsrrNFTz ... z frames hidden ...centerztraceback.error)justifyrrc3s|]}|VqdS)N)r)rr)frame_filenamer1rD Nsz*Traceback._render_stack..rrrrz in rrrrkX)r5 line_numbers line_rangehighlight_linesr6 code_widthr8dedentr)r)rr5rqrgr r!r:rSrr~ enumeraterrhanyr9rrirjrrr*r4r6r8rdrmr)rrrr5rrexclude_framesexcluded frame_indexrfirst suppressedrrrsyntaxerrorr1)rrrrDrs           zTraceback._render_stack)!rnrorp__doc__rLOCALS_MAX_LENGTHLOCALS_MAX_STRINGr rrsrqr\r rrr classmethodrrrbrrBrrr r"rr#rurrrxrr1r1r1rDrAs     .      6 j NrA__main__)racCsd}t||dS)Nr)r@)roner1r1rDbarsrcCsd}hddd}t|dS)NT> Duncan Idaho Thufir Hawat Paul AtreidesVladimir Harkonnen)NFT) characters atomic_types)r)rrzedr1r1rDfoos  r%cCs>zztdWWdStYWdStjddYdS)NrTr)r% slfkjsldkfjr2print_exceptionr1r1r1rDrs r)r;N)S __future__rrplatformr` dataclassesrrr?rtypesrrtypingrr r r r r rrrpip._vendor.pygments.lexersrpip._vendor.pygments.tokenrrrrrrrrrrkr_looprrcolumnsrr2rr r!r"r# constrainr$rr%r&panelr'scoper(rr)rr*rr5r+systemWINDOWSrrrsrqr\rbrfrgrurxrrrArnrr%rr1r1r1rDs  ,                 l T