python-2.5.2/win32/Lib/test/test_compiler.py
changeset 0 ae805ac0140d
equal deleted inserted replaced
-1:000000000000 0:ae805ac0140d
       
     1 import compiler
       
     2 from compiler.ast import flatten
       
     3 import os, sys, time, unittest
       
     4 import test.test_support
       
     5 from random import random
       
     6 
       
     7 # How much time in seconds can pass before we print a 'Still working' message.
       
     8 _PRINT_WORKING_MSG_INTERVAL = 5 * 60
       
     9 
       
    10 class TrivialContext(object):
       
    11     def __enter__(self):
       
    12         return self
       
    13     def __exit__(self, *exc_info):
       
    14         pass
       
    15 
       
    16 class CompilerTest(unittest.TestCase):
       
    17 
       
    18     def testCompileLibrary(self):
       
    19         # A simple but large test.  Compile all the code in the
       
    20         # standard library and its test suite.  This doesn't verify
       
    21         # that any of the code is correct, merely the compiler is able
       
    22         # to generate some kind of code for it.
       
    23 
       
    24         next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
       
    25         libdir = os.path.dirname(unittest.__file__)
       
    26         testdir = os.path.dirname(test.test_support.__file__)
       
    27 
       
    28         for dir in [libdir, testdir]:
       
    29             for basename in os.listdir(dir):
       
    30                 # Print still working message since this test can be really slow
       
    31                 if next_time <= time.time():
       
    32                     next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
       
    33                     print >>sys.__stdout__, \
       
    34                        '  testCompileLibrary still working, be patient...'
       
    35                     sys.__stdout__.flush()
       
    36 
       
    37                 if not basename.endswith(".py"):
       
    38                     continue
       
    39                 if not TEST_ALL and random() < 0.98:
       
    40                     continue
       
    41                 path = os.path.join(dir, basename)
       
    42                 if test.test_support.verbose:
       
    43                     print "compiling", path
       
    44                 f = open(path, "U")
       
    45                 buf = f.read()
       
    46                 f.close()
       
    47                 if "badsyntax" in basename or "bad_coding" in basename:
       
    48                     self.assertRaises(SyntaxError, compiler.compile,
       
    49                                       buf, basename, "exec")
       
    50                 else:
       
    51                     try:
       
    52                         compiler.compile(buf, basename, "exec")
       
    53                     except Exception, e:
       
    54                         args = list(e.args)
       
    55                         args[0] += "[in file %s]" % basename
       
    56                         e.args = tuple(args)
       
    57                         raise
       
    58 
       
    59     def testNewClassSyntax(self):
       
    60         compiler.compile("class foo():pass\n\n","<string>","exec")
       
    61 
       
    62     def testYieldExpr(self):
       
    63         compiler.compile("def g(): yield\n\n", "<string>", "exec")
       
    64 
       
    65     def testTryExceptFinally(self):
       
    66         # Test that except and finally clauses in one try stmt are recognized
       
    67         c = compiler.compile("try:\n 1/0\nexcept:\n e = 1\nfinally:\n f = 1",
       
    68                              "<string>", "exec")
       
    69         dct = {}
       
    70         exec c in dct
       
    71         self.assertEquals(dct.get('e'), 1)
       
    72         self.assertEquals(dct.get('f'), 1)
       
    73 
       
    74     def testDefaultArgs(self):
       
    75         self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass")
       
    76 
       
    77     def testDocstrings(self):
       
    78         c = compiler.compile('"doc"', '<string>', 'exec')
       
    79         self.assert_('__doc__' in c.co_names)
       
    80         c = compiler.compile('def f():\n "doc"', '<string>', 'exec')
       
    81         g = {}
       
    82         exec c in g
       
    83         self.assertEquals(g['f'].__doc__, "doc")
       
    84 
       
    85     def testLineNo(self):
       
    86         # Test that all nodes except Module have a correct lineno attribute.
       
    87         filename = __file__
       
    88         if filename.endswith((".pyc", ".pyo")):
       
    89             filename = filename[:-1]
       
    90         tree = compiler.parseFile(filename)
       
    91         self.check_lineno(tree)
       
    92 
       
    93     def check_lineno(self, node):
       
    94         try:
       
    95             self._check_lineno(node)
       
    96         except AssertionError:
       
    97             print node.__class__, node.lineno
       
    98             raise
       
    99 
       
   100     def _check_lineno(self, node):
       
   101         if not node.__class__ in NOLINENO:
       
   102             self.assert_(isinstance(node.lineno, int),
       
   103                 "lineno=%s on %s" % (node.lineno, node.__class__))
       
   104             self.assert_(node.lineno > 0,
       
   105                 "lineno=%s on %s" % (node.lineno, node.__class__))
       
   106         for child in node.getChildNodes():
       
   107             self.check_lineno(child)
       
   108 
       
   109     def testFlatten(self):
       
   110         self.assertEquals(flatten([1, [2]]), [1, 2])
       
   111         self.assertEquals(flatten((1, (2,))), [1, 2])
       
   112 
       
   113     def testNestedScope(self):
       
   114         c = compiler.compile('def g():\n'
       
   115                              '    a = 1\n'
       
   116                              '    def f(): return a + 2\n'
       
   117                              '    return f()\n'
       
   118                              'result = g()',
       
   119                              '<string>',
       
   120                              'exec')
       
   121         dct = {}
       
   122         exec c in dct
       
   123         self.assertEquals(dct.get('result'), 3)
       
   124 
       
   125     def testGenExp(self):
       
   126         c = compiler.compile('list((i,j) for i in range(3) if i < 3'
       
   127                              '           for j in range(4) if j > 2)',
       
   128                              '<string>',
       
   129                              'eval')
       
   130         self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)])
       
   131 
       
   132     def testWith(self):
       
   133         # SF bug 1638243
       
   134         c = compiler.compile('from __future__ import with_statement\n'
       
   135                              'def f():\n'
       
   136                              '    with TrivialContext():\n'
       
   137                              '        return 1\n'
       
   138                              'result = f()',
       
   139                              '<string>',
       
   140                              'exec' )
       
   141         dct = {'TrivialContext': TrivialContext}
       
   142         exec c in dct
       
   143         self.assertEquals(dct.get('result'), 1)
       
   144 
       
   145     def testWithAss(self):
       
   146         c = compiler.compile('from __future__ import with_statement\n'
       
   147                              'def f():\n'
       
   148                              '    with TrivialContext() as tc:\n'
       
   149                              '        return 1\n'
       
   150                              'result = f()',
       
   151                              '<string>',
       
   152                              'exec' )
       
   153         dct = {'TrivialContext': TrivialContext}
       
   154         exec c in dct
       
   155         self.assertEquals(dct.get('result'), 1)
       
   156 
       
   157 
       
   158     def _testErrEnc(self, src, text, offset):
       
   159         try:
       
   160             compile(src, "", "exec")
       
   161         except SyntaxError, e:
       
   162             self.assertEquals(e.offset, offset)
       
   163             self.assertEquals(e.text, text)
       
   164 
       
   165     def testSourceCodeEncodingsError(self):
       
   166         # Test SyntaxError with encoding definition
       
   167         sjis = "print '\x83\x70\x83\x43\x83\x5c\x83\x93', '\n"
       
   168         ascii = "print '12345678', '\n"
       
   169         encdef = "#! -*- coding: ShiftJIS -*-\n"
       
   170 
       
   171         # ascii source without encdef
       
   172         self._testErrEnc(ascii, ascii, 19)
       
   173 
       
   174         # ascii source with encdef
       
   175         self._testErrEnc(encdef+ascii, ascii, 19)
       
   176 
       
   177         # non-ascii source with encdef
       
   178         self._testErrEnc(encdef+sjis, sjis, 19)
       
   179 
       
   180         # ShiftJIS source without encdef
       
   181         self._testErrEnc(sjis, sjis, 19)
       
   182 
       
   183 
       
   184 NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
       
   185 
       
   186 ###############################################################################
       
   187 # code below is just used to trigger some possible errors, for the benefit of
       
   188 # testLineNo
       
   189 ###############################################################################
       
   190 
       
   191 class Toto:
       
   192     """docstring"""
       
   193     pass
       
   194 
       
   195 a, b = 2, 3
       
   196 [c, d] = 5, 6
       
   197 l = [(x, y) for x, y in zip(range(5), range(5,10))]
       
   198 l[0]
       
   199 l[3:4]
       
   200 d = {'a': 2}
       
   201 d = {}
       
   202 t = ()
       
   203 t = (1, 2)
       
   204 l = []
       
   205 l = [1, 2]
       
   206 if l:
       
   207     pass
       
   208 else:
       
   209     a, b = b, a
       
   210 
       
   211 try:
       
   212     print yo
       
   213 except:
       
   214     yo = 3
       
   215 else:
       
   216     yo += 3
       
   217 
       
   218 try:
       
   219     a += b
       
   220 finally:
       
   221     b = 0
       
   222 
       
   223 from math import *
       
   224 
       
   225 ###############################################################################
       
   226 
       
   227 def test_main():
       
   228     global TEST_ALL
       
   229     TEST_ALL = test.test_support.is_resource_enabled("compiler")
       
   230     test.test_support.run_unittest(CompilerTest)
       
   231 
       
   232 if __name__ == "__main__":
       
   233     test_main()