Django

Code

Changeset 5051

Show
Ignore:
Timestamp:
04/20/07 23:37:31 (2 years ago)
Author:
mtredinnick
Message:

Fixed #3734 -- Added support for import hooks to the debugging traceback
output. Also respect hidden traceback frames. Thanks to Armin Ronacher.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r5042 r5051  
    181181    remco@diji.biz 
    182182    rhettg@gmail.com 
     183    Armin Ronacher 
    183184    Oliver Rutherfurd <http://rutherfurd.net/> 
    184185    Ivan Sagalaev (Maniac) <http://www.softwaremaniacs.org/> 
  • django/trunk/django/views/debug.py

    r4959 r5051  
    9191    frames = [] 
    9292    while tb is not None: 
     93        # support for __traceback_hide__ which is used by a few libraries 
     94        # to hide internal frames. 
     95        if tb.tb_frame.f_locals.get('__traceback_hide__'): 
     96            tb = tb.tb_next 
     97            continue 
    9398        filename = tb.tb_frame.f_code.co_filename 
    9499        function = tb.tb_frame.f_code.co_name 
    95100        lineno = tb.tb_lineno - 1 
    96         pre_context_lineno, pre_context, context_line, post_context = _get_lines_from_file(filename, lineno, 7) 
    97         if pre_context_lineno: 
     101        loader = tb.tb_frame.f_globals.get('__loader__') 
     102        module_name = tb.tb_frame.f_globals.get('__name__') 
     103        pre_context_lineno, pre_context, context_line, post_context = _get_lines_from_file(filename, lineno, 7, loader, module_name) 
     104        if pre_context_lineno is not None: 
    98105            frames.append({ 
    99106                'tb': tb, 
     
    162169    return HttpResponseNotFound(t.render(c), mimetype='text/html') 
    163170 
    164 def _get_lines_from_file(filename, lineno, context_lines): 
     171def _get_lines_from_file(filename, lineno, context_lines, loader=None, module_name=None): 
    165172    """ 
    166173    Returns context_lines before and after lineno from file. 
    167174    Returns (pre_context_lineno, pre_context, context_line, post_context). 
    168175    """ 
    169     try: 
    170         source = open(filename).readlines() 
    171         lower_bound = max(0, lineno - context_lines) 
    172         upper_bound = lineno + context_lines 
    173  
    174         pre_context = [line.strip('\n') for line in source[lower_bound:lineno]] 
    175         context_line = source[lineno].strip('\n') 
    176         post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]] 
    177  
    178         return lower_bound, pre_context, context_line, post_context 
    179     except (OSError, IOError): 
     176    source = None 
     177    if loader is not None: 
     178        source = loader.get_source(module_name).splitlines() 
     179    else: 
     180        try: 
     181            f = open(filename) 
     182            try: 
     183                source = f.readlines() 
     184            finally: 
     185                f.close() 
     186        except (OSError, IOError): 
     187            pass 
     188    if source is None: 
    180189        return None, [], None, [] 
     190 
     191    lower_bound = max(0, lineno - context_lines) 
     192    upper_bound = lineno + context_lines 
     193 
     194    pre_context = [line.strip('\n') for line in source[lower_bound:lineno]] 
     195    context_line = source[lineno].strip('\n') 
     196    post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]] 
     197 
     198    return lower_bound, pre_context, context_line, post_context 
    181199 
    182200# 
     
    315333    <tr> 
    316334      <th>Exception Location:</th> 
    317       <td>{{ lastframe.filename }} in {{ lastframe.function }}, line {{ lastframe.lineno }}</td> 
     335      <td>{{ lastframe.filename|escape }} in {{ lastframe.function|escape }}, line {{ lastframe.lineno }}</td> 
    318336    </tr> 
    319337  </table> 
     
    362380      {% for frame in frames %} 
    363381        <li class="frame"> 
    364           <code>{{ frame.filename }}</code> in <code>{{ frame.function }}</code> 
     382          <code>{{ frame.filename|escape }}</code> in <code>{{ frame.function|escape }}</code> 
    365383 
    366384          {% if frame.context_line %}