
_                @   s  d  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 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% d d d d d d  d! d" d# d$ d% d& d' d' d( d) i Z& e' e( d*  rd* Z) n d+ Z) d, g Z* y e+ d-  e* j, d.  Wn e- k
 rYn Xy e+ d/  Wn e- k
 r;d0 Z. Yn Xd1 Z. d2 d3   Z/ d4 d0 d1 d5 d6  Z0 d7 d8   Z1 d9 d:   Z2 Gd; d<   d< e3  Z4 Gd= d>   d> e3  Z5 Gd? d@   d@ e6  Z7 GdA dB   dB e%  Z8 GdC dD   dD e%  Z9 GdE dF   dF e:  Z; GdG dH   dH e%  Z< d4 S)Iz0Compiles nodes from the parser into Python code.    )
namedtuple)update_wrapper)chain)	iskeyword)escape)Markup   )nodes)imap)	iteritems)izip)NativeStringIO)
range_type)string_types)	text_type)TemplateAssertionError)Symbols)VAR_LOAD_ALIAS)VAR_LOAD_PARAMETER)VAR_LOAD_RESOLVE)VAR_LOAD_UNDEFINED)EvalContext)	Optimizer)concat)NodeVisitoreqz==nez!=gt>Zgteqz>=lt<Zlteqz<=innotinznot inr   itemsdivisionz%from __future__ import generator_stopgenerator_stopzdef f(): yield from x()FTc                s     f d d   } t  |    S)Nc                s]   |  j  rJ | j j rJ |  j j | | j  } | | k rJ |  j | |  S  |  | | |  S)N)	optimizedeval_ctxvolatile	optimizervisit)selfnodeframekwargsZnew_node)f 1/tmp/pip-build-5gj8f0j9/Jinja2/jinja2/compiler.pynew_funcC   s
    zoptimizeconst.<locals>.new_func)r   )r/   r2   r0   )r/   r1   optimizeconstB   s    r3   Nc             C   sf   t  |  t j  s t d   | j | | | | | |  } | j |   | d k rb | j j   Sd S)z+Generate the python source for a node tree.z Can't compile non template nodesN)
isinstancer	   Template	TypeErrorZcode_generator_classr*   streamgetvalue)r,   environmentnamefilenamer7   
defer_initr&   	generatorr0   r0   r1   generateN   s    r>   c             C   s   |  d k s$ |  t  k s$ |  t k r( d St |   t t t t t t f t	 k rT d St |   t
 t t t f k r x |  D] } t |  sy d Sqy Wd St |   t k r x: t |   D], \ } }  t |  s d St |   s d Sq Wd Sd S)z)Does the node have a safe representation?NTF)NotImplementedEllipsistypeboolintfloatcomplexr   r   r   tuplelistset	frozensethas_safe_reprdictr   )valueitemkeyr0   r0   r1   rJ   \   s"    $(rJ   c             C   sJ   t  |  } y" x |  D] } | j |  q WWn t k
 rB Yn X| j S)zCheck if the names passed are accessed undeclared.  The return value
    is a set of all the undeclared names from the sequence of names found.
    )UndeclaredNameVisitorr*   VisitorExit
undeclared)r	   namesvisitorr,   r0   r0   r1   find_undeclaredq   s    rT   c               @   s   e  Z d  Z d d   Z d S)MacroRefc             C   s(   | |  _  d |  _ d |  _ d |  _ d  S)NF)r,   accesses_calleraccesses_kwargsaccesses_varargs)r+   r,   r0   r0   r1   __init__   s    			zMacroRef.__init__N)__name__
__module____qualname__rY   r0   r0   r0   r1   rU   ~   s   rU   c               @   sU   e  Z d  Z d Z d d d d  Z d d   Z d d d	  Z d
 d   Z e Z d S)Framez&Holds compile time information for us.Nc             C   s   | |  _  t | r | j p d  d | |  _ d |  _ d |  _ | oK | j |  _ d  |  _ | ri | j pl d  |  _ | |  _ | d  k	 r | j |  _ d  S)NlevelF)	r'   r   symbolstoplevel	rootlevelrequire_output_checkbufferblockparent)r+   r'   re   r^   r0   r0   r1   rY      s    	$				zFrame.__init__c             C   s;   t  j |  j  } | j j |  j  |  j j   | _ | S)z!Create a copy of the current one.)object__new__	__class____dict__updater_   copy)r+   rvr0   r0   r1   rk      s    z
Frame.copyFc             C   s3   | r# t  |  j d |  j j d St  |  j |   S)zReturn an inner frame.r^   r   )r]   r'   r_   r^   )r+   isolatedr0   r0   r1   inner   s    zFrame.innerc             C   s   |  j    } d | _ | S)a  Return a soft frame.  A soft frame may not be modified as
        standalone thing as it shares the resources with the frame it
        was created of, but it's not a rootlevel frame any longer.

        This is only used to implement if-statements.
        F)rk   ra   )r+   rl   r0   r0   r1   soft   s    	z
Frame.soft)	rZ   r[   r\   __doc__rY   rk   rn   ro   __copy__r0   r0   r0   r1   r]      s   r]   c               @   s   e  Z d  Z d Z d S)rP   z?Exception used by the `UndeclaredNameVisitor` to signal a stop.N)rZ   r[   r\   rp   r0   r0   r0   r1   rP      s   rP   c               @   sF   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 S)DependencyFinderVisitorz.A visitor that collects filter and test calls.c             C   s   t    |  _ t    |  _ d  S)N)rH   filterstests)r+   r0   r0   r1   rY      s    z DependencyFinderVisitor.__init__c             C   s$   |  j  |  |  j j | j  d  S)N)generic_visitrs   addr:   )r+   r,   r0   r0   r1   visit_Filter   s    z$DependencyFinderVisitor.visit_Filterc             C   s$   |  j  |  |  j j | j  d  S)N)ru   rt   rv   r:   )r+   r,   r0   r0   r1   
visit_Test   s    z"DependencyFinderVisitor.visit_Testc             C   s   d S)zStop visiting at blocks.Nr0   )r+   r,   r0   r0   r1   visit_Block   s    z#DependencyFinderVisitor.visit_BlockN)rZ   r[   r\   rp   rY   rw   rx   ry   r0   r0   r0   r1   rr      s
   rr   c               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	rO   zA visitor that checks if a name is accessed without being
    declared.  This is different from the frame visitor as it will
    not stop at closure frames.
    c             C   s   t  |  |  _ t    |  _ d  S)N)rH   rR   rQ   )r+   rR   r0   r0   r1   rY      s    zUndeclaredNameVisitor.__init__c             C   si   | j  d k rR | j |  j k rR |  j j | j  |  j |  j k re t    n |  j j | j  d  S)Nload)ctxr:   rR   rQ   rv   rP   discard)r+   r,   r0   r0   r1   
visit_Name   s
    !z UndeclaredNameVisitor.visit_Namec             C   s   d S)zStop visiting a blocks.Nr0   )r+   r,   r0   r0   r1   ry      s    z!UndeclaredNameVisitor.visit_BlockN)rZ   r[   r\   rp   rY   r}   ry   r0   r0   r0   r1   rO      s   rO   c               @   s   e  Z d  Z d Z d S)CompilerExitzRaised if the compiler encountered a situation where it just
    doesn't make sense to further process the code.  Any block that
    raises such an exception is not further processed.
    N)rZ   r[   r\   rp   r0   r0   r0   r1   r~      s   r~   c               @   sn  e  Z d  Z d d d d d  Z d d   Z d d	   Z d
 d   Z d d d  Z d d   Z d d d  Z	 d d d  Z
 d d   Z d d d  Z d d   Z d d   Z d d d d  Z d d d  d!  Z d d" d#  Z d$ d%   Z d& d'   Z d d( d)  Z d* d+   Z d, d-   Z d. d/   Z d0 d1   Z d2 d3   Z d4 d5   Z d6 d7   Z d8 d9   Z d: d;   Z d< d=   Z d> d?   Z d@ dA   Z  dB dC   Z! dD dE   Z" dF dG   Z# dH dI   Z$ dJ dK   Z% d dL dM  Z& dN dO   Z' dP dQ   Z( dR dS   Z) dT dU   Z* dV dW   Z+ dX dY   Z, dZ d[   Z- d\ d]   Z. d^ d_   Z/ d` da   Z0 db dc   Z1 dd de   Z2 e3 df d  Z4 e5 Z6 d Z7 di dj   Z8 dk dl   Z9 dm dn   Z: do dp   Z; dq dr   Z< ds dt   Z= du dv   Z> dw dx   Z? dy dz   Z@ d{ d|   ZA d} d~   ZB d d   ZC d d   ZD d d   ZE d d   ZF d d d  ZG d d d  ZH eG d  ZI eG d  ZJ eG d  ZK eG d  ZL eG d  ZM eG d  ZN eG d  ZO eG d d d ZP eG d d d ZQ eH d  ZR eH d  ZS eH d d d ZT [G [H eU d d    ZV eU d d    ZW d d   ZX eU d d    ZY eU d d    ZZ d d   Z[ eU d d    Z\ eU d d    Z] eU d d    Z^ eU d d d   Z_ d d   Z` d d   Za d d   Zb d d   Zc d d   Zd d d   Ze d d   Zf d d   Zg d d   Zh d d   Zi d d   Zj d d   Zk d d   Zl d d   Zm d d   Zn d S)CodeGeneratorNFTc             C   s	  | d  k r t    } | |  _ | |  _ | |  _ | |  _ d |  _ | |  _ | |  _ | ri t |  |  _	 i  |  _
 i  |  _ d |  _ d |  _ d |  _ i  |  _ i  |  _ g  |  _ d  |  _ d |  _ d |  _ d |  _ d |  _ d |  _ g  |  _ g  |  _ d g |  _ d  S)NFr   r   Tcontext)r   r9   r:   r;   r7   Zcreated_block_contextr<   r&   r   r)   import_aliasesblocksextends_so_farhas_known_extendscode_linenort   rs   
debug_info_write_debug_info
_new_lines
_last_line_first_write_last_identifier_indentation_assign_stack_param_def_block_context_reference_stack)r+   r9   r:   r;   r7   r<   r&   r0   r0   r1   rY      s8    																								zCodeGenerator.__init__c             C   s   t  | | |  j |  j   d S)z*Fail with a :exc:`TemplateAssertionError`.N)r   r:   r;   )r+   msglinenor0   r0   r1   fail<  s    zCodeGenerator.failc             C   s   |  j  d 7_  d |  j  S)zGet a new unique identifier.r   zt_%d)r   )r+   r0   r0   r1   temporary_identifier@  s    z"CodeGenerator.temporary_identifierc             C   s'   |  j    | _ |  j d | j  d S)z7Enable buffering for the frame from that point onwards.z%s = []N)r   rc   	writeline)r+   r-   r0   r0   r1   rc   E  s    zCodeGenerator.bufferc             C   s   | s | j  j r |  j d  |  j   |  j d | j  |  j   |  j d  |  j   |  j d | j  |  j   d S| j  j r |  j d | j  d S|  j d | j  d S)z(Return the buffer contents of the frame.zif context.eval_ctx.autoescape:zreturn Markup(concat(%s))zelse:zreturn concat(%s)N)r'   r(   r   indentrc   outdent
autoescape)r+   r-   force_unescapedr0   r0   r1   return_buffer_contentsJ  s    



z$CodeGenerator.return_buffer_contentsc             C   s   |  j  d 7_  d S)zIndent by one.r   N)r   )r+   r0   r0   r1   r   \  s    zCodeGenerator.indentr   c             C   s   |  j  | 8_  d S)zOutdent by step.N)r   )r+   stepr0   r0   r1   r   `  s    zCodeGenerator.outdentc             C   s=   | j  d k r" |  j d |  n |  j d | j  |  d S)z%Yield or write into the frame buffer.Nzyield z
%s.append()rc   r   )r+   r-   r,   r0   r0   r1   start_writed  s    zCodeGenerator.start_writec             C   s    | j  d k	 r |  j d  d S)z1End the writing process started by `start_write`.N))rc   write)r+   r-   r0   r0   r1   	end_writek  s    zCodeGenerator.end_writec             C   s.   |  j  | |  |  j |  |  j |  d S)z4Simple shortcut for start_write + write + end_write.N)r   r   r   )r+   sr-   r,   r0   r0   r1   simple_writep  s    zCodeGenerator.simple_writec             C   sK   y2 |  j  d  x | D] } |  j | |  q WWn t k
 rF Yn Xd S)zVisit a list of nodes as block in a frame.  If the current frame
        is no buffer a dummy ``if 0: yield None`` is written automatically.
        passN)r   r*   r~   )r+   r	   r-   r,   r0   r0   r1   
blockvisitv  s    zCodeGenerator.blockvisitc             C   s   |  j  r |  j so |  j j d |  j   |  j |  j  7_ |  j d k	 ro |  j j |  j |  j f  d |  _ d |  _ |  j j d |  j  d |  _  |  j j |  d S)z&Write a string into the output stream.
NFz    r   )	r   r   r7   r   r   r   r   appendr   )r+   xr0   r0   r1   r     s    					zCodeGenerator.writer   c             C   s!   |  j  | |  |  j |  d S)z!Combination of newline and write.N)newliner   )r+   r   r,   extrar0   r0   r1   r     s    zCodeGenerator.writelinec             C   sS   t  |  j d |  |  _ | d k	 rO | j |  j k rO | j |  _ | j |  _ d S)z/Add one or more newlines before the next write.r   N)maxr   r   r   r   )r+   r,   r   r0   r0   r1   r     s    zCodeGenerator.newlinec       	      C   sV  d } x@ t  d d   | j D | p( f   D] } t |  r, d } Pq, Wx. | j D]# } |  j d  |  j | |  qS W| s x. | j D]# } |  j d  |  j | |  q W| d k	 r x1 t |  D]# \ } } |  j d | | f  q W| j r|  j d  |  j | j |  | r#| j d k	 r?|  j d	  n |  j d
  xE | j D]: } |  j d | j	  |  j | j
 |  |  j d  qVW| d k	 rx1 t |  D]# \ } } |  j d | | f  qW| j d k	 r|  j d  |  j | j |  |  j d  qR|  j d  n/ | j d k	 rR|  j d  |  j | j |  d S)a+  Writes a function call to the stream for the current node.
        A leading comma is added automatically.  The extra keyword
        arguments may not include python keywords otherwise a syntax
        error could occur.  The extra keyword arguments should be given
        as python dict.
        Fc             s   s   |  ] } | j  Vq d  S)N)rN   ).0r   r0   r0   r1   	<genexpr>  s    z*CodeGenerator.signature.<locals>.<genexpr>Tz, Nz, %s=%sz, *z
, **dict({z, **{z%r: z%r: %s, z}, **r   }z, **)r   r.   is_python_keywordargsr   r*   r   Zdyn_argsZ
dyn_kwargsrN   rL   )	r+   r,   r-   extra_kwargsZkwarg_workaroundZkwargargrN   rL   r0   r0   r1   	signature  sJ    	,	zCodeGenerator.signaturec             C   s   t    } x | D] } | j |  q Wxq d D]i } t |  |  } xQ t | |  D]@ } | | k ru |  j   | | <|  j d | | | | f  qS Wq. Wd S)zPull all the dependencies.rs   rt   z%s = environment.%s[%r]N)rs   rt   )rr   r*   getattrr   r   )r+   r	   rS   r,   
dependencymappingr:   r0   r0   r1   pull_dependencies  s    	zCodeGenerator.pull_dependenciesc             C   s   g  } x t  | j j  D] \ } \ } } | t k r: q | t k ri |  j d | |  j   | f  q | t k r |  j d | | f  q | t k r | j	 |  q t
 d   q W| r |  j d d j |   d  S)Nz%s = %s(%r)z%s = %szunknown load instructionz%s = missingz = )r   r_   loadsr   r   r   get_resolve_funcr   r   r   NotImplementedErrorjoin)r+   r-   undefstargetactionparamr0   r0   r1   enter_frame  s    %#zCodeGenerator.enter_framec             C   s`   | s\ g  } x- t  | j j  D] \ } } | j |  q W| r\ |  j d d j |   d  S)Nz%s = missingz = )r   r_   r   r   r   r   )r+   r-   with_python_scoper   r   _r0   r0   r1   leave_frame  s    zCodeGenerator.leave_framec             C   s   |  j  j r d | Sd | S)Nzasync def %szdef %s)r9   is_async)r+   r:   r0   r0   r1   func  s    zCodeGenerator.funcc             C   s`  | j    } | j j |  t |  } d } t   } g  } xm t | j  D]\ \ } } | j d k rn | } | j d k r | j | j  | j	 | j j
 | j   qM Wt | j d  }	 d |	 k r@| d k	 ry | j | t | j  Wq7t k
 r|  j d | j  Yq7Xn | j	 | j j d   d | _ d |	 k rzd | k rz| j	 | j j d   d | _ d |	 k rd | k r| j	 | j j d   d | _ d | _ | j j |  |  j d |  j d	  d
 j |  f |  |  j   |  j |  |  j |  |  j |  x t | j  D] \ } } | j j
 | j  }
 |  j d |
  |  j   y | j | t | j  } Wn6 t k
 r|  j d |
 d | j | j f  Yn" X|  j d |
  |  j | |  |  j |
  |  j    q:W|  j!   |  j" | j |  |  j# | d d |  j$ | d d |  j    | | f S)z/Dump the function def of a macro or call block.Ncallerr.   varargszhWhen defining macros or call blocks the special "caller" argument must be omitted or be given a default.TFz%s(%s):macroz, zif %s is missing:z%s = undefined(%r, name=%r)zparameter %r was not providedz%s = r   r   )r.   r   )r   r.   r   )%rn   r_   analyze_noderU   rH   	enumerater   r:   rv   r   refrT   bodydefaultslen
IndexErrorr   r   declare_parameterrV   rW   rX   rb   r   r   r   r   rc   r   push_parameter_definitionsr*   mark_parameter_storedr   pop_parameter_definitionsr   r   r   )r+   r,   r-   	macro_refZexplicit_callerZskip_special_paramsr   idxr   rQ   r   defaultr0   r0   r1   
macro_body  sp    	 				,

 

zCodeGenerator.macro_bodyc             C   s   d j  d d   | j j D  } t | j d d  } t | j j  d k rY | d 7} |  j d | | | j | j | j f  d S)	z<Dump the macro definition for the def created by macro_body.z, c             s   s   |  ] } t  | j  Vq d  S)N)reprr:   )r   r   r0   r0   r1   r   M  s    z*CodeGenerator.macro_def.<locals>.<genexpr>r:   Nr   ,zLMacro(environment, macro, %r, (%s), %r, %r, %r, context.eval_ctx.autoescape))	r   r,   r   r   r   r   rW   rX   rV   )r+   r   r-   Z	arg_tupler:   r0   r0   r1   	macro_defK  s    "
zCodeGenerator.macro_defc             C   s7   d | j  } |  j d k	 r3 | d t |  j  7} | S)z.Return a human readable position for the node.zline %dNz in )r   r:   r   )r+   r,   rl   r0   r0   r1   position]  s    zCodeGenerator.positionc             C   s-   d d j  d d   t | j j    D  S)Nz{%s}z, c             s   s%   |  ] \ } } d  | | f Vq d S)z%r: %sNr0   )r   r:   r   r0   r0   r1   r   f  s   z3CodeGenerator.dump_local_context.<locals>.<genexpr>)r   r   r_   Zdump_stores)r+   r-   r0   r0   r1   dump_local_contextd  s    		z CodeGenerator.dump_local_contextc             C   s8   |  j  d  |  j  d  |  j  d  |  j  d  d S)zWrites a common preamble that is used by root and block functions.
        Primarily this sets up common local helpers and enforces a generator
        through a dead branch.
        z$resolve = context.resolve_or_missingz!undefined = environment.undefinedzcond_expr_undefined = Undefinedzif 0: yield NoneN)r   )r+   r0   r0   r1   write_commonsj  s    zCodeGenerator.write_commonsc             C   s   |  j  j | j j    d S)aQ  Pushes all parameter targets from the given frame into a local
        stack that permits tracking of yet to be assigned parameters.  In
        particular this enables the optimization from `visit_Name` to skip
        undefined expressions for parameters in macros as macros can reference
        otherwise unbound parameters.
        N)r   r   r_   Zdump_param_targets)r+   r-   r0   r0   r1   r   v  s    z(CodeGenerator.push_parameter_definitionsc             C   s   |  j  j   d S)z+Pops the current parameter definitions set.N)r   pop)r+   r0   r0   r1   r     s    z'CodeGenerator.pop_parameter_definitionsc             C   s!   |  j  r |  j  d j |  d S)zMarks a parameter in the current parameter definitions as stored.
        This will skip the enforced undefined checks.
        r   N)r   r|   )r+   r   r0   r0   r1   r     s    	z#CodeGenerator.mark_parameter_storedc             C   s   |  j  j |  d  S)N)r   r   )r+   r   r0   r0   r1   push_context_reference  s    z$CodeGenerator.push_context_referencec             C   s   |  j  j   d  S)N)r   r   )r+   r0   r0   r1   pop_context_reference  s    z#CodeGenerator.pop_context_referencec             C   s   |  j  d S)Nr   r   )r   )r+   r0   r0   r1   get_context_ref  s    zCodeGenerator.get_context_refc             C   s%   |  j  d } | d k r d Sd | S)Nr   r   resolvez
%s.resolver   )r   )r+   r   r0   r0   r1   r     s    zCodeGenerator.get_resolve_funcc             C   s   d |  j    |  j |  f S)Nz%s.derived(%s))r   r   )r+   r-   r0   r0   r1   derive_context  s    	zCodeGenerator.derive_contextc             C   s   |  j  s d S| |  j  d k S)z4Checks if a given target is an undeclared parameter.Fr   r   )r   )r+   r   r0   r0   r1   parameter_is_undeclared  s    	z%CodeGenerator.parameter_is_undeclaredc             C   s   |  j  j t    d S)z+Pushes a new layer for assignment tracking.N)r   r   rH   )r+   r0   r0   r1   push_assign_tracking  s    z"CodeGenerator.push_assign_trackingc             C   sQ  |  j  j   } | j s  | r$ d Sd d   | D } t |  d k r t t |   } | j j |  } |  j d | | f  ns |  j d  xV t	 |  D]H \ } } | r |  j
 d  | j j |  } |  j
 d | | f  q W|  j
 d	  | rMt |  d k r*|  j d
 | d  n# |  j d d j t t |    d S)zoPops the topmost level for assignment tracking and updates the
        context variables if necessary.
        Nc             S   s,   g  |  ]" } | d  d  d k r |  q S)Nr   r   r0   )r   r   r0   r0   r1   
<listcomp>  s   	 z5CodeGenerator.pop_assign_tracking.<locals>.<listcomp>r   zcontext.vars[%r] = %szcontext.vars.update({z, z%r: %sz})zcontext.exported_vars.add(%r)r   z"context.exported_vars.update((%s)))r   r   r`   r   nextiterr_   r   r   r   r   r   r
   r   )r+   r-   varsZpublic_namesr:   r   r   r0   r0   r1   pop_assign_tracking  s*    z!CodeGenerator.pop_assign_trackingc             C   s3  | d  k s t  d   t |  j |  j  } d d l m } |  j d d j t   |  j d d j |   |  j j	 r |  j d  |  j
 r d p d	 } | j t j  d  k	 } xV | j t j  D]B } | j |  j k r |  j d
 | j | j  | |  j | j <q Wx | j t j  D] } | j |  j k r$| j }	 |  j   |  j |	 <}
 d |	 k r|	 j d d  \ } } |  j d | | |
 f  q$|  j d |	 |
 f  q$W|  j d |  j  |  j d |  j d  | f d d |  j   |  j   t |  } d t | j d%  k rM| j j d  } |  j d |  | j j  |  d | _! | _" | oz|  j# | _$ | r|  j d  |  j% |  |  j& | j  |  j' | j |  |  j( | d d |  j)   | r|  j# s|  j   |  j d  |  j   t* r3|  j j	 r3|  j d  nD |  j d |  j j	 rNd pQd	  |  j   |  j d  |  j)   |  j) d |  j#  xFt+ |  j  D]5\ } } |  j d |  j d |  | f | d  |  j   |  j   t |  } t | j d&  } d | k r3| j j d  } |  j d |  d | k rk| j j d  } |  j d | | | f  | j j  |  | | _, |  j% |  |  j& | j  |  j' | j |  |  j( | d d |  j)   qW|  j d d j d  d!   |  j D  d d |  j d" d# j d$ d!   |  j- D   d  S)'Nzno root frame allowedr   )exportedzfrom __future__ import %sz, zfrom jinja2.runtime import zHfrom jinja2.asyncsupport import auto_await, auto_aiter, AsyncLoopContextz, environment=environment zblock %r defined twice.zfrom %s import %s as %szimport %s as %sz	name = %rz%s(context, missing=missing%s):rootr   r+   z%s = TemplateReference(context)Tzparent_template = Noner   zif parent_template is not None:z4yield from parent_template.root_render_func(context)z9%sfor event in parent_template.root_render_func(context):zasync zyield eventZblock_superz %s = context.super(%r, block_%s)zblocks = {%s}c             s   s   |  ] } d  | | f Vq d S)z%r: block_%sNr0   )r   r   r0   r0   r1   r   ;  s    z/CodeGenerator.visit_Template.<locals>.<genexpr>zdebug_info = %r&c             s   s   |  ] } d  | Vq d S)z%s=%sNr0   )r   r   r0   r0   r1   r   A  s    )r+   )r+   r   ).AssertionErrorr   r9   r:   Zruntimer   r   r   code_featuresr   r<   findr	   ZExtendsfind_allZBlockr   r   r   ZImportedName
importnamer   r   rsplitr   r   r   r]   rT   r   r_   r   r   r`   ra   r   rb   r   r   r   r   r   supports_yield_fromr   rd   r   )r+   r,   r-   r'   r   ZenvenvZhave_extendsrd   import_impaliasmoduleobjr   r:   block_framerQ   r0   r0   r1   visit_Template  s    	 


	





	#zCodeGenerator.visit_Templatec             C   s"  d } | j  rL |  j r d S|  j d k rL |  j d  |  j   | d 7} | j rg |  j |  } n |  j   } t r |  j	 j
 r | j d k r |  j d | j | f |  n\ |  j	 j
 r d p d } |  j d | | j | f |  |  j   |  j d	 |  |  j   |  j |  d S)
z.Call a block and register it for the template.r   Nzif parent_template is None:r   z$yield from context.blocks[%r][0](%s)z	async forforz&%s event in context.blocks[%r][0](%s):event)r`   r   r   r   r   scopedr   r   r   r9   r   rc   r:   r   r   )r+   r,   r-   r^   r   loopr0   r0   r1   ry   D  s0    		

	

zCodeGenerator.visit_Blockc             C   s	  | j  s |  j d | j  |  j d k r{ |  j sK |  j d  |  j   |  j d d  |  j rq t    n
 |  j   |  j d |  |  j	 | j
 |  |  j d |  j  |  j d t  |  j   |  j d	  |  j   | j r d
 |  _ |  j d 7_ d S)zCalls the extender.z,cannot use extend from a non top-level scoper   zif parent_template is not None:zraise TemplateRuntimeError(%r)zextended multiple timesz+parent_template = environment.get_template(z, %r)z6for name, parent_block in parent_template.blocks.%s():z8context.blocks.setdefault(name, []).append(parent_block)Tr   N)r`   r   r   r   r   r   r   r~   r   r*   templater   r:   dict_item_iterra   )r+   r,   r-   r0   r0   r1   visit_Extendsj  s*    		
	


		zCodeGenerator.visit_Extendsc             C   s  | j  r  |  j d  |  j   d } t | j t j  r} t | j j t  rY d } q t | j j t	 t
 f  r d } n$ t | j t j t j f  r d } |  j d | |  |  j | j |  |  j d |  j  | j  r4|  j   |  j d  |  j   |  j d  |  j   |  j d	  |  j   d
 } | j r~|  j j rUd pXd } |  j d | |  j |  f  nE |  j j r|  j d  n) t r|  j d  d } n |  j d  | s|  j   |  j d |  |  j   | j  r |  j   d S)zHandles includes.ztry:Zget_or_select_templateZget_templateZselect_templateztemplate = environment.%s(z, %r)zexcept TemplateNotFound:r   zelse:Fz	async forr   zY%s event in template.root_render_func(template.new_context(context.get_all(), True, %s)):zGfor event in (await template._get_default_module_async())._body_stream:z6yield from template._get_default_module()._body_streamTz9for event in template._get_default_module()._body_stream:r   N)Zignore_missingr   r   r4   r  r	   ZConstrL   r   rF   rG   ZTupleZListr*   r   r:   r   with_contextr9   r   r   r   r   )r+   r,   r-   	func_nameZskip_event_yieldr   r0   r0   r1   visit_Include  sT    	
			



	
	

	zCodeGenerator.visit_Includec             C   s(  |  j  d | j j | j  |  | j r@ |  j d | j  |  j j rY |  j d  |  j d  |  j | j	 |  |  j d |  j
  | j r |  j d |  j j r d p d |  j |  f  n) |  j j r |  j d	  n |  j d
  | j r$| j j d  r$|  j  d | j  d S)zVisit regular imports.z%s = zcontext.vars[%r] = zawait zenvironment.get_template(z, %r).z*make_module%s(context.get_all(), True, %s)_asyncr   z_get_default_module_async()z_get_default_module()r   z!context.exported_vars.discard(%r)N)r   r_   r   r   r`   r   r9   r   r*   r  r:   r  r   
startswith)r+   r,   r-   r0   r0   r1   visit_Import  s$    #		zCodeGenerator.visit_Importc                s  |  j  |  |  j d |  j j r( d p+ d  |  j | j    |  j d |  j  | j r |  j d |  j j r{ d p~ d |  j    f  n) |  j j r |  j d  n |  j d  g  } g  } x | j	 D] } t
 | t  r | \ } } n | } |  j d	   j j |  | f  |  j d
   j j |   |  j   |  j d   j j |  d |  j |  t |  f | f  |  j     j r | j |  | j d  s | j |  q W| r<t |  d k r| d } |  j d |   j j |  f  n- |  j d d j   f d d   | D   | rt |  d k rl|  j d | d  n# |  j d d j t t |    d S)zVisit named imports.z/included_template = %senvironment.get_template(zawait r   z, %r).z*make_module%s(context.get_all(), True, %s)r  z_get_default_module_async()z_get_default_module()z,%s = getattr(included_template, %r, missing)zif %s is missing:z9%s = undefined(%r %% included_template.__name__, name=%r)zGthe template %%r (imported on %s) does not export the requested name %sr   r   r   zcontext.vars[%r] = %szcontext.vars.update({%s})z, c             3   s+   |  ]! } d  |   j  j |  f Vq d S)z%r: %sN)r_   r   )r   r:   )r-   r0   r1   r   )  s    z1CodeGenerator.visit_FromImport.<locals>.<genexpr>z!context.exported_vars.discard(%r)z-context.exported_vars.difference_update((%s))N)r   r   r9   r   r*   r  r:   r  r   rR   r4   rF   r   r_   r   r   r   r   r   r`   r   r  r   r   r
   )r+   r,   r-   Z	var_namesZdiscarded_namesr:   r   r0   )r-   r1   visit_FromImport  sf    	

	
 zCodeGenerator.visit_FromImportc             C   s  | j    } | j    } | j    } | j pK d t | j d d%  d&  k } d  } | rl | j j d  } | j j | d d | j r | j j | d d | j r|  j	   } | j j | d d |  j
 d |  j |  | j  |  j   |  j |  |  j
 |  j j rd pd	  |  j | j |  |  j d
  |  j |  j j rZd p]d  |  j d  |  j   |  j
 d | j  |  j | j |  |  j d  |  j   |  j
 d  |  j | j |  |  j d  |  j | d d | j r>|  j
 d |  j d  |  |  j   |  j |  | j | _ | rU|  j
 d |  xK | j t j  D]7 }	 |	 j d k rh|	 j d k rh|  j d |	 j  qhW| j r|  j	   }
 |  j
 d |
  |  j
 |  j j rd pd	 |  |  j | j |  | r8|  j j r$|  j d |  qE|  j d |  n |  j d
  | j r_|  j d |  | j rx|  j d  nS |  j j r| r|  j d  |  j | j |  |  j j r| r|  j d  | j r|  j d  | j r|  j d  n |  j | rd pd  |  j   |  j |  |  j | j |  | j rW|  j
 d  |
  |  j   |  j | d | j o}| j | j r|  j
 d! |
  |  j   |  j |  |  j | j |  |  j |  |  j   | j r|  j  |  |  j   |  j! | |  |  j j r%|  j d"  |  j d#  |  j j rK|  j d  |  j | j |  |  j j rw|  j d  |  j d$  |  j" |  d  S)'Nr   onlyr   Z
for_branchelsetestz
%s(fiter):z
async for zfor z in zauto_aiter(fiter)Zfiter:zif zyield    r   Tz'%s(reciter, loop_render_func, depth=0):z%s = missingstorez8Can't assign to special loop variable in for-loop targetz%s = 1z, %s in AsyncLoopContext(z, %s in LoopContext(z%s(Zreciterzauto_aiter(r   z&, undefined, loop_render_func, depth):z, undefined):z%s = 0zif %s:zawait zloop(z, loop))r   )r   )#rn   	recursiverT   Ziter_child_nodesr_   r   r   else_r  r   r   r   r   r   r9   r   r*   r   r   r   r   rc   r   r	   Namer{   r:   r   r   r   r   r   r   r   r   )r+   r,   r-   Z
loop_frameZ
test_frameZ
else_frameZextended_loopZloop_refZloop_filter_funcr:   Ziteration_indicatorr0   r0   r1   	visit_For5  s    		 


	
	"				
	
	

	
zCodeGenerator.visit_Forc             C   s  | j    } |  j d |  |  j | j |  |  j d  |  j   |  j | j |  |  j   xh | j	 D]] } |  j d |  |  j | j |  |  j d  |  j   |  j | j |  |  j   qm W| j
 r|  j d  |  j   |  j | j
 |  |  j   d  S)Nzif r  zelif zelse:)ro   r   r*   r  r   r   r   r   r   elif_r  )r+   r,   r-   Zif_framer  r0   r0   r1   visit_If  s&    


	
zCodeGenerator.visit_Ifc             C   s   |  j  | |  \ } } |  j   | j re | j j d  sQ |  j d | j  |  j d | j  |  j d | j j | j   |  j	 | |  d  S)Nr   zcontext.exported_vars.add(%r)zcontext.vars[%r] = z%s = )
r   r   r`   r:   r  r   r   r_   r   r   )r+   r,   r-   Zmacro_framer   r0   r0   r1   visit_Macro  s    
	 zCodeGenerator.visit_Macroc             C   so   |  j  | |  \ } } |  j d  |  j | |  |  j | |  |  j | j | d d |  j |  d  S)Nz	caller = forward_callerT)r   r   r   r   
visit_Callcallr   )r+   r,   r-   Z
call_framer   r0   r0   r1   visit_CallBlock  s    zCodeGenerator.visit_CallBlockc             C   s   | j    } | j j |  |  j |  |  j |  |  j | j |  |  j | |  |  j | j	 |  |  j
 |  |  j |  d  S)N)rn   r_   r   r   rc   r   r   r   rw   filterr   r   )r+   r,   r-   Zfilter_framer0   r0   r1   visit_FilterBlock  s    zCodeGenerator.visit_FilterBlockc             C   s   | j    } | j j |  |  j |  xZ t | j | j  D]C \ } } |  j   |  j | |  |  j	 d  |  j | |  q? W|  j
 | j |  |  j |  d  S)Nz = )rn   r_   r   r   r   targetsvaluesr   r*   r   r   r   r   )r+   r,   r-   Z
with_framer   exprr0   r0   r1   
visit_With  s    "
zCodeGenerator.visit_Withc             C   s$   |  j  |  |  j | j |  d  S)N)r   r*   r,   )r+   r,   r-   r0   r0   r1   visit_ExprStmt  s    zCodeGenerator.visit_ExprStmt_FinalizeInfoconstsrcc                s   j  d k	 r  j  S j }   d }  j j r d }  j j     f d d   } t  d d  d k r | d 7} d } ne t  d	 d  d k r | d
 7} d } n: t  d d  d k r | d 7}     f d d   }  j | |   _   j  S)a  Build the finalize function to be used on constants and at
        runtime. Cached so it's only created once for all output nodes.

        Returns a ``namedtuple`` with the following attributes:

        ``const``
            A function to finalize constant data at compile time.

        ``src``
            Source code to output around nodes to be evaluated at
            runtime.
        Nzenvironment.finalize(c                s      |    S)Nr0   )rL   )r   env_finalizer0   r1   finalize  s    z.CodeGenerator._make_finalize.<locals>.finalizeZcontextfunctionFTz	context, Zevalcontextfunctionzcontext.eval_ctx, Zenvironmentfunctionzenvironment, c                s       j  |    S)N)r9   )rL   )r   r&  r+   r0   r1   r'  '  s    )	_finalize_default_finalizer9   r'  r   r#  )r+   r'  r%  r0   )r   r&  r+   r1   _make_finalize  s&    
	
	
zCodeGenerator._make_finalizec             C   s   t  t |   S)zGiven a group of constant values converted from ``Output``
        child nodes, produce a string to write to the template module
        source.
        )r   r   )r+   groupr0   r0   r1   _output_const_repr-  s    z CodeGenerator._output_const_reprc             C   sS   | j  | j  } | j j r* t |  } t | t j  rF t |  S| j |  S)aC  Try to optimize a child of an ``Output`` node by trying to
        convert it to constant, finalized data at compile time.

        If :exc:`Impossible` is raised, the node is not constant and
        will be evaluated at runtime. Any other exception will also be
        evaluated at runtime for easier debugging.
        )	as_constr'   r   r   r4   r	   TemplateDatar   r$  )r+   r,   r-   r'  r$  r0   r0   r1   _output_child_to_const4  s    
z$CodeGenerator._output_child_to_constc             C   sh   | j  j r |  j d  n) | j  j r8 |  j d  n |  j d  | j d k	 rd |  j | j  d S)zXOutput extra source code before visiting a child of an
        ``Output`` node.
        z7(escape if context.eval_ctx.autoescape else to_string)(zescape(z
to_string(N)r'   r(   r   r   r%  )r+   r,   r-   r'  r0   r0   r1   _output_child_preG  s    zCodeGenerator._output_child_prec             C   s-   |  j  d  | j d k	 r) |  j  d  d S)zWOutput extra source code after visiting a child of an
        ``Output`` node.
        r   N)r   r%  )r+   r,   r-   r'  r0   r0   r1   _output_child_postU  s    z CodeGenerator._output_child_postc       	      C   s  | j  r- |  j r d  S|  j d  |  j   |  j   } g  } x | j D] } y@ | j pj t | t j  sy t j	    |  j
 | | |  } Wn+ t j	 t f k
 r | j |  wI Yn X| r t | d	 t  r | d
 j |  qI | j | g  qI W| j d  k	 rTt |  d k r6|  j d | j  n |  j d | j  |  j   x | D] } t | t  r|  j |  } | j d  k r|  j d |  q7|  j | d  q[| j d  k r|  j d |  n |  j |  |  j | | |  |  j | |  |  j | | |  | j d  k	 r[|  j d  q[W| j d  k	 ry|  j   |  j t |  d k rrd n d  | j  r|  j   d  S)Nzif parent_template is None:r   z
%s.append(z%s.extend((zyield r   r   z))r   r   )rb   r   r   r   r*  r	   r$  r4   r.  
Impossibler/  	Exceptionr   rG   rc   r   r,  r   r0  r*   r1  r   r   )	r+   r,   r-   r'  r   childr$  rM   valr0   r0   r1   visit_Output^  sV    		
	

%	zCodeGenerator.visit_Outputc             C   s[   |  j    |  j |  |  j | j |  |  j d  |  j | j |  |  j |  d  S)Nz = )r   r   r*   r   r   r,   r   )r+   r,   r-   r0   r0   r1   visit_Assign  s    
zCodeGenerator.visit_Assignc             C   s   |  j    | j   } d | _ | j j |  |  j |  |  j |  |  j | j |  |  j	 |  |  j
 | j |  |  j d  | j d  k	 r |  j | j |  n |  j d | j  |  j d  |  j |  |  j |  d  S)NFz9 = (Markup if context.eval_ctx.autoescape else identity)(z
concat(%s)r   )r   rn   rb   r_   r   r   rc   r   r   r   r*   r   r   r  rw   r   r   )r+   r,   r-   r   r0   r0   r1   visit_AssignBlock  s     
	zCodeGenerator.visit_AssignBlockc             C   s   | j  d k r8 | j r8 |  j r8 |  j d j | j  | j j | j  } | j  d k r | j j |  } | d  k	 o | d t k o |  j	 |  s |  j
 d | j | | f  d  S|  j
 |  d  S)Nr  r   rz   r   z-(undefined(name=%r) if %s is missing else %s)r   )r{   r`   r   rv   r:   r_   r   Z	find_loadr   r   r   )r+   r,   r-   r   rz   r0   r0   r1   r}     s    	zCodeGenerator.visit_Namec             C   si   | j  j | j  } |  j d |  |  j   |  j d d  |  j   |  j d | | j f  d  S)Nz!if not isinstance(%s, Namespace):zraise TemplateRuntimeError(%r)z/cannot assign attribute on non-namespace objectz%s[%r])r_   r   r:   r   r   r   attr)r+   r,   r-   r   r0   r0   r1   visit_NSRef  s    

zCodeGenerator.visit_NSRefc             C   sN   | j  | j  } t | t  r7 |  j t |   n |  j t |   d  S)N)r-  r'   r4   rD   r   strr   )r+   r,   r-   r5  r0   r0   r1   visit_Const  s    zCodeGenerator.visit_Constc             C   sS   y# |  j  t | j | j    Wn) t j k
 rN |  j  d | j  Yn Xd  S)Nz9(Markup if context.eval_ctx.autoescape else identity)(%r))r   r   r-  r'   r	   r2  data)r+   r,   r-   r0   r0   r1   visit_TemplateData  s
    #z CodeGenerator.visit_TemplateDatac             C   sy   |  j  d  d } x@ t | j  D]/ \ } } | rB |  j  d  |  j | |  q# W|  j  | d k rn d pq d  d  S)N(r   z, r   z,)r   r   )r   r   r#   r*   )r+   r,   r-   r   rM   r0   r0   r1   visit_Tuple  s    zCodeGenerator.visit_Tuplec             C   sa   |  j  d  x@ t | j  D]/ \ } } | r< |  j  d  |  j | |  q W|  j  d  d  S)N[z, ])r   r   r#   r*   )r+   r,   r-   r   rM   r0   r0   r1   
visit_List  s    zCodeGenerator.visit_Listc             C   s   |  j  d  xc t | j  D]R \ } } | r< |  j  d  |  j | j |  |  j  d  |  j | j |  q W|  j  d  d  S)N{z, z: r   )r   r   r#   r*   rN   rL   )r+   r,   r-   r   rM   r0   r0   r1   
visit_Dict  s    zCodeGenerator.visit_Dictc                s   t    f d d    } | S)Nc                s   |  j  j re   |  j  j k re |  j d    |  j | j |  |  j d  |  j | j |  nD |  j d  |  j | j |  |  j d    |  j | j |  |  j d  d  S)Nz$environment.call_binop(context, %r, z, r?  z %s r   )r9   	sandboxedZintercepted_binopsr   r*   leftright)r+   r,   r-   )operatorr0   r1   rS     s    z$CodeGenerator.binop.<locals>.visitor)r3   )rI  interceptablerS   r0   )rI  r1   binop  s    zCodeGenerator.binopc                s   t    f d d    } | S)Nc                sz   |  j  j rE   |  j  j k rE |  j d    |  j | j |  n$ |  j d    |  j | j |  |  j d  d  S)Nz#environment.call_unop(context, %r, r?  r   )r9   rF  Zintercepted_unopsr   r*   r,   )r+   r,   r-   )rI  r0   r1   rS   2  s    z#CodeGenerator.uaop.<locals>.visitor)r3   )rI  rJ  rS   r0   )rI  r1   uaop1  s    zCodeGenerator.uaop+-*/z//z**%andrJ  orznot c             C   s   | j  j r d } n | j  j r* d } n d } |  j d |  x. | j D]# } |  j | |  |  j d  qK W|  j d  d  S)Nz;(context.eval_ctx.volatile and markup_join or unicode_join)Zmarkup_joinZunicode_joinz%s((z, z)))r'   r(   r   r   r	   r*   )r+   r,   r-   r  r   r0   r0   r1   visit_ConcatO  s    		zCodeGenerator.visit_Concatc             C   sU   |  j  d  |  j | j |  x! | j D] } |  j | |  q* W|  j  d  d  S)Nr?  r   )r   r*   r   ops)r+   r,   r-   opr0   r0   r1   visit_Compare]  s
    zCodeGenerator.visit_Comparec             C   s/   |  j  d t | j  |  j | j |  d  S)Nz %s )r   	operatorsrV  r*   r   )r+   r,   r-   r0   r0   r1   visit_Operande  s    zCodeGenerator.visit_Operandc             C   sj   |  j  j r |  j d  |  j d  |  j | j |  |  j d | j  |  j  j rf |  j d  d  S)Nz(await auto_await(zenvironment.getattr(z, %r)z)))r9   r   r   r*   r,   r9  )r+   r,   r-   r0   r0   r1   visit_Getattri  s    zCodeGenerator.visit_Getattrc             C   s   t  | j t j  rX |  j | j |  |  j d  |  j | j |  |  j d  n |  j j rq |  j d  |  j d  |  j | j |  |  j d  |  j | j |  |  j d  |  j j r |  j d  d  S)NrA  rB  z(await auto_await(zenvironment.getitem(z, r   z)))	r4   r   r	   ZSlicer*   r,   r   r9   r   )r+   r,   r-   r0   r0   r1   visit_Getitemu  s    zCodeGenerator.visit_Getitemc             C   s   | j  d  k	 r" |  j | j  |  |  j d  | j d  k	 rQ |  j | j |  | j d  k	 r |  j d  |  j | j |  d  S)Nr  )startr*   r   stopr   )r+   r,   r-   r0   r0   r1   visit_Slice  s    zCodeGenerator.visit_Slicec             C   s  |  j  j r |  j d  |  j |  j | j d  |  j  j j | j  } | d  k rr |  j d | j | j  t | d d  d k r |  j d  nM t | d d  d k r |  j d	  n% t | d
 d  d k r |  j d  | j	 d  k	 r|  j
 | j	 |  nc | j j r8|  j d | j | j f  n7 | j j r[|  j d | j  n |  j d | j  |  j | |  |  j d  |  j  j r|  j d  d  S)Nzawait auto_await(r?  zno filter named %rZcontextfilterFTz	context, Zevalcontextfilterzcontext.eval_ctx, Zenvironmentfilterzenvironment, zB(context.eval_ctx.autoescape and Markup(concat(%s)) or concat(%s))zMarkup(concat(%s))z
concat(%s)r   )r9   r   r   rs   r:   getr   r   r   r,   r*   r'   r(   rc   r   r   )r+   r,   r-   r   r0   r0   r1   rw     s2    zCodeGenerator.visit_Filterc             C   s~   |  j  |  j | j d  | j |  j j k rJ |  j d | j | j  |  j | j |  |  j | |  |  j  d  d  S)Nr?  zno test named %rr   )	r   rt   r:   r9   r   r   r*   r,   r   )r+   r,   r-   r0   r0   r1   rx     s    zCodeGenerator.visit_Testc                s}       f d d   }  j  d   j  j     j  d   j  j     j  d  |    j  d  d  S)Nc                  sD    j  d  k	 r"  j  j     S j d d  j    d  S)Nzcond_expr_undefined(%r)zRthe inline if-expression on %s evaluated to false and no else section was defined.)Zexpr2r*   r   r   r0   )r-   r,   r+   r0   r1   write_expr2  s    z1CodeGenerator.visit_CondExpr.<locals>.write_expr2r?  z if z else r   )r   r*   Zexpr1r  )r+   r,   r-   r`  r0   )r-   r,   r+   r1   visit_CondExpr  s    zCodeGenerator.visit_CondExprc             C   s   |  j  j r |  j d  |  j  j r5 |  j d  n |  j d  |  j | j |  | rg d d i pj d  } |  j | | |  |  j d  |  j  j r |  j d  d  S)Nzawait auto_await(zenvironment.call(context, zcontext.call(r   r   )r9   r   r   rF  r*   r,   r   )r+   r,   r-   r  r   r0   r0   r1   r    s    zCodeGenerator.visit_Callc             C   s+   |  j  | j d  |  j | j |  d  S)N=)r   rN   r*   rL   )r+   r,   r-   r0   r0   r1   visit_Keyword  s    zCodeGenerator.visit_Keywordc             C   s1   |  j  d  |  j | j |  |  j  d  d  S)NzMarkup(r   )r   r*   r   )r+   r,   r-   r0   r0   r1   visit_MarkSafe  s    zCodeGenerator.visit_MarkSafec             C   s1   |  j  d  |  j | j |  |  j  d  d  S)Nz5(context.eval_ctx.autoescape and Markup or identity)(r   )r   r*   r   )r+   r,   r-   r0   r0   r1   visit_MarkSafeIfAutoescape  s    z(CodeGenerator.visit_MarkSafeIfAutoescapec             C   s   |  j  d | j  d  S)Nzenvironment.)r   r:   )r+   r,   r-   r0   r0   r1   visit_EnvironmentAttribute  s    z(CodeGenerator.visit_EnvironmentAttributec             C   s!   |  j  d | j | j f  d  S)Nzenvironment.extensions[%r].%s)r   
identifierr:   )r+   r,   r-   r0   r0   r1   visit_ExtensionAttribute  s    z&CodeGenerator.visit_ExtensionAttributec             C   s   |  j  |  j | j  d  S)N)r   r   r   )r+   r,   r-   r0   r0   r1   visit_ImportedName  s    z CodeGenerator.visit_ImportedNamec             C   s   |  j  | j  d  S)N)r   r:   )r+   r,   r-   r0   r0   r1   visit_InternalName  s    z CodeGenerator.visit_InternalNamec             C   s   |  j  d  d  S)Nr   )r   )r+   r,   r-   r0   r0   r1   visit_ContextReference  s    z$CodeGenerator.visit_ContextReferencec             C   s   |  j  |  j |   d  S)N)r   r   )r+   r,   r-   r0   r0   r1   visit_DerivedContextReference  s    z+CodeGenerator.visit_DerivedContextReferencec             C   s   |  j  d |  d  S)Ncontinue)r   )r+   r,   r-   r0   r0   r1   visit_Continue  s    zCodeGenerator.visit_Continuec             C   s   |  j  d |  d  S)Nbreak)r   )r+   r,   r-   r0   r0   r1   visit_Break  s    zCodeGenerator.visit_Breakc             C   sM   | j    } | j j |  |  j |  |  j | j |  |  j |  d  S)N)rn   r_   r   r   r   r   r   )r+   r,   r-   scope_framer0   r0   r1   visit_Scope  s
    zCodeGenerator.visit_Scopec             C   s   |  j    } |  j d | |  j |  f  |  j d |  |  j | j |  |  j |  | j d d  } | j j |  |  j	 |  |  j
 | j |  |  j |  |  j   d  S)Nz%s = %sz
%s.vars = rm   T)r   r   r   r*   r   r   rn   r_   r   r   r   r   r   r   )r+   r,   r-   r{   rq  r0   r0   r1   visit_OverlayScope  s     z CodeGenerator.visit_OverlayScopec             C   s   x | j  D] } |  j d | j  |  j | j |  y | j j | j  } Wn! t j k
 rs d | j _	 Yq
 Xt
 | j | j |  q
 Wd  S)Nzcontext.eval_ctx.%s = T)optionsr   rN   r*   rL   r-  r'   r	   r2  r(   setattr)r+   r,   r-   keywordr5  r0   r0   r1   visit_EvalContextModifier   s    z'CodeGenerator.visit_EvalContextModifierc             C   s   |  j    } | j j   } |  j d |  |  j | |  x! | j D] } |  j | |  qF W| j j |  |  j d |  d  S)Nz%s = context.eval_ctx.save()zcontext.eval_ctx.revert(%s))r   r'   saver   rw  r   r*   revert)r+   r,   r-   Zold_ctx_nameZ	saved_ctxr4  r0   r0   r1   visit_ScopedEvalContextModifier+  s    z-CodeGenerator.visit_ScopedEvalContextModifier)r$  r%  )orZ   r[   r\   rY   r   r   rc   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ry   r  r  r	  r
  r  r  r  r  r  r!  r"  r   r#  r   r)  r(  r*  r,  r/  r0  r1  r6  r7  r8  r}   r:  r<  r>  r@  rC  rE  rK  rL  Z	visit_AddZ	visit_SubZ	visit_MulZ	visit_DivZvisit_FloorDivZ	visit_PowZ	visit_ModZ	visit_AndZvisit_OrZ	visit_PosZ	visit_NegZ	visit_Notr3   rT  rW  rY  rZ  r[  r^  rw   rx   ra  r  rc  rd  re  rf  rh  ri  rj  rk  rl  rn  rp  rr  rs  rw  rz  r0   r0   r0   r1   r      s   C5P	|&-:K
)	O	

!	r   )=rp   collectionsr   	functoolsr   	itertoolsr   rv  r   r   Z
markupsafer   r   r   r	   _compatr
   r   r   r   r   r   r   
exceptionsr   Z
idtrackingr   r   r   r   r   r   r)   r   utilsr   rS   r   rX  hasattrrK   r  r   execr   SyntaxErrorr   r3   r>   rJ   rT   rf   rU   r]   RuntimeErrorrP   rr   rO   r3  r~   r   r0   r0   r0   r1   <module>   sr   		
=