Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(79858)

Delta Between Two Patch Sets: Lib/test/test_ast.py

Issue 11682: PEP 380 reference implementation for 3.3
Left Patch Set: Created 7 years, 8 months ago
Right Patch Set: Created 7 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 import os 1 import os
2 import sys 2 import sys
3 import unittest 3 import unittest
4 from test import support 4 from test import support
5 import ast 5 import ast
6 6
7 def to_tuple(t): 7 def to_tuple(t):
8 if t is None or isinstance(t, (str, int, complex)): 8 if t is None or isinstance(t, (str, int, complex)):
9 return t 9 return t
10 elif isinstance(t, list): 10 elif isinstance(t, list):
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 288
289 self.assertRaises(TypeError, ast.Num, 1, 2) 289 self.assertRaises(TypeError, ast.Num, 1, 2)
290 self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0) 290 self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0)
291 291
292 def test_module(self): 292 def test_module(self):
293 body = [ast.Num(42)] 293 body = [ast.Num(42)]
294 x = ast.Module(body) 294 x = ast.Module(body)
295 self.assertEqual(x.body, body) 295 self.assertEqual(x.body, body)
296 296
297 def test_nodeclasses(self): 297 def test_nodeclasses(self):
298 # Zero arguments constructor explicitely allowed 298 # Zero arguments constructor explicitly allowed
299 x = ast.BinOp() 299 x = ast.BinOp()
300 self.assertEqual(x._fields, ('left', 'op', 'right')) 300 self.assertEqual(x._fields, ('left', 'op', 'right'))
301 301
302 # Random attribute allowed too 302 # Random attribute allowed too
303 x.foobarbaz = 5 303 x.foobarbaz = 5
304 self.assertEqual(x.foobarbaz, 5) 304 self.assertEqual(x.foobarbaz, 5)
305 305
306 n1 = ast.Num(1) 306 n1 = ast.Num(1)
307 n3 = ast.Num(3) 307 n3 = ast.Num(3)
308 addop = ast.Add() 308 addop = ast.Add()
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 self.assertEqual(ast.literal_eval('b"hi"'), b"hi") 483 self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
484 self.assertRaises(ValueError, ast.literal_eval, 'foo()') 484 self.assertRaises(ValueError, ast.literal_eval, 'foo()')
485 self.assertEqual(ast.literal_eval('-6'), -6) 485 self.assertEqual(ast.literal_eval('-6'), -6)
486 self.assertEqual(ast.literal_eval('-6j+3'), 3-6j) 486 self.assertEqual(ast.literal_eval('-6j+3'), 3-6j)
487 self.assertEqual(ast.literal_eval('3.25'), 3.25) 487 self.assertEqual(ast.literal_eval('3.25'), 3.25)
488 488
489 def test_literal_eval_issue4907(self): 489 def test_literal_eval_issue4907(self):
490 self.assertEqual(ast.literal_eval('2j'), 2j) 490 self.assertEqual(ast.literal_eval('2j'), 2j)
491 self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j) 491 self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
492 self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j) 492 self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
493
494 def test_bad_integer(self):
495 # issue13436: Bad error message with invalid numeric values
496 body = [ast.ImportFrom(module='time',
497 names=[ast.alias(name='sleep')],
498 level=None,
499 lineno=None, col_offset=None)]
500 mod = ast.Module(body)
501 with self.assertRaises(ValueError) as cm:
502 compile(mod, 'test', 'exec')
503 self.assertIn("invalid integer value: None", str(cm.exception))
493 504
494 505
495 class ASTValidatorTests(unittest.TestCase): 506 class ASTValidatorTests(unittest.TestCase):
496 507
497 def mod(self, mod, msg=None, mode="exec", *, exc=ValueError): 508 def mod(self, mod, msg=None, mode="exec", *, exc=ValueError):
498 mod.lineno = mod.col_offset = 0 509 mod.lineno = mod.col_offset = 0
499 ast.fix_missing_locations(mod) 510 ast.fix_missing_locations(mod)
500 with self.assertRaises(exc) as cm: 511 with self.assertRaises(exc) as cm:
501 compile(mod, "<test>", mode) 512 compile(mod, "<test>", mode)
502 if msg is not None: 513 if msg is not None:
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after
978 ('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Lo ad',))), 989 ('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Lo ad',))),
979 ('Expression', ('Name', (1, 0), 'v', ('Load',))), 990 ('Expression', ('Name', (1, 0), 'v', ('Load',))),
980 ('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))), 991 ('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
981 ('Expression', ('List', (1, 0), [], ('Load',))), 992 ('Expression', ('List', (1, 0), [], ('Load',))),
982 ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num' , (1, 4), 3)], ('Load',))), 993 ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num' , (1, 4), 3)], ('Load',))),
983 ('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num' , (1, 5), 3)], ('Load',))), 994 ('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num' , (1, 5), 3)], ('Load',))),
984 ('Expression', ('Tuple', (1, 0), [], ('Load',))), 995 ('Expression', ('Tuple', (1, 0), [], ('Load',))),
985 ('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('At tribute', (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)), 996 ('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('At tribute', (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)),
986 ] 997 ]
987 main() 998 main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+