B /g^[c@s:dZddlZddlZddlZddlZddddddd d d d d ddddddddgZd3ddZddZd4ddZd5dd Z d6ddZ dZ dZ d7dd Z d8d dZd!dZd"d#Zd$d%Zd9d&d Zd:d'd Zd;d(d Zdd+dZd,dZGd-ddZd.dZd/dZd0ZGd1ddeZGd2ddZdS)?z@Extract, format and print information about Python stack traces.N extract_stack extract_tbformat_exceptionformat_exception_only format_list format_stack format_tb print_exc format_excprint_exception print_last print_stackprint_tb clear_frames FrameSummary StackSummaryTracebackException walk_stackwalk_tbcCs8|dkrtj}x$t|D]}t||ddqWdS)zyPrint the list of tuples as returned by extract_tb() or extract_stack() as a formatted stack trace to the given file.NfileZend)sysstderrr from_listformatprint)extracted_listrZitemr!/usr/lib64/python3.7/traceback.py print_listsrcCst|S)aFormat a list of tuples or FrameSummary objects for printing. Given a list of tuples or FrameSummary objects 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. )rrr)rrrrrs cCstt||d|ddS)aPrint up to 'limit' stack trace entries from the traceback 'tb'. If 'limit' is omitted or None, all entries are printed. If 'file' is omitted or None, the output goes to sys.stderr; otherwise 'file' should be an open file or file-like object with a write() method. limitrN)rr)tbr!rrrrr -scCst||dS)z5A shorthand for 'format_list(extract_tb(tb, limit))'.r )rrr#r!rrrr7cCstjt||dS)a# Return a StackSummary object representing a list of pre-processed entries from traceback. This is useful for alternate formatting of stack traces. If 'limit' is omitted or None, all entries are extracted. A pre-processed stack trace entry is a FrameSummary object containing attributes filename, lineno, name, and line representing the information that is usually printed for a stack trace. The line is a string with leading and trailing whitespace stripped; if the source is not available it is None. r )rextractrr$rrrr;s zG The above exception was the direct cause of the following exception: zF During handling of the above exception, another exception occurred: TcCsF|dkrtj}x2tt||||dj|dD]}t||ddq,WdS)aPrint exception up to 'limit' stack trace entries from 'tb' to 'file'. This differs from print_tb() in the following ways: (1) if traceback is not None, it prints a header "Traceback (most recent call last):"; (2) it prints the exception type and value after the stack trace; (3) if type is SyntaxError and value has the appropriate format, it prints the line where the syntax error occurred with a caret on the next line indicating the approximate position of the error. Nr chainrr)rrrtyperr)etypevaluer#r!rr(linerrrr Ws cCs ttt||||dj|dS)azFormat a stack trace and the exception information. The arguments have the same meaning as the corresponding arguments to print_exception(). The return value is a list of strings, each ending in a newline and some containing internal newlines. When these lines are concatenated and printed, exactly the same text is printed as does print_exception(). r r')listrr)r)r*r+r#r!r(rrrrls cCstt||dS)aFormat the exception part of a traceback. The arguments are the exception type and value such as given by sys.last_type and sys.last_value. 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 always the last string in the list. N)r-rr)r*r+rrrr|scCs.t|}|dks|sd|}n d||f}|S)Nz%s z%s: %s ) _some_str)r*r+Zvaluestrr,rrr_format_final_exc_lines    r/cCs"yt|Sdt|jSdS)Nz)strr)__name__)r+rrrr.sr.cCstt|||ddS)z>Shorthand for 'print_exception(*sys.exc_info(), limit, file)'.r!rr(N)r rexc_infor2rrrrr%cCsdtt||dS)z%Like print_exc() but return a string.rr!r()joinrrr3r4rrrr r%cCs.ttdstdttjtjtj|||dS)znThis is a shorthand for 'print_exception(sys.last_type, sys.last_value, sys.last_traceback, limit, file)'. last_typezno last exceptionN)ZhasattrrZ ValueErrorr r6Z last_valueZlast_tracebackr2rrrr s cCs*|dkrtj}tt||d|ddS)zPrint a stack trace from its invocation point. The optional 'f' argument can be used to specify an alternate stack frame at which to start. The optional 'limit' and 'file' arguments have the same meaning as for print_exception(). Nr r")r _getframef_backrr)fr!rrrrr s cCs"|dkrtj}tt||dS)z5Shorthand for 'format_list(extract_stack(f, limit))'.Nr )rr7r8rr)r9r!rrrrs cCs0|dkrtj}tjt||d}||S)asExtract the raw traceback from the current stack frame. The return value has the same format as for extract_tb(). The optional 'f' and 'limit' arguments have the same meaning as for print_stack(). Each item in the list is a quadruple (filename, line number, function name, text), and the entries are in order from oldest to newest stack frame. Nr )rr7r8rr&rZreverse)r9r!stackrrrrs  cCs<x6|dk r6y|jWntk r,YnX|j}qWdS)zEClear all references to local variables in the frames of a traceback.N)tb_frameZclearZ RuntimeErrortb_nextr#rrrrs  c@sReZdZdZdZddddddZdd Zd d Zd d ZddZ e ddZ dS)ra,A single frame from a traceback. - :attr:`filename` The filename for the frame. - :attr:`lineno` The line within filename for the frame that was active when the frame was captured. - :attr:`name` The name of the function or method that was executing when the frame was captured. - :attr:`line` The text from the linecache module for the of code that was running when the frame was captured. - :attr:`locals` Either None if locals were not supplied, or a dict mapping the name to the repr() of the variable. )filenamelinenoname_linelocalsTN) lookup_linerBr,cCsB||_||_||_||_|r"|j|r8dd|Dnd|_dS)aConstruct a FrameSummary. :param lookup_line: If True, `linecache` is consulted for the source code line. Otherwise, the line will be looked up when first needed. :param locals: If supplied the frame locals, which will be captured as object representations. :param line: If provided, use this instead of looking up the line in the linecache. cSsi|]\}}t||qSr)Zrepr).0ZkZvrrrz sz)FrameSummary.__init__..N)r>r?r@rAr,itemsrB)selfr>r?r@rCrBr,rrr__init__s zFrameSummary.__init__cCs`t|tr:|j|jko8|j|jko8|j|jko8|j|jkSt|tr\|j|j|j|jf|kStSN) isinstancerr>r?r@rBZtupler,ZNotImplementedrFZotherrrr__eq__s      zFrameSummary.__eq__cCs|j|j|j|jf|SrH)r>r?r@r,)rFZposrrr __getitem__zFrameSummary.__getitem__cCst|j|j|j|jgSrH)Ziterr>r?r@r,rFrrr__iter__rMzFrameSummary.__iter__cCsdj|j|j|jdS)Nz7)r>r?r@)rr>r?r@rNrrr__repr__szFrameSummary.__repr__cCs&|jdkr t|j|j|_|jSrH)rA linecacheZgetliner>r?striprNrrrr,s zFrameSummary.line) r1 __module__ __qualname____doc__Z __slots__rGrKrLrOrPZpropertyr,rrrrrs  ccs8|dkrtjj}x|dk r2||jfV|j}qWdS)zWalk a stack yielding the frame and line number for each frame. This will follow f.f_back from the given frame. If no frame is given, the current stack is used. Usually used with StackSummary.extract. N)rr7r8Zf_lineno)r9rrrr!s    ccs&x |dk r |j|jfV|j}qWdS)zWalk a traceback yielding the frame and line number for each frame. This will follow tb.tb_next (and thus is in the opposite order to walk_stack). Usually used with StackSummary.extract. N)r;Z tb_linenor<r=rrrr.s ic@s:eZdZdZeddddddZedd Zd d ZdS) rzA stack of frames.NTFr! lookup_linescapture_localsc Cs|dkr(ttdd}|dk r(|dkr(d}|dk rV|dkrFt||}ntj|| d}|}t}xb|D]Z\}}|j} | j} | j } | | t | |j |r|j} nd} |t| || d| dqhWx|D]} t | qW|rx|D] }|jqW|S)a?Create a StackSummary from a traceback or stack object. :param frame_gen: A generator that yields (frame, lineno) tuples to include in the stack. :param limit: None to include all frames or the number of frames to include. :param lookup_lines: If True, lookup lines for each frame immediately, otherwise lookup is deferred until the frame is rendered. :param capture_locals: If True, the local variables from each frame will be captured as object representations into the FrameSummary. NZtracebacklimitr)ZmaxlenF)rCrB)Zgetattrr itertoolsZislice collectionsZdequesetZf_codeZ co_filenameZco_nameaddrQZ lazycacheZ f_globalsf_localsappendrZ checkcacher,) klassZ frame_genr!rWrXresultZfnamesr9r?Zcor>r@r]rrrr&>s6     zStackSummary.extractc CsPt}xD|D]<}t|tr&||q |\}}}}|t||||dq W|S)z Create a StackSummary object from a supplied list of FrameSummary objects or old-style list of tuples. )r,)rrIrr^)r_Za_listr`framer>r?r@r,rrrrns     zStackSummary.from_listc Csxg}d}d}d}d}x$|D]}|dksX||jksX|dksX||jksX|dksX||jkr|tkr|t8}|d|d|dkrdndd|j}|j}|j}d}|d7}|tkrqg}|d |j|j|j|jr|d |j|jr(x0t |j D]\}} |d j|| d qW|d |qW|tkrt|t8}|d|d|dkrhdndd|S) aFormat the stack ready for printing. Returns a list of strings ready for printing. Each string in the resulting list corresponds to a single frame from the stack. Each string ends in a newline; the strings may contain internal newlines as well, for those items with source text lines. For long sequences of the same frame and line, the first few repetitions are shown, followed by a summary line stating the exact number of further repetitions. Nrz [Previous line repeated z more timeZsrz] z File "{}", line {}, in {}  {} z {name} = {value} )r@r+) r>r?r@_RECURSIVE_CUTOFFr^rr,rRrBZsortedrEr5) rFr`Z last_fileZ last_lineZ last_nameZcountraZrowr@r+rrrrsD   "zStackSummary.format)r1rSrTrU classmethodr&rrrrrrr;s . c@s^eZdZdZdddddddZedd Zd d Zd d ZddZ ddZ ddddZ dS)raAn exception ready for rendering. The traceback module captures enough attributes from the original exception to this intermediary form to ensure that no references are held, while still being able to fully print or format it. Use `from_exception` to create TracebackException instances from exception objects, or the constructor to create TracebackException instances from individual components. - :attr:`__cause__` A TracebackException of the original *__cause__*. - :attr:`__context__` A TracebackException of the original *__context__*. - :attr:`__suppress_context__` The *__suppress_context__* value from the original exception. - :attr:`stack` A `StackSummary` representing the traceback. - :attr:`exc_type` The class of the original traceback. - :attr:`filename` For syntax errors - the filename where the error occurred. - :attr:`lineno` For syntax errors - the linenumber where the error occurred. - :attr:`text` For syntax errors - the text where the error occurred. - :attr:`offset` For syntax errors - the offset into the text where the error occurred. - :attr:`msg` For syntax errors - the compiler error message. NTFr!rWrX_seenc Cs>|dkrt}|t||r\|jdk r\t|j|kr\tt|j|j|jj|d||d}nd}|r|jdk rt|j|krtt|j|j|jj|d||d} nd} ||_||_| |_|r|j nd|_ t j t ||||d|_ ||_t||_|r,t|tr,|j|_t|j|_|j|_|j|_|j|_|r:|dS)NFrfrV)r[r\Zid __cause__rr) __traceback__ __context__ exc_traceback__suppress_context__rr&rr:exc_typer._str issubclass SyntaxErrorr>r0r?textoffsetmsg _load_lines) rFrmZ exc_valuerkr!rWrXrgZcauseZcontextrrrrGsT      zTracebackException.__init__cOs|t|||jf||S)z.Create a TracebackException from an exception.)r)ri)ZclsZexcZargsZkwargsrrrfrom_exception sz!TracebackException.from_exceptioncCs:x|jD] }|jqW|jr&|j|jr6|jdS)z7Private API. force all lines in the stack to be loaded.N)r:r,rjrtrh)rFrarrrrts    zTracebackException._load_linescCs |j|jkSrH)Z__dict__rJrrrrKrMzTracebackException.__eq__cCs|jSrH)rnrNrrr__str__rMzTracebackException.__str__c cs|jdkrtd|jVdS|jj}|jj}|dkr@|d|}t|jts^t||jVdS|jpfd}t|j ptd}d ||V|j }|j }|dk rd | V|dk r|d}tt||d }|d|}d d |D}d d |V|jpd}d ||VdS)aFormat the exception part of the traceback. The return value is a generator of strings, each ending in a newline. Normally, the generator emits a single string; however, for SyntaxError exceptions, it emites several lines that (when printed) display detailed information about where the syntax error occurred. The message indicating which exception occurred is always the last string in the output. N)Z__main__ZbuiltinsZ.zz?z File "{}", line {} rcZ rbcss|]}|r|pdVqdS)z N)Zisspace)rDZcrrrz Gsz;TracebackException.format_exception_only..z {}^ rzz{}: {} )rmr/rnrTrSrorpr>r0r?rrqrrrRZrstripZminZlenZlstripr5rs) rFZstypeZsmodr>r?ZbadlinerrZ caretspacersrrrrs2      z(TracebackException.format_exception_onlyr'ccs|rT|jdk r*|jj|dEdHtVn*|jdk rT|jsT|jj|dEdHtV|jdk rddV|jEdH|EdHdS)aFormat the exception. If chain is not *True*, *__cause__* and *__context__* will not be formatted. The return value is a generator of strings, each ending in a newline and some containing internal newlines. `print_exception` is a wrapper around this method which just prints the lines to a file. The message indicating which exception occurred is always the last string in the output. Nr'z#Traceback (most recent call last): ) rhr_cause_messagerjrl_context_messagerkr:r)rFr(rrrrLs    zTracebackException.format) r1rSrTrUrGrerurtrKrvrrrrrrrs9  -rHNNrHrHNNTNTrzr{rz)NNNryry) rUrZrYrQrZ__all__rrr rrrwrxr rrr/r.rr r r rrrrrrrdr-rrrrrrZsF          >  z