
v^P                 @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z d  d l m	 Z	 m
 Z
 m Z d  d l m Z m Z 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 e d d d d d i  Z e j d d e j  Z! d Z" e e#  j$ Z% Gd d   d  Z& d d   Z' d d   Z( d d d  Z) e  j*   d d    Z+ d  d!   Z, Gd" d#   d#  Z- Gd$ d%   d% e-  Z. Gd& d'   d'  Z/ d( d)   Z0 d* d+   Z1 d S),    N)Path)settings)Http404HttpResponseHttpResponseNotFound)ContextEngineTemplateDoesNotExist)pprint)resolve)timezone)MultiValueDict)	force_str)import_string)get_docs_versiondebugT	librariesZi18nzdjango.templatetags.i18nz#API|TOKEN|KEY|SECRET|PASS|SIGNATUREflagsz********************c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)CallableSettingWrapperz
    Object to wrap callable appearing in settings.
    * Not to call in the debug page (#21345).
    * Not to break the debug page if the callable forbidding to set attributes
      (#23070).
    c             C   s   | |  _  d  S)N)_wrapped)selfZcallable_setting r   4/tmp/pip-build-8lau8j11/django/django/views/debug.py__init__)   s    zCallableSettingWrapper.__init__c             C   s   t  |  j  S)N)reprr   )r   r   r   r   __repr__,   s    zCallableSettingWrapper.__repr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   "   s   r   c             C   s   yM t  j |   r t } n1 t | t  rF d d   | j   D } n | } Wn t k
 rg | } Yn Xt |  r t |  } | S)z
    Cleanse an individual setting key/value of sensitive content. If the value
    is a dictionary, recursively cleanse the keys in that dictionary.
    c             S   s(   i  |  ] \ } } t  | |  |  q Sr   )cleanse_setting).0kvr   r   r   
<dictcomp>:   s   	 z#cleanse_setting.<locals>.<dictcomp>)	HIDDEN_SETTINGSsearchCLEANSED_SUBSTITUTE
isinstancedictitems	TypeErrorcallabler   )keyvaluecleansedr   r   r   r    0   s    	
r    c              C   sI   i  }  x< t  t  D]. } | j   r t | t t |   |  | <q W|  S)z{
    Return a dictionary of the settings module with values of sensitive
    settings replaced with stars (*********).
    )dirr   isupperr    getattr)Zsettings_dictr"   r   r   r   get_safe_settingsH   s
     r3   i  c             C   si   t  |  | | |  } |  j   rC | j   } t | d | d d S| j   } t | d | d d Sd S)z
    Create a technical server error response. The last three arguments are
    the values returned from sys.exc_info() and friends.
    statuscontent_typeztext/plain; charset=utf-8z	text/htmlN)ExceptionReporterZis_ajaxget_traceback_textr   get_traceback_html)requestexc_type	exc_valuetbstatus_codeZreportertexthtmlr   r   r   technical_500_responseT   s    r@   c               C   s   t  t j    S)N)r   r   Z!DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   %get_default_exception_reporter_filterb   s    rA   c             C   s   t    } t |  d |  S)NZexception_reporter_filter)rA   r2   )r9   Zdefault_filterr   r   r   get_exception_reporter_filterh   s    	rB   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)ExceptionReporterFilterzv
    Base for all exception reporter filter classes. All overridable hooks
    contain lenient default behaviors.
    c             C   s   | d  k r i  S| j  Sd  S)N)POST)r   r9   r   r   r   get_post_parameterss   s    z+ExceptionReporterFilter.get_post_parametersc             C   s   t  | j j    S)N)listf_localsr*   )r   r9   tb_framer   r   r   get_traceback_frame_variablesy   s    z5ExceptionReporterFilter.get_traceback_frame_variablesN)r   r   r   r   rE   rI   r   r   r   r   rC   m   s   rC   c               @   sR   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d   Z d S)SafeExceptionReporterFilterz
    Use annotations made by the sensitive_post_parameters and
    sensitive_variables decorators to filter out sensitive information.
    c             C   s   t  j d k S)a  
        This filter is to add safety in production environments (i.e. DEBUG
        is False). If DEBUG is True then your site is not safe anyway.
        This hook is provided as a convenience to easily activate or
        deactivate the filter on a per request basis.
        F)r   DEBUG)r   r9   r   r   r   	is_active   s    z%SafeExceptionReporterFilter.is_activec             C   s^   t  | d g   } |  j |  rZ | rZ | j   } x$ | D] } | | k r: t | | <q: W| S)z
        Replace the keys in a MultiValueDict marked as sensitive with stars.
        This mitigates leaking sensitive POST parameters if something like
        request.POST['nonexistent_key'] throws an exception (#21098).
        sensitive_post_parameters)r2   rL   copyr'   )r   r9   ZmultivaluedictrM   paramr   r   r   get_cleansed_multivaluedict   s    z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc             C   s   | d k r i  St  | d g   } |  j |  r | r | j j   } | d k rq x | D] } t | | <qY W| Sx$ | D] } | | k rx t | | <qx W| Sn | j Sd S)zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        NrM   __ALL__)r2   rL   rD   rN   r'   )r   r9   rM   r/   r"   rO   r   r   r   rE      s    z/SafeExceptionReporterFilter.get_post_parametersc             C   sg   y t  | t  } Wn5 t k
 rJ } z d j | |  SWYd  d  } ~ Xn X| rc |  j | |  } | S)Nz{!r} while evaluating {!r})r(   r   	ExceptionformatrP   )r   r9   r.   Zis_multivalue_dicter   r   r   cleanse_special_types   s    #z1SafeExceptionReporterFilter.cleanse_special_typesc       	      C   s}  | j  } d } xZ | d k	 rk | j j d k r_ d | j k r_ | j d } t | d d  } P| j  } q Wi  } |  j |  r| r| d k r xo | j D] } t | | <q Wq>x | j j   D]= \ } } | | k r t } n |  j | |  } | | | <q Wn6 x3 | j j   D]" \ } } |  j | |  | | <qW| j j d k rsd | j k rst | d <t | d <| j   S)ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        NZsensitive_variables_wrappersensitive_variablesrQ   	func_argsZfunc_kwargs)	f_backf_codeco_namerG   r2   rL   r'   r*   rU   )	r   r9   rH   current_framerV   wrapperr/   namer.   r   r   r   rI      s4    		

z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)	r   r   r   r   rL   rP   rE   rU   rI   r   r   r   r   rJ   }   s   	rJ   c               @   sg   e  Z d  Z d Z d d d  Z d d   Z d d   Z d	 d
   Z d d d d  Z d d   Z	 d S)r6   z0Organize and coordinate reporting on exceptions.Fc             C   sm   | |  _  t |  j   |  _ | |  _ | |  _ | |  _ | |  _ t |  j d d   |  _ d |  _	 d  |  _
 d  S)NZtemplate_debugF)r9   rB   filterr:   r;   r<   is_emailr2   template_infotemplate_does_not_exist
postmortem)r   r9   r:   r;   r<   r_   r   r   r   r      s    						zExceptionReporter.__init__c          &   C   sP  |  j  r? t |  j  t  r? d |  _ |  j j p9 |  j g |  _ |  j   } x t |  D] \ } } d | k r g  } xi | d D]] \ } } t	 |  } t
 |  d k r d | d d  t
 |  f } | j | | f  q W| | d <| | | <qX Wd } |  j  rt |  j  t  rt |  j d d  } t |  j d	 d  }	 | d k	 r|	 d k	 r|  j j d
 }
 t |
 t | d d  t |	 d t
 |
    d d d } d d l m } |  j d k rd } n1 y t |  j j  } Wn t k
 rd } Yn Xd |  j d | d | d |  j d | d t |  j j |  j  j    d t   d t j d d t j  d d  d t! j"   d |   d t j# d |  j$ d  |  j d! |  j i } |  j d k	 r|  j j% j   | d" <|  j j& j   | d# <|  j j' j   | d$ <|  j  r|  j  j( | d% <|  j r8t |  j  | d& <| rL| d( | d' <| S))z5Return a dictionary containing traceback information.Tvarsi   u   %s… <trimmed %d bytes string>r    startNend      asciierrorsreplace)get_versionz%[unable to retrieve the current user]r_   unicode_hintframesr9   user_strZfiltered_POST_itemsr   Zsys_executableZsys_version_infoz%d.%d.%d   Zserver_timeZdjango_version_infosys_pathr`   ra   rb   Zrequest_GET_itemsZrequest_FILES_itemsZrequest_COOKIES_itemsZexception_typeZexception_valueZ	lastframe))r:   
issubclassr	   ra   r;   chainrb   get_traceback_frames	enumerater
   lenappendUnicodeErrorr2   argsr   maxmindjangorl   r9   struserrR   r_   rF   r^   rE   r*   r3   sys
executableversion_infor   nowpathr`   GETZFILESZCOOKIESr   )r   rn   iframeZ
frame_varsr"   r#   rm   re   rf   Zunicode_strrl   ro   cr   r   r   get_traceback_data  sp    	 
-			!								z$ExceptionReporter.get_traceback_datac             C   sb   t  t d d  j d d   } t j | j    } Wd QRXt |  j   d d } | j |  S)z1Return HTML version of debug 500 HTTP error page.	templatesztechnical_500.htmlencodingzutf-8Nuse_l10nF)	r   CURRENT_DIRopenDEBUG_ENGINEfrom_stringreadr   r   render)r   fhtr   r   r   r   r8   I  s    !z$ExceptionReporter.get_traceback_htmlc             C   sh   t  t d d  j d d   } t j | j    } Wd QRXt |  j   d d d d } | j |  S)	z7Return plain text version of debug 500 HTTP error page.r   ztechnical_500.txtr   zutf-8NZ
autoescapeFr   )	r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r7   P  s    !z$ExceptionReporter.get_traceback_textNc                s  d } t  | d  rU y | j |  } Wn t k
 r< Yn X| d k	 rU | j   } | d k r y/ t | d   } | j   j   } Wd QRXWn t k
 r Yn X| d k r d g  d g  f St | d t  r?d   xI | d d  D]7 } t	 j
 d |  }	 |	 r |	 j d  j d    Pq W  f d	 d
   | D } t d | |  }
 | | } | |
 |  } | | } | | d |  } |
 | | | f S)z
        Return context_lines before and after lineno from file.
        Return (pre_context_lineno, pre_context, context_line, post_context).
        N
get_sourcerbr   ri      s   coding[:=]\s*([-\w.]+)rg   c                s"   g  |  ] } t  |   d    q S)rk   )r~   )r!   sline)r   r   r   
<listcomp>y  s   	 z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)hasattrr   ImportError
splitlinesr   r   OSErrorr(   bytesrer&   groupdecoder{   )r   filenamelinenoZcontext_linesloadermodule_namesourcefplinematchZlower_boundZupper_boundpre_contextcontext_linepost_contextr   )r   r   _get_lines_from_fileW  s<    

z&ExceptionReporter._get_lines_from_filec             C   s6  d d   } g  } |  j  } x0 | rM | j |  | |  } | | k r Pq Wg  } | s^ | S| j   } | sy |  j n | j } x| d  k	 r1| j j j d  r | j } q | j j	 j
 } | j j	 j } | j d } | j j j d  }	 | j j j d  p
d }
 |  j | | d |	 |
  \ } } } } | d  k rX| } g  } d	 } g  } | j d
 | |  d t | d d  d | d |
 j d  rd n d d | d | d | d d |  j j |  j | j  d t |  d | d | d | d | d i  | j r%| r%| j   } | j } q | j } q W| S)Nc             S   s.   t  |  d d   } t  |  d d   } | p- | S)N	__cause____context__)r2   )r;   explicitZimplicitr   r   r   explicit_or_implicit_cause  s    zJExceptionReporter.get_traceback_frames.<locals>.explicit_or_implicit_causeZ__traceback_hide__rg   
__loader__r   rd      z<source code not available>Z	exc_causeZexc_cause_explicitr   Tr<   typezdjango.r}   r   r   functionr   rc   idr   r   r   pre_context_lineno)r;   rx   popr<   __traceback__rH   rG   gettb_nextrY   co_filenamerZ   	tb_lineno	f_globalsr   r2   
startswithr^   rI   r9   r   )r   r   
exceptionsr;   rn   r<   r   r   r   r   r   r   r   r   r   r   r   r   ru     s`    			!
z&ExceptionReporter.get_traceback_frames)
r   r   r   r   r   r   r8   r7   r   ru   r   r   r   r   r6      s   G-r6   c          0   C   s  y | j  d d } Wn. t t t f k
 rE |  j d d  } Yn Xy | j  d d } Wn! t t t f k
 r~ g  } Yn X| s|  j d k rt |  d k rt | d  d k rt | d d d d  t | d d d	 d  k o d
 k n rt |   St |  d t	 j
  } t | t j  r;| j } d } y t |  j  } Wn t k
 rhYnz X| j } t | d  r| j } n- t | d  rt | j d  r| j j } t | d  r| j } d | | f } t t d d  j d d   }	 t j |	 j    }
 Wd QRXt d | d t	 j
 d | d | d t |  d |  d t   d | i  } t |
 j |  d d S)zBCreate a technical 404 error response. `exception` is the Http404.r   r   rg   Ntried/Zapp_namerd   	namespaceZadminurlconfr   	__class__r   z%s.%sr   ztechnical_404.htmlr   zutf-8Zroot_urlconfrequest_pathZurlpatternsreasonr9   r   Zraising_view_namer5   z	text/html) rz   
IndexErrorr+   KeyErrorZ	path_infor   rw   r2   default_urlconfr   ZROOT_URLCONFr(   types
ModuleTyper   r   r   funcr   r   r   r   r   r   r   r   r   r   r~   r3   r   r   )r9   	exceptionZ	error_urlr   r   ZcallerZresolver_matchobjmoduler   r   r   r   r   r   technical_404_response  sT    D
		!	!		r   c             C   sk   t  t d d  j d d   } t j | j    } Wd QRXt d t   i  } t | j	 |  d d S)	z+Create an empty URLconf 404 error response.r   zdefault_urlconf.htmlr   zutf-8Nversionr5   z	text/html)
r   r   r   r   r   r   r   r   r   r   )r9   r   r   r   r   r   r   r     s
    !r   )2	functoolsr   r   r   Zpathlibr   Zdjango.confr   Zdjango.httpr   r   r   Zdjango.templater   r   r	   Zdjango.template.defaultfiltersr
   Zdjango.urlsr   Zdjango.utilsr   Zdjango.utils.datastructuresr   Zdjango.utils.encodingr   Zdjango.utils.module_loadingr   Zdjango.utils.versionr   r   compile
IGNORECASEr%   r'   __file__parentr   r   r    r3   r@   	lru_cacherA   rB   rC   rJ   r6   r   r   r   r   r   r   <module>   s>   w8