PageRenderTime 83ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/Python/Python-ast.c

http://unladen-swallow.googlecode.com/
C | 6134 lines | 5910 code | 206 blank | 18 comment | 1940 complexity | abfbffdcfe07f2a65d5898b87275d44b MD5 | raw file
Possible License(s): 0BSD, BSD-3-Clause
  1. /* File automatically generated by Parser/asdl_c.py. */
  2. /*
  3. __version__ 62047.
  4. This module must be committed separately after each AST grammar change;
  5. The __version__ number is set to the revision number of the commit
  6. containing the grammar change.
  7. */
  8. #include "Python.h"
  9. #include "Python-ast.h"
  10. static PyTypeObject AST_type;
  11. static PyTypeObject *mod_type;
  12. static PyObject* ast2obj_mod(void*);
  13. static PyTypeObject *Module_type;
  14. static char *Module_fields[]={
  15. "body",
  16. };
  17. static PyTypeObject *Interactive_type;
  18. static char *Interactive_fields[]={
  19. "body",
  20. };
  21. static PyTypeObject *Expression_type;
  22. static char *Expression_fields[]={
  23. "body",
  24. };
  25. static PyTypeObject *Suite_type;
  26. static char *Suite_fields[]={
  27. "body",
  28. };
  29. static PyTypeObject *stmt_type;
  30. static char *stmt_attributes[] = {
  31. "lineno",
  32. "col_offset",
  33. };
  34. static PyObject* ast2obj_stmt(void*);
  35. static PyTypeObject *FunctionDef_type;
  36. static char *FunctionDef_fields[]={
  37. "name",
  38. "args",
  39. "body",
  40. "decorator_list",
  41. };
  42. static PyTypeObject *ClassDef_type;
  43. static char *ClassDef_fields[]={
  44. "name",
  45. "bases",
  46. "body",
  47. "decorator_list",
  48. };
  49. static PyTypeObject *Return_type;
  50. static char *Return_fields[]={
  51. "value",
  52. };
  53. static PyTypeObject *Delete_type;
  54. static char *Delete_fields[]={
  55. "targets",
  56. };
  57. static PyTypeObject *Assign_type;
  58. static char *Assign_fields[]={
  59. "targets",
  60. "value",
  61. };
  62. static PyTypeObject *AugAssign_type;
  63. static char *AugAssign_fields[]={
  64. "target",
  65. "op",
  66. "value",
  67. };
  68. static PyTypeObject *Print_type;
  69. static char *Print_fields[]={
  70. "dest",
  71. "values",
  72. "nl",
  73. };
  74. static PyTypeObject *For_type;
  75. static char *For_fields[]={
  76. "target",
  77. "iter",
  78. "body",
  79. "orelse",
  80. };
  81. static PyTypeObject *While_type;
  82. static char *While_fields[]={
  83. "test",
  84. "body",
  85. "orelse",
  86. };
  87. static PyTypeObject *If_type;
  88. static char *If_fields[]={
  89. "test",
  90. "body",
  91. "orelse",
  92. };
  93. static PyTypeObject *With_type;
  94. static char *With_fields[]={
  95. "context_expr",
  96. "optional_vars",
  97. "body",
  98. };
  99. static PyTypeObject *Raise_type;
  100. static char *Raise_fields[]={
  101. "type",
  102. "inst",
  103. "tback",
  104. };
  105. static PyTypeObject *TryExcept_type;
  106. static char *TryExcept_fields[]={
  107. "body",
  108. "handlers",
  109. "orelse",
  110. };
  111. static PyTypeObject *TryFinally_type;
  112. static char *TryFinally_fields[]={
  113. "body",
  114. "finalbody",
  115. };
  116. static PyTypeObject *Assert_type;
  117. static char *Assert_fields[]={
  118. "test",
  119. "msg",
  120. };
  121. static PyTypeObject *Import_type;
  122. static char *Import_fields[]={
  123. "names",
  124. };
  125. static PyTypeObject *ImportFrom_type;
  126. static char *ImportFrom_fields[]={
  127. "module",
  128. "names",
  129. "level",
  130. };
  131. static PyTypeObject *Exec_type;
  132. static char *Exec_fields[]={
  133. "body",
  134. "globals",
  135. "locals",
  136. };
  137. static PyTypeObject *Global_type;
  138. static char *Global_fields[]={
  139. "names",
  140. };
  141. static PyTypeObject *Expr_type;
  142. static char *Expr_fields[]={
  143. "value",
  144. };
  145. static PyTypeObject *Pass_type;
  146. static PyTypeObject *Break_type;
  147. static PyTypeObject *Continue_type;
  148. static PyTypeObject *expr_type;
  149. static char *expr_attributes[] = {
  150. "lineno",
  151. "col_offset",
  152. };
  153. static PyObject* ast2obj_expr(void*);
  154. static PyTypeObject *BoolOp_type;
  155. static char *BoolOp_fields[]={
  156. "op",
  157. "values",
  158. };
  159. static PyTypeObject *BinOp_type;
  160. static char *BinOp_fields[]={
  161. "left",
  162. "op",
  163. "right",
  164. };
  165. static PyTypeObject *UnaryOp_type;
  166. static char *UnaryOp_fields[]={
  167. "op",
  168. "operand",
  169. };
  170. static PyTypeObject *Lambda_type;
  171. static char *Lambda_fields[]={
  172. "args",
  173. "body",
  174. };
  175. static PyTypeObject *IfExp_type;
  176. static char *IfExp_fields[]={
  177. "test",
  178. "body",
  179. "orelse",
  180. };
  181. static PyTypeObject *Dict_type;
  182. static char *Dict_fields[]={
  183. "keys",
  184. "values",
  185. };
  186. static PyTypeObject *ListComp_type;
  187. static char *ListComp_fields[]={
  188. "elt",
  189. "generators",
  190. };
  191. static PyTypeObject *GeneratorExp_type;
  192. static char *GeneratorExp_fields[]={
  193. "elt",
  194. "generators",
  195. };
  196. static PyTypeObject *Yield_type;
  197. static char *Yield_fields[]={
  198. "value",
  199. };
  200. static PyTypeObject *Compare_type;
  201. static char *Compare_fields[]={
  202. "left",
  203. "ops",
  204. "comparators",
  205. };
  206. static PyTypeObject *Call_type;
  207. static char *Call_fields[]={
  208. "func",
  209. "args",
  210. "keywords",
  211. "starargs",
  212. "kwargs",
  213. };
  214. static PyTypeObject *Repr_type;
  215. static char *Repr_fields[]={
  216. "value",
  217. };
  218. static PyTypeObject *Num_type;
  219. static char *Num_fields[]={
  220. "n",
  221. };
  222. static PyTypeObject *Str_type;
  223. static char *Str_fields[]={
  224. "s",
  225. };
  226. static PyTypeObject *Attribute_type;
  227. static char *Attribute_fields[]={
  228. "value",
  229. "attr",
  230. "ctx",
  231. };
  232. static PyTypeObject *Subscript_type;
  233. static char *Subscript_fields[]={
  234. "value",
  235. "slice",
  236. "ctx",
  237. };
  238. static PyTypeObject *Name_type;
  239. static char *Name_fields[]={
  240. "id",
  241. "ctx",
  242. };
  243. static PyTypeObject *List_type;
  244. static char *List_fields[]={
  245. "elts",
  246. "ctx",
  247. };
  248. static PyTypeObject *Tuple_type;
  249. static char *Tuple_fields[]={
  250. "elts",
  251. "ctx",
  252. };
  253. static PyTypeObject *expr_context_type;
  254. static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
  255. *AugLoad_singleton, *AugStore_singleton, *Param_singleton;
  256. static PyObject* ast2obj_expr_context(expr_context_ty);
  257. static PyTypeObject *Load_type;
  258. static PyTypeObject *Store_type;
  259. static PyTypeObject *Del_type;
  260. static PyTypeObject *AugLoad_type;
  261. static PyTypeObject *AugStore_type;
  262. static PyTypeObject *Param_type;
  263. static PyTypeObject *slice_type;
  264. static PyObject* ast2obj_slice(void*);
  265. static PyTypeObject *Ellipsis_type;
  266. static PyTypeObject *Slice_type;
  267. static char *Slice_fields[]={
  268. "lower",
  269. "upper",
  270. "step",
  271. };
  272. static PyTypeObject *ExtSlice_type;
  273. static char *ExtSlice_fields[]={
  274. "dims",
  275. };
  276. static PyTypeObject *Index_type;
  277. static char *Index_fields[]={
  278. "value",
  279. };
  280. static PyTypeObject *boolop_type;
  281. static PyObject *And_singleton, *Or_singleton;
  282. static PyObject* ast2obj_boolop(boolop_ty);
  283. static PyTypeObject *And_type;
  284. static PyTypeObject *Or_type;
  285. static PyTypeObject *operator_type;
  286. static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
  287. *Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
  288. *RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
  289. *FloorDiv_singleton;
  290. static PyObject* ast2obj_operator(operator_ty);
  291. static PyTypeObject *Add_type;
  292. static PyTypeObject *Sub_type;
  293. static PyTypeObject *Mult_type;
  294. static PyTypeObject *Div_type;
  295. static PyTypeObject *Mod_type;
  296. static PyTypeObject *Pow_type;
  297. static PyTypeObject *LShift_type;
  298. static PyTypeObject *RShift_type;
  299. static PyTypeObject *BitOr_type;
  300. static PyTypeObject *BitXor_type;
  301. static PyTypeObject *BitAnd_type;
  302. static PyTypeObject *FloorDiv_type;
  303. static PyTypeObject *unaryop_type;
  304. static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
  305. *USub_singleton;
  306. static PyObject* ast2obj_unaryop(unaryop_ty);
  307. static PyTypeObject *Invert_type;
  308. static PyTypeObject *Not_type;
  309. static PyTypeObject *UAdd_type;
  310. static PyTypeObject *USub_type;
  311. static PyTypeObject *cmpop_type;
  312. static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
  313. *Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
  314. *NotIn_singleton;
  315. static PyObject* ast2obj_cmpop(cmpop_ty);
  316. static PyTypeObject *Eq_type;
  317. static PyTypeObject *NotEq_type;
  318. static PyTypeObject *Lt_type;
  319. static PyTypeObject *LtE_type;
  320. static PyTypeObject *Gt_type;
  321. static PyTypeObject *GtE_type;
  322. static PyTypeObject *Is_type;
  323. static PyTypeObject *IsNot_type;
  324. static PyTypeObject *In_type;
  325. static PyTypeObject *NotIn_type;
  326. static PyTypeObject *comprehension_type;
  327. static PyObject* ast2obj_comprehension(void*);
  328. static char *comprehension_fields[]={
  329. "target",
  330. "iter",
  331. "ifs",
  332. };
  333. static PyTypeObject *excepthandler_type;
  334. static char *excepthandler_attributes[] = {
  335. "lineno",
  336. "col_offset",
  337. };
  338. static PyObject* ast2obj_excepthandler(void*);
  339. static PyTypeObject *ExceptHandler_type;
  340. static char *ExceptHandler_fields[]={
  341. "type",
  342. "name",
  343. "body",
  344. };
  345. static PyTypeObject *arguments_type;
  346. static PyObject* ast2obj_arguments(void*);
  347. static char *arguments_fields[]={
  348. "args",
  349. "vararg",
  350. "kwarg",
  351. "defaults",
  352. };
  353. static PyTypeObject *keyword_type;
  354. static PyObject* ast2obj_keyword(void*);
  355. static char *keyword_fields[]={
  356. "arg",
  357. "value",
  358. };
  359. static PyTypeObject *alias_type;
  360. static PyObject* ast2obj_alias(void*);
  361. static char *alias_fields[]={
  362. "name",
  363. "asname",
  364. };
  365. static int
  366. ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
  367. {
  368. Py_ssize_t i, numfields = 0;
  369. int res = -1;
  370. PyObject *key, *value, *fields;
  371. fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
  372. if (!fields)
  373. PyErr_Clear();
  374. if (fields) {
  375. numfields = PySequence_Size(fields);
  376. if (numfields == -1)
  377. goto cleanup;
  378. }
  379. res = 0; /* if no error occurs, this stays 0 to the end */
  380. if (PyTuple_GET_SIZE(args) > 0) {
  381. if (numfields != PyTuple_GET_SIZE(args)) {
  382. PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
  383. "%zd positional argument%s",
  384. Py_TYPE(self)->tp_name,
  385. numfields == 0 ? "" : "either 0 or ",
  386. numfields, numfields == 1 ? "" : "s");
  387. res = -1;
  388. goto cleanup;
  389. }
  390. for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
  391. /* cannot be reached when fields is NULL */
  392. PyObject *name = PySequence_GetItem(fields, i);
  393. if (!name) {
  394. res = -1;
  395. goto cleanup;
  396. }
  397. res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
  398. Py_DECREF(name);
  399. if (res < 0)
  400. goto cleanup;
  401. }
  402. }
  403. if (kw) {
  404. i = 0; /* needed by PyDict_Next */
  405. while (PyDict_Next(kw, &i, &key, &value)) {
  406. res = PyObject_SetAttr(self, key, value);
  407. if (res < 0)
  408. goto cleanup;
  409. }
  410. }
  411. cleanup:
  412. Py_XDECREF(fields);
  413. return res;
  414. }
  415. /* Pickling support */
  416. static PyObject *
  417. ast_type_reduce(PyObject *self, PyObject *unused)
  418. {
  419. PyObject *res;
  420. PyObject *dict = PyObject_GetAttrString(self, "__dict__");
  421. if (dict == NULL) {
  422. if (PyErr_ExceptionMatches(PyExc_AttributeError))
  423. PyErr_Clear();
  424. else
  425. return NULL;
  426. }
  427. if (dict) {
  428. res = Py_BuildValue("O()O", Py_TYPE(self), dict);
  429. Py_DECREF(dict);
  430. return res;
  431. }
  432. return Py_BuildValue("O()", Py_TYPE(self));
  433. }
  434. static PyMethodDef ast_type_methods[] = {
  435. {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
  436. {NULL}
  437. };
  438. static PyTypeObject AST_type = {
  439. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  440. "_ast.AST",
  441. sizeof(PyObject),
  442. 0,
  443. 0, /* tp_dealloc */
  444. 0, /* tp_print */
  445. 0, /* tp_getattr */
  446. 0, /* tp_setattr */
  447. 0, /* tp_compare */
  448. 0, /* tp_repr */
  449. 0, /* tp_as_number */
  450. 0, /* tp_as_sequence */
  451. 0, /* tp_as_mapping */
  452. 0, /* tp_hash */
  453. 0, /* tp_call */
  454. 0, /* tp_str */
  455. PyObject_GenericGetAttr, /* tp_getattro */
  456. PyObject_GenericSetAttr, /* tp_setattro */
  457. 0, /* tp_as_buffer */
  458. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
  459. 0, /* tp_doc */
  460. 0, /* tp_traverse */
  461. 0, /* tp_clear */
  462. 0, /* tp_richcompare */
  463. 0, /* tp_weaklistoffset */
  464. 0, /* tp_iter */
  465. 0, /* tp_iternext */
  466. ast_type_methods, /* tp_methods */
  467. 0, /* tp_members */
  468. 0, /* tp_getset */
  469. 0, /* tp_base */
  470. 0, /* tp_dict */
  471. 0, /* tp_descr_get */
  472. 0, /* tp_descr_set */
  473. 0, /* tp_dictoffset */
  474. (initproc)ast_type_init, /* tp_init */
  475. PyType_GenericAlloc, /* tp_alloc */
  476. PyType_GenericNew, /* tp_new */
  477. PyObject_Del, /* tp_free */
  478. };
  479. static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
  480. {
  481. PyObject *fnames, *result;
  482. int i;
  483. fnames = PyTuple_New(num_fields);
  484. if (!fnames) return NULL;
  485. for (i = 0; i < num_fields; i++) {
  486. PyObject *field = PyString_FromString(fields[i]);
  487. if (!field) {
  488. Py_DECREF(fnames);
  489. return NULL;
  490. }
  491. PyTuple_SET_ITEM(fnames, i, field);
  492. }
  493. result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
  494. type, base, "_fields", fnames, "__module__", "_ast");
  495. Py_DECREF(fnames);
  496. return (PyTypeObject*)result;
  497. }
  498. static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
  499. {
  500. int i, result;
  501. PyObject *s, *l = PyTuple_New(num_fields);
  502. if (!l) return 0;
  503. for(i = 0; i < num_fields; i++) {
  504. s = PyString_FromString(attrs[i]);
  505. if (!s) {
  506. Py_DECREF(l);
  507. return 0;
  508. }
  509. PyTuple_SET_ITEM(l, i, s);
  510. }
  511. result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
  512. Py_DECREF(l);
  513. return result;
  514. }
  515. /* Conversion AST -> Python */
  516. static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
  517. {
  518. int i, n = asdl_seq_LEN(seq);
  519. PyObject *result = PyList_New(n);
  520. PyObject *value;
  521. if (!result)
  522. return NULL;
  523. for (i = 0; i < n; i++) {
  524. value = func(asdl_seq_GET(seq, i));
  525. if (!value) {
  526. Py_DECREF(result);
  527. return NULL;
  528. }
  529. PyList_SET_ITEM(result, i, value);
  530. }
  531. return result;
  532. }
  533. static PyObject* ast2obj_object(void *o)
  534. {
  535. if (!o)
  536. o = Py_None;
  537. Py_INCREF((PyObject*)o);
  538. return (PyObject*)o;
  539. }
  540. #define ast2obj_identifier ast2obj_object
  541. #define ast2obj_string ast2obj_object
  542. static PyObject* ast2obj_bool(bool b)
  543. {
  544. return PyBool_FromLong(b);
  545. }
  546. static PyObject* ast2obj_int(long b)
  547. {
  548. return PyInt_FromLong(b);
  549. }
  550. /* Conversion Python -> AST */
  551. static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
  552. {
  553. if (obj == Py_None)
  554. obj = NULL;
  555. if (obj)
  556. PyArena_AddPyObject(arena, obj);
  557. Py_XINCREF(obj);
  558. *out = obj;
  559. return 0;
  560. }
  561. #define obj2ast_identifier obj2ast_object
  562. #define obj2ast_string obj2ast_object
  563. static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
  564. {
  565. int i;
  566. if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
  567. PyObject *s = PyObject_Repr(obj);
  568. if (s == NULL) return 1;
  569. PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
  570. PyString_AS_STRING(s));
  571. Py_DECREF(s);
  572. return 1;
  573. }
  574. i = (int)PyLong_AsLong(obj);
  575. if (i == -1 && PyErr_Occurred())
  576. return 1;
  577. *out = i;
  578. return 0;
  579. }
  580. static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
  581. {
  582. if (!PyBool_Check(obj)) {
  583. PyObject *s = PyObject_Repr(obj);
  584. if (s == NULL) return 1;
  585. PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
  586. PyString_AS_STRING(s));
  587. Py_DECREF(s);
  588. return 1;
  589. }
  590. *out = (obj == Py_True);
  591. return 0;
  592. }
  593. static int add_ast_fields(void)
  594. {
  595. PyObject *empty_tuple, *d;
  596. if (PyType_Ready(&AST_type) < 0)
  597. return -1;
  598. d = AST_type.tp_dict;
  599. empty_tuple = PyTuple_New(0);
  600. if (!empty_tuple ||
  601. PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
  602. PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
  603. Py_XDECREF(empty_tuple);
  604. return -1;
  605. }
  606. Py_DECREF(empty_tuple);
  607. return 0;
  608. }
  609. static int init_types(void)
  610. {
  611. static int initialized;
  612. if (initialized) return 1;
  613. if (add_ast_fields() < 0) return 0;
  614. mod_type = make_type("mod", &AST_type, NULL, 0);
  615. if (!mod_type) return 0;
  616. if (!add_attributes(mod_type, NULL, 0)) return 0;
  617. Module_type = make_type("Module", mod_type, Module_fields, 1);
  618. if (!Module_type) return 0;
  619. Interactive_type = make_type("Interactive", mod_type,
  620. Interactive_fields, 1);
  621. if (!Interactive_type) return 0;
  622. Expression_type = make_type("Expression", mod_type, Expression_fields,
  623. 1);
  624. if (!Expression_type) return 0;
  625. Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
  626. if (!Suite_type) return 0;
  627. stmt_type = make_type("stmt", &AST_type, NULL, 0);
  628. if (!stmt_type) return 0;
  629. if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
  630. FunctionDef_type = make_type("FunctionDef", stmt_type,
  631. FunctionDef_fields, 4);
  632. if (!FunctionDef_type) return 0;
  633. ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
  634. if (!ClassDef_type) return 0;
  635. Return_type = make_type("Return", stmt_type, Return_fields, 1);
  636. if (!Return_type) return 0;
  637. Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
  638. if (!Delete_type) return 0;
  639. Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
  640. if (!Assign_type) return 0;
  641. AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
  642. if (!AugAssign_type) return 0;
  643. Print_type = make_type("Print", stmt_type, Print_fields, 3);
  644. if (!Print_type) return 0;
  645. For_type = make_type("For", stmt_type, For_fields, 4);
  646. if (!For_type) return 0;
  647. While_type = make_type("While", stmt_type, While_fields, 3);
  648. if (!While_type) return 0;
  649. If_type = make_type("If", stmt_type, If_fields, 3);
  650. if (!If_type) return 0;
  651. With_type = make_type("With", stmt_type, With_fields, 3);
  652. if (!With_type) return 0;
  653. Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
  654. if (!Raise_type) return 0;
  655. TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
  656. if (!TryExcept_type) return 0;
  657. TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
  658. 2);
  659. if (!TryFinally_type) return 0;
  660. Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
  661. if (!Assert_type) return 0;
  662. Import_type = make_type("Import", stmt_type, Import_fields, 1);
  663. if (!Import_type) return 0;
  664. ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
  665. 3);
  666. if (!ImportFrom_type) return 0;
  667. Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
  668. if (!Exec_type) return 0;
  669. Global_type = make_type("Global", stmt_type, Global_fields, 1);
  670. if (!Global_type) return 0;
  671. Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
  672. if (!Expr_type) return 0;
  673. Pass_type = make_type("Pass", stmt_type, NULL, 0);
  674. if (!Pass_type) return 0;
  675. Break_type = make_type("Break", stmt_type, NULL, 0);
  676. if (!Break_type) return 0;
  677. Continue_type = make_type("Continue", stmt_type, NULL, 0);
  678. if (!Continue_type) return 0;
  679. expr_type = make_type("expr", &AST_type, NULL, 0);
  680. if (!expr_type) return 0;
  681. if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
  682. BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
  683. if (!BoolOp_type) return 0;
  684. BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
  685. if (!BinOp_type) return 0;
  686. UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
  687. if (!UnaryOp_type) return 0;
  688. Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
  689. if (!Lambda_type) return 0;
  690. IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
  691. if (!IfExp_type) return 0;
  692. Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
  693. if (!Dict_type) return 0;
  694. ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
  695. if (!ListComp_type) return 0;
  696. GeneratorExp_type = make_type("GeneratorExp", expr_type,
  697. GeneratorExp_fields, 2);
  698. if (!GeneratorExp_type) return 0;
  699. Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
  700. if (!Yield_type) return 0;
  701. Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
  702. if (!Compare_type) return 0;
  703. Call_type = make_type("Call", expr_type, Call_fields, 5);
  704. if (!Call_type) return 0;
  705. Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
  706. if (!Repr_type) return 0;
  707. Num_type = make_type("Num", expr_type, Num_fields, 1);
  708. if (!Num_type) return 0;
  709. Str_type = make_type("Str", expr_type, Str_fields, 1);
  710. if (!Str_type) return 0;
  711. Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
  712. if (!Attribute_type) return 0;
  713. Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
  714. if (!Subscript_type) return 0;
  715. Name_type = make_type("Name", expr_type, Name_fields, 2);
  716. if (!Name_type) return 0;
  717. List_type = make_type("List", expr_type, List_fields, 2);
  718. if (!List_type) return 0;
  719. Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
  720. if (!Tuple_type) return 0;
  721. expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
  722. if (!expr_context_type) return 0;
  723. if (!add_attributes(expr_context_type, NULL, 0)) return 0;
  724. Load_type = make_type("Load", expr_context_type, NULL, 0);
  725. if (!Load_type) return 0;
  726. Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
  727. if (!Load_singleton) return 0;
  728. Store_type = make_type("Store", expr_context_type, NULL, 0);
  729. if (!Store_type) return 0;
  730. Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
  731. if (!Store_singleton) return 0;
  732. Del_type = make_type("Del", expr_context_type, NULL, 0);
  733. if (!Del_type) return 0;
  734. Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
  735. if (!Del_singleton) return 0;
  736. AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
  737. if (!AugLoad_type) return 0;
  738. AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
  739. if (!AugLoad_singleton) return 0;
  740. AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
  741. if (!AugStore_type) return 0;
  742. AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
  743. if (!AugStore_singleton) return 0;
  744. Param_type = make_type("Param", expr_context_type, NULL, 0);
  745. if (!Param_type) return 0;
  746. Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
  747. if (!Param_singleton) return 0;
  748. slice_type = make_type("slice", &AST_type, NULL, 0);
  749. if (!slice_type) return 0;
  750. if (!add_attributes(slice_type, NULL, 0)) return 0;
  751. Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
  752. if (!Ellipsis_type) return 0;
  753. Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
  754. if (!Slice_type) return 0;
  755. ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
  756. if (!ExtSlice_type) return 0;
  757. Index_type = make_type("Index", slice_type, Index_fields, 1);
  758. if (!Index_type) return 0;
  759. boolop_type = make_type("boolop", &AST_type, NULL, 0);
  760. if (!boolop_type) return 0;
  761. if (!add_attributes(boolop_type, NULL, 0)) return 0;
  762. And_type = make_type("And", boolop_type, NULL, 0);
  763. if (!And_type) return 0;
  764. And_singleton = PyType_GenericNew(And_type, NULL, NULL);
  765. if (!And_singleton) return 0;
  766. Or_type = make_type("Or", boolop_type, NULL, 0);
  767. if (!Or_type) return 0;
  768. Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
  769. if (!Or_singleton) return 0;
  770. operator_type = make_type("operator", &AST_type, NULL, 0);
  771. if (!operator_type) return 0;
  772. if (!add_attributes(operator_type, NULL, 0)) return 0;
  773. Add_type = make_type("Add", operator_type, NULL, 0);
  774. if (!Add_type) return 0;
  775. Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
  776. if (!Add_singleton) return 0;
  777. Sub_type = make_type("Sub", operator_type, NULL, 0);
  778. if (!Sub_type) return 0;
  779. Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
  780. if (!Sub_singleton) return 0;
  781. Mult_type = make_type("Mult", operator_type, NULL, 0);
  782. if (!Mult_type) return 0;
  783. Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
  784. if (!Mult_singleton) return 0;
  785. Div_type = make_type("Div", operator_type, NULL, 0);
  786. if (!Div_type) return 0;
  787. Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
  788. if (!Div_singleton) return 0;
  789. Mod_type = make_type("Mod", operator_type, NULL, 0);
  790. if (!Mod_type) return 0;
  791. Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
  792. if (!Mod_singleton) return 0;
  793. Pow_type = make_type("Pow", operator_type, NULL, 0);
  794. if (!Pow_type) return 0;
  795. Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
  796. if (!Pow_singleton) return 0;
  797. LShift_type = make_type("LShift", operator_type, NULL, 0);
  798. if (!LShift_type) return 0;
  799. LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
  800. if (!LShift_singleton) return 0;
  801. RShift_type = make_type("RShift", operator_type, NULL, 0);
  802. if (!RShift_type) return 0;
  803. RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
  804. if (!RShift_singleton) return 0;
  805. BitOr_type = make_type("BitOr", operator_type, NULL, 0);
  806. if (!BitOr_type) return 0;
  807. BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
  808. if (!BitOr_singleton) return 0;
  809. BitXor_type = make_type("BitXor", operator_type, NULL, 0);
  810. if (!BitXor_type) return 0;
  811. BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
  812. if (!BitXor_singleton) return 0;
  813. BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
  814. if (!BitAnd_type) return 0;
  815. BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
  816. if (!BitAnd_singleton) return 0;
  817. FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
  818. if (!FloorDiv_type) return 0;
  819. FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
  820. if (!FloorDiv_singleton) return 0;
  821. unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
  822. if (!unaryop_type) return 0;
  823. if (!add_attributes(unaryop_type, NULL, 0)) return 0;
  824. Invert_type = make_type("Invert", unaryop_type, NULL, 0);
  825. if (!Invert_type) return 0;
  826. Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
  827. if (!Invert_singleton) return 0;
  828. Not_type = make_type("Not", unaryop_type, NULL, 0);
  829. if (!Not_type) return 0;
  830. Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
  831. if (!Not_singleton) return 0;
  832. UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
  833. if (!UAdd_type) return 0;
  834. UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
  835. if (!UAdd_singleton) return 0;
  836. USub_type = make_type("USub", unaryop_type, NULL, 0);
  837. if (!USub_type) return 0;
  838. USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
  839. if (!USub_singleton) return 0;
  840. cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
  841. if (!cmpop_type) return 0;
  842. if (!add_attributes(cmpop_type, NULL, 0)) return 0;
  843. Eq_type = make_type("Eq", cmpop_type, NULL, 0);
  844. if (!Eq_type) return 0;
  845. Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
  846. if (!Eq_singleton) return 0;
  847. NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
  848. if (!NotEq_type) return 0;
  849. NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
  850. if (!NotEq_singleton) return 0;
  851. Lt_type = make_type("Lt", cmpop_type, NULL, 0);
  852. if (!Lt_type) return 0;
  853. Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
  854. if (!Lt_singleton) return 0;
  855. LtE_type = make_type("LtE", cmpop_type, NULL, 0);
  856. if (!LtE_type) return 0;
  857. LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
  858. if (!LtE_singleton) return 0;
  859. Gt_type = make_type("Gt", cmpop_type, NULL, 0);
  860. if (!Gt_type) return 0;
  861. Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
  862. if (!Gt_singleton) return 0;
  863. GtE_type = make_type("GtE", cmpop_type, NULL, 0);
  864. if (!GtE_type) return 0;
  865. GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
  866. if (!GtE_singleton) return 0;
  867. Is_type = make_type("Is", cmpop_type, NULL, 0);
  868. if (!Is_type) return 0;
  869. Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
  870. if (!Is_singleton) return 0;
  871. IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
  872. if (!IsNot_type) return 0;
  873. IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
  874. if (!IsNot_singleton) return 0;
  875. In_type = make_type("In", cmpop_type, NULL, 0);
  876. if (!In_type) return 0;
  877. In_singleton = PyType_GenericNew(In_type, NULL, NULL);
  878. if (!In_singleton) return 0;
  879. NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
  880. if (!NotIn_type) return 0;
  881. NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
  882. if (!NotIn_singleton) return 0;
  883. comprehension_type = make_type("comprehension", &AST_type,
  884. comprehension_fields, 3);
  885. if (!comprehension_type) return 0;
  886. excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
  887. if (!excepthandler_type) return 0;
  888. if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
  889. return 0;
  890. ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
  891. ExceptHandler_fields, 3);
  892. if (!ExceptHandler_type) return 0;
  893. arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
  894. if (!arguments_type) return 0;
  895. keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
  896. if (!keyword_type) return 0;
  897. alias_type = make_type("alias", &AST_type, alias_fields, 2);
  898. if (!alias_type) return 0;
  899. initialized = 1;
  900. return 1;
  901. }
  902. static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
  903. static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
  904. static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
  905. static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
  906. arena);
  907. static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
  908. static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
  909. static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
  910. static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
  911. static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
  912. static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
  913. arena);
  914. static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
  915. arena);
  916. static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
  917. static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
  918. static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
  919. mod_ty
  920. Module(asdl_seq * body, PyArena *arena)
  921. {
  922. mod_ty p;
  923. p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
  924. if (!p)
  925. return NULL;
  926. p->kind = Module_kind;
  927. p->v.Module.body = body;
  928. return p;
  929. }
  930. mod_ty
  931. Interactive(asdl_seq * body, PyArena *arena)
  932. {
  933. mod_ty p;
  934. p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
  935. if (!p)
  936. return NULL;
  937. p->kind = Interactive_kind;
  938. p->v.Interactive.body = body;
  939. return p;
  940. }
  941. mod_ty
  942. Expression(expr_ty body, PyArena *arena)
  943. {
  944. mod_ty p;
  945. if (!body) {
  946. PyErr_SetString(PyExc_ValueError,
  947. "field body is required for Expression");
  948. return NULL;
  949. }
  950. p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
  951. if (!p)
  952. return NULL;
  953. p->kind = Expression_kind;
  954. p->v.Expression.body = body;
  955. return p;
  956. }
  957. mod_ty
  958. Suite(asdl_seq * body, PyArena *arena)
  959. {
  960. mod_ty p;
  961. p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
  962. if (!p)
  963. return NULL;
  964. p->kind = Suite_kind;
  965. p->v.Suite.body = body;
  966. return p;
  967. }
  968. stmt_ty
  969. FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
  970. decorator_list, int lineno, int col_offset, PyArena *arena)
  971. {
  972. stmt_ty p;
  973. if (!name) {
  974. PyErr_SetString(PyExc_ValueError,
  975. "field name is required for FunctionDef");
  976. return NULL;
  977. }
  978. if (!args) {
  979. PyErr_SetString(PyExc_ValueError,
  980. "field args is required for FunctionDef");
  981. return NULL;
  982. }
  983. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  984. if (!p)
  985. return NULL;
  986. p->kind = FunctionDef_kind;
  987. p->v.FunctionDef.name = name;
  988. p->v.FunctionDef.args = args;
  989. p->v.FunctionDef.body = body;
  990. p->v.FunctionDef.decorator_list = decorator_list;
  991. p->lineno = lineno;
  992. p->col_offset = col_offset;
  993. return p;
  994. }
  995. stmt_ty
  996. ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
  997. decorator_list, int lineno, int col_offset, PyArena *arena)
  998. {
  999. stmt_ty p;
  1000. if (!name) {
  1001. PyErr_SetString(PyExc_ValueError,
  1002. "field name is required for ClassDef");
  1003. return NULL;
  1004. }
  1005. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1006. if (!p)
  1007. return NULL;
  1008. p->kind = ClassDef_kind;
  1009. p->v.ClassDef.name = name;
  1010. p->v.ClassDef.bases = bases;
  1011. p->v.ClassDef.body = body;
  1012. p->v.ClassDef.decorator_list = decorator_list;
  1013. p->lineno = lineno;
  1014. p->col_offset = col_offset;
  1015. return p;
  1016. }
  1017. stmt_ty
  1018. Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
  1019. {
  1020. stmt_ty p;
  1021. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1022. if (!p)
  1023. return NULL;
  1024. p->kind = Return_kind;
  1025. p->v.Return.value = value;
  1026. p->lineno = lineno;
  1027. p->col_offset = col_offset;
  1028. return p;
  1029. }
  1030. stmt_ty
  1031. Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
  1032. {
  1033. stmt_ty p;
  1034. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1035. if (!p)
  1036. return NULL;
  1037. p->kind = Delete_kind;
  1038. p->v.Delete.targets = targets;
  1039. p->lineno = lineno;
  1040. p->col_offset = col_offset;
  1041. return p;
  1042. }
  1043. stmt_ty
  1044. Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
  1045. *arena)
  1046. {
  1047. stmt_ty p;
  1048. if (!value) {
  1049. PyErr_SetString(PyExc_ValueError,
  1050. "field value is required for Assign");
  1051. return NULL;
  1052. }
  1053. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1054. if (!p)
  1055. return NULL;
  1056. p->kind = Assign_kind;
  1057. p->v.Assign.targets = targets;
  1058. p->v.Assign.value = value;
  1059. p->lineno = lineno;
  1060. p->col_offset = col_offset;
  1061. return p;
  1062. }
  1063. stmt_ty
  1064. AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
  1065. col_offset, PyArena *arena)
  1066. {
  1067. stmt_ty p;
  1068. if (!target) {
  1069. PyErr_SetString(PyExc_ValueError,
  1070. "field target is required for AugAssign");
  1071. return NULL;
  1072. }
  1073. if (!op) {
  1074. PyErr_SetString(PyExc_ValueError,
  1075. "field op is required for AugAssign");
  1076. return NULL;
  1077. }
  1078. if (!value) {
  1079. PyErr_SetString(PyExc_ValueError,
  1080. "field value is required for AugAssign");
  1081. return NULL;
  1082. }
  1083. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1084. if (!p)
  1085. return NULL;
  1086. p->kind = AugAssign_kind;
  1087. p->v.AugAssign.target = target;
  1088. p->v.AugAssign.op = op;
  1089. p->v.AugAssign.value = value;
  1090. p->lineno = lineno;
  1091. p->col_offset = col_offset;
  1092. return p;
  1093. }
  1094. stmt_ty
  1095. Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
  1096. PyArena *arena)
  1097. {
  1098. stmt_ty p;
  1099. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1100. if (!p)
  1101. return NULL;
  1102. p->kind = Print_kind;
  1103. p->v.Print.dest = dest;
  1104. p->v.Print.values = values;
  1105. p->v.Print.nl = nl;
  1106. p->lineno = lineno;
  1107. p->col_offset = col_offset;
  1108. return p;
  1109. }
  1110. stmt_ty
  1111. For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
  1112. lineno, int col_offset, PyArena *arena)
  1113. {
  1114. stmt_ty p;
  1115. if (!target) {
  1116. PyErr_SetString(PyExc_ValueError,
  1117. "field target is required for For");
  1118. return NULL;
  1119. }
  1120. if (!iter) {
  1121. PyErr_SetString(PyExc_ValueError,
  1122. "field iter is required for For");
  1123. return NULL;
  1124. }
  1125. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1126. if (!p)
  1127. return NULL;
  1128. p->kind = For_kind;
  1129. p->v.For.target = target;
  1130. p->v.For.iter = iter;
  1131. p->v.For.body = body;
  1132. p->v.For.orelse = orelse;
  1133. p->lineno = lineno;
  1134. p->col_offset = col_offset;
  1135. return p;
  1136. }
  1137. stmt_ty
  1138. While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
  1139. col_offset, PyArena *arena)
  1140. {
  1141. stmt_ty p;
  1142. if (!test) {
  1143. PyErr_SetString(PyExc_ValueError,
  1144. "field test is required for While");
  1145. return NULL;
  1146. }
  1147. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1148. if (!p)
  1149. return NULL;
  1150. p->kind = While_kind;
  1151. p->v.While.test = test;
  1152. p->v.While.body = body;
  1153. p->v.While.orelse = orelse;
  1154. p->lineno = lineno;
  1155. p->col_offset = col_offset;
  1156. return p;
  1157. }
  1158. stmt_ty
  1159. If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
  1160. col_offset, PyArena *arena)
  1161. {
  1162. stmt_ty p;
  1163. if (!test) {
  1164. PyErr_SetString(PyExc_ValueError,
  1165. "field test is required for If");
  1166. return NULL;
  1167. }
  1168. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1169. if (!p)
  1170. return NULL;
  1171. p->kind = If_kind;
  1172. p->v.If.test = test;
  1173. p->v.If.body = body;
  1174. p->v.If.orelse = orelse;
  1175. p->lineno = lineno;
  1176. p->col_offset = col_offset;
  1177. return p;
  1178. }
  1179. stmt_ty
  1180. With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
  1181. int col_offset, PyArena *arena)
  1182. {
  1183. stmt_ty p;
  1184. if (!context_expr) {
  1185. PyErr_SetString(PyExc_ValueError,
  1186. "field context_expr is required for With");
  1187. return NULL;
  1188. }
  1189. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1190. if (!p)
  1191. return NULL;
  1192. p->kind = With_kind;
  1193. p->v.With.context_expr = context_expr;
  1194. p->v.With.optional_vars = optional_vars;
  1195. p->v.With.body = body;
  1196. p->lineno = lineno;
  1197. p->col_offset = col_offset;
  1198. return p;
  1199. }
  1200. stmt_ty
  1201. Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
  1202. PyArena *arena)
  1203. {
  1204. stmt_ty p;
  1205. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1206. if (!p)
  1207. return NULL;
  1208. p->kind = Raise_kind;
  1209. p->v.Raise.type = type;
  1210. p->v.Raise.inst = inst;
  1211. p->v.Raise.tback = tback;
  1212. p->lineno = lineno;
  1213. p->col_offset = col_offset;
  1214. return p;
  1215. }
  1216. stmt_ty
  1217. TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
  1218. int col_offset, PyArena *arena)
  1219. {
  1220. stmt_ty p;
  1221. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1222. if (!p)
  1223. return NULL;
  1224. p->kind = TryExcept_kind;
  1225. p->v.TryExcept.body = body;
  1226. p->v.TryExcept.handlers = handlers;
  1227. p->v.TryExcept.orelse = orelse;
  1228. p->lineno = lineno;
  1229. p->col_offset = col_offset;
  1230. return p;
  1231. }
  1232. stmt_ty
  1233. TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
  1234. PyArena *arena)
  1235. {
  1236. stmt_ty p;
  1237. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1238. if (!p)
  1239. return NULL;
  1240. p->kind = TryFinally_kind;
  1241. p->v.TryFinally.body = body;
  1242. p->v.TryFinally.finalbody = finalbody;
  1243. p->lineno = lineno;
  1244. p->col_offset = col_offset;
  1245. return p;
  1246. }
  1247. stmt_ty
  1248. Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
  1249. {
  1250. stmt_ty p;
  1251. if (!test) {
  1252. PyErr_SetString(PyExc_ValueError,
  1253. "field test is required for Assert");
  1254. return NULL;
  1255. }
  1256. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1257. if (!p)
  1258. return NULL;
  1259. p->kind = Assert_kind;
  1260. p->v.Assert.test = test;
  1261. p->v.Assert.msg = msg;
  1262. p->lineno = lineno;
  1263. p->col_offset = col_offset;
  1264. return p;
  1265. }
  1266. stmt_ty
  1267. Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
  1268. {
  1269. stmt_ty p;
  1270. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1271. if (!p)
  1272. return NULL;
  1273. p->kind = Import_kind;
  1274. p->v.Import.names = names;
  1275. p->lineno = lineno;
  1276. p->col_offset = col_offset;
  1277. return p;
  1278. }
  1279. stmt_ty
  1280. ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
  1281. col_offset, PyArena *arena)
  1282. {
  1283. stmt_ty p;
  1284. if (!module) {
  1285. PyErr_SetString(PyExc_ValueError,
  1286. "field module is required for ImportFrom");
  1287. return NULL;
  1288. }
  1289. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1290. if (!p)
  1291. return NULL;
  1292. p->kind = ImportFrom_kind;
  1293. p->v.ImportFrom.module = module;
  1294. p->v.ImportFrom.names = names;
  1295. p->v.ImportFrom.level = level;
  1296. p->lineno = lineno;
  1297. p->col_offset = col_offset;
  1298. return p;
  1299. }
  1300. stmt_ty
  1301. Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
  1302. PyArena *arena)
  1303. {
  1304. stmt_ty p;
  1305. if (!body) {
  1306. PyErr_SetString(PyExc_ValueError,
  1307. "field body is required for Exec");
  1308. return NULL;
  1309. }
  1310. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1311. if (!p)
  1312. return NULL;
  1313. p->kind = Exec_kind;
  1314. p->v.Exec.body = body;
  1315. p->v.Exec.globals = globals;
  1316. p->v.Exec.locals = locals;
  1317. p->lineno = lineno;
  1318. p->col_offset = col_offset;
  1319. return p;
  1320. }
  1321. stmt_ty
  1322. Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
  1323. {
  1324. stmt_ty p;
  1325. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1326. if (!p)
  1327. return NULL;
  1328. p->kind = Global_kind;
  1329. p->v.Global.names = names;
  1330. p->lineno = lineno;
  1331. p->col_offset = col_offset;
  1332. return p;
  1333. }
  1334. stmt_ty
  1335. Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
  1336. {
  1337. stmt_ty p;
  1338. if (!value) {
  1339. PyErr_SetString(PyExc_ValueError,
  1340. "field value is required for Expr");
  1341. return NULL;
  1342. }
  1343. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1344. if (!p)
  1345. return NULL;
  1346. p->kind = Expr_kind;
  1347. p->v.Expr.value = value;
  1348. p->lineno = lineno;
  1349. p->col_offset = col_offset;
  1350. return p;
  1351. }
  1352. stmt_ty
  1353. Pass(int lineno, int col_offset, PyArena *arena)
  1354. {
  1355. stmt_ty p;
  1356. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1357. if (!p)
  1358. return NULL;
  1359. p->kind = Pass_kind;
  1360. p->lineno = lineno;
  1361. p->col_offset = col_offset;
  1362. return p;
  1363. }
  1364. stmt_ty
  1365. Break(int lineno, int col_offset, PyArena *arena)
  1366. {
  1367. stmt_ty p;
  1368. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1369. if (!p)
  1370. return NULL;
  1371. p->kind = Break_kind;
  1372. p->lineno = lineno;
  1373. p->col_offset = col_offset;
  1374. return p;
  1375. }
  1376. stmt_ty
  1377. Continue(int lineno, int col_offset, PyArena *arena)
  1378. {
  1379. stmt_ty p;
  1380. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1381. if (!p)
  1382. return NULL;
  1383. p->kind = Continue_kind;
  1384. p->lineno = lineno;
  1385. p->col_offset = col_offset;
  1386. return p;
  1387. }
  1388. expr_ty
  1389. BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
  1390. *arena)
  1391. {
  1392. expr_ty p;
  1393. if (!op) {
  1394. PyErr_SetString(PyExc_ValueError,
  1395. "field op is required for BoolOp");
  1396. return NULL;
  1397. }
  1398. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1399. if (!p)
  1400. return NULL;
  1401. p->kind = BoolOp_kind;
  1402. p->v.BoolOp.op = op;
  1403. p->v.BoolOp.values = values;
  1404. p->lineno = lineno;
  1405. p->col_offset = col_offset;
  1406. return p;
  1407. }
  1408. expr_ty
  1409. BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
  1410. PyArena *arena)
  1411. {
  1412. expr_ty p;
  1413. if (!left) {
  1414. PyErr_SetString(PyExc_ValueError,
  1415. "field left is required for BinOp");
  1416. return NULL;
  1417. }
  1418. if (!op) {
  1419. PyErr_SetString(PyExc_ValueError,
  1420. "field op is required for BinOp");
  1421. return NULL;
  1422. }
  1423. if (!right) {
  1424. PyErr_SetString(PyExc_ValueError,
  1425. "field right is required for BinOp");
  1426. return NULL;
  1427. }
  1428. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1429. if (!p)
  1430. return NULL;
  1431. p->kind = BinOp_kind;
  1432. p->v.BinOp.left = left;
  1433. p->v.BinOp.op = op;
  1434. p->v.BinOp.right = right;
  1435. p->lineno = lineno;
  1436. p->col_offset = col_offset;
  1437. return p;
  1438. }
  1439. expr_ty
  1440. UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
  1441. *arena)
  1442. {
  1443. expr_ty p;
  1444. if (!op) {
  1445. PyErr_SetString(PyExc_ValueError,
  1446. "field op is required for UnaryOp");
  1447. return NULL;
  1448. }
  1449. if (!operand) {
  1450. PyErr_SetString(PyExc_ValueError,
  1451. "field operand is required for UnaryOp");
  1452. return NULL;
  1453. }
  1454. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1455. if (!p)
  1456. return NULL;
  1457. p->kind = UnaryOp_kind;
  1458. p->v.UnaryOp.op = op;
  1459. p->v.UnaryOp.operand = operand;
  1460. p->lineno = lineno;
  1461. p->col_offset = col_offset;
  1462. return p;
  1463. }
  1464. expr_ty
  1465. Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
  1466. *arena)
  1467. {
  1468. expr_ty p;
  1469. if (!args) {
  1470. PyErr_SetString(PyExc_ValueError,
  1471. "field args is required for Lambda");
  1472. return NULL;
  1473. }
  1474. if (!body) {
  1475. PyErr_SetString(PyExc_ValueError,
  1476. "field body is required for Lambda");
  1477. return NULL;
  1478. }
  1479. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1480. if (!p)
  1481. return NULL;
  1482. p->kind = Lambda_kind;
  1483. p->v.Lambda.args = args;
  1484. p->v.Lambda.body = body;
  1485. p->lineno = lineno;
  1486. p->col_offset = col_offset;
  1487. return p;
  1488. }
  1489. expr_ty
  1490. IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
  1491. PyArena *arena)
  1492. {
  1493. expr_ty p;
  1494. if (!test) {
  1495. PyErr_SetString(PyExc_ValueError,
  1496. "field test is required for IfExp");
  1497. return NULL;
  1498. }
  1499. if (!body) {
  1500. PyErr_SetString(PyExc_ValueError,
  1501. "field body is required for IfExp");
  1502. return NULL;
  1503. }
  1504. if (!orelse) {
  1505. PyErr_SetString(PyExc_ValueError,
  1506. "field orelse is required for IfExp");
  1507. return NULL;
  1508. }
  1509. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1510. if (!p)
  1511. return NULL;
  1512. p->kind = IfExp_kind;
  1513. p->v.IfExp.test = test;
  1514. p->v.IfExp.body = body;
  1515. p->v.IfExp.orelse = orelse;
  1516. p->lineno = lineno;
  1517. p->col_offset = col_offset;
  1518. return p;
  1519. }
  1520. expr_ty
  1521. Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
  1522. *arena)
  1523. {
  1524. expr_ty p;
  1525. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1526. if (!p)
  1527. return NULL;
  1528. p->kind = Dict_kind;
  1529. p->v.Dict.keys = keys;
  1530. p->v.Dict.values = values;
  1531. p->lineno = lineno;
  1532. p->col_offset = col_offset;
  1533. return p;
  1534. }
  1535. expr_ty
  1536. ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
  1537. PyArena *arena)
  1538. {
  1539. expr_ty p;
  1540. if (!elt) {
  1541. PyErr_SetString(PyExc_ValueError,
  1542. "field elt is required for ListComp");
  1543. return NULL;
  1544. }
  1545. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1546. if (!p)
  1547. return NULL;
  1548. p->kind = ListComp_kind;
  1549. p->v.ListComp.elt = elt;
  1550. p->v.ListComp.generators = generators;
  1551. p->lineno = lineno;
  1552. p->col_offset = col_offset;
  1553. return p;
  1554. }
  1555. expr_ty
  1556. GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
  1557. PyArena *arena)
  1558. {
  1559. expr_ty p;
  1560. if (!elt) {
  1561. PyErr_SetString(PyExc_ValueError,
  1562. "field elt is required for GeneratorExp");
  1563. return NULL;
  1564. }
  1565. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1566. if (!p)
  1567. return NULL;
  1568. p->kind = GeneratorExp_kind;
  1569. p->v.GeneratorExp.elt = elt;
  1570. p->v.GeneratorExp.generators = generators;
  1571. p->lineno = lineno;
  1572. p->col_offset = col_offset;
  1573. return p;
  1574. }
  1575. expr_ty
  1576. Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
  1577. {
  1578. expr_ty p;
  1579. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1580. if (!p)
  1581. return NULL;
  1582. p->kind = Yield_kind;
  1583. p->v.Yield.value = value;
  1584. p->lineno = lineno;
  1585. p->col_offset = col_offset;
  1586. return p;
  1587. }
  1588. expr_ty
  1589. Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
  1590. int col_offset, PyArena *arena)
  1591. {
  1592. expr_ty p;
  1593. if (!left) {
  1594. PyErr_SetString(PyExc_ValueError,
  1595. "field left is required for Compare");
  1596. return NULL;
  1597. }
  1598. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1599. if (!p)
  1600. return NULL;
  1601. p->kind = Compare_kind;
  1602. p->v.Compare.left = left;
  1603. p->v.Compare.ops = ops;
  1604. p->v.Compare.comparators = comparators;
  1605. p->lineno = lineno;
  1606. p->col_offset = col_offset;
  1607. return p;
  1608. }
  1609. expr_ty
  1610. Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
  1611. expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
  1612. {
  1613. expr_ty p;
  1614. if (!func) {
  1615. PyErr_SetString(PyExc_ValueError,
  1616. "field func is required for Call");
  1617. return NULL;
  1618. }
  1619. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1620. if (!p)
  1621. return NULL;
  1622. p->kind = Call_kind;
  1623. p->v.Call.func = func;
  1624. p->v.Call.args = args;
  1625. p->v.Call.keywords = keywords;
  1626. p->v.Call.starargs = starargs;
  1627. p->v.Call.kwargs = kwargs;
  1628. p->lineno = lineno;
  1629. p->col_offset = col_offset;
  1630. return p;
  1631. }
  1632. expr_ty
  1633. Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
  1634. {
  1635. expr_ty p;
  1636. if (!value) {
  1637. PyErr_SetString(PyExc_ValueError,
  1638. "field value is required for Repr");
  1639. return NULL;
  1640. }
  1641. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1642. if (!p)
  1643. return NULL;
  1644. p->kind = Repr_kind;
  1645. p->v.Repr.value = value;
  1646. p->lineno = lineno;
  1647. p->col_offset = col_offset;
  1648. return p;
  1649. }
  1650. expr_ty
  1651. Num(object n, int lineno, int col_offset, PyArena *arena)
  1652. {
  1653. expr_ty p;
  1654. if (!n) {
  1655. PyErr_SetString(PyExc_ValueError,
  1656. "field n is required for Num");
  1657. return NULL;
  1658. }
  1659. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1660. if (!p)
  1661. return NULL;
  1662. p->kind = Num_kind;
  1663. p->v.Num.n = n;
  1664. p->lineno = lineno;
  1665. p->col_offset = col_offset;
  1666. return p;
  1667. }
  1668. expr_ty
  1669. Str(string s, int lineno, int col_offset, PyArena *arena)
  1670. {
  1671. expr_ty p;
  1672. if (!s) {
  1673. PyErr_SetString(PyExc_ValueError,
  1674. "field s is required for Str");
  1675. return NULL;
  1676. }
  1677. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1678. if (!p)
  1679. return NULL;
  1680. p->kind = Str_kind;
  1681. p->v.Str.s = s;
  1682. p->lineno = lineno;
  1683. p->col_offset = col_offset;
  1684. return p;
  1685. }
  1686. expr_ty
  1687. Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
  1688. col_offset, PyArena *arena)
  1689. {
  1690. expr_ty p;
  1691. if (!value) {
  1692. PyErr_SetString(PyExc_ValueError,
  1693. "field value is required for Attribute");
  1694. return NULL;
  1695. }
  1696. if (!attr) {
  1697. PyErr_SetString(PyExc_ValueError,
  1698. "field attr is required for Attribute");
  1699. return NULL;
  1700. }
  1701. if (!ctx) {
  1702. PyErr_SetString(PyExc_ValueError,
  1703. "field ctx is required for Attribute");
  1704. return NULL;
  1705. }
  1706. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1707. if (!p)
  1708. return NULL;
  1709. p->kind = Attribute_kind;
  1710. p->v.Attribute.value = value;
  1711. p->v.Attribute.attr = attr;
  1712. p->v.Attribute.ctx = ctx;
  1713. p->lineno = lineno;
  1714. p->col_offset = col_offset;
  1715. return p;
  1716. }
  1717. expr_ty
  1718. Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
  1719. col_offset, PyArena *arena)
  1720. {
  1721. expr_ty p;
  1722. if (!value) {
  1723. PyErr_SetString(PyExc_ValueError,
  1724. "field value is required for Subscript");
  1725. return NULL;
  1726. }
  1727. if (!slice) {
  1728. PyErr_SetString(PyExc_ValueError,
  1729. "field slice is required for Subscript");
  1730. return NULL;
  1731. }
  1732. if (!ctx) {
  1733. PyErr_SetString(PyExc_ValueError,
  1734. "field ctx is required for Subscript");
  1735. return NULL;
  1736. }
  1737. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1738. if (!p)
  1739. return NULL;
  1740. p->kind = Subscript_kind;
  1741. p->v.Subscript.value = value;
  1742. p->v.Subscript.slice = slice;
  1743. p->v.Subscript.ctx = ctx;
  1744. p->lineno = lineno;
  1745. p->col_offset = col_offset;
  1746. return p;
  1747. }
  1748. expr_ty
  1749. Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
  1750. *arena)
  1751. {
  1752. expr_ty p;
  1753. if (!id) {
  1754. PyErr_SetString(PyExc_ValueError,
  1755. "field id is required for Name");
  1756. return NULL;
  1757. }
  1758. if (!ctx) {
  1759. PyErr_SetString(PyExc_ValueError,
  1760. "field ctx is required for Name");
  1761. return NULL;
  1762. }
  1763. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1764. if (!p)
  1765. return NULL;
  1766. p->kind = Name_kind;
  1767. p->v.Name.id = id;
  1768. p->v.Name.ctx = ctx;
  1769. p->lineno = lineno;
  1770. p->col_offset = col_offset;
  1771. return p;
  1772. }
  1773. expr_ty
  1774. List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
  1775. *arena)
  1776. {
  1777. expr_ty p;
  1778. if (!ctx) {
  1779. PyErr_SetString(PyExc_ValueError,
  1780. "field ctx is required for List");
  1781. return NULL;
  1782. }
  1783. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1784. if (!p)
  1785. return NULL;
  1786. p->kind = List_kind;
  1787. p->v.List.elts = elts;
  1788. p->v.List.ctx = ctx;
  1789. p->lineno = lineno;
  1790. p->col_offset = col_offset;
  1791. return p;
  1792. }
  1793. expr_ty
  1794. Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
  1795. *arena)
  1796. {
  1797. expr_ty p;
  1798. if (!ctx) {
  1799. PyErr_SetString(PyExc_ValueError,
  1800. "field ctx is required for Tuple");
  1801. return NULL;
  1802. }
  1803. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1804. if (!p)
  1805. return NULL;
  1806. p->kind = Tuple_kind;
  1807. p->v.Tuple.elts = elts;
  1808. p->v.Tuple.ctx = ctx;
  1809. p->lineno = lineno;
  1810. p->col_offset = col_offset;
  1811. return p;
  1812. }
  1813. slice_ty
  1814. Ellipsis(PyArena *arena)
  1815. {
  1816. slice_ty p;
  1817. p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
  1818. if (!p)
  1819. return NULL;
  1820. p->kind = Ellipsis_kind;
  1821. return p;
  1822. }
  1823. slice_ty
  1824. Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
  1825. {
  1826. slice_ty p;
  1827. p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
  1828. if (!p)
  1829. return NULL;
  1830. p->kind = Slice_kind;
  1831. p->v.Slice.lower = lower;
  1832. p->v.Slice.upper = upper;
  1833. p->v.Slice.step = step;
  1834. return p;
  1835. }
  1836. slice_ty
  1837. ExtSlice(asdl_seq * dims, PyArena *arena)
  1838. {
  1839. slice_ty p;
  1840. p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
  1841. if (!p)
  1842. return NULL;
  1843. p->kind = ExtSlice_kind;
  1844. p->v.ExtSlice.dims = dims;
  1845. return p;
  1846. }
  1847. slice_ty
  1848. Index(expr_ty value, PyArena *arena)
  1849. {
  1850. slice_ty p;
  1851. if (!value) {
  1852. PyErr_SetString(PyExc_ValueError,
  1853. "field value is required for Index");
  1854. return NULL;
  1855. }
  1856. p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
  1857. if (!p)
  1858. return NULL;
  1859. p->kind = Index_kind;
  1860. p->v.Index.value = value;
  1861. return p;
  1862. }
  1863. comprehension_ty
  1864. comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
  1865. {
  1866. comprehension_ty p;
  1867. if (!target) {
  1868. PyErr_SetString(PyExc_ValueError,
  1869. "field target is required for comprehension");
  1870. return NULL;
  1871. }
  1872. if (!iter) {
  1873. PyErr_SetString(PyExc_ValueError,
  1874. "field iter is required for comprehension");
  1875. return NULL;
  1876. }
  1877. p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
  1878. if (!p)
  1879. return NULL;
  1880. p->target = target;
  1881. p->iter = iter;
  1882. p->ifs = ifs;
  1883. return p;
  1884. }
  1885. excepthandler_ty
  1886. ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
  1887. col_offset, PyArena *arena)
  1888. {
  1889. excepthandler_ty p;
  1890. p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
  1891. if (!p)
  1892. return NULL;
  1893. p->kind = ExceptHandler_kind;
  1894. p->v.ExceptHandler.type = type;
  1895. p->v.ExceptHandler.name = name;
  1896. p->v.ExceptHandler.body = body;
  1897. p->lineno = lineno;
  1898. p->col_offset = col_offset;
  1899. return p;
  1900. }
  1901. arguments_ty
  1902. arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
  1903. defaults, PyArena *arena)
  1904. {
  1905. arguments_ty p;
  1906. p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
  1907. if (!p)
  1908. return NULL;
  1909. p->args = args;
  1910. p->vararg = vararg;
  1911. p->kwarg = kwarg;
  1912. p->defaults = defaults;
  1913. return p;
  1914. }
  1915. keyword_ty
  1916. keyword(identifier arg, expr_ty value, PyArena *arena)
  1917. {
  1918. keyword_ty p;
  1919. if (!arg) {
  1920. PyErr_SetString(PyExc_ValueError,
  1921. "field arg is required for keyword");
  1922. return NULL;
  1923. }
  1924. if (!value) {
  1925. PyErr_SetString(PyExc_ValueError,
  1926. "field value is required for keyword");
  1927. return NULL;
  1928. }
  1929. p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
  1930. if (!p)
  1931. return NULL;
  1932. p->arg = arg;
  1933. p->value = value;
  1934. return p;
  1935. }
  1936. alias_ty
  1937. alias(identifier name, identifier asname, PyArena *arena)
  1938. {
  1939. alias_ty p;
  1940. if (!name) {
  1941. PyErr_SetString(PyExc_ValueError,
  1942. "field name is required for alias");
  1943. return NULL;
  1944. }
  1945. p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
  1946. if (!p)
  1947. return NULL;
  1948. p->name = name;
  1949. p->asname = asname;
  1950. return p;
  1951. }
  1952. PyObject*
  1953. ast2obj_mod(void* _o)
  1954. {
  1955. mod_ty o = (mod_ty)_o;
  1956. PyObject *result = NULL, *value = NULL;
  1957. if (!o) {
  1958. Py_INCREF(Py_None);
  1959. return Py_None;
  1960. }
  1961. switch (o->kind) {
  1962. case Module_kind:
  1963. result = PyType_GenericNew(Module_type, NULL, NULL);
  1964. if (!result) goto failed;
  1965. value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
  1966. if (!value) goto failed;
  1967. if (PyObject_SetAttrString(result, "body", value) == -1)
  1968. goto failed;
  1969. Py_DECREF(value);
  1970. break;
  1971. case Interactive_kind:
  1972. result = PyType_GenericNew(Interactive_type, NULL, NULL);
  1973. if (!result) goto failed;
  1974. value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
  1975. if (!value) goto failed;
  1976. if (PyObject_SetAttrString(result, "body", value) == -1)
  1977. goto failed;
  1978. Py_DECREF(value);
  1979. break;
  1980. case Expression_kind:
  1981. result = PyType_GenericNew(Expression_type, NULL, NULL);
  1982. if (!result) goto failed;
  1983. value = ast2obj_expr(o->v.Expression.body);
  1984. if (!value) goto failed;
  1985. if (PyObject_SetAttrString(result, "body", value) == -1)
  1986. goto failed;
  1987. Py_DECREF(value);
  1988. break;
  1989. case Suite_kind:
  1990. result = PyType_GenericNew(Suite_type, NULL, NULL);
  1991. if (!result) goto failed;
  1992. value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
  1993. if (!value) goto failed;
  1994. if (PyObject_SetAttrString(result, "body", value) == -1)
  1995. goto failed;
  1996. Py_DECREF(value);
  1997. break;
  1998. }
  1999. return result;
  2000. failed:
  2001. Py_XDECREF(value);
  2002. Py_XDECREF(result);
  2003. return NULL;
  2004. }
  2005. PyObject*
  2006. ast2obj_stmt(void* _o)
  2007. {
  2008. stmt_ty o = (stmt_ty)_o;
  2009. PyObject *result = NULL, *value = NULL;
  2010. if (!o) {
  2011. Py_INCREF(Py_None);
  2012. return Py_None;
  2013. }
  2014. switch (o->kind) {
  2015. case FunctionDef_kind:
  2016. result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
  2017. if (!result) goto failed;
  2018. value = ast2obj_identifier(o->v.FunctionDef.name);
  2019. if (!value) goto failed;
  2020. if (PyObject_SetAttrString(result, "name", value) == -1)
  2021. goto failed;
  2022. Py_DECREF(value);
  2023. value = ast2obj_arguments(o->v.FunctionDef.args);
  2024. if (!value) goto failed;
  2025. if (PyObject_SetAttrString(result, "args", value) == -1)
  2026. goto failed;
  2027. Py_DECREF(value);
  2028. value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
  2029. if (!value) goto failed;
  2030. if (PyObject_SetAttrString(result, "body", value) == -1)
  2031. goto failed;
  2032. Py_DECREF(value);
  2033. value = ast2obj_list(o->v.FunctionDef.decorator_list,
  2034. ast2obj_expr);
  2035. if (!value) goto failed;
  2036. if (PyObject_SetAttrString(result, "decorator_list", value) ==
  2037. -1)
  2038. goto failed;
  2039. Py_DECREF(value);
  2040. break;
  2041. case ClassDef_kind:
  2042. result = PyType_GenericNew(ClassDef_type, NULL, NULL);
  2043. if (!result) goto failed;
  2044. value = ast2obj_identifier(o->v.ClassDef.name);
  2045. if (!value) goto failed;
  2046. if (PyObject_SetAttrString(result, "name", value) == -1)
  2047. goto failed;
  2048. Py_DECREF(value);
  2049. value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
  2050. if (!value) goto failed;
  2051. if (PyObject_SetAttrString(result, "bases", value) == -1)
  2052. goto failed;
  2053. Py_DECREF(value);
  2054. value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
  2055. if (!value) goto failed;
  2056. if (PyObject_SetAttrString(result, "body", value) == -1)
  2057. goto failed;
  2058. Py_DECREF(value);
  2059. value = ast2obj_list(o->v.ClassDef.decorator_list,
  2060. ast2obj_expr);
  2061. if (!value) goto failed;
  2062. if (PyObject_SetAttrString(result, "decorator_list", value) ==
  2063. -1)
  2064. goto failed;
  2065. Py_DECREF(value);
  2066. break;
  2067. case Return_kind:
  2068. result = PyType_GenericNew(Return_type, NULL, NULL);
  2069. if (!result) goto failed;
  2070. value = ast2obj_expr(o->v.Return.value);
  2071. if (!value) goto failed;
  2072. if (PyObject_SetAttrString(result, "value", value) == -1)
  2073. goto failed;
  2074. Py_DECREF(value);
  2075. break;
  2076. case Delete_kind:
  2077. result = PyType_GenericNew(Delete_type, NULL, NULL);
  2078. if (!result) goto failed;
  2079. value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
  2080. if (!value) goto failed;
  2081. if (PyObject_SetAttrString(result, "targets", value) == -1)
  2082. goto failed;
  2083. Py_DECREF(value);
  2084. break;
  2085. case Assign_kind:
  2086. result = PyType_GenericNew(Assign_type, NULL, NULL);
  2087. if (!result) goto failed;
  2088. value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
  2089. if (!value) goto failed;
  2090. if (PyObject_SetAttrString(result, "targets", value) == -1)
  2091. goto failed;
  2092. Py_DECREF(value);
  2093. value = ast2obj_expr(o->v.Assign.value);
  2094. if (!value) goto failed;
  2095. if (PyObject_SetAttrString(result, "value", value) == -1)
  2096. goto failed;
  2097. Py_DECREF(value);
  2098. break;
  2099. case AugAssign_kind:
  2100. result = PyType_GenericNew(AugAssign_type, NULL, NULL);
  2101. if (!result) goto failed;
  2102. value = ast2obj_expr(o->v.AugAssign.target);
  2103. if (!value) goto failed;
  2104. if (PyObject_SetAttrString(result, "target", value) == -1)
  2105. goto failed;
  2106. Py_DECREF(value);
  2107. value = ast2obj_operator(o->v.AugAssign.op);
  2108. if (!value) goto failed;
  2109. if (PyObject_SetAttrString(result, "op", value) == -1)
  2110. goto failed;
  2111. Py_DECREF(value);
  2112. value = ast2obj_expr(o->v.AugAssign.value);
  2113. if (!value) goto failed;
  2114. if (PyObject_SetAttrString(result, "value", value) == -1)
  2115. goto failed;
  2116. Py_DECREF(value);
  2117. break;
  2118. case Print_kind:
  2119. result = PyType_GenericNew(Print_type, NULL, NULL);
  2120. if (!result) goto failed;
  2121. value = ast2obj_expr(o->v.Print.dest);
  2122. if (!value) goto failed;
  2123. if (PyObject_SetAttrString(result, "dest", value) == -1)
  2124. goto failed;
  2125. Py_DECREF(value);
  2126. value = ast2obj_list(o->v.Print.values, ast2obj_expr);
  2127. if (!value) goto failed;
  2128. if (PyObject_SetAttrString(result, "values", value) == -1)
  2129. goto failed;
  2130. Py_DECREF(value);
  2131. value = ast2obj_bool(o->v.Print.nl);
  2132. if (!value) goto failed;
  2133. if (PyObject_SetAttrString(result, "nl", value) == -1)
  2134. goto failed;
  2135. Py_DECREF(value);
  2136. break;
  2137. case For_kind:
  2138. result = PyType_GenericNew(For_type, NULL, NULL);
  2139. if (!result) goto failed;
  2140. value = ast2obj_expr(o->v.For.target);
  2141. if (!value) goto failed;
  2142. if (PyObject_SetAttrString(result, "target", value) == -1)
  2143. goto failed;
  2144. Py_DECREF(value);
  2145. value = ast2obj_expr(o->v.For.iter);
  2146. if (!value) goto failed;
  2147. if (PyObject_SetAttrString(result, "iter", value) == -1)
  2148. goto failed;
  2149. Py_DECREF(value);
  2150. value = ast2obj_list(o->v.For.body, ast2obj_stmt);
  2151. if (!value) goto failed;
  2152. if (PyObject_SetAttrString(result, "body", value) == -1)
  2153. goto failed;
  2154. Py_DECREF(value);
  2155. value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
  2156. if (!value) goto failed;
  2157. if (PyObject_SetAttrString(result, "orelse", value) == -1)
  2158. goto failed;
  2159. Py_DECREF(value);
  2160. break;
  2161. case While_kind:
  2162. result = PyType_GenericNew(While_type, NULL, NULL);
  2163. if (!result) goto failed;
  2164. value = ast2obj_expr(o->v.While.test);
  2165. if (!value) goto failed;
  2166. if (PyObject_SetAttrString(result, "test", value) == -1)
  2167. goto failed;
  2168. Py_DECREF(value);
  2169. value = ast2obj_list(o->v.While.body, ast2obj_stmt);
  2170. if (!value) goto failed;
  2171. if (PyObject_SetAttrString(result, "body", value) == -1)
  2172. goto failed;
  2173. Py_DECREF(value);
  2174. value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
  2175. if (!value) goto failed;
  2176. if (PyObject_SetAttrString(result, "orelse", value) == -1)
  2177. goto failed;
  2178. Py_DECREF(value);
  2179. break;
  2180. case If_kind:
  2181. result = PyType_GenericNew(If_type, NULL, NULL);
  2182. if (!result) goto failed;
  2183. value = ast2obj_expr(o->v.If.test);
  2184. if (!value) goto failed;
  2185. if (PyObject_SetAttrString(result, "test", value) == -1)
  2186. goto failed;
  2187. Py_DECREF(value);
  2188. value = ast2obj_list(o->v.If.body, ast2obj_stmt);
  2189. if (!value) goto failed;
  2190. if (PyObject_SetAttrString(result, "body", value) == -1)
  2191. goto failed;
  2192. Py_DECREF(value);
  2193. value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
  2194. if (!value) goto failed;
  2195. if (PyObject_SetAttrString(result, "orelse", value) == -1)
  2196. goto failed;
  2197. Py_DECREF(value);
  2198. break;
  2199. case With_kind:
  2200. result = PyType_GenericNew(With_type, NULL, NULL);
  2201. if (!result) goto failed;
  2202. value = ast2obj_expr(o->v.With.context_expr);
  2203. if (!value) goto failed;
  2204. if (PyObject_SetAttrString(result, "context_expr", value) == -1)
  2205. goto failed;
  2206. Py_DECREF(value);
  2207. value = ast2obj_expr(o->v.With.optional_vars);
  2208. if (!value) goto failed;
  2209. if (PyObject_SetAttrString(result, "optional_vars", value) ==
  2210. -1)
  2211. goto failed;
  2212. Py_DECREF(value);
  2213. value = ast2obj_list(o->v.With.body, ast2obj_stmt);
  2214. if (!value) goto failed;
  2215. if (PyObject_SetAttrString(result, "body", value) == -1)
  2216. goto failed;
  2217. Py_DECREF(value);
  2218. break;
  2219. case Raise_kind:
  2220. result = PyType_GenericNew(Raise_type, NULL, NULL);
  2221. if (!result) goto failed;
  2222. value = ast2obj_expr(o->v.Raise.type);
  2223. if (!value) goto failed;
  2224. if (PyObject_SetAttrString(result, "type", value) == -1)
  2225. goto failed;
  2226. Py_DECREF(value);
  2227. value = ast2obj_expr(o->v.Raise.inst);
  2228. if (!value) goto failed;
  2229. if (PyObject_SetAttrString(result, "inst", value) == -1)
  2230. goto failed;
  2231. Py_DECREF(value);
  2232. value = ast2obj_expr(o->v.Raise.tback);
  2233. if (!value) goto failed;
  2234. if (PyObject_SetAttrString(result, "tback", value) == -1)
  2235. goto failed;
  2236. Py_DECREF(value);
  2237. break;
  2238. case TryExcept_kind:
  2239. result = PyType_GenericNew(TryExcept_type, NULL, NULL);
  2240. if (!result) goto failed;
  2241. value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
  2242. if (!value) goto failed;
  2243. if (PyObject_SetAttrString(result, "body", value) == -1)
  2244. goto failed;
  2245. Py_DECREF(value);
  2246. value = ast2obj_list(o->v.TryExcept.handlers,
  2247. ast2obj_excepthandler);
  2248. if (!value) goto failed;
  2249. if (PyObject_SetAttrString(result, "handlers", value) == -1)
  2250. goto failed;
  2251. Py_DECREF(value);
  2252. value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
  2253. if (!value) goto failed;
  2254. if (PyObject_SetAttrString(result, "orelse", value) == -1)
  2255. goto failed;
  2256. Py_DECREF(value);
  2257. break;
  2258. case TryFinally_kind:
  2259. result = PyType_GenericNew(TryFinally_type, NULL, NULL);
  2260. if (!result) goto failed;
  2261. value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
  2262. if (!value) goto failed;
  2263. if (PyObject_SetAttrString(result, "body", value) == -1)
  2264. goto failed;
  2265. Py_DECREF(value);
  2266. value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
  2267. if (!value) goto failed;
  2268. if (PyObject_SetAttrString(result, "finalbody", value) == -1)
  2269. goto failed;
  2270. Py_DECREF(value);
  2271. break;
  2272. case Assert_kind:
  2273. result = PyType_GenericNew(Assert_type, NULL, NULL);
  2274. if (!result) goto failed;
  2275. value = ast2obj_expr(o->v.Assert.test);
  2276. if (!value) goto failed;
  2277. if (PyObject_SetAttrString(result, "test", value) == -1)
  2278. goto failed;
  2279. Py_DECREF(value);
  2280. value = ast2obj_expr(o->v.Assert.msg);
  2281. if (!value) goto failed;
  2282. if (PyObject_SetAttrString(result, "msg", value) == -1)
  2283. goto failed;
  2284. Py_DECREF(value);
  2285. break;
  2286. case Import_kind:
  2287. result = PyType_GenericNew(Import_type, NULL, NULL);
  2288. if (!result) goto failed;
  2289. value = ast2obj_list(o->v.Import.names, ast2obj_alias);
  2290. if (!value) goto failed;
  2291. if (PyObject_SetAttrString(result, "names", value) == -1)
  2292. goto failed;
  2293. Py_DECREF(value);
  2294. break;
  2295. case ImportFrom_kind:
  2296. result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
  2297. if (!result) goto failed;
  2298. value = ast2obj_identifier(o->v.ImportFrom.module);
  2299. if (!value) goto failed;
  2300. if (PyObject_SetAttrString(result, "module", value) == -1)
  2301. goto failed;
  2302. Py_DECREF(value);
  2303. value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
  2304. if (!value) goto failed;
  2305. if (PyObject_SetAttrString(result, "names", value) == -1)
  2306. goto failed;
  2307. Py_DECREF(value);
  2308. value = ast2obj_int(o->v.ImportFrom.level);
  2309. if (!value) goto failed;
  2310. if (PyObject_SetAttrString(result, "level", value) == -1)
  2311. goto failed;
  2312. Py_DECREF(value);
  2313. break;
  2314. case Exec_kind:
  2315. result = PyType_GenericNew(Exec_type, NULL, NULL);
  2316. if (!result) goto failed;
  2317. value = ast2obj_expr(o->v.Exec.body);
  2318. if (!value) goto failed;
  2319. if (PyObject_SetAttrString(result, "body", value) == -1)
  2320. goto failed;
  2321. Py_DECREF(value);
  2322. value = ast2obj_expr(o->v.Exec.globals);
  2323. if (!value) goto failed;
  2324. if (PyObject_SetAttrString(result, "globals", value) == -1)
  2325. goto failed;
  2326. Py_DECREF(value);
  2327. value = ast2obj_expr(o->v.Exec.locals);
  2328. if (!value) goto failed;
  2329. if (PyObject_SetAttrString(result, "locals", value) == -1)
  2330. goto failed;
  2331. Py_DECREF(value);
  2332. break;
  2333. case Global_kind:
  2334. result = PyType_GenericNew(Global_type, NULL, NULL);
  2335. if (!result) goto failed;
  2336. value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
  2337. if (!value) goto failed;
  2338. if (PyObject_SetAttrString(result, "names", value) == -1)
  2339. goto failed;
  2340. Py_DECREF(value);
  2341. break;
  2342. case Expr_kind:
  2343. result = PyType_GenericNew(Expr_type, NULL, NULL);
  2344. if (!result) goto failed;
  2345. value = ast2obj_expr(o->v.Expr.value);
  2346. if (!value) goto failed;
  2347. if (PyObject_SetAttrString(result, "value", value) == -1)
  2348. goto failed;
  2349. Py_DECREF(value);
  2350. break;
  2351. case Pass_kind:
  2352. result = PyType_GenericNew(Pass_type, NULL, NULL);
  2353. if (!result) goto failed;
  2354. break;
  2355. case Break_kind:
  2356. result = PyType_GenericNew(Break_type, NULL, NULL);
  2357. if (!result) goto failed;
  2358. break;
  2359. case Continue_kind:
  2360. result = PyType_GenericNew(Continue_type, NULL, NULL);
  2361. if (!result) goto failed;
  2362. break;
  2363. }
  2364. value = ast2obj_int(o->lineno);
  2365. if (!value) goto failed;
  2366. if (PyObject_SetAttrString(result, "lineno", value) < 0)
  2367. goto failed;
  2368. Py_DECREF(value);
  2369. value = ast2obj_int(o->col_offset);
  2370. if (!value) goto failed;
  2371. if (PyObject_SetAttrString(result, "col_offset", value) < 0)
  2372. goto failed;
  2373. Py_DECREF(value);
  2374. return result;
  2375. failed:
  2376. Py_XDECREF(value);
  2377. Py_XDECREF(result);
  2378. return NULL;
  2379. }
  2380. PyObject*
  2381. ast2obj_expr(void* _o)
  2382. {
  2383. expr_ty o = (expr_ty)_o;
  2384. PyObject *result = NULL, *value = NULL;
  2385. if (!o) {
  2386. Py_INCREF(Py_None);
  2387. return Py_None;
  2388. }
  2389. switch (o->kind) {
  2390. case BoolOp_kind:
  2391. result = PyType_GenericNew(BoolOp_type, NULL, NULL);
  2392. if (!result) goto failed;
  2393. value = ast2obj_boolop(o->v.BoolOp.op);
  2394. if (!value) goto failed;
  2395. if (PyObject_SetAttrString(result, "op", value) == -1)
  2396. goto failed;
  2397. Py_DECREF(value);
  2398. value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
  2399. if (!value) goto failed;
  2400. if (PyObject_SetAttrString(result, "values", value) == -1)
  2401. goto failed;
  2402. Py_DECREF(value);
  2403. break;
  2404. case BinOp_kind:
  2405. result = PyType_GenericNew(BinOp_type, NULL, NULL);
  2406. if (!result) goto failed;
  2407. value = ast2obj_expr(o->v.BinOp.left);
  2408. if (!value) goto failed;
  2409. if (PyObject_SetAttrString(result, "left", value) == -1)
  2410. goto failed;
  2411. Py_DECREF(value);
  2412. value = ast2obj_operator(o->v.BinOp.op);
  2413. if (!value) goto failed;
  2414. if (PyObject_SetAttrString(result, "op", value) == -1)
  2415. goto failed;
  2416. Py_DECREF(value);
  2417. value = ast2obj_expr(o->v.BinOp.right);
  2418. if (!value) goto failed;
  2419. if (PyObject_SetAttrString(result, "right", value) == -1)
  2420. goto failed;
  2421. Py_DECREF(value);
  2422. break;
  2423. case UnaryOp_kind:
  2424. result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
  2425. if (!result) goto failed;
  2426. value = ast2obj_unaryop(o->v.UnaryOp.op);
  2427. if (!value) goto failed;
  2428. if (PyObject_SetAttrString(result, "op", value) == -1)
  2429. goto failed;
  2430. Py_DECREF(value);
  2431. value = ast2obj_expr(o->v.UnaryOp.operand);
  2432. if (!value) goto failed;
  2433. if (PyObject_SetAttrString(result, "operand", value) == -1)
  2434. goto failed;
  2435. Py_DECREF(value);
  2436. break;
  2437. case Lambda_kind:
  2438. result = PyType_GenericNew(Lambda_type, NULL, NULL);
  2439. if (!result) goto failed;
  2440. value = ast2obj_arguments(o->v.Lambda.args);
  2441. if (!value) goto failed;
  2442. if (PyObject_SetAttrString(result, "args", value) == -1)
  2443. goto failed;
  2444. Py_DECREF(value);
  2445. value = ast2obj_expr(o->v.Lambda.body);
  2446. if (!value) goto failed;
  2447. if (PyObject_SetAttrString(result, "body", value) == -1)
  2448. goto failed;
  2449. Py_DECREF(value);
  2450. break;
  2451. case IfExp_kind:
  2452. result = PyType_GenericNew(IfExp_type, NULL, NULL);
  2453. if (!result) goto failed;
  2454. value = ast2obj_expr(o->v.IfExp.test);
  2455. if (!value) goto failed;
  2456. if (PyObject_SetAttrString(result, "test", value) == -1)
  2457. goto failed;
  2458. Py_DECREF(value);
  2459. value = ast2obj_expr(o->v.IfExp.body);
  2460. if (!value) goto failed;
  2461. if (PyObject_SetAttrString(result, "body", value) == -1)
  2462. goto failed;
  2463. Py_DECREF(value);
  2464. value = ast2obj_expr(o->v.IfExp.orelse);
  2465. if (!value) goto failed;
  2466. if (PyObject_SetAttrString(result, "orelse", value) == -1)
  2467. goto failed;
  2468. Py_DECREF(value);
  2469. break;
  2470. case Dict_kind:
  2471. result = PyType_GenericNew(Dict_type, NULL, NULL);
  2472. if (!result) goto failed;
  2473. value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
  2474. if (!value) goto failed;
  2475. if (PyObject_SetAttrString(result, "keys", value) == -1)
  2476. goto failed;
  2477. Py_DECREF(value);
  2478. value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
  2479. if (!value) goto failed;
  2480. if (PyObject_SetAttrString(result, "values", value) == -1)
  2481. goto failed;
  2482. Py_DECREF(value);
  2483. break;
  2484. case ListComp_kind:
  2485. result = PyType_GenericNew(ListComp_type, NULL, NULL);
  2486. if (!result) goto failed;
  2487. value = ast2obj_expr(o->v.ListComp.elt);
  2488. if (!value) goto failed;
  2489. if (PyObject_SetAttrString(result, "elt", value) == -1)
  2490. goto failed;
  2491. Py_DECREF(value);
  2492. value = ast2obj_list(o->v.ListComp.generators,
  2493. ast2obj_comprehension);
  2494. if (!value) goto failed;
  2495. if (PyObject_SetAttrString(result, "generators", value) == -1)
  2496. goto failed;
  2497. Py_DECREF(value);
  2498. break;
  2499. case GeneratorExp_kind:
  2500. result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
  2501. if (!result) goto failed;
  2502. value = ast2obj_expr(o->v.GeneratorExp.elt);
  2503. if (!value) goto failed;
  2504. if (PyObject_SetAttrString(result, "elt", value) == -1)
  2505. goto failed;
  2506. Py_DECREF(value);
  2507. value = ast2obj_list(o->v.GeneratorExp.generators,
  2508. ast2obj_comprehension);
  2509. if (!value) goto failed;
  2510. if (PyObject_SetAttrString(result, "generators", value) == -1)
  2511. goto failed;
  2512. Py_DECREF(value);
  2513. break;
  2514. case Yield_kind:
  2515. result = PyType_GenericNew(Yield_type, NULL, NULL);
  2516. if (!result) goto failed;
  2517. value = ast2obj_expr(o->v.Yield.value);
  2518. if (!value) goto failed;
  2519. if (PyObject_SetAttrString(result, "value", value) == -1)
  2520. goto failed;
  2521. Py_DECREF(value);
  2522. break;
  2523. case Compare_kind:
  2524. result = PyType_GenericNew(Compare_type, NULL, NULL);
  2525. if (!result) goto failed;
  2526. value = ast2obj_expr(o->v.Compare.left);
  2527. if (!value) goto failed;
  2528. if (PyObject_SetAttrString(result, "left", value) == -1)
  2529. goto failed;
  2530. Py_DECREF(value);
  2531. {
  2532. int i, n = asdl_seq_LEN(o->v.Compare.ops);
  2533. value = PyList_New(n);
  2534. if (!value) goto failed;
  2535. for(i = 0; i < n; i++)
  2536. PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
  2537. }
  2538. if (!value) goto failed;
  2539. if (PyObject_SetAttrString(result, "ops", value) == -1)
  2540. goto failed;
  2541. Py_DECREF(value);
  2542. value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
  2543. if (!value) goto failed;
  2544. if (PyObject_SetAttrString(result, "comparators", value) == -1)
  2545. goto failed;
  2546. Py_DECREF(value);
  2547. break;
  2548. case Call_kind:
  2549. result = PyType_GenericNew(Call_type, NULL, NULL);
  2550. if (!result) goto failed;
  2551. value = ast2obj_expr(o->v.Call.func);
  2552. if (!value) goto failed;
  2553. if (PyObject_SetAttrString(result, "func", value) == -1)
  2554. goto failed;
  2555. Py_DECREF(value);
  2556. value = ast2obj_list(o->v.Call.args, ast2obj_expr);
  2557. if (!value) goto failed;
  2558. if (PyObject_SetAttrString(result, "args", value) == -1)
  2559. goto failed;
  2560. Py_DECREF(value);
  2561. value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
  2562. if (!value) goto failed;
  2563. if (PyObject_SetAttrString(result, "keywords", value) == -1)
  2564. goto failed;
  2565. Py_DECREF(value);
  2566. value = ast2obj_expr(o->v.Call.starargs);
  2567. if (!value) goto failed;
  2568. if (PyObject_SetAttrString(result, "starargs", value) == -1)
  2569. goto failed;
  2570. Py_DECREF(value);
  2571. value = ast2obj_expr(o->v.Call.kwargs);
  2572. if (!value) goto failed;
  2573. if (PyObject_SetAttrString(result, "kwargs", value) == -1)
  2574. goto failed;
  2575. Py_DECREF(value);
  2576. break;
  2577. case Repr_kind:
  2578. result = PyType_GenericNew(Repr_type, NULL, NULL);
  2579. if (!result) goto failed;
  2580. value = ast2obj_expr(o->v.Repr.value);
  2581. if (!value) goto failed;
  2582. if (PyObject_SetAttrString(result, "value", value) == -1)
  2583. goto failed;
  2584. Py_DECREF(value);
  2585. break;
  2586. case Num_kind:
  2587. result = PyType_GenericNew(Num_type, NULL, NULL);
  2588. if (!result) goto failed;
  2589. value = ast2obj_object(o->v.Num.n);
  2590. if (!value) goto failed;
  2591. if (PyObject_SetAttrString(result, "n", value) == -1)
  2592. goto failed;
  2593. Py_DECREF(value);
  2594. break;
  2595. case Str_kind:
  2596. result = PyType_GenericNew(Str_type, NULL, NULL);
  2597. if (!result) goto failed;
  2598. value = ast2obj_string(o->v.Str.s);
  2599. if (!value) goto failed;
  2600. if (PyObject_SetAttrString(result, "s", value) == -1)
  2601. goto failed;
  2602. Py_DECREF(value);
  2603. break;
  2604. case Attribute_kind:
  2605. result = PyType_GenericNew(Attribute_type, NULL, NULL);
  2606. if (!result) goto failed;
  2607. value = ast2obj_expr(o->v.Attribute.value);
  2608. if (!value) goto failed;
  2609. if (PyObject_SetAttrString(result, "value", value) == -1)
  2610. goto failed;
  2611. Py_DECREF(value);
  2612. value = ast2obj_identifier(o->v.Attribute.attr);
  2613. if (!value) goto failed;
  2614. if (PyObject_SetAttrString(result, "attr", value) == -1)
  2615. goto failed;
  2616. Py_DECREF(value);
  2617. value = ast2obj_expr_context(o->v.Attribute.ctx);
  2618. if (!value) goto failed;
  2619. if (PyObject_SetAttrString(result, "ctx", value) == -1)
  2620. goto failed;
  2621. Py_DECREF(value);
  2622. break;
  2623. case Subscript_kind:
  2624. result = PyType_GenericNew(Subscript_type, NULL, NULL);
  2625. if (!result) goto failed;
  2626. value = ast2obj_expr(o->v.Subscript.value);
  2627. if (!value) goto failed;
  2628. if (PyObject_SetAttrString(result, "value", value) == -1)
  2629. goto failed;
  2630. Py_DECREF(value);
  2631. value = ast2obj_slice(o->v.Subscript.slice);
  2632. if (!value) goto failed;
  2633. if (PyObject_SetAttrString(result, "slice", value) == -1)
  2634. goto failed;
  2635. Py_DECREF(value);
  2636. value = ast2obj_expr_context(o->v.Subscript.ctx);
  2637. if (!value) goto failed;
  2638. if (PyObject_SetAttrString(result, "ctx", value) == -1)
  2639. goto failed;
  2640. Py_DECREF(value);
  2641. break;
  2642. case Name_kind:
  2643. result = PyType_GenericNew(Name_type, NULL, NULL);
  2644. if (!result) goto failed;
  2645. value = ast2obj_identifier(o->v.Name.id);
  2646. if (!value) goto failed;
  2647. if (PyObject_SetAttrString(result, "id", value) == -1)
  2648. goto failed;
  2649. Py_DECREF(value);
  2650. value = ast2obj_expr_context(o->v.Name.ctx);
  2651. if (!value) goto failed;
  2652. if (PyObject_SetAttrString(result, "ctx", value) == -1)
  2653. goto failed;
  2654. Py_DECREF(value);
  2655. break;
  2656. case List_kind:
  2657. result = PyType_GenericNew(List_type, NULL, NULL);
  2658. if (!result) goto failed;
  2659. value = ast2obj_list(o->v.List.elts, ast2obj_expr);
  2660. if (!value) goto failed;
  2661. if (PyObject_SetAttrString(result, "elts", value) == -1)
  2662. goto failed;
  2663. Py_DECREF(value);
  2664. value = ast2obj_expr_context(o->v.List.ctx);
  2665. if (!value) goto failed;
  2666. if (PyObject_SetAttrString(result, "ctx", value) == -1)
  2667. goto failed;
  2668. Py_DECREF(value);
  2669. break;
  2670. case Tuple_kind:
  2671. result = PyType_GenericNew(Tuple_type, NULL, NULL);
  2672. if (!result) goto failed;
  2673. value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
  2674. if (!value) goto failed;
  2675. if (PyObject_SetAttrString(result, "elts", value) == -1)
  2676. goto failed;
  2677. Py_DECREF(value);
  2678. value = ast2obj_expr_context(o->v.Tuple.ctx);
  2679. if (!value) goto failed;
  2680. if (PyObject_SetAttrString(result, "ctx", value) == -1)
  2681. goto failed;
  2682. Py_DECREF(value);
  2683. break;
  2684. }
  2685. value = ast2obj_int(o->lineno);
  2686. if (!value) goto failed;
  2687. if (PyObject_SetAttrString(result, "lineno", value) < 0)
  2688. goto failed;
  2689. Py_DECREF(value);
  2690. value = ast2obj_int(o->col_offset);
  2691. if (!value) goto failed;
  2692. if (PyObject_SetAttrString(result, "col_offset", value) < 0)
  2693. goto failed;
  2694. Py_DECREF(value);
  2695. return result;
  2696. failed:
  2697. Py_XDECREF(value);
  2698. Py_XDECREF(result);
  2699. return NULL;
  2700. }
  2701. PyObject* ast2obj_expr_context(expr_context_ty o)
  2702. {
  2703. switch(o) {
  2704. case Load:
  2705. Py_INCREF(Load_singleton);
  2706. return Load_singleton;
  2707. case Store:
  2708. Py_INCREF(Store_singleton);
  2709. return Store_singleton;
  2710. case Del:
  2711. Py_INCREF(Del_singleton);
  2712. return Del_singleton;
  2713. case AugLoad:
  2714. Py_INCREF(AugLoad_singleton);
  2715. return AugLoad_singleton;
  2716. case AugStore:
  2717. Py_INCREF(AugStore_singleton);
  2718. return AugStore_singleton;
  2719. case Param:
  2720. Py_INCREF(Param_singleton);
  2721. return Param_singleton;
  2722. default:
  2723. /* should never happen, but just in case ... */
  2724. PyErr_Format(PyExc_SystemError, "unknown expr_context found");
  2725. return NULL;
  2726. }
  2727. }
  2728. PyObject*
  2729. ast2obj_slice(void* _o)
  2730. {
  2731. slice_ty o = (slice_ty)_o;
  2732. PyObject *result = NULL, *value = NULL;
  2733. if (!o) {
  2734. Py_INCREF(Py_None);
  2735. return Py_None;
  2736. }
  2737. switch (o->kind) {
  2738. case Ellipsis_kind:
  2739. result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
  2740. if (!result) goto failed;
  2741. break;
  2742. case Slice_kind:
  2743. result = PyType_GenericNew(Slice_type, NULL, NULL);
  2744. if (!result) goto failed;
  2745. value = ast2obj_expr(o->v.Slice.lower);
  2746. if (!value) goto failed;
  2747. if (PyObject_SetAttrString(result, "lower", value) == -1)
  2748. goto failed;
  2749. Py_DECREF(value);
  2750. value = ast2obj_expr(o->v.Slice.upper);
  2751. if (!value) goto failed;
  2752. if (PyObject_SetAttrString(result, "upper", value) == -1)
  2753. goto failed;
  2754. Py_DECREF(value);
  2755. value = ast2obj_expr(o->v.Slice.step);
  2756. if (!value) goto failed;
  2757. if (PyObject_SetAttrString(result, "step", value) == -1)
  2758. goto failed;
  2759. Py_DECREF(value);
  2760. break;
  2761. case ExtSlice_kind:
  2762. result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
  2763. if (!result) goto failed;
  2764. value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
  2765. if (!value) goto failed;
  2766. if (PyObject_SetAttrString(result, "dims", value) == -1)
  2767. goto failed;
  2768. Py_DECREF(value);
  2769. break;
  2770. case Index_kind:
  2771. result = PyType_GenericNew(Index_type, NULL, NULL);
  2772. if (!result) goto failed;
  2773. value = ast2obj_expr(o->v.Index.value);
  2774. if (!value) goto failed;
  2775. if (PyObject_SetAttrString(result, "value", value) == -1)
  2776. goto failed;
  2777. Py_DECREF(value);
  2778. break;
  2779. }
  2780. return result;
  2781. failed:
  2782. Py_XDECREF(value);
  2783. Py_XDECREF(result);
  2784. return NULL;
  2785. }
  2786. PyObject* ast2obj_boolop(boolop_ty o)
  2787. {
  2788. switch(o) {
  2789. case And:
  2790. Py_INCREF(And_singleton);
  2791. return And_singleton;
  2792. case Or:
  2793. Py_INCREF(Or_singleton);
  2794. return Or_singleton;
  2795. default:
  2796. /* should never happen, but just in case ... */
  2797. PyErr_Format(PyExc_SystemError, "unknown boolop found");
  2798. return NULL;
  2799. }
  2800. }
  2801. PyObject* ast2obj_operator(operator_ty o)
  2802. {
  2803. switch(o) {
  2804. case Add:
  2805. Py_INCREF(Add_singleton);
  2806. return Add_singleton;
  2807. case Sub:
  2808. Py_INCREF(Sub_singleton);
  2809. return Sub_singleton;
  2810. case Mult:
  2811. Py_INCREF(Mult_singleton);
  2812. return Mult_singleton;
  2813. case Div:
  2814. Py_INCREF(Div_singleton);
  2815. return Div_singleton;
  2816. case Mod:
  2817. Py_INCREF(Mod_singleton);
  2818. return Mod_singleton;
  2819. case Pow:
  2820. Py_INCREF(Pow_singleton);
  2821. return Pow_singleton;
  2822. case LShift:
  2823. Py_INCREF(LShift_singleton);
  2824. return LShift_singleton;
  2825. case RShift:
  2826. Py_INCREF(RShift_singleton);
  2827. return RShift_singleton;
  2828. case BitOr:
  2829. Py_INCREF(BitOr_singleton);
  2830. return BitOr_singleton;
  2831. case BitXor:
  2832. Py_INCREF(BitXor_singleton);
  2833. return BitXor_singleton;
  2834. case BitAnd:
  2835. Py_INCREF(BitAnd_singleton);
  2836. return BitAnd_singleton;
  2837. case FloorDiv:
  2838. Py_INCREF(FloorDiv_singleton);
  2839. return FloorDiv_singleton;
  2840. default:
  2841. /* should never happen, but just in case ... */
  2842. PyErr_Format(PyExc_SystemError, "unknown operator found");
  2843. return NULL;
  2844. }
  2845. }
  2846. PyObject* ast2obj_unaryop(unaryop_ty o)
  2847. {
  2848. switch(o) {
  2849. case Invert:
  2850. Py_INCREF(Invert_singleton);
  2851. return Invert_singleton;
  2852. case Not:
  2853. Py_INCREF(Not_singleton);
  2854. return Not_singleton;
  2855. case UAdd:
  2856. Py_INCREF(UAdd_singleton);
  2857. return UAdd_singleton;
  2858. case USub:
  2859. Py_INCREF(USub_singleton);
  2860. return USub_singleton;
  2861. default:
  2862. /* should never happen, but just in case ... */
  2863. PyErr_Format(PyExc_SystemError, "unknown unaryop found");
  2864. return NULL;
  2865. }
  2866. }
  2867. PyObject* ast2obj_cmpop(cmpop_ty o)
  2868. {
  2869. switch(o) {
  2870. case Eq:
  2871. Py_INCREF(Eq_singleton);
  2872. return Eq_singleton;
  2873. case NotEq:
  2874. Py_INCREF(NotEq_singleton);
  2875. return NotEq_singleton;
  2876. case Lt:
  2877. Py_INCREF(Lt_singleton);
  2878. return Lt_singleton;
  2879. case LtE:
  2880. Py_INCREF(LtE_singleton);
  2881. return LtE_singleton;
  2882. case Gt:
  2883. Py_INCREF(Gt_singleton);
  2884. return Gt_singleton;
  2885. case GtE:
  2886. Py_INCREF(GtE_singleton);
  2887. return GtE_singleton;
  2888. case Is:
  2889. Py_INCREF(Is_singleton);
  2890. return Is_singleton;
  2891. case IsNot:
  2892. Py_INCREF(IsNot_singleton);
  2893. return IsNot_singleton;
  2894. case In:
  2895. Py_INCREF(In_singleton);
  2896. return In_singleton;
  2897. case NotIn:
  2898. Py_INCREF(NotIn_singleton);
  2899. return NotIn_singleton;
  2900. default:
  2901. /* should never happen, but just in case ... */
  2902. PyErr_Format(PyExc_SystemError, "unknown cmpop found");
  2903. return NULL;
  2904. }
  2905. }
  2906. PyObject*
  2907. ast2obj_comprehension(void* _o)
  2908. {
  2909. comprehension_ty o = (comprehension_ty)_o;
  2910. PyObject *result = NULL, *value = NULL;
  2911. if (!o) {
  2912. Py_INCREF(Py_None);
  2913. return Py_None;
  2914. }
  2915. result = PyType_GenericNew(comprehension_type, NULL, NULL);
  2916. if (!result) return NULL;
  2917. value = ast2obj_expr(o->target);
  2918. if (!value) goto failed;
  2919. if (PyObject_SetAttrString(result, "target", value) == -1)
  2920. goto failed;
  2921. Py_DECREF(value);
  2922. value = ast2obj_expr(o->iter);
  2923. if (!value) goto failed;
  2924. if (PyObject_SetAttrString(result, "iter", value) == -1)
  2925. goto failed;
  2926. Py_DECREF(value);
  2927. value = ast2obj_list(o->ifs, ast2obj_expr);
  2928. if (!value) goto failed;
  2929. if (PyObject_SetAttrString(result, "ifs", value) == -1)
  2930. goto failed;
  2931. Py_DECREF(value);
  2932. return result;
  2933. failed:
  2934. Py_XDECREF(value);
  2935. Py_XDECREF(result);
  2936. return NULL;
  2937. }
  2938. PyObject*
  2939. ast2obj_excepthandler(void* _o)
  2940. {
  2941. excepthandler_ty o = (excepthandler_ty)_o;
  2942. PyObject *result = NULL, *value = NULL;
  2943. if (!o) {
  2944. Py_INCREF(Py_None);
  2945. return Py_None;
  2946. }
  2947. switch (o->kind) {
  2948. case ExceptHandler_kind:
  2949. result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
  2950. if (!result) goto failed;
  2951. value = ast2obj_expr(o->v.ExceptHandler.type);
  2952. if (!value) goto failed;
  2953. if (PyObject_SetAttrString(result, "type", value) == -1)
  2954. goto failed;
  2955. Py_DECREF(value);
  2956. value = ast2obj_expr(o->v.ExceptHandler.name);
  2957. if (!value) goto failed;
  2958. if (PyObject_SetAttrString(result, "name", value) == -1)
  2959. goto failed;
  2960. Py_DECREF(value);
  2961. value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
  2962. if (!value) goto failed;
  2963. if (PyObject_SetAttrString(result, "body", value) == -1)
  2964. goto failed;
  2965. Py_DECREF(value);
  2966. break;
  2967. }
  2968. value = ast2obj_int(o->lineno);
  2969. if (!value) goto failed;
  2970. if (PyObject_SetAttrString(result, "lineno", value) < 0)
  2971. goto failed;
  2972. Py_DECREF(value);
  2973. value = ast2obj_int(o->col_offset);
  2974. if (!value) goto failed;
  2975. if (PyObject_SetAttrString(result, "col_offset", value) < 0)
  2976. goto failed;
  2977. Py_DECREF(value);
  2978. return result;
  2979. failed:
  2980. Py_XDECREF(value);
  2981. Py_XDECREF(result);
  2982. return NULL;
  2983. }
  2984. PyObject*
  2985. ast2obj_arguments(void* _o)
  2986. {
  2987. arguments_ty o = (arguments_ty)_o;
  2988. PyObject *result = NULL, *value = NULL;
  2989. if (!o) {
  2990. Py_INCREF(Py_None);
  2991. return Py_None;
  2992. }
  2993. result = PyType_GenericNew(arguments_type, NULL, NULL);
  2994. if (!result) return NULL;
  2995. value = ast2obj_list(o->args, ast2obj_expr);
  2996. if (!value) goto failed;
  2997. if (PyObject_SetAttrString(result, "args", value) == -1)
  2998. goto failed;
  2999. Py_DECREF(value);
  3000. value = ast2obj_identifier(o->vararg);
  3001. if (!value) goto failed;
  3002. if (PyObject_SetAttrString(result, "vararg", value) == -1)
  3003. goto failed;
  3004. Py_DECREF(value);
  3005. value = ast2obj_identifier(o->kwarg);
  3006. if (!value) goto failed;
  3007. if (PyObject_SetAttrString(result, "kwarg", value) == -1)
  3008. goto failed;
  3009. Py_DECREF(value);
  3010. value = ast2obj_list(o->defaults, ast2obj_expr);
  3011. if (!value) goto failed;
  3012. if (PyObject_SetAttrString(result, "defaults", value) == -1)
  3013. goto failed;
  3014. Py_DECREF(value);
  3015. return result;
  3016. failed:
  3017. Py_XDECREF(value);
  3018. Py_XDECREF(result);
  3019. return NULL;
  3020. }
  3021. PyObject*
  3022. ast2obj_keyword(void* _o)
  3023. {
  3024. keyword_ty o = (keyword_ty)_o;
  3025. PyObject *result = NULL, *value = NULL;
  3026. if (!o) {
  3027. Py_INCREF(Py_None);
  3028. return Py_None;
  3029. }
  3030. result = PyType_GenericNew(keyword_type, NULL, NULL);
  3031. if (!result) return NULL;
  3032. value = ast2obj_identifier(o->arg);
  3033. if (!value) goto failed;
  3034. if (PyObject_SetAttrString(result, "arg", value) == -1)
  3035. goto failed;
  3036. Py_DECREF(value);
  3037. value = ast2obj_expr(o->value);
  3038. if (!value) goto failed;
  3039. if (PyObject_SetAttrString(result, "value", value) == -1)
  3040. goto failed;
  3041. Py_DECREF(value);
  3042. return result;
  3043. failed:
  3044. Py_XDECREF(value);
  3045. Py_XDECREF(result);
  3046. return NULL;
  3047. }
  3048. PyObject*
  3049. ast2obj_alias(void* _o)
  3050. {
  3051. alias_ty o = (alias_ty)_o;
  3052. PyObject *result = NULL, *value = NULL;
  3053. if (!o) {
  3054. Py_INCREF(Py_None);
  3055. return Py_None;
  3056. }
  3057. result = PyType_GenericNew(alias_type, NULL, NULL);
  3058. if (!result) return NULL;
  3059. value = ast2obj_identifier(o->name);
  3060. if (!value) goto failed;
  3061. if (PyObject_SetAttrString(result, "name", value) == -1)
  3062. goto failed;
  3063. Py_DECREF(value);
  3064. value = ast2obj_identifier(o->asname);
  3065. if (!value) goto failed;
  3066. if (PyObject_SetAttrString(result, "asname", value) == -1)
  3067. goto failed;
  3068. Py_DECREF(value);
  3069. return result;
  3070. failed:
  3071. Py_XDECREF(value);
  3072. Py_XDECREF(result);
  3073. return NULL;
  3074. }
  3075. int
  3076. obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
  3077. {
  3078. PyObject* tmp = NULL;
  3079. if (obj == Py_None) {
  3080. *out = NULL;
  3081. return 0;
  3082. }
  3083. if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
  3084. asdl_seq* body;
  3085. if (PyObject_HasAttrString(obj, "body")) {
  3086. int res;
  3087. Py_ssize_t len;
  3088. Py_ssize_t i;
  3089. tmp = PyObject_GetAttrString(obj, "body");
  3090. if (tmp == NULL) goto failed;
  3091. if (!PyList_Check(tmp)) {
  3092. PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3093. goto failed;
  3094. }
  3095. len = PyList_GET_SIZE(tmp);
  3096. body = asdl_seq_new(len, arena);
  3097. if (body == NULL) goto failed;
  3098. for (i = 0; i < len; i++) {
  3099. stmt_ty value;
  3100. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3101. if (res != 0) goto failed;
  3102. asdl_seq_SET(body, i, value);
  3103. }
  3104. Py_XDECREF(tmp);
  3105. tmp = NULL;
  3106. } else {
  3107. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
  3108. return 1;
  3109. }
  3110. *out = Module(body, arena);
  3111. if (*out == NULL) goto failed;
  3112. return 0;
  3113. }
  3114. if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
  3115. asdl_seq* body;
  3116. if (PyObject_HasAttrString(obj, "body")) {
  3117. int res;
  3118. Py_ssize_t len;
  3119. Py_ssize_t i;
  3120. tmp = PyObject_GetAttrString(obj, "body");
  3121. if (tmp == NULL) goto failed;
  3122. if (!PyList_Check(tmp)) {
  3123. PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3124. goto failed;
  3125. }
  3126. len = PyList_GET_SIZE(tmp);
  3127. body = asdl_seq_new(len, arena);
  3128. if (body == NULL) goto failed;
  3129. for (i = 0; i < len; i++) {
  3130. stmt_ty value;
  3131. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3132. if (res != 0) goto failed;
  3133. asdl_seq_SET(body, i, value);
  3134. }
  3135. Py_XDECREF(tmp);
  3136. tmp = NULL;
  3137. } else {
  3138. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
  3139. return 1;
  3140. }
  3141. *out = Interactive(body, arena);
  3142. if (*out == NULL) goto failed;
  3143. return 0;
  3144. }
  3145. if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
  3146. expr_ty body;
  3147. if (PyObject_HasAttrString(obj, "body")) {
  3148. int res;
  3149. tmp = PyObject_GetAttrString(obj, "body");
  3150. if (tmp == NULL) goto failed;
  3151. res = obj2ast_expr(tmp, &body, arena);
  3152. if (res != 0) goto failed;
  3153. Py_XDECREF(tmp);
  3154. tmp = NULL;
  3155. } else {
  3156. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
  3157. return 1;
  3158. }
  3159. *out = Expression(body, arena);
  3160. if (*out == NULL) goto failed;
  3161. return 0;
  3162. }
  3163. if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
  3164. asdl_seq* body;
  3165. if (PyObject_HasAttrString(obj, "body")) {
  3166. int res;
  3167. Py_ssize_t len;
  3168. Py_ssize_t i;
  3169. tmp = PyObject_GetAttrString(obj, "body");
  3170. if (tmp == NULL) goto failed;
  3171. if (!PyList_Check(tmp)) {
  3172. PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3173. goto failed;
  3174. }
  3175. len = PyList_GET_SIZE(tmp);
  3176. body = asdl_seq_new(len, arena);
  3177. if (body == NULL) goto failed;
  3178. for (i = 0; i < len; i++) {
  3179. stmt_ty value;
  3180. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3181. if (res != 0) goto failed;
  3182. asdl_seq_SET(body, i, value);
  3183. }
  3184. Py_XDECREF(tmp);
  3185. tmp = NULL;
  3186. } else {
  3187. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
  3188. return 1;
  3189. }
  3190. *out = Suite(body, arena);
  3191. if (*out == NULL) goto failed;
  3192. return 0;
  3193. }
  3194. tmp = PyObject_Repr(obj);
  3195. if (tmp == NULL) goto failed;
  3196. PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
  3197. failed:
  3198. Py_XDECREF(tmp);
  3199. return 1;
  3200. }
  3201. int
  3202. obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
  3203. {
  3204. PyObject* tmp = NULL;
  3205. int lineno;
  3206. int col_offset;
  3207. if (obj == Py_None) {
  3208. *out = NULL;
  3209. return 0;
  3210. }
  3211. if (PyObject_HasAttrString(obj, "lineno")) {
  3212. int res;
  3213. tmp = PyObject_GetAttrString(obj, "lineno");
  3214. if (tmp == NULL) goto failed;
  3215. res = obj2ast_int(tmp, &lineno, arena);
  3216. if (res != 0) goto failed;
  3217. Py_XDECREF(tmp);
  3218. tmp = NULL;
  3219. } else {
  3220. PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
  3221. return 1;
  3222. }
  3223. if (PyObject_HasAttrString(obj, "col_offset")) {
  3224. int res;
  3225. tmp = PyObject_GetAttrString(obj, "col_offset");
  3226. if (tmp == NULL) goto failed;
  3227. res = obj2ast_int(tmp, &col_offset, arena);
  3228. if (res != 0) goto failed;
  3229. Py_XDECREF(tmp);
  3230. tmp = NULL;
  3231. } else {
  3232. PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
  3233. return 1;
  3234. }
  3235. if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
  3236. identifier name;
  3237. arguments_ty args;
  3238. asdl_seq* body;
  3239. asdl_seq* decorator_list;
  3240. if (PyObject_HasAttrString(obj, "name")) {
  3241. int res;
  3242. tmp = PyObject_GetAttrString(obj, "name");
  3243. if (tmp == NULL) goto failed;
  3244. res = obj2ast_identifier(tmp, &name, arena);
  3245. if (res != 0) goto failed;
  3246. Py_XDECREF(tmp);
  3247. tmp = NULL;
  3248. } else {
  3249. PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
  3250. return 1;
  3251. }
  3252. if (PyObject_HasAttrString(obj, "args")) {
  3253. int res;
  3254. tmp = PyObject_GetAttrString(obj, "args");
  3255. if (tmp == NULL) goto failed;
  3256. res = obj2ast_arguments(tmp, &args, arena);
  3257. if (res != 0) goto failed;
  3258. Py_XDECREF(tmp);
  3259. tmp = NULL;
  3260. } else {
  3261. PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
  3262. return 1;
  3263. }
  3264. if (PyObject_HasAttrString(obj, "body")) {
  3265. int res;
  3266. Py_ssize_t len;
  3267. Py_ssize_t i;
  3268. tmp = PyObject_GetAttrString(obj, "body");
  3269. if (tmp == NULL) goto failed;
  3270. if (!PyList_Check(tmp)) {
  3271. PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3272. goto failed;
  3273. }
  3274. len = PyList_GET_SIZE(tmp);
  3275. body = asdl_seq_new(len, arena);
  3276. if (body == NULL) goto failed;
  3277. for (i = 0; i < len; i++) {
  3278. stmt_ty value;
  3279. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3280. if (res != 0) goto failed;
  3281. asdl_seq_SET(body, i, value);
  3282. }
  3283. Py_XDECREF(tmp);
  3284. tmp = NULL;
  3285. } else {
  3286. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
  3287. return 1;
  3288. }
  3289. if (PyObject_HasAttrString(obj, "decorator_list")) {
  3290. int res;
  3291. Py_ssize_t len;
  3292. Py_ssize_t i;
  3293. tmp = PyObject_GetAttrString(obj, "decorator_list");
  3294. if (tmp == NULL) goto failed;
  3295. if (!PyList_Check(tmp)) {
  3296. PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3297. goto failed;
  3298. }
  3299. len = PyList_GET_SIZE(tmp);
  3300. decorator_list = asdl_seq_new(len, arena);
  3301. if (decorator_list == NULL) goto failed;
  3302. for (i = 0; i < len; i++) {
  3303. expr_ty value;
  3304. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  3305. if (res != 0) goto failed;
  3306. asdl_seq_SET(decorator_list, i, value);
  3307. }
  3308. Py_XDECREF(tmp);
  3309. tmp = NULL;
  3310. } else {
  3311. PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
  3312. return 1;
  3313. }
  3314. *out = FunctionDef(name, args, body, decorator_list, lineno,
  3315. col_offset, arena);
  3316. if (*out == NULL) goto failed;
  3317. return 0;
  3318. }
  3319. if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
  3320. identifier name;
  3321. asdl_seq* bases;
  3322. asdl_seq* body;
  3323. asdl_seq* decorator_list;
  3324. if (PyObject_HasAttrString(obj, "name")) {
  3325. int res;
  3326. tmp = PyObject_GetAttrString(obj, "name");
  3327. if (tmp == NULL) goto failed;
  3328. res = obj2ast_identifier(tmp, &name, arena);
  3329. if (res != 0) goto failed;
  3330. Py_XDECREF(tmp);
  3331. tmp = NULL;
  3332. } else {
  3333. PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
  3334. return 1;
  3335. }
  3336. if (PyObject_HasAttrString(obj, "bases")) {
  3337. int res;
  3338. Py_ssize_t len;
  3339. Py_ssize_t i;
  3340. tmp = PyObject_GetAttrString(obj, "bases");
  3341. if (tmp == NULL) goto failed;
  3342. if (!PyList_Check(tmp)) {
  3343. PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3344. goto failed;
  3345. }
  3346. len = PyList_GET_SIZE(tmp);
  3347. bases = asdl_seq_new(len, arena);
  3348. if (bases == NULL) goto failed;
  3349. for (i = 0; i < len; i++) {
  3350. expr_ty value;
  3351. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  3352. if (res != 0) goto failed;
  3353. asdl_seq_SET(bases, i, value);
  3354. }
  3355. Py_XDECREF(tmp);
  3356. tmp = NULL;
  3357. } else {
  3358. PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
  3359. return 1;
  3360. }
  3361. if (PyObject_HasAttrString(obj, "body")) {
  3362. int res;
  3363. Py_ssize_t len;
  3364. Py_ssize_t i;
  3365. tmp = PyObject_GetAttrString(obj, "body");
  3366. if (tmp == NULL) goto failed;
  3367. if (!PyList_Check(tmp)) {
  3368. PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3369. goto failed;
  3370. }
  3371. len = PyList_GET_SIZE(tmp);
  3372. body = asdl_seq_new(len, arena);
  3373. if (body == NULL) goto failed;
  3374. for (i = 0; i < len; i++) {
  3375. stmt_ty value;
  3376. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3377. if (res != 0) goto failed;
  3378. asdl_seq_SET(body, i, value);
  3379. }
  3380. Py_XDECREF(tmp);
  3381. tmp = NULL;
  3382. } else {
  3383. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
  3384. return 1;
  3385. }
  3386. if (PyObject_HasAttrString(obj, "decorator_list")) {
  3387. int res;
  3388. Py_ssize_t len;
  3389. Py_ssize_t i;
  3390. tmp = PyObject_GetAttrString(obj, "decorator_list");
  3391. if (tmp == NULL) goto failed;
  3392. if (!PyList_Check(tmp)) {
  3393. PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3394. goto failed;
  3395. }
  3396. len = PyList_GET_SIZE(tmp);
  3397. decorator_list = asdl_seq_new(len, arena);
  3398. if (decorator_list == NULL) goto failed;
  3399. for (i = 0; i < len; i++) {
  3400. expr_ty value;
  3401. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  3402. if (res != 0) goto failed;
  3403. asdl_seq_SET(decorator_list, i, value);
  3404. }
  3405. Py_XDECREF(tmp);
  3406. tmp = NULL;
  3407. } else {
  3408. PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
  3409. return 1;
  3410. }
  3411. *out = ClassDef(name, bases, body, decorator_list, lineno,
  3412. col_offset, arena);
  3413. if (*out == NULL) goto failed;
  3414. return 0;
  3415. }
  3416. if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
  3417. expr_ty value;
  3418. if (PyObject_HasAttrString(obj, "value")) {
  3419. int res;
  3420. tmp = PyObject_GetAttrString(obj, "value");
  3421. if (tmp == NULL) goto failed;
  3422. res = obj2ast_expr(tmp, &value, arena);
  3423. if (res != 0) goto failed;
  3424. Py_XDECREF(tmp);
  3425. tmp = NULL;
  3426. } else {
  3427. value = NULL;
  3428. }
  3429. *out = Return(value, lineno, col_offset, arena);
  3430. if (*out == NULL) goto failed;
  3431. return 0;
  3432. }
  3433. if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
  3434. asdl_seq* targets;
  3435. if (PyObject_HasAttrString(obj, "targets")) {
  3436. int res;
  3437. Py_ssize_t len;
  3438. Py_ssize_t i;
  3439. tmp = PyObject_GetAttrString(obj, "targets");
  3440. if (tmp == NULL) goto failed;
  3441. if (!PyList_Check(tmp)) {
  3442. PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3443. goto failed;
  3444. }
  3445. len = PyList_GET_SIZE(tmp);
  3446. targets = asdl_seq_new(len, arena);
  3447. if (targets == NULL) goto failed;
  3448. for (i = 0; i < len; i++) {
  3449. expr_ty value;
  3450. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  3451. if (res != 0) goto failed;
  3452. asdl_seq_SET(targets, i, value);
  3453. }
  3454. Py_XDECREF(tmp);
  3455. tmp = NULL;
  3456. } else {
  3457. PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
  3458. return 1;
  3459. }
  3460. *out = Delete(targets, lineno, col_offset, arena);
  3461. if (*out == NULL) goto failed;
  3462. return 0;
  3463. }
  3464. if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
  3465. asdl_seq* targets;
  3466. expr_ty value;
  3467. if (PyObject_HasAttrString(obj, "targets")) {
  3468. int res;
  3469. Py_ssize_t len;
  3470. Py_ssize_t i;
  3471. tmp = PyObject_GetAttrString(obj, "targets");
  3472. if (tmp == NULL) goto failed;
  3473. if (!PyList_Check(tmp)) {
  3474. PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3475. goto failed;
  3476. }
  3477. len = PyList_GET_SIZE(tmp);
  3478. targets = asdl_seq_new(len, arena);
  3479. if (targets == NULL) goto failed;
  3480. for (i = 0; i < len; i++) {
  3481. expr_ty value;
  3482. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  3483. if (res != 0) goto failed;
  3484. asdl_seq_SET(targets, i, value);
  3485. }
  3486. Py_XDECREF(tmp);
  3487. tmp = NULL;
  3488. } else {
  3489. PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
  3490. return 1;
  3491. }
  3492. if (PyObject_HasAttrString(obj, "value")) {
  3493. int res;
  3494. tmp = PyObject_GetAttrString(obj, "value");
  3495. if (tmp == NULL) goto failed;
  3496. res = obj2ast_expr(tmp, &value, arena);
  3497. if (res != 0) goto failed;
  3498. Py_XDECREF(tmp);
  3499. tmp = NULL;
  3500. } else {
  3501. PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
  3502. return 1;
  3503. }
  3504. *out = Assign(targets, value, lineno, col_offset, arena);
  3505. if (*out == NULL) goto failed;
  3506. return 0;
  3507. }
  3508. if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
  3509. expr_ty target;
  3510. operator_ty op;
  3511. expr_ty value;
  3512. if (PyObject_HasAttrString(obj, "target")) {
  3513. int res;
  3514. tmp = PyObject_GetAttrString(obj, "target");
  3515. if (tmp == NULL) goto failed;
  3516. res = obj2ast_expr(tmp, &target, arena);
  3517. if (res != 0) goto failed;
  3518. Py_XDECREF(tmp);
  3519. tmp = NULL;
  3520. } else {
  3521. PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
  3522. return 1;
  3523. }
  3524. if (PyObject_HasAttrString(obj, "op")) {
  3525. int res;
  3526. tmp = PyObject_GetAttrString(obj, "op");
  3527. if (tmp == NULL) goto failed;
  3528. res = obj2ast_operator(tmp, &op, arena);
  3529. if (res != 0) goto failed;
  3530. Py_XDECREF(tmp);
  3531. tmp = NULL;
  3532. } else {
  3533. PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
  3534. return 1;
  3535. }
  3536. if (PyObject_HasAttrString(obj, "value")) {
  3537. int res;
  3538. tmp = PyObject_GetAttrString(obj, "value");
  3539. if (tmp == NULL) goto failed;
  3540. res = obj2ast_expr(tmp, &value, arena);
  3541. if (res != 0) goto failed;
  3542. Py_XDECREF(tmp);
  3543. tmp = NULL;
  3544. } else {
  3545. PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
  3546. return 1;
  3547. }
  3548. *out = AugAssign(target, op, value, lineno, col_offset, arena);
  3549. if (*out == NULL) goto failed;
  3550. return 0;
  3551. }
  3552. if (PyObject_IsInstance(obj, (PyObject*)Print_type)) {
  3553. expr_ty dest;
  3554. asdl_seq* values;
  3555. bool nl;
  3556. if (PyObject_HasAttrString(obj, "dest")) {
  3557. int res;
  3558. tmp = PyObject_GetAttrString(obj, "dest");
  3559. if (tmp == NULL) goto failed;
  3560. res = obj2ast_expr(tmp, &dest, arena);
  3561. if (res != 0) goto failed;
  3562. Py_XDECREF(tmp);
  3563. tmp = NULL;
  3564. } else {
  3565. dest = NULL;
  3566. }
  3567. if (PyObject_HasAttrString(obj, "values")) {
  3568. int res;
  3569. Py_ssize_t len;
  3570. Py_ssize_t i;
  3571. tmp = PyObject_GetAttrString(obj, "values");
  3572. if (tmp == NULL) goto failed;
  3573. if (!PyList_Check(tmp)) {
  3574. PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3575. goto failed;
  3576. }
  3577. len = PyList_GET_SIZE(tmp);
  3578. values = asdl_seq_new(len, arena);
  3579. if (values == NULL) goto failed;
  3580. for (i = 0; i < len; i++) {
  3581. expr_ty value;
  3582. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  3583. if (res != 0) goto failed;
  3584. asdl_seq_SET(values, i, value);
  3585. }
  3586. Py_XDECREF(tmp);
  3587. tmp = NULL;
  3588. } else {
  3589. PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
  3590. return 1;
  3591. }
  3592. if (PyObject_HasAttrString(obj, "nl")) {
  3593. int res;
  3594. tmp = PyObject_GetAttrString(obj, "nl");
  3595. if (tmp == NULL) goto failed;
  3596. res = obj2ast_bool(tmp, &nl, arena);
  3597. if (res != 0) goto failed;
  3598. Py_XDECREF(tmp);
  3599. tmp = NULL;
  3600. } else {
  3601. PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
  3602. return 1;
  3603. }
  3604. *out = Print(dest, values, nl, lineno, col_offset, arena);
  3605. if (*out == NULL) goto failed;
  3606. return 0;
  3607. }
  3608. if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
  3609. expr_ty target;
  3610. expr_ty iter;
  3611. asdl_seq* body;
  3612. asdl_seq* orelse;
  3613. if (PyObject_HasAttrString(obj, "target")) {
  3614. int res;
  3615. tmp = PyObject_GetAttrString(obj, "target");
  3616. if (tmp == NULL) goto failed;
  3617. res = obj2ast_expr(tmp, &target, arena);
  3618. if (res != 0) goto failed;
  3619. Py_XDECREF(tmp);
  3620. tmp = NULL;
  3621. } else {
  3622. PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
  3623. return 1;
  3624. }
  3625. if (PyObject_HasAttrString(obj, "iter")) {
  3626. int res;
  3627. tmp = PyObject_GetAttrString(obj, "iter");
  3628. if (tmp == NULL) goto failed;
  3629. res = obj2ast_expr(tmp, &iter, arena);
  3630. if (res != 0) goto failed;
  3631. Py_XDECREF(tmp);
  3632. tmp = NULL;
  3633. } else {
  3634. PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
  3635. return 1;
  3636. }
  3637. if (PyObject_HasAttrString(obj, "body")) {
  3638. int res;
  3639. Py_ssize_t len;
  3640. Py_ssize_t i;
  3641. tmp = PyObject_GetAttrString(obj, "body");
  3642. if (tmp == NULL) goto failed;
  3643. if (!PyList_Check(tmp)) {
  3644. PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3645. goto failed;
  3646. }
  3647. len = PyList_GET_SIZE(tmp);
  3648. body = asdl_seq_new(len, arena);
  3649. if (body == NULL) goto failed;
  3650. for (i = 0; i < len; i++) {
  3651. stmt_ty value;
  3652. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3653. if (res != 0) goto failed;
  3654. asdl_seq_SET(body, i, value);
  3655. }
  3656. Py_XDECREF(tmp);
  3657. tmp = NULL;
  3658. } else {
  3659. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
  3660. return 1;
  3661. }
  3662. if (PyObject_HasAttrString(obj, "orelse")) {
  3663. int res;
  3664. Py_ssize_t len;
  3665. Py_ssize_t i;
  3666. tmp = PyObject_GetAttrString(obj, "orelse");
  3667. if (tmp == NULL) goto failed;
  3668. if (!PyList_Check(tmp)) {
  3669. PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3670. goto failed;
  3671. }
  3672. len = PyList_GET_SIZE(tmp);
  3673. orelse = asdl_seq_new(len, arena);
  3674. if (orelse == NULL) goto failed;
  3675. for (i = 0; i < len; i++) {
  3676. stmt_ty value;
  3677. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3678. if (res != 0) goto failed;
  3679. asdl_seq_SET(orelse, i, value);
  3680. }
  3681. Py_XDECREF(tmp);
  3682. tmp = NULL;
  3683. } else {
  3684. PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
  3685. return 1;
  3686. }
  3687. *out = For(target, iter, body, orelse, lineno, col_offset,
  3688. arena);
  3689. if (*out == NULL) goto failed;
  3690. return 0;
  3691. }
  3692. if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
  3693. expr_ty test;
  3694. asdl_seq* body;
  3695. asdl_seq* orelse;
  3696. if (PyObject_HasAttrString(obj, "test")) {
  3697. int res;
  3698. tmp = PyObject_GetAttrString(obj, "test");
  3699. if (tmp == NULL) goto failed;
  3700. res = obj2ast_expr(tmp, &test, arena);
  3701. if (res != 0) goto failed;
  3702. Py_XDECREF(tmp);
  3703. tmp = NULL;
  3704. } else {
  3705. PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
  3706. return 1;
  3707. }
  3708. if (PyObject_HasAttrString(obj, "body")) {
  3709. int res;
  3710. Py_ssize_t len;
  3711. Py_ssize_t i;
  3712. tmp = PyObject_GetAttrString(obj, "body");
  3713. if (tmp == NULL) goto failed;
  3714. if (!PyList_Check(tmp)) {
  3715. PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3716. goto failed;
  3717. }
  3718. len = PyList_GET_SIZE(tmp);
  3719. body = asdl_seq_new(len, arena);
  3720. if (body == NULL) goto failed;
  3721. for (i = 0; i < len; i++) {
  3722. stmt_ty value;
  3723. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3724. if (res != 0) goto failed;
  3725. asdl_seq_SET(body, i, value);
  3726. }
  3727. Py_XDECREF(tmp);
  3728. tmp = NULL;
  3729. } else {
  3730. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
  3731. return 1;
  3732. }
  3733. if (PyObject_HasAttrString(obj, "orelse")) {
  3734. int res;
  3735. Py_ssize_t len;
  3736. Py_ssize_t i;
  3737. tmp = PyObject_GetAttrString(obj, "orelse");
  3738. if (tmp == NULL) goto failed;
  3739. if (!PyList_Check(tmp)) {
  3740. PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3741. goto failed;
  3742. }
  3743. len = PyList_GET_SIZE(tmp);
  3744. orelse = asdl_seq_new(len, arena);
  3745. if (orelse == NULL) goto failed;
  3746. for (i = 0; i < len; i++) {
  3747. stmt_ty value;
  3748. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3749. if (res != 0) goto failed;
  3750. asdl_seq_SET(orelse, i, value);
  3751. }
  3752. Py_XDECREF(tmp);
  3753. tmp = NULL;
  3754. } else {
  3755. PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
  3756. return 1;
  3757. }
  3758. *out = While(test, body, orelse, lineno, col_offset, arena);
  3759. if (*out == NULL) goto failed;
  3760. return 0;
  3761. }
  3762. if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
  3763. expr_ty test;
  3764. asdl_seq* body;
  3765. asdl_seq* orelse;
  3766. if (PyObject_HasAttrString(obj, "test")) {
  3767. int res;
  3768. tmp = PyObject_GetAttrString(obj, "test");
  3769. if (tmp == NULL) goto failed;
  3770. res = obj2ast_expr(tmp, &test, arena);
  3771. if (res != 0) goto failed;
  3772. Py_XDECREF(tmp);
  3773. tmp = NULL;
  3774. } else {
  3775. PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
  3776. return 1;
  3777. }
  3778. if (PyObject_HasAttrString(obj, "body")) {
  3779. int res;
  3780. Py_ssize_t len;
  3781. Py_ssize_t i;
  3782. tmp = PyObject_GetAttrString(obj, "body");
  3783. if (tmp == NULL) goto failed;
  3784. if (!PyList_Check(tmp)) {
  3785. PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3786. goto failed;
  3787. }
  3788. len = PyList_GET_SIZE(tmp);
  3789. body = asdl_seq_new(len, arena);
  3790. if (body == NULL) goto failed;
  3791. for (i = 0; i < len; i++) {
  3792. stmt_ty value;
  3793. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3794. if (res != 0) goto failed;
  3795. asdl_seq_SET(body, i, value);
  3796. }
  3797. Py_XDECREF(tmp);
  3798. tmp = NULL;
  3799. } else {
  3800. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
  3801. return 1;
  3802. }
  3803. if (PyObject_HasAttrString(obj, "orelse")) {
  3804. int res;
  3805. Py_ssize_t len;
  3806. Py_ssize_t i;
  3807. tmp = PyObject_GetAttrString(obj, "orelse");
  3808. if (tmp == NULL) goto failed;
  3809. if (!PyList_Check(tmp)) {
  3810. PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3811. goto failed;
  3812. }
  3813. len = PyList_GET_SIZE(tmp);
  3814. orelse = asdl_seq_new(len, arena);
  3815. if (orelse == NULL) goto failed;
  3816. for (i = 0; i < len; i++) {
  3817. stmt_ty value;
  3818. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3819. if (res != 0) goto failed;
  3820. asdl_seq_SET(orelse, i, value);
  3821. }
  3822. Py_XDECREF(tmp);
  3823. tmp = NULL;
  3824. } else {
  3825. PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
  3826. return 1;
  3827. }
  3828. *out = If(test, body, orelse, lineno, col_offset, arena);
  3829. if (*out == NULL) goto failed;
  3830. return 0;
  3831. }
  3832. if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
  3833. expr_ty context_expr;
  3834. expr_ty optional_vars;
  3835. asdl_seq* body;
  3836. if (PyObject_HasAttrString(obj, "context_expr")) {
  3837. int res;
  3838. tmp = PyObject_GetAttrString(obj, "context_expr");
  3839. if (tmp == NULL) goto failed;
  3840. res = obj2ast_expr(tmp, &context_expr, arena);
  3841. if (res != 0) goto failed;
  3842. Py_XDECREF(tmp);
  3843. tmp = NULL;
  3844. } else {
  3845. PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
  3846. return 1;
  3847. }
  3848. if (PyObject_HasAttrString(obj, "optional_vars")) {
  3849. int res;
  3850. tmp = PyObject_GetAttrString(obj, "optional_vars");
  3851. if (tmp == NULL) goto failed;
  3852. res = obj2ast_expr(tmp, &optional_vars, arena);
  3853. if (res != 0) goto failed;
  3854. Py_XDECREF(tmp);
  3855. tmp = NULL;
  3856. } else {
  3857. optional_vars = NULL;
  3858. }
  3859. if (PyObject_HasAttrString(obj, "body")) {
  3860. int res;
  3861. Py_ssize_t len;
  3862. Py_ssize_t i;
  3863. tmp = PyObject_GetAttrString(obj, "body");
  3864. if (tmp == NULL) goto failed;
  3865. if (!PyList_Check(tmp)) {
  3866. PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3867. goto failed;
  3868. }
  3869. len = PyList_GET_SIZE(tmp);
  3870. body = asdl_seq_new(len, arena);
  3871. if (body == NULL) goto failed;
  3872. for (i = 0; i < len; i++) {
  3873. stmt_ty value;
  3874. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3875. if (res != 0) goto failed;
  3876. asdl_seq_SET(body, i, value);
  3877. }
  3878. Py_XDECREF(tmp);
  3879. tmp = NULL;
  3880. } else {
  3881. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
  3882. return 1;
  3883. }
  3884. *out = With(context_expr, optional_vars, body, lineno,
  3885. col_offset, arena);
  3886. if (*out == NULL) goto failed;
  3887. return 0;
  3888. }
  3889. if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
  3890. expr_ty type;
  3891. expr_ty inst;
  3892. expr_ty tback;
  3893. if (PyObject_HasAttrString(obj, "type")) {
  3894. int res;
  3895. tmp = PyObject_GetAttrString(obj, "type");
  3896. if (tmp == NULL) goto failed;
  3897. res = obj2ast_expr(tmp, &type, arena);
  3898. if (res != 0) goto failed;
  3899. Py_XDECREF(tmp);
  3900. tmp = NULL;
  3901. } else {
  3902. type = NULL;
  3903. }
  3904. if (PyObject_HasAttrString(obj, "inst")) {
  3905. int res;
  3906. tmp = PyObject_GetAttrString(obj, "inst");
  3907. if (tmp == NULL) goto failed;
  3908. res = obj2ast_expr(tmp, &inst, arena);
  3909. if (res != 0) goto failed;
  3910. Py_XDECREF(tmp);
  3911. tmp = NULL;
  3912. } else {
  3913. inst = NULL;
  3914. }
  3915. if (PyObject_HasAttrString(obj, "tback")) {
  3916. int res;
  3917. tmp = PyObject_GetAttrString(obj, "tback");
  3918. if (tmp == NULL) goto failed;
  3919. res = obj2ast_expr(tmp, &tback, arena);
  3920. if (res != 0) goto failed;
  3921. Py_XDECREF(tmp);
  3922. tmp = NULL;
  3923. } else {
  3924. tback = NULL;
  3925. }
  3926. *out = Raise(type, inst, tback, lineno, col_offset, arena);
  3927. if (*out == NULL) goto failed;
  3928. return 0;
  3929. }
  3930. if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
  3931. asdl_seq* body;
  3932. asdl_seq* handlers;
  3933. asdl_seq* orelse;
  3934. if (PyObject_HasAttrString(obj, "body")) {
  3935. int res;
  3936. Py_ssize_t len;
  3937. Py_ssize_t i;
  3938. tmp = PyObject_GetAttrString(obj, "body");
  3939. if (tmp == NULL) goto failed;
  3940. if (!PyList_Check(tmp)) {
  3941. PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3942. goto failed;
  3943. }
  3944. len = PyList_GET_SIZE(tmp);
  3945. body = asdl_seq_new(len, arena);
  3946. if (body == NULL) goto failed;
  3947. for (i = 0; i < len; i++) {
  3948. stmt_ty value;
  3949. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  3950. if (res != 0) goto failed;
  3951. asdl_seq_SET(body, i, value);
  3952. }
  3953. Py_XDECREF(tmp);
  3954. tmp = NULL;
  3955. } else {
  3956. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
  3957. return 1;
  3958. }
  3959. if (PyObject_HasAttrString(obj, "handlers")) {
  3960. int res;
  3961. Py_ssize_t len;
  3962. Py_ssize_t i;
  3963. tmp = PyObject_GetAttrString(obj, "handlers");
  3964. if (tmp == NULL) goto failed;
  3965. if (!PyList_Check(tmp)) {
  3966. PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3967. goto failed;
  3968. }
  3969. len = PyList_GET_SIZE(tmp);
  3970. handlers = asdl_seq_new(len, arena);
  3971. if (handlers == NULL) goto failed;
  3972. for (i = 0; i < len; i++) {
  3973. excepthandler_ty value;
  3974. res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
  3975. if (res != 0) goto failed;
  3976. asdl_seq_SET(handlers, i, value);
  3977. }
  3978. Py_XDECREF(tmp);
  3979. tmp = NULL;
  3980. } else {
  3981. PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
  3982. return 1;
  3983. }
  3984. if (PyObject_HasAttrString(obj, "orelse")) {
  3985. int res;
  3986. Py_ssize_t len;
  3987. Py_ssize_t i;
  3988. tmp = PyObject_GetAttrString(obj, "orelse");
  3989. if (tmp == NULL) goto failed;
  3990. if (!PyList_Check(tmp)) {
  3991. PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  3992. goto failed;
  3993. }
  3994. len = PyList_GET_SIZE(tmp);
  3995. orelse = asdl_seq_new(len, arena);
  3996. if (orelse == NULL) goto failed;
  3997. for (i = 0; i < len; i++) {
  3998. stmt_ty value;
  3999. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  4000. if (res != 0) goto failed;
  4001. asdl_seq_SET(orelse, i, value);
  4002. }
  4003. Py_XDECREF(tmp);
  4004. tmp = NULL;
  4005. } else {
  4006. PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
  4007. return 1;
  4008. }
  4009. *out = TryExcept(body, handlers, orelse, lineno, col_offset,
  4010. arena);
  4011. if (*out == NULL) goto failed;
  4012. return 0;
  4013. }
  4014. if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
  4015. asdl_seq* body;
  4016. asdl_seq* finalbody;
  4017. if (PyObject_HasAttrString(obj, "body")) {
  4018. int res;
  4019. Py_ssize_t len;
  4020. Py_ssize_t i;
  4021. tmp = PyObject_GetAttrString(obj, "body");
  4022. if (tmp == NULL) goto failed;
  4023. if (!PyList_Check(tmp)) {
  4024. PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4025. goto failed;
  4026. }
  4027. len = PyList_GET_SIZE(tmp);
  4028. body = asdl_seq_new(len, arena);
  4029. if (body == NULL) goto failed;
  4030. for (i = 0; i < len; i++) {
  4031. stmt_ty value;
  4032. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  4033. if (res != 0) goto failed;
  4034. asdl_seq_SET(body, i, value);
  4035. }
  4036. Py_XDECREF(tmp);
  4037. tmp = NULL;
  4038. } else {
  4039. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
  4040. return 1;
  4041. }
  4042. if (PyObject_HasAttrString(obj, "finalbody")) {
  4043. int res;
  4044. Py_ssize_t len;
  4045. Py_ssize_t i;
  4046. tmp = PyObject_GetAttrString(obj, "finalbody");
  4047. if (tmp == NULL) goto failed;
  4048. if (!PyList_Check(tmp)) {
  4049. PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4050. goto failed;
  4051. }
  4052. len = PyList_GET_SIZE(tmp);
  4053. finalbody = asdl_seq_new(len, arena);
  4054. if (finalbody == NULL) goto failed;
  4055. for (i = 0; i < len; i++) {
  4056. stmt_ty value;
  4057. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  4058. if (res != 0) goto failed;
  4059. asdl_seq_SET(finalbody, i, value);
  4060. }
  4061. Py_XDECREF(tmp);
  4062. tmp = NULL;
  4063. } else {
  4064. PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
  4065. return 1;
  4066. }
  4067. *out = TryFinally(body, finalbody, lineno, col_offset, arena);
  4068. if (*out == NULL) goto failed;
  4069. return 0;
  4070. }
  4071. if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
  4072. expr_ty test;
  4073. expr_ty msg;
  4074. if (PyObject_HasAttrString(obj, "test")) {
  4075. int res;
  4076. tmp = PyObject_GetAttrString(obj, "test");
  4077. if (tmp == NULL) goto failed;
  4078. res = obj2ast_expr(tmp, &test, arena);
  4079. if (res != 0) goto failed;
  4080. Py_XDECREF(tmp);
  4081. tmp = NULL;
  4082. } else {
  4083. PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
  4084. return 1;
  4085. }
  4086. if (PyObject_HasAttrString(obj, "msg")) {
  4087. int res;
  4088. tmp = PyObject_GetAttrString(obj, "msg");
  4089. if (tmp == NULL) goto failed;
  4090. res = obj2ast_expr(tmp, &msg, arena);
  4091. if (res != 0) goto failed;
  4092. Py_XDECREF(tmp);
  4093. tmp = NULL;
  4094. } else {
  4095. msg = NULL;
  4096. }
  4097. *out = Assert(test, msg, lineno, col_offset, arena);
  4098. if (*out == NULL) goto failed;
  4099. return 0;
  4100. }
  4101. if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
  4102. asdl_seq* names;
  4103. if (PyObject_HasAttrString(obj, "names")) {
  4104. int res;
  4105. Py_ssize_t len;
  4106. Py_ssize_t i;
  4107. tmp = PyObject_GetAttrString(obj, "names");
  4108. if (tmp == NULL) goto failed;
  4109. if (!PyList_Check(tmp)) {
  4110. PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4111. goto failed;
  4112. }
  4113. len = PyList_GET_SIZE(tmp);
  4114. names = asdl_seq_new(len, arena);
  4115. if (names == NULL) goto failed;
  4116. for (i = 0; i < len; i++) {
  4117. alias_ty value;
  4118. res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
  4119. if (res != 0) goto failed;
  4120. asdl_seq_SET(names, i, value);
  4121. }
  4122. Py_XDECREF(tmp);
  4123. tmp = NULL;
  4124. } else {
  4125. PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
  4126. return 1;
  4127. }
  4128. *out = Import(names, lineno, col_offset, arena);
  4129. if (*out == NULL) goto failed;
  4130. return 0;
  4131. }
  4132. if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
  4133. identifier module;
  4134. asdl_seq* names;
  4135. int level;
  4136. if (PyObject_HasAttrString(obj, "module")) {
  4137. int res;
  4138. tmp = PyObject_GetAttrString(obj, "module");
  4139. if (tmp == NULL) goto failed;
  4140. res = obj2ast_identifier(tmp, &module, arena);
  4141. if (res != 0) goto failed;
  4142. Py_XDECREF(tmp);
  4143. tmp = NULL;
  4144. } else {
  4145. PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom");
  4146. return 1;
  4147. }
  4148. if (PyObject_HasAttrString(obj, "names")) {
  4149. int res;
  4150. Py_ssize_t len;
  4151. Py_ssize_t i;
  4152. tmp = PyObject_GetAttrString(obj, "names");
  4153. if (tmp == NULL) goto failed;
  4154. if (!PyList_Check(tmp)) {
  4155. PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4156. goto failed;
  4157. }
  4158. len = PyList_GET_SIZE(tmp);
  4159. names = asdl_seq_new(len, arena);
  4160. if (names == NULL) goto failed;
  4161. for (i = 0; i < len; i++) {
  4162. alias_ty value;
  4163. res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
  4164. if (res != 0) goto failed;
  4165. asdl_seq_SET(names, i, value);
  4166. }
  4167. Py_XDECREF(tmp);
  4168. tmp = NULL;
  4169. } else {
  4170. PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
  4171. return 1;
  4172. }
  4173. if (PyObject_HasAttrString(obj, "level")) {
  4174. int res;
  4175. tmp = PyObject_GetAttrString(obj, "level");
  4176. if (tmp == NULL) goto failed;
  4177. res = obj2ast_int(tmp, &level, arena);
  4178. if (res != 0) goto failed;
  4179. Py_XDECREF(tmp);
  4180. tmp = NULL;
  4181. } else {
  4182. level = 0;
  4183. }
  4184. *out = ImportFrom(module, names, level, lineno, col_offset,
  4185. arena);
  4186. if (*out == NULL) goto failed;
  4187. return 0;
  4188. }
  4189. if (PyObject_IsInstance(obj, (PyObject*)Exec_type)) {
  4190. expr_ty body;
  4191. expr_ty globals;
  4192. expr_ty locals;
  4193. if (PyObject_HasAttrString(obj, "body")) {
  4194. int res;
  4195. tmp = PyObject_GetAttrString(obj, "body");
  4196. if (tmp == NULL) goto failed;
  4197. res = obj2ast_expr(tmp, &body, arena);
  4198. if (res != 0) goto failed;
  4199. Py_XDECREF(tmp);
  4200. tmp = NULL;
  4201. } else {
  4202. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
  4203. return 1;
  4204. }
  4205. if (PyObject_HasAttrString(obj, "globals")) {
  4206. int res;
  4207. tmp = PyObject_GetAttrString(obj, "globals");
  4208. if (tmp == NULL) goto failed;
  4209. res = obj2ast_expr(tmp, &globals, arena);
  4210. if (res != 0) goto failed;
  4211. Py_XDECREF(tmp);
  4212. tmp = NULL;
  4213. } else {
  4214. globals = NULL;
  4215. }
  4216. if (PyObject_HasAttrString(obj, "locals")) {
  4217. int res;
  4218. tmp = PyObject_GetAttrString(obj, "locals");
  4219. if (tmp == NULL) goto failed;
  4220. res = obj2ast_expr(tmp, &locals, arena);
  4221. if (res != 0) goto failed;
  4222. Py_XDECREF(tmp);
  4223. tmp = NULL;
  4224. } else {
  4225. locals = NULL;
  4226. }
  4227. *out = Exec(body, globals, locals, lineno, col_offset, arena);
  4228. if (*out == NULL) goto failed;
  4229. return 0;
  4230. }
  4231. if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
  4232. asdl_seq* names;
  4233. if (PyObject_HasAttrString(obj, "names")) {
  4234. int res;
  4235. Py_ssize_t len;
  4236. Py_ssize_t i;
  4237. tmp = PyObject_GetAttrString(obj, "names");
  4238. if (tmp == NULL) goto failed;
  4239. if (!PyList_Check(tmp)) {
  4240. PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4241. goto failed;
  4242. }
  4243. len = PyList_GET_SIZE(tmp);
  4244. names = asdl_seq_new(len, arena);
  4245. if (names == NULL) goto failed;
  4246. for (i = 0; i < len; i++) {
  4247. identifier value;
  4248. res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
  4249. if (res != 0) goto failed;
  4250. asdl_seq_SET(names, i, value);
  4251. }
  4252. Py_XDECREF(tmp);
  4253. tmp = NULL;
  4254. } else {
  4255. PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
  4256. return 1;
  4257. }
  4258. *out = Global(names, lineno, col_offset, arena);
  4259. if (*out == NULL) goto failed;
  4260. return 0;
  4261. }
  4262. if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
  4263. expr_ty value;
  4264. if (PyObject_HasAttrString(obj, "value")) {
  4265. int res;
  4266. tmp = PyObject_GetAttrString(obj, "value");
  4267. if (tmp == NULL) goto failed;
  4268. res = obj2ast_expr(tmp, &value, arena);
  4269. if (res != 0) goto failed;
  4270. Py_XDECREF(tmp);
  4271. tmp = NULL;
  4272. } else {
  4273. PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
  4274. return 1;
  4275. }
  4276. *out = Expr(value, lineno, col_offset, arena);
  4277. if (*out == NULL) goto failed;
  4278. return 0;
  4279. }
  4280. if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
  4281. *out = Pass(lineno, col_offset, arena);
  4282. if (*out == NULL) goto failed;
  4283. return 0;
  4284. }
  4285. if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
  4286. *out = Break(lineno, col_offset, arena);
  4287. if (*out == NULL) goto failed;
  4288. return 0;
  4289. }
  4290. if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
  4291. *out = Continue(lineno, col_offset, arena);
  4292. if (*out == NULL) goto failed;
  4293. return 0;
  4294. }
  4295. tmp = PyObject_Repr(obj);
  4296. if (tmp == NULL) goto failed;
  4297. PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
  4298. failed:
  4299. Py_XDECREF(tmp);
  4300. return 1;
  4301. }
  4302. int
  4303. obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
  4304. {
  4305. PyObject* tmp = NULL;
  4306. int lineno;
  4307. int col_offset;
  4308. if (obj == Py_None) {
  4309. *out = NULL;
  4310. return 0;
  4311. }
  4312. if (PyObject_HasAttrString(obj, "lineno")) {
  4313. int res;
  4314. tmp = PyObject_GetAttrString(obj, "lineno");
  4315. if (tmp == NULL) goto failed;
  4316. res = obj2ast_int(tmp, &lineno, arena);
  4317. if (res != 0) goto failed;
  4318. Py_XDECREF(tmp);
  4319. tmp = NULL;
  4320. } else {
  4321. PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
  4322. return 1;
  4323. }
  4324. if (PyObject_HasAttrString(obj, "col_offset")) {
  4325. int res;
  4326. tmp = PyObject_GetAttrString(obj, "col_offset");
  4327. if (tmp == NULL) goto failed;
  4328. res = obj2ast_int(tmp, &col_offset, arena);
  4329. if (res != 0) goto failed;
  4330. Py_XDECREF(tmp);
  4331. tmp = NULL;
  4332. } else {
  4333. PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
  4334. return 1;
  4335. }
  4336. if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
  4337. boolop_ty op;
  4338. asdl_seq* values;
  4339. if (PyObject_HasAttrString(obj, "op")) {
  4340. int res;
  4341. tmp = PyObject_GetAttrString(obj, "op");
  4342. if (tmp == NULL) goto failed;
  4343. res = obj2ast_boolop(tmp, &op, arena);
  4344. if (res != 0) goto failed;
  4345. Py_XDECREF(tmp);
  4346. tmp = NULL;
  4347. } else {
  4348. PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
  4349. return 1;
  4350. }
  4351. if (PyObject_HasAttrString(obj, "values")) {
  4352. int res;
  4353. Py_ssize_t len;
  4354. Py_ssize_t i;
  4355. tmp = PyObject_GetAttrString(obj, "values");
  4356. if (tmp == NULL) goto failed;
  4357. if (!PyList_Check(tmp)) {
  4358. PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4359. goto failed;
  4360. }
  4361. len = PyList_GET_SIZE(tmp);
  4362. values = asdl_seq_new(len, arena);
  4363. if (values == NULL) goto failed;
  4364. for (i = 0; i < len; i++) {
  4365. expr_ty value;
  4366. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  4367. if (res != 0) goto failed;
  4368. asdl_seq_SET(values, i, value);
  4369. }
  4370. Py_XDECREF(tmp);
  4371. tmp = NULL;
  4372. } else {
  4373. PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
  4374. return 1;
  4375. }
  4376. *out = BoolOp(op, values, lineno, col_offset, arena);
  4377. if (*out == NULL) goto failed;
  4378. return 0;
  4379. }
  4380. if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
  4381. expr_ty left;
  4382. operator_ty op;
  4383. expr_ty right;
  4384. if (PyObject_HasAttrString(obj, "left")) {
  4385. int res;
  4386. tmp = PyObject_GetAttrString(obj, "left");
  4387. if (tmp == NULL) goto failed;
  4388. res = obj2ast_expr(tmp, &left, arena);
  4389. if (res != 0) goto failed;
  4390. Py_XDECREF(tmp);
  4391. tmp = NULL;
  4392. } else {
  4393. PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
  4394. return 1;
  4395. }
  4396. if (PyObject_HasAttrString(obj, "op")) {
  4397. int res;
  4398. tmp = PyObject_GetAttrString(obj, "op");
  4399. if (tmp == NULL) goto failed;
  4400. res = obj2ast_operator(tmp, &op, arena);
  4401. if (res != 0) goto failed;
  4402. Py_XDECREF(tmp);
  4403. tmp = NULL;
  4404. } else {
  4405. PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
  4406. return 1;
  4407. }
  4408. if (PyObject_HasAttrString(obj, "right")) {
  4409. int res;
  4410. tmp = PyObject_GetAttrString(obj, "right");
  4411. if (tmp == NULL) goto failed;
  4412. res = obj2ast_expr(tmp, &right, arena);
  4413. if (res != 0) goto failed;
  4414. Py_XDECREF(tmp);
  4415. tmp = NULL;
  4416. } else {
  4417. PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
  4418. return 1;
  4419. }
  4420. *out = BinOp(left, op, right, lineno, col_offset, arena);
  4421. if (*out == NULL) goto failed;
  4422. return 0;
  4423. }
  4424. if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
  4425. unaryop_ty op;
  4426. expr_ty operand;
  4427. if (PyObject_HasAttrString(obj, "op")) {
  4428. int res;
  4429. tmp = PyObject_GetAttrString(obj, "op");
  4430. if (tmp == NULL) goto failed;
  4431. res = obj2ast_unaryop(tmp, &op, arena);
  4432. if (res != 0) goto failed;
  4433. Py_XDECREF(tmp);
  4434. tmp = NULL;
  4435. } else {
  4436. PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
  4437. return 1;
  4438. }
  4439. if (PyObject_HasAttrString(obj, "operand")) {
  4440. int res;
  4441. tmp = PyObject_GetAttrString(obj, "operand");
  4442. if (tmp == NULL) goto failed;
  4443. res = obj2ast_expr(tmp, &operand, arena);
  4444. if (res != 0) goto failed;
  4445. Py_XDECREF(tmp);
  4446. tmp = NULL;
  4447. } else {
  4448. PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
  4449. return 1;
  4450. }
  4451. *out = UnaryOp(op, operand, lineno, col_offset, arena);
  4452. if (*out == NULL) goto failed;
  4453. return 0;
  4454. }
  4455. if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
  4456. arguments_ty args;
  4457. expr_ty body;
  4458. if (PyObject_HasAttrString(obj, "args")) {
  4459. int res;
  4460. tmp = PyObject_GetAttrString(obj, "args");
  4461. if (tmp == NULL) goto failed;
  4462. res = obj2ast_arguments(tmp, &args, arena);
  4463. if (res != 0) goto failed;
  4464. Py_XDECREF(tmp);
  4465. tmp = NULL;
  4466. } else {
  4467. PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
  4468. return 1;
  4469. }
  4470. if (PyObject_HasAttrString(obj, "body")) {
  4471. int res;
  4472. tmp = PyObject_GetAttrString(obj, "body");
  4473. if (tmp == NULL) goto failed;
  4474. res = obj2ast_expr(tmp, &body, arena);
  4475. if (res != 0) goto failed;
  4476. Py_XDECREF(tmp);
  4477. tmp = NULL;
  4478. } else {
  4479. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
  4480. return 1;
  4481. }
  4482. *out = Lambda(args, body, lineno, col_offset, arena);
  4483. if (*out == NULL) goto failed;
  4484. return 0;
  4485. }
  4486. if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
  4487. expr_ty test;
  4488. expr_ty body;
  4489. expr_ty orelse;
  4490. if (PyObject_HasAttrString(obj, "test")) {
  4491. int res;
  4492. tmp = PyObject_GetAttrString(obj, "test");
  4493. if (tmp == NULL) goto failed;
  4494. res = obj2ast_expr(tmp, &test, arena);
  4495. if (res != 0) goto failed;
  4496. Py_XDECREF(tmp);
  4497. tmp = NULL;
  4498. } else {
  4499. PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
  4500. return 1;
  4501. }
  4502. if (PyObject_HasAttrString(obj, "body")) {
  4503. int res;
  4504. tmp = PyObject_GetAttrString(obj, "body");
  4505. if (tmp == NULL) goto failed;
  4506. res = obj2ast_expr(tmp, &body, arena);
  4507. if (res != 0) goto failed;
  4508. Py_XDECREF(tmp);
  4509. tmp = NULL;
  4510. } else {
  4511. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
  4512. return 1;
  4513. }
  4514. if (PyObject_HasAttrString(obj, "orelse")) {
  4515. int res;
  4516. tmp = PyObject_GetAttrString(obj, "orelse");
  4517. if (tmp == NULL) goto failed;
  4518. res = obj2ast_expr(tmp, &orelse, arena);
  4519. if (res != 0) goto failed;
  4520. Py_XDECREF(tmp);
  4521. tmp = NULL;
  4522. } else {
  4523. PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
  4524. return 1;
  4525. }
  4526. *out = IfExp(test, body, orelse, lineno, col_offset, arena);
  4527. if (*out == NULL) goto failed;
  4528. return 0;
  4529. }
  4530. if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
  4531. asdl_seq* keys;
  4532. asdl_seq* values;
  4533. if (PyObject_HasAttrString(obj, "keys")) {
  4534. int res;
  4535. Py_ssize_t len;
  4536. Py_ssize_t i;
  4537. tmp = PyObject_GetAttrString(obj, "keys");
  4538. if (tmp == NULL) goto failed;
  4539. if (!PyList_Check(tmp)) {
  4540. PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4541. goto failed;
  4542. }
  4543. len = PyList_GET_SIZE(tmp);
  4544. keys = asdl_seq_new(len, arena);
  4545. if (keys == NULL) goto failed;
  4546. for (i = 0; i < len; i++) {
  4547. expr_ty value;
  4548. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  4549. if (res != 0) goto failed;
  4550. asdl_seq_SET(keys, i, value);
  4551. }
  4552. Py_XDECREF(tmp);
  4553. tmp = NULL;
  4554. } else {
  4555. PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
  4556. return 1;
  4557. }
  4558. if (PyObject_HasAttrString(obj, "values")) {
  4559. int res;
  4560. Py_ssize_t len;
  4561. Py_ssize_t i;
  4562. tmp = PyObject_GetAttrString(obj, "values");
  4563. if (tmp == NULL) goto failed;
  4564. if (!PyList_Check(tmp)) {
  4565. PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4566. goto failed;
  4567. }
  4568. len = PyList_GET_SIZE(tmp);
  4569. values = asdl_seq_new(len, arena);
  4570. if (values == NULL) goto failed;
  4571. for (i = 0; i < len; i++) {
  4572. expr_ty value;
  4573. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  4574. if (res != 0) goto failed;
  4575. asdl_seq_SET(values, i, value);
  4576. }
  4577. Py_XDECREF(tmp);
  4578. tmp = NULL;
  4579. } else {
  4580. PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
  4581. return 1;
  4582. }
  4583. *out = Dict(keys, values, lineno, col_offset, arena);
  4584. if (*out == NULL) goto failed;
  4585. return 0;
  4586. }
  4587. if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
  4588. expr_ty elt;
  4589. asdl_seq* generators;
  4590. if (PyObject_HasAttrString(obj, "elt")) {
  4591. int res;
  4592. tmp = PyObject_GetAttrString(obj, "elt");
  4593. if (tmp == NULL) goto failed;
  4594. res = obj2ast_expr(tmp, &elt, arena);
  4595. if (res != 0) goto failed;
  4596. Py_XDECREF(tmp);
  4597. tmp = NULL;
  4598. } else {
  4599. PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
  4600. return 1;
  4601. }
  4602. if (PyObject_HasAttrString(obj, "generators")) {
  4603. int res;
  4604. Py_ssize_t len;
  4605. Py_ssize_t i;
  4606. tmp = PyObject_GetAttrString(obj, "generators");
  4607. if (tmp == NULL) goto failed;
  4608. if (!PyList_Check(tmp)) {
  4609. PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4610. goto failed;
  4611. }
  4612. len = PyList_GET_SIZE(tmp);
  4613. generators = asdl_seq_new(len, arena);
  4614. if (generators == NULL) goto failed;
  4615. for (i = 0; i < len; i++) {
  4616. comprehension_ty value;
  4617. res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
  4618. if (res != 0) goto failed;
  4619. asdl_seq_SET(generators, i, value);
  4620. }
  4621. Py_XDECREF(tmp);
  4622. tmp = NULL;
  4623. } else {
  4624. PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
  4625. return 1;
  4626. }
  4627. *out = ListComp(elt, generators, lineno, col_offset, arena);
  4628. if (*out == NULL) goto failed;
  4629. return 0;
  4630. }
  4631. if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
  4632. expr_ty elt;
  4633. asdl_seq* generators;
  4634. if (PyObject_HasAttrString(obj, "elt")) {
  4635. int res;
  4636. tmp = PyObject_GetAttrString(obj, "elt");
  4637. if (tmp == NULL) goto failed;
  4638. res = obj2ast_expr(tmp, &elt, arena);
  4639. if (res != 0) goto failed;
  4640. Py_XDECREF(tmp);
  4641. tmp = NULL;
  4642. } else {
  4643. PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
  4644. return 1;
  4645. }
  4646. if (PyObject_HasAttrString(obj, "generators")) {
  4647. int res;
  4648. Py_ssize_t len;
  4649. Py_ssize_t i;
  4650. tmp = PyObject_GetAttrString(obj, "generators");
  4651. if (tmp == NULL) goto failed;
  4652. if (!PyList_Check(tmp)) {
  4653. PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4654. goto failed;
  4655. }
  4656. len = PyList_GET_SIZE(tmp);
  4657. generators = asdl_seq_new(len, arena);
  4658. if (generators == NULL) goto failed;
  4659. for (i = 0; i < len; i++) {
  4660. comprehension_ty value;
  4661. res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
  4662. if (res != 0) goto failed;
  4663. asdl_seq_SET(generators, i, value);
  4664. }
  4665. Py_XDECREF(tmp);
  4666. tmp = NULL;
  4667. } else {
  4668. PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
  4669. return 1;
  4670. }
  4671. *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
  4672. if (*out == NULL) goto failed;
  4673. return 0;
  4674. }
  4675. if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
  4676. expr_ty value;
  4677. if (PyObject_HasAttrString(obj, "value")) {
  4678. int res;
  4679. tmp = PyObject_GetAttrString(obj, "value");
  4680. if (tmp == NULL) goto failed;
  4681. res = obj2ast_expr(tmp, &value, arena);
  4682. if (res != 0) goto failed;
  4683. Py_XDECREF(tmp);
  4684. tmp = NULL;
  4685. } else {
  4686. value = NULL;
  4687. }
  4688. *out = Yield(value, lineno, col_offset, arena);
  4689. if (*out == NULL) goto failed;
  4690. return 0;
  4691. }
  4692. if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
  4693. expr_ty left;
  4694. asdl_int_seq* ops;
  4695. asdl_seq* comparators;
  4696. if (PyObject_HasAttrString(obj, "left")) {
  4697. int res;
  4698. tmp = PyObject_GetAttrString(obj, "left");
  4699. if (tmp == NULL) goto failed;
  4700. res = obj2ast_expr(tmp, &left, arena);
  4701. if (res != 0) goto failed;
  4702. Py_XDECREF(tmp);
  4703. tmp = NULL;
  4704. } else {
  4705. PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
  4706. return 1;
  4707. }
  4708. if (PyObject_HasAttrString(obj, "ops")) {
  4709. int res;
  4710. Py_ssize_t len;
  4711. Py_ssize_t i;
  4712. tmp = PyObject_GetAttrString(obj, "ops");
  4713. if (tmp == NULL) goto failed;
  4714. if (!PyList_Check(tmp)) {
  4715. PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4716. goto failed;
  4717. }
  4718. len = PyList_GET_SIZE(tmp);
  4719. ops = asdl_int_seq_new(len, arena);
  4720. if (ops == NULL) goto failed;
  4721. for (i = 0; i < len; i++) {
  4722. cmpop_ty value;
  4723. res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
  4724. if (res != 0) goto failed;
  4725. asdl_seq_SET(ops, i, value);
  4726. }
  4727. Py_XDECREF(tmp);
  4728. tmp = NULL;
  4729. } else {
  4730. PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
  4731. return 1;
  4732. }
  4733. if (PyObject_HasAttrString(obj, "comparators")) {
  4734. int res;
  4735. Py_ssize_t len;
  4736. Py_ssize_t i;
  4737. tmp = PyObject_GetAttrString(obj, "comparators");
  4738. if (tmp == NULL) goto failed;
  4739. if (!PyList_Check(tmp)) {
  4740. PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4741. goto failed;
  4742. }
  4743. len = PyList_GET_SIZE(tmp);
  4744. comparators = asdl_seq_new(len, arena);
  4745. if (comparators == NULL) goto failed;
  4746. for (i = 0; i < len; i++) {
  4747. expr_ty value;
  4748. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  4749. if (res != 0) goto failed;
  4750. asdl_seq_SET(comparators, i, value);
  4751. }
  4752. Py_XDECREF(tmp);
  4753. tmp = NULL;
  4754. } else {
  4755. PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
  4756. return 1;
  4757. }
  4758. *out = Compare(left, ops, comparators, lineno, col_offset,
  4759. arena);
  4760. if (*out == NULL) goto failed;
  4761. return 0;
  4762. }
  4763. if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
  4764. expr_ty func;
  4765. asdl_seq* args;
  4766. asdl_seq* keywords;
  4767. expr_ty starargs;
  4768. expr_ty kwargs;
  4769. if (PyObject_HasAttrString(obj, "func")) {
  4770. int res;
  4771. tmp = PyObject_GetAttrString(obj, "func");
  4772. if (tmp == NULL) goto failed;
  4773. res = obj2ast_expr(tmp, &func, arena);
  4774. if (res != 0) goto failed;
  4775. Py_XDECREF(tmp);
  4776. tmp = NULL;
  4777. } else {
  4778. PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
  4779. return 1;
  4780. }
  4781. if (PyObject_HasAttrString(obj, "args")) {
  4782. int res;
  4783. Py_ssize_t len;
  4784. Py_ssize_t i;
  4785. tmp = PyObject_GetAttrString(obj, "args");
  4786. if (tmp == NULL) goto failed;
  4787. if (!PyList_Check(tmp)) {
  4788. PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4789. goto failed;
  4790. }
  4791. len = PyList_GET_SIZE(tmp);
  4792. args = asdl_seq_new(len, arena);
  4793. if (args == NULL) goto failed;
  4794. for (i = 0; i < len; i++) {
  4795. expr_ty value;
  4796. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  4797. if (res != 0) goto failed;
  4798. asdl_seq_SET(args, i, value);
  4799. }
  4800. Py_XDECREF(tmp);
  4801. tmp = NULL;
  4802. } else {
  4803. PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
  4804. return 1;
  4805. }
  4806. if (PyObject_HasAttrString(obj, "keywords")) {
  4807. int res;
  4808. Py_ssize_t len;
  4809. Py_ssize_t i;
  4810. tmp = PyObject_GetAttrString(obj, "keywords");
  4811. if (tmp == NULL) goto failed;
  4812. if (!PyList_Check(tmp)) {
  4813. PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  4814. goto failed;
  4815. }
  4816. len = PyList_GET_SIZE(tmp);
  4817. keywords = asdl_seq_new(len, arena);
  4818. if (keywords == NULL) goto failed;
  4819. for (i = 0; i < len; i++) {
  4820. keyword_ty value;
  4821. res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
  4822. if (res != 0) goto failed;
  4823. asdl_seq_SET(keywords, i, value);
  4824. }
  4825. Py_XDECREF(tmp);
  4826. tmp = NULL;
  4827. } else {
  4828. PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
  4829. return 1;
  4830. }
  4831. if (PyObject_HasAttrString(obj, "starargs")) {
  4832. int res;
  4833. tmp = PyObject_GetAttrString(obj, "starargs");
  4834. if (tmp == NULL) goto failed;
  4835. res = obj2ast_expr(tmp, &starargs, arena);
  4836. if (res != 0) goto failed;
  4837. Py_XDECREF(tmp);
  4838. tmp = NULL;
  4839. } else {
  4840. starargs = NULL;
  4841. }
  4842. if (PyObject_HasAttrString(obj, "kwargs")) {
  4843. int res;
  4844. tmp = PyObject_GetAttrString(obj, "kwargs");
  4845. if (tmp == NULL) goto failed;
  4846. res = obj2ast_expr(tmp, &kwargs, arena);
  4847. if (res != 0) goto failed;
  4848. Py_XDECREF(tmp);
  4849. tmp = NULL;
  4850. } else {
  4851. kwargs = NULL;
  4852. }
  4853. *out = Call(func, args, keywords, starargs, kwargs, lineno,
  4854. col_offset, arena);
  4855. if (*out == NULL) goto failed;
  4856. return 0;
  4857. }
  4858. if (PyObject_IsInstance(obj, (PyObject*)Repr_type)) {
  4859. expr_ty value;
  4860. if (PyObject_HasAttrString(obj, "value")) {
  4861. int res;
  4862. tmp = PyObject_GetAttrString(obj, "value");
  4863. if (tmp == NULL) goto failed;
  4864. res = obj2ast_expr(tmp, &value, arena);
  4865. if (res != 0) goto failed;
  4866. Py_XDECREF(tmp);
  4867. tmp = NULL;
  4868. } else {
  4869. PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
  4870. return 1;
  4871. }
  4872. *out = Repr(value, lineno, col_offset, arena);
  4873. if (*out == NULL) goto failed;
  4874. return 0;
  4875. }
  4876. if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
  4877. object n;
  4878. if (PyObject_HasAttrString(obj, "n")) {
  4879. int res;
  4880. tmp = PyObject_GetAttrString(obj, "n");
  4881. if (tmp == NULL) goto failed;
  4882. res = obj2ast_object(tmp, &n, arena);
  4883. if (res != 0) goto failed;
  4884. Py_XDECREF(tmp);
  4885. tmp = NULL;
  4886. } else {
  4887. PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
  4888. return 1;
  4889. }
  4890. *out = Num(n, lineno, col_offset, arena);
  4891. if (*out == NULL) goto failed;
  4892. return 0;
  4893. }
  4894. if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
  4895. string s;
  4896. if (PyObject_HasAttrString(obj, "s")) {
  4897. int res;
  4898. tmp = PyObject_GetAttrString(obj, "s");
  4899. if (tmp == NULL) goto failed;
  4900. res = obj2ast_string(tmp, &s, arena);
  4901. if (res != 0) goto failed;
  4902. Py_XDECREF(tmp);
  4903. tmp = NULL;
  4904. } else {
  4905. PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
  4906. return 1;
  4907. }
  4908. *out = Str(s, lineno, col_offset, arena);
  4909. if (*out == NULL) goto failed;
  4910. return 0;
  4911. }
  4912. if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
  4913. expr_ty value;
  4914. identifier attr;
  4915. expr_context_ty ctx;
  4916. if (PyObject_HasAttrString(obj, "value")) {
  4917. int res;
  4918. tmp = PyObject_GetAttrString(obj, "value");
  4919. if (tmp == NULL) goto failed;
  4920. res = obj2ast_expr(tmp, &value, arena);
  4921. if (res != 0) goto failed;
  4922. Py_XDECREF(tmp);
  4923. tmp = NULL;
  4924. } else {
  4925. PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
  4926. return 1;
  4927. }
  4928. if (PyObject_HasAttrString(obj, "attr")) {
  4929. int res;
  4930. tmp = PyObject_GetAttrString(obj, "attr");
  4931. if (tmp == NULL) goto failed;
  4932. res = obj2ast_identifier(tmp, &attr, arena);
  4933. if (res != 0) goto failed;
  4934. Py_XDECREF(tmp);
  4935. tmp = NULL;
  4936. } else {
  4937. PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
  4938. return 1;
  4939. }
  4940. if (PyObject_HasAttrString(obj, "ctx")) {
  4941. int res;
  4942. tmp = PyObject_GetAttrString(obj, "ctx");
  4943. if (tmp == NULL) goto failed;
  4944. res = obj2ast_expr_context(tmp, &ctx, arena);
  4945. if (res != 0) goto failed;
  4946. Py_XDECREF(tmp);
  4947. tmp = NULL;
  4948. } else {
  4949. PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
  4950. return 1;
  4951. }
  4952. *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
  4953. if (*out == NULL) goto failed;
  4954. return 0;
  4955. }
  4956. if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
  4957. expr_ty value;
  4958. slice_ty slice;
  4959. expr_context_ty ctx;
  4960. if (PyObject_HasAttrString(obj, "value")) {
  4961. int res;
  4962. tmp = PyObject_GetAttrString(obj, "value");
  4963. if (tmp == NULL) goto failed;
  4964. res = obj2ast_expr(tmp, &value, arena);
  4965. if (res != 0) goto failed;
  4966. Py_XDECREF(tmp);
  4967. tmp = NULL;
  4968. } else {
  4969. PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
  4970. return 1;
  4971. }
  4972. if (PyObject_HasAttrString(obj, "slice")) {
  4973. int res;
  4974. tmp = PyObject_GetAttrString(obj, "slice");
  4975. if (tmp == NULL) goto failed;
  4976. res = obj2ast_slice(tmp, &slice, arena);
  4977. if (res != 0) goto failed;
  4978. Py_XDECREF(tmp);
  4979. tmp = NULL;
  4980. } else {
  4981. PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
  4982. return 1;
  4983. }
  4984. if (PyObject_HasAttrString(obj, "ctx")) {
  4985. int res;
  4986. tmp = PyObject_GetAttrString(obj, "ctx");
  4987. if (tmp == NULL) goto failed;
  4988. res = obj2ast_expr_context(tmp, &ctx, arena);
  4989. if (res != 0) goto failed;
  4990. Py_XDECREF(tmp);
  4991. tmp = NULL;
  4992. } else {
  4993. PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
  4994. return 1;
  4995. }
  4996. *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
  4997. if (*out == NULL) goto failed;
  4998. return 0;
  4999. }
  5000. if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
  5001. identifier id;
  5002. expr_context_ty ctx;
  5003. if (PyObject_HasAttrString(obj, "id")) {
  5004. int res;
  5005. tmp = PyObject_GetAttrString(obj, "id");
  5006. if (tmp == NULL) goto failed;
  5007. res = obj2ast_identifier(tmp, &id, arena);
  5008. if (res != 0) goto failed;
  5009. Py_XDECREF(tmp);
  5010. tmp = NULL;
  5011. } else {
  5012. PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
  5013. return 1;
  5014. }
  5015. if (PyObject_HasAttrString(obj, "ctx")) {
  5016. int res;
  5017. tmp = PyObject_GetAttrString(obj, "ctx");
  5018. if (tmp == NULL) goto failed;
  5019. res = obj2ast_expr_context(tmp, &ctx, arena);
  5020. if (res != 0) goto failed;
  5021. Py_XDECREF(tmp);
  5022. tmp = NULL;
  5023. } else {
  5024. PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
  5025. return 1;
  5026. }
  5027. *out = Name(id, ctx, lineno, col_offset, arena);
  5028. if (*out == NULL) goto failed;
  5029. return 0;
  5030. }
  5031. if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
  5032. asdl_seq* elts;
  5033. expr_context_ty ctx;
  5034. if (PyObject_HasAttrString(obj, "elts")) {
  5035. int res;
  5036. Py_ssize_t len;
  5037. Py_ssize_t i;
  5038. tmp = PyObject_GetAttrString(obj, "elts");
  5039. if (tmp == NULL) goto failed;
  5040. if (!PyList_Check(tmp)) {
  5041. PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  5042. goto failed;
  5043. }
  5044. len = PyList_GET_SIZE(tmp);
  5045. elts = asdl_seq_new(len, arena);
  5046. if (elts == NULL) goto failed;
  5047. for (i = 0; i < len; i++) {
  5048. expr_ty value;
  5049. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  5050. if (res != 0) goto failed;
  5051. asdl_seq_SET(elts, i, value);
  5052. }
  5053. Py_XDECREF(tmp);
  5054. tmp = NULL;
  5055. } else {
  5056. PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
  5057. return 1;
  5058. }
  5059. if (PyObject_HasAttrString(obj, "ctx")) {
  5060. int res;
  5061. tmp = PyObject_GetAttrString(obj, "ctx");
  5062. if (tmp == NULL) goto failed;
  5063. res = obj2ast_expr_context(tmp, &ctx, arena);
  5064. if (res != 0) goto failed;
  5065. Py_XDECREF(tmp);
  5066. tmp = NULL;
  5067. } else {
  5068. PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
  5069. return 1;
  5070. }
  5071. *out = List(elts, ctx, lineno, col_offset, arena);
  5072. if (*out == NULL) goto failed;
  5073. return 0;
  5074. }
  5075. if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
  5076. asdl_seq* elts;
  5077. expr_context_ty ctx;
  5078. if (PyObject_HasAttrString(obj, "elts")) {
  5079. int res;
  5080. Py_ssize_t len;
  5081. Py_ssize_t i;
  5082. tmp = PyObject_GetAttrString(obj, "elts");
  5083. if (tmp == NULL) goto failed;
  5084. if (!PyList_Check(tmp)) {
  5085. PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  5086. goto failed;
  5087. }
  5088. len = PyList_GET_SIZE(tmp);
  5089. elts = asdl_seq_new(len, arena);
  5090. if (elts == NULL) goto failed;
  5091. for (i = 0; i < len; i++) {
  5092. expr_ty value;
  5093. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  5094. if (res != 0) goto failed;
  5095. asdl_seq_SET(elts, i, value);
  5096. }
  5097. Py_XDECREF(tmp);
  5098. tmp = NULL;
  5099. } else {
  5100. PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
  5101. return 1;
  5102. }
  5103. if (PyObject_HasAttrString(obj, "ctx")) {
  5104. int res;
  5105. tmp = PyObject_GetAttrString(obj, "ctx");
  5106. if (tmp == NULL) goto failed;
  5107. res = obj2ast_expr_context(tmp, &ctx, arena);
  5108. if (res != 0) goto failed;
  5109. Py_XDECREF(tmp);
  5110. tmp = NULL;
  5111. } else {
  5112. PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
  5113. return 1;
  5114. }
  5115. *out = Tuple(elts, ctx, lineno, col_offset, arena);
  5116. if (*out == NULL) goto failed;
  5117. return 0;
  5118. }
  5119. tmp = PyObject_Repr(obj);
  5120. if (tmp == NULL) goto failed;
  5121. PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
  5122. failed:
  5123. Py_XDECREF(tmp);
  5124. return 1;
  5125. }
  5126. int
  5127. obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
  5128. {
  5129. PyObject* tmp = NULL;
  5130. if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
  5131. *out = Load;
  5132. return 0;
  5133. }
  5134. if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
  5135. *out = Store;
  5136. return 0;
  5137. }
  5138. if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
  5139. *out = Del;
  5140. return 0;
  5141. }
  5142. if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
  5143. *out = AugLoad;
  5144. return 0;
  5145. }
  5146. if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
  5147. *out = AugStore;
  5148. return 0;
  5149. }
  5150. if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
  5151. *out = Param;
  5152. return 0;
  5153. }
  5154. tmp = PyObject_Repr(obj);
  5155. if (tmp == NULL) goto failed;
  5156. PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
  5157. failed:
  5158. Py_XDECREF(tmp);
  5159. return 1;
  5160. }
  5161. int
  5162. obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
  5163. {
  5164. PyObject* tmp = NULL;
  5165. if (obj == Py_None) {
  5166. *out = NULL;
  5167. return 0;
  5168. }
  5169. if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
  5170. *out = Ellipsis(arena);
  5171. if (*out == NULL) goto failed;
  5172. return 0;
  5173. }
  5174. if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
  5175. expr_ty lower;
  5176. expr_ty upper;
  5177. expr_ty step;
  5178. if (PyObject_HasAttrString(obj, "lower")) {
  5179. int res;
  5180. tmp = PyObject_GetAttrString(obj, "lower");
  5181. if (tmp == NULL) goto failed;
  5182. res = obj2ast_expr(tmp, &lower, arena);
  5183. if (res != 0) goto failed;
  5184. Py_XDECREF(tmp);
  5185. tmp = NULL;
  5186. } else {
  5187. lower = NULL;
  5188. }
  5189. if (PyObject_HasAttrString(obj, "upper")) {
  5190. int res;
  5191. tmp = PyObject_GetAttrString(obj, "upper");
  5192. if (tmp == NULL) goto failed;
  5193. res = obj2ast_expr(tmp, &upper, arena);
  5194. if (res != 0) goto failed;
  5195. Py_XDECREF(tmp);
  5196. tmp = NULL;
  5197. } else {
  5198. upper = NULL;
  5199. }
  5200. if (PyObject_HasAttrString(obj, "step")) {
  5201. int res;
  5202. tmp = PyObject_GetAttrString(obj, "step");
  5203. if (tmp == NULL) goto failed;
  5204. res = obj2ast_expr(tmp, &step, arena);
  5205. if (res != 0) goto failed;
  5206. Py_XDECREF(tmp);
  5207. tmp = NULL;
  5208. } else {
  5209. step = NULL;
  5210. }
  5211. *out = Slice(lower, upper, step, arena);
  5212. if (*out == NULL) goto failed;
  5213. return 0;
  5214. }
  5215. if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
  5216. asdl_seq* dims;
  5217. if (PyObject_HasAttrString(obj, "dims")) {
  5218. int res;
  5219. Py_ssize_t len;
  5220. Py_ssize_t i;
  5221. tmp = PyObject_GetAttrString(obj, "dims");
  5222. if (tmp == NULL) goto failed;
  5223. if (!PyList_Check(tmp)) {
  5224. PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  5225. goto failed;
  5226. }
  5227. len = PyList_GET_SIZE(tmp);
  5228. dims = asdl_seq_new(len, arena);
  5229. if (dims == NULL) goto failed;
  5230. for (i = 0; i < len; i++) {
  5231. slice_ty value;
  5232. res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
  5233. if (res != 0) goto failed;
  5234. asdl_seq_SET(dims, i, value);
  5235. }
  5236. Py_XDECREF(tmp);
  5237. tmp = NULL;
  5238. } else {
  5239. PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
  5240. return 1;
  5241. }
  5242. *out = ExtSlice(dims, arena);
  5243. if (*out == NULL) goto failed;
  5244. return 0;
  5245. }
  5246. if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
  5247. expr_ty value;
  5248. if (PyObject_HasAttrString(obj, "value")) {
  5249. int res;
  5250. tmp = PyObject_GetAttrString(obj, "value");
  5251. if (tmp == NULL) goto failed;
  5252. res = obj2ast_expr(tmp, &value, arena);
  5253. if (res != 0) goto failed;
  5254. Py_XDECREF(tmp);
  5255. tmp = NULL;
  5256. } else {
  5257. PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
  5258. return 1;
  5259. }
  5260. *out = Index(value, arena);
  5261. if (*out == NULL) goto failed;
  5262. return 0;
  5263. }
  5264. tmp = PyObject_Repr(obj);
  5265. if (tmp == NULL) goto failed;
  5266. PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
  5267. failed:
  5268. Py_XDECREF(tmp);
  5269. return 1;
  5270. }
  5271. int
  5272. obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
  5273. {
  5274. PyObject* tmp = NULL;
  5275. if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
  5276. *out = And;
  5277. return 0;
  5278. }
  5279. if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
  5280. *out = Or;
  5281. return 0;
  5282. }
  5283. tmp = PyObject_Repr(obj);
  5284. if (tmp == NULL) goto failed;
  5285. PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
  5286. failed:
  5287. Py_XDECREF(tmp);
  5288. return 1;
  5289. }
  5290. int
  5291. obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
  5292. {
  5293. PyObject* tmp = NULL;
  5294. if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
  5295. *out = Add;
  5296. return 0;
  5297. }
  5298. if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
  5299. *out = Sub;
  5300. return 0;
  5301. }
  5302. if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
  5303. *out = Mult;
  5304. return 0;
  5305. }
  5306. if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
  5307. *out = Div;
  5308. return 0;
  5309. }
  5310. if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
  5311. *out = Mod;
  5312. return 0;
  5313. }
  5314. if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
  5315. *out = Pow;
  5316. return 0;
  5317. }
  5318. if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
  5319. *out = LShift;
  5320. return 0;
  5321. }
  5322. if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
  5323. *out = RShift;
  5324. return 0;
  5325. }
  5326. if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
  5327. *out = BitOr;
  5328. return 0;
  5329. }
  5330. if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
  5331. *out = BitXor;
  5332. return 0;
  5333. }
  5334. if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
  5335. *out = BitAnd;
  5336. return 0;
  5337. }
  5338. if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
  5339. *out = FloorDiv;
  5340. return 0;
  5341. }
  5342. tmp = PyObject_Repr(obj);
  5343. if (tmp == NULL) goto failed;
  5344. PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
  5345. failed:
  5346. Py_XDECREF(tmp);
  5347. return 1;
  5348. }
  5349. int
  5350. obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
  5351. {
  5352. PyObject* tmp = NULL;
  5353. if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
  5354. *out = Invert;
  5355. return 0;
  5356. }
  5357. if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
  5358. *out = Not;
  5359. return 0;
  5360. }
  5361. if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
  5362. *out = UAdd;
  5363. return 0;
  5364. }
  5365. if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
  5366. *out = USub;
  5367. return 0;
  5368. }
  5369. tmp = PyObject_Repr(obj);
  5370. if (tmp == NULL) goto failed;
  5371. PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
  5372. failed:
  5373. Py_XDECREF(tmp);
  5374. return 1;
  5375. }
  5376. int
  5377. obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
  5378. {
  5379. PyObject* tmp = NULL;
  5380. if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
  5381. *out = Eq;
  5382. return 0;
  5383. }
  5384. if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
  5385. *out = NotEq;
  5386. return 0;
  5387. }
  5388. if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
  5389. *out = Lt;
  5390. return 0;
  5391. }
  5392. if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
  5393. *out = LtE;
  5394. return 0;
  5395. }
  5396. if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
  5397. *out = Gt;
  5398. return 0;
  5399. }
  5400. if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
  5401. *out = GtE;
  5402. return 0;
  5403. }
  5404. if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
  5405. *out = Is;
  5406. return 0;
  5407. }
  5408. if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
  5409. *out = IsNot;
  5410. return 0;
  5411. }
  5412. if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
  5413. *out = In;
  5414. return 0;
  5415. }
  5416. if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
  5417. *out = NotIn;
  5418. return 0;
  5419. }
  5420. tmp = PyObject_Repr(obj);
  5421. if (tmp == NULL) goto failed;
  5422. PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
  5423. failed:
  5424. Py_XDECREF(tmp);
  5425. return 1;
  5426. }
  5427. int
  5428. obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
  5429. {
  5430. PyObject* tmp = NULL;
  5431. expr_ty target;
  5432. expr_ty iter;
  5433. asdl_seq* ifs;
  5434. if (PyObject_HasAttrString(obj, "target")) {
  5435. int res;
  5436. tmp = PyObject_GetAttrString(obj, "target");
  5437. if (tmp == NULL) goto failed;
  5438. res = obj2ast_expr(tmp, &target, arena);
  5439. if (res != 0) goto failed;
  5440. Py_XDECREF(tmp);
  5441. tmp = NULL;
  5442. } else {
  5443. PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
  5444. return 1;
  5445. }
  5446. if (PyObject_HasAttrString(obj, "iter")) {
  5447. int res;
  5448. tmp = PyObject_GetAttrString(obj, "iter");
  5449. if (tmp == NULL) goto failed;
  5450. res = obj2ast_expr(tmp, &iter, arena);
  5451. if (res != 0) goto failed;
  5452. Py_XDECREF(tmp);
  5453. tmp = NULL;
  5454. } else {
  5455. PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
  5456. return 1;
  5457. }
  5458. if (PyObject_HasAttrString(obj, "ifs")) {
  5459. int res;
  5460. Py_ssize_t len;
  5461. Py_ssize_t i;
  5462. tmp = PyObject_GetAttrString(obj, "ifs");
  5463. if (tmp == NULL) goto failed;
  5464. if (!PyList_Check(tmp)) {
  5465. PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  5466. goto failed;
  5467. }
  5468. len = PyList_GET_SIZE(tmp);
  5469. ifs = asdl_seq_new(len, arena);
  5470. if (ifs == NULL) goto failed;
  5471. for (i = 0; i < len; i++) {
  5472. expr_ty value;
  5473. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  5474. if (res != 0) goto failed;
  5475. asdl_seq_SET(ifs, i, value);
  5476. }
  5477. Py_XDECREF(tmp);
  5478. tmp = NULL;
  5479. } else {
  5480. PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
  5481. return 1;
  5482. }
  5483. *out = comprehension(target, iter, ifs, arena);
  5484. return 0;
  5485. failed:
  5486. Py_XDECREF(tmp);
  5487. return 1;
  5488. }
  5489. int
  5490. obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
  5491. {
  5492. PyObject* tmp = NULL;
  5493. int lineno;
  5494. int col_offset;
  5495. if (obj == Py_None) {
  5496. *out = NULL;
  5497. return 0;
  5498. }
  5499. if (PyObject_HasAttrString(obj, "lineno")) {
  5500. int res;
  5501. tmp = PyObject_GetAttrString(obj, "lineno");
  5502. if (tmp == NULL) goto failed;
  5503. res = obj2ast_int(tmp, &lineno, arena);
  5504. if (res != 0) goto failed;
  5505. Py_XDECREF(tmp);
  5506. tmp = NULL;
  5507. } else {
  5508. PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
  5509. return 1;
  5510. }
  5511. if (PyObject_HasAttrString(obj, "col_offset")) {
  5512. int res;
  5513. tmp = PyObject_GetAttrString(obj, "col_offset");
  5514. if (tmp == NULL) goto failed;
  5515. res = obj2ast_int(tmp, &col_offset, arena);
  5516. if (res != 0) goto failed;
  5517. Py_XDECREF(tmp);
  5518. tmp = NULL;
  5519. } else {
  5520. PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
  5521. return 1;
  5522. }
  5523. if (PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type)) {
  5524. expr_ty type;
  5525. expr_ty name;
  5526. asdl_seq* body;
  5527. if (PyObject_HasAttrString(obj, "type")) {
  5528. int res;
  5529. tmp = PyObject_GetAttrString(obj, "type");
  5530. if (tmp == NULL) goto failed;
  5531. res = obj2ast_expr(tmp, &type, arena);
  5532. if (res != 0) goto failed;
  5533. Py_XDECREF(tmp);
  5534. tmp = NULL;
  5535. } else {
  5536. type = NULL;
  5537. }
  5538. if (PyObject_HasAttrString(obj, "name")) {
  5539. int res;
  5540. tmp = PyObject_GetAttrString(obj, "name");
  5541. if (tmp == NULL) goto failed;
  5542. res = obj2ast_expr(tmp, &name, arena);
  5543. if (res != 0) goto failed;
  5544. Py_XDECREF(tmp);
  5545. tmp = NULL;
  5546. } else {
  5547. name = NULL;
  5548. }
  5549. if (PyObject_HasAttrString(obj, "body")) {
  5550. int res;
  5551. Py_ssize_t len;
  5552. Py_ssize_t i;
  5553. tmp = PyObject_GetAttrString(obj, "body");
  5554. if (tmp == NULL) goto failed;
  5555. if (!PyList_Check(tmp)) {
  5556. PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  5557. goto failed;
  5558. }
  5559. len = PyList_GET_SIZE(tmp);
  5560. body = asdl_seq_new(len, arena);
  5561. if (body == NULL) goto failed;
  5562. for (i = 0; i < len; i++) {
  5563. stmt_ty value;
  5564. res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
  5565. if (res != 0) goto failed;
  5566. asdl_seq_SET(body, i, value);
  5567. }
  5568. Py_XDECREF(tmp);
  5569. tmp = NULL;
  5570. } else {
  5571. PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
  5572. return 1;
  5573. }
  5574. *out = ExceptHandler(type, name, body, lineno, col_offset,
  5575. arena);
  5576. if (*out == NULL) goto failed;
  5577. return 0;
  5578. }
  5579. tmp = PyObject_Repr(obj);
  5580. if (tmp == NULL) goto failed;
  5581. PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
  5582. failed:
  5583. Py_XDECREF(tmp);
  5584. return 1;
  5585. }
  5586. int
  5587. obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
  5588. {
  5589. PyObject* tmp = NULL;
  5590. asdl_seq* args;
  5591. identifier vararg;
  5592. identifier kwarg;
  5593. asdl_seq* defaults;
  5594. if (PyObject_HasAttrString(obj, "args")) {
  5595. int res;
  5596. Py_ssize_t len;
  5597. Py_ssize_t i;
  5598. tmp = PyObject_GetAttrString(obj, "args");
  5599. if (tmp == NULL) goto failed;
  5600. if (!PyList_Check(tmp)) {
  5601. PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  5602. goto failed;
  5603. }
  5604. len = PyList_GET_SIZE(tmp);
  5605. args = asdl_seq_new(len, arena);
  5606. if (args == NULL) goto failed;
  5607. for (i = 0; i < len; i++) {
  5608. expr_ty value;
  5609. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  5610. if (res != 0) goto failed;
  5611. asdl_seq_SET(args, i, value);
  5612. }
  5613. Py_XDECREF(tmp);
  5614. tmp = NULL;
  5615. } else {
  5616. PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
  5617. return 1;
  5618. }
  5619. if (PyObject_HasAttrString(obj, "vararg")) {
  5620. int res;
  5621. tmp = PyObject_GetAttrString(obj, "vararg");
  5622. if (tmp == NULL) goto failed;
  5623. res = obj2ast_identifier(tmp, &vararg, arena);
  5624. if (res != 0) goto failed;
  5625. Py_XDECREF(tmp);
  5626. tmp = NULL;
  5627. } else {
  5628. vararg = NULL;
  5629. }
  5630. if (PyObject_HasAttrString(obj, "kwarg")) {
  5631. int res;
  5632. tmp = PyObject_GetAttrString(obj, "kwarg");
  5633. if (tmp == NULL) goto failed;
  5634. res = obj2ast_identifier(tmp, &kwarg, arena);
  5635. if (res != 0) goto failed;
  5636. Py_XDECREF(tmp);
  5637. tmp = NULL;
  5638. } else {
  5639. kwarg = NULL;
  5640. }
  5641. if (PyObject_HasAttrString(obj, "defaults")) {
  5642. int res;
  5643. Py_ssize_t len;
  5644. Py_ssize_t i;
  5645. tmp = PyObject_GetAttrString(obj, "defaults");
  5646. if (tmp == NULL) goto failed;
  5647. if (!PyList_Check(tmp)) {
  5648. PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
  5649. goto failed;
  5650. }
  5651. len = PyList_GET_SIZE(tmp);
  5652. defaults = asdl_seq_new(len, arena);
  5653. if (defaults == NULL) goto failed;
  5654. for (i = 0; i < len; i++) {
  5655. expr_ty value;
  5656. res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
  5657. if (res != 0) goto failed;
  5658. asdl_seq_SET(defaults, i, value);
  5659. }
  5660. Py_XDECREF(tmp);
  5661. tmp = NULL;
  5662. } else {
  5663. PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
  5664. return 1;
  5665. }
  5666. *out = arguments(args, vararg, kwarg, defaults, arena);
  5667. return 0;
  5668. failed:
  5669. Py_XDECREF(tmp);
  5670. return 1;
  5671. }
  5672. int
  5673. obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
  5674. {
  5675. PyObject* tmp = NULL;
  5676. identifier arg;
  5677. expr_ty value;
  5678. if (PyObject_HasAttrString(obj, "arg")) {
  5679. int res;
  5680. tmp = PyObject_GetAttrString(obj, "arg");
  5681. if (tmp == NULL) goto failed;
  5682. res = obj2ast_identifier(tmp, &arg, arena);
  5683. if (res != 0) goto failed;
  5684. Py_XDECREF(tmp);
  5685. tmp = NULL;
  5686. } else {
  5687. PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
  5688. return 1;
  5689. }
  5690. if (PyObject_HasAttrString(obj, "value")) {
  5691. int res;
  5692. tmp = PyObject_GetAttrString(obj, "value");
  5693. if (tmp == NULL) goto failed;
  5694. res = obj2ast_expr(tmp, &value, arena);
  5695. if (res != 0) goto failed;
  5696. Py_XDECREF(tmp);
  5697. tmp = NULL;
  5698. } else {
  5699. PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
  5700. return 1;
  5701. }
  5702. *out = keyword(arg, value, arena);
  5703. return 0;
  5704. failed:
  5705. Py_XDECREF(tmp);
  5706. return 1;
  5707. }
  5708. int
  5709. obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
  5710. {
  5711. PyObject* tmp = NULL;
  5712. identifier name;
  5713. identifier asname;
  5714. if (PyObject_HasAttrString(obj, "name")) {
  5715. int res;
  5716. tmp = PyObject_GetAttrString(obj, "name");
  5717. if (tmp == NULL) goto failed;
  5718. res = obj2ast_identifier(tmp, &name, arena);
  5719. if (res != 0) goto failed;
  5720. Py_XDECREF(tmp);
  5721. tmp = NULL;
  5722. } else {
  5723. PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
  5724. return 1;
  5725. }
  5726. if (PyObject_HasAttrString(obj, "asname")) {
  5727. int res;
  5728. tmp = PyObject_GetAttrString(obj, "asname");
  5729. if (tmp == NULL) goto failed;
  5730. res = obj2ast_identifier(tmp, &asname, arena);
  5731. if (res != 0) goto failed;
  5732. Py_XDECREF(tmp);
  5733. tmp = NULL;
  5734. } else {
  5735. asname = NULL;
  5736. }
  5737. *out = alias(name, asname, arena);
  5738. return 0;
  5739. failed:
  5740. Py_XDECREF(tmp);
  5741. return 1;
  5742. }
  5743. PyMODINIT_FUNC
  5744. init_ast(void)
  5745. {
  5746. PyObject *m, *d;
  5747. if (!init_types()) return;
  5748. m = Py_InitModule3("_ast", NULL, NULL);
  5749. if (!m) return;
  5750. d = PyModule_GetDict(m);
  5751. if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
  5752. if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
  5753. return;
  5754. if (PyModule_AddStringConstant(m, "__version__", "62047") < 0)
  5755. return;
  5756. if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
  5757. if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
  5758. return;
  5759. if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
  5760. < 0) return;
  5761. if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
  5762. 0) return;
  5763. if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
  5764. if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
  5765. if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
  5766. < 0) return;
  5767. if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
  5768. return;
  5769. if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
  5770. return;
  5771. if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
  5772. return;
  5773. if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
  5774. return;
  5775. if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
  5776. 0) return;
  5777. if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
  5778. if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
  5779. if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
  5780. if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
  5781. if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
  5782. if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
  5783. if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
  5784. 0) return;
  5785. if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
  5786. 0) return;
  5787. if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
  5788. return;
  5789. if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
  5790. return;
  5791. if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
  5792. 0) return;
  5793. if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
  5794. if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
  5795. return;
  5796. if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
  5797. if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
  5798. if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
  5799. if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
  5800. return;
  5801. if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
  5802. if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
  5803. return;
  5804. if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
  5805. if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
  5806. return;
  5807. if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
  5808. return;
  5809. if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
  5810. if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
  5811. if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
  5812. return;
  5813. if (PyDict_SetItemString(d, "GeneratorExp",
  5814. (PyObject*)GeneratorExp_type) < 0) return;
  5815. if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
  5816. if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
  5817. return;
  5818. if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
  5819. if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
  5820. if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
  5821. if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
  5822. if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
  5823. 0) return;
  5824. if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
  5825. 0) return;
  5826. if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
  5827. if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
  5828. if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
  5829. if (PyDict_SetItemString(d, "expr_context",
  5830. (PyObject*)expr_context_type) < 0) return;
  5831. if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
  5832. if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
  5833. if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
  5834. if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
  5835. return;
  5836. if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
  5837. return;
  5838. if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
  5839. if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
  5840. if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
  5841. return;
  5842. if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
  5843. if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
  5844. return;
  5845. if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
  5846. if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
  5847. return;
  5848. if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
  5849. if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
  5850. if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
  5851. return;
  5852. if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
  5853. if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
  5854. if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
  5855. if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
  5856. if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
  5857. if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
  5858. if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
  5859. return;
  5860. if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
  5861. return;
  5862. if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
  5863. if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
  5864. return;
  5865. if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
  5866. return;
  5867. if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
  5868. return;
  5869. if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
  5870. return;
  5871. if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
  5872. return;
  5873. if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
  5874. if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
  5875. if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
  5876. if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
  5877. if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
  5878. if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
  5879. if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
  5880. if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
  5881. if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
  5882. if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
  5883. if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
  5884. if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
  5885. if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
  5886. if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
  5887. if (PyDict_SetItemString(d, "comprehension",
  5888. (PyObject*)comprehension_type) < 0) return;
  5889. if (PyDict_SetItemString(d, "excepthandler",
  5890. (PyObject*)excepthandler_type) < 0) return;
  5891. if (PyDict_SetItemString(d, "ExceptHandler",
  5892. (PyObject*)ExceptHandler_type) < 0) return;
  5893. if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
  5894. 0) return;
  5895. if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
  5896. return;
  5897. if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
  5898. }
  5899. PyObject* PyAST_mod2obj(mod_ty t)
  5900. {
  5901. init_types();
  5902. return ast2obj_mod(t);
  5903. }
  5904. /* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
  5905. mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
  5906. {
  5907. mod_ty res;
  5908. PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
  5909. (PyObject*)Interactive_type};
  5910. char *req_name[] = {"Module", "Expression", "Interactive"};
  5911. assert(0 <= mode && mode <= 2);
  5912. init_types();
  5913. if (!PyObject_IsInstance(ast, req_type[mode])) {
  5914. PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
  5915. req_name[mode], Py_TYPE(ast)->tp_name);
  5916. return NULL;
  5917. }
  5918. if (obj2ast_mod(ast, &res, arena) != 0)
  5919. return NULL;
  5920. else
  5921. return res;
  5922. }
  5923. int PyAST_Check(PyObject* obj)
  5924. {
  5925. init_types();
  5926. return PyObject_IsInstance(obj, (PyObject*)&AST_type);
  5927. }