def test_augassign(self): aug = ast.AugAssign(ast.Name("x", ast.Load, 0, 0), ast.Add, ast.Name("y", ast.Load, 0, 0), 0, 0) self.stmt(aug, "must have Store context") aug = ast.AugAssign(ast.Name("x", ast.Store, 0, 0), ast.Add, ast.Name("y", ast.Store, 0, 0), 0, 0) self.stmt(aug, "must have Load context")
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")
def test_dict(self): d = ast.Dict([], [ast.Name("x", ast.Load, 0, 0)], 0, 0) self.expr(d, "same number of keys as values") # This is now valid, and used for ``{**x}`` #d = ast.Dict([None], [ast.Name("x", ast.Load, 0, 0)], 0, 0) #self.expr(d, "None disallowed") d = ast.Dict([ast.Name("x", ast.Load, 0, 0)], [None], 0, 0) self.expr(d, "None disallowed")
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")
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")
def _simple_comp(self, fac): g = ast.comprehension(ast.Name("x", ast.Store, 0, 0), ast.Name("x", ast.Load, 0, 0), [], False) self.expr(fac(ast.Name("x", ast.Store, 0, 0), [g], 0, 0), "must have Load context") def wrap(gens): return fac(ast.Name("x", ast.Store, 0, 0), gens, 0, 0) self._check_comprehension(wrap)
def test_call(self): func = ast.Name("x", ast.Load, 0, 0) args = [ast.Name("y", ast.Load, 0, 0)] keywords = [ast.keyword("w", ast.Name("z", ast.Load, 0, 0))] call = ast.Call(ast.Name("x", ast.Store, 0, 0), args, keywords, 0, 0) self.expr(call, "must have Load context") call = ast.Call(func, [None], keywords, 0, 0) self.expr(call, "None disallowed") bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store, 0, 0))] call = ast.Call(func, args, bad_keywords, 0, 0) self.expr(call, "must have Load context")
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")
def test_for(self): x = ast.Name("x", ast.Store, 0, 0) y = ast.Name("y", ast.Load, 0, 0) p = ast.Pass(0, 0) self.stmt(ast.For(x, y, [], [], 0, 0), "empty body on For") self.stmt(ast.For(ast.Name("x", ast.Load, 0, 0), y, [p], [], 0, 0), "must have Store context") self.stmt(ast.For(x, ast.Name("y", ast.Store, 0, 0), [p], [], 0, 0), "must have Load context") e = ast.Expr(ast.Name("x", ast.Store, 0, 0), 0, 0) self.stmt(ast.For(x, y, [e], [], 0, 0), "must have Load context") self.stmt(ast.For(x, y, [p], [e], 0, 0), "must have Load context")
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")
def test_funcdef(self): a = ast.arguments([], None, [], [], None, []) f = ast.FunctionDef("x", a, [], [], None, 0, 0) self.stmt(f, "empty body on FunctionDef") f = ast.FunctionDef("x", a, [ast.Pass(0, 0)], [ast.Name("x", ast.Store, 0, 0)], None, 0, 0) self.stmt(f, "must have Load context") f = ast.FunctionDef("x", a, [ast.Pass(0, 0)], [], ast.Name("x", ast.Store, 0, 0), 0, 0) self.stmt(f, "must have Load context") def fac(args): return ast.FunctionDef("x", args, [ast.Pass(0, 0)], [], None, 0, 0) self._check_arguments(fac, self.stmt)
def parse_attraccess(tokens, builder): """parses token list like ['a', '.', 'b', '.', 'c', ...] and returns an ast node : ast.Getattr(Getattr(Name('a'), 'b'), 'c' ...) """ token = tokens[0] # XXX HACK for when parse_attraccess is called from build_decorator if isinstance(token, TokenObject): val = token.get_value() result = ast.Name(val, token.lineno) else: result = token index = 1 while index < len(tokens): token = tokens[index] if isinstance( token, TokenObject) and token.name == builder.parser.tokens['DOT']: index += 1 token = tokens[index] assert isinstance(token, TokenObject) result = ast.Getattr(result, token.get_value(), token.lineno) elif isinstance(token, ArglistObject): result = reduce_callfunc(result, token) elif isinstance(token, SubscriptObject): result = reduce_subscript(result, token) elif isinstance(token, SlicelistObject): result = reduce_slice(result, token) else: assert False, "Don't know how to handle index %s of %s" % ( index, len(tokens)) index += 1 return result
def test_lambda(self): a = ast.arguments([], None, [], [], None, []) self.expr(ast.Lambda(a, ast.Name("x", ast.Store, 0, 0), 0, 0), "must have Load context") def fac(args): return ast.Lambda(args, ast.Name("x", ast.Load, 0, 0), 0, 0) self._check_arguments(fac, self.expr)
def build_atom(builder, nb): atoms = get_atoms(builder, nb) top = atoms[0] if isinstance(top, TokenObject): # assert isinstance(top, TokenObject) # rtyper if top.name == builder.parser.tokens['LPAR']: if len(atoms) == 2: builder.push(ast.Tuple([], top.lineno)) else: builder.push(atoms[1]) elif top.name == builder.parser.tokens['LSQB']: if len(atoms) == 2: builder.push(ast.List([], top.lineno)) else: list_node = atoms[1] list_node.lineno = top.lineno builder.push(list_node) elif top.name == builder.parser.tokens['LBRACE']: items = [] for index in range(1, len(atoms) - 1, 4): # a : b , c : d # ^ +1 +2 +3 +4 items.append((atoms[index], atoms[index + 2])) builder.push(ast.Dict(items, top.lineno)) elif top.name == builder.parser.tokens['NAME']: val = top.get_value() builder.push(ast.Name(val, top.lineno)) elif top.name == builder.parser.tokens['NUMBER']: builder.push( ast.Const(builder.eval_number(top.get_value()), top.lineno)) elif top.name == builder.parser.tokens['STRING']: # need to concatenate strings in atoms s = '' if len(atoms) == 1: token = atoms[0] assert isinstance(token, TokenObject) builder.push( ast.Const( parsestr(builder.space, builder.source_encoding, token.get_value()), top.lineno)) else: space = builder.space empty = space.wrap('') accum = [] for token in atoms: assert isinstance(token, TokenObject) accum.append( parsestr(builder.space, builder.source_encoding, token.get_value())) w_s = space.call_method(empty, 'join', space.newlist(accum)) builder.push(ast.Const(w_s, top.lineno)) elif top.name == builder.parser.tokens['BACKQUOTE']: builder.push(ast.Backquote(atoms[1], atoms[1].lineno)) else: raise SyntaxError("unexpected tokens", top.lineno, top.col)
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")
def test_classdef(self): def cls(bases=None, keywords=None, body=None, decorator_list=None): if bases is None: bases = [] if keywords is None: keywords = [] if body is None: body = [ast.Pass(0, 0)] if decorator_list is None: decorator_list = [] return ast.ClassDef("myclass", bases, keywords, body, decorator_list, 0, 0) self.stmt(cls(bases=[ast.Name("x", ast.Store, 0, 0)]), "must have Load context") self.stmt( cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store, 0, 0))]), "must have Load context") self.stmt(cls(body=[]), "empty body on ClassDef") self.stmt(cls(body=[None]), "None disallowed") self.stmt(cls(decorator_list=[ast.Name("x", ast.Store, 0, 0)]), "must have Load context")
def test_try(self): p = ast.Pass(0, 0) t = ast.Try([], [], [], [p], 0, 0) self.stmt(t, "empty body on Try") t = ast.Try([ast.Expr(ast.Name("x", ast.Store, 0, 0), 0, 0)], [], [], [p], 0, 0) self.stmt(t, "must have Load context") t = ast.Try([p], [], [], [], 0, 0) self.stmt(t, "Try has neither except handlers nor finalbody") t = ast.Try([p], [], [p], [p], 0, 0) self.stmt(t, "Try has orelse but no except handlers") t = ast.Try([p], [ast.ExceptHandler(None, "x", [], 0, 0)], [], [], 0, 0) self.stmt(t, "empty body on ExceptHandler") e = [ast.ExceptHandler(ast.Name("x", ast.Store, 0, 0), "y", [p], 0, 0)] self.stmt(ast.Try([p], e, [], [], 0, 0), "must have Load context") e = [ast.ExceptHandler(None, "x", [p], 0, 0)] t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store, 0, 0), 0, 0)], [p], 0, 0) self.stmt(t, "must have Load context") t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store, 0, 0), 0, 0)], 0, 0) self.stmt(t, "must have Load context")
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")
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)
def _check_comprehension(self, fac): self.expr(fac([]), "comprehension with no generators") g = ast.comprehension(ast.Name("x", ast.Load, 0, 0), ast.Name("x", ast.Load, 0, 0), [], False) self.expr(fac([g]), "must have Store context") g = ast.comprehension(ast.Name("x", ast.Store, 0, 0), ast.Name("x", ast.Store, 0, 0), [], False) self.expr(fac([g]), "must have Load context") x = ast.Name("x", ast.Store, 0, 0) y = ast.Name("y", ast.Load, 0, 0) g = ast.comprehension(x, y, [None], False) self.expr(fac([g]), "None disallowed") g = ast.comprehension(x, y, [ast.Name("x", ast.Store, 0, 0)], False) self.expr(fac([g]), "must have Load context")
def test_dictcomp(self): g = ast.comprehension(ast.Name("y", ast.Store, 0, 0), ast.Name("p", ast.Load, 0, 0), [], False) c = ast.DictComp(ast.Name("x", ast.Store, 0, 0), ast.Name("y", ast.Load, 0, 0), [g], 0, 0) self.expr(c, "must have Load context") c = ast.DictComp(ast.Name("x", ast.Load, 0, 0), ast.Name("y", ast.Store, 0, 0), [g], 0, 0) self.expr(c, "must have Load context") def factory(comps): k = ast.Name("x", ast.Load, 0, 0) v = ast.Name("y", ast.Load, 0, 0) return ast.DictComp(k, v, comps, 0, 0) self._check_comprehension(factory)
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")
def _sequence(self, fac): self.expr(fac([None], ast.Load, 0, 0), "None disallowed") self.expr(fac([ast.Name("x", ast.Store, 0, 0)], ast.Load, 0, 0), "must have Load context")
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")
def test_attribute(self): attr = ast.Attribute(ast.Name("x", ast.Store, 0, 0), "y", ast.Load, 0, 0) self.expr(attr, "must have Load context")
def test_set(self): self.expr(ast.Set([None], 0, 0), "None disallowed") s = ast.Set([ast.Name("x", ast.Store, 0, 0)], 0, 0) self.expr(s, "must have Load context")
def test_yield(self): self.expr(ast.Yield(ast.Name("x", ast.Store, 0, 0), 0, 0), "must have Load") self.expr(ast.YieldFrom(ast.Name("x", ast.Store, 0, 0), 0, 0), "must have Load")
def test_module(self): m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store, 0, 0), 0, 0)]) self.mod(m, "must have Load context", "single") m = ast.Expression(ast.Name("x", ast.Store, 0, 0)) self.mod(m, "must have Load context", "eval")
def factory(comps): k = ast.Name("x", ast.Load, 0, 0) v = ast.Name("y", ast.Load, 0, 0) return ast.DictComp(k, v, comps, 0, 0)
def wrap(gens): return fac(ast.Name("x", ast.Store, 0, 0), gens, 0, 0)