Пример #1
0
 def test_raise(self):
     r = ast.Raise(None, ast.Num(self.space.wrap(3), 0, 0), 0, 0)
     self.stmt(r, "Raise with cause but no exception")
     r = ast.Raise(ast.Name("x", ast.Store, 0, 0), None, 0, 0)
     self.stmt(r, "must have Load context")
     r = ast.Raise(ast.Num(self.space.wrap(4), 0, 0),
                   ast.Name("x", ast.Store, 0, 0), 0, 0)
     self.stmt(r, "must have Load context")
Пример #2
0
 def test_with(self):
     p = ast.Pass(0, 0)
     self.stmt(ast.With([], [p], 0, 0), "empty items on With")
     i = ast.withitem(ast.Num(self.space.wrap(3), 0, 0), None)
     self.stmt(ast.With([i], [], 0, 0), "empty body on With")
     i = ast.withitem(ast.Name("x", ast.Store, 0, 0), None)
     self.stmt(ast.With([i], [p], 0, 0), "must have Load context")
     i = ast.withitem(ast.Num(self.space.wrap(3), 0, 0),
                      ast.Name("x", ast.Load, 0, 0))
     self.stmt(ast.With([i], [p], 0, 0), "must have Store context")
Пример #3
0
 def test_while(self):
     self.stmt(ast.While(ast.Num(self.space.wrap(3), 0, 0), [], [], 0, 0),
               "empty body on While")
     self.stmt(
         ast.While(ast.Name("x", ast.Store, 0, 0), [ast.Pass(0, 0)], [], 0,
                   0), "must have Load context")
     self.stmt(
         ast.While(ast.Num(self.space.wrap(3), 0, 0), [ast.Pass(0, 0)],
                   [ast.Expr(ast.Name("x", ast.Store, 0, 0), 0, 0)], 0, 0),
         "must have Load context")
Пример #4
0
 def test_operation(self, space):
     val1 = ast.Num(space.wrap(1), lineno=1, col_offset=1)
     val2 = ast.Num(space.wrap(2), lineno=1, col_offset=1)
     node = ast.BinOp(left=val1,
                      right=val2,
                      op=ast.Add,
                      lineno=1,
                      col_offset=1)
     w_node = node.to_object(space)
     w_op = space.getattr(w_node, space.wrap("op"))
     assert space.isinstance_w(w_op, ast.get(space).w_operator)
Пример #5
0
 def test_if(self):
     self.stmt(ast.If(ast.Num(self.space.wrap(3), 0, 0), [], [], 0, 0),
               "empty body on If")
     i = ast.If(ast.Name("x", ast.Store, 0, 0), [ast.Pass(0, 0)], [], 0, 0)
     self.stmt(i, "must have Load context")
     i = ast.If(ast.Num(self.space.wrap(3), 0, 0),
                [ast.Expr(ast.Name("x", ast.Store, 0, 0), 0, 0)], [], 0, 0)
     self.stmt(i, "must have Load context")
     i = ast.If(ast.Num(self.space.wrap(3), 0, 0), [ast.Pass(0, 0)],
                [ast.Expr(ast.Name("x", ast.Store, 0, 0), 0, 0)], 0, 0)
     self.stmt(i, "must have Load context")
Пример #6
0
 def test_assign(self):
     self.stmt(ast.Assign([], ast.Num(self.space.wrap(3), 0, 0), 0, 0),
               "empty targets on Assign")
     self.stmt(ast.Assign([None], ast.Num(self.space.wrap(3), 0, 0), 0, 0),
               "None disallowed")
     self.stmt(
         ast.Assign([ast.Name("x", ast.Load, 0, 0)],
                    ast.Num(self.space.wrap(3), 0, 0), 0, 0),
         "must have Store context")
     self.stmt(
         ast.Assign([ast.Name("x", ast.Store, 0, 0)],
                    ast.Name("y", ast.Store, 0, 0), 0, 0),
         "must have Load context")
Пример #7
0
 def test_compare(self):
     left = ast.Name("x", ast.Load, 0, 0)
     comp = ast.Compare(left, [ast.In], [], 0, 0)
     self.expr(comp, "no comparators")
     comp = ast.Compare(left, [ast.In], [
         ast.Num(self.space.wrap(4), 0, 0),
         ast.Num(self.space.wrap(5), 0, 0)
     ], 0, 0)
     self.expr(comp, "different number of comparators and operands")
     comp = ast.Compare(ast.Num(self.space.wrap("blah"), 0, 0), [ast.In],
                        [left], 0, 0)
     self.expr(comp, "non-numeric", exc=self.space.w_TypeError)
     comp = ast.Compare(left, [ast.In],
                        [ast.Num(self.space.wrap("blah"), 0, 0)], 0, 0)
     self.expr(comp, "non-numeric", exc=self.space.w_TypeError)
Пример #8
0
 def test_boolop(self):
     b = ast.BoolOp(ast.And, [], 0, 0)
     self.expr(b, "less than 2 values")
     b = ast.BoolOp(ast.And, None, 0, 0)
     self.expr(b, "less than 2 values")
     b = ast.BoolOp(ast.And, [ast.Num(self.space.wrap(3), 0, 0)], 0, 0)
     self.expr(b, "less than 2 values")
     b = ast.BoolOp(ast.And, [ast.Num(self.space.wrap(4), 0, 0), None], 0,
                    0)
     self.expr(b, "None disallowed")
     b = ast.BoolOp(ast.And, [
         ast.Num(self.space.wrap(4), 0, 0),
         ast.Name("x", ast.Store, 0, 0)
     ], 0, 0)
     self.expr(b, "must have Load context")
Пример #9
0
 def test_expr(self, space):
     value = space.wrap(42)
     node = ast.Num(value, lineno=1, col_offset=1)
     expr = ast.Expr(node, lineno=1, col_offset=1)
     w_node = expr.to_object(space)
     # node.value.n
     assert space.getattr(space.getattr(w_node, space.wrap("value")),
                          space.wrap("n")) is value
Пример #10
0
    def _check_arguments(self, fac, check):
        def arguments(args=None,
                      vararg=None,
                      kwonlyargs=None,
                      kw_defaults=None,
                      kwarg=None,
                      defaults=None):
            if args is None:
                args = []
            if kwonlyargs is None:
                kwonlyargs = []
            if defaults is None:
                defaults = []
            if kw_defaults is None:
                kw_defaults = []
            args = ast.arguments(args, vararg, kwonlyargs, kw_defaults, kwarg,
                                 defaults)
            return fac(args)

        args = [ast.arg("x", ast.Name("x", ast.Store, 0, 0), 0, 0)]
        check(arguments(args=args), "must have Load context")
        check(arguments(kwonlyargs=args), "must have Load context")
        check(arguments(defaults=[ast.Num(self.space.wrap(3), 0, 0)]),
              "more positional defaults than args")
        check(arguments(kw_defaults=[ast.Num(self.space.wrap(4), 0, 0)]),
              "length of kwonlyargs is not the same as kw_defaults")
        args = [ast.arg("x", ast.Name("x", ast.Load, 0, 0), 0, 0)]
        check(arguments(args=args, defaults=[ast.Name("x", ast.Store, 0, 0)]),
              "must have Load context")
        args = [
            ast.arg("a", ast.Name("x", ast.Load, 0, 0), 0, 0),
            ast.arg("b", ast.Name("y", ast.Load, 0, 0), 0, 0)
        ]
        check(
            arguments(kwonlyargs=args,
                      kw_defaults=[None, ast.Name("x", ast.Store, 0, 0)]),
            "must have Load context")
Пример #11
0
 def test_num(self):
     space = self.space
     w_objs = space.appexec([], """():
     class subint(int):
         pass
     class subfloat(float):
         pass
     class subcomplex(complex):
         pass
     return ("0", "hello", subint(), subfloat(), subcomplex())
     """)
     for w_obj in space.unpackiterable(w_objs):
         self.expr(ast.Num(w_obj, 0, 0),
                   "non-numeric",
                   exc=self.space.w_TypeError)
Пример #12
0
 def test_subscript(self):
     sub = ast.Subscript(ast.Name("x", ast.Store, 0, 0),
                         ast.Index(ast.Num(self.space.wrap(3), 0, 0)),
                         ast.Load, 0, 0)
     self.expr(sub, "must have Load context")
     x = ast.Name("x", ast.Load, 0, 0)
     sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store, 0, 0)),
                         ast.Load, 0, 0)
     self.expr(sub, "must have Load context")
     s = ast.Name("x", ast.Store, 0, 0)
     for args in (s, None, None), (None, s, None), (None, None, s):
         sl = ast.Slice(*args)
         self.expr(ast.Subscript(x, sl, ast.Load, 0, 0),
                   "must have Load context")
     sl = ast.ExtSlice([])
     self.expr(ast.Subscript(x, sl, ast.Load, 0, 0),
               "empty dims on ExtSlice")
     sl = ast.ExtSlice([ast.Index(s)])
     self.expr(ast.Subscript(x, sl, ast.Load, 0, 0),
               "must have Load context")
Пример #13
0
 def test_num(self, space):
     value = space.wrap(42)
     node = ast.Num(value, lineno=1, col_offset=1)
     w_node = node.to_object(space)
     assert space.is_w(space.getattr(w_node, space.wrap("n")), value)
Пример #14
0
 def test_starred(self):
     left = ast.List(
         [ast.Starred(ast.Name("x", ast.Load, 0, 0), ast.Store, 0, 0)],
         ast.Store, 0, 0)
     assign = ast.Assign([left], ast.Num(self.space.wrap(4), 0, 0), 0, 0)
     self.stmt(assign, "must have Store context")