PageRenderTime 78ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 1ms

/Languages/IronPython/Tests/test_ast.py

http://github.com/IronLanguages/main
Python | 1377 lines | 1203 code | 100 blank | 74 comment | 29 complexity | abf2651bc6f0676d944d6799327bdc1e MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. import sys, itertools, unittest
  2. from test import test_support
  3. from StringIO import StringIO
  4. import ast
  5. import types
  6. def to_tuple(t):
  7. if t is None or isinstance(t, (basestring, int, long, complex)):
  8. return t
  9. elif isinstance(t, list):
  10. return [to_tuple(e) for e in t]
  11. result = [t.__class__.__name__]
  12. if hasattr(t, 'lineno') and hasattr(t, 'col_offset'):
  13. result.append((t.lineno, t.col_offset))
  14. if t._fields is None:
  15. return tuple(result)
  16. for f in t._fields:
  17. result.append(to_tuple(getattr(t, f)))
  18. return tuple(result)
  19. # These tests are compiled through "exec"
  20. # There should be at least one test per statement
  21. exec_tests = [
  22. # FunctionDef
  23. "def f(): pass",
  24. "def f(a): pass",
  25. "def f(a=1): pass",
  26. "def f(*args, **kwargs): pass",
  27. # ClassDef
  28. "class C:pass",
  29. # Return
  30. "def f():return 1",
  31. # Delete
  32. "del v",
  33. # Assign
  34. "v = 1",
  35. # AugAssign
  36. "v += 1",
  37. # Print
  38. "print >>f, 1, ",
  39. # For
  40. "for v in v:pass",
  41. # While
  42. "while v:pass",
  43. # If
  44. "if v:pass",
  45. # If elif else
  46. "if v:pass\nelif u:pass\nelse: pass",
  47. # Raise
  48. "raise Exception, 'string'",
  49. # TryExcept
  50. "try:\n pass\nexcept Exception:\n pass",
  51. # TryFinally
  52. "try:\n pass\nfinally:\n pass",
  53. # Assert
  54. "assert v",
  55. # Import
  56. "import sys",
  57. # ImportFrom
  58. "from sys import v",
  59. # Exec
  60. "exec 'v'",
  61. # Global
  62. "global v",
  63. # Expr
  64. "1",
  65. # Pass,
  66. "pass",
  67. # Break
  68. # "break", doesn't work outside a loop
  69. "while x: break",
  70. # Continue
  71. # "continue", doesn't work outside a loop
  72. "while x: continue",
  73. # for statements with naked tuples (see http://bugs.python.org/issue6704)
  74. "for a,b in c: pass",
  75. "[(a,b) for a,b in c]",
  76. "((a,b) for a,b in c)",
  77. # yield makes no sense outside function
  78. "def f(): yield 1",
  79. # CP35001
  80. "def f(): yield",
  81. # comment
  82. "#"
  83. ]
  84. # These are compiled through "single"
  85. # because of overlap with "eval", it just tests what
  86. # can't be tested with "eval"
  87. single_tests = [
  88. "1+2"
  89. ]
  90. # These are compiled through "eval"
  91. # It should test all expressions
  92. eval_tests = [
  93. # BoolOp
  94. "a and b",
  95. # BinOp
  96. "a + b",
  97. # UnaryOp
  98. "not v",
  99. # Lambda
  100. "lambda:None",
  101. "lambda x: x",
  102. "lambda x: (yield x)",
  103. # Dict
  104. "{ 1:2 }",
  105. # ListComp
  106. "[a for b in c if d]",
  107. # GeneratorExp
  108. "(a for b in c for d in e for f in g)",
  109. "(a for b in c if d)",
  110. "(a for b in c for c in d)",
  111. # Yield
  112. "((yield i) for i in range(5))",
  113. # Compare
  114. "1 < 2 < 3",
  115. # Call
  116. "f(1,2,c=3,*d,**e)",
  117. # Repr
  118. "`v`",
  119. # Num
  120. "10L",
  121. # Str
  122. "'string'",
  123. # Attribute
  124. "a.b",
  125. # Subscript
  126. "a[b:c]",
  127. # Name
  128. "v",
  129. # List
  130. "[1,2,3]",
  131. # Tuple
  132. "1,2,3",
  133. # Combination
  134. "a.b.c.d(a.b[1:2])",
  135. # ellipsis
  136. "a[...]",
  137. # index
  138. "a[1]",
  139. # set
  140. "{a,b,c}",
  141. # DictComp
  142. "{k:v for k,v in li}",
  143. # SetComp
  144. "{e for e in li}",
  145. ]
  146. # TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
  147. # excepthandler, arguments, keywords, alias
  148. class AST_Tests(unittest.TestCase):
  149. def _assertTrueorder(self, ast_node, parent_pos):
  150. if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
  151. return
  152. if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
  153. node_pos = (ast_node.lineno, ast_node.col_offset)
  154. self.assertTrue(node_pos >= parent_pos)
  155. parent_pos = (ast_node.lineno, ast_node.col_offset)
  156. for name in ast_node._fields:
  157. value = getattr(ast_node, name)
  158. if isinstance(value, list):
  159. for child in value:
  160. self._assertTrueorder(child, parent_pos)
  161. elif value is not None:
  162. self._assertTrueorder(value, parent_pos)
  163. def test_compile_from_ast_001(self):
  164. p = ast.parse("-1", mode="eval")
  165. c = compile(p,"<unknown>", mode="eval" )
  166. self.assertEqual( eval(c), -1)
  167. def test_compile_from_ast_002(self):
  168. p = ast.parse("+1", mode="eval")
  169. c = compile(p,"<unknown>", mode="eval" )
  170. self.assertEqual( eval(c), 1)
  171. def test_compile_from_ast_003(self):
  172. p = ast.parse("not True", mode="eval")
  173. c = compile(p,"<unknown>", mode="eval" )
  174. self.assertEqual( eval(c), False)
  175. def test_compile_from_ast_004(self):
  176. p = ast.parse("2+2", mode="eval")
  177. c = compile(p,"<unknown>", mode="eval")
  178. self.assertEqual( eval(c), 4 )
  179. def test_compile_from_ast_005(self):
  180. p = ast.parse("5-1", mode="eval")
  181. c = compile(p,"<unknown>", mode="eval")
  182. self.assertEqual( eval(c), 4 )
  183. def test_compile_from_ast_006(self):
  184. p = ast.parse("42%13", mode="eval")
  185. c = compile(p,"<unknown>", mode="eval")
  186. self.assertEqual( eval(c), 3 )
  187. def test_compile_from_ast_007(self):
  188. p = ast.parse("2**8", mode="eval")
  189. c = compile(p,"<unknown>", mode="eval")
  190. self.assertEqual( eval(c), 256 )
  191. def test_compile_from_ast_008(self):
  192. p = ast.parse("True or False", mode="eval")
  193. c = compile(p,"<unknown>", mode="eval")
  194. self.assertEqual( eval(c), True )
  195. def test_compile_from_ast_009(self):
  196. p = ast.parse("True and False", mode="eval")
  197. c = compile(p,"<unknown>", mode="eval")
  198. self.assertEqual( eval(c), False )
  199. def test_compile_from_ast_010(self):
  200. p = ast.parse("'a'", mode="eval")
  201. c = compile(p,"<unknown>", mode="eval")
  202. self.assertEqual( eval(c), "a" )
  203. def test_compile_from_ast_011(self):
  204. p = ast.parse("42", mode="eval")
  205. c = compile(p,"<unknown>", mode="eval")
  206. self.assertEqual( eval(c), 42 )
  207. def test_compile_from_ast_012(self):
  208. p = ast.parse("None", mode="eval")
  209. c = compile(p,"<unknown>", mode="eval")
  210. self.assertEqual( eval(c), None )
  211. def test_compile_from_ast_013(self):
  212. p = ast.parse("[1,2,3]", mode="eval")
  213. c = compile(p,"<unknown>", mode="eval")
  214. self.assertEqual( eval(c), [1,2,3] )
  215. def test_compile_from_ast_014(self):
  216. p = ast.parse("{1,2,3}", mode="eval")
  217. c = compile(p,"<unknown>", mode="eval")
  218. self.assertEqual( eval(c), {1,2,3} )
  219. def test_compile_from_ast_015(self):
  220. p = ast.parse("{1:'a', 2:'b'}", mode="eval")
  221. c = compile(p,"<unknown>", mode="eval")
  222. self.assertEqual( eval(c), {1:'a',2:'b'} )
  223. def test_compile_from_ast_016(self):
  224. p = ast.parse("1,2", mode="eval")
  225. c = compile(p,"<unknown>", mode="eval")
  226. self.assertEqual( eval(c), (1,2) )
  227. def test_compile_from_ast_017(self):
  228. p = ast.parse("dict()", mode="eval") # trivial call expression
  229. c = compile(p,"<unknown>", mode="eval")
  230. self.assertEqual( eval(c), {} )
  231. # parenthesis ?
  232. def test_compile_from_ast_018(self):
  233. p = ast.parse("(1)", mode="eval") # failed attempt to generate parenthesis expression
  234. c = compile(p,"<unknown>", mode="eval")
  235. self.assertEqual( eval(c), 1 )
  236. def test_compile_from_ast_019(self):
  237. p = ast.parse("[x for x in range(2)]", mode="eval") # list comprehension
  238. c = compile(p,"<unknown>", mode="eval")
  239. self.assertEqual( eval(c), [0,1] )
  240. def test_compile_from_ast_020(self):
  241. # list comprehension
  242. p = ast.parse("[(x, y, z) for x in [1,2,3] if x!=2 for y in [3,1,4] for z in [7,8,9] if x != y]", mode="eval")
  243. c = compile(p,"<unknown>", mode="eval")
  244. self.assertEqual( eval(c), [(1, 3, 7), (1, 3, 8), (1, 3, 9), (1, 4, 7),
  245. (1, 4, 8), (1, 4, 9), (3, 1, 7), (3, 1, 8),
  246. (3, 1, 9), (3, 4, 7), (3, 4, 8), (3, 4, 9)] )
  247. def test_compile_from_ast_021(self):
  248. p = ast.parse("2>1", mode="eval") # Compare
  249. c = compile(p,"<unknown>", mode="eval")
  250. self.assertEqual( eval(c), True )
  251. def test_compile_from_ast_022(self):
  252. p = ast.parse("2>1<3==3", mode="eval") # All comparisons evaluate to True
  253. c = compile(p,"<unknown>", mode="eval")
  254. self.assertEqual( eval(c), True )
  255. p = ast.parse("1>1<3==3", mode="eval") # False at first position
  256. c = compile(p,"<unknown>", mode="eval")
  257. self.assertEqual( eval(c), False )
  258. p = ast.parse("2>1<0==0", mode="eval") # False at second position
  259. c = compile(p,"<unknown>", mode="eval")
  260. self.assertEqual( eval(c), False )
  261. p = ast.parse("2>1<3==1", mode="eval") # False at third position
  262. c = compile(p,"<unknown>", mode="eval")
  263. self.assertEqual( eval(c), False )
  264. def test_compile_from_ast_023(self):
  265. p = ast.parse("{x for x in range(3) if x!=2 }", mode="eval") # set comprehension
  266. c = compile(p,"<unknown>", mode="eval")
  267. self.assertEqual( eval(c), {0,1} )
  268. def test_compile_from_ast_024(self):
  269. p = ast.parse("{ x : ord(x) for x in ['a','b'] }", mode="eval") # dict comprehension
  270. c = compile(p,"<unknown>", mode="eval")
  271. self.assertEqual( eval(c), {'a':97, 'b':98 } )
  272. def test_compile_from_ast_025(self):
  273. p = ast.parse("'a'.upper()", mode="eval") # attribute
  274. c = compile(p,"<unknown>", mode="eval")
  275. self.assertEqual( eval(c), 'A' )
  276. def test_compile_from_ast_026(self):
  277. p = ast.parse("lambda x: x", mode="eval") # lambda
  278. c = compile(p,"<unknown>", mode="eval")
  279. f = eval(c)
  280. self.assertEqual( f(42),42)
  281. def test_compile_from_ast_027(self):
  282. p = ast.parse("lambda x=42: x", mode="eval") # default argument
  283. c = compile(p,"<unknown>", mode="eval")
  284. f = eval(c)
  285. self.assertEqual( f(),42)
  286. def test_compile_from_ast_028(self):
  287. p = ast.parse("1 if True else 2", mode="eval") # conditional expression
  288. c = compile(p,"<unknown>", mode="eval")
  289. self.assertEqual( eval(c), 1 )
  290. p = ast.parse("1 if False else 2", mode="eval") # conditional expression
  291. c = compile(p,"<unknown>", mode="eval")
  292. self.assertEqual( eval(c), 2 )
  293. def test_compile_from_ast_029(self):
  294. p = ast.parse("(x for x in [1,2,3] if x!=1)", mode="eval") # generator
  295. c = compile(p,"<unknown>", mode="eval")
  296. g = eval(c)
  297. self.assertEqual( g.next(), 2 )
  298. self.assertEqual( g.next(), 3 )
  299. self.assertRaises( StopIteration, g.next )
  300. def test_compile_from_ast_030(self):
  301. p = ast.parse("`101`", mode="eval") # repr
  302. c = compile(p,"<unknown>", mode="eval")
  303. self.assertEqual( eval(c), '101' )
  304. def test_compile_from_ast_031(self):
  305. p = ast.parse("range(13)[10]", mode="eval") # index
  306. c = compile(p,"<unknown>", mode="eval")
  307. self.assertEqual( eval(c), 10 )
  308. def test_compile_from_ast_032(self):
  309. p = ast.parse("range(42)[1:5]", mode="eval") # slice
  310. c = compile(p,"<unknown>", mode="eval")
  311. self.assertEqual( eval(c), range(1,5))
  312. def test_compile_from_ast_033(self):
  313. p = ast.parse("range(42)[1:5:2]", mode="eval") # extended? slice
  314. c = compile(p,"<unknown>", mode="eval")
  315. self.assertEqual( eval(c), [1,3])
  316. def test_compile_from_ast_034(self):
  317. # plain generator
  318. page = [ "line1 aaaaa bbb cccccc ddddddddd", "line2 xxxxxxxx yyyyyyy zzzzzz", "line3 ssssss ttttttttttt uuuu" ]
  319. p = ast.parse("(word for line in page for word in line.split())", mode="eval")
  320. c = compile(p,"<unknown>", mode="eval")
  321. g = eval(c)
  322. self.assertEqual( g.next(), 'line1' )
  323. self.assertEqual( g.next(), 'aaaaa' )
  324. self.assertEqual( g.next(), 'bbb')
  325. self.assertEqual( g.next(), 'cccccc' )
  326. self.assertEqual( g.next(), 'ddddddddd' )
  327. self.assertEqual( g.next(), 'line2' )
  328. self.assertEqual( g.next(), 'xxxxxxxx' )
  329. self.assertEqual( g.next(), 'yyyyyyy' )
  330. self.assertEqual( g.next(), 'zzzzzz' )
  331. self.assertEqual( g.next(), 'line3' )
  332. self.assertEqual( g.next(), 'ssssss' )
  333. self.assertEqual( g.next(), 'ttttttttttt' )
  334. self.assertEqual( g.next(), 'uuuu' )
  335. self.assertRaises( StopIteration, g.next )
  336. def test_compile_from_ast_035(self):
  337. # generator with multiple ifs
  338. page = [ "line1 aaaaa bbb cccccc ddddddddd", "short line", "line2 xxxxxxxx yyyyyyy zzzzzz", "line3 ssssss ttttttttttt uuuu" ]
  339. p = ast.parse("(word for line in page if len(line)>10 for word in line.split() if word!='ssssss' if word!='zzzzzz')",
  340. mode="eval")
  341. c = compile(p,"<unknown>", mode="eval")
  342. g = eval(c)
  343. self.assertEqual( g.next(), 'line1' )
  344. self.assertEqual( g.next(), 'aaaaa' )
  345. self.assertEqual( g.next(), 'bbb')
  346. self.assertEqual( g.next(), 'cccccc' )
  347. self.assertEqual( g.next(), 'ddddddddd' )
  348. self.assertEqual( g.next(), 'line2' )
  349. self.assertEqual( g.next(), 'xxxxxxxx' )
  350. self.assertEqual( g.next(), 'yyyyyyy' )
  351. self.assertEqual( g.next(), 'line3' )
  352. self.assertEqual( g.next(), 'ttttttttttt' )
  353. self.assertEqual( g.next(), 'uuuu' )
  354. self.assertRaises( StopIteration, g.next )
  355. def test_compile_from_ast_036(self):
  356. # the results comply 1:1 with cpython 2.7.3 on Linux
  357. p = ast.parse("((yield i) for i in range(3))", mode="eval") # yield inside generator
  358. c = compile(p,"<unknown>", mode="eval")
  359. g = eval(c)
  360. self.assertEqual(g.next(),0)
  361. self.assertIsNone(g.next())
  362. self.assertEqual(g.next(),1)
  363. self.assertIsNone(g.next())
  364. self.assertEqual(g.next(),2)
  365. self.assertIsNone(g.next())
  366. self.assertRaises(StopIteration, g.next )
  367. # TODO: more testing for extended slices [1:2, 5:10], [1:2, ...] etc
  368. # inspiration at: http://ilan.schnell-web.net/prog/slicing/
  369. def test_compile_from_ast_038(self):
  370. p = ast.parse("lambda x: (yield x)", mode="eval") # yield inside lambda
  371. c = compile(p,"<unknown>", mode="eval")
  372. eval(c)
  373. f = eval(c)
  374. g = f(1)
  375. self.assertEqual(g.next(),1)
  376. self.assertRaises(StopIteration, g.next )
  377. def test_compile_from_ast_100(self):
  378. p = ast.parse("pass", mode="exec")
  379. c = compile(p,"<unknown>", mode="exec")
  380. self.assertEqual( eval(c), None)
  381. def test_compile_from_ast_101(self):
  382. cap = StringIO()
  383. p = ast.parse("print >> cap, 1, 2,", mode="exec") # print
  384. c = compile(p,"<unknown>", mode="exec")
  385. exec c
  386. self.assertEqual( cap.getvalue(), "1 2")
  387. def test_compile_from_ast_102(self):
  388. p = ast.parse("a=b=42", mode="exec") # assignment
  389. c = compile(p,"<unknown>", mode="exec")
  390. exec c
  391. self.assertEqual(a, 42)
  392. self.assertEqual(b, 42)
  393. def test_compile_from_ast_103(self):
  394. p = ast.parse("a,b=13,42", mode="exec") # assignment
  395. c = compile(p,"<unknown>", mode="exec")
  396. exec c
  397. self.assertEqual(a, 13)
  398. self.assertEqual(b, 42)
  399. def test_compile_from_ast_104(self):
  400. p = ast.parse("a=42\na+=1", mode="exec") # assignment
  401. c = compile(p,"<unknown>", mode="exec")
  402. exec c
  403. self.assertEqual(a, 43)
  404. def test_compile_from_ast_105(self):
  405. p = ast.parse("assert True", mode="exec") # assert no exception
  406. c = compile(p,"<unknown>", mode="exec")
  407. exec c
  408. p = ast.parse("assert False", mode="exec") # assert with exception
  409. c = compile(p,"<unknown>", mode="exec")
  410. raised=False
  411. try:
  412. exec c
  413. except AssertionError:
  414. raised=True
  415. self.assertTrue(raised)
  416. def test_compile_from_ast_106(self):
  417. p = ast.parse("a=1\ndel a\nprint a", mode="exec") # delete statement
  418. c = compile(p,"<unknown>", mode="exec")
  419. raised=False
  420. try:
  421. exec c
  422. except NameError:
  423. raised=True
  424. self.assertTrue(raised)
  425. def test_compile_from_ast_107(self):
  426. p = ast.parse("def f(): return\nf()", mode="exec") # return statement
  427. c = compile(p,"<unknown>", mode="exec")
  428. exec c
  429. def test_compile_from_ast_108(self):
  430. cap = StringIO()
  431. p = ast.parse("def f(): return 42\nprint >> cap, f(),", mode="exec") # return statement
  432. c = compile(p,"<unknown>", mode="exec")
  433. exec c
  434. self.assertEqual(cap.getvalue(),'42')
  435. def test_compile_from_ast_109(self):
  436. p = ast.parse("def f(): yield 42", mode="exec") # yield statement
  437. c = compile(p,"<unknown>", mode="exec")
  438. exec c
  439. g = f()
  440. self.assertEqual(g.next(),42)
  441. self.assertRaises(StopIteration, g.next )
  442. def test_compile_from_ast_110(self):
  443. p = ast.parse("raise Exception('expected')", mode="exec") # raise
  444. c = compile(p,"<unknown>", mode="exec")
  445. raised=False
  446. try:
  447. exec c
  448. except Exception as e:
  449. raised=True
  450. self.assertEqual(e.message,'expected')
  451. self.assertTrue(raised)
  452. def test_compile_from_ast_111(self):
  453. p = ast.parse("n=0\nwhile n==0:\n n=1\n break\n n=2", mode="exec") # break
  454. c = compile(p,"<unknown>", mode="exec")
  455. exec c
  456. self.assertEqual(n,1)
  457. def test_compile_from_ast_112(self):
  458. p = ast.parse("n=0\nwhile n==0:\n n=1\n continue\n raise Exception()", mode="exec") # continue
  459. c = compile(p,"<unknown>", mode="exec")
  460. exec c
  461. self.assertEqual(n,1)
  462. # TODO: test relative imports
  463. # TODO: test imports with subpackages structure
  464. def test_compile_from_ast_113(self):
  465. p = ast.parse("import dummy_module", mode="exec") # import
  466. c = compile(p,"<unknown>", mode="exec")
  467. exec c
  468. from sys import modules
  469. self.assertNotEqual(modules.get('dummy_module'),None)
  470. self.assertEqual(modules.get('dummy_module'),dummy_module)
  471. def test_compile_from_ast_114(self):
  472. p = ast.parse("import dummy_module as baz", mode="exec") # import as
  473. c = compile(p,"<unknown>", mode="exec")
  474. exec c
  475. from sys import modules
  476. self.assertNotEqual(modules.get('dummy_module'),None)
  477. self.assertEqual(modules.get('dummy_module'),baz)
  478. def test_compile_from_ast_115(self):
  479. p = ast.parse("from dummy_module import *", mode="exec") # from import *
  480. c = compile(p,"<unknown>", mode="exec")
  481. exec c
  482. from sys import modules
  483. self.assertNotEqual(modules.get('dummy_module'),None)
  484. self.assertEqual(foo,1)
  485. self.assertEqual(bar,2)
  486. self.assertEqual(foobar,3)
  487. def test_compile_from_ast_116(self):
  488. p = ast.parse("def f():\n global i\n i+=41\nf()", mode="exec") # global
  489. c = compile(p,"<unknown>", mode="exec")
  490. v = {'i':1}
  491. exec c in v
  492. self.assertEqual(v['i'],42)
  493. def test_compile_from_ast_117(self):
  494. cap = StringIO()
  495. p = ast.parse("exec 'print >> cap, 42,'", mode="exec") # exec
  496. c = compile(p,"<unknown>", mode="exec")
  497. exec c
  498. self.assertEqual(cap.getvalue(), '42')
  499. def test_compile_from_ast_118(self):
  500. p = ast.parse("if a:\n x=1\nelif b:\n x=2\nelse:\n x=3", mode="exec") # if elif else
  501. c = compile(p,"<unknown>", mode="exec")
  502. a = True
  503. exec c
  504. self.assertEqual(x,1)
  505. a = False
  506. b = True
  507. exec c
  508. self.assertEqual(x,2)
  509. a = False
  510. b = False
  511. exec c
  512. self.assertEqual(x,3)
  513. def test_compile_from_ast_119(self):
  514. p = ast.parse("a=1; b=2", mode="exec") # statement list?
  515. c = compile(p,"<unknown>", mode="exec")
  516. exec c
  517. self.assertEqual(a,1)
  518. self.assertEqual(b,2)
  519. def test_compile_from_ast_120(self):
  520. p = ast.parse("n=1\nwhile n==0:\n n=13\nelse: n=42", mode="exec") # while with else
  521. c = compile(p,"<unknown>", mode="exec")
  522. exec c
  523. self.assertEqual(n,42)
  524. def test_compile_from_ast_121(self):
  525. cap = StringIO()
  526. p = ast.parse("for i in [0,1,2]:\n print>> cap, i,\nelse:\n print>>cap, 'else',", mode="exec") # for with else
  527. exec compile(p,"<unknown>", mode="exec")
  528. self.assertEqual(cap.getvalue(),"0 1 2 else")
  529. def test_compile_from_ast_122(self):
  530. cap = StringIO()
  531. tc = """
  532. try:
  533. print >> cap, 1,
  534. raise Exception("test")
  535. print >> cap, 2,
  536. except Exception as e:
  537. print >> cap, 3,
  538. else:
  539. print >> cap, 4,
  540. finally:
  541. print >> cap, 5,
  542. """
  543. p = ast.parse( tc, mode="exec") # try except
  544. exec compile(p,"<unknown>", mode="exec")
  545. self.assertEquals(cap.getvalue(), "1 3 5")
  546. def test_compile_from_ast_123(self):
  547. cap = StringIO()
  548. tc = """
  549. try:
  550. print >> cap, 1,
  551. # raise Exception("test")
  552. print >> cap, 2,
  553. except Exception as e:
  554. print >> cap, 3
  555. else:
  556. print >> cap, 4,
  557. finally:
  558. print >> cap, 5,
  559. """
  560. p = ast.parse( tc, mode="exec") # try except
  561. exec compile(p,"<unknown>", mode="exec")
  562. self.assertEqual(cap.getvalue(),"1 2 4 5")
  563. def test_compile_from_ast_124(self):
  564. tc = """
  565. with open("dummy_module.py") as dm:
  566. l = len(dm.read())
  567. """
  568. p = ast.parse( tc, mode="exec") # try except
  569. exec compile(p,"<unknown>", mode="exec")
  570. self.assertEqual(l,20)
  571. def test_compile_from_ast_125(self):
  572. tc = """
  573. class Foo(object):
  574. pass
  575. foo=Foo()
  576. """
  577. p = ast.parse( tc, mode="exec") # try except
  578. exec compile(p,"<unknown>", mode="exec")
  579. self.assertIsInstance(foo,Foo)
  580. def test_compile_from_ast_126(self):
  581. cap = StringIO()
  582. tc = """
  583. def f(a):
  584. return a
  585. print >> cap, f(22),
  586. """
  587. p = ast.parse( tc, mode="exec") # call function with an argument
  588. exec compile(p,"<unknown>", mode="exec")
  589. self.assertEquals(cap.getvalue(), "22")
  590. def test_compile_from_ast_127(self):
  591. cap = StringIO()
  592. tc = """
  593. def f(a):
  594. return a
  595. print >> cap, f(a=222),
  596. """
  597. p = ast.parse( tc, mode="exec") # call function with an argument, pass as keyword
  598. c = compile(p,"<unknown>", mode="exec")
  599. exec c
  600. self.assertEquals(cap.getvalue(), "222")
  601. def test_compile_from_ast_128(self):
  602. cap = StringIO()
  603. tc = """
  604. def f(a,*args):
  605. return args[1]
  606. print >> cap, f(1,2,42),
  607. """
  608. p = ast.parse( tc, mode="exec") # call function with a variable number of arguments
  609. c = compile(p,"<unknown>", mode="exec")
  610. exec c
  611. self.assertEquals(cap.getvalue(), "42")
  612. def test_compile_from_ast_129(self):
  613. cap = StringIO()
  614. tc = """
  615. def f(a,**kwargs):
  616. return kwargs["two"]
  617. d={ "one":13, "two":42 }
  618. print >> cap, f(1, **d),
  619. """
  620. p = ast.parse( tc, mode="exec") # call function with a keyword argument
  621. c = compile(p,"<unknown>", mode="exec")
  622. exec c
  623. self.assertEquals(cap.getvalue(), "42")
  624. def test_compile_from_ast_130(self):
  625. cap = StringIO()
  626. tc = """
  627. def f(a,**kwargs):
  628. return kwargs["two"]
  629. print >> cap, f(1, two=42, one=13),
  630. """
  631. p = ast.parse( tc, mode="exec") # call function with a keyword argument
  632. c = compile(p,"<unknown>", mode="exec")
  633. exec c
  634. self.assertEquals(cap.getvalue(), "42")
  635. def test_compile_from_ast_131(self):
  636. cap = StringIO()
  637. tc = """
  638. def deco(fx):
  639. def wrap():
  640. print >> cap, "wrapped"
  641. fx()
  642. return wrap
  643. @deco
  644. def f():
  645. print >> cap, "f"
  646. f()
  647. """
  648. p = ast.parse( tc, mode="exec") # function decorator
  649. c = compile(p,"<unknown>", mode="exec")
  650. exec c in {"cap": cap}
  651. self.assertEquals(cap.getvalue(), "wrapped\nf\n")
  652. def test_compile_from_ast_132(self):
  653. cap = StringIO()
  654. tc = """
  655. def deco(k):
  656. k.foo = 42
  657. return k
  658. @deco
  659. class C:
  660. pass
  661. c=C()
  662. print >> cap, c.foo,
  663. """
  664. p = ast.parse( tc, mode="exec") # function decorator
  665. c = compile(p,"<unknown>", mode="exec")
  666. exec c
  667. self.assertEquals(cap.getvalue(), "42")
  668. def test_compile_from_ast_133(self):
  669. cap = StringIO()
  670. tc = """
  671. def f1():
  672. yield 1
  673. def fNone():
  674. yield
  675. for v in f1():
  676. print >> cap, v
  677. for v in fNone():
  678. print >> cap, v
  679. """
  680. p = ast.parse( tc, mode="exec") # yield
  681. c = compile(p,"<unknown>", mode="exec")
  682. exec c
  683. self.assertEquals(cap.getvalue(), "1\nNone\n")
  684. def test_compile_from_ast_200(self):
  685. p = ast.parse("a=1; b=2", mode="single") # something with single
  686. c = compile(p,"<unknown>", mode="single")
  687. exec c
  688. self.assertEqual(a,1)
  689. self.assertEqual(b,2)
  690. def test_compile_from_ast_201(self):
  691. p = ast.parse("1+1", mode="single") # expression in single should find its way into stdout
  692. c = compile(p,"<unknown>", mode="single")
  693. saveStdout = sys.stdout
  694. sys.stdout = cap = StringIO()
  695. exec c
  696. sys.stdout = saveStdout
  697. self.assertEqual(cap.getvalue(), "2\n")
  698. def test_compile_argument_bytes(self):
  699. p = ast.parse(b'1+1', "<unknown>", mode="eval")
  700. c = compile(p, "<unknown>", mode="eval")
  701. self.assertEqual(eval(c),2)
  702. def test_compile_argument_buffer(self):
  703. p = ast.parse(buffer('1+1'), "<unknown>", mode="eval")
  704. c = compile(p, "<unknown>", mode="eval")
  705. self.assertEqual(eval(c),2)
  706. def test_compile_argument_bytearray(self):
  707. p = ast.parse(bytearray('1+1','ascii'), "<unknown>", mode="eval")
  708. c = compile(p, "<unknown>", mode="eval")
  709. self.assertEqual(eval(c),2)
  710. def test_compile_argument_error(self):
  711. self.assertRaises( TypeError, ast.parse, ['1+1'], "<unknown>", mode="eval")
  712. def test_snippets(self):
  713. # Things which diverted from cpython:
  714. # - col_offset of list comprehension in ironpython uses opening bracket, cpython points to first expr
  715. # - same for generator
  716. # - Slice in iron has col_offset and lineno set, in cpython both are not set
  717. for input, output, kind in ((exec_tests, exec_results, "exec"),
  718. (single_tests, single_results, "single"),
  719. (eval_tests, eval_results, "eval")):
  720. for i, o in itertools.izip(input, output):
  721. ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
  722. self.assertEqual(to_tuple(ast_tree), o)
  723. self._assertTrueorder(ast_tree, (0, 0))
  724. def test_slicex(self):
  725. slc = ast.parse("x[1:2:3]").body[0].value.slice
  726. self.assertEqual(slc.lower.n, 1)
  727. self.assertEqual(slc.upper.n, 2)
  728. self.assertEqual(slc.step.n, 3)
  729. def test_slice(self):
  730. slc = ast.parse("x[::]").body[0].value.slice
  731. self.assertIsNone(slc.upper)
  732. self.assertIsNone(slc.lower)
  733. self.assertIsInstance(slc.step, ast.Name)
  734. self.assertEqual(slc.step.id, "None")
  735. def test_from_import(self):
  736. im = ast.parse("from . import y").body[0]
  737. self.assertIsNone(im.module)
  738. def test_base_classes(self):
  739. self.assertTrue(issubclass(ast.For, ast.stmt))
  740. self.assertTrue(issubclass(ast.Name, ast.expr))
  741. self.assertTrue(issubclass(ast.stmt, ast.AST))
  742. self.assertTrue(issubclass(ast.expr, ast.AST))
  743. self.assertTrue(issubclass(ast.comprehension, ast.AST))
  744. self.assertTrue(issubclass(ast.Gt, ast.AST))
  745. def test_nodeclasses(self):
  746. # IronPyhon performs argument typechecking
  747. l=ast.Str('A')
  748. o=ast.Mult()
  749. r=ast.Num('13')
  750. x=ast.BinOp(l,o,r,lineno=42)
  751. self.assertEqual(x.left, l)
  752. self.assertEqual(x.op, o)
  753. self.assertEqual(x.right, r)
  754. self.assertEqual(x.lineno, 42)
  755. # node raises exception when not given enough arguments
  756. self.assertRaises(TypeError, ast.BinOp, l, o)
  757. # can set attributes through kwargs too
  758. x = ast.BinOp(left=l, op=o, right=r, lineno=42)
  759. self.assertEqual(x.left, l)
  760. self.assertEqual(x.op, o)
  761. self.assertEqual(x.right, r)
  762. self.assertEqual(x.lineno, 42)
  763. # this used to fail because Sub._fields was None
  764. x = ast.Sub()
  765. def test_docexample(self):
  766. # used to fail on ironpython for various reason
  767. node = ast.UnaryOp(ast.USub(), ast.Num(5, lineno=0, col_offset=0),
  768. lineno=0, col_offset=0)
  769. # the same with zero argument constructors
  770. node = ast.UnaryOp()
  771. node.op = ast.USub()
  772. node.operand = ast.Num()
  773. node.operand.n = 5
  774. node.operand.lineno = 0
  775. node.operand.col_offset = 0
  776. node.lineno = 0
  777. node.col_offset = 0
  778. def test_example_from_net(self):
  779. node = ast.Expression(ast.BinOp(ast.Str('xy'), ast.Mult(), ast.Num(3)))
  780. def _test_extra_attribute(self):
  781. n=ast.Num()
  782. n.extra_attribute=2
  783. self.assertTrue(hasattr(n,'extra_attribute'))
  784. def test_operators(self):
  785. boolop0 = ast.BoolOp()
  786. boolop1 = ast.BoolOp(ast.And(),
  787. [ ast.Name('True', ast.Load()), ast.Name('False', ast.Load()), ast.Name('a',ast.Load())])
  788. boolop2 = ast.BoolOp(ast.And(),
  789. [ ast.Name('True', ast.Load()), ast.Name('False', ast.Load()), ast.Name('a',ast.Load())],
  790. 0, 0)
  791. binop0 = ast.BinOp()
  792. binop1 = ast.BinOp(ast.Str('xy'), ast.Mult(), ast.Num(3))
  793. binop2 = ast.BinOp(ast.Str('xy'), ast.Mult(), ast.Num(3), 0, 0)
  794. unaryop0 = ast.UnaryOp()
  795. unaryop1 = ast.UnaryOp(ast.Not(), ast.Name('True',ast.Load()))
  796. unaryop2 = ast.UnaryOp(ast.Not(), ast.Name('True',ast.Load()), 0, 0)
  797. lambda0 = ast.Lambda()
  798. lambda1 = ast.Lambda(ast.arguments([ast.Name('x', ast.Param())], None, None, []), ast.Name('x', ast.Load()))
  799. ifexp0 = ast.IfExp()
  800. ifexp1 = ast.IfExp(ast.Name('True',ast.Load()), ast.Num(1), ast.Num(0))
  801. ifexp2 = ast.IfExp(ast.Name('True',ast.Load()), ast.Num(1), ast.Num(0), 0, 0)
  802. dict0 = ast.Dict()
  803. dict1 = ast.Dict([ast.Num(1), ast.Num(2)], [ast.Str('a'), ast.Str('b')])
  804. dict2 = ast.Dict([ast.Num(1), ast.Num(2)], [ast.Str('a'), ast.Str('b')], 0, 0)
  805. set0 = ast.Set()
  806. set1 = ast.Set([ast.Num(1), ast.Num(2)])
  807. set2 = ast.Set([ast.Num(1), ast.Num(2)], 0, 0)
  808. lc0 = ast.ListComp()
  809. lc1 = ast.ListComp( ast.Name('x',ast.Load()),
  810. [ast.comprehension(ast.Name('x', ast.Store()),
  811. ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])])
  812. lc2 = ast.ListComp( ast.Name('x',ast.Load()),
  813. [ast.comprehension(ast.Name('x', ast.Store()),
  814. ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])], 0, 0)
  815. setcomp0 = ast.SetComp()
  816. setcomp1 = ast.SetComp(ast.Name('x', ast.Load()),
  817. [ast.comprehension(ast.Name('x', ast.Store()), ast.Str('abracadabra'),
  818. [ast.Compare(ast.Name('x', ast.Load()), [ast.NotIn()],
  819. [ast.Str('abc')])])])
  820. comprehension0 = ast.comprehension()
  821. comprehension1 = ast.comprehension(ast.Name('x', ast.Store()),
  822. ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])
  823. # "{i : chr(65+i) for i in (1,2)}")
  824. dictcomp0 = ast.DictComp()
  825. dictcomp1 = ast.DictComp(ast.Name('i', ast.Load()),
  826. ast.Call(ast.Name('chr', ast.Load()),
  827. [ast.BinOp(ast.Num(65), ast.Add(), ast.Name('i', ast.Load()))],
  828. [], None, None),
  829. [ast.comprehension(ast.Name('i', ast.Store()),
  830. ast.Tuple([ast.Num(1), ast.Num(n=2)], ast.Load()), [])])
  831. dictcomp2 = ast.DictComp(ast.Name('i', ast.Load()),
  832. ast.Call(ast.Name('chr', ast.Load()),
  833. [ast.BinOp(ast.Num(65), ast.Add(), ast.Name('i', ast.Load()))],
  834. [], None, None),
  835. [ast.comprehension(ast.Name('i', ast.Store()),
  836. ast.Tuple([ast.Num(1), ast.Num(n=2)], ast.Load()), [])],0,0)
  837. # (x for x in (1,2))
  838. genexp0 = ast.GeneratorExp()
  839. genexp1 = ast.GeneratorExp(ast.Name('x', ast.Load()),
  840. [ast.comprehension(ast.Name('x', ast.Store()),
  841. ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])])
  842. genexp2 = ast.GeneratorExp(ast.Name('x', ast.Load()),
  843. [ast.comprehension(ast.Name('x', ast.Store()),
  844. ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])],0,0)
  845. # yield 2
  846. yield0 = ast.Yield()
  847. yield1 = ast.Yield(ast.Num(2))
  848. yield2 = ast.Yield(ast.Num(2),0,0)
  849. yield20 = ast.Yield(lineno=0, col_offset=0)
  850. # a>0
  851. compare0 = ast.Compare()
  852. compare1 = ast.Compare(ast.Name('a', ast.Load()), [ast.Gt()], [ast.Num(0)])
  853. compare2 = ast.Compare(ast.Name('a', ast.Load()), [ast.Gt()], [ast.Num(0)],0,0)
  854. # chr(65)
  855. call0 = ast.Call()
  856. call1 = ast.Call(ast.Name('chr', ast.Load()), [ast.Num(65)], [], None, None)
  857. call2 = ast.Call(ast.Name('chr', ast.Load()), [ast.Num(65)], [], None, None, 0, 0)
  858. call20 = ast.Call(ast.Name('f', ast.Load()), [ast.Num(0)], [])
  859. call21 = ast.Call(ast.Name('f', ast.Load()), [ast.Num(0)], [], lineno=0, col_offset=0)
  860. # 0
  861. num0 = ast.Num()
  862. num1 = ast.Num(0)
  863. num2 = ast.Num(0,0,0)
  864. # "foo"
  865. str0 = ast.Str()
  866. str1 = ast.Str("foo")
  867. str2 = ast.Str("foo",0,0)
  868. # TODO: come back
  869. repr0 = ast.Repr()
  870. repr1 = ast.Repr(ast.Num(0))
  871. repr2 = ast.Repr(ast.Num(0),0,0)
  872. # foo.bar
  873. attr0 = ast.Attribute()
  874. attr1 = ast.Attribute(ast.Name('foo', ast.Load()), 'bar', ast.Load())
  875. attr2 = ast.Attribute(ast.Name('foo', ast.Load()), 'bar', ast.Load(), 0,0)
  876. # a[1:2]
  877. subscript0 = ast.Subscript()
  878. subscript1 = ast.Subscript(ast.Name('a', ast.Load()), ast.Slice(ast.Num(1), ast.Num(2)), ast.Load())
  879. subscript2 = ast.Subscript(ast.Name('a', ast.Load()), ast.ExtSlice([ast.Num(1), ast.Num(2)]), ast.Load(), 0, 0)
  880. # name
  881. name0 = ast.Name()
  882. name1 = ast.Name("name", ast.Load())
  883. name2 = ast.Name("name", ast.Load(),0,0)
  884. # [1,2]
  885. list0 = ast.List()
  886. list1 = ast.List([ast.Num(1), ast.Num(2)], ast.Load())
  887. list2 = ast.List([ast.Num(1), ast.Num(2)], ast.Load(),0,0)
  888. # (1,2)
  889. tuple0 = ast.Tuple()
  890. tuple1 = ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load())
  891. tuple2 = ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load(), 0, 0)
  892. def test_stmt(self):
  893. # def foo():
  894. # pass
  895. fundef0 = ast.FunctionDef()
  896. fundef1 = ast.FunctionDef('foo', ast.arguments([], None, None, []), [ast.Pass()], [])
  897. fundef2 = ast.FunctionDef('foo', ast.arguments([], None, None, []), [ast.Pass()], [], 0,0 )
  898. # class foo(object):
  899. # pass
  900. classdef0 = ast.ClassDef()
  901. classdef1 = ast.ClassDef('foo', [ast.Name('object', ast.Load())], [ast.Pass()], [])
  902. classdef1 = ast.ClassDef('foo', [ast.Name('object', ast.Load())], [ast.Pass()], [], 0,0)
  903. # return 0
  904. return0 = ast.Return()
  905. return1 = ast.Return(ast.Num(0))
  906. return2 = ast.Return(ast.Num(0),0,0)
  907. return20 = ast.Return(lineno=0, col_offset=0)
  908. # del d[1]
  909. del0 = ast.Delete()
  910. del1 = ast.Delete([ast.Subscript(ast.Name('d', ast.Load()), ast.Index(ast.Num(1)), ast.Del())])
  911. del2 = ast.Delete([ast.Subscript(ast.Name('d', ast.Load()), ast.Index(ast.Num(1)), ast.Del())],0,0)
  912. # a=1
  913. assign0=ast.Assign()
  914. assign1=ast.Assign([ast.Name('a', ast.Store())], ast.Num(1))
  915. assign2=ast.Assign([ast.Name('a', ast.Store())], ast.Num(1),0,0)
  916. # a+=1
  917. augassign0=ast.AugAssign()
  918. augassign1=ast.AugAssign(ast.Name('a', ast.Store()), ast.Add(), ast.Num(1))
  919. augassign2=ast.AugAssign(ast.Name('a', ast.Store()), ast.Add(), ast.Num(1),0,0)
  920. # print 1
  921. print0 = ast.Print()
  922. print1 = ast.Print(None, [ast.Num(1)], True)
  923. print2 = ast.Print(None, [ast.Num(1)], True, 0, 0)
  924. print20 = ast.Print( values=[ast.Num(1)], nl=True)
  925. # for i in l:
  926. # print i
  927. # else:
  928. # pass
  929. for0 = ast.For()
  930. for1 = ast.For(ast.Name('i', ast.Store()),
  931. ast.Name('l', ast.Load()),
  932. [ast.Print(None, [ast.Name('i', ast.Load())], True)],
  933. [ast.Pass()])
  934. for2 = ast.For(ast.Name('i', ast.Store()),
  935. ast.Name('l', ast.Load()),
  936. [ast.Print(None, [ast.Name('i', ast.Load())], True)],
  937. [ast.Pass()],0,0)
  938. # while True:
  939. # pass
  940. # else:
  941. # pass
  942. while0 = ast.While()
  943. while1 = ast.While(ast.Name('True', ast.Load()), [ast.Pass()], [ast.Pass()])
  944. while2 = ast.While(ast.Name('True', ast.Load()), [ast.Pass()], [ast.Pass()], 0,0 )
  945. # if a:
  946. # pass
  947. # else:
  948. # pass
  949. if0 = ast.If()
  950. if1 = ast.If(ast.Name('a', ast.Load()), [ast.Pass()], [ast.Pass()])
  951. if2 = ast.If(ast.Name('a', ast.Load()), [ast.Pass()], [ast.Pass()] ,0,0)
  952. # with with open("foo") as f:
  953. # pass
  954. with0 = ast.With()
  955. with0 = ast.With(ast.Call(ast.Name('open', ast.Load()), [ast.Str('foo')], []),
  956. ast.Name('f', ast.Store()),
  957. [ast.Pass()])
  958. # raise Exception()
  959. raise0 = ast.Raise()
  960. raise1 = ast.Raise(ast.Call(ast.Name('Exception', ast.Load()), [], []), None, None)
  961. raise2 = ast.Raise(ast.Call(ast.Name('Exception', ast.Load()), [], []), None, None, 0, 0)
  962. def test_attributes(self):
  963. # assert True, "bad"
  964. assert0 = ast.Assert()
  965. self.assertFalse(hasattr(assert0, 'lineno'))
  966. self.assertFalse(hasattr(assert0, 'col_offset'))
  967. assert1 = ast.Assert(ast.Name('True', ast.Load()), ast.Str('bad'))
  968. self.assertFalse(hasattr(assert1, 'lineno'))
  969. self.assertFalse(hasattr(assert1, 'col_offset'))
  970. try:
  971. tmp=assert1.lineno
  972. except Exception as e:
  973. self.assertTrue(isinstance(e,AttributeError))
  974. try:
  975. tmp=assert1.col_offset
  976. except Exception as e:
  977. self.assertTrue(isinstance(e,AttributeError))
  978. assert2 = ast.Assert(ast.Name('True', ast.Load()), ast.Str('bad'),2,3)
  979. self.assertEqual(assert2.lineno,2)
  980. self.assertEqual(assert2.col_offset,3)
  981. def test_compare(self):
  982. #
  983. c0 = to_tuple(ast.parse("a<b>c"))
  984. c1 = to_tuple(ast.parse("(a<b)>c"))
  985. c2 = to_tuple(ast.parse("a<(b>c)"))
  986. self.assertNotEqual(c0,c1)
  987. self.assertNotEqual(c1,c2)
  988. self.assertNotEqual(c0,c2)
  989. def test_pickling(self):
  990. import pickle
  991. mods = [pickle]
  992. try:
  993. import cPickle
  994. mods.append(cPickle)
  995. except ImportError:
  996. pass
  997. protocols = [0, 1, 2]
  998. for mod in mods:
  999. for protocol in protocols:
  1000. for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
  1001. ast2 = mod.loads(mod.dumps(ast, protocol))
  1002. self.assertEquals(to_tuple(ast2), to_tuple(ast))
  1003. def test_compile_comment(self):
  1004. self.assertRaisesRegexp( SyntaxError, "unexpected EOF while parsing", compile, "#", "string", "single" )
  1005. class ASTHelpers_Test(unittest.TestCase):
  1006. def test_parse(self):
  1007. a = ast.parse('foo(1 + 1)')
  1008. b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
  1009. self.assertEqual(ast.dump(a), ast.dump(b))
  1010. def test_dump(self):
  1011. node = ast.parse('spam(eggs, "and cheese")')
  1012. self.assertEqual(ast.dump(node),
  1013. "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
  1014. "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
  1015. "keywords=[], starargs=None, kwargs=None))])"
  1016. )
  1017. self.assertEqual(ast.dump(node, annotate_fields=False),
  1018. "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
  1019. "Str('and cheese')], [], None, None))])"
  1020. )
  1021. self.assertEqual(ast.dump(node, include_attributes=True),
  1022. "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
  1023. "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
  1024. "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
  1025. "col_offset=11)], keywords=[], starargs=None, kwargs=None, "
  1026. "lineno=1, col_offset=0), lineno=1, col_offset=0)])"
  1027. )
  1028. def test_copy_location(self):
  1029. src = ast.parse('1 + 1', mode='eval')
  1030. src.body.right = ast.copy_location(ast.Num(2), src.body.right)
  1031. self.assertEqual(ast.dump(src, include_attributes=True),
  1032. 'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
  1033. 'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
  1034. 'col_offset=0))'
  1035. )
  1036. def test_fix_missing_locations(self):
  1037. src = ast.parse('write("spam")')
  1038. src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
  1039. [ast.Str('eggs')], [], None, None)))
  1040. self.assertEqual(src, ast.fix_missing_locations(src))
  1041. self.assertEqual(ast.dump(src, include_attributes=True),
  1042. "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
  1043. "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
  1044. "col_offset=6)], keywords=[], starargs=None, kwargs=None, "
  1045. "lineno=1, col_offset=0), lineno=1, col_offset=0), "
  1046. "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
  1047. "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
  1048. "keywords=[], starargs=None, kwargs=None, lineno=1, "
  1049. "col_offset=0), lineno=1, col_offset=0)])"
  1050. )
  1051. def test_increment_lineno(self):
  1052. src = ast.parse('1 + 1', mode='eval')
  1053. self.assertEqual(ast.increment_lineno(src, n=3), src)
  1054. self.assertEqual(ast.dump(src, include_attributes=True),
  1055. 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
  1056. 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
  1057. 'col_offset=0))'
  1058. )
  1059. # issue10869: do not increment lineno of root twice
  1060. src = ast.parse('1 + 1', mode='eval')
  1061. self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
  1062. self.assertEqual(ast.dump(src, include_attributes=True),
  1063. 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
  1064. 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
  1065. 'col_offset=0))'
  1066. )
  1067. def test_iter_fields(self):
  1068. node = ast.parse('foo()', mode='eval')
  1069. d = dict(ast.iter_fields(node.body))
  1070. self.assertEqual(d.pop('func').id, 'foo')
  1071. self.assertEqual(d, {'keywords': [], 'kwargs': None,
  1072. 'args': [], 'starargs': None})
  1073. def test_iter_child_nodes(self):
  1074. node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
  1075. self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
  1076. iterator = ast.iter_child_nodes(node.body)
  1077. self.assertEqual(next(iterator).id, 'spam')
  1078. self.assertEqual(next(iterator).n, 23)
  1079. self.assertEqual(next(iterator).n, 42)
  1080. self.assertEqual(ast.dump(next(iterator)),
  1081. "keyword(arg='eggs', value=Str(s='leek'))"
  1082. )
  1083. def test_get_docstring(self):
  1084. node = ast.parse('def foo():\n """line one\n line two"""')
  1085. self.assertEqual(ast.get_docstring(node.body[0]),
  1086. 'line one\nline two')
  1087. def test_literal_eval(self):
  1088. self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
  1089. self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
  1090. self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
  1091. self.assertRaises(ValueError, ast.literal_eval, 'foo()')
  1092. def test_literal_eval_issue4907(self):
  1093. self.assertEqual(ast.literal_eval('2j'), 2j)
  1094. self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
  1095. self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
  1096. self.assertRaises(ValueError, ast.literal_eval, '2 + (3 + 4j)')
  1097. def test_literal_eval_cp35572(self):
  1098. self.assertEqual(ast.literal_eval('-1'), -1)
  1099. self.assertEqual(ast.literal_eval('+1'), 1)
  1100. self.assertEqual(ast.literal_eval('-1j'), -1j)
  1101. self.assertEqual(ast.literal_eval('+1j'), 1j)
  1102. self.assertEqual(ast.literal_eval('-1.1'), -1.1)
  1103. self.assertEqual(ast.literal_eval('+1.1'), 1.1)
  1104. self.assertEqual(ast.literal_eval('-1L'), -1L)
  1105. self.assertEqual(ast.literal_eval('+1L'), 1L)
  1106. def test_main():
  1107. with test_support.check_py3k_warnings(("backquote not supported",
  1108. SyntaxWarning)):
  1109. test_support.run_unittest(AST_Tests, ASTHelpers_Test)
  1110. def main():
  1111. if __name__ != '__main__':
  1112. return
  1113. if sys.argv[1:] == ['-x']:
  1114. import os
  1115. # place for a quick individual test
  1116. p = ast.parse( "dict(a=1)", mode="exec") # call function with an argument
  1117. raw_input("attach to %s and press enter" % os.getpid())
  1118. c = compile(p,"<unknown>", mode="exec")
  1119. exec c
  1120. sys.exit()
  1121. if sys.argv[1:] == ['-g']:
  1122. for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
  1123. (eval_tests, "eval")):
  1124. print kind+"_results = ["
  1125. for s in statements:
  1126. print repr(to_tuple(compile(s, "?", kind, 0x400)))+","
  1127. print "]"
  1128. print "main()"
  1129. raise SystemExit
  1130. test_main()
  1131. #### GENERATED FOR IRONPYTHON ####
  1132. exec_results = [
  1133. ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Pass', (1, 9))], [])]),
  1134. ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',))], None, None, []), [('Pass', (1, 10))], [])]),
  1135. ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',))], None, None, [('Num', (1, 8), 1)]), [('Pass', (1, 12))], [])]),
  1136. ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], 'args', 'kwargs', []), [('Pass', (1, 24))], [])]),
  1137. ('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))], [])]),
  1138. ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [])]),
  1139. ('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
  1140. ('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
  1141. ('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]),
  1142. ('Module', [('Print', (1, 0), ('Name', (1, 8), 'f', ('Load',)), [('Num', (1, 11), 1)], False)]),
  1143. ('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
  1144. ('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
  1145. ('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
  1146. ('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [('If', (2, 0), ('Name', (2, 5), 'u', ('Load',)), [('Pass', (2, 7))], [('Pass', (3, 6))])])]),
  1147. ('Module', [('Raise', (1, 0), ('Name', (1, 6), 'Exception', ('Load',)), ('Str', (1, 17), 'string'), None)]),
  1148. ('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [])]),
  1149. ('Module', [('TryFinally', (1, 0), [('Pass', (2, 2))], [('Pass', (4, 2))])]),
  1150. ('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
  1151. ('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),
  1152. ('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]),
  1153. ('Module', [('Exec', (1, 0), ('Str', (1, 5), 'v'), None, None)]),
  1154. ('Module', [('Global', (1, 0), ['v'])]),
  1155. ('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]),
  1156. ('Module', [('Pass', (1, 0))]),
  1157. ('Module', [('While', (1, 0), ('Name', (1, 6), 'x', ('Load',)), [('Break', (1, 9))], [])]),
  1158. ('Module', [('While', (1, 0), ('Name', (1, 6), 'x', ('Load',)), [('Continue', (1, 9))], [])]),
  1159. ('Module', [('For', (1, 0), ('Tuple', (1, 4), [('Name', (1, 4), 'a', ('Store',)), ('Name', (1, 6), 'b', ('Store',))], ('Store',)), ('Name', (1, 11), 'c', ('Load',)), [('Pass', (1, 14))], [])]),
  1160. ('Module', [('Expr', (1, 0), ('ListComp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Na…

Large files files are truncated, but you can click here to view the full file