blob: 725ce31fe3c037cd887abb95ad9eeaff5e3e9a73 [file] [log] [blame]
Guido van Rossum95e4d582018-01-26 08:20:18 -08001#include <stdbool.h>
2#include "Python.h"
3#include "Python-ast.h"
4
5static PyObject *_str_open_br;
6static PyObject *_str_dbl_open_br;
7static PyObject *_str_close_br;
8static PyObject *_str_dbl_close_br;
9
10/* Forward declarations for recursion via helper functions. */
11static PyObject *
Serhiy Storchaka64fddc42018-05-17 06:17:48 +030012expr_as_unicode(expr_ty e, int level);
Guido van Rossum95e4d582018-01-26 08:20:18 -080013static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +030014append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level);
Guido van Rossum95e4d582018-01-26 08:20:18 -080015static int
16append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec);
17static int
18append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec);
Serhiy Storchaka64fddc42018-05-17 06:17:48 +030019static int
20append_ast_slice(_PyUnicodeWriter *writer, slice_ty slice);
Guido van Rossum95e4d582018-01-26 08:20:18 -080021
22static int
23append_charp(_PyUnicodeWriter *writer, const char *charp)
24{
Stéphane Wirtel83ab9952018-02-01 17:59:27 +010025 return _PyUnicodeWriter_WriteASCIIString(writer, charp, -1);
Guido van Rossum95e4d582018-01-26 08:20:18 -080026}
27
Serhiy Storchaka64fddc42018-05-17 06:17:48 +030028#define APPEND_STR_FINISH(str) do { \
29 return append_charp(writer, (str)); \
30 } while (0)
31
32#define APPEND_STR(str) do { \
33 if (-1 == append_charp(writer, (str))) { \
34 return -1; \
35 } \
36 } while (0)
37
38#define APPEND_STR_IF(cond, str) do { \
39 if ((cond) && -1 == append_charp(writer, (str))) { \
40 return -1; \
41 } \
42 } while (0)
43
44#define APPEND_STR_IF_NOT_FIRST(str) do { \
45 APPEND_STR_IF(!first, (str)); \
46 first = false; \
47 } while (0)
48
49#define APPEND_EXPR(expr, pr) do { \
50 if (-1 == append_ast_expr(writer, (expr), (pr))) { \
51 return -1; \
52 } \
53 } while (0)
54
55#define APPEND(type, value) do { \
56 if (-1 == append_ast_ ## type(writer, (value))) { \
57 return -1; \
58 } \
59 } while (0)
60
Guido van Rossum95e4d582018-01-26 08:20:18 -080061static int
62append_repr(_PyUnicodeWriter *writer, PyObject *obj)
63{
64 int ret;
65 PyObject *repr;
66 repr = PyObject_Repr(obj);
67 if (!repr) {
68 return -1;
69 }
70 ret = _PyUnicodeWriter_WriteStr(writer, repr);
71 Py_DECREF(repr);
72 return ret;
73}
74
Serhiy Storchaka64fddc42018-05-17 06:17:48 +030075/* Priority levels */
76
77enum {
78 PR_TUPLE,
79 PR_TEST, /* 'if'-'else', 'lambda' */
80 PR_OR, /* 'or' */
81 PR_AND, /* 'and' */
82 PR_NOT, /* 'not' */
83 PR_CMP, /* '<', '>', '==', '>=', '<=', '!=',
84 'in', 'not in', 'is', 'is not' */
85 PR_EXPR,
86 PR_BOR = PR_EXPR, /* '|' */
87 PR_BXOR, /* '^' */
88 PR_BAND, /* '&' */
89 PR_SHIFT, /* '<<', '>>' */
90 PR_ARITH, /* '+', '-' */
91 PR_TERM, /* '*', '@', '/', '%', '//' */
92 PR_FACTOR, /* unary '+', '-', '~' */
93 PR_POWER, /* '**' */
94 PR_AWAIT, /* 'await' */
95 PR_ATOM,
96};
97
Guido van Rossum95e4d582018-01-26 08:20:18 -080098static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +030099append_ast_boolop(_PyUnicodeWriter *writer, expr_ty e, int level)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800100{
101 Py_ssize_t i, value_count;
102 asdl_seq *values;
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300103 const char *op = (e->v.BoolOp.op == And) ? " and " : " or ";
104 int pr = (e->v.BoolOp.op == And) ? PR_AND : PR_OR;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800105
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300106 APPEND_STR_IF(level > pr, "(");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800107
108 values = e->v.BoolOp.values;
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300109 value_count = asdl_seq_LEN(values);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800110
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300111 for (i = 0; i < value_count; ++i) {
112 APPEND_STR_IF(i > 0, op);
113 APPEND_EXPR((expr_ty)asdl_seq_GET(values, i), pr + 1);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800114 }
115
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300116 APPEND_STR_IF(level > pr, ")");
117 return 0;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800118}
119
120static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300121append_ast_binop(_PyUnicodeWriter *writer, expr_ty e, int level)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800122{
123 const char *op;
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300124 int pr;
125 bool rassoc = false; /* is right-associative? */
Guido van Rossum95e4d582018-01-26 08:20:18 -0800126
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300127 switch (e->v.BinOp.op) {
128 case Add: op = " + "; pr = PR_ARITH; break;
129 case Sub: op = " - "; pr = PR_ARITH; break;
130 case Mult: op = " * "; pr = PR_TERM; break;
131 case MatMult: op = " @ "; pr = PR_TERM; break;
132 case Div: op = " / "; pr = PR_TERM; break;
133 case Mod: op = " % "; pr = PR_TERM; break;
134 case LShift: op = " << "; pr = PR_SHIFT; break;
135 case RShift: op = " >> "; pr = PR_SHIFT; break;
136 case BitOr: op = " | "; pr = PR_BOR; break;
137 case BitXor: op = " ^ "; pr = PR_BXOR; break;
138 case BitAnd: op = " & "; pr = PR_BAND; break;
139 case FloorDiv: op = " // "; pr = PR_TERM; break;
140 case Pow: op = " ** "; pr = PR_POWER; rassoc = true; break;
Stéphane Wirtel83ab9952018-02-01 17:59:27 +0100141 default:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300142 PyErr_SetString(PyExc_SystemError,
143 "unknown binary operator");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800144 return -1;
145 }
146
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300147 APPEND_STR_IF(level > pr, "(");
148 APPEND_EXPR(e->v.BinOp.left, pr + rassoc);
149 APPEND_STR(op);
150 APPEND_EXPR(e->v.BinOp.right, pr + !rassoc);
151 APPEND_STR_IF(level > pr, ")");
152 return 0;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800153}
154
155static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300156append_ast_unaryop(_PyUnicodeWriter *writer, expr_ty e, int level)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800157{
158 const char *op;
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300159 int pr;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800160
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300161 switch (e->v.UnaryOp.op) {
162 case Invert: op = "~"; pr = PR_FACTOR; break;
163 case Not: op = "not "; pr = PR_NOT; break;
164 case UAdd: op = "+"; pr = PR_FACTOR; break;
165 case USub: op = "-"; pr = PR_FACTOR; break;
Stéphane Wirtel83ab9952018-02-01 17:59:27 +0100166 default:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300167 PyErr_SetString(PyExc_SystemError,
168 "unknown unary operator");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800169 return -1;
170 }
171
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300172 APPEND_STR_IF(level > pr, "(");
173 APPEND_STR(op);
174 APPEND_EXPR(e->v.UnaryOp.operand, pr);
175 APPEND_STR_IF(level > pr, ")");
176 return 0;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800177}
178
179static int
180append_ast_arg(_PyUnicodeWriter *writer, arg_ty arg)
181{
182 if (-1 == _PyUnicodeWriter_WriteStr(writer, arg->arg)) {
183 return -1;
184 }
185 if (arg->annotation) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300186 APPEND_STR(": ");
187 APPEND_EXPR(arg->annotation, PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800188 }
189 return 0;
190}
191
192static int
193append_ast_args(_PyUnicodeWriter *writer, arguments_ty args)
194{
195 bool first;
196 Py_ssize_t i, di, arg_count, default_count;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800197
198 first = true;
199
200 /* positional arguments with defaults */
201 arg_count = asdl_seq_LEN(args->args);
202 default_count = asdl_seq_LEN(args->defaults);
203 for (i = 0; i < arg_count; i++) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300204 APPEND_STR_IF_NOT_FIRST(", ");
205 APPEND(arg, (arg_ty)asdl_seq_GET(args->args, i));
Guido van Rossum95e4d582018-01-26 08:20:18 -0800206
207 di = i - arg_count + default_count;
208 if (di >= 0) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300209 APPEND_STR("=");
210 APPEND_EXPR((expr_ty)asdl_seq_GET(args->defaults, di), PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800211 }
212 }
213
214 /* vararg, or bare '*' if no varargs but keyword-only arguments present */
215 if (args->vararg || args->kwonlyargs) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300216 APPEND_STR_IF_NOT_FIRST(", ");
217 APPEND_STR("*");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800218 if (args->vararg) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300219 APPEND(arg, args->vararg);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800220 }
221 }
222
223 /* keyword-only arguments */
224 arg_count = asdl_seq_LEN(args->kwonlyargs);
225 default_count = asdl_seq_LEN(args->kw_defaults);
226 for (i = 0; i < arg_count; i++) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300227 APPEND_STR_IF_NOT_FIRST(", ");
228 APPEND(arg, (arg_ty)asdl_seq_GET(args->kwonlyargs, i));
Guido van Rossum95e4d582018-01-26 08:20:18 -0800229
230 di = i - arg_count + default_count;
231 if (di >= 0) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300232 APPEND_STR("=");
233 APPEND_EXPR((expr_ty)asdl_seq_GET(args->kw_defaults, di), PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800234 }
235 }
236
237 /* **kwargs */
238 if (args->kwarg) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300239 APPEND_STR_IF_NOT_FIRST(", ");
240 APPEND_STR("**");
241 APPEND(arg, args->kwarg);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800242 }
243
244 return 0;
245}
246
247static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300248append_ast_lambda(_PyUnicodeWriter *writer, expr_ty e, int level)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800249{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300250 APPEND_STR_IF(level > PR_TEST, "(");
251 APPEND_STR("lambda ");
252 APPEND(args, e->v.Lambda.args);
253 APPEND_STR(": ");
254 APPEND_EXPR(e->v.Lambda.body, PR_TEST);
255 APPEND_STR_IF(level > PR_TEST, ")");
256 return 0;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800257}
258
259static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300260append_ast_ifexp(_PyUnicodeWriter *writer, expr_ty e, int level)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800261{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300262 APPEND_STR_IF(level > PR_TEST, "(");
263 APPEND_EXPR(e->v.IfExp.body, PR_TEST + 1);
264 APPEND_STR(" if ");
265 APPEND_EXPR(e->v.IfExp.test, PR_TEST + 1);
266 APPEND_STR(" else ");
267 APPEND_EXPR(e->v.IfExp.orelse, PR_TEST);
268 APPEND_STR_IF(level > PR_TEST, ")");
269 return 0;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800270}
271
272static int
273append_ast_dict(_PyUnicodeWriter *writer, expr_ty e)
274{
275 Py_ssize_t i, value_count;
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300276 expr_ty key_node;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800277
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300278 APPEND_STR("{");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800279 value_count = asdl_seq_LEN(e->v.Dict.values);
280
281 for (i = 0; i < value_count; i++) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300282 APPEND_STR_IF(i > 0, ", ");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800283 key_node = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i);
284 if (key_node != NULL) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300285 APPEND_EXPR(key_node, PR_TEST);
286 APPEND_STR(": ");
287 APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Dict.values, i), PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800288 }
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300289 else {
290 APPEND_STR("**");
291 APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Dict.values, i), PR_EXPR);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800292 }
293 }
294
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300295 APPEND_STR_FINISH("}");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800296}
297
298static int
299append_ast_set(_PyUnicodeWriter *writer, expr_ty e)
300{
301 Py_ssize_t i, elem_count;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800302
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300303 APPEND_STR("{");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800304 elem_count = asdl_seq_LEN(e->v.Set.elts);
305 for (i = 0; i < elem_count; i++) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300306 APPEND_STR_IF(i > 0, ", ");
307 APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Set.elts, i), PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800308 }
309
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300310 APPEND_STR_FINISH("}");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800311}
312
313static int
314append_ast_list(_PyUnicodeWriter *writer, expr_ty e)
315{
316 Py_ssize_t i, elem_count;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800317
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300318 APPEND_STR("[");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800319 elem_count = asdl_seq_LEN(e->v.List.elts);
320 for (i = 0; i < elem_count; i++) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300321 APPEND_STR_IF(i > 0, ", ");
322 APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.List.elts, i), PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800323 }
324
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300325 APPEND_STR_FINISH("]");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800326}
327
328static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300329append_ast_tuple(_PyUnicodeWriter *writer, expr_ty e, int level)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800330{
331 Py_ssize_t i, elem_count;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800332
333 elem_count = asdl_seq_LEN(e->v.Tuple.elts);
334
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300335 if (elem_count == 0) {
336 APPEND_STR_FINISH("()");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800337 }
338
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300339 APPEND_STR_IF(level > PR_TUPLE, "(");
340
Guido van Rossum95e4d582018-01-26 08:20:18 -0800341 for (i = 0; i < elem_count; i++) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300342 APPEND_STR_IF(i > 0, ", ");
343 APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Tuple.elts, i), PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800344 }
345
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300346 APPEND_STR_IF(elem_count == 1, ",");
347 APPEND_STR_IF(level > PR_TUPLE, ")");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800348 return 0;
349}
350
351static int
352append_ast_comprehension(_PyUnicodeWriter *writer, comprehension_ty gen)
353{
354 Py_ssize_t i, if_count;
355
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300356 APPEND_STR(gen->is_async ? " async for " : " for ");
357 APPEND_EXPR(gen->target, PR_TUPLE);
358 APPEND_STR(" in ");
359 APPEND_EXPR(gen->iter, PR_TEST + 1);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800360
361 if_count = asdl_seq_LEN(gen->ifs);
362 for (i = 0; i < if_count; i++) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300363 APPEND_STR(" if ");
364 APPEND_EXPR((expr_ty)asdl_seq_GET(gen->ifs, i), PR_TEST + 1);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800365 }
366 return 0;
367}
368
369static int
370append_ast_comprehensions(_PyUnicodeWriter *writer, asdl_seq *comprehensions)
371{
372 Py_ssize_t i, gen_count;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800373 gen_count = asdl_seq_LEN(comprehensions);
374
375 for (i = 0; i < gen_count; i++) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300376 APPEND(comprehension, (comprehension_ty)asdl_seq_GET(comprehensions, i));
Guido van Rossum95e4d582018-01-26 08:20:18 -0800377 }
378
379 return 0;
380}
381
382static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300383append_ast_genexp(_PyUnicodeWriter *writer, expr_ty e)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800384{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300385 APPEND_STR("(");
386 APPEND_EXPR(e->v.GeneratorExp.elt, PR_TEST);
387 APPEND(comprehensions, e->v.GeneratorExp.generators);
388 APPEND_STR_FINISH(")");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800389}
390
391static int
392append_ast_listcomp(_PyUnicodeWriter *writer, expr_ty e)
393{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300394 APPEND_STR("[");
395 APPEND_EXPR(e->v.ListComp.elt, PR_TEST);
396 APPEND(comprehensions, e->v.ListComp.generators);
397 APPEND_STR_FINISH("]");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800398}
399
400static int
401append_ast_setcomp(_PyUnicodeWriter *writer, expr_ty e)
402{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300403 APPEND_STR("{");
404 APPEND_EXPR(e->v.SetComp.elt, PR_TEST);
405 APPEND(comprehensions, e->v.SetComp.generators);
406 APPEND_STR_FINISH("}");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800407}
408
409static int
410append_ast_dictcomp(_PyUnicodeWriter *writer, expr_ty e)
411{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300412 APPEND_STR("{");
413 APPEND_EXPR(e->v.DictComp.key, PR_TEST);
414 APPEND_STR(": ");
415 APPEND_EXPR(e->v.DictComp.value, PR_TEST);
416 APPEND(comprehensions, e->v.DictComp.generators);
417 APPEND_STR_FINISH("}");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800418}
419
420static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300421append_ast_compare(_PyUnicodeWriter *writer, expr_ty e, int level)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800422{
423 const char *op;
424 Py_ssize_t i, comparator_count;
425 asdl_seq *comparators;
426 asdl_int_seq *ops;
427
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300428 APPEND_STR_IF(level > PR_CMP, "(");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800429
430 comparators = e->v.Compare.comparators;
431 ops = e->v.Compare.ops;
432 comparator_count = asdl_seq_LEN(comparators);
433 assert(comparator_count > 0);
434 assert(comparator_count == asdl_seq_LEN(ops));
435
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300436 APPEND_EXPR(e->v.Compare.left, PR_CMP + 1);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800437
438 for (i = 0; i < comparator_count; i++) {
439 switch ((cmpop_ty)asdl_seq_GET(ops, i)) {
440 case Eq:
441 op = " == ";
442 break;
443 case NotEq:
444 op = " != ";
445 break;
446 case Lt:
447 op = " < ";
448 break;
449 case LtE:
450 op = " <= ";
451 break;
452 case Gt:
453 op = " > ";
454 break;
455 case GtE:
456 op = " >= ";
457 break;
458 case Is:
459 op = " is ";
460 break;
461 case IsNot:
462 op = " is not ";
463 break;
464 case In:
465 op = " in ";
466 break;
467 case NotIn:
468 op = " not in ";
469 break;
470 default:
471 PyErr_SetString(PyExc_SystemError,
472 "unexpected comparison kind");
473 return -1;
474 }
475
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300476 APPEND_STR(op);
477 APPEND_EXPR((expr_ty)asdl_seq_GET(comparators, i), PR_CMP + 1);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800478 }
479
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300480 APPEND_STR_IF(level > PR_CMP, ")");
481 return 0;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800482}
483
484static int
485append_ast_keyword(_PyUnicodeWriter *writer, keyword_ty kw)
486{
487 if (kw->arg == NULL) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300488 APPEND_STR("**");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800489 }
490 else {
491 if (-1 == _PyUnicodeWriter_WriteStr(writer, kw->arg)) {
492 return -1;
493 }
494
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300495 APPEND_STR("=");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800496 }
497
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300498 APPEND_EXPR(kw->value, PR_TEST);
499 return 0;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800500}
501
502static int
503append_ast_call(_PyUnicodeWriter *writer, expr_ty e)
504{
505 bool first;
506 Py_ssize_t i, arg_count, kw_count;
507 expr_ty expr;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800508
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300509 APPEND_EXPR(e->v.Call.func, PR_ATOM);
510
511 arg_count = asdl_seq_LEN(e->v.Call.args);
512 kw_count = asdl_seq_LEN(e->v.Call.keywords);
513 if (arg_count == 1 && kw_count == 0) {
514 expr = (expr_ty)asdl_seq_GET(e->v.Call.args, 0);
515 if (expr->kind == GeneratorExp_kind) {
516 /* Special case: a single generator expression. */
517 return append_ast_genexp(writer, expr);
518 }
Guido van Rossum95e4d582018-01-26 08:20:18 -0800519 }
520
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300521 APPEND_STR("(");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800522
523 first = true;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800524 for (i = 0; i < arg_count; i++) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300525 APPEND_STR_IF_NOT_FIRST(", ");
526 APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Call.args, i), PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800527 }
528
Guido van Rossum95e4d582018-01-26 08:20:18 -0800529 for (i = 0; i < kw_count; i++) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300530 APPEND_STR_IF_NOT_FIRST(", ");
531 APPEND(keyword, (keyword_ty)asdl_seq_GET(e->v.Call.keywords, i));
Guido van Rossum95e4d582018-01-26 08:20:18 -0800532 }
533
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300534 APPEND_STR_FINISH(")");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800535}
536
537static PyObject *
538escape_braces(PyObject *orig)
539{
540 PyObject *temp;
541 PyObject *result;
542 temp = PyUnicode_Replace(orig, _str_open_br, _str_dbl_open_br, -1);
543 if (!temp) {
544 return NULL;
545 }
546 result = PyUnicode_Replace(temp, _str_close_br, _str_dbl_close_br, -1);
547 Py_DECREF(temp);
548 return result;
549}
550
551static int
552append_fstring_unicode(_PyUnicodeWriter *writer, PyObject *unicode)
553{
554 PyObject *escaped;
555 int result = -1;
556 escaped = escape_braces(unicode);
557 if (escaped) {
558 result = _PyUnicodeWriter_WriteStr(writer, escaped);
559 Py_DECREF(escaped);
560 }
561 return result;
562}
563
564static int
565append_fstring_element(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
566{
567 switch (e->kind) {
568 case Constant_kind:
569 return append_fstring_unicode(writer, e->v.Constant.value);
570 case Str_kind:
571 return append_fstring_unicode(writer, e->v.Str.s);
572 case JoinedStr_kind:
573 return append_joinedstr(writer, e, is_format_spec);
574 case FormattedValue_kind:
575 return append_formattedvalue(writer, e, is_format_spec);
576 default:
577 PyErr_SetString(PyExc_SystemError,
578 "unknown expression kind inside f-string");
579 return -1;
580 }
581}
582
583/* Build body separately to enable wrapping the entire stream of Strs,
584 Constants and FormattedValues in one opening and one closing quote. */
585static PyObject *
586build_fstring_body(asdl_seq *values, bool is_format_spec)
587{
588 Py_ssize_t i, value_count;
589 _PyUnicodeWriter body_writer;
590 _PyUnicodeWriter_Init(&body_writer);
591 body_writer.min_length = 256;
592 body_writer.overallocate = 1;
593
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300594 value_count = asdl_seq_LEN(values);
595 for (i = 0; i < value_count; ++i) {
Guido van Rossum95e4d582018-01-26 08:20:18 -0800596 if (-1 == append_fstring_element(&body_writer,
597 (expr_ty)asdl_seq_GET(values, i),
598 is_format_spec
599 )) {
600 _PyUnicodeWriter_Dealloc(&body_writer);
601 return NULL;
602 }
603 }
604
605 return _PyUnicodeWriter_Finish(&body_writer);
606}
607
608static int
609append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
610{
611 int result = -1;
612 PyObject *body = build_fstring_body(e->v.JoinedStr.values, is_format_spec);
613 if (!body) {
614 return -1;
615 }
616
617 if (!is_format_spec) {
618 if (-1 != append_charp(writer, "f") &&
619 -1 != append_repr(writer, body))
620 {
621 result = 0;
622 }
623 }
624 else {
625 result = _PyUnicodeWriter_WriteStr(writer, body);
626 }
627 Py_DECREF(body);
628 return result;
629}
630
631static int
632append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
633{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300634 const char *conversion;
635 const char *outer_brace = "{";
636 /* Grammar allows PR_TUPLE, but use >PR_TEST for adding parenthesis
637 around a lambda with ':' */
638 PyObject *temp_fv_str = expr_as_unicode(e->v.FormattedValue.value, PR_TEST + 1);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800639 if (!temp_fv_str) {
640 return -1;
641 }
642 if (PyUnicode_Find(temp_fv_str, _str_open_br, 0, 1, 1) == 0) {
643 /* Expression starts with a brace, split it with a space from the outer
644 one. */
645 outer_brace = "{ ";
646 }
647 if (-1 == append_charp(writer, outer_brace)) {
648 Py_DECREF(temp_fv_str);
649 return -1;
650 }
651 if (-1 == _PyUnicodeWriter_WriteStr(writer, temp_fv_str)) {
652 Py_DECREF(temp_fv_str);
653 return -1;
654 }
655 Py_DECREF(temp_fv_str);
656
657 if (e->v.FormattedValue.conversion > 0) {
658 switch (e->v.FormattedValue.conversion) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300659 case 'a':
Guido van Rossum95e4d582018-01-26 08:20:18 -0800660 conversion = "!a";
661 break;
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300662 case 'r':
Guido van Rossum95e4d582018-01-26 08:20:18 -0800663 conversion = "!r";
664 break;
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300665 case 's':
Guido van Rossum95e4d582018-01-26 08:20:18 -0800666 conversion = "!s";
667 break;
668 default:
669 PyErr_SetString(PyExc_SystemError,
670 "unknown f-value conversion kind");
671 return -1;
672 }
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300673 APPEND_STR(conversion);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800674 }
Stéphane Wirtel83ab9952018-02-01 17:59:27 +0100675 if (e->v.FormattedValue.format_spec) {
Guido van Rossum95e4d582018-01-26 08:20:18 -0800676 if (-1 == _PyUnicodeWriter_WriteASCIIString(writer, ":", 1) ||
677 -1 == append_fstring_element(writer,
678 e->v.FormattedValue.format_spec,
679 true
680 ))
681 {
682 return -1;
683 }
684 }
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300685
686 APPEND_STR_FINISH("}");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800687}
688
689static int
690append_ast_attribute(_PyUnicodeWriter *writer, expr_ty e)
691{
692 const char *period;
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300693 APPEND_EXPR(e->v.Attribute.value, PR_ATOM);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800694
695 /* Special case: integers require a space for attribute access to be
696 unambiguous. Floats and complex numbers don't but work with it, too. */
697 if (e->v.Attribute.value->kind == Num_kind ||
698 e->v.Attribute.value->kind == Constant_kind)
699 {
700 period = " .";
701 }
702 else {
703 period = ".";
704 }
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300705 APPEND_STR(period);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800706
707 return _PyUnicodeWriter_WriteStr(writer, e->v.Attribute.attr);
708}
709
710static int
711append_ast_simple_slice(_PyUnicodeWriter *writer, slice_ty slice)
712{
713 if (slice->v.Slice.lower) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300714 APPEND_EXPR(slice->v.Slice.lower, PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800715 }
716
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300717 APPEND_STR(":");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800718
719 if (slice->v.Slice.upper) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300720 APPEND_EXPR(slice->v.Slice.upper, PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800721 }
722
723 if (slice->v.Slice.step) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300724 APPEND_STR(":");
725 APPEND_EXPR(slice->v.Slice.step, PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800726 }
727 return 0;
728}
729
730static int
731append_ast_ext_slice(_PyUnicodeWriter *writer, slice_ty slice)
732{
733 Py_ssize_t i, dims_count;
734 dims_count = asdl_seq_LEN(slice->v.ExtSlice.dims);
735 for (i = 0; i < dims_count; i++) {
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300736 APPEND_STR_IF(i > 0, ", ");
737 APPEND(slice, (slice_ty)asdl_seq_GET(slice->v.ExtSlice.dims, i));
Guido van Rossum95e4d582018-01-26 08:20:18 -0800738 }
739 return 0;
740}
741
742static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300743append_ast_slice(_PyUnicodeWriter *writer, slice_ty slice)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800744{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300745 switch (slice->kind) {
Guido van Rossum95e4d582018-01-26 08:20:18 -0800746 case Slice_kind:
747 return append_ast_simple_slice(writer, slice);
748 case ExtSlice_kind:
749 return append_ast_ext_slice(writer, slice);
750 case Index_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300751 APPEND_EXPR(slice->v.Index.value, PR_TUPLE);
752 return 0;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800753 default:
754 PyErr_SetString(PyExc_SystemError,
755 "unexpected slice kind");
756 return -1;
757 }
758}
759
760static int
761append_ast_subscript(_PyUnicodeWriter *writer, expr_ty e)
762{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300763 APPEND_EXPR(e->v.Subscript.value, PR_ATOM);
764 APPEND_STR("[");
765 APPEND(slice, e->v.Subscript.slice);
766 APPEND_STR_FINISH("]");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800767}
768
769static int
770append_ast_starred(_PyUnicodeWriter *writer, expr_ty e)
771{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300772 APPEND_STR("*");
773 APPEND_EXPR(e->v.Starred.value, PR_EXPR);
774 return 0;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800775}
776
777static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300778append_ast_yield(_PyUnicodeWriter *writer, expr_ty e)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800779{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300780 if (!e->v.Yield.value) {
781 APPEND_STR_FINISH("(yield)");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800782 }
783
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300784 APPEND_STR("(yield ");
785 APPEND_EXPR(e->v.Yield.value, PR_TEST);
786 APPEND_STR_FINISH(")");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800787}
788
789static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300790append_ast_yield_from(_PyUnicodeWriter *writer, expr_ty e)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800791{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300792 APPEND_STR("(yield from ");
793 APPEND_EXPR(e->v.YieldFrom.value, PR_TEST);
794 APPEND_STR_FINISH(")");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800795}
796
797static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300798append_ast_await(_PyUnicodeWriter *writer, expr_ty e, int level)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800799{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300800 APPEND_STR_IF(level > PR_AWAIT, "(");
801 APPEND_STR("await ");
802 APPEND_EXPR(e->v.Await.value, PR_ATOM);
803 APPEND_STR_IF(level > PR_AWAIT, ")");
804 return 0;
Guido van Rossum95e4d582018-01-26 08:20:18 -0800805}
806
807static int
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300808append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800809{
810 switch (e->kind) {
811 case BoolOp_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300812 return append_ast_boolop(writer, e, level);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800813 case BinOp_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300814 return append_ast_binop(writer, e, level);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800815 case UnaryOp_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300816 return append_ast_unaryop(writer, e, level);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800817 case Lambda_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300818 return append_ast_lambda(writer, e, level);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800819 case IfExp_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300820 return append_ast_ifexp(writer, e, level);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800821 case Dict_kind:
822 return append_ast_dict(writer, e);
823 case Set_kind:
824 return append_ast_set(writer, e);
825 case GeneratorExp_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300826 return append_ast_genexp(writer, e);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800827 case ListComp_kind:
828 return append_ast_listcomp(writer, e);
829 case SetComp_kind:
830 return append_ast_setcomp(writer, e);
831 case DictComp_kind:
832 return append_ast_dictcomp(writer, e);
833 case Yield_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300834 return append_ast_yield(writer, e);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800835 case YieldFrom_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300836 return append_ast_yield_from(writer, e);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800837 case Await_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300838 return append_ast_await(writer, e, level);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800839 case Compare_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300840 return append_ast_compare(writer, e, level);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800841 case Call_kind:
842 return append_ast_call(writer, e);
843 case Constant_kind:
844 return append_repr(writer, e->v.Constant.value);
845 case Num_kind:
846 return append_repr(writer, e->v.Num.n);
847 case Str_kind:
848 return append_repr(writer, e->v.Str.s);
849 case JoinedStr_kind:
850 return append_joinedstr(writer, e, false);
851 case FormattedValue_kind:
852 return append_formattedvalue(writer, e, false);
853 case Bytes_kind:
854 return append_repr(writer, e->v.Bytes.s);
855 case Ellipsis_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300856 APPEND_STR_FINISH("...");
Guido van Rossum95e4d582018-01-26 08:20:18 -0800857 case NameConstant_kind:
858 return append_repr(writer, e->v.NameConstant.value);
859 /* The following exprs can be assignment targets. */
860 case Attribute_kind:
861 return append_ast_attribute(writer, e);
862 case Subscript_kind:
863 return append_ast_subscript(writer, e);
864 case Starred_kind:
865 return append_ast_starred(writer, e);
866 case Name_kind:
867 return _PyUnicodeWriter_WriteStr(writer, e->v.Name.id);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800868 case List_kind:
869 return append_ast_list(writer, e);
870 case Tuple_kind:
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300871 return append_ast_tuple(writer, e, level);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800872 default:
873 PyErr_SetString(PyExc_SystemError,
874 "unknown expression kind");
875 return -1;
876 }
877}
878
879static int
Stéphane Wirtel83ab9952018-02-01 17:59:27 +0100880maybe_init_static_strings(void)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800881{
882 if (!_str_open_br &&
883 !(_str_open_br = PyUnicode_InternFromString("{"))) {
884 return -1;
885 }
886 if (!_str_dbl_open_br &&
887 !(_str_dbl_open_br = PyUnicode_InternFromString("{{"))) {
888 return -1;
889 }
890 if (!_str_close_br &&
891 !(_str_close_br = PyUnicode_InternFromString("}"))) {
892 return -1;
893 }
894 if (!_str_dbl_close_br &&
895 !(_str_dbl_close_br = PyUnicode_InternFromString("}}"))) {
896 return -1;
897 }
898 return 0;
899}
900
901static PyObject *
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300902expr_as_unicode(expr_ty e, int level)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800903{
904 _PyUnicodeWriter writer;
905 _PyUnicodeWriter_Init(&writer);
906 writer.min_length = 256;
907 writer.overallocate = 1;
908 if (-1 == maybe_init_static_strings() ||
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300909 -1 == append_ast_expr(&writer, e, level))
Guido van Rossum95e4d582018-01-26 08:20:18 -0800910 {
911 _PyUnicodeWriter_Dealloc(&writer);
912 return NULL;
913 }
914 return _PyUnicodeWriter_Finish(&writer);
915}
916
917PyObject *
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300918_PyAST_ExprAsUnicode(expr_ty e)
Guido van Rossum95e4d582018-01-26 08:20:18 -0800919{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +0300920 return expr_as_unicode(e, PR_TEST);
Guido van Rossum95e4d582018-01-26 08:20:18 -0800921}