B
    ram                 @   sX  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ G dd de	Z
G d	d
 d
eZG dd de	Ze ZdZG dd de	ZG dd de	ZG dd de	ZG dd de	ZG dd deZG dd deZdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd7d(d)Zd8d+d,Zd-d. Zd/d0 Z d1d2 Z!d9d3d4Z"d5d6 Z#dS ):zdprovides runtime services for templates, including Context,
Namespace, and various helper functions.    N)compat)
exceptions)util)compat_builtinsc               @   s   e Zd ZdZdd Zdd Zedd Zedd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zd'ddZdd Zdd  Zd!d" Zd#d$ Zd%d& ZdS )(ContextzProvides runtime namespace, output buffer, and various
    callstacks for templates.

    See :ref:`runtime_toplevel` for detail on the usage of
    :class:`.Context`.

     c             K   sR   |g| _ || _| | _d | _d | _i | _tt	| | jd< t
  | _| jd< d S )Ncapturecaller)_buffer_stack_datacopy_kwargs_with_template_outputting_as_unicode
namespaces	functoolspartialr   CallerStackcaller_stack)selfbufferdata r   ./tmp/pip-install-0_xzzbcd/Mako/mako/runtime.py__init__   s    
zContext.__init__c             C   s0   || _ |j| j}|r,tdd| d S )Nz%Reserved words passed to render(): %sz, )r   Zreserved_namesintersectionr
   r   ZNameConflictErrorjoin)r   tZillegal_namesr   r   r   _set_with_template.   s    zContext._set_with_templatec             C   s   | j jS )z]Return the :class:`.TemplateLookup` associated
        with this :class:`.Context`.

        )r   lookup)r   r   r   r   r   7   s    zContext.lookupc             C   s
   | j  S )a]  Return the dictionary of top level keyword arguments associated
        with this :class:`.Context`.

        This dictionary only includes the top-level arguments passed to
        :meth:`.Template.render`.  It does not include names produced within
        the template execution such as local variable names or special names
        such as ``self``, ``next``, etc.

        The purpose of this dictionary is primarily for the case that
        a :class:`.Template` accepts arguments via its ``<%page>`` tag,
        which are normally expected to be passed via :meth:`.Template.render`,
        except the template is being called in an inheritance context,
        using the ``body()`` method.   :attr:`.Context.kwargs` can then be
        used to propagate these arguments to the inheriting template::

            ${next.body(**context.kwargs)}

        )r   r   )r   r   r   r   kwargs?   s    zContext.kwargsc             C   s   | j | dS )zQPush a ``caller`` callable onto the callstack for
        this :class:`.Context`.N)r   append)r   r   r   r   r   push_callerU   s    zContext.push_callerc             C   s   | j d= dS )zPPop a ``caller`` callable onto the callstack for this
        :class:`.Context`.N)r   )r   r   r   r   
pop_caller[   s    zContext.pop_callerc             C   s   t | j S )zAReturn a list of all names established in this :class:`.Context`.)listr
   keys)r   r   r   r   r%   a   s    zContext.keysc             C   s"   || j kr| j | S tj| S d S )N)r
   r   __dict__)r   keyr   r   r   __getitem__f   s    

zContext.__getitem__c             C   s   t  }| j| |jS )zUpush a capturing buffer onto this Context and return
        the new writer function.)r   FastEncodingBufferr	   r    write)r   bufr   r   r   _push_writerl   s    zContext._push_writerc             C   s   | j  }|| j d jfS )zupop the most recent capturing buffer from this Context
        and return the current writer after the pop.

        r"   )r	   popr*   )r   r+   r   r   r   _pop_buffer_and_writert   s    
zContext._pop_buffer_and_writerc             C   s   |    dS )z*push a capturing buffer onto this Context.N)r,   )r   r   r   r   _push_buffer}   s    zContext._push_bufferc             C   s
   | j  S )z7pop the most recent capturing buffer from this Context.)r	   r-   )r   r   r   r   _pop_buffer   s    zContext._pop_bufferNc             C   s   | j |tj||S )z+Return a value from this :class:`.Context`.)r
   getr   r&   )r   r'   defaultr   r   r   r1      s    zContext.getc             C   s   | j d | dS )zSWrite a string to this :class:`.Context` object's
        underlying output buffer.r"   N)r	   r*   )r   stringr   r   r   r*      s    zContext.writec             C   s   | j d jS )z#Return the current writer function.r"   )r	   r*   )r   r   r   r   writer   s    zContext.writerc             C   sJ   t t }| j|_| j |_| j|_| j|_| j|_| j|_| j	|_	|S )N)
r   __new__r	   r
   r   r   r   r   r   r   )r   cr   r   r   _copy   s    
zContext._copyc             C   s    |s| S |   }|j| |S )zCreate a new :class:`.Context` with a copy of this
        :class:`.Context`'s current state,
        updated with the given dictionary.

        The :attr:`.Context.kwargs` collection remains
        unaffected.


        )r7   r
   update)r   dr6   r   r   r   _locals   s
    zContext._localsc             C   s6   |   }|j}|dd |dd |dd |S )zfcreate a new copy of this :class:`.Context`. with
        tokens related to inheritance state removed.r   Nparentnext)r7   r
   r-   )r   r6   xr   r   r   _clean_inheritance_tokens   s    z!Context._clean_inheritance_tokens)N)__name__
__module____qualname____doc__r   r   propertyr   r   r!   r#   r%   r(   r,   r.   r/   r0   r1   r*   r4   r7   r:   r>   r   r   r   r   r      s&   		
r   c               @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )r   c             C   s
   d | _ d S )N)
nextcaller)r   r   r   r   r      s    zCallerStack.__init__c             C   s   |   S )N)__bool__)r   r   r   r   __nonzero__   s    zCallerStack.__nonzero__c             C   s   t | r|  rdpdS )NTF)len_get_caller)r   r   r   r   rE      s    zCallerStack.__bool__c             C   s   | d S )Nr"   r   )r   r   r   r   rH      s    zCallerStack._get_callerc             C   s   t |  |S )N)getattrrH   )r   r'   r   r   r   __getattr__   s    zCallerStack.__getattr__c             C   s   | j pd }| | d | _ |S )N)rD   r    )r   framer   r   r   _push_frame   s    

zCallerStack._push_framec             C   s   |   | _d S )N)r-   rD   )r   r   r   r   
_pop_frame   s    zCallerStack._pop_frameN)
r?   r@   rA   r   rF   rE   rH   rJ   rL   rM   r   r   r   r   r      s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )		UndefinedzRepresents an undefined value in a template.

    All template modules have a constant value
    ``UNDEFINED`` present which is an instance of this
    object.

    c             C   s   t dd S )NrN   )	NameError)r   r   r   r   __str__   s    zUndefined.__str__c             C   s   |   S )N)rE   )r   r   r   r   rF      s    zUndefined.__nonzero__c             C   s   dS )NFr   )r   r   r   r   rE      s    zUndefined.__bool__N)r?   r@   rA   rB   rP   rF   rE   r   r   r   r   rN      s   rN    c               @   sT   e Zd ZdZdd Zdd Zdd Zedd	 Zd
d Z	dd Z
dd Zdd ZdS )	LoopStackza stack for LoopContexts that implements the context manager protocol
    to automatically pop off the top of the stack on context exit
    c             C   s
   g | _ d S )N)stack)r   r   r   r   r      s    zLoopStack.__init__c             C   s   |  | | jS )N)_push_top)r   iterabler   r   r   _enter   s    
zLoopStack._enterc             C   s   |    | jS )N)_poprU   )r   r   r   r   _exit   s    zLoopStack._exitc             C   s   | j r| j d S | S d S )Nr"   )rS   )r   r   r   r   rU     s    
zLoopStack._topc             C   s
   | j  S )N)rS   r-   )r   r   r   r   rX     s    zLoopStack._popc             C   s&   t |}| jr| jd |_| j|S )Nr"   )LoopContextrS   r;   r    )r   rV   newr   r   r   rT     s    zLoopStack._pushc             C   s   t dd S )NzNo loop context is established)r   RuntimeException)r   r'   r   r   r   rJ     s    zLoopStack.__getattr__c             C   s
   t | jS )N)iterrU   )r   r   r   r   __iter__  s    zLoopStack.__iter__N)r?   r@   rA   rB   r   rW   rY   rC   rU   rX   rT   rJ   r^   r   r   r   r   rR      s   rR   c               @   sr   e Zd ZdZdd Zdd Zejdd Ze	dd	 Z
e	d
d Ze	dd Ze	dd Ze	dd Zdd ZdS )rZ   a  A magic loop variable.
    Automatically accessible in any ``% for`` block.

    See the section :ref:`loop_context` for usage
    notes.

    :attr:`parent` -> :class:`.LoopContext` or ``None``
        The parent loop, if one exists.
    :attr:`index` -> `int`
        The 0-based iteration count.
    :attr:`reverse_index` -> `int`
        The number of iterations remaining.
    :attr:`first` -> `bool`
        ``True`` on the first iteration, ``False`` otherwise.
    :attr:`last` -> `bool`
        ``True`` on the last iteration, ``False`` otherwise.
    :attr:`even` -> `bool`
        ``True`` when ``index`` is even.
    :attr:`odd` -> `bool`
        ``True`` when ``index`` is odd.
    c             C   s   || _ d| _d | _d S )Nr   )	_iterableindexr;   )r   rV   r   r   r   r   3  s    zLoopContext.__init__c             c   s(   x"| j D ]}|V  |  jd7  _qW d S )N   )r_   r`   )r   ir   r   r   r^   8  s    zLoopContext.__iter__c             C   s
   t | jS )N)rG   r_   )r   r   r   r   __len__=  s    zLoopContext.__len__c             C   s   t | | j d S )Nra   )rG   r`   )r   r   r   r   reverse_indexA  s    zLoopContext.reverse_indexc             C   s
   | j dkS )Nr   )r`   )r   r   r   r   firstE  s    zLoopContext.firstc             C   s   | j t| d kS )Nra   )r`   rG   )r   r   r   r   lastI  s    zLoopContext.lastc             C   s   | j  S )N)odd)r   r   r   r   evenM  s    zLoopContext.evenc             C   s   t | jd S )N   )boolr`   )r   r   r   r   rg   Q  s    zLoopContext.oddc             G   s   |st d|| jt|  S )z5Cycle through values as the loop progresses.
        z(You must provide values to cycle through)
ValueErrorr`   rG   )r   valuesr   r   r   cycleU  s    zLoopContext.cycleN)r?   r@   rA   rB   r   r^   r   Zmemoized_instancemethodrc   rC   rd   re   rf   rh   rg   rm   r   r   r   r   rZ     s   rZ   c               @   s   e Zd Zdd Zdd ZdS )_NSAttrc             C   s
   || _ d S )N)_NSAttr__parent)r   r;   r   r   r   r   ^  s    z_NSAttr.__init__c             C   s:   | j }x&|r,t|j|r$t|j|S |j}qW t|d S )N)ro   hasattrmodulerI   inheritsAttributeError)r   r'   nsr   r   r   rJ   a  s    
z_NSAttr.__getattr__N)r?   r@   rA   r   rJ   r   r   r   r   rn   ]  s   rn   c               @   s   e Zd ZdZdddZdZdZdZdZdZ	dZ
dZejdd Zd	d
 Zdd Zdd Zedd Zdd Zdd Zdd Zdd ZdS )	Namespacea  Provides access to collections of rendering methods, which
      can be local, from other templates, or from imported modules.

      To access a particular rendering method referenced by a
      :class:`.Namespace`, use plain attribute access:

      .. sourcecode:: mako

        ${some_namespace.foo(x, y, z)}

      :class:`.Namespace` also contains several built-in attributes
      described here.

      NTc             C   s2   || _ || _|| _|d k	r.tdd |D | _d S )Nc             S   s   g | ]}|j |fqS r   )r?   ).0r6   r   r   r   
<listcomp>  s    z&Namespace.__init__.<locals>.<listcomp>)namecontextrr   dict	callables)r   rx   ry   r{   rr   populate_selfcalling_urir   r   r   r   |  s
    	zNamespace.__init__r   c             C   s   t | S )a-  Access module level attributes by name.

        This accessor allows templates to supply "scalar"
        attributes which are particularly handy in inheritance
        relationships.

        .. seealso::

            :ref:`inheritance_attr`

            :ref:`namespace_attr_for_includes`

        )rn   )r   r   r   r   attr  s    zNamespace.attrc             C   sL   | |f}|| j jkr | j j| S t|| j  || jd}|| j j|< |S dS )a]  Return a :class:`.Namespace` corresponding to the given ``uri``.

        If the given ``uri`` is a relative URI (i.e. it does not
        contain a leading slash ``/``), the ``uri`` is adjusted to
        be relative to the ``uri`` of the namespace itself. This
        method is therefore mostly useful off of the built-in
        ``local`` namespace, described in :ref:`namespace_local`.

        In
        most cases, a template wouldn't need this function, and
        should instead use the ``<%namespace>`` tag to load
        namespaces. However, since all ``<%namespace>`` tags are
        evaluated before the body of a template ever runs,
        this method can be used to locate namespaces using
        expressions that were generated within the body code of
        the template, or to conditionally use a particular
        namespace.

        )templateurir}   N)ry   r   TemplateNamespacer7   _templateuri)r   urir'   rt   r   r   r   get_namespace  s    
zNamespace.get_namespacec             C   s   t | j|| jS )zReturn a :class:`.Template` from the given ``uri``.

        The ``uri`` resolution is relative to the ``uri`` of this
        :class:`.Namespace` object's :class:`.Template`.

        )_lookup_templatery   r   )r   r   r   r   r   get_template  s    zNamespace.get_templatec             K   s   | j j|f|S )a  Return a value from the :class:`.Cache` referenced by this
        :class:`.Namespace` object's :class:`.Template`.

        The advantage to this method versus direct access to the
        :class:`.Cache` is that the configuration parameters
        declared in ``<%page>`` take effect here, thereby calling
        up the same configured backend as that configured
        by ``<%page>``.

        )cacher1   )r   r'   r   r   r   r   
get_cached  s    zNamespace.get_cachedc             C   s   | j jS )zReturn the :class:`.Cache` object referenced
        by this :class:`.Namespace` object's
        :class:`.Template`.

        )templater   )r   r   r   r   r     s    zNamespace.cachec             K   s   t | j|| jf| dS )z$Include a file at the given ``uri``.N)_include_filery   r   )r   r   r   r   r   r   include_file  s    zNamespace.include_filec             C   sH   xB|D ]:}|dkr2x,|   D ]\}}|||< qW qt| |||< qW d S )N*)	_get_starrI   )r   r9   lidentkvr   r   r   	_populate  s
    
zNamespace._populatec             c   s*   | j r&x| j D ]}|| j | fV  qW d S )N)r{   )r   r'   r   r   r   r     s    zNamespace._get_starc             C   sL   || j kr| j | }n&| jr*t| j|}ntd| j|f t| || |S )Nz!Namespace '%s' has no member '%s')r{   rr   rI   rs   rx   setattr)r   r'   valr   r   r   rJ   !  s    
zNamespace.__getattr__)NNTN)r?   r@   rA   rB   r   r{   rq   r   ry   filenamer   r   r   Zmemoized_propertyr~   r   r   r   rC   r   r   r   r   rJ   r   r   r   r   ru   k  s*      
		!		ru   c               @   sN   e Zd ZdZdddZedd Zedd	 Zed
d Zdd Z	dd Z
dS )r   z@A :class:`.Namespace` specific to a :class:`.Template` instance.NTc	             C   s   || _ || _|| _|d k	r.tdd |D | _|d k	rRt|||| _| jjj| _	n"|d k	rl|| _|jj| _	nt
d|rt|| j| d\}	}
d S )Nc             S   s   g | ]}|j |fqS r   )r?   )rv   r6   r   r   r   rw   A  s    z.TemplateNamespace.__init__.<locals>.<listcomp>z 'template' argument is required.)self_ns)rx   ry   rr   rz   r{   r   r   rq   Z_template_urir   	TypeError_populate_self_namespace)r   rx   ry   r   r   r{   rr   r|   r}   Zlclcallable
lclcontextr   r   r   r   2  s    zTemplateNamespace.__init__c             C   s   | j jS )zThe Python module referenced by this :class:`.Namespace`.

        If the namespace references a :class:`.Template`, then
        this module is the equivalent of ``template.module``,
        i.e. the generated module for the template.

        )r   rq   )r   r   r   r   rq   Q  s    	zTemplateNamespace.modulec             C   s   | j jS )zhThe path of the filesystem file used for this
        :class:`.Namespace`'s module or template.
        )r   r   )r   r   r   r   r   \  s    zTemplateNamespace.filenamec             C   s   | j jS )zThe URI for this :class:`.Namespace`'s template.

        I.e. whatever was sent to :meth:`.TemplateLookup.get_template()`.

        This is the equivalent of :attr:`.Template.uri`.

        )r   r   )r   r   r   r   r   c  s    	zTemplateNamespace.uric             #   sX    j r&x j D ]}| j | fV  qW  fdd}x  jjjD ]}|||fV  q>W d S )Nc                s    j | }t| jS )N)r   _get_def_callabler   r   ry   )r'   	callable_)r   r   r   r1   s  s    z(TemplateNamespace._get_star.<locals>.get)r{   r   rq   Z_exports)r   r'   r1   r   r   )r   r   r   n  s    zTemplateNamespace._get_starc             C   st   || j kr| j | }nN| j|r>| j|}t|| j}n&| jrRt| j|}nt	d| j
|f t| || |S )Nz!Namespace '%s' has no member '%s')r{   r   Zhas_defr   r   r   ry   rr   rI   rs   rx   r   )r   r'   r   r   r   r   r   rJ   z  s    
zTemplateNamespace.__getattr__)NNNNTN)r?   r@   rA   rB   r   rC   rq   r   r   r   rJ   r   r   r   r   r   .  s        
r   c               @   s6   e Zd ZdZdddZedd Zdd	 Zd
d ZdS )ModuleNamespacez;A :class:`.Namespace` specific to a Python module instance.NTc       
      C   sf   || _ || _|| _|d k	r.tdd |D | _t|}x$|ddd  D ]}	t||	}qJW || _d S )Nc             S   s   g | ]}|j |fqS r   )r?   )rv   r6   r   r   r   rw     s    z,ModuleNamespace.__init__.<locals>.<listcomp>.ra   )	rx   ry   rr   rz   r{   
__import__splitrI   rq   )
r   rx   ry   rq   r{   rr   r|   r}   modtokenr   r   r   r     s    
zModuleNamespace.__init__c             C   s   | j jS )zhThe path of the filesystem file used for this
        :class:`.Namespace`'s module or template.
        )rq   __file__)r   r   r   r   r     s    zModuleNamespace.filenamec             c   sr   | j r&x| j D ]}|| j | fV  qW xFt| jD ]8}|d dkr2t| j|}t|r2|t|| jfV  q2W d S )Nr   _)r{   dirrq   rI   callabler   r   ry   )r   r'   r   r   r   r   r     s    zModuleNamespace._get_starc             C   st   || j kr| j | }nNt| j|r>t| j|}t|| j}n&| jrRt| j|}ntd| j	|f t
| || |S )Nz!Namespace '%s' has no member '%s')r{   rp   rq   rI   r   r   ry   rr   rs   rx   r   )r   r'   r   r   r   r   r   rJ     s    
zModuleNamespace.__getattr__)NNTN)	r?   r@   rA   rB   r   rC   r   r   rJ   r   r   r   r   r     s      

r   c                s    fdd}|S )zApply a caller_stack compatibility decorator to a plain
    Python function.

    See the example in :ref:`namespaces_python_modules`.

    c                s,   | j   z | f||S | j   X d S )N)r   rL   rM   )ry   argsr   )funcr   r   wrap_stackframe  s    
z(supports_caller.<locals>.wrap_stackframer   )r   r   r   )r   r   supports_caller  s    r   c             O   s<   t |std|   z||| W d|  }X | S )zExecute the given template def, capturing the output into
    a buffer.

    See the example in :ref:`namespaces_python_modules`.

    z[capture() function expects a callable as its argument (i.e. capture(func, *args, **kwargs))N)r   r   r\   r/   r0   getvalue)ry   r   r   r   r+   r   r   r   r     s    
r   c                s    fdd}|S )Nc                s    fdd}|S )Nc                sL    fdd}yj dd  |_ W n tk
r6   Y nX | f||S )Nc                 s    f| |S )Nr   )r   kw)ry   	render_fnr   r   y  s    zB_decorate_toplevel.<locals>.decorate_render.<locals>.go.<locals>.y   )r?   r   )ry   r   r   r   )fnr   )ry   r   go  s    z7_decorate_toplevel.<locals>.decorate_render.<locals>.gor   )r   r   )r   )r   r   decorate_render  s    z+_decorate_toplevel.<locals>.decorate_renderr   )r   r   r   )r   r   _decorate_toplevel  s    r   c                s    fdd}|S )Nc                s   |   fdd}|S )Nc                 s    f| |S )Nr   )r   r   )ry   decr   r   r     s    z5_decorate_inline.<locals>.decorate_render.<locals>.gor   )r   r   )ry   r   )r   r   r     s    z)_decorate_inline.<locals>.decorate_renderr   )ry   r   r   r   )ry   r   r   _decorate_inline  s    r   c             K   s   t | ||}t|  |\}}t|| jf|}|jr~y||f| W q tk
rz   ||t }|svtj	t
   Y qX n||f| dS )zPlocate the template from the given uri and include it in
    the current output.N)r   r   r>   _kwargs_for_includer
   Zinclude_error_handler	Exceptionr   exception_asreraisesysexc_info)ry   r   r}   r   r   r   ctxresultr   r   r   r   
  s    r   c       
      C   s   |dkrdS t | ||}| d }|}x|jdk	r8|j}q&W | d|i}td|j ||dd|_|j | jd< |jd< t|jd	d}|dk	r|||}|r|S t|jd
d}	|	dk	r|	|  |j|fS )zcalled by the _inherit method in template modules to set
    up the inheritance chain at the start of a template's
    execution.Nr   r<   zself:%sF)r   r|   r;   local_mako_inheritZ_mako_generate_namespaces)	r   rr   r:   r   r   r
   rI   rq   r   )
ry   r   r}   r   r   Zihr   r   retZgen_nsr   r   r   _inherit_from  s.    


r   c             C   sf   | j j}|d kr"td| j j |||}y
||S  tjk
r`   ttt	
 Y nX d S )Nz.Template '%s' has no TemplateLookup associated)r   r   r   ZTemplateLookupExceptionr   Z
adjust_urir   ZTopLevelLookupExceptionstrr   r   )ry   r   Z
relativetor   r   r   r   r   >  s    
r   c             C   s^   |d krt d|j | |dd}| | jd< | jd< t|jdrT|j|| }|rT|S |j| fS )Nzself:%sF)r   r|   r   r   r   )r   r   r
   rp   rq   r   r   )ry   r   r   r   r   r   r   r   L  s    r   Fc             C   sx   |rt jdd}n$| jr"t }nt j|| j| jd}t|f|}||_|	|  t
| ||f|t|| |  S )z^create a Context and return the string
    output of the given template and template callable.T)
as_unicode)r   encodingerrors)r   r)   Zbytestring_passthroughr   StringIOoutput_encodingencoding_errorsr   r   r   _render_context_kwargs_for_callabler0   r   )r   r   r   r   r   r+   ry   r   r   r   _render\  s$    


r   c             C   sn   t | }|d r|S |d dd |dd D  }i }x0|D ](}|dkr>||kr>||kr>|| ||< q>W |S )Nri   r   c             S   s   g | ]}|d k	r|qS )Nr   )rv   r   r   r   r   rw     s    z(_kwargs_for_callable.<locals>.<listcomp>ra      ry   )r   inspect_getargspec)r   r   argspec	namedargsr   argr   r   r   r   x  s    

r   c             K   s^   t | }|d dd |dd D  }x0|D ](}|dkr.||kr.||kr.|| ||< q.W |S )Nr   c             S   s   g | ]}|d k	r|qS )Nr   )rv   r   r   r   r   rw     s    z'_kwargs_for_include.<locals>.<listcomp>ra   r   ry   )r   r   )r   r   r   r   r   r   r   r   r   r     s    

r   c             O   s\   dd l m} t| |js8t|| \}}t||||d n t|| j\}}t||||d d S )Nr   )r   r   )Zmako.templater   
isinstanceZDefTemplater   _exec_templater;   )tmplr   ry   r   r   r   Zinheritr   r   r   r   r     s    r   c             C   s   |j }|dk	rx|js|jrxy| |f|| W q tk
rR   t||t  Y q   t d }t||| Y qX n| |f|| dS )zexecute a rendering callable given the callable, a
    Context, and optional explicit arguments

    the contextual Template will be located if it exists, and
    the error handling options specified on that Template will
    be interpreted here.
    Nr   )	r   Zformat_exceptionserror_handlerr   _render_errorr   r   r   r   )r   ry   r   r   r   er   r   r   r     s    r   c             C   s   | j r&|  ||}|stjt   n\t }|jrNtj	ddg|j
d d < nt	|j|jg|j
d d < || |j||d d S )NT)r   )error)r   r   r   r   r   r   Zhtml_error_templater   r   r)   r	   r   r   r   Zrender_context)r   ry   r   r   error_templater   r   r   r     s    
r   )N)F)NN)$rB   r   r   Zmakor   r   r   Zmako.compatr   objectr   r$   r   rN   	UNDEFINEDZSTOP_RENDERINGrR   rZ   rn   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sB    -(B D]: 

	
