Update version number and release date.
[python/dscho.git] / Lib / test / test_compile.py
blob2281b3769155086221370a05fcbbb6cb1ad56863
1 from test.test_support import verbose, TestFailed
3 if verbose:
4 print "Testing whether compiler catches assignment to __debug__"
6 try:
7 compile('__debug__ = 1', '?', 'single')
8 except SyntaxError:
9 pass
11 import __builtin__
12 prev = __builtin__.__debug__
13 setattr(__builtin__, '__debug__', 'sure')
14 setattr(__builtin__, '__debug__', prev)
16 if verbose:
17 print 'Running tests on argument handling'
19 try:
20 exec 'def f(a, a): pass'
21 raise TestFailed, "duplicate arguments"
22 except SyntaxError:
23 pass
25 if verbose:
26 print "compiling string with syntax error"
28 try:
29 compile("1+*3", "filename", "exec")
30 except SyntaxError, detail:
31 if not detail.filename == "filename":
32 raise TestFailed, "expected 'filename', got %r" % detail.filename
34 try:
35 exec 'def f(a = 0, a = 1): pass'
36 raise TestFailed, "duplicate keyword arguments"
37 except SyntaxError:
38 pass
40 try:
41 exec 'def f(a): global a; a = 1'
42 raise TestFailed, "variable is global and local"
43 except SyntaxError:
44 pass
46 if verbose:
47 print "testing complex args"
49 def comp_args((a, b)):
50 print a,b
52 comp_args((1, 2))
54 def comp_args((a, b)=(3, 4)):
55 print a, b
57 comp_args((1, 2))
58 comp_args()
60 def comp_args(a, (b, c)):
61 print a, b, c
63 comp_args(1, (2, 3))
65 def comp_args(a=2, (b, c)=(3, 4)):
66 print a, b, c
68 comp_args(1, (2, 3))
69 comp_args()
71 try:
72 exec 'def f(a=1, (b, c)): pass'
73 raise TestFailed, "non-default args after default"
74 except SyntaxError:
75 pass
77 if verbose:
78 print "testing bad float literals"
80 def expect_error(s):
81 try:
82 eval(s)
83 raise TestFailed("%r accepted" % s)
84 except SyntaxError:
85 pass
87 expect_error("2e")
88 expect_error("2.0e+")
89 expect_error("1e-")
90 expect_error("3-4e/21")
92 if verbose:
93 print "testing compile() of indented block w/o trailing newline"
95 s = """
96 if 1:
97 if 2:
98 pass"""
99 compile(s, "<string>", "exec")
102 if verbose:
103 print "testing literals with leading zeroes"
105 def expect_same(test_source, expected):
106 got = eval(test_source)
107 if got != expected:
108 raise TestFailed("eval(%r) gave %r, but expected %r" %
109 (test_source, got, expected))
111 expect_error("077787")
112 expect_error("0xj")
113 expect_error("0x.")
114 expect_error("0e")
115 expect_same("0777", 511)
116 expect_same("0777L", 511)
117 expect_same("000777", 511)
118 expect_same("0xff", 255)
119 expect_same("0xffL", 255)
120 expect_same("0XfF", 255)
121 expect_same("0777.", 777)
122 expect_same("0777.0", 777)
123 expect_same("000000000000000000000000000000000000000000000000000777e0", 777)
124 expect_same("0777e1", 7770)
125 expect_same("0e0", 0)
126 expect_same("0000E-012", 0)
127 expect_same("09.5", 9.5)
128 expect_same("0777j", 777j)
129 expect_same("00j", 0j)
130 expect_same("00.0", 0)
131 expect_same("0e3", 0)
132 expect_same("090000000000000.", 90000000000000.)
133 expect_same("090000000000000.0000000000000000000000", 90000000000000.)
134 expect_same("090000000000000e0", 90000000000000.)
135 expect_same("090000000000000e-0", 90000000000000.)
136 expect_same("090000000000000j", 90000000000000j)
137 expect_error("090000000000000") # plain octal literal w/ decimal digit
138 expect_error("080000000000000") # plain octal literal w/ decimal digit
139 expect_error("000000000000009") # plain octal literal w/ decimal digit
140 expect_error("000000000000008") # plain octal literal w/ decimal digit
141 expect_same("000000000000007", 7)
142 expect_same("000000000000008.", 8.)
143 expect_same("000000000000009.", 9.)
145 # Verify treatment of unary minus on negative numbers SF bug #660455
146 import warnings
147 warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning)
148 warnings.filterwarnings("ignore", "hex.* of negative int", FutureWarning)
149 # XXX Of course the following test will have to be changed in Python 2.4
150 # This test is in a <string> so the filterwarnings() can affect it
151 import sys
152 all_one_bits = '0xffffffff'
153 if sys.maxint != 2147483647:
154 all_one_bits = '0xffffffffffffffff'
155 exec """
156 expect_same(all_one_bits, -1)
157 expect_same("-" + all_one_bits, 1)