我们从Python开源项目中,提取了以下26个代码示例,用于说明如何使用inspect.getinnerframes()。
def fix_frame_records_filenames(records): """Try to fix the filenames in each record from inspect.getinnerframes(). Particularly, modules loaded from within zip files have useless filenames attached to their code object, and inspect.getinnerframes() just uses it. """ fixed_records = [] for frame, filename, line_no, func_name, lines, index in records: # Look inside the frame's globals dictionary for __file__, # which should be better. However, keep Cython filenames since # we prefer the source filenames over the compiled .so file. if not filename.endswith(('.pyx', '.pxd', '.pxi')): better_fn = frame.f_globals.get('__file__', None) if isinstance(better_fn, str): # Check the type just in case someone did something weird with # __file__. It might also be None if the error occurred during # import. filename = better_fn fixed_records.append((frame, filename, line_no, func_name, lines, index)) return fixed_records
def get_records(self, etb, number_of_lines_of_context, tb_offset): try: # Try the default getinnerframes and Alex's: Alex's fixes some # problems, but it generates empty tracebacks for console errors # (5 blanks lines) where none should be returned. return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) except UnicodeDecodeError: # This can occur if a file's encoding magic comment is wrong. # I can't see a way to recover without duplicating a bunch of code # from the stdlib traceback module. --TK error('\nUnicodeDecodeError while processing traceback.\n') return None except: # FIXME: I've been getting many crash reports from python 2.3 # users, traceable to inspect.py. If I can find a small test-case # to reproduce this, I should either write a better workaround or # file a bug report against inspect (if that's the real problem). # So far, I haven't been able to find an isolated example to # reproduce the problem. inspect_error() traceback.print_exc(file=self.ostream) info('\nUnfortunately, your original traceback can not be constructed.\n') return None
def char_parse_exception(error_object, *args, ch): # Parser for exceptions with a CH entity for extra msging merc.GDF = False wrap_call = inspect.getinnerframes(sys.exc_info()[2]) if ch.level == merc.ML: ch.send("An Exception Occurred: \n%s %s\n\n" % (type(error_object), str(error_object))) logger.debug("Exception: %s %s" % (type(error_object), str(error_object))) for call_info in reversed(wrap_call): local_calls = call_info[0].f_locals if '_logged__tracer_var_' in local_calls: continue if ch.level == merc.ML: ch.send("--Frame Trace-- \nFile: %s \nFunction: %s \nLine: %d \nCode: %s " % (call_info[1], call_info[3], call_info[2], call_info[4][0].lstrip())) ch.send("\n") logger.debug("--Frame Trace-- \nFile: %s \nFunction: %s \nLine: %d \nCode: %s " % (call_info[1], call_info[3], call_info[2], call_info[4][0].lstrip())) logger.debug("Local Env Variables: ") for k, v in local_calls.items(): levtrace = value_to_str(v) logger.debug("%s : %s", k, levtrace)
def fix_frame_records_filenames(records): """Try to fix the filenames in each record from inspect.getinnerframes(). Particularly, modules loaded from within zip files have useless filenames attached to their code object, and inspect.getinnerframes() just uses it. """ fixed_records = [] for frame, filename, line_no, func_name, lines, index in records: # Look inside the frame's globals dictionary for __file__, which should # be better. better_fn = frame.f_globals.get('__file__', None) if isinstance(better_fn, str): # Check the type just in case someone did something weird with # __file__. It might also be None if the error occurred during # import. filename = better_fn fixed_records.append((frame, filename, line_no, func_name, lines, index)) return fixed_records
def fix_frame_records_filenames(records): """Try to fix the filenames in each record from inspect.getinnerframes(). Particularly, modules loaded from within zip files have useless filenames attached to their code object, and inspect.getinnerframes() just uses it. """ fixed_records = [] for frame, filename, line_no, func_name, lines, index in records: # Look inside the frame's globals dictionary for __file__, # which should be better. However, keep Cython filenames since # we prefer the source filenames over the compiled .so file. filename = py3compat.cast_unicode_py2(filename, "utf-8") if not filename.endswith(('.pyx', '.pxd', '.pxi')): better_fn = frame.f_globals.get('__file__', None) if isinstance(better_fn, str): # Check the type just in case someone did something weird with # __file__. It might also be None if the error occurred during # import. filename = better_fn fixed_records.append((frame, filename, line_no, func_name, lines, index)) return fixed_records
def get_records(self, etb, number_of_lines_of_context, tb_offset): try: # Try the default getinnerframes and Alex's: Alex's fixes some # problems, but it generates empty tracebacks for console errors # (5 blanks lines) where none should be returned. return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) except: # FIXME: I've been getting many crash reports from python 2.3 # users, traceable to inspect.py. If I can find a small test-case # to reproduce this, I should either write a better workaround or # file a bug report against inspect (if that's the real problem). # So far, I haven't been able to find an isolated example to # reproduce the problem. inspect_error() traceback.print_exc(file=self.ostream) info('\nUnfortunately, your original traceback can not be constructed.\n') return None
def extract_traceback(tb, context=1): frames = inspect.getinnerframes(tb, context) for frame, filename, lineno, function, code_context, index in frames: formatted_args, cls = _get_frame_args(frame) if cls: function = '%s.%s' % (cls, function) yield TracebackEntry(filename, lineno, function, formatted_args, code_context, index)
def _fixed_getinnerframes(etb, context=1, tb_offset=0): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) # If the error is at the console, don't build any context, since it would # otherwise produce 5 blank lines printed out (there is no file at the # console) rec_check = records[tb_offset:] try: rname = rec_check[0][1] if rname == '<ipython console>' or rname.endswith('<string>'): return rec_check except IndexError: pass aux = traceback.extract_tb(etb) assert len(records) == len(aux) for i, (file, lnum, _, _) in enumerate(aux): maybeStart = lnum - 1 - context // 2 start = max(maybeStart, 0) end = start + context lines = linecache.getlines(file)[start:end] buf = list(records[i]) buf[LNUM_POS] = lnum buf[INDEX_POS] = lnum - 1 - start buf[LINES_POS] = lines records[i] = tuple(buf) return records[tb_offset:] # Helper function -- largely belongs to VerboseTB, but we need the same # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they # can be recognized properly by ipython.el's py-traceback-line-re # (SyntaxErrors have to be treated specially because they have no traceback)
def noch_parse_exception(error_object, *args): merc.GDF = False wrap_call = inspect.getinnerframes(sys.exc_info()[2]) logger.debug("Exception: %s %s" % (type(error_object), str(error_object))) for call_info in reversed(wrap_call): local_calls = call_info[0].f_locals if '_logged__tracer_var_' in local_calls: continue tracestring = "Frame Trace: \nFile: %s \nLine: %d \n ", call_info[1], call_info[2] tracestring += "Function: %s \nCode: %s ", call_info[3], call_info[4][0].lstrip() logger.debug(tracestring) logger.debug("Local Env Variables: ") for k, v in local_calls.items(): levtrace = value_to_str(v) logger.debug("%s : %s", k, levtrace)
def reraise(original, exception): prev_cls, prev, tb = sys.exc_info() frames = inspect.getinnerframes(tb) if len(frames) > 1: exception = original try: raise exception.with_traceback(tb) except AttributeError: # This syntax is not a valid Python 3 syntax so we have # to work around that exec('raise exception.__class__, exception, tb')
def _fixed_getframes(etb, context=1, tb_offset=0): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) # If the error is at the console, don't build any context, since it would # otherwise produce 5 blank lines printed out (there is no file at the # console) rec_check = records[tb_offset:] try: rname = rec_check[0][1] if rname == '<ipython console>' or rname.endswith('<string>'): return rec_check except IndexError: pass aux = traceback.extract_tb(etb) assert len(records) == len(aux) for i, (file, lnum, _, _) in enumerate(aux): maybe_start = lnum - 1 - context // 2 start = max(maybe_start, 0) end = start + context lines = linecache.getlines(file)[start:end] buf = list(records[i]) buf[LNUM_POS] = lnum buf[INDEX_POS] = lnum - 1 - start buf[LINES_POS] = lines records[i] = tuple(buf) return records[tb_offset:]
def _fixed_getinnerframes(etb, context=1, tb_offset=0): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) # If the error is at the console, don't build any context, since it would # otherwise produce 5 blank lines printed out (there is no file at the # console) rec_check = records[tb_offset:] try: rname = rec_check[0][1] if rname == '<ipython console>' or rname.endswith('<string>'): return rec_check except IndexError: pass aux = traceback.extract_tb(etb) assert len(records) == len(aux) for i, (file, lnum, _, _) in zip(range(len(records)), aux): maybeStart = lnum - 1 - context // 2 start = max(maybeStart, 0) end = start + context lines = ulinecache.getlines(file)[start:end] buf = list(records[i]) buf[LNUM_POS] = lnum buf[INDEX_POS] = lnum - 1 - start buf[LINES_POS] = lines records[i] = tuple(buf) return records[tb_offset:] # Helper function -- largely belongs to VerboseTB, but we need the same # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they # can be recognized properly by ipython.el's py-traceback-line-re # (SyntaxErrors have to be treated specially because they have no traceback)
def _exc_info_to_string(self, err, test): """Converts a sys.exc_info()-style tuple of values into a string. This method is overridden here because we want to colorize lines if --color is passed, and display local variables if --verbose is passed """ exctype, exc, tb = err output = ['Traceback (most recent call last)'] frames = inspect.getinnerframes(tb) colorize = self.colorize frames = enumerate(self._iter_valid_frames(frames)) for index, (frame, filename, lineno, funcname, ctx, ctxindex) in frames: filename = osp.abspath(filename) if ctx is None: # pyc files or C extensions for instance source = '<no source available>' else: source = ''.join(ctx) if colorize: filename = textutils.colorize_ansi(filename, 'magenta') source = colorize_source(source) output.append(' File "%s", line %s, in %s' % (filename, lineno, funcname)) output.append(' %s' % source.strip()) if self.verbose: output.append('%r == %r' % (dir(frame), test.__module__)) output.append('') output.append(' ' + ' local variables '.center(66, '-')) for varname, value in sorted(frame.f_locals.items()): output.append(' %s: %r' % (varname, value)) if varname == 'self': # special handy processing for self for varname, value in sorted(vars(value).items()): output.append(' self.%s: %r' % (varname, value)) output.append(' ' + '-' * 66) output.append('') output.append(''.join(traceback.format_exception_only(exctype, exc))) return '\n'.join(output)
def _fixed_getframes(etb, context=1, tb_offset=0): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) # If the error is at the console, don't build any context, since it would # otherwise produce 5 blank lines printed out (there is no file at the # console) rec_check = records[tb_offset:] try: rname = rec_check[0][1] if rname == '<ipython console>' or rname.endswith('<string>'): return rec_check except IndexError: pass aux = traceback.extract_tb(etb) assert len(records) == len(aux) for i, (file, lnum, _, _) in enumerate(aux): maybeStart = lnum - 1 - context // 2 start = max(maybeStart, 0) end = start + context lines = linecache.getlines(file)[start:end] # pad with empty lines if necessary if maybeStart < 0: lines = (['\n'] * -maybeStart) + lines if len(lines) < context: lines += ['\n'] * (context - len(lines)) buf = list(records[i]) buf[LNUM_POS] = lnum buf[INDEX_POS] = lnum - 1 - start buf[LINES_POS] = lines records[i] = tuple(buf) return records[tb_offset:]