[+@sdZddlZddlZddlZddddddd d d d d dddgZddZdddZddZddZddZ ddddZ ddd Z dddZ dZ dZdddd Zd!d"Zddd#d$d Zdd#d%dZd&dZd'd(Zd)d*Zd+d,Zddd#d-d Zdd#d.d Zddd#d/d Zdd0d1Zd2d3Zdddd4dZddd5dZddd6dZd7dZdS)8z@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_framesccs]xV|D]N\}}}}dj|||}|rP|dj|j}n|VqWdS)Nz File "{}", line {}, in {} z {} )formatstrip)extracted_listfilenamelinenonamelineitemr/usr/lib/python3.4/traceback.py_format_list_iters rcCsI|dkrtj}nx*t|D]}t|d|ddq%WdS)zyPrint the list of tuples as returned by extract_tb() or extract_stack() as a formatted stack trace to the given file.Nfileend)sysstderrrprint)rrrrrr print_lists  r!cCstt|S)aFormat 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. )listr)rrrrr s c cs|dkr!ttdd}nd}x|dk r|dksN||kr||\}}}|j}|j}|j} tj|tj|||j} | r| j } nd} ||| | fV|}|d7}q*WdS)Ntracebacklimitr) getattrrf_code co_filenameco_name linecache checkcachegetline f_globalsr) currlimit extractornfr next_itemcorrrrrr_extract_tb_or_stack_iter5s  '    r4cCst||tjdddS)Ntb_frame tb_linenotb_next)r4operator attrgetter)tbr.rrr_extract_tb_iterLsr;cCs tt|d|d|dS)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. r.rN)r!r)r:r.rrrrrQscCstt|d|S)z5A shorthand for 'format_list(extract_tb(tb, limit))'.r.)rr)r:r.rrrr[scCstt|d|S)aReturn list of up to limit 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 quadruple (filename, line number, function name, text) representing the information that is usually printed for a stack trace. The text is a string with leading and trailing whitespace stripped; if the source is not available it is None. r.)r"r;)r:r.rrrr_s zF The above exception was the direct cause of the following exception: zE During handling of the above exception, another exception occurred: ccs|dkrt}n|j|g}|j}|j}|dk r||kr|jt|d||jtdfgnT|dk r|j r||kr|jt|d||jtdfgn|j||p|j fgx|D]}|DdHqWdS)NF) setadd __context__ __cause__append _iter_chain_cause_message__suppress_context___context_message __traceback__)exc custom_tbseenitscontextcauseitrrrrAxs"         rAccs|rt||}n||fg}xs|D]k\}}t|trX|dVq.n|rdVtt|d|DdHntt||DdHq.WdS)N z#Traceback (most recent call last): r.)rA isinstancestrrr;_format_exception_only_itertype)etypevaluer:r.chainvaluesrrr_format_exception_iters rVTcCsU|dkrtj}nx6t|||||D]}t|d|ddq1WdS)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. Nrrr)rrrVr )rRrSr:r.rrTrrrrr s  cCstt|||||S)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"rV)rRrSr:r.rTrrrrs cCstt||S)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. )r"rP)rRrSrrrrsc css|dkrt||VdS|j}|j}|dkrM|d|}nt|tsnt||VdS|jpzd}t|jpd}dj||V|j }|j }|dk rOdj|j V|dk rO|j d}t t||d }|d|j}d d |D}d jd j|VqOn|jp[d} dj|| VdS)N__main__builtins.z?z File "{}", line {} z {} rMr$css'|]}|jr|pdVqdS) N)isspace).0crrr sz._format_exception_only_iter..z {}^ rzz{}: {} )z__main__zbuiltins)_format_final_exc_line__name__ __module__ issubclass SyntaxErrorrrOrrtextoffsetrrstripminlenlstripjoinmsg) rRrSstypesmodrrbadlinerf caretspacerlrrrrPs2        rPcCs@t|}|dks| r,d|}nd||f}|S)Nz%s z%s: %s ) _some_str)rRrSvaluestrrrrrr`s   r`c Cs.yt|SWndt|jSYnXdS)Nz)rOrQra)rSrrrrqsrqcCs&td|d|d|tjdS)z>Shorthand for 'print_exception(*sys.exc_info(), limit, file)'.r.rrTN)r rexc_info)r.rrTrrrr scCs%djtd|d|tjS)z%Like print_exc() but return a string.rr.rT)rkrrrs)r.rTrrrr scCsDttdstdnttjtjtj|||dS)znThis is a shorthand for 'print_exception(sys.last_type, sys.last_value, sys.last_traceback, limit, file)'. last_typezno last exceptionN)hasattrr ValueErrorr rt last_valuelast_traceback)r.rrTrrrr scCst||ddS)NcSs||j|jfS)N)f_linenof_back)r1rrrsz%_extract_stack_iter..)r4)r1r.rrr_extract_stack_itersr|cCs%|dkr!tjjj}n|S)N)r _getframerz)r1rrr _get_stacks r~cCs&ttt|d|d|dS)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(). r.rN)r!rr~)r1r.rrrrr scCsttt|d|S)z5Shorthand for 'format_list(extract_stack(f, limit))'.r.)rrr~)r1r.rrrr scCs,ttt|d|}|j|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. r.)r"r|r~reverse)r1r.stackrrrr$s  c CsFx?|dk rAy|jjWntk r4YnX|j}qWdS)zEClear all references to local variables in the frames of a traceback.N)r5clear RuntimeErrorr7)r:rrrr1s  ) __doc__r)rr8__all__rr!rr4r;rrrrBrDrArVr rrrPr`rqr r r r|r~r rrrrrrrsF                #