def _getNativePythonFunctionDefFromWithBlockDescription(self, withBlockDescription, objectIdToObjectDefinition): sourceText = objectIdToObjectDefinition[withBlockDescription.sourceFileId].text sourceLineOffsets = PythonAstConverter.computeLineOffsets(sourceText) sourceTree = ast.parse(sourceText) withTree = PyAstUtil.withBlockAtLineNumber( sourceTree, withBlockDescription.lineNumber ) withBodyAsFunctionAst = ast.FunctionDef( name="__withBodyFunction", lineno=withTree.lineno, col_offset=withTree.col_offset, args=ast.arguments( args=[], vararg=None, kwarg=None, defaults=[]), body=withTree.body, decorator_list=[] ) nativeWithBodyAst = PythonAstConverter.convertPythonAstToForaPythonAst( withBodyAsFunctionAst, sourceLineOffsets ) return nativeWithBodyAst
def test_python_ast_conversion_lots_of_code(self): modules = allSubmodules(ufora) filenames = [] for m in modules: if '__file__' in dir(m): filename = m.__file__ if filename.endswith(".pyc"): filename = filename[:-4] + ".py" if filename.endswith(".py"): filenames.append(filename) filenames = sorted(filenames) for filename in filenames: print filename with open(filename, "r") as f: txt = f.read() if txt: PythonAstConverter.parseStringToPythonAst(txt)
def _convertClassOrFunctionDefinitionToNativePyAst(classOrFunctionDefinition, objectIdToObjectDefinition): sourceText = objectIdToObjectDefinition[classOrFunctionDefinition.sourceFileId].text pyAst = PythonAstConverter.parseStringToPythonAst(sourceText) assert pyAst is not None pyAst = pyAst.functionClassOrLambdaDefAtLine(classOrFunctionDefinition.lineNumber) assert pyAst is not None, (sourceText, classOrFunctionDefinition.lineNumber) return pyAst