python类arg()的实例源码

tailbiter2_py35.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def visit_keyword(self, t):
        return self.load_const(t.arg) + self(t.value)
tailbiter2_py35.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def compile_function(self, t):
        self.load_const(ast.get_docstring(t))
        for arg in t.args.args:
            self.varnames[arg.arg]
        assembly = self(t.body) + self.load_const(None) + op.RETURN_VALUE
        return self.make_code(assembly, t.name, len(t.args.args))
tailbiter2_py35.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
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)
tailbiter2_py35.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def visit_Function(self, t):
        subscope = Scope(t, [arg.arg for arg in t.args.args])
        self.children[t] = subscope
        for stmt in t.body: subscope.visit(stmt)
tailbiter2.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def __init__(self, opcode, arg):
        self.opcode = opcode
        self.arg    = arg
        self.length = 1 if arg is None else 3
tailbiter2.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def plumb(self, depths):
        arg = 0 if isinstance(self.arg, Label) else self.arg
        depths.append(depths[-1] + dis.stack_effect(self.opcode, arg))
tailbiter2.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def denotation(opcode):
    if opcode < dis.HAVE_ARGUMENT:
        return Instruction(opcode, None)
    else:
        return lambda arg: Instruction(opcode, arg)
tailbiter2.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def visit_keyword(self, t):
        return self.load_const(t.arg) + self(t.value)
tailbiter2.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def compile_function(self, t):
        self.load_const(ast.get_docstring(t))
        for arg in t.args.args:
            self.varnames[arg.arg]
        assembly = self(t.body) + self.load_const(None) + op.RETURN_VALUE
        return self.make_code(assembly, t.name, len(t.args.args))
tailbiter2.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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)
tailbiter2_py36.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __init__(self, opcode, arg):
        self.opcode = opcode
        self.arg    = arg
tailbiter2_py36.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def encode(self, start, addresses):
        if   self.opcode in dis.hasjabs: arg = addresses[self.arg]
        elif self.opcode in dis.hasjrel: arg = addresses[self.arg] - (start+2)
        else:                            arg = self.arg
        return bytes([self.opcode, arg or 0])
tailbiter2_py36.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def plumb(self, depths):
        arg = 0 if isinstance(self.arg, Label) else self.arg
        depths.append(depths[-1] + dis.stack_effect(self.opcode, arg))
tailbiter2_py36.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def denotation(opcode):
    if opcode < dis.HAVE_ARGUMENT:
        return Instruction(opcode, None)
    else:
        return lambda arg: Instruction(opcode, arg)
tailbiter2_py36.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def visit_Call(self, t):
        assert len(t.args) + len(t.keywords) < 256
        if t.keywords:
            return (self(t.func) + self(t.args)
                    + self(t.keywords) + self.load_const(tuple([k.arg for k in t.keywords]))
                    + op.CALL_FUNCTION_KW(len(t.args) + len(t.keywords)))
        else:
            return (self(t.func) + self(t.args)
                    + op.CALL_FUNCTION(len(t.args)))
tailbiter2_py36.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
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)
tailbiter2_py36.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def visit_Function(self, t):
        subscope = Scope(t, [arg.arg for arg in t.args.args])
        self.children[t] = subscope
        for stmt in t.body: subscope.visit(stmt)
transformations.py 文件源码 项目:ITAP-django 作者: krivers 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def updateVariableNames(a, varMap, scopeName, randomCounter, imports):
    if not isinstance(a, ast.AST):
        return

    if type(a) in [ast.FunctionDef, ast.ClassDef]:
        if a.name in varMap:
            if not hasattr(a, "originalId"):
                a.originalId = a.name
            a.name = varMap[a.name]
        anonymizeStatementNames(a, varMap, "_" + a.name, imports)
    elif type(a) == ast.arg:
        if a.arg not in varMap and not (builtInName(a.arg) or importedName(a.arg, imports)):
            log("Can't assign to arg?", "bug")
        if a.arg in varMap:
            if not hasattr(a, "originalId"):
                a.originalId = a.arg
            if varMap[a.arg][0] == "r":
                a.randomVar = True # so we know it can crash
            if a.arg == varMap[a.arg]:
                # Check whether this is a given name
                if not isAnonVariable(varMap[a.arg]):
                    a.dontChangeName = True
            a.arg = varMap[a.arg]
    elif type(a) == ast.Name:
        if a.id not in varMap and not (builtInName(a.id) or importedName(a.id, imports)):
            varMap[a.id] = "r" + str(randomCounter[0]) + scopeName
            randomCounter[0] += 1
        if a.id in varMap:
            if not hasattr(a, "originalId"):
                a.originalId = a.id
            if varMap[a.id][0] == "r":
                a.randomVar = True # so we know it can crash
            if a.id == varMap[a.id]:
                # Check whether this is a given name
                if not isAnonVariable(varMap[a.id]):
                    a.dontChangeName = True
            a.id = varMap[a.id]
    else:
        for child in ast.iter_child_nodes(a):
            updateVariableNames(child, varMap, scopeName, randomCounter, imports)
transformations.py 文件源码 项目:ITAP-django 作者: krivers 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def propogateNameMetadata(a, namesToKeep, imports):
    """Propogates name metadata through a state. We assume that the names are all properly formatted"""
    if type(a) == list:
        for child in a:
            child = propogateNameMetadata(child, namesToKeep, imports)
        return a
    elif not isinstance(a, ast.AST):
        return a
    if type(a) == ast.Name:
        if (builtInName(a.id) or importedName(a.id, imports)):
            pass
        elif a.id in namesToKeep:
            a.dontChangeName = True
        else:
            if not hasattr(a, "originalId"):
                a.originalId = a.id
            if not isAnonVariable(a.id):
                a.dontChangeName = True # it's a name we shouldn't mess with
    elif type(a) == ast.arg:
        if (builtInName(a.arg) or importedName(a.arg, imports)):
            pass
        elif a.arg in namesToKeep:
            a.dontChangeName = True
        else:
            if not hasattr(a, "originalId"):
                a.originalId = a.arg
            if not isAnonVariable(a.arg):
                a.dontChangeName = True # it's a name we shouldn't mess with
    for child in ast.iter_child_nodes(a):
        child = propogateNameMetadata(child, namesToKeep, imports)
    return a

### HELPER FOLDING ###
transformations.py 文件源码 项目:ITAP-django 作者: krivers 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def individualizeVariables(a, variablePairs, idNum, imports):
    """Replace variable names with new individualized ones (for inlining methods)"""
    if not isinstance(a, ast.AST):
        return

    if type(a) == ast.Name:
        if a.id not in variablePairs and not (builtInName(a.id) or importedName(a.id, imports)):
            name = "_var_" + a.id + "_" + str(idNum[0])
            variablePairs[a.id] = name
        if a.id in variablePairs:
            a.id = variablePairs[a.id]
    # Override built-in names when they're assigned to.
    elif type(a) == ast.Assign and type(a.targets[0]) == ast.Name:
        if a.targets[0].id not in variablePairs:
            name = "_var_" + a.targets[0].id + "_" + str(idNum[0])
            variablePairs[a.targets[0].id] = name
    elif type(a) == ast.arguments:
        for arg in a.args:
            if type(arg) == ast.arg:
                name = "_arg_" + arg.arg + "_" + str(idNum[0])
                variablePairs[arg.arg] = name
                arg.arg = variablePairs[arg.arg]
        return
    elif type(a) == ast.Call:
        if type(a.func) == ast.Name:
            variablePairs[a.func.id] = a.func.id # save the function name!

    for child in ast.iter_child_nodes(a):
        individualizeVariables(child, variablePairs, idNum, imports)


问题


面经


文章

微信
公众号

扫码关注公众号