annotate Lib/test/test_compile.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 81d7f78a633a
children 27359a4e0f8c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
1 import unittest
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
2 import sys
46208
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
3 import _ast
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46227
diff changeset
4 from test import support
20939
13df8b6c2b80 Fix SF buf #480096: Assign to __debug__ still allowed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19621
diff changeset
5
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
6 class TestSpecifics(unittest.TestCase):
20939
13df8b6c2b80 Fix SF buf #480096: Assign to __debug__ still allowed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19621
diff changeset
7
57206
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
8 def test_no_ending_newline(self):
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
9 compile("hi", "<test>", "exec")
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
10 compile("hi\r", "<test>", "exec")
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
11
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
12 def test_empty(self):
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
13 compile("", "<test>", "exec")
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
14
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
15 def test_other_newlines(self):
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
16 compile("\r\n", "<test>", "exec")
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
17 compile("\r", "<test>", "exec")
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
18 compile("hi\r\nstuff\r\ndef f():\n pass\r", "<test>", "exec")
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
19 compile("this_is\rreally_old_mac\rdef f():\n pass", "<test>", "exec")
b2d90c1c637d Merged revisions 76230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55003
diff changeset
20
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
21 def test_debug_assignment(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
22 # catch assignments to __debug__
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
23 self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single')
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43777
diff changeset
24 import builtins
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43777
diff changeset
25 prev = builtins.__debug__
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43777
diff changeset
26 setattr(builtins, '__debug__', 'sure')
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43777
diff changeset
27 setattr(builtins, '__debug__', prev)
13714
c9d039196db6 added test case for fixed duplicate arguments bug in Python/compile.c
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff changeset
28
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
29 def test_argument_handling(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
30 # detect duplicate positional and keyword arguments
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
31 self.assertRaises(SyntaxError, eval, 'lambda a,a:0')
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
32 self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0')
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
33 self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0')
48387
b9d2ff5e3dcf now that exec is a function, we can use the shorter assertRaises to test
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
34 self.assertRaises(SyntaxError, exec, 'def f(a, a): pass')
b9d2ff5e3dcf now that exec is a function, we can use the shorter assertRaises to test
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
35 self.assertRaises(SyntaxError, exec, 'def f(a = 0, a = 1): pass')
b9d2ff5e3dcf now that exec is a function, we can use the shorter assertRaises to test
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
36 self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1')
24159
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 20968
diff changeset
37
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
38 def test_syntax_error(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
39 self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec")
16700
d2a8414a1eab SF patch #103749: implicit tuple + default arg
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16018
diff changeset
40
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
41 def test_none_keyword_arg(self):
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
42 self.assertRaises(SyntaxError, compile, "f(None=1)", "<string>", "exec")
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
43
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
44 def test_duplicate_global_local(self):
48387
b9d2ff5e3dcf now that exec is a function, we can use the shorter assertRaises to test
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
45 self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1')
16700
d2a8414a1eab SF patch #103749: implicit tuple + default arg
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16018
diff changeset
46
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
47 def test_exec_with_general_mapping_for_locals(self):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
48
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
49 class M:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
50 "Test mapping interface versus possible calls from eval()."
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
51 def __getitem__(self, key):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
52 if key == 'a':
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
53 return 12
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
54 raise KeyError
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
55 def __setitem__(self, key, value):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
56 self.results = (key, value)
40890
20e08a87915a Fix the last two tests.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
57 def keys(self):
20e08a87915a Fix the last two tests.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
58 return list('xyz')
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
59
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
60 m = M()
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
61 g = globals()
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39442
diff changeset
62 exec('z = a', g, m)
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
63 self.assertEqual(m.results, ('z', 12))
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
64 try:
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39442
diff changeset
65 exec('z = b', g, m)
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
66 except NameError:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
67 pass
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
68 else:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
69 self.fail('Did not detect a KeyError')
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39442
diff changeset
70 exec('z = dir()', g, m)
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
71 self.assertEqual(m.results, ('z', list('xyz')))
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39442
diff changeset
72 exec('z = globals()', g, m)
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
73 self.assertEqual(m.results, ('z', g))
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39442
diff changeset
74 exec('z = locals()', g, m)
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
75 self.assertEqual(m.results, ('z', m))
48387
b9d2ff5e3dcf now that exec is a function, we can use the shorter assertRaises to test
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
76 self.assertRaises(TypeError, exec, 'z = b', m)
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
77
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
78 class A:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
79 "Non-mapping"
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
80 pass
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
81 m = A()
48387
b9d2ff5e3dcf now that exec is a function, we can use the shorter assertRaises to test
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
82 self.assertRaises(TypeError, exec, 'z = a', g, m)
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
83
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
84 # Verify that dict subclasses work as well
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
85 class D(dict):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
86 def __getitem__(self, key):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
87 if key == 'a':
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
88 return 12
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
89 return dict.__getitem__(self, key)
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
90 d = D()
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39442
diff changeset
91 exec('z = a', g, d)
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
92 self.assertEqual(d['z'], 12)
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
93
35792
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
94 def test_extended_arg(self):
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
95 longexpr = 'x = x or ' + '-x' * 2500
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39442
diff changeset
96 g = {}
35792
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
97 code = '''
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
98 def f(x):
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
99 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
100 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
101 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
102 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
103 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
104 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
105 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
106 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
107 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
108 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
109 # the expressions above have no effect, x == argument
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
110 while x:
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
111 x -= 1
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
112 # EXTENDED_ARG/JUMP_ABSOLUTE here
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
113 return x
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
114 ''' % ((longexpr,)*10)
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39442
diff changeset
115 exec(code, g)
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39442
diff changeset
116 self.assertEqual(g['f'](5), 0)
35792
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
117
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
118 def test_argument_order(self):
48387
b9d2ff5e3dcf now that exec is a function, we can use the shorter assertRaises to test
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
119 self.assertRaises(SyntaxError, exec, 'def f(a=1, b): pass')
19501
ce4f1e98ba7e SF bug [#455775] float parsing discrepancy.
Tim Peters <tim.peters@gmail.com>
parents: 16723
diff changeset
120
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
121 def test_float_literals(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
122 # testing bad float literals
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
123 self.assertRaises(SyntaxError, eval, "2e")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
124 self.assertRaises(SyntaxError, eval, "2.0e+")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
125 self.assertRaises(SyntaxError, eval, "1e-")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
126 self.assertRaises(SyntaxError, eval, "3-4e/21")
19621
0bec699783d0 SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents: 19501
diff changeset
127
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
128 def test_indentation(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
129 # testing compile() of indented block w/o trailing newline"
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
130 s = """
27878
be7683a6f521 - Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents: 27846
diff changeset
131 if 1:
be7683a6f521 - Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents: 27846
diff changeset
132 if 2:
be7683a6f521 - Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents: 27846
diff changeset
133 pass"""
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
134 compile(s, "<string>", "exec")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
135
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
136 # This test is probably specific to CPython and may not generalize
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
137 # to other implementations. We are trying to ensure that when
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
138 # the first line of code starts after 256, correct line numbers
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
139 # in tracebacks are still produced.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
140 def test_leading_newlines(self):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
141 s256 = "".join(["\n"] * 256 + ["spam"])
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
142 co = compile(s256, 'fn', 'exec')
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
143 self.assertEqual(co.co_firstlineno, 257)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
144 self.assertEqual(co.co_lnotab, bytes())
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
145
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
146 def test_literals_with_leading_zeroes(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
147 for arg in ["077787", "0xj", "0x.", "0e", "090000000000000",
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
148 "080000000000000", "000000000000009", "000000000000008",
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
149 "0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2",
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
150 "0b101j2", "0o153j2", "0b100e1", "0o777e1", "0777",
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
151 "000777", "000000000000007"]:
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
152 self.assertRaises(SyntaxError, eval, arg)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
153
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
154 self.assertEqual(eval("0xff"), 255)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
155 self.assertEqual(eval("0777."), 777)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
156 self.assertEqual(eval("0777.0"), 777)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
157 self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
158 self.assertEqual(eval("0777e1"), 7770)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
159 self.assertEqual(eval("0e0"), 0)
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
160 self.assertEqual(eval("0000e-012"), 0)
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
161 self.assertEqual(eval("09.5"), 9.5)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
162 self.assertEqual(eval("0777j"), 777j)
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
163 self.assertEqual(eval("000"), 0)
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
164 self.assertEqual(eval("00j"), 0j)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
165 self.assertEqual(eval("00.0"), 0)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
166 self.assertEqual(eval("0e3"), 0)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
167 self.assertEqual(eval("090000000000000."), 90000000000000.)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
168 self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
169 self.assertEqual(eval("090000000000000e0"), 90000000000000.)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
170 self.assertEqual(eval("090000000000000e-0"), 90000000000000.)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
171 self.assertEqual(eval("090000000000000j"), 90000000000000j)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
172 self.assertEqual(eval("000000000000008."), 8.)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
173 self.assertEqual(eval("000000000000009."), 9.)
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
174 self.assertEqual(eval("0b101010"), 42)
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
175 self.assertEqual(eval("-0b000000000010"), -2)
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
176 self.assertEqual(eval("0o777"), 511)
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
177 self.assertEqual(eval("-0o0000010"), -8)
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
178
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
179 def test_unary_minus(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
180 # Verify treatment of unary minus on negative numbers SF bug #660455
44160
9d152530910a Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
181 if sys.maxsize == 2147483647:
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
182 # 32-bit machine
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
183 all_one_bits = '0xffffffff'
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
184 self.assertEqual(eval(all_one_bits), 4294967295)
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
185 self.assertEqual(eval("-" + all_one_bits), -4294967295)
44160
9d152530910a Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
186 elif sys.maxsize == 9223372036854775807:
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
187 # 64-bit machine
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
188 all_one_bits = '0xffffffffffffffff'
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
189 self.assertEqual(eval(all_one_bits), 18446744073709551615)
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
190 self.assertEqual(eval("-" + all_one_bits), -18446744073709551615)
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
191 else:
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
192 self.fail("How many bits *does* this machine have???")
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
193 # Verify treatment of constant folding on -(sys.maxsize+1)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
194 # i.e. -2147483648 on 32 bit platforms. Should return int, not long.
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
195 self.assertIsInstance(eval("%s" % (-sys.maxsize - 1)), int)
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
196 self.assertIsInstance(eval("%s" % (-sys.maxsize - 2)), int)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
197
44160
9d152530910a Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
198 if sys.maxsize == 9223372036854775807:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
199 def test_32_63_bit_values(self):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
200 a = +4294967296 # 1 << 32
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
201 b = -4294967296 # 1 << 32
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
202 c = +281474976710656 # 1 << 48
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
203 d = -281474976710656 # 1 << 48
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
204 e = +4611686018427387904 # 1 << 62
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
205 f = -4611686018427387904 # 1 << 62
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
206 g = +9223372036854775807 # 1 << 63 - 1
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
207 h = -9223372036854775807 # 1 << 63 - 1
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
208
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40890
diff changeset
209 for variable in self.test_32_63_bit_values.__code__.co_consts:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
210 if variable is not None:
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
211 self.assertIsInstance(variable, int)
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
212
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
213 def test_sequence_unpacking_error(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
214 # Verify sequence packing/unpacking with "or". SF bug #757818
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
215 i,j = (1, -1) or (-1, 1)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
216 self.assertEqual(i, 1)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
217 self.assertEqual(j, -1)
27878
be7683a6f521 - Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents: 27846
diff changeset
218
32723
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
219 def test_none_assignment(self):
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
220 stmts = [
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
221 'None = 0',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
222 'None += 0',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
223 '__builtins__.None = 0',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
224 'def None(): pass',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
225 'class None: pass',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
226 '(a, None) = 0, 0',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
227 'for None in range(10): pass',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
228 'def f(None): pass',
54918
0704f15b4682 Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48387
diff changeset
229 'import None',
0704f15b4682 Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48387
diff changeset
230 'import x as None',
0704f15b4682 Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48387
diff changeset
231 'from x import None',
0704f15b4682 Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48387
diff changeset
232 'from x import y as None'
32723
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
233 ]
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
234 for stmt in stmts:
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
235 stmt += "\n"
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
236 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
237 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
19621
0bec699783d0 SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents: 19501
diff changeset
238
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
239 def test_import(self):
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
240 succeed = [
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
241 'import sys',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
242 'import os, sys',
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
243 'import os as bar',
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
244 'import os.path as bar',
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
245 'from __future__ import nested_scopes, generators',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
246 'from __future__ import (nested_scopes,\ngenerators)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
247 'from __future__ import (nested_scopes,\ngenerators,)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
248 'from sys import stdin, stderr, stdout',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
249 'from sys import (stdin, stderr,\nstdout)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
250 'from sys import (stdin, stderr,\nstdout,)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
251 'from sys import (stdin\n, stderr, stdout)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
252 'from sys import (stdin\n, stderr, stdout,)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
253 'from sys import stdin as si, stdout as so, stderr as se',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
254 'from sys import (stdin as si, stdout as so, stderr as se)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
255 'from sys import (stdin as si, stdout as so, stderr as se,)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
256 ]
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
257 fail = [
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
258 'import (os, sys)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
259 'import (os), (sys)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
260 'import ((os), (sys))',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
261 'import (sys',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
262 'import sys)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
263 'import (os,)',
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
264 'import os As bar',
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
265 'import os.path a bar',
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
266 'from sys import stdin As stdout',
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
267 'from sys import stdin a stdout',
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
268 'from (sys) import stdin',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
269 'from __future__ import (nested_scopes',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
270 'from __future__ import nested_scopes)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
271 'from __future__ import nested_scopes,\ngenerators',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
272 'from sys import (stdin',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
273 'from sys import stdin)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
274 'from sys import stdin, stdout,\nstderr',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
275 'from sys import stdin si',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
276 'from sys import stdin,'
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
277 'from sys import (*)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
278 'from sys import (stdin,, stdout, stderr)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
279 'from sys import (stdin, stdout),',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
280 ]
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
281 for stmt in succeed:
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
282 compile(stmt, 'tmp', 'exec')
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
283 for stmt in fail:
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
284 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
285
33822
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
286 def test_for_distinct_code_objects(self):
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
287 # SF bug 1048870
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
288 def f():
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
289 f1 = lambda x=1: x
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
290 f2 = lambda x=2: x
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
291 return f1, f2
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
292 f1, f2 = f()
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40890
diff changeset
293 self.assertNotEqual(id(f1.__code__), id(f2.__code__))
33822
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
294
59673
52479e0454e9 Merged revisions 79034 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58473
diff changeset
295 def test_lambda_doc(self):
52479e0454e9 Merged revisions 79034 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58473
diff changeset
296 l = lambda: "foo"
52479e0454e9 Merged revisions 79034 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58473
diff changeset
297 self.assertIsNone(l.__doc__)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36965
diff changeset
298
42340
f4af6f4b51cc Make test_compile not fail.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
299 ## def test_unicode_encoding(self):
f4af6f4b51cc Make test_compile not fail.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
300 ## code = "# -*- coding: utf-8 -*-\npass\n"
f4af6f4b51cc Make test_compile not fail.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
301 ## self.assertRaises(SyntaxError, compile, code, "tmp", "exec")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36965
diff changeset
302
36965
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
303 def test_subscripts(self):
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
304 # SF bug 1448804
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
305 # Class to make testing subscript results easy
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
306 class str_map(object):
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
307 def __init__(self):
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
308 self.data = {}
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
309 def __getitem__(self, key):
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
310 return self.data[str(key)]
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
311 def __setitem__(self, key, value):
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
312 self.data[str(key)] = value
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
313 def __delitem__(self, key):
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
314 del self.data[str(key)]
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
315 def __contains__(self, key):
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
316 return str(key) in self.data
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
317 d = str_map()
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
318 # Index
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
319 d[1] = 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
320 self.assertEqual(d[1], 1)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
321 d[1] += 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
322 self.assertEqual(d[1], 2)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
323 del d[1]
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
324 self.assertNotIn(1, d)
36965
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
325 # Tuple of indices
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
326 d[1, 1] = 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
327 self.assertEqual(d[1, 1], 1)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
328 d[1, 1] += 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
329 self.assertEqual(d[1, 1], 2)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
330 del d[1, 1]
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
331 self.assertNotIn((1, 1), d)
36965
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
332 # Simple slice
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
333 d[1:2] = 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
334 self.assertEqual(d[1:2], 1)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
335 d[1:2] += 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
336 self.assertEqual(d[1:2], 2)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
337 del d[1:2]
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
338 self.assertNotIn(slice(1, 2), d)
36965
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
339 # Tuple of simple slices
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
340 d[1:2, 1:2] = 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
341 self.assertEqual(d[1:2, 1:2], 1)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
342 d[1:2, 1:2] += 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
343 self.assertEqual(d[1:2, 1:2], 2)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
344 del d[1:2, 1:2]
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
345 self.assertNotIn((slice(1, 2), slice(1, 2)), d)
36965
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
346 # Extended slice
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
347 d[1:2:3] = 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
348 self.assertEqual(d[1:2:3], 1)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
349 d[1:2:3] += 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
350 self.assertEqual(d[1:2:3], 2)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
351 del d[1:2:3]
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
352 self.assertNotIn(slice(1, 2, 3), d)
36965
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
353 # Tuple of extended slices
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
354 d[1:2:3, 1:2:3] = 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
355 self.assertEqual(d[1:2:3, 1:2:3], 1)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
356 d[1:2:3, 1:2:3] += 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
357 self.assertEqual(d[1:2:3, 1:2:3], 2)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
358 del d[1:2:3, 1:2:3]
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
359 self.assertNotIn((slice(1, 2, 3), slice(1, 2, 3)), d)
36965
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
360 # Ellipsis
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
361 d[...] = 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
362 self.assertEqual(d[...], 1)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
363 d[...] += 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
364 self.assertEqual(d[...], 2)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
365 del d[...]
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
366 self.assertNotIn(Ellipsis, d)
36965
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
367 # Tuple of Ellipses
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
368 d[..., ...] = 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
369 self.assertEqual(d[..., ...], 1)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
370 d[..., ...] += 1
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
371 self.assertEqual(d[..., ...], 2)
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
372 del d[..., ...]
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
373 self.assertNotIn((Ellipsis, Ellipsis), d)
36965
9a08724110d0 Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 35792
diff changeset
374
40943
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40910
diff changeset
375 def test_annotation_limit(self):
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
376 # 16 bits are available for # of annotations, but only 8 bits are
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
377 # available for the parameter count, hence 255
40943
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40910
diff changeset
378 # is the max. Ensure the result of too many annotations is a
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40910
diff changeset
379 # SyntaxError.
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
380 s = "def f(%s): pass"
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
381 s %= ', '.join('a%d:%d' % (i,i) for i in range(256))
40943
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40910
diff changeset
382 self.assertRaises(SyntaxError, compile, s, '?', 'exec')
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40910
diff changeset
383 # Test that the max # of annotations compiles.
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
384 s = "def f(%s): pass"
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
385 s %= ', '.join('a%d:%d' % (i,i) for i in range(255))
40943
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40910
diff changeset
386 compile(s, '?', 'exec')
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
387
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
388 def test_mangling(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
389 class A:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
390 def f():
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
391 __mangled = 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
392 __not_mangled__ = 2
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
393 import __mangled_mod
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
394 import __package__.module
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
395
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57206
diff changeset
396 self.assertIn("_A__mangled", A.f.__code__.co_varnames)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57206
diff changeset
397 self.assertIn("__not_mangled__", A.f.__code__.co_varnames)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57206
diff changeset
398 self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57206
diff changeset
399 self.assertIn("__package__", A.f.__code__.co_varnames)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
400
46208
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
401 def test_compile_ast(self):
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
402 fname = __file__
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
403 if fname.lower().endswith(('pyc', 'pyo')):
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
404 fname = fname[:-1]
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
405 with open(fname, 'r') as f:
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
406 fcontents = f.read()
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
407 sample_code = [
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
408 ['<assign>', 'x = 5'],
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
409 ['<ifblock>', """if True:\n pass\n"""],
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
410 ['<forblock>', """for n in [1, 2, 3]:\n print(n)\n"""],
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
411 ['<deffunc>', """def foo():\n pass\nfoo()\n"""],
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
412 [fname, fcontents],
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
413 ]
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
414
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
415 for fname, code in sample_code:
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
416 co1 = compile(code, '%s1' % fname, 'exec')
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
417 ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48387
diff changeset
418 self.assertTrue(type(ast) == _ast.Module)
46208
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
419 co2 = compile(ast, '%s3' % fname, 'exec')
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
420 self.assertEqual(co1, co2)
46227
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
421 # the code object's filename comes from the second compilation step
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
422 self.assertEqual(co2.co_filename, '%s3' % fname)
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
423
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
424 # raise exception when node type doesn't match with compile mode
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
425 co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST)
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
426 self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
427
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
428 # raise exception when node type is no start node
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
429 self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
430
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
431 # raise exception when node has invalid children
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
432 ast = _ast.Module()
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
433 ast.body = [_ast.BoolOp()]
b2d93a960b98 Merged revisions 62039-62042 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 46208
diff changeset
434 self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
46208
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
435
ea75b12fe8e7 Merged revisions 62004 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 44160
diff changeset
436
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
437 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46227
diff changeset
438 support.run_unittest(TestSpecifics)
19621
0bec699783d0 SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents: 19501
diff changeset
439
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
440 if __name__ == "__main__":
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
441 test_main()