traceback2.py (5434B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\brennivin\traceback2.py 2 import linecache as _linecache 3 import pprint as _pprint 4 import sys as _sys 5 from traceback import format_exception_only as _format_exception_only 6 from . import compat as _compat 7 FORMAT_NORMAL = 0 8 FORMAT_LOGSRV = 2 9 FORMAT_SINGLE = 4 10 LAYOUT_FOR_FORMAT_NORMAL = ' File "%s", line %d, in %s' 11 LAYOUT_FOR_FORMAT_LOGSRV = '%s(%s) %s' 12 13 def print_exc(limit = None, file = None, show_locals = 0, format = FORMAT_NORMAL): 14 _getfile(file).write(format_exc(limit, show_locals, format)) 15 16 17 def format_exc(limit = None, show_locals = 0, format = FORMAT_NORMAL): 18 etype, value, tb = _sys.exc_info() 19 try: 20 return ''.join(format_exception(etype, value, tb, limit, show_locals, format)) 21 finally: 22 del etype 23 del value 24 del tb 25 26 27 def print_exception(etype, value, tb, limit = None, file = None, show_locals = 0, format = FORMAT_NORMAL): 28 _getfile(file).write(''.join(format_exception(etype, value, tb, limit, show_locals, format))) 29 30 31 def format_exception(etype, value, tb, limit = None, show_locals = 0, format = FORMAT_NORMAL): 32 lst = [] 33 if tb: 34 lst.append('Traceback (most recent call last):\n') 35 lst.extend(format_tb(tb, limit, show_locals, format)) 36 return lst + _format_exception_only(etype, value) 37 38 39 def print_stack(f = None, limit = None, up = 0, show_locals = 0, format = FORMAT_NORMAL, file = None): 40 if f is None: 41 up += 1 42 _getfile(file).write(''.join(format_stack(f, limit, up, show_locals, format))) 43 44 45 def format_stack(f = None, limit = None, up = 0, show_locals = 0, format = FORMAT_NORMAL): 46 if f is None: 47 up += 1 48 return format_list(extract_stack(f, limit, up, show_locals), show_locals, format) 49 50 51 def print_tb(tb, limit = None, file = None, show_locals = 0, format = FORMAT_NORMAL): 52 _getfile(file).write(''.join(format_tb(tb, limit, show_locals, format))) 53 54 55 def format_tb(tb, limit = None, show_locals = 0, format = FORMAT_NORMAL): 56 return format_list(extract_tb(tb, limit, show_locals), show_locals, format) 57 58 59 def format_list(extracted_list, show_locals = 0, format = FORMAT_NORMAL): 60 if show_locals < 0: 61 start_locals = 0 62 else: 63 start_locals = len(extracted_list) - show_locals 64 layoutTemplate = LAYOUT_FOR_FORMAT_LOGSRV if format & FORMAT_LOGSRV else LAYOUT_FOR_FORMAT_NORMAL 65 data = [] 66 for i, (pathName, lineNumber, functionName, lineText, f_locals) in enumerate(extracted_list): 67 item = layoutTemplate % (pathName, lineNumber, functionName) 68 if lineText: 69 if format & FORMAT_SINGLE: 70 item += ' : %s\n' % (lineText.strip(),) 71 else: 72 item += '\n %s\n' % (lineText.strip(),) 73 else: 74 item += '\n' 75 if i >= start_locals: 76 item += ''.join(_format_locals(f_locals, format)) 77 data.append(item) 78 79 return data 80 81 82 def _format_locals(f_locals, format): 83 lines = [] 84 if f_locals is None: 85 return lines 86 for key, value in sorted(f_locals.items()): 87 if format & FORMAT_LOGSRV: 88 extra = ' %s = ' % (key,) 89 else: 90 extra = '%20s = ' % (key,) 91 try: 92 width = 253 - len(extra) 93 val = _pprint.pformat(value, depth=1, width=width) 94 if len(val) > 1024: 95 val = val[:1024] + '...' 96 vlines = val.splitlines() 97 if len(vlines) > 4: 98 vlines[4:] = ['...'] 99 for i in _compat.xrange(1, len(vlines)): 100 vlines[i] = ' ' * 23 + vlines[i] 101 102 extra += '\n'.join(vlines) + '\n' 103 except Exception as e: 104 try: 105 extra += '<error printing value: %r>' % (e,) 106 except Exception: 107 extra += '<error printing value>' 108 109 lines.append(extra) 110 111 return lines 112 113 114 def extract_tb(tb, limit = None, extract_locals = 0): 115 frames = [] 116 n = 1 117 while tb is not None and (limit is None or n < limit): 118 frames.append((tb.tb_frame, tb.tb_lineno)) 119 tb = tb.tb_next 120 n += 1 121 122 return _extract_frames(frames, extract_locals) 123 124 125 def extract_stack(f = None, limit = None, up = 0, extract_locals = 0): 126 if f is None: 127 try: 128 raise ZeroDivisionError 129 except ZeroDivisionError: 130 f = _sys.exc_info()[2].tb_frame.f_back 131 132 frames = [] 133 n = 0 134 while f is not None and (limit is None or n < limit + up): 135 frames.append((f, f.f_lineno)) 136 f = f.f_back 137 n += 1 138 139 frames.reverse() 140 if up > 0: 141 del frames[-up:] 142 return _extract_frames(frames, extract_locals) 143 144 145 def _extract_frames(frames, extract_locals = 0): 146 result = [] 147 if extract_locals >= 0: 148 j = len(frames) - extract_locals 149 else: 150 j = 0 151 for i, (f, lineno) in enumerate(frames): 152 co = f.f_code 153 filename = co.co_filename 154 name = co.co_name 155 _linecache.checkcache(filename) 156 line = _linecache.getline(filename, lineno, f.f_globals) 157 if line: 158 line = line.strip() 159 else: 160 line = None 161 locals = f.f_locals if i >= j else None 162 result.append((filename, 163 lineno, 164 name, 165 line, 166 locals)) 167 168 return result 169 170 171 def _getfile(file): 172 if file is None: 173 file = _sys.stderr 174 return file