2 from compiler
.ast
import flatten
3 import os
, sys
, time
, unittest
4 import test
.test_support
5 from random
import random
7 # How much time in seconds can pass before we print a 'Still working' message.
8 _PRINT_WORKING_MSG_INTERVAL
= 5 * 60
10 class CompilerTest(unittest
.TestCase
):
12 def testCompileLibrary(self
):
13 # A simple but large test. Compile all the code in the
14 # standard library and its test suite. This doesn't verify
15 # that any of the code is correct, merely the compiler is able
16 # to generate some kind of code for it.
18 next_time
= time
.time() + _PRINT_WORKING_MSG_INTERVAL
19 libdir
= os
.path
.dirname(unittest
.__file
__)
20 testdir
= os
.path
.dirname(test
.test_support
.__file
__)
22 for dir in [libdir
, testdir
]:
23 for basename
in os
.listdir(dir):
24 # Print still working message since this test can be really slow
25 if next_time
<= time
.time():
26 next_time
= time
.time() + _PRINT_WORKING_MSG_INTERVAL
27 print >>sys
.__stdout
__, \
28 ' testCompileLibrary still working, be patient...'
29 sys
.__stdout
__.flush()
31 if not basename
.endswith(".py"):
33 if not TEST_ALL
and random() < 0.98:
35 path
= os
.path
.join(dir, basename
)
36 if test
.test_support
.verbose
:
37 print "compiling", path
41 if "badsyntax" in basename
or "bad_coding" in basename
:
42 self
.assertRaises(SyntaxError, compiler
.compile,
43 buf
, basename
, "exec")
46 compiler
.compile(buf
, basename
, "exec")
49 args
[0] += "[in file %s]" % basename
53 def testNewClassSyntax(self
):
54 compiler
.compile("class foo():pass\n\n","<string>","exec")
56 def testYieldExpr(self
):
57 compiler
.compile("def g(): yield\n\n", "<string>", "exec")
59 def testTryExceptFinally(self
):
60 # Test that except and finally clauses in one try stmt are recognized
61 c
= compiler
.compile("try:\n 1/0\nexcept:\n e = 1\nfinally:\n f = 1",
65 self
.assertEquals(dct
.get('e'), 1)
66 self
.assertEquals(dct
.get('f'), 1)
68 def testDefaultArgs(self
):
69 self
.assertRaises(SyntaxError, compiler
.parse
, "def foo(a=1, b): pass")
71 def testDocstrings(self
):
72 c
= compiler
.compile('"doc"', '<string>', 'exec')
73 self
.assert_('__doc__' in c
.co_names
)
74 c
= compiler
.compile('def f():\n "doc"', '<string>', 'exec')
77 self
.assertEquals(g
['f'].__doc
__, "doc")
80 # Test that all nodes except Module have a correct lineno attribute.
82 if filename
.endswith((".pyc", ".pyo")):
83 filename
= filename
[:-1]
84 tree
= compiler
.parseFile(filename
)
85 self
.check_lineno(tree
)
87 def check_lineno(self
, node
):
89 self
._check
_lineno
(node
)
90 except AssertionError:
91 print node
.__class
__, node
.lineno
94 def _check_lineno(self
, node
):
95 if not node
.__class
__ in NOLINENO
:
96 self
.assert_(isinstance(node
.lineno
, int),
97 "lineno=%s on %s" % (node
.lineno
, node
.__class
__))
98 self
.assert_(node
.lineno
> 0,
99 "lineno=%s on %s" % (node
.lineno
, node
.__class
__))
100 for child
in node
.getChildNodes():
101 self
.check_lineno(child
)
103 def testFlatten(self
):
104 self
.assertEquals(flatten([1, [2]]), [1, 2])
105 self
.assertEquals(flatten((1, (2,))), [1, 2])
107 def testNestedScope(self
):
108 c
= compiler
.compile('def g():\n'
110 ' def f(): return a + 2\n'
117 self
.assertEquals(dct
.get('result'), 3)
119 def testGenExp(self
):
120 c
= compiler
.compile('list((i,j) for i in range(3) if i < 3'
121 ' for j in range(4) if j > 2)',
124 self
.assertEquals(eval(c
), [(0, 3), (1, 3), (2, 3)])
127 NOLINENO
= (compiler
.ast
.Module
, compiler
.ast
.Stmt
, compiler
.ast
.Discard
)
129 ###############################################################################
130 # code below is just used to trigger some possible errors, for the benefit of
132 ###############################################################################
140 l
= [(x
, y
) for x
, y
in zip(range(5), range(5,10))]
168 ###############################################################################
172 TEST_ALL
= test
.test_support
.is_resource_enabled("compiler")
173 test
.test_support
.run_unittest(CompilerTest
)
175 if __name__
== "__main__":