3
¥Yði¢K  ã               @   sX  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZ ddlmZ ddlmZ dd	lmZ d
dlmZ ejdƒZejdejƒZejdƒZdZdZed e d Zed e Z dZ!dZ"dZ#d'e$e%e$dœdd„Z&d(e%e%e'ddœdd„Z(G d d!„ d!ƒZ)G d"d„ dƒZ*G d#d$„ d$ƒZ+G d%d&„ d&ƒZ,dS ))é    N)Úescape)Ú
TokenError)ÚCodeType)ÚTracebackTypeé   )Ú_to_str)Úget_filesystem_encoding)Úcached_propertyé   )ÚConsoles   coding[:=]\s*([-\w.]+)s   ^(.*?)$z+^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)aÉ  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>%(title)s // Werkzeug Debugger</title>
    <link rel="stylesheet" href="?__debugger__=yes&amp;cmd=resource&amp;f=style.css"
        type="text/css">
    <!-- We need to make sure this has a favicon so that the debugger does
         not accidentally trigger a request to /favicon.ico which might
         change the application's state. -->
    <link rel="shortcut icon"
        href="?__debugger__=yes&amp;cmd=resource&amp;f=console.png">
    <script src="?__debugger__=yes&amp;cmd=resource&amp;f=debugger.js"></script>
    <script type="text/javascript">
      var TRACEBACK = %(traceback_id)d,
          CONSOLE_MODE = %(console)s,
          EVALEX = %(evalex)s,
          EVALEX_TRUSTED = %(evalex_trusted)s,
          SECRET = "%(secret)s";
    </script>
  </head>
  <body style="background-color: #fff">
    <div class="debugger">
a¡        <div class="footer">
        Brought to you by <strong class="arthur">DON'T PANIC</strong>, your
        friendly Werkzeug powered traceback interpreter.
      </div>
    </div>

    <div class="pin-prompt">
      <div class="inner">
        <h3>Console Locked</h3>
        <p>
          The console is locked and needs to be unlocked by entering the PIN.
          You can find the PIN printed out on the standard output of your
          shell that runs the server.
        <form>
          <p>PIN:
            <input type=text name=pin size=14>
            <input type=submit name=btn value="Confirm Pin">
        </form>
      </div>
    </div>
  </body>
</html>
aß  <h1>%(exception_type)s</h1>
<div class="detail">
  <p class="errormsg">%(exception)s</p>
</div>
<h2 class="traceback">Traceback <em>(most recent call last)</em></h2>
%(summary)s
<div class="plain">
    <p>
      This is the Copy/Paste friendly version of the traceback.
    </p>
    <textarea cols="50" rows="10" name="code" readonly>%(plaintext)s</textarea>
</div>
<div class="explanation">
  The debugger caught an exception in your WSGI application.  You can now
  look at the traceback which led to the error.  <span class="nojavascript">
  If you enable JavaScript you can also use additional features such as code
  execution (if the evalex feature is enabled), automatic pasting of the
  exceptions and much more.</span>
</div>
z
<!--

%(plaintext_cs)s

-->
a+  <h1>Interactive Console</h1>
<div class="explanation">
In this console you can execute Python expressions in the context of the
application.  The initial namespace was created by the debugger automatically.
</div>
<div class="console"><div class="inner">The Console requires JavaScript.</div></div>
zU<div class="%(classes)s">
  %(title)s
  <ul>%(frames)s</ul>
  %(description)s
</div>
a  <div class="frame" id="frame-%(id)d">
  <h4>File <cite class="filename">"%(filename)s"</cite>,
      line <em class="line">%(lineno)s</em>,
      in <code class="function">%(function_name)s</code></h4>
  <div class="source %(library)s">%(lines)s</div>
</div>
zV<tr class="%(classes)s">
  <td class=lineno>%(lineno)s</td>
  <td>%(code)s</td>
</tr>
T)ÚsecretÚevalex_trustedÚreturnc             C   s   t d|rdnddd| ddœ S )NÚtrueÚfalser   r
   )Úevalexr   ÚconsoleÚtitler   Útraceback_idéÿÿÿÿ)ÚCONSOLE_HTML)r   r   © r   ú:/tmp/pip-build-8om4l6ph/Werkzeug/werkzeug/debug/tbtools.pyÚrender_console_html�   s    
r   FÚ	Traceback)Úignore_system_exceptionsÚshow_hidden_framesÚskipr   c             C   s€   t jt jt jt ttf tjƒ ƒ}|\}}}| r@|tt	t
hkr@‚ x"t|ƒD ]}|jdkrZP |j}qJW t|||ƒ}|s||jƒ  |S )a  Get the current exception info as `Traceback` object.  Per default
    calling this method will reraise system exceptions such as generator exit,
    system exit or others.  This behavior can be disabled by passing `False`
    to the function as first parameter.
    N)ÚtÚcastÚTupleÚTypeÚBaseExceptionr   ÚsysÚexc_infoÚ
SystemExitÚKeyboardInterruptÚGeneratorExitÚrangeÚtb_nextr   Úfilter_hidden_frames)r   r   r   ÚinfoÚexc_typeÚ	exc_valueÚtbÚ_r   r   r   Úget_current_traceback›   s     



r0   c               @   sL   e Zd ZdZdZeeddœdd	„Zee	j
e d
œdd„ƒZed
œdd„ZdS )ÚLinezHelper for the source renderer.ÚlinenoÚcodeÚin_frameÚcurrentN)r2   r3   r   c             C   s   || _ || _d| _d| _d S )NF)r2   r3   r4   r5   )Úselfr2   r3   r   r   r   Ú__init__¿   s    zLine.__init__)r   c             C   s*   dg}| j r|jdƒ | jr&|jdƒ |S )NÚlinezin-framer5   )r4   Úappendr5   )r6   Úrvr   r   r   ÚclassesÅ   s    

zLine.classesc             C   s    t dj| jƒ| jt| jƒdœ S )Nú )r;   r2   r3   )ÚSOURCE_LINE_HTMLÚjoinr;   r2   r   r3   )r6   r   r   r   ÚrenderÎ   s    
zLine.render)r2   r3   r4   r5   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__ÚintÚstrr7   Úpropertyr   ÚListr;   r?   r   r   r   r   r1   º   s   r1   c               @   sÌ   e Zd ZdZeje eeddœdd„Zddœdd„Z	e
edœd	d
„ƒZe
edœdd„ƒZdejeje  ddœdd„Zdeedœdd„Zdeeje eedœdd„Zeedœdd„ƒZe
edœdd„ƒZdS )r   zWraps a traceback.N)r,   r-   r.   r   c             C   s¼   || _ || _|| _|j}|jdkr2|j› d|› �}|| _g | _tƒ }xV| jjt	|||ƒƒ |j
t|ƒƒ |jpr|j}|d ksˆt|ƒ|krŠP t|ƒ}|j}qFW | jjƒ  dd„ | jD ƒ| _d S )NÚbuiltinsÚ__builtin__Ú
exceptionsÚ.c             S   s   g | ]}|j D ]}|‘qqS r   )Úframes)Ú.0ÚgroupÚframer   r   r   ú
<listcomp>ó   s    z&Traceback.__init__.<locals>.<listcomp>>   rK   rI   rJ   )r,   r-   r.   r@   rA   Úexception_typeÚgroupsÚsetr9   ÚGroupÚaddÚidÚ	__cause__Ú__context__ÚtypeÚ__traceback__ÚreverserM   )r6   r,   r-   r.   rR   Úmemor   r   r   r7   Ù   s&    


zTraceback.__init__)r   c             C   s6   x| j D ]}|jƒ  qW dd„ | j D ƒ| jdd…< dS )z.Remove the frames according to the paste spec.c             S   s   g | ]}|j D ]}|‘qqS r   )rM   )rN   rO   rP   r   r   r   rQ   ú   s    z2Traceback.filter_hidden_frames.<locals>.<listcomp>N)rS   r*   rM   )r6   rO   r   r   r   r*   õ   s    zTraceback.filter_hidden_framesc             C   s   t | jtƒS )zIs it a syntax error?)Ú
isinstancer-   ÚSyntaxError)r6   r   r   r   Úis_syntax_errorü   s    zTraceback.is_syntax_errorc             C   s   | j d jS )z-String representation of the final exception.r
   r   )rS   Ú	exception)r6   r   r   r   ra     s    zTraceback.exception)Úlogfiler   c             C   s,   |dkrt j}| jjƒ › d�}|j|ƒ dS )z+Log the ASCII traceback into a file object.NÚ
)r#   ÚstderrÚ	plaintextÚrstripÚwrite)r6   rb   r.   r   r   r   Úlog  s    zTraceback.logT)Úinclude_titler   c                sÔ   d}dg}| j s |jdƒ g }nFtdd„ | j D ƒƒ}d|  k oLt| j ƒk n  ‰ ‡ fdd„| jD ƒ}|rz| jrvd	}nd
}| jr”dt| jƒ› d�}ndt| jƒ› d�}tdj	|ƒd|rº|nd› d�dj	|ƒ|dœ S )z1Render the traceback for the interactive console.Ú Ú	tracebackznoframe-tracebackc             s   s   | ]}|j V  qd S )N)Ú
is_library)rN   rP   r   r   r   ú	<genexpr>  s    z+Traceback.render_summary.<locals>.<genexpr>r   c                s   g | ]}|j ˆ d �‘qS ))Úmark_lib)r?   )rN   rO   )rn   r   r   rQ     s    z,Traceback.render_summary.<locals>.<listcomp>zSyntax Errorz+Traceback <em>(most recent call last)</em>:z<pre class=syntaxerror>z</pre>z<blockquote>z</blockquote>r<   z<h3>z</h3>rc   )r;   r   rM   Údescription)
rM   r9   ÚsumÚlenrS   r`   r   ra   ÚSUMMARY_HTMLr>   )r6   ri   r   r;   rM   Zlibrary_framesro   r   )rn   r   Úrender_summary  s(    
zTraceback.render_summaryF)r   r   r   r   c             C   s\   t | jƒ}t|rdnd|rdndd||t | jƒ| jdd�t | jƒtjdd| jƒ| j|dœ S )z2Render the Full HTML page with the traceback info.r   r   F)ri   z-{2,}ú-)r   r   r   r   ra   rR   Úsummaryre   Zplaintext_csr   r   )	r   ra   Ú	PAGE_HTMLrR   rs   re   ÚreÚsubrW   )r6   r   r   r   Úexcr   r   r   Úrender_full+  s    



zTraceback.render_fullc             C   s   dj dd„ | jD ƒƒS )Nrc   c             S   s   g | ]}|j ƒ ‘qS r   )Úrender_text)rN   rO   r   r   r   rQ   C  s    z'Traceback.plaintext.<locals>.<listcomp>)r>   rS   )r6   r   r   r   re   A  s    zTraceback.plaintextc             C   s   t | ƒS )N)rW   )r6   r   r   r   rW   E  s    zTraceback.id)N)T)FNT)r@   rA   rB   rC   r   r!   r"   r   r7   r*   rG   Úboolr`   rF   ra   ÚOptionalZIOrh   rs   rz   r	   re   rE   rW   r   r   r   r   r   Ö   s(      c               @   sj   e Zd ZdZeje eeddœdd„Zddœdd„Z	e
edœd	d
„ƒZdeedœdd„Zedœdd„ZdS )rU   z�A group of frames for an exception in a traceback. If the
    exception has a ``__cause__`` or ``__context__``, there are multiple
    exception groups.
    N)r,   r-   r.   r   c             C   sf   || _ || _d | _|jd k	r$d| _n|jd k	r4d| _g | _x&|d k	r`| jjt|||ƒƒ |j}q<W d S )NzCThe above exception was the direct cause of the following exceptionzBDuring handling of the above exception, another exception occurred)	r,   r-   r+   rX   rY   rM   r9   ÚFramer)   )r6   r,   r-   r.   r   r   r   r7   P  s    


zGroup.__init__)r   c             C   sÒ   | j s
d S g }d}xt| j D ]j}|j}|dkr@g }d}|dkrzqn:|dkrXd}|dkrzqn"|dkrpd}|dkrzqn
|s|rzq|j|ƒ qW t|ƒd	kr²| j d
 jdkr²| j d d …= n| j d |krÎ|| j d d …< d S )NFÚbeforeÚbefore_and_thisÚresetÚreset_and_thisÚafterÚafter_and_thisTr
   r   Zcodeop)r   r€   )r�   r‚   )rƒ   r„   r   )rM   Úhider9   rq   Úmodule)r6   Z
new_framesÚhiddenrP   r…   r   r   r   r*   g  s4    zGroup.filter_hidden_framesc             C   s*   t j| j| jƒ}dj|ƒjƒ }t|ddƒS )z'String representation of the exception.rj   zutf-8Úreplace)rk   Úformat_exception_onlyr,   r-   r>   Ústripr   )r6   Úbufr:   r   r   r   ra   Œ  s    zGroup.exceptionT)rn   r   c             C   sv   g }| j d k	r"|jd| j › d�ƒ xH| jD ]>}|j rFdt|j ƒ› d�nd}|jd|› d|j|d�› �ƒ q*W d	j|ƒS )
Nz<li><div class="exc-divider">z:</div>z title="ú"rj   z<liú>)rn   rc   )r+   r9   rM   r   r?   r>   )r6   rn   ÚoutrP   r   r   r   r   r?   “  s    
"zGroup.renderc             C   s`   g }| j d k	r"|jd| j › d�ƒ |jdƒ x| jD ]}|j|jƒ ƒ q4W |j| jƒ dj|ƒS )Nrc   z:
z"Traceback (most recent call last):)r+   r9   rM   r{   ra   r>   )r6   rŽ   rP   r   r   r   r{   œ  s    

zGroup.render_text)T)r@   rA   rB   rC   r   r!   r"   r   r7   r*   rG   rF   ra   r|   r?   r{   r   r   r   r   rU   J  s   %	rU   c               @   s  e Zd ZdZeje eeddœdd„Zd#e	e
dœdd	„Zee	d
œdd„ƒZe
d
œdd„Ze
d
œdd„Zeje d
œdd„Zd$eje
ef e
ejdœdd„Zeeje
 d
œdd„ƒZd%eejeje
 e
eje
 f dœdd„Zee
d
œdd„ƒZeed
œdd „ƒZeed
œd!d"„ƒZdS )&r~   zA single frame in a traceback.N)r,   r-   r.   r   c             C   sò   |j | _|jjj| _|jj| _|jj| _	t
j|ƒp:t
j|ƒ}|dd … dkrX|d d… }tjj|ƒrptjj|ƒ}t|tƒ ƒ| _| j	jd| jjdƒƒ| _| j	jd| jjdƒƒ| _|jj| _| jjddƒ| _| jjd	ƒ}|d k	rèt|d
dƒ}|| _d S )Né   ú.pyoú.pycr
   r@   Ú
__loader__Z__traceback_hide__FZ__traceback_info__zutf-8rˆ   éüÿÿÿ)r�   r‘   r   )Ú	tb_linenor2   Útb_frameÚf_codeÚco_nameÚfunction_nameÚf_localsÚlocalsÚ	f_globalsÚglobalsÚinspectÚgetsourcefileÚgetfileÚosÚpathÚisfileÚrealpathr   r   ÚfilenameÚgetr†   Úloaderr3   r…   r+   )r6   r,   r-   r.   Úfnr+   r   r   r   r7   ª  s$    


zFrame.__init__T)rn   r   c             C   s8   t | jt| jƒ| jt| jƒ| jƒ |r.| jr.dnddœ S )z%Render a single frame in a traceback.Úlibraryrj   )rW   r¤   r2   r˜   Úlinesr¨   )Ú
FRAME_HTMLrW   r   r¤   r2   r˜   Úrender_line_contextrl   )r6   rn   r   r   r   r?   Ç  s    zFrame.render)r   c                s   t ‡ fdd„tjƒ jƒ D ƒƒS )Nc             3   s"   | ]}ˆ j jtjj|ƒƒV  qd S )N)r¤   Ú
startswithr    r¡   r£   )rN   r¡   )r6   r   r   rm   Õ  s   z#Frame.is_library.<locals>.<genexpr>)ÚanyÚ	sysconfigÚ	get_pathsÚvalues)r6   r   )r6   r   rl   Ò  s    
zFrame.is_libraryc             C   s(   d| j › d| j› d| j› d| jjƒ › �S )Nz  File "z", line z, in z
    )r¤   r2   r˜   Úcurrent_linerŠ   )r6   r   r   r   r{   Ù  s    zFrame.render_textc                sl   | j ƒ \}}}g ‰ ttd dœ‡ fdd„}x|D ]}||dƒ q.W ||dƒ x|D ]}||dƒ qPW djˆ ƒS )N)r8   Úclsr   c                sV   | j ƒ jƒ } | jƒ }t| ƒt|ƒ }ˆ jd|› dd| › d|rFt|ƒnd› d�ƒ d S )Nz<pre class="line z"><span class="ws">r<   z</span>z</pre>)Ú
expandtabsrf   rŠ   rq   r9   r   )r8   r²   Zstripped_lineÚprefix)r:   r   r   Úrender_lineã  s
    z.Frame.render_line_context.<locals>.render_liner   r5   rƒ   rc   )Úget_context_linesrF   r>   )r6   r   r5   rƒ   rµ   r8   r   )r:   r   r«   ß  s    	


zFrame.render_line_contextc             C   sà   dd„ t | jƒD ƒ}t| jdƒr²| jjd }x&|dkrRtj|| jƒrHP |d8 }q.W y$ttj	dd„ ||d… D ƒƒƒ}W n t
k
r�   d}Y nX x|||| … D ]
}d|_q¤W yd|| jd  _W n tk
rÚ   Y nX |S )	z:Helper function that returns lines with extra information.c             S   s   g | ]\}}t |d  |ƒ‘qS )r
   )r1   )rN   ÚidxÚxr   r   r   rQ   ö  s    z-Frame.get_annotated_lines.<locals>.<listcomp>Úco_firstlinenor
   r   c             S   s   g | ]}|j › d �‘qS )rc   )r3   )rN   r¸   r   r   r   rQ      s    NT)Ú	enumerateÚsourcelinesÚhasattrr3   r¹   Ú_funcdef_reÚmatchrq   r�   Úgetblockr   r4   r2   r5   Ú
IndexError)r6   r©   r2   Úoffsetr8   r   r   r   Úget_annotated_linesô  s$    
$

zFrame.get_annotated_linesÚsingle)r3   Úmoder   c             C   s&   t |tƒrt|d|ƒ}t|| j| jƒS )z*Evaluate code in the context of the frame.z<interactive>)r^   rF   ÚcompileÚevalrœ   rš   )r6   r3   rÄ   r   r   r   rÆ     s    
z
Frame.evalc              C   sd  d}| j dk	r^y:t| j dƒr,| j j| jƒ}nt| j dƒrF| j j| jƒ}W n tk
r\   Y nX |dkr¢y&t| jdd��}|j	ƒ }W dQ R X W n t
k
r    g S X t|tƒr´|jƒ S d}|jtjƒrÒ|dd… }nPxNttj|ƒƒD ]<\}}tj|jƒ ƒ}|dk	�r|jdƒjdƒ}P |dkrâP qâW t|ƒ}ytj|ƒ W n tk
�rR   d}Y nX |j|d	ƒjƒ S )
z.The sourcecode of the file as list of strings.NÚ
get_sourceÚget_source_by_codeÚrb)rÄ   zutf-8é   r
   rˆ   )r¦   r¼   rÇ   r†   rÈ   r3   Ú	ExceptionÚopenr¤   ÚreadÚOSErrorr^   rF   Ú
splitlinesr¬   ÚcodecsÚBOM_UTF8rº   Ú_line_reÚfinditerÚ
_coding_reÚsearchrO   Údecoder   ÚlookupÚLookupError)r6   ÚsourceÚfÚcharsetr·   r¾   Zcoding_matchr   r   r   r»     sB    



zFrame.sourcelinesé   )Úcontextr   c             C   s@   | j | j| d | jd … }| j | j| j| … }|| j|fS )Nr
   )r»   r2   r±   )r6   rÝ   r   Zpastr   r   r   r¶   D  s    zFrame.get_context_linesc             C   s*   y| j | jd  S  tk
r$   dS X d S )Nr
   rj   )r»   r2   rÀ   )r6   r   r   r   r±   K  s    zFrame.current_linec             C   s   t | j| jƒS )N)r   rœ   rš   )r6   r   r   r   r   R  s    zFrame.consolec             C   s   t | ƒS )N)rW   )r6   r   r   r   rW   V  s    zFrame.id)T)rÃ   )rÜ   ) r@   rA   rB   rC   r   r!   r"   r   r7   r|   rF   r?   r	   rl   r{   r«   rH   r1   rÂ   ÚUnionr   ZAnyrÆ   r»   rE   r    r¶   rG   r±   r   r   rW   r   r   r   r   r~   §  s,    0&r~   )T)FFr   )-rÐ   r�   r    rw   r#   r®   rk   Útypingr   Úhtmlr   Útokenizer   Útypesr   r   Z	_internalr   Ú
filesystemr   Úutilsr	   r   r   rÅ   rÔ   Ú	MULTILINErÒ   r½   ZHEADERZFOOTERrv   r   rr   rª   r=   rF   r|   r   rE   r0   r1   r   rU   r~   r   r   r   r   Ú<module>   sF   


		  t]