def getframeinfo(frame, context=1): """ Get information about a frame or traceback object. A tuple of five things is returned: the filename, the line number of the current line, the function name, a list of lines of context from the source code, and the index of the current line within that list. The optional second argument specifies the number of lines of context to return, which are centered around the current line. This originally comes from ``inspect`` but is modified to handle issues with ``findsource()``. """ if inspect.istraceback(frame): lineno = frame.tb_lineno frame = frame.tb_frame else: lineno = frame.f_lineno if not inspect.isframe(frame): raise TypeError('arg is not a frame or traceback object') filename = inspect.getsourcefile(frame) or inspect.getfile(frame) if context > 0: start = lineno - 1 - context // 2 try: lines, lnum = inspect.findsource(frame) except Exception: # findsource raises platform-dependant exceptions first_lines = lines = index = None else: start = max(start, 1) start = max(0, min(start, len(lines) - context)) first_lines = lines[:2] lines = lines[start:(start + context)] index = lineno - 1 - start else: first_lines = lines = index = None # Code taken from Django's ExceptionReporter._get_lines_from_file if first_lines and isinstance(first_lines[0], bytes): encoding = 'ascii' for line in first_lines[:2]: # File coding may be specified. Match pattern from PEP-263 # (http://www.python.org/dev/peps/pep-0263/) match = re.search(br'coding[:=]\s*([-\w.]+)', line) if match: encoding = match.group(1).decode('ascii') break lines = [line.decode(encoding, 'replace') for line in lines] if hasattr(inspect, 'Traceback'): return inspect.Traceback(filename, lineno, frame.f_code.co_name, lines, index) else: return (filename, lineno, frame.f_code.co_name, lines, index)
def getframeinfo(frame, context=1): """ Get information about a frame or traceback object. A tuple of five things is returned: the filename, the line number of the current line, the function name, a list of lines of context from the source code, and the index of the current line within that list. The optional second argument specifies the number of lines of context to return, which are centered around the current line. This originally comes from ``inspect`` but is modified to handle issues with ``findsource()``. """ if inspect.istraceback(frame): lineno = frame.tb_lineno frame = frame.tb_frame else: lineno = frame.f_lineno if not inspect.isframe(frame): raise TypeError('arg is not a frame or traceback object') filename = inspect.getsourcefile(frame) or inspect.getfile(frame) if context > 0: start = lineno - 1 - context // 2 try: lines, lnum = inspect.findsource(frame) except (IOError, IndexError): lines = index = None else: start = max(start, 1) start = max(0, min(start, len(lines) - context)) lines = lines[start:(start + context)] index = lineno - 1 - start else: lines = index = None if hasattr(inspect, 'Traceback'): return inspect.Traceback(filename, lineno, frame.f_code.co_name, lines, index) else: return (filename, lineno, frame.f_code.co_name, lines, index)
def getframeinfo_except_zip_file_path(frame, context=1): f = orig_getframeinfo(frame, context) if f.filename != module_path_in_zip: return f return inspect.Traceback(fake_module_path_in_zip, f.lineno, f.function, f.code_context, f.index)