a
    xdS                     @   sB  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 d dlmZ edddidZ ee!j"Z#G dd dZ$d%ddZ%e & dd Z'dd Z(dd Z)G dd dZ*G dd  d Z+d!d" Z,d#d$ Z-dS )&    N)Path)settings)Http404HttpResponseHttpResponseNotFound)ContextEngineTemplateDoesNotExist)pprint)resolve)timezone)MultiValueDict)	force_str)import_string)_lazy_re_compile)get_docs_versionTZi18nzdjango.templatetags.i18n)debug	librariesc                   @   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   J/var/www/html/Ranjet/env/lib/python3.9/site-packages/django/views/debug.py__init__&   s    zCallableSettingWrapper.__init__c                 C   s
   t | jS r   )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   sL   t | | |||}| dr2| }t||ddS | }t||ddS dS )z
    Create a technical server error response. The last three arguments are
    the values returned from sys.exc_info() and friends.
    	text/html)statuscontent_typeztext/plain; charset=utf-8N)get_exception_reporter_classZacceptsget_traceback_htmlr   get_traceback_text)requestexc_type	exc_valuetbstatus_codeZreporterhtmltextr   r   r   technical_500_response-   s    
r0   c                   C   s   t tj S r   )r   r   Z!DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   %get_default_exception_reporter_filter;   s    r1   c                 C   s   t  }t| d|S )NZexception_reporter_filter)r1   getattr)r)   Zdefault_filterr   r   r   get_exception_reporter_filterA   s    r3   c                 C   s   t tj}t| d|S )NZexception_reporter_class)r   r   ZDEFAULT_EXCEPTION_REPORTERr2   )r)   Z default_exception_reporter_classr   r   r   r&   F   s    
r&   c                   @   sb   e Zd ZdZdZedej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S )SafeExceptionReporterFilterz
    Use annotations made by the sensitive_post_parameters and
    sensitive_variables decorators to filter out sensitive information.
    z********************z#API|TOKEN|KEY|SECRET|PASS|SIGNATURE)flagsc                    s   z~ j |r j}nft|tr8 fdd| D }nDt|trV fdd|D }n&t|trxt fdd|D }n|}W n ty   |}Y n0 t	|rt
|}|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   i | ]\}}|  ||qS r   cleanse_setting.0kvr   r   r   
<dictcomp>\       z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<dictcomp>c                    s   g | ]}  d |qS  r6   r9   r;   r   r   r   
<listcomp>^   r=   z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<listcomp>c                    s   g | ]}  d |qS r>   r6   r@   r   r   r   rA   `   r=   )hidden_settingssearchcleansed_substitute
isinstancedictitemslisttuple	TypeErrorcallabler   )r   keyvaluecleansedr   r   r   r7   S   s    



z+SafeExceptionReporterFilter.cleanse_settingc                 C   s4   i }t tD ]"}| r| |tt|||< q|S )z
        Return a dictionary of the settings module with values of sensitive
        settings replaced with stars (*********).
        )dirr   isupperr7   r2   )r   Zsettings_dictr:   r   r   r   get_safe_settingsl   s
    z-SafeExceptionReporterFilter.get_safe_settingsc                    s&   t |dsi S  fdd|j D S )zU
        Return a dictionary of request.META with sensitive values redacted.
        METAc                    s   i | ]\}}|  ||qS r   r6   r8   r   r   r   r<   }   r=   zESafeExceptionReporterFilter.get_safe_request_meta.<locals>.<dictcomp>)hasattrrR   rG   r   r)   r   r   r   get_safe_request_metaw   s    
z1SafeExceptionReporterFilter.get_safe_request_metac                 C   s
   t jdu 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   DEBUGrT   r   r   r   	is_active   s    z%SafeExceptionReporterFilter.is_activec                 C   sB   t |dg }| |r>|r>| }|D ]}||v r&| j||< q&|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   rW   copyrD   )r   r)   ZmultivaluedictrX   paramr   r   r   get_cleansed_multivaluedict   s    z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc                 C   s|   |du ri S t |dg }| |rr|rr|j }|dkrP|D ]}| j||< q<|S |D ]}||v rT| j||< qT|S n|jS dS )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        NrX   __ALL__)r2   rW   POSTrY   rD   )r   r)   rX   rN   r:   rZ   r   r   r   get_post_parameters   s    
z/SafeExceptionReporterFilter.get_post_parametersc              
   C   sT   zt |t}W n0 ty> } zd||W  Y d }~S d }~0 0 |rP| ||}|S )Nz{!r} while evaluating {!r})rE   r   	Exceptionformatr[   )r   r)   rM   Zis_multivalue_dicter   r   r   cleanse_special_types   s    "z1SafeExceptionReporterFilter.cleanse_special_typesc           	      C   s  |j }d}|durH|jjdkr@d|jv r@|jd }t|dd}qH|j }q
i }| |r|r|dkrz|jD ]}| j||< qhq|j D ],\}}||v r| j}n| ||}|||< qn$|j D ]\}}| ||||< q|jjdkrd|jv r| j|d< | j|d< | S )ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        NZsensitive_variables_wrappersensitive_variablesr\   	func_argsZfunc_kwargs)	f_backf_codeco_namef_localsr2   rW   rD   rG   rb   )	r   r)   tb_framecurrent_framerc   wrapperrN   namerM   r   r   r   get_traceback_frame_variables   s8    



z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)r   r   r    r!   rD   r   reIrB   r7   rQ   rU   rW   r[   r^   rb   rm   r   r   r   r   r4   K   s   	r4   c                   @   sL   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdddZ	dd Z
dS )ExceptionReporterz0Organize and coordinate reporting on exceptions.Fc                 C   sJ   || _ t| j | _|| _|| _|| _|| _t| jdd | _d| _	d | _
d S )NZtemplate_debugF)r)   r3   filterr*   r+   r,   is_emailr2   template_infotemplate_does_not_exist
postmortem)r   r)   r*   r+   r,   rr   r   r   r   r      s    zExceptionReporter.__init__c                 C   s^  | j r*t| j tr*d| _| jjp&| jg| _|  }t|D ]p\}}d|v rg }|d D ]B\}}t	|}t
|dkrd|dd t
|f }|||f qV||d< |||< q:d}| j r0t| j tr0t| jdd}t| jd	d}	|dur0|	dur0| jjd
 }
t|
t|d dt|	d t
|
 ddd}ddlm} | jdu rNd}n*zt| jj}W n tyv   d}Y n0 | j||| j| j| j|t| j| j | j tj dtj!dd  t"# | tj$| j%| j| jd}| jdur| jj& |d< | jj' |d< | jj( |d< | j r2| j j)|d< | jrHt| j|d< |rZ|d |d< |S )z5Return a dictionary containing traceback information.Tvarsi   u   %s… <trimmed %d bytes string>r   r?   startNend      asciireplace)errors)get_versionz%[unable to retrieve the current user]z%d.%d.%d   )rr   unicode_hintframesr)   Zrequest_metauser_strZfiltered_POST_itemsr   Zsys_executableZsys_version_infoZserver_timeZdjango_version_infosys_pathrs   rt   ru   Zrequest_GET_itemsZrequest_FILES_itemsZrequest_COOKIES_itemsZexception_typeZexception_valueZ	lastframe)*r*   
issubclassr	   rt   r+   chainru   get_traceback_frames	enumerater
   lenappendUnicodeErrorr2   argsr   maxmindjangor~   r)   struserr_   rr   rq   rU   rH   r^   rG   rQ   sys
executableversion_infor   nowpathrs   GETZFILESZCOOKIESr   )r   r   iframeZ
frame_varsr:   r;   r   rw   rx   Zunicode_strr~   r   cr   r   r   get_traceback_data   sv    
"
z$ExceptionReporter.get_traceback_datac                 C   s\   t tddjdd}t| }W d   n1 s80    Y  t|  dd}||S )z1Return HTML version of debug 500 HTTP error page.	templatesztechnical_500.htmlutf-8encodingNF)use_l10n	r   CURRENT_DIRopenDEBUG_ENGINEfrom_stringreadr   r   renderr   fhtr   r   r   r   r'   F  s    ,z$ExceptionReporter.get_traceback_htmlc                 C   s^   t tddjdd}t| }W d   n1 s80    Y  t|  ddd}||S )z7Return plain text version of debug 500 HTTP error page.r   ztechnical_500.txtr   r   NF)Z
autoescaper   r   r   r   r   r   r(   M  s    ,z$ExceptionReporter.get_traceback_textc                 C   s   d }t |dr@z||}W n ty.   Y n0 |d ur@| }|d u rz:t|d}|  }W d    n1 sv0    Y  W n ty   Y n0 |S )N
get_sourcerb)rS   r   ImportError
splitlinesr   r   OSError)r   filenameloadermodule_namesourcefpr   r   r   _get_sourceT  s    
.zExceptionReporter._get_sourceNc                    s   |  |||}|du r"dg dg fS t|d trzd |dd D ]&}td|}|r@|d d  qhq@ fdd|D }td|| }	|| }
z(||	| }|| }||d |
 }W n ty   dg dg f Y S 0 |	|||fS )	z
        Return context_lines before and after lineno from file.
        Return (pre_context_lineno, pre_context, context_line, post_context).
        Nr   r{      s   coding[:=]\s*([-\w.]+)ry   c                    s   g | ]}t | d qS )r|   )r   )r9   sliner   r   r   rA   z  r=   z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)r   rE   bytesrn   rC   decoder   
IndexError)r   r   linenoZcontext_linesr   r   r   linematchZlower_boundZupper_boundpre_contextcontext_linepost_contextr   r   r   _get_lines_from_filee  s(    z&ExceptionReporter._get_lines_from_filec                 C   sn  dd }g }| j }|r4|| ||}||v rq4qg }|s@|S | }|sR| jn|j}|d urj|jjdrx|j}qX|jj	j
}|jj	j}|jd }|jjd}	|jjdpd}
| ||d|	|
\}}}}|d u r|}g }d	}g }|||t|d
d||
drdnd|||d | j| j|jt|||||d d |jsb|rb| }|j}qX|j}qX|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__ry   
__loader__r   r?      z<source code not available>r   Tzdjango.r   r   )Z	exc_causeZexc_cause_explicitr,   typer   functionr   rv   idr   r   r   pre_context_lineno)r+   r   popr,   __traceback__ri   rh   gettb_nextrf   co_filenamerg   	tb_lineno	f_globalsr   r2   
startswithrq   rm   r)   r   )r   r   
exceptionsr+   r   r,   r   r   r   r   r   r   r   r   r   r   r   r   r     sd    






z&ExceptionReporter.get_traceback_frames)F)NN)r   r   r    r!   r   r   r'   r(   r   r   r   r   r   r   r   rp      s   
H
"rp   c              
   C   s  z|j d d }W n& tttfy8   | jdd }Y n0 z|j d d }W n tttfyh   g }Y nl0 |r| jdkrt|dkrt|d dkrt|d d ddt|d d d	d  krd
krn nt| S t| dt	j
}t|tjr|j}d}zt| j}W n ty   Y n\0 |j}t|dr6|j}n"t|drXt|jdrX|jj}t|drv|j}d||f }ttddjdd}	t|	 }
W d   n1 s0    Y  t }t|t	j
||t|| | |d}t|
 |ddS )zBCreate a technical 404 error response. `exception` is the Http404.r   r   ry   Ntried/Zapp_namer?   	namespaceZadminurlconfr   	__class__r   z%s.%sr   ztechnical_404.htmlr   r   )r   Zroot_urlconfrequest_pathZurlpatternsreasonr)   r   Zraising_view_namer#   r%   )!r   r   rJ   KeyErrorZ	path_infor   r   r2   default_urlconfr   ZROOT_URLCONFrE   types
ModuleTyper   r   r   funcrS   r   r   r   r   r   r   r   r   r1   r   r   rQ   r   r   )r)   	exceptionZ	error_urlr   r   ZcallerZresolver_matchobjmoduler   r   Zreporter_filterr   r   r   r   technical_404_response  sd    

$
.
r   c                 C   sb   t tddjdd}t| }W d   n1 s80    Y  tdt i}t|	|ddS )	z+Create an empty URLconf 404 error response.r   zdefault_urlconf.htmlr   r   Nversionr#   r   )
r   r   r   r   r   r   r   r   r   r   )r)   r   r   r   r   r   r   r     s    ,r   )r"   ).	functoolsrn   r   r   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.regex_helperr   Zdjango.utils.versionr   r   __file__parentr   r   r0   	lru_cacher1   r3   r&   r4   rp   r   r   r   r   r   r   <module>   s@   


 & _9