Changeset 1373
- Timestamp:
- 11/23/05 13:37:17 (3 years ago)
- Files:
-
- django/branches/new-admin/django/views/debug.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/new-admin/django/views/debug.py
r1361 r1373 1 import re2 import os3 import sys4 import inspect5 1 from django.conf import settings 2 from django.core.template import Template, Context 3 from django.utils.html import escape 4 from django.utils.httpwrappers import HttpResponseServerError, HttpResponseNotFound 5 import inspect, os, re, sys 6 from itertools import count, izip 6 7 from os.path import dirname, join as pathjoin 7 from django.core.template import Template, Context8 from django.utils.httpwrappers import HttpResponseServerError, HttpResponseNotFound9 from itertools import count, izip10 from django.utils.html import escape11 8 12 9 HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD') 13 10 14 11 def linebreak_iter(template_source): 15 import re 16 newline_re = re.compile("^", re.M) 17 for match in newline_re.finditer(template_source): 18 yield match.start() 19 yield len(template_source) + 1 20 21 def get_template_exception_info(exc_type,exc_value,tb): 12 newline_re = re.compile("^", re.M) 13 for match in newline_re.finditer(template_source): 14 yield match.start() 15 yield len(template_source) + 1 16 17 def get_template_exception_info(exc_type, exc_value, tb): 22 18 origin, (start, end) = exc_value.source 23 19 template_source = origin.reload() … … 29 25 linebreaks.next() # skip the nothing before initial line start 30 26 for num, next in linebreaks: 31 if start >= upto and end <= next :27 if start >= upto and end <= next: 32 28 line = num 33 29 before = escape(template_source[upto:start]) 34 30 during = escape(template_source[start:end]) 35 31 after = escape(template_source[end:next - 1]) 36 37 32 source_lines.append( (num, escape(template_source[upto:next - 1])) ) 38 33 upto = next 39 40 34 total = len(source_lines) 41 35 42 36 top = max(0, line - context_lines) 43 37 bottom = min(total, line + 1 + context_lines) 44 45 template_info = {46 'message' : exc_value.args[0],47 'source_lines': source_lines[top:bottom],48 'before' : before,49 'during': during,50 'after': after,51 'top': top ,52 'bottom': bottom ,53 'total': total,54 'line': line,55 'name': origin.name,56 }57 exc_info = hasattr(exc_value, 'exc_info') and exc_value.exc_info or (exc_type, exc_value,tb)38 39 template_info = { 40 'message': exc_value.args[0], 41 'source_lines': source_lines[top:bottom], 42 'before': before, 43 'during': during, 44 'after': after, 45 'top': top , 46 'bottom': bottom , 47 'total': total, 48 'line': line, 49 'name': origin.name, 50 } 51 exc_info = hasattr(exc_value, 'exc_info') and exc_value.exc_info or (exc_type, exc_value, tb) 58 52 return exc_info + (template_info,) 59 53 … … 65 59 template_info = None 66 60 if settings.TEMPLATE_DEBUG and hasattr(exc_value, 'source'): 67 exc_type, exc_value, tb, template_info = get_template_exception_info(exc_type, exc_value,tb)61 exc_type, exc_value, tb, template_info = get_template_exception_info(exc_type, exc_value, tb) 68 62 frames = [] 69 63 while tb is not None: … … 73 67 pre_context_lineno, pre_context, context_line, post_context = _get_lines_from_file(filename, lineno, 7) 74 68 frames.append({ 75 'tb' : tb,76 'filename' : filename,77 'function' : function,78 'lineno' : lineno,79 'vars' : tb.tb_frame.f_locals.items(),80 'id' : id(tb),81 'pre_context' : pre_context,82 'context_line' : context_line,83 'post_context' : post_context,84 'pre_context_lineno' : pre_context_lineno,69 'tb': tb, 70 'filename': filename, 71 'function': function, 72 'lineno': lineno, 73 'vars': tb.tb_frame.f_locals.items(), 74 'id': id(tb), 75 'pre_context': pre_context, 76 'context_line': context_line, 77 'post_context': post_context, 78 'pre_context_lineno': pre_context_lineno, 85 79 }) 86 80 tb = tb.tb_next … … 98 92 t = Template(TECHNICAL_500_TEMPLATE) 99 93 c = Context({ 100 'exception_type' : exc_type.__name__,101 'exception_value' : exc_value,102 'frames' : frames,103 'lastframe' : frames[-1],104 'request' : request,105 'request_protocol' : os.environ.get("HTTPS") == "on" and "https" or "http",106 'settings' : settings_dict,94 'exception_type': exc_type.__name__, 95 'exception_value': exc_value, 96 'frames': frames, 97 'lastframe': frames[-1], 98 'request': request, 99 'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http", 100 'settings': settings_dict, 107 101 'template_info': template_info, 108 102 }) … … 121 115 t = Template(TECHNICAL_404_TEMPLATE) 122 116 c = Context({ 123 'root_urlconf' : settings.ROOT_URLCONF,124 'urlpatterns' : tried,125 'reason' : str(exception),126 'request' : request,127 'request_protocol' : os.environ.get("HTTPS") == "on" and "https" or "http",128 'settings' : dict([(k, getattr(settings, k)) for k in dir(settings) if k.isupper()]),117 'root_urlconf': settings.ROOT_URLCONF, 118 'urlpatterns': tried, 119 'reason': str(exception), 120 'request': request, 121 'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http", 122 'settings': dict([(k, getattr(settings, k)) for k in dir(settings) if k.isupper()]), 129 123 }) 130 124 return HttpResponseNotFound(t.render(c), mimetype='text/html') … … 196 190 #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; } 197 191 #requestinfo h3 { margin-bottom:-1em; } 198 table.source td { font-family: monospace; white-space: pre;}199 span.specific {background:#ffcab7;}200 .error { background: #ffc; }192 table.source td { font-family: monospace; white-space: pre; } 193 span.specific { background:#ffcab7; } 194 .error { background: #ffc; } 201 195 </style> 202 196 <script type="text/javascript"> … … 276 270 </table> 277 271 </div> 278 {% if template_info %}272 {% if template_info %} 279 273 <div id="template"> 280 274 <h2>Template</h2> 281 In template {{ template_info.name}}, error at line {{template_info.line}}282 <div>{{ template_info.message|escape}}</div>283 <table class="source{% if template_info.top%} cut-top{%endif%}{%ifnotequal template_info.bottom template_info.total%} cut-bottom{%endifnotequal%}">275 In template {{ template_info.name }}, error at line {{ template_info.line }} 276 <div>{{ template_info.message|escape }}</div> 277 <table class="source{% if template_info.top %} cut-top{% endif %}{% ifnotequal template_info.bottom template_info.total %} cut-bottom{% endifnotequal %}"> 284 278 {% for source_line in template_info.source_lines %} 285 {% ifequal source_line.0 template_info.line %}286 <tr class="error"><td>{{ source_line.0}}</td>287 <td> {{template_info.before}}<span class="specific">{{template_info.during}}</span>{{template_info.after}}</td></tr>288 {% else%}289 <tr><td>{{ source_line.0}}</td>290 <td> {{ source_line.1}}</td></tr>291 {% endifequal%}292 {% endfor%}279 {% ifequal source_line.0 template_info.line %} 280 <tr class="error"><td>{{ source_line.0 }}</td> 281 <td>{{ template_info.before }}<span class="specific">{{ template_info.during }}</span>{{ template_info.after }}</td></tr> 282 {% else %} 283 <tr><td>{{ source_line.0 }}</td> 284 <td> {{ source_line.1 }}</td></tr> 285 {% endifequal %} 286 {% endfor %} 293 287 </table> 294 288 </div> 295 {% endif%}289 {% endif %} 296 290 <div id="traceback"> 297 291 <h2>Traceback <span>(innermost last)</span></h2>
