Python ast 模块,dump() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用ast.dump()

项目:viper    作者:ethereum    | 项目源码 | 文件源码
def __init__(self, expr, context):
        self.expr = expr
        self.context = context
        self.expr_table = {
            LLLnode: self.get_expr,
            ast.Num: self.number,
            ast.Str: self.string,
            ast.NameConstant: self.constants,
            ast.Name: self.variables,
            ast.Attribute: self.attribute,
            ast.Subscript: self.subscript,
            ast.BinOp: self.arithmetic,
            ast.Compare: self.compare,
            ast.BoolOp: self.boolean_operations,
            ast.UnaryOp: self.unary_operations,
            ast.Call: self.call,
            ast.List: self.list_literals,
            ast.Dict: self.struct_literals,
            ast.Tuple: self.tuple_literals,
        }
        expr_type = self.expr.__class__
        if expr_type in self.expr_table:
            self.lll_node = self.expr_table[expr_type]()
        else:
            raise Exception("Unsupported operator: %r" % ast.dump(self.expr))
项目:pseudo-python    作者:alehander42    | 项目源码 | 文件源码
def _translate_handler(self, handler):
        if not isinstance(handler.type, ast.Name) or handler.type.id not in self._exceptions:
            raise PseudoPythonTypeCheckError('%s' % str(ast.dump(handler.type)))
        h = self.type_env[handler.name]
        if h and h != 'Exception':
            raise PseudoPythonTypeCheckError("can't change the type of exception %s to %s" % (handler.name, serialize_type(h)))
        self.type_env[handler.name] = 'Exception'
        return {
            'type': 'exception_handler',
            'pseudo_type': 'Void',
            'exception': handler.type.id,
            'is_builtin': handler.type.id == 'Exception',
            'instance': handler.name,
            'block': self._translate_node(handler.body)
            #block [self._translate_node(z) for z in handler.body]
        }
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
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)])"
        )
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:oil    作者:oilshell    | 项目源码 | 文件源码
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)])"
        )
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
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)])"
        )
项目:ml-utils    作者:LinxiFan    | 项目源码 | 文件源码
def mess_control(f, *, debug=0):
    assert inspect.isfunction(f)
    members = dict(inspect.getmembers(f))
    global_dict = members["__globals__"]
    source_filename = inspect.getsourcefile(f)
    f_ast = ast.parse(inspect.getsource(f))
    _, starting_line = inspect.getsourcelines(f)
    # ast_demo.increment_lineno(f_ast, starting_line - 1)
    if debug:
        print("AST:", ast.dump(f_ast))
    visitor = ControlMess()
    new_ast = visitor.visit(f_ast)
    if debug:
        print('NEW AST:', ast.dump(new_ast))
    ast.fix_missing_locations(new_ast)
    co = compile(new_ast, '<ast_demo>', 'exec')
    fake_locals = {}
    # exec will define the new function into fake_locals scope
    # this is to avoid conflict with vars in the real locals()
    # https://stackoverflow.com/questions/24733831/using-a-function-defined-in-an-execed-string-in-python-3
    exec(co, None, fake_locals)
    # new_f = locals()[visitor._new_func_name(f.__name__)]
    return fake_locals[f.__name__]
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
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)])"
        )
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:fatoptimizer    作者:vstinner    | 项目源码 | 文件源码
def check_call_visitor(self, visitor):
        tree = ast.parse("1+1")
        with self.assertRaises(Exception) as cm:
            visitor.visit(tree)

        binop = tree.body[0].value
        what = ast.dump(binop)
        self.assertEqual(str(cm.exception),
                         'error at <string>:1 on visiting %s: bug' % what)

        # Test truncature of the AST dump
        with mock.patch('fatoptimizer.tools.COMPACT_DUMP_MAXLEN', 5):
            with self.assertRaises(Exception) as cm:
                visitor.visit(tree)

            what = 'BinOp(...)'
            self.assertEqual(str(cm.exception),
                             'error at <string>:1 on visiting %s: bug' % what)
项目:TerpreT    作者:51alg    | 项目源码 | 文件源码
def var_to_dvar(var_node):
    if isinstance(var_node, ast.Name):
        name = var_node
        new_name = ast.Name()
        new_id =  "d%s" % name.id
        new_name.id = new_id
        return new_name

    elif isinstance(var_node, ast.Subscript):
        subscript = var_node
        name = subscript.value
        new_name = ast.Name(id="d%s" % name.id)
        new_subscript = ast.Subscript(value=new_name, slice=subscript.slice)
        return new_subscript

    else:
        print "Error: don't know how to dvar a %s" % ast.dump(var_node)
        print r.pretty(var_node)
        assert False
项目:TerpreT    作者:51alg    | 项目源码 | 文件源码
def parse_set_to(expr_node):
    assert isinstance(expr_node, ast.Expr), "set_to node should be Expr"
    call_node = u.cast(expr_node.value, ast.Call)
    attribute_node = u.cast(call_node.func, ast.Attribute)
    name_node = u.cast(attribute_node.value, ast.Name)
    lhs_var = name_node.id

    possible_attributes = ["set_to", "set_to_constant", "observe_value", "set_message"]
    assert attribute_node.attr in possible_attributes, "unexpected attribute " + ast.dump(attribute_node)

    op, args = u.parse_factor_expression(call_node.args[0])
    if attribute_node.attr == "set_to_constant":
        assert op is None, "set_to_constant isn't a constant"
        op = "SetToConstant"
    elif attribute_node.attr == "set_to":
        if op is None:
            op = "Copy"
    elif attribute_node.attr == "observe_value":
        assert op is None, "observe_value isn't a constant"
        op = "ObserveValue"

    return lhs_var, [op] + args
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
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)])"
        )
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
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)])"
        )
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
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)])"
        )
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:py101    作者:sophilabs    | 项目源码 | 文件源码
def runTest(self):
        """Makes a simple test of the output"""

        body = ast.parse(self.candidate_code, self.file_name, 'exec')

        # Looks if the code is the same as the provided code.
        body_dump = ast.dump(body)
        for node in self.inmutable_code.body:
            self.assertTrue(body_dump.find(ast.dump(node)) >= 0,
                            "Provided code should not be modified")

        code = compile(self.candidate_code, self.file_name, 'exec')
        exec(code)
        self.assertMultiLineEqual('1\n2\n3\n',
                                  self.__mockstdout.getvalue(),
                                  'Output is not correct')
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
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)])"
        )
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:SkiRaff    作者:mathiascj    | 项目源码 | 文件源码
def test_start_ModifyPositionalArg(self):
        # Arrange
        program = "{}(conn, args)"
        transform_visitor = TransformVisitor(self.conn_scope)
        for s in MODIFY_LIST:
            p = program.format(s)
            actual = ast.parse(p)
            old = ast.parse(p)

            # Act
            transform_visitor.start(actual)

            # Assert
            # We check that some modification has been made
            self.assertNotEqual(dump(actual), dump(old))

            # We check that the correct change was made 
            self.assertEqual(actual.body[0].value.args[0].id, 'conn_a')

            # We check that nothing else was changed, this is done by removing
            # the part that should be changed from both.
            actual.body[0].value.args[0] = None
            old.body[0].value.args[0] = None
            self.assertEqual(dump(actual), dump(old))
项目:SkiRaff    作者:mathiascj    | 项目源码 | 文件源码
def test_start_ModifyKeywordArg(self):
        # Arrange
        program = "{}(connection=conn, args=args)"
        transform_visitor = TransformVisitor(self.conn_scope)
        for s in MODIFY_LIST:
            p = program.format(s)
            actual = ast.parse(p)
            old = ast.parse(p)

            # Act
            transform_visitor.start(actual)

            # Assert
            # We check that some modification has been made
            self.assertNotEqual(dump(actual), dump(old))

            # We check that the correct change was made
            self.assertEqual(actual.body[0].value.keywords[0].value.id, 'conn_a')

            # We check that nothing else was changed, this is done by removing
            # the part that should be changed from both.
            actual.body[0].value.keywords[0].value = None
            old.body[0].value.keywords[0].value = None
            self.assertEqual(dump(actual), dump(old))
项目:pseudo-python    作者:alehander42    | 项目源码 | 文件源码
def _translate_raise(self, exc, cause, location):
        self.assert_translatable('raise', cause=(None, cause))
        if not isinstance(exc.func, ast.Name) or exc.func.id not in self._exceptions:
            raise PseudoPythonTypeCheckError('pseudo-python can raise only Exception or custom exceptions: %s ' % ast.dump(exc.func))

        return {
            'type': 'throw_statement',
            'pseudo_type': 'Void',
            'exception': exc.func.id,
            'value': self._translate_node(exc.args[0])
        }
项目:py2cpp    作者:mugwort-rc    | 项目源码 | 文件源码
def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument("input", type=argparse.FileType("r"))

    args = parser.parse_args(argv)

    node = ast.parse(args.input.read())
    print(ast.dump(node))

    return 0
项目:skastic    作者:mypalmike    | 项目源码 | 文件源码
def exec(self):
    function_asts = [x.python_ast for x in self.function_pages.values()]
    module_ast = make_module(function_asts, self.main_page.python_ast)

    if self.args.dump_ast:
      print(ast.dump(module_ast))

    exec(compile(module_ast, filename="<ast>", mode="exec"))
项目:skastic    作者:mypalmike    | 项目源码 | 文件源码
def main(argv=sys.argv):
  parser = argparse.ArgumentParser(description='Execute a skastic image.')
  parser.add_argument('filename', help='Name of .ska.png file to execute')
  parser.add_argument('--include', '-i', action='append', help='Include file', default=[])
  parser.add_argument('--draw', action='append', help='Do not execute. For debugging an '
                      'image, display one of (contours, lines, connections, parse-tree)', default=[])
  parser.add_argument('--dump-ast', action='store_true', help='Do not execute. For debugging, '
                      'dump ast')
  args = parser.parse_args(argv[1:])

  skastic = Skastic(args.filename, args)

  if args.draw:
    # Meh, probably better to wait for a key from the console?
    cv2.waitKey(0)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_parse(self):
        a = ast.parse('foo(1 + 1)')
        b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
        self.assertEqual(ast.dump(a), ast.dump(b))
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
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)])"
        )
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_parse(self):
        a = ast.parse('foo(1 + 1)')
        b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
        self.assertEqual(ast.dump(a), ast.dump(b))
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:oil    作者:oilshell    | 项目源码 | 文件源码
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)])"
        )
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def assertASTEqual(self, ast1, ast2):
        dump1 = ast.dump(ast1)
        dump2 = ast.dump(ast2)
        self.assertEqual(ast.dump(ast1), ast.dump(ast2))
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_parse(self):
        a = ast.parse('foo(1 + 1)')
        b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
        self.assertEqual(ast.dump(a), ast.dump(b))
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
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)])"
        )
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_iter_child_nodes(self):
        node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
        self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
        iterator = ast.iter_child_nodes(node.body)
        self.assertEqual(next(iterator).id, 'spam')
        self.assertEqual(next(iterator).n, 23)
        self.assertEqual(next(iterator).n, 42)
        self.assertEqual(ast.dump(next(iterator)),
            "keyword(arg='eggs', value=Str(s='leek'))"
        )
项目:bandit-ss    作者:zeroSteiner    | 项目源码 | 文件源码
def pre_visit(self, node, preprocess=False):
        self.context = {}
        self.context['imports'] = self.imports
        self.context['import_aliases'] = self.import_aliases

        if self.debug:
            LOG.debug(ast.dump(node))
            self.metaast.add_node(node, '', self.depth)

        if hasattr(node, 'lineno'):
            self.context['lineno'] = node.lineno

            if node.lineno in self.nosec_lines:
                LOG.debug("skipped, nosec")
                self.metrics.note_nosec()
                return False

        if preprocess:
            return True

        self.context['imports'] = self.imports
        self.context['import_aliases'] = self.import_aliases
        self.context['node'] = node
        self.context['linerange'] = b_utils.linerange_fix(node)
        self.context['filename'] = self.fname

        self.seen += 1
        LOG.debug("entering: %s %s [%s]", hex(id(node)), type(node),
                  self.depth)
        self.depth += 1
        LOG.debug(self.context)
        return True
项目:bandit-ss    作者:zeroSteiner    | 项目源码 | 文件源码
def visit(self, node, phase=None):
        phase = phase or constants.PRIMARY
        name = node.__class__.__name__
        method = 'visit_' + name
        visitor = getattr(self, method, None)
        if visitor is not None:
            if self.debug:
                LOG.debug("%s called (%s)", method, ast.dump(node))
            visitor(node, phase)
        else:
            self.update_scores(self.tester.run_tests(self.context, name, phase=phase))
项目:vulture    作者:jendrikseipp    | 项目源码 | 文件源码
def visit(self, node):
        method = 'visit_' + node.__class__.__name__
        visitor = getattr(self, method, None)
        if self.verbose:
            lineno = getattr(node, 'lineno', 1)
            line = self.code[lineno - 1] if self.code else ''
            self._log(lineno, ast.dump(node), line)
        if visitor:
            visitor(node)
        return self.generic_visit(node)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_parse(self):
        a = ast.parse('foo(1 + 1)')
        b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
        self.assertEqual(ast.dump(a), ast.dump(b))
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
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)])"
        )
项目:fatoptimizer    作者:vstinner    | 项目源码 | 文件源码
def _get_assign_names(targets, load_names, store_names):
    for target in targets:
        orig_target = target
        target = _get_ast_name_node(target)
        if (isinstance(target, ast.Name)
             and isinstance(target.ctx, ast.Store)):
            # 'x = value': store name 'x'
            store_names.add(target.id)
        elif (isinstance(target, ast.Name)
             and isinstance(target.ctx, ast.Load)):
            # 'obj.attr = value': load name 'obj'
            load_names.add(target.id)
        elif isinstance(target, ast.Tuple):
            # x, y = ...
            _get_assign_names(target.elts, load_names, store_names)
        elif isinstance(target, ast.Constant):
            # '(1).__class__ = MyInt': it raises a TypeError
            raise ComplexAssignment(orig_target)
        elif isinstance(target, (ast.Dict, ast.List)):
            # '{...}[key] = ...', '[...][index] = ...'
            pass
        elif isinstance(target, ast.Call):
            # 'globals()[key] = value'
            # 'type(mock)._mock_check_sig = checksig'
            raise ComplexAssignment(orig_target)
        else:
            raise Exception("unsupported assign target: %s"
                            % ast.dump(target))
项目:fatoptimizer    作者:vstinner    | 项目源码 | 文件源码
def compact_dump(node, maxlen=COMPACT_DUMP_MAXLEN):
    if isinstance(node, list):
        return repr([compact_dump(node_item, maxlen) for node_item in node])
    node_repr = ast.dump(node)
    if len(node_repr) > maxlen:
        node_repr = node_repr[:maxlen] + '(...)'
    return node_repr


# FIXME: replace it with FindNodes, see unroll.py
项目:fatoptimizer    作者:vstinner    | 项目源码 | 文件源码
def call_builtin(self, node, pure_func):
        value = pure_func.call_func(node)
        if value is UNSET:
            return

        new_node = self.new_constant(node, value)
        if new_node is None:
            return

        self.log(node, "call pure builtin function: replace %s with %r",
                 ast.dump(node), value, add_line=True)
        self.add_guard(BuiltinGuard(node.func.id, 'call builtin'))
        return new_node
项目:typed-astunparse    作者:mbdevpl    | 项目源码 | 文件源码
def test_files(self):
        """Keep Python stdlib tree the same after roundtrip parse-unparse."""
        for path in PATHS:
            with open(path, 'r', encoding='utf-8') as py_file:
                original_code = py_file.read()
            tree = typed_ast.ast3.parse(source=original_code, filename=path)
            code = typed_astunparse.unparse(tree)
            roundtrip_tree = typed_ast.ast3.parse(source=code)
            tree_dump = typed_ast.ast3.dump(tree, include_attributes=False)
            roundtrip_tree_dump = typed_ast.ast3.dump(roundtrip_tree, include_attributes=False)
            self.assertEqual(tree_dump, roundtrip_tree_dump, msg=path)
项目:typed-astunparse    作者:mbdevpl    | 项目源码 | 文件源码
def test_untyped_files(self):
        """Unparse Python stdlib correctly even if parsed using built-in ast package."""
        for path in PATHS:
            with open(path, 'r', encoding='utf-8') as py_file:
                original_code = py_file.read()
            tree = ast.parse(source=original_code, filename=path)
            code = typed_astunparse.unparse(tree)
            roundtrip_tree = ast.parse(source=code)
            tree_dump = ast.dump(tree, include_attributes=False)
            roundtrip_tree_dump = ast.dump(roundtrip_tree, include_attributes=False)
            self.assertEqual(tree_dump, roundtrip_tree_dump, msg=path)