3 \.@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddZ gZ ddZ ddZd d Zd d Zd dZdddZdddZGdddZejZdddZdS)aMore comprehensive traceback formatting for Python scripts. To enable this module, do: import cgitb; cgitb.enable() at the top of your script. The optional arguments to enable() are: display - if true, tracebacks are displayed in the web browser logdir - if set, tracebacks are written to files in this directory context - number of lines of source code to show for each stack frame format - 'text' or 'html' controls the output format By default, tracebacks are displayed but not saved, the context is 5 lines and the output format is 'html' (for backwards compatibility with the original use of this module) Alternatively, if you have caught an exception and want cgitb to display it for you, call cgitb.handler(). The optional argument to handler() is a 3-item tuple (etype, evalue, etb) just like the value of sys.exc_info(). The default handler displays output as HTML. NcCsdS)zAReturn a string that resets the CGI and browser to a known state.a' --> --> rrr/usr/lib64/python3.6/cgitb.pyreset#srcCs|rd|dSdSdS)Nzzr)textrrrsmall.s rcCs|rd|dSdSdS)Nzz rr)rrrrstrong4s rcCs|rd|dSdSdS)Nzzrr)rrrrgrey:s r cCs||krd||fS||jkr,d|j|fSd|jkr~|jd}t|tikrf||kr~d||fSnt||r~dt||fSdtfS)z9Find the value for a given name in the given environment.localglobal __builtins__builtinN) f_globalstypehasattrgetattr __UNDEF__)nameframelocalsbuiltinsrrrlookup@s     rcCsgdddtf\}}}}}xtj|D]\}} } } } |tjkr>P|tjkr| tjkr|dkr|tk rt|| t}|j|| ||fqt | ||\} }|j| | |fn"| dkr||d7}|}nd\}}| }q$W|S)zEScan one logical line of Python and look up values of variables used.Nr.)Nr) rtokenizegenerate_tokensNEWLINENAMEkeywordkwlistrappendr)readerrrvarsZ lasttokenparentprefixvalueZttypetokenstartendlinewhererrrscanvarsPs"   r*c" s|\}}}t|tr|j}dtjjddtj}tjtj}dt j j dt t j j t|dd|d|d }d td d d }g} tj||} xJ| D]@\} } } }}rtjjdt j j f}nd}tj| \}}}}d}| dkr2dt t j j | tj||||ddd}i| gffdd }t|| |}dd||fg}|dk r| |}x|D]}td d tt|t|d }|krd|t j j|f}|jd|n&d|t j j|f}|jdt||d7}q~Wig}}x|D]\}}} ||kr0qd||<| tk r|d-kr^d!|t |}n*|d"krrt |}n|t |jd#d.}|jd$|t j j| fn|j|d%qW|jdttd&j|| jd'd(j|qWd)t t j j t|t j j t|fg}!xNt|D]B}|ddd*kr:q t j jt ||} |!jd+||| fq W|dj| dj|!d,t j j djt!j"|||S)/z9Return a nice HTML document describing a given traceback.zPython rz: zz%sz#ffffffz#6622aaz
z

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.

zz r+z  z%s?rzin cSsdtjj|S)N=)pydochtmlrepr)r$rrrszhtml..) formatvaluec s4d|d<ztj|dS|dd7<XdS)Nr) linecachegetline)lnum)file highlightrrr s zhtml..readerz+%s%s %sz Nz=>%s%sz&%sz  %s%sz%sr3r r z %s r rz %s = %sz undefinedz, zF %s
 z

%s: %s_z
%s%s = %sz )r r )# isinstancer__name__sysversionsplit executabletimectimer.r/Zheadingrescapestrrinspectgetinnerframesospathabspath getargvaluesformatargvaluesr*lenZ preformatrr rr0joindirr tracebackformat_exception)"einfocontextetypeevalueetbpyverdateheadindentframesrecordsrr6funclinesindexlinkargsvarargsvarkwrcallr r!rowsir(numdonedumprr)r$ exceptionr)r7r8rr/es|  0       $        r/c s|\}}}t|tr|j}dtjjddtj}tjtj}dt |||fd}g}t j ||} x| D]\} } } } }rt j jpdt j| \}}}}d}| dkrd| t j||||d d d }i| gffd d }t|| |}d|fg}|dk rL| |}x0| D](}d|}|j||j|d7}q Wig}}x|D]\}}}||krtq\d||<|tk r|dkrd|}n|dkr||jdd}|jd|tjj|fn|j|dq\W|jdj||jddj|qrWdt |t |fg}x8t|D],}tjjt||}|jdd ||fq.W|dj|dj|ddjtj|||S)!z:Return a plain text document describing a given traceback.zPython rz: z %s %s %s z A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred. r,rzin cSsdtjj|S)Nr-)r.rr0)r$rrrr1sztext..)r2c s4d|d<ztj|dS|dd7<XdS)Nr3r)r4r5)r6)r7r8rrr s ztext..readerz %s %sNz%5d r3r zglobal r rz%s = %sz undefinedr9z %s z%s: %sz %s%s = %s zc The above is a description of an error in a Python program. Here is the original traceback: %s r;z )r<rr=r>r?r@rArBrCrErFrGrHrIrJrKrLr*rrstriprr.rr0rNrOrrPrQ) rRrSrTrUrVrWrXrYr[r\rr6r]r^r_rarbrcrrdr r!rerfr(rgrhrirr)r$rjr)r7r8rrs^            rc@s,eZdZdZd ddZdd Zd d d ZdS)Hookz?A hook to replace sys.excepthook that shows tracebacks in HTML.r3Nr+r/cCs(||_||_||_|ptj|_||_dS)N)displaylogdirrSr>stdoutr7format)selfrorprSr7rrrrr__init__s  z Hook.__init__cCs|j|||fdS)N)handle)rsrTrUrVrrr__call__ sz Hook.__call__c Csz|p tj}|jdkr$|jjt|jdkr2tp4t}d}y|||j}Wn dj t j |}d}YnX|j r|rt jj|}|jjd|dq|jj|dn |jjd|jdk rZd d g|jdk}tj||jd \}}y.tj|d }|j|WdQRXd |} Wnd|} YnX|jdkrJ|jjd| n|jj| dy|jjWn YnXdS)Nr/FrTz

z
r9z*

A problem occurred in a Python script. z.txtz.html)suffixrOwz*%s contains the description of this error.z*Tried to save traceback to %s, but failed.z

%s

)r>exc_inforrr7writerr/rrSrNrPrQror.rDrptempfileZmkstemprHfdopenflush) rsinfoZ formatterZplaindocrwfdrIr7msgrrrrus@        z Hook.handle)r3Nr+Nr/)N)r= __module__ __qualname____doc__rtrvrurrrrrns  rnr3cCst||||dt_dS)aInstall an exception handler that formats tracebacks as HTML. The optional argument 'display' can be set to 0 to suppress sending the traceback to the browser, and 'logdir' can be set to a directory to cause tracebacks to be written to files there.)rorprSrrN)rnr> excepthook)rorprSrrrrrenable8sr)r+)r+)r3Nr+r/)rrFrr4rHr.r>r{rBrrPrrrrr rr*r/rrnruZhandlerrrrrrs,  Z A7