Ticket #3734: debug_with_traceback_hide.patch

File debug_with_traceback_hide.patch, 3.7 KB (added by Armin Ronacher, 17 years ago)

like the other patch but with additional support for traceback_hide

  • django/views/debug.py

     
    9090        exc_type, exc_value, tb, template_info = get_template_exception_info(exc_type, exc_value, tb)
    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,
    100107                'filename': filename,
     
    160167    })
    161168    return HttpResponseNotFound(t.render(c), mimetype='text/html')
    162169
    163 def _get_lines_from_file(filename, lineno, context_lines):
     170def _get_lines_from_file(filename, lineno, context_lines, loader=None, module_name=None):
    164171    """
    165172    Returns context_lines before and after lineno from file.
    166173    Returns (pre_context_lineno, pre_context, context_line, post_context).
    167174    """
    168     try:
    169         source = open(filename).readlines()
    170         lower_bound = max(0, lineno - context_lines)
    171         upper_bound = lineno + context_lines
     175    source = None
     176    if loader is not None:
     177        source = loader.get_source(module_name).splitlines()
     178    else:
     179        try:
     180            f = open(filename)
     181            try:
     182                source = f.readlines()
     183            finally:
     184                f.close()
     185        except (OSError, IOError):
     186            pass
     187    if source is None:
     188        return None, [], None, []
    172189
    173         pre_context = [line.strip('\n') for line in source[lower_bound:lineno]]
    174         context_line = source[lineno].strip('\n')
    175         post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]]
     190    lower_bound = max(0, lineno - context_lines)
     191    upper_bound = lineno + context_lines
    176192
    177         return lower_bound, pre_context, context_line, post_context
    178     except (OSError, IOError):
    179         return None, [], None, []
     193    pre_context = [line.strip('\n') for line in source[lower_bound:lineno]]
     194    context_line = source[lineno].strip('\n')
     195    post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]]
    180196
     197    return lower_bound, pre_context, context_line, post_context
     198
    181199#
    182200# Templates are embedded in the file so that we know the error handler will
    183201# always work even if the template loader is broken.
     
    313331    </tr>
    314332    <tr>
    315333      <th>Exception Location:</th>
    316       <td>{{ lastframe.filename }} in {{ lastframe.function }}, line {{ lastframe.lineno }}</td>
     334      <td>{{ lastframe.filename|escape }} in {{ lastframe.function|escape }}, line {{ lastframe.lineno }}</td>
    317335    </tr>
    318336  </table>
    319337</div>
     
    360378    <ul class="traceback">
    361379      {% for frame in frames %}
    362380        <li class="frame">
    363           <code>{{ frame.filename }}</code> in <code>{{ frame.function }}</code>
     381          <code>{{ frame.filename|escape }}</code> in <code>{{ frame.function|escape }}</code>
    364382
    365383          {% if frame.context_line %}
    366384            <div class="context" id="c{{ frame.id }}">
Back to Top