Django

Code

Changeset 1373

Show
Ignore:
Timestamp:
11/23/05 13:37:17 (3 years ago)
Author:
adrian
Message:

new-admin: Negligible syntax changes in django.views.debug in preparation for merge to trunk

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/new-admin/django/views/debug.py

    r1361 r1373  
    1 import re 
    2 import os 
    3 import sys 
    4 import inspect 
    51from django.conf import settings 
     2from django.core.template import Template, Context 
     3from django.utils.html import escape 
     4from django.utils.httpwrappers import HttpResponseServerError, HttpResponseNotFound 
     5import inspect, os, re, sys 
     6from itertools import count, izip 
    67from os.path import dirname, join as pathjoin 
    7 from django.core.template import Template, Context 
    8 from django.utils.httpwrappers import HttpResponseServerError, HttpResponseNotFound 
    9 from itertools import count, izip 
    10 from django.utils.html import escape 
    118 
    129HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD') 
    1310 
    1411def 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 
     17def get_template_exception_info(exc_type, exc_value, tb): 
    2218    origin, (start, end) = exc_value.source 
    2319    template_source = origin.reload() 
     
    2925    linebreaks.next() # skip the nothing before initial line start 
    3026    for num, next in linebreaks: 
    31         if start >= upto and end <= next
     27        if start >= upto and end <= next
    3228            line = num 
    3329            before = escape(template_source[upto:start]) 
    3430            during = escape(template_source[start:end]) 
    3531            after = escape(template_source[end:next - 1]) 
    36          
    3732        source_lines.append( (num, escape(template_source[upto:next - 1])) ) 
    3833        upto = next 
    39          
    4034    total = len(source_lines) 
    41      
     35 
    4236    top = max(0, line - context_lines) 
    4337    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) 
    5852    return exc_info + (template_info,) 
    5953 
     
    6559    template_info = None 
    6660    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) 
    6862    frames = [] 
    6963    while tb is not None: 
     
    7367        pre_context_lineno, pre_context, context_line, post_context = _get_lines_from_file(filename, lineno, 7) 
    7468        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, 
    8579        }) 
    8680        tb = tb.tb_next 
     
    9892    t = Template(TECHNICAL_500_TEMPLATE) 
    9993    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, 
    107101        'template_info': template_info, 
    108102    }) 
     
    121115    t = Template(TECHNICAL_404_TEMPLATE) 
    122116    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()]), 
    129123    }) 
    130124    return HttpResponseNotFound(t.render(c), mimetype='text/html') 
     
    196190    #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; } 
    197191    #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; } 
    201195  </style> 
    202196  <script type="text/javascript"> 
     
    276270  </table> 
    277271</div> 
    278 {%if template_info %} 
     272{% if template_info %} 
    279273<div id="template"> 
    280274   <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 %}"> 
    284278   {% 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 %} 
    293287   </table> 
    294288</div> 
    295 {%endif%} 
     289{% endif %} 
    296290<div id="traceback"> 
    297291  <h2>Traceback <span>(innermost last)</span></h2>