annotate Lib/test/test_compile.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents c0ab2bafb264
children 657ae877c0bf 788e1397ce77
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
46162
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
3 import _ast
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
4 from test import test_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
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
8 def test_debug_assignment(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
9 # catch assignments to __debug__
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
10 self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single')
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
11 import __builtin__
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
12 prev = __builtin__.__debug__
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
13 setattr(__builtin__, '__debug__', 'sure')
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
14 setattr(__builtin__, '__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
15
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
16 def test_argument_handling(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
17 # 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
18 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
19 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
20 self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0')
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
21 try:
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
22 exec 'def f(a, a): pass'
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
23 self.fail("duplicate arguments")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
24 except SyntaxError:
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
25 pass
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
26 try:
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
27 exec 'def f(a = 0, a = 1): pass'
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
28 self.fail("duplicate keyword arguments")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
29 except SyntaxError:
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
30 pass
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
31 try:
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
32 exec 'def f(a): global a; a = 1'
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
33 self.fail("variable is global and local")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
34 except SyntaxError:
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
35 pass
24159
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 20968
diff changeset
36
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
37 def test_syntax_error(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
38 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
39
41974
c8b0e3b8b456 Disallow function calls like foo(None=1).
Georg Brandl <georg@python.org>
parents: 40960
diff changeset
40 def test_none_keyword_arg(self):
c8b0e3b8b456 Disallow function calls like foo(None=1).
Georg Brandl <georg@python.org>
parents: 40960
diff changeset
41 self.assertRaises(SyntaxError, compile, "f(None=1)", "<string>", "exec")
c8b0e3b8b456 Disallow function calls like foo(None=1).
Georg Brandl <georg@python.org>
parents: 40960
diff changeset
42
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
43 def test_duplicate_global_local(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
44 try:
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
45 exec 'def f(a): global a; a = 1'
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
46 self.fail("variable is global and local")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
47 except SyntaxError:
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
48 pass
16700
d2a8414a1eab SF patch #103749: implicit tuple + default arg
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16018
diff changeset
49
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
50 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
51
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
52 class M:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
53 "Test mapping interface versus possible calls from eval()."
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
54 def __getitem__(self, key):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
55 if key == 'a':
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
56 return 12
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
57 raise KeyError
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
58 def __setitem__(self, key, value):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
59 self.results = (key, value)
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
60 def keys(self):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
61 return list('xyz')
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
62
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
63 m = M()
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
64 g = globals()
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
65 exec 'z = a' in g, m
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
66 self.assertEqual(m.results, ('z', 12))
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
67 try:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
68 exec 'z = b' in g, m
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
69 except NameError:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
70 pass
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
71 else:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
72 self.fail('Did not detect a KeyError')
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
73 exec 'z = dir()' in g, m
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
74 self.assertEqual(m.results, ('z', list('xyz')))
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
75 exec 'z = globals()' in g, m
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
76 self.assertEqual(m.results, ('z', g))
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
77 exec 'z = locals()' in g, m
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
78 self.assertEqual(m.results, ('z', m))
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
79 try:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
80 exec 'z = b' in m
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
81 except TypeError:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
82 pass
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
83 else:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
84 self.fail('Did not validate globals as a real dict')
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
85
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
86 class A:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
87 "Non-mapping"
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
88 pass
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
89 m = A()
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
90 try:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
91 exec 'z = a' in g, m
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
92 except TypeError:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
93 pass
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
94 else:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
95 self.fail('Did not validate locals as a mapping')
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
96
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
97 # Verify that dict subclasses work as well
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
98 class D(dict):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
99 def __getitem__(self, key):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
100 if key == 'a':
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
101 return 12
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
102 return dict.__getitem__(self, key)
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
103 d = D()
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
104 exec 'z = a' in g, d
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
105 self.assertEqual(d['z'], 12)
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32723
diff changeset
106
35792
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
107 def test_extended_arg(self):
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
108 longexpr = 'x = x or ' + '-x' * 2500
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
109 code = '''
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
110 def f(x):
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
111 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
112 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
113 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
114 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
115 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
116 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
117 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
118 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
119 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
120 %s
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
121 # 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
122 while x:
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
123 x -= 1
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
124 # EXTENDED_ARG/JUMP_ABSOLUTE here
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
125 return x
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
126 ''' % ((longexpr,)*10)
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
127 exec code
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
128 self.assertEqual(f(5), 0)
b8b13b1ffe75 Fix problem handling EXTENDED_ARGs from SF bug # 1333982
Neal Norwitz <nnorwitz@gmail.com>
parents: 33822
diff changeset
129
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
130 def test_complex_args(self):
16700
d2a8414a1eab SF patch #103749: implicit tuple + default arg
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16018
diff changeset
131
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
132 def comp_args((a, b)):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
133 return a,b
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
134 self.assertEqual(comp_args((1, 2)), (1, 2))
16700
d2a8414a1eab SF patch #103749: implicit tuple + default arg
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16018
diff changeset
135
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
136 def comp_args((a, b)=(3, 4)):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
137 return a, b
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
138 self.assertEqual(comp_args((1, 2)), (1, 2))
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
139 self.assertEqual(comp_args(), (3, 4))
16700
d2a8414a1eab SF patch #103749: implicit tuple + default arg
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16018
diff changeset
140
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
141 def comp_args(a, (b, c)):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
142 return a, b, c
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
143 self.assertEqual(comp_args(1, (2, 3)), (1, 2, 3))
16700
d2a8414a1eab SF patch #103749: implicit tuple + default arg
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16018
diff changeset
144
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
145 def comp_args(a=2, (b, c)=(3, 4)):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
146 return a, b, c
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
147 self.assertEqual(comp_args(1, (2, 3)), (1, 2, 3))
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
148 self.assertEqual(comp_args(), (2, 3, 4))
19501
ce4f1e98ba7e SF bug [#455775] float parsing discrepancy.
Tim Peters <tim.peters@gmail.com>
parents: 16723
diff changeset
149
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
150 def test_argument_order(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
151 try:
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
152 exec 'def f(a=1, (b, c)): pass'
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
153 self.fail("non-default args after default")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
154 except SyntaxError:
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
155 pass
19501
ce4f1e98ba7e SF bug [#455775] float parsing discrepancy.
Tim Peters <tim.peters@gmail.com>
parents: 16723
diff changeset
156
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
157 def test_float_literals(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
158 # testing bad float literals
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
159 self.assertRaises(SyntaxError, eval, "2e")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
160 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
161 self.assertRaises(SyntaxError, eval, "1e-")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
162 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
163
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
164 def test_indentation(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
165 # 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
166 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
167 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
168 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
169 pass"""
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
170 compile(s, "<string>", "exec")
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
171
39040
ca3d9a67126b Bug #1512814, Fix incorrect lineno's when code at module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 39039
diff changeset
172 # This test is probably specific to CPython and may not generalize
ca3d9a67126b Bug #1512814, Fix incorrect lineno's when code at module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 39039
diff changeset
173 # to other implementations. We are trying to ensure that when
ca3d9a67126b Bug #1512814, Fix incorrect lineno's when code at module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 39039
diff changeset
174 # the first line of code starts after 256, correct line numbers
ca3d9a67126b Bug #1512814, Fix incorrect lineno's when code at module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 39039
diff changeset
175 # in tracebacks are still produced.
ca3d9a67126b Bug #1512814, Fix incorrect lineno's when code at module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 39039
diff changeset
176 def test_leading_newlines(self):
ca3d9a67126b Bug #1512814, Fix incorrect lineno's when code at module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 39039
diff changeset
177 s256 = "".join(["\n"] * 256 + ["spam"])
ca3d9a67126b Bug #1512814, Fix incorrect lineno's when code at module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 39039
diff changeset
178 co = compile(s256, 'fn', 'exec')
ca3d9a67126b Bug #1512814, Fix incorrect lineno's when code at module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 39039
diff changeset
179 self.assertEqual(co.co_firstlineno, 257)
ca3d9a67126b Bug #1512814, Fix incorrect lineno's when code at module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 39039
diff changeset
180 self.assertEqual(co.co_lnotab, '')
ca3d9a67126b Bug #1512814, Fix incorrect lineno's when code at module scope
Neal Norwitz <nnorwitz@gmail.com>
parents: 39039
diff changeset
181
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
182 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
183 for arg in ["077787", "0xj", "0x.", "0e", "090000000000000",
45791
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 41974
diff changeset
184 "080000000000000", "000000000000009", "000000000000008",
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 41974
diff changeset
185 "0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2",
46545
459083c6be15 Issue2681: the literal 0o8 was wrongly accepted, and evaluated as float(0.0).
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46193
diff changeset
186 "0b101j2", "0o153j2", "0b100e1", "0o777e1", "0o8", "0o78"]:
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
187 self.assertRaises(SyntaxError, eval, arg)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
188
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
189 self.assertEqual(eval("0777"), 511)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
190 self.assertEqual(eval("0777L"), 511)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
191 self.assertEqual(eval("000777"), 511)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
192 self.assertEqual(eval("0xff"), 255)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
193 self.assertEqual(eval("0xffL"), 255)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
194 self.assertEqual(eval("0XfF"), 255)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
195 self.assertEqual(eval("0777."), 777)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
196 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
197 self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
198 self.assertEqual(eval("0777e1"), 7770)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
199 self.assertEqual(eval("0e0"), 0)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
200 self.assertEqual(eval("0000E-012"), 0)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
201 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
202 self.assertEqual(eval("0777j"), 777j)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
203 self.assertEqual(eval("00j"), 0j)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
204 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
205 self.assertEqual(eval("0e3"), 0)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
206 self.assertEqual(eval("090000000000000."), 90000000000000.)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
207 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
208 self.assertEqual(eval("090000000000000e0"), 90000000000000.)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
209 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
210 self.assertEqual(eval("090000000000000j"), 90000000000000j)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
211 self.assertEqual(eval("000000000000007"), 7)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
212 self.assertEqual(eval("000000000000008."), 8.)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
213 self.assertEqual(eval("000000000000009."), 9.)
45791
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 41974
diff changeset
214 self.assertEqual(eval("0b101010"), 42)
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 41974
diff changeset
215 self.assertEqual(eval("-0b000000000010"), -2)
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 41974
diff changeset
216 self.assertEqual(eval("0o777"), 511)
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 41974
diff changeset
217 self.assertEqual(eval("-0o0000010"), -8)
47977
c0ab2bafb264 Issue #3360: Fix incorrect parsing of "020000000000.0".
Mark Dickinson <dickinsm@gmail.com>
parents: 46545
diff changeset
218 self.assertEqual(eval("020000000000.0"), 20000000000.0)
c0ab2bafb264 Issue #3360: Fix incorrect parsing of "020000000000.0".
Mark Dickinson <dickinsm@gmail.com>
parents: 46545
diff changeset
219 self.assertEqual(eval("037777777777e0"), 37777777777.0)
c0ab2bafb264 Issue #3360: Fix incorrect parsing of "020000000000.0".
Mark Dickinson <dickinsm@gmail.com>
parents: 46545
diff changeset
220 self.assertEqual(eval("01000000000000000000000.0"),
c0ab2bafb264 Issue #3360: Fix incorrect parsing of "020000000000.0".
Mark Dickinson <dickinsm@gmail.com>
parents: 46545
diff changeset
221 1000000000000000000000.0)
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
222
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
223 def test_unary_minus(self):
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
224 # Verify treatment of unary minus on negative numbers SF bug #660455
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
225 if sys.maxint == 2147483647:
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
226 # 32-bit machine
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
227 all_one_bits = '0xffffffff'
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
228 self.assertEqual(eval(all_one_bits), 4294967295L)
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
229 self.assertEqual(eval("-" + all_one_bits), -4294967295L)
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
230 elif sys.maxint == 9223372036854775807:
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
231 # 64-bit machine
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
232 all_one_bits = '0xffffffffffffffff'
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
233 self.assertEqual(eval(all_one_bits), 18446744073709551615L)
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
234 self.assertEqual(eval("-" + all_one_bits), -18446744073709551615L)
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
235 else:
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 29583
diff changeset
236 self.fail("How many bits *does* this machine have???")
39038
64ccd745ea92 Fix SF bug 1441486: bad unary minus folding in compiler.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39035
diff changeset
237 # Verify treatment of contant folding on -(sys.maxint+1)
64ccd745ea92 Fix SF bug 1441486: bad unary minus folding in compiler.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39035
diff changeset
238 # i.e. -2147483648 on 32 bit platforms. Should return int, not long.
64ccd745ea92 Fix SF bug 1441486: bad unary minus folding in compiler.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39035
diff changeset
239 self.assertTrue(isinstance(eval("%s" % (-sys.maxint - 1)), int))
64ccd745ea92 Fix SF bug 1441486: bad unary minus folding in compiler.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 39035
diff changeset
240 self.assertTrue(isinstance(eval("%s" % (-sys.maxint - 2)), long))
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
241
39039
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
242 if sys.maxint == 9223372036854775807:
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
243 def test_32_63_bit_values(self):
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
244 a = +4294967296 # 1 << 32
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
245 b = -4294967296 # 1 << 32
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
246 c = +281474976710656 # 1 << 48
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
247 d = -281474976710656 # 1 << 48
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
248 e = +4611686018427387904 # 1 << 62
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
249 f = -4611686018427387904 # 1 << 62
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
250 g = +9223372036854775807 # 1 << 63 - 1
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
251 h = -9223372036854775807 # 1 << 63 - 1
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
252
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
253 for variable in self.test_32_63_bit_values.func_code.co_consts:
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
254 if variable is not None:
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
255 self.assertTrue(isinstance(variable, int))
95f9e0184555 On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents: 39038
diff changeset
256
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
257 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
258 # 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
259 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
260 self.assertEqual(i, 1)
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
261 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
262
32723
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
263 def test_none_assignment(self):
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
264 stmts = [
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
265 'None = 0',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
266 'None += 0',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
267 '__builtins__.None = 0',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
268 'def None(): pass',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
269 'class None: pass',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
270 '(a, None) = 0, 0',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
271 'for None in range(10): pass',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
272 'def f(None): pass',
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
273 ]
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
274 for stmt in stmts:
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
275 stmt += "\n"
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
276 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
277 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
278
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
279 def test_import(self):
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
280 succeed = [
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
281 'import sys',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
282 'import os, sys',
39034
066aa42ce5f2 Fix SF bug #1519018: 'as' is now validated properly in import statements
Neal Norwitz <nnorwitz@gmail.com>
parents: 37162
diff changeset
283 'import os as bar',
066aa42ce5f2 Fix SF bug #1519018: 'as' is now validated properly in import statements
Neal Norwitz <nnorwitz@gmail.com>
parents: 37162
diff changeset
284 'import os.path as bar',
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
285 'from __future__ import nested_scopes, generators',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
286 'from __future__ import (nested_scopes,\ngenerators)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
287 'from __future__ import (nested_scopes,\ngenerators,)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
288 'from sys import stdin, stderr, stdout',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
289 'from sys import (stdin, stderr,\nstdout)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
290 'from sys import (stdin, stderr,\nstdout,)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
291 '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
292 '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
293 '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
294 '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
295 '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
296 ]
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
297 fail = [
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
298 'import (os, sys)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
299 'import (os), (sys)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
300 'import ((os), (sys))',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
301 'import (sys',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
302 'import sys)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
303 'import (os,)',
39034
066aa42ce5f2 Fix SF bug #1519018: 'as' is now validated properly in import statements
Neal Norwitz <nnorwitz@gmail.com>
parents: 37162
diff changeset
304 'import os As bar',
066aa42ce5f2 Fix SF bug #1519018: 'as' is now validated properly in import statements
Neal Norwitz <nnorwitz@gmail.com>
parents: 37162
diff changeset
305 'import os.path a bar',
39035
abb2c76a5e05 Add an additional test for bug #1519018.
Georg Brandl <georg@python.org>
parents: 39034
diff changeset
306 'from sys import stdin As stdout',
abb2c76a5e05 Add an additional test for bug #1519018.
Georg Brandl <georg@python.org>
parents: 39034
diff changeset
307 '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
308 'from (sys) import stdin',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
309 'from __future__ import (nested_scopes',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
310 'from __future__ import nested_scopes)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
311 'from __future__ import nested_scopes,\ngenerators',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
312 'from sys import (stdin',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
313 'from sys import stdin)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
314 'from sys import stdin, stdout,\nstderr',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
315 'from sys import stdin si',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
316 'from sys import stdin,'
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
317 'from sys import (*)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
318 'from sys import (stdin,, stdout, stderr)',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
319 'from sys import (stdin, stdout),',
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
320 ]
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
321 for stmt in succeed:
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
322 compile(stmt, 'tmp', 'exec')
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
323 for stmt in fail:
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32890
diff changeset
324 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
325
33822
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
326 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
327 # SF bug 1048870
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
328 def f():
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
329 f1 = lambda x=1: x
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
330 f2 = lambda x=2: x
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
331 return f1, f2
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
332 f1, f2 = f()
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
333 self.assertNotEqual(id(f1.func_code), id(f2.func_code))
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
334
37162
b4d92ef2f338 Forward port MvL's fix in 43227:
Neal Norwitz <nnorwitz@gmail.com>
parents: 36965
diff changeset
335 def test_unicode_encoding(self):
b4d92ef2f338 Forward port MvL's fix in 43227:
Neal Norwitz <nnorwitz@gmail.com>
parents: 36965
diff changeset
336 code = u"# -*- coding: utf-8 -*-\npass\n"
b4d92ef2f338 Forward port MvL's fix in 43227:
Neal Norwitz <nnorwitz@gmail.com>
parents: 36965
diff changeset
337 self.assertRaises(SyntaxError, compile, code, "tmp", "exec")
b4d92ef2f338 Forward port MvL's fix in 43227:
Neal Norwitz <nnorwitz@gmail.com>
parents: 36965
diff changeset
338
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 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
340 # 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
341 # 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
342 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
343 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
344 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
345 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
346 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
347 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
348 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
349 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
350 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
351 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
352 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
353 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
354 # 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
355 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
356 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
357 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
358 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
359 del 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
360 self.assertEqual(1 in d, False)
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 # 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
362 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
363 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
364 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
365 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
366 del 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
367 self.assertEqual((1, 1) in d, False)
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 # 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
369 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
370 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
371 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
372 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
373 del 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
374 self.assertEqual(slice(1, 2) in d, False)
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
375 # 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
376 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
377 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
378 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
379 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
380 del d[1:2, 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
381 self.assertEqual((slice(1, 2), slice(1, 2)) in d, False)
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
382 # 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
383 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
384 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
385 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
386 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
387 del d[1:2:3]
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
388 self.assertEqual(slice(1, 2, 3) in d, False)
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
389 # 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
390 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
391 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
392 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
393 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
394 del d[1:2:3, 1:2:3]
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
395 self.assertEqual((slice(1, 2, 3), slice(1, 2, 3)) in d, False)
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
396 # 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
397 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
398 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
399 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
400 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
401 del d[...]
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
402 self.assertEqual(Ellipsis in d, False)
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
403 # 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
404 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
405 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
406 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
407 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
408 del d[..., ...]
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
409 self.assertEqual((Ellipsis, Ellipsis) in d, False)
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
410
40960
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
411 def test_mangling(self):
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
412 class A:
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
413 def f():
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
414 __mangled = 1
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
415 __not_mangled__ = 2
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
416 import __mangled_mod
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
417 import __package__.module
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
418
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
419 self.assert_("_A__mangled" in A.f.func_code.co_varnames)
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
420 self.assert_("__not_mangled__" in A.f.func_code.co_varnames)
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
421 self.assert_("_A__mangled_mod" in A.f.func_code.co_varnames)
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
422 self.assert_("__package__" in A.f.func_code.co_varnames)
541a8b1b5da7 Fix long-standing bug in name mangling for package imports
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40453
diff changeset
423
46162
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
424 def test_compile_ast(self):
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
425 fname = __file__
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
426 if fname.lower().endswith(('pyc', 'pyo')):
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
427 fname = fname[:-1]
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
428 with open(fname, 'r') as f:
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
429 fcontents = f.read()
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
430 sample_code = [
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
431 ['<assign>', 'x = 5'],
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
432 ['<print1>', 'print 1'],
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
433 ['<printv>', 'print v'],
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
434 ['<printTrue>', 'print True'],
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
435 ['<printList>', 'print []'],
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
436 ['<ifblock>', """if True:\n pass\n"""],
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
437 ['<forblock>', """for n in [1, 2, 3]:\n print n\n"""],
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
438 ['<deffunc>', """def foo():\n pass\nfoo()\n"""],
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
439 [fname, fcontents],
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
440 ]
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
441
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
442 for fname, code in sample_code:
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
443 co1 = compile(code, '%s1' % fname, 'exec')
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
444 ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
445 self.assert_(type(ast) == _ast.Module)
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
446 co2 = compile(ast, '%s3' % fname, 'exec')
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
447 self.assertEqual(co1, co2)
46193
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
448 # the code object's filename comes from the second compilation step
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
449 self.assertEqual(co2.co_filename, '%s3' % fname)
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
450
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
451 # raise exception when node type doesn't match with compile mode
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
452 co1 = compile('print 1', '<string>', 'exec', _ast.PyCF_ONLY_AST)
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
453 self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
454
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
455 # raise exception when node type is no start node
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
456 self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
457
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
458 # raise exception when node has invalid children
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
459 ast = _ast.Module()
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
460 ast.body = [_ast.BoolOp()]
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
461 self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
46162
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
462
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
463
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
464 def test_main():
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
465 test_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
466
29583
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
467 if __name__ == "__main__":
7fe4e9f16634 SF patch #736962. Converted test_compile to unittest format.
Raymond Hettinger <python@rcn.com>
parents: 29561
diff changeset
468 test_main()