Ticket #3734: debug.patch

File debug.patch, 3.3 KB (added by Armin Ronacher, 17 years ago)

adds loader support to django

  • django/views/debug.py

     
    9393        filename = tb.tb_frame.f_code.co_filename
    9494        function = tb.tb_frame.f_code.co_name
    9595        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:
     96        loader = tb.tb_frame.f_globals.get('__loader__')
     97        module_name = tb.tb_frame.f_globals.get('__name__')
     98        pre_context_lineno, pre_context, context_line, post_context = _get_lines_from_file(filename, lineno, 7, loader, module_name)
     99        if pre_context_lineno is not None:
    98100            frames.append({
    99101                'tb': tb,
    100102                'filename': filename,
     
    160162    })
    161163    return HttpResponseNotFound(t.render(c), mimetype='text/html')
    162164
    163 def _get_lines_from_file(filename, lineno, context_lines):
     165def _get_lines_from_file(filename, lineno, context_lines, loader=None, module_name=None):
    164166    """
    165167    Returns context_lines before and after lineno from file.
    166168    Returns (pre_context_lineno, pre_context, context_line, post_context).
    167169    """
    168     try:
    169         source = open(filename).readlines()
    170         lower_bound = max(0, lineno - context_lines)
    171         upper_bound = lineno + context_lines
     170    source = None
     171    if loader is not None:
     172        source = loader.get_source(module_name).splitlines()
     173    else:
     174        try:
     175            f = open(filename)
     176            try:
     177                source = f.readlines()
     178            finally:
     179                f.close()
     180        except (OSError, IOError):
     181            pass
     182    if source is None:
     183        return None, [], None, []
    172184
    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]]
     185    lower_bound = max(0, lineno - context_lines)
     186    upper_bound = lineno + context_lines
    176187
    177         return lower_bound, pre_context, context_line, post_context
    178     except (OSError, IOError):
    179         return None, [], None, []
     188    pre_context = [line.strip('\n') for line in source[lower_bound:lineno]]
     189    context_line = source[lineno].strip('\n')
     190    post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]]
    180191
     192    return lower_bound, pre_context, context_line, post_context
     193
    181194#
    182195# Templates are embedded in the file so that we know the error handler will
    183196# always work even if the template loader is broken.
     
    313326    </tr>
    314327    <tr>
    315328      <th>Exception Location:</th>
    316       <td>{{ lastframe.filename }} in {{ lastframe.function }}, line {{ lastframe.lineno }}</td>
     329      <td>{{ lastframe.filename|escape }} in {{ lastframe.function|escape }}, line {{ lastframe.lineno }}</td>
    317330    </tr>
    318331  </table>
    319332</div>
     
    360373    <ul class="traceback">
    361374      {% for frame in frames %}
    362375        <li class="frame">
    363           <code>{{ frame.filename }}</code> in <code>{{ frame.function }}</code>
     376          <code>{{ frame.filename|escape }}</code> in <code>{{ frame.function|escape }}</code>
    364377
    365378          {% if frame.context_line %}
    366379            <div class="context" id="c{{ frame.id }}">
Back to Top