test_parser.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. import parser
  2. import unittest
  3. import sys
  4. import struct
  5. from test import test_support as support
  6. from test.script_helper import assert_python_failure
  7. #
  8. # First, we test that we can generate trees from valid source fragments,
  9. # and that these valid trees are indeed allowed by the tree-loading side
  10. # of the parser module.
  11. #
  12. class RoundtripLegalSyntaxTestCase(unittest.TestCase):
  13. def roundtrip(self, f, s):
  14. st1 = f(s)
  15. t = st1.totuple()
  16. try:
  17. st2 = parser.sequence2st(t)
  18. except parser.ParserError, why:
  19. self.fail("could not roundtrip %r: %s" % (s, why))
  20. self.assertEqual(t, st2.totuple(),
  21. "could not re-generate syntax tree")
  22. def check_expr(self, s):
  23. self.roundtrip(parser.expr, s)
  24. def test_flags_passed(self):
  25. # The unicode literals flags has to be passed from the paser to AST
  26. # generation.
  27. suite = parser.suite("from __future__ import unicode_literals; x = ''")
  28. code = suite.compile()
  29. scope = {}
  30. exec code in scope
  31. self.assertIsInstance(scope["x"], unicode)
  32. def check_suite(self, s):
  33. self.roundtrip(parser.suite, s)
  34. def test_yield_statement(self):
  35. self.check_suite("def f(): yield 1")
  36. self.check_suite("def f(): yield")
  37. self.check_suite("def f(): x += yield")
  38. self.check_suite("def f(): x = yield 1")
  39. self.check_suite("def f(): x = y = yield 1")
  40. self.check_suite("def f(): x = yield")
  41. self.check_suite("def f(): x = y = yield")
  42. self.check_suite("def f(): 1 + (yield)*2")
  43. self.check_suite("def f(): (yield 1)*2")
  44. self.check_suite("def f(): return; yield 1")
  45. self.check_suite("def f(): yield 1; return")
  46. self.check_suite("def f():\n"
  47. " for x in range(30):\n"
  48. " yield x\n")
  49. self.check_suite("def f():\n"
  50. " if (yield):\n"
  51. " yield x\n")
  52. def test_expressions(self):
  53. self.check_expr("foo(1)")
  54. self.check_expr("{1:1}")
  55. self.check_expr("{1:1, 2:2, 3:3}")
  56. self.check_expr("{1:1, 2:2, 3:3,}")
  57. self.check_expr("{1}")
  58. self.check_expr("{1, 2, 3}")
  59. self.check_expr("{1, 2, 3,}")
  60. self.check_expr("[]")
  61. self.check_expr("[1]")
  62. self.check_expr("[1, 2, 3]")
  63. self.check_expr("[1, 2, 3,]")
  64. self.check_expr("()")
  65. self.check_expr("(1,)")
  66. self.check_expr("(1, 2, 3)")
  67. self.check_expr("(1, 2, 3,)")
  68. self.check_expr("[x**3 for x in range(20)]")
  69. self.check_expr("[x**3 for x in range(20) if x % 3]")
  70. self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
  71. self.check_expr("[x+y for x in range(30) for y in range(20) if x % 2 if y % 3]")
  72. #self.check_expr("[x for x in lambda: True, lambda: False if x()]")
  73. self.check_expr("list(x**3 for x in range(20))")
  74. self.check_expr("list(x**3 for x in range(20) if x % 3)")
  75. self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
  76. self.check_expr("list(x+y for x in range(30) for y in range(20) if x % 2 if y % 3)")
  77. self.check_expr("{x**3 for x in range(30)}")
  78. self.check_expr("{x**3 for x in range(30) if x % 3}")
  79. self.check_expr("{x**3 for x in range(30) if x % 2 if x % 3}")
  80. self.check_expr("{x+y for x in range(30) for y in range(20) if x % 2 if y % 3}")
  81. self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30))}")
  82. self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30)) if x % 3}")
  83. self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30)) if x % 3 if y % 3}")
  84. self.check_expr("{x:y for x in range(30) for y in range(20) if x % 2 if y % 3}")
  85. self.check_expr("foo(*args)")
  86. self.check_expr("foo(*args, **kw)")
  87. self.check_expr("foo(**kw)")
  88. self.check_expr("foo(key=value)")
  89. self.check_expr("foo(key=value, *args)")
  90. self.check_expr("foo(key=value, *args, **kw)")
  91. self.check_expr("foo(key=value, **kw)")
  92. self.check_expr("foo(a, b, c, *args)")
  93. self.check_expr("foo(a, b, c, *args, **kw)")
  94. self.check_expr("foo(a, b, c, **kw)")
  95. self.check_expr("foo(a, *args, keyword=23)")
  96. self.check_expr("foo + bar")
  97. self.check_expr("foo - bar")
  98. self.check_expr("foo * bar")
  99. self.check_expr("foo / bar")
  100. self.check_expr("foo // bar")
  101. self.check_expr("lambda: 0")
  102. self.check_expr("lambda x: 0")
  103. self.check_expr("lambda *y: 0")
  104. self.check_expr("lambda *y, **z: 0")
  105. self.check_expr("lambda **z: 0")
  106. self.check_expr("lambda x, y: 0")
  107. self.check_expr("lambda foo=bar: 0")
  108. self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
  109. self.check_expr("lambda foo=bar, **z: 0")
  110. self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
  111. self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
  112. self.check_expr("lambda x, *y, **z: 0")
  113. self.check_expr("lambda x: 5 if x else 2")
  114. self.check_expr("(x for x in range(10))")
  115. self.check_expr("foo(x for x in range(10))")
  116. def test_print(self):
  117. self.check_suite("print")
  118. self.check_suite("print 1")
  119. self.check_suite("print 1,")
  120. self.check_suite("print >>fp")
  121. self.check_suite("print >>fp, 1")
  122. self.check_suite("print >>fp, 1,")
  123. def test_simple_expression(self):
  124. # expr_stmt
  125. self.check_suite("a")
  126. def test_simple_assignments(self):
  127. self.check_suite("a = b")
  128. self.check_suite("a = b = c = d = e")
  129. def test_simple_augmented_assignments(self):
  130. self.check_suite("a += b")
  131. self.check_suite("a -= b")
  132. self.check_suite("a *= b")
  133. self.check_suite("a /= b")
  134. self.check_suite("a //= b")
  135. self.check_suite("a %= b")
  136. self.check_suite("a &= b")
  137. self.check_suite("a |= b")
  138. self.check_suite("a ^= b")
  139. self.check_suite("a <<= b")
  140. self.check_suite("a >>= b")
  141. self.check_suite("a **= b")
  142. def test_function_defs(self):
  143. self.check_suite("def f(): pass")
  144. self.check_suite("def f(*args): pass")
  145. self.check_suite("def f(*args, **kw): pass")
  146. self.check_suite("def f(**kw): pass")
  147. self.check_suite("def f(foo=bar): pass")
  148. self.check_suite("def f(foo=bar, *args): pass")
  149. self.check_suite("def f(foo=bar, *args, **kw): pass")
  150. self.check_suite("def f(foo=bar, **kw): pass")
  151. self.check_suite("def f(a, b): pass")
  152. self.check_suite("def f(a, b, *args): pass")
  153. self.check_suite("def f(a, b, *args, **kw): pass")
  154. self.check_suite("def f(a, b, **kw): pass")
  155. self.check_suite("def f(a, b, foo=bar): pass")
  156. self.check_suite("def f(a, b, foo=bar, *args): pass")
  157. self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
  158. self.check_suite("def f(a, b, foo=bar, **kw): pass")
  159. self.check_suite("@staticmethod\n"
  160. "def f(): pass")
  161. self.check_suite("@staticmethod\n"
  162. "@funcattrs(x, y)\n"
  163. "def f(): pass")
  164. self.check_suite("@funcattrs()\n"
  165. "def f(): pass")
  166. def test_class_defs(self):
  167. self.check_suite("class foo():pass")
  168. self.check_suite("@class_decorator\n"
  169. "class foo():pass")
  170. self.check_suite("@class_decorator(arg)\n"
  171. "class foo():pass")
  172. self.check_suite("@decorator1\n"
  173. "@decorator2\n"
  174. "class foo():pass")
  175. def test_import_from_statement(self):
  176. self.check_suite("from sys.path import *")
  177. self.check_suite("from sys.path import dirname")
  178. self.check_suite("from sys.path import (dirname)")
  179. self.check_suite("from sys.path import (dirname,)")
  180. self.check_suite("from sys.path import dirname as my_dirname")
  181. self.check_suite("from sys.path import (dirname as my_dirname)")
  182. self.check_suite("from sys.path import (dirname as my_dirname,)")
  183. self.check_suite("from sys.path import dirname, basename")
  184. self.check_suite("from sys.path import (dirname, basename)")
  185. self.check_suite("from sys.path import (dirname, basename,)")
  186. self.check_suite(
  187. "from sys.path import dirname as my_dirname, basename")
  188. self.check_suite(
  189. "from sys.path import (dirname as my_dirname, basename)")
  190. self.check_suite(
  191. "from sys.path import (dirname as my_dirname, basename,)")
  192. self.check_suite(
  193. "from sys.path import dirname, basename as my_basename")
  194. self.check_suite(
  195. "from sys.path import (dirname, basename as my_basename)")
  196. self.check_suite(
  197. "from sys.path import (dirname, basename as my_basename,)")
  198. self.check_suite("from .bogus import x")
  199. def test_basic_import_statement(self):
  200. self.check_suite("import sys")
  201. self.check_suite("import sys as system")
  202. self.check_suite("import sys, math")
  203. self.check_suite("import sys as system, math")
  204. self.check_suite("import sys, math as my_math")
  205. def test_relative_imports(self):
  206. self.check_suite("from . import name")
  207. self.check_suite("from .. import name")
  208. self.check_suite("from .pkg import name")
  209. self.check_suite("from ..pkg import name")
  210. def test_pep263(self):
  211. self.check_suite("# -*- coding: iso-8859-1 -*-\n"
  212. "pass\n")
  213. def test_assert(self):
  214. self.check_suite("assert alo < ahi and blo < bhi\n")
  215. def test_with(self):
  216. self.check_suite("with open('x'): pass\n")
  217. self.check_suite("with open('x') as f: pass\n")
  218. self.check_suite("with open('x') as f, open('y') as g: pass\n")
  219. def test_try_stmt(self):
  220. self.check_suite("try: pass\nexcept: pass\n")
  221. self.check_suite("try: pass\nfinally: pass\n")
  222. self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
  223. self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
  224. "finally: pass\n")
  225. self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
  226. self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
  227. "finally: pass\n")
  228. def test_except_clause(self):
  229. self.check_suite("try: pass\nexcept: pass\n")
  230. self.check_suite("try: pass\nexcept A: pass\n")
  231. self.check_suite("try: pass\nexcept A, e: pass\n")
  232. self.check_suite("try: pass\nexcept A as e: pass\n")
  233. def test_position(self):
  234. # An absolutely minimal test of position information. Better
  235. # tests would be a big project.
  236. code = "def f(x):\n return x + 1"
  237. st1 = parser.suite(code)
  238. st2 = st1.totuple(line_info=1, col_info=1)
  239. def walk(tree):
  240. node_type = tree[0]
  241. next = tree[1]
  242. if isinstance(next, tuple):
  243. for elt in tree[1:]:
  244. for x in walk(elt):
  245. yield x
  246. else:
  247. yield tree
  248. terminals = list(walk(st2))
  249. self.assertEqual([
  250. (1, 'def', 1, 0),
  251. (1, 'f', 1, 4),
  252. (7, '(', 1, 5),
  253. (1, 'x', 1, 6),
  254. (8, ')', 1, 7),
  255. (11, ':', 1, 8),
  256. (4, '', 1, 9),
  257. (5, '', 2, -1),
  258. (1, 'return', 2, 4),
  259. (1, 'x', 2, 11),
  260. (14, '+', 2, 13),
  261. (2, '1', 2, 15),
  262. (4, '', 2, 16),
  263. (6, '', 2, -1),
  264. (4, '', 2, -1),
  265. (0, '', 2, -1)],
  266. terminals)
  267. #
  268. # Second, we take *invalid* trees and make sure we get ParserError
  269. # rejections for them.
  270. #
  271. class IllegalSyntaxTestCase(unittest.TestCase):
  272. def check_bad_tree(self, tree, label):
  273. try:
  274. parser.sequence2st(tree)
  275. except parser.ParserError:
  276. pass
  277. else:
  278. self.fail("did not detect invalid tree for %r" % label)
  279. def test_junk(self):
  280. # not even remotely valid:
  281. self.check_bad_tree((1, 2, 3), "<junk>")
  282. def test_illegal_yield_1(self):
  283. # Illegal yield statement: def f(): return 1; yield 1
  284. tree = \
  285. (257,
  286. (264,
  287. (285,
  288. (259,
  289. (1, 'def'),
  290. (1, 'f'),
  291. (260, (7, '('), (8, ')')),
  292. (11, ':'),
  293. (291,
  294. (4, ''),
  295. (5, ''),
  296. (264,
  297. (265,
  298. (266,
  299. (272,
  300. (275,
  301. (1, 'return'),
  302. (313,
  303. (292,
  304. (293,
  305. (294,
  306. (295,
  307. (297,
  308. (298,
  309. (299,
  310. (300,
  311. (301,
  312. (302, (303, (304, (305, (2, '1')))))))))))))))))),
  313. (264,
  314. (265,
  315. (266,
  316. (272,
  317. (276,
  318. (1, 'yield'),
  319. (313,
  320. (292,
  321. (293,
  322. (294,
  323. (295,
  324. (297,
  325. (298,
  326. (299,
  327. (300,
  328. (301,
  329. (302,
  330. (303, (304, (305, (2, '1')))))))))))))))))),
  331. (4, ''))),
  332. (6, ''))))),
  333. (4, ''),
  334. (0, ''))))
  335. self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
  336. def test_illegal_yield_2(self):
  337. # Illegal return in generator: def f(): return 1; yield 1
  338. tree = \
  339. (257,
  340. (264,
  341. (265,
  342. (266,
  343. (278,
  344. (1, 'from'),
  345. (281, (1, '__future__')),
  346. (1, 'import'),
  347. (279, (1, 'generators')))),
  348. (4, ''))),
  349. (264,
  350. (285,
  351. (259,
  352. (1, 'def'),
  353. (1, 'f'),
  354. (260, (7, '('), (8, ')')),
  355. (11, ':'),
  356. (291,
  357. (4, ''),
  358. (5, ''),
  359. (264,
  360. (265,
  361. (266,
  362. (272,
  363. (275,
  364. (1, 'return'),
  365. (313,
  366. (292,
  367. (293,
  368. (294,
  369. (295,
  370. (297,
  371. (298,
  372. (299,
  373. (300,
  374. (301,
  375. (302, (303, (304, (305, (2, '1')))))))))))))))))),
  376. (264,
  377. (265,
  378. (266,
  379. (272,
  380. (276,
  381. (1, 'yield'),
  382. (313,
  383. (292,
  384. (293,
  385. (294,
  386. (295,
  387. (297,
  388. (298,
  389. (299,
  390. (300,
  391. (301,
  392. (302,
  393. (303, (304, (305, (2, '1')))))))))))))))))),
  394. (4, ''))),
  395. (6, ''))))),
  396. (4, ''),
  397. (0, ''))))
  398. self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
  399. def test_print_chevron_comma(self):
  400. # Illegal input: print >>fp,
  401. tree = \
  402. (257,
  403. (264,
  404. (265,
  405. (266,
  406. (268,
  407. (1, 'print'),
  408. (35, '>>'),
  409. (290,
  410. (291,
  411. (292,
  412. (293,
  413. (295,
  414. (296,
  415. (297,
  416. (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
  417. (12, ','))),
  418. (4, ''))),
  419. (0, ''))
  420. self.check_bad_tree(tree, "print >>fp,")
  421. def test_a_comma_comma_c(self):
  422. # Illegal input: a,,c
  423. tree = \
  424. (258,
  425. (311,
  426. (290,
  427. (291,
  428. (292,
  429. (293,
  430. (295,
  431. (296,
  432. (297,
  433. (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
  434. (12, ','),
  435. (12, ','),
  436. (290,
  437. (291,
  438. (292,
  439. (293,
  440. (295,
  441. (296,
  442. (297,
  443. (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
  444. (4, ''),
  445. (0, ''))
  446. self.check_bad_tree(tree, "a,,c")
  447. def test_illegal_operator(self):
  448. # Illegal input: a $= b
  449. tree = \
  450. (257,
  451. (264,
  452. (265,
  453. (266,
  454. (267,
  455. (312,
  456. (291,
  457. (292,
  458. (293,
  459. (294,
  460. (296,
  461. (297,
  462. (298,
  463. (299,
  464. (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
  465. (268, (37, '$=')),
  466. (312,
  467. (291,
  468. (292,
  469. (293,
  470. (294,
  471. (296,
  472. (297,
  473. (298,
  474. (299,
  475. (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
  476. (4, ''))),
  477. (0, ''))
  478. self.check_bad_tree(tree, "a $= b")
  479. def test_malformed_global(self):
  480. #doesn't have global keyword in ast
  481. tree = (257,
  482. (264,
  483. (265,
  484. (266,
  485. (282, (1, 'foo'))), (4, ''))),
  486. (4, ''),
  487. (0, ''))
  488. self.check_bad_tree(tree, "malformed global ast")
  489. def test_missing_import_source(self):
  490. # from import a
  491. tree = \
  492. (257,
  493. (267,
  494. (268,
  495. (269,
  496. (281,
  497. (283, (1, 'from'), (1, 'import'),
  498. (286, (284, (1, 'fred')))))),
  499. (4, ''))),
  500. (4, ''), (0, ''))
  501. self.check_bad_tree(tree, "from import a")
  502. class CompileTestCase(unittest.TestCase):
  503. # These tests are very minimal. :-(
  504. def test_compile_expr(self):
  505. st = parser.expr('2 + 3')
  506. code = parser.compilest(st)
  507. self.assertEqual(eval(code), 5)
  508. def test_compile_suite(self):
  509. st = parser.suite('x = 2; y = x + 3')
  510. code = parser.compilest(st)
  511. globs = {}
  512. exec code in globs
  513. self.assertEqual(globs['y'], 5)
  514. def test_compile_error(self):
  515. st = parser.suite('1 = 3 + 4')
  516. self.assertRaises(SyntaxError, parser.compilest, st)
  517. def test_compile_badunicode(self):
  518. st = parser.suite('a = u"\U12345678"')
  519. self.assertRaises(SyntaxError, parser.compilest, st)
  520. st = parser.suite('a = u"\u1"')
  521. self.assertRaises(SyntaxError, parser.compilest, st)
  522. def test_issue_9011(self):
  523. # Issue 9011: compilation of an unary minus expression changed
  524. # the meaning of the ST, so that a second compilation produced
  525. # incorrect results.
  526. st = parser.expr('-3')
  527. code1 = parser.compilest(st)
  528. self.assertEqual(eval(code1), -3)
  529. code2 = parser.compilest(st)
  530. self.assertEqual(eval(code2), -3)
  531. class ParserStackLimitTestCase(unittest.TestCase):
  532. """try to push the parser to/over its limits.
  533. see http://bugs.python.org/issue1881 for a discussion
  534. """
  535. def _nested_expression(self, level):
  536. return "["*level+"]"*level
  537. def test_deeply_nested_list(self):
  538. e = self._nested_expression(99)
  539. st = parser.expr(e)
  540. st.compile()
  541. def test_trigger_memory_error(self):
  542. e = self._nested_expression(100)
  543. rc, out, err = assert_python_failure('-c', e)
  544. # parsing the expression will result in an error message
  545. # followed by a MemoryError (see #11963)
  546. self.assertIn(b's_push: parser stack overflow', err)
  547. self.assertIn(b'MemoryError', err)
  548. class STObjectTestCase(unittest.TestCase):
  549. """Test operations on ST objects themselves"""
  550. check_sizeof = support.check_sizeof
  551. @support.cpython_only
  552. def test_sizeof(self):
  553. def XXXROUNDUP(n):
  554. if n <= 1:
  555. return n
  556. if n <= 128:
  557. return (n + 3) & ~3
  558. return 1 << (n - 1).bit_length()
  559. basesize = support.calcobjsize('Pii')
  560. nodesize = struct.calcsize('hP3iP0h')
  561. def sizeofchildren(node):
  562. if node is None:
  563. return 0
  564. res = 0
  565. hasstr = len(node) > 1 and isinstance(node[-1], str)
  566. if hasstr:
  567. res += len(node[-1]) + 1
  568. children = node[1:-1] if hasstr else node[1:]
  569. if children:
  570. res += XXXROUNDUP(len(children)) * nodesize
  571. for child in children:
  572. res += sizeofchildren(child)
  573. return res
  574. def check_st_sizeof(st):
  575. self.check_sizeof(st, basesize + nodesize +
  576. sizeofchildren(st.totuple()))
  577. check_st_sizeof(parser.expr('2 + 3'))
  578. check_st_sizeof(parser.expr('2 + 3 + 4'))
  579. check_st_sizeof(parser.suite('x = 2 + 3'))
  580. check_st_sizeof(parser.suite(''))
  581. check_st_sizeof(parser.suite('# -*- coding: utf-8 -*-'))
  582. check_st_sizeof(parser.expr('[' + '2,' * 1000 + ']'))
  583. # XXX tests for pickling and unpickling of ST objects should go here
  584. def test_main():
  585. support.run_unittest(
  586. RoundtripLegalSyntaxTestCase,
  587. IllegalSyntaxTestCase,
  588. CompileTestCase,
  589. ParserStackLimitTestCase,
  590. STObjectTestCase,
  591. )
  592. if __name__ == "__main__":
  593. test_main()