Ticket #3734: debug.patch
File debug.patch, 3.3 KB (added by , 18 years ago) |
---|
-
django/views/debug.py
93 93 filename = tb.tb_frame.f_code.co_filename 94 94 function = tb.tb_frame.f_code.co_name 95 95 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: 98 100 frames.append({ 99 101 'tb': tb, 100 102 'filename': filename, … … 160 162 }) 161 163 return HttpResponseNotFound(t.render(c), mimetype='text/html') 162 164 163 def _get_lines_from_file(filename, lineno, context_lines ):165 def _get_lines_from_file(filename, lineno, context_lines, loader=None, module_name=None): 164 166 """ 165 167 Returns context_lines before and after lineno from file. 166 168 Returns (pre_context_lineno, pre_context, context_line, post_context). 167 169 """ 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, [] 172 184 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 176 187 177 return lower_bound, pre_context, context_line, post_context178 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]] 180 191 192 return lower_bound, pre_context, context_line, post_context 193 181 194 # 182 195 # Templates are embedded in the file so that we know the error handler will 183 196 # always work even if the template loader is broken. … … 313 326 </tr> 314 327 <tr> 315 328 <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> 317 330 </tr> 318 331 </table> 319 332 </div> … … 360 373 <ul class="traceback"> 361 374 {% for frame in frames %} 362 375 <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> 364 377 365 378 {% if frame.context_line %} 366 379 <div class="context" id="c{{ frame.id }}">