
Á«‘_Q!  ã               @   s  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 d	 „ Z
 d
 d „  Z d d „  Z e  j d k r¡ d d „  Z nw e rç y d  d l Z Wn e k
 r× d d „  Z YqXd d „  Z n1 d  d l Z Gd d „  d e j ƒ Z d d „  Z d S)é    N)ÚCodeTypeé   )ÚTemplateSyntaxError)ÚPYPY)Úinternal_code)Úmissingc       	      C   st  t  j ƒ  \ } } } t | t ƒ rŠ | j rŠ d | _ |  | _ y | j d ƒ Wn t k
 re Yn Xt | d | j	 p{ d | j
 ƒ } n	 | j } g  } xŸ | d k	 r:| j j t k rÆ | j } qœ | j j j d ƒ } | d k	 r!| j | j ƒ } t | | | j	 | ƒ } | j | ƒ n | j | ƒ | j } qœ Wd } x# t | ƒ D] } t | | ƒ } qNW| | | f S)aå  Rewrite the current exception to replace any tracebacks from
    within compiled template code with tracebacks that look like they
    came from the template source.

    This must be called within an ``except`` block.

    :param exc_info: A :meth:`sys.exc_info` tuple. If not provided,
        the current ``exc_info`` is used.
    :param source: For ``TemplateSyntaxError``, the original source if
        known.
    :return: A :meth:`sys.exc_info` tuple that can be re-raised.
    TNz	<unknown>Z__jinja_template__)ÚsysÚexc_infoÚ
isinstancer   Z
translatedÚsourceÚwith_tracebackÚAttributeErrorÚfake_tracebackÚfilenameÚlinenoÚtb_nextÚtb_frameÚf_coder   Ú	f_globalsÚgetZget_corresponding_linenoÚ	tb_linenoÚappendÚreversedÚtb_set_next)	r   Úexc_typeÚ	exc_valueÚtbÚstackÚtemplater   Zfake_tbr   © r   ú./tmp/pip-build-5gj8f0j9/Jinja2/jinja2/debug.pyÚrewrite_traceback_stack
   s6    		!		r!   c             C   sÕ  | d k	 r1 t  | j j ƒ } | j d d ƒ n i  } d | d | d |  i } t d | d d | d ƒ } yd	 } | d k	 rÉ | j j j } | d
 k r¦ d } n# | j d ƒ rÉ d | d d … } g  }	 x£ d d d d d d d d d d d | f d | f d d d d f D]_ }
 t |
 t	 ƒ r;|	 j
 |
 d ƒ qy |	 j
 t | d |
 ƒ ƒ Wqt k
 rpwYqXqWt |	 Œ  } Wn t k
 r–Yn Xy t | | | ƒ Wn# t k
 rÐt j ƒ  d  j SYn Xd S)!a,  Produce a new traceback object that looks like it came from the
    template source instead of the compiled code. The filename, line
    number, and location name will point to the template, and the local
    variables will be the current template context.

    :param exc_value: The original exception to be re-raised to create
        the new traceback.
    :param tb: The original traceback to get the local variables and
        code info from.
    :param filename: The template filename.
    :param lineno: The line number in the template source.
    NZ__jinja_exception__Ú__name__Ú__file__Ú
r   zraise __jinja_exception__Úexecr   Úrootztop-level template codeZblock_z
block "%s"é   ZargcountZposonlyargcountZkwonlyargcountZnlocalsZ	stacksizeÚflagsÚcodeZconstsÚnamesZvarnamesr   ÚnameZfirstlinenoZlnotabZfreevarsZcellvarsZco_é   )Úget_template_localsr   Úf_localsÚpopÚcompiler   Úco_nameÚ
startswithr
   Útupler   Úgetattrr   r   Ú	Exceptionr%   ÚBaseExceptionr   r	   r   )r   r   r   r   ÚlocalsÚglobalsr)   ÚlocationÚfunctionZ	code_argsÚattrr   r   r    r   L   s`    			r   c       	      C   s7  |  j  d ƒ } | r* | j ƒ  j ƒ  } n i  } i  } x® |  j ƒ  D]  \ } } | j d ƒ sC | t k rn qC y+ | j d d ƒ \ } } } t | ƒ } Wn t k
 r° wC Yn X| j  | d	 ƒ d } | | k  rC | | f | | <qC WxI | j ƒ  D]; \ } \ } } | t k r%| j	 | d ƒ qô | | | <qô W| S)
zlBased on the runtime locals, get the context that would be
    available at that point in the template.
    ÚcontextZl_Ú_r,   r   r   Néÿÿÿÿ)r>   )
r   Úget_allÚcopyÚitemsr2   r   ÚsplitÚintÚ
ValueErrorr/   )	Zreal_localsÚctxÚdataZlocal_overridesr+   Úvaluer=   ÚdepthZ	cur_depthr   r   r    r-   §   s*    r-   é   é   c             C   s   | |  _  |  S)N)r   )r   r   r   r   r    r   Õ   s    	r   c             C   s   |  S)Nr   )r   r   r   r   r    r   à   s    c                s%   ‡  f d d †  } t  j | d |  ƒS)Nc                s0   |  j  d k r& |  j d d k r& ˆ  S|  j ƒ  S)NÚ__getattribute__r   r   )ÚopnameÚargsZdelegate)Úop)r   r   r    Ú
controllerç   s    "ztb_set_next.<locals>.controllerÚobj)ÚtputilZ
make_proxy)r   r   rO   r   )r   r    r   æ   s    c               @   s;   e  Z d  Z d e j e ƒ  j ƒ  f d e j f g Z d S)Ú_CTracebackZPyObject_HEADr   N)	r"   Ú
__module__Ú__qualname__ÚctypesÚc_byteÚobjectÚ
__sizeof__Ú	py_objectÚ_fields_r   r   r   r    rR   õ   s   rR   c             C   s�   t  j t |  ƒ ƒ } |  j d  k	 rU t j |  j ƒ } t j ƒ  | _ t j j | ƒ | d  k	 r‰ t j | ƒ } t j j | ƒ | | _ |  S)N)	rR   Úfrom_addressÚidr   rU   rY   Ú	pythonapiZ	Py_DecRefZ	Py_IncRef)r   r   Zc_tbZ	c_tb_nextr   r   r    r   ý   s    	)rI   rJ   )r   Útypesr   Ú r   Ú_compatr   Úutilsr   r   r!   r   r-   Úversion_infor   rQ   ÚImportErrorrU   Ú	StructurerR   r   r   r   r    Ú<module>   s&   B[,