You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6780 lines
258 KiB

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