source: trunk/essentials/dev-lang/python/Lib/test/test_syntax.py@ 3317

Last change on this file since 3317 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 12.3 KB
Line 
1"""This module tests SyntaxErrors.
2
3Here's an example of the sort of thing that is tested.
4
5>>> def f(x):
6... global x
7Traceback (most recent call last):
8SyntaxError: name 'x' is local and global
9
10The tests are all raise SyntaxErrors. They were created by checking
11each C call that raises SyntaxError. There are several modules that
12raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and
13symtable.c.
14
15The parser itself outlaws a lot of invalid syntax. None of these
16errors are tested here at the moment. We should add some tests; since
17there are infinitely many programs with invalid syntax, we would need
18to be judicious in selecting some.
19
20The compiler generates a synthetic module name for code executed by
21doctest. Since all the code comes from the same module, a suffix like
22[1] is appended to the module name, As a consequence, changing the
23order of tests in this module means renumbering all the errors after
24it. (Maybe we should enable the ellipsis option for these tests.)
25
26In ast.c, syntax errors are raised by calling ast_error().
27
28Errors from set_context():
29
30TODO(jhylton): "assignment to None" is inconsistent with other messages
31
32>>> obj.None = 1
33Traceback (most recent call last):
34SyntaxError: assignment to None (<doctest test.test_syntax[1]>, line 1)
35
36>>> None = 1
37Traceback (most recent call last):
38SyntaxError: assignment to None (<doctest test.test_syntax[2]>, line 1)
39
40It's a syntax error to assign to the empty tuple. Why isn't it an
41error to assign to the empty list? It will always raise some error at
42runtime.
43
44>>> () = 1
45Traceback (most recent call last):
46SyntaxError: can't assign to () (<doctest test.test_syntax[3]>, line 1)
47
48>>> f() = 1
49Traceback (most recent call last):
50SyntaxError: can't assign to function call (<doctest test.test_syntax[4]>, line 1)
51
52>>> del f()
53Traceback (most recent call last):
54SyntaxError: can't delete function call (<doctest test.test_syntax[5]>, line 1)
55
56>>> a + 1 = 2
57Traceback (most recent call last):
58SyntaxError: can't assign to operator (<doctest test.test_syntax[6]>, line 1)
59
60>>> (x for x in x) = 1
61Traceback (most recent call last):
62SyntaxError: can't assign to generator expression (<doctest test.test_syntax[7]>, line 1)
63
64>>> 1 = 1
65Traceback (most recent call last):
66SyntaxError: can't assign to literal (<doctest test.test_syntax[8]>, line 1)
67
68>>> "abc" = 1
69Traceback (most recent call last):
70SyntaxError: can't assign to literal (<doctest test.test_syntax[9]>, line 1)
71
72>>> `1` = 1
73Traceback (most recent call last):
74SyntaxError: can't assign to repr (<doctest test.test_syntax[10]>, line 1)
75
76If the left-hand side of an assignment is a list or tuple, an illegal
77expression inside that contain should still cause a syntax error.
78This test just checks a couple of cases rather than enumerating all of
79them.
80
81>>> (a, "b", c) = (1, 2, 3)
82Traceback (most recent call last):
83SyntaxError: can't assign to literal (<doctest test.test_syntax[11]>, line 1)
84
85>>> [a, b, c + 1] = [1, 2, 3]
86Traceback (most recent call last):
87SyntaxError: can't assign to operator (<doctest test.test_syntax[12]>, line 1)
88
89>>> a if 1 else b = 1
90Traceback (most recent call last):
91SyntaxError: can't assign to conditional expression (<doctest test.test_syntax[13]>, line 1)
92
93From compiler_complex_args():
94
95>>> def f(None=1):
96... pass
97Traceback (most recent call last):
98SyntaxError: assignment to None (<doctest test.test_syntax[14]>, line 1)
99
100
101From ast_for_arguments():
102
103>>> def f(x, y=1, z):
104... pass
105Traceback (most recent call last):
106SyntaxError: non-default argument follows default argument (<doctest test.test_syntax[15]>, line 1)
107
108>>> def f(x, None):
109... pass
110Traceback (most recent call last):
111SyntaxError: assignment to None (<doctest test.test_syntax[16]>, line 1)
112
113>>> def f(*None):
114... pass
115Traceback (most recent call last):
116SyntaxError: assignment to None (<doctest test.test_syntax[17]>, line 1)
117
118>>> def f(**None):
119... pass
120Traceback (most recent call last):
121SyntaxError: assignment to None (<doctest test.test_syntax[18]>, line 1)
122
123
124From ast_for_funcdef():
125
126>>> def None(x):
127... pass
128Traceback (most recent call last):
129SyntaxError: assignment to None (<doctest test.test_syntax[19]>, line 1)
130
131
132From ast_for_call():
133
134>>> def f(it, *varargs):
135... return list(it)
136>>> L = range(10)
137>>> f(x for x in L)
138[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
139>>> f(x for x in L, 1)
140Traceback (most recent call last):
141SyntaxError: Generator expression must be parenthesized if not sole argument (<doctest test.test_syntax[23]>, line 1)
142>>> f((x for x in L), 1)
143[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
144
145>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
146... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
147... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
148... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
149... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
150... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
151... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
152... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
153... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
154... i100, i101, i102, i103, i104, i105, i106, i107, i108,
155... i109, i110, i111, i112, i113, i114, i115, i116, i117,
156... i118, i119, i120, i121, i122, i123, i124, i125, i126,
157... i127, i128, i129, i130, i131, i132, i133, i134, i135,
158... i136, i137, i138, i139, i140, i141, i142, i143, i144,
159... i145, i146, i147, i148, i149, i150, i151, i152, i153,
160... i154, i155, i156, i157, i158, i159, i160, i161, i162,
161... i163, i164, i165, i166, i167, i168, i169, i170, i171,
162... i172, i173, i174, i175, i176, i177, i178, i179, i180,
163... i181, i182, i183, i184, i185, i186, i187, i188, i189,
164... i190, i191, i192, i193, i194, i195, i196, i197, i198,
165... i199, i200, i201, i202, i203, i204, i205, i206, i207,
166... i208, i209, i210, i211, i212, i213, i214, i215, i216,
167... i217, i218, i219, i220, i221, i222, i223, i224, i225,
168... i226, i227, i228, i229, i230, i231, i232, i233, i234,
169... i235, i236, i237, i238, i239, i240, i241, i242, i243,
170... i244, i245, i246, i247, i248, i249, i250, i251, i252,
171... i253, i254, i255)
172Traceback (most recent call last):
173SyntaxError: more than 255 arguments (<doctest test.test_syntax[25]>, line 1)
174
175The actual error cases counts positional arguments, keyword arguments,
176and generator expression arguments separately. This test combines the
177three.
178
179>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
180... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
181... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
182... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
183... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
184... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
185... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
186... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
187... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
188... i100, i101, i102, i103, i104, i105, i106, i107, i108,
189... i109, i110, i111, i112, i113, i114, i115, i116, i117,
190... i118, i119, i120, i121, i122, i123, i124, i125, i126,
191... i127, i128, i129, i130, i131, i132, i133, i134, i135,
192... i136, i137, i138, i139, i140, i141, i142, i143, i144,
193... i145, i146, i147, i148, i149, i150, i151, i152, i153,
194... i154, i155, i156, i157, i158, i159, i160, i161, i162,
195... i163, i164, i165, i166, i167, i168, i169, i170, i171,
196... i172, i173, i174, i175, i176, i177, i178, i179, i180,
197... i181, i182, i183, i184, i185, i186, i187, i188, i189,
198... i190, i191, i192, i193, i194, i195, i196, i197, i198,
199... i199, i200, i201, i202, i203, i204, i205, i206, i207,
200... i208, i209, i210, i211, i212, i213, i214, i215, i216,
201... i217, i218, i219, i220, i221, i222, i223, i224, i225,
202... i226, i227, i228, i229, i230, i231, i232, i233, i234,
203... i235, i236, i237, i238, i239, i240, i241, i242, i243,
204... (x for x in i244), i245, i246, i247, i248, i249, i250, i251,
205... i252=1, i253=1, i254=1, i255=1)
206Traceback (most recent call last):
207SyntaxError: more than 255 arguments (<doctest test.test_syntax[26]>, line 1)
208
209>>> f(lambda x: x[0] = 3)
210Traceback (most recent call last):
211SyntaxError: lambda cannot contain assignment (<doctest test.test_syntax[27]>, line 1)
212
213The grammar accepts any test (basically, any expression) in the
214keyword slot of a call site. Test a few different options.
215
216>>> f(x()=2)
217Traceback (most recent call last):
218SyntaxError: keyword can't be an expression (<doctest test.test_syntax[28]>, line 1)
219>>> f(a or b=1)
220Traceback (most recent call last):
221SyntaxError: keyword can't be an expression (<doctest test.test_syntax[29]>, line 1)
222>>> f(x.y=1)
223Traceback (most recent call last):
224SyntaxError: keyword can't be an expression (<doctest test.test_syntax[30]>, line 1)
225
226
227From ast_for_expr_stmt():
228
229>>> (x for x in x) += 1
230Traceback (most recent call last):
231SyntaxError: augmented assignment to generator expression not possible (<doctest test.test_syntax[31]>, line 1)
232>>> None += 1
233Traceback (most recent call last):
234SyntaxError: assignment to None (<doctest test.test_syntax[32]>, line 1)
235>>> f() += 1
236Traceback (most recent call last):
237SyntaxError: illegal expression for augmented assignment (<doctest test.test_syntax[33]>, line 1)
238"""
239
240import re
241import unittest
242import warnings
243
244from test import test_support
245
246class SyntaxTestCase(unittest.TestCase):
247
248 def _check_error(self, code, errtext,
249 filename="<testcase>", mode="exec", subclass=None):
250 """Check that compiling code raises SyntaxError with errtext.
251
252 errtest is a regular expression that must be present in the
253 test of the exception raised. If subclass is specified it
254 is the expected subclass of SyntaxError (e.g. IndentationError).
255 """
256 try:
257 compile(code, filename, mode)
258 except SyntaxError, err:
259 if subclass and not isinstance(err, subclass):
260 self.fail("SyntaxError is not a %s" % subclass.__name__)
261 mo = re.search(errtext, str(err))
262 if mo is None:
263 self.fail("SyntaxError did not contain '%r'" % (errtext,))
264 else:
265 self.fail("compile() did not raise SyntaxError")
266
267 def test_assign_call(self):
268 self._check_error("f() = 1", "assign")
269
270 def test_assign_del(self):
271 self._check_error("del f()", "delete")
272
273 def test_global_err_then_warn(self):
274 # Bug tickler: The SyntaxError raised for one global statement
275 # shouldn't be clobbered by a SyntaxWarning issued for a later one.
276 source = re.sub('(?m)^ *:', '', """\
277 :def error(a):
278 : global a # SyntaxError
279 :def warning():
280 : b = 1
281 : global b # SyntaxWarning
282 :""")
283 warnings.filterwarnings(action='ignore', category=SyntaxWarning)
284 self._check_error(source, "global")
285 warnings.filters.pop(0)
286
287 def test_break_outside_loop(self):
288 self._check_error("break", "outside loop")
289
290 def test_delete_deref(self):
291 source = re.sub('(?m)^ *:', '', """\
292 :def foo(x):
293 : def bar():
294 : print x
295 : del x
296 :""")
297 self._check_error(source, "nested scope")
298
299 def test_unexpected_indent(self):
300 self._check_error("foo()\n bar()\n", "unexpected indent",
301 subclass=IndentationError)
302
303 def test_no_indent(self):
304 self._check_error("if 1:\nfoo()", "expected an indented block",
305 subclass=IndentationError)
306
307 def test_bad_outdent(self):
308 self._check_error("if 1:\n foo()\n bar()",
309 "unindent does not match .* level",
310 subclass=IndentationError)
311
312 def test_kwargs_last(self):
313 self._check_error("int(base=10, '2')", "non-keyword arg")
314
315def test_main():
316 test_support.run_unittest(SyntaxTestCase)
317 from test import test_syntax
318 test_support.run_doctest(test_syntax, verbosity=True)
319
320if __name__ == "__main__":
321 test_main()
Note: See TracBrowser for help on using the repository browser.