Esempio n. 1
0
 def test_functiondef(self):
     import ast
     fAst = ast.FunctionDef(
         name="foo",
         args=ast.arguments(
             args=[], vararg=None, kwarg=None, defaults=[],
             kwonlyargs=[], kw_defaults=[]),
         body=[ast.Expr(ast.Str('docstring'))],
         decorator_list=[], lineno=5, col_offset=0)
     exprAst = ast.Interactive(body=[fAst])
     ast.fix_missing_locations(exprAst)
     compiled = compile(exprAst, "<foo>", "single")
     #
     d = {}
     eval(compiled, d, d)
     assert type(d['foo']) is type(lambda: 42)
     assert d['foo']() is None
Esempio n. 2
0
 def test_functiondef(self):
     import ast
     fAst = ast.FunctionDef(
         name="foo",
         args=ast.arguments(
             args=[], vararg=None, kwarg=None, defaults=[],
             kwonlyargs=[], kw_defaults=[]),
         body=[ast.Expr(ast.Str('docstring'))],
         decorator_list=[], lineno=5, col_offset=0)
     exprAst = ast.Interactive(body=[fAst])
     ast.fix_missing_locations(exprAst)
     compiled = compile(exprAst, "<foo>", "single")
     #
     d = {}
     eval(compiled, d, d)
     assert type(d['foo']) is type(lambda: 42)
     assert d['foo']() is None
Esempio n. 3
0
def parse_file(fileName):
    global xmlns,folder
    if not isinstance(fileName, file):
        fileName= kast.kast_util.findFile(fileName)
        _file=open(fileName)
    else:
        _file=fileName
        fileName=_file.name
        # folder=_file.name
    if fileName.endswith("rb"):
        kastFile=fileName.replace(".rb",".kast")
        os.system('ruby2kast.rb '+fileName +' > '+ kastFile) # /tmp/
        return parse_file(kastFile)
    elif fileName.endswith("py"):
        # contents=_file.readlines()
        file_ast=compile(_file.read(), fileName, 'exec',_ast.PyCF_ONLY_AST) # AAAAHHH!!!
        for clazz in file_ast.body:
            if isinstance(clazz,ClassDef):
                classes[clazz.name]=clazz
        return file_ast
    elif fileName.endswith("yml"):
        # from cStringIO import StringIO
        xml =yml2xml(StringIO.StringIO(), yaml.load(_file)).getvalue()
        fileName = xml # StringIO 'file' ;)
        print(xml)
        root = Xml.fromstring(xml)
    else:
        tree = Xml.parse(fileName)
        root = tree.getroot()

    import re
    if "}" in xmlns:
        xmlns=re.sub("\}.*","}",root.tag)
    # if xmlns!=root.tag: xmlns=xmlns[1:-1]

    my_ast=build(root) # <<<<<<<<<<<< BUILD AST !!!
    # Rewriter().visit(my_ast)
    if not isinstance(my_ast,Module): ## ARGGHH! BROKEN PYTHON INHERITANCE
        if(isinstance(my_ast,list)):
            my_ast=Module(body=my_ast)
        else:
            my_ast=Module(body=[my_ast])
    my_ast.body.insert(0,Import([name('_global')]))
    # alias
    # my_ast.body.insert(0,ImportFrom(module='parser_test_helper',names=[alias('*',None)],level=0)) #asname=None

    my_ast=_ast.fix_missing_locations(my_ast)
    modules.append(my_ast)
    for clazz in classes.values():
        fix_missing_self(clazz)

    # x=ast.dump(my_ast, annotate_fields=True, include_attributes=True)
    # x=ast.dump(my_ast, annotate_fields=False, include_attributes=False)
    # print("\n".join(x.split("),")))

    return my_ast
Esempio n. 4
0
 def test_invalid_string(self):
     import ast
     m = ast.Module([ast.Expr(ast.Str(43))])
     ast.fix_missing_locations(m)
     exc = raises(TypeError, compile, m, "<test>", "exec")
Esempio n. 5
0
 def test_invalid_identitifer(self):
     import ast
     m = ast.Module([ast.Expr(ast.Name(b"x", ast.Load()))])
     ast.fix_missing_locations(m)
     exc = raises(TypeError, compile, m, "<test>", "exec")
Esempio n. 6
0
 def test_empty_set(self):
     import ast
     m = ast.Module(body=[ast.Expr(value=ast.Set(elts=[]))])
     ast.fix_missing_locations(m)
     compile(m, "<test>", "exec")
Esempio n. 7
0
 def _mod(mod, msg=None, mode="exec", exc=ValueError):
     mod.lineno = mod.col_offset = 0
     ast.fix_missing_locations(mod)
     exc = raises(exc, compile, mod, "<test>", mode)
     if msg is not None:
         assert msg in str(exc.value)
Esempio n. 8
0
 def test_invalid_string(self):
     import ast
     m = ast.Module([ast.Expr(ast.Str(43))])
     ast.fix_missing_locations(m)
     exc = raises(TypeError, compile, m, "<test>", "exec")
Esempio n. 9
0
 def test_invalid_identitifer(self):
     import ast
     m = ast.Module([ast.Expr(ast.Name(u"x", ast.Load()))])
     ast.fix_missing_locations(m)
     exc = raises(TypeError, compile, m, "<test>", "exec")
Esempio n. 10
0
 def test_empty_set(self):
     import ast
     m = ast.Module(body=[ast.Expr(value=ast.Set(elts=[]))])
     ast.fix_missing_locations(m)
     compile(m, "<test>", "exec")