U aG\ /@sDdZddlZddlZddlmZmZddlmZmZddl m Z ddl m Z m Z mZzddlmZWnek r|dZYnXz edWn.ek rd ZYnek rd ZYnXGd d d eZd dZGdddeZdddZdddZdddZddZddZddZdZ edkr@z eZ Wn YnX[dS) 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) TracebackTypeCodeType)missing internal_code)TemplateSyntaxError) iteritemsreraisePY2)tproxyzraise TypeError, 'foo'zraise __jinja_exception__[1]z4raise __jinja_exception__[0], __jinja_exception__[1]c@s@eZdZdZddZeddZddZedd Zd d Z d S) TracebackFrameProxyzProxies a traceback frame.cCs||_d|_dSN)tb_tb_nextselfr r./usr/lib/python3/dist-packages/jinja2/debug.py__init__'szTracebackFrameProxy.__init__cCs|jSr )rrrrrtb_next+szTracebackFrameProxy.tb_nextcCsBtdk r8zt|j|r|jpdWntk r6YnX||_dSr ) tb_set_nextr Exceptionr)rnextrrrset_next/s zTracebackFrameProxy.set_nextcCsd|jjjkS)N__jinja_template__)r tb_frame f_globalsrrrris_jinja_frame:sz"TracebackFrameProxy.is_jinja_framecCs t|j|Sr )getattrr )rnamerrr __getattr__>szTracebackFrameProxy.__getattr__N) __name__ __module__ __qualname____doc__rpropertyrrrr rrrrr $s   r cs*t|tdkrSfdd}tt|S)Ncs@|dkrt|dS|dkr,j||nt|||SdS)N)__getattribute__r r __setattr__)rr')Z operationargskwargsproxyrroperation_handlerFs z+make_frame_proxy..operation_handler)r r r)framer,rr*rmake_frame_proxyBs  r.c@sPeZdZdZddZdddZddd Zed d Zed d Z eddZ dS)ProcessedTracebackz?Holds a Jinja preprocessed traceback for printing or reraising.cCsR|s td||_||_||_d}|jD]}|dk r>|||}q(|ddS)Nzno frames for this traceback?)AssertionErrorexc_type exc_valueframesr)rr1r2r3Zprev_tbr rrrrSs   zProcessedTraceback.__init__NcCs*tj|j|j|jd|d}d|S)z#Return a string with the traceback.r)limit) tracebackformat_exceptionr1r2r3joinrstrip)rr4linesrrrrender_as_textas  z!ProcessedTraceback.render_as_textFcCs,ddlm}d|||d|ddfS)z)fullzutf-8replace)Zjinja2.debugrendererr<r;decode)rr=r<rrrrender_as_htmlgs   z!ProcessedTraceback.render_as_htmlcCs t|jtS)z*`True` if this is a template syntax error.) isinstancer2rrrrris_template_syntax_errorosz+ProcessedTraceback.is_template_syntax_errorcCs|j|j|jdfS)z;Exception info tuple with a proxy around the frame objects.r)r1r2r3rrrrexc_infotszProcessedTraceback.exc_infocCs*|jd}t|tk r|j}|j|j|fS)z'Standard python exc_info for re-raisingr)r3typerr r1r2rrrrstandard_exc_infoys  z$ProcessedTraceback.standard_exc_info)N)F) r!r"r#r$rr;r@r%rBrCrErrrrr/Ps    r/cCs2|\}}}t|tr$t||}d}nd}t||S)z7Creates a processed traceback object from the exc_info.r)rArtranslate_syntax_errortranslate_exception)rCZ source_hintr1r2r initial_skiprrrmake_tracebacks    rJcCs8||_d|_|j|df}|j}|dkr*d}t|||jS)z4Rewrites a syntax error to please traceback systems.TNz )sourceZ translated __class__filename fake_exc_infolineno)errorrKrCrMrrrrGs rGc Cs|d}g}t|D]}|dk r|j}q|}|dk r|jjtkrH|j}q,|j}|jjd}|dk r||j}t |dd|f|j |d}| t ||}q,|st |d|d|dt|d|d|S)zIf passed an exc_info it will automatically rewrite the exceptions all the way down to the correct line numbers and frames. NrrrF)rangerrf_coderrgetZget_corresponding_lineno tb_linenorNrMappendr.rr/) rCrIr r3xZ initial_tbrtemplaterOrrrrHs0   rHc Cs|d}|r|}ni}i}t|D]x\}}|dr,|tkrHq,z|dd\}}}t|}Wntk r~Yq,YnX||dd}||kr,||f||<q,t|D]*\}\}}|tkr| |dq|||<q|S)NcontextZl__rQ)r) rTZget_allcopyr startswithrsplitint ValueErrorpop) Z real_localsZctxlocalsZlocal_overridesrvaluerZZdepthZ cur_depthrrrget_jinja_localss*    rdc Cs|\}}}|dk r,t|jj}|ddni}|||dddd}td|dt|d}z|dkrnd} n8|jjj} | d krd } n | d rd | d d} nd} t rt d|j |j |j |j|j|j|j|| |j|jdd}n8t d|j|j |j |j |j|j|j|j|| |j|jdd}Wn$tk r>} zW5d} ~ XYnXzt|||Wnt}|dj} YnX|dd| fS)z!Helper for `translate_exception`.N__jinja_exception__rQ)r!__file__rer rFexecrXrootztop-level template codeZblock_z block "%s"rr)rdrf_localsracompile raise_helperrSco_namer]r r co_nlocals co_stacksizeco_flagsco_code co_constsco_names co_varnamesco_firstlineno co_lnotabco_kwonlyargcountrrhsysrCr) rCrMrOr1r2r rbglobalscodelocationZfunctioneZnew_tbrrrrNsr       rNcsddlddlmtr4tjdr,j}q:j}nj}Gdddj }d|fd |fg|_ tt d rGd ddj }d  |fd  |fd|fd |fg|_ Gd dd|d fd |fdjfdjfg_ fdd}|S)zThis 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 rN)rZPy_InitModule4_64c@s eZdZdSz"_init_ugly_crap.._PyObjectNr!r"r#rrrr _PyObject>sr ob_refcntob_typeZ getobjectsc@s eZdZdSr~rrrrrrGsZ_ob_nextZ_ob_prevc@s eZdZdS)z#_init_ugly_crap.._TracebackNrrrrr _TracebackPsrrrtb_lastirUcst|r|dks$t|s$tdt|}|jdk rZt|j}|jd8_|dkrr|_n(t|}|jd7_||_dS)z0Set the tb_next attribute of a traceback object.Nz/tb_set_next arguments must be traceback objectsrF)rA TypeErrorZ from_addressidrrPOINTERZpointer)r robjoldrrctypesrrrYs  z$_init_ugly_crap..tb_set_next) rtypesrr hasattrZ pythonapiZc_int64Zc_intZ c_ssize_tZ StructurerZ_fields_ry)Z _Py_ssize_trrrrr_init_ugly_crap*s4         r)N)N)r)!r$ryr6rrrZ jinja2.utilsrrZjinja2.exceptionsrZjinja2._compatrrr Z__pypy__r ImportErrorrh SyntaxErrorrmrobjectr r.r/rJrGrHrdrNrrrrrrs@     4 ,FD