test_syntax.py 18 KB

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