blob: f04addbe2011ddbf6c79b57a3da1da3a4b75118a [file] [log] [blame]
Victor Stinner5cf47822020-11-02 22:03:28 +01001// File automatically generated by Parser/asdl_c.py.
2
Thomas Wouterscf297e42007-02-23 15:07:44 +00003
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -07004#include <stddef.h>
5
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007
Victor Stinner5cf47822020-11-02 22:03:28 +01008#ifdef Py_BUILD_CORE
9# include "pycore_ast.h" // struct ast_state
10# include "pycore_interp.h" // _PyInterpreterState.ast
11# include "pycore_pystate.h" // _PyInterpreterState_GET()
12#else
13struct ast_state {
Dino Viehlandac46eb42019-09-11 10:16:34 -070014 int initialized;
15 PyObject *AST_type;
16 PyObject *Add_singleton;
17 PyObject *Add_type;
18 PyObject *And_singleton;
19 PyObject *And_type;
20 PyObject *AnnAssign_type;
21 PyObject *Assert_type;
22 PyObject *Assign_type;
23 PyObject *AsyncFor_type;
24 PyObject *AsyncFunctionDef_type;
25 PyObject *AsyncWith_type;
26 PyObject *Attribute_type;
27 PyObject *AugAssign_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -070028 PyObject *Await_type;
29 PyObject *BinOp_type;
30 PyObject *BitAnd_singleton;
31 PyObject *BitAnd_type;
32 PyObject *BitOr_singleton;
33 PyObject *BitOr_type;
34 PyObject *BitXor_singleton;
35 PyObject *BitXor_type;
36 PyObject *BoolOp_type;
37 PyObject *Break_type;
38 PyObject *Call_type;
39 PyObject *ClassDef_type;
40 PyObject *Compare_type;
41 PyObject *Constant_type;
42 PyObject *Continue_type;
43 PyObject *Del_singleton;
44 PyObject *Del_type;
45 PyObject *Delete_type;
46 PyObject *DictComp_type;
47 PyObject *Dict_type;
48 PyObject *Div_singleton;
49 PyObject *Div_type;
50 PyObject *Eq_singleton;
51 PyObject *Eq_type;
52 PyObject *ExceptHandler_type;
53 PyObject *Expr_type;
54 PyObject *Expression_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -070055 PyObject *FloorDiv_singleton;
56 PyObject *FloorDiv_type;
57 PyObject *For_type;
58 PyObject *FormattedValue_type;
59 PyObject *FunctionDef_type;
60 PyObject *FunctionType_type;
61 PyObject *GeneratorExp_type;
62 PyObject *Global_type;
63 PyObject *GtE_singleton;
64 PyObject *GtE_type;
65 PyObject *Gt_singleton;
66 PyObject *Gt_type;
67 PyObject *IfExp_type;
68 PyObject *If_type;
69 PyObject *ImportFrom_type;
70 PyObject *Import_type;
71 PyObject *In_singleton;
72 PyObject *In_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -070073 PyObject *Interactive_type;
74 PyObject *Invert_singleton;
75 PyObject *Invert_type;
76 PyObject *IsNot_singleton;
77 PyObject *IsNot_type;
78 PyObject *Is_singleton;
79 PyObject *Is_type;
80 PyObject *JoinedStr_type;
81 PyObject *LShift_singleton;
82 PyObject *LShift_type;
83 PyObject *Lambda_type;
84 PyObject *ListComp_type;
85 PyObject *List_type;
86 PyObject *Load_singleton;
87 PyObject *Load_type;
88 PyObject *LtE_singleton;
89 PyObject *LtE_type;
90 PyObject *Lt_singleton;
91 PyObject *Lt_type;
92 PyObject *MatMult_singleton;
93 PyObject *MatMult_type;
94 PyObject *Mod_singleton;
95 PyObject *Mod_type;
96 PyObject *Module_type;
97 PyObject *Mult_singleton;
98 PyObject *Mult_type;
99 PyObject *Name_type;
100 PyObject *NamedExpr_type;
101 PyObject *Nonlocal_type;
102 PyObject *NotEq_singleton;
103 PyObject *NotEq_type;
104 PyObject *NotIn_singleton;
105 PyObject *NotIn_type;
106 PyObject *Not_singleton;
107 PyObject *Not_type;
108 PyObject *Or_singleton;
109 PyObject *Or_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700110 PyObject *Pass_type;
111 PyObject *Pow_singleton;
112 PyObject *Pow_type;
113 PyObject *RShift_singleton;
114 PyObject *RShift_type;
115 PyObject *Raise_type;
116 PyObject *Return_type;
117 PyObject *SetComp_type;
118 PyObject *Set_type;
119 PyObject *Slice_type;
120 PyObject *Starred_type;
121 PyObject *Store_singleton;
122 PyObject *Store_type;
123 PyObject *Sub_singleton;
124 PyObject *Sub_type;
125 PyObject *Subscript_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700126 PyObject *Try_type;
127 PyObject *Tuple_type;
128 PyObject *TypeIgnore_type;
129 PyObject *UAdd_singleton;
130 PyObject *UAdd_type;
131 PyObject *USub_singleton;
132 PyObject *USub_type;
133 PyObject *UnaryOp_type;
134 PyObject *While_type;
135 PyObject *With_type;
136 PyObject *YieldFrom_type;
137 PyObject *Yield_type;
138 PyObject *__dict__;
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +0300139 PyObject *__doc__;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700140 PyObject *__module__;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700141 PyObject *_attributes;
142 PyObject *_fields;
143 PyObject *alias_type;
144 PyObject *annotation;
145 PyObject *arg;
146 PyObject *arg_type;
147 PyObject *args;
148 PyObject *argtypes;
149 PyObject *arguments_type;
150 PyObject *asname;
Serhiy Storchakabace59d2020-03-22 20:33:34 +0200151 PyObject *ast;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700152 PyObject *attr;
153 PyObject *bases;
154 PyObject *body;
155 PyObject *boolop_type;
156 PyObject *cause;
157 PyObject *cmpop_type;
158 PyObject *col_offset;
159 PyObject *comparators;
160 PyObject *comprehension_type;
161 PyObject *context_expr;
162 PyObject *conversion;
163 PyObject *ctx;
164 PyObject *decorator_list;
165 PyObject *defaults;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700166 PyObject *elt;
167 PyObject *elts;
168 PyObject *end_col_offset;
169 PyObject *end_lineno;
170 PyObject *exc;
171 PyObject *excepthandler_type;
172 PyObject *expr_context_type;
173 PyObject *expr_type;
174 PyObject *finalbody;
175 PyObject *format_spec;
176 PyObject *func;
177 PyObject *generators;
178 PyObject *handlers;
179 PyObject *id;
180 PyObject *ifs;
181 PyObject *is_async;
182 PyObject *items;
183 PyObject *iter;
184 PyObject *key;
185 PyObject *keys;
186 PyObject *keyword_type;
187 PyObject *keywords;
188 PyObject *kind;
189 PyObject *kw_defaults;
190 PyObject *kwarg;
191 PyObject *kwonlyargs;
192 PyObject *left;
193 PyObject *level;
194 PyObject *lineno;
195 PyObject *lower;
196 PyObject *mod_type;
197 PyObject *module;
198 PyObject *msg;
199 PyObject *name;
200 PyObject *names;
201 PyObject *op;
202 PyObject *operand;
203 PyObject *operator_type;
204 PyObject *ops;
205 PyObject *optional_vars;
206 PyObject *orelse;
207 PyObject *posonlyargs;
208 PyObject *returns;
209 PyObject *right;
210 PyObject *simple;
211 PyObject *slice;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700212 PyObject *step;
213 PyObject *stmt_type;
214 PyObject *tag;
215 PyObject *target;
216 PyObject *targets;
217 PyObject *test;
218 PyObject *type;
219 PyObject *type_comment;
220 PyObject *type_ignore_type;
221 PyObject *type_ignores;
222 PyObject *unaryop_type;
223 PyObject *upper;
224 PyObject *value;
225 PyObject *values;
226 PyObject *vararg;
227 PyObject *withitem_type;
Victor Stinner5cf47822020-11-02 22:03:28 +0100228};
229#endif // Py_BUILD_CORE
Dino Viehlandac46eb42019-09-11 10:16:34 -0700230
Victor Stinnere5fbe0c2020-09-15 18:03:34 +0200231// Forward declaration
Victor Stinner5cf47822020-11-02 22:03:28 +0100232static int init_types(struct ast_state *state);
Victor Stinnere5fbe0c2020-09-15 18:03:34 +0200233
Victor Stinner5cf47822020-11-02 22:03:28 +0100234#ifdef Py_BUILD_CORE
235static struct ast_state*
236get_ast_state(void)
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +0200237{
Victor Stinner5cf47822020-11-02 22:03:28 +0100238 PyInterpreterState *interp = _PyInterpreterState_GET();
239 struct ast_state *state = &interp->ast;
Victor Stinnere5fbe0c2020-09-15 18:03:34 +0200240 if (!init_types(state)) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +0200241 return NULL;
242 }
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +0200243 return state;
Victor Stinner74419f02020-07-03 11:35:37 +0200244}
Victor Stinner5cf47822020-11-02 22:03:28 +0100245#else
246static struct ast_state global_ast_state;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700247
Victor Stinner5cf47822020-11-02 22:03:28 +0100248static struct ast_state*
249get_ast_state(void)
Dino Viehlandac46eb42019-09-11 10:16:34 -0700250{
Victor Stinner5cf47822020-11-02 22:03:28 +0100251 struct ast_state *state = &global_ast_state;
252 if (!init_types(state)) {
253 return NULL;
254 }
Victor Stinnere5fbe0c2020-09-15 18:03:34 +0200255 return state;
256}
Victor Stinner5cf47822020-11-02 22:03:28 +0100257#endif // Py_BUILD_CORE
258
259// Include Python-ast.h after pycore_interp.h to avoid conflicts
260// with the Yield macro redefined by <winbase.h>
261#include "Python-ast.h"
262#include "structmember.h"
Victor Stinnere5fbe0c2020-09-15 18:03:34 +0200263
264void _PyAST_Fini(PyThreadState *tstate)
265{
Victor Stinner5cf47822020-11-02 22:03:28 +0100266#ifdef Py_BUILD_CORE
267 struct ast_state *state = &tstate->interp->ast;
268#else
269 struct ast_state *state = &global_ast_state;
270#endif
271
Victor Stinner74419f02020-07-03 11:35:37 +0200272 Py_CLEAR(state->AST_type);
273 Py_CLEAR(state->Add_singleton);
274 Py_CLEAR(state->Add_type);
275 Py_CLEAR(state->And_singleton);
276 Py_CLEAR(state->And_type);
277 Py_CLEAR(state->AnnAssign_type);
278 Py_CLEAR(state->Assert_type);
279 Py_CLEAR(state->Assign_type);
280 Py_CLEAR(state->AsyncFor_type);
281 Py_CLEAR(state->AsyncFunctionDef_type);
282 Py_CLEAR(state->AsyncWith_type);
283 Py_CLEAR(state->Attribute_type);
284 Py_CLEAR(state->AugAssign_type);
285 Py_CLEAR(state->Await_type);
286 Py_CLEAR(state->BinOp_type);
287 Py_CLEAR(state->BitAnd_singleton);
288 Py_CLEAR(state->BitAnd_type);
289 Py_CLEAR(state->BitOr_singleton);
290 Py_CLEAR(state->BitOr_type);
291 Py_CLEAR(state->BitXor_singleton);
292 Py_CLEAR(state->BitXor_type);
293 Py_CLEAR(state->BoolOp_type);
294 Py_CLEAR(state->Break_type);
295 Py_CLEAR(state->Call_type);
296 Py_CLEAR(state->ClassDef_type);
297 Py_CLEAR(state->Compare_type);
298 Py_CLEAR(state->Constant_type);
299 Py_CLEAR(state->Continue_type);
300 Py_CLEAR(state->Del_singleton);
301 Py_CLEAR(state->Del_type);
302 Py_CLEAR(state->Delete_type);
303 Py_CLEAR(state->DictComp_type);
304 Py_CLEAR(state->Dict_type);
305 Py_CLEAR(state->Div_singleton);
306 Py_CLEAR(state->Div_type);
307 Py_CLEAR(state->Eq_singleton);
308 Py_CLEAR(state->Eq_type);
309 Py_CLEAR(state->ExceptHandler_type);
310 Py_CLEAR(state->Expr_type);
311 Py_CLEAR(state->Expression_type);
312 Py_CLEAR(state->FloorDiv_singleton);
313 Py_CLEAR(state->FloorDiv_type);
314 Py_CLEAR(state->For_type);
315 Py_CLEAR(state->FormattedValue_type);
316 Py_CLEAR(state->FunctionDef_type);
317 Py_CLEAR(state->FunctionType_type);
318 Py_CLEAR(state->GeneratorExp_type);
319 Py_CLEAR(state->Global_type);
320 Py_CLEAR(state->GtE_singleton);
321 Py_CLEAR(state->GtE_type);
322 Py_CLEAR(state->Gt_singleton);
323 Py_CLEAR(state->Gt_type);
324 Py_CLEAR(state->IfExp_type);
325 Py_CLEAR(state->If_type);
326 Py_CLEAR(state->ImportFrom_type);
327 Py_CLEAR(state->Import_type);
328 Py_CLEAR(state->In_singleton);
329 Py_CLEAR(state->In_type);
330 Py_CLEAR(state->Interactive_type);
331 Py_CLEAR(state->Invert_singleton);
332 Py_CLEAR(state->Invert_type);
333 Py_CLEAR(state->IsNot_singleton);
334 Py_CLEAR(state->IsNot_type);
335 Py_CLEAR(state->Is_singleton);
336 Py_CLEAR(state->Is_type);
337 Py_CLEAR(state->JoinedStr_type);
338 Py_CLEAR(state->LShift_singleton);
339 Py_CLEAR(state->LShift_type);
340 Py_CLEAR(state->Lambda_type);
341 Py_CLEAR(state->ListComp_type);
342 Py_CLEAR(state->List_type);
343 Py_CLEAR(state->Load_singleton);
344 Py_CLEAR(state->Load_type);
345 Py_CLEAR(state->LtE_singleton);
346 Py_CLEAR(state->LtE_type);
347 Py_CLEAR(state->Lt_singleton);
348 Py_CLEAR(state->Lt_type);
349 Py_CLEAR(state->MatMult_singleton);
350 Py_CLEAR(state->MatMult_type);
351 Py_CLEAR(state->Mod_singleton);
352 Py_CLEAR(state->Mod_type);
353 Py_CLEAR(state->Module_type);
354 Py_CLEAR(state->Mult_singleton);
355 Py_CLEAR(state->Mult_type);
356 Py_CLEAR(state->Name_type);
357 Py_CLEAR(state->NamedExpr_type);
358 Py_CLEAR(state->Nonlocal_type);
359 Py_CLEAR(state->NotEq_singleton);
360 Py_CLEAR(state->NotEq_type);
361 Py_CLEAR(state->NotIn_singleton);
362 Py_CLEAR(state->NotIn_type);
363 Py_CLEAR(state->Not_singleton);
364 Py_CLEAR(state->Not_type);
365 Py_CLEAR(state->Or_singleton);
366 Py_CLEAR(state->Or_type);
367 Py_CLEAR(state->Pass_type);
368 Py_CLEAR(state->Pow_singleton);
369 Py_CLEAR(state->Pow_type);
370 Py_CLEAR(state->RShift_singleton);
371 Py_CLEAR(state->RShift_type);
372 Py_CLEAR(state->Raise_type);
373 Py_CLEAR(state->Return_type);
374 Py_CLEAR(state->SetComp_type);
375 Py_CLEAR(state->Set_type);
376 Py_CLEAR(state->Slice_type);
377 Py_CLEAR(state->Starred_type);
378 Py_CLEAR(state->Store_singleton);
379 Py_CLEAR(state->Store_type);
380 Py_CLEAR(state->Sub_singleton);
381 Py_CLEAR(state->Sub_type);
382 Py_CLEAR(state->Subscript_type);
383 Py_CLEAR(state->Try_type);
384 Py_CLEAR(state->Tuple_type);
385 Py_CLEAR(state->TypeIgnore_type);
386 Py_CLEAR(state->UAdd_singleton);
387 Py_CLEAR(state->UAdd_type);
388 Py_CLEAR(state->USub_singleton);
389 Py_CLEAR(state->USub_type);
390 Py_CLEAR(state->UnaryOp_type);
391 Py_CLEAR(state->While_type);
392 Py_CLEAR(state->With_type);
393 Py_CLEAR(state->YieldFrom_type);
394 Py_CLEAR(state->Yield_type);
395 Py_CLEAR(state->__dict__);
396 Py_CLEAR(state->__doc__);
397 Py_CLEAR(state->__module__);
398 Py_CLEAR(state->_attributes);
399 Py_CLEAR(state->_fields);
400 Py_CLEAR(state->alias_type);
401 Py_CLEAR(state->annotation);
402 Py_CLEAR(state->arg);
403 Py_CLEAR(state->arg_type);
404 Py_CLEAR(state->args);
405 Py_CLEAR(state->argtypes);
406 Py_CLEAR(state->arguments_type);
407 Py_CLEAR(state->asname);
408 Py_CLEAR(state->ast);
409 Py_CLEAR(state->attr);
410 Py_CLEAR(state->bases);
411 Py_CLEAR(state->body);
412 Py_CLEAR(state->boolop_type);
413 Py_CLEAR(state->cause);
414 Py_CLEAR(state->cmpop_type);
415 Py_CLEAR(state->col_offset);
416 Py_CLEAR(state->comparators);
417 Py_CLEAR(state->comprehension_type);
418 Py_CLEAR(state->context_expr);
419 Py_CLEAR(state->conversion);
420 Py_CLEAR(state->ctx);
421 Py_CLEAR(state->decorator_list);
422 Py_CLEAR(state->defaults);
423 Py_CLEAR(state->elt);
424 Py_CLEAR(state->elts);
425 Py_CLEAR(state->end_col_offset);
426 Py_CLEAR(state->end_lineno);
427 Py_CLEAR(state->exc);
428 Py_CLEAR(state->excepthandler_type);
429 Py_CLEAR(state->expr_context_type);
430 Py_CLEAR(state->expr_type);
431 Py_CLEAR(state->finalbody);
432 Py_CLEAR(state->format_spec);
433 Py_CLEAR(state->func);
434 Py_CLEAR(state->generators);
435 Py_CLEAR(state->handlers);
436 Py_CLEAR(state->id);
437 Py_CLEAR(state->ifs);
438 Py_CLEAR(state->is_async);
439 Py_CLEAR(state->items);
440 Py_CLEAR(state->iter);
441 Py_CLEAR(state->key);
442 Py_CLEAR(state->keys);
443 Py_CLEAR(state->keyword_type);
444 Py_CLEAR(state->keywords);
445 Py_CLEAR(state->kind);
446 Py_CLEAR(state->kw_defaults);
447 Py_CLEAR(state->kwarg);
448 Py_CLEAR(state->kwonlyargs);
449 Py_CLEAR(state->left);
450 Py_CLEAR(state->level);
451 Py_CLEAR(state->lineno);
452 Py_CLEAR(state->lower);
453 Py_CLEAR(state->mod_type);
454 Py_CLEAR(state->module);
455 Py_CLEAR(state->msg);
456 Py_CLEAR(state->name);
457 Py_CLEAR(state->names);
458 Py_CLEAR(state->op);
459 Py_CLEAR(state->operand);
460 Py_CLEAR(state->operator_type);
461 Py_CLEAR(state->ops);
462 Py_CLEAR(state->optional_vars);
463 Py_CLEAR(state->orelse);
464 Py_CLEAR(state->posonlyargs);
465 Py_CLEAR(state->returns);
466 Py_CLEAR(state->right);
467 Py_CLEAR(state->simple);
468 Py_CLEAR(state->slice);
469 Py_CLEAR(state->step);
470 Py_CLEAR(state->stmt_type);
471 Py_CLEAR(state->tag);
472 Py_CLEAR(state->target);
473 Py_CLEAR(state->targets);
474 Py_CLEAR(state->test);
475 Py_CLEAR(state->type);
476 Py_CLEAR(state->type_comment);
477 Py_CLEAR(state->type_ignore_type);
478 Py_CLEAR(state->type_ignores);
479 Py_CLEAR(state->unaryop_type);
480 Py_CLEAR(state->upper);
481 Py_CLEAR(state->value);
482 Py_CLEAR(state->values);
483 Py_CLEAR(state->vararg);
484 Py_CLEAR(state->withitem_type);
Dino Viehlandac46eb42019-09-11 10:16:34 -0700485
Victor Stinnere5fbe0c2020-09-15 18:03:34 +0200486 state->initialized = 0;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700487}
488
Victor Stinner5cf47822020-11-02 22:03:28 +0100489static int init_identifiers(struct ast_state *state)
Dino Viehlandac46eb42019-09-11 10:16:34 -0700490{
Dino Viehlandac46eb42019-09-11 10:16:34 -0700491 if ((state->__dict__ = PyUnicode_InternFromString("__dict__")) == NULL) return 0;
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +0300492 if ((state->__doc__ = PyUnicode_InternFromString("__doc__")) == NULL) return 0;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700493 if ((state->__module__ = PyUnicode_InternFromString("__module__")) == NULL) return 0;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700494 if ((state->_attributes = PyUnicode_InternFromString("_attributes")) == NULL) return 0;
495 if ((state->_fields = PyUnicode_InternFromString("_fields")) == NULL) return 0;
496 if ((state->annotation = PyUnicode_InternFromString("annotation")) == NULL) return 0;
497 if ((state->arg = PyUnicode_InternFromString("arg")) == NULL) return 0;
498 if ((state->args = PyUnicode_InternFromString("args")) == NULL) return 0;
499 if ((state->argtypes = PyUnicode_InternFromString("argtypes")) == NULL) return 0;
500 if ((state->asname = PyUnicode_InternFromString("asname")) == NULL) return 0;
Serhiy Storchakabace59d2020-03-22 20:33:34 +0200501 if ((state->ast = PyUnicode_InternFromString("ast")) == NULL) return 0;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700502 if ((state->attr = PyUnicode_InternFromString("attr")) == NULL) return 0;
503 if ((state->bases = PyUnicode_InternFromString("bases")) == NULL) return 0;
504 if ((state->body = PyUnicode_InternFromString("body")) == NULL) return 0;
505 if ((state->cause = PyUnicode_InternFromString("cause")) == NULL) return 0;
506 if ((state->col_offset = PyUnicode_InternFromString("col_offset")) == NULL) return 0;
507 if ((state->comparators = PyUnicode_InternFromString("comparators")) == NULL) return 0;
508 if ((state->context_expr = PyUnicode_InternFromString("context_expr")) == NULL) return 0;
509 if ((state->conversion = PyUnicode_InternFromString("conversion")) == NULL) return 0;
510 if ((state->ctx = PyUnicode_InternFromString("ctx")) == NULL) return 0;
511 if ((state->decorator_list = PyUnicode_InternFromString("decorator_list")) == NULL) return 0;
512 if ((state->defaults = PyUnicode_InternFromString("defaults")) == NULL) return 0;
Dino Viehlandac46eb42019-09-11 10:16:34 -0700513 if ((state->elt = PyUnicode_InternFromString("elt")) == NULL) return 0;
514 if ((state->elts = PyUnicode_InternFromString("elts")) == NULL) return 0;
515 if ((state->end_col_offset = PyUnicode_InternFromString("end_col_offset")) == NULL) return 0;
516 if ((state->end_lineno = PyUnicode_InternFromString("end_lineno")) == NULL) return 0;
517 if ((state->exc = PyUnicode_InternFromString("exc")) == NULL) return 0;
518 if ((state->finalbody = PyUnicode_InternFromString("finalbody")) == NULL) return 0;
519 if ((state->format_spec = PyUnicode_InternFromString("format_spec")) == NULL) return 0;
520 if ((state->func = PyUnicode_InternFromString("func")) == NULL) return 0;
521 if ((state->generators = PyUnicode_InternFromString("generators")) == NULL) return 0;
522 if ((state->handlers = PyUnicode_InternFromString("handlers")) == NULL) return 0;
523 if ((state->id = PyUnicode_InternFromString("id")) == NULL) return 0;
524 if ((state->ifs = PyUnicode_InternFromString("ifs")) == NULL) return 0;
525 if ((state->is_async = PyUnicode_InternFromString("is_async")) == NULL) return 0;
526 if ((state->items = PyUnicode_InternFromString("items")) == NULL) return 0;
527 if ((state->iter = PyUnicode_InternFromString("iter")) == NULL) return 0;
528 if ((state->key = PyUnicode_InternFromString("key")) == NULL) return 0;
529 if ((state->keys = PyUnicode_InternFromString("keys")) == NULL) return 0;
530 if ((state->keywords = PyUnicode_InternFromString("keywords")) == NULL) return 0;
531 if ((state->kind = PyUnicode_InternFromString("kind")) == NULL) return 0;
532 if ((state->kw_defaults = PyUnicode_InternFromString("kw_defaults")) == NULL) return 0;
533 if ((state->kwarg = PyUnicode_InternFromString("kwarg")) == NULL) return 0;
534 if ((state->kwonlyargs = PyUnicode_InternFromString("kwonlyargs")) == NULL) return 0;
535 if ((state->left = PyUnicode_InternFromString("left")) == NULL) return 0;
536 if ((state->level = PyUnicode_InternFromString("level")) == NULL) return 0;
537 if ((state->lineno = PyUnicode_InternFromString("lineno")) == NULL) return 0;
538 if ((state->lower = PyUnicode_InternFromString("lower")) == NULL) return 0;
539 if ((state->module = PyUnicode_InternFromString("module")) == NULL) return 0;
540 if ((state->msg = PyUnicode_InternFromString("msg")) == NULL) return 0;
541 if ((state->name = PyUnicode_InternFromString("name")) == NULL) return 0;
542 if ((state->names = PyUnicode_InternFromString("names")) == NULL) return 0;
543 if ((state->op = PyUnicode_InternFromString("op")) == NULL) return 0;
544 if ((state->operand = PyUnicode_InternFromString("operand")) == NULL) return 0;
545 if ((state->ops = PyUnicode_InternFromString("ops")) == NULL) return 0;
546 if ((state->optional_vars = PyUnicode_InternFromString("optional_vars")) == NULL) return 0;
547 if ((state->orelse = PyUnicode_InternFromString("orelse")) == NULL) return 0;
548 if ((state->posonlyargs = PyUnicode_InternFromString("posonlyargs")) == NULL) return 0;
549 if ((state->returns = PyUnicode_InternFromString("returns")) == NULL) return 0;
550 if ((state->right = PyUnicode_InternFromString("right")) == NULL) return 0;
551 if ((state->simple = PyUnicode_InternFromString("simple")) == NULL) return 0;
552 if ((state->slice = PyUnicode_InternFromString("slice")) == NULL) return 0;
553 if ((state->step = PyUnicode_InternFromString("step")) == NULL) return 0;
554 if ((state->tag = PyUnicode_InternFromString("tag")) == NULL) return 0;
555 if ((state->target = PyUnicode_InternFromString("target")) == NULL) return 0;
556 if ((state->targets = PyUnicode_InternFromString("targets")) == NULL) return 0;
557 if ((state->test = PyUnicode_InternFromString("test")) == NULL) return 0;
558 if ((state->type = PyUnicode_InternFromString("type")) == NULL) return 0;
559 if ((state->type_comment = PyUnicode_InternFromString("type_comment")) == NULL) return 0;
560 if ((state->type_ignores = PyUnicode_InternFromString("type_ignores")) == NULL) return 0;
561 if ((state->upper = PyUnicode_InternFromString("upper")) == NULL) return 0;
562 if ((state->value = PyUnicode_InternFromString("value")) == NULL) return 0;
563 if ((state->values = PyUnicode_InternFromString("values")) == NULL) return 0;
564 if ((state->vararg = PyUnicode_InternFromString("vararg")) == NULL) return 0;
565 return 1;
566};
567
Pablo Galindoa5634c42020-09-16 19:42:00 +0100568GENERATE_ASDL_SEQ_CONSTRUCTOR(mod, mod_ty)
569GENERATE_ASDL_SEQ_CONSTRUCTOR(stmt, stmt_ty)
570GENERATE_ASDL_SEQ_CONSTRUCTOR(expr, expr_ty)
571GENERATE_ASDL_SEQ_CONSTRUCTOR(comprehension, comprehension_ty)
572GENERATE_ASDL_SEQ_CONSTRUCTOR(excepthandler, excepthandler_ty)
573GENERATE_ASDL_SEQ_CONSTRUCTOR(arguments, arguments_ty)
574GENERATE_ASDL_SEQ_CONSTRUCTOR(arg, arg_ty)
575GENERATE_ASDL_SEQ_CONSTRUCTOR(keyword, keyword_ty)
576GENERATE_ASDL_SEQ_CONSTRUCTOR(alias, alias_ty)
577GENERATE_ASDL_SEQ_CONSTRUCTOR(withitem, withitem_ty)
578GENERATE_ASDL_SEQ_CONSTRUCTOR(type_ignore, type_ignore_ty)
579
Victor Stinner5cf47822020-11-02 22:03:28 +0100580static PyObject* ast2obj_mod(struct ast_state *state, void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300581static const char * const Module_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200582 "body",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800583 "type_ignores",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000584};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300585static const char * const Interactive_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200586 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000587};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300588static const char * const Expression_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200589 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000590};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300591static const char * const FunctionType_fields[]={
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800592 "argtypes",
593 "returns",
594};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300595static const char * const stmt_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200596 "lineno",
597 "col_offset",
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000598 "end_lineno",
599 "end_col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000600};
Victor Stinner5cf47822020-11-02 22:03:28 +0100601static PyObject* ast2obj_stmt(struct ast_state *state, void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300602static const char * const FunctionDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200603 "name",
604 "args",
605 "body",
606 "decorator_list",
607 "returns",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800608 "type_comment",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000609};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300610static const char * const AsyncFunctionDef_fields[]={
Yury Selivanov75445082015-05-11 22:57:16 -0400611 "name",
612 "args",
613 "body",
614 "decorator_list",
615 "returns",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800616 "type_comment",
Yury Selivanov75445082015-05-11 22:57:16 -0400617};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300618static const char * const ClassDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200619 "name",
620 "bases",
621 "keywords",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200622 "body",
623 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000624};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300625static const char * const Return_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200626 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000627};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300628static const char * const Delete_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200629 "targets",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000630};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300631static const char * const Assign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200632 "targets",
633 "value",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800634 "type_comment",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000635};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300636static const char * const AugAssign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200637 "target",
638 "op",
639 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000640};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300641static const char * const AnnAssign_fields[]={
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700642 "target",
643 "annotation",
644 "value",
645 "simple",
646};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300647static const char * const For_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200648 "target",
649 "iter",
650 "body",
651 "orelse",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800652 "type_comment",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000653};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300654static const char * const AsyncFor_fields[]={
Yury Selivanov75445082015-05-11 22:57:16 -0400655 "target",
656 "iter",
657 "body",
658 "orelse",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800659 "type_comment",
Yury Selivanov75445082015-05-11 22:57:16 -0400660};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300661static const char * const While_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200662 "test",
663 "body",
664 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000665};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300666static const char * const If_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200667 "test",
668 "body",
669 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000670};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300671static const char * const With_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200672 "items",
673 "body",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800674 "type_comment",
Guido van Rossumc2e20742006-02-27 22:32:47 +0000675};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300676static const char * const AsyncWith_fields[]={
Yury Selivanov75445082015-05-11 22:57:16 -0400677 "items",
678 "body",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800679 "type_comment",
Yury Selivanov75445082015-05-11 22:57:16 -0400680};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300681static const char * const Raise_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200682 "exc",
683 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000684};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300685static const char * const Try_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200686 "body",
687 "handlers",
688 "orelse",
689 "finalbody",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000690};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300691static const char * const Assert_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200692 "test",
693 "msg",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000694};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300695static const char * const Import_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200696 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000697};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300698static const char * const ImportFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200699 "module",
700 "names",
701 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000702};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300703static const char * const Global_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200704 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000705};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300706static const char * const Nonlocal_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200707 "names",
Jeremy Hylton81e95022007-02-27 06:50:52 +0000708};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300709static const char * const Expr_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200710 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000711};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300712static const char * const expr_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200713 "lineno",
714 "col_offset",
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000715 "end_lineno",
716 "end_col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000717};
Victor Stinner5cf47822020-11-02 22:03:28 +0100718static PyObject* ast2obj_expr(struct ast_state *state, void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300719static const char * const BoolOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200720 "op",
721 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000722};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300723static const char * const NamedExpr_fields[]={
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700724 "target",
725 "value",
726};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300727static const char * const BinOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200728 "left",
729 "op",
730 "right",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000731};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300732static const char * const UnaryOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200733 "op",
734 "operand",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000735};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300736static const char * const Lambda_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200737 "args",
738 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000739};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300740static const char * const IfExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200741 "test",
742 "body",
743 "orelse",
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000744};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300745static const char * const Dict_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200746 "keys",
747 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000748};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300749static const char * const Set_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200750 "elts",
Guido van Rossum86e58e22006-08-28 15:27:34 +0000751};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300752static const char * const ListComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200753 "elt",
754 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000755};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300756static const char * const SetComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200757 "elt",
758 "generators",
Nick Coghlan650f0d02007-04-15 12:05:43 +0000759};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300760static const char * const DictComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200761 "key",
762 "value",
763 "generators",
Guido van Rossum992d4a32007-07-11 13:09:30 +0000764};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300765static const char * const GeneratorExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200766 "elt",
767 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000768};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300769static const char * const Await_fields[]={
Yury Selivanov75445082015-05-11 22:57:16 -0400770 "value",
771};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300772static const char * const Yield_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200773 "value",
Benjamin Peterson527c6222012-01-14 08:58:23 -0500774};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300775static const char * const YieldFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200776 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000777};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300778static const char * const Compare_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200779 "left",
780 "ops",
781 "comparators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000782};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300783static const char * const Call_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200784 "func",
785 "args",
786 "keywords",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000787};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300788static const char * const FormattedValue_fields[]={
Eric V. Smith235a6f02015-09-19 14:51:32 -0400789 "value",
790 "conversion",
791 "format_spec",
792};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300793static const char * const JoinedStr_fields[]={
Eric V. Smith235a6f02015-09-19 14:51:32 -0400794 "values",
795};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300796static const char * const Constant_fields[]={
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100797 "value",
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700798 "kind",
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100799};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300800static const char * const Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200801 "value",
802 "attr",
803 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000804};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300805static const char * const Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200806 "value",
807 "slice",
808 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000809};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300810static const char * const Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200811 "value",
812 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000813};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300814static const char * const Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200815 "id",
816 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000817};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300818static const char * const List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200819 "elts",
820 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000821};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300822static const char * const Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200823 "elts",
824 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000825};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300826static const char * const Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200827 "lower",
828 "upper",
829 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000830};
Victor Stinner5cf47822020-11-02 22:03:28 +0100831static PyObject* ast2obj_expr_context(struct ast_state *state, expr_context_ty);
832static PyObject* ast2obj_boolop(struct ast_state *state, boolop_ty);
833static PyObject* ast2obj_operator(struct ast_state *state, operator_ty);
834static PyObject* ast2obj_unaryop(struct ast_state *state, unaryop_ty);
835static PyObject* ast2obj_cmpop(struct ast_state *state, cmpop_ty);
836static PyObject* ast2obj_comprehension(struct ast_state *state, void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300837static const char * const comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200838 "target",
839 "iter",
840 "ifs",
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700841 "is_async",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000842};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300843static const char * const excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200844 "lineno",
845 "col_offset",
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000846 "end_lineno",
847 "end_col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000848};
Victor Stinner5cf47822020-11-02 22:03:28 +0100849static PyObject* ast2obj_excepthandler(struct ast_state *state, void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300850static const char * const ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200851 "type",
852 "name",
853 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000854};
Victor Stinner5cf47822020-11-02 22:03:28 +0100855static PyObject* ast2obj_arguments(struct ast_state *state, void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300856static const char * const arguments_fields[]={
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100857 "posonlyargs",
Pablo Galindocd6e83b2019-07-15 01:32:18 +0200858 "args",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200859 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200860 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200861 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200862 "kwarg",
863 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000864};
Victor Stinner5cf47822020-11-02 22:03:28 +0100865static PyObject* ast2obj_arg(struct ast_state *state, void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300866static const char * const arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200867 "lineno",
868 "col_offset",
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000869 "end_lineno",
870 "end_col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700871};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300872static const char * const arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200873 "arg",
874 "annotation",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800875 "type_comment",
Neal Norwitzc1505362006-12-28 06:47:50 +0000876};
Victor Stinner5cf47822020-11-02 22:03:28 +0100877static PyObject* ast2obj_keyword(struct ast_state *state, void*);
Pablo Galindo168660b2020-04-02 00:47:39 +0100878static const char * const keyword_attributes[] = {
879 "lineno",
880 "col_offset",
881 "end_lineno",
882 "end_col_offset",
883};
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300884static const char * const keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200885 "arg",
886 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000887};
Victor Stinner5cf47822020-11-02 22:03:28 +0100888static PyObject* ast2obj_alias(struct ast_state *state, void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300889static const char * const alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200890 "name",
891 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000892};
Victor Stinner5cf47822020-11-02 22:03:28 +0100893static PyObject* ast2obj_withitem(struct ast_state *state, void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300894static const char * const withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200895 "context_expr",
896 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500897};
Victor Stinner5cf47822020-11-02 22:03:28 +0100898static PyObject* ast2obj_type_ignore(struct ast_state *state, void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300899static const char * const TypeIgnore_fields[]={
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800900 "lineno",
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700901 "tag",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800902};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000903
904
INADA Naokifc489082017-01-25 22:33:43 +0900905
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700906typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100907 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700908 PyObject *dict;
909} AST_object;
910
Benjamin Peterson1767e022012-03-14 21:50:29 -0500911static void
912ast_dealloc(AST_object *self)
913{
INADA Naokia6296d32017-08-24 14:55:17 +0900914 /* bpo-31095: UnTrack is needed before calling any callbacks */
Eddie Elizondo0247e802019-09-14 09:38:17 -0400915 PyTypeObject *tp = Py_TYPE(self);
INADA Naokia6296d32017-08-24 14:55:17 +0900916 PyObject_GC_UnTrack(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500917 Py_CLEAR(self->dict);
Eddie Elizondo0247e802019-09-14 09:38:17 -0400918 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
919 assert(free_func != NULL);
920 free_func(self);
921 Py_DECREF(tp);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500922}
923
Neal Norwitz207c9f32008-03-31 04:42:11 +0000924static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700925ast_traverse(AST_object *self, visitproc visit, void *arg)
926{
Pablo Galindo1cf15af2020-05-27 10:03:38 +0100927 Py_VISIT(Py_TYPE(self));
Benjamin Peterson81071762012-07-08 11:03:46 -0700928 Py_VISIT(self->dict);
929 return 0;
930}
931
Serhiy Storchakaa5c42282018-05-31 07:34:34 +0300932static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700933ast_clear(AST_object *self)
934{
935 Py_CLEAR(self->dict);
Serhiy Storchakaa5c42282018-05-31 07:34:34 +0300936 return 0;
Benjamin Peterson81071762012-07-08 11:03:46 -0700937}
938
939static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000940ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
941{
Victor Stinner5cf47822020-11-02 22:03:28 +0100942 struct ast_state *state = get_ast_state();
Victor Stinner1f764532020-07-04 23:18:15 +0200943 if (state == NULL) {
944 return -1;
945 }
946
Neal Norwitz207c9f32008-03-31 04:42:11 +0000947 Py_ssize_t i, numfields = 0;
948 int res = -1;
949 PyObject *key, *value, *fields;
Victor Stinner74419f02020-07-03 11:35:37 +0200950 if (_PyObject_LookupAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200951 goto cleanup;
952 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000953 if (fields) {
954 numfields = PySequence_Size(fields);
Rémi Lapeyrec73914a2020-05-24 23:12:57 +0200955 if (numfields == -1) {
Neal Norwitz207c9f32008-03-31 04:42:11 +0000956 goto cleanup;
Rémi Lapeyrec73914a2020-05-24 23:12:57 +0200957 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000958 }
INADA Naoki4c78c522017-02-24 02:48:17 +0900959
Neal Norwitz207c9f32008-03-31 04:42:11 +0000960 res = 0; /* if no error occurs, this stays 0 to the end */
INADA Naoki4c78c522017-02-24 02:48:17 +0900961 if (numfields < PyTuple_GET_SIZE(args)) {
962 PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
963 "%zd positional argument%s",
Dino Viehlandac46eb42019-09-11 10:16:34 -0700964 _PyType_Name(Py_TYPE(self)),
INADA Naoki4c78c522017-02-24 02:48:17 +0900965 numfields, numfields == 1 ? "" : "s");
966 res = -1;
967 goto cleanup;
968 }
969 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
970 /* cannot be reached when fields is NULL */
971 PyObject *name = PySequence_GetItem(fields, i);
972 if (!name) {
Neal Norwitz207c9f32008-03-31 04:42:11 +0000973 res = -1;
974 goto cleanup;
975 }
INADA Naoki4c78c522017-02-24 02:48:17 +0900976 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
977 Py_DECREF(name);
Rémi Lapeyrec73914a2020-05-24 23:12:57 +0200978 if (res < 0) {
INADA Naoki4c78c522017-02-24 02:48:17 +0900979 goto cleanup;
Rémi Lapeyrec73914a2020-05-24 23:12:57 +0200980 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000981 }
982 if (kw) {
983 i = 0; /* needed by PyDict_Next */
984 while (PyDict_Next(kw, &i, &key, &value)) {
Rémi Lapeyrec73914a2020-05-24 23:12:57 +0200985 int contains = PySequence_Contains(fields, key);
986 if (contains == -1) {
987 res = -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000988 goto cleanup;
Rémi Lapeyrec73914a2020-05-24 23:12:57 +0200989 } else if (contains == 1) {
990 Py_ssize_t p = PySequence_Index(fields, key);
991 if (p == -1) {
992 res = -1;
993 goto cleanup;
994 }
995 if (p < PyTuple_GET_SIZE(args)) {
996 PyErr_Format(PyExc_TypeError,
997 "%.400s got multiple values for argument '%U'",
998 Py_TYPE(self)->tp_name, key);
999 res = -1;
1000 goto cleanup;
1001 }
1002 }
1003 res = PyObject_SetAttr(self, key, value);
1004 if (res < 0) {
1005 goto cleanup;
1006 }
Neal Norwitz207c9f32008-03-31 04:42:11 +00001007 }
1008 }
1009 cleanup:
1010 Py_XDECREF(fields);
1011 return res;
1012}
1013
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001014/* Pickling support */
1015static PyObject *
1016ast_type_reduce(PyObject *self, PyObject *unused)
1017{
Victor Stinner5cf47822020-11-02 22:03:28 +01001018 struct ast_state *state = get_ast_state();
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02001019 if (state == NULL) {
1020 return NULL;
1021 }
1022
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001023 PyObject *dict;
Victor Stinner74419f02020-07-03 11:35:37 +02001024 if (_PyObject_LookupAttr(self, state->__dict__, &dict) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001025 return NULL;
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001026 }
1027 if (dict) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001028 return Py_BuildValue("O()N", Py_TYPE(self), dict);
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001029 }
1030 return Py_BuildValue("O()", Py_TYPE(self));
1031}
1032
Eddie Elizondo3368f3c2019-09-19 09:29:05 -07001033static PyMemberDef ast_type_members[] = {
1034 {"__dictoffset__", T_PYSSIZET, offsetof(AST_object, dict), READONLY},
1035 {NULL} /* Sentinel */
1036};
1037
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001038static PyMethodDef ast_type_methods[] = {
1039 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
1040 {NULL}
1041};
1042
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -07001043static PyGetSetDef ast_type_getsets[] = {
1044 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
1045 {NULL}
1046};
1047
Dino Viehlandac46eb42019-09-11 10:16:34 -07001048static PyType_Slot AST_type_slots[] = {
1049 {Py_tp_dealloc, ast_dealloc},
1050 {Py_tp_getattro, PyObject_GenericGetAttr},
1051 {Py_tp_setattro, PyObject_GenericSetAttr},
1052 {Py_tp_traverse, ast_traverse},
1053 {Py_tp_clear, ast_clear},
Eddie Elizondo3368f3c2019-09-19 09:29:05 -07001054 {Py_tp_members, ast_type_members},
Dino Viehlandac46eb42019-09-11 10:16:34 -07001055 {Py_tp_methods, ast_type_methods},
1056 {Py_tp_getset, ast_type_getsets},
1057 {Py_tp_init, ast_type_init},
1058 {Py_tp_alloc, PyType_GenericAlloc},
1059 {Py_tp_new, PyType_GenericNew},
Dino Viehlandac46eb42019-09-11 10:16:34 -07001060 {Py_tp_free, PyObject_GC_Del},
1061 {0, 0},
1062};
1063
1064static PyType_Spec AST_type_spec = {
Serhiy Storchakabace59d2020-03-22 20:33:34 +02001065 "ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -07001066 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +00001067 0,
Dino Viehlandac46eb42019-09-11 10:16:34 -07001068 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1069 AST_type_slots
Neal Norwitz207c9f32008-03-31 04:42:11 +00001070};
1071
Dino Viehlandac46eb42019-09-11 10:16:34 -07001072static PyObject *
Victor Stinner5cf47822020-11-02 22:03:28 +01001073make_type(struct ast_state *state, const char *type, PyObject* base,
Victor Stinner74419f02020-07-03 11:35:37 +02001074 const char* const* fields, int num_fields, const char *doc)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001075{
1076 PyObject *fnames, *result;
1077 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001078 fnames = PyTuple_New(num_fields);
1079 if (!fnames) return NULL;
1080 for (i = 0; i < num_fields; i++) {
Serhiy Storchaka43c97312019-09-10 13:02:30 +03001081 PyObject *field = PyUnicode_InternFromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001082 if (!field) {
1083 Py_DECREF(fnames);
1084 return NULL;
1085 }
1086 PyTuple_SET_ITEM(fnames, i, field);
1087 }
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001088 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOOOs}",
INADA Naokifc489082017-01-25 22:33:43 +09001089 type, base,
Victor Stinner74419f02020-07-03 11:35:37 +02001090 state->_fields, fnames,
1091 state->__module__,
1092 state->ast,
1093 state->__doc__, doc);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001094 Py_DECREF(fnames);
Dino Viehlandac46eb42019-09-11 10:16:34 -07001095 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001096}
1097
Serhiy Storchaka43c97312019-09-10 13:02:30 +03001098static int
Victor Stinner5cf47822020-11-02 22:03:28 +01001099add_attributes(struct ast_state *state, PyObject *type, const char * const *attrs, int num_fields)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00001100{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001101 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +00001102 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +00001103 if (!l)
1104 return 0;
1105 for (i = 0; i < num_fields; i++) {
Serhiy Storchaka43c97312019-09-10 13:02:30 +03001106 s = PyUnicode_InternFromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00001107 if (!s) {
1108 Py_DECREF(l);
1109 return 0;
1110 }
Neal Norwitz207c9f32008-03-31 04:42:11 +00001111 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00001112 }
Victor Stinner74419f02020-07-03 11:35:37 +02001113 result = PyObject_SetAttr(type, state->_attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001114 Py_DECREF(l);
1115 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00001116}
1117
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001118/* Conversion AST -> Python */
1119
Victor Stinner5cf47822020-11-02 22:03:28 +01001120static PyObject* ast2obj_list(struct ast_state *state, asdl_seq *seq, PyObject* (*func)(struct ast_state *state, void*))
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001121{
Benjamin Peterson77fa9372012-05-15 10:10:27 -07001122 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001123 PyObject *result = PyList_New(n);
1124 PyObject *value;
1125 if (!result)
1126 return NULL;
1127 for (i = 0; i < n; i++) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01001128 value = func(state, asdl_seq_GET_UNTYPED(seq, i));
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001129 if (!value) {
1130 Py_DECREF(result);
1131 return NULL;
1132 }
1133 PyList_SET_ITEM(result, i, value);
1134 }
1135 return result;
1136}
1137
Victor Stinner5cf47822020-11-02 22:03:28 +01001138static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), void *o)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001139{
1140 if (!o)
1141 o = Py_None;
1142 Py_INCREF((PyObject*)o);
1143 return (PyObject*)o;
1144}
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001145#define ast2obj_constant ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001146#define ast2obj_identifier ast2obj_object
1147#define ast2obj_string ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001148
Victor Stinner5cf47822020-11-02 22:03:28 +01001149static PyObject* ast2obj_int(struct ast_state *Py_UNUSED(state), long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00001150{
Christian Heimes217cfd12007-12-02 14:31:20 +00001151 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00001152}
1153
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001154/* Conversion Python -> AST */
1155
Victor Stinner5cf47822020-11-02 22:03:28 +01001156static int obj2ast_object(struct ast_state *Py_UNUSED(state), PyObject* obj, PyObject** out, PyArena* arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001157{
1158 if (obj == Py_None)
1159 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +02001160 if (obj) {
1161 if (PyArena_AddPyObject(arena, obj) < 0) {
1162 *out = NULL;
1163 return -1;
1164 }
1165 Py_INCREF(obj);
1166 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001167 *out = obj;
1168 return 0;
1169}
1170
Victor Stinner5cf47822020-11-02 22:03:28 +01001171static int obj2ast_constant(struct ast_state *Py_UNUSED(state), PyObject* obj, PyObject** out, PyArena* arena)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001172{
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001173 if (PyArena_AddPyObject(arena, obj) < 0) {
1174 *out = NULL;
1175 return -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001176 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001177 Py_INCREF(obj);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001178 *out = obj;
1179 return 0;
1180}
1181
Victor Stinner5cf47822020-11-02 22:03:28 +01001182static int obj2ast_identifier(struct ast_state *state, PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -05001183{
Benjamin Peterson180e6352011-07-22 11:09:07 -05001184 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
1185 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -05001186 return 1;
1187 }
Victor Stinner74419f02020-07-03 11:35:37 +02001188 return obj2ast_object(state, obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -05001189}
1190
Victor Stinner5cf47822020-11-02 22:03:28 +01001191static int obj2ast_string(struct ast_state *state, PyObject* obj, PyObject** out, PyArena* arena)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001192{
1193 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
1194 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
1195 return 1;
1196 }
Victor Stinner74419f02020-07-03 11:35:37 +02001197 return obj2ast_object(state, obj, out, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001198}
1199
Victor Stinner5cf47822020-11-02 22:03:28 +01001200static int obj2ast_int(struct ast_state* Py_UNUSED(state), PyObject* obj, int* out, PyArena* arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001201{
1202 int i;
1203 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +01001204 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001205 return 1;
1206 }
1207
Serhiy Storchaka56f6e762015-09-06 21:25:30 +03001208 i = _PyLong_AsInt(obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001209 if (i == -1 && PyErr_Occurred())
1210 return 1;
1211 *out = i;
1212 return 0;
1213}
1214
Victor Stinner5cf47822020-11-02 22:03:28 +01001215static int add_ast_fields(struct ast_state *state)
Benjamin Petersonce825f12008-10-24 23:11:02 +00001216{
Dino Viehlandac46eb42019-09-11 10:16:34 -07001217 PyObject *empty_tuple;
Benjamin Petersonce825f12008-10-24 23:11:02 +00001218 empty_tuple = PyTuple_New(0);
1219 if (!empty_tuple ||
Victor Stinner74419f02020-07-03 11:35:37 +02001220 PyObject_SetAttrString(state->AST_type, "_fields", empty_tuple) < 0 ||
1221 PyObject_SetAttrString(state->AST_type, "_attributes", empty_tuple) < 0) {
Benjamin Petersonce825f12008-10-24 23:11:02 +00001222 Py_XDECREF(empty_tuple);
1223 return -1;
1224 }
1225 Py_DECREF(empty_tuple);
1226 return 0;
1227}
1228
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001229
Victor Stinner5cf47822020-11-02 22:03:28 +01001230static int init_types(struct ast_state *state)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001231{
Dino Viehlandac46eb42019-09-11 10:16:34 -07001232 if (state->initialized) return 1;
Victor Stinner74419f02020-07-03 11:35:37 +02001233 if (init_identifiers(state) < 0) return 0;
Dino Viehlandac46eb42019-09-11 10:16:34 -07001234 state->AST_type = PyType_FromSpec(&AST_type_spec);
1235 if (!state->AST_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001236 if (add_ast_fields(state) < 0) return 0;
1237 state->mod_type = make_type(state, "mod", state->AST_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001238 "mod = Module(stmt* body, type_ignore* type_ignores)\n"
1239 " | Interactive(stmt* body)\n"
1240 " | Expression(expr body)\n"
1241 " | FunctionType(expr* argtypes, expr returns)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001242 if (!state->mod_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001243 if (!add_attributes(state, state->mod_type, NULL, 0)) return 0;
1244 state->Module_type = make_type(state, "Module", state->mod_type,
1245 Module_fields, 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001246 "Module(stmt* body, type_ignore* type_ignores)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001247 if (!state->Module_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001248 state->Interactive_type = make_type(state, "Interactive", state->mod_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001249 Interactive_fields, 1,
1250 "Interactive(stmt* body)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001251 if (!state->Interactive_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001252 state->Expression_type = make_type(state, "Expression", state->mod_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001253 Expression_fields, 1,
1254 "Expression(expr body)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001255 if (!state->Expression_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001256 state->FunctionType_type = make_type(state, "FunctionType",
1257 state->mod_type, FunctionType_fields,
1258 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001259 "FunctionType(expr* argtypes, expr returns)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001260 if (!state->FunctionType_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001261 state->stmt_type = make_type(state, "stmt", state->AST_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001262 "stmt = FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns, string? type_comment)\n"
1263 " | AsyncFunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns, string? type_comment)\n"
1264 " | ClassDef(identifier name, expr* bases, keyword* keywords, stmt* body, expr* decorator_list)\n"
1265 " | Return(expr? value)\n"
1266 " | Delete(expr* targets)\n"
1267 " | Assign(expr* targets, expr value, string? type_comment)\n"
1268 " | AugAssign(expr target, operator op, expr value)\n"
1269 " | AnnAssign(expr target, expr annotation, expr? value, int simple)\n"
1270 " | For(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)\n"
1271 " | AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)\n"
1272 " | While(expr test, stmt* body, stmt* orelse)\n"
1273 " | If(expr test, stmt* body, stmt* orelse)\n"
1274 " | With(withitem* items, stmt* body, string? type_comment)\n"
1275 " | AsyncWith(withitem* items, stmt* body, string? type_comment)\n"
1276 " | Raise(expr? exc, expr? cause)\n"
1277 " | Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)\n"
1278 " | Assert(expr test, expr? msg)\n"
1279 " | Import(alias* names)\n"
1280 " | ImportFrom(identifier? module, alias* names, int? level)\n"
1281 " | Global(identifier* names)\n"
1282 " | Nonlocal(identifier* names)\n"
1283 " | Expr(expr value)\n"
1284 " | Pass\n"
1285 " | Break\n"
1286 " | Continue");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001287 if (!state->stmt_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001288 if (!add_attributes(state, state->stmt_type, stmt_attributes, 4)) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001289 if (PyObject_SetAttr(state->stmt_type, state->end_lineno, Py_None) == -1)
1290 return 0;
1291 if (PyObject_SetAttr(state->stmt_type, state->end_col_offset, Py_None) ==
1292 -1)
1293 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001294 state->FunctionDef_type = make_type(state, "FunctionDef", state->stmt_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001295 FunctionDef_fields, 6,
1296 "FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns, string? type_comment)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001297 if (!state->FunctionDef_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001298 if (PyObject_SetAttr(state->FunctionDef_type, state->returns, Py_None) ==
1299 -1)
1300 return 0;
1301 if (PyObject_SetAttr(state->FunctionDef_type, state->type_comment, Py_None)
1302 == -1)
1303 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001304 state->AsyncFunctionDef_type = make_type(state, "AsyncFunctionDef",
Dino Viehlandac46eb42019-09-11 10:16:34 -07001305 state->stmt_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001306 AsyncFunctionDef_fields, 6,
1307 "AsyncFunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns, string? type_comment)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001308 if (!state->AsyncFunctionDef_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001309 if (PyObject_SetAttr(state->AsyncFunctionDef_type, state->returns, Py_None)
1310 == -1)
1311 return 0;
1312 if (PyObject_SetAttr(state->AsyncFunctionDef_type, state->type_comment,
1313 Py_None) == -1)
1314 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001315 state->ClassDef_type = make_type(state, "ClassDef", state->stmt_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001316 ClassDef_fields, 5,
1317 "ClassDef(identifier name, expr* bases, keyword* keywords, stmt* body, expr* decorator_list)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001318 if (!state->ClassDef_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001319 state->Return_type = make_type(state, "Return", state->stmt_type,
1320 Return_fields, 1,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001321 "Return(expr? value)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001322 if (!state->Return_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001323 if (PyObject_SetAttr(state->Return_type, state->value, Py_None) == -1)
1324 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001325 state->Delete_type = make_type(state, "Delete", state->stmt_type,
1326 Delete_fields, 1,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001327 "Delete(expr* targets)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001328 if (!state->Delete_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001329 state->Assign_type = make_type(state, "Assign", state->stmt_type,
1330 Assign_fields, 3,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001331 "Assign(expr* targets, expr value, string? type_comment)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001332 if (!state->Assign_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001333 if (PyObject_SetAttr(state->Assign_type, state->type_comment, Py_None) ==
1334 -1)
1335 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001336 state->AugAssign_type = make_type(state, "AugAssign", state->stmt_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001337 AugAssign_fields, 3,
1338 "AugAssign(expr target, operator op, expr value)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001339 if (!state->AugAssign_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001340 state->AnnAssign_type = make_type(state, "AnnAssign", state->stmt_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001341 AnnAssign_fields, 4,
1342 "AnnAssign(expr target, expr annotation, expr? value, int simple)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001343 if (!state->AnnAssign_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001344 if (PyObject_SetAttr(state->AnnAssign_type, state->value, Py_None) == -1)
1345 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001346 state->For_type = make_type(state, "For", state->stmt_type, For_fields, 5,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001347 "For(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001348 if (!state->For_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001349 if (PyObject_SetAttr(state->For_type, state->type_comment, Py_None) == -1)
1350 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001351 state->AsyncFor_type = make_type(state, "AsyncFor", state->stmt_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001352 AsyncFor_fields, 5,
1353 "AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001354 if (!state->AsyncFor_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001355 if (PyObject_SetAttr(state->AsyncFor_type, state->type_comment, Py_None) ==
1356 -1)
1357 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001358 state->While_type = make_type(state, "While", state->stmt_type,
1359 While_fields, 3,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001360 "While(expr test, stmt* body, stmt* orelse)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001361 if (!state->While_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001362 state->If_type = make_type(state, "If", state->stmt_type, If_fields, 3,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001363 "If(expr test, stmt* body, stmt* orelse)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001364 if (!state->If_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001365 state->With_type = make_type(state, "With", state->stmt_type, With_fields,
1366 3,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001367 "With(withitem* items, stmt* body, string? type_comment)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001368 if (!state->With_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001369 if (PyObject_SetAttr(state->With_type, state->type_comment, Py_None) == -1)
1370 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001371 state->AsyncWith_type = make_type(state, "AsyncWith", state->stmt_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001372 AsyncWith_fields, 3,
1373 "AsyncWith(withitem* items, stmt* body, string? type_comment)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001374 if (!state->AsyncWith_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001375 if (PyObject_SetAttr(state->AsyncWith_type, state->type_comment, Py_None)
1376 == -1)
1377 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001378 state->Raise_type = make_type(state, "Raise", state->stmt_type,
1379 Raise_fields, 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001380 "Raise(expr? exc, expr? cause)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001381 if (!state->Raise_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001382 if (PyObject_SetAttr(state->Raise_type, state->exc, Py_None) == -1)
1383 return 0;
1384 if (PyObject_SetAttr(state->Raise_type, state->cause, Py_None) == -1)
1385 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001386 state->Try_type = make_type(state, "Try", state->stmt_type, Try_fields, 4,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001387 "Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001388 if (!state->Try_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001389 state->Assert_type = make_type(state, "Assert", state->stmt_type,
1390 Assert_fields, 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001391 "Assert(expr test, expr? msg)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001392 if (!state->Assert_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001393 if (PyObject_SetAttr(state->Assert_type, state->msg, Py_None) == -1)
1394 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001395 state->Import_type = make_type(state, "Import", state->stmt_type,
1396 Import_fields, 1,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001397 "Import(alias* names)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001398 if (!state->Import_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001399 state->ImportFrom_type = make_type(state, "ImportFrom", state->stmt_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001400 ImportFrom_fields, 3,
1401 "ImportFrom(identifier? module, alias* names, int? level)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001402 if (!state->ImportFrom_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001403 if (PyObject_SetAttr(state->ImportFrom_type, state->module, Py_None) == -1)
1404 return 0;
1405 if (PyObject_SetAttr(state->ImportFrom_type, state->level, Py_None) == -1)
1406 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001407 state->Global_type = make_type(state, "Global", state->stmt_type,
1408 Global_fields, 1,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001409 "Global(identifier* names)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001410 if (!state->Global_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001411 state->Nonlocal_type = make_type(state, "Nonlocal", state->stmt_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001412 Nonlocal_fields, 1,
1413 "Nonlocal(identifier* names)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001414 if (!state->Nonlocal_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001415 state->Expr_type = make_type(state, "Expr", state->stmt_type, Expr_fields,
1416 1,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001417 "Expr(expr value)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001418 if (!state->Expr_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001419 state->Pass_type = make_type(state, "Pass", state->stmt_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001420 "Pass");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001421 if (!state->Pass_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001422 state->Break_type = make_type(state, "Break", state->stmt_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001423 "Break");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001424 if (!state->Break_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001425 state->Continue_type = make_type(state, "Continue", state->stmt_type, NULL,
1426 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001427 "Continue");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001428 if (!state->Continue_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001429 state->expr_type = make_type(state, "expr", state->AST_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001430 "expr = BoolOp(boolop op, expr* values)\n"
1431 " | NamedExpr(expr target, expr value)\n"
1432 " | BinOp(expr left, operator op, expr right)\n"
1433 " | UnaryOp(unaryop op, expr operand)\n"
1434 " | Lambda(arguments args, expr body)\n"
1435 " | IfExp(expr test, expr body, expr orelse)\n"
1436 " | Dict(expr* keys, expr* values)\n"
1437 " | Set(expr* elts)\n"
1438 " | ListComp(expr elt, comprehension* generators)\n"
1439 " | SetComp(expr elt, comprehension* generators)\n"
1440 " | DictComp(expr key, expr value, comprehension* generators)\n"
1441 " | GeneratorExp(expr elt, comprehension* generators)\n"
1442 " | Await(expr value)\n"
1443 " | Yield(expr? value)\n"
1444 " | YieldFrom(expr value)\n"
1445 " | Compare(expr left, cmpop* ops, expr* comparators)\n"
1446 " | Call(expr func, expr* args, keyword* keywords)\n"
1447 " | FormattedValue(expr value, int? conversion, expr? format_spec)\n"
1448 " | JoinedStr(expr* values)\n"
1449 " | Constant(constant value, string? kind)\n"
1450 " | Attribute(expr value, identifier attr, expr_context ctx)\n"
1451 " | Subscript(expr value, expr slice, expr_context ctx)\n"
1452 " | Starred(expr value, expr_context ctx)\n"
1453 " | Name(identifier id, expr_context ctx)\n"
1454 " | List(expr* elts, expr_context ctx)\n"
1455 " | Tuple(expr* elts, expr_context ctx)\n"
1456 " | Slice(expr? lower, expr? upper, expr? step)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001457 if (!state->expr_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001458 if (!add_attributes(state, state->expr_type, expr_attributes, 4)) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001459 if (PyObject_SetAttr(state->expr_type, state->end_lineno, Py_None) == -1)
1460 return 0;
1461 if (PyObject_SetAttr(state->expr_type, state->end_col_offset, Py_None) ==
1462 -1)
1463 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001464 state->BoolOp_type = make_type(state, "BoolOp", state->expr_type,
1465 BoolOp_fields, 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001466 "BoolOp(boolop op, expr* values)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001467 if (!state->BoolOp_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001468 state->NamedExpr_type = make_type(state, "NamedExpr", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001469 NamedExpr_fields, 2,
1470 "NamedExpr(expr target, expr value)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001471 if (!state->NamedExpr_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001472 state->BinOp_type = make_type(state, "BinOp", state->expr_type,
1473 BinOp_fields, 3,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001474 "BinOp(expr left, operator op, expr right)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001475 if (!state->BinOp_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001476 state->UnaryOp_type = make_type(state, "UnaryOp", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001477 UnaryOp_fields, 2,
1478 "UnaryOp(unaryop op, expr operand)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001479 if (!state->UnaryOp_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001480 state->Lambda_type = make_type(state, "Lambda", state->expr_type,
1481 Lambda_fields, 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001482 "Lambda(arguments args, expr body)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001483 if (!state->Lambda_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001484 state->IfExp_type = make_type(state, "IfExp", state->expr_type,
1485 IfExp_fields, 3,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001486 "IfExp(expr test, expr body, expr orelse)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001487 if (!state->IfExp_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001488 state->Dict_type = make_type(state, "Dict", state->expr_type, Dict_fields,
1489 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001490 "Dict(expr* keys, expr* values)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001491 if (!state->Dict_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001492 state->Set_type = make_type(state, "Set", state->expr_type, Set_fields, 1,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001493 "Set(expr* elts)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001494 if (!state->Set_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001495 state->ListComp_type = make_type(state, "ListComp", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001496 ListComp_fields, 2,
1497 "ListComp(expr elt, comprehension* generators)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001498 if (!state->ListComp_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001499 state->SetComp_type = make_type(state, "SetComp", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001500 SetComp_fields, 2,
1501 "SetComp(expr elt, comprehension* generators)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001502 if (!state->SetComp_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001503 state->DictComp_type = make_type(state, "DictComp", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001504 DictComp_fields, 3,
1505 "DictComp(expr key, expr value, comprehension* generators)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001506 if (!state->DictComp_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001507 state->GeneratorExp_type = make_type(state, "GeneratorExp",
1508 state->expr_type, GeneratorExp_fields,
1509 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001510 "GeneratorExp(expr elt, comprehension* generators)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001511 if (!state->GeneratorExp_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001512 state->Await_type = make_type(state, "Await", state->expr_type,
1513 Await_fields, 1,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001514 "Await(expr value)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001515 if (!state->Await_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001516 state->Yield_type = make_type(state, "Yield", state->expr_type,
1517 Yield_fields, 1,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001518 "Yield(expr? value)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001519 if (!state->Yield_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001520 if (PyObject_SetAttr(state->Yield_type, state->value, Py_None) == -1)
1521 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001522 state->YieldFrom_type = make_type(state, "YieldFrom", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001523 YieldFrom_fields, 1,
1524 "YieldFrom(expr value)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001525 if (!state->YieldFrom_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001526 state->Compare_type = make_type(state, "Compare", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001527 Compare_fields, 3,
1528 "Compare(expr left, cmpop* ops, expr* comparators)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001529 if (!state->Compare_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001530 state->Call_type = make_type(state, "Call", state->expr_type, Call_fields,
1531 3,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001532 "Call(expr func, expr* args, keyword* keywords)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001533 if (!state->Call_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001534 state->FormattedValue_type = make_type(state, "FormattedValue",
1535 state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001536 FormattedValue_fields, 3,
1537 "FormattedValue(expr value, int? conversion, expr? format_spec)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001538 if (!state->FormattedValue_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001539 if (PyObject_SetAttr(state->FormattedValue_type, state->conversion,
1540 Py_None) == -1)
1541 return 0;
1542 if (PyObject_SetAttr(state->FormattedValue_type, state->format_spec,
1543 Py_None) == -1)
1544 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001545 state->JoinedStr_type = make_type(state, "JoinedStr", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001546 JoinedStr_fields, 1,
1547 "JoinedStr(expr* values)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001548 if (!state->JoinedStr_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001549 state->Constant_type = make_type(state, "Constant", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001550 Constant_fields, 2,
1551 "Constant(constant value, string? kind)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001552 if (!state->Constant_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001553 if (PyObject_SetAttr(state->Constant_type, state->kind, Py_None) == -1)
1554 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001555 state->Attribute_type = make_type(state, "Attribute", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001556 Attribute_fields, 3,
1557 "Attribute(expr value, identifier attr, expr_context ctx)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001558 if (!state->Attribute_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001559 state->Subscript_type = make_type(state, "Subscript", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001560 Subscript_fields, 3,
1561 "Subscript(expr value, expr slice, expr_context ctx)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001562 if (!state->Subscript_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001563 state->Starred_type = make_type(state, "Starred", state->expr_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001564 Starred_fields, 2,
1565 "Starred(expr value, expr_context ctx)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001566 if (!state->Starred_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001567 state->Name_type = make_type(state, "Name", state->expr_type, Name_fields,
1568 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001569 "Name(identifier id, expr_context ctx)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001570 if (!state->Name_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001571 state->List_type = make_type(state, "List", state->expr_type, List_fields,
1572 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001573 "List(expr* elts, expr_context ctx)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001574 if (!state->List_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001575 state->Tuple_type = make_type(state, "Tuple", state->expr_type,
1576 Tuple_fields, 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001577 "Tuple(expr* elts, expr_context ctx)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001578 if (!state->Tuple_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001579 state->Slice_type = make_type(state, "Slice", state->expr_type,
1580 Slice_fields, 3,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001581 "Slice(expr? lower, expr? upper, expr? step)");
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001582 if (!state->Slice_type) return 0;
1583 if (PyObject_SetAttr(state->Slice_type, state->lower, Py_None) == -1)
1584 return 0;
1585 if (PyObject_SetAttr(state->Slice_type, state->upper, Py_None) == -1)
1586 return 0;
1587 if (PyObject_SetAttr(state->Slice_type, state->step, Py_None) == -1)
1588 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001589 state->expr_context_type = make_type(state, "expr_context",
1590 state->AST_type, NULL, 0,
Serhiy Storchaka6b975982020-03-17 23:41:08 +02001591 "expr_context = Load | Store | Del");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001592 if (!state->expr_context_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001593 if (!add_attributes(state, state->expr_context_type, NULL, 0)) return 0;
1594 state->Load_type = make_type(state, "Load", state->expr_context_type, NULL,
1595 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001596 "Load");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001597 if (!state->Load_type) return 0;
1598 state->Load_singleton = PyType_GenericNew((PyTypeObject *)state->Load_type,
1599 NULL, NULL);
1600 if (!state->Load_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001601 state->Store_type = make_type(state, "Store", state->expr_context_type,
1602 NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001603 "Store");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001604 if (!state->Store_type) return 0;
1605 state->Store_singleton = PyType_GenericNew((PyTypeObject
1606 *)state->Store_type, NULL, NULL);
1607 if (!state->Store_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001608 state->Del_type = make_type(state, "Del", state->expr_context_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001609 "Del");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001610 if (!state->Del_type) return 0;
1611 state->Del_singleton = PyType_GenericNew((PyTypeObject *)state->Del_type,
1612 NULL, NULL);
1613 if (!state->Del_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001614 state->boolop_type = make_type(state, "boolop", state->AST_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001615 "boolop = And | Or");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001616 if (!state->boolop_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001617 if (!add_attributes(state, state->boolop_type, NULL, 0)) return 0;
1618 state->And_type = make_type(state, "And", state->boolop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001619 "And");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001620 if (!state->And_type) return 0;
1621 state->And_singleton = PyType_GenericNew((PyTypeObject *)state->And_type,
1622 NULL, NULL);
1623 if (!state->And_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001624 state->Or_type = make_type(state, "Or", state->boolop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001625 "Or");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001626 if (!state->Or_type) return 0;
1627 state->Or_singleton = PyType_GenericNew((PyTypeObject *)state->Or_type,
1628 NULL, NULL);
1629 if (!state->Or_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001630 state->operator_type = make_type(state, "operator", state->AST_type, NULL,
1631 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001632 "operator = Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift | RShift | BitOr | BitXor | BitAnd | FloorDiv");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001633 if (!state->operator_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001634 if (!add_attributes(state, state->operator_type, NULL, 0)) return 0;
1635 state->Add_type = make_type(state, "Add", state->operator_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001636 "Add");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001637 if (!state->Add_type) return 0;
1638 state->Add_singleton = PyType_GenericNew((PyTypeObject *)state->Add_type,
1639 NULL, NULL);
1640 if (!state->Add_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001641 state->Sub_type = make_type(state, "Sub", state->operator_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001642 "Sub");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001643 if (!state->Sub_type) return 0;
1644 state->Sub_singleton = PyType_GenericNew((PyTypeObject *)state->Sub_type,
1645 NULL, NULL);
1646 if (!state->Sub_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001647 state->Mult_type = make_type(state, "Mult", state->operator_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001648 "Mult");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001649 if (!state->Mult_type) return 0;
1650 state->Mult_singleton = PyType_GenericNew((PyTypeObject *)state->Mult_type,
1651 NULL, NULL);
1652 if (!state->Mult_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001653 state->MatMult_type = make_type(state, "MatMult", state->operator_type,
1654 NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001655 "MatMult");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001656 if (!state->MatMult_type) return 0;
1657 state->MatMult_singleton = PyType_GenericNew((PyTypeObject
1658 *)state->MatMult_type, NULL,
1659 NULL);
1660 if (!state->MatMult_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001661 state->Div_type = make_type(state, "Div", state->operator_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001662 "Div");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001663 if (!state->Div_type) return 0;
1664 state->Div_singleton = PyType_GenericNew((PyTypeObject *)state->Div_type,
1665 NULL, NULL);
1666 if (!state->Div_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001667 state->Mod_type = make_type(state, "Mod", state->operator_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001668 "Mod");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001669 if (!state->Mod_type) return 0;
1670 state->Mod_singleton = PyType_GenericNew((PyTypeObject *)state->Mod_type,
1671 NULL, NULL);
1672 if (!state->Mod_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001673 state->Pow_type = make_type(state, "Pow", state->operator_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001674 "Pow");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001675 if (!state->Pow_type) return 0;
1676 state->Pow_singleton = PyType_GenericNew((PyTypeObject *)state->Pow_type,
1677 NULL, NULL);
1678 if (!state->Pow_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001679 state->LShift_type = make_type(state, "LShift", state->operator_type, NULL,
1680 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001681 "LShift");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001682 if (!state->LShift_type) return 0;
1683 state->LShift_singleton = PyType_GenericNew((PyTypeObject
1684 *)state->LShift_type, NULL,
1685 NULL);
1686 if (!state->LShift_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001687 state->RShift_type = make_type(state, "RShift", state->operator_type, NULL,
1688 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001689 "RShift");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001690 if (!state->RShift_type) return 0;
1691 state->RShift_singleton = PyType_GenericNew((PyTypeObject
1692 *)state->RShift_type, NULL,
1693 NULL);
1694 if (!state->RShift_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001695 state->BitOr_type = make_type(state, "BitOr", state->operator_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001696 "BitOr");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001697 if (!state->BitOr_type) return 0;
1698 state->BitOr_singleton = PyType_GenericNew((PyTypeObject
1699 *)state->BitOr_type, NULL, NULL);
1700 if (!state->BitOr_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001701 state->BitXor_type = make_type(state, "BitXor", state->operator_type, NULL,
1702 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001703 "BitXor");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001704 if (!state->BitXor_type) return 0;
1705 state->BitXor_singleton = PyType_GenericNew((PyTypeObject
1706 *)state->BitXor_type, NULL,
1707 NULL);
1708 if (!state->BitXor_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001709 state->BitAnd_type = make_type(state, "BitAnd", state->operator_type, NULL,
1710 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001711 "BitAnd");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001712 if (!state->BitAnd_type) return 0;
1713 state->BitAnd_singleton = PyType_GenericNew((PyTypeObject
1714 *)state->BitAnd_type, NULL,
1715 NULL);
1716 if (!state->BitAnd_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001717 state->FloorDiv_type = make_type(state, "FloorDiv", state->operator_type,
1718 NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001719 "FloorDiv");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001720 if (!state->FloorDiv_type) return 0;
1721 state->FloorDiv_singleton = PyType_GenericNew((PyTypeObject
1722 *)state->FloorDiv_type, NULL,
1723 NULL);
1724 if (!state->FloorDiv_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001725 state->unaryop_type = make_type(state, "unaryop", state->AST_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001726 "unaryop = Invert | Not | UAdd | USub");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001727 if (!state->unaryop_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001728 if (!add_attributes(state, state->unaryop_type, NULL, 0)) return 0;
1729 state->Invert_type = make_type(state, "Invert", state->unaryop_type, NULL,
1730 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001731 "Invert");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001732 if (!state->Invert_type) return 0;
1733 state->Invert_singleton = PyType_GenericNew((PyTypeObject
1734 *)state->Invert_type, NULL,
1735 NULL);
1736 if (!state->Invert_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001737 state->Not_type = make_type(state, "Not", state->unaryop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001738 "Not");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001739 if (!state->Not_type) return 0;
1740 state->Not_singleton = PyType_GenericNew((PyTypeObject *)state->Not_type,
1741 NULL, NULL);
1742 if (!state->Not_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001743 state->UAdd_type = make_type(state, "UAdd", state->unaryop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001744 "UAdd");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001745 if (!state->UAdd_type) return 0;
1746 state->UAdd_singleton = PyType_GenericNew((PyTypeObject *)state->UAdd_type,
1747 NULL, NULL);
1748 if (!state->UAdd_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001749 state->USub_type = make_type(state, "USub", state->unaryop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001750 "USub");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001751 if (!state->USub_type) return 0;
1752 state->USub_singleton = PyType_GenericNew((PyTypeObject *)state->USub_type,
1753 NULL, NULL);
1754 if (!state->USub_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001755 state->cmpop_type = make_type(state, "cmpop", state->AST_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001756 "cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001757 if (!state->cmpop_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001758 if (!add_attributes(state, state->cmpop_type, NULL, 0)) return 0;
1759 state->Eq_type = make_type(state, "Eq", state->cmpop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001760 "Eq");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001761 if (!state->Eq_type) return 0;
1762 state->Eq_singleton = PyType_GenericNew((PyTypeObject *)state->Eq_type,
1763 NULL, NULL);
1764 if (!state->Eq_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001765 state->NotEq_type = make_type(state, "NotEq", state->cmpop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001766 "NotEq");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001767 if (!state->NotEq_type) return 0;
1768 state->NotEq_singleton = PyType_GenericNew((PyTypeObject
1769 *)state->NotEq_type, NULL, NULL);
1770 if (!state->NotEq_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001771 state->Lt_type = make_type(state, "Lt", state->cmpop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001772 "Lt");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001773 if (!state->Lt_type) return 0;
1774 state->Lt_singleton = PyType_GenericNew((PyTypeObject *)state->Lt_type,
1775 NULL, NULL);
1776 if (!state->Lt_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001777 state->LtE_type = make_type(state, "LtE", state->cmpop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001778 "LtE");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001779 if (!state->LtE_type) return 0;
1780 state->LtE_singleton = PyType_GenericNew((PyTypeObject *)state->LtE_type,
1781 NULL, NULL);
1782 if (!state->LtE_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001783 state->Gt_type = make_type(state, "Gt", state->cmpop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001784 "Gt");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001785 if (!state->Gt_type) return 0;
1786 state->Gt_singleton = PyType_GenericNew((PyTypeObject *)state->Gt_type,
1787 NULL, NULL);
1788 if (!state->Gt_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001789 state->GtE_type = make_type(state, "GtE", state->cmpop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001790 "GtE");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001791 if (!state->GtE_type) return 0;
1792 state->GtE_singleton = PyType_GenericNew((PyTypeObject *)state->GtE_type,
1793 NULL, NULL);
1794 if (!state->GtE_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001795 state->Is_type = make_type(state, "Is", state->cmpop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001796 "Is");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001797 if (!state->Is_type) return 0;
1798 state->Is_singleton = PyType_GenericNew((PyTypeObject *)state->Is_type,
1799 NULL, NULL);
1800 if (!state->Is_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001801 state->IsNot_type = make_type(state, "IsNot", state->cmpop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001802 "IsNot");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001803 if (!state->IsNot_type) return 0;
1804 state->IsNot_singleton = PyType_GenericNew((PyTypeObject
1805 *)state->IsNot_type, NULL, NULL);
1806 if (!state->IsNot_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001807 state->In_type = make_type(state, "In", state->cmpop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001808 "In");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001809 if (!state->In_type) return 0;
1810 state->In_singleton = PyType_GenericNew((PyTypeObject *)state->In_type,
1811 NULL, NULL);
1812 if (!state->In_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001813 state->NotIn_type = make_type(state, "NotIn", state->cmpop_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001814 "NotIn");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001815 if (!state->NotIn_type) return 0;
1816 state->NotIn_singleton = PyType_GenericNew((PyTypeObject
1817 *)state->NotIn_type, NULL, NULL);
1818 if (!state->NotIn_singleton) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001819 state->comprehension_type = make_type(state, "comprehension",
1820 state->AST_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001821 comprehension_fields, 4,
1822 "comprehension(expr target, expr iter, expr* ifs, int is_async)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001823 if (!state->comprehension_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001824 if (!add_attributes(state, state->comprehension_type, NULL, 0)) return 0;
1825 state->excepthandler_type = make_type(state, "excepthandler",
1826 state->AST_type, NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001827 "excepthandler = ExceptHandler(expr? type, identifier? name, stmt* body)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001828 if (!state->excepthandler_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001829 if (!add_attributes(state, state->excepthandler_type,
1830 excepthandler_attributes, 4)) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001831 if (PyObject_SetAttr(state->excepthandler_type, state->end_lineno, Py_None)
1832 == -1)
1833 return 0;
1834 if (PyObject_SetAttr(state->excepthandler_type, state->end_col_offset,
1835 Py_None) == -1)
1836 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001837 state->ExceptHandler_type = make_type(state, "ExceptHandler",
Dino Viehlandac46eb42019-09-11 10:16:34 -07001838 state->excepthandler_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001839 ExceptHandler_fields, 3,
1840 "ExceptHandler(expr? type, identifier? name, stmt* body)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001841 if (!state->ExceptHandler_type) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001842 if (PyObject_SetAttr(state->ExceptHandler_type, state->type, Py_None) == -1)
1843 return 0;
1844 if (PyObject_SetAttr(state->ExceptHandler_type, state->name, Py_None) == -1)
1845 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001846 state->arguments_type = make_type(state, "arguments", state->AST_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001847 arguments_fields, 7,
1848 "arguments(arg* posonlyargs, arg* args, arg? vararg, arg* kwonlyargs, expr* kw_defaults, arg? kwarg, expr* defaults)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001849 if (!state->arguments_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001850 if (!add_attributes(state, state->arguments_type, NULL, 0)) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001851 if (PyObject_SetAttr(state->arguments_type, state->vararg, Py_None) == -1)
1852 return 0;
1853 if (PyObject_SetAttr(state->arguments_type, state->kwarg, Py_None) == -1)
1854 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001855 state->arg_type = make_type(state, "arg", state->AST_type, arg_fields, 3,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001856 "arg(identifier arg, expr? annotation, string? type_comment)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001857 if (!state->arg_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001858 if (!add_attributes(state, state->arg_type, arg_attributes, 4)) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001859 if (PyObject_SetAttr(state->arg_type, state->annotation, Py_None) == -1)
1860 return 0;
1861 if (PyObject_SetAttr(state->arg_type, state->type_comment, Py_None) == -1)
1862 return 0;
1863 if (PyObject_SetAttr(state->arg_type, state->end_lineno, Py_None) == -1)
1864 return 0;
1865 if (PyObject_SetAttr(state->arg_type, state->end_col_offset, Py_None) == -1)
1866 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001867 state->keyword_type = make_type(state, "keyword", state->AST_type,
1868 keyword_fields, 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001869 "keyword(identifier? arg, expr value)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001870 if (!state->keyword_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001871 if (!add_attributes(state, state->keyword_type, keyword_attributes, 4))
1872 return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001873 if (PyObject_SetAttr(state->keyword_type, state->arg, Py_None) == -1)
1874 return 0;
Pablo Galindo168660b2020-04-02 00:47:39 +01001875 if (PyObject_SetAttr(state->keyword_type, state->end_lineno, Py_None) == -1)
1876 return 0;
1877 if (PyObject_SetAttr(state->keyword_type, state->end_col_offset, Py_None)
1878 == -1)
1879 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001880 state->alias_type = make_type(state, "alias", state->AST_type,
1881 alias_fields, 2,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001882 "alias(identifier name, identifier? asname)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001883 if (!state->alias_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001884 if (!add_attributes(state, state->alias_type, NULL, 0)) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001885 if (PyObject_SetAttr(state->alias_type, state->asname, Py_None) == -1)
1886 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001887 state->withitem_type = make_type(state, "withitem", state->AST_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001888 withitem_fields, 2,
1889 "withitem(expr context_expr, expr? optional_vars)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001890 if (!state->withitem_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001891 if (!add_attributes(state, state->withitem_type, NULL, 0)) return 0;
Serhiy Storchakab7e95252020-03-10 00:07:47 +02001892 if (PyObject_SetAttr(state->withitem_type, state->optional_vars, Py_None)
1893 == -1)
1894 return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001895 state->type_ignore_type = make_type(state, "type_ignore", state->AST_type,
1896 NULL, 0,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001897 "type_ignore = TypeIgnore(int lineno, string tag)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001898 if (!state->type_ignore_type) return 0;
Victor Stinner74419f02020-07-03 11:35:37 +02001899 if (!add_attributes(state, state->type_ignore_type, NULL, 0)) return 0;
1900 state->TypeIgnore_type = make_type(state, "TypeIgnore",
1901 state->type_ignore_type,
Batuhan Taşkaya4ab362c2020-03-16 11:12:53 +03001902 TypeIgnore_fields, 2,
1903 "TypeIgnore(int lineno, string tag)");
Dino Viehlandac46eb42019-09-11 10:16:34 -07001904 if (!state->TypeIgnore_type) return 0;
1905 state->initialized = 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001906 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001907}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001908
Victor Stinner5cf47822020-11-02 22:03:28 +01001909static int obj2ast_mod(struct ast_state *state, PyObject* obj, mod_ty* out,
Victor Stinner74419f02020-07-03 11:35:37 +02001910 PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001911static int obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out,
Victor Stinner74419f02020-07-03 11:35:37 +02001912 PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001913static int obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out,
Victor Stinner74419f02020-07-03 11:35:37 +02001914 PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001915static int obj2ast_expr_context(struct ast_state *state, PyObject* obj,
Victor Stinner74419f02020-07-03 11:35:37 +02001916 expr_context_ty* out, PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001917static int obj2ast_boolop(struct ast_state *state, PyObject* obj, boolop_ty*
1918 out, PyArena* arena);
1919static int obj2ast_operator(struct ast_state *state, PyObject* obj,
1920 operator_ty* out, PyArena* arena);
1921static int obj2ast_unaryop(struct ast_state *state, PyObject* obj, unaryop_ty*
Victor Stinner74419f02020-07-03 11:35:37 +02001922 out, PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001923static int obj2ast_cmpop(struct ast_state *state, PyObject* obj, cmpop_ty* out,
Victor Stinner74419f02020-07-03 11:35:37 +02001924 PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001925static int obj2ast_comprehension(struct ast_state *state, PyObject* obj,
Victor Stinner74419f02020-07-03 11:35:37 +02001926 comprehension_ty* out, PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001927static int obj2ast_excepthandler(struct ast_state *state, PyObject* obj,
Victor Stinner74419f02020-07-03 11:35:37 +02001928 excepthandler_ty* out, PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001929static int obj2ast_arguments(struct ast_state *state, PyObject* obj,
Victor Stinner74419f02020-07-03 11:35:37 +02001930 arguments_ty* out, PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001931static int obj2ast_arg(struct ast_state *state, PyObject* obj, arg_ty* out,
Victor Stinner74419f02020-07-03 11:35:37 +02001932 PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001933static int obj2ast_keyword(struct ast_state *state, PyObject* obj, keyword_ty*
Victor Stinner74419f02020-07-03 11:35:37 +02001934 out, PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001935static int obj2ast_alias(struct ast_state *state, PyObject* obj, alias_ty* out,
Victor Stinner74419f02020-07-03 11:35:37 +02001936 PyArena* arena);
Victor Stinner5cf47822020-11-02 22:03:28 +01001937static int obj2ast_withitem(struct ast_state *state, PyObject* obj,
1938 withitem_ty* out, PyArena* arena);
1939static int obj2ast_type_ignore(struct ast_state *state, PyObject* obj,
Victor Stinner74419f02020-07-03 11:35:37 +02001940 type_ignore_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001941
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942mod_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01001943Module(asdl_stmt_seq * body, asdl_type_ignore_seq * type_ignores, PyArena
1944 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001946 mod_ty p;
1947 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1948 if (!p)
1949 return NULL;
1950 p->kind = Module_kind;
1951 p->v.Module.body = body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001952 p->v.Module.type_ignores = type_ignores;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001953 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954}
1955
1956mod_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01001957Interactive(asdl_stmt_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001959 mod_ty p;
1960 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1961 if (!p)
1962 return NULL;
1963 p->kind = Interactive_kind;
1964 p->v.Interactive.body = body;
1965 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966}
1967
1968mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001969Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001971 mod_ty p;
1972 if (!body) {
1973 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03001974 "field 'body' is required for Expression");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001975 return NULL;
1976 }
1977 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1978 if (!p)
1979 return NULL;
1980 p->kind = Expression_kind;
1981 p->v.Expression.body = body;
1982 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983}
1984
1985mod_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01001986FunctionType(asdl_expr_seq * argtypes, expr_ty returns, PyArena *arena)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001987{
1988 mod_ty p;
1989 if (!returns) {
1990 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03001991 "field 'returns' is required for FunctionType");
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001992 return NULL;
1993 }
1994 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1995 if (!p)
1996 return NULL;
1997 p->kind = FunctionType_kind;
1998 p->v.FunctionType.argtypes = argtypes;
1999 p->v.FunctionType.returns = returns;
2000 return p;
2001}
2002
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002004FunctionDef(identifier name, arguments_ty args, asdl_stmt_seq * body,
2005 asdl_expr_seq * decorator_list, expr_ty returns, string
2006 type_comment, int lineno, int col_offset, int end_lineno, int
2007 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002009 stmt_ty p;
2010 if (!name) {
2011 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002012 "field 'name' is required for FunctionDef");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002013 return NULL;
2014 }
2015 if (!args) {
2016 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002017 "field 'args' is required for FunctionDef");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002018 return NULL;
2019 }
2020 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2021 if (!p)
2022 return NULL;
2023 p->kind = FunctionDef_kind;
2024 p->v.FunctionDef.name = name;
2025 p->v.FunctionDef.args = args;
2026 p->v.FunctionDef.body = body;
2027 p->v.FunctionDef.decorator_list = decorator_list;
2028 p->v.FunctionDef.returns = returns;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002029 p->v.FunctionDef.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002030 p->lineno = lineno;
2031 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002032 p->end_lineno = end_lineno;
2033 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002034 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035}
2036
2037stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002038AsyncFunctionDef(identifier name, arguments_ty args, asdl_stmt_seq * body,
2039 asdl_expr_seq * decorator_list, expr_ty returns, string
2040 type_comment, int lineno, int col_offset, int end_lineno, int
2041 end_col_offset, PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04002042{
2043 stmt_ty p;
2044 if (!name) {
2045 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002046 "field 'name' is required for AsyncFunctionDef");
Yury Selivanov75445082015-05-11 22:57:16 -04002047 return NULL;
2048 }
2049 if (!args) {
2050 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002051 "field 'args' is required for AsyncFunctionDef");
Yury Selivanov75445082015-05-11 22:57:16 -04002052 return NULL;
2053 }
2054 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2055 if (!p)
2056 return NULL;
2057 p->kind = AsyncFunctionDef_kind;
2058 p->v.AsyncFunctionDef.name = name;
2059 p->v.AsyncFunctionDef.args = args;
2060 p->v.AsyncFunctionDef.body = body;
2061 p->v.AsyncFunctionDef.decorator_list = decorator_list;
2062 p->v.AsyncFunctionDef.returns = returns;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002063 p->v.AsyncFunctionDef.type_comment = type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04002064 p->lineno = lineno;
2065 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002066 p->end_lineno = end_lineno;
2067 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04002068 return p;
2069}
2070
2071stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002072ClassDef(identifier name, asdl_expr_seq * bases, asdl_keyword_seq * keywords,
2073 asdl_stmt_seq * body, asdl_expr_seq * decorator_list, int lineno, int
2074 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002076 stmt_ty p;
2077 if (!name) {
2078 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002079 "field 'name' is required for ClassDef");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002080 return NULL;
2081 }
2082 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2083 if (!p)
2084 return NULL;
2085 p->kind = ClassDef_kind;
2086 p->v.ClassDef.name = name;
2087 p->v.ClassDef.bases = bases;
2088 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002089 p->v.ClassDef.body = body;
2090 p->v.ClassDef.decorator_list = decorator_list;
2091 p->lineno = lineno;
2092 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002093 p->end_lineno = end_lineno;
2094 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002095 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096}
2097
2098stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002099Return(expr_ty value, int lineno, int col_offset, int end_lineno, int
2100 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002102 stmt_ty p;
2103 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2104 if (!p)
2105 return NULL;
2106 p->kind = Return_kind;
2107 p->v.Return.value = value;
2108 p->lineno = lineno;
2109 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002110 p->end_lineno = end_lineno;
2111 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002112 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113}
2114
2115stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002116Delete(asdl_expr_seq * targets, int lineno, int col_offset, int end_lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002117 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002119 stmt_ty p;
2120 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2121 if (!p)
2122 return NULL;
2123 p->kind = Delete_kind;
2124 p->v.Delete.targets = targets;
2125 p->lineno = lineno;
2126 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002127 p->end_lineno = end_lineno;
2128 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002129 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130}
2131
2132stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002133Assign(asdl_expr_seq * targets, expr_ty value, string type_comment, int lineno,
2134 int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002136 stmt_ty p;
2137 if (!value) {
2138 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002139 "field 'value' is required for Assign");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002140 return NULL;
2141 }
2142 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2143 if (!p)
2144 return NULL;
2145 p->kind = Assign_kind;
2146 p->v.Assign.targets = targets;
2147 p->v.Assign.value = value;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002148 p->v.Assign.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002149 p->lineno = lineno;
2150 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002151 p->end_lineno = end_lineno;
2152 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002153 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154}
2155
2156stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002157AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002158 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002160 stmt_ty p;
2161 if (!target) {
2162 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002163 "field 'target' is required for AugAssign");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002164 return NULL;
2165 }
2166 if (!op) {
2167 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002168 "field 'op' is required for AugAssign");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002169 return NULL;
2170 }
2171 if (!value) {
2172 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002173 "field 'value' is required for AugAssign");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002174 return NULL;
2175 }
2176 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2177 if (!p)
2178 return NULL;
2179 p->kind = AugAssign_kind;
2180 p->v.AugAssign.target = target;
2181 p->v.AugAssign.op = op;
2182 p->v.AugAssign.value = value;
2183 p->lineno = lineno;
2184 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002185 p->end_lineno = end_lineno;
2186 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002187 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188}
2189
2190stmt_ty
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002191AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002192 lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
2193 *arena)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002194{
2195 stmt_ty p;
2196 if (!target) {
2197 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002198 "field 'target' is required for AnnAssign");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002199 return NULL;
2200 }
2201 if (!annotation) {
2202 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002203 "field 'annotation' is required for AnnAssign");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002204 return NULL;
2205 }
2206 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2207 if (!p)
2208 return NULL;
2209 p->kind = AnnAssign_kind;
2210 p->v.AnnAssign.target = target;
2211 p->v.AnnAssign.annotation = annotation;
2212 p->v.AnnAssign.value = value;
2213 p->v.AnnAssign.simple = simple;
2214 p->lineno = lineno;
2215 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002216 p->end_lineno = end_lineno;
2217 p->end_col_offset = end_col_offset;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002218 return p;
2219}
2220
2221stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002222For(expr_ty target, expr_ty iter, asdl_stmt_seq * body, asdl_stmt_seq * orelse,
2223 string type_comment, int lineno, int col_offset, int end_lineno, int
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002224 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002226 stmt_ty p;
2227 if (!target) {
2228 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002229 "field 'target' is required for For");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002230 return NULL;
2231 }
2232 if (!iter) {
2233 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002234 "field 'iter' is required for For");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002235 return NULL;
2236 }
2237 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2238 if (!p)
2239 return NULL;
2240 p->kind = For_kind;
2241 p->v.For.target = target;
2242 p->v.For.iter = iter;
2243 p->v.For.body = body;
2244 p->v.For.orelse = orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002245 p->v.For.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002246 p->lineno = lineno;
2247 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002248 p->end_lineno = end_lineno;
2249 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002250 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251}
2252
2253stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002254AsyncFor(expr_ty target, expr_ty iter, asdl_stmt_seq * body, asdl_stmt_seq *
2255 orelse, string type_comment, int lineno, int col_offset, int
2256 end_lineno, int end_col_offset, PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04002257{
2258 stmt_ty p;
2259 if (!target) {
2260 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002261 "field 'target' is required for AsyncFor");
Yury Selivanov75445082015-05-11 22:57:16 -04002262 return NULL;
2263 }
2264 if (!iter) {
2265 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002266 "field 'iter' is required for AsyncFor");
Yury Selivanov75445082015-05-11 22:57:16 -04002267 return NULL;
2268 }
2269 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2270 if (!p)
2271 return NULL;
2272 p->kind = AsyncFor_kind;
2273 p->v.AsyncFor.target = target;
2274 p->v.AsyncFor.iter = iter;
2275 p->v.AsyncFor.body = body;
2276 p->v.AsyncFor.orelse = orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002277 p->v.AsyncFor.type_comment = type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04002278 p->lineno = lineno;
2279 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002280 p->end_lineno = end_lineno;
2281 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04002282 return p;
2283}
2284
2285stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002286While(expr_ty test, asdl_stmt_seq * body, asdl_stmt_seq * orelse, int lineno,
2287 int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002289 stmt_ty p;
2290 if (!test) {
2291 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002292 "field 'test' is required for While");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002293 return NULL;
2294 }
2295 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2296 if (!p)
2297 return NULL;
2298 p->kind = While_kind;
2299 p->v.While.test = test;
2300 p->v.While.body = body;
2301 p->v.While.orelse = orelse;
2302 p->lineno = lineno;
2303 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002304 p->end_lineno = end_lineno;
2305 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002306 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307}
2308
2309stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002310If(expr_ty test, asdl_stmt_seq * body, asdl_stmt_seq * orelse, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002311 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002313 stmt_ty p;
2314 if (!test) {
2315 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002316 "field 'test' is required for If");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002317 return NULL;
2318 }
2319 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2320 if (!p)
2321 return NULL;
2322 p->kind = If_kind;
2323 p->v.If.test = test;
2324 p->v.If.body = body;
2325 p->v.If.orelse = orelse;
2326 p->lineno = lineno;
2327 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002328 p->end_lineno = end_lineno;
2329 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002330 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331}
2332
2333stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002334With(asdl_withitem_seq * items, asdl_stmt_seq * body, string type_comment, int
2335 lineno, int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002336{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002337 stmt_ty p;
2338 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2339 if (!p)
2340 return NULL;
2341 p->kind = With_kind;
2342 p->v.With.items = items;
2343 p->v.With.body = body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002344 p->v.With.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002345 p->lineno = lineno;
2346 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002347 p->end_lineno = end_lineno;
2348 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002349 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002350}
2351
2352stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002353AsyncWith(asdl_withitem_seq * items, asdl_stmt_seq * body, string type_comment,
2354 int lineno, int col_offset, int end_lineno, int end_col_offset,
2355 PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04002356{
2357 stmt_ty p;
2358 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2359 if (!p)
2360 return NULL;
2361 p->kind = AsyncWith_kind;
2362 p->v.AsyncWith.items = items;
2363 p->v.AsyncWith.body = body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002364 p->v.AsyncWith.type_comment = type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04002365 p->lineno = lineno;
2366 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002367 p->end_lineno = end_lineno;
2368 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04002369 return p;
2370}
2371
2372stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002373Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, int end_lineno,
2374 int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002376 stmt_ty p;
2377 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2378 if (!p)
2379 return NULL;
2380 p->kind = Raise_kind;
2381 p->v.Raise.exc = exc;
2382 p->v.Raise.cause = cause;
2383 p->lineno = lineno;
2384 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002385 p->end_lineno = end_lineno;
2386 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002387 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388}
2389
2390stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002391Try(asdl_stmt_seq * body, asdl_excepthandler_seq * handlers, asdl_stmt_seq *
2392 orelse, asdl_stmt_seq * finalbody, int lineno, int col_offset, int
2393 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002395 stmt_ty p;
2396 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2397 if (!p)
2398 return NULL;
2399 p->kind = Try_kind;
2400 p->v.Try.body = body;
2401 p->v.Try.handlers = handlers;
2402 p->v.Try.orelse = orelse;
2403 p->v.Try.finalbody = finalbody;
2404 p->lineno = lineno;
2405 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002406 p->end_lineno = end_lineno;
2407 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002408 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409}
2410
2411stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002412Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, int end_lineno,
2413 int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002415 stmt_ty p;
2416 if (!test) {
2417 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002418 "field 'test' is required for Assert");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002419 return NULL;
2420 }
2421 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2422 if (!p)
2423 return NULL;
2424 p->kind = Assert_kind;
2425 p->v.Assert.test = test;
2426 p->v.Assert.msg = msg;
2427 p->lineno = lineno;
2428 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002429 p->end_lineno = end_lineno;
2430 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002431 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432}
2433
2434stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002435Import(asdl_alias_seq * names, int lineno, int col_offset, int end_lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002436 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002438 stmt_ty p;
2439 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2440 if (!p)
2441 return NULL;
2442 p->kind = Import_kind;
2443 p->v.Import.names = names;
2444 p->lineno = lineno;
2445 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002446 p->end_lineno = end_lineno;
2447 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002448 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449}
2450
2451stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002452ImportFrom(identifier module, asdl_alias_seq * names, int level, int lineno,
2453 int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002455 stmt_ty p;
2456 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2457 if (!p)
2458 return NULL;
2459 p->kind = ImportFrom_kind;
2460 p->v.ImportFrom.module = module;
2461 p->v.ImportFrom.names = names;
2462 p->v.ImportFrom.level = level;
2463 p->lineno = lineno;
2464 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002465 p->end_lineno = end_lineno;
2466 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002467 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468}
2469
2470stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002471Global(asdl_identifier_seq * names, int lineno, int col_offset, int end_lineno,
2472 int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002474 stmt_ty p;
2475 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2476 if (!p)
2477 return NULL;
2478 p->kind = Global_kind;
2479 p->v.Global.names = names;
2480 p->lineno = lineno;
2481 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002482 p->end_lineno = end_lineno;
2483 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002484 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485}
2486
2487stmt_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002488Nonlocal(asdl_identifier_seq * names, int lineno, int col_offset, int
2489 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton81e95022007-02-27 06:50:52 +00002490{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002491 stmt_ty p;
2492 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2493 if (!p)
2494 return NULL;
2495 p->kind = Nonlocal_kind;
2496 p->v.Nonlocal.names = names;
2497 p->lineno = lineno;
2498 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002499 p->end_lineno = end_lineno;
2500 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002501 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002502}
2503
2504stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002505Expr(expr_ty value, int lineno, int col_offset, int end_lineno, int
2506 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002508 stmt_ty p;
2509 if (!value) {
2510 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002511 "field 'value' is required for Expr");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002512 return NULL;
2513 }
2514 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2515 if (!p)
2516 return NULL;
2517 p->kind = Expr_kind;
2518 p->v.Expr.value = value;
2519 p->lineno = lineno;
2520 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002521 p->end_lineno = end_lineno;
2522 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002523 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524}
2525
2526stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002527Pass(int lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
2528 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002530 stmt_ty p;
2531 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2532 if (!p)
2533 return NULL;
2534 p->kind = Pass_kind;
2535 p->lineno = lineno;
2536 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002537 p->end_lineno = end_lineno;
2538 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002539 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540}
2541
2542stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002543Break(int lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
2544 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002546 stmt_ty p;
2547 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2548 if (!p)
2549 return NULL;
2550 p->kind = Break_kind;
2551 p->lineno = lineno;
2552 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002553 p->end_lineno = end_lineno;
2554 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002555 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556}
2557
2558stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002559Continue(int lineno, int col_offset, int end_lineno, int end_col_offset,
2560 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002562 stmt_ty p;
2563 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
2564 if (!p)
2565 return NULL;
2566 p->kind = Continue_kind;
2567 p->lineno = lineno;
2568 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002569 p->end_lineno = end_lineno;
2570 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002571 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572}
2573
2574expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002575BoolOp(boolop_ty op, asdl_expr_seq * values, int lineno, int col_offset, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002576 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002578 expr_ty p;
2579 if (!op) {
2580 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002581 "field 'op' is required for BoolOp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002582 return NULL;
2583 }
2584 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2585 if (!p)
2586 return NULL;
2587 p->kind = BoolOp_kind;
2588 p->v.BoolOp.op = op;
2589 p->v.BoolOp.values = values;
2590 p->lineno = lineno;
2591 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002592 p->end_lineno = end_lineno;
2593 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002594 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595}
2596
2597expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002598NamedExpr(expr_ty target, expr_ty value, int lineno, int col_offset, int
2599 end_lineno, int end_col_offset, PyArena *arena)
2600{
2601 expr_ty p;
2602 if (!target) {
2603 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002604 "field 'target' is required for NamedExpr");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002605 return NULL;
2606 }
2607 if (!value) {
2608 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002609 "field 'value' is required for NamedExpr");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002610 return NULL;
2611 }
2612 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2613 if (!p)
2614 return NULL;
2615 p->kind = NamedExpr_kind;
2616 p->v.NamedExpr.target = target;
2617 p->v.NamedExpr.value = value;
2618 p->lineno = lineno;
2619 p->col_offset = col_offset;
2620 p->end_lineno = end_lineno;
2621 p->end_col_offset = end_col_offset;
2622 return p;
2623}
2624
2625expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002626BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002627 int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002629 expr_ty p;
2630 if (!left) {
2631 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002632 "field 'left' is required for BinOp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002633 return NULL;
2634 }
2635 if (!op) {
2636 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002637 "field 'op' is required for BinOp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002638 return NULL;
2639 }
2640 if (!right) {
2641 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002642 "field 'right' is required for BinOp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002643 return NULL;
2644 }
2645 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2646 if (!p)
2647 return NULL;
2648 p->kind = BinOp_kind;
2649 p->v.BinOp.left = left;
2650 p->v.BinOp.op = op;
2651 p->v.BinOp.right = right;
2652 p->lineno = lineno;
2653 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002654 p->end_lineno = end_lineno;
2655 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002656 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657}
2658
2659expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002660UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, int
2661 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002663 expr_ty p;
2664 if (!op) {
2665 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002666 "field 'op' is required for UnaryOp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002667 return NULL;
2668 }
2669 if (!operand) {
2670 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002671 "field 'operand' is required for UnaryOp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002672 return NULL;
2673 }
2674 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2675 if (!p)
2676 return NULL;
2677 p->kind = UnaryOp_kind;
2678 p->v.UnaryOp.op = op;
2679 p->v.UnaryOp.operand = operand;
2680 p->lineno = lineno;
2681 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002682 p->end_lineno = end_lineno;
2683 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002684 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685}
2686
2687expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002688Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, int
2689 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002691 expr_ty p;
2692 if (!args) {
2693 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002694 "field 'args' is required for Lambda");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002695 return NULL;
2696 }
2697 if (!body) {
2698 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002699 "field 'body' is required for Lambda");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002700 return NULL;
2701 }
2702 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2703 if (!p)
2704 return NULL;
2705 p->kind = Lambda_kind;
2706 p->v.Lambda.args = args;
2707 p->v.Lambda.body = body;
2708 p->lineno = lineno;
2709 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002710 p->end_lineno = end_lineno;
2711 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002712 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713}
2714
2715expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002716IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002717 int end_lineno, int end_col_offset, PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002718{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002719 expr_ty p;
2720 if (!test) {
2721 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002722 "field 'test' is required for IfExp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002723 return NULL;
2724 }
2725 if (!body) {
2726 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002727 "field 'body' is required for IfExp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002728 return NULL;
2729 }
2730 if (!orelse) {
2731 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002732 "field 'orelse' is required for IfExp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002733 return NULL;
2734 }
2735 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2736 if (!p)
2737 return NULL;
2738 p->kind = IfExp_kind;
2739 p->v.IfExp.test = test;
2740 p->v.IfExp.body = body;
2741 p->v.IfExp.orelse = orelse;
2742 p->lineno = lineno;
2743 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002744 p->end_lineno = end_lineno;
2745 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002746 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002747}
2748
2749expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002750Dict(asdl_expr_seq * keys, asdl_expr_seq * values, int lineno, int col_offset,
2751 int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002753 expr_ty p;
2754 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2755 if (!p)
2756 return NULL;
2757 p->kind = Dict_kind;
2758 p->v.Dict.keys = keys;
2759 p->v.Dict.values = values;
2760 p->lineno = lineno;
2761 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002762 p->end_lineno = end_lineno;
2763 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002764 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765}
2766
2767expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002768Set(asdl_expr_seq * elts, int lineno, int col_offset, int end_lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002769 end_col_offset, PyArena *arena)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002770{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002771 expr_ty p;
2772 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2773 if (!p)
2774 return NULL;
2775 p->kind = Set_kind;
2776 p->v.Set.elts = elts;
2777 p->lineno = lineno;
2778 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002779 p->end_lineno = end_lineno;
2780 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002781 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002782}
2783
2784expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002785ListComp(expr_ty elt, asdl_comprehension_seq * generators, int lineno, int
2786 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002788 expr_ty p;
2789 if (!elt) {
2790 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002791 "field 'elt' is required for ListComp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002792 return NULL;
2793 }
2794 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2795 if (!p)
2796 return NULL;
2797 p->kind = ListComp_kind;
2798 p->v.ListComp.elt = elt;
2799 p->v.ListComp.generators = generators;
2800 p->lineno = lineno;
2801 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002802 p->end_lineno = end_lineno;
2803 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002804 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805}
2806
2807expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002808SetComp(expr_ty elt, asdl_comprehension_seq * generators, int lineno, int
2809 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002810{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002811 expr_ty p;
2812 if (!elt) {
2813 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002814 "field 'elt' is required for SetComp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002815 return NULL;
2816 }
2817 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2818 if (!p)
2819 return NULL;
2820 p->kind = SetComp_kind;
2821 p->v.SetComp.elt = elt;
2822 p->v.SetComp.generators = generators;
2823 p->lineno = lineno;
2824 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002825 p->end_lineno = end_lineno;
2826 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002827 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002828}
2829
2830expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002831DictComp(expr_ty key, expr_ty value, asdl_comprehension_seq * generators, int
2832 lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
2833 *arena)
Guido van Rossum992d4a32007-07-11 13:09:30 +00002834{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002835 expr_ty p;
2836 if (!key) {
2837 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002838 "field 'key' is required for DictComp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002839 return NULL;
2840 }
2841 if (!value) {
2842 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002843 "field 'value' is required for DictComp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002844 return NULL;
2845 }
2846 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2847 if (!p)
2848 return NULL;
2849 p->kind = DictComp_kind;
2850 p->v.DictComp.key = key;
2851 p->v.DictComp.value = value;
2852 p->v.DictComp.generators = generators;
2853 p->lineno = lineno;
2854 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002855 p->end_lineno = end_lineno;
2856 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002857 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002858}
2859
2860expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002861GeneratorExp(expr_ty elt, asdl_comprehension_seq * generators, int lineno, int
2862 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002864 expr_ty p;
2865 if (!elt) {
2866 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002867 "field 'elt' is required for GeneratorExp");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002868 return NULL;
2869 }
2870 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2871 if (!p)
2872 return NULL;
2873 p->kind = GeneratorExp_kind;
2874 p->v.GeneratorExp.elt = elt;
2875 p->v.GeneratorExp.generators = generators;
2876 p->lineno = lineno;
2877 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002878 p->end_lineno = end_lineno;
2879 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002880 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881}
2882
2883expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002884Await(expr_ty value, int lineno, int col_offset, int end_lineno, int
2885 end_col_offset, PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04002886{
2887 expr_ty p;
2888 if (!value) {
2889 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002890 "field 'value' is required for Await");
Yury Selivanov75445082015-05-11 22:57:16 -04002891 return NULL;
2892 }
2893 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2894 if (!p)
2895 return NULL;
2896 p->kind = Await_kind;
2897 p->v.Await.value = value;
2898 p->lineno = lineno;
2899 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002900 p->end_lineno = end_lineno;
2901 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04002902 return p;
2903}
2904
2905expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002906Yield(expr_ty value, int lineno, int col_offset, int end_lineno, int
2907 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002909 expr_ty p;
2910 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2911 if (!p)
2912 return NULL;
2913 p->kind = Yield_kind;
2914 p->v.Yield.value = value;
2915 p->lineno = lineno;
2916 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002917 p->end_lineno = end_lineno;
2918 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002919 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920}
2921
2922expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002923YieldFrom(expr_ty value, int lineno, int col_offset, int end_lineno, int
2924 end_col_offset, PyArena *arena)
Benjamin Peterson527c6222012-01-14 08:58:23 -05002925{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002926 expr_ty p;
2927 if (!value) {
2928 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002929 "field 'value' is required for YieldFrom");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002930 return NULL;
2931 }
2932 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2933 if (!p)
2934 return NULL;
2935 p->kind = YieldFrom_kind;
2936 p->v.YieldFrom.value = value;
2937 p->lineno = lineno;
2938 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002939 p->end_lineno = end_lineno;
2940 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002941 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05002942}
2943
2944expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002945Compare(expr_ty left, asdl_int_seq * ops, asdl_expr_seq * comparators, int
2946 lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
2947 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002949 expr_ty p;
2950 if (!left) {
2951 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002952 "field 'left' is required for Compare");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002953 return NULL;
2954 }
2955 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2956 if (!p)
2957 return NULL;
2958 p->kind = Compare_kind;
2959 p->v.Compare.left = left;
2960 p->v.Compare.ops = ops;
2961 p->v.Compare.comparators = comparators;
2962 p->lineno = lineno;
2963 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002964 p->end_lineno = end_lineno;
2965 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002966 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967}
2968
2969expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01002970Call(expr_ty func, asdl_expr_seq * args, asdl_keyword_seq * keywords, int
2971 lineno, int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002973 expr_ty p;
2974 if (!func) {
2975 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03002976 "field 'func' is required for Call");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002977 return NULL;
2978 }
2979 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2980 if (!p)
2981 return NULL;
2982 p->kind = Call_kind;
2983 p->v.Call.func = func;
2984 p->v.Call.args = args;
2985 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002986 p->lineno = lineno;
2987 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002988 p->end_lineno = end_lineno;
2989 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002990 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991}
2992
2993expr_ty
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04002994FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2995 int col_offset, int end_lineno, int end_col_offset, PyArena
2996 *arena)
Eric V. Smith235a6f02015-09-19 14:51:32 -04002997{
2998 expr_ty p;
2999 if (!value) {
3000 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003001 "field 'value' is required for FormattedValue");
Eric V. Smith235a6f02015-09-19 14:51:32 -04003002 return NULL;
3003 }
3004 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3005 if (!p)
3006 return NULL;
3007 p->kind = FormattedValue_kind;
3008 p->v.FormattedValue.value = value;
3009 p->v.FormattedValue.conversion = conversion;
3010 p->v.FormattedValue.format_spec = format_spec;
3011 p->lineno = lineno;
3012 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003013 p->end_lineno = end_lineno;
3014 p->end_col_offset = end_col_offset;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003015 return p;
3016}
3017
3018expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01003019JoinedStr(asdl_expr_seq * values, int lineno, int col_offset, int end_lineno,
3020 int end_col_offset, PyArena *arena)
Eric V. Smith235a6f02015-09-19 14:51:32 -04003021{
3022 expr_ty p;
3023 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3024 if (!p)
3025 return NULL;
3026 p->kind = JoinedStr_kind;
3027 p->v.JoinedStr.values = values;
3028 p->lineno = lineno;
3029 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003030 p->end_lineno = end_lineno;
3031 p->end_col_offset = end_col_offset;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003032 return p;
3033}
3034
3035expr_ty
Guido van Rossum10f8ce62019-03-13 13:00:46 -07003036Constant(constant value, string kind, int lineno, int col_offset, int
3037 end_lineno, int end_col_offset, PyArena *arena)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003038{
3039 expr_ty p;
3040 if (!value) {
3041 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003042 "field 'value' is required for Constant");
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003043 return NULL;
3044 }
3045 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3046 if (!p)
3047 return NULL;
3048 p->kind = Constant_kind;
3049 p->v.Constant.value = value;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07003050 p->v.Constant.kind = kind;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003051 p->lineno = lineno;
3052 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003053 p->end_lineno = end_lineno;
3054 p->end_col_offset = end_col_offset;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003055 return p;
3056}
3057
3058expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003059Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003060 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003062 expr_ty p;
3063 if (!value) {
3064 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003065 "field 'value' is required for Attribute");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003066 return NULL;
3067 }
3068 if (!attr) {
3069 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003070 "field 'attr' is required for Attribute");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003071 return NULL;
3072 }
3073 if (!ctx) {
3074 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003075 "field 'ctx' is required for Attribute");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003076 return NULL;
3077 }
3078 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3079 if (!p)
3080 return NULL;
3081 p->kind = Attribute_kind;
3082 p->v.Attribute.value = value;
3083 p->v.Attribute.attr = attr;
3084 p->v.Attribute.ctx = ctx;
3085 p->lineno = lineno;
3086 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003087 p->end_lineno = end_lineno;
3088 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003089 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003090}
3091
3092expr_ty
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02003093Subscript(expr_ty value, expr_ty slice, expr_context_ty ctx, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003094 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003096 expr_ty p;
3097 if (!value) {
3098 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003099 "field 'value' is required for Subscript");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003100 return NULL;
3101 }
3102 if (!slice) {
3103 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003104 "field 'slice' is required for Subscript");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003105 return NULL;
3106 }
3107 if (!ctx) {
3108 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003109 "field 'ctx' is required for Subscript");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003110 return NULL;
3111 }
3112 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3113 if (!p)
3114 return NULL;
3115 p->kind = Subscript_kind;
3116 p->v.Subscript.value = value;
3117 p->v.Subscript.slice = slice;
3118 p->v.Subscript.ctx = ctx;
3119 p->lineno = lineno;
3120 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003121 p->end_lineno = end_lineno;
3122 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003123 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124}
3125
3126expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003127Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, int
3128 end_lineno, int end_col_offset, PyArena *arena)
Guido van Rossum0368b722007-05-11 16:50:42 +00003129{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003130 expr_ty p;
3131 if (!value) {
3132 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003133 "field 'value' is required for Starred");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003134 return NULL;
3135 }
3136 if (!ctx) {
3137 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003138 "field 'ctx' is required for Starred");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003139 return NULL;
3140 }
3141 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3142 if (!p)
3143 return NULL;
3144 p->kind = Starred_kind;
3145 p->v.Starred.value = value;
3146 p->v.Starred.ctx = ctx;
3147 p->lineno = lineno;
3148 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003149 p->end_lineno = end_lineno;
3150 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003151 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00003152}
3153
3154expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003155Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, int
3156 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003158 expr_ty p;
3159 if (!id) {
3160 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003161 "field 'id' is required for Name");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003162 return NULL;
3163 }
3164 if (!ctx) {
3165 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003166 "field 'ctx' is required for Name");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003167 return NULL;
3168 }
3169 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3170 if (!p)
3171 return NULL;
3172 p->kind = Name_kind;
3173 p->v.Name.id = id;
3174 p->v.Name.ctx = ctx;
3175 p->lineno = lineno;
3176 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003177 p->end_lineno = end_lineno;
3178 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003179 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180}
3181
3182expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01003183List(asdl_expr_seq * elts, expr_context_ty ctx, int lineno, int col_offset, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003184 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003186 expr_ty p;
3187 if (!ctx) {
3188 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003189 "field 'ctx' is required for List");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003190 return NULL;
3191 }
3192 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3193 if (!p)
3194 return NULL;
3195 p->kind = List_kind;
3196 p->v.List.elts = elts;
3197 p->v.List.ctx = ctx;
3198 p->lineno = lineno;
3199 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003200 p->end_lineno = end_lineno;
3201 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003202 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203}
3204
3205expr_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01003206Tuple(asdl_expr_seq * elts, expr_context_ty ctx, int lineno, int col_offset,
3207 int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003209 expr_ty p;
3210 if (!ctx) {
3211 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003212 "field 'ctx' is required for Tuple");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003213 return NULL;
3214 }
3215 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
3216 if (!p)
3217 return NULL;
3218 p->kind = Tuple_kind;
3219 p->v.Tuple.elts = elts;
3220 p->v.Tuple.ctx = ctx;
3221 p->lineno = lineno;
3222 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003223 p->end_lineno = end_lineno;
3224 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003225 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226}
3227
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02003228expr_ty
3229Slice(expr_ty lower, expr_ty upper, expr_ty step, int lineno, int col_offset,
3230 int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02003232 expr_ty p;
3233 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003234 if (!p)
3235 return NULL;
3236 p->kind = Slice_kind;
3237 p->v.Slice.lower = lower;
3238 p->v.Slice.upper = upper;
3239 p->v.Slice.step = step;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02003240 p->lineno = lineno;
3241 p->col_offset = col_offset;
3242 p->end_lineno = end_lineno;
3243 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003244 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245}
3246
3247comprehension_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01003248comprehension(expr_ty target, expr_ty iter, asdl_expr_seq * ifs, int is_async,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003249 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003250{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003251 comprehension_ty p;
3252 if (!target) {
3253 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003254 "field 'target' is required for comprehension");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003255 return NULL;
3256 }
3257 if (!iter) {
3258 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003259 "field 'iter' is required for comprehension");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003260 return NULL;
3261 }
3262 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
3263 if (!p)
3264 return NULL;
3265 p->target = target;
3266 p->iter = iter;
3267 p->ifs = ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003268 p->is_async = is_async;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003269 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270}
3271
3272excepthandler_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01003273ExceptHandler(expr_ty type, identifier name, asdl_stmt_seq * body, int lineno,
3274 int col_offset, int end_lineno, int end_col_offset, PyArena
3275 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003276{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003277 excepthandler_ty p;
3278 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
3279 if (!p)
3280 return NULL;
3281 p->kind = ExceptHandler_kind;
3282 p->v.ExceptHandler.type = type;
3283 p->v.ExceptHandler.name = name;
3284 p->v.ExceptHandler.body = body;
3285 p->lineno = lineno;
3286 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003287 p->end_lineno = end_lineno;
3288 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003289 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290}
3291
3292arguments_ty
Pablo Galindoa5634c42020-09-16 19:42:00 +01003293arguments(asdl_arg_seq * posonlyargs, asdl_arg_seq * args, arg_ty vararg,
3294 asdl_arg_seq * kwonlyargs, asdl_expr_seq * kw_defaults, arg_ty kwarg,
3295 asdl_expr_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003297 arguments_ty p;
3298 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
3299 if (!p)
3300 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003301 p->posonlyargs = posonlyargs;
Pablo Galindocd6e83b2019-07-15 01:32:18 +02003302 p->args = args;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003303 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003304 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003305 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003306 p->kwarg = kwarg;
3307 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003308 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309}
3310
Neal Norwitzc1505362006-12-28 06:47:50 +00003311arg_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003312arg(identifier arg, expr_ty annotation, string type_comment, int lineno, int
3313 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00003314{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003315 arg_ty p;
3316 if (!arg) {
3317 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003318 "field 'arg' is required for arg");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003319 return NULL;
3320 }
3321 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
3322 if (!p)
3323 return NULL;
3324 p->arg = arg;
3325 p->annotation = annotation;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003326 p->type_comment = type_comment;
Victor Stinnerc106c682015-11-06 17:01:48 +01003327 p->lineno = lineno;
3328 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003329 p->end_lineno = end_lineno;
3330 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003331 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00003332}
3333
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334keyword_ty
Pablo Galindo168660b2020-04-02 00:47:39 +01003335keyword(identifier arg, expr_ty value, int lineno, int col_offset, int
3336 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003338 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003339 if (!value) {
3340 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003341 "field 'value' is required for keyword");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003342 return NULL;
3343 }
3344 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
3345 if (!p)
3346 return NULL;
3347 p->arg = arg;
3348 p->value = value;
Pablo Galindo168660b2020-04-02 00:47:39 +01003349 p->lineno = lineno;
3350 p->col_offset = col_offset;
3351 p->end_lineno = end_lineno;
3352 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003353 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354}
3355
3356alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003357alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003359 alias_ty p;
3360 if (!name) {
3361 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003362 "field 'name' is required for alias");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003363 return NULL;
3364 }
3365 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
3366 if (!p)
3367 return NULL;
3368 p->name = name;
3369 p->asname = asname;
3370 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371}
3372
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003373withitem_ty
3374withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
3375{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003376 withitem_ty p;
3377 if (!context_expr) {
3378 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003379 "field 'context_expr' is required for withitem");
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003380 return NULL;
3381 }
3382 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
3383 if (!p)
3384 return NULL;
3385 p->context_expr = context_expr;
3386 p->optional_vars = optional_vars;
3387 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003388}
3389
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003390type_ignore_ty
Michael J. Sullivan933e1502019-05-22 07:54:20 -07003391TypeIgnore(int lineno, string tag, PyArena *arena)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003392{
3393 type_ignore_ty p;
Michael J. Sullivan933e1502019-05-22 07:54:20 -07003394 if (!tag) {
3395 PyErr_SetString(PyExc_ValueError,
Batuhan Taskaya091951a2020-05-06 17:29:32 +03003396 "field 'tag' is required for TypeIgnore");
Michael J. Sullivan933e1502019-05-22 07:54:20 -07003397 return NULL;
3398 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003399 p = (type_ignore_ty)PyArena_Malloc(arena, sizeof(*p));
3400 if (!p)
3401 return NULL;
3402 p->kind = TypeIgnore_kind;
3403 p->v.TypeIgnore.lineno = lineno;
Michael J. Sullivan933e1502019-05-22 07:54:20 -07003404 p->v.TypeIgnore.tag = tag;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003405 return p;
3406}
3407
Neal Norwitz7b5a6042005-11-13 19:14:20 +00003408
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003409PyObject*
Victor Stinner5cf47822020-11-02 22:03:28 +01003410ast2obj_mod(struct ast_state *state, void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00003411{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003412 mod_ty o = (mod_ty)_o;
3413 PyObject *result = NULL, *value = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003414 PyTypeObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003415 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003416 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003417 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003418 switch (o->kind) {
3419 case Module_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003420 tp = (PyTypeObject *)state->Module_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003421 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003422 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003423 value = ast2obj_list(state, (asdl_seq*)o->v.Module.body, ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003424 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003425 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003426 goto failed;
3427 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003428 value = ast2obj_list(state, (asdl_seq*)o->v.Module.type_ignores,
Victor Stinner74419f02020-07-03 11:35:37 +02003429 ast2obj_type_ignore);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003430 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003431 if (PyObject_SetAttr(result, state->type_ignores, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003432 goto failed;
3433 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003434 break;
3435 case Interactive_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003436 tp = (PyTypeObject *)state->Interactive_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003437 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003438 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003439 value = ast2obj_list(state, (asdl_seq*)o->v.Interactive.body,
3440 ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003441 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003442 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003443 goto failed;
3444 Py_DECREF(value);
3445 break;
3446 case Expression_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003447 tp = (PyTypeObject *)state->Expression_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003448 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003449 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003450 value = ast2obj_expr(state, o->v.Expression.body);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003451 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003452 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003453 goto failed;
3454 Py_DECREF(value);
3455 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003456 case FunctionType_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003457 tp = (PyTypeObject *)state->FunctionType_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003458 result = PyType_GenericNew(tp, NULL, NULL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003459 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003460 value = ast2obj_list(state, (asdl_seq*)o->v.FunctionType.argtypes,
3461 ast2obj_expr);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003462 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003463 if (PyObject_SetAttr(result, state->argtypes, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003464 goto failed;
3465 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003466 value = ast2obj_expr(state, o->v.FunctionType.returns);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003467 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003468 if (PyObject_SetAttr(result, state->returns, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003469 goto failed;
3470 Py_DECREF(value);
3471 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003472 }
3473 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003474failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003475 Py_XDECREF(value);
3476 Py_XDECREF(result);
3477 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478}
3479
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003480PyObject*
Victor Stinner5cf47822020-11-02 22:03:28 +01003481ast2obj_stmt(struct ast_state *state, void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003483 stmt_ty o = (stmt_ty)_o;
3484 PyObject *result = NULL, *value = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003485 PyTypeObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003486 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003487 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003488 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003489 switch (o->kind) {
3490 case FunctionDef_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003491 tp = (PyTypeObject *)state->FunctionDef_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003492 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003493 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003494 value = ast2obj_identifier(state, o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003495 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003496 if (PyObject_SetAttr(result, state->name, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003497 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003498 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003499 value = ast2obj_arguments(state, o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003500 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003501 if (PyObject_SetAttr(result, state->args, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003502 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003503 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003504 value = ast2obj_list(state, (asdl_seq*)o->v.FunctionDef.body,
3505 ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003506 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003507 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003508 goto failed;
3509 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003510 value = ast2obj_list(state, (asdl_seq*)o->v.FunctionDef.decorator_list,
Victor Stinner74419f02020-07-03 11:35:37 +02003511 ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003512 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003513 if (PyObject_SetAttr(result, state->decorator_list, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003514 goto failed;
3515 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003516 value = ast2obj_expr(state, o->v.FunctionDef.returns);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003517 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003518 if (PyObject_SetAttr(result, state->returns, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003519 goto failed;
3520 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003521 value = ast2obj_string(state, o->v.FunctionDef.type_comment);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003522 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003523 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003524 goto failed;
3525 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003526 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003527 case AsyncFunctionDef_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003528 tp = (PyTypeObject *)state->AsyncFunctionDef_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003529 result = PyType_GenericNew(tp, NULL, NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04003530 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003531 value = ast2obj_identifier(state, o->v.AsyncFunctionDef.name);
Yury Selivanov75445082015-05-11 22:57:16 -04003532 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003533 if (PyObject_SetAttr(result, state->name, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04003534 goto failed;
3535 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003536 value = ast2obj_arguments(state, o->v.AsyncFunctionDef.args);
Yury Selivanov75445082015-05-11 22:57:16 -04003537 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003538 if (PyObject_SetAttr(result, state->args, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04003539 goto failed;
3540 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003541 value = ast2obj_list(state, (asdl_seq*)o->v.AsyncFunctionDef.body,
3542 ast2obj_stmt);
Yury Selivanov75445082015-05-11 22:57:16 -04003543 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003544 if (PyObject_SetAttr(result, state->body, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04003545 goto failed;
3546 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003547 value = ast2obj_list(state,
3548 (asdl_seq*)o->v.AsyncFunctionDef.decorator_list,
Yury Selivanov75445082015-05-11 22:57:16 -04003549 ast2obj_expr);
3550 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003551 if (PyObject_SetAttr(result, state->decorator_list, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04003552 goto failed;
3553 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003554 value = ast2obj_expr(state, o->v.AsyncFunctionDef.returns);
Yury Selivanov75445082015-05-11 22:57:16 -04003555 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003556 if (PyObject_SetAttr(result, state->returns, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04003557 goto failed;
3558 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003559 value = ast2obj_string(state, o->v.AsyncFunctionDef.type_comment);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003560 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003561 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003562 goto failed;
3563 Py_DECREF(value);
Yury Selivanov75445082015-05-11 22:57:16 -04003564 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003565 case ClassDef_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003566 tp = (PyTypeObject *)state->ClassDef_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003567 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003568 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003569 value = ast2obj_identifier(state, o->v.ClassDef.name);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003570 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003571 if (PyObject_SetAttr(result, state->name, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003572 goto failed;
3573 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003574 value = ast2obj_list(state, (asdl_seq*)o->v.ClassDef.bases,
3575 ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003576 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003577 if (PyObject_SetAttr(result, state->bases, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003578 goto failed;
3579 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003580 value = ast2obj_list(state, (asdl_seq*)o->v.ClassDef.keywords,
3581 ast2obj_keyword);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003582 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003583 if (PyObject_SetAttr(result, state->keywords, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003584 goto failed;
3585 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003586 value = ast2obj_list(state, (asdl_seq*)o->v.ClassDef.body,
3587 ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003588 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003589 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003590 goto failed;
3591 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003592 value = ast2obj_list(state, (asdl_seq*)o->v.ClassDef.decorator_list,
3593 ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003594 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003595 if (PyObject_SetAttr(result, state->decorator_list, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003596 goto failed;
3597 Py_DECREF(value);
3598 break;
3599 case Return_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003600 tp = (PyTypeObject *)state->Return_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003601 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003602 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003603 value = ast2obj_expr(state, o->v.Return.value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003604 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003605 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003606 goto failed;
3607 Py_DECREF(value);
3608 break;
3609 case Delete_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003610 tp = (PyTypeObject *)state->Delete_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003611 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003612 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003613 value = ast2obj_list(state, (asdl_seq*)o->v.Delete.targets,
3614 ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003615 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003616 if (PyObject_SetAttr(result, state->targets, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003617 goto failed;
3618 Py_DECREF(value);
3619 break;
3620 case Assign_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003621 tp = (PyTypeObject *)state->Assign_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003622 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003623 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003624 value = ast2obj_list(state, (asdl_seq*)o->v.Assign.targets,
3625 ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003626 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003627 if (PyObject_SetAttr(result, state->targets, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003628 goto failed;
3629 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003630 value = ast2obj_expr(state, o->v.Assign.value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003631 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003632 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003633 goto failed;
3634 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003635 value = ast2obj_string(state, o->v.Assign.type_comment);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003636 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003637 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003638 goto failed;
3639 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003640 break;
3641 case AugAssign_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003642 tp = (PyTypeObject *)state->AugAssign_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003643 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003644 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003645 value = ast2obj_expr(state, o->v.AugAssign.target);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003646 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003647 if (PyObject_SetAttr(result, state->target, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003648 goto failed;
3649 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003650 value = ast2obj_operator(state, o->v.AugAssign.op);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003651 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003652 if (PyObject_SetAttr(result, state->op, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003653 goto failed;
3654 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003655 value = ast2obj_expr(state, o->v.AugAssign.value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003656 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003657 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003658 goto failed;
3659 Py_DECREF(value);
3660 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003661 case AnnAssign_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003662 tp = (PyTypeObject *)state->AnnAssign_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003663 result = PyType_GenericNew(tp, NULL, NULL);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003664 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003665 value = ast2obj_expr(state, o->v.AnnAssign.target);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003666 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003667 if (PyObject_SetAttr(result, state->target, value) == -1)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003668 goto failed;
3669 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003670 value = ast2obj_expr(state, o->v.AnnAssign.annotation);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003671 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003672 if (PyObject_SetAttr(result, state->annotation, value) == -1)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003673 goto failed;
3674 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003675 value = ast2obj_expr(state, o->v.AnnAssign.value);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003676 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003677 if (PyObject_SetAttr(result, state->value, value) == -1)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003678 goto failed;
3679 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003680 value = ast2obj_int(state, o->v.AnnAssign.simple);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003681 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003682 if (PyObject_SetAttr(result, state->simple, value) == -1)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003683 goto failed;
3684 Py_DECREF(value);
3685 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003686 case For_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003687 tp = (PyTypeObject *)state->For_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003688 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003689 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003690 value = ast2obj_expr(state, o->v.For.target);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003691 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003692 if (PyObject_SetAttr(result, state->target, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003693 goto failed;
3694 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003695 value = ast2obj_expr(state, o->v.For.iter);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003696 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003697 if (PyObject_SetAttr(result, state->iter, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003698 goto failed;
3699 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003700 value = ast2obj_list(state, (asdl_seq*)o->v.For.body, ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003701 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003702 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003703 goto failed;
3704 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003705 value = ast2obj_list(state, (asdl_seq*)o->v.For.orelse, ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003706 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003707 if (PyObject_SetAttr(result, state->orelse, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003708 goto failed;
3709 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003710 value = ast2obj_string(state, o->v.For.type_comment);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003711 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003712 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003713 goto failed;
3714 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003715 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003716 case AsyncFor_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003717 tp = (PyTypeObject *)state->AsyncFor_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003718 result = PyType_GenericNew(tp, NULL, NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04003719 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003720 value = ast2obj_expr(state, o->v.AsyncFor.target);
Yury Selivanov75445082015-05-11 22:57:16 -04003721 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003722 if (PyObject_SetAttr(result, state->target, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04003723 goto failed;
3724 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003725 value = ast2obj_expr(state, o->v.AsyncFor.iter);
Yury Selivanov75445082015-05-11 22:57:16 -04003726 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003727 if (PyObject_SetAttr(result, state->iter, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04003728 goto failed;
3729 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003730 value = ast2obj_list(state, (asdl_seq*)o->v.AsyncFor.body,
3731 ast2obj_stmt);
Yury Selivanov75445082015-05-11 22:57:16 -04003732 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003733 if (PyObject_SetAttr(result, state->body, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04003734 goto failed;
3735 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003736 value = ast2obj_list(state, (asdl_seq*)o->v.AsyncFor.orelse,
3737 ast2obj_stmt);
Yury Selivanov75445082015-05-11 22:57:16 -04003738 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003739 if (PyObject_SetAttr(result, state->orelse, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04003740 goto failed;
3741 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003742 value = ast2obj_string(state, o->v.AsyncFor.type_comment);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003743 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003744 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003745 goto failed;
3746 Py_DECREF(value);
Yury Selivanov75445082015-05-11 22:57:16 -04003747 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003748 case While_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003749 tp = (PyTypeObject *)state->While_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003750 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003751 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003752 value = ast2obj_expr(state, o->v.While.test);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003753 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003754 if (PyObject_SetAttr(result, state->test, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003755 goto failed;
3756 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003757 value = ast2obj_list(state, (asdl_seq*)o->v.While.body, ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003758 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003759 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003760 goto failed;
3761 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003762 value = ast2obj_list(state, (asdl_seq*)o->v.While.orelse, ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003763 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003764 if (PyObject_SetAttr(result, state->orelse, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003765 goto failed;
3766 Py_DECREF(value);
3767 break;
3768 case If_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003769 tp = (PyTypeObject *)state->If_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003770 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003771 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003772 value = ast2obj_expr(state, o->v.If.test);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003773 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003774 if (PyObject_SetAttr(result, state->test, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003775 goto failed;
3776 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003777 value = ast2obj_list(state, (asdl_seq*)o->v.If.body, ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003778 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003779 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003780 goto failed;
3781 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003782 value = ast2obj_list(state, (asdl_seq*)o->v.If.orelse, ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003783 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003784 if (PyObject_SetAttr(result, state->orelse, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003785 goto failed;
3786 Py_DECREF(value);
3787 break;
3788 case With_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003789 tp = (PyTypeObject *)state->With_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003790 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003791 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003792 value = ast2obj_list(state, (asdl_seq*)o->v.With.items,
3793 ast2obj_withitem);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003794 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003795 if (PyObject_SetAttr(result, state->items, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003796 goto failed;
3797 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003798 value = ast2obj_list(state, (asdl_seq*)o->v.With.body, ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003799 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003800 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003801 goto failed;
3802 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003803 value = ast2obj_string(state, o->v.With.type_comment);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003804 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003805 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003806 goto failed;
3807 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003808 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003809 case AsyncWith_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003810 tp = (PyTypeObject *)state->AsyncWith_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003811 result = PyType_GenericNew(tp, NULL, NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04003812 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003813 value = ast2obj_list(state, (asdl_seq*)o->v.AsyncWith.items,
3814 ast2obj_withitem);
Yury Selivanov75445082015-05-11 22:57:16 -04003815 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003816 if (PyObject_SetAttr(result, state->items, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04003817 goto failed;
3818 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003819 value = ast2obj_list(state, (asdl_seq*)o->v.AsyncWith.body,
3820 ast2obj_stmt);
Yury Selivanov75445082015-05-11 22:57:16 -04003821 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003822 if (PyObject_SetAttr(result, state->body, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04003823 goto failed;
3824 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003825 value = ast2obj_string(state, o->v.AsyncWith.type_comment);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003826 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003827 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003828 goto failed;
3829 Py_DECREF(value);
Yury Selivanov75445082015-05-11 22:57:16 -04003830 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003831 case Raise_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003832 tp = (PyTypeObject *)state->Raise_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003833 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003834 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003835 value = ast2obj_expr(state, o->v.Raise.exc);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003836 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003837 if (PyObject_SetAttr(result, state->exc, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003838 goto failed;
3839 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003840 value = ast2obj_expr(state, o->v.Raise.cause);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003841 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003842 if (PyObject_SetAttr(result, state->cause, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003843 goto failed;
3844 Py_DECREF(value);
3845 break;
3846 case Try_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003847 tp = (PyTypeObject *)state->Try_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003848 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003849 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003850 value = ast2obj_list(state, (asdl_seq*)o->v.Try.body, ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003851 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003852 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003853 goto failed;
3854 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003855 value = ast2obj_list(state, (asdl_seq*)o->v.Try.handlers,
3856 ast2obj_excepthandler);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003857 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003858 if (PyObject_SetAttr(result, state->handlers, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003859 goto failed;
3860 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003861 value = ast2obj_list(state, (asdl_seq*)o->v.Try.orelse, ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003862 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003863 if (PyObject_SetAttr(result, state->orelse, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003864 goto failed;
3865 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003866 value = ast2obj_list(state, (asdl_seq*)o->v.Try.finalbody,
3867 ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003868 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003869 if (PyObject_SetAttr(result, state->finalbody, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003870 goto failed;
3871 Py_DECREF(value);
3872 break;
3873 case Assert_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003874 tp = (PyTypeObject *)state->Assert_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003875 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003876 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003877 value = ast2obj_expr(state, o->v.Assert.test);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003878 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003879 if (PyObject_SetAttr(result, state->test, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003880 goto failed;
3881 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003882 value = ast2obj_expr(state, o->v.Assert.msg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003883 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003884 if (PyObject_SetAttr(result, state->msg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003885 goto failed;
3886 Py_DECREF(value);
3887 break;
3888 case Import_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003889 tp = (PyTypeObject *)state->Import_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003890 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003891 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003892 value = ast2obj_list(state, (asdl_seq*)o->v.Import.names,
3893 ast2obj_alias);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003894 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003895 if (PyObject_SetAttr(result, state->names, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003896 goto failed;
3897 Py_DECREF(value);
3898 break;
3899 case ImportFrom_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003900 tp = (PyTypeObject *)state->ImportFrom_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003901 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003902 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003903 value = ast2obj_identifier(state, o->v.ImportFrom.module);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003904 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003905 if (PyObject_SetAttr(result, state->module, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003906 goto failed;
3907 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01003908 value = ast2obj_list(state, (asdl_seq*)o->v.ImportFrom.names,
3909 ast2obj_alias);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003910 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003911 if (PyObject_SetAttr(result, state->names, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003912 goto failed;
3913 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003914 value = ast2obj_int(state, o->v.ImportFrom.level);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003915 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003916 if (PyObject_SetAttr(result, state->level, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003917 goto failed;
3918 Py_DECREF(value);
3919 break;
3920 case Global_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003921 tp = (PyTypeObject *)state->Global_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003922 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003923 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003924 value = ast2obj_list(state, (asdl_seq*)o->v.Global.names,
3925 ast2obj_identifier);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003926 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003927 if (PyObject_SetAttr(result, state->names, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003928 goto failed;
3929 Py_DECREF(value);
3930 break;
3931 case Nonlocal_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003932 tp = (PyTypeObject *)state->Nonlocal_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003933 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003934 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003935 value = ast2obj_list(state, (asdl_seq*)o->v.Nonlocal.names,
3936 ast2obj_identifier);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003937 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003938 if (PyObject_SetAttr(result, state->names, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003939 goto failed;
3940 Py_DECREF(value);
3941 break;
3942 case Expr_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003943 tp = (PyTypeObject *)state->Expr_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003944 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003945 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003946 value = ast2obj_expr(state, o->v.Expr.value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003947 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003948 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003949 goto failed;
3950 Py_DECREF(value);
3951 break;
3952 case Pass_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003953 tp = (PyTypeObject *)state->Pass_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003954 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003955 if (!result) goto failed;
3956 break;
3957 case Break_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003958 tp = (PyTypeObject *)state->Break_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003959 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003960 if (!result) goto failed;
3961 break;
3962 case Continue_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02003963 tp = (PyTypeObject *)state->Continue_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07003964 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003965 if (!result) goto failed;
3966 break;
3967 }
Victor Stinner74419f02020-07-03 11:35:37 +02003968 value = ast2obj_int(state, o->lineno);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003969 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003970 if (PyObject_SetAttr(result, state->lineno, value) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003971 goto failed;
3972 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003973 value = ast2obj_int(state, o->col_offset);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003974 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003975 if (PyObject_SetAttr(result, state->col_offset, value) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003976 goto failed;
3977 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003978 value = ast2obj_int(state, o->end_lineno);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003979 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003980 if (PyObject_SetAttr(result, state->end_lineno, value) < 0)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003981 goto failed;
3982 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02003983 value = ast2obj_int(state, o->end_col_offset);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003984 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02003985 if (PyObject_SetAttr(result, state->end_col_offset, value) < 0)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003986 goto failed;
3987 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003988 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003989failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003990 Py_XDECREF(value);
3991 Py_XDECREF(result);
3992 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993}
3994
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003995PyObject*
Victor Stinner5cf47822020-11-02 22:03:28 +01003996ast2obj_expr(struct ast_state *state, void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003997{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003998 expr_ty o = (expr_ty)_o;
3999 PyObject *result = NULL, *value = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004000 PyTypeObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004001 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004002 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004003 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004004 switch (o->kind) {
4005 case BoolOp_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004006 tp = (PyTypeObject *)state->BoolOp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004007 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004008 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004009 value = ast2obj_boolop(state, o->v.BoolOp.op);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004010 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004011 if (PyObject_SetAttr(result, state->op, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004012 goto failed;
4013 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004014 value = ast2obj_list(state, (asdl_seq*)o->v.BoolOp.values,
4015 ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004016 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004017 if (PyObject_SetAttr(result, state->values, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004018 goto failed;
4019 Py_DECREF(value);
4020 break;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004021 case NamedExpr_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004022 tp = (PyTypeObject *)state->NamedExpr_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004023 result = PyType_GenericNew(tp, NULL, NULL);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004024 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004025 value = ast2obj_expr(state, o->v.NamedExpr.target);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004026 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004027 if (PyObject_SetAttr(result, state->target, value) == -1)
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004028 goto failed;
4029 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004030 value = ast2obj_expr(state, o->v.NamedExpr.value);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004031 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004032 if (PyObject_SetAttr(result, state->value, value) == -1)
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004033 goto failed;
4034 Py_DECREF(value);
4035 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004036 case BinOp_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004037 tp = (PyTypeObject *)state->BinOp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004038 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004039 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004040 value = ast2obj_expr(state, o->v.BinOp.left);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004041 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004042 if (PyObject_SetAttr(result, state->left, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004043 goto failed;
4044 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004045 value = ast2obj_operator(state, o->v.BinOp.op);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004046 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004047 if (PyObject_SetAttr(result, state->op, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004048 goto failed;
4049 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004050 value = ast2obj_expr(state, o->v.BinOp.right);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004051 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004052 if (PyObject_SetAttr(result, state->right, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004053 goto failed;
4054 Py_DECREF(value);
4055 break;
4056 case UnaryOp_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004057 tp = (PyTypeObject *)state->UnaryOp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004058 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004059 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004060 value = ast2obj_unaryop(state, o->v.UnaryOp.op);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004061 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004062 if (PyObject_SetAttr(result, state->op, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004063 goto failed;
4064 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004065 value = ast2obj_expr(state, o->v.UnaryOp.operand);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004066 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004067 if (PyObject_SetAttr(result, state->operand, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004068 goto failed;
4069 Py_DECREF(value);
4070 break;
4071 case Lambda_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004072 tp = (PyTypeObject *)state->Lambda_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004073 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004074 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004075 value = ast2obj_arguments(state, o->v.Lambda.args);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004076 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004077 if (PyObject_SetAttr(result, state->args, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004078 goto failed;
4079 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004080 value = ast2obj_expr(state, o->v.Lambda.body);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004081 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004082 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004083 goto failed;
4084 Py_DECREF(value);
4085 break;
4086 case IfExp_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004087 tp = (PyTypeObject *)state->IfExp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004088 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004089 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004090 value = ast2obj_expr(state, o->v.IfExp.test);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004091 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004092 if (PyObject_SetAttr(result, state->test, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004093 goto failed;
4094 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004095 value = ast2obj_expr(state, o->v.IfExp.body);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004096 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004097 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004098 goto failed;
4099 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004100 value = ast2obj_expr(state, o->v.IfExp.orelse);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004101 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004102 if (PyObject_SetAttr(result, state->orelse, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004103 goto failed;
4104 Py_DECREF(value);
4105 break;
4106 case Dict_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004107 tp = (PyTypeObject *)state->Dict_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004108 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004109 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004110 value = ast2obj_list(state, (asdl_seq*)o->v.Dict.keys, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004111 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004112 if (PyObject_SetAttr(result, state->keys, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004113 goto failed;
4114 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004115 value = ast2obj_list(state, (asdl_seq*)o->v.Dict.values, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004116 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004117 if (PyObject_SetAttr(result, state->values, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004118 goto failed;
4119 Py_DECREF(value);
4120 break;
4121 case Set_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004122 tp = (PyTypeObject *)state->Set_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004123 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004124 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004125 value = ast2obj_list(state, (asdl_seq*)o->v.Set.elts, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004126 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004127 if (PyObject_SetAttr(result, state->elts, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004128 goto failed;
4129 Py_DECREF(value);
4130 break;
4131 case ListComp_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004132 tp = (PyTypeObject *)state->ListComp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004133 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004134 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004135 value = ast2obj_expr(state, o->v.ListComp.elt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004136 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004137 if (PyObject_SetAttr(result, state->elt, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004138 goto failed;
4139 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004140 value = ast2obj_list(state, (asdl_seq*)o->v.ListComp.generators,
Victor Stinner74419f02020-07-03 11:35:37 +02004141 ast2obj_comprehension);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004142 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004143 if (PyObject_SetAttr(result, state->generators, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004144 goto failed;
4145 Py_DECREF(value);
4146 break;
4147 case SetComp_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004148 tp = (PyTypeObject *)state->SetComp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004149 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004150 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004151 value = ast2obj_expr(state, o->v.SetComp.elt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004152 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004153 if (PyObject_SetAttr(result, state->elt, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004154 goto failed;
4155 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004156 value = ast2obj_list(state, (asdl_seq*)o->v.SetComp.generators,
Victor Stinner74419f02020-07-03 11:35:37 +02004157 ast2obj_comprehension);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004158 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004159 if (PyObject_SetAttr(result, state->generators, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004160 goto failed;
4161 Py_DECREF(value);
4162 break;
4163 case DictComp_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004164 tp = (PyTypeObject *)state->DictComp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004165 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004166 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004167 value = ast2obj_expr(state, o->v.DictComp.key);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004168 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004169 if (PyObject_SetAttr(result, state->key, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004170 goto failed;
4171 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004172 value = ast2obj_expr(state, o->v.DictComp.value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004173 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004174 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004175 goto failed;
4176 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004177 value = ast2obj_list(state, (asdl_seq*)o->v.DictComp.generators,
Victor Stinner74419f02020-07-03 11:35:37 +02004178 ast2obj_comprehension);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004179 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004180 if (PyObject_SetAttr(result, state->generators, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004181 goto failed;
4182 Py_DECREF(value);
4183 break;
4184 case GeneratorExp_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004185 tp = (PyTypeObject *)state->GeneratorExp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004186 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004187 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004188 value = ast2obj_expr(state, o->v.GeneratorExp.elt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004189 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004190 if (PyObject_SetAttr(result, state->elt, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004191 goto failed;
4192 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004193 value = ast2obj_list(state, (asdl_seq*)o->v.GeneratorExp.generators,
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004194 ast2obj_comprehension);
4195 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004196 if (PyObject_SetAttr(result, state->generators, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004197 goto failed;
4198 Py_DECREF(value);
4199 break;
Yury Selivanov75445082015-05-11 22:57:16 -04004200 case Await_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004201 tp = (PyTypeObject *)state->Await_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004202 result = PyType_GenericNew(tp, NULL, NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04004203 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004204 value = ast2obj_expr(state, o->v.Await.value);
Yury Selivanov75445082015-05-11 22:57:16 -04004205 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004206 if (PyObject_SetAttr(result, state->value, value) == -1)
Yury Selivanov75445082015-05-11 22:57:16 -04004207 goto failed;
4208 Py_DECREF(value);
4209 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004210 case Yield_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004211 tp = (PyTypeObject *)state->Yield_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004212 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004213 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004214 value = ast2obj_expr(state, o->v.Yield.value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004215 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004216 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004217 goto failed;
4218 Py_DECREF(value);
4219 break;
4220 case YieldFrom_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004221 tp = (PyTypeObject *)state->YieldFrom_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004222 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004223 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004224 value = ast2obj_expr(state, o->v.YieldFrom.value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004225 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004226 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004227 goto failed;
4228 Py_DECREF(value);
4229 break;
4230 case Compare_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004231 tp = (PyTypeObject *)state->Compare_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004232 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004233 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004234 value = ast2obj_expr(state, o->v.Compare.left);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004235 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004236 if (PyObject_SetAttr(result, state->left, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004237 goto failed;
4238 Py_DECREF(value);
4239 {
4240 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
4241 value = PyList_New(n);
4242 if (!value) goto failed;
4243 for(i = 0; i < n; i++)
Victor Stinner74419f02020-07-03 11:35:37 +02004244 PyList_SET_ITEM(value, i, ast2obj_cmpop(state, (cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004245 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00004246 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004247 if (PyObject_SetAttr(result, state->ops, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004248 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00004249 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004250 value = ast2obj_list(state, (asdl_seq*)o->v.Compare.comparators,
4251 ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00004252 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004253 if (PyObject_SetAttr(result, state->comparators, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004254 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00004255 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004256 break;
4257 case Call_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004258 tp = (PyTypeObject *)state->Call_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004259 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004260 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004261 value = ast2obj_expr(state, o->v.Call.func);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004262 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004263 if (PyObject_SetAttr(result, state->func, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004264 goto failed;
4265 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004266 value = ast2obj_list(state, (asdl_seq*)o->v.Call.args, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004267 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004268 if (PyObject_SetAttr(result, state->args, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004269 goto failed;
4270 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004271 value = ast2obj_list(state, (asdl_seq*)o->v.Call.keywords,
4272 ast2obj_keyword);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004273 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004274 if (PyObject_SetAttr(result, state->keywords, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004275 goto failed;
4276 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004277 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004278 case FormattedValue_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004279 tp = (PyTypeObject *)state->FormattedValue_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004280 result = PyType_GenericNew(tp, NULL, NULL);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004281 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004282 value = ast2obj_expr(state, o->v.FormattedValue.value);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004283 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004284 if (PyObject_SetAttr(result, state->value, value) == -1)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004285 goto failed;
4286 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004287 value = ast2obj_int(state, o->v.FormattedValue.conversion);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004288 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004289 if (PyObject_SetAttr(result, state->conversion, value) == -1)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004290 goto failed;
4291 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004292 value = ast2obj_expr(state, o->v.FormattedValue.format_spec);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004293 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004294 if (PyObject_SetAttr(result, state->format_spec, value) == -1)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004295 goto failed;
4296 Py_DECREF(value);
4297 break;
4298 case JoinedStr_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004299 tp = (PyTypeObject *)state->JoinedStr_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004300 result = PyType_GenericNew(tp, NULL, NULL);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004301 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004302 value = ast2obj_list(state, (asdl_seq*)o->v.JoinedStr.values,
4303 ast2obj_expr);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004304 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004305 if (PyObject_SetAttr(result, state->values, value) == -1)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004306 goto failed;
4307 Py_DECREF(value);
4308 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01004309 case Constant_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004310 tp = (PyTypeObject *)state->Constant_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004311 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01004312 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004313 value = ast2obj_constant(state, o->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01004314 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004315 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01004316 goto failed;
4317 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004318 value = ast2obj_string(state, o->v.Constant.kind);
Guido van Rossum10f8ce62019-03-13 13:00:46 -07004319 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004320 if (PyObject_SetAttr(result, state->kind, value) == -1)
Guido van Rossum10f8ce62019-03-13 13:00:46 -07004321 goto failed;
4322 Py_DECREF(value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01004323 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004324 case Attribute_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004325 tp = (PyTypeObject *)state->Attribute_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004326 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004327 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004328 value = ast2obj_expr(state, o->v.Attribute.value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004329 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004330 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004331 goto failed;
4332 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004333 value = ast2obj_identifier(state, o->v.Attribute.attr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004334 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004335 if (PyObject_SetAttr(result, state->attr, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004336 goto failed;
4337 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004338 value = ast2obj_expr_context(state, o->v.Attribute.ctx);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004339 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004340 if (PyObject_SetAttr(result, state->ctx, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004341 goto failed;
4342 Py_DECREF(value);
4343 break;
4344 case Subscript_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004345 tp = (PyTypeObject *)state->Subscript_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004346 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004347 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004348 value = ast2obj_expr(state, o->v.Subscript.value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004349 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004350 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004351 goto failed;
4352 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004353 value = ast2obj_expr(state, o->v.Subscript.slice);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004354 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004355 if (PyObject_SetAttr(result, state->slice, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004356 goto failed;
4357 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004358 value = ast2obj_expr_context(state, o->v.Subscript.ctx);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004359 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004360 if (PyObject_SetAttr(result, state->ctx, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004361 goto failed;
4362 Py_DECREF(value);
4363 break;
4364 case Starred_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004365 tp = (PyTypeObject *)state->Starred_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004366 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004367 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004368 value = ast2obj_expr(state, o->v.Starred.value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004369 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004370 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004371 goto failed;
4372 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004373 value = ast2obj_expr_context(state, o->v.Starred.ctx);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004374 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004375 if (PyObject_SetAttr(result, state->ctx, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004376 goto failed;
4377 Py_DECREF(value);
4378 break;
4379 case Name_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004380 tp = (PyTypeObject *)state->Name_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004381 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004382 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004383 value = ast2obj_identifier(state, o->v.Name.id);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004384 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004385 if (PyObject_SetAttr(result, state->id, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004386 goto failed;
4387 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004388 value = ast2obj_expr_context(state, o->v.Name.ctx);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004389 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004390 if (PyObject_SetAttr(result, state->ctx, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004391 goto failed;
4392 Py_DECREF(value);
4393 break;
4394 case List_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004395 tp = (PyTypeObject *)state->List_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004396 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004397 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004398 value = ast2obj_list(state, (asdl_seq*)o->v.List.elts, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004399 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004400 if (PyObject_SetAttr(result, state->elts, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004401 goto failed;
4402 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004403 value = ast2obj_expr_context(state, o->v.List.ctx);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004404 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004405 if (PyObject_SetAttr(result, state->ctx, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004406 goto failed;
4407 Py_DECREF(value);
4408 break;
4409 case Tuple_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004410 tp = (PyTypeObject *)state->Tuple_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004411 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004412 if (!result) goto failed;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004413 value = ast2obj_list(state, (asdl_seq*)o->v.Tuple.elts, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004414 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004415 if (PyObject_SetAttr(result, state->elts, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004416 goto failed;
4417 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004418 value = ast2obj_expr_context(state, o->v.Tuple.ctx);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004419 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004420 if (PyObject_SetAttr(result, state->ctx, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004421 goto failed;
4422 Py_DECREF(value);
4423 break;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004424 case Slice_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004425 tp = (PyTypeObject *)state->Slice_type;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004426 result = PyType_GenericNew(tp, NULL, NULL);
4427 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004428 value = ast2obj_expr(state, o->v.Slice.lower);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004429 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004430 if (PyObject_SetAttr(result, state->lower, value) == -1)
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004431 goto failed;
4432 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004433 value = ast2obj_expr(state, o->v.Slice.upper);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004434 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004435 if (PyObject_SetAttr(result, state->upper, value) == -1)
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004436 goto failed;
4437 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004438 value = ast2obj_expr(state, o->v.Slice.step);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004439 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004440 if (PyObject_SetAttr(result, state->step, value) == -1)
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004441 goto failed;
4442 Py_DECREF(value);
4443 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004444 }
Victor Stinner74419f02020-07-03 11:35:37 +02004445 value = ast2obj_int(state, o->lineno);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004446 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004447 if (PyObject_SetAttr(result, state->lineno, value) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004448 goto failed;
4449 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004450 value = ast2obj_int(state, o->col_offset);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004451 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004452 if (PyObject_SetAttr(result, state->col_offset, value) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004453 goto failed;
4454 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004455 value = ast2obj_int(state, o->end_lineno);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004456 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004457 if (PyObject_SetAttr(result, state->end_lineno, value) < 0)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004458 goto failed;
4459 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004460 value = ast2obj_int(state, o->end_col_offset);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004461 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004462 if (PyObject_SetAttr(result, state->end_col_offset, value) < 0)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004463 goto failed;
4464 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004465 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004466failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004467 Py_XDECREF(value);
4468 Py_XDECREF(result);
4469 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004470}
4471
Victor Stinner5cf47822020-11-02 22:03:28 +01004472PyObject* ast2obj_expr_context(struct ast_state *state, expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004473{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004474 switch(o) {
4475 case Load:
Victor Stinner74419f02020-07-03 11:35:37 +02004476 Py_INCREF(state->Load_singleton);
4477 return state->Load_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004478 case Store:
Victor Stinner74419f02020-07-03 11:35:37 +02004479 Py_INCREF(state->Store_singleton);
4480 return state->Store_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004481 case Del:
Victor Stinner74419f02020-07-03 11:35:37 +02004482 Py_INCREF(state->Del_singleton);
4483 return state->Del_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004484 }
Batuhan Taskaya091951a2020-05-06 17:29:32 +03004485 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004486}
Victor Stinner5cf47822020-11-02 22:03:28 +01004487PyObject* ast2obj_boolop(struct ast_state *state, boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004488{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004489 switch(o) {
4490 case And:
Victor Stinner74419f02020-07-03 11:35:37 +02004491 Py_INCREF(state->And_singleton);
4492 return state->And_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004493 case Or:
Victor Stinner74419f02020-07-03 11:35:37 +02004494 Py_INCREF(state->Or_singleton);
4495 return state->Or_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004496 }
Batuhan Taskaya091951a2020-05-06 17:29:32 +03004497 Py_UNREACHABLE();
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004498}
Victor Stinner5cf47822020-11-02 22:03:28 +01004499PyObject* ast2obj_operator(struct ast_state *state, operator_ty o)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004500{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004501 switch(o) {
4502 case Add:
Victor Stinner74419f02020-07-03 11:35:37 +02004503 Py_INCREF(state->Add_singleton);
4504 return state->Add_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004505 case Sub:
Victor Stinner74419f02020-07-03 11:35:37 +02004506 Py_INCREF(state->Sub_singleton);
4507 return state->Sub_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004508 case Mult:
Victor Stinner74419f02020-07-03 11:35:37 +02004509 Py_INCREF(state->Mult_singleton);
4510 return state->Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04004511 case MatMult:
Victor Stinner74419f02020-07-03 11:35:37 +02004512 Py_INCREF(state->MatMult_singleton);
4513 return state->MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004514 case Div:
Victor Stinner74419f02020-07-03 11:35:37 +02004515 Py_INCREF(state->Div_singleton);
4516 return state->Div_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004517 case Mod:
Victor Stinner74419f02020-07-03 11:35:37 +02004518 Py_INCREF(state->Mod_singleton);
4519 return state->Mod_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004520 case Pow:
Victor Stinner74419f02020-07-03 11:35:37 +02004521 Py_INCREF(state->Pow_singleton);
4522 return state->Pow_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004523 case LShift:
Victor Stinner74419f02020-07-03 11:35:37 +02004524 Py_INCREF(state->LShift_singleton);
4525 return state->LShift_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004526 case RShift:
Victor Stinner74419f02020-07-03 11:35:37 +02004527 Py_INCREF(state->RShift_singleton);
4528 return state->RShift_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004529 case BitOr:
Victor Stinner74419f02020-07-03 11:35:37 +02004530 Py_INCREF(state->BitOr_singleton);
4531 return state->BitOr_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004532 case BitXor:
Victor Stinner74419f02020-07-03 11:35:37 +02004533 Py_INCREF(state->BitXor_singleton);
4534 return state->BitXor_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004535 case BitAnd:
Victor Stinner74419f02020-07-03 11:35:37 +02004536 Py_INCREF(state->BitAnd_singleton);
4537 return state->BitAnd_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004538 case FloorDiv:
Victor Stinner74419f02020-07-03 11:35:37 +02004539 Py_INCREF(state->FloorDiv_singleton);
4540 return state->FloorDiv_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004541 }
Batuhan Taskaya091951a2020-05-06 17:29:32 +03004542 Py_UNREACHABLE();
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004543}
Victor Stinner5cf47822020-11-02 22:03:28 +01004544PyObject* ast2obj_unaryop(struct ast_state *state, unaryop_ty o)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004545{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004546 switch(o) {
4547 case Invert:
Victor Stinner74419f02020-07-03 11:35:37 +02004548 Py_INCREF(state->Invert_singleton);
4549 return state->Invert_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004550 case Not:
Victor Stinner74419f02020-07-03 11:35:37 +02004551 Py_INCREF(state->Not_singleton);
4552 return state->Not_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004553 case UAdd:
Victor Stinner74419f02020-07-03 11:35:37 +02004554 Py_INCREF(state->UAdd_singleton);
4555 return state->UAdd_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004556 case USub:
Victor Stinner74419f02020-07-03 11:35:37 +02004557 Py_INCREF(state->USub_singleton);
4558 return state->USub_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004559 }
Batuhan Taskaya091951a2020-05-06 17:29:32 +03004560 Py_UNREACHABLE();
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004561}
Victor Stinner5cf47822020-11-02 22:03:28 +01004562PyObject* ast2obj_cmpop(struct ast_state *state, cmpop_ty o)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004563{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004564 switch(o) {
4565 case Eq:
Victor Stinner74419f02020-07-03 11:35:37 +02004566 Py_INCREF(state->Eq_singleton);
4567 return state->Eq_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004568 case NotEq:
Victor Stinner74419f02020-07-03 11:35:37 +02004569 Py_INCREF(state->NotEq_singleton);
4570 return state->NotEq_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004571 case Lt:
Victor Stinner74419f02020-07-03 11:35:37 +02004572 Py_INCREF(state->Lt_singleton);
4573 return state->Lt_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004574 case LtE:
Victor Stinner74419f02020-07-03 11:35:37 +02004575 Py_INCREF(state->LtE_singleton);
4576 return state->LtE_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004577 case Gt:
Victor Stinner74419f02020-07-03 11:35:37 +02004578 Py_INCREF(state->Gt_singleton);
4579 return state->Gt_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004580 case GtE:
Victor Stinner74419f02020-07-03 11:35:37 +02004581 Py_INCREF(state->GtE_singleton);
4582 return state->GtE_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004583 case Is:
Victor Stinner74419f02020-07-03 11:35:37 +02004584 Py_INCREF(state->Is_singleton);
4585 return state->Is_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004586 case IsNot:
Victor Stinner74419f02020-07-03 11:35:37 +02004587 Py_INCREF(state->IsNot_singleton);
4588 return state->IsNot_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004589 case In:
Victor Stinner74419f02020-07-03 11:35:37 +02004590 Py_INCREF(state->In_singleton);
4591 return state->In_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004592 case NotIn:
Victor Stinner74419f02020-07-03 11:35:37 +02004593 Py_INCREF(state->NotIn_singleton);
4594 return state->NotIn_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004595 }
Batuhan Taskaya091951a2020-05-06 17:29:32 +03004596 Py_UNREACHABLE();
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004597}
4598PyObject*
Victor Stinner5cf47822020-11-02 22:03:28 +01004599ast2obj_comprehension(struct ast_state *state, void* _o)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004600{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004601 comprehension_ty o = (comprehension_ty)_o;
4602 PyObject *result = NULL, *value = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004603 PyTypeObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004604 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004605 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004606 }
Victor Stinner74419f02020-07-03 11:35:37 +02004607 tp = (PyTypeObject *)state->comprehension_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004608 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004609 if (!result) return NULL;
Victor Stinner74419f02020-07-03 11:35:37 +02004610 value = ast2obj_expr(state, o->target);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004611 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004612 if (PyObject_SetAttr(result, state->target, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004613 goto failed;
4614 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004615 value = ast2obj_expr(state, o->iter);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004616 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004617 if (PyObject_SetAttr(result, state->iter, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004618 goto failed;
4619 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004620 value = ast2obj_list(state, (asdl_seq*)o->ifs, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004621 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004622 if (PyObject_SetAttr(result, state->ifs, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004623 goto failed;
4624 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004625 value = ast2obj_int(state, o->is_async);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004626 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004627 if (PyObject_SetAttr(result, state->is_async, value) == -1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004628 goto failed;
4629 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004630 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004631failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004632 Py_XDECREF(value);
4633 Py_XDECREF(result);
4634 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004635}
4636
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004637PyObject*
Victor Stinner5cf47822020-11-02 22:03:28 +01004638ast2obj_excepthandler(struct ast_state *state, void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004639{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004640 excepthandler_ty o = (excepthandler_ty)_o;
4641 PyObject *result = NULL, *value = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004642 PyTypeObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004643 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004644 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004645 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004646 switch (o->kind) {
4647 case ExceptHandler_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004648 tp = (PyTypeObject *)state->ExceptHandler_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004649 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004650 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004651 value = ast2obj_expr(state, o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004652 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004653 if (PyObject_SetAttr(result, state->type, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004654 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004655 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004656 value = ast2obj_identifier(state, o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004657 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004658 if (PyObject_SetAttr(result, state->name, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004659 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004660 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004661 value = ast2obj_list(state, (asdl_seq*)o->v.ExceptHandler.body,
4662 ast2obj_stmt);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004663 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004664 if (PyObject_SetAttr(result, state->body, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004665 goto failed;
4666 Py_DECREF(value);
4667 break;
4668 }
Victor Stinner74419f02020-07-03 11:35:37 +02004669 value = ast2obj_int(state, o->lineno);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004670 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004671 if (PyObject_SetAttr(result, state->lineno, value) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004672 goto failed;
4673 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004674 value = ast2obj_int(state, o->col_offset);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004675 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004676 if (PyObject_SetAttr(result, state->col_offset, value) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004677 goto failed;
4678 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004679 value = ast2obj_int(state, o->end_lineno);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004680 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004681 if (PyObject_SetAttr(result, state->end_lineno, value) < 0)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004682 goto failed;
4683 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004684 value = ast2obj_int(state, o->end_col_offset);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004685 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004686 if (PyObject_SetAttr(result, state->end_col_offset, value) < 0)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004687 goto failed;
4688 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004689 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004690failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004691 Py_XDECREF(value);
4692 Py_XDECREF(result);
4693 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004694}
4695
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004696PyObject*
Victor Stinner5cf47822020-11-02 22:03:28 +01004697ast2obj_arguments(struct ast_state *state, void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004698{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004699 arguments_ty o = (arguments_ty)_o;
4700 PyObject *result = NULL, *value = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004701 PyTypeObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004702 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004703 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004704 }
Victor Stinner74419f02020-07-03 11:35:37 +02004705 tp = (PyTypeObject *)state->arguments_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004706 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004707 if (!result) return NULL;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004708 value = ast2obj_list(state, (asdl_seq*)o->posonlyargs, ast2obj_arg);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004709 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004710 if (PyObject_SetAttr(result, state->posonlyargs, value) == -1)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004711 goto failed;
4712 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004713 value = ast2obj_list(state, (asdl_seq*)o->args, ast2obj_arg);
Pablo Galindocd6e83b2019-07-15 01:32:18 +02004714 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004715 if (PyObject_SetAttr(result, state->args, value) == -1)
Pablo Galindocd6e83b2019-07-15 01:32:18 +02004716 goto failed;
4717 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004718 value = ast2obj_arg(state, o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004719 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004720 if (PyObject_SetAttr(result, state->vararg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004721 goto failed;
4722 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004723 value = ast2obj_list(state, (asdl_seq*)o->kwonlyargs, ast2obj_arg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004724 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004725 if (PyObject_SetAttr(result, state->kwonlyargs, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004726 goto failed;
4727 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004728 value = ast2obj_list(state, (asdl_seq*)o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004729 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004730 if (PyObject_SetAttr(result, state->kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004731 goto failed;
4732 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004733 value = ast2obj_arg(state, o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004734 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004735 if (PyObject_SetAttr(result, state->kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004736 goto failed;
4737 Py_DECREF(value);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004738 value = ast2obj_list(state, (asdl_seq*)o->defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004739 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004740 if (PyObject_SetAttr(result, state->defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004741 goto failed;
4742 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004743 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004744failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004745 Py_XDECREF(value);
4746 Py_XDECREF(result);
4747 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004748}
4749
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004750PyObject*
Victor Stinner5cf47822020-11-02 22:03:28 +01004751ast2obj_arg(struct ast_state *state, void* _o)
Neal Norwitzc1505362006-12-28 06:47:50 +00004752{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004753 arg_ty o = (arg_ty)_o;
4754 PyObject *result = NULL, *value = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004755 PyTypeObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004756 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004757 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004758 }
Victor Stinner74419f02020-07-03 11:35:37 +02004759 tp = (PyTypeObject *)state->arg_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004760 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004761 if (!result) return NULL;
Victor Stinner74419f02020-07-03 11:35:37 +02004762 value = ast2obj_identifier(state, o->arg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004763 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004764 if (PyObject_SetAttr(result, state->arg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004765 goto failed;
4766 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004767 value = ast2obj_expr(state, o->annotation);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004768 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004769 if (PyObject_SetAttr(result, state->annotation, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004770 goto failed;
4771 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004772 value = ast2obj_string(state, o->type_comment);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004773 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004774 if (PyObject_SetAttr(result, state->type_comment, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004775 goto failed;
4776 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004777 value = ast2obj_int(state, o->lineno);
Victor Stinneree4b59c2013-07-27 00:01:35 +02004778 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004779 if (PyObject_SetAttr(result, state->lineno, value) < 0)
Victor Stinneree4b59c2013-07-27 00:01:35 +02004780 goto failed;
4781 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004782 value = ast2obj_int(state, o->col_offset);
Victor Stinneree4b59c2013-07-27 00:01:35 +02004783 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004784 if (PyObject_SetAttr(result, state->col_offset, value) < 0)
Victor Stinneree4b59c2013-07-27 00:01:35 +02004785 goto failed;
4786 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004787 value = ast2obj_int(state, o->end_lineno);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004788 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004789 if (PyObject_SetAttr(result, state->end_lineno, value) < 0)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004790 goto failed;
4791 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004792 value = ast2obj_int(state, o->end_col_offset);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004793 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004794 if (PyObject_SetAttr(result, state->end_col_offset, value) < 0)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004795 goto failed;
4796 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004797 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00004798failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004799 Py_XDECREF(value);
4800 Py_XDECREF(result);
4801 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00004802}
4803
4804PyObject*
Victor Stinner5cf47822020-11-02 22:03:28 +01004805ast2obj_keyword(struct ast_state *state, void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004806{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004807 keyword_ty o = (keyword_ty)_o;
4808 PyObject *result = NULL, *value = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004809 PyTypeObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004810 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004811 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004812 }
Victor Stinner74419f02020-07-03 11:35:37 +02004813 tp = (PyTypeObject *)state->keyword_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004814 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004815 if (!result) return NULL;
Victor Stinner74419f02020-07-03 11:35:37 +02004816 value = ast2obj_identifier(state, o->arg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004817 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004818 if (PyObject_SetAttr(result, state->arg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004819 goto failed;
4820 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004821 value = ast2obj_expr(state, o->value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004822 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004823 if (PyObject_SetAttr(result, state->value, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004824 goto failed;
4825 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004826 value = ast2obj_int(state, o->lineno);
Pablo Galindo168660b2020-04-02 00:47:39 +01004827 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004828 if (PyObject_SetAttr(result, state->lineno, value) < 0)
Pablo Galindo168660b2020-04-02 00:47:39 +01004829 goto failed;
4830 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004831 value = ast2obj_int(state, o->col_offset);
Pablo Galindo168660b2020-04-02 00:47:39 +01004832 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004833 if (PyObject_SetAttr(result, state->col_offset, value) < 0)
Pablo Galindo168660b2020-04-02 00:47:39 +01004834 goto failed;
4835 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004836 value = ast2obj_int(state, o->end_lineno);
Pablo Galindo168660b2020-04-02 00:47:39 +01004837 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004838 if (PyObject_SetAttr(result, state->end_lineno, value) < 0)
Pablo Galindo168660b2020-04-02 00:47:39 +01004839 goto failed;
4840 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004841 value = ast2obj_int(state, o->end_col_offset);
Pablo Galindo168660b2020-04-02 00:47:39 +01004842 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004843 if (PyObject_SetAttr(result, state->end_col_offset, value) < 0)
Pablo Galindo168660b2020-04-02 00:47:39 +01004844 goto failed;
4845 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004846 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004847failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004848 Py_XDECREF(value);
4849 Py_XDECREF(result);
4850 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004851}
4852
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004853PyObject*
Victor Stinner5cf47822020-11-02 22:03:28 +01004854ast2obj_alias(struct ast_state *state, void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004855{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004856 alias_ty o = (alias_ty)_o;
4857 PyObject *result = NULL, *value = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004858 PyTypeObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004859 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004860 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004861 }
Victor Stinner74419f02020-07-03 11:35:37 +02004862 tp = (PyTypeObject *)state->alias_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004863 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004864 if (!result) return NULL;
Victor Stinner74419f02020-07-03 11:35:37 +02004865 value = ast2obj_identifier(state, o->name);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004866 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004867 if (PyObject_SetAttr(result, state->name, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004868 goto failed;
4869 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004870 value = ast2obj_identifier(state, o->asname);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004871 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004872 if (PyObject_SetAttr(result, state->asname, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004873 goto failed;
4874 Py_DECREF(value);
4875 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004876failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004877 Py_XDECREF(value);
4878 Py_XDECREF(result);
4879 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004880}
4881
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004882PyObject*
Victor Stinner5cf47822020-11-02 22:03:28 +01004883ast2obj_withitem(struct ast_state *state, void* _o)
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004884{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004885 withitem_ty o = (withitem_ty)_o;
4886 PyObject *result = NULL, *value = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004887 PyTypeObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004888 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004889 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004890 }
Victor Stinner74419f02020-07-03 11:35:37 +02004891 tp = (PyTypeObject *)state->withitem_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004892 result = PyType_GenericNew(tp, NULL, NULL);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004893 if (!result) return NULL;
Victor Stinner74419f02020-07-03 11:35:37 +02004894 value = ast2obj_expr(state, o->context_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004895 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004896 if (PyObject_SetAttr(result, state->context_expr, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004897 goto failed;
4898 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004899 value = ast2obj_expr(state, o->optional_vars);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004900 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004901 if (PyObject_SetAttr(result, state->optional_vars, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004902 goto failed;
4903 Py_DECREF(value);
4904 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004905failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004906 Py_XDECREF(value);
4907 Py_XDECREF(result);
4908 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004909}
4910
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004911PyObject*
Victor Stinner5cf47822020-11-02 22:03:28 +01004912ast2obj_type_ignore(struct ast_state *state, void* _o)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004913{
4914 type_ignore_ty o = (type_ignore_ty)_o;
4915 PyObject *result = NULL, *value = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004916 PyTypeObject *tp;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004917 if (!o) {
4918 Py_RETURN_NONE;
4919 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004920 switch (o->kind) {
4921 case TypeIgnore_kind:
Victor Stinner74419f02020-07-03 11:35:37 +02004922 tp = (PyTypeObject *)state->TypeIgnore_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004923 result = PyType_GenericNew(tp, NULL, NULL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004924 if (!result) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004925 value = ast2obj_int(state, o->v.TypeIgnore.lineno);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004926 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004927 if (PyObject_SetAttr(result, state->lineno, value) == -1)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004928 goto failed;
4929 Py_DECREF(value);
Victor Stinner74419f02020-07-03 11:35:37 +02004930 value = ast2obj_string(state, o->v.TypeIgnore.tag);
Michael J. Sullivan933e1502019-05-22 07:54:20 -07004931 if (!value) goto failed;
Victor Stinner74419f02020-07-03 11:35:37 +02004932 if (PyObject_SetAttr(result, state->tag, value) == -1)
Michael J. Sullivan933e1502019-05-22 07:54:20 -07004933 goto failed;
4934 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004935 break;
4936 }
4937 return result;
4938failed:
4939 Py_XDECREF(value);
4940 Py_XDECREF(result);
4941 return NULL;
4942}
4943
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004944
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004945int
Victor Stinner5cf47822020-11-02 22:03:28 +01004946obj2ast_mod(struct ast_state *state, PyObject* obj, mod_ty* out, PyArena* arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004947{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004948 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004949
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004950 PyObject *tmp = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004951 PyObject *tp;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004952
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004953 if (obj == Py_None) {
4954 *out = NULL;
4955 return 0;
4956 }
Victor Stinner74419f02020-07-03 11:35:37 +02004957 tp = state->Module_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07004958 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004959 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004960 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004961 }
4962 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01004963 asdl_stmt_seq* body;
4964 asdl_type_ignore_seq* type_ignores;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004965
Victor Stinner74419f02020-07-03 11:35:37 +02004966 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004967 return 1;
4968 }
4969 if (tmp == NULL) {
4970 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
4971 return 1;
4972 }
4973 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004974 int res;
4975 Py_ssize_t len;
4976 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004977 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07004978 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004979 goto failed;
4980 }
4981 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01004982 body = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004983 if (body == NULL) goto failed;
4984 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004985 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004986 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4987 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02004988 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004989 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004990 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004991 if (len != PyList_GET_SIZE(tmp)) {
4992 PyErr_SetString(PyExc_RuntimeError, "Module field \"body\" changed size during iteration");
4993 goto failed;
4994 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004995 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004996 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004997 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004998 }
Victor Stinner74419f02020-07-03 11:35:37 +02004999 if (_PyObject_LookupAttr(obj, state->type_ignores, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005000 return 1;
5001 }
5002 if (tmp == NULL) {
5003 PyErr_SetString(PyExc_TypeError, "required field \"type_ignores\" missing from Module");
5004 return 1;
5005 }
5006 else {
5007 int res;
5008 Py_ssize_t len;
5009 Py_ssize_t i;
5010 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005011 PyErr_Format(PyExc_TypeError, "Module field \"type_ignores\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005012 goto failed;
5013 }
5014 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005015 type_ignores = _Py_asdl_type_ignore_seq_new(len, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005016 if (type_ignores == NULL) goto failed;
5017 for (i = 0; i < len; i++) {
5018 type_ignore_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005019 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5020 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005021 res = obj2ast_type_ignore(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005022 Py_DECREF(tmp2);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005023 if (res != 0) goto failed;
5024 if (len != PyList_GET_SIZE(tmp)) {
5025 PyErr_SetString(PyExc_RuntimeError, "Module field \"type_ignores\" changed size during iteration");
5026 goto failed;
5027 }
5028 asdl_seq_SET(type_ignores, i, val);
5029 }
5030 Py_CLEAR(tmp);
5031 }
5032 *out = Module(body, type_ignores, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005033 if (*out == NULL) goto failed;
5034 return 0;
5035 }
Victor Stinner74419f02020-07-03 11:35:37 +02005036 tp = state->Interactive_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005037 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005038 if (isinstance == -1) {
5039 return 1;
5040 }
5041 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01005042 asdl_stmt_seq* body;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005043
Victor Stinner74419f02020-07-03 11:35:37 +02005044 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005045 return 1;
5046 }
5047 if (tmp == NULL) {
5048 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
5049 return 1;
5050 }
5051 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005052 int res;
5053 Py_ssize_t len;
5054 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005055 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005056 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005057 goto failed;
5058 }
5059 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005060 body = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005061 if (body == NULL) goto failed;
5062 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005063 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005064 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5065 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005066 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005067 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005068 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005069 if (len != PyList_GET_SIZE(tmp)) {
5070 PyErr_SetString(PyExc_RuntimeError, "Interactive field \"body\" changed size during iteration");
5071 goto failed;
5072 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005073 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005074 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005075 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005076 }
5077 *out = Interactive(body, arena);
5078 if (*out == NULL) goto failed;
5079 return 0;
5080 }
Victor Stinner74419f02020-07-03 11:35:37 +02005081 tp = state->Expression_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005082 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005083 if (isinstance == -1) {
5084 return 1;
5085 }
5086 if (isinstance) {
5087 expr_ty body;
5088
Victor Stinner74419f02020-07-03 11:35:37 +02005089 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005090 return 1;
5091 }
5092 if (tmp == NULL) {
5093 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
5094 return 1;
5095 }
5096 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005097 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005098 res = obj2ast_expr(state, tmp, &body, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005099 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005100 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005101 }
5102 *out = Expression(body, arena);
5103 if (*out == NULL) goto failed;
5104 return 0;
5105 }
Victor Stinner74419f02020-07-03 11:35:37 +02005106 tp = state->FunctionType_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005107 isinstance = PyObject_IsInstance(obj, tp);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005108 if (isinstance == -1) {
5109 return 1;
5110 }
5111 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01005112 asdl_expr_seq* argtypes;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005113 expr_ty returns;
5114
Victor Stinner74419f02020-07-03 11:35:37 +02005115 if (_PyObject_LookupAttr(obj, state->argtypes, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005116 return 1;
5117 }
5118 if (tmp == NULL) {
5119 PyErr_SetString(PyExc_TypeError, "required field \"argtypes\" missing from FunctionType");
5120 return 1;
5121 }
5122 else {
5123 int res;
5124 Py_ssize_t len;
5125 Py_ssize_t i;
5126 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005127 PyErr_Format(PyExc_TypeError, "FunctionType field \"argtypes\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005128 goto failed;
5129 }
5130 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005131 argtypes = _Py_asdl_expr_seq_new(len, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005132 if (argtypes == NULL) goto failed;
5133 for (i = 0; i < len; i++) {
5134 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005135 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5136 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005137 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005138 Py_DECREF(tmp2);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005139 if (res != 0) goto failed;
5140 if (len != PyList_GET_SIZE(tmp)) {
5141 PyErr_SetString(PyExc_RuntimeError, "FunctionType field \"argtypes\" changed size during iteration");
5142 goto failed;
5143 }
5144 asdl_seq_SET(argtypes, i, val);
5145 }
5146 Py_CLEAR(tmp);
5147 }
Victor Stinner74419f02020-07-03 11:35:37 +02005148 if (_PyObject_LookupAttr(obj, state->returns, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005149 return 1;
5150 }
5151 if (tmp == NULL) {
5152 PyErr_SetString(PyExc_TypeError, "required field \"returns\" missing from FunctionType");
5153 return 1;
5154 }
5155 else {
5156 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005157 res = obj2ast_expr(state, tmp, &returns, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005158 if (res != 0) goto failed;
5159 Py_CLEAR(tmp);
5160 }
5161 *out = FunctionType(argtypes, returns, arena);
5162 if (*out == NULL) goto failed;
5163 return 0;
5164 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005165
5166 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
5167 failed:
5168 Py_XDECREF(tmp);
5169 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005170}
5171
5172int
Victor Stinner5cf47822020-11-02 22:03:28 +01005173obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
5174 arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005175{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005176 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005177
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005178 PyObject *tmp = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005179 PyObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005180 int lineno;
5181 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005182 int end_lineno;
5183 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005184
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005185 if (obj == Py_None) {
5186 *out = NULL;
5187 return 0;
5188 }
Victor Stinner74419f02020-07-03 11:35:37 +02005189 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005190 return 1;
5191 }
5192 if (tmp == NULL) {
5193 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
5194 return 1;
5195 }
5196 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005197 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005198 res = obj2ast_int(state, tmp, &lineno, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005199 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005200 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005201 }
Victor Stinner74419f02020-07-03 11:35:37 +02005202 if (_PyObject_LookupAttr(obj, state->col_offset, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005203 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005204 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005205 if (tmp == NULL) {
5206 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
5207 return 1;
5208 }
5209 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005210 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005211 res = obj2ast_int(state, tmp, &col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005212 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005213 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005214 }
Victor Stinner74419f02020-07-03 11:35:37 +02005215 if (_PyObject_LookupAttr(obj, state->end_lineno, &tmp) < 0) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005216 return 1;
5217 }
5218 if (tmp == NULL || tmp == Py_None) {
5219 Py_CLEAR(tmp);
5220 end_lineno = 0;
5221 }
5222 else {
5223 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005224 res = obj2ast_int(state, tmp, &end_lineno, arena);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005225 if (res != 0) goto failed;
5226 Py_CLEAR(tmp);
5227 }
Victor Stinner74419f02020-07-03 11:35:37 +02005228 if (_PyObject_LookupAttr(obj, state->end_col_offset, &tmp) < 0) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005229 return 1;
5230 }
5231 if (tmp == NULL || tmp == Py_None) {
5232 Py_CLEAR(tmp);
5233 end_col_offset = 0;
5234 }
5235 else {
5236 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005237 res = obj2ast_int(state, tmp, &end_col_offset, arena);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005238 if (res != 0) goto failed;
5239 Py_CLEAR(tmp);
5240 }
Victor Stinner74419f02020-07-03 11:35:37 +02005241 tp = state->FunctionDef_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005242 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005243 if (isinstance == -1) {
5244 return 1;
5245 }
5246 if (isinstance) {
5247 identifier name;
5248 arguments_ty args;
Pablo Galindoa5634c42020-09-16 19:42:00 +01005249 asdl_stmt_seq* body;
5250 asdl_expr_seq* decorator_list;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005251 expr_ty returns;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005252 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005253
Victor Stinner74419f02020-07-03 11:35:37 +02005254 if (_PyObject_LookupAttr(obj, state->name, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005255 return 1;
5256 }
5257 if (tmp == NULL) {
5258 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
5259 return 1;
5260 }
5261 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005262 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005263 res = obj2ast_identifier(state, tmp, &name, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005264 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005265 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005266 }
Victor Stinner74419f02020-07-03 11:35:37 +02005267 if (_PyObject_LookupAttr(obj, state->args, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005268 return 1;
5269 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005270 if (tmp == NULL) {
5271 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
5272 return 1;
5273 }
5274 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005275 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005276 res = obj2ast_arguments(state, tmp, &args, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005277 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005278 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005279 }
Victor Stinner74419f02020-07-03 11:35:37 +02005280 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005281 return 1;
5282 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005283 if (tmp == NULL) {
5284 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
5285 return 1;
5286 }
5287 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005288 int res;
5289 Py_ssize_t len;
5290 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005291 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005292 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005293 goto failed;
5294 }
5295 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005296 body = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005297 if (body == NULL) goto failed;
5298 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005299 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005300 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5301 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005302 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005303 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005304 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005305 if (len != PyList_GET_SIZE(tmp)) {
5306 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"body\" changed size during iteration");
5307 goto failed;
5308 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005309 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005310 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005311 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005312 }
Victor Stinner74419f02020-07-03 11:35:37 +02005313 if (_PyObject_LookupAttr(obj, state->decorator_list, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005314 return 1;
5315 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005316 if (tmp == NULL) {
5317 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
5318 return 1;
5319 }
5320 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005321 int res;
5322 Py_ssize_t len;
5323 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005324 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005325 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005326 goto failed;
5327 }
5328 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005329 decorator_list = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005330 if (decorator_list == NULL) goto failed;
5331 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005332 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005333 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5334 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005335 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005336 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005337 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005338 if (len != PyList_GET_SIZE(tmp)) {
5339 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"decorator_list\" changed size during iteration");
5340 goto failed;
5341 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005342 asdl_seq_SET(decorator_list, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005343 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005344 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005345 }
Victor Stinner74419f02020-07-03 11:35:37 +02005346 if (_PyObject_LookupAttr(obj, state->returns, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005347 return 1;
5348 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005349 if (tmp == NULL || tmp == Py_None) {
5350 Py_CLEAR(tmp);
5351 returns = NULL;
5352 }
5353 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005354 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005355 res = obj2ast_expr(state, tmp, &returns, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005356 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005357 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005358 }
Victor Stinner74419f02020-07-03 11:35:37 +02005359 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005360 return 1;
5361 }
5362 if (tmp == NULL || tmp == Py_None) {
5363 Py_CLEAR(tmp);
5364 type_comment = NULL;
5365 }
5366 else {
5367 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005368 res = obj2ast_string(state, tmp, &type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005369 if (res != 0) goto failed;
5370 Py_CLEAR(tmp);
5371 }
5372 *out = FunctionDef(name, args, body, decorator_list, returns,
5373 type_comment, lineno, col_offset, end_lineno,
5374 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005375 if (*out == NULL) goto failed;
5376 return 0;
5377 }
Victor Stinner74419f02020-07-03 11:35:37 +02005378 tp = state->AsyncFunctionDef_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005379 isinstance = PyObject_IsInstance(obj, tp);
Yury Selivanov75445082015-05-11 22:57:16 -04005380 if (isinstance == -1) {
5381 return 1;
5382 }
5383 if (isinstance) {
5384 identifier name;
5385 arguments_ty args;
Pablo Galindoa5634c42020-09-16 19:42:00 +01005386 asdl_stmt_seq* body;
5387 asdl_expr_seq* decorator_list;
Yury Selivanov75445082015-05-11 22:57:16 -04005388 expr_ty returns;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005389 string type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04005390
Victor Stinner74419f02020-07-03 11:35:37 +02005391 if (_PyObject_LookupAttr(obj, state->name, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005392 return 1;
5393 }
5394 if (tmp == NULL) {
5395 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
5396 return 1;
5397 }
5398 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005399 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005400 res = obj2ast_identifier(state, tmp, &name, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005401 if (res != 0) goto failed;
5402 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005403 }
Victor Stinner74419f02020-07-03 11:35:37 +02005404 if (_PyObject_LookupAttr(obj, state->args, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005405 return 1;
5406 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005407 if (tmp == NULL) {
5408 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
5409 return 1;
5410 }
5411 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005412 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005413 res = obj2ast_arguments(state, tmp, &args, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005414 if (res != 0) goto failed;
5415 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005416 }
Victor Stinner74419f02020-07-03 11:35:37 +02005417 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005418 return 1;
5419 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005420 if (tmp == NULL) {
5421 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
5422 return 1;
5423 }
5424 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005425 int res;
5426 Py_ssize_t len;
5427 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04005428 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005429 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Yury Selivanov75445082015-05-11 22:57:16 -04005430 goto failed;
5431 }
5432 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005433 body = _Py_asdl_stmt_seq_new(len, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005434 if (body == NULL) goto failed;
5435 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005436 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005437 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5438 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005439 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005440 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04005441 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005442 if (len != PyList_GET_SIZE(tmp)) {
5443 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"body\" changed size during iteration");
5444 goto failed;
5445 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005446 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005447 }
5448 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005449 }
Victor Stinner74419f02020-07-03 11:35:37 +02005450 if (_PyObject_LookupAttr(obj, state->decorator_list, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005451 return 1;
5452 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005453 if (tmp == NULL) {
5454 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
5455 return 1;
5456 }
5457 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005458 int res;
5459 Py_ssize_t len;
5460 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04005461 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005462 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Yury Selivanov75445082015-05-11 22:57:16 -04005463 goto failed;
5464 }
5465 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005466 decorator_list = _Py_asdl_expr_seq_new(len, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005467 if (decorator_list == NULL) goto failed;
5468 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005469 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005470 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5471 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005472 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005473 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04005474 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005475 if (len != PyList_GET_SIZE(tmp)) {
5476 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"decorator_list\" changed size during iteration");
5477 goto failed;
5478 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005479 asdl_seq_SET(decorator_list, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005480 }
5481 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005482 }
Victor Stinner74419f02020-07-03 11:35:37 +02005483 if (_PyObject_LookupAttr(obj, state->returns, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005484 return 1;
5485 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005486 if (tmp == NULL || tmp == Py_None) {
5487 Py_CLEAR(tmp);
5488 returns = NULL;
5489 }
5490 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005491 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005492 res = obj2ast_expr(state, tmp, &returns, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005493 if (res != 0) goto failed;
5494 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04005495 }
Victor Stinner74419f02020-07-03 11:35:37 +02005496 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005497 return 1;
5498 }
5499 if (tmp == NULL || tmp == Py_None) {
5500 Py_CLEAR(tmp);
5501 type_comment = NULL;
5502 }
5503 else {
5504 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005505 res = obj2ast_string(state, tmp, &type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005506 if (res != 0) goto failed;
5507 Py_CLEAR(tmp);
5508 }
Yury Selivanov75445082015-05-11 22:57:16 -04005509 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005510 type_comment, lineno, col_offset, end_lineno,
5511 end_col_offset, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005512 if (*out == NULL) goto failed;
5513 return 0;
5514 }
Victor Stinner74419f02020-07-03 11:35:37 +02005515 tp = state->ClassDef_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005516 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005517 if (isinstance == -1) {
5518 return 1;
5519 }
5520 if (isinstance) {
5521 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01005522 asdl_expr_seq* bases;
5523 asdl_keyword_seq* keywords;
5524 asdl_stmt_seq* body;
5525 asdl_expr_seq* decorator_list;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005526
Victor Stinner74419f02020-07-03 11:35:37 +02005527 if (_PyObject_LookupAttr(obj, state->name, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005528 return 1;
5529 }
5530 if (tmp == NULL) {
5531 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
5532 return 1;
5533 }
5534 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005535 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005536 res = obj2ast_identifier(state, tmp, &name, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005537 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005538 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005539 }
Victor Stinner74419f02020-07-03 11:35:37 +02005540 if (_PyObject_LookupAttr(obj, state->bases, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005541 return 1;
5542 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005543 if (tmp == NULL) {
5544 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
5545 return 1;
5546 }
5547 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005548 int res;
5549 Py_ssize_t len;
5550 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005551 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005552 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005553 goto failed;
5554 }
5555 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005556 bases = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005557 if (bases == NULL) goto failed;
5558 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005559 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005560 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5561 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005562 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005563 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005564 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005565 if (len != PyList_GET_SIZE(tmp)) {
5566 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"bases\" changed size during iteration");
5567 goto failed;
5568 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005569 asdl_seq_SET(bases, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005570 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005571 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005572 }
Victor Stinner74419f02020-07-03 11:35:37 +02005573 if (_PyObject_LookupAttr(obj, state->keywords, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005574 return 1;
5575 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005576 if (tmp == NULL) {
5577 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
5578 return 1;
5579 }
5580 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005581 int res;
5582 Py_ssize_t len;
5583 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005584 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005585 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005586 goto failed;
5587 }
5588 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005589 keywords = _Py_asdl_keyword_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005590 if (keywords == NULL) goto failed;
5591 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005592 keyword_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005593 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5594 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005595 res = obj2ast_keyword(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005596 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005597 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005598 if (len != PyList_GET_SIZE(tmp)) {
5599 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"keywords\" changed size during iteration");
5600 goto failed;
5601 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005602 asdl_seq_SET(keywords, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005603 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005604 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005605 }
Victor Stinner74419f02020-07-03 11:35:37 +02005606 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005607 return 1;
5608 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005609 if (tmp == NULL) {
5610 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
5611 return 1;
5612 }
5613 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005614 int res;
5615 Py_ssize_t len;
5616 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005617 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005618 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005619 goto failed;
5620 }
5621 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005622 body = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005623 if (body == NULL) goto failed;
5624 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005625 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005626 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5627 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005628 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005629 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005630 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005631 if (len != PyList_GET_SIZE(tmp)) {
5632 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"body\" changed size during iteration");
5633 goto failed;
5634 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005635 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005636 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005637 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005638 }
Victor Stinner74419f02020-07-03 11:35:37 +02005639 if (_PyObject_LookupAttr(obj, state->decorator_list, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005640 return 1;
5641 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005642 if (tmp == NULL) {
5643 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
5644 return 1;
5645 }
5646 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005647 int res;
5648 Py_ssize_t len;
5649 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005650 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005651 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005652 goto failed;
5653 }
5654 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005655 decorator_list = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005656 if (decorator_list == NULL) goto failed;
5657 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005658 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005659 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5660 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005661 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005662 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005663 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005664 if (len != PyList_GET_SIZE(tmp)) {
5665 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"decorator_list\" changed size during iteration");
5666 goto failed;
5667 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005668 asdl_seq_SET(decorator_list, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005669 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005670 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005671 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03005672 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005673 col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005674 if (*out == NULL) goto failed;
5675 return 0;
5676 }
Victor Stinner74419f02020-07-03 11:35:37 +02005677 tp = state->Return_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005678 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005679 if (isinstance == -1) {
5680 return 1;
5681 }
5682 if (isinstance) {
5683 expr_ty value;
5684
Victor Stinner74419f02020-07-03 11:35:37 +02005685 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005686 return 1;
5687 }
5688 if (tmp == NULL || tmp == Py_None) {
5689 Py_CLEAR(tmp);
5690 value = NULL;
5691 }
5692 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005693 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005694 res = obj2ast_expr(state, tmp, &value, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005695 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005696 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005697 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005698 *out = Return(value, lineno, col_offset, end_lineno, end_col_offset,
5699 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005700 if (*out == NULL) goto failed;
5701 return 0;
5702 }
Victor Stinner74419f02020-07-03 11:35:37 +02005703 tp = state->Delete_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005704 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005705 if (isinstance == -1) {
5706 return 1;
5707 }
5708 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01005709 asdl_expr_seq* targets;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005710
Victor Stinner74419f02020-07-03 11:35:37 +02005711 if (_PyObject_LookupAttr(obj, state->targets, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005712 return 1;
5713 }
5714 if (tmp == NULL) {
5715 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
5716 return 1;
5717 }
5718 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005719 int res;
5720 Py_ssize_t len;
5721 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005722 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005723 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005724 goto failed;
5725 }
5726 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005727 targets = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005728 if (targets == NULL) goto failed;
5729 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005730 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005731 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5732 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005733 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005734 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005735 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005736 if (len != PyList_GET_SIZE(tmp)) {
5737 PyErr_SetString(PyExc_RuntimeError, "Delete field \"targets\" changed size during iteration");
5738 goto failed;
5739 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005740 asdl_seq_SET(targets, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005741 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005742 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005743 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005744 *out = Delete(targets, lineno, col_offset, end_lineno, end_col_offset,
5745 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005746 if (*out == NULL) goto failed;
5747 return 0;
5748 }
Victor Stinner74419f02020-07-03 11:35:37 +02005749 tp = state->Assign_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005750 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005751 if (isinstance == -1) {
5752 return 1;
5753 }
5754 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01005755 asdl_expr_seq* targets;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005756 expr_ty value;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005757 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005758
Victor Stinner74419f02020-07-03 11:35:37 +02005759 if (_PyObject_LookupAttr(obj, state->targets, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005760 return 1;
5761 }
5762 if (tmp == NULL) {
5763 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
5764 return 1;
5765 }
5766 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005767 int res;
5768 Py_ssize_t len;
5769 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005770 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005771 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005772 goto failed;
5773 }
5774 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005775 targets = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005776 if (targets == NULL) goto failed;
5777 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005778 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005779 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5780 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02005781 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005782 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005783 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005784 if (len != PyList_GET_SIZE(tmp)) {
5785 PyErr_SetString(PyExc_RuntimeError, "Assign field \"targets\" changed size during iteration");
5786 goto failed;
5787 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005788 asdl_seq_SET(targets, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005789 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005790 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005791 }
Victor Stinner74419f02020-07-03 11:35:37 +02005792 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005793 return 1;
5794 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005795 if (tmp == NULL) {
5796 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
5797 return 1;
5798 }
5799 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005800 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005801 res = obj2ast_expr(state, tmp, &value, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005802 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005803 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005804 }
Victor Stinner74419f02020-07-03 11:35:37 +02005805 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005806 return 1;
5807 }
5808 if (tmp == NULL || tmp == Py_None) {
5809 Py_CLEAR(tmp);
5810 type_comment = NULL;
5811 }
5812 else {
5813 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005814 res = obj2ast_string(state, tmp, &type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005815 if (res != 0) goto failed;
5816 Py_CLEAR(tmp);
5817 }
5818 *out = Assign(targets, value, type_comment, lineno, col_offset,
5819 end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005820 if (*out == NULL) goto failed;
5821 return 0;
5822 }
Victor Stinner74419f02020-07-03 11:35:37 +02005823 tp = state->AugAssign_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005824 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005825 if (isinstance == -1) {
5826 return 1;
5827 }
5828 if (isinstance) {
5829 expr_ty target;
5830 operator_ty op;
5831 expr_ty value;
5832
Victor Stinner74419f02020-07-03 11:35:37 +02005833 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005834 return 1;
5835 }
5836 if (tmp == NULL) {
5837 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
5838 return 1;
5839 }
5840 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005841 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005842 res = obj2ast_expr(state, tmp, &target, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005843 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005844 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005845 }
Victor Stinner74419f02020-07-03 11:35:37 +02005846 if (_PyObject_LookupAttr(obj, state->op, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005847 return 1;
5848 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005849 if (tmp == NULL) {
5850 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
5851 return 1;
5852 }
5853 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005854 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005855 res = obj2ast_operator(state, tmp, &op, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005856 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005857 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005858 }
Victor Stinner74419f02020-07-03 11:35:37 +02005859 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005860 return 1;
5861 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005862 if (tmp == NULL) {
5863 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
5864 return 1;
5865 }
5866 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005867 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005868 res = obj2ast_expr(state, tmp, &value, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005869 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005870 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005871 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005872 *out = AugAssign(target, op, value, lineno, col_offset, end_lineno,
5873 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005874 if (*out == NULL) goto failed;
5875 return 0;
5876 }
Victor Stinner74419f02020-07-03 11:35:37 +02005877 tp = state->AnnAssign_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005878 isinstance = PyObject_IsInstance(obj, tp);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005879 if (isinstance == -1) {
5880 return 1;
5881 }
5882 if (isinstance) {
5883 expr_ty target;
5884 expr_ty annotation;
5885 expr_ty value;
5886 int simple;
5887
Victor Stinner74419f02020-07-03 11:35:37 +02005888 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005889 return 1;
5890 }
5891 if (tmp == NULL) {
5892 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AnnAssign");
5893 return 1;
5894 }
5895 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005896 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005897 res = obj2ast_expr(state, tmp, &target, arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005898 if (res != 0) goto failed;
5899 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005900 }
Victor Stinner74419f02020-07-03 11:35:37 +02005901 if (_PyObject_LookupAttr(obj, state->annotation, &tmp) < 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005902 return 1;
5903 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005904 if (tmp == NULL) {
5905 PyErr_SetString(PyExc_TypeError, "required field \"annotation\" missing from AnnAssign");
5906 return 1;
5907 }
5908 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005909 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005910 res = obj2ast_expr(state, tmp, &annotation, arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005911 if (res != 0) goto failed;
5912 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005913 }
Victor Stinner74419f02020-07-03 11:35:37 +02005914 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005915 return 1;
5916 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005917 if (tmp == NULL || tmp == Py_None) {
5918 Py_CLEAR(tmp);
5919 value = NULL;
5920 }
5921 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005922 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005923 res = obj2ast_expr(state, tmp, &value, arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005924 if (res != 0) goto failed;
5925 Py_CLEAR(tmp);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005926 }
Victor Stinner74419f02020-07-03 11:35:37 +02005927 if (_PyObject_LookupAttr(obj, state->simple, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005928 return 1;
5929 }
5930 if (tmp == NULL) {
5931 PyErr_SetString(PyExc_TypeError, "required field \"simple\" missing from AnnAssign");
5932 return 1;
5933 }
5934 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005935 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005936 res = obj2ast_int(state, tmp, &simple, arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005937 if (res != 0) goto failed;
5938 Py_CLEAR(tmp);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005939 }
5940 *out = AnnAssign(target, annotation, value, simple, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005941 end_lineno, end_col_offset, arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005942 if (*out == NULL) goto failed;
5943 return 0;
5944 }
Victor Stinner74419f02020-07-03 11:35:37 +02005945 tp = state->For_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07005946 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005947 if (isinstance == -1) {
5948 return 1;
5949 }
5950 if (isinstance) {
5951 expr_ty target;
5952 expr_ty iter;
Pablo Galindoa5634c42020-09-16 19:42:00 +01005953 asdl_stmt_seq* body;
5954 asdl_stmt_seq* orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005955 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005956
Victor Stinner74419f02020-07-03 11:35:37 +02005957 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005958 return 1;
5959 }
5960 if (tmp == NULL) {
5961 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
5962 return 1;
5963 }
5964 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005965 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005966 res = obj2ast_expr(state, tmp, &target, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005967 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005968 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005969 }
Victor Stinner74419f02020-07-03 11:35:37 +02005970 if (_PyObject_LookupAttr(obj, state->iter, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005971 return 1;
5972 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005973 if (tmp == NULL) {
5974 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
5975 return 1;
5976 }
5977 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005978 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02005979 res = obj2ast_expr(state, tmp, &iter, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005980 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005981 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005982 }
Victor Stinner74419f02020-07-03 11:35:37 +02005983 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005984 return 1;
5985 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005986 if (tmp == NULL) {
5987 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
5988 return 1;
5989 }
5990 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005991 int res;
5992 Py_ssize_t len;
5993 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005994 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07005995 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005996 goto failed;
5997 }
5998 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01005999 body = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006000 if (body == NULL) goto failed;
6001 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006002 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006003 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6004 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006005 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006006 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006007 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006008 if (len != PyList_GET_SIZE(tmp)) {
6009 PyErr_SetString(PyExc_RuntimeError, "For field \"body\" changed size during iteration");
6010 goto failed;
6011 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006012 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006013 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006014 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006015 }
Victor Stinner74419f02020-07-03 11:35:37 +02006016 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006017 return 1;
6018 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006019 if (tmp == NULL) {
6020 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
6021 return 1;
6022 }
6023 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006024 int res;
6025 Py_ssize_t len;
6026 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006027 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006028 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006029 goto failed;
6030 }
6031 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006032 orelse = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006033 if (orelse == NULL) goto failed;
6034 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006035 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006036 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6037 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006038 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006039 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006040 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006041 if (len != PyList_GET_SIZE(tmp)) {
6042 PyErr_SetString(PyExc_RuntimeError, "For field \"orelse\" changed size during iteration");
6043 goto failed;
6044 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006045 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006046 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006047 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006048 }
Victor Stinner74419f02020-07-03 11:35:37 +02006049 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08006050 return 1;
6051 }
6052 if (tmp == NULL || tmp == Py_None) {
6053 Py_CLEAR(tmp);
6054 type_comment = NULL;
6055 }
6056 else {
6057 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006058 res = obj2ast_string(state, tmp, &type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08006059 if (res != 0) goto failed;
6060 Py_CLEAR(tmp);
6061 }
6062 *out = For(target, iter, body, orelse, type_comment, lineno,
6063 col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006064 if (*out == NULL) goto failed;
6065 return 0;
6066 }
Victor Stinner74419f02020-07-03 11:35:37 +02006067 tp = state->AsyncFor_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006068 isinstance = PyObject_IsInstance(obj, tp);
Yury Selivanov75445082015-05-11 22:57:16 -04006069 if (isinstance == -1) {
6070 return 1;
6071 }
6072 if (isinstance) {
6073 expr_ty target;
6074 expr_ty iter;
Pablo Galindoa5634c42020-09-16 19:42:00 +01006075 asdl_stmt_seq* body;
6076 asdl_stmt_seq* orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08006077 string type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04006078
Victor Stinner74419f02020-07-03 11:35:37 +02006079 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006080 return 1;
6081 }
6082 if (tmp == NULL) {
6083 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
6084 return 1;
6085 }
6086 else {
Yury Selivanov75445082015-05-11 22:57:16 -04006087 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006088 res = obj2ast_expr(state, tmp, &target, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04006089 if (res != 0) goto failed;
6090 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006091 }
Victor Stinner74419f02020-07-03 11:35:37 +02006092 if (_PyObject_LookupAttr(obj, state->iter, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04006093 return 1;
6094 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006095 if (tmp == NULL) {
6096 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
6097 return 1;
6098 }
6099 else {
Yury Selivanov75445082015-05-11 22:57:16 -04006100 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006101 res = obj2ast_expr(state, tmp, &iter, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04006102 if (res != 0) goto failed;
6103 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006104 }
Victor Stinner74419f02020-07-03 11:35:37 +02006105 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04006106 return 1;
6107 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006108 if (tmp == NULL) {
6109 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
6110 return 1;
6111 }
6112 else {
Yury Selivanov75445082015-05-11 22:57:16 -04006113 int res;
6114 Py_ssize_t len;
6115 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04006116 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006117 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Yury Selivanov75445082015-05-11 22:57:16 -04006118 goto failed;
6119 }
6120 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006121 body = _Py_asdl_stmt_seq_new(len, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04006122 if (body == NULL) goto failed;
6123 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006124 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006125 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6126 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006127 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006128 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04006129 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006130 if (len != PyList_GET_SIZE(tmp)) {
6131 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"body\" changed size during iteration");
6132 goto failed;
6133 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006134 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04006135 }
6136 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006137 }
Victor Stinner74419f02020-07-03 11:35:37 +02006138 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04006139 return 1;
6140 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006141 if (tmp == NULL) {
6142 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
6143 return 1;
6144 }
6145 else {
Yury Selivanov75445082015-05-11 22:57:16 -04006146 int res;
6147 Py_ssize_t len;
6148 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04006149 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006150 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Yury Selivanov75445082015-05-11 22:57:16 -04006151 goto failed;
6152 }
6153 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006154 orelse = _Py_asdl_stmt_seq_new(len, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04006155 if (orelse == NULL) goto failed;
6156 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006157 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006158 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6159 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006160 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006161 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04006162 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006163 if (len != PyList_GET_SIZE(tmp)) {
6164 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"orelse\" changed size during iteration");
6165 goto failed;
6166 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006167 asdl_seq_SET(orelse, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04006168 }
6169 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04006170 }
Victor Stinner74419f02020-07-03 11:35:37 +02006171 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08006172 return 1;
6173 }
6174 if (tmp == NULL || tmp == Py_None) {
6175 Py_CLEAR(tmp);
6176 type_comment = NULL;
6177 }
6178 else {
6179 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006180 res = obj2ast_string(state, tmp, &type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08006181 if (res != 0) goto failed;
6182 Py_CLEAR(tmp);
6183 }
6184 *out = AsyncFor(target, iter, body, orelse, type_comment, lineno,
6185 col_offset, end_lineno, end_col_offset, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04006186 if (*out == NULL) goto failed;
6187 return 0;
6188 }
Victor Stinner74419f02020-07-03 11:35:37 +02006189 tp = state->While_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006190 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006191 if (isinstance == -1) {
6192 return 1;
6193 }
6194 if (isinstance) {
6195 expr_ty test;
Pablo Galindoa5634c42020-09-16 19:42:00 +01006196 asdl_stmt_seq* body;
6197 asdl_stmt_seq* orelse;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006198
Victor Stinner74419f02020-07-03 11:35:37 +02006199 if (_PyObject_LookupAttr(obj, state->test, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006200 return 1;
6201 }
6202 if (tmp == NULL) {
6203 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
6204 return 1;
6205 }
6206 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006207 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006208 res = obj2ast_expr(state, tmp, &test, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006209 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006210 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006211 }
Victor Stinner74419f02020-07-03 11:35:37 +02006212 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006213 return 1;
6214 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006215 if (tmp == NULL) {
6216 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
6217 return 1;
6218 }
6219 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006220 int res;
6221 Py_ssize_t len;
6222 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006223 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006224 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006225 goto failed;
6226 }
6227 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006228 body = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006229 if (body == NULL) goto failed;
6230 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006231 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006232 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6233 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006234 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006235 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006236 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006237 if (len != PyList_GET_SIZE(tmp)) {
6238 PyErr_SetString(PyExc_RuntimeError, "While field \"body\" changed size during iteration");
6239 goto failed;
6240 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006241 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006242 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006243 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006244 }
Victor Stinner74419f02020-07-03 11:35:37 +02006245 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006246 return 1;
6247 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006248 if (tmp == NULL) {
6249 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
6250 return 1;
6251 }
6252 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006253 int res;
6254 Py_ssize_t len;
6255 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006256 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006257 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006258 goto failed;
6259 }
6260 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006261 orelse = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006262 if (orelse == NULL) goto failed;
6263 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006264 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006265 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6266 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006267 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006268 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006269 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006270 if (len != PyList_GET_SIZE(tmp)) {
6271 PyErr_SetString(PyExc_RuntimeError, "While field \"orelse\" changed size during iteration");
6272 goto failed;
6273 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006274 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006275 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006276 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006277 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006278 *out = While(test, body, orelse, lineno, col_offset, end_lineno,
6279 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006280 if (*out == NULL) goto failed;
6281 return 0;
6282 }
Victor Stinner74419f02020-07-03 11:35:37 +02006283 tp = state->If_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006284 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006285 if (isinstance == -1) {
6286 return 1;
6287 }
6288 if (isinstance) {
6289 expr_ty test;
Pablo Galindoa5634c42020-09-16 19:42:00 +01006290 asdl_stmt_seq* body;
6291 asdl_stmt_seq* orelse;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006292
Victor Stinner74419f02020-07-03 11:35:37 +02006293 if (_PyObject_LookupAttr(obj, state->test, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006294 return 1;
6295 }
6296 if (tmp == NULL) {
6297 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
6298 return 1;
6299 }
6300 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006301 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006302 res = obj2ast_expr(state, tmp, &test, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006303 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006304 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006305 }
Victor Stinner74419f02020-07-03 11:35:37 +02006306 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006307 return 1;
6308 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006309 if (tmp == NULL) {
6310 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
6311 return 1;
6312 }
6313 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006314 int res;
6315 Py_ssize_t len;
6316 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006317 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006318 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006319 goto failed;
6320 }
6321 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006322 body = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006323 if (body == NULL) goto failed;
6324 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006325 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006326 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6327 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006328 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006329 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006330 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006331 if (len != PyList_GET_SIZE(tmp)) {
6332 PyErr_SetString(PyExc_RuntimeError, "If field \"body\" changed size during iteration");
6333 goto failed;
6334 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006335 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006336 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006337 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006338 }
Victor Stinner74419f02020-07-03 11:35:37 +02006339 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006340 return 1;
6341 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006342 if (tmp == NULL) {
6343 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
6344 return 1;
6345 }
6346 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006347 int res;
6348 Py_ssize_t len;
6349 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006350 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006351 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006352 goto failed;
6353 }
6354 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006355 orelse = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006356 if (orelse == NULL) goto failed;
6357 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006358 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006359 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6360 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006361 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006362 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006363 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006364 if (len != PyList_GET_SIZE(tmp)) {
6365 PyErr_SetString(PyExc_RuntimeError, "If field \"orelse\" changed size during iteration");
6366 goto failed;
6367 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006368 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006369 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006370 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006371 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006372 *out = If(test, body, orelse, lineno, col_offset, end_lineno,
6373 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006374 if (*out == NULL) goto failed;
6375 return 0;
6376 }
Victor Stinner74419f02020-07-03 11:35:37 +02006377 tp = state->With_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006378 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006379 if (isinstance == -1) {
6380 return 1;
6381 }
6382 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01006383 asdl_withitem_seq* items;
6384 asdl_stmt_seq* body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08006385 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006386
Victor Stinner74419f02020-07-03 11:35:37 +02006387 if (_PyObject_LookupAttr(obj, state->items, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006388 return 1;
6389 }
6390 if (tmp == NULL) {
6391 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
6392 return 1;
6393 }
6394 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006395 int res;
6396 Py_ssize_t len;
6397 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006398 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006399 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006400 goto failed;
6401 }
6402 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006403 items = _Py_asdl_withitem_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006404 if (items == NULL) goto failed;
6405 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006406 withitem_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006407 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6408 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006409 res = obj2ast_withitem(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006410 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006411 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006412 if (len != PyList_GET_SIZE(tmp)) {
6413 PyErr_SetString(PyExc_RuntimeError, "With field \"items\" changed size during iteration");
6414 goto failed;
6415 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006416 asdl_seq_SET(items, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006417 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006418 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006419 }
Victor Stinner74419f02020-07-03 11:35:37 +02006420 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006421 return 1;
6422 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006423 if (tmp == NULL) {
6424 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
6425 return 1;
6426 }
6427 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006428 int res;
6429 Py_ssize_t len;
6430 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006431 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006432 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006433 goto failed;
6434 }
6435 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006436 body = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006437 if (body == NULL) goto failed;
6438 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006439 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006440 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6441 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006442 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006443 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006444 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006445 if (len != PyList_GET_SIZE(tmp)) {
6446 PyErr_SetString(PyExc_RuntimeError, "With field \"body\" changed size during iteration");
6447 goto failed;
6448 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006449 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006450 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006451 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006452 }
Victor Stinner74419f02020-07-03 11:35:37 +02006453 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08006454 return 1;
6455 }
6456 if (tmp == NULL || tmp == Py_None) {
6457 Py_CLEAR(tmp);
6458 type_comment = NULL;
6459 }
6460 else {
6461 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006462 res = obj2ast_string(state, tmp, &type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08006463 if (res != 0) goto failed;
6464 Py_CLEAR(tmp);
6465 }
6466 *out = With(items, body, type_comment, lineno, col_offset, end_lineno,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006467 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006468 if (*out == NULL) goto failed;
6469 return 0;
6470 }
Victor Stinner74419f02020-07-03 11:35:37 +02006471 tp = state->AsyncWith_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006472 isinstance = PyObject_IsInstance(obj, tp);
Yury Selivanov75445082015-05-11 22:57:16 -04006473 if (isinstance == -1) {
6474 return 1;
6475 }
6476 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01006477 asdl_withitem_seq* items;
6478 asdl_stmt_seq* body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08006479 string type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04006480
Victor Stinner74419f02020-07-03 11:35:37 +02006481 if (_PyObject_LookupAttr(obj, state->items, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006482 return 1;
6483 }
6484 if (tmp == NULL) {
6485 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
6486 return 1;
6487 }
6488 else {
Yury Selivanov75445082015-05-11 22:57:16 -04006489 int res;
6490 Py_ssize_t len;
6491 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04006492 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006493 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Yury Selivanov75445082015-05-11 22:57:16 -04006494 goto failed;
6495 }
6496 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006497 items = _Py_asdl_withitem_seq_new(len, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04006498 if (items == NULL) goto failed;
6499 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006500 withitem_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006501 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6502 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006503 res = obj2ast_withitem(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006504 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04006505 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006506 if (len != PyList_GET_SIZE(tmp)) {
6507 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"items\" changed size during iteration");
6508 goto failed;
6509 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006510 asdl_seq_SET(items, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04006511 }
6512 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006513 }
Victor Stinner74419f02020-07-03 11:35:37 +02006514 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04006515 return 1;
6516 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006517 if (tmp == NULL) {
6518 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
6519 return 1;
6520 }
6521 else {
Yury Selivanov75445082015-05-11 22:57:16 -04006522 int res;
6523 Py_ssize_t len;
6524 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04006525 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006526 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Yury Selivanov75445082015-05-11 22:57:16 -04006527 goto failed;
6528 }
6529 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006530 body = _Py_asdl_stmt_seq_new(len, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04006531 if (body == NULL) goto failed;
6532 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006533 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006534 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6535 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006536 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006537 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04006538 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006539 if (len != PyList_GET_SIZE(tmp)) {
6540 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"body\" changed size during iteration");
6541 goto failed;
6542 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006543 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04006544 }
6545 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04006546 }
Victor Stinner74419f02020-07-03 11:35:37 +02006547 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08006548 return 1;
6549 }
6550 if (tmp == NULL || tmp == Py_None) {
6551 Py_CLEAR(tmp);
6552 type_comment = NULL;
6553 }
6554 else {
6555 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006556 res = obj2ast_string(state, tmp, &type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08006557 if (res != 0) goto failed;
6558 Py_CLEAR(tmp);
6559 }
6560 *out = AsyncWith(items, body, type_comment, lineno, col_offset,
6561 end_lineno, end_col_offset, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04006562 if (*out == NULL) goto failed;
6563 return 0;
6564 }
Victor Stinner74419f02020-07-03 11:35:37 +02006565 tp = state->Raise_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006566 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006567 if (isinstance == -1) {
6568 return 1;
6569 }
6570 if (isinstance) {
6571 expr_ty exc;
6572 expr_ty cause;
6573
Victor Stinner74419f02020-07-03 11:35:37 +02006574 if (_PyObject_LookupAttr(obj, state->exc, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006575 return 1;
6576 }
6577 if (tmp == NULL || tmp == Py_None) {
6578 Py_CLEAR(tmp);
6579 exc = NULL;
6580 }
6581 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006582 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006583 res = obj2ast_expr(state, tmp, &exc, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006584 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006585 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006586 }
Victor Stinner74419f02020-07-03 11:35:37 +02006587 if (_PyObject_LookupAttr(obj, state->cause, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006588 return 1;
6589 }
6590 if (tmp == NULL || tmp == Py_None) {
6591 Py_CLEAR(tmp);
6592 cause = NULL;
6593 }
6594 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006595 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006596 res = obj2ast_expr(state, tmp, &cause, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006597 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006598 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006599 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006600 *out = Raise(exc, cause, lineno, col_offset, end_lineno,
6601 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006602 if (*out == NULL) goto failed;
6603 return 0;
6604 }
Victor Stinner74419f02020-07-03 11:35:37 +02006605 tp = state->Try_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006606 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006607 if (isinstance == -1) {
6608 return 1;
6609 }
6610 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01006611 asdl_stmt_seq* body;
6612 asdl_excepthandler_seq* handlers;
6613 asdl_stmt_seq* orelse;
6614 asdl_stmt_seq* finalbody;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006615
Victor Stinner74419f02020-07-03 11:35:37 +02006616 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006617 return 1;
6618 }
6619 if (tmp == NULL) {
6620 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
6621 return 1;
6622 }
6623 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006624 int res;
6625 Py_ssize_t len;
6626 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006627 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006628 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006629 goto failed;
6630 }
6631 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006632 body = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006633 if (body == NULL) goto failed;
6634 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006635 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006636 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6637 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006638 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006639 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006640 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006641 if (len != PyList_GET_SIZE(tmp)) {
6642 PyErr_SetString(PyExc_RuntimeError, "Try field \"body\" changed size during iteration");
6643 goto failed;
6644 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006645 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006646 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006647 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006648 }
Victor Stinner74419f02020-07-03 11:35:37 +02006649 if (_PyObject_LookupAttr(obj, state->handlers, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006650 return 1;
6651 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006652 if (tmp == NULL) {
6653 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
6654 return 1;
6655 }
6656 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006657 int res;
6658 Py_ssize_t len;
6659 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006660 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006661 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006662 goto failed;
6663 }
6664 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006665 handlers = _Py_asdl_excepthandler_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006666 if (handlers == NULL) goto failed;
6667 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006668 excepthandler_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006669 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6670 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006671 res = obj2ast_excepthandler(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006672 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006673 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006674 if (len != PyList_GET_SIZE(tmp)) {
6675 PyErr_SetString(PyExc_RuntimeError, "Try field \"handlers\" changed size during iteration");
6676 goto failed;
6677 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006678 asdl_seq_SET(handlers, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006679 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006680 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006681 }
Victor Stinner74419f02020-07-03 11:35:37 +02006682 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006683 return 1;
6684 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006685 if (tmp == NULL) {
6686 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
6687 return 1;
6688 }
6689 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006690 int res;
6691 Py_ssize_t len;
6692 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006693 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006694 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006695 goto failed;
6696 }
6697 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006698 orelse = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006699 if (orelse == NULL) goto failed;
6700 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006701 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006702 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6703 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006704 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006705 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006706 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006707 if (len != PyList_GET_SIZE(tmp)) {
6708 PyErr_SetString(PyExc_RuntimeError, "Try field \"orelse\" changed size during iteration");
6709 goto failed;
6710 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006711 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006712 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006713 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006714 }
Victor Stinner74419f02020-07-03 11:35:37 +02006715 if (_PyObject_LookupAttr(obj, state->finalbody, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006716 return 1;
6717 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006718 if (tmp == NULL) {
6719 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
6720 return 1;
6721 }
6722 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006723 int res;
6724 Py_ssize_t len;
6725 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006726 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006727 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006728 goto failed;
6729 }
6730 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006731 finalbody = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006732 if (finalbody == NULL) goto failed;
6733 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006734 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006735 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6736 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006737 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006738 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006739 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006740 if (len != PyList_GET_SIZE(tmp)) {
6741 PyErr_SetString(PyExc_RuntimeError, "Try field \"finalbody\" changed size during iteration");
6742 goto failed;
6743 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006744 asdl_seq_SET(finalbody, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006745 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006746 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006747 }
6748 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006749 end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006750 if (*out == NULL) goto failed;
6751 return 0;
6752 }
Victor Stinner74419f02020-07-03 11:35:37 +02006753 tp = state->Assert_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006754 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006755 if (isinstance == -1) {
6756 return 1;
6757 }
6758 if (isinstance) {
6759 expr_ty test;
6760 expr_ty msg;
6761
Victor Stinner74419f02020-07-03 11:35:37 +02006762 if (_PyObject_LookupAttr(obj, state->test, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006763 return 1;
6764 }
6765 if (tmp == NULL) {
6766 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
6767 return 1;
6768 }
6769 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006770 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006771 res = obj2ast_expr(state, tmp, &test, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006772 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006773 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006774 }
Victor Stinner74419f02020-07-03 11:35:37 +02006775 if (_PyObject_LookupAttr(obj, state->msg, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006776 return 1;
6777 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006778 if (tmp == NULL || tmp == Py_None) {
6779 Py_CLEAR(tmp);
6780 msg = NULL;
6781 }
6782 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006783 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006784 res = obj2ast_expr(state, tmp, &msg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006785 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006786 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006787 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006788 *out = Assert(test, msg, lineno, col_offset, end_lineno,
6789 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006790 if (*out == NULL) goto failed;
6791 return 0;
6792 }
Victor Stinner74419f02020-07-03 11:35:37 +02006793 tp = state->Import_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006794 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006795 if (isinstance == -1) {
6796 return 1;
6797 }
6798 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01006799 asdl_alias_seq* names;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006800
Victor Stinner74419f02020-07-03 11:35:37 +02006801 if (_PyObject_LookupAttr(obj, state->names, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006802 return 1;
6803 }
6804 if (tmp == NULL) {
6805 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
6806 return 1;
6807 }
6808 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006809 int res;
6810 Py_ssize_t len;
6811 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006812 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006813 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006814 goto failed;
6815 }
6816 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006817 names = _Py_asdl_alias_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006818 if (names == NULL) goto failed;
6819 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006820 alias_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006821 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6822 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006823 res = obj2ast_alias(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006824 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006825 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006826 if (len != PyList_GET_SIZE(tmp)) {
6827 PyErr_SetString(PyExc_RuntimeError, "Import field \"names\" changed size during iteration");
6828 goto failed;
6829 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006830 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006831 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006832 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006833 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006834 *out = Import(names, lineno, col_offset, end_lineno, end_col_offset,
6835 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006836 if (*out == NULL) goto failed;
6837 return 0;
6838 }
Victor Stinner74419f02020-07-03 11:35:37 +02006839 tp = state->ImportFrom_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006840 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006841 if (isinstance == -1) {
6842 return 1;
6843 }
6844 if (isinstance) {
6845 identifier module;
Pablo Galindoa5634c42020-09-16 19:42:00 +01006846 asdl_alias_seq* names;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006847 int level;
6848
Victor Stinner74419f02020-07-03 11:35:37 +02006849 if (_PyObject_LookupAttr(obj, state->module, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006850 return 1;
6851 }
6852 if (tmp == NULL || tmp == Py_None) {
6853 Py_CLEAR(tmp);
6854 module = NULL;
6855 }
6856 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006857 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006858 res = obj2ast_identifier(state, tmp, &module, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006859 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006860 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006861 }
Victor Stinner74419f02020-07-03 11:35:37 +02006862 if (_PyObject_LookupAttr(obj, state->names, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006863 return 1;
6864 }
6865 if (tmp == NULL) {
6866 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
6867 return 1;
6868 }
6869 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006870 int res;
6871 Py_ssize_t len;
6872 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006873 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006874 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006875 goto failed;
6876 }
6877 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006878 names = _Py_asdl_alias_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006879 if (names == NULL) goto failed;
6880 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006881 alias_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006882 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6883 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006884 res = obj2ast_alias(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006885 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006886 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006887 if (len != PyList_GET_SIZE(tmp)) {
6888 PyErr_SetString(PyExc_RuntimeError, "ImportFrom field \"names\" changed size during iteration");
6889 goto failed;
6890 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006891 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006892 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006893 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006894 }
Victor Stinner74419f02020-07-03 11:35:37 +02006895 if (_PyObject_LookupAttr(obj, state->level, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006896 return 1;
6897 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006898 if (tmp == NULL || tmp == Py_None) {
6899 Py_CLEAR(tmp);
6900 level = 0;
6901 }
6902 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006903 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02006904 res = obj2ast_int(state, tmp, &level, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006905 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006906 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006907 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006908 *out = ImportFrom(module, names, level, lineno, col_offset, end_lineno,
6909 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006910 if (*out == NULL) goto failed;
6911 return 0;
6912 }
Victor Stinner74419f02020-07-03 11:35:37 +02006913 tp = state->Global_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006914 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006915 if (isinstance == -1) {
6916 return 1;
6917 }
6918 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01006919 asdl_identifier_seq* names;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006920
Victor Stinner74419f02020-07-03 11:35:37 +02006921 if (_PyObject_LookupAttr(obj, state->names, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006922 return 1;
6923 }
6924 if (tmp == NULL) {
6925 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
6926 return 1;
6927 }
6928 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006929 int res;
6930 Py_ssize_t len;
6931 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006932 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006933 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006934 goto failed;
6935 }
6936 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006937 names = _Py_asdl_identifier_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006938 if (names == NULL) goto failed;
6939 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006940 identifier val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006941 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6942 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006943 res = obj2ast_identifier(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006944 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006945 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006946 if (len != PyList_GET_SIZE(tmp)) {
6947 PyErr_SetString(PyExc_RuntimeError, "Global field \"names\" changed size during iteration");
6948 goto failed;
6949 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006950 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006951 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006952 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006953 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006954 *out = Global(names, lineno, col_offset, end_lineno, end_col_offset,
6955 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006956 if (*out == NULL) goto failed;
6957 return 0;
6958 }
Victor Stinner74419f02020-07-03 11:35:37 +02006959 tp = state->Nonlocal_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07006960 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006961 if (isinstance == -1) {
6962 return 1;
6963 }
6964 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01006965 asdl_identifier_seq* names;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006966
Victor Stinner74419f02020-07-03 11:35:37 +02006967 if (_PyObject_LookupAttr(obj, state->names, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006968 return 1;
6969 }
6970 if (tmp == NULL) {
6971 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
6972 return 1;
6973 }
6974 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006975 int res;
6976 Py_ssize_t len;
6977 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006978 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07006979 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006980 goto failed;
6981 }
6982 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01006983 names = _Py_asdl_identifier_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006984 if (names == NULL) goto failed;
6985 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006986 identifier val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006987 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6988 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02006989 res = obj2ast_identifier(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006990 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006991 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006992 if (len != PyList_GET_SIZE(tmp)) {
6993 PyErr_SetString(PyExc_RuntimeError, "Nonlocal field \"names\" changed size during iteration");
6994 goto failed;
6995 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006996 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006997 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006998 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006999 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007000 *out = Nonlocal(names, lineno, col_offset, end_lineno, end_col_offset,
7001 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007002 if (*out == NULL) goto failed;
7003 return 0;
7004 }
Victor Stinner74419f02020-07-03 11:35:37 +02007005 tp = state->Expr_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007006 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007007 if (isinstance == -1) {
7008 return 1;
7009 }
7010 if (isinstance) {
7011 expr_ty value;
7012
Victor Stinner74419f02020-07-03 11:35:37 +02007013 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007014 return 1;
7015 }
7016 if (tmp == NULL) {
7017 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
7018 return 1;
7019 }
7020 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007021 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007022 res = obj2ast_expr(state, tmp, &value, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007023 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007024 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007025 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007026 *out = Expr(value, lineno, col_offset, end_lineno, end_col_offset,
7027 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007028 if (*out == NULL) goto failed;
7029 return 0;
7030 }
Victor Stinner74419f02020-07-03 11:35:37 +02007031 tp = state->Pass_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007032 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007033 if (isinstance == -1) {
7034 return 1;
7035 }
7036 if (isinstance) {
7037
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007038 *out = Pass(lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007039 if (*out == NULL) goto failed;
7040 return 0;
7041 }
Victor Stinner74419f02020-07-03 11:35:37 +02007042 tp = state->Break_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007043 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007044 if (isinstance == -1) {
7045 return 1;
7046 }
7047 if (isinstance) {
7048
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007049 *out = Break(lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007050 if (*out == NULL) goto failed;
7051 return 0;
7052 }
Victor Stinner74419f02020-07-03 11:35:37 +02007053 tp = state->Continue_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007054 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007055 if (isinstance == -1) {
7056 return 1;
7057 }
7058 if (isinstance) {
7059
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007060 *out = Continue(lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007061 if (*out == NULL) goto failed;
7062 return 0;
7063 }
7064
7065 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
7066 failed:
7067 Py_XDECREF(tmp);
7068 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007069}
7070
7071int
Victor Stinner5cf47822020-11-02 22:03:28 +01007072obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
7073 arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007074{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007075 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007076
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007077 PyObject *tmp = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007078 PyObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007079 int lineno;
7080 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007081 int end_lineno;
7082 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007083
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007084 if (obj == Py_None) {
7085 *out = NULL;
7086 return 0;
7087 }
Victor Stinner74419f02020-07-03 11:35:37 +02007088 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007089 return 1;
7090 }
7091 if (tmp == NULL) {
7092 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
7093 return 1;
7094 }
7095 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007096 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007097 res = obj2ast_int(state, tmp, &lineno, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007098 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007099 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007100 }
Victor Stinner74419f02020-07-03 11:35:37 +02007101 if (_PyObject_LookupAttr(obj, state->col_offset, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007102 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007103 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007104 if (tmp == NULL) {
7105 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
7106 return 1;
7107 }
7108 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007109 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007110 res = obj2ast_int(state, tmp, &col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007111 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007112 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007113 }
Victor Stinner74419f02020-07-03 11:35:37 +02007114 if (_PyObject_LookupAttr(obj, state->end_lineno, &tmp) < 0) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007115 return 1;
7116 }
7117 if (tmp == NULL || tmp == Py_None) {
7118 Py_CLEAR(tmp);
7119 end_lineno = 0;
7120 }
7121 else {
7122 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007123 res = obj2ast_int(state, tmp, &end_lineno, arena);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007124 if (res != 0) goto failed;
7125 Py_CLEAR(tmp);
7126 }
Victor Stinner74419f02020-07-03 11:35:37 +02007127 if (_PyObject_LookupAttr(obj, state->end_col_offset, &tmp) < 0) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007128 return 1;
7129 }
7130 if (tmp == NULL || tmp == Py_None) {
7131 Py_CLEAR(tmp);
7132 end_col_offset = 0;
7133 }
7134 else {
7135 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007136 res = obj2ast_int(state, tmp, &end_col_offset, arena);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007137 if (res != 0) goto failed;
7138 Py_CLEAR(tmp);
7139 }
Victor Stinner74419f02020-07-03 11:35:37 +02007140 tp = state->BoolOp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007141 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007142 if (isinstance == -1) {
7143 return 1;
7144 }
7145 if (isinstance) {
7146 boolop_ty op;
Pablo Galindoa5634c42020-09-16 19:42:00 +01007147 asdl_expr_seq* values;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007148
Victor Stinner74419f02020-07-03 11:35:37 +02007149 if (_PyObject_LookupAttr(obj, state->op, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007150 return 1;
7151 }
7152 if (tmp == NULL) {
7153 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
7154 return 1;
7155 }
7156 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007157 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007158 res = obj2ast_boolop(state, tmp, &op, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007159 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007160 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007161 }
Victor Stinner74419f02020-07-03 11:35:37 +02007162 if (_PyObject_LookupAttr(obj, state->values, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007163 return 1;
7164 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007165 if (tmp == NULL) {
7166 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
7167 return 1;
7168 }
7169 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007170 int res;
7171 Py_ssize_t len;
7172 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007173 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07007174 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007175 goto failed;
7176 }
7177 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01007178 values = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007179 if (values == NULL) goto failed;
7180 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007181 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007182 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7183 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02007184 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007185 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007186 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007187 if (len != PyList_GET_SIZE(tmp)) {
7188 PyErr_SetString(PyExc_RuntimeError, "BoolOp field \"values\" changed size during iteration");
7189 goto failed;
7190 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007191 asdl_seq_SET(values, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007192 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007193 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007194 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007195 *out = BoolOp(op, values, lineno, col_offset, end_lineno,
7196 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007197 if (*out == NULL) goto failed;
7198 return 0;
7199 }
Victor Stinner74419f02020-07-03 11:35:37 +02007200 tp = state->NamedExpr_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007201 isinstance = PyObject_IsInstance(obj, tp);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07007202 if (isinstance == -1) {
7203 return 1;
7204 }
7205 if (isinstance) {
7206 expr_ty target;
7207 expr_ty value;
7208
Victor Stinner74419f02020-07-03 11:35:37 +02007209 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07007210 return 1;
7211 }
7212 if (tmp == NULL) {
7213 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from NamedExpr");
7214 return 1;
7215 }
7216 else {
7217 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007218 res = obj2ast_expr(state, tmp, &target, arena);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07007219 if (res != 0) goto failed;
7220 Py_CLEAR(tmp);
7221 }
Victor Stinner74419f02020-07-03 11:35:37 +02007222 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07007223 return 1;
7224 }
7225 if (tmp == NULL) {
7226 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NamedExpr");
7227 return 1;
7228 }
7229 else {
7230 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007231 res = obj2ast_expr(state, tmp, &value, arena);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07007232 if (res != 0) goto failed;
7233 Py_CLEAR(tmp);
7234 }
7235 *out = NamedExpr(target, value, lineno, col_offset, end_lineno,
7236 end_col_offset, arena);
7237 if (*out == NULL) goto failed;
7238 return 0;
7239 }
Victor Stinner74419f02020-07-03 11:35:37 +02007240 tp = state->BinOp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007241 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007242 if (isinstance == -1) {
7243 return 1;
7244 }
7245 if (isinstance) {
7246 expr_ty left;
7247 operator_ty op;
7248 expr_ty right;
7249
Victor Stinner74419f02020-07-03 11:35:37 +02007250 if (_PyObject_LookupAttr(obj, state->left, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007251 return 1;
7252 }
7253 if (tmp == NULL) {
7254 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
7255 return 1;
7256 }
7257 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007258 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007259 res = obj2ast_expr(state, tmp, &left, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007260 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007261 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007262 }
Victor Stinner74419f02020-07-03 11:35:37 +02007263 if (_PyObject_LookupAttr(obj, state->op, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007264 return 1;
7265 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007266 if (tmp == NULL) {
7267 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
7268 return 1;
7269 }
7270 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007271 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007272 res = obj2ast_operator(state, tmp, &op, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007273 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007274 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007275 }
Victor Stinner74419f02020-07-03 11:35:37 +02007276 if (_PyObject_LookupAttr(obj, state->right, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007277 return 1;
7278 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007279 if (tmp == NULL) {
7280 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
7281 return 1;
7282 }
7283 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007284 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007285 res = obj2ast_expr(state, tmp, &right, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007286 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007287 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007288 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007289 *out = BinOp(left, op, right, lineno, col_offset, end_lineno,
7290 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007291 if (*out == NULL) goto failed;
7292 return 0;
7293 }
Victor Stinner74419f02020-07-03 11:35:37 +02007294 tp = state->UnaryOp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007295 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007296 if (isinstance == -1) {
7297 return 1;
7298 }
7299 if (isinstance) {
7300 unaryop_ty op;
7301 expr_ty operand;
7302
Victor Stinner74419f02020-07-03 11:35:37 +02007303 if (_PyObject_LookupAttr(obj, state->op, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007304 return 1;
7305 }
7306 if (tmp == NULL) {
7307 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
7308 return 1;
7309 }
7310 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007311 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007312 res = obj2ast_unaryop(state, tmp, &op, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007313 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007314 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007315 }
Victor Stinner74419f02020-07-03 11:35:37 +02007316 if (_PyObject_LookupAttr(obj, state->operand, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007317 return 1;
7318 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007319 if (tmp == NULL) {
7320 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
7321 return 1;
7322 }
7323 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007324 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007325 res = obj2ast_expr(state, tmp, &operand, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007326 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007327 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007328 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007329 *out = UnaryOp(op, operand, lineno, col_offset, end_lineno,
7330 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007331 if (*out == NULL) goto failed;
7332 return 0;
7333 }
Victor Stinner74419f02020-07-03 11:35:37 +02007334 tp = state->Lambda_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007335 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007336 if (isinstance == -1) {
7337 return 1;
7338 }
7339 if (isinstance) {
7340 arguments_ty args;
7341 expr_ty body;
7342
Victor Stinner74419f02020-07-03 11:35:37 +02007343 if (_PyObject_LookupAttr(obj, state->args, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007344 return 1;
7345 }
7346 if (tmp == NULL) {
7347 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
7348 return 1;
7349 }
7350 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007351 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007352 res = obj2ast_arguments(state, tmp, &args, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007353 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007354 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007355 }
Victor Stinner74419f02020-07-03 11:35:37 +02007356 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007357 return 1;
7358 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007359 if (tmp == NULL) {
7360 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
7361 return 1;
7362 }
7363 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007364 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007365 res = obj2ast_expr(state, tmp, &body, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007366 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007367 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007368 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007369 *out = Lambda(args, body, lineno, col_offset, end_lineno,
7370 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007371 if (*out == NULL) goto failed;
7372 return 0;
7373 }
Victor Stinner74419f02020-07-03 11:35:37 +02007374 tp = state->IfExp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007375 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007376 if (isinstance == -1) {
7377 return 1;
7378 }
7379 if (isinstance) {
7380 expr_ty test;
7381 expr_ty body;
7382 expr_ty orelse;
7383
Victor Stinner74419f02020-07-03 11:35:37 +02007384 if (_PyObject_LookupAttr(obj, state->test, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007385 return 1;
7386 }
7387 if (tmp == NULL) {
7388 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
7389 return 1;
7390 }
7391 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007392 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007393 res = obj2ast_expr(state, tmp, &test, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007394 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007395 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007396 }
Victor Stinner74419f02020-07-03 11:35:37 +02007397 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007398 return 1;
7399 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007400 if (tmp == NULL) {
7401 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
7402 return 1;
7403 }
7404 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007405 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007406 res = obj2ast_expr(state, tmp, &body, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007407 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007408 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007409 }
Victor Stinner74419f02020-07-03 11:35:37 +02007410 if (_PyObject_LookupAttr(obj, state->orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007411 return 1;
7412 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007413 if (tmp == NULL) {
7414 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
7415 return 1;
7416 }
7417 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007418 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007419 res = obj2ast_expr(state, tmp, &orelse, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007420 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007421 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007422 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007423 *out = IfExp(test, body, orelse, lineno, col_offset, end_lineno,
7424 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007425 if (*out == NULL) goto failed;
7426 return 0;
7427 }
Victor Stinner74419f02020-07-03 11:35:37 +02007428 tp = state->Dict_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007429 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007430 if (isinstance == -1) {
7431 return 1;
7432 }
7433 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01007434 asdl_expr_seq* keys;
7435 asdl_expr_seq* values;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007436
Victor Stinner74419f02020-07-03 11:35:37 +02007437 if (_PyObject_LookupAttr(obj, state->keys, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007438 return 1;
7439 }
7440 if (tmp == NULL) {
7441 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
7442 return 1;
7443 }
7444 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007445 int res;
7446 Py_ssize_t len;
7447 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007448 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07007449 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007450 goto failed;
7451 }
7452 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01007453 keys = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007454 if (keys == NULL) goto failed;
7455 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007456 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007457 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7458 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02007459 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007460 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007461 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007462 if (len != PyList_GET_SIZE(tmp)) {
7463 PyErr_SetString(PyExc_RuntimeError, "Dict field \"keys\" changed size during iteration");
7464 goto failed;
7465 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007466 asdl_seq_SET(keys, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007467 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007468 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007469 }
Victor Stinner74419f02020-07-03 11:35:37 +02007470 if (_PyObject_LookupAttr(obj, state->values, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007471 return 1;
7472 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007473 if (tmp == NULL) {
7474 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
7475 return 1;
7476 }
7477 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007478 int res;
7479 Py_ssize_t len;
7480 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007481 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07007482 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007483 goto failed;
7484 }
7485 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01007486 values = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007487 if (values == NULL) goto failed;
7488 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007489 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007490 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7491 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02007492 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007493 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007494 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007495 if (len != PyList_GET_SIZE(tmp)) {
7496 PyErr_SetString(PyExc_RuntimeError, "Dict field \"values\" changed size during iteration");
7497 goto failed;
7498 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007499 asdl_seq_SET(values, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007500 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007501 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007502 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007503 *out = Dict(keys, values, lineno, col_offset, end_lineno,
7504 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007505 if (*out == NULL) goto failed;
7506 return 0;
7507 }
Victor Stinner74419f02020-07-03 11:35:37 +02007508 tp = state->Set_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007509 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007510 if (isinstance == -1) {
7511 return 1;
7512 }
7513 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01007514 asdl_expr_seq* elts;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007515
Victor Stinner74419f02020-07-03 11:35:37 +02007516 if (_PyObject_LookupAttr(obj, state->elts, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007517 return 1;
7518 }
7519 if (tmp == NULL) {
7520 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
7521 return 1;
7522 }
7523 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007524 int res;
7525 Py_ssize_t len;
7526 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007527 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07007528 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007529 goto failed;
7530 }
7531 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01007532 elts = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007533 if (elts == NULL) goto failed;
7534 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007535 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007536 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7537 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02007538 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007539 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007540 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007541 if (len != PyList_GET_SIZE(tmp)) {
7542 PyErr_SetString(PyExc_RuntimeError, "Set field \"elts\" changed size during iteration");
7543 goto failed;
7544 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007545 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007546 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007547 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007548 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007549 *out = Set(elts, lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007550 if (*out == NULL) goto failed;
7551 return 0;
7552 }
Victor Stinner74419f02020-07-03 11:35:37 +02007553 tp = state->ListComp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007554 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007555 if (isinstance == -1) {
7556 return 1;
7557 }
7558 if (isinstance) {
7559 expr_ty elt;
Pablo Galindoa5634c42020-09-16 19:42:00 +01007560 asdl_comprehension_seq* generators;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007561
Victor Stinner74419f02020-07-03 11:35:37 +02007562 if (_PyObject_LookupAttr(obj, state->elt, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007563 return 1;
7564 }
7565 if (tmp == NULL) {
7566 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
7567 return 1;
7568 }
7569 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007570 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007571 res = obj2ast_expr(state, tmp, &elt, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007572 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007573 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007574 }
Victor Stinner74419f02020-07-03 11:35:37 +02007575 if (_PyObject_LookupAttr(obj, state->generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007576 return 1;
7577 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007578 if (tmp == NULL) {
7579 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
7580 return 1;
7581 }
7582 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007583 int res;
7584 Py_ssize_t len;
7585 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007586 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07007587 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007588 goto failed;
7589 }
7590 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01007591 generators = _Py_asdl_comprehension_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007592 if (generators == NULL) goto failed;
7593 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007594 comprehension_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007595 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7596 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02007597 res = obj2ast_comprehension(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007598 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007599 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007600 if (len != PyList_GET_SIZE(tmp)) {
7601 PyErr_SetString(PyExc_RuntimeError, "ListComp field \"generators\" changed size during iteration");
7602 goto failed;
7603 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007604 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007605 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007606 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007607 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007608 *out = ListComp(elt, generators, lineno, col_offset, end_lineno,
7609 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007610 if (*out == NULL) goto failed;
7611 return 0;
7612 }
Victor Stinner74419f02020-07-03 11:35:37 +02007613 tp = state->SetComp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007614 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007615 if (isinstance == -1) {
7616 return 1;
7617 }
7618 if (isinstance) {
7619 expr_ty elt;
Pablo Galindoa5634c42020-09-16 19:42:00 +01007620 asdl_comprehension_seq* generators;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007621
Victor Stinner74419f02020-07-03 11:35:37 +02007622 if (_PyObject_LookupAttr(obj, state->elt, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007623 return 1;
7624 }
7625 if (tmp == NULL) {
7626 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
7627 return 1;
7628 }
7629 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007630 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007631 res = obj2ast_expr(state, tmp, &elt, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007632 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007633 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007634 }
Victor Stinner74419f02020-07-03 11:35:37 +02007635 if (_PyObject_LookupAttr(obj, state->generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007636 return 1;
7637 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007638 if (tmp == NULL) {
7639 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
7640 return 1;
7641 }
7642 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007643 int res;
7644 Py_ssize_t len;
7645 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007646 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07007647 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007648 goto failed;
7649 }
7650 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01007651 generators = _Py_asdl_comprehension_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007652 if (generators == NULL) goto failed;
7653 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007654 comprehension_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007655 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7656 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02007657 res = obj2ast_comprehension(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007658 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007659 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007660 if (len != PyList_GET_SIZE(tmp)) {
7661 PyErr_SetString(PyExc_RuntimeError, "SetComp field \"generators\" changed size during iteration");
7662 goto failed;
7663 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007664 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007665 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007666 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007667 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007668 *out = SetComp(elt, generators, lineno, col_offset, end_lineno,
7669 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007670 if (*out == NULL) goto failed;
7671 return 0;
7672 }
Victor Stinner74419f02020-07-03 11:35:37 +02007673 tp = state->DictComp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007674 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007675 if (isinstance == -1) {
7676 return 1;
7677 }
7678 if (isinstance) {
7679 expr_ty key;
7680 expr_ty value;
Pablo Galindoa5634c42020-09-16 19:42:00 +01007681 asdl_comprehension_seq* generators;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007682
Victor Stinner74419f02020-07-03 11:35:37 +02007683 if (_PyObject_LookupAttr(obj, state->key, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007684 return 1;
7685 }
7686 if (tmp == NULL) {
7687 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
7688 return 1;
7689 }
7690 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007691 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007692 res = obj2ast_expr(state, tmp, &key, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007693 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007694 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007695 }
Victor Stinner74419f02020-07-03 11:35:37 +02007696 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007697 return 1;
7698 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007699 if (tmp == NULL) {
7700 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
7701 return 1;
7702 }
7703 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007704 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007705 res = obj2ast_expr(state, tmp, &value, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007706 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007707 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007708 }
Victor Stinner74419f02020-07-03 11:35:37 +02007709 if (_PyObject_LookupAttr(obj, state->generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007710 return 1;
7711 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007712 if (tmp == NULL) {
7713 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
7714 return 1;
7715 }
7716 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007717 int res;
7718 Py_ssize_t len;
7719 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007720 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07007721 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007722 goto failed;
7723 }
7724 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01007725 generators = _Py_asdl_comprehension_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007726 if (generators == NULL) goto failed;
7727 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007728 comprehension_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007729 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7730 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02007731 res = obj2ast_comprehension(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007732 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007733 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007734 if (len != PyList_GET_SIZE(tmp)) {
7735 PyErr_SetString(PyExc_RuntimeError, "DictComp field \"generators\" changed size during iteration");
7736 goto failed;
7737 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007738 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007739 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007740 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007741 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007742 *out = DictComp(key, value, generators, lineno, col_offset, end_lineno,
7743 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007744 if (*out == NULL) goto failed;
7745 return 0;
7746 }
Victor Stinner74419f02020-07-03 11:35:37 +02007747 tp = state->GeneratorExp_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007748 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007749 if (isinstance == -1) {
7750 return 1;
7751 }
7752 if (isinstance) {
7753 expr_ty elt;
Pablo Galindoa5634c42020-09-16 19:42:00 +01007754 asdl_comprehension_seq* generators;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007755
Victor Stinner74419f02020-07-03 11:35:37 +02007756 if (_PyObject_LookupAttr(obj, state->elt, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007757 return 1;
7758 }
7759 if (tmp == NULL) {
7760 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
7761 return 1;
7762 }
7763 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007764 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007765 res = obj2ast_expr(state, tmp, &elt, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007766 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007767 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007768 }
Victor Stinner74419f02020-07-03 11:35:37 +02007769 if (_PyObject_LookupAttr(obj, state->generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007770 return 1;
7771 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007772 if (tmp == NULL) {
7773 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
7774 return 1;
7775 }
7776 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007777 int res;
7778 Py_ssize_t len;
7779 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007780 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07007781 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007782 goto failed;
7783 }
7784 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01007785 generators = _Py_asdl_comprehension_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007786 if (generators == NULL) goto failed;
7787 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007788 comprehension_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007789 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7790 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02007791 res = obj2ast_comprehension(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007792 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007793 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007794 if (len != PyList_GET_SIZE(tmp)) {
7795 PyErr_SetString(PyExc_RuntimeError, "GeneratorExp field \"generators\" changed size during iteration");
7796 goto failed;
7797 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007798 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007799 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007800 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007801 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007802 *out = GeneratorExp(elt, generators, lineno, col_offset, end_lineno,
7803 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007804 if (*out == NULL) goto failed;
7805 return 0;
7806 }
Victor Stinner74419f02020-07-03 11:35:37 +02007807 tp = state->Await_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007808 isinstance = PyObject_IsInstance(obj, tp);
Yury Selivanov75445082015-05-11 22:57:16 -04007809 if (isinstance == -1) {
7810 return 1;
7811 }
7812 if (isinstance) {
7813 expr_ty value;
7814
Victor Stinner74419f02020-07-03 11:35:37 +02007815 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007816 return 1;
7817 }
7818 if (tmp == NULL) {
7819 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
7820 return 1;
7821 }
7822 else {
Yury Selivanov75445082015-05-11 22:57:16 -04007823 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007824 res = obj2ast_expr(state, tmp, &value, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04007825 if (res != 0) goto failed;
7826 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04007827 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007828 *out = Await(value, lineno, col_offset, end_lineno, end_col_offset,
7829 arena);
Yury Selivanov75445082015-05-11 22:57:16 -04007830 if (*out == NULL) goto failed;
7831 return 0;
7832 }
Victor Stinner74419f02020-07-03 11:35:37 +02007833 tp = state->Yield_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007834 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007835 if (isinstance == -1) {
7836 return 1;
7837 }
7838 if (isinstance) {
7839 expr_ty value;
7840
Victor Stinner74419f02020-07-03 11:35:37 +02007841 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007842 return 1;
7843 }
7844 if (tmp == NULL || tmp == Py_None) {
7845 Py_CLEAR(tmp);
7846 value = NULL;
7847 }
7848 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007849 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007850 res = obj2ast_expr(state, tmp, &value, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007851 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007852 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007853 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007854 *out = Yield(value, lineno, col_offset, end_lineno, end_col_offset,
7855 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007856 if (*out == NULL) goto failed;
7857 return 0;
7858 }
Victor Stinner74419f02020-07-03 11:35:37 +02007859 tp = state->YieldFrom_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007860 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007861 if (isinstance == -1) {
7862 return 1;
7863 }
7864 if (isinstance) {
7865 expr_ty value;
7866
Victor Stinner74419f02020-07-03 11:35:37 +02007867 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007868 return 1;
7869 }
7870 if (tmp == NULL) {
7871 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
7872 return 1;
7873 }
7874 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007875 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007876 res = obj2ast_expr(state, tmp, &value, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007877 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007878 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007879 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007880 *out = YieldFrom(value, lineno, col_offset, end_lineno, end_col_offset,
7881 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007882 if (*out == NULL) goto failed;
7883 return 0;
7884 }
Victor Stinner74419f02020-07-03 11:35:37 +02007885 tp = state->Compare_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007886 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007887 if (isinstance == -1) {
7888 return 1;
7889 }
7890 if (isinstance) {
7891 expr_ty left;
7892 asdl_int_seq* ops;
Pablo Galindoa5634c42020-09-16 19:42:00 +01007893 asdl_expr_seq* comparators;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007894
Victor Stinner74419f02020-07-03 11:35:37 +02007895 if (_PyObject_LookupAttr(obj, state->left, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007896 return 1;
7897 }
7898 if (tmp == NULL) {
7899 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
7900 return 1;
7901 }
7902 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007903 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007904 res = obj2ast_expr(state, tmp, &left, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007905 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007906 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007907 }
Victor Stinner74419f02020-07-03 11:35:37 +02007908 if (_PyObject_LookupAttr(obj, state->ops, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007909 return 1;
7910 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007911 if (tmp == NULL) {
7912 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
7913 return 1;
7914 }
7915 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007916 int res;
7917 Py_ssize_t len;
7918 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007919 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07007920 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007921 goto failed;
7922 }
7923 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007924 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007925 if (ops == NULL) goto failed;
7926 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007927 cmpop_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007928 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7929 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02007930 res = obj2ast_cmpop(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007931 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007932 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007933 if (len != PyList_GET_SIZE(tmp)) {
7934 PyErr_SetString(PyExc_RuntimeError, "Compare field \"ops\" changed size during iteration");
7935 goto failed;
7936 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007937 asdl_seq_SET(ops, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007938 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007939 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007940 }
Victor Stinner74419f02020-07-03 11:35:37 +02007941 if (_PyObject_LookupAttr(obj, state->comparators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007942 return 1;
7943 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007944 if (tmp == NULL) {
7945 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
7946 return 1;
7947 }
7948 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007949 int res;
7950 Py_ssize_t len;
7951 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007952 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07007953 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007954 goto failed;
7955 }
7956 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01007957 comparators = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007958 if (comparators == NULL) goto failed;
7959 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007960 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007961 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7962 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02007963 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007964 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007965 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007966 if (len != PyList_GET_SIZE(tmp)) {
7967 PyErr_SetString(PyExc_RuntimeError, "Compare field \"comparators\" changed size during iteration");
7968 goto failed;
7969 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007970 asdl_seq_SET(comparators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007971 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007972 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007973 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007974 *out = Compare(left, ops, comparators, lineno, col_offset, end_lineno,
7975 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007976 if (*out == NULL) goto failed;
7977 return 0;
7978 }
Victor Stinner74419f02020-07-03 11:35:37 +02007979 tp = state->Call_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07007980 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007981 if (isinstance == -1) {
7982 return 1;
7983 }
7984 if (isinstance) {
7985 expr_ty func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01007986 asdl_expr_seq* args;
7987 asdl_keyword_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007988
Victor Stinner74419f02020-07-03 11:35:37 +02007989 if (_PyObject_LookupAttr(obj, state->func, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007990 return 1;
7991 }
7992 if (tmp == NULL) {
7993 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
7994 return 1;
7995 }
7996 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007997 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02007998 res = obj2ast_expr(state, tmp, &func, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007999 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008000 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008001 }
Victor Stinner74419f02020-07-03 11:35:37 +02008002 if (_PyObject_LookupAttr(obj, state->args, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008003 return 1;
8004 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008005 if (tmp == NULL) {
8006 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
8007 return 1;
8008 }
8009 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008010 int res;
8011 Py_ssize_t len;
8012 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008013 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07008014 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008015 goto failed;
8016 }
8017 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01008018 args = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008019 if (args == NULL) goto failed;
8020 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008021 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008022 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8023 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02008024 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008025 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008026 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008027 if (len != PyList_GET_SIZE(tmp)) {
8028 PyErr_SetString(PyExc_RuntimeError, "Call field \"args\" changed size during iteration");
8029 goto failed;
8030 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008031 asdl_seq_SET(args, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008032 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008033 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008034 }
Victor Stinner74419f02020-07-03 11:35:37 +02008035 if (_PyObject_LookupAttr(obj, state->keywords, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008036 return 1;
8037 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008038 if (tmp == NULL) {
8039 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
8040 return 1;
8041 }
8042 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008043 int res;
8044 Py_ssize_t len;
8045 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008046 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07008047 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008048 goto failed;
8049 }
8050 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01008051 keywords = _Py_asdl_keyword_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008052 if (keywords == NULL) goto failed;
8053 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008054 keyword_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008055 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8056 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02008057 res = obj2ast_keyword(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008058 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008059 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008060 if (len != PyList_GET_SIZE(tmp)) {
8061 PyErr_SetString(PyExc_RuntimeError, "Call field \"keywords\" changed size during iteration");
8062 goto failed;
8063 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008064 asdl_seq_SET(keywords, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008065 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008066 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008067 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008068 *out = Call(func, args, keywords, lineno, col_offset, end_lineno,
8069 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008070 if (*out == NULL) goto failed;
8071 return 0;
8072 }
Victor Stinner74419f02020-07-03 11:35:37 +02008073 tp = state->FormattedValue_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07008074 isinstance = PyObject_IsInstance(obj, tp);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008075 if (isinstance == -1) {
8076 return 1;
8077 }
8078 if (isinstance) {
8079 expr_ty value;
8080 int conversion;
8081 expr_ty format_spec;
8082
Victor Stinner74419f02020-07-03 11:35:37 +02008083 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008084 return 1;
8085 }
8086 if (tmp == NULL) {
8087 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
8088 return 1;
8089 }
8090 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04008091 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008092 res = obj2ast_expr(state, tmp, &value, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008093 if (res != 0) goto failed;
8094 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008095 }
Victor Stinner74419f02020-07-03 11:35:37 +02008096 if (_PyObject_LookupAttr(obj, state->conversion, &tmp) < 0) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04008097 return 1;
8098 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008099 if (tmp == NULL || tmp == Py_None) {
8100 Py_CLEAR(tmp);
8101 conversion = 0;
8102 }
8103 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04008104 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008105 res = obj2ast_int(state, tmp, &conversion, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008106 if (res != 0) goto failed;
8107 Py_CLEAR(tmp);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008108 }
Victor Stinner74419f02020-07-03 11:35:37 +02008109 if (_PyObject_LookupAttr(obj, state->format_spec, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008110 return 1;
8111 }
8112 if (tmp == NULL || tmp == Py_None) {
8113 Py_CLEAR(tmp);
8114 format_spec = NULL;
8115 }
8116 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04008117 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008118 res = obj2ast_expr(state, tmp, &format_spec, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008119 if (res != 0) goto failed;
8120 Py_CLEAR(tmp);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008121 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04008122 *out = FormattedValue(value, conversion, format_spec, lineno,
8123 col_offset, end_lineno, end_col_offset, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008124 if (*out == NULL) goto failed;
8125 return 0;
8126 }
Victor Stinner74419f02020-07-03 11:35:37 +02008127 tp = state->JoinedStr_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07008128 isinstance = PyObject_IsInstance(obj, tp);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008129 if (isinstance == -1) {
8130 return 1;
8131 }
8132 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01008133 asdl_expr_seq* values;
Eric V. Smith235a6f02015-09-19 14:51:32 -04008134
Victor Stinner74419f02020-07-03 11:35:37 +02008135 if (_PyObject_LookupAttr(obj, state->values, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008136 return 1;
8137 }
8138 if (tmp == NULL) {
8139 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
8140 return 1;
8141 }
8142 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04008143 int res;
8144 Py_ssize_t len;
8145 Py_ssize_t i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04008146 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07008147 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Eric V. Smith235a6f02015-09-19 14:51:32 -04008148 goto failed;
8149 }
8150 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01008151 values = _Py_asdl_expr_seq_new(len, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008152 if (values == NULL) goto failed;
8153 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008154 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008155 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8156 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02008157 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008158 Py_DECREF(tmp2);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008159 if (res != 0) goto failed;
Serhiy Storchaka5e808552016-10-07 21:55:49 +03008160 if (len != PyList_GET_SIZE(tmp)) {
8161 PyErr_SetString(PyExc_RuntimeError, "JoinedStr field \"values\" changed size during iteration");
8162 goto failed;
8163 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008164 asdl_seq_SET(values, i, val);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008165 }
8166 Py_CLEAR(tmp);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008167 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008168 *out = JoinedStr(values, lineno, col_offset, end_lineno,
8169 end_col_offset, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04008170 if (*out == NULL) goto failed;
8171 return 0;
8172 }
Victor Stinner74419f02020-07-03 11:35:37 +02008173 tp = state->Constant_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07008174 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01008175 if (isinstance == -1) {
8176 return 1;
8177 }
8178 if (isinstance) {
8179 constant value;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07008180 string kind;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01008181
Victor Stinner74419f02020-07-03 11:35:37 +02008182 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008183 return 1;
8184 }
8185 if (tmp == NULL) {
8186 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
8187 return 1;
8188 }
8189 else {
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01008190 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008191 res = obj2ast_constant(state, tmp, &value, arena);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01008192 if (res != 0) goto failed;
8193 Py_CLEAR(tmp);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01008194 }
Victor Stinner74419f02020-07-03 11:35:37 +02008195 if (_PyObject_LookupAttr(obj, state->kind, &tmp) < 0) {
Guido van Rossum10f8ce62019-03-13 13:00:46 -07008196 return 1;
8197 }
8198 if (tmp == NULL || tmp == Py_None) {
8199 Py_CLEAR(tmp);
8200 kind = NULL;
8201 }
8202 else {
8203 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008204 res = obj2ast_string(state, tmp, &kind, arena);
Guido van Rossum10f8ce62019-03-13 13:00:46 -07008205 if (res != 0) goto failed;
8206 Py_CLEAR(tmp);
8207 }
8208 *out = Constant(value, kind, lineno, col_offset, end_lineno,
8209 end_col_offset, arena);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01008210 if (*out == NULL) goto failed;
8211 return 0;
8212 }
Victor Stinner74419f02020-07-03 11:35:37 +02008213 tp = state->Attribute_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07008214 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008215 if (isinstance == -1) {
8216 return 1;
8217 }
8218 if (isinstance) {
8219 expr_ty value;
8220 identifier attr;
8221 expr_context_ty ctx;
8222
Victor Stinner74419f02020-07-03 11:35:37 +02008223 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008224 return 1;
8225 }
8226 if (tmp == NULL) {
8227 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
8228 return 1;
8229 }
8230 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008231 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008232 res = obj2ast_expr(state, tmp, &value, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008233 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008234 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008235 }
Victor Stinner74419f02020-07-03 11:35:37 +02008236 if (_PyObject_LookupAttr(obj, state->attr, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008237 return 1;
8238 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008239 if (tmp == NULL) {
8240 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
8241 return 1;
8242 }
8243 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008244 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008245 res = obj2ast_identifier(state, tmp, &attr, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008246 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008247 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008248 }
Victor Stinner74419f02020-07-03 11:35:37 +02008249 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008250 return 1;
8251 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008252 if (tmp == NULL) {
8253 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
8254 return 1;
8255 }
8256 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008257 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008258 res = obj2ast_expr_context(state, tmp, &ctx, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008259 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008260 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008261 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008262 *out = Attribute(value, attr, ctx, lineno, col_offset, end_lineno,
8263 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008264 if (*out == NULL) goto failed;
8265 return 0;
8266 }
Victor Stinner74419f02020-07-03 11:35:37 +02008267 tp = state->Subscript_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07008268 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008269 if (isinstance == -1) {
8270 return 1;
8271 }
8272 if (isinstance) {
8273 expr_ty value;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02008274 expr_ty slice;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008275 expr_context_ty ctx;
8276
Victor Stinner74419f02020-07-03 11:35:37 +02008277 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008278 return 1;
8279 }
8280 if (tmp == NULL) {
8281 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
8282 return 1;
8283 }
8284 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008285 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008286 res = obj2ast_expr(state, tmp, &value, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008287 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008288 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008289 }
Victor Stinner74419f02020-07-03 11:35:37 +02008290 if (_PyObject_LookupAttr(obj, state->slice, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008291 return 1;
8292 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008293 if (tmp == NULL) {
8294 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
8295 return 1;
8296 }
8297 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008298 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008299 res = obj2ast_expr(state, tmp, &slice, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008300 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008301 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008302 }
Victor Stinner74419f02020-07-03 11:35:37 +02008303 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008304 return 1;
8305 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008306 if (tmp == NULL) {
8307 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
8308 return 1;
8309 }
8310 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008311 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008312 res = obj2ast_expr_context(state, tmp, &ctx, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008313 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008314 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008315 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008316 *out = Subscript(value, slice, ctx, lineno, col_offset, end_lineno,
8317 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008318 if (*out == NULL) goto failed;
8319 return 0;
8320 }
Victor Stinner74419f02020-07-03 11:35:37 +02008321 tp = state->Starred_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07008322 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008323 if (isinstance == -1) {
8324 return 1;
8325 }
8326 if (isinstance) {
8327 expr_ty value;
8328 expr_context_ty ctx;
8329
Victor Stinner74419f02020-07-03 11:35:37 +02008330 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008331 return 1;
8332 }
8333 if (tmp == NULL) {
8334 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
8335 return 1;
8336 }
8337 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008338 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008339 res = obj2ast_expr(state, tmp, &value, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008340 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008341 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008342 }
Victor Stinner74419f02020-07-03 11:35:37 +02008343 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008344 return 1;
8345 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008346 if (tmp == NULL) {
8347 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
8348 return 1;
8349 }
8350 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008351 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008352 res = obj2ast_expr_context(state, tmp, &ctx, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008353 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008354 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008355 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008356 *out = Starred(value, ctx, lineno, col_offset, end_lineno,
8357 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008358 if (*out == NULL) goto failed;
8359 return 0;
8360 }
Victor Stinner74419f02020-07-03 11:35:37 +02008361 tp = state->Name_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07008362 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008363 if (isinstance == -1) {
8364 return 1;
8365 }
8366 if (isinstance) {
8367 identifier id;
8368 expr_context_ty ctx;
8369
Victor Stinner74419f02020-07-03 11:35:37 +02008370 if (_PyObject_LookupAttr(obj, state->id, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008371 return 1;
8372 }
8373 if (tmp == NULL) {
8374 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
8375 return 1;
8376 }
8377 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008378 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008379 res = obj2ast_identifier(state, tmp, &id, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008380 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008381 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008382 }
Victor Stinner74419f02020-07-03 11:35:37 +02008383 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008384 return 1;
8385 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008386 if (tmp == NULL) {
8387 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
8388 return 1;
8389 }
8390 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008391 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008392 res = obj2ast_expr_context(state, tmp, &ctx, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008393 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008394 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008395 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008396 *out = Name(id, ctx, lineno, col_offset, end_lineno, end_col_offset,
8397 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008398 if (*out == NULL) goto failed;
8399 return 0;
8400 }
Victor Stinner74419f02020-07-03 11:35:37 +02008401 tp = state->List_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07008402 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008403 if (isinstance == -1) {
8404 return 1;
8405 }
8406 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01008407 asdl_expr_seq* elts;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008408 expr_context_ty ctx;
8409
Victor Stinner74419f02020-07-03 11:35:37 +02008410 if (_PyObject_LookupAttr(obj, state->elts, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008411 return 1;
8412 }
8413 if (tmp == NULL) {
8414 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
8415 return 1;
8416 }
8417 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008418 int res;
8419 Py_ssize_t len;
8420 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008421 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07008422 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008423 goto failed;
8424 }
8425 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01008426 elts = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008427 if (elts == NULL) goto failed;
8428 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008429 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008430 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8431 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02008432 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008433 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008434 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008435 if (len != PyList_GET_SIZE(tmp)) {
8436 PyErr_SetString(PyExc_RuntimeError, "List field \"elts\" changed size during iteration");
8437 goto failed;
8438 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008439 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008440 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008441 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008442 }
Victor Stinner74419f02020-07-03 11:35:37 +02008443 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008444 return 1;
8445 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008446 if (tmp == NULL) {
8447 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
8448 return 1;
8449 }
8450 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008451 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008452 res = obj2ast_expr_context(state, tmp, &ctx, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008453 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008454 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008455 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008456 *out = List(elts, ctx, lineno, col_offset, end_lineno, end_col_offset,
8457 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008458 if (*out == NULL) goto failed;
8459 return 0;
8460 }
Victor Stinner74419f02020-07-03 11:35:37 +02008461 tp = state->Tuple_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07008462 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008463 if (isinstance == -1) {
8464 return 1;
8465 }
8466 if (isinstance) {
Pablo Galindoa5634c42020-09-16 19:42:00 +01008467 asdl_expr_seq* elts;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008468 expr_context_ty ctx;
8469
Victor Stinner74419f02020-07-03 11:35:37 +02008470 if (_PyObject_LookupAttr(obj, state->elts, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008471 return 1;
8472 }
8473 if (tmp == NULL) {
8474 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
8475 return 1;
8476 }
8477 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008478 int res;
8479 Py_ssize_t len;
8480 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008481 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07008482 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008483 goto failed;
8484 }
8485 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01008486 elts = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008487 if (elts == NULL) goto failed;
8488 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008489 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008490 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8491 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02008492 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008493 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008494 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008495 if (len != PyList_GET_SIZE(tmp)) {
8496 PyErr_SetString(PyExc_RuntimeError, "Tuple field \"elts\" changed size during iteration");
8497 goto failed;
8498 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008499 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008500 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008501 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008502 }
Victor Stinner74419f02020-07-03 11:35:37 +02008503 if (_PyObject_LookupAttr(obj, state->ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008504 return 1;
8505 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008506 if (tmp == NULL) {
8507 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
8508 return 1;
8509 }
8510 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008511 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008512 res = obj2ast_expr_context(state, tmp, &ctx, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008513 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008514 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008515 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008516 *out = Tuple(elts, ctx, lineno, col_offset, end_lineno, end_col_offset,
8517 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008518 if (*out == NULL) goto failed;
8519 return 0;
8520 }
Victor Stinner74419f02020-07-03 11:35:37 +02008521 tp = state->Slice_type;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02008522 isinstance = PyObject_IsInstance(obj, tp);
8523 if (isinstance == -1) {
8524 return 1;
8525 }
8526 if (isinstance) {
8527 expr_ty lower;
8528 expr_ty upper;
8529 expr_ty step;
8530
Victor Stinner74419f02020-07-03 11:35:37 +02008531 if (_PyObject_LookupAttr(obj, state->lower, &tmp) < 0) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02008532 return 1;
8533 }
8534 if (tmp == NULL || tmp == Py_None) {
8535 Py_CLEAR(tmp);
8536 lower = NULL;
8537 }
8538 else {
8539 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008540 res = obj2ast_expr(state, tmp, &lower, arena);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02008541 if (res != 0) goto failed;
8542 Py_CLEAR(tmp);
8543 }
Victor Stinner74419f02020-07-03 11:35:37 +02008544 if (_PyObject_LookupAttr(obj, state->upper, &tmp) < 0) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02008545 return 1;
8546 }
8547 if (tmp == NULL || tmp == Py_None) {
8548 Py_CLEAR(tmp);
8549 upper = NULL;
8550 }
8551 else {
8552 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008553 res = obj2ast_expr(state, tmp, &upper, arena);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02008554 if (res != 0) goto failed;
8555 Py_CLEAR(tmp);
8556 }
Victor Stinner74419f02020-07-03 11:35:37 +02008557 if (_PyObject_LookupAttr(obj, state->step, &tmp) < 0) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02008558 return 1;
8559 }
8560 if (tmp == NULL || tmp == Py_None) {
8561 Py_CLEAR(tmp);
8562 step = NULL;
8563 }
8564 else {
8565 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008566 res = obj2ast_expr(state, tmp, &step, arena);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02008567 if (res != 0) goto failed;
8568 Py_CLEAR(tmp);
8569 }
8570 *out = Slice(lower, upper, step, lineno, col_offset, end_lineno,
8571 end_col_offset, arena);
8572 if (*out == NULL) goto failed;
8573 return 0;
8574 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008575
8576 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
8577 failed:
8578 Py_XDECREF(tmp);
8579 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008580}
8581
8582int
Victor Stinner5cf47822020-11-02 22:03:28 +01008583obj2ast_expr_context(struct ast_state *state, PyObject* obj, expr_context_ty*
Victor Stinner74419f02020-07-03 11:35:37 +02008584 out, PyArena* arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008585{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008586 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008587
Victor Stinner74419f02020-07-03 11:35:37 +02008588 isinstance = PyObject_IsInstance(obj, state->Load_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008589 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008590 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008591 }
8592 if (isinstance) {
8593 *out = Load;
8594 return 0;
8595 }
Victor Stinner74419f02020-07-03 11:35:37 +02008596 isinstance = PyObject_IsInstance(obj, state->Store_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008597 if (isinstance == -1) {
8598 return 1;
8599 }
8600 if (isinstance) {
8601 *out = Store;
8602 return 0;
8603 }
Victor Stinner74419f02020-07-03 11:35:37 +02008604 isinstance = PyObject_IsInstance(obj, state->Del_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008605 if (isinstance == -1) {
8606 return 1;
8607 }
8608 if (isinstance) {
8609 *out = Del;
8610 return 0;
8611 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008612
8613 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
8614 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008615}
8616
8617int
Victor Stinner5cf47822020-11-02 22:03:28 +01008618obj2ast_boolop(struct ast_state *state, PyObject* obj, boolop_ty* out, PyArena*
Victor Stinner74419f02020-07-03 11:35:37 +02008619 arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008620{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008621 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008622
Victor Stinner74419f02020-07-03 11:35:37 +02008623 isinstance = PyObject_IsInstance(obj, state->And_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008624 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008625 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008626 }
8627 if (isinstance) {
8628 *out = And;
8629 return 0;
8630 }
Victor Stinner74419f02020-07-03 11:35:37 +02008631 isinstance = PyObject_IsInstance(obj, state->Or_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008632 if (isinstance == -1) {
8633 return 1;
8634 }
8635 if (isinstance) {
8636 *out = Or;
8637 return 0;
8638 }
8639
8640 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
8641 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008642}
8643
8644int
Victor Stinner5cf47822020-11-02 22:03:28 +01008645obj2ast_operator(struct ast_state *state, PyObject* obj, operator_ty* out,
Victor Stinner74419f02020-07-03 11:35:37 +02008646 PyArena* arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008647{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008648 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008649
Victor Stinner74419f02020-07-03 11:35:37 +02008650 isinstance = PyObject_IsInstance(obj, state->Add_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008651 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008652 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008653 }
8654 if (isinstance) {
8655 *out = Add;
8656 return 0;
8657 }
Victor Stinner74419f02020-07-03 11:35:37 +02008658 isinstance = PyObject_IsInstance(obj, state->Sub_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008659 if (isinstance == -1) {
8660 return 1;
8661 }
8662 if (isinstance) {
8663 *out = Sub;
8664 return 0;
8665 }
Victor Stinner74419f02020-07-03 11:35:37 +02008666 isinstance = PyObject_IsInstance(obj, state->Mult_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008667 if (isinstance == -1) {
8668 return 1;
8669 }
8670 if (isinstance) {
8671 *out = Mult;
8672 return 0;
8673 }
Victor Stinner74419f02020-07-03 11:35:37 +02008674 isinstance = PyObject_IsInstance(obj, state->MatMult_type);
Benjamin Petersond51374e2014-04-09 23:55:56 -04008675 if (isinstance == -1) {
8676 return 1;
8677 }
8678 if (isinstance) {
8679 *out = MatMult;
8680 return 0;
8681 }
Victor Stinner74419f02020-07-03 11:35:37 +02008682 isinstance = PyObject_IsInstance(obj, state->Div_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008683 if (isinstance == -1) {
8684 return 1;
8685 }
8686 if (isinstance) {
8687 *out = Div;
8688 return 0;
8689 }
Victor Stinner74419f02020-07-03 11:35:37 +02008690 isinstance = PyObject_IsInstance(obj, state->Mod_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008691 if (isinstance == -1) {
8692 return 1;
8693 }
8694 if (isinstance) {
8695 *out = Mod;
8696 return 0;
8697 }
Victor Stinner74419f02020-07-03 11:35:37 +02008698 isinstance = PyObject_IsInstance(obj, state->Pow_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008699 if (isinstance == -1) {
8700 return 1;
8701 }
8702 if (isinstance) {
8703 *out = Pow;
8704 return 0;
8705 }
Victor Stinner74419f02020-07-03 11:35:37 +02008706 isinstance = PyObject_IsInstance(obj, state->LShift_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008707 if (isinstance == -1) {
8708 return 1;
8709 }
8710 if (isinstance) {
8711 *out = LShift;
8712 return 0;
8713 }
Victor Stinner74419f02020-07-03 11:35:37 +02008714 isinstance = PyObject_IsInstance(obj, state->RShift_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008715 if (isinstance == -1) {
8716 return 1;
8717 }
8718 if (isinstance) {
8719 *out = RShift;
8720 return 0;
8721 }
Victor Stinner74419f02020-07-03 11:35:37 +02008722 isinstance = PyObject_IsInstance(obj, state->BitOr_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008723 if (isinstance == -1) {
8724 return 1;
8725 }
8726 if (isinstance) {
8727 *out = BitOr;
8728 return 0;
8729 }
Victor Stinner74419f02020-07-03 11:35:37 +02008730 isinstance = PyObject_IsInstance(obj, state->BitXor_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008731 if (isinstance == -1) {
8732 return 1;
8733 }
8734 if (isinstance) {
8735 *out = BitXor;
8736 return 0;
8737 }
Victor Stinner74419f02020-07-03 11:35:37 +02008738 isinstance = PyObject_IsInstance(obj, state->BitAnd_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008739 if (isinstance == -1) {
8740 return 1;
8741 }
8742 if (isinstance) {
8743 *out = BitAnd;
8744 return 0;
8745 }
Victor Stinner74419f02020-07-03 11:35:37 +02008746 isinstance = PyObject_IsInstance(obj, state->FloorDiv_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008747 if (isinstance == -1) {
8748 return 1;
8749 }
8750 if (isinstance) {
8751 *out = FloorDiv;
8752 return 0;
8753 }
8754
8755 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
8756 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008757}
8758
8759int
Victor Stinner5cf47822020-11-02 22:03:28 +01008760obj2ast_unaryop(struct ast_state *state, PyObject* obj, unaryop_ty* out,
8761 PyArena* arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008762{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008763 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008764
Victor Stinner74419f02020-07-03 11:35:37 +02008765 isinstance = PyObject_IsInstance(obj, state->Invert_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008766 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008767 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008768 }
8769 if (isinstance) {
8770 *out = Invert;
8771 return 0;
8772 }
Victor Stinner74419f02020-07-03 11:35:37 +02008773 isinstance = PyObject_IsInstance(obj, state->Not_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008774 if (isinstance == -1) {
8775 return 1;
8776 }
8777 if (isinstance) {
8778 *out = Not;
8779 return 0;
8780 }
Victor Stinner74419f02020-07-03 11:35:37 +02008781 isinstance = PyObject_IsInstance(obj, state->UAdd_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008782 if (isinstance == -1) {
8783 return 1;
8784 }
8785 if (isinstance) {
8786 *out = UAdd;
8787 return 0;
8788 }
Victor Stinner74419f02020-07-03 11:35:37 +02008789 isinstance = PyObject_IsInstance(obj, state->USub_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008790 if (isinstance == -1) {
8791 return 1;
8792 }
8793 if (isinstance) {
8794 *out = USub;
8795 return 0;
8796 }
8797
8798 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
8799 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008800}
8801
8802int
Victor Stinner5cf47822020-11-02 22:03:28 +01008803obj2ast_cmpop(struct ast_state *state, PyObject* obj, cmpop_ty* out, PyArena*
Victor Stinner74419f02020-07-03 11:35:37 +02008804 arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008805{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008806 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008807
Victor Stinner74419f02020-07-03 11:35:37 +02008808 isinstance = PyObject_IsInstance(obj, state->Eq_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008809 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008810 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008811 }
8812 if (isinstance) {
8813 *out = Eq;
8814 return 0;
8815 }
Victor Stinner74419f02020-07-03 11:35:37 +02008816 isinstance = PyObject_IsInstance(obj, state->NotEq_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008817 if (isinstance == -1) {
8818 return 1;
8819 }
8820 if (isinstance) {
8821 *out = NotEq;
8822 return 0;
8823 }
Victor Stinner74419f02020-07-03 11:35:37 +02008824 isinstance = PyObject_IsInstance(obj, state->Lt_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008825 if (isinstance == -1) {
8826 return 1;
8827 }
8828 if (isinstance) {
8829 *out = Lt;
8830 return 0;
8831 }
Victor Stinner74419f02020-07-03 11:35:37 +02008832 isinstance = PyObject_IsInstance(obj, state->LtE_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008833 if (isinstance == -1) {
8834 return 1;
8835 }
8836 if (isinstance) {
8837 *out = LtE;
8838 return 0;
8839 }
Victor Stinner74419f02020-07-03 11:35:37 +02008840 isinstance = PyObject_IsInstance(obj, state->Gt_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008841 if (isinstance == -1) {
8842 return 1;
8843 }
8844 if (isinstance) {
8845 *out = Gt;
8846 return 0;
8847 }
Victor Stinner74419f02020-07-03 11:35:37 +02008848 isinstance = PyObject_IsInstance(obj, state->GtE_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008849 if (isinstance == -1) {
8850 return 1;
8851 }
8852 if (isinstance) {
8853 *out = GtE;
8854 return 0;
8855 }
Victor Stinner74419f02020-07-03 11:35:37 +02008856 isinstance = PyObject_IsInstance(obj, state->Is_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008857 if (isinstance == -1) {
8858 return 1;
8859 }
8860 if (isinstance) {
8861 *out = Is;
8862 return 0;
8863 }
Victor Stinner74419f02020-07-03 11:35:37 +02008864 isinstance = PyObject_IsInstance(obj, state->IsNot_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008865 if (isinstance == -1) {
8866 return 1;
8867 }
8868 if (isinstance) {
8869 *out = IsNot;
8870 return 0;
8871 }
Victor Stinner74419f02020-07-03 11:35:37 +02008872 isinstance = PyObject_IsInstance(obj, state->In_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008873 if (isinstance == -1) {
8874 return 1;
8875 }
8876 if (isinstance) {
8877 *out = In;
8878 return 0;
8879 }
Victor Stinner74419f02020-07-03 11:35:37 +02008880 isinstance = PyObject_IsInstance(obj, state->NotIn_type);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008881 if (isinstance == -1) {
8882 return 1;
8883 }
8884 if (isinstance) {
8885 *out = NotIn;
8886 return 0;
8887 }
8888
8889 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
8890 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008891}
8892
8893int
Victor Stinner5cf47822020-11-02 22:03:28 +01008894obj2ast_comprehension(struct ast_state *state, PyObject* obj, comprehension_ty*
Victor Stinner74419f02020-07-03 11:35:37 +02008895 out, PyArena* arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008896{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008897 PyObject* tmp = NULL;
8898 expr_ty target;
8899 expr_ty iter;
Pablo Galindoa5634c42020-09-16 19:42:00 +01008900 asdl_expr_seq* ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008901 int is_async;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008902
Victor Stinner74419f02020-07-03 11:35:37 +02008903 if (_PyObject_LookupAttr(obj, state->target, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008904 return 1;
8905 }
8906 if (tmp == NULL) {
8907 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
8908 return 1;
8909 }
8910 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008911 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008912 res = obj2ast_expr(state, tmp, &target, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008913 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008914 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008915 }
Victor Stinner74419f02020-07-03 11:35:37 +02008916 if (_PyObject_LookupAttr(obj, state->iter, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008917 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008918 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008919 if (tmp == NULL) {
8920 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
8921 return 1;
8922 }
8923 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008924 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008925 res = obj2ast_expr(state, tmp, &iter, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008926 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008927 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008928 }
Victor Stinner74419f02020-07-03 11:35:37 +02008929 if (_PyObject_LookupAttr(obj, state->ifs, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008930 return 1;
8931 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008932 if (tmp == NULL) {
8933 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
8934 return 1;
8935 }
8936 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008937 int res;
8938 Py_ssize_t len;
8939 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008940 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07008941 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008942 goto failed;
8943 }
8944 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01008945 ifs = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008946 if (ifs == NULL) goto failed;
8947 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008948 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008949 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8950 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02008951 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008952 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008953 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008954 if (len != PyList_GET_SIZE(tmp)) {
8955 PyErr_SetString(PyExc_RuntimeError, "comprehension field \"ifs\" changed size during iteration");
8956 goto failed;
8957 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008958 asdl_seq_SET(ifs, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008959 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008960 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008961 }
Victor Stinner74419f02020-07-03 11:35:37 +02008962 if (_PyObject_LookupAttr(obj, state->is_async, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008963 return 1;
8964 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008965 if (tmp == NULL) {
8966 PyErr_SetString(PyExc_TypeError, "required field \"is_async\" missing from comprehension");
8967 return 1;
8968 }
8969 else {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008970 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02008971 res = obj2ast_int(state, tmp, &is_async, arena);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008972 if (res != 0) goto failed;
8973 Py_CLEAR(tmp);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008974 }
8975 *out = comprehension(target, iter, ifs, is_async, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008976 return 0;
8977failed:
8978 Py_XDECREF(tmp);
8979 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008980}
8981
8982int
Victor Stinner5cf47822020-11-02 22:03:28 +01008983obj2ast_excepthandler(struct ast_state *state, PyObject* obj, excepthandler_ty*
Victor Stinner74419f02020-07-03 11:35:37 +02008984 out, PyArena* arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008985{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008986 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00008987
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008988 PyObject *tmp = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07008989 PyObject *tp;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008990 int lineno;
8991 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008992 int end_lineno;
8993 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008994
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008995 if (obj == Py_None) {
8996 *out = NULL;
8997 return 0;
8998 }
Victor Stinner74419f02020-07-03 11:35:37 +02008999 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009000 return 1;
9001 }
9002 if (tmp == NULL) {
9003 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
9004 return 1;
9005 }
9006 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009007 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009008 res = obj2ast_int(state, tmp, &lineno, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009009 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009010 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009011 }
Victor Stinner74419f02020-07-03 11:35:37 +02009012 if (_PyObject_LookupAttr(obj, state->col_offset, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009013 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009014 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009015 if (tmp == NULL) {
9016 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
9017 return 1;
9018 }
9019 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009020 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009021 res = obj2ast_int(state, tmp, &col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009022 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009023 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009024 }
Victor Stinner74419f02020-07-03 11:35:37 +02009025 if (_PyObject_LookupAttr(obj, state->end_lineno, &tmp) < 0) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00009026 return 1;
9027 }
9028 if (tmp == NULL || tmp == Py_None) {
9029 Py_CLEAR(tmp);
9030 end_lineno = 0;
9031 }
9032 else {
9033 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009034 res = obj2ast_int(state, tmp, &end_lineno, arena);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00009035 if (res != 0) goto failed;
9036 Py_CLEAR(tmp);
9037 }
Victor Stinner74419f02020-07-03 11:35:37 +02009038 if (_PyObject_LookupAttr(obj, state->end_col_offset, &tmp) < 0) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00009039 return 1;
9040 }
9041 if (tmp == NULL || tmp == Py_None) {
9042 Py_CLEAR(tmp);
9043 end_col_offset = 0;
9044 }
9045 else {
9046 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009047 res = obj2ast_int(state, tmp, &end_col_offset, arena);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00009048 if (res != 0) goto failed;
9049 Py_CLEAR(tmp);
9050 }
Victor Stinner74419f02020-07-03 11:35:37 +02009051 tp = state->ExceptHandler_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07009052 isinstance = PyObject_IsInstance(obj, tp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009053 if (isinstance == -1) {
9054 return 1;
9055 }
9056 if (isinstance) {
9057 expr_ty type;
9058 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01009059 asdl_stmt_seq* body;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009060
Victor Stinner74419f02020-07-03 11:35:37 +02009061 if (_PyObject_LookupAttr(obj, state->type, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009062 return 1;
9063 }
9064 if (tmp == NULL || tmp == Py_None) {
9065 Py_CLEAR(tmp);
9066 type = NULL;
9067 }
9068 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009069 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009070 res = obj2ast_expr(state, tmp, &type, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009071 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009072 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009073 }
Victor Stinner74419f02020-07-03 11:35:37 +02009074 if (_PyObject_LookupAttr(obj, state->name, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009075 return 1;
9076 }
9077 if (tmp == NULL || tmp == Py_None) {
9078 Py_CLEAR(tmp);
9079 name = NULL;
9080 }
9081 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009082 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009083 res = obj2ast_identifier(state, tmp, &name, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009084 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009085 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009086 }
Victor Stinner74419f02020-07-03 11:35:37 +02009087 if (_PyObject_LookupAttr(obj, state->body, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009088 return 1;
9089 }
9090 if (tmp == NULL) {
9091 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
9092 return 1;
9093 }
9094 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009095 int res;
9096 Py_ssize_t len;
9097 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009098 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07009099 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009100 goto failed;
9101 }
9102 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01009103 body = _Py_asdl_stmt_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009104 if (body == NULL) goto failed;
9105 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07009106 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009107 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9108 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02009109 res = obj2ast_stmt(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009110 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009111 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03009112 if (len != PyList_GET_SIZE(tmp)) {
9113 PyErr_SetString(PyExc_RuntimeError, "ExceptHandler field \"body\" changed size during iteration");
9114 goto failed;
9115 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07009116 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009117 }
Victor Stinner1acc1292013-07-27 00:03:47 +02009118 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009119 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00009120 *out = ExceptHandler(type, name, body, lineno, col_offset, end_lineno,
9121 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009122 if (*out == NULL) goto failed;
9123 return 0;
9124 }
9125
9126 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
9127 failed:
9128 Py_XDECREF(tmp);
9129 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009130}
9131
9132int
Victor Stinner5cf47822020-11-02 22:03:28 +01009133obj2ast_arguments(struct ast_state *state, PyObject* obj, arguments_ty* out,
Victor Stinner74419f02020-07-03 11:35:37 +02009134 PyArena* arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009135{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009136 PyObject* tmp = NULL;
Pablo Galindoa5634c42020-09-16 19:42:00 +01009137 asdl_arg_seq* posonlyargs;
9138 asdl_arg_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02009139 arg_ty vararg;
Pablo Galindoa5634c42020-09-16 19:42:00 +01009140 asdl_arg_seq* kwonlyargs;
9141 asdl_expr_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02009142 arg_ty kwarg;
Pablo Galindoa5634c42020-09-16 19:42:00 +01009143 asdl_expr_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009144
Victor Stinner74419f02020-07-03 11:35:37 +02009145 if (_PyObject_LookupAttr(obj, state->posonlyargs, &tmp) < 0) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01009146 return 1;
9147 }
9148 if (tmp == NULL) {
9149 PyErr_SetString(PyExc_TypeError, "required field \"posonlyargs\" missing from arguments");
9150 return 1;
9151 }
9152 else {
9153 int res;
9154 Py_ssize_t len;
9155 Py_ssize_t i;
9156 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07009157 PyErr_Format(PyExc_TypeError, "arguments field \"posonlyargs\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01009158 goto failed;
9159 }
9160 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01009161 posonlyargs = _Py_asdl_arg_seq_new(len, arena);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01009162 if (posonlyargs == NULL) goto failed;
9163 for (i = 0; i < len; i++) {
9164 arg_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009165 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9166 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02009167 res = obj2ast_arg(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009168 Py_DECREF(tmp2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01009169 if (res != 0) goto failed;
9170 if (len != PyList_GET_SIZE(tmp)) {
9171 PyErr_SetString(PyExc_RuntimeError, "arguments field \"posonlyargs\" changed size during iteration");
9172 goto failed;
9173 }
9174 asdl_seq_SET(posonlyargs, i, val);
9175 }
9176 Py_CLEAR(tmp);
9177 }
Victor Stinner74419f02020-07-03 11:35:37 +02009178 if (_PyObject_LookupAttr(obj, state->args, &tmp) < 0) {
Pablo Galindocd6e83b2019-07-15 01:32:18 +02009179 return 1;
9180 }
9181 if (tmp == NULL) {
9182 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
9183 return 1;
9184 }
9185 else {
9186 int res;
9187 Py_ssize_t len;
9188 Py_ssize_t i;
9189 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07009190 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Pablo Galindocd6e83b2019-07-15 01:32:18 +02009191 goto failed;
9192 }
9193 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01009194 args = _Py_asdl_arg_seq_new(len, arena);
Pablo Galindocd6e83b2019-07-15 01:32:18 +02009195 if (args == NULL) goto failed;
9196 for (i = 0; i < len; i++) {
9197 arg_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009198 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9199 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02009200 res = obj2ast_arg(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009201 Py_DECREF(tmp2);
Pablo Galindocd6e83b2019-07-15 01:32:18 +02009202 if (res != 0) goto failed;
9203 if (len != PyList_GET_SIZE(tmp)) {
9204 PyErr_SetString(PyExc_RuntimeError, "arguments field \"args\" changed size during iteration");
9205 goto failed;
9206 }
9207 asdl_seq_SET(args, i, val);
9208 }
9209 Py_CLEAR(tmp);
9210 }
Victor Stinner74419f02020-07-03 11:35:37 +02009211 if (_PyObject_LookupAttr(obj, state->vararg, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009212 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009213 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009214 if (tmp == NULL || tmp == Py_None) {
9215 Py_CLEAR(tmp);
9216 vararg = NULL;
9217 }
9218 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009219 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009220 res = obj2ast_arg(state, tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009221 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009222 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009223 }
Victor Stinner74419f02020-07-03 11:35:37 +02009224 if (_PyObject_LookupAttr(obj, state->kwonlyargs, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009225 return 1;
9226 }
9227 if (tmp == NULL) {
9228 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
9229 return 1;
9230 }
9231 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009232 int res;
9233 Py_ssize_t len;
9234 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009235 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07009236 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009237 goto failed;
9238 }
9239 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01009240 kwonlyargs = _Py_asdl_arg_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009241 if (kwonlyargs == NULL) goto failed;
9242 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07009243 arg_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009244 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9245 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02009246 res = obj2ast_arg(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009247 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009248 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03009249 if (len != PyList_GET_SIZE(tmp)) {
9250 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kwonlyargs\" changed size during iteration");
9251 goto failed;
9252 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07009253 asdl_seq_SET(kwonlyargs, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009254 }
Victor Stinner1acc1292013-07-27 00:03:47 +02009255 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009256 }
Victor Stinner74419f02020-07-03 11:35:37 +02009257 if (_PyObject_LookupAttr(obj, state->kw_defaults, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009258 return 1;
9259 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009260 if (tmp == NULL) {
9261 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
9262 return 1;
9263 }
9264 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009265 int res;
9266 Py_ssize_t len;
9267 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009268 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07009269 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009270 goto failed;
9271 }
9272 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01009273 kw_defaults = _Py_asdl_expr_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009274 if (kw_defaults == NULL) goto failed;
9275 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07009276 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009277 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9278 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02009279 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009280 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009281 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03009282 if (len != PyList_GET_SIZE(tmp)) {
9283 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kw_defaults\" changed size during iteration");
9284 goto failed;
9285 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07009286 asdl_seq_SET(kw_defaults, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009287 }
Victor Stinner1acc1292013-07-27 00:03:47 +02009288 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009289 }
Victor Stinner74419f02020-07-03 11:35:37 +02009290 if (_PyObject_LookupAttr(obj, state->kwarg, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009291 return 1;
9292 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009293 if (tmp == NULL || tmp == Py_None) {
9294 Py_CLEAR(tmp);
9295 kwarg = NULL;
9296 }
9297 else {
Victor Stinneree4b59c2013-07-27 00:01:35 +02009298 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009299 res = obj2ast_arg(state, tmp, &kwarg, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02009300 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02009301 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02009302 }
Victor Stinner74419f02020-07-03 11:35:37 +02009303 if (_PyObject_LookupAttr(obj, state->defaults, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009304 return 1;
9305 }
9306 if (tmp == NULL) {
9307 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
9308 return 1;
9309 }
9310 else {
Victor Stinneree4b59c2013-07-27 00:01:35 +02009311 int res;
9312 Py_ssize_t len;
9313 Py_ssize_t i;
Victor Stinneree4b59c2013-07-27 00:01:35 +02009314 if (!PyList_Check(tmp)) {
Dino Viehlandac46eb42019-09-11 10:16:34 -07009315 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", _PyType_Name(Py_TYPE(tmp)));
Victor Stinneree4b59c2013-07-27 00:01:35 +02009316 goto failed;
9317 }
9318 len = PyList_GET_SIZE(tmp);
Pablo Galindoa5634c42020-09-16 19:42:00 +01009319 defaults = _Py_asdl_expr_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02009320 if (defaults == NULL) goto failed;
9321 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07009322 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009323 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
9324 Py_INCREF(tmp2);
Victor Stinner74419f02020-07-03 11:35:37 +02009325 res = obj2ast_expr(state, tmp2, &val, arena);
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009326 Py_DECREF(tmp2);
Victor Stinneree4b59c2013-07-27 00:01:35 +02009327 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03009328 if (len != PyList_GET_SIZE(tmp)) {
9329 PyErr_SetString(PyExc_RuntimeError, "arguments field \"defaults\" changed size during iteration");
9330 goto failed;
9331 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07009332 asdl_seq_SET(defaults, i, val);
Victor Stinneree4b59c2013-07-27 00:01:35 +02009333 }
Victor Stinnerb3189902013-07-27 00:04:42 +02009334 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02009335 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02009336 *out = arguments(posonlyargs, args, vararg, kwonlyargs, kw_defaults, kwarg,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01009337 defaults, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009338 return 0;
9339failed:
9340 Py_XDECREF(tmp);
9341 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009342}
9343
9344int
Victor Stinner5cf47822020-11-02 22:03:28 +01009345obj2ast_arg(struct ast_state *state, PyObject* obj, arg_ty* out, PyArena* arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009346{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009347 PyObject* tmp = NULL;
9348 identifier arg;
9349 expr_ty annotation;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009350 string type_comment;
Victor Stinnerc106c682015-11-06 17:01:48 +01009351 int lineno;
9352 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00009353 int end_lineno;
9354 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009355
Victor Stinner74419f02020-07-03 11:35:37 +02009356 if (_PyObject_LookupAttr(obj, state->arg, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009357 return 1;
9358 }
9359 if (tmp == NULL) {
9360 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
9361 return 1;
9362 }
9363 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009364 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009365 res = obj2ast_identifier(state, tmp, &arg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009366 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009367 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009368 }
Victor Stinner74419f02020-07-03 11:35:37 +02009369 if (_PyObject_LookupAttr(obj, state->annotation, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009370 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009371 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009372 if (tmp == NULL || tmp == Py_None) {
9373 Py_CLEAR(tmp);
9374 annotation = NULL;
9375 }
9376 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009377 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009378 res = obj2ast_expr(state, tmp, &annotation, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009379 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009380 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009381 }
Victor Stinner74419f02020-07-03 11:35:37 +02009382 if (_PyObject_LookupAttr(obj, state->type_comment, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009383 return 1;
9384 }
9385 if (tmp == NULL || tmp == Py_None) {
9386 Py_CLEAR(tmp);
9387 type_comment = NULL;
9388 }
9389 else {
9390 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009391 res = obj2ast_string(state, tmp, &type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009392 if (res != 0) goto failed;
9393 Py_CLEAR(tmp);
9394 }
Victor Stinner74419f02020-07-03 11:35:37 +02009395 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009396 return 1;
9397 }
9398 if (tmp == NULL) {
9399 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
9400 return 1;
9401 }
9402 else {
Victor Stinnerc106c682015-11-06 17:01:48 +01009403 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009404 res = obj2ast_int(state, tmp, &lineno, arena);
Victor Stinnerc106c682015-11-06 17:01:48 +01009405 if (res != 0) goto failed;
9406 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009407 }
Victor Stinner74419f02020-07-03 11:35:37 +02009408 if (_PyObject_LookupAttr(obj, state->col_offset, &tmp) < 0) {
Victor Stinnerc106c682015-11-06 17:01:48 +01009409 return 1;
9410 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009411 if (tmp == NULL) {
9412 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
9413 return 1;
9414 }
9415 else {
Victor Stinnerc106c682015-11-06 17:01:48 +01009416 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009417 res = obj2ast_int(state, tmp, &col_offset, arena);
Victor Stinnerc106c682015-11-06 17:01:48 +01009418 if (res != 0) goto failed;
9419 Py_CLEAR(tmp);
Victor Stinnerc106c682015-11-06 17:01:48 +01009420 }
Victor Stinner74419f02020-07-03 11:35:37 +02009421 if (_PyObject_LookupAttr(obj, state->end_lineno, &tmp) < 0) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00009422 return 1;
9423 }
9424 if (tmp == NULL || tmp == Py_None) {
9425 Py_CLEAR(tmp);
9426 end_lineno = 0;
9427 }
9428 else {
9429 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009430 res = obj2ast_int(state, tmp, &end_lineno, arena);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00009431 if (res != 0) goto failed;
9432 Py_CLEAR(tmp);
9433 }
Victor Stinner74419f02020-07-03 11:35:37 +02009434 if (_PyObject_LookupAttr(obj, state->end_col_offset, &tmp) < 0) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00009435 return 1;
9436 }
9437 if (tmp == NULL || tmp == Py_None) {
9438 Py_CLEAR(tmp);
9439 end_col_offset = 0;
9440 }
9441 else {
9442 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009443 res = obj2ast_int(state, tmp, &end_col_offset, arena);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00009444 if (res != 0) goto failed;
9445 Py_CLEAR(tmp);
9446 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009447 *out = arg(arg, annotation, type_comment, lineno, col_offset, end_lineno,
9448 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009449 return 0;
9450failed:
9451 Py_XDECREF(tmp);
9452 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009453}
9454
9455int
Victor Stinner5cf47822020-11-02 22:03:28 +01009456obj2ast_keyword(struct ast_state *state, PyObject* obj, keyword_ty* out,
9457 PyArena* arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009458{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009459 PyObject* tmp = NULL;
9460 identifier arg;
9461 expr_ty value;
Pablo Galindo168660b2020-04-02 00:47:39 +01009462 int lineno;
9463 int col_offset;
9464 int end_lineno;
9465 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009466
Victor Stinner74419f02020-07-03 11:35:37 +02009467 if (_PyObject_LookupAttr(obj, state->arg, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009468 return 1;
9469 }
9470 if (tmp == NULL || tmp == Py_None) {
9471 Py_CLEAR(tmp);
9472 arg = NULL;
9473 }
9474 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009475 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009476 res = obj2ast_identifier(state, tmp, &arg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009477 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009478 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009479 }
Victor Stinner74419f02020-07-03 11:35:37 +02009480 if (_PyObject_LookupAttr(obj, state->value, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009481 return 1;
9482 }
9483 if (tmp == NULL) {
9484 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
9485 return 1;
9486 }
9487 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009488 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009489 res = obj2ast_expr(state, tmp, &value, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009490 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009491 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009492 }
Victor Stinner74419f02020-07-03 11:35:37 +02009493 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
Pablo Galindo168660b2020-04-02 00:47:39 +01009494 return 1;
9495 }
9496 if (tmp == NULL) {
9497 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from keyword");
9498 return 1;
9499 }
9500 else {
9501 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009502 res = obj2ast_int(state, tmp, &lineno, arena);
Pablo Galindo168660b2020-04-02 00:47:39 +01009503 if (res != 0) goto failed;
9504 Py_CLEAR(tmp);
9505 }
Victor Stinner74419f02020-07-03 11:35:37 +02009506 if (_PyObject_LookupAttr(obj, state->col_offset, &tmp) < 0) {
Pablo Galindo168660b2020-04-02 00:47:39 +01009507 return 1;
9508 }
9509 if (tmp == NULL) {
9510 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from keyword");
9511 return 1;
9512 }
9513 else {
9514 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009515 res = obj2ast_int(state, tmp, &col_offset, arena);
Pablo Galindo168660b2020-04-02 00:47:39 +01009516 if (res != 0) goto failed;
9517 Py_CLEAR(tmp);
9518 }
Victor Stinner74419f02020-07-03 11:35:37 +02009519 if (_PyObject_LookupAttr(obj, state->end_lineno, &tmp) < 0) {
Pablo Galindo168660b2020-04-02 00:47:39 +01009520 return 1;
9521 }
9522 if (tmp == NULL || tmp == Py_None) {
9523 Py_CLEAR(tmp);
9524 end_lineno = 0;
9525 }
9526 else {
9527 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009528 res = obj2ast_int(state, tmp, &end_lineno, arena);
Pablo Galindo168660b2020-04-02 00:47:39 +01009529 if (res != 0) goto failed;
9530 Py_CLEAR(tmp);
9531 }
Victor Stinner74419f02020-07-03 11:35:37 +02009532 if (_PyObject_LookupAttr(obj, state->end_col_offset, &tmp) < 0) {
Pablo Galindo168660b2020-04-02 00:47:39 +01009533 return 1;
9534 }
9535 if (tmp == NULL || tmp == Py_None) {
9536 Py_CLEAR(tmp);
9537 end_col_offset = 0;
9538 }
9539 else {
9540 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009541 res = obj2ast_int(state, tmp, &end_col_offset, arena);
Pablo Galindo168660b2020-04-02 00:47:39 +01009542 if (res != 0) goto failed;
9543 Py_CLEAR(tmp);
9544 }
9545 *out = keyword(arg, value, lineno, col_offset, end_lineno, end_col_offset,
9546 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009547 return 0;
9548failed:
9549 Py_XDECREF(tmp);
9550 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009551}
9552
9553int
Victor Stinner5cf47822020-11-02 22:03:28 +01009554obj2ast_alias(struct ast_state *state, PyObject* obj, alias_ty* out, PyArena*
Victor Stinner74419f02020-07-03 11:35:37 +02009555 arena)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009556{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009557 PyObject* tmp = NULL;
9558 identifier name;
9559 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009560
Victor Stinner74419f02020-07-03 11:35:37 +02009561 if (_PyObject_LookupAttr(obj, state->name, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009562 return 1;
9563 }
9564 if (tmp == NULL) {
9565 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
9566 return 1;
9567 }
9568 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009569 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009570 res = obj2ast_identifier(state, tmp, &name, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009571 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009572 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009573 }
Victor Stinner74419f02020-07-03 11:35:37 +02009574 if (_PyObject_LookupAttr(obj, state->asname, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009575 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009576 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009577 if (tmp == NULL || tmp == Py_None) {
9578 Py_CLEAR(tmp);
9579 asname = NULL;
9580 }
9581 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009582 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009583 res = obj2ast_identifier(state, tmp, &asname, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009584 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009585 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009586 }
9587 *out = alias(name, asname, arena);
9588 return 0;
9589failed:
9590 Py_XDECREF(tmp);
9591 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009592}
9593
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05009594int
Victor Stinner5cf47822020-11-02 22:03:28 +01009595obj2ast_withitem(struct ast_state *state, PyObject* obj, withitem_ty* out,
Victor Stinner74419f02020-07-03 11:35:37 +02009596 PyArena* arena)
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05009597{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009598 PyObject* tmp = NULL;
9599 expr_ty context_expr;
9600 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05009601
Victor Stinner74419f02020-07-03 11:35:37 +02009602 if (_PyObject_LookupAttr(obj, state->context_expr, &tmp) < 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009603 return 1;
9604 }
9605 if (tmp == NULL) {
9606 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
9607 return 1;
9608 }
9609 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009610 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009611 res = obj2ast_expr(state, tmp, &context_expr, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009612 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009613 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009614 }
Victor Stinner74419f02020-07-03 11:35:37 +02009615 if (_PyObject_LookupAttr(obj, state->optional_vars, &tmp) < 0) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05009616 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009617 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02009618 if (tmp == NULL || tmp == Py_None) {
9619 Py_CLEAR(tmp);
9620 optional_vars = NULL;
9621 }
9622 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009623 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009624 res = obj2ast_expr(state, tmp, &optional_vars, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009625 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02009626 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009627 }
9628 *out = withitem(context_expr, optional_vars, arena);
9629 return 0;
9630failed:
9631 Py_XDECREF(tmp);
9632 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05009633}
9634
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009635int
Victor Stinner5cf47822020-11-02 22:03:28 +01009636obj2ast_type_ignore(struct ast_state *state, PyObject* obj, type_ignore_ty*
9637 out, PyArena* arena)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009638{
9639 int isinstance;
9640
9641 PyObject *tmp = NULL;
Dino Viehlandac46eb42019-09-11 10:16:34 -07009642 PyObject *tp;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009643
9644 if (obj == Py_None) {
9645 *out = NULL;
9646 return 0;
9647 }
Victor Stinner74419f02020-07-03 11:35:37 +02009648 tp = state->TypeIgnore_type;
Dino Viehlandac46eb42019-09-11 10:16:34 -07009649 isinstance = PyObject_IsInstance(obj, tp);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009650 if (isinstance == -1) {
9651 return 1;
9652 }
9653 if (isinstance) {
9654 int lineno;
Michael J. Sullivan933e1502019-05-22 07:54:20 -07009655 string tag;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009656
Victor Stinner74419f02020-07-03 11:35:37 +02009657 if (_PyObject_LookupAttr(obj, state->lineno, &tmp) < 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009658 return 1;
9659 }
9660 if (tmp == NULL) {
9661 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from TypeIgnore");
9662 return 1;
9663 }
9664 else {
9665 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009666 res = obj2ast_int(state, tmp, &lineno, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009667 if (res != 0) goto failed;
9668 Py_CLEAR(tmp);
9669 }
Victor Stinner74419f02020-07-03 11:35:37 +02009670 if (_PyObject_LookupAttr(obj, state->tag, &tmp) < 0) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -07009671 return 1;
9672 }
9673 if (tmp == NULL) {
9674 PyErr_SetString(PyExc_TypeError, "required field \"tag\" missing from TypeIgnore");
9675 return 1;
9676 }
9677 else {
9678 int res;
Victor Stinner74419f02020-07-03 11:35:37 +02009679 res = obj2ast_string(state, tmp, &tag, arena);
Michael J. Sullivan933e1502019-05-22 07:54:20 -07009680 if (res != 0) goto failed;
9681 Py_CLEAR(tmp);
9682 }
9683 *out = TypeIgnore(lineno, tag, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009684 if (*out == NULL) goto failed;
9685 return 0;
9686 }
9687
9688 PyErr_Format(PyExc_TypeError, "expected some sort of type_ignore, but got %R", obj);
9689 failed:
9690 Py_XDECREF(tmp);
9691 return 1;
9692}
9693
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009694
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009695static int
9696astmodule_exec(PyObject *m)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00009697{
Victor Stinner5cf47822020-11-02 22:03:28 +01009698 struct ast_state *state = get_ast_state();
9699 if (state == NULL) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009700 return -1;
Victor Stinner91e1bc12020-07-03 14:15:53 +02009701 }
Victor Stinner74419f02020-07-03 11:35:37 +02009702 if (PyModule_AddObject(m, "AST", state->AST_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009703 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009704 }
Victor Stinner74419f02020-07-03 11:35:37 +02009705 Py_INCREF(state->AST_type);
Brandt Bucherd2f96672020-02-06 06:45:46 -08009706 if (PyModule_AddIntMacro(m, PyCF_ALLOW_TOP_LEVEL_AWAIT) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009707 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009708 }
9709 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009710 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009711 }
9712 if (PyModule_AddIntMacro(m, PyCF_TYPE_COMMENTS) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009713 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009714 }
Victor Stinner74419f02020-07-03 11:35:37 +02009715 if (PyModule_AddObject(m, "mod", state->mod_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009716 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009717 }
Victor Stinner74419f02020-07-03 11:35:37 +02009718 Py_INCREF(state->mod_type);
9719 if (PyModule_AddObject(m, "Module", state->Module_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009720 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009721 }
Victor Stinner74419f02020-07-03 11:35:37 +02009722 Py_INCREF(state->Module_type);
9723 if (PyModule_AddObject(m, "Interactive", state->Interactive_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009724 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009725 }
Victor Stinner74419f02020-07-03 11:35:37 +02009726 Py_INCREF(state->Interactive_type);
9727 if (PyModule_AddObject(m, "Expression", state->Expression_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009728 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009729 }
Victor Stinner74419f02020-07-03 11:35:37 +02009730 Py_INCREF(state->Expression_type);
9731 if (PyModule_AddObject(m, "FunctionType", state->FunctionType_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009732 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009733 }
Victor Stinner74419f02020-07-03 11:35:37 +02009734 Py_INCREF(state->FunctionType_type);
9735 if (PyModule_AddObject(m, "stmt", state->stmt_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009736 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009737 }
Victor Stinner74419f02020-07-03 11:35:37 +02009738 Py_INCREF(state->stmt_type);
9739 if (PyModule_AddObject(m, "FunctionDef", state->FunctionDef_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009740 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009741 }
Victor Stinner74419f02020-07-03 11:35:37 +02009742 Py_INCREF(state->FunctionDef_type);
9743 if (PyModule_AddObject(m, "AsyncFunctionDef", state->AsyncFunctionDef_type)
Brandt Bucherd2f96672020-02-06 06:45:46 -08009744 < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009745 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009746 }
Victor Stinner74419f02020-07-03 11:35:37 +02009747 Py_INCREF(state->AsyncFunctionDef_type);
9748 if (PyModule_AddObject(m, "ClassDef", state->ClassDef_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009749 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009750 }
9751 Py_INCREF(state->ClassDef_type);
9752 if (PyModule_AddObject(m, "Return", state->Return_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009753 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009754 }
9755 Py_INCREF(state->Return_type);
9756 if (PyModule_AddObject(m, "Delete", state->Delete_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009757 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009758 }
9759 Py_INCREF(state->Delete_type);
9760 if (PyModule_AddObject(m, "Assign", state->Assign_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009761 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009762 }
9763 Py_INCREF(state->Assign_type);
9764 if (PyModule_AddObject(m, "AugAssign", state->AugAssign_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009765 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009766 }
9767 Py_INCREF(state->AugAssign_type);
9768 if (PyModule_AddObject(m, "AnnAssign", state->AnnAssign_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009769 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009770 }
9771 Py_INCREF(state->AnnAssign_type);
9772 if (PyModule_AddObject(m, "For", state->For_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009773 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009774 }
9775 Py_INCREF(state->For_type);
9776 if (PyModule_AddObject(m, "AsyncFor", state->AsyncFor_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009777 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009778 }
9779 Py_INCREF(state->AsyncFor_type);
9780 if (PyModule_AddObject(m, "While", state->While_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009781 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009782 }
9783 Py_INCREF(state->While_type);
9784 if (PyModule_AddObject(m, "If", state->If_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009785 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009786 }
9787 Py_INCREF(state->If_type);
9788 if (PyModule_AddObject(m, "With", state->With_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009789 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009790 }
9791 Py_INCREF(state->With_type);
9792 if (PyModule_AddObject(m, "AsyncWith", state->AsyncWith_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009793 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009794 }
9795 Py_INCREF(state->AsyncWith_type);
9796 if (PyModule_AddObject(m, "Raise", state->Raise_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009797 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009798 }
9799 Py_INCREF(state->Raise_type);
9800 if (PyModule_AddObject(m, "Try", state->Try_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009801 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009802 }
9803 Py_INCREF(state->Try_type);
9804 if (PyModule_AddObject(m, "Assert", state->Assert_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009805 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009806 }
9807 Py_INCREF(state->Assert_type);
9808 if (PyModule_AddObject(m, "Import", state->Import_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009809 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009810 }
9811 Py_INCREF(state->Import_type);
9812 if (PyModule_AddObject(m, "ImportFrom", state->ImportFrom_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009813 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009814 }
9815 Py_INCREF(state->ImportFrom_type);
9816 if (PyModule_AddObject(m, "Global", state->Global_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009817 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009818 }
9819 Py_INCREF(state->Global_type);
9820 if (PyModule_AddObject(m, "Nonlocal", state->Nonlocal_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009821 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009822 }
9823 Py_INCREF(state->Nonlocal_type);
9824 if (PyModule_AddObject(m, "Expr", state->Expr_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009825 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009826 }
9827 Py_INCREF(state->Expr_type);
9828 if (PyModule_AddObject(m, "Pass", state->Pass_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009829 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009830 }
9831 Py_INCREF(state->Pass_type);
9832 if (PyModule_AddObject(m, "Break", state->Break_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009833 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009834 }
9835 Py_INCREF(state->Break_type);
9836 if (PyModule_AddObject(m, "Continue", state->Continue_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009837 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009838 }
9839 Py_INCREF(state->Continue_type);
9840 if (PyModule_AddObject(m, "expr", state->expr_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009841 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009842 }
9843 Py_INCREF(state->expr_type);
9844 if (PyModule_AddObject(m, "BoolOp", state->BoolOp_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009845 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009846 }
9847 Py_INCREF(state->BoolOp_type);
9848 if (PyModule_AddObject(m, "NamedExpr", state->NamedExpr_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009849 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009850 }
9851 Py_INCREF(state->NamedExpr_type);
9852 if (PyModule_AddObject(m, "BinOp", state->BinOp_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009853 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009854 }
9855 Py_INCREF(state->BinOp_type);
9856 if (PyModule_AddObject(m, "UnaryOp", state->UnaryOp_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009857 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009858 }
9859 Py_INCREF(state->UnaryOp_type);
9860 if (PyModule_AddObject(m, "Lambda", state->Lambda_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009861 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009862 }
9863 Py_INCREF(state->Lambda_type);
9864 if (PyModule_AddObject(m, "IfExp", state->IfExp_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009865 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009866 }
9867 Py_INCREF(state->IfExp_type);
9868 if (PyModule_AddObject(m, "Dict", state->Dict_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009869 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009870 }
9871 Py_INCREF(state->Dict_type);
9872 if (PyModule_AddObject(m, "Set", state->Set_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009873 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009874 }
9875 Py_INCREF(state->Set_type);
9876 if (PyModule_AddObject(m, "ListComp", state->ListComp_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009877 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009878 }
9879 Py_INCREF(state->ListComp_type);
9880 if (PyModule_AddObject(m, "SetComp", state->SetComp_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009881 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009882 }
9883 Py_INCREF(state->SetComp_type);
9884 if (PyModule_AddObject(m, "DictComp", state->DictComp_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009885 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009886 }
9887 Py_INCREF(state->DictComp_type);
9888 if (PyModule_AddObject(m, "GeneratorExp", state->GeneratorExp_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009889 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009890 }
9891 Py_INCREF(state->GeneratorExp_type);
9892 if (PyModule_AddObject(m, "Await", state->Await_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009893 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009894 }
9895 Py_INCREF(state->Await_type);
9896 if (PyModule_AddObject(m, "Yield", state->Yield_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009897 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009898 }
9899 Py_INCREF(state->Yield_type);
9900 if (PyModule_AddObject(m, "YieldFrom", state->YieldFrom_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009901 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009902 }
9903 Py_INCREF(state->YieldFrom_type);
9904 if (PyModule_AddObject(m, "Compare", state->Compare_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009905 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009906 }
9907 Py_INCREF(state->Compare_type);
9908 if (PyModule_AddObject(m, "Call", state->Call_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009909 return -1;
Victor Stinner74419f02020-07-03 11:35:37 +02009910 }
9911 Py_INCREF(state->Call_type);
9912 if (PyModule_AddObject(m, "FormattedValue", state->FormattedValue_type) <
Brandt Bucherd2f96672020-02-06 06:45:46 -08009913 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009914 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009915 }
Victor Stinner74419f02020-07-03 11:35:37 +02009916 Py_INCREF(state->FormattedValue_type);
9917 if (PyModule_AddObject(m, "JoinedStr", state->JoinedStr_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009918 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009919 }
Victor Stinner74419f02020-07-03 11:35:37 +02009920 Py_INCREF(state->JoinedStr_type);
9921 if (PyModule_AddObject(m, "Constant", state->Constant_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009922 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009923 }
Victor Stinner74419f02020-07-03 11:35:37 +02009924 Py_INCREF(state->Constant_type);
9925 if (PyModule_AddObject(m, "Attribute", state->Attribute_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009926 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009927 }
Victor Stinner74419f02020-07-03 11:35:37 +02009928 Py_INCREF(state->Attribute_type);
9929 if (PyModule_AddObject(m, "Subscript", state->Subscript_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009930 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009931 }
Victor Stinner74419f02020-07-03 11:35:37 +02009932 Py_INCREF(state->Subscript_type);
9933 if (PyModule_AddObject(m, "Starred", state->Starred_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009934 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009935 }
Victor Stinner74419f02020-07-03 11:35:37 +02009936 Py_INCREF(state->Starred_type);
9937 if (PyModule_AddObject(m, "Name", state->Name_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009938 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009939 }
Victor Stinner74419f02020-07-03 11:35:37 +02009940 Py_INCREF(state->Name_type);
9941 if (PyModule_AddObject(m, "List", state->List_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009942 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009943 }
Victor Stinner74419f02020-07-03 11:35:37 +02009944 Py_INCREF(state->List_type);
9945 if (PyModule_AddObject(m, "Tuple", state->Tuple_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009946 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009947 }
Victor Stinner74419f02020-07-03 11:35:37 +02009948 Py_INCREF(state->Tuple_type);
9949 if (PyModule_AddObject(m, "Slice", state->Slice_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009950 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009951 }
Victor Stinner74419f02020-07-03 11:35:37 +02009952 Py_INCREF(state->Slice_type);
9953 if (PyModule_AddObject(m, "expr_context", state->expr_context_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009954 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009955 }
Victor Stinner74419f02020-07-03 11:35:37 +02009956 Py_INCREF(state->expr_context_type);
9957 if (PyModule_AddObject(m, "Load", state->Load_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009958 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009959 }
Victor Stinner74419f02020-07-03 11:35:37 +02009960 Py_INCREF(state->Load_type);
9961 if (PyModule_AddObject(m, "Store", state->Store_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009962 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009963 }
Victor Stinner74419f02020-07-03 11:35:37 +02009964 Py_INCREF(state->Store_type);
9965 if (PyModule_AddObject(m, "Del", state->Del_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009966 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009967 }
Victor Stinner74419f02020-07-03 11:35:37 +02009968 Py_INCREF(state->Del_type);
9969 if (PyModule_AddObject(m, "boolop", state->boolop_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009970 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009971 }
Victor Stinner74419f02020-07-03 11:35:37 +02009972 Py_INCREF(state->boolop_type);
9973 if (PyModule_AddObject(m, "And", state->And_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009974 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009975 }
Victor Stinner74419f02020-07-03 11:35:37 +02009976 Py_INCREF(state->And_type);
9977 if (PyModule_AddObject(m, "Or", state->Or_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009978 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009979 }
Victor Stinner74419f02020-07-03 11:35:37 +02009980 Py_INCREF(state->Or_type);
9981 if (PyModule_AddObject(m, "operator", state->operator_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009982 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009983 }
Victor Stinner74419f02020-07-03 11:35:37 +02009984 Py_INCREF(state->operator_type);
9985 if (PyModule_AddObject(m, "Add", state->Add_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009986 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009987 }
Victor Stinner74419f02020-07-03 11:35:37 +02009988 Py_INCREF(state->Add_type);
9989 if (PyModule_AddObject(m, "Sub", state->Sub_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009990 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009991 }
Victor Stinner74419f02020-07-03 11:35:37 +02009992 Py_INCREF(state->Sub_type);
9993 if (PyModule_AddObject(m, "Mult", state->Mult_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009994 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009995 }
Victor Stinner74419f02020-07-03 11:35:37 +02009996 Py_INCREF(state->Mult_type);
9997 if (PyModule_AddObject(m, "MatMult", state->MatMult_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +02009998 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -08009999 }
Victor Stinner74419f02020-07-03 11:35:37 +020010000 Py_INCREF(state->MatMult_type);
10001 if (PyModule_AddObject(m, "Div", state->Div_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010002 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010003 }
Victor Stinner74419f02020-07-03 11:35:37 +020010004 Py_INCREF(state->Div_type);
10005 if (PyModule_AddObject(m, "Mod", state->Mod_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010006 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010007 }
Victor Stinner74419f02020-07-03 11:35:37 +020010008 Py_INCREF(state->Mod_type);
10009 if (PyModule_AddObject(m, "Pow", state->Pow_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010010 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010011 }
Victor Stinner74419f02020-07-03 11:35:37 +020010012 Py_INCREF(state->Pow_type);
10013 if (PyModule_AddObject(m, "LShift", state->LShift_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010014 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010015 }
Victor Stinner74419f02020-07-03 11:35:37 +020010016 Py_INCREF(state->LShift_type);
10017 if (PyModule_AddObject(m, "RShift", state->RShift_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010018 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010019 }
Victor Stinner74419f02020-07-03 11:35:37 +020010020 Py_INCREF(state->RShift_type);
10021 if (PyModule_AddObject(m, "BitOr", state->BitOr_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010022 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010023 }
Victor Stinner74419f02020-07-03 11:35:37 +020010024 Py_INCREF(state->BitOr_type);
10025 if (PyModule_AddObject(m, "BitXor", state->BitXor_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010026 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010027 }
Victor Stinner74419f02020-07-03 11:35:37 +020010028 Py_INCREF(state->BitXor_type);
10029 if (PyModule_AddObject(m, "BitAnd", state->BitAnd_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010030 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010031 }
Victor Stinner74419f02020-07-03 11:35:37 +020010032 Py_INCREF(state->BitAnd_type);
10033 if (PyModule_AddObject(m, "FloorDiv", state->FloorDiv_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010034 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010035 }
Victor Stinner74419f02020-07-03 11:35:37 +020010036 Py_INCREF(state->FloorDiv_type);
10037 if (PyModule_AddObject(m, "unaryop", state->unaryop_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010038 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010039 }
Victor Stinner74419f02020-07-03 11:35:37 +020010040 Py_INCREF(state->unaryop_type);
10041 if (PyModule_AddObject(m, "Invert", state->Invert_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010042 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010043 }
Victor Stinner74419f02020-07-03 11:35:37 +020010044 Py_INCREF(state->Invert_type);
10045 if (PyModule_AddObject(m, "Not", state->Not_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010046 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010047 }
Victor Stinner74419f02020-07-03 11:35:37 +020010048 Py_INCREF(state->Not_type);
10049 if (PyModule_AddObject(m, "UAdd", state->UAdd_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010050 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010051 }
Victor Stinner74419f02020-07-03 11:35:37 +020010052 Py_INCREF(state->UAdd_type);
10053 if (PyModule_AddObject(m, "USub", state->USub_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010054 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010055 }
Victor Stinner74419f02020-07-03 11:35:37 +020010056 Py_INCREF(state->USub_type);
10057 if (PyModule_AddObject(m, "cmpop", state->cmpop_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010058 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010059 }
Victor Stinner74419f02020-07-03 11:35:37 +020010060 Py_INCREF(state->cmpop_type);
10061 if (PyModule_AddObject(m, "Eq", state->Eq_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010062 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010063 }
Victor Stinner74419f02020-07-03 11:35:37 +020010064 Py_INCREF(state->Eq_type);
10065 if (PyModule_AddObject(m, "NotEq", state->NotEq_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010066 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010067 }
Victor Stinner74419f02020-07-03 11:35:37 +020010068 Py_INCREF(state->NotEq_type);
10069 if (PyModule_AddObject(m, "Lt", state->Lt_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010070 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010071 }
Victor Stinner74419f02020-07-03 11:35:37 +020010072 Py_INCREF(state->Lt_type);
10073 if (PyModule_AddObject(m, "LtE", state->LtE_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010074 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010075 }
Victor Stinner74419f02020-07-03 11:35:37 +020010076 Py_INCREF(state->LtE_type);
10077 if (PyModule_AddObject(m, "Gt", state->Gt_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010078 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010079 }
Victor Stinner74419f02020-07-03 11:35:37 +020010080 Py_INCREF(state->Gt_type);
10081 if (PyModule_AddObject(m, "GtE", state->GtE_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010082 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010083 }
Victor Stinner74419f02020-07-03 11:35:37 +020010084 Py_INCREF(state->GtE_type);
10085 if (PyModule_AddObject(m, "Is", state->Is_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010086 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010087 }
Victor Stinner74419f02020-07-03 11:35:37 +020010088 Py_INCREF(state->Is_type);
10089 if (PyModule_AddObject(m, "IsNot", state->IsNot_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010090 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010091 }
Victor Stinner74419f02020-07-03 11:35:37 +020010092 Py_INCREF(state->IsNot_type);
10093 if (PyModule_AddObject(m, "In", state->In_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010094 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010095 }
Victor Stinner74419f02020-07-03 11:35:37 +020010096 Py_INCREF(state->In_type);
10097 if (PyModule_AddObject(m, "NotIn", state->NotIn_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010098 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010099 }
Victor Stinner74419f02020-07-03 11:35:37 +020010100 Py_INCREF(state->NotIn_type);
10101 if (PyModule_AddObject(m, "comprehension", state->comprehension_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010102 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010103 }
Victor Stinner74419f02020-07-03 11:35:37 +020010104 Py_INCREF(state->comprehension_type);
10105 if (PyModule_AddObject(m, "excepthandler", state->excepthandler_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010106 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010107 }
Victor Stinner74419f02020-07-03 11:35:37 +020010108 Py_INCREF(state->excepthandler_type);
10109 if (PyModule_AddObject(m, "ExceptHandler", state->ExceptHandler_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010110 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010111 }
Victor Stinner74419f02020-07-03 11:35:37 +020010112 Py_INCREF(state->ExceptHandler_type);
10113 if (PyModule_AddObject(m, "arguments", state->arguments_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010114 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010115 }
Victor Stinner74419f02020-07-03 11:35:37 +020010116 Py_INCREF(state->arguments_type);
10117 if (PyModule_AddObject(m, "arg", state->arg_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010118 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010119 }
Victor Stinner74419f02020-07-03 11:35:37 +020010120 Py_INCREF(state->arg_type);
10121 if (PyModule_AddObject(m, "keyword", state->keyword_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010122 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010123 }
Victor Stinner74419f02020-07-03 11:35:37 +020010124 Py_INCREF(state->keyword_type);
10125 if (PyModule_AddObject(m, "alias", state->alias_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010126 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010127 }
Victor Stinner74419f02020-07-03 11:35:37 +020010128 Py_INCREF(state->alias_type);
10129 if (PyModule_AddObject(m, "withitem", state->withitem_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010130 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010131 }
Victor Stinner74419f02020-07-03 11:35:37 +020010132 Py_INCREF(state->withitem_type);
10133 if (PyModule_AddObject(m, "type_ignore", state->type_ignore_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010134 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010135 }
Victor Stinner74419f02020-07-03 11:35:37 +020010136 Py_INCREF(state->type_ignore_type);
10137 if (PyModule_AddObject(m, "TypeIgnore", state->TypeIgnore_type) < 0) {
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010138 return -1;
Brandt Bucherd2f96672020-02-06 06:45:46 -080010139 }
Victor Stinner74419f02020-07-03 11:35:37 +020010140 Py_INCREF(state->TypeIgnore_type);
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010141 return 0;
10142}
Victor Stinner91e1bc12020-07-03 14:15:53 +020010143
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010144static PyModuleDef_Slot astmodule_slots[] = {
10145 {Py_mod_exec, astmodule_exec},
10146 {0, NULL}
10147};
10148
10149static struct PyModuleDef _astmodule = {
10150 PyModuleDef_HEAD_INIT,
10151 .m_name = "_ast",
Victor Stinner5cf47822020-11-02 22:03:28 +010010152 // The _ast module uses a per-interpreter state (PyInterpreterState.ast)
Victor Stinnere5fbe0c2020-09-15 18:03:34 +020010153 .m_size = 0,
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010154 .m_slots = astmodule_slots,
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010155};
10156
10157PyMODINIT_FUNC
10158PyInit__ast(void)
10159{
10160 return PyModuleDef_Init(&_astmodule);
Martin v. Löwis577b5b92006-02-27 15:23:19 +000010161}
10162
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010163
Martin v. Löwisbd260da2006-02-26 19:42:26 +000010164PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010165{
Victor Stinner5cf47822020-11-02 22:03:28 +010010166 struct ast_state *state = get_ast_state();
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010167 if (state == NULL) {
Victor Stinnerbdf630c2013-07-17 00:17:15 +020010168 return NULL;
Victor Stinner91e1bc12020-07-03 14:15:53 +020010169 }
Victor Stinner74419f02020-07-03 11:35:37 +020010170 return ast2obj_mod(state, t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010171}
Martin v. Löwis5b222132007-06-10 09:51:05 +000010172
Neal Norwitzdb4115f2008-03-31 04:20:05 +000010173/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
10174mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +000010175{
Serhiy Storchaka43c97312019-09-10 13:02:30 +030010176 const char * const req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +000010177 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -050010178
Steve Dowerb82e17e2019-05-23 08:45:22 -070010179 if (PySys_Audit("compile", "OO", ast, Py_None) < 0) {
10180 return NULL;
10181 }
10182
Victor Stinner5cf47822020-11-02 22:03:28 +010010183 struct ast_state *state = get_ast_state();
10184 if (state == NULL) {
10185 return NULL;
10186 }
10187
Victor Stinner74419f02020-07-03 11:35:37 +020010188 PyObject *req_type[3];
10189 req_type[0] = state->Module_type;
10190 req_type[1] = state->Expression_type;
10191 req_type[2] = state->Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -050010192
Guido van Rossum3a32e3b2019-02-01 11:37:34 -080010193 assert(0 <= mode && mode <= 2);
Neal Norwitzdb4115f2008-03-31 04:20:05 +000010194
Benjamin Peterson0496c9e2009-12-13 01:24:58 +000010195 isinstance = PyObject_IsInstance(ast, req_type[mode]);
10196 if (isinstance == -1)
10197 return NULL;
10198 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +000010199 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
Dino Viehlandac46eb42019-09-11 10:16:34 -070010200 req_name[mode], _PyType_Name(Py_TYPE(ast)));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +000010201 return NULL;
10202 }
Dong-hee Naa05fcd32019-10-10 16:41:26 +090010203
10204 mod_ty res = NULL;
Victor Stinner74419f02020-07-03 11:35:37 +020010205 if (obj2ast_mod(state, ast, &res, arena) != 0)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +000010206 return NULL;
10207 else
10208 return res;
10209}
10210
10211int PyAST_Check(PyObject* obj)
10212{
Victor Stinner5cf47822020-11-02 22:03:28 +010010213 struct ast_state *state = get_ast_state();
Victor Stinnerb1cc6ba2020-07-03 20:01:46 +020010214 if (state == NULL) {
Victor Stinnerbdf630c2013-07-17 00:17:15 +020010215 return -1;
Victor Stinner91e1bc12020-07-03 14:15:53 +020010216 }
Victor Stinner74419f02020-07-03 11:35:37 +020010217 return PyObject_IsInstance(obj, state->AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +000010218}
10219
Martin v. Löwis5b222132007-06-10 09:51:05 +000010220