test_ast.py 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575
  1. import ast
  2. import builtins
  3. import dis
  4. import enum
  5. import os
  6. import sys
  7. import types
  8. import unittest
  9. import warnings
  10. import weakref
  11. from textwrap import dedent
  12. from test import support
  13. def to_tuple(t):
  14. if t is None or isinstance(t, (str, int, complex)) or t is Ellipsis:
  15. return t
  16. elif isinstance(t, list):
  17. return [to_tuple(e) for e in t]
  18. result = [t.__class__.__name__]
  19. if hasattr(t, 'lineno') and hasattr(t, 'col_offset'):
  20. result.append((t.lineno, t.col_offset))
  21. if hasattr(t, 'end_lineno') and hasattr(t, 'end_col_offset'):
  22. result[-1] += (t.end_lineno, t.end_col_offset)
  23. if t._fields is None:
  24. return tuple(result)
  25. for f in t._fields:
  26. result.append(to_tuple(getattr(t, f)))
  27. return tuple(result)
  28. # These tests are compiled through "exec"
  29. # There should be at least one test per statement
  30. exec_tests = [
  31. # None
  32. "None",
  33. # Module docstring
  34. "'module docstring'",
  35. # FunctionDef
  36. "def f(): pass",
  37. # FunctionDef with docstring
  38. "def f(): 'function docstring'",
  39. # FunctionDef with arg
  40. "def f(a): pass",
  41. # FunctionDef with arg and default value
  42. "def f(a=0): pass",
  43. # FunctionDef with varargs
  44. "def f(*args): pass",
  45. # FunctionDef with varargs as TypeVarTuple
  46. "def f(*args: *Ts): pass",
  47. # FunctionDef with varargs as unpacked Tuple
  48. "def f(*args: *tuple[int, ...]): pass",
  49. # FunctionDef with varargs as unpacked Tuple *and* TypeVarTuple
  50. "def f(*args: *tuple[int, *Ts]): pass",
  51. # FunctionDef with kwargs
  52. "def f(**kwargs): pass",
  53. # FunctionDef with all kind of args and docstring
  54. "def f(a, b=1, c=None, d=[], e={}, *args, f=42, **kwargs): 'doc for f()'",
  55. # FunctionDef with type annotation on return involving unpacking
  56. "def f() -> tuple[*Ts]: pass",
  57. "def f() -> tuple[int, *Ts]: pass",
  58. "def f() -> tuple[int, *tuple[int, ...]]: pass",
  59. # ClassDef
  60. "class C:pass",
  61. # ClassDef with docstring
  62. "class C: 'docstring for class C'",
  63. # ClassDef, new style class
  64. "class C(object): pass",
  65. # Return
  66. "def f():return 1",
  67. # Delete
  68. "del v",
  69. # Assign
  70. "v = 1",
  71. "a,b = c",
  72. "(a,b) = c",
  73. "[a,b] = c",
  74. # AnnAssign with unpacked types
  75. "x: tuple[*Ts]",
  76. "x: tuple[int, *Ts]",
  77. "x: tuple[int, *tuple[str, ...]]",
  78. # AugAssign
  79. "v += 1",
  80. # For
  81. "for v in v:pass",
  82. # While
  83. "while v:pass",
  84. # If
  85. "if v:pass",
  86. # If-Elif
  87. "if a:\n pass\nelif b:\n pass",
  88. # If-Elif-Else
  89. "if a:\n pass\nelif b:\n pass\nelse:\n pass",
  90. # With
  91. "with x as y: pass",
  92. "with x as y, z as q: pass",
  93. # Raise
  94. "raise Exception('string')",
  95. # TryExcept
  96. "try:\n pass\nexcept Exception:\n pass",
  97. # TryFinally
  98. "try:\n pass\nfinally:\n pass",
  99. # TryStarExcept
  100. "try:\n pass\nexcept* Exception:\n pass",
  101. # Assert
  102. "assert v",
  103. # Import
  104. "import sys",
  105. # ImportFrom
  106. "from sys import v",
  107. # Global
  108. "global v",
  109. # Expr
  110. "1",
  111. # Pass,
  112. "pass",
  113. # Break
  114. "for v in v:break",
  115. # Continue
  116. "for v in v:continue",
  117. # for statements with naked tuples (see http://bugs.python.org/issue6704)
  118. "for a,b in c: pass",
  119. "for (a,b) in c: pass",
  120. "for [a,b] in c: pass",
  121. # Multiline generator expression (test for .lineno & .col_offset)
  122. """(
  123. (
  124. Aa
  125. ,
  126. Bb
  127. )
  128. for
  129. Aa
  130. ,
  131. Bb in Cc
  132. )""",
  133. # dictcomp
  134. "{a : b for w in x for m in p if g}",
  135. # dictcomp with naked tuple
  136. "{a : b for v,w in x}",
  137. # setcomp
  138. "{r for l in x if g}",
  139. # setcomp with naked tuple
  140. "{r for l,m in x}",
  141. # AsyncFunctionDef
  142. "async def f():\n 'async function'\n await something()",
  143. # AsyncFor
  144. "async def f():\n async for e in i: 1\n else: 2",
  145. # AsyncWith
  146. "async def f():\n async with a as b: 1",
  147. # PEP 448: Additional Unpacking Generalizations
  148. "{**{1:2}, 2:3}",
  149. "{*{1, 2}, 3}",
  150. # Asynchronous comprehensions
  151. "async def f():\n [i async for b in c]",
  152. # Decorated FunctionDef
  153. "@deco1\n@deco2()\n@deco3(1)\ndef f(): pass",
  154. # Decorated AsyncFunctionDef
  155. "@deco1\n@deco2()\n@deco3(1)\nasync def f(): pass",
  156. # Decorated ClassDef
  157. "@deco1\n@deco2()\n@deco3(1)\nclass C: pass",
  158. # Decorator with generator argument
  159. "@deco(a for a in b)\ndef f(): pass",
  160. # Decorator with attribute
  161. "@a.b.c\ndef f(): pass",
  162. # Simple assignment expression
  163. "(a := 1)",
  164. # Positional-only arguments
  165. "def f(a, /,): pass",
  166. "def f(a, /, c, d, e): pass",
  167. "def f(a, /, c, *, d, e): pass",
  168. "def f(a, /, c, *, d, e, **kwargs): pass",
  169. # Positional-only arguments with defaults
  170. "def f(a=1, /,): pass",
  171. "def f(a=1, /, b=2, c=4): pass",
  172. "def f(a=1, /, b=2, *, c=4): pass",
  173. "def f(a=1, /, b=2, *, c): pass",
  174. "def f(a=1, /, b=2, *, c=4, **kwargs): pass",
  175. "def f(a=1, /, b=2, *, c, **kwargs): pass",
  176. ]
  177. # These are compiled through "single"
  178. # because of overlap with "eval", it just tests what
  179. # can't be tested with "eval"
  180. single_tests = [
  181. "1+2"
  182. ]
  183. # These are compiled through "eval"
  184. # It should test all expressions
  185. eval_tests = [
  186. # None
  187. "None",
  188. # BoolOp
  189. "a and b",
  190. # BinOp
  191. "a + b",
  192. # UnaryOp
  193. "not v",
  194. # Lambda
  195. "lambda:None",
  196. # Dict
  197. "{ 1:2 }",
  198. # Empty dict
  199. "{}",
  200. # Set
  201. "{None,}",
  202. # Multiline dict (test for .lineno & .col_offset)
  203. """{
  204. 1
  205. :
  206. 2
  207. }""",
  208. # ListComp
  209. "[a for b in c if d]",
  210. # GeneratorExp
  211. "(a for b in c if d)",
  212. # Comprehensions with multiple for targets
  213. "[(a,b) for a,b in c]",
  214. "[(a,b) for (a,b) in c]",
  215. "[(a,b) for [a,b] in c]",
  216. "{(a,b) for a,b in c}",
  217. "{(a,b) for (a,b) in c}",
  218. "{(a,b) for [a,b] in c}",
  219. "((a,b) for a,b in c)",
  220. "((a,b) for (a,b) in c)",
  221. "((a,b) for [a,b] in c)",
  222. # Yield - yield expressions can't work outside a function
  223. #
  224. # Compare
  225. "1 < 2 < 3",
  226. # Call
  227. "f(1,2,c=3,*d,**e)",
  228. # Call with multi-character starred
  229. "f(*[0, 1])",
  230. # Call with a generator argument
  231. "f(a for a in b)",
  232. # Num
  233. "10",
  234. # Str
  235. "'string'",
  236. # Attribute
  237. "a.b",
  238. # Subscript
  239. "a[b:c]",
  240. # Name
  241. "v",
  242. # List
  243. "[1,2,3]",
  244. # Empty list
  245. "[]",
  246. # Tuple
  247. "1,2,3",
  248. # Tuple
  249. "(1,2,3)",
  250. # Empty tuple
  251. "()",
  252. # Combination
  253. "a.b.c.d(a.b[1:2])",
  254. ]
  255. # TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
  256. # excepthandler, arguments, keywords, alias
  257. class AST_Tests(unittest.TestCase):
  258. def _is_ast_node(self, name, node):
  259. if not isinstance(node, type):
  260. return False
  261. if "ast" not in node.__module__:
  262. return False
  263. return name != 'AST' and name[0].isupper()
  264. def _assertTrueorder(self, ast_node, parent_pos):
  265. if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
  266. return
  267. if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
  268. node_pos = (ast_node.lineno, ast_node.col_offset)
  269. self.assertGreaterEqual(node_pos, parent_pos)
  270. parent_pos = (ast_node.lineno, ast_node.col_offset)
  271. for name in ast_node._fields:
  272. value = getattr(ast_node, name)
  273. if isinstance(value, list):
  274. first_pos = parent_pos
  275. if value and name == 'decorator_list':
  276. first_pos = (value[0].lineno, value[0].col_offset)
  277. for child in value:
  278. self._assertTrueorder(child, first_pos)
  279. elif value is not None:
  280. self._assertTrueorder(value, parent_pos)
  281. self.assertEqual(ast_node._fields, ast_node.__match_args__)
  282. def test_AST_objects(self):
  283. x = ast.AST()
  284. self.assertEqual(x._fields, ())
  285. x.foobar = 42
  286. self.assertEqual(x.foobar, 42)
  287. self.assertEqual(x.__dict__["foobar"], 42)
  288. with self.assertRaises(AttributeError):
  289. x.vararg
  290. with self.assertRaises(TypeError):
  291. # "ast.AST constructor takes 0 positional arguments"
  292. ast.AST(2)
  293. def test_AST_garbage_collection(self):
  294. class X:
  295. pass
  296. a = ast.AST()
  297. a.x = X()
  298. a.x.a = a
  299. ref = weakref.ref(a.x)
  300. del a
  301. support.gc_collect()
  302. self.assertIsNone(ref())
  303. def test_snippets(self):
  304. for input, output, kind in ((exec_tests, exec_results, "exec"),
  305. (single_tests, single_results, "single"),
  306. (eval_tests, eval_results, "eval")):
  307. for i, o in zip(input, output):
  308. with self.subTest(action="parsing", input=i):
  309. ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
  310. self.assertEqual(to_tuple(ast_tree), o)
  311. self._assertTrueorder(ast_tree, (0, 0))
  312. with self.subTest(action="compiling", input=i, kind=kind):
  313. compile(ast_tree, "?", kind)
  314. def test_ast_validation(self):
  315. # compile() is the only function that calls PyAST_Validate
  316. snippets_to_validate = exec_tests + single_tests + eval_tests
  317. for snippet in snippets_to_validate:
  318. tree = ast.parse(snippet)
  319. compile(tree, '<string>', 'exec')
  320. def test_invalid_position_information(self):
  321. invalid_linenos = [
  322. (10, 1), (-10, -11), (10, -11), (-5, -2), (-5, 1)
  323. ]
  324. for lineno, end_lineno in invalid_linenos:
  325. with self.subTest(f"Check invalid linenos {lineno}:{end_lineno}"):
  326. snippet = "a = 1"
  327. tree = ast.parse(snippet)
  328. tree.body[0].lineno = lineno
  329. tree.body[0].end_lineno = end_lineno
  330. with self.assertRaises(ValueError):
  331. compile(tree, '<string>', 'exec')
  332. invalid_col_offsets = [
  333. (10, 1), (-10, -11), (10, -11), (-5, -2), (-5, 1)
  334. ]
  335. for col_offset, end_col_offset in invalid_col_offsets:
  336. with self.subTest(f"Check invalid col_offset {col_offset}:{end_col_offset}"):
  337. snippet = "a = 1"
  338. tree = ast.parse(snippet)
  339. tree.body[0].col_offset = col_offset
  340. tree.body[0].end_col_offset = end_col_offset
  341. with self.assertRaises(ValueError):
  342. compile(tree, '<string>', 'exec')
  343. def test_compilation_of_ast_nodes_with_default_end_position_values(self):
  344. tree = ast.Module(body=[
  345. ast.Import(names=[ast.alias(name='builtins', lineno=1, col_offset=0)], lineno=1, col_offset=0),
  346. ast.Import(names=[ast.alias(name='traceback', lineno=0, col_offset=0)], lineno=0, col_offset=1)
  347. ], type_ignores=[])
  348. # Check that compilation doesn't crash. Note: this may crash explicitly only on debug mode.
  349. compile(tree, "<string>", "exec")
  350. def test_slice(self):
  351. slc = ast.parse("x[::]").body[0].value.slice
  352. self.assertIsNone(slc.upper)
  353. self.assertIsNone(slc.lower)
  354. self.assertIsNone(slc.step)
  355. def test_from_import(self):
  356. im = ast.parse("from . import y").body[0]
  357. self.assertIsNone(im.module)
  358. def test_non_interned_future_from_ast(self):
  359. mod = ast.parse("from __future__ import division")
  360. self.assertIsInstance(mod.body[0], ast.ImportFrom)
  361. mod.body[0].module = " __future__ ".strip()
  362. compile(mod, "<test>", "exec")
  363. def test_alias(self):
  364. im = ast.parse("from bar import y").body[0]
  365. self.assertEqual(len(im.names), 1)
  366. alias = im.names[0]
  367. self.assertEqual(alias.name, 'y')
  368. self.assertIsNone(alias.asname)
  369. self.assertEqual(alias.lineno, 1)
  370. self.assertEqual(alias.end_lineno, 1)
  371. self.assertEqual(alias.col_offset, 16)
  372. self.assertEqual(alias.end_col_offset, 17)
  373. im = ast.parse("from bar import *").body[0]
  374. alias = im.names[0]
  375. self.assertEqual(alias.name, '*')
  376. self.assertIsNone(alias.asname)
  377. self.assertEqual(alias.lineno, 1)
  378. self.assertEqual(alias.end_lineno, 1)
  379. self.assertEqual(alias.col_offset, 16)
  380. self.assertEqual(alias.end_col_offset, 17)
  381. im = ast.parse("from bar import y as z").body[0]
  382. alias = im.names[0]
  383. self.assertEqual(alias.name, "y")
  384. self.assertEqual(alias.asname, "z")
  385. self.assertEqual(alias.lineno, 1)
  386. self.assertEqual(alias.end_lineno, 1)
  387. self.assertEqual(alias.col_offset, 16)
  388. self.assertEqual(alias.end_col_offset, 22)
  389. im = ast.parse("import bar as foo").body[0]
  390. alias = im.names[0]
  391. self.assertEqual(alias.name, "bar")
  392. self.assertEqual(alias.asname, "foo")
  393. self.assertEqual(alias.lineno, 1)
  394. self.assertEqual(alias.end_lineno, 1)
  395. self.assertEqual(alias.col_offset, 7)
  396. self.assertEqual(alias.end_col_offset, 17)
  397. def test_base_classes(self):
  398. self.assertTrue(issubclass(ast.For, ast.stmt))
  399. self.assertTrue(issubclass(ast.Name, ast.expr))
  400. self.assertTrue(issubclass(ast.stmt, ast.AST))
  401. self.assertTrue(issubclass(ast.expr, ast.AST))
  402. self.assertTrue(issubclass(ast.comprehension, ast.AST))
  403. self.assertTrue(issubclass(ast.Gt, ast.AST))
  404. def test_field_attr_existence(self):
  405. for name, item in ast.__dict__.items():
  406. if self._is_ast_node(name, item):
  407. if name == 'Index':
  408. # Index(value) just returns value now.
  409. # The argument is required.
  410. continue
  411. x = item()
  412. if isinstance(x, ast.AST):
  413. self.assertEqual(type(x._fields), tuple)
  414. def test_arguments(self):
  415. x = ast.arguments()
  416. self.assertEqual(x._fields, ('posonlyargs', 'args', 'vararg', 'kwonlyargs',
  417. 'kw_defaults', 'kwarg', 'defaults'))
  418. with self.assertRaises(AttributeError):
  419. x.args
  420. self.assertIsNone(x.vararg)
  421. x = ast.arguments(*range(1, 8))
  422. self.assertEqual(x.args, 2)
  423. self.assertEqual(x.vararg, 3)
  424. def test_field_attr_writable(self):
  425. x = ast.Num()
  426. # We can assign to _fields
  427. x._fields = 666
  428. self.assertEqual(x._fields, 666)
  429. def test_classattrs(self):
  430. x = ast.Num()
  431. self.assertEqual(x._fields, ('value', 'kind'))
  432. with self.assertRaises(AttributeError):
  433. x.value
  434. with self.assertRaises(AttributeError):
  435. x.n
  436. x = ast.Num(42)
  437. self.assertEqual(x.value, 42)
  438. self.assertEqual(x.n, 42)
  439. with self.assertRaises(AttributeError):
  440. x.lineno
  441. with self.assertRaises(AttributeError):
  442. x.foobar
  443. x = ast.Num(lineno=2)
  444. self.assertEqual(x.lineno, 2)
  445. x = ast.Num(42, lineno=0)
  446. self.assertEqual(x.lineno, 0)
  447. self.assertEqual(x._fields, ('value', 'kind'))
  448. self.assertEqual(x.value, 42)
  449. self.assertEqual(x.n, 42)
  450. self.assertRaises(TypeError, ast.Num, 1, None, 2)
  451. self.assertRaises(TypeError, ast.Num, 1, None, 2, lineno=0)
  452. # Arbitrary keyword arguments are supported
  453. self.assertEqual(ast.Constant(1, foo='bar').foo, 'bar')
  454. self.assertEqual(ast.Num(1, foo='bar').foo, 'bar')
  455. with self.assertRaisesRegex(TypeError, "Num got multiple values for argument 'n'"):
  456. ast.Num(1, n=2)
  457. with self.assertRaisesRegex(TypeError, "Constant got multiple values for argument 'value'"):
  458. ast.Constant(1, value=2)
  459. self.assertEqual(ast.Num(42).n, 42)
  460. self.assertEqual(ast.Num(4.25).n, 4.25)
  461. self.assertEqual(ast.Num(4.25j).n, 4.25j)
  462. self.assertEqual(ast.Str('42').s, '42')
  463. self.assertEqual(ast.Bytes(b'42').s, b'42')
  464. self.assertIs(ast.NameConstant(True).value, True)
  465. self.assertIs(ast.NameConstant(False).value, False)
  466. self.assertIs(ast.NameConstant(None).value, None)
  467. self.assertEqual(ast.Constant(42).value, 42)
  468. self.assertEqual(ast.Constant(4.25).value, 4.25)
  469. self.assertEqual(ast.Constant(4.25j).value, 4.25j)
  470. self.assertEqual(ast.Constant('42').value, '42')
  471. self.assertEqual(ast.Constant(b'42').value, b'42')
  472. self.assertIs(ast.Constant(True).value, True)
  473. self.assertIs(ast.Constant(False).value, False)
  474. self.assertIs(ast.Constant(None).value, None)
  475. self.assertIs(ast.Constant(...).value, ...)
  476. def test_realtype(self):
  477. self.assertEqual(type(ast.Num(42)), ast.Constant)
  478. self.assertEqual(type(ast.Num(4.25)), ast.Constant)
  479. self.assertEqual(type(ast.Num(4.25j)), ast.Constant)
  480. self.assertEqual(type(ast.Str('42')), ast.Constant)
  481. self.assertEqual(type(ast.Bytes(b'42')), ast.Constant)
  482. self.assertEqual(type(ast.NameConstant(True)), ast.Constant)
  483. self.assertEqual(type(ast.NameConstant(False)), ast.Constant)
  484. self.assertEqual(type(ast.NameConstant(None)), ast.Constant)
  485. self.assertEqual(type(ast.Ellipsis()), ast.Constant)
  486. def test_isinstance(self):
  487. self.assertTrue(isinstance(ast.Num(42), ast.Num))
  488. self.assertTrue(isinstance(ast.Num(4.2), ast.Num))
  489. self.assertTrue(isinstance(ast.Num(4.2j), ast.Num))
  490. self.assertTrue(isinstance(ast.Str('42'), ast.Str))
  491. self.assertTrue(isinstance(ast.Bytes(b'42'), ast.Bytes))
  492. self.assertTrue(isinstance(ast.NameConstant(True), ast.NameConstant))
  493. self.assertTrue(isinstance(ast.NameConstant(False), ast.NameConstant))
  494. self.assertTrue(isinstance(ast.NameConstant(None), ast.NameConstant))
  495. self.assertTrue(isinstance(ast.Ellipsis(), ast.Ellipsis))
  496. self.assertTrue(isinstance(ast.Constant(42), ast.Num))
  497. self.assertTrue(isinstance(ast.Constant(4.2), ast.Num))
  498. self.assertTrue(isinstance(ast.Constant(4.2j), ast.Num))
  499. self.assertTrue(isinstance(ast.Constant('42'), ast.Str))
  500. self.assertTrue(isinstance(ast.Constant(b'42'), ast.Bytes))
  501. self.assertTrue(isinstance(ast.Constant(True), ast.NameConstant))
  502. self.assertTrue(isinstance(ast.Constant(False), ast.NameConstant))
  503. self.assertTrue(isinstance(ast.Constant(None), ast.NameConstant))
  504. self.assertTrue(isinstance(ast.Constant(...), ast.Ellipsis))
  505. self.assertFalse(isinstance(ast.Str('42'), ast.Num))
  506. self.assertFalse(isinstance(ast.Num(42), ast.Str))
  507. self.assertFalse(isinstance(ast.Str('42'), ast.Bytes))
  508. self.assertFalse(isinstance(ast.Num(42), ast.NameConstant))
  509. self.assertFalse(isinstance(ast.Num(42), ast.Ellipsis))
  510. self.assertFalse(isinstance(ast.NameConstant(True), ast.Num))
  511. self.assertFalse(isinstance(ast.NameConstant(False), ast.Num))
  512. self.assertFalse(isinstance(ast.Constant('42'), ast.Num))
  513. self.assertFalse(isinstance(ast.Constant(42), ast.Str))
  514. self.assertFalse(isinstance(ast.Constant('42'), ast.Bytes))
  515. self.assertFalse(isinstance(ast.Constant(42), ast.NameConstant))
  516. self.assertFalse(isinstance(ast.Constant(42), ast.Ellipsis))
  517. self.assertFalse(isinstance(ast.Constant(True), ast.Num))
  518. self.assertFalse(isinstance(ast.Constant(False), ast.Num))
  519. self.assertFalse(isinstance(ast.Constant(), ast.Num))
  520. self.assertFalse(isinstance(ast.Constant(), ast.Str))
  521. self.assertFalse(isinstance(ast.Constant(), ast.Bytes))
  522. self.assertFalse(isinstance(ast.Constant(), ast.NameConstant))
  523. self.assertFalse(isinstance(ast.Constant(), ast.Ellipsis))
  524. class S(str): pass
  525. self.assertTrue(isinstance(ast.Constant(S('42')), ast.Str))
  526. self.assertFalse(isinstance(ast.Constant(S('42')), ast.Num))
  527. def test_subclasses(self):
  528. class N(ast.Num):
  529. def __init__(self, *args, **kwargs):
  530. super().__init__(*args, **kwargs)
  531. self.z = 'spam'
  532. class N2(ast.Num):
  533. pass
  534. n = N(42)
  535. self.assertEqual(n.n, 42)
  536. self.assertEqual(n.z, 'spam')
  537. self.assertEqual(type(n), N)
  538. self.assertTrue(isinstance(n, N))
  539. self.assertTrue(isinstance(n, ast.Num))
  540. self.assertFalse(isinstance(n, N2))
  541. self.assertFalse(isinstance(ast.Num(42), N))
  542. n = N(n=42)
  543. self.assertEqual(n.n, 42)
  544. self.assertEqual(type(n), N)
  545. def test_module(self):
  546. body = [ast.Num(42)]
  547. x = ast.Module(body, [])
  548. self.assertEqual(x.body, body)
  549. def test_nodeclasses(self):
  550. # Zero arguments constructor explicitly allowed
  551. x = ast.BinOp()
  552. self.assertEqual(x._fields, ('left', 'op', 'right'))
  553. # Random attribute allowed too
  554. x.foobarbaz = 5
  555. self.assertEqual(x.foobarbaz, 5)
  556. n1 = ast.Num(1)
  557. n3 = ast.Num(3)
  558. addop = ast.Add()
  559. x = ast.BinOp(n1, addop, n3)
  560. self.assertEqual(x.left, n1)
  561. self.assertEqual(x.op, addop)
  562. self.assertEqual(x.right, n3)
  563. x = ast.BinOp(1, 2, 3)
  564. self.assertEqual(x.left, 1)
  565. self.assertEqual(x.op, 2)
  566. self.assertEqual(x.right, 3)
  567. x = ast.BinOp(1, 2, 3, lineno=0)
  568. self.assertEqual(x.left, 1)
  569. self.assertEqual(x.op, 2)
  570. self.assertEqual(x.right, 3)
  571. self.assertEqual(x.lineno, 0)
  572. # node raises exception when given too many arguments
  573. self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
  574. # node raises exception when given too many arguments
  575. self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
  576. # can set attributes through kwargs too
  577. x = ast.BinOp(left=1, op=2, right=3, lineno=0)
  578. self.assertEqual(x.left, 1)
  579. self.assertEqual(x.op, 2)
  580. self.assertEqual(x.right, 3)
  581. self.assertEqual(x.lineno, 0)
  582. # Random kwargs also allowed
  583. x = ast.BinOp(1, 2, 3, foobarbaz=42)
  584. self.assertEqual(x.foobarbaz, 42)
  585. def test_no_fields(self):
  586. # this used to fail because Sub._fields was None
  587. x = ast.Sub()
  588. self.assertEqual(x._fields, ())
  589. def test_pickling(self):
  590. import pickle
  591. mods = [pickle]
  592. try:
  593. import cPickle
  594. mods.append(cPickle)
  595. except ImportError:
  596. pass
  597. protocols = [0, 1, 2]
  598. for mod in mods:
  599. for protocol in protocols:
  600. for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
  601. ast2 = mod.loads(mod.dumps(ast, protocol))
  602. self.assertEqual(to_tuple(ast2), to_tuple(ast))
  603. def test_invalid_sum(self):
  604. pos = dict(lineno=2, col_offset=3)
  605. m = ast.Module([ast.Expr(ast.expr(**pos), **pos)], [])
  606. with self.assertRaises(TypeError) as cm:
  607. compile(m, "<test>", "exec")
  608. self.assertIn("but got <ast.expr", str(cm.exception))
  609. def test_invalid_identifier(self):
  610. m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))], [])
  611. ast.fix_missing_locations(m)
  612. with self.assertRaises(TypeError) as cm:
  613. compile(m, "<test>", "exec")
  614. self.assertIn("identifier must be of type str", str(cm.exception))
  615. def test_invalid_constant(self):
  616. for invalid_constant in int, (1, 2, int), frozenset((1, 2, int)):
  617. e = ast.Expression(body=ast.Constant(invalid_constant))
  618. ast.fix_missing_locations(e)
  619. with self.assertRaisesRegex(
  620. TypeError, "invalid type in Constant: type"
  621. ):
  622. compile(e, "<test>", "eval")
  623. def test_empty_yield_from(self):
  624. # Issue 16546: yield from value is not optional.
  625. empty_yield_from = ast.parse("def f():\n yield from g()")
  626. empty_yield_from.body[0].body[0].value.value = None
  627. with self.assertRaises(ValueError) as cm:
  628. compile(empty_yield_from, "<test>", "exec")
  629. self.assertIn("field 'value' is required", str(cm.exception))
  630. @support.cpython_only
  631. def test_issue31592(self):
  632. # There shouldn't be an assertion failure in case of a bad
  633. # unicodedata.normalize().
  634. import unicodedata
  635. def bad_normalize(*args):
  636. return None
  637. with support.swap_attr(unicodedata, 'normalize', bad_normalize):
  638. self.assertRaises(TypeError, ast.parse, '\u03D5')
  639. def test_issue18374_binop_col_offset(self):
  640. tree = ast.parse('4+5+6+7')
  641. parent_binop = tree.body[0].value
  642. child_binop = parent_binop.left
  643. grandchild_binop = child_binop.left
  644. self.assertEqual(parent_binop.col_offset, 0)
  645. self.assertEqual(parent_binop.end_col_offset, 7)
  646. self.assertEqual(child_binop.col_offset, 0)
  647. self.assertEqual(child_binop.end_col_offset, 5)
  648. self.assertEqual(grandchild_binop.col_offset, 0)
  649. self.assertEqual(grandchild_binop.end_col_offset, 3)
  650. tree = ast.parse('4+5-\\\n 6-7')
  651. parent_binop = tree.body[0].value
  652. child_binop = parent_binop.left
  653. grandchild_binop = child_binop.left
  654. self.assertEqual(parent_binop.col_offset, 0)
  655. self.assertEqual(parent_binop.lineno, 1)
  656. self.assertEqual(parent_binop.end_col_offset, 4)
  657. self.assertEqual(parent_binop.end_lineno, 2)
  658. self.assertEqual(child_binop.col_offset, 0)
  659. self.assertEqual(child_binop.lineno, 1)
  660. self.assertEqual(child_binop.end_col_offset, 2)
  661. self.assertEqual(child_binop.end_lineno, 2)
  662. self.assertEqual(grandchild_binop.col_offset, 0)
  663. self.assertEqual(grandchild_binop.lineno, 1)
  664. self.assertEqual(grandchild_binop.end_col_offset, 3)
  665. self.assertEqual(grandchild_binop.end_lineno, 1)
  666. def test_issue39579_dotted_name_end_col_offset(self):
  667. tree = ast.parse('@a.b.c\ndef f(): pass')
  668. attr_b = tree.body[0].decorator_list[0].value
  669. self.assertEqual(attr_b.end_col_offset, 4)
  670. def test_ast_asdl_signature(self):
  671. self.assertEqual(ast.withitem.__doc__, "withitem(expr context_expr, expr? optional_vars)")
  672. self.assertEqual(ast.GtE.__doc__, "GtE")
  673. self.assertEqual(ast.Name.__doc__, "Name(identifier id, expr_context ctx)")
  674. self.assertEqual(ast.cmpop.__doc__, "cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn")
  675. expressions = [f" | {node.__doc__}" for node in ast.expr.__subclasses__()]
  676. expressions[0] = f"expr = {ast.expr.__subclasses__()[0].__doc__}"
  677. self.assertCountEqual(ast.expr.__doc__.split("\n"), expressions)
  678. def test_positional_only_feature_version(self):
  679. ast.parse('def foo(x, /): ...', feature_version=(3, 8))
  680. ast.parse('def bar(x=1, /): ...', feature_version=(3, 8))
  681. with self.assertRaises(SyntaxError):
  682. ast.parse('def foo(x, /): ...', feature_version=(3, 7))
  683. with self.assertRaises(SyntaxError):
  684. ast.parse('def bar(x=1, /): ...', feature_version=(3, 7))
  685. ast.parse('lambda x, /: ...', feature_version=(3, 8))
  686. ast.parse('lambda x=1, /: ...', feature_version=(3, 8))
  687. with self.assertRaises(SyntaxError):
  688. ast.parse('lambda x, /: ...', feature_version=(3, 7))
  689. with self.assertRaises(SyntaxError):
  690. ast.parse('lambda x=1, /: ...', feature_version=(3, 7))
  691. def test_parenthesized_with_feature_version(self):
  692. ast.parse('with (CtxManager() as example): ...', feature_version=(3, 10))
  693. # While advertised as a feature in Python 3.10, this was allowed starting 3.9
  694. ast.parse('with (CtxManager() as example): ...', feature_version=(3, 9))
  695. with self.assertRaises(SyntaxError):
  696. ast.parse('with (CtxManager() as example): ...', feature_version=(3, 8))
  697. ast.parse('with CtxManager() as example: ...', feature_version=(3, 8))
  698. def test_debug_f_string_feature_version(self):
  699. ast.parse('f"{x=}"', feature_version=(3, 8))
  700. with self.assertRaises(SyntaxError):
  701. ast.parse('f"{x=}"', feature_version=(3, 7))
  702. def test_assignment_expression_feature_version(self):
  703. ast.parse('(x := 0)', feature_version=(3, 8))
  704. with self.assertRaises(SyntaxError):
  705. ast.parse('(x := 0)', feature_version=(3, 7))
  706. def test_exception_groups_feature_version(self):
  707. code = dedent('''
  708. try: ...
  709. except* Exception: ...
  710. ''')
  711. ast.parse(code)
  712. with self.assertRaises(SyntaxError):
  713. ast.parse(code, feature_version=(3, 10))
  714. def test_constant_as_name(self):
  715. for constant in "True", "False", "None":
  716. expr = ast.Expression(ast.Name(constant, ast.Load()))
  717. ast.fix_missing_locations(expr)
  718. with self.assertRaisesRegex(ValueError, f"identifier field can't represent '{constant}' constant"):
  719. compile(expr, "<test>", "eval")
  720. def test_precedence_enum(self):
  721. class _Precedence(enum.IntEnum):
  722. """Precedence table that originated from python grammar."""
  723. NAMED_EXPR = enum.auto() # <target> := <expr1>
  724. TUPLE = enum.auto() # <expr1>, <expr2>
  725. YIELD = enum.auto() # 'yield', 'yield from'
  726. TEST = enum.auto() # 'if'-'else', 'lambda'
  727. OR = enum.auto() # 'or'
  728. AND = enum.auto() # 'and'
  729. NOT = enum.auto() # 'not'
  730. CMP = enum.auto() # '<', '>', '==', '>=', '<=', '!=',
  731. # 'in', 'not in', 'is', 'is not'
  732. EXPR = enum.auto()
  733. BOR = EXPR # '|'
  734. BXOR = enum.auto() # '^'
  735. BAND = enum.auto() # '&'
  736. SHIFT = enum.auto() # '<<', '>>'
  737. ARITH = enum.auto() # '+', '-'
  738. TERM = enum.auto() # '*', '@', '/', '%', '//'
  739. FACTOR = enum.auto() # unary '+', '-', '~'
  740. POWER = enum.auto() # '**'
  741. AWAIT = enum.auto() # 'await'
  742. ATOM = enum.auto()
  743. def next(self):
  744. try:
  745. return self.__class__(self + 1)
  746. except ValueError:
  747. return self
  748. enum._test_simple_enum(_Precedence, ast._Precedence)
  749. @support.cpython_only
  750. def test_ast_recursion_limit(self):
  751. fail_depth = sys.getrecursionlimit() * 3
  752. crash_depth = sys.getrecursionlimit() * 300
  753. success_depth = int(fail_depth * 0.75)
  754. def check_limit(prefix, repeated):
  755. expect_ok = prefix + repeated * success_depth
  756. ast.parse(expect_ok)
  757. for depth in (fail_depth, crash_depth):
  758. broken = prefix + repeated * depth
  759. details = "Compiling ({!r} + {!r} * {})".format(
  760. prefix, repeated, depth)
  761. with self.assertRaises(RecursionError, msg=details):
  762. with support.infinite_recursion():
  763. ast.parse(broken)
  764. check_limit("a", "()")
  765. check_limit("a", ".b")
  766. check_limit("a", "[0]")
  767. check_limit("a", "*a")
  768. class ASTHelpers_Test(unittest.TestCase):
  769. maxDiff = None
  770. def test_parse(self):
  771. a = ast.parse('foo(1 + 1)')
  772. b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
  773. self.assertEqual(ast.dump(a), ast.dump(b))
  774. def test_parse_in_error(self):
  775. try:
  776. 1/0
  777. except Exception:
  778. with self.assertRaises(SyntaxError) as e:
  779. ast.literal_eval(r"'\U'")
  780. self.assertIsNotNone(e.exception.__context__)
  781. def test_dump(self):
  782. node = ast.parse('spam(eggs, "and cheese")')
  783. self.assertEqual(ast.dump(node),
  784. "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
  785. "args=[Name(id='eggs', ctx=Load()), Constant(value='and cheese')], "
  786. "keywords=[]))], type_ignores=[])"
  787. )
  788. self.assertEqual(ast.dump(node, annotate_fields=False),
  789. "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
  790. "Constant('and cheese')], []))], [])"
  791. )
  792. self.assertEqual(ast.dump(node, include_attributes=True),
  793. "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
  794. "lineno=1, col_offset=0, end_lineno=1, end_col_offset=4), "
  795. "args=[Name(id='eggs', ctx=Load(), lineno=1, col_offset=5, "
  796. "end_lineno=1, end_col_offset=9), Constant(value='and cheese', "
  797. "lineno=1, col_offset=11, end_lineno=1, end_col_offset=23)], keywords=[], "
  798. "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24), "
  799. "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24)], type_ignores=[])"
  800. )
  801. def test_dump_indent(self):
  802. node = ast.parse('spam(eggs, "and cheese")')
  803. self.assertEqual(ast.dump(node, indent=3), """\
  804. Module(
  805. body=[
  806. Expr(
  807. value=Call(
  808. func=Name(id='spam', ctx=Load()),
  809. args=[
  810. Name(id='eggs', ctx=Load()),
  811. Constant(value='and cheese')],
  812. keywords=[]))],
  813. type_ignores=[])""")
  814. self.assertEqual(ast.dump(node, annotate_fields=False, indent='\t'), """\
  815. Module(
  816. \t[
  817. \t\tExpr(
  818. \t\t\tCall(
  819. \t\t\t\tName('spam', Load()),
  820. \t\t\t\t[
  821. \t\t\t\t\tName('eggs', Load()),
  822. \t\t\t\t\tConstant('and cheese')],
  823. \t\t\t\t[]))],
  824. \t[])""")
  825. self.assertEqual(ast.dump(node, include_attributes=True, indent=3), """\
  826. Module(
  827. body=[
  828. Expr(
  829. value=Call(
  830. func=Name(
  831. id='spam',
  832. ctx=Load(),
  833. lineno=1,
  834. col_offset=0,
  835. end_lineno=1,
  836. end_col_offset=4),
  837. args=[
  838. Name(
  839. id='eggs',
  840. ctx=Load(),
  841. lineno=1,
  842. col_offset=5,
  843. end_lineno=1,
  844. end_col_offset=9),
  845. Constant(
  846. value='and cheese',
  847. lineno=1,
  848. col_offset=11,
  849. end_lineno=1,
  850. end_col_offset=23)],
  851. keywords=[],
  852. lineno=1,
  853. col_offset=0,
  854. end_lineno=1,
  855. end_col_offset=24),
  856. lineno=1,
  857. col_offset=0,
  858. end_lineno=1,
  859. end_col_offset=24)],
  860. type_ignores=[])""")
  861. def test_dump_incomplete(self):
  862. node = ast.Raise(lineno=3, col_offset=4)
  863. self.assertEqual(ast.dump(node),
  864. "Raise()"
  865. )
  866. self.assertEqual(ast.dump(node, include_attributes=True),
  867. "Raise(lineno=3, col_offset=4)"
  868. )
  869. node = ast.Raise(exc=ast.Name(id='e', ctx=ast.Load()), lineno=3, col_offset=4)
  870. self.assertEqual(ast.dump(node),
  871. "Raise(exc=Name(id='e', ctx=Load()))"
  872. )
  873. self.assertEqual(ast.dump(node, annotate_fields=False),
  874. "Raise(Name('e', Load()))"
  875. )
  876. self.assertEqual(ast.dump(node, include_attributes=True),
  877. "Raise(exc=Name(id='e', ctx=Load()), lineno=3, col_offset=4)"
  878. )
  879. self.assertEqual(ast.dump(node, annotate_fields=False, include_attributes=True),
  880. "Raise(Name('e', Load()), lineno=3, col_offset=4)"
  881. )
  882. node = ast.Raise(cause=ast.Name(id='e', ctx=ast.Load()))
  883. self.assertEqual(ast.dump(node),
  884. "Raise(cause=Name(id='e', ctx=Load()))"
  885. )
  886. self.assertEqual(ast.dump(node, annotate_fields=False),
  887. "Raise(cause=Name('e', Load()))"
  888. )
  889. def test_copy_location(self):
  890. src = ast.parse('1 + 1', mode='eval')
  891. src.body.right = ast.copy_location(ast.Num(2), src.body.right)
  892. self.assertEqual(ast.dump(src, include_attributes=True),
  893. 'Expression(body=BinOp(left=Constant(value=1, lineno=1, col_offset=0, '
  894. 'end_lineno=1, end_col_offset=1), op=Add(), right=Constant(value=2, '
  895. 'lineno=1, col_offset=4, end_lineno=1, end_col_offset=5), lineno=1, '
  896. 'col_offset=0, end_lineno=1, end_col_offset=5))'
  897. )
  898. src = ast.Call(col_offset=1, lineno=1, end_lineno=1, end_col_offset=1)
  899. new = ast.copy_location(src, ast.Call(col_offset=None, lineno=None))
  900. self.assertIsNone(new.end_lineno)
  901. self.assertIsNone(new.end_col_offset)
  902. self.assertEqual(new.lineno, 1)
  903. self.assertEqual(new.col_offset, 1)
  904. def test_fix_missing_locations(self):
  905. src = ast.parse('write("spam")')
  906. src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
  907. [ast.Str('eggs')], [])))
  908. self.assertEqual(src, ast.fix_missing_locations(src))
  909. self.maxDiff = None
  910. self.assertEqual(ast.dump(src, include_attributes=True),
  911. "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
  912. "lineno=1, col_offset=0, end_lineno=1, end_col_offset=5), "
  913. "args=[Constant(value='spam', lineno=1, col_offset=6, end_lineno=1, "
  914. "end_col_offset=12)], keywords=[], lineno=1, col_offset=0, end_lineno=1, "
  915. "end_col_offset=13), lineno=1, col_offset=0, end_lineno=1, "
  916. "end_col_offset=13), Expr(value=Call(func=Name(id='spam', ctx=Load(), "
  917. "lineno=1, col_offset=0, end_lineno=1, end_col_offset=0), "
  918. "args=[Constant(value='eggs', lineno=1, col_offset=0, end_lineno=1, "
  919. "end_col_offset=0)], keywords=[], lineno=1, col_offset=0, end_lineno=1, "
  920. "end_col_offset=0), lineno=1, col_offset=0, end_lineno=1, end_col_offset=0)], "
  921. "type_ignores=[])"
  922. )
  923. def test_increment_lineno(self):
  924. src = ast.parse('1 + 1', mode='eval')
  925. self.assertEqual(ast.increment_lineno(src, n=3), src)
  926. self.assertEqual(ast.dump(src, include_attributes=True),
  927. 'Expression(body=BinOp(left=Constant(value=1, lineno=4, col_offset=0, '
  928. 'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, '
  929. 'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, '
  930. 'col_offset=0, end_lineno=4, end_col_offset=5))'
  931. )
  932. # issue10869: do not increment lineno of root twice
  933. src = ast.parse('1 + 1', mode='eval')
  934. self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
  935. self.assertEqual(ast.dump(src, include_attributes=True),
  936. 'Expression(body=BinOp(left=Constant(value=1, lineno=4, col_offset=0, '
  937. 'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, '
  938. 'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, '
  939. 'col_offset=0, end_lineno=4, end_col_offset=5))'
  940. )
  941. src = ast.Call(
  942. func=ast.Name("test", ast.Load()), args=[], keywords=[], lineno=1
  943. )
  944. self.assertEqual(ast.increment_lineno(src).lineno, 2)
  945. self.assertIsNone(ast.increment_lineno(src).end_lineno)
  946. def test_increment_lineno_on_module(self):
  947. src = ast.parse(dedent("""\
  948. a = 1
  949. b = 2 # type: ignore
  950. c = 3
  951. d = 4 # type: ignore@tag
  952. """), type_comments=True)
  953. ast.increment_lineno(src, n=5)
  954. self.assertEqual(src.type_ignores[0].lineno, 7)
  955. self.assertEqual(src.type_ignores[1].lineno, 9)
  956. self.assertEqual(src.type_ignores[1].tag, '@tag')
  957. def test_iter_fields(self):
  958. node = ast.parse('foo()', mode='eval')
  959. d = dict(ast.iter_fields(node.body))
  960. self.assertEqual(d.pop('func').id, 'foo')
  961. self.assertEqual(d, {'keywords': [], 'args': []})
  962. def test_iter_child_nodes(self):
  963. node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
  964. self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
  965. iterator = ast.iter_child_nodes(node.body)
  966. self.assertEqual(next(iterator).id, 'spam')
  967. self.assertEqual(next(iterator).value, 23)
  968. self.assertEqual(next(iterator).value, 42)
  969. self.assertEqual(ast.dump(next(iterator)),
  970. "keyword(arg='eggs', value=Constant(value='leek'))"
  971. )
  972. def test_get_docstring(self):
  973. node = ast.parse('"""line one\n line two"""')
  974. self.assertEqual(ast.get_docstring(node),
  975. 'line one\nline two')
  976. node = ast.parse('class foo:\n """line one\n line two"""')
  977. self.assertEqual(ast.get_docstring(node.body[0]),
  978. 'line one\nline two')
  979. node = ast.parse('def foo():\n """line one\n line two"""')
  980. self.assertEqual(ast.get_docstring(node.body[0]),
  981. 'line one\nline two')
  982. node = ast.parse('async def foo():\n """spam\n ham"""')
  983. self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
  984. def test_get_docstring_none(self):
  985. self.assertIsNone(ast.get_docstring(ast.parse('')))
  986. node = ast.parse('x = "not docstring"')
  987. self.assertIsNone(ast.get_docstring(node))
  988. node = ast.parse('def foo():\n pass')
  989. self.assertIsNone(ast.get_docstring(node))
  990. node = ast.parse('class foo:\n pass')
  991. self.assertIsNone(ast.get_docstring(node.body[0]))
  992. node = ast.parse('class foo:\n x = "not docstring"')
  993. self.assertIsNone(ast.get_docstring(node.body[0]))
  994. node = ast.parse('class foo:\n def bar(self): pass')
  995. self.assertIsNone(ast.get_docstring(node.body[0]))
  996. node = ast.parse('def foo():\n pass')
  997. self.assertIsNone(ast.get_docstring(node.body[0]))
  998. node = ast.parse('def foo():\n x = "not docstring"')
  999. self.assertIsNone(ast.get_docstring(node.body[0]))
  1000. node = ast.parse('async def foo():\n pass')
  1001. self.assertIsNone(ast.get_docstring(node.body[0]))
  1002. node = ast.parse('async def foo():\n x = "not docstring"')
  1003. self.assertIsNone(ast.get_docstring(node.body[0]))
  1004. def test_multi_line_docstring_col_offset_and_lineno_issue16806(self):
  1005. node = ast.parse(
  1006. '"""line one\nline two"""\n\n'
  1007. 'def foo():\n """line one\n line two"""\n\n'
  1008. ' def bar():\n """line one\n line two"""\n'
  1009. ' """line one\n line two"""\n'
  1010. '"""line one\nline two"""\n\n'
  1011. )
  1012. self.assertEqual(node.body[0].col_offset, 0)
  1013. self.assertEqual(node.body[0].lineno, 1)
  1014. self.assertEqual(node.body[1].body[0].col_offset, 2)
  1015. self.assertEqual(node.body[1].body[0].lineno, 5)
  1016. self.assertEqual(node.body[1].body[1].body[0].col_offset, 4)
  1017. self.assertEqual(node.body[1].body[1].body[0].lineno, 9)
  1018. self.assertEqual(node.body[1].body[2].col_offset, 2)
  1019. self.assertEqual(node.body[1].body[2].lineno, 11)
  1020. self.assertEqual(node.body[2].col_offset, 0)
  1021. self.assertEqual(node.body[2].lineno, 13)
  1022. def test_elif_stmt_start_position(self):
  1023. node = ast.parse('if a:\n pass\nelif b:\n pass\n')
  1024. elif_stmt = node.body[0].orelse[0]
  1025. self.assertEqual(elif_stmt.lineno, 3)
  1026. self.assertEqual(elif_stmt.col_offset, 0)
  1027. def test_elif_stmt_start_position_with_else(self):
  1028. node = ast.parse('if a:\n pass\nelif b:\n pass\nelse:\n pass\n')
  1029. elif_stmt = node.body[0].orelse[0]
  1030. self.assertEqual(elif_stmt.lineno, 3)
  1031. self.assertEqual(elif_stmt.col_offset, 0)
  1032. def test_starred_expr_end_position_within_call(self):
  1033. node = ast.parse('f(*[0, 1])')
  1034. starred_expr = node.body[0].value.args[0]
  1035. self.assertEqual(starred_expr.end_lineno, 1)
  1036. self.assertEqual(starred_expr.end_col_offset, 9)
  1037. def test_literal_eval(self):
  1038. self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
  1039. self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
  1040. self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
  1041. self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3})
  1042. self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
  1043. self.assertEqual(ast.literal_eval('set()'), set())
  1044. self.assertRaises(ValueError, ast.literal_eval, 'foo()')
  1045. self.assertEqual(ast.literal_eval('6'), 6)
  1046. self.assertEqual(ast.literal_eval('+6'), 6)
  1047. self.assertEqual(ast.literal_eval('-6'), -6)
  1048. self.assertEqual(ast.literal_eval('3.25'), 3.25)
  1049. self.assertEqual(ast.literal_eval('+3.25'), 3.25)
  1050. self.assertEqual(ast.literal_eval('-3.25'), -3.25)
  1051. self.assertEqual(repr(ast.literal_eval('-0.0')), '-0.0')
  1052. self.assertRaises(ValueError, ast.literal_eval, '++6')
  1053. self.assertRaises(ValueError, ast.literal_eval, '+True')
  1054. self.assertRaises(ValueError, ast.literal_eval, '2+3')
  1055. def test_literal_eval_str_int_limit(self):
  1056. with support.adjust_int_max_str_digits(4000):
  1057. ast.literal_eval('3'*4000) # no error
  1058. with self.assertRaises(SyntaxError) as err_ctx:
  1059. ast.literal_eval('3'*4001)
  1060. self.assertIn('Exceeds the limit ', str(err_ctx.exception))
  1061. self.assertIn(' Consider hexadecimal ', str(err_ctx.exception))
  1062. def test_literal_eval_complex(self):
  1063. # Issue #4907
  1064. self.assertEqual(ast.literal_eval('6j'), 6j)
  1065. self.assertEqual(ast.literal_eval('-6j'), -6j)
  1066. self.assertEqual(ast.literal_eval('6.75j'), 6.75j)
  1067. self.assertEqual(ast.literal_eval('-6.75j'), -6.75j)
  1068. self.assertEqual(ast.literal_eval('3+6j'), 3+6j)
  1069. self.assertEqual(ast.literal_eval('-3+6j'), -3+6j)
  1070. self.assertEqual(ast.literal_eval('3-6j'), 3-6j)
  1071. self.assertEqual(ast.literal_eval('-3-6j'), -3-6j)
  1072. self.assertEqual(ast.literal_eval('3.25+6.75j'), 3.25+6.75j)
  1073. self.assertEqual(ast.literal_eval('-3.25+6.75j'), -3.25+6.75j)
  1074. self.assertEqual(ast.literal_eval('3.25-6.75j'), 3.25-6.75j)
  1075. self.assertEqual(ast.literal_eval('-3.25-6.75j'), -3.25-6.75j)
  1076. self.assertEqual(ast.literal_eval('(3+6j)'), 3+6j)
  1077. self.assertRaises(ValueError, ast.literal_eval, '-6j+3')
  1078. self.assertRaises(ValueError, ast.literal_eval, '-6j+3j')
  1079. self.assertRaises(ValueError, ast.literal_eval, '3+-6j')
  1080. self.assertRaises(ValueError, ast.literal_eval, '3+(0+6j)')
  1081. self.assertRaises(ValueError, ast.literal_eval, '-(3+6j)')
  1082. def test_literal_eval_malformed_dict_nodes(self):
  1083. malformed = ast.Dict(keys=[ast.Constant(1), ast.Constant(2)], values=[ast.Constant(3)])
  1084. self.assertRaises(ValueError, ast.literal_eval, malformed)
  1085. malformed = ast.Dict(keys=[ast.Constant(1)], values=[ast.Constant(2), ast.Constant(3)])
  1086. self.assertRaises(ValueError, ast.literal_eval, malformed)
  1087. def test_literal_eval_trailing_ws(self):
  1088. self.assertEqual(ast.literal_eval(" -1"), -1)
  1089. self.assertEqual(ast.literal_eval("\t\t-1"), -1)
  1090. self.assertEqual(ast.literal_eval(" \t -1"), -1)
  1091. self.assertRaises(IndentationError, ast.literal_eval, "\n -1")
  1092. def test_literal_eval_malformed_lineno(self):
  1093. msg = r'malformed node or string on line 3:'
  1094. with self.assertRaisesRegex(ValueError, msg):
  1095. ast.literal_eval("{'a': 1,\n'b':2,\n'c':++3,\n'd':4}")
  1096. node = ast.UnaryOp(
  1097. ast.UAdd(), ast.UnaryOp(ast.UAdd(), ast.Constant(6)))
  1098. self.assertIsNone(getattr(node, 'lineno', None))
  1099. msg = r'malformed node or string:'
  1100. with self.assertRaisesRegex(ValueError, msg):
  1101. ast.literal_eval(node)
  1102. def test_literal_eval_syntax_errors(self):
  1103. with self.assertRaisesRegex(SyntaxError, "unexpected indent"):
  1104. ast.literal_eval(r'''
  1105. \
  1106. (\
  1107. \ ''')
  1108. def test_bad_integer(self):
  1109. # issue13436: Bad error message with invalid numeric values
  1110. body = [ast.ImportFrom(module='time',
  1111. names=[ast.alias(name='sleep')],
  1112. level=None,
  1113. lineno=None, col_offset=None)]
  1114. mod = ast.Module(body, [])
  1115. with self.assertRaises(ValueError) as cm:
  1116. compile(mod, 'test', 'exec')
  1117. self.assertIn("invalid integer value: None", str(cm.exception))
  1118. def test_level_as_none(self):
  1119. body = [ast.ImportFrom(module='time',
  1120. names=[ast.alias(name='sleep',
  1121. lineno=0, col_offset=0)],
  1122. level=None,
  1123. lineno=0, col_offset=0)]
  1124. mod = ast.Module(body, [])
  1125. code = compile(mod, 'test', 'exec')
  1126. ns = {}
  1127. exec(code, ns)
  1128. self.assertIn('sleep', ns)
  1129. def test_recursion_direct(self):
  1130. e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
  1131. e.operand = e
  1132. with self.assertRaises(RecursionError):
  1133. with support.infinite_recursion():
  1134. compile(ast.Expression(e), "<test>", "eval")
  1135. def test_recursion_indirect(self):
  1136. e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
  1137. f = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
  1138. e.operand = f
  1139. f.operand = e
  1140. with self.assertRaises(RecursionError):
  1141. with support.infinite_recursion():
  1142. compile(ast.Expression(e), "<test>", "eval")
  1143. class ASTValidatorTests(unittest.TestCase):
  1144. def mod(self, mod, msg=None, mode="exec", *, exc=ValueError):
  1145. mod.lineno = mod.col_offset = 0
  1146. ast.fix_missing_locations(mod)
  1147. if msg is None:
  1148. compile(mod, "<test>", mode)
  1149. else:
  1150. with self.assertRaises(exc) as cm:
  1151. compile(mod, "<test>", mode)
  1152. self.assertIn(msg, str(cm.exception))
  1153. def expr(self, node, msg=None, *, exc=ValueError):
  1154. mod = ast.Module([ast.Expr(node)], [])
  1155. self.mod(mod, msg, exc=exc)
  1156. def stmt(self, stmt, msg=None):
  1157. mod = ast.Module([stmt], [])
  1158. self.mod(mod, msg)
  1159. def test_module(self):
  1160. m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))])
  1161. self.mod(m, "must have Load context", "single")
  1162. m = ast.Expression(ast.Name("x", ast.Store()))
  1163. self.mod(m, "must have Load context", "eval")
  1164. def _check_arguments(self, fac, check):
  1165. def arguments(args=None, posonlyargs=None, vararg=None,
  1166. kwonlyargs=None, kwarg=None,
  1167. defaults=None, kw_defaults=None):
  1168. if args is None:
  1169. args = []
  1170. if posonlyargs is None:
  1171. posonlyargs = []
  1172. if kwonlyargs is None:
  1173. kwonlyargs = []
  1174. if defaults is None:
  1175. defaults = []
  1176. if kw_defaults is None:
  1177. kw_defaults = []
  1178. args = ast.arguments(args, posonlyargs, vararg, kwonlyargs,
  1179. kw_defaults, kwarg, defaults)
  1180. return fac(args)
  1181. args = [ast.arg("x", ast.Name("x", ast.Store()))]
  1182. check(arguments(args=args), "must have Load context")
  1183. check(arguments(posonlyargs=args), "must have Load context")
  1184. check(arguments(kwonlyargs=args), "must have Load context")
  1185. check(arguments(defaults=[ast.Num(3)]),
  1186. "more positional defaults than args")
  1187. check(arguments(kw_defaults=[ast.Num(4)]),
  1188. "length of kwonlyargs is not the same as kw_defaults")
  1189. args = [ast.arg("x", ast.Name("x", ast.Load()))]
  1190. check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]),
  1191. "must have Load context")
  1192. args = [ast.arg("a", ast.Name("x", ast.Load())),
  1193. ast.arg("b", ast.Name("y", ast.Load()))]
  1194. check(arguments(kwonlyargs=args,
  1195. kw_defaults=[None, ast.Name("x", ast.Store())]),
  1196. "must have Load context")
  1197. def test_funcdef(self):
  1198. a = ast.arguments([], [], None, [], [], None, [])
  1199. f = ast.FunctionDef("x", a, [], [], None)
  1200. self.stmt(f, "empty body on FunctionDef")
  1201. f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())],
  1202. None)
  1203. self.stmt(f, "must have Load context")
  1204. f = ast.FunctionDef("x", a, [ast.Pass()], [],
  1205. ast.Name("x", ast.Store()))
  1206. self.stmt(f, "must have Load context")
  1207. def fac(args):
  1208. return ast.FunctionDef("x", args, [ast.Pass()], [], None)
  1209. self._check_arguments(fac, self.stmt)
  1210. def test_classdef(self):
  1211. def cls(bases=None, keywords=None, body=None, decorator_list=None):
  1212. if bases is None:
  1213. bases = []
  1214. if keywords is None:
  1215. keywords = []
  1216. if body is None:
  1217. body = [ast.Pass()]
  1218. if decorator_list is None:
  1219. decorator_list = []
  1220. return ast.ClassDef("myclass", bases, keywords,
  1221. body, decorator_list)
  1222. self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
  1223. "must have Load context")
  1224. self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
  1225. "must have Load context")
  1226. self.stmt(cls(body=[]), "empty body on ClassDef")
  1227. self.stmt(cls(body=[None]), "None disallowed")
  1228. self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
  1229. "must have Load context")
  1230. def test_delete(self):
  1231. self.stmt(ast.Delete([]), "empty targets on Delete")
  1232. self.stmt(ast.Delete([None]), "None disallowed")
  1233. self.stmt(ast.Delete([ast.Name("x", ast.Load())]),
  1234. "must have Del context")
  1235. def test_assign(self):
  1236. self.stmt(ast.Assign([], ast.Num(3)), "empty targets on Assign")
  1237. self.stmt(ast.Assign([None], ast.Num(3)), "None disallowed")
  1238. self.stmt(ast.Assign([ast.Name("x", ast.Load())], ast.Num(3)),
  1239. "must have Store context")
  1240. self.stmt(ast.Assign([ast.Name("x", ast.Store())],
  1241. ast.Name("y", ast.Store())),
  1242. "must have Load context")
  1243. def test_augassign(self):
  1244. aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(),
  1245. ast.Name("y", ast.Load()))
  1246. self.stmt(aug, "must have Store context")
  1247. aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(),
  1248. ast.Name("y", ast.Store()))
  1249. self.stmt(aug, "must have Load context")
  1250. def test_for(self):
  1251. x = ast.Name("x", ast.Store())
  1252. y = ast.Name("y", ast.Load())
  1253. p = ast.Pass()
  1254. self.stmt(ast.For(x, y, [], []), "empty body on For")
  1255. self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []),
  1256. "must have Store context")
  1257. self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []),
  1258. "must have Load context")
  1259. e = ast.Expr(ast.Name("x", ast.Store()))
  1260. self.stmt(ast.For(x, y, [e], []), "must have Load context")
  1261. self.stmt(ast.For(x, y, [p], [e]), "must have Load context")
  1262. def test_while(self):
  1263. self.stmt(ast.While(ast.Num(3), [], []), "empty body on While")
  1264. self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []),
  1265. "must have Load context")
  1266. self.stmt(ast.While(ast.Num(3), [ast.Pass()],
  1267. [ast.Expr(ast.Name("x", ast.Store()))]),
  1268. "must have Load context")
  1269. def test_if(self):
  1270. self.stmt(ast.If(ast.Num(3), [], []), "empty body on If")
  1271. i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], [])
  1272. self.stmt(i, "must have Load context")
  1273. i = ast.If(ast.Num(3), [ast.Expr(ast.Name("x", ast.Store()))], [])
  1274. self.stmt(i, "must have Load context")
  1275. i = ast.If(ast.Num(3), [ast.Pass()],
  1276. [ast.Expr(ast.Name("x", ast.Store()))])
  1277. self.stmt(i, "must have Load context")
  1278. def test_with(self):
  1279. p = ast.Pass()
  1280. self.stmt(ast.With([], [p]), "empty items on With")
  1281. i = ast.withitem(ast.Num(3), None)
  1282. self.stmt(ast.With([i], []), "empty body on With")
  1283. i = ast.withitem(ast.Name("x", ast.Store()), None)
  1284. self.stmt(ast.With([i], [p]), "must have Load context")
  1285. i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load()))
  1286. self.stmt(ast.With([i], [p]), "must have Store context")
  1287. def test_raise(self):
  1288. r = ast.Raise(None, ast.Num(3))
  1289. self.stmt(r, "Raise with cause but no exception")
  1290. r = ast.Raise(ast.Name("x", ast.Store()), None)
  1291. self.stmt(r, "must have Load context")
  1292. r = ast.Raise(ast.Num(4), ast.Name("x", ast.Store()))
  1293. self.stmt(r, "must have Load context")
  1294. def test_try(self):
  1295. p = ast.Pass()
  1296. t = ast.Try([], [], [], [p])
  1297. self.stmt(t, "empty body on Try")
  1298. t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
  1299. self.stmt(t, "must have Load context")
  1300. t = ast.Try([p], [], [], [])
  1301. self.stmt(t, "Try has neither except handlers nor finalbody")
  1302. t = ast.Try([p], [], [p], [p])
  1303. self.stmt(t, "Try has orelse but no except handlers")
  1304. t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], [])
  1305. self.stmt(t, "empty body on ExceptHandler")
  1306. e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
  1307. self.stmt(ast.Try([p], e, [], []), "must have Load context")
  1308. e = [ast.ExceptHandler(None, "x", [p])]
  1309. t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
  1310. self.stmt(t, "must have Load context")
  1311. t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
  1312. self.stmt(t, "must have Load context")
  1313. def test_try_star(self):
  1314. p = ast.Pass()
  1315. t = ast.TryStar([], [], [], [p])
  1316. self.stmt(t, "empty body on TryStar")
  1317. t = ast.TryStar([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
  1318. self.stmt(t, "must have Load context")
  1319. t = ast.TryStar([p], [], [], [])
  1320. self.stmt(t, "TryStar has neither except handlers nor finalbody")
  1321. t = ast.TryStar([p], [], [p], [p])
  1322. self.stmt(t, "TryStar has orelse but no except handlers")
  1323. t = ast.TryStar([p], [ast.ExceptHandler(None, "x", [])], [], [])
  1324. self.stmt(t, "empty body on ExceptHandler")
  1325. e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
  1326. self.stmt(ast.TryStar([p], e, [], []), "must have Load context")
  1327. e = [ast.ExceptHandler(None, "x", [p])]
  1328. t = ast.TryStar([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
  1329. self.stmt(t, "must have Load context")
  1330. t = ast.TryStar([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
  1331. self.stmt(t, "must have Load context")
  1332. def test_assert(self):
  1333. self.stmt(ast.Assert(ast.Name("x", ast.Store()), None),
  1334. "must have Load context")
  1335. assrt = ast.Assert(ast.Name("x", ast.Load()),
  1336. ast.Name("y", ast.Store()))
  1337. self.stmt(assrt, "must have Load context")
  1338. def test_import(self):
  1339. self.stmt(ast.Import([]), "empty names on Import")
  1340. def test_importfrom(self):
  1341. imp = ast.ImportFrom(None, [ast.alias("x", None)], -42)
  1342. self.stmt(imp, "Negative ImportFrom level")
  1343. self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
  1344. def test_global(self):
  1345. self.stmt(ast.Global([]), "empty names on Global")
  1346. def test_nonlocal(self):
  1347. self.stmt(ast.Nonlocal([]), "empty names on Nonlocal")
  1348. def test_expr(self):
  1349. e = ast.Expr(ast.Name("x", ast.Store()))
  1350. self.stmt(e, "must have Load context")
  1351. def test_boolop(self):
  1352. b = ast.BoolOp(ast.And(), [])
  1353. self.expr(b, "less than 2 values")
  1354. b = ast.BoolOp(ast.And(), [ast.Num(3)])
  1355. self.expr(b, "less than 2 values")
  1356. b = ast.BoolOp(ast.And(), [ast.Num(4), None])
  1357. self.expr(b, "None disallowed")
  1358. b = ast.BoolOp(ast.And(), [ast.Num(4), ast.Name("x", ast.Store())])
  1359. self.expr(b, "must have Load context")
  1360. def test_unaryop(self):
  1361. u = ast.UnaryOp(ast.Not(), ast.Name("x", ast.Store()))
  1362. self.expr(u, "must have Load context")
  1363. def test_lambda(self):
  1364. a = ast.arguments([], [], None, [], [], None, [])
  1365. self.expr(ast.Lambda(a, ast.Name("x", ast.Store())),
  1366. "must have Load context")
  1367. def fac(args):
  1368. return ast.Lambda(args, ast.Name("x", ast.Load()))
  1369. self._check_arguments(fac, self.expr)
  1370. def test_ifexp(self):
  1371. l = ast.Name("x", ast.Load())
  1372. s = ast.Name("y", ast.Store())
  1373. for args in (s, l, l), (l, s, l), (l, l, s):
  1374. self.expr(ast.IfExp(*args), "must have Load context")
  1375. def test_dict(self):
  1376. d = ast.Dict([], [ast.Name("x", ast.Load())])
  1377. self.expr(d, "same number of keys as values")
  1378. d = ast.Dict([ast.Name("x", ast.Load())], [None])
  1379. self.expr(d, "None disallowed")
  1380. def test_set(self):
  1381. self.expr(ast.Set([None]), "None disallowed")
  1382. s = ast.Set([ast.Name("x", ast.Store())])
  1383. self.expr(s, "must have Load context")
  1384. def _check_comprehension(self, fac):
  1385. self.expr(fac([]), "comprehension with no generators")
  1386. g = ast.comprehension(ast.Name("x", ast.Load()),
  1387. ast.Name("x", ast.Load()), [], 0)
  1388. self.expr(fac([g]), "must have Store context")
  1389. g = ast.comprehension(ast.Name("x", ast.Store()),
  1390. ast.Name("x", ast.Store()), [], 0)
  1391. self.expr(fac([g]), "must have Load context")
  1392. x = ast.Name("x", ast.Store())
  1393. y = ast.Name("y", ast.Load())
  1394. g = ast.comprehension(x, y, [None], 0)
  1395. self.expr(fac([g]), "None disallowed")
  1396. g = ast.comprehension(x, y, [ast.Name("x", ast.Store())], 0)
  1397. self.expr(fac([g]), "must have Load context")
  1398. def _simple_comp(self, fac):
  1399. g = ast.comprehension(ast.Name("x", ast.Store()),
  1400. ast.Name("x", ast.Load()), [], 0)
  1401. self.expr(fac(ast.Name("x", ast.Store()), [g]),
  1402. "must have Load context")
  1403. def wrap(gens):
  1404. return fac(ast.Name("x", ast.Store()), gens)
  1405. self._check_comprehension(wrap)
  1406. def test_listcomp(self):
  1407. self._simple_comp(ast.ListComp)
  1408. def test_setcomp(self):
  1409. self._simple_comp(ast.SetComp)
  1410. def test_generatorexp(self):
  1411. self._simple_comp(ast.GeneratorExp)
  1412. def test_dictcomp(self):
  1413. g = ast.comprehension(ast.Name("y", ast.Store()),
  1414. ast.Name("p", ast.Load()), [], 0)
  1415. c = ast.DictComp(ast.Name("x", ast.Store()),
  1416. ast.Name("y", ast.Load()), [g])
  1417. self.expr(c, "must have Load context")
  1418. c = ast.DictComp(ast.Name("x", ast.Load()),
  1419. ast.Name("y", ast.Store()), [g])
  1420. self.expr(c, "must have Load context")
  1421. def factory(comps):
  1422. k = ast.Name("x", ast.Load())
  1423. v = ast.Name("y", ast.Load())
  1424. return ast.DictComp(k, v, comps)
  1425. self._check_comprehension(factory)
  1426. def test_yield(self):
  1427. self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load")
  1428. self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load")
  1429. def test_compare(self):
  1430. left = ast.Name("x", ast.Load())
  1431. comp = ast.Compare(left, [ast.In()], [])
  1432. self.expr(comp, "no comparators")
  1433. comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)])
  1434. self.expr(comp, "different number of comparators and operands")
  1435. comp = ast.Compare(ast.Num("blah"), [ast.In()], [left])
  1436. self.expr(comp)
  1437. comp = ast.Compare(left, [ast.In()], [ast.Num("blah")])
  1438. self.expr(comp)
  1439. def test_call(self):
  1440. func = ast.Name("x", ast.Load())
  1441. args = [ast.Name("y", ast.Load())]
  1442. keywords = [ast.keyword("w", ast.Name("z", ast.Load()))]
  1443. call = ast.Call(ast.Name("x", ast.Store()), args, keywords)
  1444. self.expr(call, "must have Load context")
  1445. call = ast.Call(func, [None], keywords)
  1446. self.expr(call, "None disallowed")
  1447. bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))]
  1448. call = ast.Call(func, args, bad_keywords)
  1449. self.expr(call, "must have Load context")
  1450. def test_num(self):
  1451. class subint(int):
  1452. pass
  1453. class subfloat(float):
  1454. pass
  1455. class subcomplex(complex):
  1456. pass
  1457. for obj in "0", "hello":
  1458. self.expr(ast.Num(obj))
  1459. for obj in subint(), subfloat(), subcomplex():
  1460. self.expr(ast.Num(obj), "invalid type", exc=TypeError)
  1461. def test_attribute(self):
  1462. attr = ast.Attribute(ast.Name("x", ast.Store()), "y", ast.Load())
  1463. self.expr(attr, "must have Load context")
  1464. def test_subscript(self):
  1465. sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Num(3),
  1466. ast.Load())
  1467. self.expr(sub, "must have Load context")
  1468. x = ast.Name("x", ast.Load())
  1469. sub = ast.Subscript(x, ast.Name("y", ast.Store()),
  1470. ast.Load())
  1471. self.expr(sub, "must have Load context")
  1472. s = ast.Name("x", ast.Store())
  1473. for args in (s, None, None), (None, s, None), (None, None, s):
  1474. sl = ast.Slice(*args)
  1475. self.expr(ast.Subscript(x, sl, ast.Load()),
  1476. "must have Load context")
  1477. sl = ast.Tuple([], ast.Load())
  1478. self.expr(ast.Subscript(x, sl, ast.Load()))
  1479. sl = ast.Tuple([s], ast.Load())
  1480. self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
  1481. def test_starred(self):
  1482. left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())],
  1483. ast.Store())
  1484. assign = ast.Assign([left], ast.Num(4))
  1485. self.stmt(assign, "must have Store context")
  1486. def _sequence(self, fac):
  1487. self.expr(fac([None], ast.Load()), "None disallowed")
  1488. self.expr(fac([ast.Name("x", ast.Store())], ast.Load()),
  1489. "must have Load context")
  1490. def test_list(self):
  1491. self._sequence(ast.List)
  1492. def test_tuple(self):
  1493. self._sequence(ast.Tuple)
  1494. def test_nameconstant(self):
  1495. self.expr(ast.NameConstant(4))
  1496. def test_stdlib_validates(self):
  1497. stdlib = os.path.dirname(ast.__file__)
  1498. tests = [fn for fn in os.listdir(stdlib) if fn.endswith(".py")]
  1499. tests.extend(["test/test_grammar.py", "test/test_unpack_ex.py"])
  1500. for module in tests:
  1501. with self.subTest(module):
  1502. fn = os.path.join(stdlib, module)
  1503. with open(fn, "r", encoding="utf-8") as fp:
  1504. source = fp.read()
  1505. mod = ast.parse(source, fn)
  1506. compile(mod, fn, "exec")
  1507. constant_1 = ast.Constant(1)
  1508. pattern_1 = ast.MatchValue(constant_1)
  1509. constant_x = ast.Constant('x')
  1510. pattern_x = ast.MatchValue(constant_x)
  1511. constant_true = ast.Constant(True)
  1512. pattern_true = ast.MatchSingleton(True)
  1513. name_carter = ast.Name('carter', ast.Load())
  1514. _MATCH_PATTERNS = [
  1515. ast.MatchValue(
  1516. ast.Attribute(
  1517. ast.Attribute(
  1518. ast.Name('x', ast.Store()),
  1519. 'y', ast.Load()
  1520. ),
  1521. 'z', ast.Load()
  1522. )
  1523. ),
  1524. ast.MatchValue(
  1525. ast.Attribute(
  1526. ast.Attribute(
  1527. ast.Name('x', ast.Load()),
  1528. 'y', ast.Store()
  1529. ),
  1530. 'z', ast.Load()
  1531. )
  1532. ),
  1533. ast.MatchValue(
  1534. ast.Constant(...)
  1535. ),
  1536. ast.MatchValue(
  1537. ast.Constant(True)
  1538. ),
  1539. ast.MatchValue(
  1540. ast.Constant((1,2,3))
  1541. ),
  1542. ast.MatchSingleton('string'),
  1543. ast.MatchSequence([
  1544. ast.MatchSingleton('string')
  1545. ]),
  1546. ast.MatchSequence(
  1547. [
  1548. ast.MatchSequence(
  1549. [
  1550. ast.MatchSingleton('string')
  1551. ]
  1552. )
  1553. ]
  1554. ),
  1555. ast.MatchMapping(
  1556. [constant_1, constant_true],
  1557. [pattern_x]
  1558. ),
  1559. ast.MatchMapping(
  1560. [constant_true, constant_1],
  1561. [pattern_x, pattern_1],
  1562. rest='True'
  1563. ),
  1564. ast.MatchMapping(
  1565. [constant_true, ast.Starred(ast.Name('lol', ast.Load()), ast.Load())],
  1566. [pattern_x, pattern_1],
  1567. rest='legit'
  1568. ),
  1569. ast.MatchClass(
  1570. ast.Attribute(
  1571. ast.Attribute(
  1572. constant_x,
  1573. 'y', ast.Load()),
  1574. 'z', ast.Load()),
  1575. patterns=[], kwd_attrs=[], kwd_patterns=[]
  1576. ),
  1577. ast.MatchClass(
  1578. name_carter,
  1579. patterns=[],
  1580. kwd_attrs=['True'],
  1581. kwd_patterns=[pattern_1]
  1582. ),
  1583. ast.MatchClass(
  1584. name_carter,
  1585. patterns=[],
  1586. kwd_attrs=[],
  1587. kwd_patterns=[pattern_1]
  1588. ),
  1589. ast.MatchClass(
  1590. name_carter,
  1591. patterns=[ast.MatchSingleton('string')],
  1592. kwd_attrs=[],
  1593. kwd_patterns=[]
  1594. ),
  1595. ast.MatchClass(
  1596. name_carter,
  1597. patterns=[ast.MatchStar()],
  1598. kwd_attrs=[],
  1599. kwd_patterns=[]
  1600. ),
  1601. ast.MatchClass(
  1602. name_carter,
  1603. patterns=[],
  1604. kwd_attrs=[],
  1605. kwd_patterns=[ast.MatchStar()]
  1606. ),
  1607. ast.MatchSequence(
  1608. [
  1609. ast.MatchStar("True")
  1610. ]
  1611. ),
  1612. ast.MatchAs(
  1613. name='False'
  1614. ),
  1615. ast.MatchOr(
  1616. []
  1617. ),
  1618. ast.MatchOr(
  1619. [pattern_1]
  1620. ),
  1621. ast.MatchOr(
  1622. [pattern_1, pattern_x, ast.MatchSingleton('xxx')]
  1623. ),
  1624. ast.MatchAs(name="_"),
  1625. ast.MatchStar(name="x"),
  1626. ast.MatchSequence([ast.MatchStar("_")]),
  1627. ast.MatchMapping([], [], rest="_"),
  1628. ]
  1629. def test_match_validation_pattern(self):
  1630. name_x = ast.Name('x', ast.Load())
  1631. for pattern in self._MATCH_PATTERNS:
  1632. with self.subTest(ast.dump(pattern, indent=4)):
  1633. node = ast.Match(
  1634. subject=name_x,
  1635. cases = [
  1636. ast.match_case(
  1637. pattern=pattern,
  1638. body = [ast.Pass()]
  1639. )
  1640. ]
  1641. )
  1642. node = ast.fix_missing_locations(node)
  1643. module = ast.Module([node], [])
  1644. with self.assertRaises(ValueError):
  1645. compile(module, "<test>", "exec")
  1646. class ConstantTests(unittest.TestCase):
  1647. """Tests on the ast.Constant node type."""
  1648. def compile_constant(self, value):
  1649. tree = ast.parse("x = 123")
  1650. node = tree.body[0].value
  1651. new_node = ast.Constant(value=value)
  1652. ast.copy_location(new_node, node)
  1653. tree.body[0].value = new_node
  1654. code = compile(tree, "<string>", "exec")
  1655. ns = {}
  1656. exec(code, ns)
  1657. return ns['x']
  1658. def test_validation(self):
  1659. with self.assertRaises(TypeError) as cm:
  1660. self.compile_constant([1, 2, 3])
  1661. self.assertEqual(str(cm.exception),
  1662. "got an invalid type in Constant: list")
  1663. def test_singletons(self):
  1664. for const in (None, False, True, Ellipsis, b'', frozenset()):
  1665. with self.subTest(const=const):
  1666. value = self.compile_constant(const)
  1667. self.assertIs(value, const)
  1668. def test_values(self):
  1669. nested_tuple = (1,)
  1670. nested_frozenset = frozenset({1})
  1671. for level in range(3):
  1672. nested_tuple = (nested_tuple, 2)
  1673. nested_frozenset = frozenset({nested_frozenset, 2})
  1674. values = (123, 123.0, 123j,
  1675. "unicode", b'bytes',
  1676. tuple("tuple"), frozenset("frozenset"),
  1677. nested_tuple, nested_frozenset)
  1678. for value in values:
  1679. with self.subTest(value=value):
  1680. result = self.compile_constant(value)
  1681. self.assertEqual(result, value)
  1682. def test_assign_to_constant(self):
  1683. tree = ast.parse("x = 1")
  1684. target = tree.body[0].targets[0]
  1685. new_target = ast.Constant(value=1)
  1686. ast.copy_location(new_target, target)
  1687. tree.body[0].targets[0] = new_target
  1688. with self.assertRaises(ValueError) as cm:
  1689. compile(tree, "string", "exec")
  1690. self.assertEqual(str(cm.exception),
  1691. "expression which can't be assigned "
  1692. "to in Store context")
  1693. def test_get_docstring(self):
  1694. tree = ast.parse("'docstring'\nx = 1")
  1695. self.assertEqual(ast.get_docstring(tree), 'docstring')
  1696. def get_load_const(self, tree):
  1697. # Compile to bytecode, disassemble and get parameter of LOAD_CONST
  1698. # instructions
  1699. co = compile(tree, '<string>', 'exec')
  1700. consts = []
  1701. for instr in dis.get_instructions(co):
  1702. if instr.opname == 'LOAD_CONST':
  1703. consts.append(instr.argval)
  1704. return consts
  1705. @support.cpython_only
  1706. def test_load_const(self):
  1707. consts = [None,
  1708. True, False,
  1709. 124,
  1710. 2.0,
  1711. 3j,
  1712. "unicode",
  1713. b'bytes',
  1714. (1, 2, 3)]
  1715. code = '\n'.join(['x={!r}'.format(const) for const in consts])
  1716. code += '\nx = ...'
  1717. consts.extend((Ellipsis, None))
  1718. tree = ast.parse(code)
  1719. self.assertEqual(self.get_load_const(tree),
  1720. consts)
  1721. # Replace expression nodes with constants
  1722. for assign, const in zip(tree.body, consts):
  1723. assert isinstance(assign, ast.Assign), ast.dump(assign)
  1724. new_node = ast.Constant(value=const)
  1725. ast.copy_location(new_node, assign.value)
  1726. assign.value = new_node
  1727. self.assertEqual(self.get_load_const(tree),
  1728. consts)
  1729. def test_literal_eval(self):
  1730. tree = ast.parse("1 + 2")
  1731. binop = tree.body[0].value
  1732. new_left = ast.Constant(value=10)
  1733. ast.copy_location(new_left, binop.left)
  1734. binop.left = new_left
  1735. new_right = ast.Constant(value=20j)
  1736. ast.copy_location(new_right, binop.right)
  1737. binop.right = new_right
  1738. self.assertEqual(ast.literal_eval(binop), 10+20j)
  1739. def test_string_kind(self):
  1740. c = ast.parse('"x"', mode='eval').body
  1741. self.assertEqual(c.value, "x")
  1742. self.assertEqual(c.kind, None)
  1743. c = ast.parse('u"x"', mode='eval').body
  1744. self.assertEqual(c.value, "x")
  1745. self.assertEqual(c.kind, "u")
  1746. c = ast.parse('r"x"', mode='eval').body
  1747. self.assertEqual(c.value, "x")
  1748. self.assertEqual(c.kind, None)
  1749. c = ast.parse('b"x"', mode='eval').body
  1750. self.assertEqual(c.value, b"x")
  1751. self.assertEqual(c.kind, None)
  1752. class EndPositionTests(unittest.TestCase):
  1753. """Tests for end position of AST nodes.
  1754. Testing end positions of nodes requires a bit of extra care
  1755. because of how LL parsers work.
  1756. """
  1757. def _check_end_pos(self, ast_node, end_lineno, end_col_offset):
  1758. self.assertEqual(ast_node.end_lineno, end_lineno)
  1759. self.assertEqual(ast_node.end_col_offset, end_col_offset)
  1760. def _check_content(self, source, ast_node, content):
  1761. self.assertEqual(ast.get_source_segment(source, ast_node), content)
  1762. def _parse_value(self, s):
  1763. # Use duck-typing to support both single expression
  1764. # and a right hand side of an assignment statement.
  1765. return ast.parse(s).body[0].value
  1766. def test_lambda(self):
  1767. s = 'lambda x, *y: None'
  1768. lam = self._parse_value(s)
  1769. self._check_content(s, lam.body, 'None')
  1770. self._check_content(s, lam.args.args[0], 'x')
  1771. self._check_content(s, lam.args.vararg, 'y')
  1772. def test_func_def(self):
  1773. s = dedent('''
  1774. def func(x: int,
  1775. *args: str,
  1776. z: float = 0,
  1777. **kwargs: Any) -> bool:
  1778. return True
  1779. ''').strip()
  1780. fdef = ast.parse(s).body[0]
  1781. self._check_end_pos(fdef, 5, 15)
  1782. self._check_content(s, fdef.body[0], 'return True')
  1783. self._check_content(s, fdef.args.args[0], 'x: int')
  1784. self._check_content(s, fdef.args.args[0].annotation, 'int')
  1785. self._check_content(s, fdef.args.kwarg, 'kwargs: Any')
  1786. self._check_content(s, fdef.args.kwarg.annotation, 'Any')
  1787. def test_call(self):
  1788. s = 'func(x, y=2, **kw)'
  1789. call = self._parse_value(s)
  1790. self._check_content(s, call.func, 'func')
  1791. self._check_content(s, call.keywords[0].value, '2')
  1792. self._check_content(s, call.keywords[1].value, 'kw')
  1793. def test_call_noargs(self):
  1794. s = 'x[0]()'
  1795. call = self._parse_value(s)
  1796. self._check_content(s, call.func, 'x[0]')
  1797. self._check_end_pos(call, 1, 6)
  1798. def test_class_def(self):
  1799. s = dedent('''
  1800. class C(A, B):
  1801. x: int = 0
  1802. ''').strip()
  1803. cdef = ast.parse(s).body[0]
  1804. self._check_end_pos(cdef, 2, 14)
  1805. self._check_content(s, cdef.bases[1], 'B')
  1806. self._check_content(s, cdef.body[0], 'x: int = 0')
  1807. def test_class_kw(self):
  1808. s = 'class S(metaclass=abc.ABCMeta): pass'
  1809. cdef = ast.parse(s).body[0]
  1810. self._check_content(s, cdef.keywords[0].value, 'abc.ABCMeta')
  1811. def test_multi_line_str(self):
  1812. s = dedent('''
  1813. x = """Some multi-line text.
  1814. It goes on starting from same indent."""
  1815. ''').strip()
  1816. assign = ast.parse(s).body[0]
  1817. self._check_end_pos(assign, 3, 40)
  1818. self._check_end_pos(assign.value, 3, 40)
  1819. def test_continued_str(self):
  1820. s = dedent('''
  1821. x = "first part" \\
  1822. "second part"
  1823. ''').strip()
  1824. assign = ast.parse(s).body[0]
  1825. self._check_end_pos(assign, 2, 13)
  1826. self._check_end_pos(assign.value, 2, 13)
  1827. def test_suites(self):
  1828. # We intentionally put these into the same string to check
  1829. # that empty lines are not part of the suite.
  1830. s = dedent('''
  1831. while True:
  1832. pass
  1833. if one():
  1834. x = None
  1835. elif other():
  1836. y = None
  1837. else:
  1838. z = None
  1839. for x, y in stuff:
  1840. assert True
  1841. try:
  1842. raise RuntimeError
  1843. except TypeError as e:
  1844. pass
  1845. pass
  1846. ''').strip()
  1847. mod = ast.parse(s)
  1848. while_loop = mod.body[0]
  1849. if_stmt = mod.body[1]
  1850. for_loop = mod.body[2]
  1851. try_stmt = mod.body[3]
  1852. pass_stmt = mod.body[4]
  1853. self._check_end_pos(while_loop, 2, 8)
  1854. self._check_end_pos(if_stmt, 9, 12)
  1855. self._check_end_pos(for_loop, 12, 15)
  1856. self._check_end_pos(try_stmt, 17, 8)
  1857. self._check_end_pos(pass_stmt, 19, 4)
  1858. self._check_content(s, while_loop.test, 'True')
  1859. self._check_content(s, if_stmt.body[0], 'x = None')
  1860. self._check_content(s, if_stmt.orelse[0].test, 'other()')
  1861. self._check_content(s, for_loop.target, 'x, y')
  1862. self._check_content(s, try_stmt.body[0], 'raise RuntimeError')
  1863. self._check_content(s, try_stmt.handlers[0].type, 'TypeError')
  1864. def test_fstring(self):
  1865. s = 'x = f"abc {x + y} abc"'
  1866. fstr = self._parse_value(s)
  1867. binop = fstr.values[1].value
  1868. self._check_content(s, binop, 'x + y')
  1869. def test_fstring_multi_line(self):
  1870. s = dedent('''
  1871. f"""Some multi-line text.
  1872. {
  1873. arg_one
  1874. +
  1875. arg_two
  1876. }
  1877. It goes on..."""
  1878. ''').strip()
  1879. fstr = self._parse_value(s)
  1880. binop = fstr.values[1].value
  1881. self._check_end_pos(binop, 5, 7)
  1882. self._check_content(s, binop.left, 'arg_one')
  1883. self._check_content(s, binop.right, 'arg_two')
  1884. def test_import_from_multi_line(self):
  1885. s = dedent('''
  1886. from x.y.z import (
  1887. a, b, c as c
  1888. )
  1889. ''').strip()
  1890. imp = ast.parse(s).body[0]
  1891. self._check_end_pos(imp, 3, 1)
  1892. self._check_end_pos(imp.names[2], 2, 16)
  1893. def test_slices(self):
  1894. s1 = 'f()[1, 2] [0]'
  1895. s2 = 'x[ a.b: c.d]'
  1896. sm = dedent('''
  1897. x[ a.b: f () ,
  1898. g () : c.d
  1899. ]
  1900. ''').strip()
  1901. i1, i2, im = map(self._parse_value, (s1, s2, sm))
  1902. self._check_content(s1, i1.value, 'f()[1, 2]')
  1903. self._check_content(s1, i1.value.slice, '1, 2')
  1904. self._check_content(s2, i2.slice.lower, 'a.b')
  1905. self._check_content(s2, i2.slice.upper, 'c.d')
  1906. self._check_content(sm, im.slice.elts[0].upper, 'f ()')
  1907. self._check_content(sm, im.slice.elts[1].lower, 'g ()')
  1908. self._check_end_pos(im, 3, 3)
  1909. def test_binop(self):
  1910. s = dedent('''
  1911. (1 * 2 + (3 ) +
  1912. 4
  1913. )
  1914. ''').strip()
  1915. binop = self._parse_value(s)
  1916. self._check_end_pos(binop, 2, 6)
  1917. self._check_content(s, binop.right, '4')
  1918. self._check_content(s, binop.left, '1 * 2 + (3 )')
  1919. self._check_content(s, binop.left.right, '3')
  1920. def test_boolop(self):
  1921. s = dedent('''
  1922. if (one_condition and
  1923. (other_condition or yet_another_one)):
  1924. pass
  1925. ''').strip()
  1926. bop = ast.parse(s).body[0].test
  1927. self._check_end_pos(bop, 2, 44)
  1928. self._check_content(s, bop.values[1],
  1929. 'other_condition or yet_another_one')
  1930. def test_tuples(self):
  1931. s1 = 'x = () ;'
  1932. s2 = 'x = 1 , ;'
  1933. s3 = 'x = (1 , 2 ) ;'
  1934. sm = dedent('''
  1935. x = (
  1936. a, b,
  1937. )
  1938. ''').strip()
  1939. t1, t2, t3, tm = map(self._parse_value, (s1, s2, s3, sm))
  1940. self._check_content(s1, t1, '()')
  1941. self._check_content(s2, t2, '1 ,')
  1942. self._check_content(s3, t3, '(1 , 2 )')
  1943. self._check_end_pos(tm, 3, 1)
  1944. def test_attribute_spaces(self):
  1945. s = 'func(x. y .z)'
  1946. call = self._parse_value(s)
  1947. self._check_content(s, call, s)
  1948. self._check_content(s, call.args[0], 'x. y .z')
  1949. def test_redundant_parenthesis(self):
  1950. s = '( ( ( a + b ) ) )'
  1951. v = ast.parse(s).body[0].value
  1952. self.assertEqual(type(v).__name__, 'BinOp')
  1953. self._check_content(s, v, 'a + b')
  1954. s2 = 'await ' + s
  1955. v = ast.parse(s2).body[0].value.value
  1956. self.assertEqual(type(v).__name__, 'BinOp')
  1957. self._check_content(s2, v, 'a + b')
  1958. def test_trailers_with_redundant_parenthesis(self):
  1959. tests = (
  1960. ('( ( ( a ) ) ) ( )', 'Call'),
  1961. ('( ( ( a ) ) ) ( b )', 'Call'),
  1962. ('( ( ( a ) ) ) [ b ]', 'Subscript'),
  1963. ('( ( ( a ) ) ) . b', 'Attribute'),
  1964. )
  1965. for s, t in tests:
  1966. with self.subTest(s):
  1967. v = ast.parse(s).body[0].value
  1968. self.assertEqual(type(v).__name__, t)
  1969. self._check_content(s, v, s)
  1970. s2 = 'await ' + s
  1971. v = ast.parse(s2).body[0].value.value
  1972. self.assertEqual(type(v).__name__, t)
  1973. self._check_content(s2, v, s)
  1974. def test_displays(self):
  1975. s1 = '[{}, {1, }, {1, 2,} ]'
  1976. s2 = '{a: b, f (): g () ,}'
  1977. c1 = self._parse_value(s1)
  1978. c2 = self._parse_value(s2)
  1979. self._check_content(s1, c1.elts[0], '{}')
  1980. self._check_content(s1, c1.elts[1], '{1, }')
  1981. self._check_content(s1, c1.elts[2], '{1, 2,}')
  1982. self._check_content(s2, c2.keys[1], 'f ()')
  1983. self._check_content(s2, c2.values[1], 'g ()')
  1984. def test_comprehensions(self):
  1985. s = dedent('''
  1986. x = [{x for x, y in stuff
  1987. if cond.x} for stuff in things]
  1988. ''').strip()
  1989. cmp = self._parse_value(s)
  1990. self._check_end_pos(cmp, 2, 37)
  1991. self._check_content(s, cmp.generators[0].iter, 'things')
  1992. self._check_content(s, cmp.elt.generators[0].iter, 'stuff')
  1993. self._check_content(s, cmp.elt.generators[0].ifs[0], 'cond.x')
  1994. self._check_content(s, cmp.elt.generators[0].target, 'x, y')
  1995. def test_yield_await(self):
  1996. s = dedent('''
  1997. async def f():
  1998. yield x
  1999. await y
  2000. ''').strip()
  2001. fdef = ast.parse(s).body[0]
  2002. self._check_content(s, fdef.body[0].value, 'yield x')
  2003. self._check_content(s, fdef.body[1].value, 'await y')
  2004. def test_source_segment_multi(self):
  2005. s_orig = dedent('''
  2006. x = (
  2007. a, b,
  2008. ) + ()
  2009. ''').strip()
  2010. s_tuple = dedent('''
  2011. (
  2012. a, b,
  2013. )
  2014. ''').strip()
  2015. binop = self._parse_value(s_orig)
  2016. self.assertEqual(ast.get_source_segment(s_orig, binop.left), s_tuple)
  2017. def test_source_segment_padded(self):
  2018. s_orig = dedent('''
  2019. class C:
  2020. def fun(self) -> None:
  2021. "ЖЖЖЖЖ"
  2022. ''').strip()
  2023. s_method = ' def fun(self) -> None:\n' \
  2024. ' "ЖЖЖЖЖ"'
  2025. cdef = ast.parse(s_orig).body[0]
  2026. self.assertEqual(ast.get_source_segment(s_orig, cdef.body[0], padded=True),
  2027. s_method)
  2028. def test_source_segment_endings(self):
  2029. s = 'v = 1\r\nw = 1\nx = 1\n\ry = 1\rz = 1\r\n'
  2030. v, w, x, y, z = ast.parse(s).body
  2031. self._check_content(s, v, 'v = 1')
  2032. self._check_content(s, w, 'w = 1')
  2033. self._check_content(s, x, 'x = 1')
  2034. self._check_content(s, y, 'y = 1')
  2035. self._check_content(s, z, 'z = 1')
  2036. def test_source_segment_tabs(self):
  2037. s = dedent('''
  2038. class C:
  2039. \t\f def fun(self) -> None:
  2040. \t\f pass
  2041. ''').strip()
  2042. s_method = ' \t\f def fun(self) -> None:\n' \
  2043. ' \t\f pass'
  2044. cdef = ast.parse(s).body[0]
  2045. self.assertEqual(ast.get_source_segment(s, cdef.body[0], padded=True), s_method)
  2046. def test_source_segment_missing_info(self):
  2047. s = 'v = 1\r\nw = 1\nx = 1\n\ry = 1\r\n'
  2048. v, w, x, y = ast.parse(s).body
  2049. del v.lineno
  2050. del w.end_lineno
  2051. del x.col_offset
  2052. del y.end_col_offset
  2053. self.assertIsNone(ast.get_source_segment(s, v))
  2054. self.assertIsNone(ast.get_source_segment(s, w))
  2055. self.assertIsNone(ast.get_source_segment(s, x))
  2056. self.assertIsNone(ast.get_source_segment(s, y))
  2057. class NodeVisitorTests(unittest.TestCase):
  2058. def test_old_constant_nodes(self):
  2059. class Visitor(ast.NodeVisitor):
  2060. def visit_Num(self, node):
  2061. log.append((node.lineno, 'Num', node.n))
  2062. def visit_Str(self, node):
  2063. log.append((node.lineno, 'Str', node.s))
  2064. def visit_Bytes(self, node):
  2065. log.append((node.lineno, 'Bytes', node.s))
  2066. def visit_NameConstant(self, node):
  2067. log.append((node.lineno, 'NameConstant', node.value))
  2068. def visit_Ellipsis(self, node):
  2069. log.append((node.lineno, 'Ellipsis', ...))
  2070. mod = ast.parse(dedent('''\
  2071. i = 42
  2072. f = 4.25
  2073. c = 4.25j
  2074. s = 'string'
  2075. b = b'bytes'
  2076. t = True
  2077. n = None
  2078. e = ...
  2079. '''))
  2080. visitor = Visitor()
  2081. log = []
  2082. with warnings.catch_warnings(record=True) as wlog:
  2083. warnings.filterwarnings('always', '', DeprecationWarning)
  2084. visitor.visit(mod)
  2085. self.assertEqual(log, [
  2086. (1, 'Num', 42),
  2087. (2, 'Num', 4.25),
  2088. (3, 'Num', 4.25j),
  2089. (4, 'Str', 'string'),
  2090. (5, 'Bytes', b'bytes'),
  2091. (6, 'NameConstant', True),
  2092. (7, 'NameConstant', None),
  2093. (8, 'Ellipsis', ...),
  2094. ])
  2095. self.assertEqual([str(w.message) for w in wlog], [
  2096. 'visit_Num is deprecated; add visit_Constant',
  2097. 'visit_Num is deprecated; add visit_Constant',
  2098. 'visit_Num is deprecated; add visit_Constant',
  2099. 'visit_Str is deprecated; add visit_Constant',
  2100. 'visit_Bytes is deprecated; add visit_Constant',
  2101. 'visit_NameConstant is deprecated; add visit_Constant',
  2102. 'visit_NameConstant is deprecated; add visit_Constant',
  2103. 'visit_Ellipsis is deprecated; add visit_Constant',
  2104. ])
  2105. @support.cpython_only
  2106. class ModuleStateTests(unittest.TestCase):
  2107. # bpo-41194, bpo-41261, bpo-41631: The _ast module uses a global state.
  2108. def check_ast_module(self):
  2109. # Check that the _ast module still works as expected
  2110. code = 'x + 1'
  2111. filename = '<string>'
  2112. mode = 'eval'
  2113. # Create _ast.AST subclasses instances
  2114. ast_tree = compile(code, filename, mode, flags=ast.PyCF_ONLY_AST)
  2115. # Call PyAST_Check()
  2116. code = compile(ast_tree, filename, mode)
  2117. self.assertIsInstance(code, types.CodeType)
  2118. def test_reload_module(self):
  2119. # bpo-41194: Importing the _ast module twice must not crash.
  2120. with support.swap_item(sys.modules, '_ast', None):
  2121. del sys.modules['_ast']
  2122. import _ast as ast1
  2123. del sys.modules['_ast']
  2124. import _ast as ast2
  2125. self.check_ast_module()
  2126. # Unloading the two _ast module instances must not crash.
  2127. del ast1
  2128. del ast2
  2129. support.gc_collect()
  2130. self.check_ast_module()
  2131. def test_sys_modules(self):
  2132. # bpo-41631: Test reproducing a Mercurial crash when PyAST_Check()
  2133. # imported the _ast module internally.
  2134. lazy_mod = object()
  2135. def my_import(name, *args, **kw):
  2136. sys.modules[name] = lazy_mod
  2137. return lazy_mod
  2138. with support.swap_item(sys.modules, '_ast', None):
  2139. del sys.modules['_ast']
  2140. with support.swap_attr(builtins, '__import__', my_import):
  2141. # Test that compile() does not import the _ast module
  2142. self.check_ast_module()
  2143. self.assertNotIn('_ast', sys.modules)
  2144. # Sanity check of the test itself
  2145. import _ast
  2146. self.assertIs(_ast, lazy_mod)
  2147. def test_subinterpreter(self):
  2148. # bpo-41631: Importing and using the _ast module in a subinterpreter
  2149. # must not crash.
  2150. code = dedent('''
  2151. import _ast
  2152. import ast
  2153. import gc
  2154. import sys
  2155. import types
  2156. # Create _ast.AST subclasses instances and call PyAST_Check()
  2157. ast_tree = compile('x+1', '<string>', 'eval',
  2158. flags=ast.PyCF_ONLY_AST)
  2159. code = compile(ast_tree, 'string', 'eval')
  2160. if not isinstance(code, types.CodeType):
  2161. raise AssertionError
  2162. # Unloading the _ast module must not crash.
  2163. del ast, _ast
  2164. del sys.modules['ast'], sys.modules['_ast']
  2165. gc.collect()
  2166. ''')
  2167. res = support.run_in_subinterp(code)
  2168. self.assertEqual(res, 0)
  2169. def main():
  2170. if __name__ != '__main__':
  2171. return
  2172. if sys.argv[1:] == ['-g']:
  2173. for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
  2174. (eval_tests, "eval")):
  2175. print(kind+"_results = [")
  2176. for statement in statements:
  2177. tree = ast.parse(statement, "?", kind)
  2178. print("%r," % (to_tuple(tree),))
  2179. print("]")
  2180. print("main()")
  2181. raise SystemExit
  2182. unittest.main()
  2183. #### EVERYTHING BELOW IS GENERATED BY python Lib/test/test_ast.py -g #####
  2184. exec_results = [
  2185. ('Module', [('Expr', (1, 0, 1, 4), ('Constant', (1, 0, 1, 4), None, None))], []),
  2186. ('Module', [('Expr', (1, 0, 1, 18), ('Constant', (1, 0, 1, 18), 'module docstring', None))], []),
  2187. ('Module', [('FunctionDef', (1, 0, 1, 13), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (1, 9, 1, 13))], [], None, None)], []),
  2188. ('Module', [('FunctionDef', (1, 0, 1, 29), 'f', ('arguments', [], [], None, [], [], None, []), [('Expr', (1, 9, 1, 29), ('Constant', (1, 9, 1, 29), 'function docstring', None))], [], None, None)], []),
  2189. ('Module', [('FunctionDef', (1, 0, 1, 14), 'f', ('arguments', [], [('arg', (1, 6, 1, 7), 'a', None, None)], None, [], [], None, []), [('Pass', (1, 10, 1, 14))], [], None, None)], []),
  2190. ('Module', [('FunctionDef', (1, 0, 1, 16), 'f', ('arguments', [], [('arg', (1, 6, 1, 7), 'a', None, None)], None, [], [], None, [('Constant', (1, 8, 1, 9), 0, None)]), [('Pass', (1, 12, 1, 16))], [], None, None)], []),
  2191. ('Module', [('FunctionDef', (1, 0, 1, 18), 'f', ('arguments', [], [], ('arg', (1, 7, 1, 11), 'args', None, None), [], [], None, []), [('Pass', (1, 14, 1, 18))], [], None, None)], []),
  2192. ('Module', [('FunctionDef', (1, 0, 1, 23), 'f', ('arguments', [], [], ('arg', (1, 7, 1, 16), 'args', ('Starred', (1, 13, 1, 16), ('Name', (1, 14, 1, 16), 'Ts', ('Load',)), ('Load',)), None), [], [], None, []), [('Pass', (1, 19, 1, 23))], [], None, None)], []),
  2193. ('Module', [('FunctionDef', (1, 0, 1, 36), 'f', ('arguments', [], [], ('arg', (1, 7, 1, 29), 'args', ('Starred', (1, 13, 1, 29), ('Subscript', (1, 14, 1, 29), ('Name', (1, 14, 1, 19), 'tuple', ('Load',)), ('Tuple', (1, 20, 1, 28), [('Name', (1, 20, 1, 23), 'int', ('Load',)), ('Constant', (1, 25, 1, 28), Ellipsis, None)], ('Load',)), ('Load',)), ('Load',)), None), [], [], None, []), [('Pass', (1, 32, 1, 36))], [], None, None)], []),
  2194. ('Module', [('FunctionDef', (1, 0, 1, 36), 'f', ('arguments', [], [], ('arg', (1, 7, 1, 29), 'args', ('Starred', (1, 13, 1, 29), ('Subscript', (1, 14, 1, 29), ('Name', (1, 14, 1, 19), 'tuple', ('Load',)), ('Tuple', (1, 20, 1, 28), [('Name', (1, 20, 1, 23), 'int', ('Load',)), ('Starred', (1, 25, 1, 28), ('Name', (1, 26, 1, 28), 'Ts', ('Load',)), ('Load',))], ('Load',)), ('Load',)), ('Load',)), None), [], [], None, []), [('Pass', (1, 32, 1, 36))], [], None, None)], []),
  2195. ('Module', [('FunctionDef', (1, 0, 1, 21), 'f', ('arguments', [], [], None, [], [], ('arg', (1, 8, 1, 14), 'kwargs', None, None), []), [('Pass', (1, 17, 1, 21))], [], None, None)], []),
  2196. ('Module', [('FunctionDef', (1, 0, 1, 71), 'f', ('arguments', [], [('arg', (1, 6, 1, 7), 'a', None, None), ('arg', (1, 9, 1, 10), 'b', None, None), ('arg', (1, 14, 1, 15), 'c', None, None), ('arg', (1, 22, 1, 23), 'd', None, None), ('arg', (1, 28, 1, 29), 'e', None, None)], ('arg', (1, 35, 1, 39), 'args', None, None), [('arg', (1, 41, 1, 42), 'f', None, None)], [('Constant', (1, 43, 1, 45), 42, None)], ('arg', (1, 49, 1, 55), 'kwargs', None, None), [('Constant', (1, 11, 1, 12), 1, None), ('Constant', (1, 16, 1, 20), None, None), ('List', (1, 24, 1, 26), [], ('Load',)), ('Dict', (1, 30, 1, 32), [], [])]), [('Expr', (1, 58, 1, 71), ('Constant', (1, 58, 1, 71), 'doc for f()', None))], [], None, None)], []),
  2197. ('Module', [('FunctionDef', (1, 0, 1, 27), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (1, 23, 1, 27))], [], ('Subscript', (1, 11, 1, 21), ('Name', (1, 11, 1, 16), 'tuple', ('Load',)), ('Tuple', (1, 17, 1, 20), [('Starred', (1, 17, 1, 20), ('Name', (1, 18, 1, 20), 'Ts', ('Load',)), ('Load',))], ('Load',)), ('Load',)), None)], []),
  2198. ('Module', [('FunctionDef', (1, 0, 1, 32), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (1, 28, 1, 32))], [], ('Subscript', (1, 11, 1, 26), ('Name', (1, 11, 1, 16), 'tuple', ('Load',)), ('Tuple', (1, 17, 1, 25), [('Name', (1, 17, 1, 20), 'int', ('Load',)), ('Starred', (1, 22, 1, 25), ('Name', (1, 23, 1, 25), 'Ts', ('Load',)), ('Load',))], ('Load',)), ('Load',)), None)], []),
  2199. ('Module', [('FunctionDef', (1, 0, 1, 45), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (1, 41, 1, 45))], [], ('Subscript', (1, 11, 1, 39), ('Name', (1, 11, 1, 16), 'tuple', ('Load',)), ('Tuple', (1, 17, 1, 38), [('Name', (1, 17, 1, 20), 'int', ('Load',)), ('Starred', (1, 22, 1, 38), ('Subscript', (1, 23, 1, 38), ('Name', (1, 23, 1, 28), 'tuple', ('Load',)), ('Tuple', (1, 29, 1, 37), [('Name', (1, 29, 1, 32), 'int', ('Load',)), ('Constant', (1, 34, 1, 37), Ellipsis, None)], ('Load',)), ('Load',)), ('Load',))], ('Load',)), ('Load',)), None)], []),
  2200. ('Module', [('ClassDef', (1, 0, 1, 12), 'C', [], [], [('Pass', (1, 8, 1, 12))], [])], []),
  2201. ('Module', [('ClassDef', (1, 0, 1, 32), 'C', [], [], [('Expr', (1, 9, 1, 32), ('Constant', (1, 9, 1, 32), 'docstring for class C', None))], [])], []),
  2202. ('Module', [('ClassDef', (1, 0, 1, 21), 'C', [('Name', (1, 8, 1, 14), 'object', ('Load',))], [], [('Pass', (1, 17, 1, 21))], [])], []),
  2203. ('Module', [('FunctionDef', (1, 0, 1, 16), 'f', ('arguments', [], [], None, [], [], None, []), [('Return', (1, 8, 1, 16), ('Constant', (1, 15, 1, 16), 1, None))], [], None, None)], []),
  2204. ('Module', [('Delete', (1, 0, 1, 5), [('Name', (1, 4, 1, 5), 'v', ('Del',))])], []),
  2205. ('Module', [('Assign', (1, 0, 1, 5), [('Name', (1, 0, 1, 1), 'v', ('Store',))], ('Constant', (1, 4, 1, 5), 1, None), None)], []),
  2206. ('Module', [('Assign', (1, 0, 1, 7), [('Tuple', (1, 0, 1, 3), [('Name', (1, 0, 1, 1), 'a', ('Store',)), ('Name', (1, 2, 1, 3), 'b', ('Store',))], ('Store',))], ('Name', (1, 6, 1, 7), 'c', ('Load',)), None)], []),
  2207. ('Module', [('Assign', (1, 0, 1, 9), [('Tuple', (1, 0, 1, 5), [('Name', (1, 1, 1, 2), 'a', ('Store',)), ('Name', (1, 3, 1, 4), 'b', ('Store',))], ('Store',))], ('Name', (1, 8, 1, 9), 'c', ('Load',)), None)], []),
  2208. ('Module', [('Assign', (1, 0, 1, 9), [('List', (1, 0, 1, 5), [('Name', (1, 1, 1, 2), 'a', ('Store',)), ('Name', (1, 3, 1, 4), 'b', ('Store',))], ('Store',))], ('Name', (1, 8, 1, 9), 'c', ('Load',)), None)], []),
  2209. ('Module', [('AnnAssign', (1, 0, 1, 13), ('Name', (1, 0, 1, 1), 'x', ('Store',)), ('Subscript', (1, 3, 1, 13), ('Name', (1, 3, 1, 8), 'tuple', ('Load',)), ('Tuple', (1, 9, 1, 12), [('Starred', (1, 9, 1, 12), ('Name', (1, 10, 1, 12), 'Ts', ('Load',)), ('Load',))], ('Load',)), ('Load',)), None, 1)], []),
  2210. ('Module', [('AnnAssign', (1, 0, 1, 18), ('Name', (1, 0, 1, 1), 'x', ('Store',)), ('Subscript', (1, 3, 1, 18), ('Name', (1, 3, 1, 8), 'tuple', ('Load',)), ('Tuple', (1, 9, 1, 17), [('Name', (1, 9, 1, 12), 'int', ('Load',)), ('Starred', (1, 14, 1, 17), ('Name', (1, 15, 1, 17), 'Ts', ('Load',)), ('Load',))], ('Load',)), ('Load',)), None, 1)], []),
  2211. ('Module', [('AnnAssign', (1, 0, 1, 31), ('Name', (1, 0, 1, 1), 'x', ('Store',)), ('Subscript', (1, 3, 1, 31), ('Name', (1, 3, 1, 8), 'tuple', ('Load',)), ('Tuple', (1, 9, 1, 30), [('Name', (1, 9, 1, 12), 'int', ('Load',)), ('Starred', (1, 14, 1, 30), ('Subscript', (1, 15, 1, 30), ('Name', (1, 15, 1, 20), 'tuple', ('Load',)), ('Tuple', (1, 21, 1, 29), [('Name', (1, 21, 1, 24), 'str', ('Load',)), ('Constant', (1, 26, 1, 29), Ellipsis, None)], ('Load',)), ('Load',)), ('Load',))], ('Load',)), ('Load',)), None, 1)], []),
  2212. ('Module', [('AugAssign', (1, 0, 1, 6), ('Name', (1, 0, 1, 1), 'v', ('Store',)), ('Add',), ('Constant', (1, 5, 1, 6), 1, None))], []),
  2213. ('Module', [('For', (1, 0, 1, 15), ('Name', (1, 4, 1, 5), 'v', ('Store',)), ('Name', (1, 9, 1, 10), 'v', ('Load',)), [('Pass', (1, 11, 1, 15))], [], None)], []),
  2214. ('Module', [('While', (1, 0, 1, 12), ('Name', (1, 6, 1, 7), 'v', ('Load',)), [('Pass', (1, 8, 1, 12))], [])], []),
  2215. ('Module', [('If', (1, 0, 1, 9), ('Name', (1, 3, 1, 4), 'v', ('Load',)), [('Pass', (1, 5, 1, 9))], [])], []),
  2216. ('Module', [('If', (1, 0, 4, 6), ('Name', (1, 3, 1, 4), 'a', ('Load',)), [('Pass', (2, 2, 2, 6))], [('If', (3, 0, 4, 6), ('Name', (3, 5, 3, 6), 'b', ('Load',)), [('Pass', (4, 2, 4, 6))], [])])], []),
  2217. ('Module', [('If', (1, 0, 6, 6), ('Name', (1, 3, 1, 4), 'a', ('Load',)), [('Pass', (2, 2, 2, 6))], [('If', (3, 0, 6, 6), ('Name', (3, 5, 3, 6), 'b', ('Load',)), [('Pass', (4, 2, 4, 6))], [('Pass', (6, 2, 6, 6))])])], []),
  2218. ('Module', [('With', (1, 0, 1, 17), [('withitem', ('Name', (1, 5, 1, 6), 'x', ('Load',)), ('Name', (1, 10, 1, 11), 'y', ('Store',)))], [('Pass', (1, 13, 1, 17))], None)], []),
  2219. ('Module', [('With', (1, 0, 1, 25), [('withitem', ('Name', (1, 5, 1, 6), 'x', ('Load',)), ('Name', (1, 10, 1, 11), 'y', ('Store',))), ('withitem', ('Name', (1, 13, 1, 14), 'z', ('Load',)), ('Name', (1, 18, 1, 19), 'q', ('Store',)))], [('Pass', (1, 21, 1, 25))], None)], []),
  2220. ('Module', [('Raise', (1, 0, 1, 25), ('Call', (1, 6, 1, 25), ('Name', (1, 6, 1, 15), 'Exception', ('Load',)), [('Constant', (1, 16, 1, 24), 'string', None)], []), None)], []),
  2221. ('Module', [('Try', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [('ExceptHandler', (3, 0, 4, 6), ('Name', (3, 7, 3, 16), 'Exception', ('Load',)), None, [('Pass', (4, 2, 4, 6))])], [], [])], []),
  2222. ('Module', [('Try', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [], [], [('Pass', (4, 2, 4, 6))])], []),
  2223. ('Module', [('TryStar', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [('ExceptHandler', (3, 0, 4, 6), ('Name', (3, 8, 3, 17), 'Exception', ('Load',)), None, [('Pass', (4, 2, 4, 6))])], [], [])], []),
  2224. ('Module', [('Assert', (1, 0, 1, 8), ('Name', (1, 7, 1, 8), 'v', ('Load',)), None)], []),
  2225. ('Module', [('Import', (1, 0, 1, 10), [('alias', (1, 7, 1, 10), 'sys', None)])], []),
  2226. ('Module', [('ImportFrom', (1, 0, 1, 17), 'sys', [('alias', (1, 16, 1, 17), 'v', None)], 0)], []),
  2227. ('Module', [('Global', (1, 0, 1, 8), ['v'])], []),
  2228. ('Module', [('Expr', (1, 0, 1, 1), ('Constant', (1, 0, 1, 1), 1, None))], []),
  2229. ('Module', [('Pass', (1, 0, 1, 4))], []),
  2230. ('Module', [('For', (1, 0, 1, 16), ('Name', (1, 4, 1, 5), 'v', ('Store',)), ('Name', (1, 9, 1, 10), 'v', ('Load',)), [('Break', (1, 11, 1, 16))], [], None)], []),
  2231. ('Module', [('For', (1, 0, 1, 19), ('Name', (1, 4, 1, 5), 'v', ('Store',)), ('Name', (1, 9, 1, 10), 'v', ('Load',)), [('Continue', (1, 11, 1, 19))], [], None)], []),
  2232. ('Module', [('For', (1, 0, 1, 18), ('Tuple', (1, 4, 1, 7), [('Name', (1, 4, 1, 5), 'a', ('Store',)), ('Name', (1, 6, 1, 7), 'b', ('Store',))], ('Store',)), ('Name', (1, 11, 1, 12), 'c', ('Load',)), [('Pass', (1, 14, 1, 18))], [], None)], []),
  2233. ('Module', [('For', (1, 0, 1, 20), ('Tuple', (1, 4, 1, 9), [('Name', (1, 5, 1, 6), 'a', ('Store',)), ('Name', (1, 7, 1, 8), 'b', ('Store',))], ('Store',)), ('Name', (1, 13, 1, 14), 'c', ('Load',)), [('Pass', (1, 16, 1, 20))], [], None)], []),
  2234. ('Module', [('For', (1, 0, 1, 20), ('List', (1, 4, 1, 9), [('Name', (1, 5, 1, 6), 'a', ('Store',)), ('Name', (1, 7, 1, 8), 'b', ('Store',))], ('Store',)), ('Name', (1, 13, 1, 14), 'c', ('Load',)), [('Pass', (1, 16, 1, 20))], [], None)], []),
  2235. ('Module', [('Expr', (1, 0, 11, 5), ('GeneratorExp', (1, 0, 11, 5), ('Tuple', (2, 4, 6, 5), [('Name', (3, 4, 3, 6), 'Aa', ('Load',)), ('Name', (5, 7, 5, 9), 'Bb', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (8, 4, 10, 6), [('Name', (8, 4, 8, 6), 'Aa', ('Store',)), ('Name', (10, 4, 10, 6), 'Bb', ('Store',))], ('Store',)), ('Name', (10, 10, 10, 12), 'Cc', ('Load',)), [], 0)]))], []),
  2236. ('Module', [('Expr', (1, 0, 1, 34), ('DictComp', (1, 0, 1, 34), ('Name', (1, 1, 1, 2), 'a', ('Load',)), ('Name', (1, 5, 1, 6), 'b', ('Load',)), [('comprehension', ('Name', (1, 11, 1, 12), 'w', ('Store',)), ('Name', (1, 16, 1, 17), 'x', ('Load',)), [], 0), ('comprehension', ('Name', (1, 22, 1, 23), 'm', ('Store',)), ('Name', (1, 27, 1, 28), 'p', ('Load',)), [('Name', (1, 32, 1, 33), 'g', ('Load',))], 0)]))], []),
  2237. ('Module', [('Expr', (1, 0, 1, 20), ('DictComp', (1, 0, 1, 20), ('Name', (1, 1, 1, 2), 'a', ('Load',)), ('Name', (1, 5, 1, 6), 'b', ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 14), [('Name', (1, 11, 1, 12), 'v', ('Store',)), ('Name', (1, 13, 1, 14), 'w', ('Store',))], ('Store',)), ('Name', (1, 18, 1, 19), 'x', ('Load',)), [], 0)]))], []),
  2238. ('Module', [('Expr', (1, 0, 1, 19), ('SetComp', (1, 0, 1, 19), ('Name', (1, 1, 1, 2), 'r', ('Load',)), [('comprehension', ('Name', (1, 7, 1, 8), 'l', ('Store',)), ('Name', (1, 12, 1, 13), 'x', ('Load',)), [('Name', (1, 17, 1, 18), 'g', ('Load',))], 0)]))], []),
  2239. ('Module', [('Expr', (1, 0, 1, 16), ('SetComp', (1, 0, 1, 16), ('Name', (1, 1, 1, 2), 'r', ('Load',)), [('comprehension', ('Tuple', (1, 7, 1, 10), [('Name', (1, 7, 1, 8), 'l', ('Store',)), ('Name', (1, 9, 1, 10), 'm', ('Store',))], ('Store',)), ('Name', (1, 14, 1, 15), 'x', ('Load',)), [], 0)]))], []),
  2240. ('Module', [('AsyncFunctionDef', (1, 0, 3, 18), 'f', ('arguments', [], [], None, [], [], None, []), [('Expr', (2, 1, 2, 17), ('Constant', (2, 1, 2, 17), 'async function', None)), ('Expr', (3, 1, 3, 18), ('Await', (3, 1, 3, 18), ('Call', (3, 7, 3, 18), ('Name', (3, 7, 3, 16), 'something', ('Load',)), [], [])))], [], None, None)], []),
  2241. ('Module', [('AsyncFunctionDef', (1, 0, 3, 8), 'f', ('arguments', [], [], None, [], [], None, []), [('AsyncFor', (2, 1, 3, 8), ('Name', (2, 11, 2, 12), 'e', ('Store',)), ('Name', (2, 16, 2, 17), 'i', ('Load',)), [('Expr', (2, 19, 2, 20), ('Constant', (2, 19, 2, 20), 1, None))], [('Expr', (3, 7, 3, 8), ('Constant', (3, 7, 3, 8), 2, None))], None)], [], None, None)], []),
  2242. ('Module', [('AsyncFunctionDef', (1, 0, 2, 21), 'f', ('arguments', [], [], None, [], [], None, []), [('AsyncWith', (2, 1, 2, 21), [('withitem', ('Name', (2, 12, 2, 13), 'a', ('Load',)), ('Name', (2, 17, 2, 18), 'b', ('Store',)))], [('Expr', (2, 20, 2, 21), ('Constant', (2, 20, 2, 21), 1, None))], None)], [], None, None)], []),
  2243. ('Module', [('Expr', (1, 0, 1, 14), ('Dict', (1, 0, 1, 14), [None, ('Constant', (1, 10, 1, 11), 2, None)], [('Dict', (1, 3, 1, 8), [('Constant', (1, 4, 1, 5), 1, None)], [('Constant', (1, 6, 1, 7), 2, None)]), ('Constant', (1, 12, 1, 13), 3, None)]))], []),
  2244. ('Module', [('Expr', (1, 0, 1, 12), ('Set', (1, 0, 1, 12), [('Starred', (1, 1, 1, 8), ('Set', (1, 2, 1, 8), [('Constant', (1, 3, 1, 4), 1, None), ('Constant', (1, 6, 1, 7), 2, None)]), ('Load',)), ('Constant', (1, 10, 1, 11), 3, None)]))], []),
  2245. ('Module', [('AsyncFunctionDef', (1, 0, 2, 21), 'f', ('arguments', [], [], None, [], [], None, []), [('Expr', (2, 1, 2, 21), ('ListComp', (2, 1, 2, 21), ('Name', (2, 2, 2, 3), 'i', ('Load',)), [('comprehension', ('Name', (2, 14, 2, 15), 'b', ('Store',)), ('Name', (2, 19, 2, 20), 'c', ('Load',)), [], 1)]))], [], None, None)], []),
  2246. ('Module', [('FunctionDef', (4, 0, 4, 13), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (4, 9, 4, 13))], [('Name', (1, 1, 1, 6), 'deco1', ('Load',)), ('Call', (2, 1, 2, 8), ('Name', (2, 1, 2, 6), 'deco2', ('Load',)), [], []), ('Call', (3, 1, 3, 9), ('Name', (3, 1, 3, 6), 'deco3', ('Load',)), [('Constant', (3, 7, 3, 8), 1, None)], [])], None, None)], []),
  2247. ('Module', [('AsyncFunctionDef', (4, 0, 4, 19), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (4, 15, 4, 19))], [('Name', (1, 1, 1, 6), 'deco1', ('Load',)), ('Call', (2, 1, 2, 8), ('Name', (2, 1, 2, 6), 'deco2', ('Load',)), [], []), ('Call', (3, 1, 3, 9), ('Name', (3, 1, 3, 6), 'deco3', ('Load',)), [('Constant', (3, 7, 3, 8), 1, None)], [])], None, None)], []),
  2248. ('Module', [('ClassDef', (4, 0, 4, 13), 'C', [], [], [('Pass', (4, 9, 4, 13))], [('Name', (1, 1, 1, 6), 'deco1', ('Load',)), ('Call', (2, 1, 2, 8), ('Name', (2, 1, 2, 6), 'deco2', ('Load',)), [], []), ('Call', (3, 1, 3, 9), ('Name', (3, 1, 3, 6), 'deco3', ('Load',)), [('Constant', (3, 7, 3, 8), 1, None)], [])])], []),
  2249. ('Module', [('FunctionDef', (2, 0, 2, 13), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (2, 9, 2, 13))], [('Call', (1, 1, 1, 19), ('Name', (1, 1, 1, 5), 'deco', ('Load',)), [('GeneratorExp', (1, 5, 1, 19), ('Name', (1, 6, 1, 7), 'a', ('Load',)), [('comprehension', ('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 17, 1, 18), 'b', ('Load',)), [], 0)])], [])], None, None)], []),
  2250. ('Module', [('FunctionDef', (2, 0, 2, 13), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (2, 9, 2, 13))], [('Attribute', (1, 1, 1, 6), ('Attribute', (1, 1, 1, 4), ('Name', (1, 1, 1, 2), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',))], None, None)], []),
  2251. ('Module', [('Expr', (1, 0, 1, 8), ('NamedExpr', (1, 1, 1, 7), ('Name', (1, 1, 1, 2), 'a', ('Store',)), ('Constant', (1, 6, 1, 7), 1, None)))], []),
  2252. ('Module', [('FunctionDef', (1, 0, 1, 18), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [], None, [], [], None, []), [('Pass', (1, 14, 1, 18))], [], None, None)], []),
  2253. ('Module', [('FunctionDef', (1, 0, 1, 26), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 12, 1, 13), 'c', None, None), ('arg', (1, 15, 1, 16), 'd', None, None), ('arg', (1, 18, 1, 19), 'e', None, None)], None, [], [], None, []), [('Pass', (1, 22, 1, 26))], [], None, None)], []),
  2254. ('Module', [('FunctionDef', (1, 0, 1, 29), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 12, 1, 13), 'c', None, None)], None, [('arg', (1, 18, 1, 19), 'd', None, None), ('arg', (1, 21, 1, 22), 'e', None, None)], [None, None], None, []), [('Pass', (1, 25, 1, 29))], [], None, None)], []),
  2255. ('Module', [('FunctionDef', (1, 0, 1, 39), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 12, 1, 13), 'c', None, None)], None, [('arg', (1, 18, 1, 19), 'd', None, None), ('arg', (1, 21, 1, 22), 'e', None, None)], [None, None], ('arg', (1, 26, 1, 32), 'kwargs', None, None), []), [('Pass', (1, 35, 1, 39))], [], None, None)], []),
  2256. ('Module', [('FunctionDef', (1, 0, 1, 20), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [], None, [], [], None, [('Constant', (1, 8, 1, 9), 1, None)]), [('Pass', (1, 16, 1, 20))], [], None, None)], []),
  2257. ('Module', [('FunctionDef', (1, 0, 1, 29), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 14, 1, 15), 'b', None, None), ('arg', (1, 19, 1, 20), 'c', None, None)], None, [], [], None, [('Constant', (1, 8, 1, 9), 1, None), ('Constant', (1, 16, 1, 17), 2, None), ('Constant', (1, 21, 1, 22), 4, None)]), [('Pass', (1, 25, 1, 29))], [], None, None)], []),
  2258. ('Module', [('FunctionDef', (1, 0, 1, 32), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 14, 1, 15), 'b', None, None)], None, [('arg', (1, 22, 1, 23), 'c', None, None)], [('Constant', (1, 24, 1, 25), 4, None)], None, [('Constant', (1, 8, 1, 9), 1, None), ('Constant', (1, 16, 1, 17), 2, None)]), [('Pass', (1, 28, 1, 32))], [], None, None)], []),
  2259. ('Module', [('FunctionDef', (1, 0, 1, 30), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 14, 1, 15), 'b', None, None)], None, [('arg', (1, 22, 1, 23), 'c', None, None)], [None], None, [('Constant', (1, 8, 1, 9), 1, None), ('Constant', (1, 16, 1, 17), 2, None)]), [('Pass', (1, 26, 1, 30))], [], None, None)], []),
  2260. ('Module', [('FunctionDef', (1, 0, 1, 42), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 14, 1, 15), 'b', None, None)], None, [('arg', (1, 22, 1, 23), 'c', None, None)], [('Constant', (1, 24, 1, 25), 4, None)], ('arg', (1, 29, 1, 35), 'kwargs', None, None), [('Constant', (1, 8, 1, 9), 1, None), ('Constant', (1, 16, 1, 17), 2, None)]), [('Pass', (1, 38, 1, 42))], [], None, None)], []),
  2261. ('Module', [('FunctionDef', (1, 0, 1, 40), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 14, 1, 15), 'b', None, None)], None, [('arg', (1, 22, 1, 23), 'c', None, None)], [None], ('arg', (1, 27, 1, 33), 'kwargs', None, None), [('Constant', (1, 8, 1, 9), 1, None), ('Constant', (1, 16, 1, 17), 2, None)]), [('Pass', (1, 36, 1, 40))], [], None, None)], []),
  2262. ]
  2263. single_results = [
  2264. ('Interactive', [('Expr', (1, 0, 1, 3), ('BinOp', (1, 0, 1, 3), ('Constant', (1, 0, 1, 1), 1, None), ('Add',), ('Constant', (1, 2, 1, 3), 2, None)))]),
  2265. ]
  2266. eval_results = [
  2267. ('Expression', ('Constant', (1, 0, 1, 4), None, None)),
  2268. ('Expression', ('BoolOp', (1, 0, 1, 7), ('And',), [('Name', (1, 0, 1, 1), 'a', ('Load',)), ('Name', (1, 6, 1, 7), 'b', ('Load',))])),
  2269. ('Expression', ('BinOp', (1, 0, 1, 5), ('Name', (1, 0, 1, 1), 'a', ('Load',)), ('Add',), ('Name', (1, 4, 1, 5), 'b', ('Load',)))),
  2270. ('Expression', ('UnaryOp', (1, 0, 1, 5), ('Not',), ('Name', (1, 4, 1, 5), 'v', ('Load',)))),
  2271. ('Expression', ('Lambda', (1, 0, 1, 11), ('arguments', [], [], None, [], [], None, []), ('Constant', (1, 7, 1, 11), None, None))),
  2272. ('Expression', ('Dict', (1, 0, 1, 7), [('Constant', (1, 2, 1, 3), 1, None)], [('Constant', (1, 4, 1, 5), 2, None)])),
  2273. ('Expression', ('Dict', (1, 0, 1, 2), [], [])),
  2274. ('Expression', ('Set', (1, 0, 1, 7), [('Constant', (1, 1, 1, 5), None, None)])),
  2275. ('Expression', ('Dict', (1, 0, 5, 6), [('Constant', (2, 6, 2, 7), 1, None)], [('Constant', (4, 10, 4, 11), 2, None)])),
  2276. ('Expression', ('ListComp', (1, 0, 1, 19), ('Name', (1, 1, 1, 2), 'a', ('Load',)), [('comprehension', ('Name', (1, 7, 1, 8), 'b', ('Store',)), ('Name', (1, 12, 1, 13), 'c', ('Load',)), [('Name', (1, 17, 1, 18), 'd', ('Load',))], 0)])),
  2277. ('Expression', ('GeneratorExp', (1, 0, 1, 19), ('Name', (1, 1, 1, 2), 'a', ('Load',)), [('comprehension', ('Name', (1, 7, 1, 8), 'b', ('Store',)), ('Name', (1, 12, 1, 13), 'c', ('Load',)), [('Name', (1, 17, 1, 18), 'd', ('Load',))], 0)])),
  2278. ('Expression', ('ListComp', (1, 0, 1, 20), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 14), [('Name', (1, 11, 1, 12), 'a', ('Store',)), ('Name', (1, 13, 1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 18, 1, 19), 'c', ('Load',)), [], 0)])),
  2279. ('Expression', ('ListComp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
  2280. ('Expression', ('ListComp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
  2281. ('Expression', ('SetComp', (1, 0, 1, 20), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 14), [('Name', (1, 11, 1, 12), 'a', ('Store',)), ('Name', (1, 13, 1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 18, 1, 19), 'c', ('Load',)), [], 0)])),
  2282. ('Expression', ('SetComp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
  2283. ('Expression', ('SetComp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
  2284. ('Expression', ('GeneratorExp', (1, 0, 1, 20), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 14), [('Name', (1, 11, 1, 12), 'a', ('Store',)), ('Name', (1, 13, 1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 18, 1, 19), 'c', ('Load',)), [], 0)])),
  2285. ('Expression', ('GeneratorExp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
  2286. ('Expression', ('GeneratorExp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
  2287. ('Expression', ('Compare', (1, 0, 1, 9), ('Constant', (1, 0, 1, 1), 1, None), [('Lt',), ('Lt',)], [('Constant', (1, 4, 1, 5), 2, None), ('Constant', (1, 8, 1, 9), 3, None)])),
  2288. ('Expression', ('Call', (1, 0, 1, 17), ('Name', (1, 0, 1, 1), 'f', ('Load',)), [('Constant', (1, 2, 1, 3), 1, None), ('Constant', (1, 4, 1, 5), 2, None), ('Starred', (1, 10, 1, 12), ('Name', (1, 11, 1, 12), 'd', ('Load',)), ('Load',))], [('keyword', (1, 6, 1, 9), 'c', ('Constant', (1, 8, 1, 9), 3, None)), ('keyword', (1, 13, 1, 16), None, ('Name', (1, 15, 1, 16), 'e', ('Load',)))])),
  2289. ('Expression', ('Call', (1, 0, 1, 10), ('Name', (1, 0, 1, 1), 'f', ('Load',)), [('Starred', (1, 2, 1, 9), ('List', (1, 3, 1, 9), [('Constant', (1, 4, 1, 5), 0, None), ('Constant', (1, 7, 1, 8), 1, None)], ('Load',)), ('Load',))], [])),
  2290. ('Expression', ('Call', (1, 0, 1, 15), ('Name', (1, 0, 1, 1), 'f', ('Load',)), [('GeneratorExp', (1, 1, 1, 15), ('Name', (1, 2, 1, 3), 'a', ('Load',)), [('comprehension', ('Name', (1, 8, 1, 9), 'a', ('Store',)), ('Name', (1, 13, 1, 14), 'b', ('Load',)), [], 0)])], [])),
  2291. ('Expression', ('Constant', (1, 0, 1, 2), 10, None)),
  2292. ('Expression', ('Constant', (1, 0, 1, 8), 'string', None)),
  2293. ('Expression', ('Attribute', (1, 0, 1, 3), ('Name', (1, 0, 1, 1), 'a', ('Load',)), 'b', ('Load',))),
  2294. ('Expression', ('Subscript', (1, 0, 1, 6), ('Name', (1, 0, 1, 1), 'a', ('Load',)), ('Slice', (1, 2, 1, 5), ('Name', (1, 2, 1, 3), 'b', ('Load',)), ('Name', (1, 4, 1, 5), 'c', ('Load',)), None), ('Load',))),
  2295. ('Expression', ('Name', (1, 0, 1, 1), 'v', ('Load',))),
  2296. ('Expression', ('List', (1, 0, 1, 7), [('Constant', (1, 1, 1, 2), 1, None), ('Constant', (1, 3, 1, 4), 2, None), ('Constant', (1, 5, 1, 6), 3, None)], ('Load',))),
  2297. ('Expression', ('List', (1, 0, 1, 2), [], ('Load',))),
  2298. ('Expression', ('Tuple', (1, 0, 1, 5), [('Constant', (1, 0, 1, 1), 1, None), ('Constant', (1, 2, 1, 3), 2, None), ('Constant', (1, 4, 1, 5), 3, None)], ('Load',))),
  2299. ('Expression', ('Tuple', (1, 0, 1, 7), [('Constant', (1, 1, 1, 2), 1, None), ('Constant', (1, 3, 1, 4), 2, None), ('Constant', (1, 5, 1, 6), 3, None)], ('Load',))),
  2300. ('Expression', ('Tuple', (1, 0, 1, 2), [], ('Load',))),
  2301. ('Expression', ('Call', (1, 0, 1, 17), ('Attribute', (1, 0, 1, 7), ('Attribute', (1, 0, 1, 5), ('Attribute', (1, 0, 1, 3), ('Name', (1, 0, 1, 1), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8, 1, 16), ('Attribute', (1, 8, 1, 11), ('Name', (1, 8, 1, 9), 'a', ('Load',)), 'b', ('Load',)), ('Slice', (1, 12, 1, 15), ('Constant', (1, 12, 1, 13), 1, None), ('Constant', (1, 14, 1, 15), 2, None), None), ('Load',))], [])),
  2302. ]
  2303. main()