Ticket #3734: debug_with_traceback_hide.patch
File debug_with_traceback_hide.patch, 3.7 KB (added by , 18 years ago) |
---|
-
django/views/debug.py
90 90 exc_type, exc_value, tb, template_info = get_template_exception_info(exc_type, exc_value, tb) 91 91 frames = [] 92 92 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 93 98 filename = tb.tb_frame.f_code.co_filename 94 99 function = tb.tb_frame.f_code.co_name 95 100 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: 98 105 frames.append({ 99 106 'tb': tb, 100 107 'filename': filename, … … 160 167 }) 161 168 return HttpResponseNotFound(t.render(c), mimetype='text/html') 162 169 163 def _get_lines_from_file(filename, lineno, context_lines ):170 def _get_lines_from_file(filename, lineno, context_lines, loader=None, module_name=None): 164 171 """ 165 172 Returns context_lines before and after lineno from file. 166 173 Returns (pre_context_lineno, pre_context, context_line, post_context). 167 174 """ 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, [] 172 189 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 176 192 177 return lower_bound, pre_context, context_line, post_context178 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]] 180 196 197 return lower_bound, pre_context, context_line, post_context 198 181 199 # 182 200 # Templates are embedded in the file so that we know the error handler will 183 201 # always work even if the template loader is broken. … … 313 331 </tr> 314 332 <tr> 315 333 <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> 317 335 </tr> 318 336 </table> 319 337 </div> … … 360 378 <ul class="traceback"> 361 379 {% for frame in frames %} 362 380 <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> 364 382 365 383 {% if frame.context_line %} 366 384 <div class="context" id="c{{ frame.id }}">