Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_compiler.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | e3fb353b3fd4 |
| children |
| 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 |
|
60188
aab64283d00a
Fix test_compiler.py that was using unittest.__file__ to find Lib/ (unittest is now a package).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58669
diff
changeset
|
26 # warning: if 'os' or 'test_support' are moved in some other dir, |
|
aab64283d00a
Fix test_compiler.py that was using unittest.__file__ to find Lib/ (unittest is now a package).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58669
diff
changeset
|
27 # they should be changed here. |
|
aab64283d00a
Fix test_compiler.py that was using unittest.__file__ to find Lib/ (unittest is now a package).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58669
diff
changeset
|
28 libdir = os.path.dirname(os.__file__) |
|
33022
2c93fceee42d
Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
29 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
|
30 |
|
75108
e3fb353b3fd4
this was supposed to die
Benjamin Peterson <benjamin@python.org>
parents:
68256
diff
changeset
|
31 for dir in [testdir]: |
|
e3fb353b3fd4
this was supposed to die
Benjamin Peterson <benjamin@python.org>
parents:
68256
diff
changeset
|
32 for basename in "test_os.py",: |
|
37663
a840e9baff96
test_compile can be really long if we are using -u compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36798
diff
changeset
|
33 # 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
|
34 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
|
35 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
|
36 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
|
37 ' 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
|
38 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
|
39 |
|
33050
c478a71c2384
Renamed locals to better reflect their meanings.
Tim Peters <tim.peters@gmail.com>
parents:
33049
diff
changeset
|
40 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
|
41 continue |
|
36798
8ddcaaa08fdd
Fix failure of test_compiler.py when compiling test_contextlib.py.
Guido van Rossum <guido@python.org>
parents:
36791
diff
changeset
|
42 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
|
43 continue |
|
33050
c478a71c2384
Renamed locals to better reflect their meanings.
Tim Peters <tim.peters@gmail.com>
parents:
33049
diff
changeset
|
44 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
|
45 if test.test_support.verbose: |
|
33050
c478a71c2384
Renamed locals to better reflect their meanings.
Tim Peters <tim.peters@gmail.com>
parents:
33049
diff
changeset
|
46 print "compiling", path |
|
33718
e6f1ae13be84
Open source files in universal newlines mode.
Michael W. Hudson <mwh@python.net>
parents:
33463
diff
changeset
|
47 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
|
48 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
|
49 f.close() |
|
35791
1ac9ed1b5a03
Fix compiler test when run with -u (long mode)
Neal Norwitz <nnorwitz@gmail.com>
parents:
34989
diff
changeset
|
50 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
|
51 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
|
52 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
|
53 else: |
|
36772
af9f9b593a53
Make failures in test cases print failing source file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36706
diff
changeset
|
54 try: |
|
af9f9b593a53
Make failures in test cases print failing source file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36706
diff
changeset
|
55 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
|
56 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
|
57 args = list(e.args) |
|
45468
1d84bf5f9478
Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents:
43229
diff
changeset
|
58 args.append("in file %s]" % basename) |
|
1d84bf5f9478
Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents:
43229
diff
changeset
|
59 #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
|
60 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
|
61 raise |
|
33022
2c93fceee42d
Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
62 |
|
34887
7c2780c9e51b
Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents:
33943
diff
changeset
|
63 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
|
64 compiler.compile("class foo():pass\n\n","<string>","exec") |
|
34920
67a89b43dcc7
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34887
diff
changeset
|
65 |
|
36798
8ddcaaa08fdd
Fix failure of test_compiler.py when compiling test_contextlib.py.
Guido van Rossum <guido@python.org>
parents:
36791
diff
changeset
|
66 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
|
67 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
|
68 |
|
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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 |
|
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 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
|
76 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
|
77 |
|
38910
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
78 def testTryExceptFinally(self): |
|
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
79 # Test that except and finally clauses in one try stmt are recognized |
|
63556
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
48313
diff
changeset
|
80 c = compiler.compile("try:\n 1//0\nexcept:\n e = 1\nfinally:\n f = 1", |
|
38910
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
81 "<string>", "exec") |
|
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
82 dct = {} |
|
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
83 exec c in dct |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
84 self.assertEqual(dct.get('e'), 1) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
85 self.assertEqual(dct.get('f'), 1) |
|
38910
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
86 |
|
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
|
87 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
|
88 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
|
89 |
|
39258
6c8bc7cd72a6
Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents:
38910
diff
changeset
|
90 def testDocstrings(self): |
|
6c8bc7cd72a6
Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents:
38910
diff
changeset
|
91 c = compiler.compile('"doc"', '<string>', 'exec') |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58250
diff
changeset
|
92 self.assertIn('__doc__', c.co_names) |
|
39258
6c8bc7cd72a6
Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents:
38910
diff
changeset
|
93 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
|
94 g = {} |
|
6c8bc7cd72a6
Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents:
38910
diff
changeset
|
95 exec c in g |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
96 self.assertEqual(g['f'].__doc__, "doc") |
|
39258
6c8bc7cd72a6
Bug #1441397: The compiler module now recognizes module and function
Georg Brandl <georg@python.org>
parents:
38910
diff
changeset
|
97 |
|
33421
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
98 def testLineNo(self): |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
99 # 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
|
100 filename = __file__ |
|
38704
ba6eebf7c719
Make use of new str.startswith/endswith semantics.
Georg Brandl <georg@python.org>
parents:
38107
diff
changeset
|
101 if filename.endswith((".pyc", ".pyo")): |
|
33421
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
102 filename = filename[:-1] |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
103 tree = compiler.parseFile(filename) |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
104 self.check_lineno(tree) |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
105 |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
106 def check_lineno(self, node): |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
107 try: |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
108 self._check_lineno(node) |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
109 except AssertionError: |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
110 print node.__class__, node.lineno |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
111 raise |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
112 |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
113 def _check_lineno(self, node): |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
114 if not node.__class__ in NOLINENO: |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
115 self.assertIsInstance(node.lineno, int, |
|
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__)) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54337
diff
changeset
|
117 self.assertTrue(node.lineno > 0, |
|
33421
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
118 "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
|
119 for child in node.getChildNodes(): |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
120 self.check_lineno(child) |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
121 |
|
34989
8b05a4732cf3
Fix compiler.ast.flatten function so that it works on lists.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34920
diff
changeset
|
122 def testFlatten(self): |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
123 self.assertEqual(flatten([1, [2]]), [1, 2]) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
124 self.assertEqual(flatten((1, (2,))), [1, 2]) |
|
34989
8b05a4732cf3
Fix compiler.ast.flatten function so that it works on lists.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34920
diff
changeset
|
125 |
|
39370
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39258
diff
changeset
|
126 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
|
127 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
|
128 ' 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
|
129 ' 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
|
130 ' 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
|
131 'result = g()', |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39258
diff
changeset
|
132 '<string>', |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39258
diff
changeset
|
133 'exec') |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39258
diff
changeset
|
134 dct = {} |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39258
diff
changeset
|
135 exec c in dct |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
136 self.assertEqual(dct.get('result'), 3) |
|
39370
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39258
diff
changeset
|
137 |
|
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
|
138 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
|
139 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
|
140 ' 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
|
141 '<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
|
142 'eval') |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
143 self.assertEqual(eval(c), [(0, 3), (1, 3), (2, 3)]) |
|
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
|
144 |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
145 def testSetLiteral(self): |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
146 c = compiler.compile('{1, 2, 3}', '<string>', 'eval') |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
147 self.assertEqual(eval(c), {1,2,3}) |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
148 c = compiler.compile('{1, 2, 3,}', '<string>', 'eval') |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
149 self.assertEqual(eval(c), {1,2,3}) |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
150 |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
151 def testDictLiteral(self): |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
152 c = compiler.compile('{1:2, 2:3, 3:4}', '<string>', 'eval') |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
153 self.assertEqual(eval(c), {1:2, 2:3, 3:4}) |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
154 c = compiler.compile('{1:2, 2:3, 3:4,}', '<string>', 'eval') |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
155 self.assertEqual(eval(c), {1:2, 2:3, 3:4}) |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
156 |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
157 def testSetComp(self): |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
158 c = compiler.compile('{x for x in range(1, 4)}', '<string>', 'eval') |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
159 self.assertEqual(eval(c), {1, 2, 3}) |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
160 c = compiler.compile('{x * y for x in range(3) if x != 0' |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
161 ' for y in range(4) if y != 0}', |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
162 '<string>', |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
163 'eval') |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
164 self.assertEqual(eval(c), {1, 2, 3, 4, 6}) |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
165 |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
166 def testDictComp(self): |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
167 c = compiler.compile('{x:x+1 for x in range(1, 4)}', '<string>', 'eval') |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
168 self.assertEqual(eval(c), {1:2, 2:3, 3:4}) |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
169 c = compiler.compile('{(x, y) : y for x in range(2) if x != 0' |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
170 ' for y in range(3) if y != 0}', |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
171 '<string>', |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
172 'eval') |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
173 self.assertEqual(eval(c), {(1, 2): 2, (1, 1): 1}) |
|
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
|
174 |
|
40719
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
175 def testWith(self): |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
176 # SF bug 1638243 |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
177 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
|
178 'def f():\n' |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
179 ' with TrivialContext():\n' |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
180 ' return 1\n' |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
181 'result = f()', |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
182 '<string>', |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
183 'exec' ) |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
184 dct = {'TrivialContext': TrivialContext} |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
185 exec c in dct |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
186 self.assertEqual(dct.get('result'), 1) |
|
40719
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
187 |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
188 def testWithAss(self): |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
189 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
|
190 'def f():\n' |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
191 ' 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
|
192 ' return 1\n' |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
193 'result = f()', |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
194 '<string>', |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
195 'exec' ) |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
196 dct = {'TrivialContext': TrivialContext} |
|
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
197 exec c in dct |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
198 self.assertEqual(dct.get('result'), 1) |
|
40719
2343c68ca1c8
Patch #1638243: the compiler package is now able to correctly compile
Georg Brandl <georg@python.org>
parents:
39532
diff
changeset
|
199 |
|
54337
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
200 def testWithMult(self): |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
201 events = [] |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
202 class Ctx: |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
203 def __init__(self, n): |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
204 self.n = n |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
205 def __enter__(self): |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
206 events.append(self.n) |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
207 def __exit__(self, *args): |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
208 pass |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
209 c = compiler.compile('from __future__ import with_statement\n' |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
210 'def f():\n' |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
211 ' with Ctx(1) as tc, Ctx(2) as tc2:\n' |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
212 ' return 1\n' |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
213 'result = f()', |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
214 '<string>', |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
215 'exec' ) |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
216 dct = {'Ctx': Ctx} |
|
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
217 exec c in dct |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
218 self.assertEqual(dct.get('result'), 1) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
219 self.assertEqual(events, [1, 2]) |
|
54337
16759f62eb22
Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents:
51444
diff
changeset
|
220 |
|
51444
494da2c71902
Add test for issue #999042, explict global statement works.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
48313
diff
changeset
|
221 def testGlobal(self): |
|
494da2c71902
Add test for issue #999042, explict global statement works.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
48313
diff
changeset
|
222 code = compiler.compile('global x\nx=1', '<string>', 'exec') |
|
494da2c71902
Add test for issue #999042, explict global statement works.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
48313
diff
changeset
|
223 d1 = {'__builtins__': {}} |
|
494da2c71902
Add test for issue #999042, explict global statement works.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
48313
diff
changeset
|
224 d2 = {} |
|
494da2c71902
Add test for issue #999042, explict global statement works.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
48313
diff
changeset
|
225 exec code in d1, d2 |
|
494da2c71902
Add test for issue #999042, explict global statement works.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
48313
diff
changeset
|
226 # x should be in the globals dict |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
227 self.assertEqual(d1.get('x'), 1) |
|
39379
ff4b15780396
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39370
diff
changeset
|
228 |
|
45901
7f7770719e84
Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents:
45468
diff
changeset
|
229 def testPrintFunction(self): |
|
7f7770719e84
Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents:
45468
diff
changeset
|
230 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
|
231 'print("a", "b", sep="**", end="++", ' |
|
7f7770719e84
Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents:
45468
diff
changeset
|
232 'file=output)', |
|
7f7770719e84
Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents:
45468
diff
changeset
|
233 '<string>', |
|
7f7770719e84
Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents:
45468
diff
changeset
|
234 'exec' ) |
|
7f7770719e84
Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents:
45468
diff
changeset
|
235 dct = {'output': StringIO()} |
|
7f7770719e84
Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents:
45468
diff
changeset
|
236 exec c in dct |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
237 self.assertEqual(dct['output'].getvalue(), 'a**b++') |
|
45901
7f7770719e84
Another test for __future__ print_function.
Eric Smith <eric@trueblade.com>
parents:
45468
diff
changeset
|
238 |
|
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
|
239 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
|
240 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
|
241 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
|
242 except SyntaxError, e: |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
243 self.assertEqual(e.offset, offset) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65318
diff
changeset
|
244 self.assertEqual(e.text, text) |
|
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
|
245 |
|
6a048ff30488
Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents:
40719
diff
changeset
|
246 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
|
247 # 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
|
248 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
|
249 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
|
250 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
|
251 |
|
6a048ff30488
Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents:
40719
diff
changeset
|
252 # 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
|
253 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
|
254 |
|
6a048ff30488
Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents:
40719
diff
changeset
|
255 # 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
|
256 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
|
257 |
|
6a048ff30488
Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents:
40719
diff
changeset
|
258 # 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
|
259 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
|
260 |
|
6a048ff30488
Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents:
40719
diff
changeset
|
261 # 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
|
262 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
|
263 |
|
6a048ff30488
Patch #1031213: Decode source line in SyntaxErrors back to its original
Martin v. Löwis <martin@v.loewis.de>
parents:
40719
diff
changeset
|
264 |
|
33421
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
265 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
|
266 |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
267 ############################################################################### |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
268 # 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
|
269 # testLineNo |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
270 ############################################################################### |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
271 |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
272 class Toto: |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
273 """docstring""" |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
274 pass |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
275 |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
276 a, b = 2, 3 |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
277 [c, d] = 5, 6 |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
278 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
|
279 l[0] |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
280 l[3:4] |
|
38910
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
281 d = {'a': 2} |
|
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
282 d = {} |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
283 d = {x: y for x, y in zip(range(5), range(5,10))} |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
284 s = {x for x in range(10)} |
|
58229
c2fdf25329ff
Issue #2335: Backport set literals syntax from Python 3.x.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58198
diff
changeset
|
285 s = {1} |
|
38910
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
286 t = () |
|
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
287 t = (1, 2) |
|
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
288 l = [] |
|
aed809443205
Test for correct compilation of try-except-finally stmt.
Georg Brandl <georg@python.org>
parents:
38704
diff
changeset
|
289 l = [1, 2] |
|
33421
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
290 if l: |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
291 pass |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
292 else: |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
293 a, b = b, a |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
294 |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
295 try: |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
296 print yo |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
297 except: |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
298 yo = 3 |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
299 else: |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
300 yo += 3 |
|
33463
28ef5fe0b8b5
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33421
diff
changeset
|
301 |
|
33421
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
302 try: |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
303 a += b |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
304 finally: |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
305 b = 0 |
|
33463
28ef5fe0b8b5
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33421
diff
changeset
|
306 |
|
33943
359ebc56093e
test for fixedness of bug #1057835.
Michael W. Hudson <mwh@python.net>
parents:
33718
diff
changeset
|
307 from math import * |
|
359ebc56093e
test for fixedness of bug #1057835.
Michael W. Hudson <mwh@python.net>
parents:
33718
diff
changeset
|
308 |
|
33421
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
309 ############################################################################### |
|
b6e875aa508b
compiler.transformer: correct lineno attribute when possible
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33405
diff
changeset
|
310 |
|
33022
2c93fceee42d
Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
311 def test_main(): |
|
33405
21c79340ded5
Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents:
33051
diff
changeset
|
312 global TEST_ALL |
|
65318
07b6bb76ee43
Merged revisions 85482 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60188
diff
changeset
|
313 TEST_ALL = test.test_support.is_resource_enabled("cpu") |
|
33022
2c93fceee42d
Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
314 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
|
315 |
|
2c93fceee42d
Add a trivial test for the compiler package, guarded by compiler resource.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
316 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
|
317 test_main() |
