U
    HaJ                     @   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dZedejZe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   )evalexr   consoletitler   traceback_id)CONSOLE_HTML)r   r    r   :/tmp/pip-unpacked-wheel-ub1y1qyw/werkzeug/debug/tbtools.pyrender_console_html   s    
r   F	Traceback)ignore_system_exceptionsshow_hidden_framesskipr   c                 C   s~   t t jt jt ttf t }|\}}}| r@|tt	t
hkr@ t|D ]}|jdkrZ qb|j}qHt|||}|sz|  |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)r3   r4   r   c                 C   s   || _ || _d| _d| _d S )NFr2   )selfr3   r4   r   r   r   __init__   s    zLine.__init__r   c                 C   s*   dg}| j r|d | jr&|d |S )Nlinezin-framer6   )r5   appendr6   )r7   rvr   r   r   classes   s    

zLine.classesc                 C   s    t d| j| jt| jd S )N )r=   r3   r4   )SOURCE_LINE_HTMLjoinr=   r3   r   r4   r7   r   r   r   render   s
    
zLine.render)__name__
__module____qualname____doc__	__slots__intstrr8   propertyr   Listr=   rB   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 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 }| jt	||| |
t| |jpp|j}|d kst||krqt|}|j}qD| j  dd | jD | _d S )N>   __builtin__
exceptionsbuiltins.c                 S   s   g | ]}|j D ]}|qqS r   frames.0groupframer   r   r   
<listcomp>   s       z&Traceback.__init__.<locals>.<listcomp>)r,   r-   r.   rC   rD   exception_typegroupssetr;   Groupaddid	__cause____context__type__traceback__reverserR   )r7   r,   r-   r.   rX   memor   r   r   r8      s$    

zTraceback.__init__r9   c                 C   s2   | j D ]}|  q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   rQ   rS   r   r   r   rW      s       z2Traceback.filter_hidden_frames.<locals>.<listcomp>N)rY   r*   rR   )r7   rU   r   r   r   r*      s    

zTraceback.filter_hidden_framesc                 C   s   t | jtS )zIs it a syntax error?)
isinstancer-   SyntaxErrorrA   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   )rY   	exceptionrA   r   r   r   rg     s    zTraceback.exception)logfiler   c                 C   s,   |dkrt j}| j  d}|| dS )z+Log the ASCII traceback into a file object.N
)r#   stderr	plaintextrstripwrite)r7   rh   r.   r   r   r   log  s    zTraceback.logT)include_titler   c                    s   d}dg}| j s |d g }nFtdd | j D }d|  k oLt| j k n    fdd| jD }|rz| jrvd	}nd
}| jrdt| j d}ndt| j d}td	|d|r|nd dd	||d S )z1Render the traceback for the interactive console. 	tracebackznoframe-tracebackc                 s   s   | ]}|j V  qd S N)
is_library)rT   rV   r   r   r   	<genexpr>  s     z+Traceback.render_summary.<locals>.<genexpr>r   c                    s   g | ]}|j  d qS )mark_lib)rB   rT   rU   ru   r   r   rW     s     z,Traceback.render_summary.<locals>.<listcomp>zSyntax Errorz+Traceback <em>(most recent call last)</em>:z<pre class=syntaxerror></pre>z<blockquote>z</blockquote>r>   z<h3>z</h3>ri   )r=   r   rR   description)
rR   r;   sumlenrY   rf   r   rg   SUMMARY_HTMLr@   )r7   ro   r   r=   rR   Zlibrary_framesry   r   ru   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dd| j| j|d S )z2Render the Full HTML page with the traceback info.r   r   F)ro   z-{2,}-)r   r   r   r   rg   rX   summaryrk   Zplaintext_csr   r   )	r   rg   	PAGE_HTMLrX   r}   rk   resubr]   )r7   r   r   r   excr   r   r   render_full+  s    



zTraceback.render_fullc                 C   s   d dd | jD S )Nri   c                 S   s   g | ]}|  qS r   )render_textrw   r   r   r   rW   C  s     z'Traceback.plaintext.<locals>.<listcomp>)r@   rY   rA   r   r   r   rk   A  s    zTraceback.plaintextc                 C   s   t | S rr   r]   rA   r   r   r   r]   E  s    zTraceback.id)N)T)FNT)rC   rD   rE   rF   r   r!   r"   r   r8   r*   rJ   boolrf   rI   rg   OptionalTextIOrn   r}   r   r	   rk   rH   r]   r   r   r   r   r      s4       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 )r[   zA group of frames for an exception in a traceback. If the
    exception has a ``__cause__`` or ``__context__``, there are multiple
    exception groups.
    NrL   c                 C   sb   || _ || _d | _|jd k	r$d| _n|jd k	r4d| _g | _|d k	r^| jt||| |j}q:d S )NzCThe above exception was the direct cause of the following exceptionzBDuring handling of the above exception, another exception occurred)	r,   r-   r+   r^   r_   rR   r;   Framer)   )r7   r,   r-   r.   r   r   r   r8   P  s    

zGroup.__init__r9   c                 C   s   g }d}| j D ]j}|j}|dkr4g }d}|dkrnqn:|dkrLd}|dkrnqn"|dkrdd}|dkrnqn
|s|rnq|| q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_thisr   )resetreset_and_thisr   )afterafter_and_thisTr   r
   r   Zcodeopr   )rR   hider;   r{   module)r7   Z
new_frameshiddenrV   r   r   r   r   r*   g  s0    
zGroup.filter_hidden_framesc                 C   s*   t | j| j}d| }t|ddS )z'String representation of the exception.rp   utf-8replace)rq   format_exception_onlyr,   r-   r@   stripr   )r7   bufr<   r   r   r   rg     s    zGroup.exceptionTrv   r   c              	   C   sr   g }| j d k	r"|d| j  d | jD ]>}|j rDdt|j  dnd}|d| d|j|d  q(d	|S )
Nz<li><div class="exc-divider">z:</div>z title=""rp   z<li>ru   ri   )r+   r;   rR   r   rB   r@   )r7   rv   outrV   r   r   r   r   rB     s    

 zGroup.renderc                 C   s\   g }| j d k	r"|d| j  d |d | jD ]}||  q2|| j d|S )Nri   z:
z"Traceback (most recent call last):)r+   r;   rR   r   rg   r@   )r7   r   rV   r   r   r   r     s    


zGroup.render_text)T)rC   rD   rE   rF   r   r!   r"   r   r8   r*   rJ   rI   rg   r   rB   r   r   r   r   r   r[   J  s    	r[   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.NrL   c                 C   s   |j | _|jjj| _|jj| _|jj| _	t
|p:t
|}|dd  dkrX|d d }tj|rptj|}t|t | _| j	d| jd| _| j	d| jd| _|jj| _| jdd| _| jd}|d k	rt|d	d
}|| _d S )N)z.pyoz.pycr   rC   
__loader__Z__traceback_hide__FZ__traceback_info__r   r   )	tb_linenor3   tb_framef_codeco_namefunction_namef_localslocals	f_globalsglobalsinspectgetsourcefilegetfileospathisfilerealpathr   r   filenamegetr   loaderr4   r   r+   )r7   r,   r-   r.   fnr+   r   r   r   r8     s$    


zFrame.__init__Tr   c                 C   s8   t | jt| j| jt| j|  |r.| jr.dndd S )z%Render a single frame in a traceback.libraryrp   )r]   r   r3   r   linesr   )
FRAME_HTMLr]   r   r   r3   r   render_line_contextrs   )r7   rv   r   r   r   rB     s    zFrame.renderr9   c                    s   t  fddt  D S )Nc                 3   s"   | ]} j tj|V  qd S rr   )r   
startswithr   r   r   )rT   r   rA   r   r   rt     s   z#Frame.is_library.<locals>.<genexpr>)any	sysconfig	get_pathsvaluesrA   r   rA   r   rs     s    
zFrame.is_libraryc              	   C   s(   d| j  d| j d| j d| j  S )Nz  File "z", line z, in z
    )r   r3   r   current_liner   rA   r   r   r   r     s    &zFrame.render_textc                    sd   |   \}}}g  ttd d fdd}|D ]}||d q,||d |D ]}||d qJd S )N)r:   clsr   c              	      sV   |    } |  }t| t| } d| dd|  d|rFt|nd d d S )Nz<pre class="line z"><span class="ws">r>   z</span>rx   )
expandtabsrl   r   r{   r;   r   )r:   r   Zstripped_lineprefixr<   r   r   render_line  s    &z.Frame.render_line_context.<locals>.render_liner   r6   r   ri   )get_context_linesrI   r@   )r7   r   r6   r   r   r:   r   r   r   r     s    	
zFrame.render_line_contextc                 C   s   dd t | jD }t| jdr| jjd }|dkrPt|| jrFqP|d8 }q,z$tt	dd ||d D }W n t
k
r   d}Y nX ||||  D ]
}d|_qz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   )rT   idxxr   r   r   rW     s     z-Frame.get_annotated_lines.<locals>.<listcomp>co_firstlinenor
   r   c                 S   s   g | ]}|j  d qS )ri   )r4   )rT   r   r   r   r   rW     s     NT)	enumeratesourcelineshasattrr4   r   _funcdef_rematchr{   r   getblockr   r5   r3   r6   
IndexError)r7   r   r3   offsetr:   r   r   r   get_annotated_lines  s$    
$
zFrame.get_annotated_linessingle)r4   moder   c                 C   s&   t |trt|d|}t|| j| jS )z*Evaluate code in the context of the frame.z<interactive>)rd   rI   compileevalr   r   )r7   r4   r   r   r   r   r   	  s    
z
Frame.evalc              	   C   sl  d}| j dk	r^z:t| j dr,| j | j}nt| j drF| j | j}W n tk
r\   Y nX |dkrz&t| jdd}|	 }W 5 Q R X W n t
k
r   g  Y S X t|tr| S d}|tjr|dd }nTtt|D ]D\}}t| }|dk	r|dd} q*|dkr q*qt|}zt| W n tk
rZ   d}Y nX ||d	 S )
z.The sourcecode of the file as list of strings.N
get_sourceget_source_by_coderb)r   r      r
   r   )r   r   r   r   r   r4   	Exceptionopenr   readOSErrorrd   rI   
splitlinesr   codecsBOM_UTF8r   _line_refinditer
_coding_researchrU   decoder   lookupLookupError)r7   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   r3   r   )r7   r   r   Zpastr   r   r   r   ?  s    zFrame.get_context_linesc                 C   s.   z| j | jd  W S  tk
r(   Y dS X d S )Nr
   rp   )r   r3   r   rA   r   r   r   r   F  s    zFrame.current_linec                 C   s   t | j| jS rr   )r   r   r   rA   r   r   r   r   M  s    zFrame.consolec                 C   s   t | S rr   r   rA   r   r   r   r]   Q  s    zFrame.id)T)r   )r   ) rC   rD   rE   rF   r   r!   r"   r   r8   r   rI   rB   r	   rs   r   r   rK   r1   r   Unionr   Anyr   r   rH   r    r   rJ   r   r   r   r]   r   r   r   r   r     s4    0 r   )T)FFr   )-r   r   r   r   r#   r   rq   typingr   htmlr   tokenizer   typesr   r   	_internalr   
filesystemr   utilsr	   r   r   r   r   	MULTILINEr   r   HEADERZFOOTERr   r   r|   r   r?   rI   r   r   rH   r0   r1   r   r[   r   r   r   r   r   <module>   sf   

"		   tX