def _format_stack(frame, maxlen=1024 * 3): frames = [] while frame: frames.append(frame) frame = frame.f_back frames.reverse() stack = [] for frame in frames: filename = frame.f_code.co_filename lineno = frame.f_lineno name = frame.f_code.co_name stack.append(' File "{0}", line {1}, in {2}\n'.format( filename, lineno, name)) try: linecache.checkcache(filename) line = linecache.getline(filename, lineno, frame.f_globals) except KeyError: pass else: stack.append(' {0}\n'.format(line.strip())) trace = ''.join(stack) if trace < maxlen: return trace short = "({0} truncated bytes)".format(len(trace) - maxlen) return short + trace[:maxlen]
def print_tb(tb, limit = None, file = None): """Print up to 'limit' stack trace entries from the traceback 'tb'. If 'limit' is omitted or None, all entries are printed. If 'file' is omitted or None, the output goes to sys.stderr; otherwise 'file' should be an open file or file-like object with a write() method. """ if file is None: file = sys.stderr if limit is None: if hasattr(sys, 'tracebacklimit'): limit = sys.tracebacklimit n = 0 while tb is not None and (limit is None or n < limit): f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename name = co.co_name _print(file, ' File "%s", line %d, in %s' % (filename, lineno, name)) linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) if line: _print(file, ' ' + line.strip()) tb = tb.tb_next n = n + 1 return
def format_frame_info(frame): """ Formats the given stack frame to show its position in the code and part of its context :param frame: A stack frame """ # Same as in traceback.extract_stack line_no = frame.f_lineno code = frame.f_code filename = code.co_filename method_name = code.co_name linecache.checkcache(filename) try: # Try to get the type of the calling object instance = frame.f_locals['self'] method_name = '{0}::{1}'.format(type(instance).__name__, method_name) except KeyError: # Not called from a bound method pass # File & line output_lines = [' File "{0}", line {1}, in {2}' .format(filename, line_no, method_name)] # Arguments if frame.f_locals: # Pypy keeps f_locals as an empty dictionary arg_info = inspect.getargvalues(frame) for name in arg_info.args: try: output_lines.append( ' - {0:s} = {1}'.format( name, repr(frame.f_locals[name]))) except TypeError: # Happens in dict/list-comprehensions in Python 2.x name = name[0] output_lines.append( ' - {0:s} = {1}'.format( name, repr(frame.f_locals[name]))) if arg_info.varargs: output_lines.append( ' - *{0:s} = {1}'.format( arg_info.varargs, frame.f_locals[arg_info.varargs])) if arg_info.keywords: output_lines.append( ' - **{0:s} = {1}'.format( arg_info.keywords, frame.f_locals[arg_info.keywords])) # Line block lines = _extract_lines(filename, frame.f_globals, line_no, 3) if lines: output_lines.append('') prefix = ' ' output_lines.append( '{0}{1}'.format(prefix, '\n{0}'.format(prefix).join(lines))) return '\n'.join(output_lines)
def showtraceback(self, IP, type, value, tb): """Display the exception that just occurred. We remove the first stack item because it is our own code. Strip out references to modules within pyraf unless reprint or debug is set. """ import linecache, traceback, sys, os import IPython.ultraTB # get the color scheme from the user configuration file and pass # it to the trace formatter csm = 'Linux' # default if ipapi: ip = ipapi.get() # this works in vers prior to 1.* csm = ip.options['colors'] linecache.checkcache() tblist = traceback.extract_tb(tb) tbskip = 0 for tb1 in tblist: path, filename = os.path.split(tb1[0]) path = os.path.normpath(os.path.join(os.getcwd(), path)) if path[:len(self.pyrafDir)] == self.pyrafDir or \ path[:len(self.ipythonDir)] == self.ipythonDir or \ filename == "<ipython console>": tbskip += 1 color_tb = IPython.ultraTB.AutoFormattedTB( mode=self.traceback_mode, tb_offset=tbskip, color_scheme=csm) color_tb(type, value, tb)
def print_exception(): import linecache linecache.checkcache() flush_stdout() efile = sys.stderr typ, val, tb = excinfo = sys.exc_info() sys.last_type, sys.last_value, sys.last_traceback = excinfo seen = set() def print_exc(typ, exc, tb): seen.add(exc) context = exc.__context__ cause = exc.__cause__ if cause is not None and cause not in seen: print_exc(type(cause), cause, cause.__traceback__) print("\nThe above exception was the direct cause " "of the following exception:\n", file=efile) elif (context is not None and not exc.__suppress_context__ and context not in seen): print_exc(type(context), context, context.__traceback__) print("\nDuring handling of the above exception, " "another exception occurred:\n", file=efile) if tb: tbe = traceback.extract_tb(tb) print('Traceback (most recent call last):', file=efile) exclude = ("run.py", "rpc.py", "threading.py", "queue.py", "debugger_r.py", "bdb.py") cleanup_traceback(tbe, exclude) traceback.print_list(tbe, file=efile) lines = traceback.format_exception_only(typ, exc) for line in lines: print(line, end='', file=efile) print_exc(typ, val, tb)
def PrintException(msg, web=False): try: LOGPATH = settings.LOG_DIR except: LOGPATH = os.path.join(settings.BASE_DIR, "logs/") if not os.path.exists(LOGPATH): os.makedirs(LOGPATH) exc_type, exc_obj, tb = sys.exc_info() f = tb.tb_frame lineno = tb.tb_lineno filename = f.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') dat = '\n[' + st + ']\n' + msg + \ ' ({0}, LINE {1} "{2}"): {3}'.format( filename, lineno, line.strip(), exc_obj) if platform.system() == "Windows": print dat else: if web: print Color.BOLD + Color.ORANGE + dat + Color.END else: print Color.BOLD + Color.RED + dat + Color.END with open(LOGPATH + 'MobSF.log', 'a') as f: f.write(dat)
def extractStack(frame, stackTraceFrameTag="__stackTraceFrameTag__"): """ | This definition extracts the stack from provided frame. | The code is similar to :func:`traceback.extract_stack` except that it allows frames to be excluded from the stack if the given stack trace frame tag is found in the frame locals and set **True**. :param frame: Frame. ( Frame ) :param stackTraceFrameTag: Stack trace frame tag. ( String ) :return: Stack. ( List ) """ stack = [] while frame is not None: skipFrame = frame.f_locals.get(stackTraceFrameTag) if not skipFrame: lineNumber = frame.f_lineno code = frame.f_code codeName = code.co_name filename = code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineNumber, frame.f_globals) line = line and line.strip() or None stack.append((filename, lineNumber, codeName, line)) frame = frame.f_back stack.reverse() return stack
def display_exception(opt_prepend=None): """ Parse and print details about the exception that is currently being handled. :param opt_prepend: (Optional) A message to prepend before the exception details are printed. """ # Following parsing logic adapted from # http://stackoverflow.com/questions/14519177/ # python-exception-handling-line-number?lq=1 exc_type, exc_obj, exc_tb = sys.exc_info() tb_frame = exc_tb.tb_frame lineno = exc_tb.tb_lineno filename = tb_frame.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, tb_frame.f_globals) print if opt_prepend: print opt_prepend print if str(exc_type) == "<class 'requests.exceptions.ConnectionError'>": print 'Connection Error: {}'.format(exc_obj) print 'Please check your internet connection.' elif str(exc_type) == "<class 'requests.exceptions.HTTPError'>": print 'HTTP Error: {}'.format(exc_obj) else: print 'Error in {} line {}:\n{}{}\n{}{}: {}'.format( filename, lineno, ' ' * 4, line.strip(), ' ' * 8, exc_type, exc_obj) print exit_script()
def loadPluginModule(name, ignoreDeprecation=False): """Loads (and returns) the module for the plugin with the given name.""" files = [] pluginDirs = conf.supybot.directories.plugins()[:] pluginDirs.append(_pluginsDir) for dir in pluginDirs: try: files.extend(os.listdir(dir)) except EnvironmentError: # OSError, IOError superclass. log.warning('Invalid plugin directory: %s; removing.', dir) conf.supybot.directories.plugins().remove(dir) moduleInfo = imp.find_module(name, pluginDirs) try: module = imp.load_module(name, *moduleInfo) except: sys.modules.pop(name, None) keys = sys.modules.keys() for key in keys: if key.startswith(name + '.'): sys.modules.pop(key) raise if 'deprecated' in module.__dict__ and module.deprecated: if ignoreDeprecation: log.warning('Deprecated plugin loaded: %s', name) else: raise Deprecated, format('Attempted to load deprecated plugin %s', name) if module.__name__ in sys.modules: sys.modules[module.__name__] = module linecache.checkcache() return module
def extract_tb(tb, limit = None): list = [] n = 0 while tb is not None and (limit is None or n < limit): f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename name = co.co_name linecache.checkcache(filename) line = "" if '__file__' in f.f_globals: for global_name, x in f.f_globals.items(): if global_name.startswith('_'): continue if inspect.isfunction(x): if global_name == name and x.__code__ == co: args, varargs, varkw, defaults = inspect.getargspec(x) name += inspect.formatargspec(args, varargs, varkw, defaults) elif inspect.isclass(x): method = find_method_in_class(name,co,x) if not method is None: args, varargs, varkw, defaults = inspect.getargspec(method) name += inspect.formatargspec(args, varargs, varkw, defaults) name = x.__name__ + '.' + name if line: line = line.strip() else: line = None list.append((filename, lineno, name, line)) tb = tb.tb_next n = n+1 return list
def boilerpipe(html): try: comm = "java -cp .:class/:libs/boilerpipe-1.2.0.jar:libs/nekohtml-1.9.13.jar:libs/xerces-2.9.1.jar Extract" p=Popen(comm, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True, # to avoid running out of file descriptors bufsize=-1, # fully buffered (use zero (default) if no p.communicate()) universal_newlines=True) # translate newlines, encode/decode text output, errors = p.communicate(input=html) return output except: _, exc_obj, tb = sys.exc_info() f = tb.tb_frame lineno = tb.tb_lineno filename = f.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj) if ('Errno 11' in exc_obj): time.sleep(5) comm = "java -cp .:class/:libs/boilerpipe-1.2.0.jar:libs/nekohtml-1.9.13.jar:libs/xerces-2.9.1.jar Extract" p=Popen(comm, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True, # to avoid running out of file descriptors bufsize=-1, # fully buffered (use zero (default) if no p.communicate()) universal_newlines=True) # translate newlines, encode/decode text output, errors = p.communicate(input=html) return output pass return None
def get_trace(self, frame, tb): """Get a dict of the traceback for wdb.js use""" import linecache frames = [] stack, _ = self.get_stack(frame, tb) current = 0 for i, (stack_frame, lno) in enumerate(stack): code = stack_frame.f_code filename = code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lno, stack_frame.f_globals) if not line: line = self.compile_cache.get(id(code), '') line = to_unicode_string(line, filename) line = line and line.strip() startlnos = dis.findlinestarts(code) lastlineno = list(startlnos)[-1][1] if frame == stack_frame: current = i frames.append({ 'file': filename, 'function': code.co_name, 'flno': code.co_firstlineno, 'llno': lastlineno, 'lno': lno, 'code': line, 'level': i, 'current': frame == stack_frame }) # While in exception always put the context to the top return stack, frames, current
def extract_tb(tb, limit=None): """Return list of up to limit pre-processed entries from traceback. This is useful for alternate formatting of stack traces. If 'limit' is omitted or None, all entries are extracted. A pre-processed stack trace entry is a quadruple (filename, line number, function name, text) representing the information that is usually printed for a stack trace. The text is a string with leading and trailing whitespace stripped; if the source is not available it is None. """ if limit is None: if hasattr(sys, 'tracebacklimit'): limit = sys.tracebacklimit list = [] n = 0 while tb is not None and (limit is None or n < limit): f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename name = co.co_name linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) if line: line = line.strip() else: line = None list.append((filename, lineno, name, line)) tb = tb.tb_next n = n+1 return list
def load_source(self, window=3, lines=None): self.lines = self.line_numbers = None if (self.lineno and ((self.filename and not self.filename.startswith('<') and not self.filename.endswith('>')) or lines)): lineno = self.lineno if not lines: linecache.checkcache(self.filename) sourcelines = linecache.getlines(self.filename, globals()) else: sourcelines = lines lines = [] line_numbers = [] start = max(1, lineno - window) end = min(len(sourcelines), lineno + window) + 1 for i in range(start, end): lines.append(sourcelines[i - 1].rstrip()) line_numbers.append(i) if lines: self.lines = typed.StrList(lines) self.line_numbers = typed.IntList(line_numbers)
def special_extract_tb(tb, limit = None): if limit is None: if hasattr(sys, 'tracebacklimit'): limit = sys.tracebacklimit list = [] n = 0 while tb is not None and (limit is None or n < limit): f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename name = co.co_name linecache.checkcache(filename) line = linecache.getline(filename, lineno) if line: line = line.strip() else: line = None # display where we failed in the sequence if co == Sequence.play.func_code: if line is None: line = '' sequence = f.f_locals['self'] line += '\n Current Sequence:\n%s' % sequence.asText() list.append((filename, lineno, name, line)) tb = tb.tb_next n = n+1 return list
def reset(self): import linecache linecache.checkcache() self.botframe = None self.stopframe = None self.returnframe = None self.quitting = 0
def _task_print_stack(task, limit, file): extracted_list = [] checked = set() for f in task.get_stack(limit=limit): lineno = f.f_lineno co = f.f_code filename = co.co_filename name = co.co_name if filename not in checked: checked.add(filename) linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) extracted_list.append((filename, lineno, name, line)) exc = task._exception if not extracted_list: print('No stack for {!r}'.format(task), file=file) elif exc is not None: print('Traceback for {!r} (most recent call last):'.format(task), file=file) else: print('Stack for {!r} (most recent call last):'.format(task), file=file) traceback.print_list(extracted_list, file=file) if exc is not None: for line in traceback.format_exception_only(exc.__class__, exc): print(line, file=file, end='')
def formatException(self, ei): etype, value, tb = ei[:3] formatted = [] formatted.append("Traceback (most recent call last):") while tb is not None: f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename name = co.co_name item = ' File "%s", line %d, in %s' % (filename, lineno, name) frame_contexts = infos().get(f) if frame_contexts: for ctx in frame_contexts: item += '\n %s' % ctx linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) if line: item += '\n ' + line.strip() formatted.append(item) tb = tb.tb_next formatted.append("%s: %s" % (etype.__name__, value)) return '\n'.join(formatted)
def extract_stack(f=None, limit=None): if f is None: try: raise ZeroDivisionError except ZeroDivisionError: f = sys.exc_info()[2].tb_frame.f_back if limit is None and hasattr(sys, 'tracebacklimit'): limit = sys.tracebacklimit list = [] n = 0 while f is not None: while limit is None or n < limit: lineno = f.f_lineno co = f.f_code filename = co.co_filename name = co.co_name linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) if line: line = line.strip() else: line = None list.append((filename, lineno, name, line)) f = f.f_back n = n + 1 list.reverse() return list
def convert_stack(stack, include_func_start_lineno=False): """Converts a stack extracted using extract_stack() to a traceback stack. Args: stack: A list of n 5-tuples, (filename, lineno, name, frame_globals, func_start_lineno). include_func_start_lineno: True if function start line number should be included as the 5th entry in return tuples. Returns: A list of n 4-tuples or 5-tuples (filename, lineno, name, code, [optional: func_start_lineno]), where the code tuple element is calculated from the corresponding elements of the input tuple. """ ret = [] for (filename, lineno, name, frame_globals, func_start_lineno, unused_frame_info) in stack: linecache.checkcache(filename) line = linecache.getline(filename, lineno, frame_globals) if line: line = line.strip() else: line = None if include_func_start_lineno: ret.append((filename, lineno, name, line, func_start_lineno)) else: ret.append((filename, lineno, name, line)) return ret
def print_exception(temp_filename=None): import linecache linecache.checkcache() flush_stdout() efile = sys.stderr typ, val, tb = excinfo = sys.exc_info() sys.last_type, sys.last_value, sys.last_traceback = excinfo tbe = traceback.extract_tb(tb) print('Traceback (most recent call last):', file=efile) exclude = ("run.py", "rpc.py", "threading.py", "queue.py", "RemoteDebugger.py", "bdb.py") cleanup_traceback(tbe, exclude) if temp_filename is not None: # Replace the name of the temporary file by 'Untitled' main_fname = 'Untitled' new_tbe = [] for t in tbe: fname = main_fname if t[0] == temp_filename else t[0] new_tbe.append((fname, ) + t[1:]) tbe = new_tbe else: main_fname = tbe[0][0] tb_print_list(tbe, main_fname, sys.stdout, efile) lines = traceback.format_exception_only(typ, val) for line in lines: print(line, end='', file=efile)
def print_stack(handle=None): f = sys._getframe(1) output = [] while f: co = f.f_code filename = co.co_filename lineno = f.f_lineno name = co.co_name linecache.checkcache(filename) line = linecache.getline(filename, lineno) # reversed so we can reverse() later if f.f_locals: for k, v in f.f_locals.items(): output.append(' %s = %r\n' % (k, v)) output.append(' Locals:\n') if line: output.append(' %s\n' % line.strip()) output.append(' File "%s", line %d, in %s\n' % ( filename, lineno, name)) f = f.f_back output.reverse() if handle is None: handle = sys.stdout for line in output: handle.write(line)
def manifest_is_a_script(cls, manifest_filepath): """ Returns True if given "manifest" is really a script. """ linecache.checkcache(manifest_filepath) first_line = linecache.getline(manifest_filepath, 1) return first_line.startswith("#!")
def test_checkcache(self): getline = linecache.getline # Create a source file and cache its contents source_name = support.TESTFN + '.py' self.addCleanup(support.unlink, source_name) with open(source_name, 'w') as source: source.write(SOURCE_1) getline(source_name, 1) # Keep a copy of the old contents source_list = [] with open(source_name) as source: for index, line in enumerate(source): self.assertEqual(line, getline(source_name, index + 1)) source_list.append(line) with open(source_name, 'w') as source: source.write(SOURCE_2) # Try to update a bogus cache entry linecache.checkcache('dummy') # Check that the cache matches the old contents for index, line in enumerate(source_list): self.assertEqual(line, getline(source_name, index + 1)) # Update the cache and check whether it matches the new source file linecache.checkcache(source_name) with open(source_name) as source: for index, line in enumerate(source): self.assertEqual(line, getline(source_name, index + 1)) source_list.append(line)
def reregister_app(self, path): """ If new functionality has been addedd (i.e. a new transform, existing transform altered but its class not renamed) to the file this will be updated, HOWEVER if a transform has been deleted/renamed then the old transform will remain memory resident :( this is a limitation of reload() see: http://mail.python.org/pipermail/python-list/2001-March/075683.html """ debug("REreg %s"%path) m_obj=self._get_module_obj_from_path(path) ##Remove object from maltego.Application() & from out self.module_table self.unregister_app(path) ##Reload the module to get changes into memory reload(m_obj) ##Update line cache - see explanation in _checkfor_ghosts ----- linecache.checkcache(path) ##NASTY HACK m_trns=self._check_for_ghosts(self._find_maltego_app(m_obj)) ##Add to our tracking table self.mod_table[m_obj]=m_trns ##Now register the transforms from the reloaded module with the maltego.Application() for trans in m_trns: self.maltego_obj.register(trans())
def _load_module_from_path(self, path): """ From a specified path string import the module if it is new to us, or reload it if we have seen it before. Also force a linecache update """ try: name, ext = os.path.splitext(path) name=os.path.split(name)[1] if ext == '.py' and name not in self.skip_list: ##potentially it is possible for an app that appears new to be an app that was previously registered, ##removed, and then added back again - possibly with changes in this case we need to reload it, not import it m_obj=self._get_module_obj_from_path(path) if m_obj: debug("Seen this module before, reloading NOT importing") m=reload(m_obj) ##Update line cache - see explanation in _checkfor_ghosts - this is to defend against ## a module being 'deleted' and then put back in the dir with class changes - ghosts can show up linecache.checkcache(path) else: debug( "Never seen this module before, IMPORTING from scratch") m=__import__(name) return m #except Exception, err: except OSError: debug ("Exception in _load_module_from_path: %s"%(err)) debug ("not importing %s"%(path) ) return None
def print_tb(tb, limit=None, file=None): """Print up to 'limit' stack trace entries from the traceback 'tb'. If 'limit' is omitted or None, all entries are printed. If 'file' is omitted or None, the output goes to sys.stderr; otherwise 'file' should be an open file or file-like object with a write() method. """ if file is None: file = sys.stderr if limit is None: if hasattr(sys, 'tracebacklimit'): limit = sys.tracebacklimit n = 0 # Pyston change: for (filename, name, lineno) in tb.getLines(): if limit and n >= limit: break _print(file, ' File "%s", line %d, in %s' % (filename, lineno, name)) linecache.checkcache(filename) line = linecache.getline(filename, lineno, None) if line: _print(file, ' ' + line.strip()) n = n+1 """
def extract_stack(f=None, limit=None): """Extract the raw traceback from the current stack frame. The return value has the same format as for extract_tb(). The optional 'f' and 'limit' arguments have the same meaning as for print_stack(). Each item in the list is a quadruple (filename, line number, function name, text), and the entries are in order from oldest to newest stack frame. """ if f is None: try: raise ZeroDivisionError except ZeroDivisionError: f = sys.exc_info()[2].tb_frame.f_back if limit is None: if hasattr(sys, 'tracebacklimit'): limit = sys.tracebacklimit list = [] n = 0 while f is not None and (limit is None or n < limit): lineno = f.f_lineno co = f.f_code filename = co.co_filename name = co.co_name linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) if line: line = line.strip() else: line = None list.append((filename, lineno, name, line)) f = f.f_back n = n+1 list.reverse() return list
def print_stack(self, *, limit=None, file=None): """Print the stack or traceback for this task's coroutine. This produces output similar to that of the traceback module, for the frames retrieved by get_stack(). The limit argument is passed to get_stack(). The file argument is an I/O stream to which the output is written; by default output is written to sys.stderr. """ extracted_list = [] checked = set() for f in self.get_stack(limit=limit): lineno = f.f_lineno co = f.f_code filename = co.co_filename name = co.co_name if filename not in checked: checked.add(filename) linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) extracted_list.append((filename, lineno, name, line)) exc = self._exception if not extracted_list: print('No stack for %r' % self, file=file) elif exc is not None: print('Traceback for %r (most recent call last):' % self, file=file) else: print('Stack for %r (most recent call last):' % self, file=file) traceback.print_list(extracted_list, file=file) if exc is not None: for line in traceback.format_exception_only(exc.__class__, exc): print(line, file=file, end='')
def sys_exc_info( level, infos, main=None ): try: filename = infos[ 2 ].tb_frame.f_code.co_filename write_line = "%s" % ( os.path.basename( os.path.splitext( filename )[ 0 ] ), ) if main: write_line += "::%s" % ( main[ 0 ].__class__.__name__, ) lineno = infos[ 2 ].tb_lineno write_line += "::%s (%d)" % ( infos[ 2 ].tb_frame.f_code.co_name, lineno, ) next = infos[ 2 ].tb_next if next is not None: write_line += " in %s" % ( next.tb_frame.f_code.co_name, ) linecache.checkcache( filename ) try:# python 2.5 strline = linecache.getline( filename, lineno, infos[ 2 ].tb_frame.f_globals ) except: strline = linecache.getline( filename, lineno ) if strline: write_line += ", %s" % ( repr( strline.strip() ), ) write_line += " - %s" % ( infos[ 1 ], ) xbmc_log( level, write_line ) except: print_exc()
def traceback_to_string(etype, value, tb): lines = [] while tb is not None: file_info = None line = None f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename file_info = ' File "{}", line {}, in {}'.format(filename, lineno, co.co_name) linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) lines.append(file_info) lines.append(' ' + line.strip()) tb = tb.tb_next lines.insert(0, 'Traceback (most recent call last):') exc_lines = traceback.format_exception_only(etype, value) for xln in exc_lines: exc_line = xln.rstrip('\r\n') if exc_line: lines.append(exc_line) return '\n'.join(lines)
def showtraceback(self, reprint=0): """Display the exception that just occurred. We remove the first stack item because it is our own code. Strip out references to modules within pyraf unless reprint or debug is set. """ try: if reprint: if self.lasttrace is None: return type, value, tbmod = self.lasttrace else: type, value, tb = sys.exc_info() linecache.checkcache() sys.last_type = type sys.last_value = value sys.last_traceback = tb tblist = traceback.extract_tb(tb) del tblist[:1] self.lasttrace = type, value, tblist if self.debug: tbmod = tblist else: tbmod = [] for tb1 in tblist: path, filename = os.path.split(tb1[0]) path = os.path.normpath(os.path.join( os.getcwd(), path)) if path[:len(pyrafDir)] != pyrafDir: tbmod.append(tb1) list = traceback.format_list(tbmod) if list: list.insert(0, "Traceback (innermost last):\n") list[len(list):] = traceback.format_exception_only(type, value) finally: tbmod = tblist = tb = None for item in list: self.write(item)
def PrintException(logger: logging.Logger = None) -> None: """ default try - exception cannot display line number. make us hard to track thr problem the solution is using this function describe in. https://stackoverflow.com/questions/14519177/python-exception-handling-line-number Examples: try: print(1/0) except: PrintException() """ exc_type, exc_obj, tb = sys.exc_info() f = tb.tb_frame lineno = tb.tb_lineno filename = f.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) if logger is None: print('EXCEPTION IN ({}, LINE {} "{}"): {}'.format( filename, lineno, line.strip(), exc_obj))
def print_tb(tb, limit=None, file=None): if file is None: file = sys.stderr if limit is None: if hasattr(sys, 'tracebacklimit'): limit = sys.tracebacklimit n = 0 while tb is not None and (limit is None or n < limit): f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename name = co.co_name _print(file, ' File "%s", line %d, in %s' % (filename, lineno, name)) linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) if line: _print(file, ' ' + line.strip()) tb = tb.tb_next n = n + 1 return
def _extract_tb_or_stack_iter(curr, limit, extractor): if limit is None: limit = getattr(sys, 'tracebacklimit', None) n = 0 while curr is not None and (limit is None or n < limit): f, lineno, next_item = extractor(curr) co = f.f_code filename = co.co_filename name = co.co_name linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) if line: line = line.strip() else: line = None yield (filename, lineno, name, line) curr = next_item n += 1
def _format_stack(task): ''' Formats a traceback from a stack of coroutines/generators ''' extracted_list = [] checked = set() for f in _get_stack(task): lineno = f.f_lineno co = f.f_code filename = co.co_filename name = co.co_name if filename not in checked: checked.add(filename) linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) extracted_list.append((filename, lineno, name, line)) if not extracted_list: resp = 'No stack for %r' % task else: resp = 'Stack for %r (most recent call last):\n' % task resp += ''.join(traceback.format_list(extracted_list)) return resp
def structured_traceback(self, etype, value, elist, tb_offset=None, context=5): # If the source file has been edited, the line in the syntax error can # be wrong (retrieved from an outdated cache). This replaces it with # the current value. if isinstance(value, SyntaxError) \ and isinstance(value.filename, str) \ and isinstance(value.lineno, int): linecache.checkcache(value.filename) newtext = linecache.getline(value.filename, value.lineno) if newtext: value.text = newtext self.last_syntax_error = value return super(SyntaxTB, self).structured_traceback(etype, value, elist, tb_offset=tb_offset, context=context)
def _file_line(self, tb): """formats the file / lineno / function line of a traceback element""" prefix = "file://" prefix = "" f = tb.tb_frame if '__unittest' in f.f_globals: # this is the magical flag that prevents unittest internal # code from junking up the stacktrace return None filename = f.f_code.co_filename lineno = tb.tb_lineno linecache.checkcache(filename) function_name = f.f_code.co_name line_contents = linecache.getline(filename, lineno, f.f_globals).strip() return " %s line %s in %s\n %s" % ( termstyle.blue(prefix, self._relative_path(filename)), lineno, termstyle.cyan(function_name), line_contents)
def get_exception_str(): """Get pretty exception string.""" _, exc_obj, tb = sys.exc_info() last_tb = tb while True: try: tb.tb_frame except Exception: break else: last_tb = tb tb = tb.tb_next f = last_tb.tb_frame lineno = last_tb.tb_lineno filename = f.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) err_str = 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format( filename, lineno, line.strip(), exc_obj) return err_str
def test_checkcache(self): getline = linecache.getline source_name = support.TESTFN + '.py' self.addCleanup(support.unlink, source_name) with open(source_name, 'w') as source: source.write(SOURCE_1) getline(source_name, 1) source_list = [] with open(source_name) as source: for index, line in enumerate(source): self.assertEqual(line, getline(source_name, index + 1)) source_list.append(line) with open(source_name, 'w') as source: source.write(SOURCE_2) linecache.checkcache('dummy') for index, line in enumerate(source_list): self.assertEqual(line, getline(source_name, index + 1)) linecache.checkcache(source_name) with open(source_name) as source: for index, line in enumerate(source): self.assertEqual(line, getline(source_name, index + 1)) source_list.append(line)
def stack_trace(e): """ Returns the stack trace for a general exception. Arguments: e = Exception """ exc_type, exc_obj, tb = sys.exc_info() f = tb.tb_frame lineno = tb.tb_lineno filenameEx = f.f_code.co_filename filename = filenameEx.split("/") filename = filename[len(filename) - 1] filename = filename.replace(".py", "") filename = filename.replace(".pyc", "") linecache.checkcache(filename) line = linecache.getline(filenameEx, lineno, f.f_globals) exceptionDetail = "Exception in %s.%s line %i:\n\t\t%s\n\t\t CODE:\n\t\t\t%s" % ( filename, f.f_code.co_name, lineno, str(e), line.replace("\t", "")) return exceptionDetail
def extract_tb(tb, limit=None): if limit is None and hasattr(sys, 'tracebacklimit'): limit = sys.tracebacklimit list = [] n = 0 while tb is not None: while limit is None or n < limit: f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename name = co.co_name linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) if line: line = line.strip() else: line = None list.append((filename, lineno, name, line)) tb = tb.tb_next n = n + 1 return list
def predict_url(url, proba=False): try: if isinstance(url, str): features = mod_feature_extraction.extract_features_from_website( url, "PREDICT", True) x_pred = pd.DataFrame(features) else: x_pred = url x_pred = x_pred[[ 'Ratio Similarity', 'Ratio Description Sim', 'Number HREF', 'Number DIV', 'Number LI', 'Ratio Title Sim', 'Number Span', 'Number UL', 'Has Bond Status', 'Number Image', 'Ratio Copyright Sim', 'Number PhishyTokens', 'Number Extern Links', 'Number Button', 'Number Inputs', 'Number Paragr', 'Ratio Unique Links', 'Has Freq Domain Extern', 'Has Copyright', 'Has Button', 'Has Redirect', 'Has iFrame', 'Has Extern Content', 'Has Meta', 'Has Input', 'Number Option', 'Has Action', 'Number OL', 'Number TR', 'Has Hidden Element', 'Number Checkbox' ]] if proba: y_pred = random_forest_pre_loaded.predict_proba(x_pred) else: y_pred = random_forest_pre_loaded.predict(x_pred) return y_pred except Exception as e: exc_type, exc_obj, tb = sys.exc_info() f = tb.tb_frame lineno = tb.tb_lineno filename = f.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) print('EXCEPTION IN ({}, LINE {} "{}"): {}'.format( filename, lineno, line.strip(), exc_obj)) log(action_logging_enum=WARNING, logging_text=str(e)) log(action_logging_enum=WARNING, logging_text=str(e.__traceback__)) return None
def on_trace_back(type, value, tb): import linecache stacks = [] localvars = [] try: while tb: f = tb.tb_frame c = f.f_code linecache.checkcache(c.co_filename) line = linecache.getline(c.co_filename, f.f_lineno, f.f_globals) stacks.append('File "%s", in %s\n > %d: %s' % (c.co_filename, c.co_name, f.f_lineno, line.strip() if line else "N/A")) f.f_locals.pop("__builtins__", None) localvars.append('\n'.join((("\t%19s : %s" % (n, repr(v)[:128])) for n, v in f.f_locals.items()))) tb = tb.tb_next except Exception as e: stacks.append(str(e)) stacks.append("%s" % type.__name__) localvars.append(" > %s" % str(value)) trace = "\n".join(("%s\n%s" % (s, l) for s, l in zip(stacks, localvars))) return trace
def loadPluginModule(name, ignoreDeprecation=False): """Loads (and returns) the module for the plugin with the given name.""" files = [] pluginDirs = conf.supybot.directories.plugins()[:] pluginDirs.append(_pluginsDir) for dir in pluginDirs: try: files.extend(os.listdir(dir)) except EnvironmentError: # OSError, IOError superclass. log.warning('Invalid plugin directory: %s; removing.', dir) conf.supybot.directories.plugins().remove(dir) if name not in files: search = lambda x: re.search(r'(?i)^%s$' % (name,), x) matched_names = list(filter(search, files)) if len(matched_names) >= 1: name = matched_names[0] else: raise ImportError(name) moduleInfo = imp.find_module(name, pluginDirs) try: module = imp.load_module(name, *moduleInfo) except: sys.modules.pop(name, None) keys = list(sys.modules.keys()) for key in keys: if key.startswith(name + '.'): sys.modules.pop(key) raise if 'deprecated' in module.__dict__ and module.deprecated: if ignoreDeprecation: log.warning('Deprecated plugin loaded: %s', name) else: raise Deprecated(format('Attempted to load deprecated plugin %s', name)) if module.__name__ in sys.modules: sys.modules[module.__name__] = module linecache.checkcache() return module
def Log(self, exception_title="", ex_type=logging.ERROR): log_data = "" if ex_type == logging.ERROR or ex_type == logging.CRITICAL: (execution_type, message, tb) = sys.exc_info() f = tb.tb_frame lineno = tb.tb_lineno fname = f.f_code.co_filename.split("\\")[-1] linecache.checkcache(fname) target = linecache.getline(fname, lineno, f.f_globals) line_len = len(str(message)) + 10 log_data = f"{exception_title}\n{'File:'.ljust(9)}{fname}\n{'Target:'.ljust(9)}{target.strip()}\n{'Message:'.ljust(9)}{message}\n{'Line:'.ljust(9)}{lineno}\n" log_data += ("-" * line_len) else: log_data = exception_title if ex_type == logging.ERROR or ex_type == logging.CRITICAL: print("-" * 23) print(f"{self.RED} {exception_title} {self.COLOR_RESET}") print("-" * 23) if ex_type == logging.DEBUG: logger.debug(log_data) elif ex_type == logging.INFO: logger.info(log_data) elif ex_type == logging.WARNING: logger.warning(log_data) elif ex_type == logging.ERROR: logger.error(log_data) elif ex_type == logging.CRITICAL: logger.critical(log_data)
def getsource(obj): """Get the source code of an object.""" if inspect.isclass(obj): # From Python 3.9 inspect library obj = inspect.unwrap(obj) file = inspect.getsourcefile(obj) if file: # Invalidate cache if needed. linecache.checkcache(file) else: file = inspect.getfile(obj) # Allow filenames in form of "<something>" to pass through. # `doctest` monkeypatches `linecache` module to enable # inspection, so let `linecache.getlines` to be called. if not (file.startswith('<') and file.endswith('>')): raise OSError('source code not available') module = inspect.getmodule(obj, file) if module: lines = linecache.getlines(file, module.__dict__) else: lines = linecache.getlines(file) if not lines: raise OSError('could not get source code') qualname = obj.__qualname__ source = ''.join(lines) tree = ast.parse(source) class_finder = _ClassFinder(qualname) try: class_finder.visit(tree) except ClassFoundException as e: line_number = e.args[0] return ''.join(inspect.getblock(lines[line_number:])) else: raise OSError('could not find class definition') else: return getattr(obj, '__inject_code__', inspect.getsource(obj))
def excepthook(exc_type, exc_value, traceback): locals_proc_cache = {} def process_var(k, v): if k == 'self': if id(v) in locals_proc_cache: return locals_proc_cache[id(v)] res = {'class': v.__class__.__name__} if hasattr(v, 'id'): res.update({'id': v.id}) locals_proc_cache[id(v)] = res return res return v def process_locals(locals_): return dict((k, process_var(k, v)) for k, v in locals_.iteritems()) traceback_msg = "[Traceback Extended:]" + linesep parent = traceback while parent: fm = parent.tb_frame filename = fm.f_code.co_filename lineno = parent.tb_lineno name = fm.f_code.co_name linecache.checkcache(filename) line = linecache.getline(filename, lineno, fm.f_globals) traceback_msg += ' File "%s", line %d, in %s' % (filename, lineno, name) + linesep traceback_msg += ' ' + line.strip() + linesep traceback_msg += ' locals: {0}'.format(process_locals( fm.f_locals)) + linesep parent = parent.tb_next traceback_msg += ', '.join( [l.strip() for l in format_exception_only(exc_type, exc_value)]) + linesep traceback_msg += "[/Traceback Extended:]" + linesep locals_proc_cache = None logger.error(traceback_msg)
def test_checkcache(self): getline = linecache.getline try: # Create a source file and cache its contents source_name = support.TESTFN + '.py' with open(source_name, 'w') as source: source.write(SOURCE_1) source.close() getline(source_name, 1) # Keep a copy of the old contents source_list = [] source = open(source_name) for index, line in enumerate(source): self.assertEquals(line, getline(source_name, index + 1)) source_list.append(line) source.close() source = open(source_name, 'w') source.write(SOURCE_2) source.close() # Try to update a bogus cache entry linecache.checkcache('dummy') # Check that the cache matches the old contents for index, line in enumerate(source_list): self.assertEquals(line, getline(source_name, index + 1)) # Update the cache and check whether it matches the new source file linecache.checkcache(source_name) source = open(source_name) for index, line in enumerate(source): self.assertEquals(line, getline(source_name, index + 1)) source_list.append(line) finally: support.unlink(source_name)
def extract_tb(tb, limit=None): list = [] n = 0 while tb is not None and (limit is None or n < limit): f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename name = co.co_name linecache.checkcache(filename) line = "" if '__file__' in f.f_globals: for global_name, x in f.f_globals.items(): if global_name.startswith('_'): continue if inspect.isfunction(x): if global_name == name and x.__code__ == co: args, varargs, varkw, defaults = inspect.getargspec(x) name += inspect.formatargspec(args, varargs, varkw, defaults) elif inspect.isclass(x): method = find_method_in_class(name, co, x) if not method is None: args, varargs, varkw, defaults = inspect.getargspec( method) name += inspect.formatargspec(args, varargs, varkw, defaults) name = x.__name__ + '.' + name if line: line = line.strip() else: line = None list.append((filename, lineno, name, line)) tb = tb.tb_next n = n + 1 return list
def extract_stack(f=None, limit = None): """Extract the raw traceback from the current stack frame. The return value has the same format as for extract_tb(). The optional 'f' and 'limit' arguments have the same meaning as for print_stack(). Each item in the list is a quadruple (filename, line number, function name, text), and the entries are in order from oldest to newest stack frame. """ if f is None: # Pyston change: """ try: raise ZeroDivisionError except ZeroDivisionError: f = sys.exc_info()[2].tb_frame.f_back """ f = sys._getframe(1) if limit is None: if hasattr(sys, 'tracebacklimit'): limit = sys.tracebacklimit list = [] n = 0 while f is not None and (limit is None or n < limit): lineno = f.f_lineno co = f.f_code filename = co.co_filename name = co.co_name linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) if line: line = line.strip() else: line = None list.append((filename, lineno, name, line)) f = f.f_back n = n+1 list.reverse() return list
def run_watch_loop(self) -> NoReturn: restart = True stats: Counter[str] = Counter() active_messages: Dict[Tuple[str, int], AnalysisMessage] while True: if restart: clear_screen() clear_line('-') line = f' Analyzing {len(self._modtimes)} files. \r' sys.stdout.write(color(line, AnsiColor.OKBLUE)) max_condition_timeout = 0.5 restart = False stats = Counter() active_messages = {} else: time.sleep(0.5) max_condition_timeout *= 2 for curstats, messages in self.run_iteration( max_condition_timeout): debug('stats', curstats, messages) stats.update(curstats) if messages_merged(active_messages, messages): linecache.checkcache() clear_screen() for message in active_messages.values(): lines = long_describe_message(message) if lines is None: continue clear_line('-') print(lines, end='') clear_line('-') line = f' Analyzed {stats["num_paths"]} paths in {len(self._modtimes)} files. \r' sys.stdout.write(color(line, AnsiColor.OKBLUE)) if self._change_flag: self._change_flag = False restart = True line = f' Restarting analysis over {len(self._modtimes)} files. \r' sys.stdout.write(color(line, AnsiColor.OKBLUE))
def updateData(self, args): self.expandedINames = set(args.get("expanded", [])) self.typeformats = args.get("typeformats", {}) self.formats = args.get("formats", {}) self.watchers = args.get("watchers", {}) self.output = "data={" # Trigger error to get a backtrace. frame = None #self.warn("frame: %s" % frame) try: raise ZeroDivisionError except ZeroDivisionError: frame = sys.exc_info()[2].tb_frame.f_back limit = 30 n = 0 isActive = False while frame is not None and n < limit: #self.warn("frame: %s" % frame.f_locals.keys()) lineno = frame.f_lineno code = frame.f_code filename = code.co_filename name = code.co_name if isActive: linecache.checkcache(filename) line = linecache.getline(filename, lineno, frame.f_globals) self.dumpFrame(frame) if name == "<module>": isActive = False if name == "trace_dispatch": isActive = True frame = frame.f_back n = n + 1 #sys.stdout.flush() self.output += '}' self.flushOutput()
def load(self, name): """load a bot module by name""" self.bot.log(2, 'LOADING: %s' % name) if name in self.modules_list: self.bot.log(1, 'ALREADY LOADED: %s' % name) return 0 module = None # first, try loading from eggs for entrypoint in pkg_resources.iter_entry_points("kibot.modules"): if entrypoint.name == name: self.bot.log( 5, 'LOADING MODULE: %s (%s)' % (entrypoint, entrypoint.dist)) module = entrypoint.load(require=True) if module: break if module is None: # legacy module loading from the load_path for directory in self.bot.op.modules.load_path: self.bot.log(5, 'LOOKING IN: %s' % directory) stuff = Loader.find_module_in_dir(name, directory) if stuff: module = Loader.load_module(name, stuff) if module: break if module: cls = getattr(module, name) # look for class of same name self.modules[name] = (module, cls(self.bot)) self.modules_list.append(name) linecache.checkcache() self._stash() return 1 else: self.bot.log(0, 'MODULE NOT FOUND: %s' % name) return 0
def report_err(save=None, comment=None, show=True): """ :param save: (str) - name of file to export error message to :param comment: (str) - additional comments to append to error report :param show: (boolean): whether to print report :return tb: (str) - get a formatted traceback message """ dtnow_str = datetime.datetime.utcnow().strftime('%b %d %H:%MZ') exception_type, value, traceback = sys.exc_info() while 1: if not traceback.tb_next: break traceback = traceback.tb_next frame = traceback.tb_frame lineno = traceback.tb_lineno filename = frame.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, frame.f_globals) error_fmt = '{0} {1} - {2} on line {3} in {4}: "{5}"' error_report = error_fmt.format(dtnow_str, exception_type.__name__, value, lineno, filename, line.strip(' ').strip('\n')) if comment is not None: error_report += ' - {0}'.format(comment) if show: print(error_report) if save is not None: if '.txt' not in save: save += '.txt' with open(save, 'a') as f: f.write(error_report) f.write('\n')
def print_exception(): import linecache linecache.checkcache() flush_stdout() efile = sys.stderr typ, val, tb = excinfo = sys.exc_info() sys.last_type, sys.last_value, sys.last_traceback = excinfo seen = set() def print_exc(typ, exc, tb): seen.add(id(exc)) context = exc.__context__ cause = exc.__cause__ if cause is not None and id(cause) not in seen: print_exc(type(cause), cause, cause.__traceback__) print( "\nThe above exception was the direct cause " "of the following exception:\n", file=efile) elif (context is not None and not exc.__suppress_context__ and id(context) not in seen): print_exc(type(context), context, context.__traceback__) print( "\nDuring handling of the above exception, " "another exception occurred:\n", file=efile) if tb: tbe = traceback.extract_tb(tb) print('Traceback (most recent call last):', file=efile) exclude = ("run.py", "rpc.py", "threading.py", "queue.py", "debugger_r.py", "bdb.py") cleanup_traceback(tbe, exclude) traceback.print_list(tbe, file=efile) lines = traceback.format_exception_only(typ, exc) for line in lines: print(line, end='', file=efile) print_exc(typ, val, tb)
def print_traceback(tb=None): """ Prints the traceback with nice colors and such. """ if tb is None: _, _, tb = sys.exc_info() while tb is not None: frame = tb.tb_frame lineno = tb.tb_lineno code = frame.f_code filename = code.co_filename name = code.co_name with indent(2): puts('File "%s", line %s, in %s' % (colored.blue(filename), colored.cyan(lineno), colored.cyan(name))) linecache.checkcache(filename) line = linecache.getline(filename, lineno, frame.f_globals) if line: with indent(2): puts(colored.black(line.strip())) tb = tb.tb_next
def error(self, msg, tag="ERROR"): current_time = datetime.now().strftime("%I:%M:%S.%f %p") exc_type, exc_obj, tb = sys.exc_info() f = tb.tb_frame lineno = tb.tb_lineno filename = f.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) msg = msg + f"\n{lineno} → {line}" text = "[{}] {} [{}] → {}".format(current_time, self.identifier, tag, msg) print(stylize(text, colored.fg("red"))) hook = Webhook("https://discordapp.com/api/webhooks/633365674592305173/oZZVdJNQOxByuJTT93_CzbFiZ-Wi-j9Zre26zrHpmkDXjHsXEyAoFOU_it-4DVW3pQAC") embed = Embed( color = "10027008", description = msg ) embed.set_author(str(socket.gethostname())) embed.set_title(f":warning:Error from **{self.identifier}**:warning:") hook.send(embed=embed)
def _format_traceback_line(self, tb): """ Formats the file / lineno / function line of a traceback element. Returns None is the line is not relevent to the user i.e. inside the test runner. """ if self._is_relevant_tb_level(tb): return None f = tb.tb_frame filename = f.f_code.co_filename lineno = tb.tb_lineno linecache.checkcache(filename) function_name = f.f_code.co_name line_contents = linecache.getline(filename, lineno, f.f_globals).strip() return " %s line %s in %s\n %s" % ( termstyle.blue(self._relative_path(filename) if self.use_relative_path else filename), termstyle.bold(termstyle.cyan(lineno)), termstyle.cyan(function_name), line_contents )
def filter_traceback(tb, excludes=None): tb_list = [] while tb is not None: f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code name = co.co_name modname = inspect.getmodule(co).__name__ if excludes is not None: if any(modname.startswith(prefix) for prefix in excludes): tb = tb.tb_next continue filename = co.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) err_at = 'module %s, line %d in %s' % (modname, lineno, name) tb_list.append({'at': err_at, 'code': line.strip()}) tb = tb.tb_next return tb_list