+++ /dev/null
-"""This module tests SyntaxErrors.\r
-\r
-Here's an example of the sort of thing that is tested.\r
-\r
->>> def f(x):\r
-... global x\r
-Traceback (most recent call last):\r
-SyntaxError: name 'x' is local and global (<doctest test.test_syntax[0]>, line 1)\r
-\r
-The tests are all raise SyntaxErrors. They were created by checking\r
-each C call that raises SyntaxError. There are several modules that\r
-raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and\r
-symtable.c.\r
-\r
-The parser itself outlaws a lot of invalid syntax. None of these\r
-errors are tested here at the moment. We should add some tests; since\r
-there are infinitely many programs with invalid syntax, we would need\r
-to be judicious in selecting some.\r
-\r
-The compiler generates a synthetic module name for code executed by\r
-doctest. Since all the code comes from the same module, a suffix like\r
-[1] is appended to the module name, As a consequence, changing the\r
-order of tests in this module means renumbering all the errors after\r
-it. (Maybe we should enable the ellipsis option for these tests.)\r
-\r
-In ast.c, syntax errors are raised by calling ast_error().\r
-\r
-Errors from set_context():\r
-\r
->>> obj.None = 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[1]>", line 1\r
-SyntaxError: cannot assign to None\r
-\r
->>> None = 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[2]>", line 1\r
-SyntaxError: cannot assign to None\r
-\r
-It's a syntax error to assign to the empty tuple. Why isn't it an\r
-error to assign to the empty list? It will always raise some error at\r
-runtime.\r
-\r
->>> () = 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[3]>", line 1\r
-SyntaxError: can't assign to ()\r
-\r
->>> f() = 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[4]>", line 1\r
-SyntaxError: can't assign to function call\r
-\r
->>> del f()\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[5]>", line 1\r
-SyntaxError: can't delete function call\r
-\r
->>> a + 1 = 2\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[6]>", line 1\r
-SyntaxError: can't assign to operator\r
-\r
->>> (x for x in x) = 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[7]>", line 1\r
-SyntaxError: can't assign to generator expression\r
-\r
->>> 1 = 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[8]>", line 1\r
-SyntaxError: can't assign to literal\r
-\r
->>> "abc" = 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[8]>", line 1\r
-SyntaxError: can't assign to literal\r
-\r
->>> `1` = 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[10]>", line 1\r
-SyntaxError: can't assign to repr\r
-\r
-If the left-hand side of an assignment is a list or tuple, an illegal\r
-expression inside that contain should still cause a syntax error.\r
-This test just checks a couple of cases rather than enumerating all of\r
-them.\r
-\r
->>> (a, "b", c) = (1, 2, 3)\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[11]>", line 1\r
-SyntaxError: can't assign to literal\r
-\r
->>> [a, b, c + 1] = [1, 2, 3]\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[12]>", line 1\r
-SyntaxError: can't assign to operator\r
-\r
->>> a if 1 else b = 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[13]>", line 1\r
-SyntaxError: can't assign to conditional expression\r
-\r
-From compiler_complex_args():\r
-\r
->>> def f(None=1):\r
-... pass\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[14]>", line 1\r
-SyntaxError: cannot assign to None\r
-\r
-\r
-From ast_for_arguments():\r
-\r
->>> def f(x, y=1, z):\r
-... pass\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[15]>", line 1\r
-SyntaxError: non-default argument follows default argument\r
-\r
->>> def f(x, None):\r
-... pass\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[16]>", line 1\r
-SyntaxError: cannot assign to None\r
-\r
->>> def f(*None):\r
-... pass\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[17]>", line 1\r
-SyntaxError: cannot assign to None\r
-\r
->>> def f(**None):\r
-... pass\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[18]>", line 1\r
-SyntaxError: cannot assign to None\r
-\r
-\r
-From ast_for_funcdef():\r
-\r
->>> def None(x):\r
-... pass\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[19]>", line 1\r
-SyntaxError: cannot assign to None\r
-\r
-\r
-From ast_for_call():\r
-\r
->>> def f(it, *varargs):\r
-... return list(it)\r
->>> L = range(10)\r
->>> f(x for x in L)\r
-[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\r
->>> f(x for x in L, 1)\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[23]>", line 1\r
-SyntaxError: Generator expression must be parenthesized if not sole argument\r
->>> f((x for x in L), 1)\r
-[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\r
-\r
->>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,\r
-... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,\r
-... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,\r
-... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,\r
-... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,\r
-... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,\r
-... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,\r
-... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,\r
-... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,\r
-... i100, i101, i102, i103, i104, i105, i106, i107, i108,\r
-... i109, i110, i111, i112, i113, i114, i115, i116, i117,\r
-... i118, i119, i120, i121, i122, i123, i124, i125, i126,\r
-... i127, i128, i129, i130, i131, i132, i133, i134, i135,\r
-... i136, i137, i138, i139, i140, i141, i142, i143, i144,\r
-... i145, i146, i147, i148, i149, i150, i151, i152, i153,\r
-... i154, i155, i156, i157, i158, i159, i160, i161, i162,\r
-... i163, i164, i165, i166, i167, i168, i169, i170, i171,\r
-... i172, i173, i174, i175, i176, i177, i178, i179, i180,\r
-... i181, i182, i183, i184, i185, i186, i187, i188, i189,\r
-... i190, i191, i192, i193, i194, i195, i196, i197, i198,\r
-... i199, i200, i201, i202, i203, i204, i205, i206, i207,\r
-... i208, i209, i210, i211, i212, i213, i214, i215, i216,\r
-... i217, i218, i219, i220, i221, i222, i223, i224, i225,\r
-... i226, i227, i228, i229, i230, i231, i232, i233, i234,\r
-... i235, i236, i237, i238, i239, i240, i241, i242, i243,\r
-... i244, i245, i246, i247, i248, i249, i250, i251, i252,\r
-... i253, i254, i255)\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[25]>", line 1\r
-SyntaxError: more than 255 arguments\r
-\r
-The actual error cases counts positional arguments, keyword arguments,\r
-and generator expression arguments separately. This test combines the\r
-three.\r
-\r
->>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,\r
-... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,\r
-... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,\r
-... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,\r
-... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,\r
-... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,\r
-... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,\r
-... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,\r
-... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,\r
-... i100, i101, i102, i103, i104, i105, i106, i107, i108,\r
-... i109, i110, i111, i112, i113, i114, i115, i116, i117,\r
-... i118, i119, i120, i121, i122, i123, i124, i125, i126,\r
-... i127, i128, i129, i130, i131, i132, i133, i134, i135,\r
-... i136, i137, i138, i139, i140, i141, i142, i143, i144,\r
-... i145, i146, i147, i148, i149, i150, i151, i152, i153,\r
-... i154, i155, i156, i157, i158, i159, i160, i161, i162,\r
-... i163, i164, i165, i166, i167, i168, i169, i170, i171,\r
-... i172, i173, i174, i175, i176, i177, i178, i179, i180,\r
-... i181, i182, i183, i184, i185, i186, i187, i188, i189,\r
-... i190, i191, i192, i193, i194, i195, i196, i197, i198,\r
-... i199, i200, i201, i202, i203, i204, i205, i206, i207,\r
-... i208, i209, i210, i211, i212, i213, i214, i215, i216,\r
-... i217, i218, i219, i220, i221, i222, i223, i224, i225,\r
-... i226, i227, i228, i229, i230, i231, i232, i233, i234,\r
-... i235, i236, i237, i238, i239, i240, i241, i242, i243,\r
-... (x for x in i244), i245, i246, i247, i248, i249, i250, i251,\r
-... i252=1, i253=1, i254=1, i255=1)\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[26]>", line 1\r
-SyntaxError: more than 255 arguments\r
-\r
->>> f(lambda x: x[0] = 3)\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[27]>", line 1\r
-SyntaxError: lambda cannot contain assignment\r
-\r
-The grammar accepts any test (basically, any expression) in the\r
-keyword slot of a call site. Test a few different options.\r
-\r
->>> f(x()=2)\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[28]>", line 1\r
-SyntaxError: keyword can't be an expression\r
->>> f(a or b=1)\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[29]>", line 1\r
-SyntaxError: keyword can't be an expression\r
->>> f(x.y=1)\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[30]>", line 1\r
-SyntaxError: keyword can't be an expression\r
-\r
-\r
-More set_context():\r
-\r
->>> (x for x in x) += 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[31]>", line 1\r
-SyntaxError: can't assign to generator expression\r
->>> None += 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[32]>", line 1\r
-SyntaxError: cannot assign to None\r
->>> f() += 1\r
-Traceback (most recent call last):\r
- File "<doctest test.test_syntax[33]>", line 1\r
-SyntaxError: can't assign to function call\r
-\r
-\r
-Test continue in finally in weird combinations.\r
-\r
-continue in for loop under finally should be ok.\r
-\r
- >>> def test():\r
- ... try:\r
- ... pass\r
- ... finally:\r
- ... for abc in range(10):\r
- ... continue\r
- ... print abc\r
- >>> test()\r
- 9\r
-\r
-Start simple, a continue in a finally should not be allowed.\r
-\r
- >>> def test():\r
- ... for abc in range(10):\r
- ... try:\r
- ... pass\r
- ... finally:\r
- ... continue\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[36]>", line 6\r
- SyntaxError: 'continue' not supported inside 'finally' clause\r
-\r
-This is essentially a continue in a finally which should not be allowed.\r
-\r
- >>> def test():\r
- ... for abc in range(10):\r
- ... try:\r
- ... pass\r
- ... finally:\r
- ... try:\r
- ... continue\r
- ... except:\r
- ... pass\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[37]>", line 6\r
- SyntaxError: 'continue' not supported inside 'finally' clause\r
-\r
- >>> def foo():\r
- ... try:\r
- ... pass\r
- ... finally:\r
- ... continue\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[38]>", line 5\r
- SyntaxError: 'continue' not supported inside 'finally' clause\r
-\r
- >>> def foo():\r
- ... for a in ():\r
- ... try:\r
- ... pass\r
- ... finally:\r
- ... continue\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[39]>", line 6\r
- SyntaxError: 'continue' not supported inside 'finally' clause\r
-\r
- >>> def foo():\r
- ... for a in ():\r
- ... try:\r
- ... pass\r
- ... finally:\r
- ... try:\r
- ... continue\r
- ... finally:\r
- ... pass\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[40]>", line 7\r
- SyntaxError: 'continue' not supported inside 'finally' clause\r
-\r
- >>> def foo():\r
- ... for a in ():\r
- ... try: pass\r
- ... finally:\r
- ... try:\r
- ... pass\r
- ... except:\r
- ... continue\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[41]>", line 8\r
- SyntaxError: 'continue' not supported inside 'finally' clause\r
-\r
-There is one test for a break that is not in a loop. The compiler\r
-uses a single data structure to keep track of try-finally and loops,\r
-so we need to be sure that a break is actually inside a loop. If it\r
-isn't, there should be a syntax error.\r
-\r
- >>> try:\r
- ... print 1\r
- ... break\r
- ... print 2\r
- ... finally:\r
- ... print 3\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[42]>", line 3\r
- SyntaxError: 'break' outside loop\r
-\r
-This should probably raise a better error than a SystemError (or none at all).\r
-In 2.5 there was a missing exception and an assert was triggered in a debug\r
-build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514\r
-\r
- >>> while 1:\r
- ... while 2:\r
- ... while 3:\r
- ... while 4:\r
- ... while 5:\r
- ... while 6:\r
- ... while 8:\r
- ... while 9:\r
- ... while 10:\r
- ... while 11:\r
- ... while 12:\r
- ... while 13:\r
- ... while 14:\r
- ... while 15:\r
- ... while 16:\r
- ... while 17:\r
- ... while 18:\r
- ... while 19:\r
- ... while 20:\r
- ... while 21:\r
- ... while 22:\r
- ... break\r
- Traceback (most recent call last):\r
- ...\r
- SystemError: too many statically nested blocks\r
-\r
-This tests assignment-context; there was a bug in Python 2.5 where compiling\r
-a complex 'if' (one with 'elif') would fail to notice an invalid suite,\r
-leading to spurious errors.\r
-\r
- >>> if 1:\r
- ... x() = 1\r
- ... elif 1:\r
- ... pass\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[44]>", line 2\r
- SyntaxError: can't assign to function call\r
-\r
- >>> if 1:\r
- ... pass\r
- ... elif 1:\r
- ... x() = 1\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[45]>", line 4\r
- SyntaxError: can't assign to function call\r
-\r
- >>> if 1:\r
- ... x() = 1\r
- ... elif 1:\r
- ... pass\r
- ... else:\r
- ... pass\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[46]>", line 2\r
- SyntaxError: can't assign to function call\r
-\r
- >>> if 1:\r
- ... pass\r
- ... elif 1:\r
- ... x() = 1\r
- ... else:\r
- ... pass\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[47]>", line 4\r
- SyntaxError: can't assign to function call\r
-\r
- >>> if 1:\r
- ... pass\r
- ... elif 1:\r
- ... pass\r
- ... else:\r
- ... x() = 1\r
- Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[48]>", line 6\r
- SyntaxError: can't assign to function call\r
-\r
->>> f(a=23, a=234)\r
-Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[49]>", line 1\r
-SyntaxError: keyword argument repeated\r
-\r
->>> del ()\r
-Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[50]>", line 1\r
-SyntaxError: can't delete ()\r
-\r
->>> {1, 2, 3} = 42\r
-Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_syntax[50]>", line 1\r
-SyntaxError: can't assign to literal\r
-\r
-Corner-case that used to crash:\r
-\r
- >>> def f(*xx, **__debug__): pass\r
- Traceback (most recent call last):\r
- SyntaxError: cannot assign to __debug__\r
-\r
-"""\r
-\r
-import re\r
-import unittest\r
-import warnings\r
-\r
-from test import test_support\r
-\r
-class SyntaxTestCase(unittest.TestCase):\r
-\r
- def _check_error(self, code, errtext,\r
- filename="<testcase>", mode="exec", subclass=None):\r
- """Check that compiling code raises SyntaxError with errtext.\r
-\r
- errtest is a regular expression that must be present in the\r
- test of the exception raised. If subclass is specified it\r
- is the expected subclass of SyntaxError (e.g. IndentationError).\r
- """\r
- try:\r
- compile(code, filename, mode)\r
- except SyntaxError, err:\r
- if subclass and not isinstance(err, subclass):\r
- self.fail("SyntaxError is not a %s" % subclass.__name__)\r
- mo = re.search(errtext, str(err))\r
- if mo is None:\r
- self.fail("%s did not contain '%r'" % (err, errtext,))\r
- else:\r
- self.fail("compile() did not raise SyntaxError")\r
-\r
- def test_paren_arg_with_default(self):\r
- self._check_error("def f((x)=23): pass",\r
- "parenthesized arg with default")\r
-\r
- def test_assign_call(self):\r
- self._check_error("f() = 1", "assign")\r
-\r
- def test_assign_del(self):\r
- self._check_error("del f()", "delete")\r
-\r
- def test_global_err_then_warn(self):\r
- # Bug tickler: The SyntaxError raised for one global statement\r
- # shouldn't be clobbered by a SyntaxWarning issued for a later one.\r
- source = re.sub('(?m)^ *:', '', """\\r
- :def error(a):\r
- : global a # SyntaxError\r
- :def warning():\r
- : b = 1\r
- : global b # SyntaxWarning\r
- :""")\r
- warnings.filterwarnings(action='ignore', category=SyntaxWarning)\r
- self._check_error(source, "global")\r
- warnings.filters.pop(0)\r
-\r
- def test_break_outside_loop(self):\r
- self._check_error("break", "outside loop")\r
-\r
- def test_delete_deref(self):\r
- source = re.sub('(?m)^ *:', '', """\\r
- :def foo(x):\r
- : def bar():\r
- : print x\r
- : del x\r
- :""")\r
- self._check_error(source, "nested scope")\r
-\r
- def test_unexpected_indent(self):\r
- self._check_error("foo()\n bar()\n", "unexpected indent",\r
- subclass=IndentationError)\r
-\r
- def test_no_indent(self):\r
- self._check_error("if 1:\nfoo()", "expected an indented block",\r
- subclass=IndentationError)\r
-\r
- def test_bad_outdent(self):\r
- self._check_error("if 1:\n foo()\n bar()",\r
- "unindent does not match .* level",\r
- subclass=IndentationError)\r
-\r
- def test_kwargs_last(self):\r
- self._check_error("int(base=10, '2')", "non-keyword arg")\r
-\r
-def test_main():\r
- test_support.run_unittest(SyntaxTestCase)\r
- from test import test_syntax\r
- with test_support.check_py3k_warnings(("backquote not supported",\r
- SyntaxWarning)):\r
- test_support.run_doctest(test_syntax, verbosity=True)\r
-\r
-if __name__ == "__main__":\r
- test_main()\r