Пример #1
0
 def parse(self, code: str) -> Optional[AST]:
     try:
         past = python_ast.parse(code)
         if self.mode == "exec":
             return to_ast(past, split_value=self.split_value)
         else:
             return to_ast(past.body[0], split_value=self.split_value)
     except:  # noqa
         return None
Пример #2
0
 def test_partial_code(self):
     assert to_ast(ast.parse("if True: pass\nelif False:\n  f(x)").body[0],
                   lambda x: [x]) == \
         Parser(lambda x: [x]).parse("elif False:\n f(x)")
     assert to_ast(ast.parse("if True: pass\nelse:\n  f(x)").body[0],
                   lambda x: [x]) == \
         Parser(lambda x: [x]).parse("else:\n f(x)")
     assert to_ast(ast.parse("try:\n  pass\nexcept: pass").body[0],
                   lambda x: [x]) == \
         Parser(lambda x: [x]).parse("try:")
 def test_node(self):
     node = python_ast.Expr()
     name = python_ast.Name()
     setattr(name, "id", None)
     setattr(name, "ctx", None)
     setattr(node, "value", name)
     node2 = to_python_ast(to_ast(node, lambda x: [x]))
     assert python_ast.dump(node) == python_ast.dump(node2)
 def test_variadic_args(self):
     node = python_ast.List()
     n = python_ast.Constant()
     s = python_ast.Constant()
     setattr(n, "n", 10)
     setattr(n, "kind", None)
     setattr(s, "s", "foo")
     setattr(s, "kind", None)
     setattr(node, "elts", [n, s])
     setattr(node, "ctx", None)
     assert python_ast.dump(node) == \
         python_ast.dump(to_python_ast(to_ast(node, lambda x: [x])))
    def test_builtin_type(self):
        def split_value(x):
            return list(x)

        node = python_ast.List()
        ten = python_ast.Constant()
        setattr(ten, "value", 10)
        setattr(ten, "kind", None)
        setattr(node, "elts", [ten])
        setattr(node, "ctx", None)
        assert python_ast.dump(node) == \
            python_ast.dump(to_python_ast(
                to_ast(node,
                       split_value=split_value)))
 def test_token_list(self):
     node = python_ast.Global()
     setattr(node, "names", ["v1", "v2"])
     assert python_ast.dump(node) == \
         python_ast.dump(to_python_ast(to_ast(node, lambda x: [x])))
 def test_empty_list(self):
     node = python_ast.List()
     setattr(node, "ctx", None)
     setattr(node, "elts", [])
     assert python_ast.dump(node) == \
         python_ast.dump(to_python_ast(to_ast(node, lambda x: [x])))
 def test_optional_arg(self):
     node = python_ast.Yield()
     setattr(node, "value", None)
     assert python_ast.dump(node) == \
         python_ast.dump(to_python_ast(to_ast(node,
                                              lambda x: [x])))
Пример #9
0
 def test_parse_code(self):
     assert to_ast(ast.parse("y = x + 1").body[0], lambda x: [x]) == \
         Parser(lambda x: [x]).parse("y = x + 1")
Пример #10
0
 def test_parse_with_mode(self):
     assert to_ast(ast.parse("xs = input().split()\nprint(','.join(xs))"),
                   lambda x: [x]) == \
         Parser(lambda x: [x], mode="exec").parse(
             "xs = input().split()\nprint(','.join(xs))")