Changeset 391 for python/trunk/Lib/test/test_parser.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_parser.py
r2 r391 1 1 import parser 2 import os3 2 import unittest 4 3 import sys 5 from test import test_support 4 import struct 5 from test import test_support as support 6 from test.script_helper import assert_python_failure 6 7 7 8 # … … 21 22 self.fail("could not roundtrip %r: %s" % (s, why)) 22 23 23 self.assertEqual s(t, st2.totuple(),24 24 self.assertEqual(t, st2.totuple(), 25 "could not re-generate syntax tree") 25 26 26 27 def check_expr(self, s): … … 34 35 scope = {} 35 36 exec code in scope 36 self.assert True(isinstance(scope["x"], unicode))37 self.assertIsInstance(scope["x"], unicode) 37 38 38 39 def check_suite(self, s): … … 60 61 def test_expressions(self): 61 62 self.check_expr("foo(1)") 63 self.check_expr("{1:1}") 64 self.check_expr("{1:1, 2:2, 3:3}") 65 self.check_expr("{1:1, 2:2, 3:3,}") 66 self.check_expr("{1}") 67 self.check_expr("{1, 2, 3}") 68 self.check_expr("{1, 2, 3,}") 69 self.check_expr("[]") 70 self.check_expr("[1]") 62 71 self.check_expr("[1, 2, 3]") 72 self.check_expr("[1, 2, 3,]") 73 self.check_expr("()") 74 self.check_expr("(1,)") 75 self.check_expr("(1, 2, 3)") 76 self.check_expr("(1, 2, 3,)") 63 77 self.check_expr("[x**3 for x in range(20)]") 64 78 self.check_expr("[x**3 for x in range(20) if x % 3]") 65 79 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]") 80 self.check_expr("[x+y for x in range(30) for y in range(20) if x % 2 if y % 3]") 81 #self.check_expr("[x for x in lambda: True, lambda: False if x()]") 66 82 self.check_expr("list(x**3 for x in range(20))") 67 83 self.check_expr("list(x**3 for x in range(20) if x % 3)") 68 84 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)") 85 self.check_expr("list(x+y for x in range(30) for y in range(20) if x % 2 if y % 3)") 86 self.check_expr("{x**3 for x in range(30)}") 87 self.check_expr("{x**3 for x in range(30) if x % 3}") 88 self.check_expr("{x**3 for x in range(30) if x % 2 if x % 3}") 89 self.check_expr("{x+y for x in range(30) for y in range(20) if x % 2 if y % 3}") 90 self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30))}") 91 self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30)) if x % 3}") 92 self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30)) if x % 3 if y % 3}") 93 self.check_expr("{x:y for x in range(30) for y in range(20) if x % 2 if y % 3}") 69 94 self.check_expr("foo(*args)") 70 95 self.check_expr("foo(*args, **kw)") … … 95 120 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0") 96 121 self.check_expr("lambda x, *y, **z: 0") 122 self.check_expr("lambda x: 5 if x else 2") 97 123 self.check_expr("(x for x in range(10))") 98 124 self.check_expr("foo(x for x in range(10))") … … 157 183 def test_class_defs(self): 158 184 self.check_suite("class foo():pass") 185 self.check_suite("@class_decorator\n" 186 "class foo():pass") 187 self.check_suite("@class_decorator(arg)\n" 188 "class foo():pass") 189 self.check_suite("@decorator1\n" 190 "@decorator2\n" 191 "class foo():pass") 192 159 193 160 194 def test_import_from_statement(self): … … 190 224 self.check_suite("import sys, math as my_math") 191 225 226 def test_relative_imports(self): 227 self.check_suite("from . import name") 228 self.check_suite("from .. import name") 229 self.check_suite("from .pkg import name") 230 self.check_suite("from ..pkg import name") 231 192 232 def test_pep263(self): 193 233 self.check_suite("# -*- coding: iso-8859-1 -*-\n" … … 200 240 self.check_suite("with open('x'): pass\n") 201 241 self.check_suite("with open('x') as f: pass\n") 242 self.check_suite("with open('x') as f, open('y') as g: pass\n") 202 243 203 244 def test_try_stmt(self): … … 211 252 "finally: pass\n") 212 253 254 def test_except_clause(self): 255 self.check_suite("try: pass\nexcept: pass\n") 256 self.check_suite("try: pass\nexcept A: pass\n") 257 self.check_suite("try: pass\nexcept A, e: pass\n") 258 self.check_suite("try: pass\nexcept A as e: pass\n") 259 213 260 def test_position(self): 214 261 # An absolutely minimal test of position information. Better 215 262 # tests would be a big project. 216 code = "def f(x):\n return x + 1 \n"263 code = "def f(x):\n return x + 1" 217 264 st1 = parser.suite(code) 218 265 st2 = st1.totuple(line_info=1, col_info=1) … … 481 528 self.check_bad_tree(tree, "malformed global ast") 482 529 530 def test_missing_import_source(self): 531 # from import a 532 tree = \ 533 (257, 534 (267, 535 (268, 536 (269, 537 (281, 538 (283, (1, 'from'), (1, 'import'), 539 (286, (284, (1, 'fred')))))), 540 (4, ''))), 541 (4, ''), (0, '')) 542 self.check_bad_tree(tree, "from import a") 543 483 544 484 545 class CompileTestCase(unittest.TestCase): … … 489 550 st = parser.expr('2 + 3') 490 551 code = parser.compilest(st) 491 self.assertEqual s(eval(code), 5)552 self.assertEqual(eval(code), 5) 492 553 493 554 def test_compile_suite(self): … … 496 557 globs = {} 497 558 exec code in globs 498 self.assertEqual s(globs['y'], 5)559 self.assertEqual(globs['y'], 5) 499 560 500 561 def test_compile_error(self): … … 508 569 self.assertRaises(SyntaxError, parser.compilest, st) 509 570 571 def test_issue_9011(self): 572 # Issue 9011: compilation of an unary minus expression changed 573 # the meaning of the ST, so that a second compilation produced 574 # incorrect results. 575 st = parser.expr('-3') 576 code1 = parser.compilest(st) 577 self.assertEqual(eval(code1), -3) 578 code2 = parser.compilest(st) 579 self.assertEqual(eval(code2), -3) 580 581 510 582 class ParserStackLimitTestCase(unittest.TestCase): 511 """try to push the parser to/over it 's limits.583 """try to push the parser to/over its limits. 512 584 see http://bugs.python.org/issue1881 for a discussion 513 585 """ … … 522 594 def test_trigger_memory_error(self): 523 595 e = self._nested_expression(100) 524 print >>sys.stderr, "Expecting 's_push: parser stack overflow' in next line" 525 self.assertRaises(MemoryError, parser.expr, e) 596 rc, out, err = assert_python_failure('-c', e) 597 # parsing the expression will result in an error message 598 # followed by a MemoryError (see #11963) 599 self.assertIn(b's_push: parser stack overflow', err) 600 self.assertIn(b'MemoryError', err) 601 602 class STObjectTestCase(unittest.TestCase): 603 """Test operations on ST objects themselves""" 604 605 check_sizeof = support.check_sizeof 606 607 @support.cpython_only 608 def test_sizeof(self): 609 def XXXROUNDUP(n): 610 if n <= 1: 611 return n 612 if n <= 128: 613 return (n + 3) & ~3 614 return 1 << (n - 1).bit_length() 615 616 basesize = support.calcobjsize('Pii') 617 nodesize = struct.calcsize('hP3iP0h') 618 def sizeofchildren(node): 619 if node is None: 620 return 0 621 res = 0 622 hasstr = len(node) > 1 and isinstance(node[-1], str) 623 if hasstr: 624 res += len(node[-1]) + 1 625 children = node[1:-1] if hasstr else node[1:] 626 if children: 627 res += XXXROUNDUP(len(children)) * nodesize 628 for child in children: 629 res += sizeofchildren(child) 630 return res 631 632 def check_st_sizeof(st): 633 self.check_sizeof(st, basesize + nodesize + 634 sizeofchildren(st.totuple())) 635 636 check_st_sizeof(parser.expr('2 + 3')) 637 check_st_sizeof(parser.expr('2 + 3 + 4')) 638 check_st_sizeof(parser.suite('x = 2 + 3')) 639 check_st_sizeof(parser.suite('')) 640 check_st_sizeof(parser.suite('# -*- coding: utf-8 -*-')) 641 check_st_sizeof(parser.expr('[' + '2,' * 1000 + ']')) 642 643 644 # XXX tests for pickling and unpickling of ST objects should go here 526 645 527 646 def test_main(): 528 test_support.run_unittest(647 support.run_unittest( 529 648 RoundtripLegalSyntaxTestCase, 530 649 IllegalSyntaxTestCase, 531 650 CompileTestCase, 532 651 ParserStackLimitTestCase, 652 STObjectTestCase, 533 653 ) 534 654
Note:
See TracChangeset
for help on using the changeset viewer.