<s> import datetime <EOL> import os <EOL> import re <EOL> import sys <EOL> import types <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . http import ( HttpResponse , HttpResponseServerError , <EOL> HttpResponseNotFound , HttpRequest , build_request_repr ) <EOL> from django . template import Template , Context , TemplateDoesNotExist <EOL> from django . template . defaultfilters import force_escape , pprint <EOL> from django . utils . html import escape <EOL> from django . utils . importlib import import_module <EOL> from django . utils . encoding import smart_unicode , smart_str <EOL> HIDDEN_SETTINGS = re . compile ( '<STR_LIT>' ) <EOL> CLEANSED_SUBSTITUTE = u'<STR_LIT>' <EOL> def linebreak_iter ( template_source ) : <EOL> yield <NUM_LIT:0> <EOL> p = template_source . find ( '<STR_LIT:\n>' ) <EOL> while p >= <NUM_LIT:0> : <EOL> yield p + <NUM_LIT:1> <EOL> p = template_source . find ( '<STR_LIT:\n>' , p + <NUM_LIT:1> ) <EOL> yield len ( template_source ) + <NUM_LIT:1> <EOL> def cleanse_setting ( key , value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if HIDDEN_SETTINGS . search ( key ) : <EOL> cleansed = CLEANSED_SUBSTITUTE <EOL> else : <EOL> if isinstance ( value , dict ) : <EOL> cleansed = dict ( ( k , cleanse_setting ( k , v ) ) for k , v in value . items ( ) ) <EOL> else : <EOL> cleansed = value <EOL> except TypeError : <EOL> cleansed = value <EOL> return cleansed <EOL> def get_safe_settings ( ) : <EOL> "<STR_LIT>" <EOL> settings_dict = { } <EOL> for k in dir ( settings ) : <EOL> if k . isupper ( ) : <EOL> settings_dict [ k ] = cleanse_setting ( k , getattr ( settings , k ) ) <EOL> return settings_dict <EOL> def technical_500_response ( request , exc_type , exc_value , tb ) : <EOL> """<STR_LIT>""" <EOL> reporter = ExceptionReporter ( request , exc_type , exc_value , tb ) <EOL> if request . is_ajax ( ) : <EOL> text = reporter . get_traceback_text ( ) <EOL> return HttpResponseServerError ( text , mimetype = '<STR_LIT>' ) <EOL> else : <EOL> html = reporter . get_traceback_html ( ) <EOL> return HttpResponseServerError ( html , mimetype = '<STR_LIT>' ) <EOL> default_exception_reporter_filter = None <EOL> def get_exception_reporter_filter ( request ) : <EOL> global default_exception_reporter_filter <EOL> if default_exception_reporter_filter is None : <EOL> modpath = settings . DEFAULT_EXCEPTION_REPORTER_FILTER <EOL> modname , classname = modpath . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> try : <EOL> mod = import_module ( modname ) <EOL> except ImportError , e : <EOL> raise ImproperlyConfigured ( <EOL> '<STR_LIT>' % ( modpath , e ) ) <EOL> try : <EOL> default_exception_reporter_filter = getattr ( mod , classname ) ( ) <EOL> except AttributeError : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % ( modname , classname ) ) <EOL> if request : <EOL> return getattr ( request , '<STR_LIT>' , default_exception_reporter_filter ) <EOL> else : <EOL> return default_exception_reporter_filter <EOL> class ExceptionReporterFilter ( object ) : <EOL> """<STR_LIT>""" <EOL> def get_request_repr ( self , request ) : <EOL> if request is None : <EOL> return repr ( None ) <EOL> else : <EOL> return build_request_repr ( request , POST_override = self . get_post_parameters ( request ) ) <EOL> def get_post_parameters ( self , request ) : <EOL> if request is None : <EOL> return { } <EOL> else : <EOL> return request . POST <EOL> def get_traceback_frame_variables ( self , request , tb_frame ) : <EOL> return tb_frame . f_locals . items ( ) <EOL> class SafeExceptionReporterFilter ( ExceptionReporterFilter ) : <EOL> """<STR_LIT>""" <EOL> def is_active ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return settings . DEBUG is False <EOL> def get_post_parameters ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if request is None : <EOL> return { } <EOL> else : <EOL> sensitive_post_parameters = getattr ( request , '<STR_LIT>' , [ ] ) <EOL> if self . is_active ( request ) and sensitive_post_parameters : <EOL> cleansed = request . POST . copy ( ) <EOL> if sensitive_post_parameters == '<STR_LIT>' : <EOL> for k , v in cleansed . items ( ) : <EOL> cleansed [ k ] = CLEANSED_SUBSTITUTE <EOL> return cleansed <EOL> else : <EOL> for param in sensitive_post_parameters : <EOL> if param in cleansed : <EOL> cleansed [ param ] = CLEANSED_SUBSTITUTE <EOL> return cleansed <EOL> else : <EOL> return request . POST <EOL> def get_traceback_frame_variables ( self , request , tb_frame ) : <EOL> """<STR_LIT>""" <EOL> current_frame = tb_frame . f_back <EOL> sensitive_variables = None <EOL> while current_frame is not None : <EOL> if ( current_frame . f_code . co_name == '<STR_LIT>' <EOL> and '<STR_LIT>' in current_frame . f_locals ) : <EOL> wrapper = current_frame . f_locals [ '<STR_LIT>' ] <EOL> sensitive_variables = getattr ( wrapper , '<STR_LIT>' , None ) <EOL> break <EOL> current_frame = current_frame . f_back <EOL> cleansed = [ ] <EOL> if self . is_active ( request ) and sensitive_variables : <EOL> if sensitive_variables == '<STR_LIT>' : <EOL> for name , value in tb_frame . f_locals . items ( ) : <EOL> cleansed . append ( ( name , CLEANSED_SUBSTITUTE ) ) <EOL> return cleansed <EOL> else : <EOL> for name , value in tb_frame . f_locals . items ( ) : <EOL> if name in sensitive_variables : <EOL> value = CLEANSED_SUBSTITUTE <EOL> elif isinstance ( value , HttpRequest ) : <EOL> value = self . get_request_repr ( value ) <EOL> cleansed . append ( ( name , value ) ) <EOL> return cleansed <EOL> else : <EOL> for name , value in tb_frame . f_locals . items ( ) : <EOL> if isinstance ( value , HttpRequest ) : <EOL> value = self . get_request_repr ( value ) <EOL> cleansed . append ( ( name , value ) ) <EOL> return cleansed <EOL> class ExceptionReporter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , exc_type , exc_value , tb , is_email = False ) : <EOL> self . request = request <EOL> self . filter = get_exception_reporter_filter ( self . request ) <EOL> self . exc_type = exc_type <EOL> self . exc_value = exc_value <EOL> self . tb = tb <EOL> self . is_email = is_email <EOL> self . template_info = None <EOL> self . template_does_not_exist = False <EOL> self . loader_debug_info = None <EOL> if isinstance ( self . exc_type , basestring ) : <EOL> self . exc_value = Exception ( '<STR_LIT>' % self . exc_type ) <EOL> self . exc_type = type ( self . exc_value ) <EOL> def get_traceback_data ( self ) : <EOL> "<STR_LIT>" <EOL> if self . exc_type and issubclass ( self . exc_type , TemplateDoesNotExist ) : <EOL> from django . template . loader import template_source_loaders <EOL> self . template_does_not_exist = True <EOL> self . loader_debug_info = [ ] <EOL> for loader in template_source_loaders : <EOL> try : <EOL> source_list_func = loader . get_template_sources <EOL> template_list = [ { '<STR_LIT:name>' : t , '<STR_LIT>' : os . path . exists ( t ) } for t in source_list_func ( str ( self . exc_value ) ) ] <EOL> except AttributeError : <EOL> template_list = [ ] <EOL> loader_name = loader . __module__ + '<STR_LIT:.>' + loader . __class__ . __name__ <EOL> self . loader_debug_info . append ( { <EOL> '<STR_LIT>' : loader_name , <EOL> '<STR_LIT>' : template_list , <EOL> } ) <EOL> if ( settings . TEMPLATE_DEBUG and <EOL> hasattr ( self . exc_value , '<STR_LIT>' ) ) : <EOL> self . get_template_exception_info ( ) <EOL> frames = self . get_traceback_frames ( ) <EOL> for i , frame in enumerate ( frames ) : <EOL> if '<STR_LIT>' in frame : <EOL> frame [ '<STR_LIT>' ] = [ ( k , force_escape ( pprint ( v ) ) ) for k , v in frame [ '<STR_LIT>' ] ] <EOL> frames [ i ] = frame <EOL> unicode_hint = '<STR_LIT>' <EOL> if self . exc_type and issubclass ( self . exc_type , UnicodeError ) : <EOL> start = getattr ( self . exc_value , '<STR_LIT:start>' , None ) <EOL> end = getattr ( self . exc_value , '<STR_LIT:end>' , None ) <EOL> if start is not None and end is not None : <EOL> unicode_str = self . exc_value . args [ <NUM_LIT:1> ] <EOL> unicode_hint = smart_unicode ( unicode_str [ max ( start - <NUM_LIT:5> , <NUM_LIT:0> ) : min ( end + <NUM_LIT:5> , len ( unicode_str ) ) ] , '<STR_LIT:ascii>' , errors = '<STR_LIT:replace>' ) <EOL> from django import get_version <EOL> c = { <EOL> '<STR_LIT>' : self . is_email , <EOL> '<STR_LIT>' : unicode_hint , <EOL> '<STR_LIT>' : frames , <EOL> '<STR_LIT>' : self . request , <EOL> '<STR_LIT>' : self . filter . get_post_parameters ( self . request ) , <EOL> '<STR_LIT>' : get_safe_settings ( ) , <EOL> '<STR_LIT>' : sys . executable , <EOL> '<STR_LIT>' : '<STR_LIT>' % sys . version_info [ <NUM_LIT:0> : <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : datetime . datetime . now ( ) , <EOL> '<STR_LIT>' : get_version ( ) , <EOL> '<STR_LIT>' : sys . path , <EOL> '<STR_LIT>' : self . template_info , <EOL> '<STR_LIT>' : self . template_does_not_exist , <EOL> '<STR_LIT>' : self . loader_debug_info , <EOL> } <EOL> if self . exc_type : <EOL> c [ '<STR_LIT>' ] = self . exc_type . __name__ <EOL> if self . exc_value : <EOL> c [ '<STR_LIT>' ] = smart_unicode ( self . exc_value , errors = '<STR_LIT:replace>' ) <EOL> if frames : <EOL> c [ '<STR_LIT>' ] = frames [ - <NUM_LIT:1> ] <EOL> return c <EOL> def get_traceback_html ( self ) : <EOL> "<STR_LIT>" <EOL> t = Template ( TECHNICAL_500_TEMPLATE , name = '<STR_LIT>' ) <EOL> c = Context ( self . get_traceback_data ( ) ) <EOL> return t . render ( c ) <EOL> def get_traceback_text ( self ) : <EOL> "<STR_LIT>" <EOL> t = Template ( TECHNICAL_500_TEXT_TEMPLATE , name = '<STR_LIT>' ) <EOL> c = Context ( self . get_traceback_data ( ) , autoescape = False ) <EOL> return t . render ( c ) <EOL> def get_template_exception_info ( self ) : <EOL> origin , ( start , end ) = self . exc_value . django_template_source <EOL> template_source = origin . reload ( ) <EOL> context_lines = <NUM_LIT:10> <EOL> line = <NUM_LIT:0> <EOL> upto = <NUM_LIT:0> <EOL> source_lines = [ ] <EOL> before = during = after = "<STR_LIT>" <EOL> for num , next in enumerate ( linebreak_iter ( template_source ) ) : <EOL> if start >= upto and end <= next : <EOL> line = num <EOL> before = escape ( template_source [ upto : start ] ) <EOL> during = escape ( template_source [ start : end ] ) <EOL> after = escape ( template_source [ end : next ] ) <EOL> source_lines . append ( ( num , escape ( template_source [ upto : next ] ) ) ) <EOL> upto = next <EOL> total = len ( source_lines ) <EOL> top = max ( <NUM_LIT:1> , line - context_lines ) <EOL> bottom = min ( total , line + <NUM_LIT:1> + context_lines ) <EOL> try : <EOL> message = self . exc_value . args [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> message = '<STR_LIT>' <EOL> self . template_info = { <EOL> '<STR_LIT:message>' : message , <EOL> '<STR_LIT>' : source_lines [ top : bottom ] , <EOL> '<STR_LIT>' : before , <EOL> '<STR_LIT>' : during , <EOL> '<STR_LIT>' : after , <EOL> '<STR_LIT>' : top , <EOL> '<STR_LIT>' : bottom , <EOL> '<STR_LIT>' : total , <EOL> '<STR_LIT>' : line , <EOL> '<STR_LIT:name>' : origin . name , <EOL> } <EOL> def _get_lines_from_file ( self , filename , lineno , context_lines , loader = None , module_name = None ) : <EOL> """<STR_LIT>""" <EOL> source = None <EOL> if loader is not None and hasattr ( loader , "<STR_LIT>" ) : <EOL> source = loader . get_source ( module_name ) <EOL> if source is not None : <EOL> source = source . splitlines ( ) <EOL> if source is None : <EOL> try : <EOL> f = open ( filename ) <EOL> try : <EOL> source = f . readlines ( ) <EOL> finally : <EOL> f . close ( ) <EOL> except ( OSError , IOError ) : <EOL> pass <EOL> if source is None : <EOL> return None , [ ] , None , [ ] <EOL> encoding = '<STR_LIT:ascii>' <EOL> for line in source [ : <NUM_LIT:2> ] : <EOL> match = re . search ( r'<STR_LIT>' , line ) <EOL> if match : <EOL> encoding = match . group ( <NUM_LIT:1> ) <EOL> break <EOL> source = [ unicode ( sline , encoding , '<STR_LIT:replace>' ) for sline in source ] <EOL> lower_bound = max ( <NUM_LIT:0> , lineno - context_lines ) <EOL> upper_bound = lineno + context_lines <EOL> pre_context = [ line . strip ( '<STR_LIT:\n>' ) for line in source [ lower_bound : lineno ] ] <EOL> context_line = source [ lineno ] . strip ( '<STR_LIT:\n>' ) <EOL> post_context = [ line . strip ( '<STR_LIT:\n>' ) for line in source [ lineno + <NUM_LIT:1> : upper_bound ] ] <EOL> return lower_bound , pre_context , context_line , post_context <EOL> def get_traceback_frames ( self ) : <EOL> frames = [ ] <EOL> tb = self . tb <EOL> while tb is not None : <EOL> if tb . tb_frame . f_locals . get ( '<STR_LIT>' ) : <EOL> tb = tb . tb_next <EOL> continue <EOL> filename = tb . tb_frame . f_code . co_filename <EOL> function = tb . tb_frame . f_code . co_name <EOL> lineno = tb . tb_lineno - <NUM_LIT:1> <EOL> loader = tb . tb_frame . f_globals . get ( '<STR_LIT>' ) <EOL> module_name = tb . tb_frame . f_globals . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> pre_context_lineno , pre_context , context_line , post_context = self . _get_lines_from_file ( filename , lineno , <NUM_LIT:7> , loader , module_name ) <EOL> if pre_context_lineno is not None : <EOL> frames . append ( { <EOL> '<STR_LIT>' : tb , <EOL> '<STR_LIT:type>' : module_name . startswith ( '<STR_LIT>' ) and '<STR_LIT>' or '<STR_LIT:user>' , <EOL> '<STR_LIT:filename>' : filename , <EOL> '<STR_LIT>' : function , <EOL> '<STR_LIT>' : lineno + <NUM_LIT:1> , <EOL> '<STR_LIT>' : self . filter . get_traceback_frame_variables ( self . request , tb . tb_frame ) , <EOL> '<STR_LIT:id>' : id ( tb ) , <EOL> '<STR_LIT>' : pre_context , <EOL> '<STR_LIT>' : context_line , <EOL> '<STR_LIT>' : post_context , <EOL> '<STR_LIT>' : pre_context_lineno + <NUM_LIT:1> , <EOL> } ) <EOL> tb = tb . tb_next <EOL> return frames <EOL> def format_exception ( self ) : <EOL> """<STR_LIT>""" <EOL> import traceback <EOL> frames = self . get_traceback_frames ( ) <EOL> tb = [ ( f [ '<STR_LIT:filename>' ] , f [ '<STR_LIT>' ] , f [ '<STR_LIT>' ] , f [ '<STR_LIT>' ] ) for f in frames ] <EOL> list = [ '<STR_LIT>' ] <EOL> list += traceback . format_list ( tb ) <EOL> list += traceback . format_exception_only ( self . exc_type , self . exc_value ) <EOL> return list <EOL> def technical_404_response ( request , exception ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> tried = exception . args [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> except ( IndexError , TypeError , KeyError ) : <EOL> tried = [ ] <EOL> else : <EOL> if not tried : <EOL> return empty_urlconf ( request ) <EOL> urlconf = getattr ( request , '<STR_LIT>' , settings . ROOT_URLCONF ) <EOL> if isinstance ( urlconf , types . ModuleType ) : <EOL> urlconf = urlconf . __name__ <EOL> t = Template ( TECHNICAL_404_TEMPLATE , name = '<STR_LIT>' ) <EOL> c = Context ( { <EOL> '<STR_LIT>' : urlconf , <EOL> '<STR_LIT>' : settings . ROOT_URLCONF , <EOL> '<STR_LIT>' : request . path_info [ <NUM_LIT:1> : ] , <EOL> '<STR_LIT>' : tried , <EOL> '<STR_LIT>' : smart_str ( exception , errors = '<STR_LIT:replace>' ) , <EOL> '<STR_LIT>' : request , <EOL> '<STR_LIT>' : get_safe_settings ( ) , <EOL> } ) <EOL> return HttpResponseNotFound ( t . render ( c ) , mimetype = '<STR_LIT>' ) <EOL> def empty_urlconf ( request ) : <EOL> "<STR_LIT>" <EOL> t = Template ( EMPTY_URLCONF_TEMPLATE , name = '<STR_LIT>' ) <EOL> c = Context ( { <EOL> '<STR_LIT>' : settings . SETTINGS_MODULE . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> } ) <EOL> return HttpResponse ( t . render ( c ) , mimetype = '<STR_LIT>' ) <EOL> TECHNICAL_500_TEMPLATE = """<STR_LIT>""" <EOL> TECHNICAL_500_TEXT_TEMPLATE = """<STR_LIT>""" <EOL> TECHNICAL_404_TEMPLATE = """<STR_LIT>""" <EOL> EMPTY_URLCONF_TEMPLATE = """<STR_LIT>""" </s>
