
_O                 @   sO  d  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 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 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 e  e! f Z" y e" e# f 7Z" Wn e$ k
 rYn Xd Z% d Z& e% d e& d Z' e% d e& Z( d Z) d Z* d Z+ d d d  Z, d  d  d d! d"  Z- Gd# d$   d$ e.  Z/ Gd% d&   d& e.  Z0 Gd' d(   d( e.  Z1 Gd) d*   d* e.  Z2 d S)+z
    werkzeug.debug.tbtools
    ~~~~~~~~~~~~~~~~~~~~~~

    This module provides various traceback related utility functions.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
    N)
TokenError   )PY2)
range_type)reraise)string_types)	text_type)	to_native)
to_unicode)get_filesystem_encoding)cached_property)escape   )Consoles   coding[:=]\s*([-\w.]+)s   ^(.*?)$z+^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\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 by accident trigger a request to /favicon.ico which might
         change the application state. -->
    <link rel="shortcut icon"
        href="?__debugger__=yes&amp;cmd=resource&amp;f=console.png">
    <script src="?__debugger__=yes&amp;cmd=resource&amp;f=jquery.js"></script>
    <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">
  <form action="/?__debugger__=yes&amp;cmd=paste" method="post">
    <p>
      <input type="hidden" name="language" value="pytb">
      This is the Copy/Paste friendly version of the traceback.  <span
      class="pastemessage">You can also paste this traceback into
      a <a href="https://gist.github.com/">gist</a>:
      <input type="submit" value="create paste"></span>
    </p>
    <textarea cols="50" rows="10" name="code" readonly>%(plaintext)s</textarea>
  </form>
</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>
Tc             C   s8   t  d d d | r d n d d d d d d |  d	 d i S)Nevalextrueevalex_trustedfalseconsoletitler   secrettraceback_idr   )CONSOLE_HTML)r   r    r   :/tmp/pip-build-5gj8f0j9/Werkzeug/werkzeug/debug/tbtools.pyrender_console_html   s    r   Fc             C   s   t  j   \ } } } |  r7 | t k r7 t | | |  x- t |  D] } | j d k rZ P| j } qD Wt | | |  } | 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)sysexc_infosystem_exceptionsr   r   tb_next	Tracebackfilter_hidden_frames)Zignore_system_exceptionsZshow_hidden_framesskipexc_type	exc_valuetb_r   r   r   get_current_traceback   s    
r(   c               @   sF   e  Z d  Z d Z d Z d d   Z e d d	    Z d
 d   Z d S)LinezHelper for the source renderer.linenocodein_framecurrentc             C   s(   | |  _  | |  _ d |  _ d |  _ d  S)NF)r*   r+   r,   r-   )selfr*   r+   r   r   r   __init__   s    			zLine.__init__c             C   s9   d g } |  j  r | j d  |  j r5 | j d  | S)Nlinezin-framer-   )r,   appendr-   )r.   rvr   r   r   classes   s    			zLine.classesc             C   s2   t  d d j |  j  d |  j d t |  j  i S)Nr3    r*   r+   )SOURCE_LINE_HTMLjoinr3   r*   r   r+   )r.   r   r   r   render   s    	zLine.renderN)r*   r+   r,   r-   )	__name__
__module____qualname____doc__	__slots__r/   propertyr3   r7   r   r   r   r   r)      s
   	r)   c               @   s   e  Z d  Z d Z d d   Z d d   Z e d d    Z e d d	    Z d
 d d  Z	 d d   Z
 d d d  Z d d
 d d d  Z e d d    Z e d d    Z d
 S)r!   zWraps a traceback.c             C   s  | |  _  | |  _ | |  _ | j } | j d k rD | j d | } | |  _ g  |  _ t   } x |  j j t	 | | |   | j
 t |   t r P| j p | j } | d  k s t |  | k r Pt |  } | j } qb W|  j j   d d   |  j D |  _ d  S)Nbuiltins__builtin__
exceptions.c             S   s&   g  |  ] } | j  D] } |  q q Sr   )frames).0groupframer   r   r   
<listcomp>  s   	 z&Traceback.__init__.<locals>.<listcomp>>   r?   r>   r@   )r$   r%   r&   r8   r9   exception_typegroupssetr1   Groupaddidr   	__cause____context__type__traceback__reverserB   )r.   r$   r%   r&   rG   memor   r   r   r/      s*    							zTraceback.__init__c             C   sE   x |  j  D] } | j   q
 Wd 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 Sr   )rB   )rC   rD   rE   r   r   r   rF     s   	 z2Traceback.filter_hidden_frames.<locals>.<listcomp>N)rH   r"   rB   )r.   rD   r   r   r   r"     s    zTraceback.filter_hidden_framesc             C   s   t  |  j t  S)zIs it a syntax error?)
isinstancer%   SyntaxError)r.   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   )rH   	exception)r.   r   r   r   rV     s    zTraceback.exceptionNc             C   sE   | d k r t  j } |  j j   d } | j t | d d   d S)z+Log the ASCII traceback into a file object.N
zutf-8replace)r   stderr	plaintextrstripwriter	   )r.   logfiler&   r   r   r   log  s    	zTraceback.logc             C   s   t  j d d d d d d d |  j i i i  j d  } y d	 d
 l m } Wn" t k
 rq d	 d
 l m } Yn X| d d | } t  j | j	   j
 d   } | j   d | d d | d i S)z'Create a paste and return the paste id.descriptionzWerkzeug Internal Server ErrorpublicFfilesztraceback.txtcontentzutf-8r   )urlopenzhttps://api.github.com/gistsdataurlZhtml_urlrL   )jsondumpsrZ   encodeurllib2rc   ImportErrorurllib.requestloadsreaddecodeclose)r.   rd   rc   r2   respr   r   r   paste  s    	
zTraceback.pasteTc                s  d } d g } |  j  s. | j d  g  } n] t d d   |  j  D  } d | k  oj t |  j   k  n     f d d   |  j D } | r |  j r d	 } n d
 } |  j r d } n d } t d d j |  d | r d | n d d d j |  d | t |  j	  i S)z1Render the traceback for the interactive console. 	tracebackznoframe-tracebackc             s   s   |  ] } | j  Vq d  S)N)
is_library)rC   rE   r   r   r   	<genexpr>8  s    z+Traceback.render_summary.<locals>.<genexpr>r   c                s"   g  |  ] } | j  d      q S)mark_lib)r7   )rC   rD   )rv   r   r   rF   :  s   	 z,Traceback.render_summary.<locals>.<listcomp>zSyntax Errorz+Traceback <em>(most recent call last)</em>:z<pre class=syntaxerror>%s</pre>z<blockquote>%s</blockquote>r3   r4   r   z<h3>%s</h3>rB   rW   r_   )
rB   r1   sumlenrH   rU   SUMMARY_HTMLr6   r   rV   )r.   include_titler   r3   rB   Zlibrary_framesZdescription_wrapperr   )rv   r   render_summary0  s(    			%				zTraceback.render_summaryFc             C   s   t  |  j  } t d | r! d n d d | r3 d n d d d d | d | d t  |  j  d	 |  j d
 d  d t  |  j  d t j d d |  j  d |  j d | i S)z2Render the Full HTML page with the traceback info.r   r   r   r   r   r   rV   rG   summaryrz   FrZ   Zplaintext_csz-{2,}-r   r   )	r   rV   	PAGE_HTMLrG   r{   rZ   resubrL   )r.   r   r   r   excr   r   r   render_fullN  s    	zTraceback.render_fullc             C   s   d j  d d   |  j D  S)NrW   c             S   s   g  |  ] } | j     q Sr   )render_text)rC   rD   r   r   r   rF   a  s   	 z'Traceback.plaintext.<locals>.<listcomp>)r6   rH   )r.   r   r   r   rZ   _  s    zTraceback.plaintextc             C   s
   t  |   S)N)rL   )r.   r   r   r   rL   c  s    zTraceback.id)r8   r9   r:   r;   r/   r"   r=   rU   rV   r^   rq   r{   r   r   rZ   rL   r   r   r   r   r!      s   r!   c               @   s[   e  Z d  Z d Z d d   Z d d   Z e d d    Z d d	 d
  Z d d   Z	 d S)rJ   zA group of frames for an exception in a traceback. On Python 3,
    if the exception has a ``__cause__`` or ``__context__``, there are
    multiple exception groups.
    c             C   s   | |  _  | |  _ d  |  _ t sT | j d  k	 r< d |  _ n | j d  k	 rT d |  _ g  |  _ x5 | d  k	 r |  j j t | | |   | j	 } q` Wd  S)NzCThe above exception was the direct cause of the following exceptionzBDuring handling of the above exception, another exception occurred)
r$   r%   infor   rM   rN   rB   r1   Framer    )r.   r$   r%   r&   r   r   r   r/   n  s    					zGroup.__init__c             C   s  g  } d } x |  j  D] } | j } | d k rO g  } d } | d k r q nW | d k rs d } | d k r q n3 | d k r d } | d k r q n | s | r q | j |  q Wt |  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   )rB   hider1   rx   module)r.   Z
new_frameshiddenrE   r   r   r   r   r"     s0    	(zGroup.filter_hidden_framesc             C   s=   t  j |  j |  j  } d j |  j   } t | d d  S)z'String representation of the exception.rr   zutf-8rX   )rs   format_exception_onlyr$   r%   r6   stripr
   )r.   bufr2   r   r   r   rV     s    zGroup.exceptionTc             C   s   g  } |  j  d  k	 r) | j d |  j   xP |  j D]E } | j d | j  r^ d t | j   n d | j d |  f  q3 Wd j |  S)Nz&<li><div class="exc-divider">%s:</div>z<li%s>%sz title="%s"rr   rv   rW   )r   r1   rB   r   r7   r6   )r.   rv   outrE   r   r   r   r7     s    zGroup.renderc             C   sz   g  } |  j  d  k	 r) | j d |  j   | j d  x$ |  j D] } | j | j    q@ W| j |  j  d j |  S)Nz
%s:
z"Traceback (most recent call last):rW   )r   r1   rB   r   rV   r6   )r.   r   rE   r   r   r   r     s    zGroup.render_textN)
r8   r9   r:   r;   r/   r"   r=   rV   r7   r   r   r   r   r   rJ   h  s    rJ   c               @   s   e  Z d  Z d Z d d   Z d d d  Z e d d    Z d	 d
   Z d d   Z	 d d   Z
 d d d  Z e d d    Z d d d  Z e d d    Z e d d    Z e d d    Z d S)r   zA single frame in a traceback.c             C   se  | j  |  _ | j j j |  _ | j j |  _ | j j |  _	 t
 j |  pW t
 j |  } | d d   d k r | d  d  } t j j |  r t 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	 rXt | d
 d  } | |  _ d  S)N   .pyo.pycr   r8   
__loader__Z__traceback_hide__FZ__traceback_info__zutf-8rX   )r   r   r   )	tb_linenor*   tb_framef_codeco_namefunction_namef_localslocals	f_globalsglobalsinspectZgetsourcefileZgetfileospathisfilerealpathr
   r   filenamegetr   loaderr+   r   r   )r.   r$   r%   r&   fnr   r   r   r   r/     s$    $$zFrame.__init__Tc             C   s_   t  d |  j d t |  j  d |  j d t |  j  d |  j   d | rW |  j rW d n d i S)z%Render a single frame in a traceback.rL   r   r*   r   linesZlibraryrr   )
FRAME_HTMLrL   r   r   r*   r   render_line_contextrt   )r.   rv   r   r   r   r7     s    		zFrame.renderc                s)   t    f d d   t j   j   D  S)Nc             3   s!   |  ] }   j  j |  Vq d  S)N)r   
startswith)rC   r   )r.   r   r   ru     s    z#Frame.is_library.<locals>.<genexpr>)any	sysconfig	get_pathsvalues)r.   r   )r.   r   rt     s    zFrame.is_libraryc             C   s&   d |  j  |  j |  j |  j j   f S)Nz"  File "%s", line %s, in %s
    %s)r   r*   r   current_liner   )r.   r   r   r   r     s
    zFrame.render_textc                s   |  j    \ } } } g      f d d   } x | D] } | | d  q4 W| | d  x | D] } | | d  q_ Wd j    S)Nc                sb   |  j    j   }  |  j   } t |   t |  }   j d | d | t |  pV d f  d  S)Nz7<pre class="line %s"><span class="ws">%s</span>%s</pre>r4   )
expandtabsr[   r   rx   r1   r   )r0   clsZstripped_lineprefix)r2   r   r   render_line  s    z.Frame.render_line_context.<locals>.render_liner   r-   r   rW   )get_context_linesr6   )r.   r   r-   r   r   r0   r   )r2   r   r     s    	zFrame.render_line_contextc             C   s  d d   t  |  j  D } t |  j d  r |  j j d } x1 | d k rq t j | | j  rd P| d 8} qA Wy0 t t j	 d d   | | d  D   } Wn t
 k
 r d } Yn Xx% | | | |  D] } d | _ q Wy d | |  j d _ Wn t k
 rYn X| S)	z:Helper function that returns lines with extra information.c             S   s)   g  |  ] \ } } t  | d  |   q S)r   )r)   )rC   idxxr   r   r   rF     s   	 z-Frame.get_annotated_lines.<locals>.<listcomp>co_firstlinenor   r   c             S   s   g  |  ] } | j  d   q S)rW   )r+   )rC   r   r   r   r   rF     s   	 NT)	enumeratesourcelineshasattrr+   r   _funcdef_rematchrx   r   Zgetblockr   r,   r*   r-   
IndexError)r.   r   r*   offsetr0   r   r   r   get_annotated_lines  s$    0zFrame.get_annotated_linesZsinglec             C   s_   t  | t  rI t r7 t  | t  r7 t | j d  } t | d |  } t | |  j |  j	  S)z*Evaluate code in the context of the frame.zutf-8z<interactive>)
rS   r   r   r   UTF8_COOKIErh   compileevalr   r   )r.   r+   moder   r   r   r   &  s
    z
Frame.evalc              C   s  d } |  j  d k	 r yU t |  j  d  rB |  j  j |  j  } n' t |  j  d  ri |  j  j |  j  } Wn t k
 r~ Yn X| d k r y; t t |  j	 t
    d d  } | j   } Wd QRXWn t k
 r g  SYn Xt | t  r | j   Sd } | j t  r | d d  } nd xa t t j |   D]J \ } } t j | j    } | d k	 rs| j d  } P| d k r6Pq6Wt |  } y t j |  Wn t k
 rd } Yn X| j | d	  j   S)
z6The sourcecode of the file as list of unicode strings.N
get_sourceget_source_by_coder   rbzutf-8   r   rX   )r   r   r   r   r   r+   	Exceptionopenr	   r   r   rm   IOErrorrS   r   
splitlinesr   r   r   _line_refinditer
_coding_researchrD   codecslookupLookupErrorrn   )r.   sourcefcharsetr   r   r   r   r   r   .  sF    	
"zFrame.sourcelines   c             C   sR   |  j  |  j | d |  j d  } |  j  |  j |  j |  } | |  j | f S)Nr   )r   r*   r   )r.   contextr   Zpastr   r   r   r   b  s    %zFrame.get_context_linesc             C   s3   y |  j  |  j d SWn t k
 r. d SYn Xd  S)Nr   rr   )r   r*   r   )r.   r   r   r   r   g  s    zFrame.current_linec             C   s   t  |  j |  j  S)N)r   r   r   )r.   r   r   r   r   n  s    zFrame.consolec             C   s
   t  |   S)N)rL   )r.   r   r   r   rL   r  s    zFrame.idN)r8   r9   r:   r;   r/   r7   r   rt   r   r   r   r   r   r   r=   r   r   rL   r   r   r   r   r     s   4r   )3r;   r   r   rf   r   r   r   r   rs   tokenizer   _compatr   r   r   r   r   r	   r
   
filesystemr   utilsr   r   r   r   r   r   	MULTILINEr   r   r   
SystemExitKeyboardInterruptr   GeneratorExit	NameErrorZHEADERZFOOTERr~   r   ry   r   r5   r   r(   objectr)   r!   rJ   r   r   r   r   r   <module>
   sV    		~[