1 /* File automatically generated by Parser/asdl_c.py. */
2
3 #include <stddef.h>
4
5 #include "Python.h"
6 #include "Python-ast.h"
7 #include "structmember.h" // PyMemberDef
8
9 typedef struct {
10 int initialized;
11 PyObject *AST_type;
12 PyObject *Add_singleton;
13 PyObject *Add_type;
14 PyObject *And_singleton;
15 PyObject *And_type;
16 PyObject *AnnAssign_type;
17 PyObject *Assert_type;
18 PyObject *Assign_type;
19 PyObject *AsyncFor_type;
20 PyObject *AsyncFunctionDef_type;
21 PyObject *AsyncWith_type;
22 PyObject *Attribute_type;
23 PyObject *AugAssign_type;
24 PyObject *Await_type;
25 PyObject *BinOp_type;
26 PyObject *BitAnd_singleton;
27 PyObject *BitAnd_type;
28 PyObject *BitOr_singleton;
29 PyObject *BitOr_type;
30 PyObject *BitXor_singleton;
31 PyObject *BitXor_type;
32 PyObject *BoolOp_type;
33 PyObject *Break_type;
34 PyObject *Call_type;
35 PyObject *ClassDef_type;
36 PyObject *Compare_type;
37 PyObject *Constant_type;
38 PyObject *Continue_type;
39 PyObject *Del_singleton;
40 PyObject *Del_type;
41 PyObject *Delete_type;
42 PyObject *DictComp_type;
43 PyObject *Dict_type;
44 PyObject *Div_singleton;
45 PyObject *Div_type;
46 PyObject *Eq_singleton;
47 PyObject *Eq_type;
48 PyObject *ExceptHandler_type;
49 PyObject *Expr_type;
50 PyObject *Expression_type;
51 PyObject *FloorDiv_singleton;
52 PyObject *FloorDiv_type;
53 PyObject *For_type;
54 PyObject *FormattedValue_type;
55 PyObject *FunctionDef_type;
56 PyObject *FunctionType_type;
57 PyObject *GeneratorExp_type;
58 PyObject *Global_type;
59 PyObject *GtE_singleton;
60 PyObject *GtE_type;
61 PyObject *Gt_singleton;
62 PyObject *Gt_type;
63 PyObject *IfExp_type;
64 PyObject *If_type;
65 PyObject *ImportFrom_type;
66 PyObject *Import_type;
67 PyObject *In_singleton;
68 PyObject *In_type;
69 PyObject *Interactive_type;
70 PyObject *Invert_singleton;
71 PyObject *Invert_type;
72 PyObject *IsNot_singleton;
73 PyObject *IsNot_type;
74 PyObject *Is_singleton;
75 PyObject *Is_type;
76 PyObject *JoinedStr_type;
77 PyObject *LShift_singleton;
78 PyObject *LShift_type;
79 PyObject *Lambda_type;
80 PyObject *ListComp_type;
81 PyObject *List_type;
82 PyObject *Load_singleton;
83 PyObject *Load_type;
84 PyObject *LtE_singleton;
85 PyObject *LtE_type;
86 PyObject *Lt_singleton;
87 PyObject *Lt_type;
88 PyObject *MatMult_singleton;
89 PyObject *MatMult_type;
90 PyObject *Mod_singleton;
91 PyObject *Mod_type;
92 PyObject *Module_type;
93 PyObject *Mult_singleton;
94 PyObject *Mult_type;
95 PyObject *Name_type;
96 PyObject *NamedExpr_type;
97 PyObject *Nonlocal_type;
98 PyObject *NotEq_singleton;
99 PyObject *NotEq_type;
100 PyObject *NotIn_singleton;
101 PyObject *NotIn_type;
102 PyObject *Not_singleton;
103 PyObject *Not_type;
104 PyObject *Or_singleton;
105 PyObject *Or_type;
106 PyObject *Pass_type;
107 PyObject *Pow_singleton;
108 PyObject *Pow_type;
109 PyObject *RShift_singleton;
110 PyObject *RShift_type;
111 PyObject *Raise_type;
112 PyObject *Return_type;
113 PyObject *SetComp_type;
114 PyObject *Set_type;
115 PyObject *Slice_type;
116 PyObject *Starred_type;
117 PyObject *Store_singleton;
118 PyObject *Store_type;
119 PyObject *Sub_singleton;
120 PyObject *Sub_type;
121 PyObject *Subscript_type;
122 PyObject *Try_type;
123 PyObject *Tuple_type;
124 PyObject *TypeIgnore_type;
125 PyObject *UAdd_singleton;
126 PyObject *UAdd_type;
127 PyObject *USub_singleton;
128 PyObject *USub_type;
129 PyObject *UnaryOp_type;
130 PyObject *While_type;
131 PyObject *With_type;
132 PyObject *YieldFrom_type;
133 PyObject *Yield_type;
134 PyObject *__dict__;
135 PyObject *__doc__;
136 PyObject *__module__;
137 PyObject *_attributes;
138 PyObject *_fields;
139 PyObject *alias_type;
140 PyObject *annotation;
141 PyObject *arg;
142 PyObject *arg_type;
143 PyObject *args;
144 PyObject *argtypes;
145 PyObject *arguments_type;
146 PyObject *asname;
147 PyObject *ast;
148 PyObject *attr;
149 PyObject *bases;
150 PyObject *body;
151 PyObject *boolop_type;
152 PyObject *cause;
153 PyObject *cmpop_type;
154 PyObject *col_offset;
155 PyObject *comparators;
156 PyObject *comprehension_type;
157 PyObject *context_expr;
158 PyObject *conversion;
159 PyObject *ctx;
160 PyObject *decorator_list;
161 PyObject *defaults;
162 PyObject *elt;
163 PyObject *elts;
164 PyObject *end_col_offset;
165 PyObject *end_lineno;
166 PyObject *exc;
167 PyObject *excepthandler_type;
168 PyObject *expr_context_type;
169 PyObject *expr_type;
170 PyObject *finalbody;
171 PyObject *format_spec;
172 PyObject *func;
173 PyObject *generators;
174 PyObject *handlers;
175 PyObject *id;
176 PyObject *ifs;
177 PyObject *is_async;
178 PyObject *items;
179 PyObject *iter;
180 PyObject *key;
181 PyObject *keys;
182 PyObject *keyword_type;
183 PyObject *keywords;
184 PyObject *kind;
185 PyObject *kw_defaults;
186 PyObject *kwarg;
187 PyObject *kwonlyargs;
188 PyObject *left;
189 PyObject *level;
190 PyObject *lineno;
191 PyObject *lower;
192 PyObject *mod_type;
193 PyObject *module;
194 PyObject *msg;
195 PyObject *name;
196 PyObject *names;
197 PyObject *op;
198 PyObject *operand;
199 PyObject *operator_type;
200 PyObject *ops;
201 PyObject *optional_vars;
202 PyObject *orelse;
203 PyObject *posonlyargs;
204 PyObject *returns;
205 PyObject *right;
206 PyObject *simple;
207 PyObject *slice;
208 PyObject *step;
209 PyObject *stmt_type;
210 PyObject *tag;
211 PyObject *target;
212 PyObject *targets;
213 PyObject *test;
214 PyObject *type;
215 PyObject *type_comment;
216 PyObject *type_ignore_type;
217 PyObject *type_ignores;
218 PyObject *unaryop_type;
219 PyObject *upper;
220 PyObject *value;
221 PyObject *values;
222 PyObject *vararg;
223 PyObject *withitem_type;
224 } astmodulestate;
225
226
227 // Forward declaration
228 static int init_types(astmodulestate *state);
229
230 // bpo-41194, bpo-41261, bpo-41631: The _ast module uses a global state.
231 static astmodulestate global_ast_state = {0};
232
233 static astmodulestate*
get_global_ast_state(void)234 get_global_ast_state(void)
235 {
236 astmodulestate* state = &global_ast_state;
237 if (!init_types(state)) {
238 return NULL;
239 }
240 return state;
241 }
242
243 static astmodulestate*
get_ast_state(PyObject * Py_UNUSED (module))244 get_ast_state(PyObject* Py_UNUSED(module))
245 {
246 astmodulestate* state = get_global_ast_state();
247 // get_ast_state() must only be called after _ast module is imported,
248 // and astmodule_exec() calls init_types()
249 assert(state != NULL);
250 return state;
251 }
252
_PyAST_Fini()253 void _PyAST_Fini()
254 {
255 astmodulestate* state = &global_ast_state;
256 Py_CLEAR(state->AST_type);
257 Py_CLEAR(state->Add_singleton);
258 Py_CLEAR(state->Add_type);
259 Py_CLEAR(state->And_singleton);
260 Py_CLEAR(state->And_type);
261 Py_CLEAR(state->AnnAssign_type);
262 Py_CLEAR(state->Assert_type);
263 Py_CLEAR(state->Assign_type);
264 Py_CLEAR(state->AsyncFor_type);
265 Py_CLEAR(state->AsyncFunctionDef_type);
266 Py_CLEAR(state->AsyncWith_type);
267 Py_CLEAR(state->Attribute_type);
268 Py_CLEAR(state->AugAssign_type);
269 Py_CLEAR(state->Await_type);
270 Py_CLEAR(state->BinOp_type);
271 Py_CLEAR(state->BitAnd_singleton);
272 Py_CLEAR(state->BitAnd_type);
273 Py_CLEAR(state->BitOr_singleton);
274 Py_CLEAR(state->BitOr_type);
275 Py_CLEAR(state->BitXor_singleton);
276 Py_CLEAR(state->BitXor_type);
277 Py_CLEAR(state->BoolOp_type);
278 Py_CLEAR(state->Break_type);
279 Py_CLEAR(state->Call_type);
280 Py_CLEAR(state->ClassDef_type);
281 Py_CLEAR(state->Compare_type);
282 Py_CLEAR(state->Constant_type);
283 Py_CLEAR(state->Continue_type);
284 Py_CLEAR(state->Del_singleton);
285 Py_CLEAR(state->Del_type);
286 Py_CLEAR(state->Delete_type);
287 Py_CLEAR(state->DictComp_type);
288 Py_CLEAR(state->Dict_type);
289 Py_CLEAR(state->Div_singleton);
290 Py_CLEAR(state->Div_type);
291 Py_CLEAR(state->Eq_singleton);
292 Py_CLEAR(state->Eq_type);
293 Py_CLEAR(state->ExceptHandler_type);
294 Py_CLEAR(state->Expr_type);
295 Py_CLEAR(state->Expression_type);
296 Py_CLEAR(state->FloorDiv_singleton);
297 Py_CLEAR(state->FloorDiv_type);
298 Py_CLEAR(state->For_type);
299 Py_CLEAR(state->FormattedValue_type);
300 Py_CLEAR(state->FunctionDef_type);
301 Py_CLEAR(state->FunctionType_type);
302 Py_CLEAR(state->GeneratorExp_type);
303 Py_CLEAR(state->Global_type);
304 Py_CLEAR(state->GtE_singleton);
305 Py_CLEAR(state->GtE_type);
306 Py_CLEAR(state->Gt_singleton);
307 Py_CLEAR(state->Gt_type);
308 Py_CLEAR(state->IfExp_type);
309 Py_CLEAR(state->If_type);
310 Py_CLEAR(state->ImportFrom_type);
311 Py_CLEAR(state->Import_type);
312 Py_CLEAR(state->In_singleton);
313 Py_CLEAR(state->In_type);
314 Py_CLEAR(state->Interactive_type);
315 Py_CLEAR(state->Invert_singleton);
316 Py_CLEAR(state->Invert_type);
317 Py_CLEAR(state->IsNot_singleton);
318 Py_CLEAR(state->IsNot_type);
319 Py_CLEAR(state->Is_singleton);
320 Py_CLEAR(state->Is_type);
321 Py_CLEAR(state->JoinedStr_type);
322 Py_CLEAR(state->LShift_singleton);
323 Py_CLEAR(state->LShift_type);
324 Py_CLEAR(state->Lambda_type);
325 Py_CLEAR(state->ListComp_type);
326 Py_CLEAR(state->List_type);
327 Py_CLEAR(state->Load_singleton);
328 Py_CLEAR(state->Load_type);
329 Py_CLEAR(state->LtE_singleton);
330 Py_CLEAR(state->LtE_type);
331 Py_CLEAR(state->Lt_singleton);
332 Py_CLEAR(state->Lt_type);
333 Py_CLEAR(state->MatMult_singleton);
334 Py_CLEAR(state->MatMult_type);
335 Py_CLEAR(state->Mod_singleton);
336 Py_CLEAR(state->Mod_type);
337 Py_CLEAR(state->Module_type);
338 Py_CLEAR(state->Mult_singleton);
339 Py_CLEAR(state->Mult_type);
340 Py_CLEAR(state->Name_type);
341 Py_CLEAR(state->NamedExpr_type);
342 Py_CLEAR(state->Nonlocal_type);
343 Py_CLEAR(state->NotEq_singleton);
344 Py_CLEAR(state->NotEq_type);
345 Py_CLEAR(state->NotIn_singleton);
346 Py_CLEAR(state->NotIn_type);
347 Py_CLEAR(state->Not_singleton);
348 Py_CLEAR(state->Not_type);
349 Py_CLEAR(state->Or_singleton);
350 Py_CLEAR(state->Or_type);
351 Py_CLEAR(state->Pass_type);
352 Py_CLEAR(state->Pow_singleton);
353 Py_CLEAR(state->Pow_type);
354 Py_CLEAR(state->RShift_singleton);
355 Py_CLEAR(state->RShift_type);
356 Py_CLEAR(state->Raise_type);
357 Py_CLEAR(state->Return_type);
358 Py_CLEAR(state->SetComp_type);
359 Py_CLEAR(state->Set_type);
360 Py_CLEAR(state->Slice_type);
361 Py_CLEAR(state->Starred_type);
362 Py_CLEAR(state->Store_singleton);
363 Py_CLEAR(state->Store_type);
364 Py_CLEAR(state->Sub_singleton);
365 Py_CLEAR(state->Sub_type);
366 Py_CLEAR(state->Subscript_type);
367 Py_CLEAR(state->Try_type);
368 Py_CLEAR(state->Tuple_type);
369 Py_CLEAR(state->TypeIgnore_type);
370 Py_CLEAR(state->UAdd_singleton);
371 Py_CLEAR(state->UAdd_type);
372 Py_CLEAR(state->USub_singleton);
373 Py_CLEAR(state->USub_type);
374 Py_CLEAR(state->UnaryOp_type);
375 Py_CLEAR(state->While_type);
376 Py_CLEAR(state->With_type);
377 Py_CLEAR(state->YieldFrom_type);
378 Py_CLEAR(state->Yield_type);
379 Py_CLEAR(state->__dict__);
380 Py_CLEAR(state->__doc__);
381 Py_CLEAR(state->__module__);
382 Py_CLEAR(state->_attributes);
383 Py_CLEAR(state->_fields);
384 Py_CLEAR(state->alias_type);
385 Py_CLEAR(state->annotation);
386 Py_CLEAR(state->arg);
387 Py_CLEAR(state->arg_type);
388 Py_CLEAR(state->args);
389 Py_CLEAR(state->argtypes);
390 Py_CLEAR(state->arguments_type);
391 Py_CLEAR(state->asname);
392 Py_CLEAR(state->ast);
393 Py_CLEAR(state->attr);
394 Py_CLEAR(state->bases);
395 Py_CLEAR(state->body);
396 Py_CLEAR(state->boolop_type);
397 Py_CLEAR(state->cause);
398 Py_CLEAR(state->cmpop_type);
399 Py_CLEAR(state->col_offset);
400 Py_CLEAR(state->comparators);
401 Py_CLEAR(state->comprehension_type);
402 Py_CLEAR(state->context_expr);
403 Py_CLEAR(state->conversion);
404 Py_CLEAR(state->ctx);
405 Py_CLEAR(state->decorator_list);
406 Py_CLEAR(state->defaults);
407 Py_CLEAR(state->elt);
408 Py_CLEAR(state->elts);
409 Py_CLEAR(state->end_col_offset);
410 Py_CLEAR(state->end_lineno);
411 Py_CLEAR(state->exc);
412 Py_CLEAR(state->excepthandler_type);
413 Py_CLEAR(state->expr_context_type);
414 Py_CLEAR(state->expr_type);
415 Py_CLEAR(state->finalbody);
416 Py_CLEAR(state->format_spec);
417 Py_CLEAR(state->func);
418 Py_CLEAR(state->generators);
419 Py_CLEAR(state->handlers);
420 Py_CLEAR(state->id);
421 Py_CLEAR(state->ifs);
422 Py_CLEAR(state->is_async);
423 Py_CLEAR(state->items);
424 Py_CLEAR(state->iter);
425 Py_CLEAR(state->key);
426 Py_CLEAR(state->keys);
427 Py_CLEAR(state->keyword_type);
428 Py_CLEAR(state->keywords);
429 Py_CLEAR(state->kind);
430 Py_CLEAR(state->kw_defaults);
431 Py_CLEAR(state->kwarg);
432 Py_CLEAR(state->kwonlyargs);
433 Py_CLEAR(state->left);
434 Py_CLEAR(state->level);
435 Py_CLEAR(state->lineno);
436 Py_CLEAR(state->lower);
437 Py_CLEAR(state->mod_type);
438 Py_CLEAR(state->module);
439 Py_CLEAR(state->msg);
440 Py_CLEAR(state->name);
441 Py_CLEAR(state->names);
442 Py_CLEAR(state->op);
443 Py_CLEAR(state->operand);
444 Py_CLEAR(state->operator_type);
445 Py_CLEAR(state->ops);
446 Py_CLEAR(state->optional_vars);
447 Py_CLEAR(state->orelse);
448 Py_CLEAR(state->posonlyargs);
449 Py_CLEAR(state->returns);
450 Py_CLEAR(state->right);
451 Py_CLEAR(state->simple);
452 Py_CLEAR(state->slice);
453 Py_CLEAR(state->step);
454 Py_CLEAR(state->stmt_type);
455 Py_CLEAR(state->tag);
456 Py_CLEAR(state->target);
457 Py_CLEAR(state->targets);
458 Py_CLEAR(state->test);
459 Py_CLEAR(state->type);
460 Py_CLEAR(state->type_comment);
461 Py_CLEAR(state->type_ignore_type);
462 Py_CLEAR(state->type_ignores);
463 Py_CLEAR(state->unaryop_type);
464 Py_CLEAR(state->upper);
465 Py_CLEAR(state->value);
466 Py_CLEAR(state->values);
467 Py_CLEAR(state->vararg);
468 Py_CLEAR(state->withitem_type);
469
470 state->initialized = 0;
471 }
472
init_identifiers(astmodulestate * state)473 static int init_identifiers(astmodulestate *state)
474 {
475 if ((state->__dict__ = PyUnicode_InternFromString("__dict__")) == NULL) return 0;
476 if ((state->__doc__ = PyUnicode_InternFromString("__doc__")) == NULL) return 0;
477 if ((state->__module__ = PyUnicode_InternFromString("__module__")) == NULL) return 0;
478 if ((state->_attributes = PyUnicode_InternFromString("_attributes")) == NULL) return 0;
479 if ((state->_fields = PyUnicode_InternFromString("_fields")) == NULL) return 0;
480 if ((state->annotation = PyUnicode_InternFromString("annotation")) == NULL) return 0;
481 if ((state->arg = PyUnicode_InternFromString("arg")) == NULL) return 0;
482 if ((state->args = PyUnicode_InternFromString("args")) == NULL) return 0;
483 if ((state->argtypes = PyUnicode_InternFromString("argtypes")) == NULL) return 0;
484 if ((state->asname = PyUnicode_InternFromString("asname")) == NULL) return 0;
485 if ((state->ast = PyUnicode_InternFromString("ast")) == NULL) return 0;
486 if ((state->attr = PyUnicode_InternFromString("attr")) == NULL) return 0;
487 if ((state->bases = PyUnicode_InternFromString("bases")) == NULL) return 0;
488 if ((state->body = PyUnicode_InternFromString("body")) == NULL) return 0;
489 if ((state->cause = PyUnicode_InternFromString("cause")) == NULL) return 0;
490 if ((state->col_offset = PyUnicode_InternFromString("col_offset")) == NULL) return 0;
491 if ((state->comparators = PyUnicode_InternFromString("comparators")) == NULL) return 0;
492 if ((state->context_expr = PyUnicode_InternFromString("context_expr")) == NULL) return 0;
493 if ((state->conversion = PyUnicode_InternFromString("conversion")) == NULL) return 0;
494 if ((state->ctx = PyUnicode_InternFromString("ctx")) == NULL) return 0;
495 if ((state->decorator_list = PyUnicode_InternFromString("decorator_list")) == NULL) return 0;
496 if ((state->defaults = PyUnicode_InternFromString("defaults")) == NULL) return 0;
497 if ((state->elt = PyUnicode_InternFromString("elt")) == NULL) return 0;
498 if ((state->elts = PyUnicode_InternFromString("elts")) == NULL) return 0;
499 if ((state->end_col_offset = PyUnicode_InternFromString("end_col_offset")) == NULL) return 0;
500 if ((state->end_lineno = PyUnicode_InternFromString("end_lineno")) == NULL) return 0;
501 if ((state->exc = PyUnicode_InternFromString("exc")) == NULL) return 0;
502 if ((state->finalbody = PyUnicode_InternFromString("finalbody")) == NULL) return 0;
503 if ((state->format_spec = PyUnicode_InternFromString("format_spec")) == NULL) return 0;
504 if ((state->func = PyUnicode_InternFromString("func")) == NULL) return 0;
505 if ((state->generators = PyUnicode_InternFromString("generators")) == NULL) return 0;
506 if ((state->handlers = PyUnicode_InternFromString("handlers")) == NULL) return 0;
507 if ((state->id = PyUnicode_InternFromString("id")) == NULL) return 0;
508 if ((state->ifs = PyUnicode_InternFromString("ifs")) == NULL) return 0;
509 if ((state->is_async = PyUnicode_InternFromString("is_async")) == NULL) return 0;
510 if ((state->items = PyUnicode_InternFromString("items")) == NULL) return 0;
511 if ((state->iter = PyUnicode_InternFromString("iter")) == NULL) return 0;
512 if ((state->key = PyUnicode_InternFromString("key")) == NULL) return 0;
513 if ((state->keys = PyUnicode_InternFromString("keys")) == NULL) return 0;
514 if ((state->keywords = PyUnicode_InternFromString("keywords")) == NULL) return 0;
515 if ((state->kind = PyUnicode_InternFromString("kind")) == NULL) return 0;
516 if ((state->kw_defaults = PyUnicode_InternFromString("kw_defaults")) == NULL) return 0;
517 if ((state->kwarg = PyUnicode_InternFromString("kwarg")) == NULL) return 0;
518 if ((state->kwonlyargs = PyUnicode_InternFromString("kwonlyargs")) == NULL) return 0;
519 if ((state->left = PyUnicode_InternFromString("left")) == NULL) return 0;
520 if ((state->level = PyUnicode_InternFromString("level")) == NULL) return 0;
521 if ((state->lineno = PyUnicode_InternFromString("lineno")) == NULL) return 0;
522 if ((state->lower = PyUnicode_InternFromString("lower")) == NULL) return 0;
523 if ((state->module = PyUnicode_InternFromString("module")) == NULL) return 0;
524 if ((state->msg = PyUnicode_InternFromString("msg")) == NULL) return 0;
525 if ((state->name = PyUnicode_InternFromString("name")) == NULL) return 0;
526 if ((state->names = PyUnicode_InternFromString("names")) == NULL) return 0;
527 if ((state->op = PyUnicode_InternFromString("op")) == NULL) return 0;
528 if ((state->operand = PyUnicode_InternFromString("operand")) == NULL) return 0;
529 if ((state->ops = PyUnicode_InternFromString("ops")) == NULL) return 0;
530 if ((state->optional_vars = PyUnicode_InternFromString("optional_vars")) == NULL) return 0;
531 if ((state->orelse = PyUnicode_InternFromString("orelse")) == NULL) return 0;
532 if ((state->posonlyargs = PyUnicode_InternFromString("posonlyargs")) == NULL) return 0;
533 if ((state->returns = PyUnicode_InternFromString("returns")) == NULL) return 0;
534 if ((state->right = PyUnicode_InternFromString("right")) == NULL) return 0;
535 if ((state->simple = PyUnicode_InternFromString("simple")) == NULL) return 0;
536 if ((state->slice = PyUnicode_InternFromString("slice")) == NULL) return 0;
537 if ((state->step = PyUnicode_InternFromString("step")) == NULL) return 0;
538 if ((state->tag = PyUnicode_InternFromString("tag")) == NULL) return 0;
539 if ((state->target = PyUnicode_InternFromString("target")) == NULL) return 0;
540 if ((state->targets = PyUnicode_InternFromString("targets")) == NULL) return 0;
541 if ((state->test = PyUnicode_InternFromString("test")) == NULL) return 0;
542 if ((state->type = PyUnicode_InternFromString("type")) == NULL) return 0;
543 if ((state->type_comment = PyUnicode_InternFromString("type_comment")) == NULL) return 0;
544 if ((state->type_ignores = PyUnicode_InternFromString("type_ignores")) == NULL) return 0;
545 if ((state->upper = PyUnicode_InternFromString("upper")) == NULL) return 0;
546 if ((state->value = PyUnicode_InternFromString("value")) == NULL) return 0;
547 if ((state->values = PyUnicode_InternFromString("values")) == NULL) return 0;
548 if ((state->vararg = PyUnicode_InternFromString("vararg")) == NULL) return 0;
549 return 1;
550 };
551
552 static PyObject* ast2obj_mod(astmodulestate *state, void*);
553 static const char * const Module_fields[]={
554 "body",
555 "type_ignores",
556 };
557 static const char * const Interactive_fields[]={
558 "body",
559 };
560 static const char * const Expression_fields[]={
561 "body",
562 };
563 static const char * const FunctionType_fields[]={
564 "argtypes",
565 "returns",
566 };
567 static const char * const stmt_attributes[] = {
568 "lineno",
569 "col_offset",
570 "end_lineno",
571 "end_col_offset",
572 };
573 static PyObject* ast2obj_stmt(astmodulestate *state, void*);
574 static const char * const FunctionDef_fields[]={
575 "name",
576 "args",
577 "body",
578 "decorator_list",
579 "returns",
580 "type_comment",
581 };
582 static const char * const AsyncFunctionDef_fields[]={
583 "name",
584 "args",
585 "body",
586 "decorator_list",
587 "returns",
588 "type_comment",
589 };
590 static const char * const ClassDef_fields[]={
591 "name",
592 "bases",
593 "keywords",
594 "body",
595 "decorator_list",
596 };
597 static const char * const Return_fields[]={
598 "value",
599 };
600 static const char * const Delete_fields[]={
601 "targets",
602 };
603 static const char * const Assign_fields[]={
604 "targets",
605 "value",
606 "type_comment",
607 };
608 static const char * const AugAssign_fields[]={
609 "target",
610 "op",
611 "value",
612 };
613 static const char * const AnnAssign_fields[]={
614 "target",
615 "annotation",
616 "value",
617 "simple",
618 };
619 static const char * const For_fields[]={
620 "target",
621 "iter",
622 "body",
623 "orelse",
624 "type_comment",
625 };
626 static const char * const AsyncFor_fields[]={
627 "target",
628 "iter",
629 "body",
630 "orelse",
631 "type_comment",
632 };
633 static const char * const While_fields[]={
634 "test",
635 "body",
636 "orelse",
637 };
638 static const char * const If_fields[]={
639 "test",
640 "body",
641 "orelse",
642 };
643 static const char * const With_fields[]={
644 "items",
645 "body",
646 "type_comment",
647 };
648 static const char * const AsyncWith_fields[]={
649 "items",
650 "body",
651 "type_comment",
652 };
653 static const char * const Raise_fields[]={
654 "exc",
655 "cause",
656 };
657 static const char * const Try_fields[]={
658 "body",
659 "handlers",
660 "orelse",
661 "finalbody",
662 };
663 static const char * const Assert_fields[]={
664 "test",
665 "msg",
666 };
667 static const char * const Import_fields[]={
668 "names",
669 };
670 static const char * const ImportFrom_fields[]={
671 "module",
672 "names",
673 "level",
674 };
675 static const char * const Global_fields[]={
676 "names",
677 };
678 static const char * const Nonlocal_fields[]={
679 "names",
680 };
681 static const char * const Expr_fields[]={
682 "value",
683 };
684 static const char * const expr_attributes[] = {
685 "lineno",
686 "col_offset",
687 "end_lineno",
688 "end_col_offset",
689 };
690 static PyObject* ast2obj_expr(astmodulestate *state, void*);
691 static const char * const BoolOp_fields[]={
692 "op",
693 "values",
694 };
695 static const char * const NamedExpr_fields[]={
696 "target",
697 "value",
698 };
699 static const char * const BinOp_fields[]={
700 "left",
701 "op",
702 "right",
703 };
704 static const char * const UnaryOp_fields[]={
705 "op",
706 "operand",
707 };
708 static const char * const Lambda_fields[]={
709 "args",
710 "body",
711 };
712 static const char * const IfExp_fields[]={
713 "test",
714 "body",
715 "orelse",
716 };
717 static const char * const Dict_fields[]={
718 "keys",
719 "values",
720 };
721 static const char * const Set_fields[]={
722 "elts",
723 };
724 static const char * const ListComp_fields[]={
725 "elt",
726 "generators",
727 };
728 static const char * const SetComp_fields[]={
729 "elt",
730 "generators",
731 };
732 static const char * const DictComp_fields[]={
733 "key",
734 "value",
735 "generators",
736 };
737 static const char * const GeneratorExp_fields[]={
738 "elt",
739 "generators",
740 };
741 static const char * const Await_fields[]={
742 "value",
743 };
744 static const char * const Yield_fields[]={
745 "value",
746 };
747 static const char * const YieldFrom_fields[]={
748 "value",
749 };
750 static const char * const Compare_fields[]={
751 "left",
752 "ops",
753 "comparators",
754 };
755 static const char * const Call_fields[]={
756 "func",
757 "args",
758 "keywords",
759 };
760 static const char * const FormattedValue_fields[]={
761 "value",
762 "conversion",
763 "format_spec",
764 };
765 static const char * const JoinedStr_fields[]={
766 "values",
767 };
768 static const char * const Constant_fields[]={
769 "value",
770 "kind",
771 };
772 static const char * const Attribute_fields[]={
773 "value",
774 "attr",
775 "ctx",
776 };
777 static const char * const Subscript_fields[]={
778 "value",
779 "slice",
780 "ctx",
781 };
782 static const char * const Starred_fields[]={
783 "value",
784 "ctx",
785 };
786 static const char * const Name_fields[]={
787 "id",
788 "ctx",
789 };
790 static const char * const List_fields[]={
791 "elts",
792 "ctx",
793 };
794 static const char * const Tuple_fields[]={
795 "elts",
796 "ctx",
797 };
798 static const char * const Slice_fields[]={
799 "lower",
800 "upper",
801 "step",
802 };
803 static PyObject* ast2obj_expr_context(astmodulestate *state, expr_context_ty);
804 static PyObject* ast2obj_boolop(astmodulestate *state, boolop_ty);
805 static PyObject* ast2obj_operator(astmodulestate *state, operator_ty);
806 static PyObject* ast2obj_unaryop(astmodulestate *state, unaryop_ty);
807 static PyObject* ast2obj_cmpop(astmodulestate *state, cmpop_ty);
808 static PyObject* ast2obj_comprehension(astmodulestate *state, void*);
809 static const char * const comprehension_fields[]={
810 "target",
811 "iter",
812 "ifs",
813 "is_async",
814 };
815 static const char * const excepthandler_attributes[] = {
816 "lineno",
817 "col_offset",
818 "end_lineno",
819 "end_col_offset",
820 };
821 static PyObject* ast2obj_excepthandler(astmodulestate *state, void*);
822 static const char * const ExceptHandler_fields[]={
823 "type",
824 "name",
825 "body",
826 };
827 static PyObject* ast2obj_arguments(astmodulestate *state, void*);
828 static const char * const arguments_fields[]={
829 "posonlyargs",
830 "args",
831 "vararg",
832 "kwonlyargs",
833 "kw_defaults",
834 "kwarg",
835 "defaults",
836 };
837 static PyObject* ast2obj_arg(astmodulestate *state, void*);
838 static const char * const arg_attributes[] = {
839 "lineno",
840 "col_offset",
841 "end_lineno",
842 "end_col_offset",
843 };
844 static const char * const arg_fields[]={
845 "arg",
846 "annotation",
847 "type_comment",
848 };
849 static PyObject* ast2obj_keyword(astmodulestate *state, void*);
850 static const char * const keyword_attributes[] = {
851 "lineno",
852 "col_offset",
853 "end_lineno",
854 "end_col_offset",
855 };
856 static const char * const keyword_fields[]={
857 "arg",
858 "value",
859 };
860 static PyObject* ast2obj_alias(astmodulestate *state, void*);
861 static const char * const alias_fields[]={
862 "name",
863 "asname",
864 };
865 static PyObject* ast2obj_withitem(astmodulestate *state, void*);
866 static const char * const withitem_fields[]={
867 "context_expr",
868 "optional_vars",
869 };
870 static PyObject* ast2obj_type_ignore(astmodulestate *state, void*);
871 static const char * const TypeIgnore_fields[]={
872 "lineno",
873 "tag",
874 };
875
876
877
878 typedef struct {
879 PyObject_HEAD
880 PyObject *dict;
881 } AST_object;
882
883 static void
ast_dealloc(AST_object * self)884 ast_dealloc(AST_object *self)
885 {
886 /* bpo-31095: UnTrack is needed before calling any callbacks */
887 PyTypeObject *tp = Py_TYPE(self);
888 PyObject_GC_UnTrack(self);
889 Py_CLEAR(self->dict);
890 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
891 assert(free_func != NULL);
892 free_func(self);
893 Py_DECREF(tp);
894 }
895
896 static int
ast_traverse(AST_object * self,visitproc visit,void * arg)897 ast_traverse(AST_object *self, visitproc visit, void *arg)
898 {
899 Py_VISIT(Py_TYPE(self));
900 Py_VISIT(self->dict);
901 return 0;
902 }
903
904 static int
ast_clear(AST_object * self)905 ast_clear(AST_object *self)
906 {
907 Py_CLEAR(self->dict);
908 return 0;
909 }
910
911 static int
ast_type_init(PyObject * self,PyObject * args,PyObject * kw)912 ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
913 {
914 astmodulestate *state = get_global_ast_state();
915 if (state == NULL) {
916 return -1;
917 }
918
919 Py_ssize_t i, numfields = 0;
920 int res = -1;
921 PyObject *key, *value, *fields;
922 if (_PyObject_LookupAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
923 goto cleanup;
924 }
925 if (fields) {
926 numfields = PySequence_Size(fields);
927 if (numfields == -1) {
928 goto cleanup;
929 }
930 }
931
932 res = 0; /* if no error occurs, this stays 0 to the end */
933 if (numfields < PyTuple_GET_SIZE(args)) {
934 PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
935 "%zd positional argument%s",
936 _PyType_Name(Py_TYPE(self)),
937 numfields, numfields == 1 ? "" : "s");
938 res = -1;
939 goto cleanup;
940 }
941 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
942 /* cannot be reached when fields is NULL */
943 PyObject *name = PySequence_GetItem(fields, i);
944 if (!name) {
945 res = -1;
946 goto cleanup;
947 }
948 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
949 Py_DECREF(name);
950 if (res < 0) {
951 goto cleanup;
952 }
953 }
954 if (kw) {
955 i = 0; /* needed by PyDict_Next */
956 while (PyDict_Next(kw, &i, &key, &value)) {
957 int contains = PySequence_Contains(fields, key);
958 if (contains == -1) {
959 res = -1;
960 goto cleanup;
961 } else if (contains == 1) {
962 Py_ssize_t p = PySequence_Index(fields, key);
963 if (p == -1) {
964 res = -1;
965 goto cleanup;
966 }
967 if (p < PyTuple_GET_SIZE(args)) {
968 PyErr_Format(PyExc_TypeError,
969 "%.400s got multiple values for argument '%U'",
970 Py_TYPE(self)->tp_name, key);
971 res = -1;
972 goto cleanup;
973 }
974 }
975 res = PyObject_SetAttr(self, key, value);
976 if (res < 0) {
977 goto cleanup;
978 }
979 }
980 }
981 cleanup:
982 Py_XDECREF(fields);
983 return res;
984 }
985
986 /* Pickling support */
987 static PyObject *
ast_type_reduce(PyObject * self,PyObject * unused)988 ast_type_reduce(PyObject *self, PyObject *unused)
989 {
990 astmodulestate *state = get_global_ast_state();
991 if (state == NULL) {
992 return NULL;
993 }
994
995 PyObject *dict;
996 if (_PyObject_LookupAttr(self, state->__dict__, &dict) < 0) {
997 return NULL;
998 }
999 if (dict) {
1000 return Py_BuildValue("O()N", Py_TYPE(self), dict);
1001 }
1002 return Py_BuildValue("O()", Py_TYPE(self));
1003 }
1004
1005 static PyMemberDef ast_type_members[] = {
1006 {"__dictoffset__", T_PYSSIZET, offsetof(AST_object, dict), READONLY},
1007 {NULL} /* Sentinel */
1008 };
1009
1010 static PyMethodDef ast_type_methods[] = {
1011 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
1012 {NULL}
1013 };
1014
1015 static PyGetSetDef ast_type_getsets[] = {
1016 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
1017 {NULL}
1018 };
1019
1020 static PyType_Slot AST_type_slots[] = {
1021 {Py_tp_dealloc, ast_dealloc},
1022 {Py_tp_getattro, PyObject_GenericGetAttr},
1023 {Py_tp_setattro, PyObject_GenericSetAttr},
1024 {Py_tp_traverse, ast_traverse},
1025 {Py_tp_clear, ast_clear},
1026 {Py_tp_members, ast_type_members},
1027 {Py_tp_methods, ast_type_methods},
1028 {Py_tp_getset, ast_type_getsets},
1029 {Py_tp_init, ast_type_init},
1030 {Py_tp_alloc, PyType_GenericAlloc},
1031 {Py_tp_new, PyType_GenericNew},
1032 {Py_tp_free, PyObject_GC_Del},
1033 {0, 0},
1034 };
1035
1036 static PyType_Spec AST_type_spec = {
1037 "ast.AST",
1038 sizeof(AST_object),
1039 0,
1040 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1041 AST_type_slots
1042 };
1043
1044 static PyObject *
make_type(astmodulestate * state,const char * type,PyObject * base,const char * const * fields,int num_fields,const char * doc)1045 make_type(astmodulestate *state, const char *type, PyObject* base,
1046 const char* const* fields, int num_fields, const char *doc)
1047 {
1048 PyObject *fnames, *result;
1049 int i;
1050 fnames = PyTuple_New(num_fields);
1051 if (!fnames) return NULL;
1052 for (i = 0; i < num_fields; i++) {
1053 PyObject *field = PyUnicode_InternFromString(fields[i]);
1054 if (!field) {
1055 Py_DECREF(fnames);
1056 return NULL;
1057 }
1058 PyTuple_SET_ITEM(fnames, i, field);
1059 }
1060 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOOOs}",
1061 type, base,
1062 state->_fields, fnames,
1063 state->__module__,
1064 state->ast,
1065 state->__doc__, doc);
1066 Py_DECREF(fnames);
1067 return result;
1068 }
1069
1070 static int
add_attributes(astmodulestate * state,PyObject * type,const char * const * attrs,int num_fields)1071 add_attributes(astmodulestate *state, PyObject *type, const char * const *attrs, int num_fields)
1072 {
1073 int i, result;
1074 PyObject *s, *l = PyTuple_New(num_fields);
1075 if (!l)
1076 return 0;
1077 for (i = 0; i < num_fields; i++) {
1078 s = PyUnicode_InternFromString(attrs[i]);
1079 if (!s) {
1080 Py_DECREF(l);
1081 return 0;
1082 }
1083 PyTuple_SET_ITEM(l, i, s);
1084 }
1085 result = PyObject_SetAttr(type, state->_attributes, l) >= 0;
1086 Py_DECREF(l);
1087 return result;
1088 }
1089
1090 /* Conversion AST -> Python */
1091
ast2obj_list(astmodulestate * state,asdl_seq * seq,PyObject * (* func)(astmodulestate * state,void *))1092 static PyObject* ast2obj_list(astmodulestate *state, asdl_seq *seq, PyObject* (*func)(astmodulestate *state, void*))
1093 {
1094 Py_ssize_t i, n = asdl_seq_LEN(seq);
1095 PyObject *result = PyList_New(n);
1096 PyObject *value;
1097 if (!result)
1098 return NULL;
1099 for (i = 0; i < n; i++) {
1100 value = func(state, asdl_seq_GET(seq, i));
1101 if (!value) {
1102 Py_DECREF(result);
1103 return NULL;
1104 }
1105 PyList_SET_ITEM(result, i, value);
1106 }
1107 return result;
1108 }
1109
ast2obj_object(astmodulestate * Py_UNUSED (state),void * o)1110 static PyObject* ast2obj_object(astmodulestate *Py_UNUSED(state), void *o)
1111 {
1112 if (!o)
1113 o = Py_None;
1114 Py_INCREF((PyObject*)o);
1115 return (PyObject*)o;
1116 }
1117 #define ast2obj_constant ast2obj_object
1118 #define ast2obj_identifier ast2obj_object
1119 #define ast2obj_string ast2obj_object
1120
ast2obj_int(astmodulestate * Py_UNUSED (state),long b)1121 static PyObject* ast2obj_int(astmodulestate *Py_UNUSED(state), long b)
1122 {
1123 return PyLong_FromLong(b);
1124 }
1125
1126 /* Conversion Python -> AST */
1127
obj2ast_object(astmodulestate * Py_UNUSED (state),PyObject * obj,PyObject ** out,PyArena * arena)1128 static int obj2ast_object(astmodulestate *Py_UNUSED(state), PyObject* obj, PyObject** out, PyArena* arena)
1129 {
1130 if (obj == Py_None)
1131 obj = NULL;
1132 if (obj) {
1133 if (PyArena_AddPyObject(arena, obj) < 0) {
1134 *out = NULL;
1135 return -1;
1136 }
1137 Py_INCREF(obj);
1138 }
1139 *out = obj;
1140 return 0;
1141 }
1142
obj2ast_constant(astmodulestate * Py_UNUSED (state),PyObject * obj,PyObject ** out,PyArena * arena)1143 static int obj2ast_constant(astmodulestate *Py_UNUSED(state), PyObject* obj, PyObject** out, PyArena* arena)
1144 {
1145 if (PyArena_AddPyObject(arena, obj) < 0) {
1146 *out = NULL;
1147 return -1;
1148 }
1149 Py_INCREF(obj);
1150 *out = obj;
1151 return 0;
1152 }
1153
obj2ast_identifier(astmodulestate * state,PyObject * obj,PyObject ** out,PyArena * arena)1154 static int obj2ast_identifier(astmodulestate *state, PyObject* obj, PyObject** out, PyArena* arena)
1155 {
1156 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
1157 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
1158 return 1;
1159 }
1160 return obj2ast_object(state, obj, out, arena);
1161 }
1162
obj2ast_string(astmodulestate * state,PyObject * obj,PyObject ** out,PyArena * arena)1163 static int obj2ast_string(astmodulestate *state, PyObject* obj, PyObject** out, PyArena* arena)
1164 {
1165 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
1166 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
1167 return 1;
1168 }
1169 return obj2ast_object(state, obj, out, arena);
1170 }
1171
obj2ast_int(astmodulestate * Py_UNUSED (state),PyObject * obj,int * out,PyArena * arena)1172 static int obj2ast_int(astmodulestate* Py_UNUSED(state), PyObject* obj, int* out, PyArena* arena)
1173 {
1174 int i;
1175 if (!PyLong_Check(obj)) {
1176 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
1177 return 1;
1178 }
1179
1180 i = _PyLong_AsInt(obj);
1181 if (i == -1 && PyErr_Occurred())
1182 return 1;
1183 *out = i;
1184 return 0;
1185 }
1186
add_ast_fields(astmodulestate * state)1187 static int add_ast_fields(astmodulestate *state)
1188 {
1189 PyObject *empty_tuple;
1190 empty_tuple = PyTuple_New(0);
1191 if (!empty_tuple ||
1192 PyObject_SetAttrString(state->AST_type, "_fields", empty_tuple) < 0 ||
1193 PyObject_SetAttrString(state->AST_type, "_attributes", empty_tuple) < 0) {
1194 Py_XDECREF(empty_tuple);
1195 return -1;
1196 }
1197 Py_DECREF(empty_tuple);
1198 return 0;
1199 }
1200
1201
init_types(astmodulestate * state)1202 static int init_types(astmodulestate *state)
1203 {
1204 if (state->initialized) return 1;
1205 if (init_identifiers(state) < 0) return 0;
1206 state->AST_type = PyType_FromSpec(&AST_type_spec);
1207 if (!state->AST_type) return 0;
1208 if (add_ast_fields(state) < 0) return 0;
1209 state->mod_type = make_type(state, "mod", state->AST_type, NULL, 0,
1210 "mod = Module(stmt* body, type_ignore* type_ignores)\n"
1211 " | Interactive(stmt* body)\n"
1212 " | Expression(expr body)\n"
1213 " | FunctionType(expr* argtypes, expr returns)");
1214 if (!state->mod_type) return 0;
1215 if (!add_attributes(state, state->mod_type, NULL, 0)) return 0;
1216 state->Module_type = make_type(state, "Module", state->mod_type,
1217 Module_fields, 2,
1218 "Module(stmt* body, type_ignore* type_ignores)");
1219 if (!state->Module_type) return 0;
1220 state->Interactive_type = make_type(state, "Interactive", state->mod_type,
1221 Interactive_fields, 1,
1222 "Interactive(stmt* body)");
1223 if (!state->Interactive_type) return 0;
1224 state->Expression_type = make_type(state, "Expression", state->mod_type,
1225 Expression_fields, 1,
1226 "Expression(expr body)");
1227 if (!state->Expression_type) return 0;
1228 state->FunctionType_type = make_type(state, "FunctionType",
1229 state->mod_type, FunctionType_fields,
1230 2,
1231 "FunctionType(expr* argtypes, expr returns)");
1232 if (!state->FunctionType_type) return 0;
1233 state->stmt_type = make_type(state, "stmt", state->AST_type, NULL, 0,
1234 "stmt = FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns, string? type_comment)\n"
1235 " | AsyncFunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns, string? type_comment)\n"
1236 " | ClassDef(identifier name, expr* bases, keyword* keywords, stmt* body, expr* decorator_list)\n"
1237 " | Return(expr? value)\n"
1238 " | Delete(expr* targets)\n"
1239 " | Assign(expr* targets, expr value, string? type_comment)\n"
1240 " | AugAssign(expr target, operator op, expr value)\n"
1241 " | AnnAssign(expr target, expr annotation, expr? value, int simple)\n"
1242 " | For(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)\n"
1243 " | AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)\n"
1244 " | While(expr test, stmt* body, stmt* orelse)\n"
1245 " | If(expr test, stmt* body, stmt* orelse)\n"
1246 " | With(withitem* items, stmt* body, string? type_comment)\n"
1247 " | AsyncWith(withitem* items, stmt* body, string? type_comment)\n"
1248 " | Raise(expr? exc, expr? cause)\n"
1249 " | Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)\n"
1250 " | Assert(expr test, expr? msg)\n"
1251 " | Import(alias* names)\n"
1252 " | ImportFrom(identifier? module, alias* names, int? level)\n"
1253 " | Global(identifier* names)\n"
1254 " | Nonlocal(identifier* names)\n"
1255 " | Expr(expr value)\n"
1256 " | Pass\n"
1257 " | Break\n"
1258 " | Continue");
1259 if (!state->stmt_type) return 0;
1260 if (!add_attributes(state, state->stmt_type, stmt_attributes, 4)) return 0;
1261 if (PyObject_SetAttr(state->stmt_type, state->end_lineno, Py_None) == -1)
1262 return 0;
1263 if (PyObject_SetAttr(state->stmt_type, state->end_col_offset, Py_None) ==
1264 -1)
1265 return 0;
1266 state->FunctionDef_type = make_type(state, "FunctionDef", state->stmt_type,
1267 FunctionDef_fields, 6,
1268 "FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns, string? type_comment)");
1269 if (!state->FunctionDef_type) return 0;
1270 if (PyObject_SetAttr(state->FunctionDef_type, state->returns, Py_None) ==
1271 -1)
1272 return 0;
1273 if (PyObject_SetAttr(state->FunctionDef_type, state->type_comment, Py_None)
1274 == -1)
1275 return 0;
1276 state->AsyncFunctionDef_type = make_type(state, "AsyncFunctionDef",
1277 state->stmt_type,
1278 AsyncFunctionDef_fields, 6,
1279 "AsyncFunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns, string? type_comment)");
1280 if (!state->AsyncFunctionDef_type) return 0;
1281 if (PyObject_SetAttr(state->AsyncFunctionDef_type, state->returns, Py_None)
1282 == -1)
1283 return 0;
1284 if (PyObject_SetAttr(state->AsyncFunctionDef_type, state->type_comment,
1285 Py_None) == -1)
1286 return 0;
1287 state->ClassDef_type = make_type(state, "ClassDef", state->stmt_type,
1288 ClassDef_fields, 5,
1289 "ClassDef(identifier name, expr* bases, keyword* keywords, stmt* body, expr* decorator_list)");
1290 if (!state->ClassDef_type) return 0;
1291 state->Return_type = make_type(state, "Return", state->stmt_type,
1292 Return_fields, 1,
1293 "Return(expr? value)");
1294 if (!state->Return_type) return 0;
1295 if (PyObject_SetAttr(state->Return_type, state->value, Py_None) == -1)
1296 return 0;
1297 state->Delete_type = make_type(state, "Delete", state->stmt_type,
1298 Delete_fields, 1,
1299 "Delete(expr* targets)");
1300 if (!state->Delete_type) return 0;
1301 state->Assign_type = make_type(state, "Assign", state->stmt_type,
1302 Assign_fields, 3,
1303 "Assign(expr* targets, expr value, string? type_comment)");
1304 if (!state->Assign_type) return 0;
1305 if (PyObject_SetAttr(state->Assign_type, state->type_comment, Py_None) ==
1306 -1)
1307 return 0;
1308 state->AugAssign_type = make_type(state, "AugAssign", state->stmt_type,
1309 AugAssign_fields, 3,
1310 "AugAssign(expr target, operator op, expr value)");
1311 if (!state->AugAssign_type) return 0;
1312 state->AnnAssign_type = make_type(state, "AnnAssign", state->stmt_type,
1313 AnnAssign_fields, 4,
1314 "AnnAssign(expr target, expr annotation, expr? value, int simple)");
1315 if (!state->AnnAssign_type) return 0;
1316 if (PyObject_SetAttr(state->AnnAssign_type, state->value, Py_None) == -1)
1317 return 0;
1318 state->For_type = make_type(state, "For", state->stmt_type, For_fields, 5,
1319 "For(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)");
1320 if (!state->For_type) return 0;
1321 if (PyObject_SetAttr(state->For_type, state->type_comment, Py_None) == -1)
1322 return 0;
1323 state->AsyncFor_type = make_type(state, "AsyncFor", state->stmt_type,
1324 AsyncFor_fields, 5,
1325 "AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)");
1326 if (!state->AsyncFor_type) return 0;
1327 if (PyObject_SetAttr(state->AsyncFor_type, state->type_comment, Py_None) ==
1328 -1)
1329 return 0;
1330 state->While_type = make_type(state, "While", state->stmt_type,
1331 While_fields, 3,
1332 "While(expr test, stmt* body, stmt* orelse)");
1333 if (!state->While_type) return 0;
1334 state->If_type = make_type(state, "If", state->stmt_type, If_fields, 3,
1335 "If(expr test, stmt* body, stmt* orelse)");
1336 if (!state->If_type) return 0;
1337 state->With_type = make_type(state, "With", state->stmt_type, With_fields,
1338 3,
1339 "With(withitem* items, stmt* body, string? type_comment)");
1340 if (!state->With_type) return 0;
1341 if (PyObject_SetAttr(state->With_type, state->type_comment, Py_None) == -1)
1342 return 0;
1343 state->AsyncWith_type = make_type(state, "AsyncWith", state->stmt_type,
1344 AsyncWith_fields, 3,
1345 "AsyncWith(withitem* items, stmt* body, string? type_comment)");
1346 if (!state->AsyncWith_type) return 0;
1347 if (PyObject_SetAttr(state->AsyncWith_type, state->type_comment, Py_None)
1348 == -1)
1349 return 0;
1350 state->Raise_type = make_type(state, "Raise", state->stmt_type,
1351 Raise_fields, 2,
1352 "Raise(expr? exc, expr? cause)");
1353 if (!state->Raise_type) return 0;
1354 if (PyObject_SetAttr(state->Raise_type, state->exc, Py_None) == -1)
1355 return 0;
1356 if (PyObject_SetAttr(state->Raise_type, state->cause, Py_None) == -1)
1357 return 0;
1358 state->Try_type = make_type(state, "Try", state->stmt_type, Try_fields, 4,
1359 "Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)");
1360 if (!state->Try_type) return 0;
1361 state->Assert_type = make_type(state, "Assert", state->stmt_type,
1362 Assert_fields, 2,
1363 "Assert(expr test, expr? msg)");
1364 if (!state->Assert_type) return 0;
1365 if (PyObject_SetAttr(state->Assert_type, state->msg, Py_None) == -1)
1366 return 0;
1367 state->Import_type = make_type(state, "Import", state->stmt_type,
1368 Import_fields, 1,
1369 "Import(alias* names)");
1370 if (!state->Import_type) return 0;
1371 state->ImportFrom_type = make_type(state, "ImportFrom", state->stmt_type,
1372 ImportFrom_fields, 3,
1373 "ImportFrom(identifier? module, alias* names, int? level)");
1374 if (!state->ImportFrom_type) return 0;
1375 if (PyObject_SetAttr(state->ImportFrom_type, state->module, Py_None) == -1)
1376 return 0;
1377 if (PyObject_SetAttr(state->ImportFrom_type, state->level, Py_None) == -1)
1378 return 0;
1379 state->Global_type = make_type(state, "Global", state->stmt_type,
1380 Global_fields, 1,
1381 "Global(identifier* names)");
1382 if (!state->Global_type) return 0;
1383 state->Nonlocal_type = make_type(state, "Nonlocal", state->stmt_type,
1384 Nonlocal_fields, 1,
1385 "Nonlocal(identifier* names)");
1386 if (!state->Nonlocal_type) return 0;
1387 state->Expr_type = make_type(state, "Expr", state->stmt_type, Expr_fields,
1388 1,
1389 "Expr(expr value)");
1390 if (!state->Expr_type) return 0;
1391 state->Pass_type = make_type(state, "Pass", state->stmt_type, NULL, 0,
1392 "Pass");
1393 if (!state->Pass_type) return 0;
1394 state->Break_type = make_type(state, "Break", state->stmt_type, NULL, 0,
1395 "Break");
1396 if (!state->Break_type) return 0;
1397 state->Continue_type = make_type(state, "Continue", state->stmt_type, NULL,
1398 0,
1399 "Continue");
1400 if (!state->Continue_type) return 0;
1401 state->expr_type = make_type(state, "expr", state->AST_type, NULL, 0,
1402 "expr = BoolOp(boolop op, expr* values)\n"
1403 " | NamedExpr(expr target, expr value)\n"
1404 " | BinOp(expr left, operator op, expr right)\n"
1405 " | UnaryOp(unaryop op, expr operand)\n"
1406 " | Lambda(arguments args, expr body)\n"
1407 " | IfExp(expr test, expr body, expr orelse)\n"
1408 " | Dict(expr* keys, expr* values)\n"
1409 " | Set(expr* elts)\n"
1410 " | ListComp(expr elt, comprehension* generators)\n"
1411 " | SetComp(expr elt, comprehension* generators)\n"
1412 " | DictComp(expr key, expr value, comprehension* generators)\n"
1413 " | GeneratorExp(expr elt, comprehension* generators)\n"
1414 " | Await(expr value)\n"
1415 " | Yield(expr? value)\n"
1416 " | YieldFrom(expr value)\n"
1417 " | Compare(expr left, cmpop* ops, expr* comparators)\n"
1418 " | Call(expr func, expr* args, keyword* keywords)\n"
1419 " | FormattedValue(expr value, int? conversion, expr? format_spec)\n"
1420 " | JoinedStr(expr* values)\n"
1421 " | Constant(constant value, string? kind)\n"
1422 " | Attribute(expr value, identifier attr, expr_context ctx)\n"
1423 " | Subscript(expr value, expr slice, expr_context ctx)\n"
1424 " | Starred(expr value, expr_context ctx)\n"
1425 " | Name(identifier id, expr_context ctx)\n"
1426 " | List(expr* elts, expr_context ctx)\n"
1427 " | Tuple(expr* elts, expr_context ctx)\n"
1428 " | Slice(expr? lower, expr? upper, expr? step)");
1429 if (!state->expr_type) return 0;
1430 if (!add_attributes(state, state->expr_type, expr_attributes, 4)) return 0;
1431 if (PyObject_SetAttr(state->expr_type, state->end_lineno, Py_None) == -1)
1432 return 0;
1433 if (PyObject_SetAttr(state->expr_type, state->end_col_offset, Py_None) ==
1434 -1)
1435 return 0;
1436 state->BoolOp_type = make_type(state, "BoolOp", state->expr_type,
1437 BoolOp_fields, 2,
1438 "BoolOp(boolop op, expr* values)");
1439 if (!state->BoolOp_type) return 0;
1440 state->NamedExpr_type = make_type(state, "NamedExpr", state->expr_type,
1441 NamedExpr_fields, 2,
1442 "NamedExpr(expr target, expr value)");
1443 if (!state->NamedExpr_type) return 0;
1444 state->BinOp_type = make_type(state, "BinOp", state->expr_type,
1445 BinOp_fields, 3,
1446 "BinOp(expr left, operator op, expr right)");
1447 if (!state->BinOp_type) return 0;
1448 state->UnaryOp_type = make_type(state, "UnaryOp", state->expr_type,
1449 UnaryOp_fields, 2,
1450 "UnaryOp(unaryop op, expr operand)");
1451 if (!state->UnaryOp_type) return 0;
1452 state->Lambda_type = make_type(state, "Lambda", state->expr_type,
1453 Lambda_fields, 2,
1454 "Lambda(arguments args, expr body)");
1455 if (!state->Lambda_type) return 0;
1456 state->IfExp_type = make_type(state, "IfExp", state->expr_type,
1457 IfExp_fields, 3,
1458 "IfExp(expr test, expr body, expr orelse)");
1459 if (!state->IfExp_type) return 0;
1460 state->Dict_type = make_type(state, "Dict", state->expr_type, Dict_fields,
1461 2,
1462 "Dict(expr* keys, expr* values)");
1463 if (!state->Dict_type) return 0;
1464 state->Set_type = make_type(state, "Set", state->expr_type, Set_fields, 1,
1465 "Set(expr* elts)");
1466 if (!state->Set_type) return 0;
1467 state->ListComp_type = make_type(state, "ListComp", state->expr_type,
1468 ListComp_fields, 2,
1469 "ListComp(expr elt, comprehension* generators)");
1470 if (!state->ListComp_type) return 0;
1471 state->SetComp_type = make_type(state, "SetComp", state->expr_type,
1472 SetComp_fields, 2,
1473 "SetComp(expr elt, comprehension* generators)");
1474 if (!state->SetComp_type) return 0;
1475 state->DictComp_type = make_type(state, "DictComp", state->expr_type,
1476 DictComp_fields, 3,
1477 "DictComp(expr key, expr value, comprehension* generators)");
1478 if (!state->DictComp_type) return 0;
1479 state->GeneratorExp_type = make_type(state, "GeneratorExp",
1480 state->expr_type, GeneratorExp_fields,
1481 2,
1482 "GeneratorExp(expr elt, comprehension* generators)");
1483 if (!state->GeneratorExp_type) return 0;
1484 state->Await_type = make_type(state, "Await", state->expr_type,
1485 Await_fields, 1,
1486 "Await(expr value)");
1487 if (!state->Await_type) return 0;
1488 state->Yield_type = make_type(state, "Yield", state->expr_type,
1489 Yield_fields, 1,
1490 "Yield(expr? value)");
1491 if (!state->Yield_type) return 0;
1492 if (PyObject_SetAttr(state->Yield_type, state->value, Py_None) == -1)
1493 return 0;
1494 state->YieldFrom_type = make_type(state, "YieldFrom", state->expr_type,
1495 YieldFrom_fields, 1,
1496 "YieldFrom(expr value)");
1497 if (!state->YieldFrom_type) return 0;
1498 state->Compare_type = make_type(state, "Compare", state->expr_type,
1499 Compare_fields, 3,
1500 "Compare(expr left, cmpop* ops, expr* comparators)");
1501 if (!state->Compare_type) return 0;
1502 state->Call_type = make_type(state, "Call", state->expr_type, Call_fields,
1503 3,
1504 "Call(expr func, expr* args, keyword* keywords)");
1505 if (!state->Call_type) return 0;
1506 state->FormattedValue_type = make_type(state, "FormattedValue",
1507 state->expr_type,
1508 FormattedValue_fields, 3,
1509 "FormattedValue(expr value, int? conversion, expr? format_spec)");
1510 if (!state->FormattedValue_type) return 0;
1511 if (PyObject_SetAttr(state->FormattedValue_type, state->conversion,
1512 Py_None) == -1)
1513 return 0;
1514 if (PyObject_SetAttr(state->FormattedValue_type, state->format_spec,
1515 Py_None) == -1)
1516 return 0;
1517 state->JoinedStr_type = make_type(state, "JoinedStr", state->expr_type,
1518 JoinedStr_fields, 1,
1519 "JoinedStr(expr* values)");
1520 if (!state->JoinedStr_type) return 0;
1521 state->Constant_type = make_type(state, "Constant", state->expr_type,
1522 Constant_fields, 2,
1523 "Constant(constant value, string? kind)");
1524 if (!state->Constant_type) return 0;
1525 if (PyObject_SetAttr(state->Constant_type, state->kind, Py_None) == -1)
1526 return 0;
1527 state->Attribute_type = make_type(state, "Attribute", state->expr_type,
1528 Attribute_fields, 3,
1529 "Attribute(expr value, identifier attr, expr_context ctx)");
1530 if (!state->Attribute_type) return 0;
1531 state->Subscript_type = make_type(state, "Subscript", state->expr_type,
1532 Subscript_fields, 3,
1533 "Subscript(expr value, expr slice, expr_context ctx)");
1534 if (!state->Subscript_type) return 0;
1535 state->Starred_type = make_type(state, "Starred", state->expr_type,
1536 Starred_fields, 2,
1537 "Starred(expr value, expr_context ctx)");
1538 if (!state->Starred_type) return 0;
1539 state->Name_type = make_type(state, "Name", state->expr_type, Name_fields,
1540 2,
1541 "Name(identifier id, expr_context ctx)");
1542 if (!state->Name_type) return 0;
1543 state->List_type = make_type(state, "List", state->expr_type, List_fields,
1544 2,
1545 "List(expr* elts, expr_context ctx)");
1546 if (!state->List_type) return 0;
1547 state->Tuple_type = make_type(state, "Tuple", state->expr_type,
1548 Tuple_fields, 2,
1549 "Tuple(expr* elts, expr_context ctx)");
1550 if (!state->Tuple_type) return 0;
1551 state->Slice_type = make_type(state, "Slice", state->expr_type,
1552 Slice_fields, 3,
1553 "Slice(expr? lower, expr? upper, expr? step)");
1554 if (!state->Slice_type) return 0;
1555 if (PyObject_SetAttr(state->Slice_type, state->lower, Py_None) == -1)
1556 return 0;
1557 if (PyObject_SetAttr(state->Slice_type, state->upper, Py_None) == -1)
1558 return 0;
1559 if (PyObject_SetAttr(state->Slice_type, state->step, Py_None) == -1)
1560 return 0;
1561 state->expr_context_type = make_type(state, "expr_context",
1562 state->AST_type, NULL, 0,
1563 "expr_context = Load | Store | Del");
1564 if (!state->expr_context_type) return 0;
1565 if (!add_attributes(state, state->expr_context_type, NULL, 0)) return 0;
1566 state->Load_type = make_type(state, "Load", state->expr_context_type, NULL,
1567 0,
1568 "Load");
1569 if (!state->Load_type) return 0;
1570 state->Load_singleton = PyType_GenericNew((PyTypeObject *)state->Load_type,
1571 NULL, NULL);
1572 if (!state->Load_singleton) return 0;
1573 state->Store_type = make_type(state, "Store", state->expr_context_type,
1574 NULL, 0,
1575 "Store");
1576 if (!state->Store_type) return 0;
1577 state->Store_singleton = PyType_GenericNew((PyTypeObject
1578 *)state->Store_type, NULL, NULL);
1579 if (!state->Store_singleton) return 0;
1580 state->Del_type = make_type(state, "Del", state->expr_context_type, NULL, 0,
1581 "Del");
1582 if (!state->Del_type) return 0;
1583 state->Del_singleton = PyType_GenericNew((PyTypeObject *)state->Del_type,
1584 NULL, NULL);
1585 if (!state->Del_singleton) return 0;
1586 state->boolop_type = make_type(state, "boolop", state->AST_type, NULL, 0,
1587 "boolop = And | Or");
1588 if (!state->boolop_type) return 0;
1589 if (!add_attributes(state, state->boolop_type, NULL, 0)) return 0;
1590 state->And_type = make_type(state, "And", state->boolop_type, NULL, 0,
1591 "And");
1592 if (!state->And_type) return 0;
1593 state->And_singleton = PyType_GenericNew((PyTypeObject *)state->And_type,
1594 NULL, NULL);
1595 if (!state->And_singleton) return 0;
1596 state->Or_type = make_type(state, "Or", state->boolop_type, NULL, 0,
1597 "Or");
1598 if (!state->Or_type) return 0;
1599 state->Or_singleton = PyType_GenericNew((PyTypeObject *)state->Or_type,
1600 NULL, NULL);
1601 if (!state->Or_singleton) return 0;
1602 state->operator_type = make_type(state, "operator", state->AST_type, NULL,
1603 0,
1604 "operator = Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift | RShift | BitOr | BitXor | BitAnd | FloorDiv");
1605 if (!state->operator_type) return 0;
1606 if (!add_attributes(state, state->operator_type, NULL, 0)) return 0;
1607 state->Add_type = make_type(state, "Add", state->operator_type, NULL, 0,
1608 "Add");
1609 if (!state->Add_type) return 0;
1610 state->Add_singleton = PyType_GenericNew((PyTypeObject *)state->Add_type,
1611 NULL, NULL);
1612 if (!state->Add_singleton) return 0;
1613 state->Sub_type = make_type(state, "Sub", state->operator_type, NULL, 0,
1614 "Sub");
1615 if (!state->Sub_type) return 0;
1616 state->Sub_singleton = PyType_GenericNew((PyTypeObject *)state->Sub_type,
1617 NULL, NULL);
1618 if (!state->Sub_singleton) return 0;
1619 state->Mult_type = make_type(state, "Mult", state->operator_type, NULL, 0,
1620 "Mult");
1621 if (!state->Mult_type) return 0;
1622 state->Mult_singleton = PyType_GenericNew((PyTypeObject *)state->Mult_type,
1623 NULL, NULL);
1624 if (!state->Mult_singleton) return 0;
1625 state->MatMult_type = make_type(state, "MatMult", state->operator_type,
1626 NULL, 0,
1627 "MatMult");
1628 if (!state->MatMult_type) return 0;
1629 state->MatMult_singleton = PyType_GenericNew((PyTypeObject
1630 *)state->MatMult_type, NULL,
1631 NULL);
1632 if (!state->MatMult_singleton) return 0;
1633 state->Div_type = make_type(state, "Div", state->operator_type, NULL, 0,
1634 "Div");
1635 if (!state->Div_type) return 0;
1636 state->Div_singleton = PyType_GenericNew((PyTypeObject *)state->Div_type,
1637 NULL, NULL);
1638 if (!state->Div_singleton) return 0;
1639 state->Mod_type = make_type(state, "Mod", state->operator_type, NULL, 0,
1640 "Mod");
1641 if (!state->Mod_type) return 0;
1642 state->Mod_singleton = PyType_GenericNew((PyTypeObject *)state->Mod_type,
1643 NULL, NULL);
1644 if (!state->Mod_singleton) return 0;
1645 state->Pow_type = make_type(state, "Pow", state->operator_type, NULL, 0,
1646 "Pow");
1647 if (!state->Pow_type) return 0;
1648 state->Pow_singleton = PyType_GenericNew((PyTypeObject *)state->Pow_type,
1649 NULL, NULL);
1650 if (!state->Pow_singleton) return 0;
1651 state->LShift_type = make_type(state, "LShift", state->operator_type, NULL,
1652 0,
1653 "LShift");
1654 if (!state->LShift_type) return 0;
1655 state->LShift_singleton = PyType_GenericNew((PyTypeObject
1656 *)state->LShift_type, NULL,
1657 NULL);
1658 if (!state->LShift_singleton) return 0;
1659 state->RShift_type = make_type(state, "RShift", state->operator_type, NULL,
1660 0,
1661 "RShift");
1662 if (!state->RShift_type) return 0;
1663 state->RShift_singleton = PyType_GenericNew((PyTypeObject
1664 *)state->RShift_type, NULL,
1665 NULL);
1666 if (!state->RShift_singleton) return 0;
1667 state->BitOr_type = make_type(state, "BitOr", state->operator_type, NULL, 0,
1668 "BitOr");
1669 if (!state->BitOr_type) return 0;
1670 state->BitOr_singleton = PyType_GenericNew((PyTypeObject
1671 *)state->BitOr_type, NULL, NULL);
1672 if (!state->BitOr_singleton) return 0;
1673 state->BitXor_type = make_type(state, "BitXor", state->operator_type, NULL,
1674 0,
1675 "BitXor");
1676 if (!state->BitXor_type) return 0;
1677 state->BitXor_singleton = PyType_GenericNew((PyTypeObject
1678 *)state->BitXor_type, NULL,
1679 NULL);
1680 if (!state->BitXor_singleton) return 0;
1681 state->BitAnd_type = make_type(state, "BitAnd", state->operator_type, NULL,
1682 0,
1683 "BitAnd");
1684 if (!state->BitAnd_type) return 0;
1685 state->BitAnd_singleton = PyType_GenericNew((PyTypeObject
1686 *)state->BitAnd_type, NULL,
1687 NULL);
1688 if (!state->BitAnd_singleton) return 0;
1689 state->FloorDiv_type = make_type(state, "FloorDiv", state->operator_type,
1690 NULL, 0,
1691 "FloorDiv");
1692 if (!state->FloorDiv_type) return 0;
1693 state->FloorDiv_singleton = PyType_GenericNew((PyTypeObject
1694 *)state->FloorDiv_type, NULL,
1695 NULL);
1696 if (!state->FloorDiv_singleton) return 0;
1697 state->unaryop_type = make_type(state, "unaryop", state->AST_type, NULL, 0,
1698 "unaryop = Invert | Not | UAdd | USub");
1699 if (!state->unaryop_type) return 0;
1700 if (!add_attributes(state, state->unaryop_type, NULL, 0)) return 0;
1701 state->Invert_type = make_type(state, "Invert", state->unaryop_type, NULL,
1702 0,
1703 "Invert");
1704 if (!state->Invert_type) return 0;
1705 state->Invert_singleton = PyType_GenericNew((PyTypeObject
1706 *)state->Invert_type, NULL,
1707 NULL);
1708 if (!state->Invert_singleton) return 0;
1709 state->Not_type = make_type(state, "Not", state->unaryop_type, NULL, 0,
1710 "Not");
1711 if (!state->Not_type) return 0;
1712 state->Not_singleton = PyType_GenericNew((PyTypeObject *)state->Not_type,
1713 NULL, NULL);
1714 if (!state->Not_singleton) return 0;
1715 state->UAdd_type = make_type(state, "UAdd", state->unaryop_type, NULL, 0,
1716 "UAdd");
1717 if (!state->UAdd_type) return 0;
1718 state->UAdd_singleton = PyType_GenericNew((PyTypeObject *)state->UAdd_type,
1719 NULL, NULL);
1720 if (!state->UAdd_singleton) return 0;
1721 state->USub_type = make_type(state, "USub", state->unaryop_type, NULL, 0,
1722 "USub");
1723 if (!state->USub_type) return 0;
1724 state->USub_singleton = PyType_GenericNew((PyTypeObject *)state->USub_type,
1725 NULL, NULL);
1726 if (!state->USub_singleton) return 0;
1727 state->cmpop_type = make_type(state, "cmpop", state->AST_type, NULL, 0,
1728 "cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn");
1729 if (!state->cmpop_type) return 0;
1730 if (!add_attributes(state, state->cmpop_type, NULL, 0)) return 0;
1731 state->Eq_type = make_type(state, "Eq", state->cmpop_type, NULL, 0,
1732 "Eq");
1733 if (!state->Eq_type) return 0;
1734 state->Eq_singleton = PyType_GenericNew((PyTypeObject *)state->Eq_type,
1735 NULL, NULL);
1736 if (!state->Eq_singleton) return 0;
1737 state->NotEq_type = make_type(state, "NotEq", state->cmpop_type, NULL, 0,
1738 "NotEq");
1739 if (!state->NotEq_type) return 0;
1740 state->NotEq_singleton = PyType_GenericNew((PyTypeObject
1741 *)state->NotEq_type, NULL, NULL);
1742 if (!state->NotEq_singleton) return 0;
1743 state->Lt_type = make_type(state, "Lt", state->cmpop_type, NULL, 0,
1744 "Lt");
1745 if (!state->Lt_type) return 0;
1746 state->Lt_singleton = PyType_GenericNew((PyTypeObject *)state->Lt_type,
1747 NULL, NULL);
1748 if (!state->Lt_singleton) return 0;
1749 state->LtE_type = make_type(state, "LtE", state->cmpop_type, NULL, 0,
1750 "LtE");
1751 if (!state->LtE_type) return 0;
1752 state->LtE_singleton = PyType_GenericNew((PyTypeObject *)state->LtE_type,
1753 NULL, NULL);
1754 if (!state->LtE_singleton) return 0;
1755 state->Gt_type = make_type(state, "Gt", state->cmpop_type, NULL, 0,
1756 "Gt");
1757 if (!state->Gt_type) return 0;
1758 state->Gt_singleton = PyType_GenericNew((PyTypeObject *)state->Gt_type,
1759 NULL, NULL);
1760 if (!state->Gt_singleton) return 0;
1761 state->GtE_type = make_type(state, "GtE", state->cmpop_type, NULL, 0,
1762 "GtE");
1763 if (!state->GtE_type) return 0;
1764 state->GtE_singleton = PyType_GenericNew((PyTypeObject *)state->GtE_type,
1765 NULL, NULL);
1766 if (!state->GtE_singleton) return 0;
1767 state->Is_type = make_type(state, "Is", state->cmpop_type, NULL, 0,
1768 "Is");
1769 if (!state->Is_type) return 0;
1770 state->Is_singleton = PyType_GenericNew((PyTypeObject *)state->Is_type,
1771 NULL, NULL);
1772 if (!state->Is_singleton) return 0;
1773 state->IsNot_type = make_type(state, "IsNot", state->cmpop_type, NULL, 0,
1774 "IsNot");
1775 if (!state->IsNot_type) return 0;
1776 state->IsNot_singleton = PyType_GenericNew((PyTypeObject
1777 *)state->IsNot_type, NULL, NULL);
1778 if (!state->IsNot_singleton) return 0;
1779 state->In_type = make_type(state, "In", state->cmpop_type, NULL, 0,
1780 "In");
1781 if (!state->In_type) return 0;
1782 state->In_singleton = PyType_GenericNew((PyTypeObject *)state->In_type,
1783 NULL, NULL);
1784 if (!state->In_singleton) return 0;
1785 state->NotIn_type = make_type(state, "NotIn", state->cmpop_type, NULL, 0,
1786 "NotIn");
1787 if (!state->NotIn_type) return 0;
1788 state->NotIn_singleton = PyType_GenericNew((PyTypeObject
1789 *)state->NotIn_type, NULL, NULL);
1790 if (!state->NotIn_singleton) return 0;
1791 state->comprehension_type = make_type(state, "comprehension",
1792 state->AST_type,
1793 comprehension_fields, 4,
1794 "comprehension(expr target, expr iter, expr* ifs, int is_async)");
1795 if (!state->comprehension_type) return 0;
1796 if (!add_attributes(state, state->comprehension_type, NULL, 0)) return 0;
1797 state->excepthandler_type = make_type(state, "excepthandler",
1798 state->AST_type, NULL, 0,
1799 "excepthandler = ExceptHandler(expr? type, identifier? name, stmt* body)");
1800 if (!state->excepthandler_type) return 0;
1801 if (!add_attributes(state, state->excepthandler_type,
1802 excepthandler_attributes, 4)) return 0;
1803 if (PyObject_SetAttr(state->excepthandler_type, state->end_lineno, Py_None)
1804 == -1)
1805 return 0;
1806 if (PyObject_SetAttr(state->excepthandler_type, state->end_col_offset,
1807 Py_None) == -1)
1808 return 0;
1809 state->ExceptHandler_type = make_type(state, "ExceptHandler",
1810 state->excepthandler_type,
1811 ExceptHandler_fields, 3,
1812 "ExceptHandler(expr? type, identifier? name, stmt* body)");
1813 if (!state->ExceptHandler_type) return 0;
1814 if (PyObject_SetAttr(state->ExceptHandler_type, state->type, Py_None) == -1)
1815 return 0;
1816 if (PyObject_SetAttr(state->ExceptHandler_type, state->name, Py_None) == -1)
1817 return 0;
1818 state->arguments_type = make_type(state, "arguments", state->AST_type,
1819 arguments_fields, 7,
1820 "arguments(arg* posonlyargs, arg* args, arg? vararg, arg* kwonlyargs, expr* kw_defaults, arg? kwarg, expr* defaults)");
1821 if (!state->arguments_type) return 0;
1822 if (!add_attributes(state, state->arguments_type, NULL, 0)) return 0;
1823 if (PyObject_SetAttr(state->arguments_type, state->vararg, Py_None) == -1)
1824 return 0;
1825 if (PyObject_SetAttr(state->arguments_type, state->kwarg, Py_None) == -1)
1826 return 0;
1827 state->arg_type = make_type(state, "arg", state->AST_type, arg_fields, 3,
1828 "arg(identifier arg, expr? annotation, string? type_comment)");
1829 if (!state->arg_type) return 0;
1830 if (!add_attributes(state, state->arg_type, arg_attributes, 4)) return 0;
1831 if (PyObject_SetAttr(state->arg_type, state->annotation, Py_None) == -1)
1832 return 0;
1833 if (PyObject_SetAttr(state->arg_type, state->type_comment, Py_None) == -1)
1834 return 0;
1835 if (PyObject_SetAttr(state->arg_type, state->end_lineno, Py_None) == -1)
1836 return 0;
1837 if (PyObject_SetAttr(state->arg_type, state->end_col_offset, Py_None) == -1)
1838 return 0;
1839 state->keyword_type = make_type(state, "keyword", state->AST_type,
1840 keyword_fields, 2,
1841 "keyword(identifier? arg, expr value)");
1842 if (!state->keyword_type) return 0;
1843 if (!add_attributes(state, state->keyword_type, keyword_attributes, 4))
1844 return 0;
1845 if (PyObject_SetAttr(state->keyword_type, state->arg, Py_None) == -1)
1846 return 0;
1847 if (PyObject_SetAttr(state->keyword_type, state->end_lineno, Py_None) == -1)
1848 return 0;
1849 if (PyObject_SetAttr(state->keyword_type, state->end_col_offset, Py_None)
1850 == -1)
1851 return 0;
1852 state->alias_type = make_type(state, "alias", state->AST_type,
1853 alias_fields, 2,
1854 "alias(identifier name, identifier? asname)");
1855 if (!state->alias_type) return 0;
1856 if (!add_attributes(state, state->alias_type, NULL, 0)) return 0;
1857 if (PyObject_SetAttr(state->alias_type, state->asname, Py_None) == -1)
1858 return 0;
1859 state->withitem_type = make_type(state, "withitem", state->AST_type,
1860 withitem_fields, 2,
1861 "withitem(expr context_expr, expr? optional_vars)");
1862 if (!state->withitem_type) return 0;
1863 if (!add_attributes(state, state->withitem_type, NULL, 0)) return 0;
1864 if (PyObject_SetAttr(state->withitem_type, state->optional_vars, Py_None)
1865 == -1)
1866 return 0;
1867 state->type_ignore_type = make_type(state, "type_ignore", state->AST_type,
1868 NULL, 0,
1869 "type_ignore = TypeIgnore(int lineno, string tag)");
1870 if (!state->type_ignore_type) return 0;
1871 if (!add_attributes(state, state->type_ignore_type, NULL, 0)) return 0;
1872 state->TypeIgnore_type = make_type(state, "TypeIgnore",
1873 state->type_ignore_type,
1874 TypeIgnore_fields, 2,
1875 "TypeIgnore(int lineno, string tag)");
1876 if (!state->TypeIgnore_type) return 0;
1877 state->initialized = 1;
1878 return 1;
1879 }
1880
1881 static int obj2ast_mod(astmodulestate *state, PyObject* obj, mod_ty* out,
1882 PyArena* arena);
1883 static int obj2ast_stmt(astmodulestate *state, PyObject* obj, stmt_ty* out,
1884 PyArena* arena);
1885 static int obj2ast_expr(astmodulestate *state, PyObject* obj, expr_ty* out,
1886 PyArena* arena);
1887 static int obj2ast_expr_context(astmodulestate *state, PyObject* obj,
1888 expr_context_ty* out, PyArena* arena);
1889 static int obj2ast_boolop(astmodulestate *state, PyObject* obj, boolop_ty* out,
1890 PyArena* arena);
1891 static int obj2ast_operator(astmodulestate *state, PyObject* obj, operator_ty*
1892 out, PyArena* arena);
1893 static int obj2ast_unaryop(astmodulestate *state, PyObject* obj, unaryop_ty*
1894 out, PyArena* arena);
1895 static int obj2ast_cmpop(astmodulestate *state, PyObject* obj, cmpop_ty* out,
1896 PyArena* arena);
1897 static int obj2ast_comprehension(astmodulestate *state, PyObject* obj,
1898 comprehension_ty* out, PyArena* arena);
1899 static int obj2ast_excepthandler(astmodulestate *state, PyObject* obj,
1900 excepthandler_ty* out, PyArena* arena);
1901 static int obj2ast_arguments(astmodulestate *state, PyObject* obj,
1902 arguments_ty* out, PyArena* arena);
1903 static int obj2ast_arg(astmodulestate *state, PyObject* obj, arg_ty* out,
1904 PyArena* arena);
1905 static int obj2ast_keyword(astmodulestate *state, PyObject* obj, keyword_ty*
1906 out, PyArena* arena);
1907 static int obj2ast_alias(astmodulestate *state, PyObject* obj, alias_ty* out,
1908 PyArena* arena);
1909 static int obj2ast_withitem(astmodulestate *state, PyObject* obj, withitem_ty*
1910 out, PyArena* arena);
1911 static int obj2ast_type_ignore(astmodulestate *state, PyObject* obj,
1912 type_ignore_ty* out, PyArena* arena);
1913
1914 mod_ty
Module(asdl_seq * body,asdl_seq * type_ignores,PyArena * arena)1915 Module(asdl_seq * body, asdl_seq * type_ignores, PyArena *arena)
1916 {
1917 mod_ty p;
1918 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1919 if (!p)
1920 return NULL;
1921 p->kind = Module_kind;
1922 p->v.Module.body = body;
1923 p->v.Module.type_ignores = type_ignores;
1924 return p;
1925 }
1926
1927 mod_ty
Interactive(asdl_seq * body,PyArena * arena)1928 Interactive(asdl_seq * body, PyArena *arena)
1929 {
1930 mod_ty p;
1931 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1932 if (!p)
1933 return NULL;
1934 p->kind = Interactive_kind;
1935 p->v.Interactive.body = body;
1936 return p;
1937 }
1938
1939 mod_ty
Expression(expr_ty body,PyArena * arena)1940 Expression(expr_ty body, PyArena *arena)
1941 {
1942 mod_ty p;
1943 if (!body) {
1944 PyErr_SetString(PyExc_ValueError,
1945 "field 'body' is required for Expression");
1946 return NULL;
1947 }
1948 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1949 if (!p)
1950 return NULL;
1951 p->kind = Expression_kind;
1952 p->v.Expression.body = body;
1953 return p;
1954 }
1955
1956 mod_ty
FunctionType(asdl_seq * argtypes,expr_ty returns,PyArena * arena)1957 FunctionType(asdl_seq * argtypes, expr_ty returns, PyArena *arena)
1958 {
1959 mod_ty p;
1960 if (!returns) {
1961 PyErr_SetString(PyExc_ValueError,
1962 "field 'returns' is required for FunctionType");
1963 return NULL;
1964 }
1965 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1966 if (!p)
1967 return NULL;
1968 p->kind = FunctionType_kind;
1969 p->v.FunctionType.argtypes = argtypes;
1970 p->v.FunctionType.returns = returns;
1971 return p;
1972 }
1973
1974 stmt_ty
FunctionDef(identifier name,arguments_ty args,asdl_seq * body,asdl_seq * decorator_list,expr_ty returns,string type_comment,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)1975 FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
1976 decorator_list, expr_ty returns, string type_comment, int lineno,
1977 int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
1978 {
1979 stmt_ty p;
1980 if (!name) {
1981 PyErr_SetString(PyExc_ValueError,
1982 "field 'name' is required for FunctionDef");
1983 return NULL;
1984 }
1985 if (!args) {
1986 PyErr_SetString(PyExc_ValueError,
1987 "field 'args' is required for FunctionDef");
1988 return NULL;
1989 }
1990 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1991 if (!p)
1992 return NULL;
1993 p->kind = FunctionDef_kind;
1994 p->v.FunctionDef.name = name;
1995 p->v.FunctionDef.args = args;
1996 p->v.FunctionDef.body = body;
1997 p->v.FunctionDef.decorator_list = decorator_list;
1998 p->v.FunctionDef.returns = returns;
1999 p->v.FunctionDef.type_comment = type_comment;
2000 p->lineno = lineno;
2001 p->col_offset = col_offset;
2002 p->end_lineno = end_lineno;
2003 p->end_col_offset = end_col_offset;
2004 return p;
2005 }
2006
2007 stmt_ty
AsyncFunctionDef(identifier name,arguments_ty args,asdl_seq * body,asdl_seq * decorator_list,expr_ty returns,string type_comment,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2008 AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
2009 * decorator_list, expr_ty returns, string type_comment, int
2010 lineno, int col_offset, int end_lineno, int end_col_offset,
2011 PyArena *arena)
2012 {
2013 stmt_ty p;
2014 if (!name) {
2015 PyErr_SetString(PyExc_ValueError,
2016 "field 'name' is required for AsyncFunctionDef");
2017 return NULL;
2018 }
2019 if (!args) {
2020 PyErr_SetString(PyExc_ValueError,
2021 "field 'args' is required for AsyncFunctionDef");
2022 return NULL;
2023 }
2024 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2025 if (!p)
2026 return NULL;
2027 p->kind = AsyncFunctionDef_kind;
2028 p->v.AsyncFunctionDef.name = name;
2029 p->v.AsyncFunctionDef.args = args;
2030 p->v.AsyncFunctionDef.body = body;
2031 p->v.AsyncFunctionDef.decorator_list = decorator_list;
2032 p->v.AsyncFunctionDef.returns = returns;
2033 p->v.AsyncFunctionDef.type_comment = type_comment;
2034 p->lineno = lineno;
2035 p->col_offset = col_offset;
2036 p->end_lineno = end_lineno;
2037 p->end_col_offset = end_col_offset;
2038 return p;
2039 }
2040
2041 stmt_ty
ClassDef(identifier name,asdl_seq * bases,asdl_seq * keywords,asdl_seq * body,asdl_seq * decorator_list,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2042 ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
2043 body, asdl_seq * decorator_list, int lineno, int col_offset, int
2044 end_lineno, int end_col_offset, PyArena *arena)
2045 {
2046 stmt_ty p;
2047 if (!name) {
2048 PyErr_SetString(PyExc_ValueError,
2049 "field 'name' is required for ClassDef");
2050 return NULL;
2051 }
2052 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2053 if (!p)
2054 return NULL;
2055 p->kind = ClassDef_kind;
2056 p->v.ClassDef.name = name;
2057 p->v.ClassDef.bases = bases;
2058 p->v.ClassDef.keywords = keywords;
2059 p->v.ClassDef.body = body;
2060 p->v.ClassDef.decorator_list = decorator_list;
2061 p->lineno = lineno;
2062 p->col_offset = col_offset;
2063 p->end_lineno = end_lineno;
2064 p->end_col_offset = end_col_offset;
2065 return p;
2066 }
2067
2068 stmt_ty
Return(expr_ty value,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2069 Return(expr_ty value, int lineno, int col_offset, int end_lineno, int
2070 end_col_offset, PyArena *arena)
2071 {
2072 stmt_ty p;
2073 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2074 if (!p)
2075 return NULL;
2076 p->kind = Return_kind;
2077 p->v.Return.value = value;
2078 p->lineno = lineno;
2079 p->col_offset = col_offset;
2080 p->end_lineno = end_lineno;
2081 p->end_col_offset = end_col_offset;
2082 return p;
2083 }
2084
2085 stmt_ty
Delete(asdl_seq * targets,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2086 Delete(asdl_seq * targets, int lineno, int col_offset, int end_lineno, int
2087 end_col_offset, PyArena *arena)
2088 {
2089 stmt_ty p;
2090 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2091 if (!p)
2092 return NULL;
2093 p->kind = Delete_kind;
2094 p->v.Delete.targets = targets;
2095 p->lineno = lineno;
2096 p->col_offset = col_offset;
2097 p->end_lineno = end_lineno;
2098 p->end_col_offset = end_col_offset;
2099 return p;
2100 }
2101
2102 stmt_ty
Assign(asdl_seq * targets,expr_ty value,string type_comment,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2103 Assign(asdl_seq * targets, expr_ty value, string type_comment, int lineno, int
2104 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
2105 {
2106 stmt_ty p;
2107 if (!value) {
2108 PyErr_SetString(PyExc_ValueError,
2109 "field 'value' is required for Assign");
2110 return NULL;
2111 }
2112 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2113 if (!p)
2114 return NULL;
2115 p->kind = Assign_kind;
2116 p->v.Assign.targets = targets;
2117 p->v.Assign.value = value;
2118 p->v.Assign.type_comment = type_comment;
2119 p->lineno = lineno;
2120 p->col_offset = col_offset;
2121 p->end_lineno = end_lineno;
2122 p->end_col_offset = end_col_offset;
2123 return p;
2124 }
2125
2126 stmt_ty
AugAssign(expr_ty target,operator_ty op,expr_ty value,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2127 AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
2128 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
2129 {
2130 stmt_ty p;
2131 if (!target) {
2132 PyErr_SetString(PyExc_ValueError,
2133 "field 'target' is required for AugAssign");
2134 return NULL;
2135 }
2136 if (!op) {
2137 PyErr_SetString(PyExc_ValueError,
2138 "field 'op' is required for AugAssign");
2139 return NULL;
2140 }
2141 if (!value) {
2142 PyErr_SetString(PyExc_ValueError,
2143 "field 'value' is required for AugAssign");
2144 return NULL;
2145 }
2146 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2147 if (!p)
2148 return NULL;
2149 p->kind = AugAssign_kind;
2150 p->v.AugAssign.target = target;
2151 p->v.AugAssign.op = op;
2152 p->v.AugAssign.value = value;
2153 p->lineno = lineno;
2154 p->col_offset = col_offset;
2155 p->end_lineno = end_lineno;
2156 p->end_col_offset = end_col_offset;
2157 return p;
2158 }
2159
2160 stmt_ty
AnnAssign(expr_ty target,expr_ty annotation,expr_ty value,int simple,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2161 AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
2162 lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
2163 *arena)
2164 {
2165 stmt_ty p;
2166 if (!target) {
2167 PyErr_SetString(PyExc_ValueError,
2168 "field 'target' is required for AnnAssign");
2169 return NULL;
2170 }
2171 if (!annotation) {
2172 PyErr_SetString(PyExc_ValueError,
2173 "field 'annotation' is required for AnnAssign");
2174 return NULL;
2175 }
2176 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2177 if (!p)
2178 return NULL;
2179 p->kind = AnnAssign_kind;
2180 p->v.AnnAssign.target = target;
2181 p->v.AnnAssign.annotation = annotation;
2182 p->v.AnnAssign.value = value;
2183 p->v.AnnAssign.simple = simple;
2184 p->lineno = lineno;
2185 p->col_offset = col_offset;
2186 p->end_lineno = end_lineno;
2187 p->end_col_offset = end_col_offset;
2188 return p;
2189 }
2190
2191 stmt_ty
For(expr_ty target,expr_ty iter,asdl_seq * body,asdl_seq * orelse,string type_comment,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2192 For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, string
2193 type_comment, int lineno, int col_offset, int end_lineno, int
2194 end_col_offset, PyArena *arena)
2195 {
2196 stmt_ty p;
2197 if (!target) {
2198 PyErr_SetString(PyExc_ValueError,
2199 "field 'target' is required for For");
2200 return NULL;
2201 }
2202 if (!iter) {
2203 PyErr_SetString(PyExc_ValueError,
2204 "field 'iter' is required for For");
2205 return NULL;
2206 }
2207 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2208 if (!p)
2209 return NULL;
2210 p->kind = For_kind;
2211 p->v.For.target = target;
2212 p->v.For.iter = iter;
2213 p->v.For.body = body;
2214 p->v.For.orelse = orelse;
2215 p->v.For.type_comment = type_comment;
2216 p->lineno = lineno;
2217 p->col_offset = col_offset;
2218 p->end_lineno = end_lineno;
2219 p->end_col_offset = end_col_offset;
2220 return p;
2221 }
2222
2223 stmt_ty
AsyncFor(expr_ty target,expr_ty iter,asdl_seq * body,asdl_seq * orelse,string type_comment,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2224 AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse,
2225 string type_comment, int lineno, int col_offset, int end_lineno, int
2226 end_col_offset, PyArena *arena)
2227 {
2228 stmt_ty p;
2229 if (!target) {
2230 PyErr_SetString(PyExc_ValueError,
2231 "field 'target' is required for AsyncFor");
2232 return NULL;
2233 }
2234 if (!iter) {
2235 PyErr_SetString(PyExc_ValueError,
2236 "field 'iter' is required for AsyncFor");
2237 return NULL;
2238 }
2239 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2240 if (!p)
2241 return NULL;
2242 p->kind = AsyncFor_kind;
2243 p->v.AsyncFor.target = target;
2244 p->v.AsyncFor.iter = iter;
2245 p->v.AsyncFor.body = body;
2246 p->v.AsyncFor.orelse = orelse;
2247 p->v.AsyncFor.type_comment = type_comment;
2248 p->lineno = lineno;
2249 p->col_offset = col_offset;
2250 p->end_lineno = end_lineno;
2251 p->end_col_offset = end_col_offset;
2252 return p;
2253 }
2254
2255 stmt_ty
While(expr_ty test,asdl_seq * body,asdl_seq * orelse,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2256 While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
2257 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
2258 {
2259 stmt_ty p;
2260 if (!test) {
2261 PyErr_SetString(PyExc_ValueError,
2262 "field 'test' is required for While");
2263 return NULL;
2264 }
2265 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2266 if (!p)
2267 return NULL;
2268 p->kind = While_kind;
2269 p->v.While.test = test;
2270 p->v.While.body = body;
2271 p->v.While.orelse = orelse;
2272 p->lineno = lineno;
2273 p->col_offset = col_offset;
2274 p->end_lineno = end_lineno;
2275 p->end_col_offset = end_col_offset;
2276 return p;
2277 }
2278
2279 stmt_ty
If(expr_ty test,asdl_seq * body,asdl_seq * orelse,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2280 If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
2281 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
2282 {
2283 stmt_ty p;
2284 if (!test) {
2285 PyErr_SetString(PyExc_ValueError,
2286 "field 'test' is required for If");
2287 return NULL;
2288 }
2289 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2290 if (!p)
2291 return NULL;
2292 p->kind = If_kind;
2293 p->v.If.test = test;
2294 p->v.If.body = body;
2295 p->v.If.orelse = orelse;
2296 p->lineno = lineno;
2297 p->col_offset = col_offset;
2298 p->end_lineno = end_lineno;
2299 p->end_col_offset = end_col_offset;
2300 return p;
2301 }
2302
2303 stmt_ty
With(asdl_seq * items,asdl_seq * body,string type_comment,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2304 With(asdl_seq * items, asdl_seq * body, string type_comment, int lineno, int
2305 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
2306 {
2307 stmt_ty p;
2308 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2309 if (!p)
2310 return NULL;
2311 p->kind = With_kind;
2312 p->v.With.items = items;
2313 p->v.With.body = body;
2314 p->v.With.type_comment = type_comment;
2315 p->lineno = lineno;
2316 p->col_offset = col_offset;
2317 p->end_lineno = end_lineno;
2318 p->end_col_offset = end_col_offset;
2319 return p;
2320 }
2321
2322 stmt_ty
AsyncWith(asdl_seq * items,asdl_seq * body,string type_comment,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2323 AsyncWith(asdl_seq * items, asdl_seq * body, string type_comment, int lineno,
2324 int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
2325 {
2326 stmt_ty p;
2327 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2328 if (!p)
2329 return NULL;
2330 p->kind = AsyncWith_kind;
2331 p->v.AsyncWith.items = items;
2332 p->v.AsyncWith.body = body;
2333 p->v.AsyncWith.type_comment = type_comment;
2334 p->lineno = lineno;
2335 p->col_offset = col_offset;
2336 p->end_lineno = end_lineno;
2337 p->end_col_offset = end_col_offset;
2338 return p;
2339 }
2340
2341 stmt_ty
Raise(expr_ty exc,expr_ty cause,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2342 Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, int end_lineno,
2343 int end_col_offset, PyArena *arena)
2344 {
2345 stmt_ty p;
2346 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2347 if (!p)
2348 return NULL;
2349 p->kind = Raise_kind;
2350 p->v.Raise.exc = exc;
2351 p->v.Raise.cause = cause;
2352 p->lineno = lineno;
2353 p->col_offset = col_offset;
2354 p->end_lineno = end_lineno;
2355 p->end_col_offset = end_col_offset;
2356 return p;
2357 }
2358
2359 stmt_ty
Try(asdl_seq * body,asdl_seq * handlers,asdl_seq * orelse,asdl_seq * finalbody,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2360 Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
2361 finalbody, int lineno, int col_offset, int end_lineno, int end_col_offset,
2362 PyArena *arena)
2363 {
2364 stmt_ty p;
2365 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2366 if (!p)
2367 return NULL;
2368 p->kind = Try_kind;
2369 p->v.Try.body = body;
2370 p->v.Try.handlers = handlers;
2371 p->v.Try.orelse = orelse;
2372 p->v.Try.finalbody = finalbody;
2373 p->lineno = lineno;
2374 p->col_offset = col_offset;
2375 p->end_lineno = end_lineno;
2376 p->end_col_offset = end_col_offset;
2377 return p;
2378 }
2379
2380 stmt_ty
Assert(expr_ty test,expr_ty msg,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2381 Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, int end_lineno,
2382 int end_col_offset, PyArena *arena)
2383 {
2384 stmt_ty p;
2385 if (!test) {
2386 PyErr_SetString(PyExc_ValueError,
2387 "field 'test' is required for Assert");
2388 return NULL;
2389 }
2390 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2391 if (!p)
2392 return NULL;
2393 p->kind = Assert_kind;
2394 p->v.Assert.test = test;
2395 p->v.Assert.msg = msg;
2396 p->lineno = lineno;
2397 p->col_offset = col_offset;
2398 p->end_lineno = end_lineno;
2399 p->end_col_offset = end_col_offset;
2400 return p;
2401 }
2402
2403 stmt_ty
Import(asdl_seq * names,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2404 Import(asdl_seq * names, int lineno, int col_offset, int end_lineno, int
2405 end_col_offset, PyArena *arena)
2406 {
2407 stmt_ty p;
2408 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2409 if (!p)
2410 return NULL;
2411 p->kind = Import_kind;
2412 p->v.Import.names = names;
2413 p->lineno = lineno;
2414 p->col_offset = col_offset;
2415 p->end_lineno = end_lineno;
2416 p->end_col_offset = end_col_offset;
2417 return p;
2418 }
2419
2420 stmt_ty
ImportFrom(identifier module,asdl_seq * names,int level,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2421 ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
2422 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
2423 {
2424 stmt_ty p;
2425 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2426 if (!p)
2427 return NULL;
2428 p->kind = ImportFrom_kind;
2429 p->v.ImportFrom.module = module;
2430 p->v.ImportFrom.names = names;
2431 p->v.ImportFrom.level = level;
2432 p->lineno = lineno;
2433 p->col_offset = col_offset;
2434 p->end_lineno = end_lineno;
2435 p->end_col_offset = end_col_offset;
2436 return p;
2437 }
2438
2439 stmt_ty
Global(asdl_seq * names,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2440 Global(asdl_seq * names, int lineno, int col_offset, int end_lineno, int
2441 end_col_offset, PyArena *arena)
2442 {
2443 stmt_ty p;
2444 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2445 if (!p)
2446 return NULL;
2447 p->kind = Global_kind;
2448 p->v.Global.names = names;
2449 p->lineno = lineno;
2450 p->col_offset = col_offset;
2451 p->end_lineno = end_lineno;
2452 p->end_col_offset = end_col_offset;
2453 return p;
2454 }
2455
2456 stmt_ty
Nonlocal(asdl_seq * names,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2457 Nonlocal(asdl_seq * names, int lineno, int col_offset, int end_lineno, int
2458 end_col_offset, PyArena *arena)
2459 {
2460 stmt_ty p;
2461 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2462 if (!p)
2463 return NULL;
2464 p->kind = Nonlocal_kind;
2465 p->v.Nonlocal.names = names;
2466 p->lineno = lineno;
2467 p->col_offset = col_offset;
2468 p->end_lineno = end_lineno;
2469 p->end_col_offset = end_col_offset;
2470 return p;
2471 }
2472
2473 stmt_ty
Expr(expr_ty value,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2474 Expr(expr_ty value, int lineno, int col_offset, int end_lineno, int
2475 end_col_offset, PyArena *arena)
2476 {
2477 stmt_ty p;
2478 if (!value) {
2479 PyErr_SetString(PyExc_ValueError,
2480 "field 'value' is required for Expr");
2481 return NULL;
2482 }
2483 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2484 if (!p)
2485 return NULL;
2486 p->kind = Expr_kind;
2487 p->v.Expr.value = value;
2488 p->lineno = lineno;
2489 p->col_offset = col_offset;
2490 p->end_lineno = end_lineno;
2491 p->end_col_offset = end_col_offset;
2492 return p;
2493 }
2494
2495 stmt_ty
Pass(int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2496 Pass(int lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
2497 *arena)
2498 {
2499 stmt_ty p;
2500 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2501 if (!p)
2502 return NULL;
2503 p->kind = Pass_kind;
2504 p->lineno = lineno;
2505 p->col_offset = col_offset;
2506 p->end_lineno = end_lineno;
2507 p->end_col_offset = end_col_offset;
2508 return p;
2509 }
2510
2511 stmt_ty
Break(int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2512 Break(int lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
2513 *arena)
2514 {
2515 stmt_ty p;
2516 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2517 if (!p)
2518 return NULL;
2519 p->kind = Break_kind;
2520 p->lineno = lineno;
2521 p->col_offset = col_offset;
2522 p->end_lineno = end_lineno;
2523 p->end_col_offset = end_col_offset;
2524 return p;
2525 }
2526
2527 stmt_ty
Continue(int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2528 Continue(int lineno, int col_offset, int end_lineno, int end_col_offset,
2529 PyArena *arena)
2530 {
2531 stmt_ty p;
2532 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2533 if (!p)
2534 return NULL;
2535 p->kind = Continue_kind;
2536 p->lineno = lineno;
2537 p->col_offset = col_offset;
2538 p->end_lineno = end_lineno;
2539 p->end_col_offset = end_col_offset;
2540 return p;
2541 }
2542
2543 expr_ty
BoolOp(boolop_ty op,asdl_seq * values,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2544 BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, int
2545 end_lineno, int end_col_offset, PyArena *arena)
2546 {
2547 expr_ty p;
2548 if (!op) {
2549 PyErr_SetString(PyExc_ValueError,
2550 "field 'op' is required for BoolOp");
2551 return NULL;
2552 }
2553 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2554 if (!p)
2555 return NULL;
2556 p->kind = BoolOp_kind;
2557 p->v.BoolOp.op = op;
2558 p->v.BoolOp.values = values;
2559 p->lineno = lineno;
2560 p->col_offset = col_offset;
2561 p->end_lineno = end_lineno;
2562 p->end_col_offset = end_col_offset;
2563 return p;
2564 }
2565
2566 expr_ty
NamedExpr(expr_ty target,expr_ty value,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2567 NamedExpr(expr_ty target, expr_ty value, int lineno, int col_offset, int
2568 end_lineno, int end_col_offset, PyArena *arena)
2569 {
2570 expr_ty p;
2571 if (!target) {
2572 PyErr_SetString(PyExc_ValueError,
2573 "field 'target' is required for NamedExpr");
2574 return NULL;
2575 }
2576 if (!value) {
2577 PyErr_SetString(PyExc_ValueError,
2578 "field 'value' is required for NamedExpr");
2579 return NULL;
2580 }
2581 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2582 if (!p)
2583 return NULL;
2584 p->kind = NamedExpr_kind;
2585 p->v.NamedExpr.target = target;
2586 p->v.NamedExpr.value = value;
2587 p->lineno = lineno;
2588 p->col_offset = col_offset;
2589 p->end_lineno = end_lineno;
2590 p->end_col_offset = end_col_offset;
2591 return p;
2592 }
2593
2594 expr_ty
BinOp(expr_ty left,operator_ty op,expr_ty right,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2595 BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
2596 int end_lineno, int end_col_offset, PyArena *arena)
2597 {
2598 expr_ty p;
2599 if (!left) {
2600 PyErr_SetString(PyExc_ValueError,
2601 "field 'left' is required for BinOp");
2602 return NULL;
2603 }
2604 if (!op) {
2605 PyErr_SetString(PyExc_ValueError,
2606 "field 'op' is required for BinOp");
2607 return NULL;
2608 }
2609 if (!right) {
2610 PyErr_SetString(PyExc_ValueError,
2611 "field 'right' is required for BinOp");
2612 return NULL;
2613 }
2614 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2615 if (!p)
2616 return NULL;
2617 p->kind = BinOp_kind;
2618 p->v.BinOp.left = left;
2619 p->v.BinOp.op = op;
2620 p->v.BinOp.right = right;
2621 p->lineno = lineno;
2622 p->col_offset = col_offset;
2623 p->end_lineno = end_lineno;
2624 p->end_col_offset = end_col_offset;
2625 return p;
2626 }
2627
2628 expr_ty
UnaryOp(unaryop_ty op,expr_ty operand,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2629 UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, int
2630 end_lineno, int end_col_offset, PyArena *arena)
2631 {
2632 expr_ty p;
2633 if (!op) {
2634 PyErr_SetString(PyExc_ValueError,
2635 "field 'op' is required for UnaryOp");
2636 return NULL;
2637 }
2638 if (!operand) {
2639 PyErr_SetString(PyExc_ValueError,
2640 "field 'operand' is required for UnaryOp");
2641 return NULL;
2642 }
2643 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2644 if (!p)
2645 return NULL;
2646 p->kind = UnaryOp_kind;
2647 p->v.UnaryOp.op = op;
2648 p->v.UnaryOp.operand = operand;
2649 p->lineno = lineno;
2650 p->col_offset = col_offset;
2651 p->end_lineno = end_lineno;
2652 p->end_col_offset = end_col_offset;
2653 return p;
2654 }
2655
2656 expr_ty
Lambda(arguments_ty args,expr_ty body,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2657 Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, int
2658 end_lineno, int end_col_offset, PyArena *arena)
2659 {
2660 expr_ty p;
2661 if (!args) {
2662 PyErr_SetString(PyExc_ValueError,
2663 "field 'args' is required for Lambda");
2664 return NULL;
2665 }
2666 if (!body) {
2667 PyErr_SetString(PyExc_ValueError,
2668 "field 'body' is required for Lambda");
2669 return NULL;
2670 }
2671 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2672 if (!p)
2673 return NULL;
2674 p->kind = Lambda_kind;
2675 p->v.Lambda.args = args;
2676 p->v.Lambda.body = body;
2677 p->lineno = lineno;
2678 p->col_offset = col_offset;
2679 p->end_lineno = end_lineno;
2680 p->end_col_offset = end_col_offset;
2681 return p;
2682 }
2683
2684 expr_ty
IfExp(expr_ty test,expr_ty body,expr_ty orelse,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2685 IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
2686 int end_lineno, int end_col_offset, PyArena *arena)
2687 {
2688 expr_ty p;
2689 if (!test) {
2690 PyErr_SetString(PyExc_ValueError,
2691 "field 'test' is required for IfExp");
2692 return NULL;
2693 }
2694 if (!body) {
2695 PyErr_SetString(PyExc_ValueError,
2696 "field 'body' is required for IfExp");
2697 return NULL;
2698 }
2699 if (!orelse) {
2700 PyErr_SetString(PyExc_ValueError,
2701 "field 'orelse' is required for IfExp");
2702 return NULL;
2703 }
2704 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2705 if (!p)
2706 return NULL;
2707 p->kind = IfExp_kind;
2708 p->v.IfExp.test = test;
2709 p->v.IfExp.body = body;
2710 p->v.IfExp.orelse = orelse;
2711 p->lineno = lineno;
2712 p->col_offset = col_offset;
2713 p->end_lineno = end_lineno;
2714 p->end_col_offset = end_col_offset;
2715 return p;
2716 }
2717
2718 expr_ty
Dict(asdl_seq * keys,asdl_seq * values,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2719 Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, int
2720 end_lineno, int end_col_offset, PyArena *arena)
2721 {
2722 expr_ty p;
2723 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2724 if (!p)
2725 return NULL;
2726 p->kind = Dict_kind;
2727 p->v.Dict.keys = keys;
2728 p->v.Dict.values = values;
2729 p->lineno = lineno;
2730 p->col_offset = col_offset;
2731 p->end_lineno = end_lineno;
2732 p->end_col_offset = end_col_offset;
2733 return p;
2734 }
2735
2736 expr_ty
Set(asdl_seq * elts,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2737 Set(asdl_seq * elts, int lineno, int col_offset, int end_lineno, int
2738 end_col_offset, PyArena *arena)
2739 {
2740 expr_ty p;
2741 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2742 if (!p)
2743 return NULL;
2744 p->kind = Set_kind;
2745 p->v.Set.elts = elts;
2746 p->lineno = lineno;
2747 p->col_offset = col_offset;
2748 p->end_lineno = end_lineno;
2749 p->end_col_offset = end_col_offset;
2750 return p;
2751 }
2752
2753 expr_ty
ListComp(expr_ty elt,asdl_seq * generators,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2754 ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, int
2755 end_lineno, int end_col_offset, PyArena *arena)
2756 {
2757 expr_ty p;
2758 if (!elt) {
2759 PyErr_SetString(PyExc_ValueError,
2760 "field 'elt' is required for ListComp");
2761 return NULL;
2762 }
2763 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2764 if (!p)
2765 return NULL;
2766 p->kind = ListComp_kind;
2767 p->v.ListComp.elt = elt;
2768 p->v.ListComp.generators = generators;
2769 p->lineno = lineno;
2770 p->col_offset = col_offset;
2771 p->end_lineno = end_lineno;
2772 p->end_col_offset = end_col_offset;
2773 return p;
2774 }
2775
2776 expr_ty
SetComp(expr_ty elt,asdl_seq * generators,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2777 SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, int
2778 end_lineno, int end_col_offset, PyArena *arena)
2779 {
2780 expr_ty p;
2781 if (!elt) {
2782 PyErr_SetString(PyExc_ValueError,
2783 "field 'elt' is required for SetComp");
2784 return NULL;
2785 }
2786 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2787 if (!p)
2788 return NULL;
2789 p->kind = SetComp_kind;
2790 p->v.SetComp.elt = elt;
2791 p->v.SetComp.generators = generators;
2792 p->lineno = lineno;
2793 p->col_offset = col_offset;
2794 p->end_lineno = end_lineno;
2795 p->end_col_offset = end_col_offset;
2796 return p;
2797 }
2798
2799 expr_ty
DictComp(expr_ty key,expr_ty value,asdl_seq * generators,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2800 DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
2801 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
2802 {
2803 expr_ty p;
2804 if (!key) {
2805 PyErr_SetString(PyExc_ValueError,
2806 "field 'key' is required for DictComp");
2807 return NULL;
2808 }
2809 if (!value) {
2810 PyErr_SetString(PyExc_ValueError,
2811 "field 'value' is required for DictComp");
2812 return NULL;
2813 }
2814 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2815 if (!p)
2816 return NULL;
2817 p->kind = DictComp_kind;
2818 p->v.DictComp.key = key;
2819 p->v.DictComp.value = value;
2820 p->v.DictComp.generators = generators;
2821 p->lineno = lineno;
2822 p->col_offset = col_offset;
2823 p->end_lineno = end_lineno;
2824 p->end_col_offset = end_col_offset;
2825 return p;
2826 }
2827
2828 expr_ty
GeneratorExp(expr_ty elt,asdl_seq * generators,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2829 GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
2830 int end_lineno, int end_col_offset, PyArena *arena)
2831 {
2832 expr_ty p;
2833 if (!elt) {
2834 PyErr_SetString(PyExc_ValueError,
2835 "field 'elt' is required for GeneratorExp");
2836 return NULL;
2837 }
2838 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2839 if (!p)
2840 return NULL;
2841 p->kind = GeneratorExp_kind;
2842 p->v.GeneratorExp.elt = elt;
2843 p->v.GeneratorExp.generators = generators;
2844 p->lineno = lineno;
2845 p->col_offset = col_offset;
2846 p->end_lineno = end_lineno;
2847 p->end_col_offset = end_col_offset;
2848 return p;
2849 }
2850
2851 expr_ty
Await(expr_ty value,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2852 Await(expr_ty value, int lineno, int col_offset, int end_lineno, int
2853 end_col_offset, PyArena *arena)
2854 {
2855 expr_ty p;
2856 if (!value) {
2857 PyErr_SetString(PyExc_ValueError,
2858 "field 'value' is required for Await");
2859 return NULL;
2860 }
2861 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2862 if (!p)
2863 return NULL;
2864 p->kind = Await_kind;
2865 p->v.Await.value = value;
2866 p->lineno = lineno;
2867 p->col_offset = col_offset;
2868 p->end_lineno = end_lineno;
2869 p->end_col_offset = end_col_offset;
2870 return p;
2871 }
2872
2873 expr_ty
Yield(expr_ty value,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2874 Yield(expr_ty value, int lineno, int col_offset, int end_lineno, int
2875 end_col_offset, PyArena *arena)
2876 {
2877 expr_ty p;
2878 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2879 if (!p)
2880 return NULL;
2881 p->kind = Yield_kind;
2882 p->v.Yield.value = value;
2883 p->lineno = lineno;
2884 p->col_offset = col_offset;
2885 p->end_lineno = end_lineno;
2886 p->end_col_offset = end_col_offset;
2887 return p;
2888 }
2889
2890 expr_ty
YieldFrom(expr_ty value,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2891 YieldFrom(expr_ty value, int lineno, int col_offset, int end_lineno, int
2892 end_col_offset, PyArena *arena)
2893 {
2894 expr_ty p;
2895 if (!value) {
2896 PyErr_SetString(PyExc_ValueError,
2897 "field 'value' is required for YieldFrom");
2898 return NULL;
2899 }
2900 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2901 if (!p)
2902 return NULL;
2903 p->kind = YieldFrom_kind;
2904 p->v.YieldFrom.value = value;
2905 p->lineno = lineno;
2906 p->col_offset = col_offset;
2907 p->end_lineno = end_lineno;
2908 p->end_col_offset = end_col_offset;
2909 return p;
2910 }
2911
2912 expr_ty
Compare(expr_ty left,asdl_int_seq * ops,asdl_seq * comparators,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2913 Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
2914 int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
2915 {
2916 expr_ty p;
2917 if (!left) {
2918 PyErr_SetString(PyExc_ValueError,
2919 "field 'left' is required for Compare");
2920 return NULL;
2921 }
2922 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2923 if (!p)
2924 return NULL;
2925 p->kind = Compare_kind;
2926 p->v.Compare.left = left;
2927 p->v.Compare.ops = ops;
2928 p->v.Compare.comparators = comparators;
2929 p->lineno = lineno;
2930 p->col_offset = col_offset;
2931 p->end_lineno = end_lineno;
2932 p->end_col_offset = end_col_offset;
2933 return p;
2934 }
2935
2936 expr_ty
Call(expr_ty func,asdl_seq * args,asdl_seq * keywords,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2937 Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2938 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
2939 {
2940 expr_ty p;
2941 if (!func) {
2942 PyErr_SetString(PyExc_ValueError,
2943 "field 'func' is required for Call");
2944 return NULL;
2945 }
2946 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2947 if (!p)
2948 return NULL;
2949 p->kind = Call_kind;
2950 p->v.Call.func = func;
2951 p->v.Call.args = args;
2952 p->v.Call.keywords = keywords;
2953 p->lineno = lineno;
2954 p->col_offset = col_offset;
2955 p->end_lineno = end_lineno;
2956 p->end_col_offset = end_col_offset;
2957 return p;
2958 }
2959
2960 expr_ty
FormattedValue(expr_ty value,int conversion,expr_ty format_spec,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2961 FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2962 int col_offset, int end_lineno, int end_col_offset, PyArena
2963 *arena)
2964 {
2965 expr_ty p;
2966 if (!value) {
2967 PyErr_SetString(PyExc_ValueError,
2968 "field 'value' is required for FormattedValue");
2969 return NULL;
2970 }
2971 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2972 if (!p)
2973 return NULL;
2974 p->kind = FormattedValue_kind;
2975 p->v.FormattedValue.value = value;
2976 p->v.FormattedValue.conversion = conversion;
2977 p->v.FormattedValue.format_spec = format_spec;
2978 p->lineno = lineno;
2979 p->col_offset = col_offset;
2980 p->end_lineno = end_lineno;
2981 p->end_col_offset = end_col_offset;
2982 return p;
2983 }
2984
2985 expr_ty
JoinedStr(asdl_seq * values,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)2986 JoinedStr(asdl_seq * values, int lineno, int col_offset, int end_lineno, int
2987 end_col_offset, PyArena *arena)
2988 {
2989 expr_ty p;
2990 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2991 if (!p)
2992 return NULL;
2993 p->kind = JoinedStr_kind;
2994 p->v.JoinedStr.values = values;
2995 p->lineno = lineno;
2996 p->col_offset = col_offset;
2997 p->end_lineno = end_lineno;
2998 p->end_col_offset = end_col_offset;
2999 return p;
3000 }
3001
3002 expr_ty
Constant(constant value,string kind,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)3003 Constant(constant value, string kind, int lineno, int col_offset, int
3004 end_lineno, int end_col_offset, PyArena *arena)
3005 {
3006 expr_ty p;
3007 if (!value) {
3008 PyErr_SetString(PyExc_ValueError,
3009 "field 'value' is required for Constant");
3010 return NULL;
3011 }
3012 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3013 if (!p)
3014 return NULL;
3015 p->kind = Constant_kind;
3016 p->v.Constant.value = value;
3017 p->v.Constant.kind = kind;
3018 p->lineno = lineno;
3019 p->col_offset = col_offset;
3020 p->end_lineno = end_lineno;
3021 p->end_col_offset = end_col_offset;
3022 return p;
3023 }
3024
3025 expr_ty
Attribute(expr_ty value,identifier attr,expr_context_ty ctx,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)3026 Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
3027 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
3028 {
3029 expr_ty p;
3030 if (!value) {
3031 PyErr_SetString(PyExc_ValueError,
3032 "field 'value' is required for Attribute");
3033 return NULL;
3034 }
3035 if (!attr) {
3036 PyErr_SetString(PyExc_ValueError,
3037 "field 'attr' is required for Attribute");
3038 return NULL;
3039 }
3040 if (!ctx) {
3041 PyErr_SetString(PyExc_ValueError,
3042 "field 'ctx' is required for Attribute");
3043 return NULL;
3044 }
3045 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3046 if (!p)
3047 return NULL;
3048 p->kind = Attribute_kind;
3049 p->v.Attribute.value = value;
3050 p->v.Attribute.attr = attr;
3051 p->v.Attribute.ctx = ctx;
3052 p->lineno = lineno;
3053 p->col_offset = col_offset;
3054 p->end_lineno = end_lineno;
3055 p->end_col_offset = end_col_offset;
3056 return p;
3057 }
3058
3059 expr_ty
Subscript(expr_ty value,expr_ty slice,expr_context_ty ctx,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)3060 Subscript(expr_ty value, expr_ty slice, expr_context_ty ctx, int lineno, int
3061 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
3062 {
3063 expr_ty p;
3064 if (!value) {
3065 PyErr_SetString(PyExc_ValueError,
3066 "field 'value' is required for Subscript");
3067 return NULL;
3068 }
3069 if (!slice) {
3070 PyErr_SetString(PyExc_ValueError,
3071 "field 'slice' is required for Subscript");
3072 return NULL;
3073 }
3074 if (!ctx) {
3075 PyErr_SetString(PyExc_ValueError,
3076 "field 'ctx' is required for Subscript");
3077 return NULL;
3078 }
3079 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3080 if (!p)
3081 return NULL;
3082 p->kind = Subscript_kind;
3083 p->v.Subscript.value = value;
3084 p->v.Subscript.slice = slice;
3085 p->v.Subscript.ctx = ctx;
3086 p->lineno = lineno;
3087 p->col_offset = col_offset;
3088 p->end_lineno = end_lineno;
3089 p->end_col_offset = end_col_offset;
3090 return p;
3091 }
3092
3093 expr_ty
Starred(expr_ty value,expr_context_ty ctx,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)3094 Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, int
3095 end_lineno, int end_col_offset, PyArena *arena)
3096 {
3097 expr_ty p;
3098 if (!value) {
3099 PyErr_SetString(PyExc_ValueError,
3100 "field 'value' is required for Starred");
3101 return NULL;
3102 }
3103 if (!ctx) {
3104 PyErr_SetString(PyExc_ValueError,
3105 "field 'ctx' is required for Starred");
3106 return NULL;
3107 }
3108 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3109 if (!p)
3110 return NULL;
3111 p->kind = Starred_kind;
3112 p->v.Starred.value = value;
3113 p->v.Starred.ctx = ctx;
3114 p->lineno = lineno;
3115 p->col_offset = col_offset;
3116 p->end_lineno = end_lineno;
3117 p->end_col_offset = end_col_offset;
3118 return p;
3119 }
3120
3121 expr_ty
Name(identifier id,expr_context_ty ctx,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)3122 Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, int
3123 end_lineno, int end_col_offset, PyArena *arena)
3124 {
3125 expr_ty p;
3126 if (!id) {
3127 PyErr_SetString(PyExc_ValueError,
3128 "field 'id' is required for Name");
3129 return NULL;
3130 }
3131 if (!ctx) {
3132 PyErr_SetString(PyExc_ValueError,
3133 "field 'ctx' is required for Name");
3134 return NULL;
3135 }
3136 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3137 if (!p)
3138 return NULL;
3139 p->kind = Name_kind;
3140 p->v.Name.id = id;
3141 p->v.Name.ctx = ctx;
3142 p->lineno = lineno;
3143 p->col_offset = col_offset;
3144 p->end_lineno = end_lineno;
3145 p->end_col_offset = end_col_offset;
3146 return p;
3147 }
3148
3149 expr_ty
List(asdl_seq * elts,expr_context_ty ctx,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)3150 List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, int
3151 end_lineno, int end_col_offset, PyArena *arena)
3152 {
3153 expr_ty p;
3154 if (!ctx) {
3155 PyErr_SetString(PyExc_ValueError,
3156 "field 'ctx' is required for List");
3157 return NULL;
3158 }
3159 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3160 if (!p)
3161 return NULL;
3162 p->kind = List_kind;
3163 p->v.List.elts = elts;
3164 p->v.List.ctx = ctx;
3165 p->lineno = lineno;
3166 p->col_offset = col_offset;
3167 p->end_lineno = end_lineno;
3168 p->end_col_offset = end_col_offset;
3169 return p;
3170 }
3171
3172 expr_ty
Tuple(asdl_seq * elts,expr_context_ty ctx,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)3173 Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, int
3174 end_lineno, int end_col_offset, PyArena *arena)
3175 {
3176 expr_ty p;
3177 if (!ctx) {
3178 PyErr_SetString(PyExc_ValueError,
3179 "field 'ctx' is required for Tuple");
3180 return NULL;
3181 }
3182 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3183 if (!p)
3184 return NULL;
3185 p->kind = Tuple_kind;
3186 p->v.Tuple.elts = elts;
3187 p->v.Tuple.ctx = ctx;
3188 p->lineno = lineno;
3189 p->col_offset = col_offset;
3190 p->end_lineno = end_lineno;
3191 p->end_col_offset = end_col_offset;
3192 return p;
3193 }
3194
3195 expr_ty
Slice(expr_ty lower,expr_ty upper,expr_ty step,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)3196 Slice(expr_ty lower, expr_ty upper, expr_ty step, int lineno, int col_offset,
3197 int end_lineno, int end_col_offset, PyArena *arena)
3198 {
3199 expr_ty p;
3200 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3201 if (!p)
3202 return NULL;
3203 p->kind = Slice_kind;
3204 p->v.Slice.lower = lower;
3205 p->v.Slice.upper = upper;
3206 p->v.Slice.step = step;
3207 p->lineno = lineno;
3208 p->col_offset = col_offset;
3209 p->end_lineno = end_lineno;
3210 p->end_col_offset = end_col_offset;
3211 return p;
3212 }
3213
3214 comprehension_ty
comprehension(expr_ty target,expr_ty iter,asdl_seq * ifs,int is_async,PyArena * arena)3215 comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, int is_async,
3216 PyArena *arena)
3217 {
3218 comprehension_ty p;
3219 if (!target) {
3220 PyErr_SetString(PyExc_ValueError,
3221 "field 'target' is required for comprehension");
3222 return NULL;
3223 }
3224 if (!iter) {
3225 PyErr_SetString(PyExc_ValueError,
3226 "field 'iter' is required for comprehension");
3227 return NULL;
3228 }
3229 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
3230 if (!p)
3231 return NULL;
3232 p->target = target;
3233 p->iter = iter;
3234 p->ifs = ifs;
3235 p->is_async = is_async;
3236 return p;
3237 }
3238
3239 excepthandler_ty
ExceptHandler(expr_ty type,identifier name,asdl_seq * body,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)3240 ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
3241 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
3242 {
3243 excepthandler_ty p;
3244 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
3245 if (!p)
3246 return NULL;
3247 p->kind = ExceptHandler_kind;
3248 p->v.ExceptHandler.type = type;
3249 p->v.ExceptHandler.name = name;
3250 p->v.ExceptHandler.body = body;
3251 p->lineno = lineno;
3252 p->col_offset = col_offset;
3253 p->end_lineno = end_lineno;
3254 p->end_col_offset = end_col_offset;
3255 return p;
3256 }
3257
3258 arguments_ty
arguments(asdl_seq * posonlyargs,asdl_seq * args,arg_ty vararg,asdl_seq * kwonlyargs,asdl_seq * kw_defaults,arg_ty kwarg,asdl_seq * defaults,PyArena * arena)3259 arguments(asdl_seq * posonlyargs, asdl_seq * args, arg_ty vararg, asdl_seq *
3260 kwonlyargs, asdl_seq * kw_defaults, arg_ty kwarg, asdl_seq *
3261 defaults, PyArena *arena)
3262 {
3263 arguments_ty p;
3264 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
3265 if (!p)
3266 return NULL;
3267 p->posonlyargs = posonlyargs;
3268 p->args = args;
3269 p->vararg = vararg;
3270 p->kwonlyargs = kwonlyargs;
3271 p->kw_defaults = kw_defaults;
3272 p->kwarg = kwarg;
3273 p->defaults = defaults;
3274 return p;
3275 }
3276
3277 arg_ty
arg(identifier arg,expr_ty annotation,string type_comment,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)3278 arg(identifier arg, expr_ty annotation, string type_comment, int lineno, int
3279 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
3280 {
3281 arg_ty p;
3282 if (!arg) {
3283 PyErr_SetString(PyExc_ValueError,
3284 "field 'arg' is required for arg");
3285 return NULL;
3286 }
3287 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
3288 if (!p)
3289 return NULL;
3290 p->arg = arg;
3291 p->annotation = annotation;
3292 p->type_comment = type_comment;
3293 p->lineno = lineno;
3294 p->col_offset = col_offset;
3295 p->end_lineno = end_lineno;
3296 p->end_col_offset = end_col_offset;
3297 return p;
3298 }
3299
3300 keyword_ty
keyword(identifier arg,expr_ty value,int lineno,int col_offset,int end_lineno,int end_col_offset,PyArena * arena)3301 keyword(identifier arg, expr_ty value, int lineno, int col_offset, int
3302 end_lineno, int end_col_offset, PyArena *arena)
3303 {
3304 keyword_ty p;
3305 if (!value) {
3306 PyErr_SetString(PyExc_ValueError,
3307 "field 'value' is required for keyword");
3308 return NULL;
3309 }
3310 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
3311 if (!p)
3312 return NULL;
3313 p->arg = arg;
3314 p->value = value;
3315 p->lineno = lineno;
3316 p->col_offset = col_offset;
3317 p->end_lineno = end_lineno;
3318 p->end_col_offset = end_col_offset;
3319 return p;
3320 }
3321
3322 alias_ty
alias(identifier name,identifier asname,PyArena * arena)3323 alias(identifier name, identifier asname, PyArena *arena)
3324 {
3325 alias_ty p;
3326 if (!name) {
3327 PyErr_SetString(PyExc_ValueError,
3328 "field 'name' is required for alias");
3329 return NULL;
3330 }
3331 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
3332 if (!p)
3333 return NULL;
3334 p->name = name;
3335 p->asname = asname;
3336 return p;
3337 }
3338
3339 withitem_ty
withitem(expr_ty context_expr,expr_ty optional_vars,PyArena * arena)3340 withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
3341 {
3342 withitem_ty p;
3343 if (!context_expr) {
3344 PyErr_SetString(PyExc_ValueError,
3345 "field 'context_expr' is required for withitem");
3346 return NULL;
3347 }
3348 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
3349 if (!p)
3350 return NULL;
3351 p->context_expr = context_expr;
3352 p->optional_vars = optional_vars;
3353 return p;
3354 }
3355
3356 type_ignore_ty
TypeIgnore(int lineno,string tag,PyArena * arena)3357 TypeIgnore(int lineno, string tag, PyArena *arena)
3358 {
3359 type_ignore_ty p;
3360 if (!tag) {
3361 PyErr_SetString(PyExc_ValueError,
3362 "field 'tag' is required for TypeIgnore");
3363 return NULL;
3364 }
3365 p = (type_ignore_ty)PyArena_Malloc(arena, sizeof(*p));
3366 if (!p)
3367 return NULL;
3368 p->kind = TypeIgnore_kind;
3369 p->v.TypeIgnore.lineno = lineno;
3370 p->v.TypeIgnore.tag = tag;
3371 return p;
3372 }
3373
3374
3375 PyObject*
ast2obj_mod(astmodulestate * state,void * _o)3376 ast2obj_mod(astmodulestate *state, void* _o)
3377 {
3378 mod_ty o = (mod_ty)_o;
3379 PyObject *result = NULL, *value = NULL;
3380 PyTypeObject *tp;
3381 if (!o) {
3382 Py_RETURN_NONE;
3383 }
3384 switch (o->kind) {
3385 case Module_kind:
3386 tp = (PyTypeObject *)state->Module_type;
3387 result = PyType_GenericNew(tp, NULL, NULL);
3388 if (!result) goto failed;
3389 value = ast2obj_list(state, o->v.Module.body, ast2obj_stmt);
3390 if (!value) goto failed;
3391 if (PyObject_SetAttr(result, state->body, value) == -1)
3392 goto failed;
3393 Py_DECREF(value);
3394 value = ast2obj_list(state, o->v.Module.type_ignores,
3395 ast2obj_type_ignore);
3396 if (!value) goto failed;
3397 if (PyObject_SetAttr(result, state->type_ignores, value) == -1)
3398 goto failed;
3399 Py_DECREF(value);
3400 break;
3401 case Interactive_kind:
3402 tp = (PyTypeObject *)state->Interactive_type;
3403 result = PyType_GenericNew(tp, NULL, NULL);
3404 if (!result) goto failed;
3405 value = ast2obj_list(state, o->v.Interactive.body, ast2obj_stmt);
3406 if (!value) goto failed;
3407 if (PyObject_SetAttr(result, state->body, value) == -1)
3408 goto failed;
3409 Py_DECREF(value);
3410 break;
3411 case Expression_kind:
3412 tp = (PyTypeObject *)state->Expression_type;
3413 result = PyType_GenericNew(tp, NULL, NULL);
3414 if (!result) goto failed;
3415 value = ast2obj_expr(state, o->v.Expression.body);
3416 if (!value) goto failed;
3417 if (PyObject_SetAttr(result, state->body, value) == -1)
3418 goto failed;
3419 Py_DECREF(value);
3420 break;
3421 case FunctionType_kind:
3422 tp = (PyTypeObject *)state->FunctionType_type;
3423 result = PyType_GenericNew(tp, NULL, NULL);
3424 if (!result) goto failed;
3425 value = ast2obj_list(state, o->v.FunctionType.argtypes, ast2obj_expr);
3426 if (!value) goto failed;
3427 if (PyObject_SetAttr(result, state->argtypes, value) == -1)
3428 goto failed;
3429 Py_DECREF(value);
3430 value = ast2obj_expr(state, o->v.FunctionType.returns);
3431 if (!value) goto failed;
3432 if (PyObject_SetAttr(result, state->returns, value) == -1)
3433 goto failed;
3434 Py_DECREF(value);
3435 break;
3436 }
3437 return result;
3438 failed:
3439 Py_XDECREF(value);
3440 Py_XDECREF(result);
3441 return NULL;
3442 }
3443
3444 PyObject*
ast2obj_stmt(astmodulestate * state,void * _o)3445 ast2obj_stmt(astmodulestate *state, void* _o)
3446 {
3447 stmt_ty o = (stmt_ty)_o;
3448 PyObject *result = NULL, *value = NULL;
3449 PyTypeObject *tp;
3450 if (!o) {
3451 Py_RETURN_NONE;
3452 }
3453 switch (o->kind) {
3454 case FunctionDef_kind:
3455 tp = (PyTypeObject *)state->FunctionDef_type;
3456 result = PyType_GenericNew(tp, NULL, NULL);
3457 if (!result) goto failed;
3458 value = ast2obj_identifier(state, o->v.FunctionDef.name);
3459 if (!value) goto failed;
3460 if (PyObject_SetAttr(result, state->name, value) == -1)
3461 goto failed;
3462 Py_DECREF(value);
3463 value = ast2obj_arguments(state, o->v.FunctionDef.args);
3464 if (!value) goto failed;
3465 if (PyObject_SetAttr(result, state->args, value) == -1)
3466 goto failed;
3467 Py_DECREF(value);
3468 value = ast2obj_list(state, o->v.FunctionDef.body, ast2obj_stmt);
3469 if (!value) goto failed;
3470 if (PyObject_SetAttr(result, state->body, value) == -1)
3471 goto failed;
3472 Py_DECREF(value);
3473 value = ast2obj_list(state, o->v.FunctionDef.decorator_list,
3474 ast2obj_expr);
3475 if (!value) goto failed;
3476 if (PyObject_SetAttr(result, state->decorator_list, value) == -1)
3477 goto failed;
3478 Py_DECREF(value);
3479 value = ast2obj_expr(state, o->v.FunctionDef.returns);
3480 if (!value) goto failed;
3481 if (PyObject_SetAttr(result, state->returns, value) == -1)
3482 goto failed;
3483 Py_DECREF(value);
3484 value = ast2obj_string(state, o->v.FunctionDef.type_comment);
3485 if (!value) goto failed;
3486 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
3487 goto failed;
3488 Py_DECREF(value);
3489 break;
3490 case AsyncFunctionDef_kind:
3491 tp = (PyTypeObject *)state->AsyncFunctionDef_type;
3492 result = PyType_GenericNew(tp, NULL, NULL);
3493 if (!result) goto failed;
3494 value = ast2obj_identifier(state, o->v.AsyncFunctionDef.name);
3495 if (!value) goto failed;
3496 if (PyObject_SetAttr(result, state->name, value) == -1)
3497 goto failed;
3498 Py_DECREF(value);
3499 value = ast2obj_arguments(state, o->v.AsyncFunctionDef.args);
3500 if (!value) goto failed;
3501 if (PyObject_SetAttr(result, state->args, value) == -1)
3502 goto failed;
3503 Py_DECREF(value);
3504 value = ast2obj_list(state, o->v.AsyncFunctionDef.body, ast2obj_stmt);
3505 if (!value) goto failed;
3506 if (PyObject_SetAttr(result, state->body, value) == -1)
3507 goto failed;
3508 Py_DECREF(value);
3509 value = ast2obj_list(state, o->v.AsyncFunctionDef.decorator_list,
3510 ast2obj_expr);
3511 if (!value) goto failed;
3512 if (PyObject_SetAttr(result, state->decorator_list, value) == -1)
3513 goto failed;
3514 Py_DECREF(value);
3515 value = ast2obj_expr(state, o->v.AsyncFunctionDef.returns);
3516 if (!value) goto failed;
3517 if (PyObject_SetAttr(result, state->returns, value) == -1)
3518 goto failed;
3519 Py_DECREF(value);
3520 value = ast2obj_string(state, o->v.AsyncFunctionDef.type_comment);
3521 if (!value) goto failed;
3522 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
3523 goto failed;
3524 Py_DECREF(value);
3525 break;
3526 case ClassDef_kind:
3527 tp = (PyTypeObject *)state->ClassDef_type;
3528 result = PyType_GenericNew(tp, NULL, NULL);
3529 if (!result) goto failed;
3530 value = ast2obj_identifier(state, o->v.ClassDef.name);
3531 if (!value) goto failed;
3532 if (PyObject_SetAttr(result, state->name, value) == -1)
3533 goto failed;
3534 Py_DECREF(value);
3535 value = ast2obj_list(state, o->v.ClassDef.bases, ast2obj_expr);
3536 if (!value) goto failed;
3537 if (PyObject_SetAttr(result, state->bases, value) == -1)
3538 goto failed;
3539 Py_DECREF(value);
3540 value = ast2obj_list(state, o->v.ClassDef.keywords, ast2obj_keyword);
3541 if (!value) goto failed;
3542 if (PyObject_SetAttr(result, state->keywords, value) == -1)
3543 goto failed;
3544 Py_DECREF(value);
3545 value = ast2obj_list(state, o->v.ClassDef.body, ast2obj_stmt);
3546 if (!value) goto failed;
3547 if (PyObject_SetAttr(result, state->body, value) == -1)
3548 goto failed;
3549 Py_DECREF(value);
3550 value = ast2obj_list(state, o->v.ClassDef.decorator_list, ast2obj_expr);
3551 if (!value) goto failed;
3552 if (PyObject_SetAttr(result, state->decorator_list, value) == -1)
3553 goto failed;
3554 Py_DECREF(value);
3555 break;
3556 case Return_kind:
3557 tp = (PyTypeObject *)state->Return_type;
3558 result = PyType_GenericNew(tp, NULL, NULL);
3559 if (!result) goto failed;
3560 value = ast2obj_expr(state, o->v.Return.value);
3561 if (!value) goto failed;
3562 if (PyObject_SetAttr(result, state->value, value) == -1)
3563 goto failed;
3564 Py_DECREF(value);
3565 break;
3566 case Delete_kind:
3567 tp = (PyTypeObject *)state->Delete_type;
3568 result = PyType_GenericNew(tp, NULL, NULL);
3569 if (!result) goto failed;
3570 value = ast2obj_list(state, o->v.Delete.targets, ast2obj_expr);
3571 if (!value) goto failed;
3572 if (PyObject_SetAttr(result, state->targets, value) == -1)
3573 goto failed;
3574 Py_DECREF(value);
3575 break;
3576 case Assign_kind:
3577 tp = (PyTypeObject *)state->Assign_type;
3578 result = PyType_GenericNew(tp, NULL, NULL);
3579 if (!result) goto failed;
3580 value = ast2obj_list(state, o->v.Assign.targets, ast2obj_expr);
3581 if (!value) goto failed;
3582 if (PyObject_SetAttr(result, state->targets, value) == -1)
3583 goto failed;
3584 Py_DECREF(value);
3585 value = ast2obj_expr(state, o->v.Assign.value);
3586 if (!value) goto failed;
3587 if (PyObject_SetAttr(result, state->value, value) == -1)
3588 goto failed;
3589 Py_DECREF(value);
3590 value = ast2obj_string(state, o->v.Assign.type_comment);
3591 if (!value) goto failed;
3592 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
3593 goto failed;
3594 Py_DECREF(value);
3595 break;
3596 case AugAssign_kind:
3597 tp = (PyTypeObject *)state->AugAssign_type;
3598 result = PyType_GenericNew(tp, NULL, NULL);
3599 if (!result) goto failed;
3600 value = ast2obj_expr(state, o->v.AugAssign.target);
3601 if (!value) goto failed;
3602 if (PyObject_SetAttr(result, state->target, value) == -1)
3603 goto failed;
3604 Py_DECREF(value);
3605 value = ast2obj_operator(state, o->v.AugAssign.op);
3606 if (!value) goto failed;
3607 if (PyObject_SetAttr(result, state->op, value) == -1)
3608 goto failed;
3609 Py_DECREF(value);
3610 value = ast2obj_expr(state, o->v.AugAssign.value);
3611 if (!value) goto failed;
3612 if (PyObject_SetAttr(result, state->value, value) == -1)
3613 goto failed;
3614 Py_DECREF(value);
3615 break;
3616 case AnnAssign_kind:
3617 tp = (PyTypeObject *)state->AnnAssign_type;
3618 result = PyType_GenericNew(tp, NULL, NULL);
3619 if (!result) goto failed;
3620 value = ast2obj_expr(state, o->v.AnnAssign.target);
3621 if (!value) goto failed;
3622 if (PyObject_SetAttr(result, state->target, value) == -1)
3623 goto failed;
3624 Py_DECREF(value);
3625 value = ast2obj_expr(state, o->v.AnnAssign.annotation);
3626 if (!value) goto failed;
3627 if (PyObject_SetAttr(result, state->annotation, value) == -1)
3628 goto failed;
3629 Py_DECREF(value);
3630 value = ast2obj_expr(state, o->v.AnnAssign.value);
3631 if (!value) goto failed;
3632 if (PyObject_SetAttr(result, state->value, value) == -1)
3633 goto failed;
3634 Py_DECREF(value);
3635 value = ast2obj_int(state, o->v.AnnAssign.simple);
3636 if (!value) goto failed;
3637 if (PyObject_SetAttr(result, state->simple, value) == -1)
3638 goto failed;
3639 Py_DECREF(value);
3640 break;
3641 case For_kind:
3642 tp = (PyTypeObject *)state->For_type;
3643 result = PyType_GenericNew(tp, NULL, NULL);
3644 if (!result) goto failed;
3645 value = ast2obj_expr(state, o->v.For.target);
3646 if (!value) goto failed;
3647 if (PyObject_SetAttr(result, state->target, value) == -1)
3648 goto failed;
3649 Py_DECREF(value);
3650 value = ast2obj_expr(state, o->v.For.iter);
3651 if (!value) goto failed;
3652 if (PyObject_SetAttr(result, state->iter, value) == -1)
3653 goto failed;
3654 Py_DECREF(value);
3655 value = ast2obj_list(state, o->v.For.body, ast2obj_stmt);
3656 if (!value) goto failed;
3657 if (PyObject_SetAttr(result, state->body, value) == -1)
3658 goto failed;
3659 Py_DECREF(value);
3660 value = ast2obj_list(state, o->v.For.orelse, ast2obj_stmt);
3661 if (!value) goto failed;
3662 if (PyObject_SetAttr(result, state->orelse, value) == -1)
3663 goto failed;
3664 Py_DECREF(value);
3665 value = ast2obj_string(state, o->v.For.type_comment);
3666 if (!value) goto failed;
3667 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
3668 goto failed;
3669 Py_DECREF(value);
3670 break;
3671 case AsyncFor_kind:
3672 tp = (PyTypeObject *)state->AsyncFor_type;
3673 result = PyType_GenericNew(tp, NULL, NULL);
3674 if (!result) goto failed;
3675 value = ast2obj_expr(state, o->v.AsyncFor.target);
3676 if (!value) goto failed;
3677 if (PyObject_SetAttr(result, state->target, value) == -1)
3678 goto failed;
3679 Py_DECREF(value);
3680 value = ast2obj_expr(state, o->v.AsyncFor.iter);
3681 if (!value) goto failed;
3682 if (PyObject_SetAttr(result, state->iter, value) == -1)
3683 goto failed;
3684 Py_DECREF(value);
3685 value = ast2obj_list(state, o->v.AsyncFor.body, ast2obj_stmt);
3686 if (!value) goto failed;
3687 if (PyObject_SetAttr(result, state->body, value) == -1)
3688 goto failed;
3689 Py_DECREF(value);
3690 value = ast2obj_list(state, o->v.AsyncFor.orelse, ast2obj_stmt);
3691 if (!value) goto failed;
3692 if (PyObject_SetAttr(result, state->orelse, value) == -1)
3693 goto failed;
3694 Py_DECREF(value);
3695 value = ast2obj_string(state, o->v.AsyncFor.type_comment);
3696 if (!value) goto failed;
3697 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
3698 goto failed;
3699 Py_DECREF(value);
3700 break;
3701 case While_kind:
3702 tp = (PyTypeObject *)state->While_type;
3703 result = PyType_GenericNew(tp, NULL, NULL);
3704 if (!result) goto failed;
3705 value = ast2obj_expr(state, o->v.While.test);
3706 if (!value) goto failed;
3707 if (PyObject_SetAttr(result, state->test, value) == -1)
3708 goto failed;
3709 Py_DECREF(value);
3710 value = ast2obj_list(state, o->v.While.body, ast2obj_stmt);
3711 if (!value) goto failed;
3712 if (PyObject_SetAttr(result, state->body, value) == -1)
3713 goto failed;
3714 Py_DECREF(value);
3715 value = ast2obj_list(state, o->v.While.orelse, ast2obj_stmt);
3716 if (!value) goto failed;
3717 if (PyObject_SetAttr(result, state->orelse, value) == -1)
3718 goto failed;
3719 Py_DECREF(value);
3720 break;
3721 case If_kind:
3722 tp = (PyTypeObject *)state->If_type;
3723 result = PyType_GenericNew(tp, NULL, NULL);
3724 if (!result) goto failed;
3725 value = ast2obj_expr(state, o->v.If.test);
3726 if (!value) goto failed;
3727 if (PyObject_SetAttr(result, state->test, value) == -1)
3728 goto failed;
3729 Py_DECREF(value);
3730 value = ast2obj_list(state, o->v.If.body, ast2obj_stmt);
3731 if (!value) goto failed;
3732 if (PyObject_SetAttr(result, state->body, value) == -1)
3733 goto failed;
3734 Py_DECREF(value);
3735 value = ast2obj_list(state, o->v.If.orelse, ast2obj_stmt);
3736 if (!value) goto failed;
3737 if (PyObject_SetAttr(result, state->orelse, value) == -1)
3738 goto failed;
3739 Py_DECREF(value);
3740 break;
3741 case With_kind:
3742 tp = (PyTypeObject *)state->With_type;
3743 result = PyType_GenericNew(tp, NULL, NULL);
3744 if (!result) goto failed;
3745 value = ast2obj_list(state, o->v.With.items, ast2obj_withitem);
3746 if (!value) goto failed;
3747 if (PyObject_SetAttr(result, state->items, value) == -1)
3748 goto failed;
3749 Py_DECREF(value);
3750 value = ast2obj_list(state, o->v.With.body, ast2obj_stmt);
3751 if (!value) goto failed;
3752 if (PyObject_SetAttr(result, state->body, value) == -1)
3753 goto failed;
3754 Py_DECREF(value);
3755 value = ast2obj_string(state, o->v.With.type_comment);
3756 if (!value) goto failed;
3757 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
3758 goto failed;
3759 Py_DECREF(value);
3760 break;
3761 case AsyncWith_kind:
3762 tp = (PyTypeObject *)state->AsyncWith_type;
3763 result = PyType_GenericNew(tp, NULL, NULL);
3764 if (!result) goto failed;
3765 value = ast2obj_list(state, o->v.AsyncWith.items, ast2obj_withitem);
3766 if (!value) goto failed;
3767 if (PyObject_SetAttr(result, state->items, value) == -1)
3768 goto failed;
3769 Py_DECREF(value);
3770 value = ast2obj_list(state, o->v.AsyncWith.body, ast2obj_stmt);
3771 if (!value) goto failed;
3772 if (PyObject_SetAttr(result, state->body, value) == -1)
3773 goto failed;
3774 Py_DECREF(value);
3775 value = ast2obj_string(state, o->v.AsyncWith.type_comment);
3776 if (!value) goto failed;
3777 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
3778 goto failed;
3779 Py_DECREF(value);
3780 break;
3781 case Raise_kind:
3782 tp = (PyTypeObject *)state->Raise_type;
3783 result = PyType_GenericNew(tp, NULL, NULL);
3784 if (!result) goto failed;
3785 value = ast2obj_expr(state, o->v.Raise.exc);
3786 if (!value) goto failed;
3787 if (PyObject_SetAttr(result, state->exc, value) == -1)
3788 goto failed;
3789 Py_DECREF(value);
3790 value = ast2obj_expr(state, o->v.Raise.cause);
3791 if (!value) goto failed;
3792 if (PyObject_SetAttr(result, state->cause, value) == -1)
3793 goto failed;
3794 Py_DECREF(value);
3795 break;
3796 case Try_kind:
3797 tp = (PyTypeObject *)state->Try_type;
3798 result = PyType_GenericNew(tp, NULL, NULL);
3799 if (!result) goto failed;
3800 value = ast2obj_list(state, o->v.Try.body, ast2obj_stmt);
3801 if (!value) goto failed;
3802 if (PyObject_SetAttr(result, state->body, value) == -1)
3803 goto failed;
3804 Py_DECREF(value);
3805 value = ast2obj_list(state, o->v.Try.handlers, ast2obj_excepthandler);
3806 if (!value) goto failed;
3807 if (PyObject_SetAttr(result, state->handlers, value) == -1)
3808 goto failed;
3809 Py_DECREF(value);
3810 value = ast2obj_list(state, o->v.Try.orelse, ast2obj_stmt);
3811 if (!value) goto failed;
3812 if (PyObject_SetAttr(result, state->orelse, value) == -1)
3813 goto failed;
3814 Py_DECREF(value);
3815 value = ast2obj_list(state, o->v.Try.finalbody, ast2obj_stmt);
3816 if (!value) goto failed;
3817 if (PyObject_SetAttr(result, state->finalbody, value) == -1)
3818 goto failed;
3819 Py_DECREF(value);
3820 break;
3821 case Assert_kind:
3822 tp = (PyTypeObject *)state->Assert_type;
3823 result = PyType_GenericNew(tp, NULL, NULL);
3824 if (!result) goto failed;
3825 value = ast2obj_expr(state, o->v.Assert.test);
3826 if (!value) goto failed;
3827 if (PyObject_SetAttr(result, state->test, value) == -1)
3828 goto failed;
3829 Py_DECREF(value);
3830 value = ast2obj_expr(state, o->v.Assert.msg);
3831 if (!value) goto failed;
3832 if (PyObject_SetAttr(result, state->msg, value) == -1)
3833 goto failed;
3834 Py_DECREF(value);
3835 break;
3836 case Import_kind:
3837 tp = (PyTypeObject *)state->Import_type;
3838 result = PyType_GenericNew(tp, NULL, NULL);
3839 if (!result) goto failed;
3840 value = ast2obj_list(state, o->v.Import.names, ast2obj_alias);
3841 if (!value) goto failed;
3842 if (PyObject_SetAttr(result, state->names, value) == -1)
3843 goto failed;
3844 Py_DECREF(value);
3845 break;
3846 case ImportFrom_kind:
3847 tp = (PyTypeObject *)state->ImportFrom_type;
3848 result = PyType_GenericNew(tp, NULL, NULL);
3849 if (!result) goto failed;
3850 value = ast2obj_identifier(state, o->v.ImportFrom.module);
3851 if (!value) goto failed;
3852 if (PyObject_SetAttr(result, state->module, value) == -1)
3853 goto failed;
3854 Py_DECREF(value);
3855 value = ast2obj_list(state, o->v.ImportFrom.names, ast2obj_alias);
3856 if (!value) goto failed;
3857 if (PyObject_SetAttr(result, state->names, value) == -1)
3858 goto failed;
3859 Py_DECREF(value);
3860 value = ast2obj_int(state, o->v.ImportFrom.level);
3861 if (!value) goto failed;
3862 if (PyObject_SetAttr(result, state->level, value) == -1)
3863 goto failed;
3864 Py_DECREF(value);
3865 break;
3866 case Global_kind:
3867 tp = (PyTypeObject *)state->Global_type;
3868 result = PyType_GenericNew(tp, NULL, NULL);
3869 if (!result) goto failed;
3870 value = ast2obj_list(state, o->v.Global.names, ast2obj_identifier);
3871 if (!value) goto failed;
3872 if (PyObject_SetAttr(result, state->names, value) == -1)
3873 goto failed;
3874 Py_DECREF(value);
3875 break;
3876 case Nonlocal_kind:
3877 tp = (PyTypeObject *)state->Nonlocal_type;
3878 result = PyType_GenericNew(tp, NULL, NULL);
3879 if (!result) goto failed;
3880 value = ast2obj_list(state, o->v.Nonlocal.names, ast2obj_identifier);
3881 if (!value) goto failed;
3882 if (PyObject_SetAttr(result, state->names, value) == -1)
3883 goto failed;
3884 Py_DECREF(value);
3885 break;
3886 case Expr_kind:
3887 tp = (PyTypeObject *)state->Expr_type;
3888 result = PyType_GenericNew(tp, NULL, NULL);
3889 if (!result) goto failed;
3890 value = ast2obj_expr(state, o->v.Expr.value);
3891 if (!value) goto failed;
3892 if (PyObject_SetAttr(result, state->value, value) == -1)
3893 goto failed;
3894 Py_DECREF(value);
3895 break;
3896 case Pass_kind:
3897 tp = (PyTypeObject *)state->Pass_type;
3898 result = PyType_GenericNew(tp, NULL, NULL);
3899 if (!result) goto failed;
3900 break;
3901 case Break_kind:
3902 tp = (PyTypeObject *)state->Break_type;
3903 result = PyType_GenericNew(tp, NULL, NULL);
3904 if (!result) goto failed;
3905 break;
3906 case Continue_kind:
3907 tp = (PyTypeObject *)state->Continue_type;
3908 result = PyType_GenericNew(tp, NULL, NULL);
3909 if (!result) goto failed;
3910 break;
3911 }
3912 value = ast2obj_int(state, o->lineno);
3913 if (!value) goto failed;
3914 if (PyObject_SetAttr(result, state->lineno, value) < 0)
3915 goto failed;
3916 Py_DECREF(value);
3917 value = ast2obj_int(state, o->col_offset);
3918 if (!value) goto failed;
3919 if (PyObject_SetAttr(result, state->col_offset, value) < 0)
3920 goto failed;
3921 Py_DECREF(value);
3922 value = ast2obj_int(state, o->end_lineno);
3923 if (!value) goto failed;
3924 if (PyObject_SetAttr(result, state->end_lineno, value) < 0)
3925 goto failed;
3926 Py_DECREF(value);
3927 value = ast2obj_int(state, o->end_col_offset);
3928 if (!value) goto failed;
3929 if (PyObject_SetAttr(result, state->end_col_offset, value) < 0)
3930 goto failed;
3931 Py_DECREF(value);
3932 return result;
3933 failed:
3934 Py_XDECREF(value);
3935 Py_XDECREF(result);
3936 return NULL;
3937 }
3938
3939 PyObject*
ast2obj_expr(astmodulestate * state,void * _o)3940 ast2obj_expr(astmodulestate *state, void* _o)
3941 {
3942 expr_ty o = (expr_ty)_o;
3943 PyObject *result = NULL, *value = NULL;
3944 PyTypeObject *tp;
3945 if (!o) {
3946 Py_RETURN_NONE;
3947 }
3948 switch (o->kind) {
3949 case BoolOp_kind:
3950 tp = (PyTypeObject *)state->BoolOp_type;
3951 result = PyType_GenericNew(tp, NULL, NULL);
3952 if (!result) goto failed;
3953 value = ast2obj_boolop(state, o->v.BoolOp.op);
3954 if (!value) goto failed;
3955 if (PyObject_SetAttr(result, state->op, value) == -1)
3956 goto failed;
3957 Py_DECREF(value);
3958 value = ast2obj_list(state, o->v.BoolOp.values, ast2obj_expr);
3959 if (!value) goto failed;
3960 if (PyObject_SetAttr(result, state->values, value) == -1)
3961 goto failed;
3962 Py_DECREF(value);
3963 break;
3964 case NamedExpr_kind:
3965 tp = (PyTypeObject *)state->NamedExpr_type;
3966 result = PyType_GenericNew(tp, NULL, NULL);
3967 if (!result) goto failed;
3968 value = ast2obj_expr(state, o->v.NamedExpr.target);
3969 if (!value) goto failed;
3970 if (PyObject_SetAttr(result, state->target, value) == -1)
3971 goto failed;
3972 Py_DECREF(value);
3973 value = ast2obj_expr(state, o->v.NamedExpr.value);
3974 if (!value) goto failed;
3975 if (PyObject_SetAttr(result, state->value, value) == -1)
3976 goto failed;
3977 Py_DECREF(value);
3978 break;
3979 case BinOp_kind:
3980 tp = (PyTypeObject *)state->BinOp_type;
3981 result = PyType_GenericNew(tp, NULL, NULL);
3982 if (!result) goto failed;
3983 value = ast2obj_expr(state, o->v.BinOp.left);
3984 if (!value) goto failed;
3985 if (PyObject_SetAttr(result, state->left, value) == -1)
3986 goto failed;
3987 Py_DECREF(value);
3988 value = ast2obj_operator(state, o->v.BinOp.op);
3989 if (!value) goto failed;
3990 if (PyObject_SetAttr(result, state->op, value) == -1)
3991 goto failed;
3992 Py_DECREF(value);
3993 value = ast2obj_expr(state, o->v.BinOp.right);
3994 if (!value) goto failed;
3995 if (PyObject_SetAttr(result, state->right, value) == -1)
3996 goto failed;
3997 Py_DECREF(value);
3998 break;
3999 case UnaryOp_kind:
4000 tp = (PyTypeObject *)state->UnaryOp_type;
4001 result = PyType_GenericNew(tp, NULL, NULL);
4002 if (!result) goto failed;
4003 value = ast2obj_unaryop(state, o->v.UnaryOp.op);
4004 if (!value) goto failed;
4005 if (PyObject_SetAttr(result, state->op, value) == -1)
4006 goto failed;
4007 Py_DECREF(value);
4008 value = ast2obj_expr(state, o->v.UnaryOp.operand);
4009 if (!value) goto failed;
4010 if (PyObject_SetAttr(result, state->operand, value) == -1)
4011 goto failed;
4012 Py_DECREF(value);
4013 break;
4014 case Lambda_kind:
4015 tp = (PyTypeObject *)state->Lambda_type;
4016 result = PyType_GenericNew(tp, NULL, NULL);
4017 if (!result) goto failed;
4018 value = ast2obj_arguments(state, o->v.Lambda.args);
4019 if (!value) goto failed;
4020 if (PyObject_SetAttr(result, state->args, value) == -1)
4021 goto failed;
4022 Py_DECREF(value);
4023 value = ast2obj_expr(state, o->v.Lambda.body);
4024 if (!value) goto failed;
4025 if (PyObject_SetAttr(result, state->body, value) == -1)
4026 goto failed;
4027 Py_DECREF(value);
4028 break;
4029 case IfExp_kind:
4030 tp = (PyTypeObject *)state->IfExp_type;
4031 result = PyType_GenericNew(tp, NULL, NULL);
4032 if (!result) goto failed;
4033 value = ast2obj_expr(state, o->v.IfExp.test);
4034 if (!value) goto failed;
4035 if (PyObject_SetAttr(result, state->test, value) == -1)
4036 goto failed;
4037 Py_DECREF(value);
4038 value = ast2obj_expr(state, o->v.IfExp.body);
4039 if (!value) goto failed;
4040 if (PyObject_SetAttr(result, state->body, value) == -1)
4041 goto failed;
4042 Py_DECREF(value);
4043 value = ast2obj_expr(state, o->v.IfExp.orelse);
4044 if (!value) goto failed;
4045 if (PyObject_SetAttr(result, state->orelse, value) == -1)
4046 goto failed;
4047 Py_DECREF(value);
4048 break;
4049 case Dict_kind:
4050 tp = (PyTypeObject *)state->Dict_type;
4051 result = PyType_GenericNew(tp, NULL, NULL);
4052 if (!result) goto failed;
4053 value = ast2obj_list(state, o->v.Dict.keys, ast2obj_expr);
4054 if (!value) goto failed;
4055 if (PyObject_SetAttr(result, state->keys, value) == -1)
4056 goto failed;
4057 Py_DECREF(value);
4058 value = ast2obj_list(state, o->v.Dict.values, ast2obj_expr);
4059 if (!value) goto failed;
4060 if (PyObject_SetAttr(result, state->values, value) == -1)
4061 goto failed;
4062 Py_DECREF(value);
4063 break;
4064 case Set_kind:
4065 tp = (PyTypeObject *)state->Set_type;
4066 result = PyType_GenericNew(tp, NULL, NULL);
4067 if (!result) goto failed;
4068 value = ast2obj_list(state, o->v.Set.elts, ast2obj_expr);
4069 if (!value) goto failed;
4070 if (PyObject_SetAttr(result, state->elts, value) == -1)
4071 goto failed;
4072 Py_DECREF(value);
4073 break;
4074 case ListComp_kind:
4075 tp = (PyTypeObject *)state->ListComp_type;
4076 result = PyType_GenericNew(tp, NULL, NULL);
4077 if (!result) goto failed;
4078 value = ast2obj_expr(state, o->v.ListComp.elt);
4079 if (!value) goto failed;
4080 if (PyObject_SetAttr(result, state->elt, value) == -1)
4081 goto failed;
4082 Py_DECREF(value);
4083 value = ast2obj_list(state, o->v.ListComp.generators,
4084 ast2obj_comprehension);
4085 if (!value) goto failed;
4086 if (PyObject_SetAttr(result, state->generators, value) == -1)
4087 goto failed;
4088 Py_DECREF(value);
4089 break;
4090 case SetComp_kind:
4091 tp = (PyTypeObject *)state->SetComp_type;
4092 result = PyType_GenericNew(tp, NULL, NULL);
4093 if (!result) goto failed;
4094 value = ast2obj_expr(state, o->v.SetComp.elt);
4095 if (!value) goto failed;
4096 if (PyObject_SetAttr(result, state->elt, value) == -1)
4097 goto failed;
4098 Py_DECREF(value);
4099 value = ast2obj_list(state, o->v.SetComp.generators,
4100 ast2obj_comprehension);
4101 if (!value) goto failed;
4102 if (PyObject_SetAttr(result, state->generators, value) == -1)
4103 goto failed;
4104 Py_DECREF(value);
4105 break;
4106 case DictComp_kind:
4107 tp = (PyTypeObject *)state->DictComp_type;
4108 result = PyType_GenericNew(tp, NULL, NULL);
4109 if (!result) goto failed;
4110 value = ast2obj_expr(state, o->v.DictComp.key);
4111 if (!value) goto failed;
4112 if (PyObject_SetAttr(result, state->key, value) == -1)
4113 goto failed;
4114 Py_DECREF(value);
4115 value = ast2obj_expr(state, o->v.DictComp.value);
4116 if (!value) goto failed;
4117 if (PyObject_SetAttr(result, state->value, value) == -1)
4118 goto failed;
4119 Py_DECREF(value);
4120 value = ast2obj_list(state, o->v.DictComp.generators,
4121 ast2obj_comprehension);
4122 if (!value) goto failed;
4123 if (PyObject_SetAttr(result, state->generators, value) == -1)
4124 goto failed;
4125 Py_DECREF(value);
4126 break;
4127 case GeneratorExp_kind:
4128 tp = (PyTypeObject *)state->GeneratorExp_type;
4129 result = PyType_GenericNew(tp, NULL, NULL);
4130 if (!result) goto failed;
4131 value = ast2obj_expr(state, o->v.GeneratorExp.elt);
4132 if (!value) goto failed;
4133 if (PyObject_SetAttr(result, state->elt, value) == -1)
4134 goto failed;
4135 Py_DECREF(value);
4136 value = ast2obj_list(state, o->v.GeneratorExp.generators,
4137 ast2obj_comprehension);
4138 if (!value) goto failed;
4139 if (PyObject_SetAttr(result, state->generators, value) == -1)
4140 goto failed;
4141 Py_DECREF(value);
4142 break;
4143 case Await_kind:
4144 tp = (PyTypeObject *)state->Await_type;
4145 result = PyType_GenericNew(tp, NULL, NULL);
4146 if (!result) goto failed;
4147 value = ast2obj_expr(state, o->v.Await.value);
4148 if (!value) goto failed;
4149 if (PyObject_SetAttr(result, state->value, value) == -1)
4150 goto failed;
4151 Py_DECREF(value);
4152 break;
4153 case Yield_kind:
4154 tp = (PyTypeObject *)state->Yield_type;
4155 result = PyType_GenericNew(tp, NULL, NULL);
4156 if (!result) goto failed;
4157 value = ast2obj_expr(state, o->v.Yield.value);
4158 if (!value) goto failed;
4159 if (PyObject_SetAttr(result, state->value, value) == -1)
4160 goto failed;
4161 Py_DECREF(value);
4162 break;
4163 case YieldFrom_kind:
4164 tp = (PyTypeObject *)state->YieldFrom_type;
4165 result = PyType_GenericNew(tp, NULL, NULL);
4166 if (!result) goto failed;
4167 value = ast2obj_expr(state, o->v.YieldFrom.value);
4168 if (!value) goto failed;
4169 if (PyObject_SetAttr(result, state->value, value) == -1)
4170 goto failed;
4171 Py_DECREF(value);
4172 break;
4173 case Compare_kind:
4174 tp = (PyTypeObject *)state->Compare_type;
4175 result = PyType_GenericNew(tp, NULL, NULL);
4176 if (!result) goto failed;
4177 value = ast2obj_expr(state, o->v.Compare.left);
4178 if (!value) goto failed;
4179 if (PyObject_SetAttr(result, state->left, value) == -1)
4180 goto failed;
4181 Py_DECREF(value);
4182 {
4183 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
4184 value = PyList_New(n);
4185 if (!value) goto failed;
4186 for(i = 0; i < n; i++)
4187 PyList_SET_ITEM(value, i, ast2obj_cmpop(state, (cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
4188 }
4189 if (!value) goto failed;
4190 if (PyObject_SetAttr(result, state->ops, value) == -1)
4191 goto failed;
4192 Py_DECREF(value);
4193 value = ast2obj_list(state, o->v.Compare.comparators, ast2obj_expr);
4194 if (!value) goto failed;
4195 if (PyObject_SetAttr(result, state->comparators, value) == -1)
4196 goto failed;
4197 Py_DECREF(value);
4198 break;
4199 case Call_kind:
4200 tp = (PyTypeObject *)state->Call_type;
4201 result = PyType_GenericNew(tp, NULL, NULL);
4202 if (!result) goto failed;
4203 value = ast2obj_expr(state, o->v.Call.func);
4204 if (!value) goto failed;
4205 if (PyObject_SetAttr(result, state->func, value) == -1)
4206 goto failed;
4207 Py_DECREF(value);
4208 value = ast2obj_list(state, o->v.Call.args, ast2obj_expr);
4209 if (!value) goto failed;
4210 if (PyObject_SetAttr(result, state->args, value) == -1)
4211 goto failed;
4212 Py_DECREF(value);
4213 value = ast2obj_list(state, o->v.Call.keywords, ast2obj_keyword);
4214 if (!value) goto failed;
4215 if (PyObject_SetAttr(result, state->keywords, value) == -1)
4216 goto failed;
4217 Py_DECREF(value);
4218 break;
4219 case FormattedValue_kind:
4220 tp = (PyTypeObject *)state->FormattedValue_type;
4221 result = PyType_GenericNew(tp, NULL, NULL);
4222 if (!result) goto failed;
4223 value = ast2obj_expr(state, o->v.FormattedValue.value);
4224 if (!value) goto failed;
4225 if (PyObject_SetAttr(result, state->value, value) == -1)
4226 goto failed;
4227 Py_DECREF(value);
4228 value = ast2obj_int(state, o->v.FormattedValue.conversion);
4229 if (!value) goto failed;
4230 if (PyObject_SetAttr(result, state->conversion, value) == -1)
4231 goto failed;
4232 Py_DECREF(value);
4233 value = ast2obj_expr(state, o->v.FormattedValue.format_spec);
4234 if (!value) goto failed;
4235 if (PyObject_SetAttr(result, state->format_spec, value) == -1)
4236 goto failed;
4237 Py_DECREF(value);
4238 break;
4239 case JoinedStr_kind:
4240 tp = (PyTypeObject *)state->JoinedStr_type;
4241 result = PyType_GenericNew(tp, NULL, NULL);
4242 if (!result) goto failed;
4243 value = ast2obj_list(state, o->v.JoinedStr.values, ast2obj_expr);
4244 if (!value) goto failed;
4245 if (PyObject_SetAttr(result, state->values, value) == -1)
4246 goto failed;
4247 Py_DECREF(value);
4248 break;
4249 case Constant_kind:
4250 tp = (PyTypeObject *)state->Constant_type;
4251 result = PyType_GenericNew(tp, NULL, NULL);
4252 if (!result) goto failed;
4253 value = ast2obj_constant(state, o->v.Constant.value);
4254 if (!value) goto failed;
4255 if (PyObject_SetAttr(result, state->value, value) == -1)
4256 goto failed;
4257 Py_DECREF(value);
4258 value = ast2obj_string(state, o->v.Constant.kind);
4259 if (!value) goto failed;
4260 if (PyObject_SetAttr(result, state->kind, value) == -1)
4261 goto failed;
4262 Py_DECREF(value);
4263 break;
4264 case Attribute_kind:
4265 tp = (PyTypeObject *)state->Attribute_type;
4266 result = PyType_GenericNew(tp, NULL, NULL);
4267 if (!result) goto failed;
4268 value = ast2obj_expr(state, o->v.Attribute.value);
4269 if (!value) goto failed;
4270 if (PyObject_SetAttr(result, state->value, value) == -1)
4271 goto failed;
4272 Py_DECREF(value);
4273 value = ast2obj_identifier(state, o->v.Attribute.attr);
4274 if (!value) goto failed;
4275 if (PyObject_SetAttr(result, state->attr, value) == -1)
4276 goto failed;
4277 Py_DECREF(value);
4278 value = ast2obj_expr_context(state, o->v.Attribute.ctx);
4279 if (!value) goto failed;
4280 if (PyObject_SetAttr(result, state->ctx, value) == -1)
4281 goto failed;
4282 Py_DECREF(value);
4283 break;
4284 case Subscript_kind:
4285 tp = (PyTypeObject *)state->Subscript_type;
4286 result = PyType_GenericNew(tp, NULL, NULL);
4287 if (!result) goto failed;
4288 value = ast2obj_expr(state, o->v.Subscript.value);
4289 if (!value) goto failed;
4290 if (PyObject_SetAttr(result, state->value, value) == -1)
4291 goto failed;
4292 Py_DECREF(value);
4293 value = ast2obj_expr(state, o->v.Subscript.slice);
4294 if (!value) goto failed;
4295 if (PyObject_SetAttr(result, state->slice, value) == -1)
4296 goto failed;
4297 Py_DECREF(value);
4298 value = ast2obj_expr_context(state, o->v.Subscript.ctx);
4299 if (!value) goto failed;
4300 if (PyObject_SetAttr(result, state->ctx, value) == -1)
4301 goto failed;
4302 Py_DECREF(value);
4303 break;
4304 case Starred_kind:
4305 tp = (PyTypeObject *)state->Starred_type;
4306 result = PyType_GenericNew(tp, NULL, NULL);
4307 if (!result) goto failed;
4308 value = ast2obj_expr(state, o->v.Starred.value);
4309 if (!value) goto failed;
4310 if (PyObject_SetAttr(result, state->value, value) == -1)
4311 goto failed;
4312 Py_DECREF(value);
4313 value = ast2obj_expr_context(state, o->v.Starred.ctx);
4314 if (!value) goto failed;
4315 if (PyObject_SetAttr(result, state->ctx, value) == -1)
4316 goto failed;
4317 Py_DECREF(value);
4318 break;
4319 case Name_kind:
4320 tp = (PyTypeObject *)state->Name_type;
4321 result = PyType_GenericNew(tp, NULL, NULL);
4322 if (!result) goto failed;
4323 value = ast2obj_identifier(state, o->v.Name.id);
4324 if (!value) goto failed;
4325 if (PyObject_SetAttr(result, state->id, value) == -1)
4326 goto failed;
4327 Py_DECREF(value);
4328 value = ast2obj_expr_context(state, o->v.Name.ctx);
4329 if (!value) goto failed;
4330 if (PyObject_SetAttr(result, state->ctx, value) == -1)
4331 goto failed;
4332 Py_DECREF(value);
4333 break;
4334 case List_kind:
4335 tp = (PyTypeObject *)state->List_type;
4336 result = PyType_GenericNew(tp, NULL, NULL);
4337 if (!result) goto failed;
4338 value = ast2obj_list(state, o->v.List.elts, ast2obj_expr);
4339 if (!value) goto failed;
4340 if (PyObject_SetAttr(result, state->elts, value) == -1)
4341 goto failed;
4342 Py_DECREF(value);
4343 value = ast2obj_expr_context(state, o->v.List.ctx);
4344 if (!value) goto failed;
4345 if (PyObject_SetAttr(result, state->ctx, value) == -1)
4346 goto failed;
4347 Py_DECREF(value);
4348 break;
4349 case Tuple_kind:
4350 tp = (PyTypeObject *)state->Tuple_type;
4351 result = PyType_GenericNew(tp, NULL, NULL);
4352 if (!result) goto failed;
4353 value = ast2obj_list(state, o->v.Tuple.elts, ast2obj_expr);
4354 if (!value) goto failed;
4355 if (PyObject_SetAttr(result, state->elts, value) == -1)
4356 goto failed;
4357 Py_DECREF(value);
4358 value = ast2obj_expr_context(state, o->v.Tuple.ctx);
4359 if (!value) goto failed;
4360 if (PyObject_SetAttr(result, state->ctx, value) == -1)
4361 goto failed;
4362 Py_DECREF(value);
4363 break;
4364 case Slice_kind:
4365 tp = (PyTypeObject *)state->Slice_type;
4366 result = PyType_GenericNew(tp, NULL, NULL);
4367 if (!result) goto failed;
4368 value = ast2obj_expr(state, o->v.Slice.lower);
4369 if (!value) goto failed;
4370 if (PyObject_SetAttr(result, state->lower, value) == -1)
4371 goto failed;
4372 Py_DECREF(value);
4373 value = ast2obj_expr(state, o->v.Slice.upper);
4374 if (!value) goto failed;
4375 if (PyObject_SetAttr(result, state->upper, value) == -1)
4376 goto failed;
4377 Py_DECREF(value);
4378 value = ast2obj_expr(state, o->v.Slice.step);
4379 if (!value) goto failed;
4380 if (PyObject_SetAttr(result, state->step, value) == -1)
4381 goto failed;
4382 Py_DECREF(value);
4383 break;
4384 }
4385 value = ast2obj_int(state, o->lineno);
4386 if (!value) goto failed;
4387 if (PyObject_SetAttr(result, state->lineno, value) < 0)
4388 goto failed;
4389 Py_DECREF(value);
4390 value = ast2obj_int(state, o->col_offset);
4391 if (!value) goto failed;
4392 if (PyObject_SetAttr(result, state->col_offset, value) < 0)
4393 goto failed;
4394 Py_DECREF(value);
4395 value = ast2obj_int(state, o->end_lineno);
4396 if (!value) goto failed;
4397 if (PyObject_SetAttr(result, state->end_lineno, value) < 0)
4398 goto failed;
4399 Py_DECREF(value);
4400 value = ast2obj_int(state, o->end_col_offset);
4401 if (!value) goto failed;
4402 if (PyObject_SetAttr(result, state->end_col_offset, value) < 0)
4403 goto failed;
4404 Py_DECREF(value);
4405 return result;
4406 failed:
4407 Py_XDECREF(value);
4408 Py_XDECREF(result);
4409 return NULL;
4410 }
4411
ast2obj_expr_context(astmodulestate * state,expr_context_ty o)4412 PyObject* ast2obj_expr_context(astmodulestate *state, expr_context_ty o)
4413 {
4414 switch(o) {
4415 case Load:
4416 Py_INCREF(state->Load_singleton);
4417 return state->Load_singleton;
4418 case Store:
4419 Py_INCREF(state->Store_singleton);
4420 return state->Store_singleton;
4421 case Del:
4422 Py_INCREF(state->Del_singleton);
4423 return state->Del_singleton;
4424 }
4425 Py_UNREACHABLE();
4426 }
ast2obj_boolop(astmodulestate * state,boolop_ty o)4427 PyObject* ast2obj_boolop(astmodulestate *state, boolop_ty o)
4428 {
4429 switch(o) {
4430 case And:
4431 Py_INCREF(state->And_singleton);
4432 return state->And_singleton;
4433 case Or:
4434 Py_INCREF(state->Or_singleton);
4435 return state->Or_singleton;
4436 }
4437 Py_UNREACHABLE();
4438 }
ast2obj_operator(astmodulestate * state,operator_ty o)4439 PyObject* ast2obj_operator(astmodulestate *state, operator_ty o)
4440 {
4441 switch(o) {
4442 case Add:
4443 Py_INCREF(state->Add_singleton);
4444 return state->Add_singleton;
4445 case Sub:
4446 Py_INCREF(state->Sub_singleton);
4447 return state->Sub_singleton;
4448 case Mult:
4449 Py_INCREF(state->Mult_singleton);
4450 return state->Mult_singleton;
4451 case MatMult:
4452 Py_INCREF(state->MatMult_singleton);
4453 return state->MatMult_singleton;
4454 case Div:
4455 Py_INCREF(state->Div_singleton);
4456 return state->Div_singleton;
4457 case Mod:
4458 Py_INCREF(state->Mod_singleton);
4459 return state->Mod_singleton;
4460 case Pow:
4461 Py_INCREF(state->Pow_singleton);
4462 return state->Pow_singleton;
4463 case LShift:
4464 Py_INCREF(state->LShift_singleton);
4465 return state->LShift_singleton;
4466 case RShift:
4467 Py_INCREF(state->RShift_singleton);
4468 return state->RShift_singleton;
4469 case BitOr:
4470 Py_INCREF(state->BitOr_singleton);
4471 return state->BitOr_singleton;
4472 case BitXor:
4473 Py_INCREF(state->BitXor_singleton);
4474 return state->BitXor_singleton;
4475 case BitAnd:
4476 Py_INCREF(state->BitAnd_singleton);
4477 return state->BitAnd_singleton;
4478 case FloorDiv:
4479 Py_INCREF(state->FloorDiv_singleton);
4480 return state->FloorDiv_singleton;
4481 }
4482 Py_UNREACHABLE();
4483 }
ast2obj_unaryop(astmodulestate * state,unaryop_ty o)4484 PyObject* ast2obj_unaryop(astmodulestate *state, unaryop_ty o)
4485 {
4486 switch(o) {
4487 case Invert:
4488 Py_INCREF(state->Invert_singleton);
4489 return state->Invert_singleton;
4490 case Not:
4491 Py_INCREF(state->Not_singleton);
4492 return state->Not_singleton;
4493 case UAdd:
4494 Py_INCREF(state->UAdd_singleton);
4495 return state->UAdd_singleton;
4496 case USub:
4497 Py_INCREF(state->USub_singleton);
4498 return state->USub_singleton;
4499 }
4500 Py_UNREACHABLE();
4501 }
ast2obj_cmpop(astmodulestate * state,cmpop_ty o)4502 PyObject* ast2obj_cmpop(astmodulestate *state, cmpop_ty o)
4503 {
4504 switch(o) {
4505 case Eq:
4506 Py_INCREF(state->Eq_singleton);
4507 return state->Eq_singleton;
4508 case NotEq:
4509 Py_INCREF(state->NotEq_singleton);
4510 return state->NotEq_singleton;
4511 case Lt:
4512 Py_INCREF(state->Lt_singleton);
4513 return state->Lt_singleton;
4514 case LtE:
4515 Py_INCREF(state->LtE_singleton);
4516 return state->LtE_singleton;
4517 case Gt:
4518 Py_INCREF(state->Gt_singleton);
4519 return state->Gt_singleton;
4520 case GtE:
4521 Py_INCREF(state->GtE_singleton);
4522 return state->GtE_singleton;
4523 case Is:
4524 Py_INCREF(state->Is_singleton);
4525 return state->Is_singleton;
4526 case IsNot:
4527 Py_INCREF(state->IsNot_singleton);
4528 return state->IsNot_singleton;
4529 case In:
4530 Py_INCREF(state->In_singleton);
4531 return state->In_singleton;
4532 case NotIn:
4533 Py_INCREF(state->NotIn_singleton);
4534 return state->NotIn_singleton;
4535 }
4536 Py_UNREACHABLE();
4537 }
4538 PyObject*
ast2obj_comprehension(astmodulestate * state,void * _o)4539 ast2obj_comprehension(astmodulestate *state, void* _o)
4540 {
4541 comprehension_ty o = (comprehension_ty)_o;
4542 PyObject *result = NULL, *value = NULL;
4543 PyTypeObject *tp;
4544 if (!o) {
4545 Py_RETURN_NONE;
4546 }
4547 tp = (PyTypeObject *)state->comprehension_type;
4548 result = PyType_GenericNew(tp, NULL, NULL);
4549 if (!result) return NULL;
4550 value = ast2obj_expr(state, o->target);
4551 if (!value) goto failed;
4552 if (PyObject_SetAttr(result, state->target, value) == -1)
4553 goto failed;
4554 Py_DECREF(value);
4555 value = ast2obj_expr(state, o->iter);
4556 if (!value) goto failed;
4557 if (PyObject_SetAttr(result, state->iter, value) == -1)
4558 goto failed;
4559 Py_DECREF(value);
4560 value = ast2obj_list(state, o->ifs, ast2obj_expr);
4561 if (!value) goto failed;
4562 if (PyObject_SetAttr(result, state->ifs, value) == -1)
4563 goto failed;
4564 Py_DECREF(value);
4565 value = ast2obj_int(state, o->is_async);
4566 if (!value) goto failed;
4567 if (PyObject_SetAttr(result, state->is_async, value) == -1)
4568 goto failed;
4569 Py_DECREF(value);
4570 return result;
4571 failed:
4572 Py_XDECREF(value);
4573 Py_XDECREF(result);
4574 return NULL;
4575 }
4576
4577 PyObject*
ast2obj_excepthandler(astmodulestate * state,void * _o)4578 ast2obj_excepthandler(astmodulestate *state, void* _o)
4579 {
4580 excepthandler_ty o = (excepthandler_ty)_o;
4581 PyObject *result = NULL, *value = NULL;
4582 PyTypeObject *tp;
4583 if (!o) {
4584 Py_RETURN_NONE;
4585 }
4586 switch (o->kind) {
4587 case ExceptHandler_kind:
4588 tp = (PyTypeObject *)state->ExceptHandler_type;
4589 result = PyType_GenericNew(tp, NULL, NULL);
4590 if (!result) goto failed;
4591 value = ast2obj_expr(state, o->v.ExceptHandler.type);
4592 if (!value) goto failed;
4593 if (PyObject_SetAttr(result, state->type, value) == -1)
4594 goto failed;
4595 Py_DECREF(value);
4596 value = ast2obj_identifier(state, o->v.ExceptHandler.name);
4597 if (!value) goto failed;
4598 if (PyObject_SetAttr(result, state->name, value) == -1)
4599 goto failed;
4600 Py_DECREF(value);
4601 value = ast2obj_list(state, o->v.ExceptHandler.body, ast2obj_stmt);
4602 if (!value) goto failed;
4603 if (PyObject_SetAttr(result, state->body, value) == -1)
4604 goto failed;
4605 Py_DECREF(value);
4606 break;
4607 }
4608 value = ast2obj_int(state, o->lineno);
4609 if (!value) goto failed;
4610 if (PyObject_SetAttr(result, state->lineno, value) < 0)
4611 goto failed;
4612 Py_DECREF(value);
4613 value = ast2obj_int(state, o->col_offset);
4614 if (!value) goto failed;
4615 if (PyObject_SetAttr(result, state->col_offset, value) < 0)
4616 goto failed;
4617 Py_DECREF(value);
4618 value = ast2obj_int(state, o->end_lineno);
4619 if (!value) goto failed;
4620 if (PyObject_SetAttr(result, state->end_lineno, value) < 0)
4621 goto failed;
4622 Py_DECREF(value);
4623 value = ast2obj_int(state, o->end_col_offset);
4624 if (!value) goto failed;
4625 if (PyObject_SetAttr(result, state->end_col_offset, value) < 0)
4626 goto failed;
4627 Py_DECREF(value);
4628 return result;
4629 failed:
4630 Py_XDECREF(value);
4631 Py_XDECREF(result);
4632 return NULL;
4633 }
4634
4635 PyObject*
ast2obj_arguments(astmodulestate * state,void * _o)4636 ast2obj_arguments(astmodulestate *state, void* _o)
4637 {
4638 arguments_ty o = (arguments_ty)_o;
4639 PyObject *result = NULL, *value = NULL;
4640 PyTypeObject *tp;
4641 if (!o) {
4642 Py_RETURN_NONE;
4643 }
4644 tp = (PyTypeObject *)state->arguments_type;
4645 result = PyType_GenericNew(tp, NULL, NULL);
4646 if (!result) return NULL;
4647 value = ast2obj_list(state, o->posonlyargs, ast2obj_arg);
4648 if (!value) goto failed;
4649 if (PyObject_SetAttr(result, state->posonlyargs, value) == -1)
4650 goto failed;
4651 Py_DECREF(value);
4652 value = ast2obj_list(state, o->args, ast2obj_arg);
4653 if (!value) goto failed;
4654 if (PyObject_SetAttr(result, state->args, value) == -1)
4655 goto failed;
4656 Py_DECREF(value);
4657 value = ast2obj_arg(state, o->vararg);
4658 if (!value) goto failed;
4659 if (PyObject_SetAttr(result, state->vararg, value) == -1)
4660 goto failed;
4661 Py_DECREF(value);
4662 value = ast2obj_list(state, o->kwonlyargs, ast2obj_arg);
4663 if (!value) goto failed;
4664 if (PyObject_SetAttr(result, state->kwonlyargs, value) == -1)
4665 goto failed;
4666 Py_DECREF(value);
4667 value = ast2obj_list(state, o->kw_defaults, ast2obj_expr);
4668 if (!value) goto failed;
4669 if (PyObject_SetAttr(result, state->kw_defaults, value) == -1)
4670 goto failed;
4671 Py_DECREF(value);
4672 value = ast2obj_arg(state, o->kwarg);
4673 if (!value) goto failed;
4674 if (PyObject_SetAttr(result, state->kwarg, value) == -1)
4675 goto failed;
4676 Py_DECREF(value);
4677 value = ast2obj_list(state, o->defaults, ast2obj_expr);
4678 if (!value) goto failed;
4679 if (PyObject_SetAttr(result, state->defaults, value) == -1)
4680 goto failed;
4681 Py_DECREF(value);
4682 return result;
4683 failed:
4684 Py_XDECREF(value);
4685 Py_XDECREF(result);
4686 return NULL;
4687 }
4688
4689 PyObject*
ast2obj_arg(astmodulestate * state,void * _o)4690 ast2obj_arg(astmodulestate *state, void* _o)
4691 {
4692 arg_ty o = (arg_ty)_o;
4693 PyObject *result = NULL, *value = NULL;
4694 PyTypeObject *tp;
4695 if (!o) {
4696 Py_RETURN_NONE;
4697 }
4698 tp = (PyTypeObject *)state->arg_type;
4699 result = PyType_GenericNew(tp, NULL, NULL);
4700 if (!result) return NULL;
4701 value = ast2obj_identifier(state, o->arg);
4702 if (!value) goto failed;
4703 if (PyObject_SetAttr(result, state->arg, value) == -1)
4704 goto failed;
4705 Py_DECREF(value);
4706 value = ast2obj_expr(state, o->annotation);
4707 if (!value) goto failed;
4708 if (PyObject_SetAttr(result, state->annotation, value) == -1)
4709 goto failed;
4710 Py_DECREF(value);
4711 value = ast2obj_string(state, o->type_comment);
4712 if (!value) goto failed;
4713 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
4714 goto failed;
4715 Py_DECREF(value);
4716 value = ast2obj_int(state, o->lineno);
4717 if (!value) goto failed;
4718 if (PyObject_SetAttr(result, state->lineno, value) < 0)
4719 goto failed;
4720 Py_DECREF(value);
4721 value = ast2obj_int(state, o->col_offset);
4722 if (!value) goto failed;
4723 if (PyObject_SetAttr(result, state->col_offset, value) < 0)
4724 goto failed;
4725 Py_DECREF(value);
4726 value = ast2obj_int(state, o->end_lineno);
4727 if (!value) goto failed;
4728 if (PyObject_SetAttr(result, state->end_lineno, value) < 0)
4729 goto failed;
4730 Py_DECREF(value);
4731 value = ast2obj_int(state, o->end_col_offset);
4732 if (!value) goto failed;
4733 if (PyObject_SetAttr(result, state->end_col_offset, value) < 0)
4734 goto failed;
4735 Py_DECREF(value);
4736 return result;
4737 failed:
4738 Py_XDECREF(value);
4739 Py_XDECREF(result);
4740 return NULL;
4741 }
4742
4743 PyObject*
ast2obj_keyword(astmodulestate * state,void * _o)4744 ast2obj_keyword(astmodulestate *state, void* _o)
4745 {
4746 keyword_ty o = (keyword_ty)_o;
4747 PyObject *result = NULL, *value = NULL;
4748 PyTypeObject *tp;
4749 if (!o) {
4750 Py_RETURN_NONE;
4751 }
4752 tp = (PyTypeObject *)state->keyword_type;
4753 result = PyType_GenericNew(tp, NULL, NULL);
4754 if (!result) return NULL;
4755 value = ast2obj_identifier(state, o->arg);
4756 if (!value) goto failed;
4757 if (PyObject_SetAttr(result, state->arg, value) == -1)
4758 goto failed;
4759 Py_DECREF(value);
4760 value = ast2obj_expr(state, o->value);
4761 if (!value) goto failed;
4762 if (PyObject_SetAttr(result, state->value, value) == -1)
4763 goto failed;
4764 Py_DECREF(value);
4765 value = ast2obj_int(state, o->lineno);
4766 if (!value) goto failed;
4767 if (PyObject_SetAttr(result, state->lineno, value) < 0)
4768 goto failed;
4769 Py_DECREF(value);
4770 value = ast2obj_int(state, o->col_offset);
4771 if (!value) goto failed;
4772 if (PyObject_SetAttr(result, state->col_offset, value) < 0)
4773 goto failed;
4774 Py_DECREF(value);
4775 value = ast2obj_int(state, o->end_lineno);
4776 if (!value) goto failed;
4777 if (PyObject_SetAttr(result, state->end_lineno, value) < 0)
4778 goto failed;
4779 Py_DECREF(value);
4780 value = ast2obj_int(state, o->end_col_offset);
4781 if (!value) goto failed;
4782 if (PyObject_SetAttr(result, state->end_col_offset, value) < 0)
4783 goto failed;
4784 Py_DECREF(value);
4785 return result;
4786 failed:
4787 Py_XDECREF(value);
4788 Py_XDECREF(result);
4789 return NULL;
4790 }
4791
4792 PyObject*
ast2obj_alias(astmodulestate * state,void * _o)4793 ast2obj_alias(astmodulestate *state, void* _o)
4794 {
4795 alias_ty o = (alias_ty)_o;
4796 PyObject *result = NULL, *value = NULL;
4797 PyTypeObject *tp;
4798 if (!o) {
4799 Py_RETURN_NONE;
4800 }
4801 tp = (PyTypeObject *)state->alias_type;
4802 result = PyType_GenericNew(tp, NULL, NULL);
4803 if (!result) return NULL;
4804 value = ast2obj_identifier(state, o->name);
4805 if (!value) goto failed;
4806 if (PyObject_SetAttr(result, state->name, value) == -1)
4807 goto failed;
4808 Py_DECREF(value);
4809 value = ast2obj_identifier(state, o->asname);
4810 if (!value) goto failed;
4811 if (PyObject_SetAttr(result, state->asname, value) == -1)
4812 goto failed;
4813 Py_DECREF(value);
4814 return result;
4815 failed:
4816 Py_XDECREF(value);
4817 Py_XDECREF(result);
4818 return NULL;
4819 }
4820
4821 PyObject*
ast2obj_withitem(astmodulestate * state,void * _o)4822 ast2obj_withitem(astmodulestate *state, void* _o)
4823 {
4824 withitem_ty o = (withitem_ty)_o;
4825 PyObject *result = NULL, *value = NULL;
4826 PyTypeObject *tp;
4827 if (!o) {
4828 Py_RETURN_NONE;
4829 }
4830 tp = (PyTypeObject *)state->withitem_type;
4831 result = PyType_GenericNew(tp, NULL, NULL);
4832 if (!result) return NULL;
4833 value = ast2obj_expr(state, o->context_expr);
4834 if (!value) goto failed;
4835 if (PyObject_SetAttr(result, state->context_expr, value) == -1)
4836 goto failed;
4837 Py_DECREF(value);
4838 value = ast2obj_expr(state, o->optional_vars);
4839 if (!value) goto failed;
4840 if (PyObject_SetAttr(result, state->optional_vars, value) == -1)
4841 goto failed;
4842 Py_DECREF(value);
4843 return result;
4844 failed:
4845 Py_XDECREF(value);
4846 Py_XDECREF(result);
4847 return NULL;
4848 }
4849
4850 PyObject*
ast2obj_type_ignore(astmodulestate * state,void * _o)4851 ast2obj_type_ignore(astmodulestate *state, void* _o)
4852 {
4853 type_ignore_ty o = (type_ignore_ty)_o;
4854 PyObject *result = NULL, *value = NULL;
4855 PyTypeObject *tp;
4856 if (!o) {
4857 Py_RETURN_NONE;
4858 }
4859 switch (o->kind) {
4860 case TypeIgnore_kind:
4861 tp = (PyTypeObject *)state->TypeIgnore_type;
4862 result = PyType_GenericNew(tp, NULL, NULL);
4863 if (!result) goto failed;
4864 value = ast2obj_int(state, o->v.TypeIgnore.lineno);
4865 if (!value) goto failed;
4866 if (PyObject_SetAttr(result, state->lineno, value) == -1)
4867 goto failed;
4868 Py_DECREF(value);
4869 value = ast2obj_string(state, o->v.TypeIgnore.tag);
4870 if (!value) goto failed;
4871 if (PyObject_SetAttr(result, state->tag, value) == -1)
4872 goto failed;
4873 Py_DECREF(value);
4874 break;
4875 }
4876 return result;
4877 failed:
4878 Py_XDECREF(value);
4879 Py_XDECREF(result);
4880 return NULL;
4881 }
4882
4883
4884 int
obj2ast_mod(astmodulestate * state,PyObject * obj,mod_ty * out,PyArena * arena)4885 obj2ast_mod(astmodulestate *state, PyObject* obj, mod_ty* out, PyArena* arena)
4886 {
4887 int isinstance;
4888
4889 PyObject *tmp = NULL;
4890 PyObject *tp;
4891
4892 if (obj == Py_None) {
4893 *out = NULL;
4894 return 0;
4895 }
4896 tp = state->Module_type;
4897 isinstance = PyObject_IsInstance(obj, tp);
4898 if (isinstance == -1) {
4899 return 1;
4900 }
4901 if (isinstance) {
4902 asdl_seq* body;
4903 asdl_seq* type_ignores;
4904
4905 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
4906 return 1;
4907 }
4908 if (tmp == NULL) {
4909 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
4910 return 1;
4911 }
4912 else {
4913 int res;
4914 Py_ssize_t len;
4915 Py_ssize_t i;
4916 if (!PyList_Check(tmp)) {
4917 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
4918 goto failed;
4919 }
4920 len = PyList_GET_SIZE(tmp);
4921 body = _Py_asdl_seq_new(len, arena);
4922 if (body == NULL) goto failed;
4923 for (i = 0; i < len; i++) {
4924 stmt_ty val;
4925 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4926 Py_INCREF(tmp2);
4927 res = obj2ast_stmt(state, tmp2, &val, arena);
4928 Py_DECREF(tmp2);
4929 if (res != 0) goto failed;
4930 if (len != PyList_GET_SIZE(tmp)) {
4931 PyErr_SetString(PyExc_RuntimeError, "Module field \"body\" changed size during iteration");
4932 goto failed;
4933 }
4934 asdl_seq_SET(body, i, val);
4935 }
4936 Py_CLEAR(tmp);
4937 }
4938 if (_PyObject_LookupAttr(obj, state->type_ignores, &tmp) < 0) {
4939 return 1;
4940 }
4941 if (tmp == NULL) {
4942 PyErr_SetString(PyExc_TypeError, "required field \"type_ignores\" missing from Module");
4943 return 1;
4944 }
4945 else {
4946 int res;
4947 Py_ssize_t len;
4948 Py_ssize_t i;
4949 if (!PyList_Check(tmp)) {
4950 PyErr_Format(PyExc_TypeError, "Module field \"type_ignores\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
4951 goto failed;
4952 }
4953 len = PyList_GET_SIZE(tmp);
4954 type_ignores = _Py_asdl_seq_new(len, arena);
4955 if (type_ignores == NULL) goto failed;
4956 for (i = 0; i < len; i++) {
4957 type_ignore_ty val;
4958 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4959 Py_INCREF(tmp2);
4960 res = obj2ast_type_ignore(state, tmp2, &val, arena);
4961 Py_DECREF(tmp2);
4962 if (res != 0) goto failed;
4963 if (len != PyList_GET_SIZE(tmp)) {
4964 PyErr_SetString(PyExc_RuntimeError, "Module field \"type_ignores\" changed size during iteration");
4965 goto failed;
4966 }
4967 asdl_seq_SET(type_ignores, i, val);
4968 }
4969 Py_CLEAR(tmp);
4970 }
4971 *out = Module(body, type_ignores, arena);
4972 if (*out == NULL) goto failed;
4973 return 0;
4974 }
4975 tp = state->Interactive_type;
4976 isinstance = PyObject_IsInstance(obj, tp);
4977 if (isinstance == -1) {
4978 return 1;
4979 }
4980 if (isinstance) {
4981 asdl_seq* body;
4982
4983 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
4984 return 1;
4985 }
4986 if (tmp == NULL) {
4987 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
4988 return 1;
4989 }
4990 else {
4991 int res;
4992 Py_ssize_t len;
4993 Py_ssize_t i;
4994 if (!PyList_Check(tmp)) {
4995 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
4996 goto failed;
4997 }
4998 len = PyList_GET_SIZE(tmp);
4999 body = _Py_asdl_seq_new(len, arena);
5000 if (body == NULL) goto failed;
5001 for (i = 0; i < len; i++) {
5002 stmt_ty val;
5003 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5004 Py_INCREF(tmp2);
5005 res = obj2ast_stmt(state, tmp2, &val, arena);
5006 Py_DECREF(tmp2);
5007 if (res != 0) goto failed;
5008 if (len != PyList_GET_SIZE(tmp)) {
5009 PyErr_SetString(PyExc_RuntimeError, "Interactive field \"body\" changed size during iteration");
5010 goto failed;
5011 }
5012 asdl_seq_SET(body, i, val);
5013 }
5014 Py_CLEAR(tmp);
5015 }
5016 *out = Interactive(body, arena);
5017 if (*out == NULL) goto failed;
5018 return 0;
5019 }
5020 tp = state->Expression_type;
5021 isinstance = PyObject_IsInstance(obj, tp);
5022 if (isinstance == -1) {
5023 return 1;
5024 }
5025 if (isinstance) {
5026 expr_ty body;
5027
5028 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
5029 return 1;
5030 }
5031 if (tmp == NULL) {
5032 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
5033 return 1;
5034 }
5035 else {
5036 int res;
5037 res = obj2ast_expr(state, tmp, &body, arena);
5038 if (res != 0) goto failed;
5039 Py_CLEAR(tmp);
5040 }
5041 *out = Expression(body, arena);
5042 if (*out == NULL) goto failed;
5043 return 0;
5044 }
5045 tp = state->FunctionType_type;
5046 isinstance = PyObject_IsInstance(obj, tp);
5047 if (isinstance == -1) {
5048 return 1;
5049 }
5050 if (isinstance) {
5051 asdl_seq* argtypes;
5052 expr_ty returns;
5053
5054 if (_PyObject_LookupAttr(obj, state->argtypes, &tmp) < 0) {
5055 return 1;
5056 }
5057 if (tmp == NULL) {
5058 PyErr_SetString(PyExc_TypeError, "required field \"argtypes\" missing from FunctionType");
5059 return 1;
5060 }
5061 else {
5062 int res;
5063 Py_ssize_t len;
5064 Py_ssize_t i;
5065 if (!PyList_Check(tmp)) {
5066 PyErr_Format(PyExc_TypeError, "FunctionType field \"argtypes\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5067 goto failed;
5068 }
5069 len = PyList_GET_SIZE(tmp);
5070 argtypes = _Py_asdl_seq_new(len, arena);
5071 if (argtypes == NULL) goto failed;
5072 for (i = 0; i < len; i++) {
5073 expr_ty val;
5074 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5075 Py_INCREF(tmp2);
5076 res = obj2ast_expr(state, tmp2, &val, arena);
5077 Py_DECREF(tmp2);
5078 if (res != 0) goto failed;
5079 if (len != PyList_GET_SIZE(tmp)) {
5080 PyErr_SetString(PyExc_RuntimeError, "FunctionType field \"argtypes\" changed size during iteration");
5081 goto failed;
5082 }
5083 asdl_seq_SET(argtypes, i, val);
5084 }
5085 Py_CLEAR(tmp);
5086 }
5087 if (_PyObject_LookupAttr(obj, state->returns, &tmp) < 0) {
5088 return 1;
5089 }
5090 if (tmp == NULL) {
5091 PyErr_SetString(PyExc_TypeError, "required field \"returns\" missing from FunctionType");
5092 return 1;
5093 }
5094 else {
5095 int res;
5096 res = obj2ast_expr(state, tmp, &returns, arena);
5097 if (res != 0) goto failed;
5098 Py_CLEAR(tmp);
5099 }
5100 *out = FunctionType(argtypes, returns, arena);
5101 if (*out == NULL) goto failed;
5102 return 0;
5103 }
5104
5105 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
5106 failed:
5107 Py_XDECREF(tmp);
5108 return 1;
5109 }
5110
5111 int
obj2ast_stmt(astmodulestate * state,PyObject * obj,stmt_ty * out,PyArena * arena)5112 obj2ast_stmt(astmodulestate *state, PyObject* obj, stmt_ty* out, PyArena* arena)
5113 {
5114 int isinstance;
5115
5116 PyObject *tmp = NULL;
5117 PyObject *tp;
5118 int lineno;
5119 int col_offset;
5120 int end_lineno;
5121 int end_col_offset;
5122
5123 if (obj == Py_None) {
5124 *out = NULL;
5125 return 0;
5126 }
5127 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
5128 return 1;
5129 }
5130 if (tmp == NULL) {
5131 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
5132 return 1;
5133 }
5134 else {
5135 int res;
5136 res = obj2ast_int(state, tmp, &lineno, arena);
5137 if (res != 0) goto failed;
5138 Py_CLEAR(tmp);
5139 }
5140 if (_PyObject_LookupAttr(obj, state->col_offset, &tmp) < 0) {
5141 return 1;
5142 }
5143 if (tmp == NULL) {
5144 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
5145 return 1;
5146 }
5147 else {
5148 int res;
5149 res = obj2ast_int(state, tmp, &col_offset, arena);
5150 if (res != 0) goto failed;
5151 Py_CLEAR(tmp);
5152 }
5153 if (_PyObject_LookupAttr(obj, state->end_lineno, &tmp) < 0) {
5154 return 1;
5155 }
5156 if (tmp == NULL || tmp == Py_None) {
5157 Py_CLEAR(tmp);
5158 end_lineno = 0;
5159 }
5160 else {
5161 int res;
5162 res = obj2ast_int(state, tmp, &end_lineno, arena);
5163 if (res != 0) goto failed;
5164 Py_CLEAR(tmp);
5165 }
5166 if (_PyObject_LookupAttr(obj, state->end_col_offset, &tmp) < 0) {
5167 return 1;
5168 }
5169 if (tmp == NULL || tmp == Py_None) {
5170 Py_CLEAR(tmp);
5171 end_col_offset = 0;
5172 }
5173 else {
5174 int res;
5175 res = obj2ast_int(state, tmp, &end_col_offset, arena);
5176 if (res != 0) goto failed;
5177 Py_CLEAR(tmp);
5178 }
5179 tp = state->FunctionDef_type;
5180 isinstance = PyObject_IsInstance(obj, tp);
5181 if (isinstance == -1) {
5182 return 1;
5183 }
5184 if (isinstance) {
5185 identifier name;
5186 arguments_ty args;
5187 asdl_seq* body;
5188 asdl_seq* decorator_list;
5189 expr_ty returns;
5190 string type_comment;
5191
5192 if (_PyObject_LookupAttr(obj, state->name, &tmp) < 0) {
5193 return 1;
5194 }
5195 if (tmp == NULL) {
5196 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
5197 return 1;
5198 }
5199 else {
5200 int res;
5201 res = obj2ast_identifier(state, tmp, &name, arena);
5202 if (res != 0) goto failed;
5203 Py_CLEAR(tmp);
5204 }
5205 if (_PyObject_LookupAttr(obj, state->args, &tmp) < 0) {
5206 return 1;
5207 }
5208 if (tmp == NULL) {
5209 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
5210 return 1;
5211 }
5212 else {
5213 int res;
5214 res = obj2ast_arguments(state, tmp, &args, arena);
5215 if (res != 0) goto failed;
5216 Py_CLEAR(tmp);
5217 }
5218 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
5219 return 1;
5220 }
5221 if (tmp == NULL) {
5222 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
5223 return 1;
5224 }
5225 else {
5226 int res;
5227 Py_ssize_t len;
5228 Py_ssize_t i;
5229 if (!PyList_Check(tmp)) {
5230 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5231 goto failed;
5232 }
5233 len = PyList_GET_SIZE(tmp);
5234 body = _Py_asdl_seq_new(len, arena);
5235 if (body == NULL) goto failed;
5236 for (i = 0; i < len; i++) {
5237 stmt_ty val;
5238 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5239 Py_INCREF(tmp2);
5240 res = obj2ast_stmt(state, tmp2, &val, arena);
5241 Py_DECREF(tmp2);
5242 if (res != 0) goto failed;
5243 if (len != PyList_GET_SIZE(tmp)) {
5244 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"body\" changed size during iteration");
5245 goto failed;
5246 }
5247 asdl_seq_SET(body, i, val);
5248 }
5249 Py_CLEAR(tmp);
5250 }
5251 if (_PyObject_LookupAttr(obj, state->decorator_list, &tmp) < 0) {
5252 return 1;
5253 }
5254 if (tmp == NULL) {
5255 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
5256 return 1;
5257 }
5258 else {
5259 int res;
5260 Py_ssize_t len;
5261 Py_ssize_t i;
5262 if (!PyList_Check(tmp)) {
5263 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5264 goto failed;
5265 }
5266 len = PyList_GET_SIZE(tmp);
5267 decorator_list = _Py_asdl_seq_new(len, arena);
5268 if (decorator_list == NULL) goto failed;
5269 for (i = 0; i < len; i++) {
5270 expr_ty val;
5271 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5272 Py_INCREF(tmp2);
5273 res = obj2ast_expr(state, tmp2, &val, arena);
5274 Py_DECREF(tmp2);
5275 if (res != 0) goto failed;
5276 if (len != PyList_GET_SIZE(tmp)) {
5277 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"decorator_list\" changed size during iteration");
5278 goto failed;
5279 }
5280 asdl_seq_SET(decorator_list, i, val);
5281 }
5282 Py_CLEAR(tmp);
5283 }
5284 if (_PyObject_LookupAttr(obj, state->returns, &tmp) < 0) {
5285 return 1;
5286 }
5287 if (tmp == NULL || tmp == Py_None) {
5288 Py_CLEAR(tmp);
5289 returns = NULL;
5290 }
5291 else {
5292 int res;
5293 res = obj2ast_expr(state, tmp, &returns, arena);
5294 if (res != 0) goto failed;
5295 Py_CLEAR(tmp);
5296 }
5297 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
5298 return 1;
5299 }
5300 if (tmp == NULL || tmp == Py_None) {
5301 Py_CLEAR(tmp);
5302 type_comment = NULL;
5303 }
5304 else {
5305 int res;
5306 res = obj2ast_string(state, tmp, &type_comment, arena);
5307 if (res != 0) goto failed;
5308 Py_CLEAR(tmp);
5309 }
5310 *out = FunctionDef(name, args, body, decorator_list, returns,
5311 type_comment, lineno, col_offset, end_lineno,
5312 end_col_offset, arena);
5313 if (*out == NULL) goto failed;
5314 return 0;
5315 }
5316 tp = state->AsyncFunctionDef_type;
5317 isinstance = PyObject_IsInstance(obj, tp);
5318 if (isinstance == -1) {
5319 return 1;
5320 }
5321 if (isinstance) {
5322 identifier name;
5323 arguments_ty args;
5324 asdl_seq* body;
5325 asdl_seq* decorator_list;
5326 expr_ty returns;
5327 string type_comment;
5328
5329 if (_PyObject_LookupAttr(obj, state->name, &tmp) < 0) {
5330 return 1;
5331 }
5332 if (tmp == NULL) {
5333 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
5334 return 1;
5335 }
5336 else {
5337 int res;
5338 res = obj2ast_identifier(state, tmp, &name, arena);
5339 if (res != 0) goto failed;
5340 Py_CLEAR(tmp);
5341 }
5342 if (_PyObject_LookupAttr(obj, state->args, &tmp) < 0) {
5343 return 1;
5344 }
5345 if (tmp == NULL) {
5346 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
5347 return 1;
5348 }
5349 else {
5350 int res;
5351 res = obj2ast_arguments(state, tmp, &args, arena);
5352 if (res != 0) goto failed;
5353 Py_CLEAR(tmp);
5354 }
5355 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
5356 return 1;
5357 }
5358 if (tmp == NULL) {
5359 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
5360 return 1;
5361 }
5362 else {
5363 int res;
5364 Py_ssize_t len;
5365 Py_ssize_t i;
5366 if (!PyList_Check(tmp)) {
5367 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5368 goto failed;
5369 }
5370 len = PyList_GET_SIZE(tmp);
5371 body = _Py_asdl_seq_new(len, arena);
5372 if (body == NULL) goto failed;
5373 for (i = 0; i < len; i++) {
5374 stmt_ty val;
5375 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5376 Py_INCREF(tmp2);
5377 res = obj2ast_stmt(state, tmp2, &val, arena);
5378 Py_DECREF(tmp2);
5379 if (res != 0) goto failed;
5380 if (len != PyList_GET_SIZE(tmp)) {
5381 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"body\" changed size during iteration");
5382 goto failed;
5383 }
5384 asdl_seq_SET(body, i, val);
5385 }
5386 Py_CLEAR(tmp);
5387 }
5388 if (_PyObject_LookupAttr(obj, state->decorator_list, &tmp) < 0) {
5389 return 1;
5390 }
5391 if (tmp == NULL) {
5392 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
5393 return 1;
5394 }
5395 else {
5396 int res;
5397 Py_ssize_t len;
5398 Py_ssize_t i;
5399 if (!PyList_Check(tmp)) {
5400 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5401 goto failed;
5402 }
5403 len = PyList_GET_SIZE(tmp);
5404 decorator_list = _Py_asdl_seq_new(len, arena);
5405 if (decorator_list == NULL) goto failed;
5406 for (i = 0; i < len; i++) {
5407 expr_ty val;
5408 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5409 Py_INCREF(tmp2);
5410 res = obj2ast_expr(state, tmp2, &val, arena);
5411 Py_DECREF(tmp2);
5412 if (res != 0) goto failed;
5413 if (len != PyList_GET_SIZE(tmp)) {
5414 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"decorator_list\" changed size during iteration");
5415 goto failed;
5416 }
5417 asdl_seq_SET(decorator_list, i, val);
5418 }
5419 Py_CLEAR(tmp);
5420 }
5421 if (_PyObject_LookupAttr(obj, state->returns, &tmp) < 0) {
5422 return 1;
5423 }
5424 if (tmp == NULL || tmp == Py_None) {
5425 Py_CLEAR(tmp);
5426 returns = NULL;
5427 }
5428 else {
5429 int res;
5430 res = obj2ast_expr(state, tmp, &returns, arena);
5431 if (res != 0) goto failed;
5432 Py_CLEAR(tmp);
5433 }
5434 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
5435 return 1;
5436 }
5437 if (tmp == NULL || tmp == Py_None) {
5438 Py_CLEAR(tmp);
5439 type_comment = NULL;
5440 }
5441 else {
5442 int res;
5443 res = obj2ast_string(state, tmp, &type_comment, arena);
5444 if (res != 0) goto failed;
5445 Py_CLEAR(tmp);
5446 }
5447 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
5448 type_comment, lineno, col_offset, end_lineno,
5449 end_col_offset, arena);
5450 if (*out == NULL) goto failed;
5451 return 0;
5452 }
5453 tp = state->ClassDef_type;
5454 isinstance = PyObject_IsInstance(obj, tp);
5455 if (isinstance == -1) {
5456 return 1;
5457 }
5458 if (isinstance) {
5459 identifier name;
5460 asdl_seq* bases;
5461 asdl_seq* keywords;
5462 asdl_seq* body;
5463 asdl_seq* decorator_list;
5464
5465 if (_PyObject_LookupAttr(obj, state->name, &tmp) < 0) {
5466 return 1;
5467 }
5468 if (tmp == NULL) {
5469 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
5470 return 1;
5471 }
5472 else {
5473 int res;
5474 res = obj2ast_identifier(state, tmp, &name, arena);
5475 if (res != 0) goto failed;
5476 Py_CLEAR(tmp);
5477 }
5478 if (_PyObject_LookupAttr(obj, state->bases, &tmp) < 0) {
5479 return 1;
5480 }
5481 if (tmp == NULL) {
5482 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
5483 return 1;
5484 }
5485 else {
5486 int res;
5487 Py_ssize_t len;
5488 Py_ssize_t i;
5489 if (!PyList_Check(tmp)) {
5490 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5491 goto failed;
5492 }
5493 len = PyList_GET_SIZE(tmp);
5494 bases = _Py_asdl_seq_new(len, arena);
5495 if (bases == NULL) goto failed;
5496 for (i = 0; i < len; i++) {
5497 expr_ty val;
5498 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5499 Py_INCREF(tmp2);
5500 res = obj2ast_expr(state, tmp2, &val, arena);
5501 Py_DECREF(tmp2);
5502 if (res != 0) goto failed;
5503 if (len != PyList_GET_SIZE(tmp)) {
5504 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"bases\" changed size during iteration");
5505 goto failed;
5506 }
5507 asdl_seq_SET(bases, i, val);
5508 }
5509 Py_CLEAR(tmp);
5510 }
5511 if (_PyObject_LookupAttr(obj, state->keywords, &tmp) < 0) {
5512 return 1;
5513 }
5514 if (tmp == NULL) {
5515 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
5516 return 1;
5517 }
5518 else {
5519 int res;
5520 Py_ssize_t len;
5521 Py_ssize_t i;
5522 if (!PyList_Check(tmp)) {
5523 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5524 goto failed;
5525 }
5526 len = PyList_GET_SIZE(tmp);
5527 keywords = _Py_asdl_seq_new(len, arena);
5528 if (keywords == NULL) goto failed;
5529 for (i = 0; i < len; i++) {
5530 keyword_ty val;
5531 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5532 Py_INCREF(tmp2);
5533 res = obj2ast_keyword(state, tmp2, &val, arena);
5534 Py_DECREF(tmp2);
5535 if (res != 0) goto failed;
5536 if (len != PyList_GET_SIZE(tmp)) {
5537 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"keywords\" changed size during iteration");
5538 goto failed;
5539 }
5540 asdl_seq_SET(keywords, i, val);
5541 }
5542 Py_CLEAR(tmp);
5543 }
5544 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
5545 return 1;
5546 }
5547 if (tmp == NULL) {
5548 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
5549 return 1;
5550 }
5551 else {
5552 int res;
5553 Py_ssize_t len;
5554 Py_ssize_t i;
5555 if (!PyList_Check(tmp)) {
5556 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5557 goto failed;
5558 }
5559 len = PyList_GET_SIZE(tmp);
5560 body = _Py_asdl_seq_new(len, arena);
5561 if (body == NULL) goto failed;
5562 for (i = 0; i < len; i++) {
5563 stmt_ty val;
5564 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5565 Py_INCREF(tmp2);
5566 res = obj2ast_stmt(state, tmp2, &val, arena);
5567 Py_DECREF(tmp2);
5568 if (res != 0) goto failed;
5569 if (len != PyList_GET_SIZE(tmp)) {
5570 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"body\" changed size during iteration");
5571 goto failed;
5572 }
5573 asdl_seq_SET(body, i, val);
5574 }
5575 Py_CLEAR(tmp);
5576 }
5577 if (_PyObject_LookupAttr(obj, state->decorator_list, &tmp) < 0) {
5578 return 1;
5579 }
5580 if (tmp == NULL) {
5581 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
5582 return 1;
5583 }
5584 else {
5585 int res;
5586 Py_ssize_t len;
5587 Py_ssize_t i;
5588 if (!PyList_Check(tmp)) {
5589 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5590 goto failed;
5591 }
5592 len = PyList_GET_SIZE(tmp);
5593 decorator_list = _Py_asdl_seq_new(len, arena);
5594 if (decorator_list == NULL) goto failed;
5595 for (i = 0; i < len; i++) {
5596 expr_ty val;
5597 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5598 Py_INCREF(tmp2);
5599 res = obj2ast_expr(state, tmp2, &val, arena);
5600 Py_DECREF(tmp2);
5601 if (res != 0) goto failed;
5602 if (len != PyList_GET_SIZE(tmp)) {
5603 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"decorator_list\" changed size during iteration");
5604 goto failed;
5605 }
5606 asdl_seq_SET(decorator_list, i, val);
5607 }
5608 Py_CLEAR(tmp);
5609 }
5610 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
5611 col_offset, end_lineno, end_col_offset, arena);
5612 if (*out == NULL) goto failed;
5613 return 0;
5614 }
5615 tp = state->Return_type;
5616 isinstance = PyObject_IsInstance(obj, tp);
5617 if (isinstance == -1) {
5618 return 1;
5619 }
5620 if (isinstance) {
5621 expr_ty value;
5622
5623 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
5624 return 1;
5625 }
5626 if (tmp == NULL || tmp == Py_None) {
5627 Py_CLEAR(tmp);
5628 value = NULL;
5629 }
5630 else {
5631 int res;
5632 res = obj2ast_expr(state, tmp, &value, arena);
5633 if (res != 0) goto failed;
5634 Py_CLEAR(tmp);
5635 }
5636 *out = Return(value, lineno, col_offset, end_lineno, end_col_offset,
5637 arena);
5638 if (*out == NULL) goto failed;
5639 return 0;
5640 }
5641 tp = state->Delete_type;
5642 isinstance = PyObject_IsInstance(obj, tp);
5643 if (isinstance == -1) {
5644 return 1;
5645 }
5646 if (isinstance) {
5647 asdl_seq* targets;
5648
5649 if (_PyObject_LookupAttr(obj, state->targets, &tmp) < 0) {
5650 return 1;
5651 }
5652 if (tmp == NULL) {
5653 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
5654 return 1;
5655 }
5656 else {
5657 int res;
5658 Py_ssize_t len;
5659 Py_ssize_t i;
5660 if (!PyList_Check(tmp)) {
5661 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5662 goto failed;
5663 }
5664 len = PyList_GET_SIZE(tmp);
5665 targets = _Py_asdl_seq_new(len, arena);
5666 if (targets == NULL) goto failed;
5667 for (i = 0; i < len; i++) {
5668 expr_ty val;
5669 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5670 Py_INCREF(tmp2);
5671 res = obj2ast_expr(state, tmp2, &val, arena);
5672 Py_DECREF(tmp2);
5673 if (res != 0) goto failed;
5674 if (len != PyList_GET_SIZE(tmp)) {
5675 PyErr_SetString(PyExc_RuntimeError, "Delete field \"targets\" changed size during iteration");
5676 goto failed;
5677 }
5678 asdl_seq_SET(targets, i, val);
5679 }
5680 Py_CLEAR(tmp);
5681 }
5682 *out = Delete(targets, lineno, col_offset, end_lineno, end_col_offset,
5683 arena);
5684 if (*out == NULL) goto failed;
5685 return 0;
5686 }
5687 tp = state->Assign_type;
5688 isinstance = PyObject_IsInstance(obj, tp);
5689 if (isinstance == -1) {
5690 return 1;
5691 }
5692 if (isinstance) {
5693 asdl_seq* targets;
5694 expr_ty value;
5695 string type_comment;
5696
5697 if (_PyObject_LookupAttr(obj, state->targets, &tmp) < 0) {
5698 return 1;
5699 }
5700 if (tmp == NULL) {
5701 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
5702 return 1;
5703 }
5704 else {
5705 int res;
5706 Py_ssize_t len;
5707 Py_ssize_t i;
5708 if (!PyList_Check(tmp)) {
5709 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5710 goto failed;
5711 }
5712 len = PyList_GET_SIZE(tmp);
5713 targets = _Py_asdl_seq_new(len, arena);
5714 if (targets == NULL) goto failed;
5715 for (i = 0; i < len; i++) {
5716 expr_ty val;
5717 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5718 Py_INCREF(tmp2);
5719 res = obj2ast_expr(state, tmp2, &val, arena);
5720 Py_DECREF(tmp2);
5721 if (res != 0) goto failed;
5722 if (len != PyList_GET_SIZE(tmp)) {
5723 PyErr_SetString(PyExc_RuntimeError, "Assign field \"targets\" changed size during iteration");
5724 goto failed;
5725 }
5726 asdl_seq_SET(targets, i, val);
5727 }
5728 Py_CLEAR(tmp);
5729 }
5730 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
5731 return 1;
5732 }
5733 if (tmp == NULL) {
5734 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
5735 return 1;
5736 }
5737 else {
5738 int res;
5739 res = obj2ast_expr(state, tmp, &value, arena);
5740 if (res != 0) goto failed;
5741 Py_CLEAR(tmp);
5742 }
5743 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
5744 return 1;
5745 }
5746 if (tmp == NULL || tmp == Py_None) {
5747 Py_CLEAR(tmp);
5748 type_comment = NULL;
5749 }
5750 else {
5751 int res;
5752 res = obj2ast_string(state, tmp, &type_comment, arena);
5753 if (res != 0) goto failed;
5754 Py_CLEAR(tmp);
5755 }
5756 *out = Assign(targets, value, type_comment, lineno, col_offset,
5757 end_lineno, end_col_offset, arena);
5758 if (*out == NULL) goto failed;
5759 return 0;
5760 }
5761 tp = state->AugAssign_type;
5762 isinstance = PyObject_IsInstance(obj, tp);
5763 if (isinstance == -1) {
5764 return 1;
5765 }
5766 if (isinstance) {
5767 expr_ty target;
5768 operator_ty op;
5769 expr_ty value;
5770
5771 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
5772 return 1;
5773 }
5774 if (tmp == NULL) {
5775 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
5776 return 1;
5777 }
5778 else {
5779 int res;
5780 res = obj2ast_expr(state, tmp, &target, arena);
5781 if (res != 0) goto failed;
5782 Py_CLEAR(tmp);
5783 }
5784 if (_PyObject_LookupAttr(obj, state->op, &tmp) < 0) {
5785 return 1;
5786 }
5787 if (tmp == NULL) {
5788 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
5789 return 1;
5790 }
5791 else {
5792 int res;
5793 res = obj2ast_operator(state, tmp, &op, arena);
5794 if (res != 0) goto failed;
5795 Py_CLEAR(tmp);
5796 }
5797 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
5798 return 1;
5799 }
5800 if (tmp == NULL) {
5801 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
5802 return 1;
5803 }
5804 else {
5805 int res;
5806 res = obj2ast_expr(state, tmp, &value, arena);
5807 if (res != 0) goto failed;
5808 Py_CLEAR(tmp);
5809 }
5810 *out = AugAssign(target, op, value, lineno, col_offset, end_lineno,
5811 end_col_offset, arena);
5812 if (*out == NULL) goto failed;
5813 return 0;
5814 }
5815 tp = state->AnnAssign_type;
5816 isinstance = PyObject_IsInstance(obj, tp);
5817 if (isinstance == -1) {
5818 return 1;
5819 }
5820 if (isinstance) {
5821 expr_ty target;
5822 expr_ty annotation;
5823 expr_ty value;
5824 int simple;
5825
5826 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
5827 return 1;
5828 }
5829 if (tmp == NULL) {
5830 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AnnAssign");
5831 return 1;
5832 }
5833 else {
5834 int res;
5835 res = obj2ast_expr(state, tmp, &target, arena);
5836 if (res != 0) goto failed;
5837 Py_CLEAR(tmp);
5838 }
5839 if (_PyObject_LookupAttr(obj, state->annotation, &tmp) < 0) {
5840 return 1;
5841 }
5842 if (tmp == NULL) {
5843 PyErr_SetString(PyExc_TypeError, "required field \"annotation\" missing from AnnAssign");
5844 return 1;
5845 }
5846 else {
5847 int res;
5848 res = obj2ast_expr(state, tmp, &annotation, arena);
5849 if (res != 0) goto failed;
5850 Py_CLEAR(tmp);
5851 }
5852 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
5853 return 1;
5854 }
5855 if (tmp == NULL || tmp == Py_None) {
5856 Py_CLEAR(tmp);
5857 value = NULL;
5858 }
5859 else {
5860 int res;
5861 res = obj2ast_expr(state, tmp, &value, arena);
5862 if (res != 0) goto failed;
5863 Py_CLEAR(tmp);
5864 }
5865 if (_PyObject_LookupAttr(obj, state->simple, &tmp) < 0) {
5866 return 1;
5867 }
5868 if (tmp == NULL) {
5869 PyErr_SetString(PyExc_TypeError, "required field \"simple\" missing from AnnAssign");
5870 return 1;
5871 }
5872 else {
5873 int res;
5874 res = obj2ast_int(state, tmp, &simple, arena);
5875 if (res != 0) goto failed;
5876 Py_CLEAR(tmp);
5877 }
5878 *out = AnnAssign(target, annotation, value, simple, lineno, col_offset,
5879 end_lineno, end_col_offset, arena);
5880 if (*out == NULL) goto failed;
5881 return 0;
5882 }
5883 tp = state->For_type;
5884 isinstance = PyObject_IsInstance(obj, tp);
5885 if (isinstance == -1) {
5886 return 1;
5887 }
5888 if (isinstance) {
5889 expr_ty target;
5890 expr_ty iter;
5891 asdl_seq* body;
5892 asdl_seq* orelse;
5893 string type_comment;
5894
5895 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
5896 return 1;
5897 }
5898 if (tmp == NULL) {
5899 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
5900 return 1;
5901 }
5902 else {
5903 int res;
5904 res = obj2ast_expr(state, tmp, &target, arena);
5905 if (res != 0) goto failed;
5906 Py_CLEAR(tmp);
5907 }
5908 if (_PyObject_LookupAttr(obj, state->iter, &tmp) < 0) {
5909 return 1;
5910 }
5911 if (tmp == NULL) {
5912 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
5913 return 1;
5914 }
5915 else {
5916 int res;
5917 res = obj2ast_expr(state, tmp, &iter, arena);
5918 if (res != 0) goto failed;
5919 Py_CLEAR(tmp);
5920 }
5921 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
5922 return 1;
5923 }
5924 if (tmp == NULL) {
5925 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
5926 return 1;
5927 }
5928 else {
5929 int res;
5930 Py_ssize_t len;
5931 Py_ssize_t i;
5932 if (!PyList_Check(tmp)) {
5933 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5934 goto failed;
5935 }
5936 len = PyList_GET_SIZE(tmp);
5937 body = _Py_asdl_seq_new(len, arena);
5938 if (body == NULL) goto failed;
5939 for (i = 0; i < len; i++) {
5940 stmt_ty val;
5941 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5942 Py_INCREF(tmp2);
5943 res = obj2ast_stmt(state, tmp2, &val, arena);
5944 Py_DECREF(tmp2);
5945 if (res != 0) goto failed;
5946 if (len != PyList_GET_SIZE(tmp)) {
5947 PyErr_SetString(PyExc_RuntimeError, "For field \"body\" changed size during iteration");
5948 goto failed;
5949 }
5950 asdl_seq_SET(body, i, val);
5951 }
5952 Py_CLEAR(tmp);
5953 }
5954 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
5955 return 1;
5956 }
5957 if (tmp == NULL) {
5958 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
5959 return 1;
5960 }
5961 else {
5962 int res;
5963 Py_ssize_t len;
5964 Py_ssize_t i;
5965 if (!PyList_Check(tmp)) {
5966 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
5967 goto failed;
5968 }
5969 len = PyList_GET_SIZE(tmp);
5970 orelse = _Py_asdl_seq_new(len, arena);
5971 if (orelse == NULL) goto failed;
5972 for (i = 0; i < len; i++) {
5973 stmt_ty val;
5974 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5975 Py_INCREF(tmp2);
5976 res = obj2ast_stmt(state, tmp2, &val, arena);
5977 Py_DECREF(tmp2);
5978 if (res != 0) goto failed;
5979 if (len != PyList_GET_SIZE(tmp)) {
5980 PyErr_SetString(PyExc_RuntimeError, "For field \"orelse\" changed size during iteration");
5981 goto failed;
5982 }
5983 asdl_seq_SET(orelse, i, val);
5984 }
5985 Py_CLEAR(tmp);
5986 }
5987 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
5988 return 1;
5989 }
5990 if (tmp == NULL || tmp == Py_None) {
5991 Py_CLEAR(tmp);
5992 type_comment = NULL;
5993 }
5994 else {
5995 int res;
5996 res = obj2ast_string(state, tmp, &type_comment, arena);
5997 if (res != 0) goto failed;
5998 Py_CLEAR(tmp);
5999 }
6000 *out = For(target, iter, body, orelse, type_comment, lineno,
6001 col_offset, end_lineno, end_col_offset, arena);
6002 if (*out == NULL) goto failed;
6003 return 0;
6004 }
6005 tp = state->AsyncFor_type;
6006 isinstance = PyObject_IsInstance(obj, tp);
6007 if (isinstance == -1) {
6008 return 1;
6009 }
6010 if (isinstance) {
6011 expr_ty target;
6012 expr_ty iter;
6013 asdl_seq* body;
6014 asdl_seq* orelse;
6015 string type_comment;
6016
6017 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
6018 return 1;
6019 }
6020 if (tmp == NULL) {
6021 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
6022 return 1;
6023 }
6024 else {
6025 int res;
6026 res = obj2ast_expr(state, tmp, &target, arena);
6027 if (res != 0) goto failed;
6028 Py_CLEAR(tmp);
6029 }
6030 if (_PyObject_LookupAttr(obj, state->iter, &tmp) < 0) {
6031 return 1;
6032 }
6033 if (tmp == NULL) {
6034 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
6035 return 1;
6036 }
6037 else {
6038 int res;
6039 res = obj2ast_expr(state, tmp, &iter, arena);
6040 if (res != 0) goto failed;
6041 Py_CLEAR(tmp);
6042 }
6043 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
6044 return 1;
6045 }
6046 if (tmp == NULL) {
6047 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
6048 return 1;
6049 }
6050 else {
6051 int res;
6052 Py_ssize_t len;
6053 Py_ssize_t i;
6054 if (!PyList_Check(tmp)) {
6055 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6056 goto failed;
6057 }
6058 len = PyList_GET_SIZE(tmp);
6059 body = _Py_asdl_seq_new(len, arena);
6060 if (body == NULL) goto failed;
6061 for (i = 0; i < len; i++) {
6062 stmt_ty val;
6063 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6064 Py_INCREF(tmp2);
6065 res = obj2ast_stmt(state, tmp2, &val, arena);
6066 Py_DECREF(tmp2);
6067 if (res != 0) goto failed;
6068 if (len != PyList_GET_SIZE(tmp)) {
6069 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"body\" changed size during iteration");
6070 goto failed;
6071 }
6072 asdl_seq_SET(body, i, val);
6073 }
6074 Py_CLEAR(tmp);
6075 }
6076 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
6077 return 1;
6078 }
6079 if (tmp == NULL) {
6080 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
6081 return 1;
6082 }
6083 else {
6084 int res;
6085 Py_ssize_t len;
6086 Py_ssize_t i;
6087 if (!PyList_Check(tmp)) {
6088 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6089 goto failed;
6090 }
6091 len = PyList_GET_SIZE(tmp);
6092 orelse = _Py_asdl_seq_new(len, arena);
6093 if (orelse == NULL) goto failed;
6094 for (i = 0; i < len; i++) {
6095 stmt_ty val;
6096 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6097 Py_INCREF(tmp2);
6098 res = obj2ast_stmt(state, tmp2, &val, arena);
6099 Py_DECREF(tmp2);
6100 if (res != 0) goto failed;
6101 if (len != PyList_GET_SIZE(tmp)) {
6102 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"orelse\" changed size during iteration");
6103 goto failed;
6104 }
6105 asdl_seq_SET(orelse, i, val);
6106 }
6107 Py_CLEAR(tmp);
6108 }
6109 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
6110 return 1;
6111 }
6112 if (tmp == NULL || tmp == Py_None) {
6113 Py_CLEAR(tmp);
6114 type_comment = NULL;
6115 }
6116 else {
6117 int res;
6118 res = obj2ast_string(state, tmp, &type_comment, arena);
6119 if (res != 0) goto failed;
6120 Py_CLEAR(tmp);
6121 }
6122 *out = AsyncFor(target, iter, body, orelse, type_comment, lineno,
6123 col_offset, end_lineno, end_col_offset, arena);
6124 if (*out == NULL) goto failed;
6125 return 0;
6126 }
6127 tp = state->While_type;
6128 isinstance = PyObject_IsInstance(obj, tp);
6129 if (isinstance == -1) {
6130 return 1;
6131 }
6132 if (isinstance) {
6133 expr_ty test;
6134 asdl_seq* body;
6135 asdl_seq* orelse;
6136
6137 if (_PyObject_LookupAttr(obj, state->test, &tmp) < 0) {
6138 return 1;
6139 }
6140 if (tmp == NULL) {
6141 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
6142 return 1;
6143 }
6144 else {
6145 int res;
6146 res = obj2ast_expr(state, tmp, &test, arena);
6147 if (res != 0) goto failed;
6148 Py_CLEAR(tmp);
6149 }
6150 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
6151 return 1;
6152 }
6153 if (tmp == NULL) {
6154 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
6155 return 1;
6156 }
6157 else {
6158 int res;
6159 Py_ssize_t len;
6160 Py_ssize_t i;
6161 if (!PyList_Check(tmp)) {
6162 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6163 goto failed;
6164 }
6165 len = PyList_GET_SIZE(tmp);
6166 body = _Py_asdl_seq_new(len, arena);
6167 if (body == NULL) goto failed;
6168 for (i = 0; i < len; i++) {
6169 stmt_ty val;
6170 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6171 Py_INCREF(tmp2);
6172 res = obj2ast_stmt(state, tmp2, &val, arena);
6173 Py_DECREF(tmp2);
6174 if (res != 0) goto failed;
6175 if (len != PyList_GET_SIZE(tmp)) {
6176 PyErr_SetString(PyExc_RuntimeError, "While field \"body\" changed size during iteration");
6177 goto failed;
6178 }
6179 asdl_seq_SET(body, i, val);
6180 }
6181 Py_CLEAR(tmp);
6182 }
6183 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
6184 return 1;
6185 }
6186 if (tmp == NULL) {
6187 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
6188 return 1;
6189 }
6190 else {
6191 int res;
6192 Py_ssize_t len;
6193 Py_ssize_t i;
6194 if (!PyList_Check(tmp)) {
6195 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6196 goto failed;
6197 }
6198 len = PyList_GET_SIZE(tmp);
6199 orelse = _Py_asdl_seq_new(len, arena);
6200 if (orelse == NULL) goto failed;
6201 for (i = 0; i < len; i++) {
6202 stmt_ty val;
6203 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6204 Py_INCREF(tmp2);
6205 res = obj2ast_stmt(state, tmp2, &val, arena);
6206 Py_DECREF(tmp2);
6207 if (res != 0) goto failed;
6208 if (len != PyList_GET_SIZE(tmp)) {
6209 PyErr_SetString(PyExc_RuntimeError, "While field \"orelse\" changed size during iteration");
6210 goto failed;
6211 }
6212 asdl_seq_SET(orelse, i, val);
6213 }
6214 Py_CLEAR(tmp);
6215 }
6216 *out = While(test, body, orelse, lineno, col_offset, end_lineno,
6217 end_col_offset, arena);
6218 if (*out == NULL) goto failed;
6219 return 0;
6220 }
6221 tp = state->If_type;
6222 isinstance = PyObject_IsInstance(obj, tp);
6223 if (isinstance == -1) {
6224 return 1;
6225 }
6226 if (isinstance) {
6227 expr_ty test;
6228 asdl_seq* body;
6229 asdl_seq* orelse;
6230
6231 if (_PyObject_LookupAttr(obj, state->test, &tmp) < 0) {
6232 return 1;
6233 }
6234 if (tmp == NULL) {
6235 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
6236 return 1;
6237 }
6238 else {
6239 int res;
6240 res = obj2ast_expr(state, tmp, &test, arena);
6241 if (res != 0) goto failed;
6242 Py_CLEAR(tmp);
6243 }
6244 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
6245 return 1;
6246 }
6247 if (tmp == NULL) {
6248 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
6249 return 1;
6250 }
6251 else {
6252 int res;
6253 Py_ssize_t len;
6254 Py_ssize_t i;
6255 if (!PyList_Check(tmp)) {
6256 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6257 goto failed;
6258 }
6259 len = PyList_GET_SIZE(tmp);
6260 body = _Py_asdl_seq_new(len, arena);
6261 if (body == NULL) goto failed;
6262 for (i = 0; i < len; i++) {
6263 stmt_ty val;
6264 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6265 Py_INCREF(tmp2);
6266 res = obj2ast_stmt(state, tmp2, &val, arena);
6267 Py_DECREF(tmp2);
6268 if (res != 0) goto failed;
6269 if (len != PyList_GET_SIZE(tmp)) {
6270 PyErr_SetString(PyExc_RuntimeError, "If field \"body\" changed size during iteration");
6271 goto failed;
6272 }
6273 asdl_seq_SET(body, i, val);
6274 }
6275 Py_CLEAR(tmp);
6276 }
6277 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
6278 return 1;
6279 }
6280 if (tmp == NULL) {
6281 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
6282 return 1;
6283 }
6284 else {
6285 int res;
6286 Py_ssize_t len;
6287 Py_ssize_t i;
6288 if (!PyList_Check(tmp)) {
6289 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6290 goto failed;
6291 }
6292 len = PyList_GET_SIZE(tmp);
6293 orelse = _Py_asdl_seq_new(len, arena);
6294 if (orelse == NULL) goto failed;
6295 for (i = 0; i < len; i++) {
6296 stmt_ty val;
6297 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6298 Py_INCREF(tmp2);
6299 res = obj2ast_stmt(state, tmp2, &val, arena);
6300 Py_DECREF(tmp2);
6301 if (res != 0) goto failed;
6302 if (len != PyList_GET_SIZE(tmp)) {
6303 PyErr_SetString(PyExc_RuntimeError, "If field \"orelse\" changed size during iteration");
6304 goto failed;
6305 }
6306 asdl_seq_SET(orelse, i, val);
6307 }
6308 Py_CLEAR(tmp);
6309 }
6310 *out = If(test, body, orelse, lineno, col_offset, end_lineno,
6311 end_col_offset, arena);
6312 if (*out == NULL) goto failed;
6313 return 0;
6314 }
6315 tp = state->With_type;
6316 isinstance = PyObject_IsInstance(obj, tp);
6317 if (isinstance == -1) {
6318 return 1;
6319 }
6320 if (isinstance) {
6321 asdl_seq* items;
6322 asdl_seq* body;
6323 string type_comment;
6324
6325 if (_PyObject_LookupAttr(obj, state->items, &tmp) < 0) {
6326 return 1;
6327 }
6328 if (tmp == NULL) {
6329 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
6330 return 1;
6331 }
6332 else {
6333 int res;
6334 Py_ssize_t len;
6335 Py_ssize_t i;
6336 if (!PyList_Check(tmp)) {
6337 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6338 goto failed;
6339 }
6340 len = PyList_GET_SIZE(tmp);
6341 items = _Py_asdl_seq_new(len, arena);
6342 if (items == NULL) goto failed;
6343 for (i = 0; i < len; i++) {
6344 withitem_ty val;
6345 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6346 Py_INCREF(tmp2);
6347 res = obj2ast_withitem(state, tmp2, &val, arena);
6348 Py_DECREF(tmp2);
6349 if (res != 0) goto failed;
6350 if (len != PyList_GET_SIZE(tmp)) {
6351 PyErr_SetString(PyExc_RuntimeError, "With field \"items\" changed size during iteration");
6352 goto failed;
6353 }
6354 asdl_seq_SET(items, i, val);
6355 }
6356 Py_CLEAR(tmp);
6357 }
6358 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
6359 return 1;
6360 }
6361 if (tmp == NULL) {
6362 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
6363 return 1;
6364 }
6365 else {
6366 int res;
6367 Py_ssize_t len;
6368 Py_ssize_t i;
6369 if (!PyList_Check(tmp)) {
6370 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6371 goto failed;
6372 }
6373 len = PyList_GET_SIZE(tmp);
6374 body = _Py_asdl_seq_new(len, arena);
6375 if (body == NULL) goto failed;
6376 for (i = 0; i < len; i++) {
6377 stmt_ty val;
6378 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6379 Py_INCREF(tmp2);
6380 res = obj2ast_stmt(state, tmp2, &val, arena);
6381 Py_DECREF(tmp2);
6382 if (res != 0) goto failed;
6383 if (len != PyList_GET_SIZE(tmp)) {
6384 PyErr_SetString(PyExc_RuntimeError, "With field \"body\" changed size during iteration");
6385 goto failed;
6386 }
6387 asdl_seq_SET(body, i, val);
6388 }
6389 Py_CLEAR(tmp);
6390 }
6391 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
6392 return 1;
6393 }
6394 if (tmp == NULL || tmp == Py_None) {
6395 Py_CLEAR(tmp);
6396 type_comment = NULL;
6397 }
6398 else {
6399 int res;
6400 res = obj2ast_string(state, tmp, &type_comment, arena);
6401 if (res != 0) goto failed;
6402 Py_CLEAR(tmp);
6403 }
6404 *out = With(items, body, type_comment, lineno, col_offset, end_lineno,
6405 end_col_offset, arena);
6406 if (*out == NULL) goto failed;
6407 return 0;
6408 }
6409 tp = state->AsyncWith_type;
6410 isinstance = PyObject_IsInstance(obj, tp);
6411 if (isinstance == -1) {
6412 return 1;
6413 }
6414 if (isinstance) {
6415 asdl_seq* items;
6416 asdl_seq* body;
6417 string type_comment;
6418
6419 if (_PyObject_LookupAttr(obj, state->items, &tmp) < 0) {
6420 return 1;
6421 }
6422 if (tmp == NULL) {
6423 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
6424 return 1;
6425 }
6426 else {
6427 int res;
6428 Py_ssize_t len;
6429 Py_ssize_t i;
6430 if (!PyList_Check(tmp)) {
6431 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6432 goto failed;
6433 }
6434 len = PyList_GET_SIZE(tmp);
6435 items = _Py_asdl_seq_new(len, arena);
6436 if (items == NULL) goto failed;
6437 for (i = 0; i < len; i++) {
6438 withitem_ty val;
6439 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6440 Py_INCREF(tmp2);
6441 res = obj2ast_withitem(state, tmp2, &val, arena);
6442 Py_DECREF(tmp2);
6443 if (res != 0) goto failed;
6444 if (len != PyList_GET_SIZE(tmp)) {
6445 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"items\" changed size during iteration");
6446 goto failed;
6447 }
6448 asdl_seq_SET(items, i, val);
6449 }
6450 Py_CLEAR(tmp);
6451 }
6452 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
6453 return 1;
6454 }
6455 if (tmp == NULL) {
6456 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
6457 return 1;
6458 }
6459 else {
6460 int res;
6461 Py_ssize_t len;
6462 Py_ssize_t i;
6463 if (!PyList_Check(tmp)) {
6464 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6465 goto failed;
6466 }
6467 len = PyList_GET_SIZE(tmp);
6468 body = _Py_asdl_seq_new(len, arena);
6469 if (body == NULL) goto failed;
6470 for (i = 0; i < len; i++) {
6471 stmt_ty val;
6472 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6473 Py_INCREF(tmp2);
6474 res = obj2ast_stmt(state, tmp2, &val, arena);
6475 Py_DECREF(tmp2);
6476 if (res != 0) goto failed;
6477 if (len != PyList_GET_SIZE(tmp)) {
6478 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"body\" changed size during iteration");
6479 goto failed;
6480 }
6481 asdl_seq_SET(body, i, val);
6482 }
6483 Py_CLEAR(tmp);
6484 }
6485 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
6486 return 1;
6487 }
6488 if (tmp == NULL || tmp == Py_None) {
6489 Py_CLEAR(tmp);
6490 type_comment = NULL;
6491 }
6492 else {
6493 int res;
6494 res = obj2ast_string(state, tmp, &type_comment, arena);
6495 if (res != 0) goto failed;
6496 Py_CLEAR(tmp);
6497 }
6498 *out = AsyncWith(items, body, type_comment, lineno, col_offset,
6499 end_lineno, end_col_offset, arena);
6500 if (*out == NULL) goto failed;
6501 return 0;
6502 }
6503 tp = state->Raise_type;
6504 isinstance = PyObject_IsInstance(obj, tp);
6505 if (isinstance == -1) {
6506 return 1;
6507 }
6508 if (isinstance) {
6509 expr_ty exc;
6510 expr_ty cause;
6511
6512 if (_PyObject_LookupAttr(obj, state->exc, &tmp) < 0) {
6513 return 1;
6514 }
6515 if (tmp == NULL || tmp == Py_None) {
6516 Py_CLEAR(tmp);
6517 exc = NULL;
6518 }
6519 else {
6520 int res;
6521 res = obj2ast_expr(state, tmp, &exc, arena);
6522 if (res != 0) goto failed;
6523 Py_CLEAR(tmp);
6524 }
6525 if (_PyObject_LookupAttr(obj, state->cause, &tmp) < 0) {
6526 return 1;
6527 }
6528 if (tmp == NULL || tmp == Py_None) {
6529 Py_CLEAR(tmp);
6530 cause = NULL;
6531 }
6532 else {
6533 int res;
6534 res = obj2ast_expr(state, tmp, &cause, arena);
6535 if (res != 0) goto failed;
6536 Py_CLEAR(tmp);
6537 }
6538 *out = Raise(exc, cause, lineno, col_offset, end_lineno,
6539 end_col_offset, arena);
6540 if (*out == NULL) goto failed;
6541 return 0;
6542 }
6543 tp = state->Try_type;
6544 isinstance = PyObject_IsInstance(obj, tp);
6545 if (isinstance == -1) {
6546 return 1;
6547 }
6548 if (isinstance) {
6549 asdl_seq* body;
6550 asdl_seq* handlers;
6551 asdl_seq* orelse;
6552 asdl_seq* finalbody;
6553
6554 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
6555 return 1;
6556 }
6557 if (tmp == NULL) {
6558 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
6559 return 1;
6560 }
6561 else {
6562 int res;
6563 Py_ssize_t len;
6564 Py_ssize_t i;
6565 if (!PyList_Check(tmp)) {
6566 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6567 goto failed;
6568 }
6569 len = PyList_GET_SIZE(tmp);
6570 body = _Py_asdl_seq_new(len, arena);
6571 if (body == NULL) goto failed;
6572 for (i = 0; i < len; i++) {
6573 stmt_ty val;
6574 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6575 Py_INCREF(tmp2);
6576 res = obj2ast_stmt(state, tmp2, &val, arena);
6577 Py_DECREF(tmp2);
6578 if (res != 0) goto failed;
6579 if (len != PyList_GET_SIZE(tmp)) {
6580 PyErr_SetString(PyExc_RuntimeError, "Try field \"body\" changed size during iteration");
6581 goto failed;
6582 }
6583 asdl_seq_SET(body, i, val);
6584 }
6585 Py_CLEAR(tmp);
6586 }
6587 if (_PyObject_LookupAttr(obj, state->handlers, &tmp) < 0) {
6588 return 1;
6589 }
6590 if (tmp == NULL) {
6591 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
6592 return 1;
6593 }
6594 else {
6595 int res;
6596 Py_ssize_t len;
6597 Py_ssize_t i;
6598 if (!PyList_Check(tmp)) {
6599 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6600 goto failed;
6601 }
6602 len = PyList_GET_SIZE(tmp);
6603 handlers = _Py_asdl_seq_new(len, arena);
6604 if (handlers == NULL) goto failed;
6605 for (i = 0; i < len; i++) {
6606 excepthandler_ty val;
6607 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6608 Py_INCREF(tmp2);
6609 res = obj2ast_excepthandler(state, tmp2, &val, arena);
6610 Py_DECREF(tmp2);
6611 if (res != 0) goto failed;
6612 if (len != PyList_GET_SIZE(tmp)) {
6613 PyErr_SetString(PyExc_RuntimeError, "Try field \"handlers\" changed size during iteration");
6614 goto failed;
6615 }
6616 asdl_seq_SET(handlers, i, val);
6617 }
6618 Py_CLEAR(tmp);
6619 }
6620 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
6621 return 1;
6622 }
6623 if (tmp == NULL) {
6624 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
6625 return 1;
6626 }
6627 else {
6628 int res;
6629 Py_ssize_t len;
6630 Py_ssize_t i;
6631 if (!PyList_Check(tmp)) {
6632 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6633 goto failed;
6634 }
6635 len = PyList_GET_SIZE(tmp);
6636 orelse = _Py_asdl_seq_new(len, arena);
6637 if (orelse == NULL) goto failed;
6638 for (i = 0; i < len; i++) {
6639 stmt_ty val;
6640 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6641 Py_INCREF(tmp2);
6642 res = obj2ast_stmt(state, tmp2, &val, arena);
6643 Py_DECREF(tmp2);
6644 if (res != 0) goto failed;
6645 if (len != PyList_GET_SIZE(tmp)) {
6646 PyErr_SetString(PyExc_RuntimeError, "Try field \"orelse\" changed size during iteration");
6647 goto failed;
6648 }
6649 asdl_seq_SET(orelse, i, val);
6650 }
6651 Py_CLEAR(tmp);
6652 }
6653 if (_PyObject_LookupAttr(obj, state->finalbody, &tmp) < 0) {
6654 return 1;
6655 }
6656 if (tmp == NULL) {
6657 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
6658 return 1;
6659 }
6660 else {
6661 int res;
6662 Py_ssize_t len;
6663 Py_ssize_t i;
6664 if (!PyList_Check(tmp)) {
6665 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6666 goto failed;
6667 }
6668 len = PyList_GET_SIZE(tmp);
6669 finalbody = _Py_asdl_seq_new(len, arena);
6670 if (finalbody == NULL) goto failed;
6671 for (i = 0; i < len; i++) {
6672 stmt_ty val;
6673 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6674 Py_INCREF(tmp2);
6675 res = obj2ast_stmt(state, tmp2, &val, arena);
6676 Py_DECREF(tmp2);
6677 if (res != 0) goto failed;
6678 if (len != PyList_GET_SIZE(tmp)) {
6679 PyErr_SetString(PyExc_RuntimeError, "Try field \"finalbody\" changed size during iteration");
6680 goto failed;
6681 }
6682 asdl_seq_SET(finalbody, i, val);
6683 }
6684 Py_CLEAR(tmp);
6685 }
6686 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
6687 end_lineno, end_col_offset, arena);
6688 if (*out == NULL) goto failed;
6689 return 0;
6690 }
6691 tp = state->Assert_type;
6692 isinstance = PyObject_IsInstance(obj, tp);
6693 if (isinstance == -1) {
6694 return 1;
6695 }
6696 if (isinstance) {
6697 expr_ty test;
6698 expr_ty msg;
6699
6700 if (_PyObject_LookupAttr(obj, state->test, &tmp) < 0) {
6701 return 1;
6702 }
6703 if (tmp == NULL) {
6704 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
6705 return 1;
6706 }
6707 else {
6708 int res;
6709 res = obj2ast_expr(state, tmp, &test, arena);
6710 if (res != 0) goto failed;
6711 Py_CLEAR(tmp);
6712 }
6713 if (_PyObject_LookupAttr(obj, state->msg, &tmp) < 0) {
6714 return 1;
6715 }
6716 if (tmp == NULL || tmp == Py_None) {
6717 Py_CLEAR(tmp);
6718 msg = NULL;
6719 }
6720 else {
6721 int res;
6722 res = obj2ast_expr(state, tmp, &msg, arena);
6723 if (res != 0) goto failed;
6724 Py_CLEAR(tmp);
6725 }
6726 *out = Assert(test, msg, lineno, col_offset, end_lineno,
6727 end_col_offset, arena);
6728 if (*out == NULL) goto failed;
6729 return 0;
6730 }
6731 tp = state->Import_type;
6732 isinstance = PyObject_IsInstance(obj, tp);
6733 if (isinstance == -1) {
6734 return 1;
6735 }
6736 if (isinstance) {
6737 asdl_seq* names;
6738
6739 if (_PyObject_LookupAttr(obj, state->names, &tmp) < 0) {
6740 return 1;
6741 }
6742 if (tmp == NULL) {
6743 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
6744 return 1;
6745 }
6746 else {
6747 int res;
6748 Py_ssize_t len;
6749 Py_ssize_t i;
6750 if (!PyList_Check(tmp)) {
6751 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6752 goto failed;
6753 }
6754 len = PyList_GET_SIZE(tmp);
6755 names = _Py_asdl_seq_new(len, arena);
6756 if (names == NULL) goto failed;
6757 for (i = 0; i < len; i++) {
6758 alias_ty val;
6759 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6760 Py_INCREF(tmp2);
6761 res = obj2ast_alias(state, tmp2, &val, arena);
6762 Py_DECREF(tmp2);
6763 if (res != 0) goto failed;
6764 if (len != PyList_GET_SIZE(tmp)) {
6765 PyErr_SetString(PyExc_RuntimeError, "Import field \"names\" changed size during iteration");
6766 goto failed;
6767 }
6768 asdl_seq_SET(names, i, val);
6769 }
6770 Py_CLEAR(tmp);
6771 }
6772 *out = Import(names, lineno, col_offset, end_lineno, end_col_offset,
6773 arena);
6774 if (*out == NULL) goto failed;
6775 return 0;
6776 }
6777 tp = state->ImportFrom_type;
6778 isinstance = PyObject_IsInstance(obj, tp);
6779 if (isinstance == -1) {
6780 return 1;
6781 }
6782 if (isinstance) {
6783 identifier module;
6784 asdl_seq* names;
6785 int level;
6786
6787 if (_PyObject_LookupAttr(obj, state->module, &tmp) < 0) {
6788 return 1;
6789 }
6790 if (tmp == NULL || tmp == Py_None) {
6791 Py_CLEAR(tmp);
6792 module = NULL;
6793 }
6794 else {
6795 int res;
6796 res = obj2ast_identifier(state, tmp, &module, arena);
6797 if (res != 0) goto failed;
6798 Py_CLEAR(tmp);
6799 }
6800 if (_PyObject_LookupAttr(obj, state->names, &tmp) < 0) {
6801 return 1;
6802 }
6803 if (tmp == NULL) {
6804 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
6805 return 1;
6806 }
6807 else {
6808 int res;
6809 Py_ssize_t len;
6810 Py_ssize_t i;
6811 if (!PyList_Check(tmp)) {
6812 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6813 goto failed;
6814 }
6815 len = PyList_GET_SIZE(tmp);
6816 names = _Py_asdl_seq_new(len, arena);
6817 if (names == NULL) goto failed;
6818 for (i = 0; i < len; i++) {
6819 alias_ty val;
6820 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6821 Py_INCREF(tmp2);
6822 res = obj2ast_alias(state, tmp2, &val, arena);
6823 Py_DECREF(tmp2);
6824 if (res != 0) goto failed;
6825 if (len != PyList_GET_SIZE(tmp)) {
6826 PyErr_SetString(PyExc_RuntimeError, "ImportFrom field \"names\" changed size during iteration");
6827 goto failed;
6828 }
6829 asdl_seq_SET(names, i, val);
6830 }
6831 Py_CLEAR(tmp);
6832 }
6833 if (_PyObject_LookupAttr(obj, state->level, &tmp) < 0) {
6834 return 1;
6835 }
6836 if (tmp == NULL || tmp == Py_None) {
6837 Py_CLEAR(tmp);
6838 level = 0;
6839 }
6840 else {
6841 int res;
6842 res = obj2ast_int(state, tmp, &level, arena);
6843 if (res != 0) goto failed;
6844 Py_CLEAR(tmp);
6845 }
6846 *out = ImportFrom(module, names, level, lineno, col_offset, end_lineno,
6847 end_col_offset, arena);
6848 if (*out == NULL) goto failed;
6849 return 0;
6850 }
6851 tp = state->Global_type;
6852 isinstance = PyObject_IsInstance(obj, tp);
6853 if (isinstance == -1) {
6854 return 1;
6855 }
6856 if (isinstance) {
6857 asdl_seq* names;
6858
6859 if (_PyObject_LookupAttr(obj, state->names, &tmp) < 0) {
6860 return 1;
6861 }
6862 if (tmp == NULL) {
6863 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
6864 return 1;
6865 }
6866 else {
6867 int res;
6868 Py_ssize_t len;
6869 Py_ssize_t i;
6870 if (!PyList_Check(tmp)) {
6871 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6872 goto failed;
6873 }
6874 len = PyList_GET_SIZE(tmp);
6875 names = _Py_asdl_seq_new(len, arena);
6876 if (names == NULL) goto failed;
6877 for (i = 0; i < len; i++) {
6878 identifier val;
6879 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6880 Py_INCREF(tmp2);
6881 res = obj2ast_identifier(state, tmp2, &val, arena);
6882 Py_DECREF(tmp2);
6883 if (res != 0) goto failed;
6884 if (len != PyList_GET_SIZE(tmp)) {
6885 PyErr_SetString(PyExc_RuntimeError, "Global field \"names\" changed size during iteration");
6886 goto failed;
6887 }
6888 asdl_seq_SET(names, i, val);
6889 }
6890 Py_CLEAR(tmp);
6891 }
6892 *out = Global(names, lineno, col_offset, end_lineno, end_col_offset,
6893 arena);
6894 if (*out == NULL) goto failed;
6895 return 0;
6896 }
6897 tp = state->Nonlocal_type;
6898 isinstance = PyObject_IsInstance(obj, tp);
6899 if (isinstance == -1) {
6900 return 1;
6901 }
6902 if (isinstance) {
6903 asdl_seq* names;
6904
6905 if (_PyObject_LookupAttr(obj, state->names, &tmp) < 0) {
6906 return 1;
6907 }
6908 if (tmp == NULL) {
6909 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
6910 return 1;
6911 }
6912 else {
6913 int res;
6914 Py_ssize_t len;
6915 Py_ssize_t i;
6916 if (!PyList_Check(tmp)) {
6917 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
6918 goto failed;
6919 }
6920 len = PyList_GET_SIZE(tmp);
6921 names = _Py_asdl_seq_new(len, arena);
6922 if (names == NULL) goto failed;
6923 for (i = 0; i < len; i++) {
6924 identifier val;
6925 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6926 Py_INCREF(tmp2);
6927 res = obj2ast_identifier(state, tmp2, &val, arena);
6928 Py_DECREF(tmp2);
6929 if (res != 0) goto failed;
6930 if (len != PyList_GET_SIZE(tmp)) {
6931 PyErr_SetString(PyExc_RuntimeError, "Nonlocal field \"names\" changed size during iteration");
6932 goto failed;
6933 }
6934 asdl_seq_SET(names, i, val);
6935 }
6936 Py_CLEAR(tmp);
6937 }
6938 *out = Nonlocal(names, lineno, col_offset, end_lineno, end_col_offset,
6939 arena);
6940 if (*out == NULL) goto failed;
6941 return 0;
6942 }
6943 tp = state->Expr_type;
6944 isinstance = PyObject_IsInstance(obj, tp);
6945 if (isinstance == -1) {
6946 return 1;
6947 }
6948 if (isinstance) {
6949 expr_ty value;
6950
6951 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
6952 return 1;
6953 }
6954 if (tmp == NULL) {
6955 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
6956 return 1;
6957 }
6958 else {
6959 int res;
6960 res = obj2ast_expr(state, tmp, &value, arena);
6961 if (res != 0) goto failed;
6962 Py_CLEAR(tmp);
6963 }
6964 *out = Expr(value, lineno, col_offset, end_lineno, end_col_offset,
6965 arena);
6966 if (*out == NULL) goto failed;
6967 return 0;
6968 }
6969 tp = state->Pass_type;
6970 isinstance = PyObject_IsInstance(obj, tp);
6971 if (isinstance == -1) {
6972 return 1;
6973 }
6974 if (isinstance) {
6975
6976 *out = Pass(lineno, col_offset, end_lineno, end_col_offset, arena);
6977 if (*out == NULL) goto failed;
6978 return 0;
6979 }
6980 tp = state->Break_type;
6981 isinstance = PyObject_IsInstance(obj, tp);
6982 if (isinstance == -1) {
6983 return 1;
6984 }
6985 if (isinstance) {
6986
6987 *out = Break(lineno, col_offset, end_lineno, end_col_offset, arena);
6988 if (*out == NULL) goto failed;
6989 return 0;
6990 }
6991 tp = state->Continue_type;
6992 isinstance = PyObject_IsInstance(obj, tp);
6993 if (isinstance == -1) {
6994 return 1;
6995 }
6996 if (isinstance) {
6997
6998 *out = Continue(lineno, col_offset, end_lineno, end_col_offset, arena);
6999 if (*out == NULL) goto failed;
7000 return 0;
7001 }
7002
7003 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
7004 failed:
7005 Py_XDECREF(tmp);
7006 return 1;
7007 }
7008
7009 int
obj2ast_expr(astmodulestate * state,PyObject * obj,expr_ty * out,PyArena * arena)7010 obj2ast_expr(astmodulestate *state, PyObject* obj, expr_ty* out, PyArena* arena)
7011 {
7012 int isinstance;
7013
7014 PyObject *tmp = NULL;
7015 PyObject *tp;
7016 int lineno;
7017 int col_offset;
7018 int end_lineno;
7019 int end_col_offset;
7020
7021 if (obj == Py_None) {
7022 *out = NULL;
7023 return 0;
7024 }
7025 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
7026 return 1;
7027 }
7028 if (tmp == NULL) {
7029 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
7030 return 1;
7031 }
7032 else {
7033 int res;
7034 res = obj2ast_int(state, tmp, &lineno, arena);
7035 if (res != 0) goto failed;
7036 Py_CLEAR(tmp);
7037 }
7038 if (_PyObject_LookupAttr(obj, state->col_offset, &tmp) < 0) {
7039 return 1;
7040 }
7041 if (tmp == NULL) {
7042 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
7043 return 1;
7044 }
7045 else {
7046 int res;
7047 res = obj2ast_int(state, tmp, &col_offset, arena);
7048 if (res != 0) goto failed;
7049 Py_CLEAR(tmp);
7050 }
7051 if (_PyObject_LookupAttr(obj, state->end_lineno, &tmp) < 0) {
7052 return 1;
7053 }
7054 if (tmp == NULL || tmp == Py_None) {
7055 Py_CLEAR(tmp);
7056 end_lineno = 0;
7057 }
7058 else {
7059 int res;
7060 res = obj2ast_int(state, tmp, &end_lineno, arena);
7061 if (res != 0) goto failed;
7062 Py_CLEAR(tmp);
7063 }
7064 if (_PyObject_LookupAttr(obj, state->end_col_offset, &tmp) < 0) {
7065 return 1;
7066 }
7067 if (tmp == NULL || tmp == Py_None) {
7068 Py_CLEAR(tmp);
7069 end_col_offset = 0;
7070 }
7071 else {
7072 int res;
7073 res = obj2ast_int(state, tmp, &end_col_offset, arena);
7074 if (res != 0) goto failed;
7075 Py_CLEAR(tmp);
7076 }
7077 tp = state->BoolOp_type;
7078 isinstance = PyObject_IsInstance(obj, tp);
7079 if (isinstance == -1) {
7080 return 1;
7081 }
7082 if (isinstance) {
7083 boolop_ty op;
7084 asdl_seq* values;
7085
7086 if (_PyObject_LookupAttr(obj, state->op, &tmp) < 0) {
7087 return 1;
7088 }
7089 if (tmp == NULL) {
7090 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
7091 return 1;
7092 }
7093 else {
7094 int res;
7095 res = obj2ast_boolop(state, tmp, &op, arena);
7096 if (res != 0) goto failed;
7097 Py_CLEAR(tmp);
7098 }
7099 if (_PyObject_LookupAttr(obj, state->values, &tmp) < 0) {
7100 return 1;
7101 }
7102 if (tmp == NULL) {
7103 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
7104 return 1;
7105 }
7106 else {
7107 int res;
7108 Py_ssize_t len;
7109 Py_ssize_t i;
7110 if (!PyList_Check(tmp)) {
7111 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7112 goto failed;
7113 }
7114 len = PyList_GET_SIZE(tmp);
7115 values = _Py_asdl_seq_new(len, arena);
7116 if (values == NULL) goto failed;
7117 for (i = 0; i < len; i++) {
7118 expr_ty val;
7119 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7120 Py_INCREF(tmp2);
7121 res = obj2ast_expr(state, tmp2, &val, arena);
7122 Py_DECREF(tmp2);
7123 if (res != 0) goto failed;
7124 if (len != PyList_GET_SIZE(tmp)) {
7125 PyErr_SetString(PyExc_RuntimeError, "BoolOp field \"values\" changed size during iteration");
7126 goto failed;
7127 }
7128 asdl_seq_SET(values, i, val);
7129 }
7130 Py_CLEAR(tmp);
7131 }
7132 *out = BoolOp(op, values, lineno, col_offset, end_lineno,
7133 end_col_offset, arena);
7134 if (*out == NULL) goto failed;
7135 return 0;
7136 }
7137 tp = state->NamedExpr_type;
7138 isinstance = PyObject_IsInstance(obj, tp);
7139 if (isinstance == -1) {
7140 return 1;
7141 }
7142 if (isinstance) {
7143 expr_ty target;
7144 expr_ty value;
7145
7146 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
7147 return 1;
7148 }
7149 if (tmp == NULL) {
7150 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from NamedExpr");
7151 return 1;
7152 }
7153 else {
7154 int res;
7155 res = obj2ast_expr(state, tmp, &target, arena);
7156 if (res != 0) goto failed;
7157 Py_CLEAR(tmp);
7158 }
7159 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
7160 return 1;
7161 }
7162 if (tmp == NULL) {
7163 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NamedExpr");
7164 return 1;
7165 }
7166 else {
7167 int res;
7168 res = obj2ast_expr(state, tmp, &value, arena);
7169 if (res != 0) goto failed;
7170 Py_CLEAR(tmp);
7171 }
7172 *out = NamedExpr(target, value, lineno, col_offset, end_lineno,
7173 end_col_offset, arena);
7174 if (*out == NULL) goto failed;
7175 return 0;
7176 }
7177 tp = state->BinOp_type;
7178 isinstance = PyObject_IsInstance(obj, tp);
7179 if (isinstance == -1) {
7180 return 1;
7181 }
7182 if (isinstance) {
7183 expr_ty left;
7184 operator_ty op;
7185 expr_ty right;
7186
7187 if (_PyObject_LookupAttr(obj, state->left, &tmp) < 0) {
7188 return 1;
7189 }
7190 if (tmp == NULL) {
7191 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
7192 return 1;
7193 }
7194 else {
7195 int res;
7196 res = obj2ast_expr(state, tmp, &left, arena);
7197 if (res != 0) goto failed;
7198 Py_CLEAR(tmp);
7199 }
7200 if (_PyObject_LookupAttr(obj, state->op, &tmp) < 0) {
7201 return 1;
7202 }
7203 if (tmp == NULL) {
7204 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
7205 return 1;
7206 }
7207 else {
7208 int res;
7209 res = obj2ast_operator(state, tmp, &op, arena);
7210 if (res != 0) goto failed;
7211 Py_CLEAR(tmp);
7212 }
7213 if (_PyObject_LookupAttr(obj, state->right, &tmp) < 0) {
7214 return 1;
7215 }
7216 if (tmp == NULL) {
7217 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
7218 return 1;
7219 }
7220 else {
7221 int res;
7222 res = obj2ast_expr(state, tmp, &right, arena);
7223 if (res != 0) goto failed;
7224 Py_CLEAR(tmp);
7225 }
7226 *out = BinOp(left, op, right, lineno, col_offset, end_lineno,
7227 end_col_offset, arena);
7228 if (*out == NULL) goto failed;
7229 return 0;
7230 }
7231 tp = state->UnaryOp_type;
7232 isinstance = PyObject_IsInstance(obj, tp);
7233 if (isinstance == -1) {
7234 return 1;
7235 }
7236 if (isinstance) {
7237 unaryop_ty op;
7238 expr_ty operand;
7239
7240 if (_PyObject_LookupAttr(obj, state->op, &tmp) < 0) {
7241 return 1;
7242 }
7243 if (tmp == NULL) {
7244 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
7245 return 1;
7246 }
7247 else {
7248 int res;
7249 res = obj2ast_unaryop(state, tmp, &op, arena);
7250 if (res != 0) goto failed;
7251 Py_CLEAR(tmp);
7252 }
7253 if (_PyObject_LookupAttr(obj, state->operand, &tmp) < 0) {
7254 return 1;
7255 }
7256 if (tmp == NULL) {
7257 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
7258 return 1;
7259 }
7260 else {
7261 int res;
7262 res = obj2ast_expr(state, tmp, &operand, arena);
7263 if (res != 0) goto failed;
7264 Py_CLEAR(tmp);
7265 }
7266 *out = UnaryOp(op, operand, lineno, col_offset, end_lineno,
7267 end_col_offset, arena);
7268 if (*out == NULL) goto failed;
7269 return 0;
7270 }
7271 tp = state->Lambda_type;
7272 isinstance = PyObject_IsInstance(obj, tp);
7273 if (isinstance == -1) {
7274 return 1;
7275 }
7276 if (isinstance) {
7277 arguments_ty args;
7278 expr_ty body;
7279
7280 if (_PyObject_LookupAttr(obj, state->args, &tmp) < 0) {
7281 return 1;
7282 }
7283 if (tmp == NULL) {
7284 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
7285 return 1;
7286 }
7287 else {
7288 int res;
7289 res = obj2ast_arguments(state, tmp, &args, arena);
7290 if (res != 0) goto failed;
7291 Py_CLEAR(tmp);
7292 }
7293 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
7294 return 1;
7295 }
7296 if (tmp == NULL) {
7297 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
7298 return 1;
7299 }
7300 else {
7301 int res;
7302 res = obj2ast_expr(state, tmp, &body, arena);
7303 if (res != 0) goto failed;
7304 Py_CLEAR(tmp);
7305 }
7306 *out = Lambda(args, body, lineno, col_offset, end_lineno,
7307 end_col_offset, arena);
7308 if (*out == NULL) goto failed;
7309 return 0;
7310 }
7311 tp = state->IfExp_type;
7312 isinstance = PyObject_IsInstance(obj, tp);
7313 if (isinstance == -1) {
7314 return 1;
7315 }
7316 if (isinstance) {
7317 expr_ty test;
7318 expr_ty body;
7319 expr_ty orelse;
7320
7321 if (_PyObject_LookupAttr(obj, state->test, &tmp) < 0) {
7322 return 1;
7323 }
7324 if (tmp == NULL) {
7325 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
7326 return 1;
7327 }
7328 else {
7329 int res;
7330 res = obj2ast_expr(state, tmp, &test, arena);
7331 if (res != 0) goto failed;
7332 Py_CLEAR(tmp);
7333 }
7334 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
7335 return 1;
7336 }
7337 if (tmp == NULL) {
7338 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
7339 return 1;
7340 }
7341 else {
7342 int res;
7343 res = obj2ast_expr(state, tmp, &body, arena);
7344 if (res != 0) goto failed;
7345 Py_CLEAR(tmp);
7346 }
7347 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
7348 return 1;
7349 }
7350 if (tmp == NULL) {
7351 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
7352 return 1;
7353 }
7354 else {
7355 int res;
7356 res = obj2ast_expr(state, tmp, &orelse, arena);
7357 if (res != 0) goto failed;
7358 Py_CLEAR(tmp);
7359 }
7360 *out = IfExp(test, body, orelse, lineno, col_offset, end_lineno,
7361 end_col_offset, arena);
7362 if (*out == NULL) goto failed;
7363 return 0;
7364 }
7365 tp = state->Dict_type;
7366 isinstance = PyObject_IsInstance(obj, tp);
7367 if (isinstance == -1) {
7368 return 1;
7369 }
7370 if (isinstance) {
7371 asdl_seq* keys;
7372 asdl_seq* values;
7373
7374 if (_PyObject_LookupAttr(obj, state->keys, &tmp) < 0) {
7375 return 1;
7376 }
7377 if (tmp == NULL) {
7378 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
7379 return 1;
7380 }
7381 else {
7382 int res;
7383 Py_ssize_t len;
7384 Py_ssize_t i;
7385 if (!PyList_Check(tmp)) {
7386 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7387 goto failed;
7388 }
7389 len = PyList_GET_SIZE(tmp);
7390 keys = _Py_asdl_seq_new(len, arena);
7391 if (keys == NULL) goto failed;
7392 for (i = 0; i < len; i++) {
7393 expr_ty val;
7394 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7395 Py_INCREF(tmp2);
7396 res = obj2ast_expr(state, tmp2, &val, arena);
7397 Py_DECREF(tmp2);
7398 if (res != 0) goto failed;
7399 if (len != PyList_GET_SIZE(tmp)) {
7400 PyErr_SetString(PyExc_RuntimeError, "Dict field \"keys\" changed size during iteration");
7401 goto failed;
7402 }
7403 asdl_seq_SET(keys, i, val);
7404 }
7405 Py_CLEAR(tmp);
7406 }
7407 if (_PyObject_LookupAttr(obj, state->values, &tmp) < 0) {
7408 return 1;
7409 }
7410 if (tmp == NULL) {
7411 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
7412 return 1;
7413 }
7414 else {
7415 int res;
7416 Py_ssize_t len;
7417 Py_ssize_t i;
7418 if (!PyList_Check(tmp)) {
7419 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7420 goto failed;
7421 }
7422 len = PyList_GET_SIZE(tmp);
7423 values = _Py_asdl_seq_new(len, arena);
7424 if (values == NULL) goto failed;
7425 for (i = 0; i < len; i++) {
7426 expr_ty val;
7427 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7428 Py_INCREF(tmp2);
7429 res = obj2ast_expr(state, tmp2, &val, arena);
7430 Py_DECREF(tmp2);
7431 if (res != 0) goto failed;
7432 if (len != PyList_GET_SIZE(tmp)) {
7433 PyErr_SetString(PyExc_RuntimeError, "Dict field \"values\" changed size during iteration");
7434 goto failed;
7435 }
7436 asdl_seq_SET(values, i, val);
7437 }
7438 Py_CLEAR(tmp);
7439 }
7440 *out = Dict(keys, values, lineno, col_offset, end_lineno,
7441 end_col_offset, arena);
7442 if (*out == NULL) goto failed;
7443 return 0;
7444 }
7445 tp = state->Set_type;
7446 isinstance = PyObject_IsInstance(obj, tp);
7447 if (isinstance == -1) {
7448 return 1;
7449 }
7450 if (isinstance) {
7451 asdl_seq* elts;
7452
7453 if (_PyObject_LookupAttr(obj, state->elts, &tmp) < 0) {
7454 return 1;
7455 }
7456 if (tmp == NULL) {
7457 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
7458 return 1;
7459 }
7460 else {
7461 int res;
7462 Py_ssize_t len;
7463 Py_ssize_t i;
7464 if (!PyList_Check(tmp)) {
7465 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7466 goto failed;
7467 }
7468 len = PyList_GET_SIZE(tmp);
7469 elts = _Py_asdl_seq_new(len, arena);
7470 if (elts == NULL) goto failed;
7471 for (i = 0; i < len; i++) {
7472 expr_ty val;
7473 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7474 Py_INCREF(tmp2);
7475 res = obj2ast_expr(state, tmp2, &val, arena);
7476 Py_DECREF(tmp2);
7477 if (res != 0) goto failed;
7478 if (len != PyList_GET_SIZE(tmp)) {
7479 PyErr_SetString(PyExc_RuntimeError, "Set field \"elts\" changed size during iteration");
7480 goto failed;
7481 }
7482 asdl_seq_SET(elts, i, val);
7483 }
7484 Py_CLEAR(tmp);
7485 }
7486 *out = Set(elts, lineno, col_offset, end_lineno, end_col_offset, arena);
7487 if (*out == NULL) goto failed;
7488 return 0;
7489 }
7490 tp = state->ListComp_type;
7491 isinstance = PyObject_IsInstance(obj, tp);
7492 if (isinstance == -1) {
7493 return 1;
7494 }
7495 if (isinstance) {
7496 expr_ty elt;
7497 asdl_seq* generators;
7498
7499 if (_PyObject_LookupAttr(obj, state->elt, &tmp) < 0) {
7500 return 1;
7501 }
7502 if (tmp == NULL) {
7503 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
7504 return 1;
7505 }
7506 else {
7507 int res;
7508 res = obj2ast_expr(state, tmp, &elt, arena);
7509 if (res != 0) goto failed;
7510 Py_CLEAR(tmp);
7511 }
7512 if (_PyObject_LookupAttr(obj, state->generators, &tmp) < 0) {
7513 return 1;
7514 }
7515 if (tmp == NULL) {
7516 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
7517 return 1;
7518 }
7519 else {
7520 int res;
7521 Py_ssize_t len;
7522 Py_ssize_t i;
7523 if (!PyList_Check(tmp)) {
7524 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7525 goto failed;
7526 }
7527 len = PyList_GET_SIZE(tmp);
7528 generators = _Py_asdl_seq_new(len, arena);
7529 if (generators == NULL) goto failed;
7530 for (i = 0; i < len; i++) {
7531 comprehension_ty val;
7532 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7533 Py_INCREF(tmp2);
7534 res = obj2ast_comprehension(state, tmp2, &val, arena);
7535 Py_DECREF(tmp2);
7536 if (res != 0) goto failed;
7537 if (len != PyList_GET_SIZE(tmp)) {
7538 PyErr_SetString(PyExc_RuntimeError, "ListComp field \"generators\" changed size during iteration");
7539 goto failed;
7540 }
7541 asdl_seq_SET(generators, i, val);
7542 }
7543 Py_CLEAR(tmp);
7544 }
7545 *out = ListComp(elt, generators, lineno, col_offset, end_lineno,
7546 end_col_offset, arena);
7547 if (*out == NULL) goto failed;
7548 return 0;
7549 }
7550 tp = state->SetComp_type;
7551 isinstance = PyObject_IsInstance(obj, tp);
7552 if (isinstance == -1) {
7553 return 1;
7554 }
7555 if (isinstance) {
7556 expr_ty elt;
7557 asdl_seq* generators;
7558
7559 if (_PyObject_LookupAttr(obj, state->elt, &tmp) < 0) {
7560 return 1;
7561 }
7562 if (tmp == NULL) {
7563 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
7564 return 1;
7565 }
7566 else {
7567 int res;
7568 res = obj2ast_expr(state, tmp, &elt, arena);
7569 if (res != 0) goto failed;
7570 Py_CLEAR(tmp);
7571 }
7572 if (_PyObject_LookupAttr(obj, state->generators, &tmp) < 0) {
7573 return 1;
7574 }
7575 if (tmp == NULL) {
7576 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
7577 return 1;
7578 }
7579 else {
7580 int res;
7581 Py_ssize_t len;
7582 Py_ssize_t i;
7583 if (!PyList_Check(tmp)) {
7584 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7585 goto failed;
7586 }
7587 len = PyList_GET_SIZE(tmp);
7588 generators = _Py_asdl_seq_new(len, arena);
7589 if (generators == NULL) goto failed;
7590 for (i = 0; i < len; i++) {
7591 comprehension_ty val;
7592 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7593 Py_INCREF(tmp2);
7594 res = obj2ast_comprehension(state, tmp2, &val, arena);
7595 Py_DECREF(tmp2);
7596 if (res != 0) goto failed;
7597 if (len != PyList_GET_SIZE(tmp)) {
7598 PyErr_SetString(PyExc_RuntimeError, "SetComp field \"generators\" changed size during iteration");
7599 goto failed;
7600 }
7601 asdl_seq_SET(generators, i, val);
7602 }
7603 Py_CLEAR(tmp);
7604 }
7605 *out = SetComp(elt, generators, lineno, col_offset, end_lineno,
7606 end_col_offset, arena);
7607 if (*out == NULL) goto failed;
7608 return 0;
7609 }
7610 tp = state->DictComp_type;
7611 isinstance = PyObject_IsInstance(obj, tp);
7612 if (isinstance == -1) {
7613 return 1;
7614 }
7615 if (isinstance) {
7616 expr_ty key;
7617 expr_ty value;
7618 asdl_seq* generators;
7619
7620 if (_PyObject_LookupAttr(obj, state->key, &tmp) < 0) {
7621 return 1;
7622 }
7623 if (tmp == NULL) {
7624 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
7625 return 1;
7626 }
7627 else {
7628 int res;
7629 res = obj2ast_expr(state, tmp, &key, arena);
7630 if (res != 0) goto failed;
7631 Py_CLEAR(tmp);
7632 }
7633 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
7634 return 1;
7635 }
7636 if (tmp == NULL) {
7637 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
7638 return 1;
7639 }
7640 else {
7641 int res;
7642 res = obj2ast_expr(state, tmp, &value, arena);
7643 if (res != 0) goto failed;
7644 Py_CLEAR(tmp);
7645 }
7646 if (_PyObject_LookupAttr(obj, state->generators, &tmp) < 0) {
7647 return 1;
7648 }
7649 if (tmp == NULL) {
7650 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
7651 return 1;
7652 }
7653 else {
7654 int res;
7655 Py_ssize_t len;
7656 Py_ssize_t i;
7657 if (!PyList_Check(tmp)) {
7658 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7659 goto failed;
7660 }
7661 len = PyList_GET_SIZE(tmp);
7662 generators = _Py_asdl_seq_new(len, arena);
7663 if (generators == NULL) goto failed;
7664 for (i = 0; i < len; i++) {
7665 comprehension_ty val;
7666 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7667 Py_INCREF(tmp2);
7668 res = obj2ast_comprehension(state, tmp2, &val, arena);
7669 Py_DECREF(tmp2);
7670 if (res != 0) goto failed;
7671 if (len != PyList_GET_SIZE(tmp)) {
7672 PyErr_SetString(PyExc_RuntimeError, "DictComp field \"generators\" changed size during iteration");
7673 goto failed;
7674 }
7675 asdl_seq_SET(generators, i, val);
7676 }
7677 Py_CLEAR(tmp);
7678 }
7679 *out = DictComp(key, value, generators, lineno, col_offset, end_lineno,
7680 end_col_offset, arena);
7681 if (*out == NULL) goto failed;
7682 return 0;
7683 }
7684 tp = state->GeneratorExp_type;
7685 isinstance = PyObject_IsInstance(obj, tp);
7686 if (isinstance == -1) {
7687 return 1;
7688 }
7689 if (isinstance) {
7690 expr_ty elt;
7691 asdl_seq* generators;
7692
7693 if (_PyObject_LookupAttr(obj, state->elt, &tmp) < 0) {
7694 return 1;
7695 }
7696 if (tmp == NULL) {
7697 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
7698 return 1;
7699 }
7700 else {
7701 int res;
7702 res = obj2ast_expr(state, tmp, &elt, arena);
7703 if (res != 0) goto failed;
7704 Py_CLEAR(tmp);
7705 }
7706 if (_PyObject_LookupAttr(obj, state->generators, &tmp) < 0) {
7707 return 1;
7708 }
7709 if (tmp == NULL) {
7710 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
7711 return 1;
7712 }
7713 else {
7714 int res;
7715 Py_ssize_t len;
7716 Py_ssize_t i;
7717 if (!PyList_Check(tmp)) {
7718 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7719 goto failed;
7720 }
7721 len = PyList_GET_SIZE(tmp);
7722 generators = _Py_asdl_seq_new(len, arena);
7723 if (generators == NULL) goto failed;
7724 for (i = 0; i < len; i++) {
7725 comprehension_ty val;
7726 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7727 Py_INCREF(tmp2);
7728 res = obj2ast_comprehension(state, tmp2, &val, arena);
7729 Py_DECREF(tmp2);
7730 if (res != 0) goto failed;
7731 if (len != PyList_GET_SIZE(tmp)) {
7732 PyErr_SetString(PyExc_RuntimeError, "GeneratorExp field \"generators\" changed size during iteration");
7733 goto failed;
7734 }
7735 asdl_seq_SET(generators, i, val);
7736 }
7737 Py_CLEAR(tmp);
7738 }
7739 *out = GeneratorExp(elt, generators, lineno, col_offset, end_lineno,
7740 end_col_offset, arena);
7741 if (*out == NULL) goto failed;
7742 return 0;
7743 }
7744 tp = state->Await_type;
7745 isinstance = PyObject_IsInstance(obj, tp);
7746 if (isinstance == -1) {
7747 return 1;
7748 }
7749 if (isinstance) {
7750 expr_ty value;
7751
7752 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
7753 return 1;
7754 }
7755 if (tmp == NULL) {
7756 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
7757 return 1;
7758 }
7759 else {
7760 int res;
7761 res = obj2ast_expr(state, tmp, &value, arena);
7762 if (res != 0) goto failed;
7763 Py_CLEAR(tmp);
7764 }
7765 *out = Await(value, lineno, col_offset, end_lineno, end_col_offset,
7766 arena);
7767 if (*out == NULL) goto failed;
7768 return 0;
7769 }
7770 tp = state->Yield_type;
7771 isinstance = PyObject_IsInstance(obj, tp);
7772 if (isinstance == -1) {
7773 return 1;
7774 }
7775 if (isinstance) {
7776 expr_ty value;
7777
7778 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
7779 return 1;
7780 }
7781 if (tmp == NULL || tmp == Py_None) {
7782 Py_CLEAR(tmp);
7783 value = NULL;
7784 }
7785 else {
7786 int res;
7787 res = obj2ast_expr(state, tmp, &value, arena);
7788 if (res != 0) goto failed;
7789 Py_CLEAR(tmp);
7790 }
7791 *out = Yield(value, lineno, col_offset, end_lineno, end_col_offset,
7792 arena);
7793 if (*out == NULL) goto failed;
7794 return 0;
7795 }
7796 tp = state->YieldFrom_type;
7797 isinstance = PyObject_IsInstance(obj, tp);
7798 if (isinstance == -1) {
7799 return 1;
7800 }
7801 if (isinstance) {
7802 expr_ty value;
7803
7804 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
7805 return 1;
7806 }
7807 if (tmp == NULL) {
7808 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
7809 return 1;
7810 }
7811 else {
7812 int res;
7813 res = obj2ast_expr(state, tmp, &value, arena);
7814 if (res != 0) goto failed;
7815 Py_CLEAR(tmp);
7816 }
7817 *out = YieldFrom(value, lineno, col_offset, end_lineno, end_col_offset,
7818 arena);
7819 if (*out == NULL) goto failed;
7820 return 0;
7821 }
7822 tp = state->Compare_type;
7823 isinstance = PyObject_IsInstance(obj, tp);
7824 if (isinstance == -1) {
7825 return 1;
7826 }
7827 if (isinstance) {
7828 expr_ty left;
7829 asdl_int_seq* ops;
7830 asdl_seq* comparators;
7831
7832 if (_PyObject_LookupAttr(obj, state->left, &tmp) < 0) {
7833 return 1;
7834 }
7835 if (tmp == NULL) {
7836 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
7837 return 1;
7838 }
7839 else {
7840 int res;
7841 res = obj2ast_expr(state, tmp, &left, arena);
7842 if (res != 0) goto failed;
7843 Py_CLEAR(tmp);
7844 }
7845 if (_PyObject_LookupAttr(obj, state->ops, &tmp) < 0) {
7846 return 1;
7847 }
7848 if (tmp == NULL) {
7849 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
7850 return 1;
7851 }
7852 else {
7853 int res;
7854 Py_ssize_t len;
7855 Py_ssize_t i;
7856 if (!PyList_Check(tmp)) {
7857 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7858 goto failed;
7859 }
7860 len = PyList_GET_SIZE(tmp);
7861 ops = _Py_asdl_int_seq_new(len, arena);
7862 if (ops == NULL) goto failed;
7863 for (i = 0; i < len; i++) {
7864 cmpop_ty val;
7865 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7866 Py_INCREF(tmp2);
7867 res = obj2ast_cmpop(state, tmp2, &val, arena);
7868 Py_DECREF(tmp2);
7869 if (res != 0) goto failed;
7870 if (len != PyList_GET_SIZE(tmp)) {
7871 PyErr_SetString(PyExc_RuntimeError, "Compare field \"ops\" changed size during iteration");
7872 goto failed;
7873 }
7874 asdl_seq_SET(ops, i, val);
7875 }
7876 Py_CLEAR(tmp);
7877 }
7878 if (_PyObject_LookupAttr(obj, state->comparators, &tmp) < 0) {
7879 return 1;
7880 }
7881 if (tmp == NULL) {
7882 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
7883 return 1;
7884 }
7885 else {
7886 int res;
7887 Py_ssize_t len;
7888 Py_ssize_t i;
7889 if (!PyList_Check(tmp)) {
7890 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7891 goto failed;
7892 }
7893 len = PyList_GET_SIZE(tmp);
7894 comparators = _Py_asdl_seq_new(len, arena);
7895 if (comparators == NULL) goto failed;
7896 for (i = 0; i < len; i++) {
7897 expr_ty val;
7898 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7899 Py_INCREF(tmp2);
7900 res = obj2ast_expr(state, tmp2, &val, arena);
7901 Py_DECREF(tmp2);
7902 if (res != 0) goto failed;
7903 if (len != PyList_GET_SIZE(tmp)) {
7904 PyErr_SetString(PyExc_RuntimeError, "Compare field \"comparators\" changed size during iteration");
7905 goto failed;
7906 }
7907 asdl_seq_SET(comparators, i, val);
7908 }
7909 Py_CLEAR(tmp);
7910 }
7911 *out = Compare(left, ops, comparators, lineno, col_offset, end_lineno,
7912 end_col_offset, arena);
7913 if (*out == NULL) goto failed;
7914 return 0;
7915 }
7916 tp = state->Call_type;
7917 isinstance = PyObject_IsInstance(obj, tp);
7918 if (isinstance == -1) {
7919 return 1;
7920 }
7921 if (isinstance) {
7922 expr_ty func;
7923 asdl_seq* args;
7924 asdl_seq* keywords;
7925
7926 if (_PyObject_LookupAttr(obj, state->func, &tmp) < 0) {
7927 return 1;
7928 }
7929 if (tmp == NULL) {
7930 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
7931 return 1;
7932 }
7933 else {
7934 int res;
7935 res = obj2ast_expr(state, tmp, &func, arena);
7936 if (res != 0) goto failed;
7937 Py_CLEAR(tmp);
7938 }
7939 if (_PyObject_LookupAttr(obj, state->args, &tmp) < 0) {
7940 return 1;
7941 }
7942 if (tmp == NULL) {
7943 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
7944 return 1;
7945 }
7946 else {
7947 int res;
7948 Py_ssize_t len;
7949 Py_ssize_t i;
7950 if (!PyList_Check(tmp)) {
7951 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7952 goto failed;
7953 }
7954 len = PyList_GET_SIZE(tmp);
7955 args = _Py_asdl_seq_new(len, arena);
7956 if (args == NULL) goto failed;
7957 for (i = 0; i < len; i++) {
7958 expr_ty val;
7959 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7960 Py_INCREF(tmp2);
7961 res = obj2ast_expr(state, tmp2, &val, arena);
7962 Py_DECREF(tmp2);
7963 if (res != 0) goto failed;
7964 if (len != PyList_GET_SIZE(tmp)) {
7965 PyErr_SetString(PyExc_RuntimeError, "Call field \"args\" changed size during iteration");
7966 goto failed;
7967 }
7968 asdl_seq_SET(args, i, val);
7969 }
7970 Py_CLEAR(tmp);
7971 }
7972 if (_PyObject_LookupAttr(obj, state->keywords, &tmp) < 0) {
7973 return 1;
7974 }
7975 if (tmp == NULL) {
7976 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
7977 return 1;
7978 }
7979 else {
7980 int res;
7981 Py_ssize_t len;
7982 Py_ssize_t i;
7983 if (!PyList_Check(tmp)) {
7984 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
7985 goto failed;
7986 }
7987 len = PyList_GET_SIZE(tmp);
7988 keywords = _Py_asdl_seq_new(len, arena);
7989 if (keywords == NULL) goto failed;
7990 for (i = 0; i < len; i++) {
7991 keyword_ty val;
7992 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7993 Py_INCREF(tmp2);
7994 res = obj2ast_keyword(state, tmp2, &val, arena);
7995 Py_DECREF(tmp2);
7996 if (res != 0) goto failed;
7997 if (len != PyList_GET_SIZE(tmp)) {
7998 PyErr_SetString(PyExc_RuntimeError, "Call field \"keywords\" changed size during iteration");
7999 goto failed;
8000 }
8001 asdl_seq_SET(keywords, i, val);
8002 }
8003 Py_CLEAR(tmp);
8004 }
8005 *out = Call(func, args, keywords, lineno, col_offset, end_lineno,
8006 end_col_offset, arena);
8007 if (*out == NULL) goto failed;
8008 return 0;
8009 }
8010 tp = state->FormattedValue_type;
8011 isinstance = PyObject_IsInstance(obj, tp);
8012 if (isinstance == -1) {
8013 return 1;
8014 }
8015 if (isinstance) {
8016 expr_ty value;
8017 int conversion;
8018 expr_ty format_spec;
8019
8020 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
8021 return 1;
8022 }
8023 if (tmp == NULL) {
8024 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
8025 return 1;
8026 }
8027 else {
8028 int res;
8029 res = obj2ast_expr(state, tmp, &value, arena);
8030 if (res != 0) goto failed;
8031 Py_CLEAR(tmp);
8032 }
8033 if (_PyObject_LookupAttr(obj, state->conversion, &tmp) < 0) {
8034 return 1;
8035 }
8036 if (tmp == NULL || tmp == Py_None) {
8037 Py_CLEAR(tmp);
8038 conversion = 0;
8039 }
8040 else {
8041 int res;
8042 res = obj2ast_int(state, tmp, &conversion, arena);
8043 if (res != 0) goto failed;
8044 Py_CLEAR(tmp);
8045 }
8046 if (_PyObject_LookupAttr(obj, state->format_spec, &tmp) < 0) {
8047 return 1;
8048 }
8049 if (tmp == NULL || tmp == Py_None) {
8050 Py_CLEAR(tmp);
8051 format_spec = NULL;
8052 }
8053 else {
8054 int res;
8055 res = obj2ast_expr(state, tmp, &format_spec, arena);
8056 if (res != 0) goto failed;
8057 Py_CLEAR(tmp);
8058 }
8059 *out = FormattedValue(value, conversion, format_spec, lineno,
8060 col_offset, end_lineno, end_col_offset, arena);
8061 if (*out == NULL) goto failed;
8062 return 0;
8063 }
8064 tp = state->JoinedStr_type;
8065 isinstance = PyObject_IsInstance(obj, tp);
8066 if (isinstance == -1) {
8067 return 1;
8068 }
8069 if (isinstance) {
8070 asdl_seq* values;
8071
8072 if (_PyObject_LookupAttr(obj, state->values, &tmp) < 0) {
8073 return 1;
8074 }
8075 if (tmp == NULL) {
8076 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
8077 return 1;
8078 }
8079 else {
8080 int res;
8081 Py_ssize_t len;
8082 Py_ssize_t i;
8083 if (!PyList_Check(tmp)) {
8084 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
8085 goto failed;
8086 }
8087 len = PyList_GET_SIZE(tmp);
8088 values = _Py_asdl_seq_new(len, arena);
8089 if (values == NULL) goto failed;
8090 for (i = 0; i < len; i++) {
8091 expr_ty val;
8092 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8093 Py_INCREF(tmp2);
8094 res = obj2ast_expr(state, tmp2, &val, arena);
8095 Py_DECREF(tmp2);
8096 if (res != 0) goto failed;
8097 if (len != PyList_GET_SIZE(tmp)) {
8098 PyErr_SetString(PyExc_RuntimeError, "JoinedStr field \"values\" changed size during iteration");
8099 goto failed;
8100 }
8101 asdl_seq_SET(values, i, val);
8102 }
8103 Py_CLEAR(tmp);
8104 }
8105 *out = JoinedStr(values, lineno, col_offset, end_lineno,
8106 end_col_offset, arena);
8107 if (*out == NULL) goto failed;
8108 return 0;
8109 }
8110 tp = state->Constant_type;
8111 isinstance = PyObject_IsInstance(obj, tp);
8112 if (isinstance == -1) {
8113 return 1;
8114 }
8115 if (isinstance) {
8116 constant value;
8117 string kind;
8118
8119 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
8120 return 1;
8121 }
8122 if (tmp == NULL) {
8123 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
8124 return 1;
8125 }
8126 else {
8127 int res;
8128 res = obj2ast_constant(state, tmp, &value, arena);
8129 if (res != 0) goto failed;
8130 Py_CLEAR(tmp);
8131 }
8132 if (_PyObject_LookupAttr(obj, state->kind, &tmp) < 0) {
8133 return 1;
8134 }
8135 if (tmp == NULL || tmp == Py_None) {
8136 Py_CLEAR(tmp);
8137 kind = NULL;
8138 }
8139 else {
8140 int res;
8141 res = obj2ast_string(state, tmp, &kind, arena);
8142 if (res != 0) goto failed;
8143 Py_CLEAR(tmp);
8144 }
8145 *out = Constant(value, kind, lineno, col_offset, end_lineno,
8146 end_col_offset, arena);
8147 if (*out == NULL) goto failed;
8148 return 0;
8149 }
8150 tp = state->Attribute_type;
8151 isinstance = PyObject_IsInstance(obj, tp);
8152 if (isinstance == -1) {
8153 return 1;
8154 }
8155 if (isinstance) {
8156 expr_ty value;
8157 identifier attr;
8158 expr_context_ty ctx;
8159
8160 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
8161 return 1;
8162 }
8163 if (tmp == NULL) {
8164 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
8165 return 1;
8166 }
8167 else {
8168 int res;
8169 res = obj2ast_expr(state, tmp, &value, arena);
8170 if (res != 0) goto failed;
8171 Py_CLEAR(tmp);
8172 }
8173 if (_PyObject_LookupAttr(obj, state->attr, &tmp) < 0) {
8174 return 1;
8175 }
8176 if (tmp == NULL) {
8177 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
8178 return 1;
8179 }
8180 else {
8181 int res;
8182 res = obj2ast_identifier(state, tmp, &attr, arena);
8183 if (res != 0) goto failed;
8184 Py_CLEAR(tmp);
8185 }
8186 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
8187 return 1;
8188 }
8189 if (tmp == NULL) {
8190 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
8191 return 1;
8192 }
8193 else {
8194 int res;
8195 res = obj2ast_expr_context(state, tmp, &ctx, arena);
8196 if (res != 0) goto failed;
8197 Py_CLEAR(tmp);
8198 }
8199 *out = Attribute(value, attr, ctx, lineno, col_offset, end_lineno,
8200 end_col_offset, arena);
8201 if (*out == NULL) goto failed;
8202 return 0;
8203 }
8204 tp = state->Subscript_type;
8205 isinstance = PyObject_IsInstance(obj, tp);
8206 if (isinstance == -1) {
8207 return 1;
8208 }
8209 if (isinstance) {
8210 expr_ty value;
8211 expr_ty slice;
8212 expr_context_ty ctx;
8213
8214 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
8215 return 1;
8216 }
8217 if (tmp == NULL) {
8218 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
8219 return 1;
8220 }
8221 else {
8222 int res;
8223 res = obj2ast_expr(state, tmp, &value, arena);
8224 if (res != 0) goto failed;
8225 Py_CLEAR(tmp);
8226 }
8227 if (_PyObject_LookupAttr(obj, state->slice, &tmp) < 0) {
8228 return 1;
8229 }
8230 if (tmp == NULL) {
8231 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
8232 return 1;
8233 }
8234 else {
8235 int res;
8236 res = obj2ast_expr(state, tmp, &slice, arena);
8237 if (res != 0) goto failed;
8238 Py_CLEAR(tmp);
8239 }
8240 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
8241 return 1;
8242 }
8243 if (tmp == NULL) {
8244 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
8245 return 1;
8246 }
8247 else {
8248 int res;
8249 res = obj2ast_expr_context(state, tmp, &ctx, arena);
8250 if (res != 0) goto failed;
8251 Py_CLEAR(tmp);
8252 }
8253 *out = Subscript(value, slice, ctx, lineno, col_offset, end_lineno,
8254 end_col_offset, arena);
8255 if (*out == NULL) goto failed;
8256 return 0;
8257 }
8258 tp = state->Starred_type;
8259 isinstance = PyObject_IsInstance(obj, tp);
8260 if (isinstance == -1) {
8261 return 1;
8262 }
8263 if (isinstance) {
8264 expr_ty value;
8265 expr_context_ty ctx;
8266
8267 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
8268 return 1;
8269 }
8270 if (tmp == NULL) {
8271 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
8272 return 1;
8273 }
8274 else {
8275 int res;
8276 res = obj2ast_expr(state, tmp, &value, arena);
8277 if (res != 0) goto failed;
8278 Py_CLEAR(tmp);
8279 }
8280 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
8281 return 1;
8282 }
8283 if (tmp == NULL) {
8284 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
8285 return 1;
8286 }
8287 else {
8288 int res;
8289 res = obj2ast_expr_context(state, tmp, &ctx, arena);
8290 if (res != 0) goto failed;
8291 Py_CLEAR(tmp);
8292 }
8293 *out = Starred(value, ctx, lineno, col_offset, end_lineno,
8294 end_col_offset, arena);
8295 if (*out == NULL) goto failed;
8296 return 0;
8297 }
8298 tp = state->Name_type;
8299 isinstance = PyObject_IsInstance(obj, tp);
8300 if (isinstance == -1) {
8301 return 1;
8302 }
8303 if (isinstance) {
8304 identifier id;
8305 expr_context_ty ctx;
8306
8307 if (_PyObject_LookupAttr(obj, state->id, &tmp) < 0) {
8308 return 1;
8309 }
8310 if (tmp == NULL) {
8311 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
8312 return 1;
8313 }
8314 else {
8315 int res;
8316 res = obj2ast_identifier(state, tmp, &id, arena);
8317 if (res != 0) goto failed;
8318 Py_CLEAR(tmp);
8319 }
8320 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
8321 return 1;
8322 }
8323 if (tmp == NULL) {
8324 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
8325 return 1;
8326 }
8327 else {
8328 int res;
8329 res = obj2ast_expr_context(state, tmp, &ctx, arena);
8330 if (res != 0) goto failed;
8331 Py_CLEAR(tmp);
8332 }
8333 *out = Name(id, ctx, lineno, col_offset, end_lineno, end_col_offset,
8334 arena);
8335 if (*out == NULL) goto failed;
8336 return 0;
8337 }
8338 tp = state->List_type;
8339 isinstance = PyObject_IsInstance(obj, tp);
8340 if (isinstance == -1) {
8341 return 1;
8342 }
8343 if (isinstance) {
8344 asdl_seq* elts;
8345 expr_context_ty ctx;
8346
8347 if (_PyObject_LookupAttr(obj, state->elts, &tmp) < 0) {
8348 return 1;
8349 }
8350 if (tmp == NULL) {
8351 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
8352 return 1;
8353 }
8354 else {
8355 int res;
8356 Py_ssize_t len;
8357 Py_ssize_t i;
8358 if (!PyList_Check(tmp)) {
8359 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
8360 goto failed;
8361 }
8362 len = PyList_GET_SIZE(tmp);
8363 elts = _Py_asdl_seq_new(len, arena);
8364 if (elts == NULL) goto failed;
8365 for (i = 0; i < len; i++) {
8366 expr_ty val;
8367 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8368 Py_INCREF(tmp2);
8369 res = obj2ast_expr(state, tmp2, &val, arena);
8370 Py_DECREF(tmp2);
8371 if (res != 0) goto failed;
8372 if (len != PyList_GET_SIZE(tmp)) {
8373 PyErr_SetString(PyExc_RuntimeError, "List field \"elts\" changed size during iteration");
8374 goto failed;
8375 }
8376 asdl_seq_SET(elts, i, val);
8377 }
8378 Py_CLEAR(tmp);
8379 }
8380 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
8381 return 1;
8382 }
8383 if (tmp == NULL) {
8384 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
8385 return 1;
8386 }
8387 else {
8388 int res;
8389 res = obj2ast_expr_context(state, tmp, &ctx, arena);
8390 if (res != 0) goto failed;
8391 Py_CLEAR(tmp);
8392 }
8393 *out = List(elts, ctx, lineno, col_offset, end_lineno, end_col_offset,
8394 arena);
8395 if (*out == NULL) goto failed;
8396 return 0;
8397 }
8398 tp = state->Tuple_type;
8399 isinstance = PyObject_IsInstance(obj, tp);
8400 if (isinstance == -1) {
8401 return 1;
8402 }
8403 if (isinstance) {
8404 asdl_seq* elts;
8405 expr_context_ty ctx;
8406
8407 if (_PyObject_LookupAttr(obj, state->elts, &tmp) < 0) {
8408 return 1;
8409 }
8410 if (tmp == NULL) {
8411 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
8412 return 1;
8413 }
8414 else {
8415 int res;
8416 Py_ssize_t len;
8417 Py_ssize_t i;
8418 if (!PyList_Check(tmp)) {
8419 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
8420 goto failed;
8421 }
8422 len = PyList_GET_SIZE(tmp);
8423 elts = _Py_asdl_seq_new(len, arena);
8424 if (elts == NULL) goto failed;
8425 for (i = 0; i < len; i++) {
8426 expr_ty val;
8427 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8428 Py_INCREF(tmp2);
8429 res = obj2ast_expr(state, tmp2, &val, arena);
8430 Py_DECREF(tmp2);
8431 if (res != 0) goto failed;
8432 if (len != PyList_GET_SIZE(tmp)) {
8433 PyErr_SetString(PyExc_RuntimeError, "Tuple field \"elts\" changed size during iteration");
8434 goto failed;
8435 }
8436 asdl_seq_SET(elts, i, val);
8437 }
8438 Py_CLEAR(tmp);
8439 }
8440 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
8441 return 1;
8442 }
8443 if (tmp == NULL) {
8444 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
8445 return 1;
8446 }
8447 else {
8448 int res;
8449 res = obj2ast_expr_context(state, tmp, &ctx, arena);
8450 if (res != 0) goto failed;
8451 Py_CLEAR(tmp);
8452 }
8453 *out = Tuple(elts, ctx, lineno, col_offset, end_lineno, end_col_offset,
8454 arena);
8455 if (*out == NULL) goto failed;
8456 return 0;
8457 }
8458 tp = state->Slice_type;
8459 isinstance = PyObject_IsInstance(obj, tp);
8460 if (isinstance == -1) {
8461 return 1;
8462 }
8463 if (isinstance) {
8464 expr_ty lower;
8465 expr_ty upper;
8466 expr_ty step;
8467
8468 if (_PyObject_LookupAttr(obj, state->lower, &tmp) < 0) {
8469 return 1;
8470 }
8471 if (tmp == NULL || tmp == Py_None) {
8472 Py_CLEAR(tmp);
8473 lower = NULL;
8474 }
8475 else {
8476 int res;
8477 res = obj2ast_expr(state, tmp, &lower, arena);
8478 if (res != 0) goto failed;
8479 Py_CLEAR(tmp);
8480 }
8481 if (_PyObject_LookupAttr(obj, state->upper, &tmp) < 0) {
8482 return 1;
8483 }
8484 if (tmp == NULL || tmp == Py_None) {
8485 Py_CLEAR(tmp);
8486 upper = NULL;
8487 }
8488 else {
8489 int res;
8490 res = obj2ast_expr(state, tmp, &upper, arena);
8491 if (res != 0) goto failed;
8492 Py_CLEAR(tmp);
8493 }
8494 if (_PyObject_LookupAttr(obj, state->step, &tmp) < 0) {
8495 return 1;
8496 }
8497 if (tmp == NULL || tmp == Py_None) {
8498 Py_CLEAR(tmp);
8499 step = NULL;
8500 }
8501 else {
8502 int res;
8503 res = obj2ast_expr(state, tmp, &step, arena);
8504 if (res != 0) goto failed;
8505 Py_CLEAR(tmp);
8506 }
8507 *out = Slice(lower, upper, step, lineno, col_offset, end_lineno,
8508 end_col_offset, arena);
8509 if (*out == NULL) goto failed;
8510 return 0;
8511 }
8512
8513 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
8514 failed:
8515 Py_XDECREF(tmp);
8516 return 1;
8517 }
8518
8519 int
obj2ast_expr_context(astmodulestate * state,PyObject * obj,expr_context_ty * out,PyArena * arena)8520 obj2ast_expr_context(astmodulestate *state, PyObject* obj, expr_context_ty*
8521 out, PyArena* arena)
8522 {
8523 int isinstance;
8524
8525 isinstance = PyObject_IsInstance(obj, state->Load_type);
8526 if (isinstance == -1) {
8527 return 1;
8528 }
8529 if (isinstance) {
8530 *out = Load;
8531 return 0;
8532 }
8533 isinstance = PyObject_IsInstance(obj, state->Store_type);
8534 if (isinstance == -1) {
8535 return 1;
8536 }
8537 if (isinstance) {
8538 *out = Store;
8539 return 0;
8540 }
8541 isinstance = PyObject_IsInstance(obj, state->Del_type);
8542 if (isinstance == -1) {
8543 return 1;
8544 }
8545 if (isinstance) {
8546 *out = Del;
8547 return 0;
8548 }
8549
8550 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
8551 return 1;
8552 }
8553
8554 int
obj2ast_boolop(astmodulestate * state,PyObject * obj,boolop_ty * out,PyArena * arena)8555 obj2ast_boolop(astmodulestate *state, PyObject* obj, boolop_ty* out, PyArena*
8556 arena)
8557 {
8558 int isinstance;
8559
8560 isinstance = PyObject_IsInstance(obj, state->And_type);
8561 if (isinstance == -1) {
8562 return 1;
8563 }
8564 if (isinstance) {
8565 *out = And;
8566 return 0;
8567 }
8568 isinstance = PyObject_IsInstance(obj, state->Or_type);
8569 if (isinstance == -1) {
8570 return 1;
8571 }
8572 if (isinstance) {
8573 *out = Or;
8574 return 0;
8575 }
8576
8577 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
8578 return 1;
8579 }
8580
8581 int
obj2ast_operator(astmodulestate * state,PyObject * obj,operator_ty * out,PyArena * arena)8582 obj2ast_operator(astmodulestate *state, PyObject* obj, operator_ty* out,
8583 PyArena* arena)
8584 {
8585 int isinstance;
8586
8587 isinstance = PyObject_IsInstance(obj, state->Add_type);
8588 if (isinstance == -1) {
8589 return 1;
8590 }
8591 if (isinstance) {
8592 *out = Add;
8593 return 0;
8594 }
8595 isinstance = PyObject_IsInstance(obj, state->Sub_type);
8596 if (isinstance == -1) {
8597 return 1;
8598 }
8599 if (isinstance) {
8600 *out = Sub;
8601 return 0;
8602 }
8603 isinstance = PyObject_IsInstance(obj, state->Mult_type);
8604 if (isinstance == -1) {
8605 return 1;
8606 }
8607 if (isinstance) {
8608 *out = Mult;
8609 return 0;
8610 }
8611 isinstance = PyObject_IsInstance(obj, state->MatMult_type);
8612 if (isinstance == -1) {
8613 return 1;
8614 }
8615 if (isinstance) {
8616 *out = MatMult;
8617 return 0;
8618 }
8619 isinstance = PyObject_IsInstance(obj, state->Div_type);
8620 if (isinstance == -1) {
8621 return 1;
8622 }
8623 if (isinstance) {
8624 *out = Div;
8625 return 0;
8626 }
8627 isinstance = PyObject_IsInstance(obj, state->Mod_type);
8628 if (isinstance == -1) {
8629 return 1;
8630 }
8631 if (isinstance) {
8632 *out = Mod;
8633 return 0;
8634 }
8635 isinstance = PyObject_IsInstance(obj, state->Pow_type);
8636 if (isinstance == -1) {
8637 return 1;
8638 }
8639 if (isinstance) {
8640 *out = Pow;
8641 return 0;
8642 }
8643 isinstance = PyObject_IsInstance(obj, state->LShift_type);
8644 if (isinstance == -1) {
8645 return 1;
8646 }
8647 if (isinstance) {
8648 *out = LShift;
8649 return 0;
8650 }
8651 isinstance = PyObject_IsInstance(obj, state->RShift_type);
8652 if (isinstance == -1) {
8653 return 1;
8654 }
8655 if (isinstance) {
8656 *out = RShift;
8657 return 0;
8658 }
8659 isinstance = PyObject_IsInstance(obj, state->BitOr_type);
8660 if (isinstance == -1) {
8661 return 1;
8662 }
8663 if (isinstance) {
8664 *out = BitOr;
8665 return 0;
8666 }
8667 isinstance = PyObject_IsInstance(obj, state->BitXor_type);
8668 if (isinstance == -1) {
8669 return 1;
8670 }
8671 if (isinstance) {
8672 *out = BitXor;
8673 return 0;
8674 }
8675 isinstance = PyObject_IsInstance(obj, state->BitAnd_type);
8676 if (isinstance == -1) {
8677 return 1;
8678 }
8679 if (isinstance) {
8680 *out = BitAnd;
8681 return 0;
8682 }
8683 isinstance = PyObject_IsInstance(obj, state->FloorDiv_type);
8684 if (isinstance == -1) {
8685 return 1;
8686 }
8687 if (isinstance) {
8688 *out = FloorDiv;
8689 return 0;
8690 }
8691
8692 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
8693 return 1;
8694 }
8695
8696 int
obj2ast_unaryop(astmodulestate * state,PyObject * obj,unaryop_ty * out,PyArena * arena)8697 obj2ast_unaryop(astmodulestate *state, PyObject* obj, unaryop_ty* out, PyArena*
8698 arena)
8699 {
8700 int isinstance;
8701
8702 isinstance = PyObject_IsInstance(obj, state->Invert_type);
8703 if (isinstance == -1) {
8704 return 1;
8705 }
8706 if (isinstance) {
8707 *out = Invert;
8708 return 0;
8709 }
8710 isinstance = PyObject_IsInstance(obj, state->Not_type);
8711 if (isinstance == -1) {
8712 return 1;
8713 }
8714 if (isinstance) {
8715 *out = Not;
8716 return 0;
8717 }
8718 isinstance = PyObject_IsInstance(obj, state->UAdd_type);
8719 if (isinstance == -1) {
8720 return 1;
8721 }
8722 if (isinstance) {
8723 *out = UAdd;
8724 return 0;
8725 }
8726 isinstance = PyObject_IsInstance(obj, state->USub_type);
8727 if (isinstance == -1) {
8728 return 1;
8729 }
8730 if (isinstance) {
8731 *out = USub;
8732 return 0;
8733 }
8734
8735 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
8736 return 1;
8737 }
8738
8739 int
obj2ast_cmpop(astmodulestate * state,PyObject * obj,cmpop_ty * out,PyArena * arena)8740 obj2ast_cmpop(astmodulestate *state, PyObject* obj, cmpop_ty* out, PyArena*
8741 arena)
8742 {
8743 int isinstance;
8744
8745 isinstance = PyObject_IsInstance(obj, state->Eq_type);
8746 if (isinstance == -1) {
8747 return 1;
8748 }
8749 if (isinstance) {
8750 *out = Eq;
8751 return 0;
8752 }
8753 isinstance = PyObject_IsInstance(obj, state->NotEq_type);
8754 if (isinstance == -1) {
8755 return 1;
8756 }
8757 if (isinstance) {
8758 *out = NotEq;
8759 return 0;
8760 }
8761 isinstance = PyObject_IsInstance(obj, state->Lt_type);
8762 if (isinstance == -1) {
8763 return 1;
8764 }
8765 if (isinstance) {
8766 *out = Lt;
8767 return 0;
8768 }
8769 isinstance = PyObject_IsInstance(obj, state->LtE_type);
8770 if (isinstance == -1) {
8771 return 1;
8772 }
8773 if (isinstance) {
8774 *out = LtE;
8775 return 0;
8776 }
8777 isinstance = PyObject_IsInstance(obj, state->Gt_type);
8778 if (isinstance == -1) {
8779 return 1;
8780 }
8781 if (isinstance) {
8782 *out = Gt;
8783 return 0;
8784 }
8785 isinstance = PyObject_IsInstance(obj, state->GtE_type);
8786 if (isinstance == -1) {
8787 return 1;
8788 }
8789 if (isinstance) {
8790 *out = GtE;
8791 return 0;
8792 }
8793 isinstance = PyObject_IsInstance(obj, state->Is_type);
8794 if (isinstance == -1) {
8795 return 1;
8796 }
8797 if (isinstance) {
8798 *out = Is;
8799 return 0;
8800 }
8801 isinstance = PyObject_IsInstance(obj, state->IsNot_type);
8802 if (isinstance == -1) {
8803 return 1;
8804 }
8805 if (isinstance) {
8806 *out = IsNot;
8807 return 0;
8808 }
8809 isinstance = PyObject_IsInstance(obj, state->In_type);
8810 if (isinstance == -1) {
8811 return 1;
8812 }
8813 if (isinstance) {
8814 *out = In;
8815 return 0;
8816 }
8817 isinstance = PyObject_IsInstance(obj, state->NotIn_type);
8818 if (isinstance == -1) {
8819 return 1;
8820 }
8821 if (isinstance) {
8822 *out = NotIn;
8823 return 0;
8824 }
8825
8826 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
8827 return 1;
8828 }
8829
8830 int
obj2ast_comprehension(astmodulestate * state,PyObject * obj,comprehension_ty * out,PyArena * arena)8831 obj2ast_comprehension(astmodulestate *state, PyObject* obj, comprehension_ty*
8832 out, PyArena* arena)
8833 {
8834 PyObject* tmp = NULL;
8835 expr_ty target;
8836 expr_ty iter;
8837 asdl_seq* ifs;
8838 int is_async;
8839
8840 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
8841 return 1;
8842 }
8843 if (tmp == NULL) {
8844 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
8845 return 1;
8846 }
8847 else {
8848 int res;
8849 res = obj2ast_expr(state, tmp, &target, arena);
8850 if (res != 0) goto failed;
8851 Py_CLEAR(tmp);
8852 }
8853 if (_PyObject_LookupAttr(obj, state->iter, &tmp) < 0) {
8854 return 1;
8855 }
8856 if (tmp == NULL) {
8857 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
8858 return 1;
8859 }
8860 else {
8861 int res;
8862 res = obj2ast_expr(state, tmp, &iter, arena);
8863 if (res != 0) goto failed;
8864 Py_CLEAR(tmp);
8865 }
8866 if (_PyObject_LookupAttr(obj, state->ifs, &tmp) < 0) {
8867 return 1;
8868 }
8869 if (tmp == NULL) {
8870 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
8871 return 1;
8872 }
8873 else {
8874 int res;
8875 Py_ssize_t len;
8876 Py_ssize_t i;
8877 if (!PyList_Check(tmp)) {
8878 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
8879 goto failed;
8880 }
8881 len = PyList_GET_SIZE(tmp);
8882 ifs = _Py_asdl_seq_new(len, arena);
8883 if (ifs == NULL) goto failed;
8884 for (i = 0; i < len; i++) {
8885 expr_ty val;
8886 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8887 Py_INCREF(tmp2);
8888 res = obj2ast_expr(state, tmp2, &val, arena);
8889 Py_DECREF(tmp2);
8890 if (res != 0) goto failed;
8891 if (len != PyList_GET_SIZE(tmp)) {
8892 PyErr_SetString(PyExc_RuntimeError, "comprehension field \"ifs\" changed size during iteration");
8893 goto failed;
8894 }
8895 asdl_seq_SET(ifs, i, val);
8896 }
8897 Py_CLEAR(tmp);
8898 }
8899 if (_PyObject_LookupAttr(obj, state->is_async, &tmp) < 0) {
8900 return 1;
8901 }
8902 if (tmp == NULL) {
8903 PyErr_SetString(PyExc_TypeError, "required field \"is_async\" missing from comprehension");
8904 return 1;
8905 }
8906 else {
8907 int res;
8908 res = obj2ast_int(state, tmp, &is_async, arena);
8909 if (res != 0) goto failed;
8910 Py_CLEAR(tmp);
8911 }
8912 *out = comprehension(target, iter, ifs, is_async, arena);
8913 return 0;
8914 failed:
8915 Py_XDECREF(tmp);
8916 return 1;
8917 }
8918
8919 int
obj2ast_excepthandler(astmodulestate * state,PyObject * obj,excepthandler_ty * out,PyArena * arena)8920 obj2ast_excepthandler(astmodulestate *state, PyObject* obj, excepthandler_ty*
8921 out, PyArena* arena)
8922 {
8923 int isinstance;
8924
8925 PyObject *tmp = NULL;
8926 PyObject *tp;
8927 int lineno;
8928 int col_offset;
8929 int end_lineno;
8930 int end_col_offset;
8931
8932 if (obj == Py_None) {
8933 *out = NULL;
8934 return 0;
8935 }
8936 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
8937 return 1;
8938 }
8939 if (tmp == NULL) {
8940 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
8941 return 1;
8942 }
8943 else {
8944 int res;
8945 res = obj2ast_int(state, tmp, &lineno, arena);
8946 if (res != 0) goto failed;
8947 Py_CLEAR(tmp);
8948 }
8949 if (_PyObject_LookupAttr(obj, state->col_offset, &tmp) < 0) {
8950 return 1;
8951 }
8952 if (tmp == NULL) {
8953 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
8954 return 1;
8955 }
8956 else {
8957 int res;
8958 res = obj2ast_int(state, tmp, &col_offset, arena);
8959 if (res != 0) goto failed;
8960 Py_CLEAR(tmp);
8961 }
8962 if (_PyObject_LookupAttr(obj, state->end_lineno, &tmp) < 0) {
8963 return 1;
8964 }
8965 if (tmp == NULL || tmp == Py_None) {
8966 Py_CLEAR(tmp);
8967 end_lineno = 0;
8968 }
8969 else {
8970 int res;
8971 res = obj2ast_int(state, tmp, &end_lineno, arena);
8972 if (res != 0) goto failed;
8973 Py_CLEAR(tmp);
8974 }
8975 if (_PyObject_LookupAttr(obj, state->end_col_offset, &tmp) < 0) {
8976 return 1;
8977 }
8978 if (tmp == NULL || tmp == Py_None) {
8979 Py_CLEAR(tmp);
8980 end_col_offset = 0;
8981 }
8982 else {
8983 int res;
8984 res = obj2ast_int(state, tmp, &end_col_offset, arena);
8985 if (res != 0) goto failed;
8986 Py_CLEAR(tmp);
8987 }
8988 tp = state->ExceptHandler_type;
8989 isinstance = PyObject_IsInstance(obj, tp);
8990 if (isinstance == -1) {
8991 return 1;
8992 }
8993 if (isinstance) {
8994 expr_ty type;
8995 identifier name;
8996 asdl_seq* body;
8997
8998 if (_PyObject_LookupAttr(obj, state->type, &tmp) < 0) {
8999 return 1;
9000 }
9001 if (tmp == NULL || tmp == Py_None) {
9002 Py_CLEAR(tmp);
9003 type = NULL;
9004 }
9005 else {
9006 int res;
9007 res = obj2ast_expr(state, tmp, &type, arena);
9008 if (res != 0) goto failed;
9009 Py_CLEAR(tmp);
9010 }
9011 if (_PyObject_LookupAttr(obj, state->name, &tmp) < 0) {
9012 return 1;
9013 }
9014 if (tmp == NULL || tmp == Py_None) {
9015 Py_CLEAR(tmp);
9016 name = NULL;
9017 }
9018 else {
9019 int res;
9020 res = obj2ast_identifier(state, tmp, &name, arena);
9021 if (res != 0) goto failed;
9022 Py_CLEAR(tmp);
9023 }
9024 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
9025 return 1;
9026 }
9027 if (tmp == NULL) {
9028 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
9029 return 1;
9030 }
9031 else {
9032 int res;
9033 Py_ssize_t len;
9034 Py_ssize_t i;
9035 if (!PyList_Check(tmp)) {
9036 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
9037 goto failed;
9038 }
9039 len = PyList_GET_SIZE(tmp);
9040 body = _Py_asdl_seq_new(len, arena);
9041 if (body == NULL) goto failed;
9042 for (i = 0; i < len; i++) {
9043 stmt_ty val;
9044 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9045 Py_INCREF(tmp2);
9046 res = obj2ast_stmt(state, tmp2, &val, arena);
9047 Py_DECREF(tmp2);
9048 if (res != 0) goto failed;
9049 if (len != PyList_GET_SIZE(tmp)) {
9050 PyErr_SetString(PyExc_RuntimeError, "ExceptHandler field \"body\" changed size during iteration");
9051 goto failed;
9052 }
9053 asdl_seq_SET(body, i, val);
9054 }
9055 Py_CLEAR(tmp);
9056 }
9057 *out = ExceptHandler(type, name, body, lineno, col_offset, end_lineno,
9058 end_col_offset, arena);
9059 if (*out == NULL) goto failed;
9060 return 0;
9061 }
9062
9063 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
9064 failed:
9065 Py_XDECREF(tmp);
9066 return 1;
9067 }
9068
9069 int
obj2ast_arguments(astmodulestate * state,PyObject * obj,arguments_ty * out,PyArena * arena)9070 obj2ast_arguments(astmodulestate *state, PyObject* obj, arguments_ty* out,
9071 PyArena* arena)
9072 {
9073 PyObject* tmp = NULL;
9074 asdl_seq* posonlyargs;
9075 asdl_seq* args;
9076 arg_ty vararg;
9077 asdl_seq* kwonlyargs;
9078 asdl_seq* kw_defaults;
9079 arg_ty kwarg;
9080 asdl_seq* defaults;
9081
9082 if (_PyObject_LookupAttr(obj, state->posonlyargs, &tmp) < 0) {
9083 return 1;
9084 }
9085 if (tmp == NULL) {
9086 PyErr_SetString(PyExc_TypeError, "required field \"posonlyargs\" missing from arguments");
9087 return 1;
9088 }
9089 else {
9090 int res;
9091 Py_ssize_t len;
9092 Py_ssize_t i;
9093 if (!PyList_Check(tmp)) {
9094 PyErr_Format(PyExc_TypeError, "arguments field \"posonlyargs\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
9095 goto failed;
9096 }
9097 len = PyList_GET_SIZE(tmp);
9098 posonlyargs = _Py_asdl_seq_new(len, arena);
9099 if (posonlyargs == NULL) goto failed;
9100 for (i = 0; i < len; i++) {
9101 arg_ty val;
9102 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9103 Py_INCREF(tmp2);
9104 res = obj2ast_arg(state, tmp2, &val, arena);
9105 Py_DECREF(tmp2);
9106 if (res != 0) goto failed;
9107 if (len != PyList_GET_SIZE(tmp)) {
9108 PyErr_SetString(PyExc_RuntimeError, "arguments field \"posonlyargs\" changed size during iteration");
9109 goto failed;
9110 }
9111 asdl_seq_SET(posonlyargs, i, val);
9112 }
9113 Py_CLEAR(tmp);
9114 }
9115 if (_PyObject_LookupAttr(obj, state->args, &tmp) < 0) {
9116 return 1;
9117 }
9118 if (tmp == NULL) {
9119 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
9120 return 1;
9121 }
9122 else {
9123 int res;
9124 Py_ssize_t len;
9125 Py_ssize_t i;
9126 if (!PyList_Check(tmp)) {
9127 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
9128 goto failed;
9129 }
9130 len = PyList_GET_SIZE(tmp);
9131 args = _Py_asdl_seq_new(len, arena);
9132 if (args == NULL) goto failed;
9133 for (i = 0; i < len; i++) {
9134 arg_ty val;
9135 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9136 Py_INCREF(tmp2);
9137 res = obj2ast_arg(state, tmp2, &val, arena);
9138 Py_DECREF(tmp2);
9139 if (res != 0) goto failed;
9140 if (len != PyList_GET_SIZE(tmp)) {
9141 PyErr_SetString(PyExc_RuntimeError, "arguments field \"args\" changed size during iteration");
9142 goto failed;
9143 }
9144 asdl_seq_SET(args, i, val);
9145 }
9146 Py_CLEAR(tmp);
9147 }
9148 if (_PyObject_LookupAttr(obj, state->vararg, &tmp) < 0) {
9149 return 1;
9150 }
9151 if (tmp == NULL || tmp == Py_None) {
9152 Py_CLEAR(tmp);
9153 vararg = NULL;
9154 }
9155 else {
9156 int res;
9157 res = obj2ast_arg(state, tmp, &vararg, arena);
9158 if (res != 0) goto failed;
9159 Py_CLEAR(tmp);
9160 }
9161 if (_PyObject_LookupAttr(obj, state->kwonlyargs, &tmp) < 0) {
9162 return 1;
9163 }
9164 if (tmp == NULL) {
9165 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
9166 return 1;
9167 }
9168 else {
9169 int res;
9170 Py_ssize_t len;
9171 Py_ssize_t i;
9172 if (!PyList_Check(tmp)) {
9173 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
9174 goto failed;
9175 }
9176 len = PyList_GET_SIZE(tmp);
9177 kwonlyargs = _Py_asdl_seq_new(len, arena);
9178 if (kwonlyargs == NULL) goto failed;
9179 for (i = 0; i < len; i++) {
9180 arg_ty val;
9181 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9182 Py_INCREF(tmp2);
9183 res = obj2ast_arg(state, tmp2, &val, arena);
9184 Py_DECREF(tmp2);
9185 if (res != 0) goto failed;
9186 if (len != PyList_GET_SIZE(tmp)) {
9187 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kwonlyargs\" changed size during iteration");
9188 goto failed;
9189 }
9190 asdl_seq_SET(kwonlyargs, i, val);
9191 }
9192 Py_CLEAR(tmp);
9193 }
9194 if (_PyObject_LookupAttr(obj, state->kw_defaults, &tmp) < 0) {
9195 return 1;
9196 }
9197 if (tmp == NULL) {
9198 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
9199 return 1;
9200 }
9201 else {
9202 int res;
9203 Py_ssize_t len;
9204 Py_ssize_t i;
9205 if (!PyList_Check(tmp)) {
9206 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
9207 goto failed;
9208 }
9209 len = PyList_GET_SIZE(tmp);
9210 kw_defaults = _Py_asdl_seq_new(len, arena);
9211 if (kw_defaults == NULL) goto failed;
9212 for (i = 0; i < len; i++) {
9213 expr_ty val;
9214 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9215 Py_INCREF(tmp2);
9216 res = obj2ast_expr(state, tmp2, &val, arena);
9217 Py_DECREF(tmp2);
9218 if (res != 0) goto failed;
9219 if (len != PyList_GET_SIZE(tmp)) {
9220 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kw_defaults\" changed size during iteration");
9221 goto failed;
9222 }
9223 asdl_seq_SET(kw_defaults, i, val);
9224 }
9225 Py_CLEAR(tmp);
9226 }
9227 if (_PyObject_LookupAttr(obj, state->kwarg, &tmp) < 0) {
9228 return 1;
9229 }
9230 if (tmp == NULL || tmp == Py_None) {
9231 Py_CLEAR(tmp);
9232 kwarg = NULL;
9233 }
9234 else {
9235 int res;
9236 res = obj2ast_arg(state, tmp, &kwarg, arena);
9237 if (res != 0) goto failed;
9238 Py_CLEAR(tmp);
9239 }
9240 if (_PyObject_LookupAttr(obj, state->defaults, &tmp) < 0) {
9241 return 1;
9242 }
9243 if (tmp == NULL) {
9244 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
9245 return 1;
9246 }
9247 else {
9248 int res;
9249 Py_ssize_t len;
9250 Py_ssize_t i;
9251 if (!PyList_Check(tmp)) {
9252 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
9253 goto failed;
9254 }
9255 len = PyList_GET_SIZE(tmp);
9256 defaults = _Py_asdl_seq_new(len, arena);
9257 if (defaults == NULL) goto failed;
9258 for (i = 0; i < len; i++) {
9259 expr_ty val;
9260 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9261 Py_INCREF(tmp2);
9262 res = obj2ast_expr(state, tmp2, &val, arena);
9263 Py_DECREF(tmp2);
9264 if (res != 0) goto failed;
9265 if (len != PyList_GET_SIZE(tmp)) {
9266 PyErr_SetString(PyExc_RuntimeError, "arguments field \"defaults\" changed size during iteration");
9267 goto failed;
9268 }
9269 asdl_seq_SET(defaults, i, val);
9270 }
9271 Py_CLEAR(tmp);
9272 }
9273 *out = arguments(posonlyargs, args, vararg, kwonlyargs, kw_defaults, kwarg,
9274 defaults, arena);
9275 return 0;
9276 failed:
9277 Py_XDECREF(tmp);
9278 return 1;
9279 }
9280
9281 int
obj2ast_arg(astmodulestate * state,PyObject * obj,arg_ty * out,PyArena * arena)9282 obj2ast_arg(astmodulestate *state, PyObject* obj, arg_ty* out, PyArena* arena)
9283 {
9284 PyObject* tmp = NULL;
9285 identifier arg;
9286 expr_ty annotation;
9287 string type_comment;
9288 int lineno;
9289 int col_offset;
9290 int end_lineno;
9291 int end_col_offset;
9292
9293 if (_PyObject_LookupAttr(obj, state->arg, &tmp) < 0) {
9294 return 1;
9295 }
9296 if (tmp == NULL) {
9297 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
9298 return 1;
9299 }
9300 else {
9301 int res;
9302 res = obj2ast_identifier(state, tmp, &arg, arena);
9303 if (res != 0) goto failed;
9304 Py_CLEAR(tmp);
9305 }
9306 if (_PyObject_LookupAttr(obj, state->annotation, &tmp) < 0) {
9307 return 1;
9308 }
9309 if (tmp == NULL || tmp == Py_None) {
9310 Py_CLEAR(tmp);
9311 annotation = NULL;
9312 }
9313 else {
9314 int res;
9315 res = obj2ast_expr(state, tmp, &annotation, arena);
9316 if (res != 0) goto failed;
9317 Py_CLEAR(tmp);
9318 }
9319 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
9320 return 1;
9321 }
9322 if (tmp == NULL || tmp == Py_None) {
9323 Py_CLEAR(tmp);
9324 type_comment = NULL;
9325 }
9326 else {
9327 int res;
9328 res = obj2ast_string(state, tmp, &type_comment, arena);
9329 if (res != 0) goto failed;
9330 Py_CLEAR(tmp);
9331 }
9332 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
9333 return 1;
9334 }
9335 if (tmp == NULL) {
9336 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
9337 return 1;
9338 }
9339 else {
9340 int res;
9341 res = obj2ast_int(state, tmp, &lineno, arena);
9342 if (res != 0) goto failed;
9343 Py_CLEAR(tmp);
9344 }
9345 if (_PyObject_LookupAttr(obj, state->col_offset, &tmp) < 0) {
9346 return 1;
9347 }
9348 if (tmp == NULL) {
9349 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
9350 return 1;
9351 }
9352 else {
9353 int res;
9354 res = obj2ast_int(state, tmp, &col_offset, arena);
9355 if (res != 0) goto failed;
9356 Py_CLEAR(tmp);
9357 }
9358 if (_PyObject_LookupAttr(obj, state->end_lineno, &tmp) < 0) {
9359 return 1;
9360 }
9361 if (tmp == NULL || tmp == Py_None) {
9362 Py_CLEAR(tmp);
9363 end_lineno = 0;
9364 }
9365 else {
9366 int res;
9367 res = obj2ast_int(state, tmp, &end_lineno, arena);
9368 if (res != 0) goto failed;
9369 Py_CLEAR(tmp);
9370 }
9371 if (_PyObject_LookupAttr(obj, state->end_col_offset, &tmp) < 0) {
9372 return 1;
9373 }
9374 if (tmp == NULL || tmp == Py_None) {
9375 Py_CLEAR(tmp);
9376 end_col_offset = 0;
9377 }
9378 else {
9379 int res;
9380 res = obj2ast_int(state, tmp, &end_col_offset, arena);
9381 if (res != 0) goto failed;
9382 Py_CLEAR(tmp);
9383 }
9384 *out = arg(arg, annotation, type_comment, lineno, col_offset, end_lineno,
9385 end_col_offset, arena);
9386 return 0;
9387 failed:
9388 Py_XDECREF(tmp);
9389 return 1;
9390 }
9391
9392 int
obj2ast_keyword(astmodulestate * state,PyObject * obj,keyword_ty * out,PyArena * arena)9393 obj2ast_keyword(astmodulestate *state, PyObject* obj, keyword_ty* out, PyArena*
9394 arena)
9395 {
9396 PyObject* tmp = NULL;
9397 identifier arg;
9398 expr_ty value;
9399 int lineno;
9400 int col_offset;
9401 int end_lineno;
9402 int end_col_offset;
9403
9404 if (_PyObject_LookupAttr(obj, state->arg, &tmp) < 0) {
9405 return 1;
9406 }
9407 if (tmp == NULL || tmp == Py_None) {
9408 Py_CLEAR(tmp);
9409 arg = NULL;
9410 }
9411 else {
9412 int res;
9413 res = obj2ast_identifier(state, tmp, &arg, arena);
9414 if (res != 0) goto failed;
9415 Py_CLEAR(tmp);
9416 }
9417 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
9418 return 1;
9419 }
9420 if (tmp == NULL) {
9421 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
9422 return 1;
9423 }
9424 else {
9425 int res;
9426 res = obj2ast_expr(state, tmp, &value, arena);
9427 if (res != 0) goto failed;
9428 Py_CLEAR(tmp);
9429 }
9430 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
9431 return 1;
9432 }
9433 if (tmp == NULL) {
9434 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from keyword");
9435 return 1;
9436 }
9437 else {
9438 int res;
9439 res = obj2ast_int(state, tmp, &lineno, arena);
9440 if (res != 0) goto failed;
9441 Py_CLEAR(tmp);
9442 }
9443 if (_PyObject_LookupAttr(obj, state->col_offset, &tmp) < 0) {
9444 return 1;
9445 }
9446 if (tmp == NULL) {
9447 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from keyword");
9448 return 1;
9449 }
9450 else {
9451 int res;
9452 res = obj2ast_int(state, tmp, &col_offset, arena);
9453 if (res != 0) goto failed;
9454 Py_CLEAR(tmp);
9455 }
9456 if (_PyObject_LookupAttr(obj, state->end_lineno, &tmp) < 0) {
9457 return 1;
9458 }
9459 if (tmp == NULL || tmp == Py_None) {
9460 Py_CLEAR(tmp);
9461 end_lineno = 0;
9462 }
9463 else {
9464 int res;
9465 res = obj2ast_int(state, tmp, &end_lineno, arena);
9466 if (res != 0) goto failed;
9467 Py_CLEAR(tmp);
9468 }
9469 if (_PyObject_LookupAttr(obj, state->end_col_offset, &tmp) < 0) {
9470 return 1;
9471 }
9472 if (tmp == NULL || tmp == Py_None) {
9473 Py_CLEAR(tmp);
9474 end_col_offset = 0;
9475 }
9476 else {
9477 int res;
9478 res = obj2ast_int(state, tmp, &end_col_offset, arena);
9479 if (res != 0) goto failed;
9480 Py_CLEAR(tmp);
9481 }
9482 *out = keyword(arg, value, lineno, col_offset, end_lineno, end_col_offset,
9483 arena);
9484 return 0;
9485 failed:
9486 Py_XDECREF(tmp);
9487 return 1;
9488 }
9489
9490 int
obj2ast_alias(astmodulestate * state,PyObject * obj,alias_ty * out,PyArena * arena)9491 obj2ast_alias(astmodulestate *state, PyObject* obj, alias_ty* out, PyArena*
9492 arena)
9493 {
9494 PyObject* tmp = NULL;
9495 identifier name;
9496 identifier asname;
9497
9498 if (_PyObject_LookupAttr(obj, state->name, &tmp) < 0) {
9499 return 1;
9500 }
9501 if (tmp == NULL) {
9502 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
9503 return 1;
9504 }
9505 else {
9506 int res;
9507 res = obj2ast_identifier(state, tmp, &name, arena);
9508 if (res != 0) goto failed;
9509 Py_CLEAR(tmp);
9510 }
9511 if (_PyObject_LookupAttr(obj, state->asname, &tmp) < 0) {
9512 return 1;
9513 }
9514 if (tmp == NULL || tmp == Py_None) {
9515 Py_CLEAR(tmp);
9516 asname = NULL;
9517 }
9518 else {
9519 int res;
9520 res = obj2ast_identifier(state, tmp, &asname, arena);
9521 if (res != 0) goto failed;
9522 Py_CLEAR(tmp);
9523 }
9524 *out = alias(name, asname, arena);
9525 return 0;
9526 failed:
9527 Py_XDECREF(tmp);
9528 return 1;
9529 }
9530
9531 int
obj2ast_withitem(astmodulestate * state,PyObject * obj,withitem_ty * out,PyArena * arena)9532 obj2ast_withitem(astmodulestate *state, PyObject* obj, withitem_ty* out,
9533 PyArena* arena)
9534 {
9535 PyObject* tmp = NULL;
9536 expr_ty context_expr;
9537 expr_ty optional_vars;
9538
9539 if (_PyObject_LookupAttr(obj, state->context_expr, &tmp) < 0) {
9540 return 1;
9541 }
9542 if (tmp == NULL) {
9543 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
9544 return 1;
9545 }
9546 else {
9547 int res;
9548 res = obj2ast_expr(state, tmp, &context_expr, arena);
9549 if (res != 0) goto failed;
9550 Py_CLEAR(tmp);
9551 }
9552 if (_PyObject_LookupAttr(obj, state->optional_vars, &tmp) < 0) {
9553 return 1;
9554 }
9555 if (tmp == NULL || tmp == Py_None) {
9556 Py_CLEAR(tmp);
9557 optional_vars = NULL;
9558 }
9559 else {
9560 int res;
9561 res = obj2ast_expr(state, tmp, &optional_vars, arena);
9562 if (res != 0) goto failed;
9563 Py_CLEAR(tmp);
9564 }
9565 *out = withitem(context_expr, optional_vars, arena);
9566 return 0;
9567 failed:
9568 Py_XDECREF(tmp);
9569 return 1;
9570 }
9571
9572 int
obj2ast_type_ignore(astmodulestate * state,PyObject * obj,type_ignore_ty * out,PyArena * arena)9573 obj2ast_type_ignore(astmodulestate *state, PyObject* obj, type_ignore_ty* out,
9574 PyArena* arena)
9575 {
9576 int isinstance;
9577
9578 PyObject *tmp = NULL;
9579 PyObject *tp;
9580
9581 if (obj == Py_None) {
9582 *out = NULL;
9583 return 0;
9584 }
9585 tp = state->TypeIgnore_type;
9586 isinstance = PyObject_IsInstance(obj, tp);
9587 if (isinstance == -1) {
9588 return 1;
9589 }
9590 if (isinstance) {
9591 int lineno;
9592 string tag;
9593
9594 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
9595 return 1;
9596 }
9597 if (tmp == NULL) {
9598 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from TypeIgnore");
9599 return 1;
9600 }
9601 else {
9602 int res;
9603 res = obj2ast_int(state, tmp, &lineno, arena);
9604 if (res != 0) goto failed;
9605 Py_CLEAR(tmp);
9606 }
9607 if (_PyObject_LookupAttr(obj, state->tag, &tmp) < 0) {
9608 return 1;
9609 }
9610 if (tmp == NULL) {
9611 PyErr_SetString(PyExc_TypeError, "required field \"tag\" missing from TypeIgnore");
9612 return 1;
9613 }
9614 else {
9615 int res;
9616 res = obj2ast_string(state, tmp, &tag, arena);
9617 if (res != 0) goto failed;
9618 Py_CLEAR(tmp);
9619 }
9620 *out = TypeIgnore(lineno, tag, arena);
9621 if (*out == NULL) goto failed;
9622 return 0;
9623 }
9624
9625 PyErr_Format(PyExc_TypeError, "expected some sort of type_ignore, but got %R", obj);
9626 failed:
9627 Py_XDECREF(tmp);
9628 return 1;
9629 }
9630
9631
9632 static int
astmodule_exec(PyObject * m)9633 astmodule_exec(PyObject *m)
9634 {
9635 astmodulestate *state = get_ast_state(m);
9636
9637 if (!init_types(state)) {
9638 return -1;
9639 }
9640 if (PyModule_AddObject(m, "AST", state->AST_type) < 0) {
9641 return -1;
9642 }
9643 Py_INCREF(state->AST_type);
9644 if (PyModule_AddIntMacro(m, PyCF_ALLOW_TOP_LEVEL_AWAIT) < 0) {
9645 return -1;
9646 }
9647 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0) {
9648 return -1;
9649 }
9650 if (PyModule_AddIntMacro(m, PyCF_TYPE_COMMENTS) < 0) {
9651 return -1;
9652 }
9653 if (PyModule_AddObject(m, "mod", state->mod_type) < 0) {
9654 return -1;
9655 }
9656 Py_INCREF(state->mod_type);
9657 if (PyModule_AddObject(m, "Module", state->Module_type) < 0) {
9658 return -1;
9659 }
9660 Py_INCREF(state->Module_type);
9661 if (PyModule_AddObject(m, "Interactive", state->Interactive_type) < 0) {
9662 return -1;
9663 }
9664 Py_INCREF(state->Interactive_type);
9665 if (PyModule_AddObject(m, "Expression", state->Expression_type) < 0) {
9666 return -1;
9667 }
9668 Py_INCREF(state->Expression_type);
9669 if (PyModule_AddObject(m, "FunctionType", state->FunctionType_type) < 0) {
9670 return -1;
9671 }
9672 Py_INCREF(state->FunctionType_type);
9673 if (PyModule_AddObject(m, "stmt", state->stmt_type) < 0) {
9674 return -1;
9675 }
9676 Py_INCREF(state->stmt_type);
9677 if (PyModule_AddObject(m, "FunctionDef", state->FunctionDef_type) < 0) {
9678 return -1;
9679 }
9680 Py_INCREF(state->FunctionDef_type);
9681 if (PyModule_AddObject(m, "AsyncFunctionDef", state->AsyncFunctionDef_type)
9682 < 0) {
9683 return -1;
9684 }
9685 Py_INCREF(state->AsyncFunctionDef_type);
9686 if (PyModule_AddObject(m, "ClassDef", state->ClassDef_type) < 0) {
9687 return -1;
9688 }
9689 Py_INCREF(state->ClassDef_type);
9690 if (PyModule_AddObject(m, "Return", state->Return_type) < 0) {
9691 return -1;
9692 }
9693 Py_INCREF(state->Return_type);
9694 if (PyModule_AddObject(m, "Delete", state->Delete_type) < 0) {
9695 return -1;
9696 }
9697 Py_INCREF(state->Delete_type);
9698 if (PyModule_AddObject(m, "Assign", state->Assign_type) < 0) {
9699 return -1;
9700 }
9701 Py_INCREF(state->Assign_type);
9702 if (PyModule_AddObject(m, "AugAssign", state->AugAssign_type) < 0) {
9703 return -1;
9704 }
9705 Py_INCREF(state->AugAssign_type);
9706 if (PyModule_AddObject(m, "AnnAssign", state->AnnAssign_type) < 0) {
9707 return -1;
9708 }
9709 Py_INCREF(state->AnnAssign_type);
9710 if (PyModule_AddObject(m, "For", state->For_type) < 0) {
9711 return -1;
9712 }
9713 Py_INCREF(state->For_type);
9714 if (PyModule_AddObject(m, "AsyncFor", state->AsyncFor_type) < 0) {
9715 return -1;
9716 }
9717 Py_INCREF(state->AsyncFor_type);
9718 if (PyModule_AddObject(m, "While", state->While_type) < 0) {
9719 return -1;
9720 }
9721 Py_INCREF(state->While_type);
9722 if (PyModule_AddObject(m, "If", state->If_type) < 0) {
9723 return -1;
9724 }
9725 Py_INCREF(state->If_type);
9726 if (PyModule_AddObject(m, "With", state->With_type) < 0) {
9727 return -1;
9728 }
9729 Py_INCREF(state->With_type);
9730 if (PyModule_AddObject(m, "AsyncWith", state->AsyncWith_type) < 0) {
9731 return -1;
9732 }
9733 Py_INCREF(state->AsyncWith_type);
9734 if (PyModule_AddObject(m, "Raise", state->Raise_type) < 0) {
9735 return -1;
9736 }
9737 Py_INCREF(state->Raise_type);
9738 if (PyModule_AddObject(m, "Try", state->Try_type) < 0) {
9739 return -1;
9740 }
9741 Py_INCREF(state->Try_type);
9742 if (PyModule_AddObject(m, "Assert", state->Assert_type) < 0) {
9743 return -1;
9744 }
9745 Py_INCREF(state->Assert_type);
9746 if (PyModule_AddObject(m, "Import", state->Import_type) < 0) {
9747 return -1;
9748 }
9749 Py_INCREF(state->Import_type);
9750 if (PyModule_AddObject(m, "ImportFrom", state->ImportFrom_type) < 0) {
9751 return -1;
9752 }
9753 Py_INCREF(state->ImportFrom_type);
9754 if (PyModule_AddObject(m, "Global", state->Global_type) < 0) {
9755 return -1;
9756 }
9757 Py_INCREF(state->Global_type);
9758 if (PyModule_AddObject(m, "Nonlocal", state->Nonlocal_type) < 0) {
9759 return -1;
9760 }
9761 Py_INCREF(state->Nonlocal_type);
9762 if (PyModule_AddObject(m, "Expr", state->Expr_type) < 0) {
9763 return -1;
9764 }
9765 Py_INCREF(state->Expr_type);
9766 if (PyModule_AddObject(m, "Pass", state->Pass_type) < 0) {
9767 return -1;
9768 }
9769 Py_INCREF(state->Pass_type);
9770 if (PyModule_AddObject(m, "Break", state->Break_type) < 0) {
9771 return -1;
9772 }
9773 Py_INCREF(state->Break_type);
9774 if (PyModule_AddObject(m, "Continue", state->Continue_type) < 0) {
9775 return -1;
9776 }
9777 Py_INCREF(state->Continue_type);
9778 if (PyModule_AddObject(m, "expr", state->expr_type) < 0) {
9779 return -1;
9780 }
9781 Py_INCREF(state->expr_type);
9782 if (PyModule_AddObject(m, "BoolOp", state->BoolOp_type) < 0) {
9783 return -1;
9784 }
9785 Py_INCREF(state->BoolOp_type);
9786 if (PyModule_AddObject(m, "NamedExpr", state->NamedExpr_type) < 0) {
9787 return -1;
9788 }
9789 Py_INCREF(state->NamedExpr_type);
9790 if (PyModule_AddObject(m, "BinOp", state->BinOp_type) < 0) {
9791 return -1;
9792 }
9793 Py_INCREF(state->BinOp_type);
9794 if (PyModule_AddObject(m, "UnaryOp", state->UnaryOp_type) < 0) {
9795 return -1;
9796 }
9797 Py_INCREF(state->UnaryOp_type);
9798 if (PyModule_AddObject(m, "Lambda", state->Lambda_type) < 0) {
9799 return -1;
9800 }
9801 Py_INCREF(state->Lambda_type);
9802 if (PyModule_AddObject(m, "IfExp", state->IfExp_type) < 0) {
9803 return -1;
9804 }
9805 Py_INCREF(state->IfExp_type);
9806 if (PyModule_AddObject(m, "Dict", state->Dict_type) < 0) {
9807 return -1;
9808 }
9809 Py_INCREF(state->Dict_type);
9810 if (PyModule_AddObject(m, "Set", state->Set_type) < 0) {
9811 return -1;
9812 }
9813 Py_INCREF(state->Set_type);
9814 if (PyModule_AddObject(m, "ListComp", state->ListComp_type) < 0) {
9815 return -1;
9816 }
9817 Py_INCREF(state->ListComp_type);
9818 if (PyModule_AddObject(m, "SetComp", state->SetComp_type) < 0) {
9819 return -1;
9820 }
9821 Py_INCREF(state->SetComp_type);
9822 if (PyModule_AddObject(m, "DictComp", state->DictComp_type) < 0) {
9823 return -1;
9824 }
9825 Py_INCREF(state->DictComp_type);
9826 if (PyModule_AddObject(m, "GeneratorExp", state->GeneratorExp_type) < 0) {
9827 return -1;
9828 }
9829 Py_INCREF(state->GeneratorExp_type);
9830 if (PyModule_AddObject(m, "Await", state->Await_type) < 0) {
9831 return -1;
9832 }
9833 Py_INCREF(state->Await_type);
9834 if (PyModule_AddObject(m, "Yield", state->Yield_type) < 0) {
9835 return -1;
9836 }
9837 Py_INCREF(state->Yield_type);
9838 if (PyModule_AddObject(m, "YieldFrom", state->YieldFrom_type) < 0) {
9839 return -1;
9840 }
9841 Py_INCREF(state->YieldFrom_type);
9842 if (PyModule_AddObject(m, "Compare", state->Compare_type) < 0) {
9843 return -1;
9844 }
9845 Py_INCREF(state->Compare_type);
9846 if (PyModule_AddObject(m, "Call", state->Call_type) < 0) {
9847 return -1;
9848 }
9849 Py_INCREF(state->Call_type);
9850 if (PyModule_AddObject(m, "FormattedValue", state->FormattedValue_type) <
9851 0) {
9852 return -1;
9853 }
9854 Py_INCREF(state->FormattedValue_type);
9855 if (PyModule_AddObject(m, "JoinedStr", state->JoinedStr_type) < 0) {
9856 return -1;
9857 }
9858 Py_INCREF(state->JoinedStr_type);
9859 if (PyModule_AddObject(m, "Constant", state->Constant_type) < 0) {
9860 return -1;
9861 }
9862 Py_INCREF(state->Constant_type);
9863 if (PyModule_AddObject(m, "Attribute", state->Attribute_type) < 0) {
9864 return -1;
9865 }
9866 Py_INCREF(state->Attribute_type);
9867 if (PyModule_AddObject(m, "Subscript", state->Subscript_type) < 0) {
9868 return -1;
9869 }
9870 Py_INCREF(state->Subscript_type);
9871 if (PyModule_AddObject(m, "Starred", state->Starred_type) < 0) {
9872 return -1;
9873 }
9874 Py_INCREF(state->Starred_type);
9875 if (PyModule_AddObject(m, "Name", state->Name_type) < 0) {
9876 return -1;
9877 }
9878 Py_INCREF(state->Name_type);
9879 if (PyModule_AddObject(m, "List", state->List_type) < 0) {
9880 return -1;
9881 }
9882 Py_INCREF(state->List_type);
9883 if (PyModule_AddObject(m, "Tuple", state->Tuple_type) < 0) {
9884 return -1;
9885 }
9886 Py_INCREF(state->Tuple_type);
9887 if (PyModule_AddObject(m, "Slice", state->Slice_type) < 0) {
9888 return -1;
9889 }
9890 Py_INCREF(state->Slice_type);
9891 if (PyModule_AddObject(m, "expr_context", state->expr_context_type) < 0) {
9892 return -1;
9893 }
9894 Py_INCREF(state->expr_context_type);
9895 if (PyModule_AddObject(m, "Load", state->Load_type) < 0) {
9896 return -1;
9897 }
9898 Py_INCREF(state->Load_type);
9899 if (PyModule_AddObject(m, "Store", state->Store_type) < 0) {
9900 return -1;
9901 }
9902 Py_INCREF(state->Store_type);
9903 if (PyModule_AddObject(m, "Del", state->Del_type) < 0) {
9904 return -1;
9905 }
9906 Py_INCREF(state->Del_type);
9907 if (PyModule_AddObject(m, "boolop", state->boolop_type) < 0) {
9908 return -1;
9909 }
9910 Py_INCREF(state->boolop_type);
9911 if (PyModule_AddObject(m, "And", state->And_type) < 0) {
9912 return -1;
9913 }
9914 Py_INCREF(state->And_type);
9915 if (PyModule_AddObject(m, "Or", state->Or_type) < 0) {
9916 return -1;
9917 }
9918 Py_INCREF(state->Or_type);
9919 if (PyModule_AddObject(m, "operator", state->operator_type) < 0) {
9920 return -1;
9921 }
9922 Py_INCREF(state->operator_type);
9923 if (PyModule_AddObject(m, "Add", state->Add_type) < 0) {
9924 return -1;
9925 }
9926 Py_INCREF(state->Add_type);
9927 if (PyModule_AddObject(m, "Sub", state->Sub_type) < 0) {
9928 return -1;
9929 }
9930 Py_INCREF(state->Sub_type);
9931 if (PyModule_AddObject(m, "Mult", state->Mult_type) < 0) {
9932 return -1;
9933 }
9934 Py_INCREF(state->Mult_type);
9935 if (PyModule_AddObject(m, "MatMult", state->MatMult_type) < 0) {
9936 return -1;
9937 }
9938 Py_INCREF(state->MatMult_type);
9939 if (PyModule_AddObject(m, "Div", state->Div_type) < 0) {
9940 return -1;
9941 }
9942 Py_INCREF(state->Div_type);
9943 if (PyModule_AddObject(m, "Mod", state->Mod_type) < 0) {
9944 return -1;
9945 }
9946 Py_INCREF(state->Mod_type);
9947 if (PyModule_AddObject(m, "Pow", state->Pow_type) < 0) {
9948 return -1;
9949 }
9950 Py_INCREF(state->Pow_type);
9951 if (PyModule_AddObject(m, "LShift", state->LShift_type) < 0) {
9952 return -1;
9953 }
9954 Py_INCREF(state->LShift_type);
9955 if (PyModule_AddObject(m, "RShift", state->RShift_type) < 0) {
9956 return -1;
9957 }
9958 Py_INCREF(state->RShift_type);
9959 if (PyModule_AddObject(m, "BitOr", state->BitOr_type) < 0) {
9960 return -1;
9961 }
9962 Py_INCREF(state->BitOr_type);
9963 if (PyModule_AddObject(m, "BitXor", state->BitXor_type) < 0) {
9964 return -1;
9965 }
9966 Py_INCREF(state->BitXor_type);
9967 if (PyModule_AddObject(m, "BitAnd", state->BitAnd_type) < 0) {
9968 return -1;
9969 }
9970 Py_INCREF(state->BitAnd_type);
9971 if (PyModule_AddObject(m, "FloorDiv", state->FloorDiv_type) < 0) {
9972 return -1;
9973 }
9974 Py_INCREF(state->FloorDiv_type);
9975 if (PyModule_AddObject(m, "unaryop", state->unaryop_type) < 0) {
9976 return -1;
9977 }
9978 Py_INCREF(state->unaryop_type);
9979 if (PyModule_AddObject(m, "Invert", state->Invert_type) < 0) {
9980 return -1;
9981 }
9982 Py_INCREF(state->Invert_type);
9983 if (PyModule_AddObject(m, "Not", state->Not_type) < 0) {
9984 return -1;
9985 }
9986 Py_INCREF(state->Not_type);
9987 if (PyModule_AddObject(m, "UAdd", state->UAdd_type) < 0) {
9988 return -1;
9989 }
9990 Py_INCREF(state->UAdd_type);
9991 if (PyModule_AddObject(m, "USub", state->USub_type) < 0) {
9992 return -1;
9993 }
9994 Py_INCREF(state->USub_type);
9995 if (PyModule_AddObject(m, "cmpop", state->cmpop_type) < 0) {
9996 return -1;
9997 }
9998 Py_INCREF(state->cmpop_type);
9999 if (PyModule_AddObject(m, "Eq", state->Eq_type) < 0) {
10000 return -1;
10001 }
10002 Py_INCREF(state->Eq_type);
10003 if (PyModule_AddObject(m, "NotEq", state->NotEq_type) < 0) {
10004 return -1;
10005 }
10006 Py_INCREF(state->NotEq_type);
10007 if (PyModule_AddObject(m, "Lt", state->Lt_type) < 0) {
10008 return -1;
10009 }
10010 Py_INCREF(state->Lt_type);
10011 if (PyModule_AddObject(m, "LtE", state->LtE_type) < 0) {
10012 return -1;
10013 }
10014 Py_INCREF(state->LtE_type);
10015 if (PyModule_AddObject(m, "Gt", state->Gt_type) < 0) {
10016 return -1;
10017 }
10018 Py_INCREF(state->Gt_type);
10019 if (PyModule_AddObject(m, "GtE", state->GtE_type) < 0) {
10020 return -1;
10021 }
10022 Py_INCREF(state->GtE_type);
10023 if (PyModule_AddObject(m, "Is", state->Is_type) < 0) {
10024 return -1;
10025 }
10026 Py_INCREF(state->Is_type);
10027 if (PyModule_AddObject(m, "IsNot", state->IsNot_type) < 0) {
10028 return -1;
10029 }
10030 Py_INCREF(state->IsNot_type);
10031 if (PyModule_AddObject(m, "In", state->In_type) < 0) {
10032 return -1;
10033 }
10034 Py_INCREF(state->In_type);
10035 if (PyModule_AddObject(m, "NotIn", state->NotIn_type) < 0) {
10036 return -1;
10037 }
10038 Py_INCREF(state->NotIn_type);
10039 if (PyModule_AddObject(m, "comprehension", state->comprehension_type) < 0) {
10040 return -1;
10041 }
10042 Py_INCREF(state->comprehension_type);
10043 if (PyModule_AddObject(m, "excepthandler", state->excepthandler_type) < 0) {
10044 return -1;
10045 }
10046 Py_INCREF(state->excepthandler_type);
10047 if (PyModule_AddObject(m, "ExceptHandler", state->ExceptHandler_type) < 0) {
10048 return -1;
10049 }
10050 Py_INCREF(state->ExceptHandler_type);
10051 if (PyModule_AddObject(m, "arguments", state->arguments_type) < 0) {
10052 return -1;
10053 }
10054 Py_INCREF(state->arguments_type);
10055 if (PyModule_AddObject(m, "arg", state->arg_type) < 0) {
10056 return -1;
10057 }
10058 Py_INCREF(state->arg_type);
10059 if (PyModule_AddObject(m, "keyword", state->keyword_type) < 0) {
10060 return -1;
10061 }
10062 Py_INCREF(state->keyword_type);
10063 if (PyModule_AddObject(m, "alias", state->alias_type) < 0) {
10064 return -1;
10065 }
10066 Py_INCREF(state->alias_type);
10067 if (PyModule_AddObject(m, "withitem", state->withitem_type) < 0) {
10068 return -1;
10069 }
10070 Py_INCREF(state->withitem_type);
10071 if (PyModule_AddObject(m, "type_ignore", state->type_ignore_type) < 0) {
10072 return -1;
10073 }
10074 Py_INCREF(state->type_ignore_type);
10075 if (PyModule_AddObject(m, "TypeIgnore", state->TypeIgnore_type) < 0) {
10076 return -1;
10077 }
10078 Py_INCREF(state->TypeIgnore_type);
10079 return 0;
10080 }
10081
10082 static PyModuleDef_Slot astmodule_slots[] = {
10083 {Py_mod_exec, astmodule_exec},
10084 {0, NULL}
10085 };
10086
10087 static struct PyModuleDef _astmodule = {
10088 PyModuleDef_HEAD_INIT,
10089 .m_name = "_ast",
10090 // The _ast module uses a global state (global_ast_state).
10091 .m_size = 0,
10092 .m_slots = astmodule_slots,
10093 };
10094
10095 PyMODINIT_FUNC
PyInit__ast(void)10096 PyInit__ast(void)
10097 {
10098 return PyModuleDef_Init(&_astmodule);
10099 }
10100
10101
PyAST_mod2obj(mod_ty t)10102 PyObject* PyAST_mod2obj(mod_ty t)
10103 {
10104 astmodulestate *state = get_global_ast_state();
10105 if (state == NULL) {
10106 return NULL;
10107 }
10108 return ast2obj_mod(state, t);
10109 }
10110
10111 /* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
PyAST_obj2mod(PyObject * ast,PyArena * arena,int mode)10112 mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
10113 {
10114 const char * const req_name[] = {"Module", "Expression", "Interactive"};
10115 int isinstance;
10116
10117 if (PySys_Audit("compile", "OO", ast, Py_None) < 0) {
10118 return NULL;
10119 }
10120
10121 astmodulestate *state = get_global_ast_state();
10122 PyObject *req_type[3];
10123 req_type[0] = state->Module_type;
10124 req_type[1] = state->Expression_type;
10125 req_type[2] = state->Interactive_type;
10126
10127 assert(0 <= mode && mode <= 2);
10128
10129 isinstance = PyObject_IsInstance(ast, req_type[mode]);
10130 if (isinstance == -1)
10131 return NULL;
10132 if (!isinstance) {
10133 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
10134 req_name[mode], _PyType_Name(Py_TYPE(ast)));
10135 return NULL;
10136 }
10137
10138 mod_ty res = NULL;
10139 if (obj2ast_mod(state, ast, &res, arena) != 0)
10140 return NULL;
10141 else
10142 return res;
10143 }
10144
PyAST_Check(PyObject * obj)10145 int PyAST_Check(PyObject* obj)
10146 {
10147 astmodulestate *state = get_global_ast_state();
10148 if (state == NULL) {
10149 return -1;
10150 }
10151 return PyObject_IsInstance(obj, state->AST_type);
10152 }
10153
10154
10155