我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用ast.Return()。
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 isDefault(a): """Our programs have a default setting of return 42, so we should detect that""" if type(a) == ast.Module and len(a.body) == 1: a = a.body[0] else: return False if type(a) != ast.FunctionDef: return False if len(a.body) == 0: return True elif len(a.body) == 1: if type(a.body[0]) == ast.Return: if a.body[0].value == None or \ type(a.body[0].value) == ast.Num and a.body[0].value.n == 42: return True return False
def get_resource(name, connection=None): """ Return a tuple containing the Resource, Corpus, and Lexicon of the corpus module specified by 'name'. Arguments --------- name : str The name of the corpus module connection : str or None The name of the database connection. If None, the current connection is used. Returns ------- res : tuple A tuple consisting of the Resource class, Corpus class, and Lexicon class defined in the corpus module """ if not connection: connection = cfg.current_server Resource, Corpus, Lexicon, _ = get_available_resources(connection)[name] return Resource, Corpus, Lexicon
def get_unused_code(self, min_confidence=0, sort_by_size=False): """ Return ordered list of unused Item objects. """ if not 0 <= min_confidence <= 100: raise ValueError('min_confidence must be between 0 and 100.') def by_name(item): return (item.filename.lower(), item.first_lineno) def by_size(item): return (item.size,) + by_name(item) unused_code = (self.unused_attrs + self.unused_classes + self.unused_funcs + self.unused_imports + self.unused_props + self.unused_vars + self.unreachable_code) confidently_unused = [obj for obj in unused_code if obj.confidence >= min_confidence] return sorted(confidently_unused, key=by_size if sort_by_size else by_name)
def _handle_ast_list(self, ast_list): """ Find unreachable nodes in the given sequence of ast nodes. """ for index, node in enumerate(ast_list): if isinstance(node, (ast.Break, ast.Continue, ast.Raise, ast.Return)): try: first_unreachable_node = ast_list[index + 1] except IndexError: continue class_name = node.__class__.__name__.lower() self._define( self.unreachable_code, class_name, first_unreachable_node, last_node=ast_list[-1], message="unreachable code after '{class_name}'".format( **locals()), confidence=100) return
def test_return_logger(self): self.config.logger = io.StringIO() def get_logs(): logger = self.config.logger logger.seek(0) return logger.readlines() self.check_optimize(""" def func(): x = 1 return 1 return 2 """, """ def func(): x = 1 return 1 """) self.assertEqual(get_logs(), ['<string>:4: fatoptimizer: Remove unreachable code: ' 'Return(value=Constant(value=2))\n'])
def remove_dead_code(optimizer, node_list): """Remove dead code. Modify node_list in-place. Example: replace "return 1; return 2" with "return 1". """ truncate = None stop = len(node_list) - 1 for index, node in enumerate(node_list): if index == stop: break if not isinstance(node, (ast.Return, ast.Raise)): continue if not can_remove(node_list[index+1:]): continue truncate = index break # FIXME: use for/else: ? if truncate is None: return node_list optimizer.log_node_removal("Remove unreachable code", node_list[truncate+1:]) return node_list[:truncate+1]
def build_positional_args(self, candidate, callsite): """Attempt to convert the positional and keyword args supplied at the given callsite to the positional args expected by the candidate funcdef. Return a list of ast.Node instances, or raise ValueError if it can't be done. """ if len(callsite.args) > len(candidate.args.args): raise ValueError('too many positional arguments') slots = {} for idx, arg in enumerate(callsite.args): slots[idx] = arg for actual_kwarg in get_keywords(callsite): idx = locate_kwarg(candidate, actual_kwarg.arg) if idx in slots: raise ValueError('positional slot %i already filled' % idx) slots[idx] = actual_kwarg.value actual_pos_args = [] for idx in range(len(candidate.args.args)): if idx not in slots: raise ValueError('argument %i not filled' % idx) actual_pos_args.append(slots[idx]) return actual_pos_args
def callers(self, n=4, count=0, excludeCaller=True, files=False): '''Return a list containing the callers of the function that called g.callerList. If the excludeCaller keyword is True (the default), g.callers is not on the list. If the files keyword argument is True, filenames are included in the list. ''' # sys._getframe throws ValueError in both cpython and jython if there are less than i entries. # The jython stack often has less than 8 entries, # so we must be careful to call g._callerName with smaller values of i first. result = [] i = 3 if excludeCaller else 2 while 1: s = self._callerName(i, files=files) # print(i,s) if s: result.append(s) if not s or len(result) >= n: break i += 1 result.reverse() if count > 0: result = result[: count] sep = '\n' if files else ',' return sep.join(result)
def all_matches(self, s): ''' Return a list of match objects for all matches in s. These are regex match objects or (start, end) for balanced searches. ''' trace = False if self.is_balanced(): aList, i = [], 0 while i < len(s): progress = i j = self.full_balanced_match(s, i) if j is None: i += 1 else: aList.append((i,j),) i = j assert progress < i return aList else: return list(self.regex.finditer(s))
def reduce_numbers(self, aList): ''' Return aList with all number types in aList replaced by the most general numeric type in aList. ''' trace = False found = None numbers = ('number', 'complex', 'float', 'long', 'int') for kind in numbers: for z in aList: if z == kind: found = kind break if found: break if found: assert found in numbers, found aList = [z for z in aList if z not in numbers] aList.append(found) if trace: g.trace(aList) return aList
def trace_stubs(self, stub, aList=None, header=None, level=-1): '''Return a trace of the given stub and all its descendants.''' indent = ' '*4*max(0,level) if level == -1: aList = ['===== %s...\n' % (header) if header else ''] for s in stub.out_list: aList.append('%s%s' % (indent, s.rstrip())) for child in stub.children: self.trace_stubs(child, level=level+1, aList=aList) if level == -1: return '\n'.join(aList) + '\n' # 2: ClassDef(identifier name, expr* bases, # stmt* body, expr* decorator_list) # 3: ClassDef(identifier name, expr* bases, # keyword* keywords, expr? starargs, expr? kwargs # stmt* body, expr* decorator_list) # # keyword arguments supplied to call (NULL identifier for **kwargs) # keyword = (identifier? arg, expr value)
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 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 pythoneval(self, args, debug=False): refs = [ast.Name("x{0}".format(i), ast.Load()) for i in xrange(len(args))] if sys.version_info[0] <= 2: params = ast.arguments([ast.Name("x{0}".format(i), ast.Param()) for i in xrange(len(args))], None, None, []) fcn = ast.FunctionDef("tmp", params, [ast.Return(self.pythonast(refs))], []) else: params = ast.arguments([ast.arg("x{0}".format(i), None) for i in xrange(len(args))], None, [], [], None, []) fcn = ast.FunctionDef("tmp", params, [ast.Return(self.pythonast(refs))], [], None) moduleast = ast.Module([fcn]) fakeLineNumbers(moduleast) if debug: print(astToSource(fcn)) modulecomp = compile(moduleast, "Femtocode", "exec") out = {"$importlib": importlib, "$math": math} exec(modulecomp, out) return out["tmp"](*args)
def translate_object(py_obj, body_only=False, enable_es6=False, enable_stage3=False): """Translate the given Python 3 object (function, class, etc.) to ES6 Javascript. If `body_only` is ``True``, the object itself is discarded and only its body gets translated as it was a module body. Return a ``(js_text, js_source_map)`` tuple. """ cwd = os.getcwd() src_filename = os.path.abspath(inspect.getsourcefile(py_obj)) prefix = os.path.commonpath((cwd, src_filename)) if len(prefix) > 1: src_filename = src_filename[len(prefix) + 1:] src_lines, sline_offset = inspect.getsourcelines(py_obj) # line offsets should be 0-based sline_offset = sline_offset - 1 with open(src_filename) as f: complete_src = f.read() return translates(src_lines, True, src_filename, (sline_offset, 0), body_only=body_only, complete_src=complete_src, enable_es6=enable_es6, enable_stage3=enable_stage3)
def _build_node_cfg(node): handlers = { ast.If: _build_if_cfg, ast.For: _build_loop_cfg, ast.While: _build_loop_cfg, ast.With: _build_with_cfg, ast.Break: _build_break_cfg, ast.Continue: _build_continue_cfg, ast.Return: _build_return_cfg, ast.Try: _build_try_cfg, } if type(node) in handlers: handler = handlers[type(node)] else: handler = _build_statement_cfg return handler(node)
def _inline(node, gen_sym, return_name, constants): """ Return a list of nodes, representing inlined function call. """ fn = constants[node.func.id] fn_ast = Function.from_object(fn).tree gen_sym, new_fn_ast = mangle(gen_sym, fn_ast) parameter_assignments = _build_parameter_assignments(node, new_fn_ast) body_nodes = new_fn_ast.body gen_sym, inlined_body, new_bindings = _wrap_in_loop(gen_sym, body_nodes, return_name) constants = dict(constants) constants.update(new_bindings) return parameter_assignments + inlined_body, gen_sym, constants
def filter_block(node_list): """ Remove no-op code (``pass``), or any code after an unconditional jump (``return``, ``break``, ``continue``, ``raise``). """ if len(node_list) == 1: return node_list new_list = [] for node in node_list: if type(node) == ast.Pass: continue new_list.append(node) if type(node) in (ast.Return, ast.Break, ast.Continue, ast.Raise): break if len(new_list) == len(node_list): return node_list else: return new_list
def init_inc_idx(cls, inc_idx): if inc_idx == Ellipsis: # if no index is provided, we will extract signature from the fn body pass elif isinstance(inc_idx, tuple): inc_idx = cls._normalize_fn_idx(inc_idx) (arg_types, return_type) = inc_idx for arg_type in arg_types: if not isinstance(arg_type, typy.Type): raise typy.TypeFormationError( "Argument type is not a type.") if return_type != Ellipsis: raise typy.TypeFormationError( "Return type for an incomplete fn type must be elided.") else: raise typy.TypeFormationError( "Incomplete fn type index is not an ellipsis or pair.") return inc_idx
def check_for_b901(self, node): xs = list(node.body) has_yield = False return_node = None while xs: x = xs.pop() if isinstance(x, (ast.AsyncFunctionDef, ast.FunctionDef)): continue elif isinstance(x, (ast.Yield, ast.YieldFrom)): has_yield = True elif isinstance(x, ast.Return) and x.value is not None: return_node = x if has_yield and return_node is not None: self.errors.append( B901(return_node.lineno, return_node.col_offset) ) break xs.extend(ast.iter_child_nodes(x))
def visit_Lambda(self, t): return Function('<lambda>', t.args, [ast.Return(t.body)])
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_Lambda(self, t): t = self.generic_visit(t) result = Function('<lambda>', t.args, [ast.Return(t.body)]) return ast.copy_location(result, t)
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 visit(self, node): if self._return_level: if self._node_level == self._return_level: return None elif self._node_level < self._return_level: self._return_level = None if isinstance(node, ast.Return): self._return_level = self._node_level self._node_level += 1 node = super().visit(node) self._node_level -= 1 return node
def noneSpecialFunction(cv): """If the old type is 'None' (which won't show up in the original), move up in the AST to get the metadata""" if (not isinstance(cv, AddVector)) and cv.oldSubtree == None: cvCopy = cv.deepcopy() if cv.path[0] == ('value', 'Return'): cv.oldSubtree = deepcopy(cvCopy.traverseTree(cv.start)) cv.newSubtree = ast.Return(cv.newSubtree) cv.path = cv.path[1:] elif cv.path[0] == ('value', 'Name Constant'): cv.oldSubtree = deepcopy(cvCopy.traverseTree(cv.start)) cv.newSubtree = ast.NameConstant(cv.newSubtree) cv.path = cv.path[1:] elif cv.path[0] in [('lower', 'Slice'), ('upper', 'Slice'), ('step', 'Slice')]: tmpNew = cv.newSubtree cvCopy = cv.deepcopy() cv.oldSubtree = deepcopy(cvCopy.traverseTree(cv.start)) cv.newSubtree = deepcopy(cv.oldSubtree) # use the same slice if cv.path[0][0] == 'lower': cv.newSubtree.lower = tmpNew elif cv.path[0][0] == 'upper': cv.newSubtree.upper = tmpNew else: cv.newSubtree.step = tmpNew cv.path = cv.path[1:] # get rid of None and the val else: log("Individualize\tmapEdit\tMissing option in None special case 1: " + str(cv.path[0]), "bug") elif cv.oldSubtree == "None": cv.path = cv.path[1:] # get rid of None and the id cvCopy = cv.deepcopy() cv.oldSubtree = deepcopy(cvCopy.traverseTree(cv.start)) if cv.path[0] == ('value', 'Return'): cv.newSubtree = ast.Return(ast.Name(cv.newSubtree, ast.Load())) else: log("Individualize\tmapEdit\tMissing option in None special case 2: " + str(cv.path[0]), "bug") cv.path = cv.path[1:] return cv
def getFirst(a): # Return the first value if the list has at least one element return a[0] if len(a) > 0 else None
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 _transform_multiline_return_statement(return_statement): return ast.Return(value=return_statement, lineno=return_statement.lineno, col_offset = return_statement.col_offset)
def _compile(self): c = _NameSubstitute() t = c.visit(copy(self._tree)) name = repr(self) maxarg = max((int(name[1:]) for name in c.name_cache), default=0) + 1 args = [ ast.arg(arg='_%d' % n, annotation=None) for n in range(1, maxarg) ] code = compile( ast.fix_missing_locations(ast.Module( body=[ ast.FunctionDef( name=name, args=ast.arguments( args=args, vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[], ), body=[ast.Return(value=t)], decorator_list=[], returns=None, lineno=1, col_offset=0, ), ], )), name, 'exec', ) ns = {} exec(code, ns) return asconstants(**self._constants)(ns[name])
def get_configuration_type(): """ Return the type of the current configuration. Returns ------- s : str or None Depending on the type of the currenct configuration, `s` equals either the constant SQL_MYSQL or SQL_SQLITE from defines.py. If no configuration is available, return None. """ if cfg.current_server in cfg.server_configuration: return cfg.server_configuration[cfg.current_server]["type"] else: return None
def _make_fn(name, chain_fn, args, defaults): args_with_self = ['_self'] + list(args) arguments = [_ast.Name(id=arg, ctx=_ast.Load()) for arg in args_with_self] defs = [_ast.Name(id='_def{0}'.format(idx), ctx=_ast.Load()) for idx, _ in enumerate(defaults)] if _PY2: parameters = _ast.arguments(args=[_ast.Name(id=arg, ctx=_ast.Param()) for arg in args_with_self], defaults=defs) else: parameters = _ast.arguments(args=[_ast.arg(arg=arg) for arg in args_with_self], kwonlyargs=[], defaults=defs, kw_defaults=[]) module_node = _ast.Module(body=[_ast.FunctionDef(name=name, args=parameters, body=[_ast.Return(value=_ast.Call(func=_ast.Name(id='_chain', ctx=_ast.Load()), args=arguments, keywords=[]))], decorator_list=[])]) module_node = _ast.fix_missing_locations(module_node) # compile the ast code = compile(module_node, '<string>', 'exec') # and eval it in the right context globals_ = {'_chain': chain_fn} locals_ = dict(('_def{0}'.format(idx), value) for idx, value in enumerate(defaults)) eval(code, globals_, locals_) # extract our function from the newly created module return locals_[name] ######################################################################## # Produce a docstring for the class.
def _nt_getnewargs(self): 'Return self as a plain tuple. Used by copy and pickle.' return tuple(self)
def generic_visit(self, node): if (isinstance(node, ast.stmt) and self.references_arg(node)) or isinstance(node, ast.Return): return self.get_waits() + [node] return NodeTransformer.generic_visit(self, node)
def analysis_function(self, function_node, args_type=[]): variables = Variables() return_type = Type.VOID for node in function_node.body: if isinstance(node, ast.Assign): self.analysis_assign_node(variables, node) elif isinstance(node, ast.Return): return_type = self.get_type(node.value) if return_type is None: return_type = Type.VOID generator = CodeGenerator(self.code,variables=variables) return generator, return_type
def test_builtin_frozenset(self): self.check_builtin_func('frozenset', "return frozenset(('abc',))", ast.Return(ast.Constant(value=frozenset(('abc',))))) self.check_builtin_func('frozenset', "return frozenset()", ast.Return(ast.Constant(value=frozenset()))) self.check_dont_optimize_func('frozenset(([],))')
def get_runner(path, **kw): """ Partial application of path to run_code(). Return function 'run' so run(source) => result. """ def run(source): return run_code(path, source, **kw) return run
def is_rewrite_float32(self): """ Return whether an ArrayStorage transform is used such that float arrays are rewritten to float32 format. """ return any(getattr(t, 'use_float32', False) for t in self.transformL)
def is_use_4channel(self): """ Return whether an ArrayStorage transform is used such that float arrays are rewritten to have 4 channels. """ return any(getattr(t, 'use_4channel', False) for t in self.transformL)
def is_known_type(s): ''' Return True if s is nothing but a single known type. Recursively test inner types in square brackets. ''' return ReduceTypes().is_known_type(s)
def reduce_types(aList, name=None, trace=False): ''' Return a string containing the reduction of all types in aList. The --trace-reduce command-line option sets trace=True. If present, name is the function name or class_name.method_name. ''' return ReduceTypes(aList, name, trace).reduce_types() # Top-level functions
def truncate(s, n): '''Return s truncated to n characters.''' return s if len(s) <= n else s[:n-3] + '...'
def visit(self, node): '''Return the formatted version of an Ast node, or list of Ast nodes.''' if isinstance(node, (list, tuple)): return ','.join([self.visit(z) for z in node]) elif node is None: return 'None' else: assert isinstance(node, ast.AST), node.__class__.__name__ method_name = 'do_' + node.__class__.__name__ method = getattr(self, method_name) s = method(node) # assert type(s) == type('abc'), (node, type(s)) assert g.isString(s), type(s) return s # Contexts... # 2: ClassDef(identifier name, expr* bases, # stmt* body, expr* decorator_list) # 3: ClassDef(identifier name, expr* bases, # keyword* keywords, expr? starargs, expr? kwargs # stmt* body, expr* decorator_list) # # keyword arguments supplied to call (NULL identifier for **kwargs) # keyword = (identifier? arg, expr value)
def get_import_names(self, node): '''Return a list of the the full file names in the import statement.''' result = [] for ast2 in node.names: if self.kind(ast2) == 'alias': data = ast2.name, ast2.asname result.append(data) else: print('unsupported kind in Import.names list', self.kind(ast2)) return result
def isString(self, s): '''Return True if s is any string, but not bytes.''' # pylint: disable=no-member if isPython3: return isinstance(s, str) else: return isinstance(s, types.StringTypes)