ó
S\›]c           @   s¬  d  Z  d d l Z d d l Z d d l m Z m Z d d l m Z m Z d d l	 m
 Z
 d d l m Z m Z m Z y d d l m Z Wn e k
 r£ d Z n Xy d d UWn- e k
 rÉ d	 Z n e k
 rß d
 Z n Xd e f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d d „ Z d d „ Z d d „ Z d „  Z d „  Z d „  Z d Z  e d k r¥e j! d k rd „  Z  q¥y e ƒ  Z  Wq¥e" k
 r¡q¥Xn  [ d S(   sQ  
    jinja2.debug
    ~~~~~~~~~~~~

    Implements the debug interface for Jinja.  This module does some pretty
    ugly stuff with the Python traceback system in order to achieve tracebacks
    with correct line numbers, locals and contents.

    :copyright: (c) 2017 by the Jinja Team.
    :license: BSD, see LICENSE for more details.
iÿÿÿÿN(   t   TracebackTypet   CodeType(   t   missingt   internal_code(   t   TemplateSyntaxError(   t	   iteritemst   reraiset   PY2(   t   tproxys   raise TypeError, 'foo's   raise __jinja_exception__[1]s4   raise __jinja_exception__[0], __jinja_exception__[1]t   TracebackFrameProxyc           B   sG   e  Z d  Z d „  Z e d „  ƒ Z d „  Z e d „  ƒ Z d „  Z RS(   s   Proxies a traceback frame.c         C   s   | |  _  d  |  _ d  S(   N(   t   tbt   Nonet   _tb_next(   t   selfR
   (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   __init__'   s    	c         C   s   |  j  S(   N(   R   (   R   (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   tb_next+   s    c         C   sS   t  d  k	 rF y# t  |  j | r' | j p* d  ƒ WqF t k
 rB qF Xn  | |  _ d  S(   N(   t   tb_set_nextR   R
   t	   ExceptionR   (   R   t   next(    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   set_next/   s    #c         C   s   d |  j  j j k S(   Nt   __jinja_template__(   R
   t   tb_framet	   f_globals(   R   (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   is_jinja_frame:   s    c         C   s   t  |  j | ƒ S(   N(   t   getattrR
   (   R   t   name(    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   __getattr__>   s    (	   t   __name__t
   __module__t   __doc__R   t   propertyR   R   R   R   (    (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyR	   $   s   		c            s8   t  |  ƒ ‰  t d  k r ˆ  S‡  f d †  } t t | ƒ S(   Nc            sV   |  d k r t  ˆ  | d ƒ S|  d k r< ˆ  j | | Ž  n t  ˆ  |  ƒ | | Ž  Sd  S(   Nt   __getattribute__R   i    t   __setattr__(   R   R   (   R   R    (   t	   operationt   argst   kwargs(   t   proxy(    s+   lib/python2.7/site-packages/jinja2/debug.pyt   operation_handlerF   s
    (   R	   R   R   R    (   t   frameR%   (    (   R$   s+   lib/python2.7/site-packages/jinja2/debug.pyt   make_frame_proxyB   s
    t   ProcessedTracebackc           B   s\   e  Z d  Z d „  Z d d „ Z e d „ Z e d „  ƒ Z	 e d „  ƒ Z
 e d „  ƒ Z RS(   s?   Holds a Jinja preprocessed traceback for printing or reraising.c         C   sz   | s t  d ƒ ‚ | |  _ | |  _ | |  _ d  } x3 |  j D]( } | d  k	 r_ | j | ƒ n  | } q= W| j d  ƒ d  S(   Ns   no frames for this traceback?(   t   AssertionErrort   exc_typet	   exc_valuet   framesR   R   (   R   R*   R+   R,   t   prev_tbR
   (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyR   S   s    			
c         C   s;   t  j |  j |  j |  j d d | ƒ} d j | ƒ j ƒ  S(   s#   Return a string with the traceback.i    t   limitt    (   t	   tracebackt   format_exceptionR*   R+   R,   t   joint   rstrip(   R   R.   t   lines(    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   render_as_texta   s    c         C   s<   d d l  m } d | |  d | ƒ|  j ƒ  j d d ƒ f S(   s<   Return a unicode string with the traceback as rendered HTML.iÿÿÿÿ(   t   render_tracebacku   %s

<!--
%s
-->t   fulls   utf-8t   replace(   t   jinja2.debugrendererR6   R5   t   decode(   R   R7   R6   (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   render_as_htmlg   s    c         C   s   t  |  j t ƒ S(   s*   `True` if this is a template syntax error.(   t
   isinstanceR+   R   (   R   (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   is_template_syntax_erroro   s    c         C   s   |  j  |  j |  j d f S(   s;   Exception info tuple with a proxy around the frame objects.i    (   R*   R+   R,   (   R   (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   exc_infot   s    c         C   s>   |  j  d } t | ƒ t k	 r+ | j } n  |  j |  j | f S(   s'   Standard python exc_info for re-raisingi    (   R,   t   typeR    R
   R*   R+   (   R   R
   (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   standard_exc_infoy   s    N(   R   R   R   R   R   R5   t   FalseR;   R   R=   R>   R@   (    (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyR(   P   s   	c         C   sI   |  \ } } } t  | t ƒ r6 t | | ƒ }  d } n d } t |  | ƒ S(   s7   Creates a processed traceback object from the exc_info.i    i   (   R<   R   t   translate_syntax_errort   translate_exception(   R>   t   source_hintR*   R+   R
   t   initial_skip(    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   make_traceback„   s    	c         C   sU   | |  _  t |  _ |  j |  d f } |  j } | d k rB d } n  t | | |  j ƒ S(   s4   Rewrites a syntax error to please traceback systems.s	   <unknown>N(   t   sourcet   Truet
   translatedt	   __class__R   t   filenamet   fake_exc_infot   lineno(   t   errorRG   R>   RK   (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyRB      s    				i    c   	      C   s2  |  d } g  } x, t  | ƒ D] } | d k	 r | j } q q W| } x­ | d k	 rô | j j t k ru | j } qH n  | j } | j j j d ƒ } | d k	 rØ | j | j	 ƒ } t
 |  d  | f | j | ƒ d } n  | j t | ƒ ƒ | } qH W| st |  d |  d |  d ƒ n  t |  d |  d | ƒ S(   s„   If passed an exc_info it will automatically rewrite the exceptions
    all the way down to the correct line numbers and frames.
    i   R   i    i   N(   t   rangeR   R   R   t   f_codeR   R   t   gett   get_corresponding_linenot	   tb_linenoRL   RK   t   appendR'   R   R(   (	   R>   RE   R
   R,   t   xt
   initial_tbR   t   templateRM   (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyRC   š   s*    
		
c   	      C   s<  |  j  d ƒ } | r* | j ƒ  j ƒ  } n i  } i  } x³ t |  ƒ D]¥ \ } } | j d ƒ sC | t k rq qC n  y+ | j d d ƒ \ } } } t | ƒ } Wn t k
 r² qC n X| j  | d ƒ d } | | k  rC | | f | | <qC qC WxI t | ƒ D]; \ } \ } } | t k r*| j	 | d  ƒ qù | | | <qù W| S(   Nt   contextt   l_t   _i   iÿÿÿÿi    (   iÿÿÿÿ(   RQ   t   get_allt   copyR   t
   startswithR   t   splitt   intt
   ValueErrort   popR   (	   t   real_localst   ctxt   localst   local_overridesR   t   valueRZ   t   deptht	   cur_depth(    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   get_jinja_localsÆ   s*    c         B   sî  |  \ } } } | d k	 r@ e | j j ƒ } | j d d ƒ n i  } i | d 6| d 6|  d  d 6d d 6} e d | d e | d ƒ } y| d k r¢ d	 }	 nJ | j j j }
 |
 d
 k rÆ d }	 n& |
 j	 d ƒ ræ d |
 d }	 n d	 }	 e
 rCe d | j | j | j | j | j | j | j | |	 | j | j d d ƒ } nT e d | j | j | j | j | j | j | j | j | |	 | j | j d d ƒ } Wn e k
 r­} n Xy | | | UWn  e j ƒ  }  |  d j } n X|  d  | f S(   s!   Helper for `translate_exception`.t   __jinja_exception__R   t   __file__i   R   s   
i   t   execRW   t   roots   top-level template codet   block_s
   block "%s"i   i    N(    (    (    (    (   R   Ri   R   t   f_localsRa   t   compilet   raise_helperRP   t   co_nameR]   R   R   t
   co_nlocalst   co_stacksizet   co_flagst   co_codet	   co_constst   co_namest   co_varnamest   co_firstlinenot	   co_lnotabt   co_kwonlyargcountR   t   sysR>   R   (   R>   RK   RM   R*   R+   R
   Rd   t   globalst   codet   locationt   functiont   et   new_tb(    (    s+   lib/python2.7/site-packages/jinja2/debug.pyRL   ä   sP    
				c             sy  d d l  ‰ d d l m ‰  t rL t ˆ j d ƒ r@ ˆ j }  qU ˆ j }  n	 ˆ j }  d ˆ j	 f d „  ƒ  Y} d |  f d ˆ j
 | ƒ f g | _ t t d	 ƒ rd ˆ j	 f d
 „  ƒ  Y} d ˆ j
 | ƒ f d ˆ j
 | ƒ f d |  f d ˆ j
 | ƒ f g | _ n  d | f d „  ƒ  Y‰ d ˆ j
 ˆ ƒ f d ˆ j
 | ƒ f d ˆ j f d ˆ j f g ˆ _ ‡  ‡ ‡ f d †  } | S(   sñ   This function implements a few ugly things so that we can patch the
    traceback objects.  The function returned allows resetting `tb_next` on
    any python traceback object.  Do not attempt to use this on non cpython
    interpreters
    iÿÿÿÿN(   R    t   Py_InitModule4_64t	   _PyObjectc           B   s   e  Z RS(    (   R   R   (    (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyR…   >  s   t	   ob_refcntt   ob_typet
   getobjectsc           B   s   e  Z RS(    (   R   R   (    (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyR…   G  s   t   _ob_nextt   _ob_prevt
   _Tracebackc           B   s   e  Z RS(    (   R   R   (    (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyR‹   P  s   R   R   t   tb_lastiRS   c            så   t  |  ˆ  ƒ o' | d k p' t  | ˆ  ƒ s9 t d ƒ ‚ n  ˆ j t |  ƒ ƒ } |  j d k	 r‡ ˆ j t |  j ƒ ƒ } | j d 8_ n  | d k r« ˆ j ˆ ƒ ƒ  | _ n6 ˆ j t | ƒ ƒ } | j d 7_ ˆ j | ƒ | _ d S(   s0   Set the tb_next attribute of a traceback object.s/   tb_set_next arguments must be traceback objectsi   N(	   R<   R   t	   TypeErrort   from_addresst   idR   R†   t   POINTERt   pointer(   R
   R   t   objt   old(   R    R‹   t   ctypes(    s+   lib/python2.7/site-packages/jinja2/debug.pyR   Y  s    (   R”   t   typesR    R   t   hasattrt	   pythonapit   c_int64t   c_intt	   c_ssize_tt	   StructureR   t   _fields_R}   (   t   _Py_ssize_tR…   R   (    (   R    R‹   R”   s+   lib/python2.7/site-packages/jinja2/debug.pyt   _init_ugly_crap*  s.    			i   i   c         C   s   | |  _  d  S(   N(   R   (   R
   R   (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyR   r  s    (   i   i   (#   R   R}   R0   R•   R    R   t   jinja2.utilsR   R   t   jinja2.exceptionsR   t   jinja2._compatR   R   R   t   __pypy__R   t   ImportErrorR   t   SyntaxErrorRq   R   t   objectR	   R'   R(   RF   RB   RC   Ri   RL   Rž   R   t   version_infoR   (    (    (    s+   lib/python2.7/site-packages/jinja2/debug.pyt   <module>   sD   
	
	4,		F	D