Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_parser.py

    r2 r391  
    11import parser
    2 import os
    32import unittest
    43import sys
    5 from test import test_support
     4import struct
     5from test import test_support as support
     6from test.script_helper import assert_python_failure
    67
    78#
     
    2122            self.fail("could not roundtrip %r: %s" % (s, why))
    2223
    23         self.assertEquals(t, st2.totuple(),
    24                           "could not re-generate syntax tree")
     24        self.assertEqual(t, st2.totuple(),
     25                         "could not re-generate syntax tree")
    2526
    2627    def check_expr(self, s):
     
    3435        scope = {}
    3536        exec code in scope
    36         self.assertTrue(isinstance(scope["x"], unicode))
     37        self.assertIsInstance(scope["x"], unicode)
    3738
    3839    def check_suite(self, s):
     
    6061    def test_expressions(self):
    6162        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]")
    6271        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,)")
    6377        self.check_expr("[x**3 for x in range(20)]")
    6478        self.check_expr("[x**3 for x in range(20) if x % 3]")
    6579        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()]")
    6682        self.check_expr("list(x**3 for x in range(20))")
    6783        self.check_expr("list(x**3 for x in range(20) if x % 3)")
    6884        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}")
    6994        self.check_expr("foo(*args)")
    7095        self.check_expr("foo(*args, **kw)")
     
    95120        self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
    96121        self.check_expr("lambda x, *y, **z: 0")
     122        self.check_expr("lambda x: 5 if x else 2")
    97123        self.check_expr("(x for x in range(10))")
    98124        self.check_expr("foo(x for x in range(10))")
     
    157183    def test_class_defs(self):
    158184        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
    159193
    160194    def test_import_from_statement(self):
     
    190224        self.check_suite("import sys, math as my_math")
    191225
     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
    192232    def test_pep263(self):
    193233        self.check_suite("# -*- coding: iso-8859-1 -*-\n"
     
    200240        self.check_suite("with open('x'): pass\n")
    201241        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")
    202243
    203244    def test_try_stmt(self):
     
    211252                         "finally: pass\n")
    212253
     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
    213260    def test_position(self):
    214261        # An absolutely minimal test of position information.  Better
    215262        # 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"
    217264        st1 = parser.suite(code)
    218265        st2 = st1.totuple(line_info=1, col_info=1)
     
    481528        self.check_bad_tree(tree, "malformed global ast")
    482529
     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
    483544
    484545class CompileTestCase(unittest.TestCase):
     
    489550        st = parser.expr('2 + 3')
    490551        code = parser.compilest(st)
    491         self.assertEquals(eval(code), 5)
     552        self.assertEqual(eval(code), 5)
    492553
    493554    def test_compile_suite(self):
     
    496557        globs = {}
    497558        exec code in globs
    498         self.assertEquals(globs['y'], 5)
     559        self.assertEqual(globs['y'], 5)
    499560
    500561    def test_compile_error(self):
     
    508569        self.assertRaises(SyntaxError, parser.compilest, st)
    509570
     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
    510582class ParserStackLimitTestCase(unittest.TestCase):
    511     """try to push the parser to/over it's limits.
     583    """try to push the parser to/over its limits.
    512584    see http://bugs.python.org/issue1881 for a discussion
    513585    """
     
    522594    def test_trigger_memory_error(self):
    523595        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
     602class 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
    526645
    527646def test_main():
    528     test_support.run_unittest(
     647    support.run_unittest(
    529648        RoundtripLegalSyntaxTestCase,
    530649        IllegalSyntaxTestCase,
    531650        CompileTestCase,
    532651        ParserStackLimitTestCase,
     652        STObjectTestCase,
    533653    )
    534654
Note: See TracChangeset for help on using the changeset viewer.