我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用ast.Expr()。
def _extract_scripts_from_project(setup_filename='setup.py'): """Parse setup.py and return scripts""" if not os.path.isfile(setup_filename): return '' mock_setup = textwrap.dedent('''\ def setup(*args, **kwargs): __setup_calls__.append((args, kwargs)) ''') parsed_mock_setup = ast.parse(mock_setup, filename=setup_filename) with open(setup_filename, 'rt') as setup_file: parsed = ast.parse(setup_file.read()) for index, node in enumerate(parsed.body[:]): if (not isinstance(node, ast.Expr) or not isinstance(node.value, ast.Call) or node.value.func.id != 'setup'): continue parsed.body[index:index] = parsed_mock_setup.body break fixed = ast.fix_missing_locations(parsed) codeobj = compile(fixed, setup_filename, 'exec') local_vars = {} global_vars = {'__setup_calls__': []} exec(codeobj, global_vars, local_vars) _, kwargs = global_vars['__setup_calls__'][0] return ','.join([os.path.basename(f) for f in kwargs.get('scripts', [])])
def __init__(self, stmt, context): self.stmt = stmt self.context = context self.stmt_table = { ast.Expr: self.expr, ast.Pass: self.parse_pass, ast.AnnAssign: self.ann_assign, ast.Assign: self.assign, ast.If: self.parse_if, ast.Call: self.call, ast.Assert: self.parse_assert, ast.For: self.parse_for, ast.AugAssign: self.aug_assign, ast.Break: self.parse_break, ast.Return: self.parse_return, } stmt_type = self.stmt.__class__ if stmt_type in self.stmt_table: self.lll_node = self.stmt_table[stmt_type]() elif isinstance(stmt, ast.Name) and stmt.id == "throw": self.lll_node = LLLnode.from_list(['assert', 0], typ=None, pos=getpos(stmt)) else: raise StructureException("Unsupported statement type", stmt)
def assign(self): from .parser import ( make_setter, ) # Assignment (eg. x[4] = y) if len(self.stmt.targets) != 1: raise StructureException("Assignment statement must have one target", self.stmt) sub = Expr(self.stmt.value, self.context).lll_node if isinstance(self.stmt.targets[0], ast.Name) and self.stmt.targets[0].id not in self.context.vars: pos = self.context.new_variable(self.stmt.targets[0].id, set_default_units(sub.typ)) variable_loc = LLLnode.from_list(pos, typ=sub.typ, location='memory', pos=getpos(self.stmt), annotation=self.stmt.targets[0].id) o = make_setter(variable_loc, sub, 'memory', pos=getpos(self.stmt)) else: # Checks to see if assignment is valid target = self.get_target(self.stmt.targets[0]) o = make_setter(target, sub, target.location, pos=getpos(self.stmt)) o.pos = getpos(self.stmt) return o
def get_target(self, target): if isinstance(target, ast.Subscript) and self.context.in_for_loop: # Check if we are doing assignment of an iteration loop. raise_exception = False if isinstance(target.value, ast.Attribute): list_name = "%s.%s" % (target.value.value.id, target.value.attr) if list_name in self.context.in_for_loop: raise_exception = True if isinstance(target.value, ast.Name) and \ target.value.id in self.context.in_for_loop: list_name = target.value.id raise_exception = True if raise_exception: raise StructureException("Altering list '%s' which is being iterated!" % list_name, self.stmt) if isinstance(target, ast.Name) and target.id in self.context.forvars: raise StructureException("Altering iterator '%s' which is in use!" % target.id, self.stmt) target = Expr.parse_variable_location(target, self.context) if target.location == 'storage' and self.context.is_constant: raise ConstancyViolationException("Cannot modify storage inside a constant function: %s" % target.annotation) if not target.mutable: raise ConstancyViolationException("Cannot modify function argument: %s" % target.annotation) return target
def _is_for_yield(self, node: ast.For) -> bool: if node.orelse: return False if not isinstance(node.target, ast.Name): return False body = node.body if len(body) != 1: return False expr = body[0] if not isinstance(expr, ast.Expr): return False yield_ = expr.value if not isinstance(yield_, ast.Yield): return False name = yield_.value if not isinstance(name, ast.Name): return False if name.id != node.target.id: return False return True
def iter_node(self, node): silence = (ast.Expr,) if not isinstance(node, silence): try: body = node.body # check if is iterable list(body) except TypeError: body = [node.body] except AttributeError: body = None yield node, body for attr in ('value', 'func', 'right', 'left'): if hasattr(node, attr): value = getattr(node, attr) # reversed is used here so matches are returned in the # sequence they are read, eg.: foo.bar.bang for n in reversed(list(self.iter_node(value))): yield n
def stateful_get_all_emojis(self, expr): if not isinstance(expr.value, ast.Await): return expr if not isinstance(expr.value.value, ast.Call): return expr call = expr.value.value if isinstance(call.func, ast.Attribute): if call.func.attr == 'get_all_emojis': if self.interactive and not prompt_change( 'A possible change was found to make get_all_emojis stateful.' ): return expr new_expr = ast.Expr() new_expr.value = ast.Attribute() new_expr.value.value = call.func.value new_expr.value.attr = 'emojis' new_expr.value.ctx = ast.Load() new_expr = ast.copy_location(new_expr, expr) stats_counter['expr_changes'] += 1 return new_expr return expr
def test_dump(self): node = ast.parse('spam(eggs, "and cheese")') self.assertEqual(ast.dump(node), "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), " "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], " "keywords=[], starargs=None, kwargs=None))])" ) self.assertEqual(ast.dump(node, annotate_fields=False), "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), " "Str('and cheese')], [], None, None))])" ) self.assertEqual(ast.dump(node, include_attributes=True), "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), " "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), " "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, " "col_offset=11)], keywords=[], starargs=None, kwargs=None, " "lineno=1, col_offset=0), lineno=1, col_offset=0)])" )
def _imports_unicode_literals(contents_text): try: ast_obj = ast_parse(contents_text) except SyntaxError: return False for node in ast_obj.body: # Docstring if isinstance(node, ast.Expr) and isinstance(node.value, ast.Str): continue elif isinstance(node, ast.ImportFrom): if ( node.module == '__future__' and any(name.name == 'unicode_literals' for name in node.names) ): return True elif node.module == '__future__': continue else: return False else: return False
def test_try(self): p = ast.Pass() t = ast.Try([], [], [], [p]) self.stmt(t, "empty body on Try") t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p]) self.stmt(t, "must have Load context") t = ast.Try([p], [], [], []) self.stmt(t, "Try has neither except handlers nor finalbody") t = ast.Try([p], [], [p], [p]) self.stmt(t, "Try has orelse but no except handlers") t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], []) self.stmt(t, "empty body on ExceptHandler") e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])] self.stmt(ast.Try([p], e, [], []), "must have Load context") e = [ast.ExceptHandler(None, "x", [p])] t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p]) self.stmt(t, "must have Load context") t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))]) self.stmt(t, "must have Load context")
def add_import(tree, name, asname): # import fat as __fat__ import_node = ast.Import(names=[ast.alias(name=name, asname=asname)], lineno=1, col_offset=1) for index, node in enumerate(tree.body): if (index == 0 and isinstance(node, ast.Expr) and isinstance(node.value, ast.Constant) and isinstance(node.value.value, str)): # docstring continue if (isinstance(node, ast.ImportFrom) and node.module == '__future__'): # from __future__ import ... continue tree.body.insert(index, import_node) break else: # body is empty or only contains __future__ imports tree.body.append(import_node)
def generic_visit(self, node): super(NodeTransformer, self).generic_visit(node) if hasattr(node, 'body') and type(node.body) is list: returns = [i for i, child in enumerate(node.body) if type(child) is ast.Return] if len(returns) > 0: for wait in self.get_waits(): node.body.insert(returns[0], wait) inserts = [] for i, child in enumerate(node.body): if type(child) is ast.Expr and self.is_concurrent_call(child.value): self.encounter_call(child.value) elif self.is_valid_assignment(child): call = child.value self.encounter_call(call) name = child.targets[0].value self.arguments.add(SchedulerRewriter.top_level_name(name)) index = child.targets[0].slice.value call.func = ast.Attribute(call.func, 'assign', ast.Load()) call.args = [ast.Tuple([name, index], ast.Load())] + call.args node.body[i] = ast.Expr(call) elif self.references_arg(child): inserts.insert(0, i) for index in inserts: for wait in self.get_waits(): node.body.insert(index, wait)
def valid_identifier(name): """Determines whether the given name could be used a Python identifier""" try: name = ensure_unicode(name) except ValueError: return False if name in __KEYWORDS: return False try: root = _ast.parse(name) except: return False return (isinstance(root, _ast.Module) and len(root.body) == 1 and isinstance(root.body[0], _ast.Expr) and isinstance(root.body[0].value, _ast.Name) and root.body[0].value.id == name)
def get_coverable_nodes(cls): return { ast.Assert, ast.Assign, ast.AugAssign, ast.Break, ast.Continue, ast.Delete, ast.Expr, ast.Global, ast.Import, ast.ImportFrom, ast.Nonlocal, ast.Pass, ast.Raise, ast.Return, ast.FunctionDef, ast.ClassDef, ast.TryExcept, ast.TryFinally, ast.ExceptHandler, ast.If, ast.For, ast.While, }
def get_coverable_nodes(cls): return { ast.Assert, ast.Assign, ast.AugAssign, ast.Break, ast.Continue, ast.Delete, ast.Expr, ast.Global, ast.Import, ast.ImportFrom, ast.Nonlocal, ast.Pass, ast.Raise, ast.Return, ast.ClassDef, ast.FunctionDef, ast.Try, ast.ExceptHandler, ast.If, ast.For, ast.While, }
def test_dump(self): node = ast.parse('spam(eggs, "and cheese")') self.assertEqual(ast.dump(node), "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), " "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], " "keywords=[], starargs=None, kwargs=None))])" ) self.assertEqual(ast.dump(node, annotate_fields=False), "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), " "Str('and cheese')], [], None, None))])" ) self.assertEqual(ast.dump(node, include_attributes=True), "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), " "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), " "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, " "col_offset=11)], keywords=[], starargs=None, kwargs=None, " "lineno=1, col_offset=4), lineno=1, col_offset=0)])" )
def run_in_context(code, context, defs={}): ast_ = ast.parse(code, '<code>', 'exec') last_expr = None last_def_name = None for field_ in ast.iter_fields(ast_): if 'body' != field_[0]: continue if len(field_[1]) > 0: le = field_[1][-1] if isinstance(le, ast.Expr): last_expr = ast.Expression() last_expr.body = field_[1].pop().value elif isinstance(le, (ast.FunctionDef, ast.ClassDef)): last_def_name = le.name exec(compile(ast_, '<hbi-code>', 'exec'), context, defs) if last_expr is not None: return eval(compile(last_expr, '<hbi-code>', 'eval'), context, defs) elif last_def_name is not None: return defs[last_def_name] return None
def _process_function_signature(stmt, arg_names, static_env): return_type = Ellipsis if isinstance(stmt, ast.Expr): value = stmt.value if isinstance(value, ast.BinOp): left, op, right = value.left, value.op, value.right if isinstance(op, ast.RShift): arg_types = fn._process_argument_signature(left, arg_names, static_env) return_type = static_env.eval_expr_ast(right) else: return None elif isinstance(value, ast.Dict) or isinstance(value, ast.Set): arg_types = fn._process_argument_signature(value, arg_names, static_env) else: return None else: return None if arg_types is None: return None return (arg_types, return_type)
def maybe_assignment(*expr_fns): if len(expr_fns) == 1: node0 = expr_fns[0](ast.Load()) stmt = ast.Expr(node0) else: lhses = [fn(ast.Store()) for fn in expr_fns[:-1]] node0 = lhses[0] stmt = ast.Assign(lhses, expr_fns[-1](ast.Load())) return ast.copy_location(stmt, node0)
def visit_ListComp(self, t): result_append = ast.Attribute(ast.Name('.0', load), 'append', load) body = ast.Expr(Call(result_append, [t.elt])) for loop in reversed(t.generators): for test in reversed(loop.ifs): body = ast.If(test, [body], []) body = ast.For(loop.target, loop.iter, [body], []) fn = [body, ast.Return(ast.Name('.0', load))] args = ast.arguments([ast.arg('.0', None)], None, [], None, [], []) return Call(Function('<listcomp>', args, fn), [ast.List([], load)])
def visit_ListComp(self, t): t = self.generic_visit(t) add_element = ast.Attribute(ast.Name('.elements', load), 'append', load) body = ast.Expr(Call(add_element, [t.elt])) for loop in reversed(t.generators): for test in reversed(loop.ifs): body = ast.If(test, [body], []) body = ast.For(loop.target, loop.iter, [body], []) fn = [body, ast.Return(ast.Name('.elements', load))] args = ast.arguments([ast.arg('.elements', None)], None, [], None, [], []) result = Call(Function('<listcomp>', args, fn), [ast.List([], load)]) return ast.copy_location(result, t)
def parse_if(self): from .parser import ( parse_body, ) if self.stmt.orelse: add_on = [parse_body(self.stmt.orelse, self.context)] else: add_on = [] return LLLnode.from_list(['if', Expr.parse_value_expr(self.stmt.test, self.context), parse_body(self.stmt.body, self.context)] + add_on, typ=None, pos=getpos(self.stmt))
def parse_assert(self): return LLLnode.from_list(['assert', Expr.parse_value_expr(self.stmt.test, self.context)], typ=None, pos=getpos(self.stmt))
def aug_assign(self): target = self.get_target(self.stmt.target) sub = Expr.parse_value_expr(self.stmt.value, self.context) if not isinstance(self.stmt.op, (ast.Add, ast.Sub, ast.Mult, ast.Div, ast.Mod)): raise Exception("Unsupported operator for augassign") if not isinstance(target.typ, BaseType): raise TypeMismatchException("Can only use aug-assign operators with simple types!", self.stmt.target) if target.location == 'storage': o = Expr.parse_value_expr(ast.BinOp(left=LLLnode.from_list(['sload', '_stloc'], typ=target.typ, pos=target.pos), right=sub, op=self.stmt.op, lineno=self.stmt.lineno, col_offset=self.stmt.col_offset), self.context) return LLLnode.from_list(['with', '_stloc', target, ['sstore', '_stloc', base_type_conversion(o, o.typ, target.typ)]], typ=None, pos=getpos(self.stmt)) elif target.location == 'memory': o = Expr.parse_value_expr(ast.BinOp(left=LLLnode.from_list(['mload', '_mloc'], typ=target.typ, pos=target.pos), right=sub, op=self.stmt.op, lineno=self.stmt.lineno, col_offset=self.stmt.col_offset), self.context) return LLLnode.from_list(['with', '_mloc', target, ['mstore', '_mloc', base_type_conversion(o, o.typ, target.typ)]], typ=None, pos=getpos(self.stmt))
def _translate_body(self, body, types, allow_loose_in_edges=False, allow_loose_out_edges=False): cfg_factory = CFGFactory(self._id_gen) for child in body: if isinstance(child, (ast.AnnAssign, ast.Expr)): cfg_factory.add_stmts(self.visit(child, types)) elif isinstance(child, ast.If): cfg_factory.complete_basic_block() if_cfg = self.visit(child, types) cfg_factory.append_cfg(if_cfg) elif isinstance(child, ast.While): cfg_factory.complete_basic_block() while_cfg = self.visit(child, types) cfg_factory.append_cfg(while_cfg) elif isinstance(child, ast.Break): cfg_factory.complete_basic_block() break_cfg = self.visit(child, types) cfg_factory.append_cfg(break_cfg) elif isinstance(child, ast.Continue): cfg_factory.complete_basic_block() cont_cfg = self.visit(child, types) cfg_factory.append_cfg(cont_cfg) elif isinstance(child, ast.Pass): if cfg_factory.incomplete_block(): pass else: cfg_factory.append_cfg(_dummy_cfg(self._id_gen)) else: raise NotImplementedError(f"The statement {str(type(child))} is not yet translatable to CFG!") cfg_factory.complete_basic_block() if not allow_loose_in_edges and cfg_factory.cfg and cfg_factory.cfg.loose_in_edges: cfg_factory.prepend_cfg(_dummy_cfg(self._id_gen)) if not allow_loose_out_edges and cfg_factory.cfg and cfg_factory.cfg.loose_out_edges: cfg_factory.append_cfg(_dummy_cfg(self._id_gen)) return cfg_factory.cfg
def visit_BoolOp(self, boolop): res_var = self.variable() expl_list = self.assign(ast.List([], ast.Load())) app = ast.Attribute(expl_list, "append", ast.Load()) is_or = int(isinstance(boolop.op, ast.Or)) body = save = self.statements fail_save = self.on_failure levels = len(boolop.values) - 1 self.push_format_context() # Process each operand, short-circuting if needed. for i, v in enumerate(boolop.values): if i: fail_inner = [] # cond is set in a prior loop iteration below self.on_failure.append(ast.If(cond, fail_inner, [])) # noqa self.on_failure = fail_inner self.push_format_context() res, expl = self.visit(v) body.append(ast.Assign([ast.Name(res_var, ast.Store())], res)) expl_format = self.pop_format_context(ast.Str(expl)) call = ast_Call(app, [expl_format], []) self.on_failure.append(ast.Expr(call)) if i < levels: cond = res if is_or: cond = ast.UnaryOp(ast.Not(), cond) inner = [] self.statements.append(ast.If(cond, inner, [])) self.statements = body = inner self.statements = save self.on_failure = fail_save expl_template = self.helper("format_boolop", expl_list, ast.Num(is_or)) expl = self.pop_format_context(expl_template) return ast.Name(res_var, ast.Load()), self.explanation_param(expl)
def visit_For(self, node: ast.For): node = self.generic_visit(node) if self._is_for_yield(node): yield_node = ast.YieldFrom(value = node.iter) expr_node = ast.Expr(value = yield_node) node = ast.copy_location(expr_node, node) node = ast.fix_missing_locations(node) return node
def formatStatement(a): return [a if isStatement(a) else ast.Expr(a)]
def formatStatementList(a, s): return [ast.Expr(ast.Str(s))] if len(a) > 0 else []
def isStatement(a): """Determine whether the given node is a statement (vs an expression)""" return type(a) in [ ast.Module, ast.Interactive, ast.Expression, ast.Suite, ast.FunctionDef, ast.ClassDef, ast.Return, ast.Delete, ast.Assign, ast.AugAssign, ast.For, ast.While, ast.If, ast.With, ast.Raise, ast.Try, ast.Assert, ast.Import, ast.ImportFrom, ast.Global, ast.Expr, ast.Pass, ast.Break, ast.Continue ]
def get_docstring_and_rest(filename): """Separate `filename` content between docstring and the rest Strongly inspired from ast.get_docstring. Returns ------- docstring: str docstring of `filename` rest: str `filename` content without the docstring """ with open(filename) as f: content = f.read() node = ast.parse(content) if not isinstance(node, ast.Module): raise TypeError("This function only supports modules. " "You provided {0}".format(node.__class__.__name__)) if node.body and isinstance(node.body[0], ast.Expr) and \ isinstance(node.body[0].value, ast.Str): docstring_node = node.body[0] docstring = docstring_node.value.s # This get the content of the file after the docstring last line # Note: 'maxsplit' argument is not a keyword argument in python2 rest = content.split('\n', docstring_node.lineno)[-1] return docstring, rest else: raise ValueError(('Could not find docstring in file "{0}". ' 'A docstring is required by sphinx-gallery') .format(filename))
def visit_Print(self, node): if node.__class__ != ast.Print: return node dummy_func = ast.Name(id="print", ctx=ast.Load()) keywords = [] if not node.nl: end = ast.keyword(arg="end", value=ast.Str(s="")) keywords.append(end) dummy_call = ast.Call(func=dummy_func, args=node.values, keywords=keywords, starargs=None, kwargs=None) return ast.Expr(value=dummy_call)
def match(self, node): if node.__class__ != ast.Expr: return False value = node.value if value.__class__ != ast.Call: return False if value.func.__class__ != ast.Name: return False return value.func.id == "print"
def js_window_open_get_url(js_script): # ?? Python parser ?? JavaScript code python_ast = ast.parse(js_script) assert len(python_ast.body) == 1 assert type(python_ast.body[0]) == ast.Expr assert type(python_ast.body[0].value) == ast.Call assert python_ast.body[0].value.func.value.id == 'window' assert python_ast.body[0].value.func.attr == 'open' assert len(python_ast.body[0].value.args) == 3 return python_ast.body[0].value.args[0].s # ??????????????????
def make_module(function_asts, main_ast): module_body = function_asts module_body.append(ast.Expr(value=main_ast)) module_ast = ast.Module(body=module_body) ast.fix_missing_locations(module_ast) return module_ast
def isidentifier(ident): """ Determines, if string is valid Python identifier using the ast module. Orignally posted at: http://stackoverflow.com/a/29586366 """ if not isinstance(ident, string_types): return False try: root = ast.parse(ident) except SyntaxError: return False if not isinstance(root, ast.Module): return False if len(root.body) != 1: return False if not isinstance(root.body[0], ast.Expr): return False if not isinstance(root.body[0].value, ast.Name): return False if root.body[0].value.id != ident: return False return True
def test_invalid_sum(self): pos = dict(lineno=2, col_offset=3) m = ast.Module([ast.Expr(ast.expr(**pos), **pos)]) with self.assertRaises(TypeError) as cm: compile(m, "<test>", "exec") if support.check_impl_detail(): self.assertIn("but got <_ast.expr", str(cm.exception))
def test_invalid_identitifer(self): m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))]) ast.fix_missing_locations(m) with self.assertRaises(TypeError) as cm: compile(m, "<test>", "exec") if support.check_impl_detail(): self.assertIn("identifier must be of type str", str(cm.exception))
def test_invalid_string(self): m = ast.Module([ast.Expr(ast.Str(42))]) ast.fix_missing_locations(m) with self.assertRaises(TypeError) as cm: compile(m, "<test>", "exec") if support.check_impl_detail(): self.assertIn("string must be of type str or uni", str(cm.exception))
def test_fix_missing_locations(self): src = ast.parse('write("spam")') src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()), [ast.Str('eggs')], [], None, None))) self.assertEqual(src, ast.fix_missing_locations(src)) self.assertEqual(ast.dump(src, include_attributes=True), "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), " "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, " "col_offset=6)], keywords=[], starargs=None, kwargs=None, " "lineno=1, col_offset=0), lineno=1, col_offset=0), " "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, " "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], " "keywords=[], starargs=None, kwargs=None, lineno=1, " "col_offset=0), lineno=1, col_offset=0)])" )