PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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