annotate Lib/test/test_compiler.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents a8f98a727371
children dba99828d91c 494da2c71902
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46818
be8c43a3fd62 Deprecate the compiler package for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 45901
diff changeset
1 import test.test_support
be8c43a3fd62 Deprecate the compiler package for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 45901
diff changeset
2 compiler = test.test_support.import_module('compiler', deprecated=True)
34989
8b05a4732cf3 Fix compiler.ast.flatten function so that it works on lists.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 34920
diff changeset
3 from compiler.ast import flatten
37663
a840e9baff96 test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36798
diff changeset
4 import os, sys, time, unittest
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33051
diff changeset
5 from random import random
45901
7f7770719e84 Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents: 45468
diff changeset
6 from StringIO import StringIO
33022
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
7
37663
a840e9baff96 test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36798
diff changeset
8 # How much time in seconds can pass before we print a 'Still working' message.
a840e9baff96 test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36798
diff changeset
9 _PRINT_WORKING_MSG_INTERVAL = 5 * 60
a840e9baff96 test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36798
diff changeset
10
40719
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
11 class TrivialContext(object):
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
12 def __enter__(self):
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
13 return self
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
14 def __exit__(self, *exc_info):
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
15 pass
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
16
33022
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
17 class CompilerTest(unittest.TestCase):
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19 def testCompileLibrary(self):
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
20 # A simple but large test. Compile all the code in the
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
21 # standard library and its test suite. This doesn't verify
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
22 # that any of the code is correct, merely the compiler is able
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
23 # to generate some kind of code for it.
36229
e8dec78cfa2e Revert revision 41940: the test causes -uall to
Tim Peters <tim.peters@gmail.com>
parents: 36218
diff changeset
24
37663
a840e9baff96 test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36798
diff changeset
25 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
33022
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
26 libdir = os.path.dirname(unittest.__file__)
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
27 testdir = os.path.dirname(test.test_support.__file__)
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
28
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
29 for dir in [libdir, testdir]:
33050
c478a71c2384 Renamed locals to better reflect their meanings.
Tim Peters <tim.peters@gmail.com>
parents: 33049
diff changeset
30 for basename in os.listdir(dir):
37663
a840e9baff96 test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36798
diff changeset
31 # Print still working message since this test can be really slow
a840e9baff96 test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36798
diff changeset
32 if next_time <= time.time():
a840e9baff96 test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36798
diff changeset
33 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
a840e9baff96 test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36798
diff changeset
34 print >>sys.__stdout__, \
a840e9baff96 test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36798
diff changeset
35 ' testCompileLibrary still working, be patient...'
38021
b5e7183ad112 Try to really fix the slow buildbots this time.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37663
diff changeset
36 sys.__stdout__.flush()
37663
a840e9baff96 test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36798
diff changeset
37
33050
c478a71c2384 Renamed locals to better reflect their meanings.
Tim Peters <tim.peters@gmail.com>
parents: 33049
diff changeset
38 if not basename.endswith(".py"):
33022
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
39 continue
36798
8ddcaaa08fdd Fix failure of test_compiler.py when compiling test_contextlib.py.
Guido van Rossum <guido@python.org>
parents: 36791
diff changeset
40 if not TEST_ALL and random() < 0.98:
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33051
diff changeset
41 continue
33050
c478a71c2384 Renamed locals to better reflect their meanings.
Tim Peters <tim.peters@gmail.com>
parents: 33049
diff changeset
42 path = os.path.join(dir, basename)
33049
d0eb098be264 In verbose mode, display the name of each file before trying to compile
Tim Peters <tim.peters@gmail.com>
parents: 33039
diff changeset
43 if test.test_support.verbose:
33050
c478a71c2384 Renamed locals to better reflect their meanings.
Tim Peters <tim.peters@gmail.com>
parents: 33049
diff changeset
44 print "compiling", path
33718
e6f1ae13be84 Open source files in universal newlines mode.
Michael W. Hudson <mwh@python.net>
parents: 33463
diff changeset
45 f = open(path, "U")
33022
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
46 buf = f.read()
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
47 f.close()
35791
1ac9ed1b5a03 Fix compiler test when run with -u (long mode)
Neal Norwitz <nnorwitz@gmail.com>
parents: 34989
diff changeset
48 if "badsyntax" in basename or "bad_coding" in basename:
33051
455667146d4b Tell unittest that source files with "badsyntax" in their names should
Tim Peters <tim.peters@gmail.com>
parents: 33050
diff changeset
49 self.assertRaises(SyntaxError, compiler.compile,
455667146d4b Tell unittest that source files with "badsyntax" in their names should
Tim Peters <tim.peters@gmail.com>
parents: 33050
diff changeset
50 buf, basename, "exec")
455667146d4b Tell unittest that source files with "badsyntax" in their names should
Tim Peters <tim.peters@gmail.com>
parents: 33050
diff changeset
51 else:
36772
af9f9b593a53 Make failures in test cases print failing source file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36706
diff changeset
52 try:
af9f9b593a53 Make failures in test cases print failing source file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36706
diff changeset
53 compiler.compile(buf, basename, "exec")
af9f9b593a53 Make failures in test cases print failing source file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36706
diff changeset
54 except Exception, e:
36791
604a126f83c2 Reformat the exception message by going through a list.
Martin v. Löwis <martin@v.loewis.de>
parents: 36772
diff changeset
55 args = list(e.args)
45468
1d84bf5f9478 Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents: 43229
diff changeset
56 args.append("in file %s]" % basename)
1d84bf5f9478 Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents: 43229
diff changeset
57 #args[0] += "[in file %s]" % basename
36791
604a126f83c2 Reformat the exception message by going through a list.
Martin v. Löwis <martin@v.loewis.de>
parents: 36772
diff changeset
58 e.args = tuple(args)
36772
af9f9b593a53 Make failures in test cases print failing source file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36706
diff changeset
59 raise
33022
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
60
34887
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33943
diff changeset
61 def testNewClassSyntax(self):
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33943
diff changeset
62 compiler.compile("class foo():pass\n\n","<string>","exec")
34920
67a89b43dcc7 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34887
diff changeset
63
36798
8ddcaaa08fdd Fix failure of test_compiler.py when compiling test_contextlib.py.
Guido van Rossum <guido@python.org>
parents: 36791
diff changeset
64 def testYieldExpr(self):
8ddcaaa08fdd Fix failure of test_compiler.py when compiling test_contextlib.py.
Guido van Rossum <guido@python.org>
parents: 36791
diff changeset
65 compiler.compile("def g(): yield\n\n", "<string>", "exec")
8ddcaaa08fdd Fix failure of test_compiler.py when compiling test_contextlib.py.
Guido van Rossum <guido@python.org>
parents: 36791
diff changeset
66
48313
a8f98a727371 follow-up of issue3473: update the compiler package to recognize the new syntax.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46818
diff changeset
67 def testKeywordAfterStarargs(self):
a8f98a727371 follow-up of issue3473: update the compiler package to recognize the new syntax.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46818
diff changeset
68 def f(*args, **kwargs):
a8f98a727371 follow-up of issue3473: update the compiler package to recognize the new syntax.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46818
diff changeset
69 self.assertEqual((args, kwargs), ((2,3), {'x': 1, 'y': 4}))
a8f98a727371 follow-up of issue3473: update the compiler package to recognize the new syntax.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46818
diff changeset
70 c = compiler.compile('f(x=1, *(2, 3), y=4)', '<string>', 'exec')
a8f98a727371 follow-up of issue3473: update the compiler package to recognize the new syntax.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46818
diff changeset
71 exec c in {'f': f}
a8f98a727371 follow-up of issue3473: update the compiler package to recognize the new syntax.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46818
diff changeset
72
a8f98a727371 follow-up of issue3473: update the compiler package to recognize the new syntax.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46818
diff changeset
73 self.assertRaises(SyntaxError, compiler.parse, "foo(a=1, b)")
a8f98a727371 follow-up of issue3473: update the compiler package to recognize the new syntax.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46818
diff changeset
74 self.assertRaises(SyntaxError, compiler.parse, "foo(1, *args, 3)")
a8f98a727371 follow-up of issue3473: update the compiler package to recognize the new syntax.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46818
diff changeset
75
38910
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
76 def testTryExceptFinally(self):
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
77 # Test that except and finally clauses in one try stmt are recognized
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
78 c = compiler.compile("try:\n 1/0\nexcept:\n e = 1\nfinally:\n f = 1",
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
79 "<string>", "exec")
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
80 dct = {}
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
81 exec c in dct
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
82 self.assertEquals(dct.get('e'), 1)
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
83 self.assertEquals(dct.get('f'), 1)
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
84
38107
29425086e9ab Bug #1385040: don't allow "def foo(a=1, b): pass" in the compiler package.
Georg Brandl <georg@python.org>
parents: 38021
diff changeset
85 def testDefaultArgs(self):
29425086e9ab Bug #1385040: don't allow "def foo(a=1, b): pass" in the compiler package.
Georg Brandl <georg@python.org>
parents: 38021
diff changeset
86 self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass")
29425086e9ab Bug #1385040: don't allow "def foo(a=1, b): pass" in the compiler package.
Georg Brandl <georg@python.org>
parents: 38021
diff changeset
87
39258
6c8bc7cd72a6 Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents: 38910
diff changeset
88 def testDocstrings(self):
6c8bc7cd72a6 Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents: 38910
diff changeset
89 c = compiler.compile('"doc"', '<string>', 'exec')
6c8bc7cd72a6 Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents: 38910
diff changeset
90 self.assert_('__doc__' in c.co_names)
6c8bc7cd72a6 Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents: 38910
diff changeset
91 c = compiler.compile('def f():\n "doc"', '<string>', 'exec')
6c8bc7cd72a6 Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents: 38910
diff changeset
92 g = {}
6c8bc7cd72a6 Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents: 38910
diff changeset
93 exec c in g
6c8bc7cd72a6 Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents: 38910
diff changeset
94 self.assertEquals(g['f'].__doc__, "doc")
6c8bc7cd72a6 Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents: 38910
diff changeset
95
33421
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
96 def testLineNo(self):
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
97 # Test that all nodes except Module have a correct lineno attribute.
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
98 filename = __file__
38704
ba6eebf7c719 Make use of new str.startswith/endswith semantics.
Georg Brandl <georg@python.org>
parents: 38107
diff changeset
99 if filename.endswith((".pyc", ".pyo")):
33421
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
100 filename = filename[:-1]
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
101 tree = compiler.parseFile(filename)
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
102 self.check_lineno(tree)
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
103
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
104 def check_lineno(self, node):
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
105 try:
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
106 self._check_lineno(node)
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
107 except AssertionError:
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
108 print node.__class__, node.lineno
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
109 raise
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
110
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
111 def _check_lineno(self, node):
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
112 if not node.__class__ in NOLINENO:
33463
28ef5fe0b8b5 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33421
diff changeset
113 self.assert_(isinstance(node.lineno, int),
33421
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
114 "lineno=%s on %s" % (node.lineno, node.__class__))
33463
28ef5fe0b8b5 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33421
diff changeset
115 self.assert_(node.lineno > 0,
33421
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
116 "lineno=%s on %s" % (node.lineno, node.__class__))
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
117 for child in node.getChildNodes():
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
118 self.check_lineno(child)
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
119
34989
8b05a4732cf3 Fix compiler.ast.flatten function so that it works on lists.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 34920
diff changeset
120 def testFlatten(self):
8b05a4732cf3 Fix compiler.ast.flatten function so that it works on lists.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 34920
diff changeset
121 self.assertEquals(flatten([1, [2]]), [1, 2])
8b05a4732cf3 Fix compiler.ast.flatten function so that it works on lists.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 34920
diff changeset
122 self.assertEquals(flatten((1, (2,))), [1, 2])
8b05a4732cf3 Fix compiler.ast.flatten function so that it works on lists.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 34920
diff changeset
123
39370
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
124 def testNestedScope(self):
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
125 c = compiler.compile('def g():\n'
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
126 ' a = 1\n'
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
127 ' def f(): return a + 2\n'
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
128 ' return f()\n'
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
129 'result = g()',
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
130 '<string>',
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
131 'exec')
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
132 dct = {}
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
133 exec c in dct
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
134 self.assertEquals(dct.get('result'), 3)
eb23d096d694 Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39258
diff changeset
135
39532
a6afe0eda711 Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39379
diff changeset
136 def testGenExp(self):
a6afe0eda711 Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39379
diff changeset
137 c = compiler.compile('list((i,j) for i in range(3) if i < 3'
a6afe0eda711 Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39379
diff changeset
138 ' for j in range(4) if j > 2)',
a6afe0eda711 Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39379
diff changeset
139 '<string>',
a6afe0eda711 Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39379
diff changeset
140 'eval')
a6afe0eda711 Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39379
diff changeset
141 self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)])
a6afe0eda711 Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39379
diff changeset
142
40719
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
143 def testWith(self):
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
144 # SF bug 1638243
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
145 c = compiler.compile('from __future__ import with_statement\n'
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
146 'def f():\n'
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
147 ' with TrivialContext():\n'
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
148 ' return 1\n'
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
149 'result = f()',
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
150 '<string>',
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
151 'exec' )
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
152 dct = {'TrivialContext': TrivialContext}
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
153 exec c in dct
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
154 self.assertEquals(dct.get('result'), 1)
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
155
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
156 def testWithAss(self):
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
157 c = compiler.compile('from __future__ import with_statement\n'
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
158 'def f():\n'
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
159 ' with TrivialContext() as tc:\n'
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
160 ' return 1\n'
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
161 'result = f()',
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
162 '<string>',
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
163 'exec' )
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
164 dct = {'TrivialContext': TrivialContext}
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
165 exec c in dct
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
166 self.assertEquals(dct.get('result'), 1)
2343c68ca1c8 Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents: 39532
diff changeset
167
39379
ff4b15780396 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39370
diff changeset
168
45901
7f7770719e84 Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents: 45468
diff changeset
169 def testPrintFunction(self):
7f7770719e84 Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents: 45468
diff changeset
170 c = compiler.compile('from __future__ import print_function\n'
7f7770719e84 Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents: 45468
diff changeset
171 'print("a", "b", sep="**", end="++", '
7f7770719e84 Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents: 45468
diff changeset
172 'file=output)',
7f7770719e84 Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents: 45468
diff changeset
173 '<string>',
7f7770719e84 Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents: 45468
diff changeset
174 'exec' )
7f7770719e84 Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents: 45468
diff changeset
175 dct = {'output': StringIO()}
7f7770719e84 Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents: 45468
diff changeset
176 exec c in dct
7f7770719e84 Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents: 45468
diff changeset
177 self.assertEquals(dct['output'].getvalue(), 'a**b++')
7f7770719e84 Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents: 45468
diff changeset
178
43229
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
179 def _testErrEnc(self, src, text, offset):
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
180 try:
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
181 compile(src, "", "exec")
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
182 except SyntaxError, e:
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
183 self.assertEquals(e.offset, offset)
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
184 self.assertEquals(e.text, text)
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
185
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
186 def testSourceCodeEncodingsError(self):
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
187 # Test SyntaxError with encoding definition
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
188 sjis = "print '\x83\x70\x83\x43\x83\x5c\x83\x93', '\n"
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
189 ascii = "print '12345678', '\n"
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
190 encdef = "#! -*- coding: ShiftJIS -*-\n"
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
191
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
192 # ascii source without encdef
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
193 self._testErrEnc(ascii, ascii, 19)
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
194
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
195 # ascii source with encdef
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
196 self._testErrEnc(encdef+ascii, ascii, 19)
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
197
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
198 # non-ascii source with encdef
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
199 self._testErrEnc(encdef+sjis, sjis, 19)
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
200
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
201 # ShiftJIS source without encdef
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
202 self._testErrEnc(sjis, sjis, 19)
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
203
6a048ff30488 Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents: 40719
diff changeset
204
33421
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
205 NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
206
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
207 ###############################################################################
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
208 # code below is just used to trigger some possible errors, for the benefit of
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
209 # testLineNo
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
210 ###############################################################################
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
211
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
212 class Toto:
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
213 """docstring"""
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
214 pass
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
215
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
216 a, b = 2, 3
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
217 [c, d] = 5, 6
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
218 l = [(x, y) for x, y in zip(range(5), range(5,10))]
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
219 l[0]
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
220 l[3:4]
38910
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
221 d = {'a': 2}
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
222 d = {}
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
223 t = ()
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
224 t = (1, 2)
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
225 l = []
aed809443205 Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents: 38704
diff changeset
226 l = [1, 2]
33421
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
227 if l:
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
228 pass
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
229 else:
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
230 a, b = b, a
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
231
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
232 try:
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
233 print yo
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
234 except:
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
235 yo = 3
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
236 else:
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
237 yo += 3
33463
28ef5fe0b8b5 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33421
diff changeset
238
33421
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
239 try:
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
240 a += b
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
241 finally:
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
242 b = 0
33463
28ef5fe0b8b5 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33421
diff changeset
243
33943
359ebc56093e test for fixedness of bug #1057835.
Michael W. Hudson <mwh@python.net>
parents: 33718
diff changeset
244 from math import *
359ebc56093e test for fixedness of bug #1057835.
Michael W. Hudson <mwh@python.net>
parents: 33718
diff changeset
245
33421
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
246 ###############################################################################
b6e875aa508b compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33405
diff changeset
247
33022
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
248 def test_main():
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33051
diff changeset
249 global TEST_ALL
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33051
diff changeset
250 TEST_ALL = test.test_support.is_resource_enabled("compiler")
33022
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
251 test.test_support.run_unittest(CompilerTest)
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
252
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
253 if __name__ == "__main__":
2c93fceee42d Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
254 test_main()