我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用_ast.Module()。
def extract_text_from_node(dastring, astobj): try: if issubclass(type(astobj), _ast.Module): astobj = astobj.body if isinstance(astobj, list) and len(astobj) > 0: rangeobj = TextRange(lineno=astobj[0].lineno, col_offset=astobj[0].col_offset, end_lineno=astobj[-1].end_lineno, end_col_offset=astobj[-1].end_col_offset) else: rangeobj = TextRange(lineno=astobj.lineno, col_offset=astobj.col_offset, end_lineno=astobj.end_lineno, end_col_offset=astobj.end_col_offset) return(extract_text_range(dastring, rangeobj)) except: return("") ## PRETTY PRINT FROM GREEN TREE SNAKES
def __init__(self, message, astobj = None): self.message = message self.line_info = {} try: if astobj is not None: if issubclass(type(astobj), (_ast.Module, _ast.Expression)): astobj = astobj.body if isinstance(astobj, list) and len(astobj) > 0: start = astobj[0] end = astobj[-1] else: start = astobj end = astobj if hasattr(start, "lineno") and \ hasattr(start, "col_offset") and \ hasattr(end, "end_lineno") and \ hasattr(end, "end_col_offset"): self.line_info["line_start"] = start.lineno self.line_info["column_start"] = start.col_offset self.line_info["line_end"] = end.end_lineno self.line_info["column_end"] = end.end_col_offset except: pass # TODO FILIP: No used for now, come back to this later.
def make_module(code): from ..decompiler.disassemble import disassemble instructions = Instructions(disassemble(code)) stmnts = instructions.stmnt() doc = pop_doc(stmnts) pop_return(stmnts) # stmnt = ast.Stmt(stmnts, 0) if doc is not None: stmnts = [_ast.Expr(value=doc, lineno=doc.lineno, col_offset=0)] + stmnts ast_obj = _ast.Module(body=stmnts, lineno=0, col_offset=0) return ast_obj
def test_compile_ast(self): fname = __file__ if fname.lower().endswith(('pyc', 'pyo')): fname = fname[:-1] with open(fname, 'r') as f: fcontents = f.read() sample_code = [ ['<assign>', 'x = 5'], ['<ifblock>', """if True:\n pass\n"""], ['<forblock>', """for n in [1, 2, 3]:\n print(n)\n"""], ['<deffunc>', """def foo():\n pass\nfoo()\n"""], [fname, fcontents], ] for fname, code in sample_code: co1 = compile(code, '%s1' % fname, 'exec') ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST) self.assertTrue(type(ast) == _ast.Module) co2 = compile(ast, '%s3' % fname, 'exec') self.assertEqual(co1, co2) # the code object's filename comes from the second compilation step self.assertEqual(co2.co_filename, '%s3' % fname) # raise exception when node type doesn't match with compile mode co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST) self.assertRaises(TypeError, compile, co1, '<ast>', 'eval') # raise exception when node type is no start node self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec') # raise exception when node has invalid children ast = _ast.Module() ast.body = [_ast.BoolOp()] self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
def test_compile_ast(self): fname = __file__ if fname.lower().endswith(('pyc', 'pyo')): fname = fname[:-1] with open(fname, 'r') as f: fcontents = f.read() sample_code = [ ['<assign>', 'x = 5'], ['<print1>', 'print 1'], ['<printv>', 'print v'], ['<printTrue>', 'print True'], ['<printList>', 'print []'], ['<ifblock>', """if True:\n pass\n"""], ['<forblock>', """for n in [1, 2, 3]:\n print n\n"""], ['<deffunc>', """def foo():\n pass\nfoo()\n"""], [fname, fcontents], ] for fname, code in sample_code: co1 = compile(code, '%s1' % fname, 'exec') ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST) self.assertTrue(type(ast) == _ast.Module) co2 = compile(ast, '%s3' % fname, 'exec') self.assertEqual(co1, co2) # the code object's filename comes from the second compilation step self.assertEqual(co2.co_filename, '%s3' % fname) # raise exception when node type doesn't match with compile mode co1 = compile('print 1', '<string>', 'exec', _ast.PyCF_ONLY_AST) self.assertRaises(TypeError, compile, co1, '<ast>', 'eval') # raise exception when node type is no start node self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec') # raise exception when node has invalid children ast = _ast.Module() ast.body = [_ast.BoolOp()] self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
def get_child_nodes(node): if isinstance(node, _ast.Module): return node.body result = [] if node._fields is not None: for name in node._fields: child = getattr(node, name) if isinstance(child, list): for entry in child: if isinstance(entry, _ast.AST): result.append(entry) if isinstance(child, _ast.AST): result.append(child) return result
def compile_func(ast_node, filename, globals, **defaults): ''' Compile a function from an ast.FunctionDef instance. :param ast_node: ast.FunctionDef instance :param filename: path where function source can be found. :param globals: will be used as func_globals :return: A python function object ''' funcion_name = ast_node.name module = _ast.Module(body=[ast_node]) ctx = {'%s_default' % key : arg for key, arg in defaults.items()} code = compile(module, filename, 'exec') eval(code, globals, ctx) function = ctx[funcion_name] return function #from imp import get_magic # #def extract(binary): # # if len(binary) <= 8: # raise Exception("Binary pyc must be greater than 8 bytes (got %i)" % len(binary)) # # magic = binary[:4] # MAGIC = get_magic() # # if magic != MAGIC: # raise Exception("Python version mismatch (%r != %r) Is this a pyc file?" % (magic, MAGIC)) # # modtime = time.asctime(time.localtime(struct.unpack('i', binary[4:8])[0])) # # code = marshal.loads(binary[8:]) # # return modtime, code