def renderTraceback(trace): res = [] for tb in trace: if isinstance(tb["path"],tuple): path = tb["path"] if path[0] == "ModuleImporter": path = os.path.join(_pyforaRoot, *path[1:]) + ".fora" else: path = path[0] else: path = os.path.abspath(tb["path"]) if 'range' in tb: lineNumber = tb['range']['start']['line'] else: lineNumber = tb["line"] from pyfora.pyAst.PyAstUtil import findEnclosingFunctionName, getAstFromFilePath inFunction = findEnclosingFunctionName( getAstFromFilePath(path), lineNumber ) if path.endswith(".py") else None if inFunction is not None: res.append(' File "%s", line %s, in %s' % (path, lineNumber, inFunction)) else: res.append(' File "%s", line %s' % (path, lineNumber)) lines = PyforaInspect.getlines(os.path.abspath(path)) if lines is not None and lineNumber >= 1 and lineNumber <= len(lines): res.append(" " + lines[lineNumber-1][:-1].lstrip()) return "\n".join(res)
def renderTraceback(trace): res = [] for tb in trace: if isinstance(tb["path"], tuple): path = tb["path"] if path[0] == "ModuleImporter": path = os.path.join(_pyforaRoot, *path[1:]) + ".fora" else: path = path[0] else: path = os.path.abspath(tb["path"]) if 'range' in tb: lineNumber = tb['range']['start']['line'] else: lineNumber = tb["line"] from pyfora.pyAst.PyAstUtil import findEnclosingFunctionName, getAstFromFilePath inFunction = findEnclosingFunctionName( getAstFromFilePath(path), lineNumber) if path.endswith(".py") else None if inFunction is not None: res.append(' File "%s", line %s, in %s' % (path, lineNumber, inFunction)) else: res.append(' File "%s", line %s' % (path, lineNumber)) lines = PyforaInspect.getlines(path) if lines is not None and lineNumber >= 1 and lineNumber <= len(lines): res.append(" " + lines[lineNumber - 1][:-1].lstrip()) return "\n".join(res)
def cachedFromArgs(cls, fileName, fileText=None): if fileName in cls._fileTextCache: return cls._fileTextCache[fileName] if fileText is None: fileText = "".join(PyforaInspect.getlines(fileName)) tr = cls(fileName, fileText) cls._fileTextCache[fileName] = tr return tr
def getSourceFilenameAndText(pyObject): try: sourceFile = PyforaInspect.getsourcefile(pyObject) except TypeError as e: raise Exceptions.CantGetSourceTextError(e.message) if sourceFile not in sourceFileCache_: sourceFileCache_[sourceFile] = "".join(PyforaInspect.getlines(sourceFile)) return sourceFileCache_[sourceFile], sourceFile
def getSourceFilenameAndText(pyObject): try: sourceFile = PyforaInspect.getsourcefile(pyObject) except TypeError as e: raise Exceptions.CantGetSourceTextError(e.message) if sourceFile not in sourceFileCache_: sourceFileCache_[sourceFile] = "".join( PyforaInspect.getlines(sourceFile)) return sourceFileCache_[sourceFile], sourceFile
def __enter__(self): self.frame = PyforaInspect.currentframe(1) sourceFileName, lineNumber, _, _, _ = PyforaInspect.getframeinfo(self.frame) self.sourceFileName = sourceFileName self.lineNumber = lineNumber self.sourceText = "".join(PyforaInspect.getlines(self.sourceFileName)) # Seems to "turn on" tracing, otherwise setting # frame.f_trace seems to have no effect # doesn't seem to have any effects outside of this with context. sys.settrace(lambda *args, **keys: None) self.frame.f_trace = self.trace
def getSourceFilenameAndText(pyObject): try: sourceFile = PyforaInspect.getsourcefile(pyObject) except TypeError as e: raise Exceptions.CantGetSourceTextError(e.message) linesOrNone = PyforaInspect.getlines(sourceFile) if linesOrNone is None: raise Exceptions.CantGetSourceTextError( "can't get source lines for file %s" % sourceFile) if sourceFile not in sourceFileCache_: sourceFileCache_[sourceFile] = "".join(linesOrNone) return sourceFileCache_[sourceFile], sourceFile
def getSourceFilenameAndText(pyObject): try: sourceFile = PyforaInspect.getsourcefile(pyObject) except TypeError as e: raise Exceptions.CantGetSourceTextError(e.message) linesOrNone = PyforaInspect.getlines(sourceFile) if linesOrNone is None: raise Exceptions.CantGetSourceTextError( "can't get source lines for file %s" % sourceFile ) if sourceFile not in sourceFileCache_: sourceFileCache_[sourceFile] = "".join(linesOrNone) return sourceFileCache_[sourceFile], sourceFile
def getAstFromFilePath(filename): linesOrNone = PyforaInspect.getlines(filename) if linesOrNone is not None: return pyAstFromText("".join(linesOrNone)) return None