| 1 |
import os |
| 2 |
from pytest import raises |
| 3 |
from pypy.interpreter.error import OperationError |
| 4 |
from pypy.interpreter.baseobjspace import W_Root |
| 5 |
from pypy.interpreter.astcompiler import ast |
| 6 |
from pypy.interpreter.astcompiler import validate |
| 7 |
|
| 8 |
POS = (0, 0, 0, 0) |
| 9 |
|
| 10 |
class TestASTValidator: |
| 11 |
def mod(self, mod, msg=None, mode="exec", exc=validate.ValidationError): |
| 12 |
space = self.space |
| 13 |
if isinstance(exc, W_Root): |
| 14 |
w_exc = exc |
| 15 |
exc = OperationError |
| 16 |
else: |
| 17 |
w_exc = None |
| 18 |
with raises(exc) as cm: |
| 19 |
validate.validate_ast(space, mod) |
| 20 |
if w_exc is not None: |
| 21 |
w_value = cm.value.get_w_value(space) |
| 22 |
assert cm.value.match(space, w_exc) |
| 23 |
exc_msg = str(cm.value) |
| 24 |
else: |
| 25 |
exc_msg = str(cm.value) |
| 26 |
if msg is not None: |
| 27 |
assert msg in exc_msg |
| 28 |
|
| 29 |
def expr(self, node, msg=None, exc=validate.ValidationError): |
| 30 |
mod = ast.Module([ast.Expr(node, *POS)], []) |
| 31 |
self.mod(mod, msg, exc=exc) |
| 32 |
|
| 33 |
def stmt(self, stmt, msg=None): |
| 34 |
mod = ast.Module([stmt], []) |
| 35 |
self.mod(mod, msg) |
| 36 |
|
| 37 |
def __getattr__(self, name): |
| 38 |
nodecls = getattr(ast, name) |
| 39 |
if isinstance(nodecls, type) and issubclass(nodecls, ast.AST): |
| 40 |
def make(*args): |
| 41 |
return nodecls(*(args + POS)) |
| 42 |
return make |
| 43 |
raise AttributeError(name) |
| 44 |
|
| 45 |
def Constant(self, value, ctx=None): |
| 46 |
return ast.Constant( |
| 47 |
self.space.wrap(value), |
| 48 |
self.space.w_None if ctx is None else ctx, |
| 49 |
*POS) |
| 50 |
|
| 51 |
def test_module(self): |
| 52 |
m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store, *POS), *POS)]) |
| 53 |
self.mod(m, "must have Load context", "single") |
| 54 |
m = ast.Expression(ast.Name("x", ast.Store, *POS)) |
| 55 |
self.mod(m, "must have Load context", "eval") |
| 56 |
|
| 57 |
def _check_arguments(self, fac, check): |
| 58 |
def arguments(args=None, posonlyargs=None, vararg=None, |
| 59 |
kwonlyargs=None, kwarg=None, |
| 60 |
defaults=None, kw_defaults=None): |
| 61 |
if args is None: |
| 62 |
args = [] |
| 63 |
if posonlyargs is None: |
| 64 |
posonlyargs = [] |
| 65 |
if kwonlyargs is None: |
| 66 |
kwonlyargs = [] |
| 67 |
if defaults is None: |
| 68 |
defaults = [] |
| 69 |
if kw_defaults is None: |
| 70 |
kw_defaults = [] |
| 71 |
args = ast.arguments(args, posonlyargs, vararg, kwonlyargs, |
| 72 |
kw_defaults, kwarg, defaults) |
| 73 |
return fac(args) |
| 74 |
args = [ast.arg("x", ast.Name("x", ast.Store, *POS), None, *POS)] |
| 75 |
check(arguments(args=args), "must have Load context") |
| 76 |
check(arguments(posonlyargs=args), "must have Load context") |
| 77 |
check(arguments(kwonlyargs=args), "must have Load context") |
| 78 |
check(arguments(defaults=[self.Constant(3)]), |
| 79 |
"more positional defaults than args") |
| 80 |
check(arguments(kw_defaults=[self.Constant(4)]), |
| 81 |
"length of kwonlyargs is not the same as kw_defaults") |
| 82 |
args = [ast.arg("x", ast.Name("x", ast.Load, *POS), None, *POS)] |
| 83 |
check(arguments(args=args, defaults=[ast.Name("x", ast.Store, *POS)]), |
| 84 |
"must have Load context") |
| 85 |
args = [ast.arg("a", ast.Name("x", ast.Load, *POS), None, *POS), |
| 86 |
ast.arg("b", ast.Name("y", ast.Load, *POS), None, *POS)] |
| 87 |
check(arguments(kwonlyargs=args, |
| 88 |
kw_defaults=[None, ast.Name("x", ast.Store, *POS)]), |
| 89 |
"must have Load context") |
| 90 |
|
| 91 |
def test_funcdef(self): |
| 92 |
a = ast.arguments(None, [], None, [], [], None, []) |
| 93 |
f = ast.FunctionDef("x", a, [], [], None, None, *POS) |
| 94 |
self.stmt(f, "empty body on FunctionDef") |
| 95 |
f = ast.FunctionDef("x", a, [ast.Pass(*POS)], [ast.Name("x", ast.Store, *POS)], |
| 96 |
None, None, *POS) |
| 97 |
self.stmt(f, "must have Load context") |
| 98 |
f = ast.FunctionDef("x", a, [ast.Pass(*POS)], [], |
| 99 |
ast.Name("x", ast.Store, *POS), None, *POS) |
| 100 |
self.stmt(f, "must have Load context") |
| 101 |
def fac(args): |
| 102 |
return ast.FunctionDef("x", args, [ast.Pass(*POS)], [], None, None, *POS) |
| 103 |
self._check_arguments(fac, self.stmt) |
| 104 |
|
| 105 |
def test_classdef(self): |
| 106 |
def cls(bases=None, keywords=None, body=None, decorator_list=None): |
| 107 |
if bases is None: |
| 108 |
bases = [] |
| 109 |
if keywords is None: |
| 110 |
keywords = [] |
| 111 |
if body is None: |
| 112 |
body = [ast.Pass(*POS)] |
| 113 |
if decorator_list is None: |
| 114 |
decorator_list = [] |
| 115 |
return ast.ClassDef("myclass", bases, keywords, |
| 116 |
body, decorator_list, *POS) |
| 117 |
self.stmt(cls(bases=[ast.Name("x", ast.Store, *POS)]), |
| 118 |
"must have Load context") |
| 119 |
self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store, *POS), *POS)]), |
| 120 |
"must have Load context") |
| 121 |
self.stmt(cls(body=[]), "empty body on ClassDef") |
| 122 |
self.stmt(cls(body=[None]), "None disallowed") |
| 123 |
self.stmt(cls(decorator_list=[ast.Name("x", ast.Store, *POS)]), |
| 124 |
"must have Load context") |
| 125 |
|
| 126 |
def test_delete(self): |
| 127 |
self.stmt(ast.Delete([], *POS), "empty targets on Delete") |
| 128 |
self.stmt(ast.Delete([None], *POS), "None disallowed") |
| 129 |
self.stmt(ast.Delete([ast.Name("x", ast.Load, *POS)], *POS), |
| 130 |
"must have Del context") |
| 131 |
|
| 132 |
def test_assign(self): |
| 133 |
self.stmt(ast.Assign([], ast.Constant(self.space.wrap(3), self.space.w_None, *POS), None, *POS), "empty targets on Assign") |
| 134 |
self.stmt(ast.Assign([None], ast.Constant(self.space.wrap(3), self.space.w_None, *POS), None, *POS), "None disallowed") |
| 135 |
self.stmt(ast.Assign([ast.Name("x", ast.Load, *POS)], ast.Constant(self.space.wrap(3), self.space.w_None, *POS), None, *POS), |
| 136 |
"must have Store context") |
| 137 |
self.stmt(ast.Assign([ast.Name("x", ast.Store, *POS)], |
| 138 |
ast.Name("y", ast.Store, *POS), None, *POS), |
| 139 |
"must have Load context") |
| 140 |
|
| 141 |
def test_augassign(self): |
| 142 |
aug = ast.AugAssign(ast.Name("x", ast.Load, *POS), ast.Add, |
| 143 |
ast.Name("y", ast.Load, *POS), *POS) |
| 144 |
self.stmt(aug, "must have Store context") |
| 145 |
aug = ast.AugAssign(ast.Name("x", ast.Store, *POS), ast.Add, |
| 146 |
ast.Name("y", ast.Store, *POS), *POS) |
| 147 |
self.stmt(aug, "must have Load context") |
| 148 |
|
| 149 |
def test_for(self): |
| 150 |
x = ast.Name("x", ast.Store, *POS) |
| 151 |
y = ast.Name("y", ast.Load, *POS) |
| 152 |
p = ast.Pass(*POS) |
| 153 |
self.stmt(ast.For(x, y, [], [], None, *POS), "empty body on For") |
| 154 |
self.stmt(ast.For(ast.Name("x", ast.Load, *POS), y, [p], [], None, *POS), |
| 155 |
"must have Store context") |
| 156 |
self.stmt(ast.For(x, ast.Name("y", ast.Store, *POS), [p], [], None, *POS), |
| 157 |
"must have Load context") |
| 158 |
e = ast.Expr(ast.Name("x", ast.Store, *POS), *POS) |
| 159 |
self.stmt(ast.For(x, y, [e], [], None, *POS), "must have Load context") |
| 160 |
self.stmt(ast.For(x, y, [p], [e], None, *POS), "must have Load context") |
| 161 |
|
| 162 |
def test_while(self): |
| 163 |
self.stmt(ast.While(ast.Constant(self.space.wrap(3), self.space.w_None, *POS), [], [], *POS), "empty body on While") |
| 164 |
self.stmt(ast.While(ast.Name("x", ast.Store, *POS), [ast.Pass(*POS)], [], *POS), |
| 165 |
"must have Load context") |
| 166 |
self.stmt(ast.While(ast.Constant(self.space.wrap(3), self.space.w_None, *POS), [ast.Pass(*POS)], |
| 167 |
[ast.Expr(ast.Name("x", ast.Store, *POS), *POS)], *POS), |
| 168 |
"must have Load context") |
| 169 |
|
| 170 |
def test_if(self): |
| 171 |
self.stmt(ast.If(ast.Constant(self.space.wrap(3), self.space.w_None, *POS), [], [], *POS), "empty body on If") |
| 172 |
i = ast.If(ast.Name("x", ast.Store, *POS), [ast.Pass(*POS)], [], *POS) |
| 173 |
self.stmt(i, "must have Load context") |
| 174 |
i = ast.If(ast.Constant(self.space.wrap(3), self.space.w_None, *POS), [ast.Expr(ast.Name("x", ast.Store, *POS), *POS)], [], *POS) |
| 175 |
self.stmt(i, "must have Load context") |
| 176 |
i = ast.If(ast.Constant(self.space.wrap(3), self.space.w_None, *POS), [ast.Pass(*POS)], |
| 177 |
[ast.Expr(ast.Name("x", ast.Store, *POS), *POS)], *POS) |
| 178 |
self.stmt(i, "must have Load context") |
| 179 |
|
| 180 |
def test_with(self): |
| 181 |
p = ast.Pass(*POS) |
| 182 |
self.stmt(ast.With([], [p], None, *POS), "empty items on With") |
| 183 |
i = ast.withitem(ast.Constant(self.space.wrap(3), self.space.w_None, *POS), None) |
| 184 |
self.stmt(ast.With([i], [], None, *POS), "empty body on With") |
| 185 |
i = ast.withitem(ast.Name("x", ast.Store, *POS), None) |
| 186 |
self.stmt(ast.With([i], [p], None, *POS), "must have Load context") |
| 187 |
i = ast.withitem(ast.Constant(self.space.wrap(3), self.space.w_None, *POS), ast.Name("x", ast.Load, *POS)) |
| 188 |
self.stmt(ast.With([i], [p], None, *POS), "must have Store context") |
| 189 |
|
| 190 |
def test_raise(self): |
| 191 |
r = ast.Raise(None, ast.Constant(self.space.wrap(3), self.space.w_None, *POS), *POS) |
| 192 |
self.stmt(r, "Raise with cause but no exception") |
| 193 |
r = ast.Raise(ast.Name("x", ast.Store, *POS), None, *POS) |
| 194 |
self.stmt(r, "must have Load context") |
| 195 |
r = ast.Raise(ast.Constant(self.space.wrap(4), self.space.w_None, *POS), ast.Name("x", ast.Store, *POS), *POS) |
| 196 |
self.stmt(r, "must have Load context") |
| 197 |
|
| 198 |
def test_try(self): |
| 199 |
p = ast.Pass(*POS) |
| 200 |
t = ast.Try([], [], [], [p], *POS) |
| 201 |
self.stmt(t, "empty body on Try") |
| 202 |
t = ast.Try([ast.Expr(ast.Name("x", ast.Store, *POS), *POS)], [], [], [p], *POS) |
| 203 |
self.stmt(t, "must have Load context") |
| 204 |
t = ast.Try([p], [], [], [], *POS) |
| 205 |
self.stmt(t, "Try has neither except handlers nor finalbody") |
| 206 |
t = ast.Try([p], [], [p], [p], *POS) |
| 207 |
self.stmt(t, "Try has orelse but no except handlers") |
| 208 |
t = ast.Try([p], [ast.ExceptHandler(None, "x", [], *POS)], [], [], *POS) |
| 209 |
self.stmt(t, "empty body on ExceptHandler") |
| 210 |
e = [ast.ExceptHandler(ast.Name("x", ast.Store, *POS), "y", [p], *POS)] |
| 211 |
self.stmt(ast.Try([p], e, [], [], *POS), "must have Load context") |
| 212 |
e = [ast.ExceptHandler(None, "x", [p], *POS)] |
| 213 |
t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store, *POS), *POS)], [p], *POS) |
| 214 |
self.stmt(t, "must have Load context") |
| 215 |
t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store, *POS), *POS)], *POS) |
| 216 |
self.stmt(t, "must have Load context") |
| 217 |
|
| 218 |
def test_try_star(self): |
| 219 |
p = ast.Pass(*POS) |
| 220 |
t = ast.TryStar([], [], [], [p], *POS) |
| 221 |
self.stmt(t, "empty body on TryStar") |
| 222 |
t = ast.TryStar([ast.Expr(ast.Name("x", ast.Store, *POS), *POS)], [], [], [p], *POS) |
| 223 |
self.stmt(t, "must have Load context") |
| 224 |
t = ast.TryStar([p], [], [], [], *POS) |
| 225 |
self.stmt(t, "TryStar has neither except handlers nor finalbody") |
| 226 |
t = ast.TryStar([p], [], [p], [p], *POS) |
| 227 |
self.stmt(t, "TryStar has orelse but no except handlers") |
| 228 |
t = ast.TryStar([p], [ast.ExceptHandler(None, "x", [], *POS)], [], [], *POS) |
| 229 |
self.stmt(t, "empty body on ExceptHandler") |
| 230 |
e = [ast.ExceptHandler(ast.Name("x", ast.Store, *POS), "y", [p], *POS)] |
| 231 |
self.stmt(ast.TryStar([p], e, [], [], *POS), "must have Load context") |
| 232 |
e = [ast.ExceptHandler(None, "x", [p], *POS)] |
| 233 |
t = ast.TryStar([p], e, [ast.Expr(ast.Name("x", ast.Store, *POS), *POS)], [p], *POS) |
| 234 |
self.stmt(t, "must have Load context") |
| 235 |
t = ast.TryStar([p], e, [p], [ast.Expr(ast.Name("x", ast.Store, *POS), *POS)], *POS) |
| 236 |
self.stmt(t, "must have Load context") |
| 237 |
|
| 238 |
def test_assert(self): |
| 239 |
self.stmt(ast.Assert(ast.Name("x", ast.Store, *POS), None, *POS), |
| 240 |
"must have Load context") |
| 241 |
assrt = ast.Assert(ast.Name("x", ast.Load, *POS), |
| 242 |
ast.Name("y", ast.Store, *POS), *POS) |
| 243 |
self.stmt(assrt, "must have Load context") |
| 244 |
|
| 245 |
def test_import(self): |
| 246 |
self.stmt(ast.Import([], *POS), "empty names on Import") |
| 247 |
|
| 248 |
def test_importfrom(self): |
| 249 |
imp = ast.ImportFrom(None, [ast.alias("x", None, *POS)], -42, *POS) |
| 250 |
self.stmt(imp, "Negative ImportFrom level") |
| 251 |
self.stmt(ast.ImportFrom(None, [], 0, *POS), "empty names on ImportFrom") |
| 252 |
|
| 253 |
def test_global(self): |
| 254 |
self.stmt(ast.Global([], *POS), "empty names on Global") |
| 255 |
|
| 256 |
def test_nonlocal(self): |
| 257 |
self.stmt(ast.Nonlocal([], *POS), "empty names on Nonlocal") |
| 258 |
|
| 259 |
def test_expr(self): |
| 260 |
e = ast.Expr(ast.Name("x", ast.Store, *POS), *POS) |
| 261 |
self.stmt(e, "must have Load context") |
| 262 |
|
| 263 |
def test_name(self): |
| 264 |
for name in ("True", "False", "None"): |
| 265 |
e = ast.Name(name, ast.Load, *POS) |
| 266 |
self.expr(e, "identifier field can't represent '%s' constant" % name) |
| 267 |
|
| 268 |
def test_boolop(self): |
| 269 |
b = ast.BoolOp(ast.And, [], *POS) |
| 270 |
self.expr(b, "less than 2 values") |
| 271 |
b = ast.BoolOp(ast.And, None, *POS) |
| 272 |
self.expr(b, "less than 2 values") |
| 273 |
b = ast.BoolOp(ast.And, [ast.Constant(self.space.wrap(3), self.space.w_None, *POS)], *POS) |
| 274 |
self.expr(b, "less than 2 values") |
| 275 |
b = ast.BoolOp(ast.And, [ast.Constant(self.space.wrap(4), self.space.w_None, *POS), None], *POS) |
| 276 |
self.expr(b, "None disallowed") |
| 277 |
b = ast.BoolOp(ast.And, [ast.Constant(self.space.wrap(4), self.space.w_None, *POS), ast.Name("x", ast.Store, *POS)], *POS) |
| 278 |
self.expr(b, "must have Load context") |
| 279 |
|
| 280 |
def test_unaryop(self): |
| 281 |
u = ast.UnaryOp(ast.Not, ast.Name("x", ast.Store, *POS), *POS) |
| 282 |
self.expr(u, "must have Load context") |
| 283 |
|
| 284 |
def test_lambda(self): |
| 285 |
a = ast.arguments(None, [], None, [], [], None, []) |
| 286 |
self.expr(ast.Lambda(a, ast.Name("x", ast.Store, *POS), *POS), |
| 287 |
"must have Load context") |
| 288 |
def fac(args): |
| 289 |
return ast.Lambda(args, ast.Name("x", ast.Load, *POS), *POS) |
| 290 |
self._check_arguments(fac, self.expr) |
| 291 |
|
| 292 |
def test_ifexp(self): |
| 293 |
l = ast.Name("x", ast.Load, *POS) |
| 294 |
s = ast.Name("y", ast.Store, *POS) |
| 295 |
for args in (s, l, l), (l, s, l), (l, l, s): |
| 296 |
self.expr(ast.IfExp(*(args + POS)), "must have Load context") |
| 297 |
|
| 298 |
def test_dict(self): |
| 299 |
d = ast.Dict([], [ast.Name("x", ast.Load, *POS)], *POS) |
| 300 |
self.expr(d, "same number of keys as values") |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
d = ast.Dict([ast.Name("x", ast.Load, *POS)], [None], *POS) |
| 305 |
self.expr(d, "None disallowed") |
| 306 |
|
| 307 |
def test_set(self): |
| 308 |
self.expr(ast.Set([None], *POS), "None disallowed") |
| 309 |
s = ast.Set([ast.Name("x", ast.Store, *POS)], *POS) |
| 310 |
self.expr(s, "must have Load context") |
| 311 |
|
| 312 |
def _check_comprehension(self, fac): |
| 313 |
self.expr(fac([]), "comprehension with no generators") |
| 314 |
g = ast.comprehension(ast.Name("x", ast.Load, *POS), |
| 315 |
ast.Name("x", ast.Load, *POS), [], False) |
| 316 |
self.expr(fac([g]), "must have Store context") |
| 317 |
g = ast.comprehension(ast.Name("x", ast.Store, *POS), |
| 318 |
ast.Name("x", ast.Store, *POS), [], False) |
| 319 |
self.expr(fac([g]), "must have Load context") |
| 320 |
x = ast.Name("x", ast.Store, *POS) |
| 321 |
y = ast.Name("y", ast.Load, *POS) |
| 322 |
g = ast.comprehension(x, y, [None], False) |
| 323 |
self.expr(fac([g]), "None disallowed") |
| 324 |
g = ast.comprehension(x, y, [ast.Name("x", ast.Store, *POS)], False) |
| 325 |
self.expr(fac([g]), "must have Load context") |
| 326 |
|
| 327 |
def _simple_comp(self, fac): |
| 328 |
g = ast.comprehension(ast.Name("x", ast.Store, *POS), |
| 329 |
ast.Name("x", ast.Load, *POS), [], False) |
| 330 |
self.expr(fac(ast.Name("x", ast.Store, *POS), [g], *POS), |
| 331 |
"must have Load context") |
| 332 |
def wrap(gens): |
| 333 |
return fac(ast.Name("x", ast.Store, *POS), gens, *POS) |
| 334 |
self._check_comprehension(wrap) |
| 335 |
|
| 336 |
def test_listcomp(self): |
| 337 |
self._simple_comp(ast.ListComp) |
| 338 |
|
| 339 |
def test_setcomp(self): |
| 340 |
self._simple_comp(ast.SetComp) |
| 341 |
|
| 342 |
def test_generatorexp(self): |
| 343 |
self._simple_comp(ast.GeneratorExp) |
| 344 |
|
| 345 |
def test_dictcomp(self): |
| 346 |
g = ast.comprehension(ast.Name("y", ast.Store, *POS), |
| 347 |
ast.Name("p", ast.Load, *POS), [], False) |
| 348 |
c = ast.DictComp(ast.Name("x", ast.Store, *POS), |
| 349 |
ast.Name("y", ast.Load, *POS), [g], *POS) |
| 350 |
self.expr(c, "must have Load context") |
| 351 |
c = ast.DictComp(ast.Name("x", ast.Load, *POS), |
| 352 |
ast.Name("y", ast.Store, *POS), [g], *POS) |
| 353 |
self.expr(c, "must have Load context") |
| 354 |
def factory(comps): |
| 355 |
k = ast.Name("x", ast.Load, *POS) |
| 356 |
v = ast.Name("y", ast.Load, *POS) |
| 357 |
return ast.DictComp(k, v, comps, *POS) |
| 358 |
self._check_comprehension(factory) |
| 359 |
|
| 360 |
def test_yield(self): |
| 361 |
self.expr(ast.Yield(ast.Name("x", ast.Store, *POS), *POS), "must have Load") |
| 362 |
self.expr(ast.YieldFrom(ast.Name("x", ast.Store, *POS), *POS), "must have Load") |
| 363 |
|
| 364 |
def test_compare(self): |
| 365 |
left = ast.Name("x", ast.Load, *POS) |
| 366 |
comp = ast.Compare(left, [ast.In], [], *POS) |
| 367 |
self.expr(comp, "no comparators") |
| 368 |
comp = ast.Compare(left, [ast.In], [ast.Constant(self.space.wrap(4), self.space.w_None, *POS), ast.Constant(self.space.wrap(5), self.space.w_None, *POS)], *POS) |
| 369 |
self.expr(comp, "different number of comparators and operands") |
| 370 |
|
| 371 |
def test_call(self): |
| 372 |
func = ast.Name("x", ast.Load, *POS) |
| 373 |
args = [ast.Name("y", ast.Load, *POS)] |
| 374 |
keywords = [ast.keyword("w", ast.Name("z", ast.Load, *POS), *POS)] |
| 375 |
call = ast.Call(ast.Name("x", ast.Store, *POS), args, keywords, *POS) |
| 376 |
self.expr(call, "must have Load context") |
| 377 |
call = ast.Call(func, [None], keywords, *POS) |
| 378 |
self.expr(call, "None disallowed") |
| 379 |
bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store, *POS), *POS)] |
| 380 |
call = ast.Call(func, args, bad_keywords, *POS) |
| 381 |
self.expr(call, "must have Load context") |
| 382 |
|
| 383 |
def test_attribute(self): |
| 384 |
attr = ast.Attribute(ast.Name("x", ast.Store, *POS), "y", ast.Load, *POS) |
| 385 |
self.expr(attr, "must have Load context") |
| 386 |
|
| 387 |
def test_subscript(self): |
| 388 |
sub = ast.Subscript(ast.Name("x", ast.Store, *POS), ast.Constant(self.space.wrap(3), self.space.w_None, *POS), |
| 389 |
ast.Load, *POS) |
| 390 |
self.expr(sub, "must have Load context") |
| 391 |
x = ast.Name("x", ast.Load, *POS) |
| 392 |
sub = ast.Subscript(x, ast.Name("y", ast.Store, *POS), |
| 393 |
ast.Load, *POS) |
| 394 |
self.expr(sub, "must have Load context") |
| 395 |
s = ast.Name("x", ast.Store, *POS) |
| 396 |
for args in (s, None, None), (None, s, None), (None, None, s): |
| 397 |
sl = ast.Slice(*args + POS) |
| 398 |
self.expr(ast.Subscript(x, sl, ast.Load, *POS), |
| 399 |
"must have Load context") |
| 400 |
|
| 401 |
def test_starred(self): |
| 402 |
left = ast.List([ast.Starred(ast.Name("x", ast.Load, *POS), ast.Store, *POS)], |
| 403 |
ast.Store, *POS) |
| 404 |
assign = ast.Assign([left], ast.Constant(self.space.wrap(4), self.space.w_None, *POS), None, *POS) |
| 405 |
self.stmt(assign, "must have Store context") |
| 406 |
|
| 407 |
def _sequence(self, fac): |
| 408 |
self.expr(fac([None], ast.Load, *POS), "None disallowed") |
| 409 |
self.expr(fac([ast.Name("x", ast.Store, *POS)], ast.Load, *POS), |
| 410 |
"must have Load context") |
| 411 |
|
| 412 |
def test_list(self): |
| 413 |
self._sequence(ast.List) |
| 414 |
|
| 415 |
def test_tuple(self): |
| 416 |
self._sequence(ast.Tuple) |
| 417 |
|
| 418 |
def test_constant(self): |
| 419 |
node = ast.Constant(self.space.newlist([1]), self.space.w_None, *POS) |
| 420 |
self.expr(node, "got an invalid type in Constant: list", |
| 421 |
exc=validate.ValidationTypeError) |
| 422 |
|
| 423 |
def test_constant_subtypes(self): |
| 424 |
space = self.space |
| 425 |
w_objs = space.appexec([], """(): |
| 426 |
class subint(int): |
| 427 |
pass |
| 428 |
class subfloat(float): |
| 429 |
pass |
| 430 |
class subcomplex(complex): |
| 431 |
pass |
| 432 |
return (subint(), subfloat(), subcomplex()) |
| 433 |
""") |
| 434 |
for w_obj in space.unpackiterable(w_objs): |
| 435 |
self.expr(ast.Constant(w_obj, self.space.w_None, *POS), "got an invalid type in Constant") |
| 436 |
|
| 437 |
def test_subscript_tuple(self): |
| 438 |
|
| 439 |
ec = self.space.getexecutioncontext() |
| 440 |
ast_node = ec.compiler.compile_to_ast("x = nd[()]", "?", "exec", 0) |
| 441 |
validate.validate_ast(self.space, ast_node) |
| 442 |
|
| 443 |
def test_stdlib_validates(self): |
| 444 |
stdlib = os.path.join(os.path.dirname(ast.__file__), '../../../lib-python/3') |
| 445 |
if 0: |
| 446 |
tests = [fn for fn in os.listdir(stdlib) if fn.endswith('.py')] |
| 447 |
tests += ['test/'+fn for fn in os.listdir(stdlib+'/test') |
| 448 |
if fn.endswith('.py') |
| 449 |
and not fn.startswith('bad')] |
| 450 |
tests.sort() |
| 451 |
else: |
| 452 |
|
| 453 |
|
| 454 |
tests = ["test/test_unpack_ex.py"] |
| 455 |
|
| 456 |
for module in tests: |
| 457 |
fn = os.path.join(stdlib, module) |
| 458 |
print 'compiling', fn |
| 459 |
with open(fn, "r") as fp: |
| 460 |
source = fp.read() |
| 461 |
ec = self.space.getexecutioncontext() |
| 462 |
ast_node = ec.compiler.compile_to_ast(source, fn, "exec", 0) |
| 463 |
ec.compiler.validate_ast(ast_node) |
| 464 |
ast_node.to_object(self.space) |
| 465 |
|
| 466 |
def _check_wrong_pattern_error(self, pattern, msg=''): |
| 467 |
name_x = self.Name('x', ast.Load) |
| 468 |
stmt = self.Match( |
| 469 |
name_x, |
| 470 |
[ |
| 471 |
ast.match_case( |
| 472 |
pattern, |
| 473 |
None, |
| 474 |
[self.Pass()] |
| 475 |
) |
| 476 |
] |
| 477 |
) |
| 478 |
self.mod(stmt) |
| 479 |
|
| 480 |
def test_match_wrong_singleton(self): |
| 481 |
self._check_wrong_pattern_error(self.MatchSingleton("abc")) |
| 482 |
|
| 483 |
def test_match_value_store(self): |
| 484 |
self._check_wrong_pattern_error( |
| 485 |
self.MatchValue( |
| 486 |
self.Attribute( |
| 487 |
self.Attribute( |
| 488 |
self.Name('x', ast.Store), |
| 489 |
'y', ast.Load |
| 490 |
), |
| 491 |
'z', ast.Load |
| 492 |
) |
| 493 |
) |
| 494 |
) |
| 495 |
|
| 496 |
def test_match_sequence(self): |
| 497 |
self._check_wrong_pattern_error( |
| 498 |
self.MatchSequence([self.MatchStar('a'), self.MatchStar('d')])) |
| 499 |
self._check_wrong_pattern_error( |
| 500 |
self.MatchSequence([self.MatchStar('_')])) |
| 501 |
|
| 502 |
def test_match_mapping(self): |
| 503 |
self._check_wrong_pattern_error( |
| 504 |
self.MatchMapping(["a", "b"], [self.MatchSingleton(self.space.w_None)], None)) |
| 505 |
self._check_wrong_pattern_error( |
| 506 |
self.MatchMapping([], [], "_")) |
| 507 |
|
| 508 |
def test_match_as(self): |
| 509 |
self._check_wrong_pattern_error( |
| 510 |
self.MatchAs(None, "_")) |
| 511 |
self._check_wrong_pattern_error( |
| 512 |
self.MatchAs(self.MatchSingleton(self.space.w_None), None)) |
| 513 |
|
| 514 |
def test_match_or(self): |
| 515 |
p1 = self.MatchSingleton(self.space.w_None) |
| 516 |
self._check_wrong_pattern_error( |
| 517 |
self.MatchOr([p1])) |
| 518 |
|
| 519 |
def test_match_class(self): |
| 520 |
self._check_wrong_pattern_error(self.MatchClass( |
| 521 |
self.Name('A', ast.Store), |
| 522 |
[], [], [] |
| 523 |
)) |
| 524 |
self._check_wrong_pattern_error(self.MatchClass( |
| 525 |
self.Name('A', ast.Load), |
| 526 |
[], |
| 527 |
['a'], |
| 528 |
[] |
| 529 |
)) |
| 530 |
self._check_wrong_pattern_error(self.MatchClass( |
| 531 |
self.Attribute(self.Constant(3), "a", ast.Load), |
| 532 |
[], [], [] |
| 533 |
)) |
| 534 |
self._check_wrong_pattern_error(self.MatchClass( |
| 535 |
self.Name('A', ast.Load), |
| 536 |
[], |
| 537 |
['True'], |
| 538 |
[self.MatchSingleton(self.space.w_None)] |
| 539 |
)) |
| 540 |
|
| 541 |
def test_match_complicated(self): |
| 542 |
p1 = self.MatchMapping( |
| 543 |
[ |
| 544 |
self.Constant(True), |
| 545 |
self.Starred( |
| 546 |
self.Name('lol', ast.Load), |
| 547 |
ast.Load)], |
| 548 |
[ |
| 549 |
self.MatchValue( |
| 550 |
self.Constant('x')), |
| 551 |
self.MatchValue( |
| 552 |
self.Constant(1))], |
| 553 |
'legit') |
| 554 |
self._check_wrong_pattern_error(p1) |
| 555 |
|
| 556 |
def test_match_ellipsis(self): |
| 557 |
self._check_wrong_pattern_error(self.MatchValue(self.Constant(self.space.w_Ellipsis))) |
| 558 |
|
| 559 |
def test_match_true(self): |
| 560 |
self._check_wrong_pattern_error(self.MatchValue(self.Constant(self.space.w_True))) |
| 561 |
|
| 562 |
def test_match_tuple(self): |
| 563 |
self._check_wrong_pattern_error(self.MatchValue(self.Constant(self.space.newtuple([])))) |