PageRenderTime 166ms CodeModel.GetById 80ms app.highlight 14ms RepoModel.GetById 67ms app.codeStats 0ms

/lib-python/2/test/test_syntax.py

https://bitbucket.org/kcr/pypy
Python | 573 lines | 521 code | 39 blank | 13 comment | 50 complexity | 6145b2e08c8aeb1ab31b8eb7c54974cf MD5 | raw file
  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):
  8  File "<doctest test.test_syntax[0]>", line 1
  9SyntaxError: name 'x' is local and global
 10
 11The tests are all raise SyntaxErrors.  They were created by checking
 12each C call that raises SyntaxError.  There are several modules that
 13raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and
 14symtable.c.
 15
 16The parser itself outlaws a lot of invalid syntax.  None of these
 17errors are tested here at the moment.  We should add some tests; since
 18there are infinitely many programs with invalid syntax, we would need
 19to be judicious in selecting some.
 20
 21The compiler generates a synthetic module name for code executed by
 22doctest.  Since all the code comes from the same module, a suffix like
 23[1] is appended to the module name, As a consequence, changing the
 24order of tests in this module means renumbering all the errors after
 25it.  (Maybe we should enable the ellipsis option for these tests.)
 26
 27In ast.c, syntax errors are raised by calling ast_error().
 28
 29Errors from set_context():
 30
 31>>> obj.None = 1
 32Traceback (most recent call last):
 33  File "<doctest test.test_syntax[1]>", line 1
 34SyntaxError: cannot assign to None
 35
 36>>> None = 1
 37Traceback (most recent call last):
 38  File "<doctest test.test_syntax[2]>", line 1
 39SyntaxError: cannot assign to None
 40
 41It's a syntax error to assign to the empty tuple.  Why isn't it an
 42error to assign to the empty list?  It will always raise some error at
 43runtime.
 44
 45>>> () = 1
 46Traceback (most recent call last):
 47  File "<doctest test.test_syntax[3]>", line 1
 48SyntaxError: can't assign to ()
 49
 50>>> f() = 1
 51Traceback (most recent call last):
 52  File "<doctest test.test_syntax[4]>", line 1
 53SyntaxError: can't assign to function call
 54
 55>>> del f()
 56Traceback (most recent call last):
 57  File "<doctest test.test_syntax[5]>", line 1
 58SyntaxError: can't delete function call
 59
 60>>> a + 1 = 2
 61Traceback (most recent call last):
 62  File "<doctest test.test_syntax[6]>", line 1
 63SyntaxError: can't assign to operator
 64
 65>>> (x for x in x) = 1
 66Traceback (most recent call last):
 67  File "<doctest test.test_syntax[7]>", line 1
 68SyntaxError: can't assign to generator expression
 69
 70>>> 1 = 1
 71Traceback (most recent call last):
 72  File "<doctest test.test_syntax[8]>", line 1
 73SyntaxError: can't assign to literal
 74
 75>>> "abc" = 1
 76Traceback (most recent call last):
 77  File "<doctest test.test_syntax[8]>", line 1
 78SyntaxError: can't assign to literal
 79
 80>>> `1` = 1
 81Traceback (most recent call last):
 82  File "<doctest test.test_syntax[10]>", line 1
 83SyntaxError: can't assign to repr
 84
 85If the left-hand side of an assignment is a list or tuple, an illegal
 86expression inside that contain should still cause a syntax error.
 87This test just checks a couple of cases rather than enumerating all of
 88them.
 89
 90>>> (a, "b", c) = (1, 2, 3)
 91Traceback (most recent call last):
 92  File "<doctest test.test_syntax[11]>", line 1
 93SyntaxError: can't assign to literal
 94
 95>>> [a, b, c + 1] = [1, 2, 3]
 96Traceback (most recent call last):
 97  File "<doctest test.test_syntax[12]>", line 1
 98SyntaxError: can't assign to operator
 99
100>>> a if 1 else b = 1
101Traceback (most recent call last):
102  File "<doctest test.test_syntax[13]>", line 1
103SyntaxError: can't assign to conditional expression
104
105From compiler_complex_args():
106
107>>> def f(None=1):
108...     pass
109Traceback (most recent call last):
110  File "<doctest test.test_syntax[14]>", line 1
111SyntaxError: cannot assign to None
112
113
114From ast_for_arguments():
115
116>>> def f(x, y=1, z):
117...     pass
118Traceback (most recent call last):
119  File "<doctest test.test_syntax[15]>", line 1
120SyntaxError: non-default argument follows default argument
121
122>>> def f(x, None):
123...     pass
124Traceback (most recent call last):
125  File "<doctest test.test_syntax[16]>", line 1
126SyntaxError: cannot assign to None
127
128>>> def f(*None):
129...     pass
130Traceback (most recent call last):
131  File "<doctest test.test_syntax[17]>", line 1
132SyntaxError: cannot assign to None
133
134>>> def f(**None):
135...     pass
136Traceback (most recent call last):
137  File "<doctest test.test_syntax[18]>", line 1
138SyntaxError: cannot assign to None
139
140
141From ast_for_funcdef():
142
143>>> def None(x):
144...     pass
145Traceback (most recent call last):
146  File "<doctest test.test_syntax[19]>", line 1
147SyntaxError: cannot assign to None
148
149
150From ast_for_call():
151
152>>> def f(it, *varargs):
153...     return list(it)
154>>> L = range(10)
155>>> f(x for x in L)
156[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
157>>> f(x for x in L, 1)
158Traceback (most recent call last):
159  File "<doctest test.test_syntax[23]>", line 1
160SyntaxError: Generator expression must be parenthesized if not sole argument
161>>> f((x for x in L), 1)
162[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
163
164>>> f(i0,  i1,  i2,  i3,  i4,  i5,  i6,  i7,  i8,  i9,  i10,  i11,
165...   i12,  i13,  i14,  i15,  i16,  i17,  i18,  i19,  i20,  i21,  i22,
166...   i23,  i24,  i25,  i26,  i27,  i28,  i29,  i30,  i31,  i32,  i33,
167...   i34,  i35,  i36,  i37,  i38,  i39,  i40,  i41,  i42,  i43,  i44,
168...   i45,  i46,  i47,  i48,  i49,  i50,  i51,  i52,  i53,  i54,  i55,
169...   i56,  i57,  i58,  i59,  i60,  i61,  i62,  i63,  i64,  i65,  i66,
170...   i67,  i68,  i69,  i70,  i71,  i72,  i73,  i74,  i75,  i76,  i77,
171...   i78,  i79,  i80,  i81,  i82,  i83,  i84,  i85,  i86,  i87,  i88,
172...   i89,  i90,  i91,  i92,  i93,  i94,  i95,  i96,  i97,  i98,  i99,
173...   i100,  i101,  i102,  i103,  i104,  i105,  i106,  i107,  i108,
174...   i109,  i110,  i111,  i112,  i113,  i114,  i115,  i116,  i117,
175...   i118,  i119,  i120,  i121,  i122,  i123,  i124,  i125,  i126,
176...   i127,  i128,  i129,  i130,  i131,  i132,  i133,  i134,  i135,
177...   i136,  i137,  i138,  i139,  i140,  i141,  i142,  i143,  i144,
178...   i145,  i146,  i147,  i148,  i149,  i150,  i151,  i152,  i153,
179...   i154,  i155,  i156,  i157,  i158,  i159,  i160,  i161,  i162,
180...   i163,  i164,  i165,  i166,  i167,  i168,  i169,  i170,  i171,
181...   i172,  i173,  i174,  i175,  i176,  i177,  i178,  i179,  i180,
182...   i181,  i182,  i183,  i184,  i185,  i186,  i187,  i188,  i189,
183...   i190,  i191,  i192,  i193,  i194,  i195,  i196,  i197,  i198,
184...   i199,  i200,  i201,  i202,  i203,  i204,  i205,  i206,  i207,
185...   i208,  i209,  i210,  i211,  i212,  i213,  i214,  i215,  i216,
186...   i217,  i218,  i219,  i220,  i221,  i222,  i223,  i224,  i225,
187...   i226,  i227,  i228,  i229,  i230,  i231,  i232,  i233,  i234,
188...   i235,  i236,  i237,  i238,  i239,  i240,  i241,  i242,  i243,
189...   i244,  i245,  i246,  i247,  i248,  i249,  i250,  i251,  i252,
190...   i253,  i254,  i255)
191Traceback (most recent call last):
192  File "<doctest test.test_syntax[25]>", line 1
193SyntaxError: more than 255 arguments
194
195The actual error cases counts positional arguments, keyword arguments,
196and generator expression arguments separately.  This test combines the
197three.
198
199>>> f(i0,  i1,  i2,  i3,  i4,  i5,  i6,  i7,  i8,  i9,  i10,  i11,
200...   i12,  i13,  i14,  i15,  i16,  i17,  i18,  i19,  i20,  i21,  i22,
201...   i23,  i24,  i25,  i26,  i27,  i28,  i29,  i30,  i31,  i32,  i33,
202...   i34,  i35,  i36,  i37,  i38,  i39,  i40,  i41,  i42,  i43,  i44,
203...   i45,  i46,  i47,  i48,  i49,  i50,  i51,  i52,  i53,  i54,  i55,
204...   i56,  i57,  i58,  i59,  i60,  i61,  i62,  i63,  i64,  i65,  i66,
205...   i67,  i68,  i69,  i70,  i71,  i72,  i73,  i74,  i75,  i76,  i77,
206...   i78,  i79,  i80,  i81,  i82,  i83,  i84,  i85,  i86,  i87,  i88,
207...   i89,  i90,  i91,  i92,  i93,  i94,  i95,  i96,  i97,  i98,  i99,
208...   i100,  i101,  i102,  i103,  i104,  i105,  i106,  i107,  i108,
209...   i109,  i110,  i111,  i112,  i113,  i114,  i115,  i116,  i117,
210...   i118,  i119,  i120,  i121,  i122,  i123,  i124,  i125,  i126,
211...   i127,  i128,  i129,  i130,  i131,  i132,  i133,  i134,  i135,
212...   i136,  i137,  i138,  i139,  i140,  i141,  i142,  i143,  i144,
213...   i145,  i146,  i147,  i148,  i149,  i150,  i151,  i152,  i153,
214...   i154,  i155,  i156,  i157,  i158,  i159,  i160,  i161,  i162,
215...   i163,  i164,  i165,  i166,  i167,  i168,  i169,  i170,  i171,
216...   i172,  i173,  i174,  i175,  i176,  i177,  i178,  i179,  i180,
217...   i181,  i182,  i183,  i184,  i185,  i186,  i187,  i188,  i189,
218...   i190,  i191,  i192,  i193,  i194,  i195,  i196,  i197,  i198,
219...   i199,  i200,  i201,  i202,  i203,  i204,  i205,  i206,  i207,
220...   i208,  i209,  i210,  i211,  i212,  i213,  i214,  i215,  i216,
221...   i217,  i218,  i219,  i220,  i221,  i222,  i223,  i224,  i225,
222...   i226,  i227,  i228,  i229,  i230,  i231,  i232,  i233,  i234,
223...   i235, i236,  i237,  i238,  i239,  i240,  i241,  i242,  i243,
224...   (x for x in i244),  i245,  i246,  i247,  i248,  i249,  i250,  i251,
225...    i252=1, i253=1,  i254=1,  i255=1)
226Traceback (most recent call last):
227  File "<doctest test.test_syntax[26]>", line 1
228SyntaxError: more than 255 arguments
229
230>>> f(lambda x: x[0] = 3)
231Traceback (most recent call last):
232  File "<doctest test.test_syntax[27]>", line 1
233SyntaxError: lambda cannot contain assignment
234
235The grammar accepts any test (basically, any expression) in the
236keyword slot of a call site.  Test a few different options.
237
238>>> f(x()=2)
239Traceback (most recent call last):
240  File "<doctest test.test_syntax[28]>", line 1
241SyntaxError: keyword can't be an expression
242>>> f(a or b=1)
243Traceback (most recent call last):
244  File "<doctest test.test_syntax[29]>", line 1
245SyntaxError: keyword can't be an expression
246>>> f(x.y=1)
247Traceback (most recent call last):
248  File "<doctest test.test_syntax[30]>", line 1
249SyntaxError: keyword can't be an expression
250
251
252More set_context():
253
254>>> (x for x in x) += 1
255Traceback (most recent call last):
256  File "<doctest test.test_syntax[31]>", line 1
257SyntaxError: can't assign to generator expression
258>>> None += 1
259Traceback (most recent call last):
260  File "<doctest test.test_syntax[32]>", line 1
261SyntaxError: cannot assign to None
262>>> f() += 1
263Traceback (most recent call last):
264  File "<doctest test.test_syntax[33]>", line 1
265SyntaxError: can't assign to function call
266
267
268Test continue in finally in weird combinations.
269
270continue in for loop under finally should be ok.
271
272    >>> def test():
273    ...     try:
274    ...         pass
275    ...     finally:
276    ...         for abc in range(10):
277    ...             continue
278    ...     print abc
279    >>> test()
280    9
281
282Start simple, a continue in a finally should not be allowed.
283
284    >>> def test():
285    ...    for abc in range(10):
286    ...        try:
287    ...            pass
288    ...        finally:
289    ...            continue
290    Traceback (most recent call last):
291      ...
292      File "<doctest test.test_syntax[36]>", line 6
293    SyntaxError: 'continue' not supported inside 'finally' clause
294
295This is essentially a continue in a finally which should not be allowed.
296
297    >>> def test():
298    ...    for abc in range(10):
299    ...        try:
300    ...            pass
301    ...        finally:
302    ...            try:
303    ...                continue
304    ...            except:
305    ...                pass
306    Traceback (most recent call last):
307      ...
308      File "<doctest test.test_syntax[37]>", line 6
309    SyntaxError: 'continue' not supported inside 'finally' clause
310
311    >>> def foo():
312    ...     try:
313    ...         pass
314    ...     finally:
315    ...         continue
316    Traceback (most recent call last):
317      ...
318      File "<doctest test.test_syntax[38]>", line 5
319    SyntaxError: 'continue' not supported inside 'finally' clause
320
321    >>> def foo():
322    ...     for a in ():
323    ...       try:
324    ...           pass
325    ...       finally:
326    ...           continue
327    Traceback (most recent call last):
328      ...
329      File "<doctest test.test_syntax[39]>", line 6
330    SyntaxError: 'continue' not supported inside 'finally' clause
331
332    >>> def foo():
333    ...     for a in ():
334    ...         try:
335    ...             pass
336    ...         finally:
337    ...             try:
338    ...                 continue
339    ...             finally:
340    ...                 pass
341    Traceback (most recent call last):
342      ...
343      File "<doctest test.test_syntax[40]>", line 7
344    SyntaxError: 'continue' not supported inside 'finally' clause
345
346    >>> def foo():
347    ...  for a in ():
348    ...   try: pass
349    ...   finally:
350    ...    try:
351    ...     pass
352    ...    except:
353    ...     continue
354    Traceback (most recent call last):
355      ...
356      File "<doctest test.test_syntax[41]>", line 8
357    SyntaxError: 'continue' not supported inside 'finally' clause
358
359There is one test for a break that is not in a loop.  The compiler
360uses a single data structure to keep track of try-finally and loops,
361so we need to be sure that a break is actually inside a loop.  If it
362isn't, there should be a syntax error.
363
364   >>> try:
365   ...     print 1
366   ...     break
367   ...     print 2
368   ... finally:
369   ...     print 3
370   Traceback (most recent call last):
371     ...
372     File "<doctest test.test_syntax[42]>", line 3
373   SyntaxError: 'break' outside loop
374
375This should probably raise a better error than a SystemError (or none at all).
376In 2.5 there was a missing exception and an assert was triggered in a debug
377build.  The number of blocks must be greater than CO_MAXBLOCKS.  SF #1565514
378
379   >>> while 1: # doctest:+SKIP
380   ...  while 2:
381   ...   while 3:
382   ...    while 4:
383   ...     while 5:
384   ...      while 6:
385   ...       while 8:
386   ...        while 9:
387   ...         while 10:
388   ...          while 11:
389   ...           while 12:
390   ...            while 13:
391   ...             while 14:
392   ...              while 15:
393   ...               while 16:
394   ...                while 17:
395   ...                 while 18:
396   ...                  while 19:
397   ...                   while 20:
398   ...                    while 21:
399   ...                     while 22:
400   ...                      break
401   Traceback (most recent call last):
402     ...
403   SystemError: too many statically nested blocks
404
405This tests assignment-context; there was a bug in Python 2.5 where compiling
406a complex 'if' (one with 'elif') would fail to notice an invalid suite,
407leading to spurious errors.
408
409   >>> if 1:
410   ...   x() = 1
411   ... elif 1:
412   ...   pass
413   Traceback (most recent call last):
414     ...
415     File "<doctest test.test_syntax[44]>", line 2
416   SyntaxError: can't assign to function call
417
418   >>> if 1:
419   ...   pass
420   ... elif 1:
421   ...   x() = 1
422   Traceback (most recent call last):
423     ...
424     File "<doctest test.test_syntax[45]>", line 4
425   SyntaxError: can't assign to function call
426
427   >>> if 1:
428   ...   x() = 1
429   ... elif 1:
430   ...   pass
431   ... else:
432   ...   pass
433   Traceback (most recent call last):
434     ...
435     File "<doctest test.test_syntax[46]>", line 2
436   SyntaxError: can't assign to function call
437
438   >>> if 1:
439   ...   pass
440   ... elif 1:
441   ...   x() = 1
442   ... else:
443   ...   pass
444   Traceback (most recent call last):
445     ...
446     File "<doctest test.test_syntax[47]>", line 4
447   SyntaxError: can't assign to function call
448
449   >>> if 1:
450   ...   pass
451   ... elif 1:
452   ...   pass
453   ... else:
454   ...   x() = 1
455   Traceback (most recent call last):
456     ...
457     File "<doctest test.test_syntax[48]>", line 6
458   SyntaxError: can't assign to function call
459
460>>> f(a=23, a=234)
461Traceback (most recent call last):
462   ...
463  File "<doctest test.test_syntax[49]>", line 1
464SyntaxError: keyword argument repeated
465
466>>> del ()
467Traceback (most recent call last):
468   ...
469  File "<doctest test.test_syntax[50]>", line 1
470SyntaxError: can't delete ()
471
472>>> {1, 2, 3} = 42
473Traceback (most recent call last):
474   ...
475   File "<doctest test.test_syntax[50]>", line 1
476SyntaxError: can't assign to literal
477
478Corner-case that used to crash:
479
480    >>> def f(*xx, **__debug__): pass
481    Traceback (most recent call last):
482    SyntaxError: cannot assign to __debug__
483
484"""
485
486import re
487import unittest
488import warnings
489
490from test import test_support
491
492class SyntaxTestCase(unittest.TestCase):
493
494    def _check_error(self, code, errtext,
495                     filename="<testcase>", mode="exec", subclass=None):
496        """Check that compiling code raises SyntaxError with errtext.
497
498        errtest is a regular expression that must be present in the
499        test of the exception raised.  If subclass is specified it
500        is the expected subclass of SyntaxError (e.g. IndentationError).
501        """
502        try:
503            compile(code, filename, mode)
504        except SyntaxError, err:
505            if subclass and not isinstance(err, subclass):
506                self.fail("SyntaxError is not a %s" % subclass.__name__)
507            mo = re.search(errtext, str(err))
508            if mo is None:
509                self.fail("%s did not contain '%r'" % (err, errtext,))
510        else:
511            self.fail("compile() did not raise SyntaxError")
512
513    def test_paren_arg_with_default(self):
514        self._check_error("def f((x)=23): pass",
515                          "parenthesized arg with default")
516
517    def test_assign_call(self):
518        self._check_error("f() = 1", "assign")
519
520    def test_assign_del(self):
521        self._check_error("del f()", "delete")
522
523    def test_global_err_then_warn(self):
524        # Bug tickler:  The SyntaxError raised for one global statement
525        # shouldn't be clobbered by a SyntaxWarning issued for a later one.
526        source = re.sub('(?m)^ *:', '', """\
527            :def error(a):
528            :    global a  # SyntaxError
529            :def warning():
530            :    b = 1
531            :    global b  # SyntaxWarning
532            :""")
533        warnings.filterwarnings(action='ignore', category=SyntaxWarning)
534        self._check_error(source, "global")
535        warnings.filters.pop(0)
536
537    def test_break_outside_loop(self):
538        self._check_error("break", "outside loop")
539
540    def test_delete_deref(self):
541        source = re.sub('(?m)^ *:', '', """\
542            :def foo(x):
543            :  def bar():
544            :    print x
545            :  del x
546            :""")
547        self._check_error(source, "nested scope")
548
549    def test_unexpected_indent(self):
550        self._check_error("foo()\n bar()\n", "unexpected indent",
551                          subclass=IndentationError)
552
553    def test_no_indent(self):
554        self._check_error("if 1:\nfoo()", "expected an indented block",
555                          subclass=IndentationError)
556
557    def test_bad_outdent(self):
558        self._check_error("if 1:\n  foo()\n bar()",
559                          "unindent does not match .* level",
560                          subclass=IndentationError)
561
562    def test_kwargs_last(self):
563        self._check_error("int(base=10, '2')", "non-keyword arg")
564
565def test_main():
566    test_support.run_unittest(SyntaxTestCase)
567    from test import test_syntax
568    with test_support.check_py3k_warnings(("backquote not supported",
569                                             SyntaxWarning)):
570        test_support.run_doctest(test_syntax, verbosity=True)
571
572if __name__ == "__main__":
573    test_main()