<s> from django . conf import settings <EOL> from django . template import Template , Context , TemplateDoesNotExist <EOL> from django . utils . html import escape <EOL> from django . http import HttpResponseServerError , HttpResponseNotFound <EOL> import os , re <EOL> HIDDEN_SETTINGS = re . compile ( '<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 get_template_exception_info ( exc_type , exc_value , tb ) : <EOL> origin , ( start , end ) = exc_value . 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> template_info = { <EOL> '<STR_LIT:message>' : exc_value . args [ <NUM_LIT:0> ] , <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> exc_info = hasattr ( exc_value , '<STR_LIT>' ) and exc_value . exc_info or ( exc_type , exc_value , tb ) <EOL> return exc_info + ( template_info , ) <EOL> def get_safe_settings ( ) : <EOL> "<STR_LIT>" <EOL> settings_dict = { } <EOL> for k in dir ( settings ) : <EOL> if k . isupper ( ) : <EOL> if HIDDEN_SETTINGS . search ( k ) : <EOL> settings_dict [ k ] = '<STR_LIT>' <EOL> else : <EOL> settings_dict [ k ] = getattr ( settings , k ) <EOL> return settings_dict <EOL> def technical_500_response ( request , exc_type , exc_value , tb ) : <EOL> """<STR_LIT>""" <EOL> template_info = None <EOL> template_does_not_exist = False <EOL> loader_debug_info = None <EOL> if issubclass ( exc_type , TemplateDoesNotExist ) : <EOL> from django . template . loader import template_source_loaders <EOL> template_does_not_exist = True <EOL> loader_debug_info = [ ] <EOL> for loader in template_source_loaders : <EOL> try : <EOL> source_list_func = getattr ( __import__ ( loader . __module__ , { } , { } , [ '<STR_LIT>' ] ) , '<STR_LIT>' ) <EOL> template_list = [ { '<STR_LIT:name>' : t , '<STR_LIT>' : os . path . exists ( t ) } for t in source_list_func ( str ( exc_value ) ) ] <EOL> except ( ImportError , AttributeError ) : <EOL> template_list = [ ] <EOL> loader_debug_info . append ( { <EOL> '<STR_LIT>' : loader . __module__ + '<STR_LIT:.>' + loader . __name__ , <EOL> '<STR_LIT>' : template_list , <EOL> } ) <EOL> if settings . TEMPLATE_DEBUG and hasattr ( exc_value , '<STR_LIT:source>' ) : <EOL> exc_type , exc_value , tb , template_info = get_template_exception_info ( exc_type , exc_value , tb ) <EOL> frames = [ ] <EOL> while tb is not None : <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> pre_context_lineno , pre_context , context_line , post_context = _get_lines_from_file ( filename , lineno , <NUM_LIT:7> ) <EOL> if pre_context_lineno : <EOL> frames . append ( { <EOL> '<STR_LIT>' : tb , <EOL> '<STR_LIT:filename>' : filename , <EOL> '<STR_LIT>' : function , <EOL> '<STR_LIT>' : lineno + <NUM_LIT:1> , <EOL> '<STR_LIT>' : tb . tb_frame . f_locals . items ( ) , <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> if not frames : <EOL> frames = [ { <EOL> '<STR_LIT:filename>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:?>' , <EOL> '<STR_LIT>' : '<STR_LIT:?>' , <EOL> } ] <EOL> t = Template ( TECHNICAL_500_TEMPLATE , name = '<STR_LIT>' ) <EOL> c = Context ( { <EOL> '<STR_LIT>' : exc_type . __name__ , <EOL> '<STR_LIT>' : exc_value , <EOL> '<STR_LIT>' : frames , <EOL> '<STR_LIT>' : frames [ - <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : request , <EOL> '<STR_LIT>' : request . is_secure ( ) and "<STR_LIT>" or "<STR_LIT:http>" , <EOL> '<STR_LIT>' : get_safe_settings ( ) , <EOL> '<STR_LIT>' : template_info , <EOL> '<STR_LIT>' : template_does_not_exist , <EOL> '<STR_LIT>' : loader_debug_info , <EOL> } ) <EOL> return HttpResponseServerError ( t . render ( c ) , mimetype = '<STR_LIT>' ) <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 ) : <EOL> tried = [ ] <EOL> else : <EOL> if not tried : <EOL> return empty_urlconf ( request ) <EOL> t = Template ( TECHNICAL_404_TEMPLATE , name = '<STR_LIT>' ) <EOL> c = Context ( { <EOL> '<STR_LIT>' : settings . ROOT_URLCONF , <EOL> '<STR_LIT>' : tried , <EOL> '<STR_LIT>' : str ( exception ) , <EOL> '<STR_LIT>' : request , <EOL> '<STR_LIT>' : request . is_secure ( ) and "<STR_LIT>" or "<STR_LIT:http>" , <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 HttpResponseNotFound ( t . render ( c ) , mimetype = '<STR_LIT>' ) <EOL> def _get_lines_from_file ( filename , lineno , context_lines ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> source = open ( filename ) . readlines ( ) <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> except ( OSError , IOError ) : <EOL> return None , [ ] , None , [ ] <EOL> TECHNICAL_500_TEMPLATE = """<STR_LIT>""" <EOL> TECHNICAL_404_TEMPLATE = """<STR_LIT>""" <EOL> EMPTY_URLCONF_TEMPLATE = """<STR_LIT>""" </s>
