Changeset 391 for python/trunk/Lib/test/test_ast.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_ast.py
r2 r391 19 19 20 20 # These tests are compiled through "exec" 21 # There should be at least one test per statement21 # There should be at least one test per statement 22 22 exec_tests = [ 23 # None 24 "None", 23 25 # FunctionDef 24 26 "def f(): pass", 27 # FunctionDef with arg 28 "def f(a): pass", 29 # FunctionDef with arg and default value 30 "def f(a=0): pass", 31 # FunctionDef with varargs 32 "def f(*args): pass", 33 # FunctionDef with kwargs 34 "def f(**kwargs): pass", 35 # FunctionDef with all kind of args 36 "def f(a, b=1, c=None, d=[], e={}, *args, **kwargs): pass", 25 37 # ClassDef 26 38 "class C:pass", 39 # ClassDef, new style class 40 "class C(object): pass", 27 41 # Return 28 42 "def f():return 1", … … 65 79 # Continue 66 80 "continue", 81 # for statements with naked tuples (see http://bugs.python.org/issue6704) 82 "for a,b in c: pass", 83 "[(a,b) for a,b in c]", 84 "((a,b) for a,b in c)", 85 "((a,b) for (a,b) in c)", 86 # Multiline generator expression (test for .lineno & .col_offset) 87 """( 88 ( 89 Aa 90 , 91 Bb 92 ) 93 for 94 Aa 95 , 96 Bb in Cc 97 )""", 98 # dictcomp 99 "{a : b for w in x for m in p if g}", 100 # dictcomp with naked tuple 101 "{a : b for v,w in x}", 102 # setcomp 103 "{r for l in x if g}", 104 # setcomp with naked tuple 105 "{r for l,m in x}", 67 106 ] 68 107 … … 77 116 # It should test all expressions 78 117 eval_tests = [ 118 # None 119 "None", 79 120 # BoolOp 80 121 "a and b", … … 87 128 # Dict 88 129 "{ 1:2 }", 130 # Empty dict 131 "{}", 132 # Set 133 "{None,}", 134 # Multiline dict (test for .lineno & .col_offset) 135 """{ 136 1 137 : 138 2 139 }""", 89 140 # ListComp 90 141 "[a for b in c if d]", … … 111 162 # List 112 163 "[1,2,3]", 164 # Empty list 165 "[]", 113 166 # Tuple 114 167 "1,2,3", 168 # Tuple 169 "(1,2,3)", 170 # Empty tuple 171 "()", 115 172 # Combination 116 173 "a.b.c.d(a.b[1:2])", … … 123 180 class AST_Tests(unittest.TestCase): 124 181 125 def _assert _order(self, ast_node, parent_pos):182 def _assertTrueorder(self, ast_node, parent_pos): 126 183 if not isinstance(ast_node, ast.AST) or ast_node._fields is None: 127 184 return 128 185 if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)): 129 186 node_pos = (ast_node.lineno, ast_node.col_offset) 130 self.assert _(node_pos >= parent_pos)187 self.assertTrue(node_pos >= parent_pos) 131 188 parent_pos = (ast_node.lineno, ast_node.col_offset) 132 189 for name in ast_node._fields: … … 134 191 if isinstance(value, list): 135 192 for child in value: 136 self._assert _order(child, parent_pos)193 self._assertTrueorder(child, parent_pos) 137 194 elif value is not None: 138 self._assert_order(value, parent_pos) 195 self._assertTrueorder(value, parent_pos) 196 197 def test_AST_objects(self): 198 x = ast.AST() 199 self.assertEqual(x._fields, ()) 200 201 with self.assertRaises(AttributeError): 202 x.vararg 203 204 with self.assertRaises(AttributeError): 205 x.foobar = 21 206 207 with self.assertRaises(AttributeError): 208 ast.AST(lineno=2) 209 210 with self.assertRaises(TypeError): 211 # "_ast.AST constructor takes 0 positional arguments" 212 ast.AST(2) 139 213 140 214 def test_snippets(self): … … 144 218 for i, o in itertools.izip(input, output): 145 219 ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST) 146 self.assertEquals(to_tuple(ast_tree), o) 147 self._assert_order(ast_tree, (0, 0)) 220 self.assertEqual(to_tuple(ast_tree), o) 221 self._assertTrueorder(ast_tree, (0, 0)) 222 223 def test_slice(self): 224 slc = ast.parse("x[::]").body[0].value.slice 225 self.assertIsNone(slc.upper) 226 self.assertIsNone(slc.lower) 227 self.assertIsInstance(slc.step, ast.Name) 228 self.assertEqual(slc.step.id, "None") 229 230 def test_from_import(self): 231 im = ast.parse("from . import y").body[0] 232 self.assertIsNone(im.module) 233 234 def test_non_interned_future_from_ast(self): 235 mod = ast.parse("from __future__ import division") 236 self.assertIsInstance(mod.body[0], ast.ImportFrom) 237 mod.body[0].module = " __future__ ".strip() 238 compile(mod, "<test>", "exec") 239 240 def test_base_classes(self): 241 self.assertTrue(issubclass(ast.For, ast.stmt)) 242 self.assertTrue(issubclass(ast.Name, ast.expr)) 243 self.assertTrue(issubclass(ast.stmt, ast.AST)) 244 self.assertTrue(issubclass(ast.expr, ast.AST)) 245 self.assertTrue(issubclass(ast.comprehension, ast.AST)) 246 self.assertTrue(issubclass(ast.Gt, ast.AST)) 247 248 def test_field_attr_existence(self): 249 for name, item in ast.__dict__.iteritems(): 250 if isinstance(item, type) and name != 'AST' and name[0].isupper(): 251 x = item() 252 if isinstance(x, ast.AST): 253 self.assertEqual(type(x._fields), tuple) 254 255 def test_arguments(self): 256 x = ast.arguments() 257 self.assertEqual(x._fields, ('args', 'vararg', 'kwarg', 'defaults')) 258 259 with self.assertRaises(AttributeError): 260 x.vararg 261 262 x = ast.arguments(1, 2, 3, 4) 263 self.assertEqual(x.vararg, 2) 264 265 def test_field_attr_writable(self): 266 x = ast.Num() 267 # We can assign to _fields 268 x._fields = 666 269 self.assertEqual(x._fields, 666) 270 271 def test_classattrs(self): 272 x = ast.Num() 273 self.assertEqual(x._fields, ('n',)) 274 275 with self.assertRaises(AttributeError): 276 x.n 277 278 x = ast.Num(42) 279 self.assertEqual(x.n, 42) 280 281 with self.assertRaises(AttributeError): 282 x.lineno 283 284 with self.assertRaises(AttributeError): 285 x.foobar 286 287 x = ast.Num(lineno=2) 288 self.assertEqual(x.lineno, 2) 289 290 x = ast.Num(42, lineno=0) 291 self.assertEqual(x.lineno, 0) 292 self.assertEqual(x._fields, ('n',)) 293 self.assertEqual(x.n, 42) 294 295 self.assertRaises(TypeError, ast.Num, 1, 2) 296 self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0) 297 298 def test_module(self): 299 body = [ast.Num(42)] 300 x = ast.Module(body) 301 self.assertEqual(x.body, body) 148 302 149 303 def test_nodeclasses(self): 304 # Zero arguments constructor explicitely allowed 305 x = ast.BinOp() 306 self.assertEqual(x._fields, ('left', 'op', 'right')) 307 308 # Random attribute allowed too 309 x.foobarbaz = 5 310 self.assertEqual(x.foobarbaz, 5) 311 312 n1 = ast.Num(1) 313 n3 = ast.Num(3) 314 addop = ast.Add() 315 x = ast.BinOp(n1, addop, n3) 316 self.assertEqual(x.left, n1) 317 self.assertEqual(x.op, addop) 318 self.assertEqual(x.right, n3) 319 320 x = ast.BinOp(1, 2, 3) 321 self.assertEqual(x.left, 1) 322 self.assertEqual(x.op, 2) 323 self.assertEqual(x.right, 3) 324 150 325 x = ast.BinOp(1, 2, 3, lineno=0) 151 self.assertEqual s(x.left, 1)152 self.assertEqual s(x.op, 2)153 self.assertEqual s(x.right, 3)154 self.assertEqual s(x.lineno, 0)326 self.assertEqual(x.left, 1) 327 self.assertEqual(x.op, 2) 328 self.assertEqual(x.right, 3) 329 self.assertEqual(x.lineno, 0) 155 330 156 331 # node raises exception when not given enough arguments 157 332 self.assertRaises(TypeError, ast.BinOp, 1, 2) 333 # node raises exception when given too many arguments 334 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4) 335 # node raises exception when not given enough arguments 336 self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0) 337 # node raises exception when given too many arguments 338 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0) 158 339 159 340 # can set attributes through kwargs too 160 341 x = ast.BinOp(left=1, op=2, right=3, lineno=0) 161 self.assertEquals(x.left, 1) 162 self.assertEquals(x.op, 2) 163 self.assertEquals(x.right, 3) 164 self.assertEquals(x.lineno, 0) 165 342 self.assertEqual(x.left, 1) 343 self.assertEqual(x.op, 2) 344 self.assertEqual(x.right, 3) 345 self.assertEqual(x.lineno, 0) 346 347 # Random kwargs also allowed 348 x = ast.BinOp(1, 2, 3, foobarbaz=42) 349 self.assertEqual(x.foobarbaz, 42) 350 351 def test_no_fields(self): 166 352 # this used to fail because Sub._fields was None 167 353 x = ast.Sub() 354 self.assertEqual(x._fields, ()) 168 355 169 356 def test_pickling(self): … … 180 367 for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests): 181 368 ast2 = mod.loads(mod.dumps(ast, protocol)) 182 self.assertEquals(to_tuple(ast2), to_tuple(ast)) 369 self.assertEqual(to_tuple(ast2), to_tuple(ast)) 370 371 def test_invalid_identitifer(self): 372 m = ast.Module([ast.Expr(ast.Name(u"x", ast.Load()))]) 373 ast.fix_missing_locations(m) 374 with self.assertRaises(TypeError) as cm: 375 compile(m, "<test>", "exec") 376 self.assertIn("identifier must be of type str", str(cm.exception)) 377 378 def test_invalid_string(self): 379 m = ast.Module([ast.Expr(ast.Str(43))]) 380 ast.fix_missing_locations(m) 381 with self.assertRaises(TypeError) as cm: 382 compile(m, "<test>", "exec") 383 self.assertIn("string must be of type str or uni", str(cm.exception)) 183 384 184 385 … … 242 443 'col_offset=0))' 243 444 ) 445 # issue10869: do not increment lineno of root twice 446 src = ast.parse('1 + 1', mode='eval') 447 self.assertEqual(ast.increment_lineno(src.body, n=3), src.body) 448 self.assertEqual(ast.dump(src, include_attributes=True), 449 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' 450 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' 451 'col_offset=0))' 452 ) 244 453 245 454 def test_iter_fields(self): … … 272 481 self.assertRaises(ValueError, ast.literal_eval, 'foo()') 273 482 483 def test_literal_eval_issue4907(self): 484 self.assertEqual(ast.literal_eval('2j'), 2j) 485 self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j) 486 self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j) 487 self.assertRaises(ValueError, ast.literal_eval, '2 + (3 + 4j)') 488 489 274 490 def test_main(): 275 test_support.run_unittest(AST_Tests, ASTHelpers_Test) 491 with test_support.check_py3k_warnings(("backquote not supported", 492 SyntaxWarning)): 493 test_support.run_unittest(AST_Tests, ASTHelpers_Test) 276 494 277 495 def main(): … … 284 502 for s in statements: 285 503 print repr(to_tuple(compile(s, "?", kind, 0x400)))+"," 286 504 print "]" 287 505 print "main()" 288 506 raise SystemExit … … 291 509 #### EVERYTHING BELOW IS GENERATED ##### 292 510 exec_results = [ 511 ('Module', [('Expr', (1, 0), ('Name', (1, 0), 'None', ('Load',)))]), 293 512 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Pass', (1, 9))], [])]), 513 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',))], None, None, []), [('Pass', (1, 10))], [])]), 514 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',))], None, None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [])]), 515 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], 'args', None, []), [('Pass', (1, 14))], [])]), 516 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, 'kwargs', []), [('Pass', (1, 17))], [])]), 517 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',)), ('Name', (1, 9), 'b', ('Param',)), ('Name', (1, 14), 'c', ('Param',)), ('Name', (1, 22), 'd', ('Param',)), ('Name', (1, 28), 'e', ('Param',))], 'args', 'kwargs', [('Num', (1, 11), 1), ('Name', (1, 16), 'None', ('Load',)), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])]), [('Pass', (1, 52))], [])]), 294 518 ('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))], [])]), 519 ('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [('Pass', (1, 17))], [])]), 295 520 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [])]), 296 521 ('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]), … … 313 538 ('Module', [('Break', (1, 0))]), 314 539 ('Module', [('Continue', (1, 0))]), 540 ('Module', [('For', (1, 0), ('Tuple', (1, 4), [('Name', (1, 4), 'a', ('Store',)), ('Name', (1, 6), 'b', ('Store',))], ('Store',)), ('Name', (1, 11), 'c', ('Load',)), [('Pass', (1, 14))], [])]), 541 ('Module', [('Expr', (1, 0), ('ListComp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]), 542 ('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]), 543 ('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 12), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [])]))]), 544 ('Module', [('Expr', (1, 0), ('GeneratorExp', (2, 4), ('Tuple', (3, 4), [('Name', (3, 4), 'Aa', ('Load',)), ('Name', (5, 7), 'Bb', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (8, 4), [('Name', (8, 4), 'Aa', ('Store',)), ('Name', (10, 4), 'Bb', ('Store',))], ('Store',)), ('Name', (10, 10), 'Cc', ('Load',)), [])]))]), 545 ('Module', [('Expr', (1, 0), ('DictComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Name', (1, 11), 'w', ('Store',)), ('Name', (1, 16), 'x', ('Load',)), []), ('comprehension', ('Name', (1, 22), 'm', ('Store',)), ('Name', (1, 27), 'p', ('Load',)), [('Name', (1, 32), 'g', ('Load',))])]))]), 546 ('Module', [('Expr', (1, 0), ('DictComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'v', ('Store',)), ('Name', (1, 13), 'w', ('Store',))], ('Store',)), ('Name', (1, 18), 'x', ('Load',)), [])]))]), 547 ('Module', [('Expr', (1, 0), ('SetComp', (1, 1), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 12), 'x', ('Load',)), [('Name', (1, 17), 'g', ('Load',))])]))]), 548 ('Module', [('Expr', (1, 0), ('SetComp', (1, 1), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Tuple', (1, 7), [('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 9), 'm', ('Store',))], ('Store',)), ('Name', (1, 14), 'x', ('Load',)), [])]))]), 315 549 ] 316 550 single_results = [ … … 318 552 ] 319 553 eval_results = [ 554 ('Expression', ('Name', (1, 0), 'None', ('Load',))), 320 555 ('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])), 321 556 ('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))), … … 323 558 ('Expression', ('Lambda', (1, 0), ('arguments', [], None, None, []), ('Name', (1, 7), 'None', ('Load',)))), 324 559 ('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])), 560 ('Expression', ('Dict', (1, 0), [], [])), 561 ('Expression', ('Set', (1, 0), [('Name', (1, 1), 'None', ('Load',))])), 562 ('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])), 325 563 ('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])), 326 564 ('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])), … … 334 572 ('Expression', ('Name', (1, 0), 'v', ('Load',))), 335 573 ('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))), 574 ('Expression', ('List', (1, 0), [], ('Load',))), 336 575 ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))), 576 ('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))), 577 ('Expression', ('Tuple', (1, 0), [], ('Load',))), 337 578 ('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [], None, None)), 338 579 ]
Note:
See TracChangeset
for help on using the changeset viewer.