
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
aG‘\/  ã               @   sD  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 W n ek
r|   dZY nX yedƒ W n. ek
r¢   d	ZY n ek
r¸   d
ZY nX G dd„ deƒZdd„ ZG dd„ deƒZddd„Zddd„Zddd„Zdd„ Zdd„ Zdd„ ZdZ edk�r@y
eƒ Z W n   Y nX [dS ) aQ  
    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.
é    N)ÚTracebackTypeÚCodeType)ÚmissingÚinternal_code)ÚTemplateSyntaxError)Ú	iteritemsÚreraiseÚPY2)Útproxyzraise TypeError, 'foo'zraise __jinja_exception__[1]z4raise __jinja_exception__[0], __jinja_exception__[1]c               @   s@   e Zd ZdZdd„ Zedd„ ƒZdd„ Zedd	„ ƒZd
d„ Z	dS )ÚTracebackFrameProxyzProxies a traceback frame.c             C   s   || _ d | _d S )N)ÚtbÚ_tb_next)Úselfr   © r   ú/usr/lib/python3.6/debug.pyÚ__init__'   s    zTracebackFrameProxy.__init__c             C   s   | j S )N)r   )r   r   r   r   Útb_next+   s    zTracebackFrameProxy.tb_nextc             C   sB   t d k	r8yt | j|r|jpd ƒ W n tk
r6   Y nX || _d S )N)Útb_set_nextr   Ú	Exceptionr   )r   Únextr   r   r   Úset_next/   s    zTracebackFrameProxy.set_nextc             C   s   d| j jjkS )NÚ__jinja_template__)r   Útb_frameÚ	f_globals)r   r   r   r   Úis_jinja_frame:   s    z"TracebackFrameProxy.is_jinja_framec             C   s   t | j|ƒS )N)Úgetattrr   )r   Únamer   r   r   Ú__getattr__>   s    zTracebackFrameProxy.__getattr__N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r   r   r   r   r   r   $   s   r   c                s*   t | ƒ‰ td krˆ S ‡ fdd„}tt|ƒS )Nc                s@   | dkrt ˆ |d ƒS | dkr,ˆ j||Ž nt ˆ | ƒ||ŽS d S )NÚ__getattribute__r   r   Ú__setattr__)r#   r   )r   r$   )Z	operationÚargsÚkwargs)Úproxyr   r   Úoperation_handlerF   s
    z+make_frame_proxy.<locals>.operation_handler)r   r
   r   )Úframer(   r   )r'   r   Úmake_frame_proxyB   s
    r*   c               @   sP   e Zd ZdZdd„ Zddd„Zddd	„Zed
d„ ƒZedd„ ƒZ	edd„ ƒZ
dS )ÚProcessedTracebackz?Holds a Jinja preprocessed traceback for printing or reraising.c             C   sV   |st dƒ‚|| _|| _|| _d }x$| jD ]}|d k	r@|j|ƒ |}q*W |jd ƒ d S )Nzno frames for this traceback?)ÚAssertionErrorÚexc_typeÚ	exc_valueÚframesr   )r   r-   r.   r/   Zprev_tbr   r   r   r   r   S   s    
zProcessedTraceback.__init__Nc             C   s*   t j| j| j| jd |d�}dj|ƒjƒ S )z#Return a string with the traceback.r   )ÚlimitÚ )Ú	tracebackÚformat_exceptionr-   r.   r/   ÚjoinÚrstrip)r   r0   Úlinesr   r   r   Úrender_as_texta   s    z!ProcessedTraceback.render_as_textFc             C   s,   ddl m} d|| |d�| jƒ jddƒf S )z<Return a unicode string with the traceback as rendered HTML.r   )Úrender_tracebackz%s

<!--
%s
-->)Úfullzutf-8Úreplace)Zjinja2.debugrendererr8   r7   Údecode)r   r9   r8   r   r   r   Úrender_as_htmlg   s    
z!ProcessedTraceback.render_as_htmlc             C   s   t | jtƒS )z*`True` if this is a template syntax error.)Ú
isinstancer.   r   )r   r   r   r   Úis_template_syntax_erroro   s    z+ProcessedTraceback.is_template_syntax_errorc             C   s   | j | j| jd fS )z;Exception info tuple with a proxy around the frame objects.r   )r-   r.   r/   )r   r   r   r   Úexc_infot   s    zProcessedTraceback.exc_infoc             C   s*   | j d }t|ƒtk	r|j}| j| j|fS )z'Standard python exc_info for re-raisingr   )r/   Útyper   r   r-   r.   )r   r   r   r   r   Ústandard_exc_infoy   s    
z$ProcessedTraceback.standard_exc_info)N)F)r   r   r    r!   r   r7   r<   r"   r>   r?   rA   r   r   r   r   r+   P   s   

r+   c             C   s2   | \}}}t |tƒr$t||ƒ} d}nd}t| |ƒS )z7Creates a processed traceback object from the exc_info.r   é   )r=   r   Útranslate_syntax_errorÚtranslate_exception)r?   Zsource_hintr-   r.   r   Úinitial_skipr   r   r   Úmake_traceback„   s    


rF   c             C   s8   || _ d| _| j| df}| j}|dkr*d}t||| jƒS )z4Rewrites a syntax error to please traceback systems.TNz	<unknown>)ÚsourceZ
translatedÚ	__class__ÚfilenameÚfake_exc_infoÚlineno)ÚerrorrG   r?   rI   r   r   r   rC   �   s    rC   c       	      C   sÜ   | d }g }xt |ƒD ]}|dk	r|j}qW |}xz|dk	rª|jjtkrN|j}q2|j}|jjjdƒ}|dk	r–|j|jƒ}t	| dd… |f |j
|ƒd }|jt|ƒƒ |}q2W |sÈt| d | d | d ƒ t| d | d |ƒS )z„If passed an exc_info it will automatically rewrite the exceptions
    all the way down to the correct line numbers and frames.
    é   Nr   r   rB   )Úranger   r   Úf_coder   r   ÚgetZget_corresponding_linenoÚ	tb_linenorJ   rI   Úappendr*   r   r+   )	r?   rE   r   r/   ÚxZ
initial_tbr   ÚtemplaterK   r   r   r   rD   š   s*    


rD   c       	      C   sæ   | j dƒ}|r|jƒ jƒ }ni }i }x„t| ƒD ]x\}}|jdƒ s.|tkrLq.y|jddƒ\}}}t|ƒ}W n tk
r€   w.Y nX |j |dƒd }||k r.||f||< q.W x6t|ƒD ]*\}\}}|tkrÖ|j	|d ƒ q´|||< q´W |S )	NÚcontextZl_Ú_rM   rB   r   éÿÿÿÿ)rW   )
rP   Zget_allÚcopyr   Ú
startswithr   ÚsplitÚintÚ
ValueErrorÚpop)	Zreal_localsZctxÚlocalsZlocal_overridesr   ÚvaluerV   ÚdepthZ	cur_depthr   r   r   Úget_jinja_localsÆ   s*    
ra   c             C   s‚  | \}}}|dk	r,t |jjƒ}|jddƒ ni }||| dd… ddœ}td|d  t |dƒ}y¼|dkrnd}	n8|jjj}
|
d	kr†d
}	n |
jdƒr¢d|
dd…  }	nd}	t	ràt
d|j|j|j|j|j|j|j||	|j|jf f ƒ}n8t
d|j|j|j|j|j|j|j|j||	|j|jf f ƒ}W n$ tk
�r> } zW Y dd}~X nX yt|||ƒ W n   tjƒ } | d j}Y nX | dd… |f S )z!Helper for `translate_exception`.NÚ__jinja_exception__rM   )r   Ú__file__rb   r   Ú
rB   ÚexecrT   Úrootztop-level template codeZblock_z
block "%s"é   r   )ra   r   Úf_localsr]   ÚcompileÚraise_helperrO   Úco_namerY   r	   r   Ú
co_nlocalsÚco_stacksizeÚco_flagsÚco_codeÚ	co_constsÚco_namesÚco_varnamesÚco_firstlinenoÚ	co_lnotabÚco_kwonlyargcountr   re   Úsysr?   r   )r?   rI   rK   r-   r.   r   r^   ÚglobalsÚcodeÚlocationZfunctionÚeZnew_tbr   r   r   rJ   ä   sN    





rJ   c                 s  ddl ‰ddlm‰  tr4tˆjdƒr,ˆj} q:ˆj} nˆj} G dd„ dˆj	ƒ}d| fdˆj
|ƒfg|_ttd	ƒr°G d
d„ dˆj	ƒ}dˆj
|ƒfdˆj
|ƒfd| fdˆj
|ƒfg|_G dd„ d|ƒ‰dˆj
ˆƒfdˆj
|ƒfdˆjfdˆjfgˆ_‡ ‡‡fdd„}|S )zñ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
    r   N)r   ZPy_InitModule4_64c               @   s   e Zd ZdS )z"_init_ugly_crap.<locals>._PyObjectN)r   r   r    r   r   r   r   Ú	_PyObject>  s   r{   Ú	ob_refcntÚob_typeZ
getobjectsc               @   s   e Zd ZdS )z"_init_ugly_crap.<locals>._PyObjectN)r   r   r    r   r   r   r   r{   G  s   Z_ob_nextZ_ob_prevc               @   s   e Zd ZdS )z#_init_ugly_crap.<locals>._TracebackN)r   r   r    r   r   r   r   Ú
_TracebackP  s   r~   r   r   Útb_lastirQ   c                sž   t | ˆ ƒo|dkpt |ˆ ƒs$tdƒ‚ˆjt| ƒƒ}| jdk	rZˆjt| jƒƒ}| jd8  _|dkrrˆjˆƒƒ |_n(ˆjt|ƒƒ}| jd7  _ˆj|ƒ|_dS )z0Set the tb_next attribute of a traceback object.Nz/tb_set_next arguments must be traceback objectsrB   )r=   Ú	TypeErrorZfrom_addressÚidr   r|   ÚPOINTERZpointer)r   r   ÚobjÚold)r   r~   Úctypesr   r   r   Y  s    

z$_init_ugly_crap.<locals>.tb_set_next)r…   Útypesr   r	   ÚhasattrZ	pythonapiZc_int64Zc_intZ	c_ssize_tZ	Structurer‚   Z_fields_rv   )Z_Py_ssize_tr{   r   r   )r   r~   r…   r   Ú_init_ugly_crap*  s.    
rˆ   )N)N)r   )!r!   rv   r2   r†   r   r   Zjinja2.utilsr   r   Zjinja2.exceptionsr   Zjinja2._compatr   r   r	   Z__pypy__r
   ÚImportErrorre   ÚSyntaxErrorrj   r€   Úobjectr   r*   r+   rF   rC   rD   ra   rJ   rˆ   r   r   r   r   r   Ú<module>   s@   

4


,FD

