def GetCtx(ctx_type): """Creates Load, Store, Del, and Param, used in the ctx kwarg.""" if ctx_type == CtxEnum.LOAD: return _ast.Load() elif ctx_type == CtxEnum.STORE: return _ast.Store() elif ctx_type == CtxEnum.DEL: return _ast.Del() elif ctx_type == CtxEnum.PARAM: return _ast.Param() raise InvalidCtx('ctx_type {} isn\'t a valid type'.format(ctx_type))
def make_function(code, defaults=None, lineno=0): from 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]) co_locals = list(code.co_varnames[code.co_argcount:]) #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 = [_ast.Name(id=argname, ctx=_ast.Param(), lineno=lineno, col_offset=0) for argname in varnames] args = _ast.arguments(args=args, defaults=defaults if defaults else [], kwarg=kwarg, vararg=vararg, 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 len(stmnts) == 1, stmnts 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=[], lineno=lineno, col_offset=0) return ast_obj
def visit_arguments(self, x): x.ctx=_ast.Param() # never reached! return x # WHY???