def _replace_where_block_with_function(self, with_node): return _ast.FunctionDef(name=self.feature_name + '_where', args=_ast.arguments( args=[_ast.arg(arg='self'), _ast.arg(arg='injectable_values')], kwonlyargs=[], kw_defaults=[], defaults=[] ), body=copy.deepcopy(with_node.body), decorator_list=[], returns=None)
def _transform_function_arguments(left): if type(left) is ast.Name: names = [left] else: names = left.elts # Python3 if hasattr(_ast, 'arg'): args = [ _ast.arg(annotation=None, arg=name.id, col_offset=name.col_offset, lineno=name.lineno) for name in names ] return ast.arguments(args=args, defaults=[], kwonlyargs=[], kw_defaults=[]) # Python 2 arguments = ast.arguments(args=names, defaults=[]) for argument in arguments.args: argument.ctx = ast.Param() return arguments
def visit_FunctionDef(self, feature_node): if FeatureRegistrationTransformer._skip_feature(feature_node): return feature_node feature_name = feature_node.name if not feature_name.startswith('_'): feature_name_specified = hasattr(self.spec_location, 'feature_name') if not feature_name_specified or ( feature_name_specified and self.spec_location.feature_name == feature_name): self.spec_metadata.add_feature(feature_name) FeatureBlockTransformer(self.spec_metadata, feature_name).visit(feature_node) FeatureBlockRuleEnforcer(self.spec_metadata, feature_name, feature_node).enforce_tail_end_rules() feature_variables = self.spec_metadata.feature_variables.get(feature_name) if feature_variables: existing_arg_names = [existing_arg.arg for existing_arg in feature_node.args.args] for feature_variable in feature_variables: if feature_variable in existing_arg_names: continue feature_node.args.args.append(_ast.arg(arg=feature_variable)) feature_node.args.defaults.append(ast_proxy.ast_name_constant(value=None)) if self._feature_has_a_where_function(feature_name): self._remove_where_function_from_node(feature_name, feature_node) return feature_node
def visit_FunctionDef(self, feature_node): feature_name = feature_node.name if not feature_name.startswith('_'): self.spec_metadata.add_feature(feature_name) FeatureBlockTransformer(self.spec_metadata, feature_name).visit(feature_node) FeatureBlockRuleEnforcer(self.spec_metadata, feature_name, feature_node).enforce_tail_end_rules() feature_variables = self.spec_metadata.feature_variables.get( feature_name) if feature_variables: existing_arg_names = [ existing_arg.arg for existing_arg in feature_node.args.args ] for feature_variable in feature_variables: if feature_variable in existing_arg_names: continue feature_node.args.args.append(_ast.arg(arg=feature_variable)) feature_node.args.defaults.append( _ast.NameConstant(value=None)) if self._feature_has_a_where_function(feature_name): self._remove_where_function_from_node(feature_name, feature_node) return feature_node
def async_method(self, items): f = self.async_function(items) method_args = [_ast.arg(arg='self', annotation=None)] method_args.extend(f.args.args) f.args.args = method_args return f
def func_arg(self, items): (items, ) = items identifier = items[0] if len(items) == 2: type_ = items[1] else: type_ = None return _ast.arg(arg=identifier, annotation=type_)
def _insert_kwarg(self, index, name, default, annotation=None): if self._func.is_instancemethod: index = max(index, 1 + len(self._func.args)) abs_index = index - len(self._func.args) if isinstance(default, str): default = ast.Str(default) elif isinstance(default, PythonNode): default = default._ast arg = _ast.arg(arg=name, annotation=annotation) self._func.kwargs.insert(abs_index, arg) self._func._ast.args.args.insert(index, arg) self._func._ast.args.defaults.insert(index, default)
def constructor_method(self, items): (args, body) = items method_args = [_ast.arg(arg='self', annotation=None)] method_args.extend(args) return _ast.FunctionDef(name='__init__', args=_ast.arguments(args=method_args, defaults=[], vararg=None, kwarg=None), body=body, decorator_list=[])
def EXTENDED_ARG(self, instr): code = self.ast_stack.pop() argument_names = self.ast_stack.pop() assert len(argument_names.elts) == (instr.oparg - 1) args = [] kw = dict(lineno=instr.lineno, col_offset=0) for argument_name in argument_names.elts[::-1]: annotation = self.ast_stack.pop() arg = _ast.arg(annotation=annotation, arg=argument_name.s, **kw) #@UndefinedVariable args.append(arg) for arg in args: self.ast_stack.append(arg) self.ast_stack.append(code)
def _insert_arg(self, index, name, annotation=None): if self._func.is_instancemethod: index = max(index, 1) arg = _ast.arg(arg=name, annotation=annotation) self._func.args.insert(index, arg) self._func._ast.args.args.insert(index, arg)
def make_function(code, defaults=None, annotations=(), kw_defaults=(), lineno=0): from ..decompiler.disassemble import disassemble instructions = Instructions(disassemble(code)) stmnts = instructions.stmnt() if code.co_flags & 2: vararg = None kwarg = None varnames = list(code.co_varnames[:code.co_argcount]) kwonly_varnames = list(code.co_varnames[code.co_argcount:code.co_argcount + code.co_kwonlyargcount]) co_locals = list(code.co_varnames[code.co_argcount + code.co_kwonlyargcount:]) assert (len(kw_defaults) % 2) == 0 kw_defaults = list(kw_defaults) kw_default_dict = {} while kw_defaults: name = kw_defaults.pop(0) value = kw_defaults.pop(0) kw_default_dict[name.s] = value kw_defaults = [] for argname in kwonly_varnames: kw_defaults.append(kw_default_dict.pop(argname)) #have var args if code.co_flags & 4: vararg = co_locals.pop(0) #have kw args if code.co_flags & 8: kwarg = co_locals.pop() args = [] annotation_names = [annotation.arg for annotation in annotations] for argname in varnames: if argname in annotation_names: arg = [ annotation for annotation in annotations if annotation.arg == argname ][0] else: arg = _ast.arg(annotation=None, arg=argname, lineno=lineno, col_offset=0) #@UndefinedVariable args.append(arg) kwonlyargs = [] for argname in kwonly_varnames: if argname in annotation_names: arg = [ annotation for annotation in annotations if annotation.arg == argname ][0] else: arg = _ast.arg(annotation=None, arg=argname, lineno=lineno, col_offset=0) #@UndefinedVariable kwonlyargs.append(arg) if 'return' in annotation_names: arg = [ annotation for annotation in annotations if annotation.arg == 'return' ][0] returns = arg.annotation else: returns = None if vararg in annotation_names: arg = [ annotation for annotation in annotations if annotation.arg == vararg ][0] varargannotation = arg.annotation else: varargannotation = None if kwarg in annotation_names: arg = [ annotation for annotation in annotations if annotation.arg == kwarg ][0] kwargannotation = arg.annotation else: kwargannotation = None args = _ast.arguments(args=args, defaults=defaults if defaults else [], kwarg=kwarg, vararg=vararg, kw_defaults=kw_defaults, kwonlyargs=kwonlyargs, kwargannotation=kwargannotation, varargannotation=varargannotation, lineno=lineno, col_offset=0) if code.co_name == '<lambda>': if len(stmnts) == 2: if isinstance(stmnts[0], _ast.If) and isinstance( stmnts[1], _ast.Return): assert len(stmnts[0].body) == 1 assert isinstance(stmnts[0].body[0], _ast.Return) stmnts = [ _ast.Return( _ast.IfExp(stmnts[0].test, stmnts[0].body[0].value, stmnts[1].value)) ] assert isinstance(stmnts[0], _ast.Return) stmnt = stmnts[0].value ast_obj = _ast.Lambda(args=args, body=stmnt, lineno=lineno, col_offset=0) else: if instructions.seen_yield: return_ = stmnts[-1] assert isinstance(return_, _ast.Return) assert isinstance(return_.value, _ast.Name) assert return_.value.id == 'None' return_.value = None ast_obj = _ast.FunctionDef(name=code.co_name, args=args, body=stmnts, decorator_list=[], returns=returns, lineno=lineno, col_offset=0) return ast_obj
def argument(arg: str) -> _ast.arg: return _ast.arg(arg=arg, annotation=None, type_comment=None)
def assure_arg(x): return _ast.arg(get_id(x), None)
def _replace_where_block_with_function(self, with_node): return _ast.FunctionDef(name=self.feature_name + '_where', args=ast_proxy.ast_args([_ast.arg(arg='self'), _ast.arg(arg='injectable_values')]), body=copy.deepcopy(with_node.body), decorator_list=[], returns=None)
def make_function(code, defaults=None, annotations=(), kw_defaults=(), lineno=0): from graphlab.meta.decompiler.disassemble import disassemble instructions = Instructions(disassemble(code)) stmnts = instructions.stmnt() if code.co_flags & 2: vararg = None kwarg = None varnames = list(code.co_varnames[:code.co_argcount]) kwonly_varnames = list(code.co_varnames[code.co_argcount:code.co_argcount + code.co_kwonlyargcount]) co_locals = list(code.co_varnames[code.co_argcount + code.co_kwonlyargcount:]) assert (len(kw_defaults) % 2) == 0 kw_defaults = list(kw_defaults) kw_default_dict = {} while kw_defaults: name = kw_defaults.pop(0) value = kw_defaults.pop(0) kw_default_dict[name.s] = value kw_defaults = [] for argname in kwonly_varnames: kw_defaults.append(kw_default_dict.pop(argname)) #have var args if code.co_flags & 4: vararg = co_locals.pop(0) #have kw args if code.co_flags & 8: kwarg = co_locals.pop() args = [] annotation_names = [annotation.arg for annotation in annotations] for argname in varnames: if argname in annotation_names: arg = [annotation for annotation in annotations if annotation.arg == argname][0] else: arg = _ast.arg(annotation=None, arg=argname, lineno=lineno, col_offset=0) #@UndefinedVariable args.append(arg) kwonlyargs = [] for argname in kwonly_varnames: if argname in annotation_names: arg = [annotation for annotation in annotations if annotation.arg == argname][0] else: arg = _ast.arg(annotation=None, arg=argname, lineno=lineno, col_offset=0) #@UndefinedVariable kwonlyargs.append(arg) if 'return' in annotation_names: arg = [annotation for annotation in annotations if annotation.arg == 'return'][0] returns = arg.annotation else: returns = None if vararg in annotation_names: arg = [annotation for annotation in annotations if annotation.arg == vararg][0] varargannotation = arg.annotation else: varargannotation = None if kwarg in annotation_names: arg = [annotation for annotation in annotations if annotation.arg == kwarg][0] kwargannotation = arg.annotation else: kwargannotation = None args = _ast.arguments(args=args, defaults=defaults if defaults else [], kwarg=kwarg, vararg=vararg, kw_defaults=kw_defaults, kwonlyargs=kwonlyargs, kwargannotation=kwargannotation, varargannotation=varargannotation, lineno=lineno, col_offset=0 ) if code.co_name == '<lambda>': if len(stmnts) == 2: if isinstance(stmnts[0], _ast.If) and isinstance(stmnts[1], _ast.Return): assert len(stmnts[0].body) == 1 assert isinstance(stmnts[0].body[0], _ast.Return) stmnts = [_ast.Return(_ast.IfExp(stmnts[0].test, stmnts[0].body[0].value, stmnts[1].value))] assert isinstance(stmnts[0], _ast.Return) stmnt = stmnts[0].value ast_obj = _ast.Lambda(args=args, body=stmnt, lineno=lineno, col_offset=0) else: if instructions.seen_yield: return_ = stmnts[-1] assert isinstance(return_, _ast.Return) assert isinstance(return_.value, _ast.Name) assert return_.value.id == 'None' return_.value = None ast_obj = _ast.FunctionDef(name=code.co_name, args=args, body=stmnts, decorator_list=[], returns=returns, lineno=lineno, col_offset=0) return ast_obj
def arg_or_name(name, name_ctx=Param()): """AST argument or name.""" return Name(id=name, ctx=name_ctx) if is_py2 else arg(arg=name, annotation=None)