Coverage Report

Created: 2026-04-20 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython3/Python/ast_unparse.c
Line
Count
Source
1
#include "Python.h"
2
#include "pycore_ast.h"           // expr_ty
3
#include "pycore_pystate.h"       // _PyInterpreterState_GET()
4
#include "pycore_runtime.h"       // _Py_ID()
5
#include <stdbool.h>
6
7
/* This limited unparser is used to convert annotations back to strings
8
 * during compilation rather than being a full AST unparser.
9
 * See ast.unparse for a full unparser (written in Python)
10
 */
11
12
_Py_DECLARE_STR(dbl_open_br, "{{");
13
_Py_DECLARE_STR(dbl_close_br, "}}");
14
15
/* Forward declarations for recursion via helper functions. */
16
static PyObject *
17
expr_as_unicode(expr_ty e, int level);
18
static int
19
append_ast_expr(PyUnicodeWriter *writer, expr_ty e, int level);
20
static int
21
append_templatestr(PyUnicodeWriter *writer, expr_ty e);
22
static int
23
append_joinedstr(PyUnicodeWriter *writer, expr_ty e, bool is_format_spec);
24
static int
25
append_interpolation(PyUnicodeWriter *writer, expr_ty e);
26
static int
27
append_formattedvalue(PyUnicodeWriter *writer, expr_ty e);
28
static int
29
append_ast_slice(PyUnicodeWriter *writer, expr_ty e);
30
31
static int
32
append_char(PyUnicodeWriter *writer, Py_UCS4 ch)
33
236
{
34
236
    return PyUnicodeWriter_WriteChar(writer, ch);
35
236
}
36
37
static int
38
append_charp(PyUnicodeWriter *writer, const char *charp)
39
38.8k
{
40
38.8k
    return PyUnicodeWriter_WriteUTF8(writer, charp, -1);
41
38.8k
}
42
43
100
#define APPEND_CHAR_FINISH(ch)  do { \
44
100
        return append_char(writer, (ch)); \
45
100
    } while (0)
46
47
99
#define APPEND_STR_FINISH(str)  do { \
48
99
        return append_charp(writer, (str)); \
49
99
    } while (0)
50
51
136
#define APPEND_CHAR(ch)  do { \
52
136
        if (-1 == append_char(writer, (ch))) { \
53
0
            return -1; \
54
0
        } \
55
136
    } while (0)
56
57
35.1k
#define APPEND_STR(str)  do { \
58
35.1k
        if (-1 == append_charp(writer, (str))) { \
59
0
            return -1; \
60
0
        } \
61
35.1k
    } while (0)
62
63
69.1k
#define APPEND_STR_IF(cond, str)  do { \
64
69.1k
        if ((cond) && -1 == append_charp(writer, (str))) { \
65
0
            return -1; \
66
0
        } \
67
69.1k
    } while (0)
68
69
4
#define APPEND_STR_IF_NOT_FIRST(str)  do { \
70
4
        APPEND_STR_IF(!first, (str)); \
71
4
        first = false; \
72
4
    } while (0)
73
74
61.2k
#define APPEND_EXPR(expr, pr)  do { \
75
61.2k
        if (-1 == append_ast_expr(writer, (expr), (pr))) { \
76
0
            return -1; \
77
0
        } \
78
61.2k
    } while (0)
79
80
2
#define APPEND(type, value)  do { \
81
2
        if (-1 == append_ast_ ## type(writer, (value))) { \
82
0
            return -1; \
83
0
        } \
84
2
    } while (0)
85
86
static int
87
append_repr(PyUnicodeWriter *writer, PyObject *obj)
88
25.5k
{
89
25.5k
    PyObject *repr = PyObject_Repr(obj);
90
25.5k
    if (!repr) {
91
0
        return -1;
92
0
    }
93
94
25.5k
    if ((PyFloat_CheckExact(obj) && isinf(PyFloat_AS_DOUBLE(obj))) ||
95
25.3k
        PyComplex_CheckExact(obj))
96
3.51k
    {
97
3.51k
        _Py_DECLARE_STR(str_replace_inf, "1e309");  // evaluates to inf
98
3.51k
        PyObject *new_repr = PyUnicode_Replace(
99
3.51k
            repr,
100
3.51k
            &_Py_ID(inf),
101
3.51k
            &_Py_STR(str_replace_inf),
102
3.51k
            -1
103
3.51k
        );
104
3.51k
        Py_DECREF(repr);
105
3.51k
        if (!new_repr) {
106
0
            return -1;
107
0
        }
108
3.51k
        repr = new_repr;
109
3.51k
    }
110
111
25.5k
    int ret = PyUnicodeWriter_WriteStr(writer, repr);
112
25.5k
    Py_DECREF(repr);
113
25.5k
    return ret;
114
25.5k
}
115
116
/* Priority levels */
117
118
enum {
119
    PR_TUPLE,
120
    PR_TEST,            /* 'if'-'else', 'lambda' */
121
    PR_OR,              /* 'or' */
122
    PR_AND,             /* 'and' */
123
    PR_NOT,             /* 'not' */
124
    PR_CMP,             /* '<', '>', '==', '>=', '<=', '!=',
125
                           'in', 'not in', 'is', 'is not' */
126
    PR_EXPR,
127
    PR_BOR = PR_EXPR,   /* '|' */
128
    PR_BXOR,            /* '^' */
129
    PR_BAND,            /* '&' */
130
    PR_SHIFT,           /* '<<', '>>' */
131
    PR_ARITH,           /* '+', '-' */
132
    PR_TERM,            /* '*', '@', '/', '%', '//' */
133
    PR_FACTOR,          /* unary '+', '-', '~' */
134
    PR_POWER,           /* '**' */
135
    PR_AWAIT,           /* 'await' */
136
    PR_ATOM,
137
};
138
139
static int
140
append_ast_boolop(PyUnicodeWriter *writer, expr_ty e, int level)
141
79
{
142
79
    Py_ssize_t i, value_count;
143
79
    asdl_expr_seq *values;
144
79
    const char *op = (e->v.BoolOp.op == And) ? " and " : " or ";
145
79
    int pr = (e->v.BoolOp.op == And) ? PR_AND : PR_OR;
146
147
79
    APPEND_STR_IF(level > pr, "(");
148
149
79
    values = e->v.BoolOp.values;
150
79
    value_count = asdl_seq_LEN(values);
151
152
743
    for (i = 0; i < value_count; ++i) {
153
664
        APPEND_STR_IF(i > 0, op);
154
664
        APPEND_EXPR((expr_ty)asdl_seq_GET(values, i), pr + 1);
155
664
    }
156
157
79
    APPEND_STR_IF(level > pr, ")");
158
79
    return 0;
159
79
}
160
161
static int
162
append_ast_binop(PyUnicodeWriter *writer, expr_ty e, int level)
163
24.5k
{
164
24.5k
    const char *op;
165
24.5k
    int pr;
166
24.5k
    bool rassoc = false;  /* is right-associative? */
167
168
24.5k
    switch (e->v.BinOp.op) {
169
2.49k
    case Add: op = " + "; pr = PR_ARITH; break;
170
1.08k
    case Sub: op = " - "; pr = PR_ARITH; break;
171
10.2k
    case Mult: op = " * "; pr = PR_TERM; break;
172
68
    case MatMult: op = " @ "; pr = PR_TERM; break;
173
860
    case Div: op = " / "; pr = PR_TERM; break;
174
720
    case Mod: op = " % "; pr = PR_TERM; break;
175
469
    case LShift: op = " << "; pr = PR_SHIFT; break;
176
94
    case RShift: op = " >> "; pr = PR_SHIFT; break;
177
8
    case BitOr: op = " | "; pr = PR_BOR; break;
178
5.67k
    case BitXor: op = " ^ "; pr = PR_BXOR; break;
179
214
    case BitAnd: op = " & "; pr = PR_BAND; break;
180
402
    case FloorDiv: op = " // "; pr = PR_TERM; break;
181
2.25k
    case Pow: op = " ** "; pr = PR_POWER; rassoc = true; break;
182
0
    default:
183
0
        PyErr_SetString(PyExc_SystemError,
184
0
                        "unknown binary operator");
185
0
        return -1;
186
24.5k
    }
187
188
24.5k
    APPEND_STR_IF(level > pr, "(");
189
24.5k
    APPEND_EXPR(e->v.BinOp.left, pr + rassoc);
190
24.5k
    APPEND_STR(op);
191
24.5k
    APPEND_EXPR(e->v.BinOp.right, pr + !rassoc);
192
24.5k
    APPEND_STR_IF(level > pr, ")");
193
24.5k
    return 0;
194
24.5k
}
195
196
static int
197
append_ast_unaryop(PyUnicodeWriter *writer, expr_ty e, int level)
198
8.83k
{
199
8.83k
    const char *op;
200
8.83k
    int pr;
201
202
8.83k
    switch (e->v.UnaryOp.op) {
203
65
    case Invert: op = "~"; pr = PR_FACTOR; break;
204
0
    case Not: op = "not "; pr = PR_NOT; break;
205
6.99k
    case UAdd: op = "+"; pr = PR_FACTOR; break;
206
1.77k
    case USub: op = "-"; pr = PR_FACTOR; break;
207
0
    default:
208
0
        PyErr_SetString(PyExc_SystemError,
209
0
                        "unknown unary operator");
210
0
        return -1;
211
8.83k
    }
212
213
8.83k
    APPEND_STR_IF(level > pr, "(");
214
8.83k
    APPEND_STR(op);
215
8.83k
    APPEND_EXPR(e->v.UnaryOp.operand, pr);
216
8.83k
    APPEND_STR_IF(level > pr, ")");
217
8.83k
    return 0;
218
8.83k
}
219
220
static int
221
append_ast_arg(PyUnicodeWriter *writer, arg_ty arg)
222
0
{
223
0
    if (PyUnicodeWriter_WriteStr(writer, arg->arg) < 0) {
224
0
        return -1;
225
0
    }
226
0
    if (arg->annotation) {
227
0
        APPEND_STR(": ");
228
0
        APPEND_EXPR(arg->annotation, PR_TEST);
229
0
    }
230
0
    return 0;
231
0
}
232
233
static int
234
append_ast_args(PyUnicodeWriter *writer, arguments_ty args)
235
1
{
236
1
    bool first;
237
1
    Py_ssize_t i, di, arg_count, posonlyarg_count, default_count;
238
239
1
    first = true;
240
241
    /* positional-only and positional arguments with defaults */
242
1
    posonlyarg_count = asdl_seq_LEN(args->posonlyargs);
243
1
    arg_count = asdl_seq_LEN(args->args);
244
1
    default_count = asdl_seq_LEN(args->defaults);
245
1
    for (i = 0; i < posonlyarg_count + arg_count; i++) {
246
0
        APPEND_STR_IF_NOT_FIRST(", ");
247
0
        if (i < posonlyarg_count){
248
0
            APPEND(arg, (arg_ty)asdl_seq_GET(args->posonlyargs, i));
249
0
        } else {
250
0
            APPEND(arg, (arg_ty)asdl_seq_GET(args->args, i-posonlyarg_count));
251
0
        }
252
253
0
        di = i - posonlyarg_count - arg_count + default_count;
254
0
        if (di >= 0) {
255
0
            APPEND_CHAR('=');
256
0
            APPEND_EXPR((expr_ty)asdl_seq_GET(args->defaults, di), PR_TEST);
257
0
        }
258
0
        if (posonlyarg_count && i + 1 == posonlyarg_count) {
259
0
            APPEND_STR(", /");
260
0
        }
261
0
    }
262
263
    /* vararg, or bare '*' if no varargs but keyword-only arguments present */
264
1
    if (args->vararg || asdl_seq_LEN(args->kwonlyargs)) {
265
0
        APPEND_STR_IF_NOT_FIRST(", ");
266
0
        APPEND_STR("*");
267
0
        if (args->vararg) {
268
0
            APPEND(arg, args->vararg);
269
0
        }
270
0
    }
271
272
    /* keyword-only arguments */
273
1
    arg_count = asdl_seq_LEN(args->kwonlyargs);
274
1
    default_count = asdl_seq_LEN(args->kw_defaults);
275
1
    for (i = 0; i < arg_count; i++) {
276
0
        APPEND_STR_IF_NOT_FIRST(", ");
277
0
        APPEND(arg, (arg_ty)asdl_seq_GET(args->kwonlyargs, i));
278
279
0
        di = i - arg_count + default_count;
280
0
        if (di >= 0) {
281
0
            expr_ty default_ = (expr_ty)asdl_seq_GET(args->kw_defaults, di);
282
0
            if (default_) {
283
0
                APPEND_CHAR('=');
284
0
                APPEND_EXPR(default_, PR_TEST);
285
0
            }
286
0
        }
287
0
    }
288
289
    /* **kwargs */
290
1
    if (args->kwarg) {
291
0
        APPEND_STR_IF_NOT_FIRST(", ");
292
0
        APPEND_STR("**");
293
0
        APPEND(arg, args->kwarg);
294
0
    }
295
296
1
    return 0;
297
1
}
298
299
static int
300
append_ast_lambda(PyUnicodeWriter *writer, expr_ty e, int level)
301
1
{
302
1
    APPEND_STR_IF(level > PR_TEST, "(");
303
1
    Py_ssize_t n_positional = (asdl_seq_LEN(e->v.Lambda.args->args) +
304
1
                               asdl_seq_LEN(e->v.Lambda.args->posonlyargs));
305
1
    APPEND_STR(n_positional ? "lambda " : "lambda");
306
1
    APPEND(args, e->v.Lambda.args);
307
1
    APPEND_STR(": ");
308
1
    APPEND_EXPR(e->v.Lambda.body, PR_TEST);
309
1
    APPEND_STR_IF(level > PR_TEST, ")");
310
1
    return 0;
311
1
}
312
313
static int
314
append_ast_ifexp(PyUnicodeWriter *writer, expr_ty e, int level)
315
5
{
316
5
    APPEND_STR_IF(level > PR_TEST, "(");
317
5
    APPEND_EXPR(e->v.IfExp.body, PR_TEST + 1);
318
5
    APPEND_STR(" if ");
319
5
    APPEND_EXPR(e->v.IfExp.test, PR_TEST + 1);
320
5
    APPEND_STR(" else ");
321
5
    APPEND_EXPR(e->v.IfExp.orelse, PR_TEST);
322
5
    APPEND_STR_IF(level > PR_TEST, ")");
323
5
    return 0;
324
5
}
325
326
static int
327
append_ast_dict(PyUnicodeWriter *writer, expr_ty e)
328
4
{
329
4
    Py_ssize_t i, value_count;
330
4
    expr_ty key_node;
331
332
4
    APPEND_CHAR('{');
333
4
    value_count = asdl_seq_LEN(e->v.Dict.values);
334
335
4
    for (i = 0; i < value_count; i++) {
336
0
        APPEND_STR_IF(i > 0, ", ");
337
0
        key_node = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i);
338
0
        if (key_node != NULL) {
339
0
            APPEND_EXPR(key_node, PR_TEST);
340
0
            APPEND_STR(": ");
341
0
            APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Dict.values, i), PR_TEST);
342
0
        }
343
0
        else {
344
0
            APPEND_STR("**");
345
0
            APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Dict.values, i), PR_EXPR);
346
0
        }
347
0
    }
348
349
4
    APPEND_CHAR_FINISH('}');
350
4
}
351
352
static int
353
append_ast_set(PyUnicodeWriter *writer, expr_ty e)
354
9
{
355
9
    Py_ssize_t i, elem_count;
356
357
9
    APPEND_CHAR('{');
358
9
    elem_count = asdl_seq_LEN(e->v.Set.elts);
359
18
    for (i = 0; i < elem_count; i++) {
360
9
        APPEND_STR_IF(i > 0, ", ");
361
9
        APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Set.elts, i), PR_TEST);
362
9
    }
363
364
9
    APPEND_CHAR_FINISH('}');
365
9
}
366
367
static int
368
append_ast_list(PyUnicodeWriter *writer, expr_ty e)
369
10
{
370
10
    Py_ssize_t i, elem_count;
371
372
10
    APPEND_CHAR('[');
373
10
    elem_count = asdl_seq_LEN(e->v.List.elts);
374
51
    for (i = 0; i < elem_count; i++) {
375
41
        APPEND_STR_IF(i > 0, ", ");
376
41
        APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.List.elts, i), PR_TEST);
377
41
    }
378
379
10
    APPEND_CHAR_FINISH(']');
380
10
}
381
382
static int
383
append_ast_tuple(PyUnicodeWriter *writer, expr_ty e, int level)
384
7
{
385
7
    Py_ssize_t i, elem_count;
386
387
7
    elem_count = asdl_seq_LEN(e->v.Tuple.elts);
388
389
7
    if (elem_count == 0) {
390
1
        APPEND_STR_FINISH("()");
391
1
    }
392
393
6
    APPEND_STR_IF(level > PR_TUPLE, "(");
394
395
21
    for (i = 0; i < elem_count; i++) {
396
15
        APPEND_STR_IF(i > 0, ", ");
397
15
        APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Tuple.elts, i), PR_TEST);
398
15
    }
399
400
6
    APPEND_STR_IF(elem_count == 1, ",");
401
6
    APPEND_STR_IF(level > PR_TUPLE, ")");
402
6
    return 0;
403
6
}
404
405
static int
406
append_ast_comprehension(PyUnicodeWriter *writer, comprehension_ty gen)
407
0
{
408
0
    Py_ssize_t i, if_count;
409
410
0
    APPEND_STR(gen->is_async ? " async for " : " for ");
411
0
    APPEND_EXPR(gen->target, PR_TUPLE);
412
0
    APPEND_STR(" in ");
413
0
    APPEND_EXPR(gen->iter, PR_TEST + 1);
414
415
0
    if_count = asdl_seq_LEN(gen->ifs);
416
0
    for (i = 0; i < if_count; i++) {
417
0
        APPEND_STR(" if ");
418
0
        APPEND_EXPR((expr_ty)asdl_seq_GET(gen->ifs, i), PR_TEST + 1);
419
0
    }
420
0
    return 0;
421
0
}
422
423
static int
424
append_ast_comprehensions(PyUnicodeWriter *writer, asdl_comprehension_seq *comprehensions)
425
0
{
426
0
    Py_ssize_t i, gen_count;
427
0
    gen_count = asdl_seq_LEN(comprehensions);
428
429
0
    for (i = 0; i < gen_count; i++) {
430
0
        APPEND(comprehension, (comprehension_ty)asdl_seq_GET(comprehensions, i));
431
0
    }
432
433
0
    return 0;
434
0
}
435
436
static int
437
append_ast_genexp(PyUnicodeWriter *writer, expr_ty e)
438
0
{
439
0
    APPEND_CHAR('(');
440
0
    APPEND_EXPR(e->v.GeneratorExp.elt, PR_TEST);
441
0
    APPEND(comprehensions, e->v.GeneratorExp.generators);
442
0
    APPEND_CHAR_FINISH(')');
443
0
}
444
445
static int
446
append_ast_listcomp(PyUnicodeWriter *writer, expr_ty e)
447
0
{
448
0
    APPEND_CHAR('[');
449
0
    APPEND_EXPR(e->v.ListComp.elt, PR_TEST);
450
0
    APPEND(comprehensions, e->v.ListComp.generators);
451
0
    APPEND_CHAR_FINISH(']');
452
0
}
453
454
static int
455
append_ast_setcomp(PyUnicodeWriter *writer, expr_ty e)
456
0
{
457
0
    APPEND_CHAR('{');
458
0
    APPEND_EXPR(e->v.SetComp.elt, PR_TEST);
459
0
    APPEND(comprehensions, e->v.SetComp.generators);
460
0
    APPEND_CHAR_FINISH('}');
461
0
}
462
463
static int
464
append_ast_dictcomp(PyUnicodeWriter *writer, expr_ty e)
465
0
{
466
0
    APPEND_CHAR('{');
467
0
    if (e->v.DictComp.value) {
468
0
        APPEND_EXPR(e->v.DictComp.key, PR_TEST);
469
0
        APPEND_STR(": ");
470
0
        APPEND_EXPR(e->v.DictComp.value, PR_TEST);
471
0
    }
472
0
    else {
473
0
        APPEND_STR("**");
474
0
        APPEND_EXPR(e->v.DictComp.key, PR_TEST);
475
0
    }
476
0
    APPEND(comprehensions, e->v.DictComp.generators);
477
0
    APPEND_CHAR_FINISH('}');
478
0
}
479
480
static int
481
append_ast_compare(PyUnicodeWriter *writer, expr_ty e, int level)
482
680
{
483
680
    const char *op;
484
680
    Py_ssize_t i, comparator_count;
485
680
    asdl_expr_seq *comparators;
486
680
    asdl_int_seq *ops;
487
488
680
    APPEND_STR_IF(level > PR_CMP, "(");
489
490
680
    comparators = e->v.Compare.comparators;
491
680
    ops = e->v.Compare.ops;
492
680
    comparator_count = asdl_seq_LEN(comparators);
493
680
    assert(comparator_count > 0);
494
680
    assert(comparator_count == asdl_seq_LEN(ops));
495
496
680
    APPEND_EXPR(e->v.Compare.left, PR_CMP + 1);
497
498
2.32k
    for (i = 0; i < comparator_count; i++) {
499
1.64k
        switch ((cmpop_ty)asdl_seq_GET(ops, i)) {
500
1
        case Eq:
501
1
            op = " == ";
502
1
            break;
503
5
        case NotEq:
504
5
            op = " != ";
505
5
            break;
506
1.25k
        case Lt:
507
1.25k
            op = " < ";
508
1.25k
            break;
509
7
        case LtE:
510
7
            op = " <= ";
511
7
            break;
512
114
        case Gt:
513
114
            op = " > ";
514
114
            break;
515
4
        case GtE:
516
4
            op = " >= ";
517
4
            break;
518
248
        case Is:
519
248
            op = " is ";
520
248
            break;
521
0
        case IsNot:
522
0
            op = " is not ";
523
0
            break;
524
18
        case In:
525
18
            op = " in ";
526
18
            break;
527
0
        case NotIn:
528
0
            op = " not in ";
529
0
            break;
530
0
        default:
531
0
            PyErr_SetString(PyExc_SystemError,
532
0
                            "unexpected comparison kind");
533
0
            return -1;
534
1.64k
        }
535
536
1.64k
        APPEND_STR(op);
537
1.64k
        APPEND_EXPR((expr_ty)asdl_seq_GET(comparators, i), PR_CMP + 1);
538
1.64k
    }
539
540
680
    APPEND_STR_IF(level > PR_CMP, ")");
541
680
    return 0;
542
680
}
543
544
static int
545
append_ast_keyword(PyUnicodeWriter *writer, keyword_ty kw)
546
1
{
547
1
    if (kw->arg == NULL) {
548
0
        APPEND_STR("**");
549
0
    }
550
1
    else {
551
1
        if (-1 == PyUnicodeWriter_WriteStr(writer, kw->arg)) {
552
0
            return -1;
553
0
        }
554
555
1
        APPEND_CHAR('=');
556
1
    }
557
558
1
    APPEND_EXPR(kw->value, PR_TEST);
559
1
    return 0;
560
1
}
561
562
static int
563
append_ast_call(PyUnicodeWriter *writer, expr_ty e)
564
12
{
565
12
    bool first;
566
12
    Py_ssize_t i, arg_count, kw_count;
567
12
    expr_ty expr;
568
569
12
    APPEND_EXPR(e->v.Call.func, PR_ATOM);
570
571
12
    arg_count = asdl_seq_LEN(e->v.Call.args);
572
12
    kw_count = asdl_seq_LEN(e->v.Call.keywords);
573
12
    if (arg_count == 1 && kw_count == 0) {
574
3
        expr = (expr_ty)asdl_seq_GET(e->v.Call.args, 0);
575
3
        if (expr->kind == GeneratorExp_kind) {
576
            /* Special case: a single generator expression. */
577
0
            return append_ast_genexp(writer, expr);
578
0
        }
579
3
    }
580
581
12
    APPEND_CHAR('(');
582
583
12
    first = true;
584
15
    for (i = 0; i < arg_count; i++) {
585
3
        APPEND_STR_IF_NOT_FIRST(", ");
586
3
        APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Call.args, i), PR_TEST);
587
3
    }
588
589
13
    for (i = 0; i < kw_count; i++) {
590
1
        APPEND_STR_IF_NOT_FIRST(", ");
591
1
        APPEND(keyword, (keyword_ty)asdl_seq_GET(e->v.Call.keywords, i));
592
1
    }
593
594
12
    APPEND_CHAR_FINISH(')');
595
12
}
596
597
static PyObject *
598
escape_braces(PyObject *orig)
599
142
{
600
142
    PyObject *temp;
601
142
    PyObject *result;
602
142
    temp = PyUnicode_Replace(orig, _Py_LATIN1_CHR('{'),
603
142
                             &_Py_STR(dbl_open_br), -1);
604
142
    if (!temp) {
605
0
        return NULL;
606
0
    }
607
142
    result = PyUnicode_Replace(temp, _Py_LATIN1_CHR('}'),
608
142
                               &_Py_STR(dbl_close_br), -1);
609
142
    Py_DECREF(temp);
610
142
    return result;
611
142
}
612
613
static int
614
append_fstring_unicode(PyUnicodeWriter *writer, PyObject *unicode)
615
142
{
616
142
    PyObject *escaped;
617
142
    int result = -1;
618
142
    escaped = escape_braces(unicode);
619
142
    if (escaped) {
620
142
        result = PyUnicodeWriter_WriteStr(writer, escaped);
621
142
        Py_DECREF(escaped);
622
142
    }
623
142
    return result;
624
142
}
625
626
static int
627
append_fstring_element(PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
628
285
{
629
285
    switch (e->kind) {
630
142
    case Constant_kind:
631
142
        return append_fstring_unicode(writer, e->v.Constant.value);
632
4
    case JoinedStr_kind:
633
4
        return append_joinedstr(writer, e, is_format_spec);
634
0
    case TemplateStr_kind:
635
0
        return append_templatestr(writer, e);
636
47
    case FormattedValue_kind:
637
47
        return append_formattedvalue(writer, e);
638
92
    case Interpolation_kind:
639
92
        return append_interpolation(writer, e);
640
0
    default:
641
0
        PyErr_SetString(PyExc_SystemError,
642
0
                        "unknown expression kind inside f-string or t-string");
643
0
        return -1;
644
285
    }
645
285
}
646
647
/* Build body separately to enable wrapping the entire stream of Strs,
648
   Constants and FormattedValues in one opening and one closing quote. */
649
static PyObject *
650
build_ftstring_body(asdl_expr_seq *values, bool is_format_spec)
651
112
{
652
112
    PyUnicodeWriter *body_writer = PyUnicodeWriter_Create(256);
653
112
    if (body_writer == NULL) {
654
0
        return NULL;
655
0
    }
656
657
112
    Py_ssize_t value_count = asdl_seq_LEN(values);
658
393
    for (Py_ssize_t i = 0; i < value_count; ++i) {
659
281
        if (-1 == append_fstring_element(body_writer,
660
281
                                         (expr_ty)asdl_seq_GET(values, i),
661
281
                                         is_format_spec
662
281
                                         )) {
663
0
            PyUnicodeWriter_Discard(body_writer);
664
0
            return NULL;
665
0
        }
666
281
    }
667
668
112
    return PyUnicodeWriter_Finish(body_writer);
669
112
}
670
671
static int
672
append_templatestr(PyUnicodeWriter *writer, expr_ty e)
673
51
{
674
51
    int result = -1;
675
51
    PyObject *body = build_ftstring_body(e->v.TemplateStr.values, false);
676
51
    if (!body) {
677
0
        return -1;
678
0
    }
679
680
51
    if (-1 != append_charp(writer, "t") &&
681
51
        -1 != append_repr(writer, body))
682
51
    {
683
51
        result = 0;
684
51
    }
685
51
    Py_DECREF(body);
686
51
    return result;
687
51
}
688
689
static int
690
append_joinedstr(PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
691
61
{
692
61
    int result = -1;
693
61
    PyObject *body = build_ftstring_body(e->v.JoinedStr.values, is_format_spec);
694
61
    if (!body) {
695
0
        return -1;
696
0
    }
697
698
61
    if (!is_format_spec) {
699
57
        if (-1 != append_charp(writer, "f") &&
700
57
            -1 != append_repr(writer, body))
701
57
        {
702
57
            result = 0;
703
57
        }
704
57
    }
705
4
    else {
706
4
        result = PyUnicodeWriter_WriteStr(writer, body);
707
4
    }
708
61
    Py_DECREF(body);
709
61
    return result;
710
61
}
711
712
static int
713
append_interpolation_str(PyUnicodeWriter *writer, PyObject *str)
714
139
{
715
139
    const char *outer_brace = "{";
716
139
    if (PyUnicode_Find(str, _Py_LATIN1_CHR('{'), 0, 1, 1) == 0) {
717
        /* Expression starts with a brace, split it with a space from the outer
718
           one. */
719
0
        outer_brace = "{ ";
720
0
    }
721
139
    if (-1 == append_charp(writer, outer_brace)) {
722
0
        return -1;
723
0
    }
724
139
    if (-1 == PyUnicodeWriter_WriteStr(writer, str)) {
725
0
        return -1;
726
0
    }
727
139
    return 0;
728
139
}
729
730
static int
731
append_interpolation_value(PyUnicodeWriter *writer, expr_ty e)
732
47
{
733
    /* Grammar allows PR_TUPLE, but use >PR_TEST for adding parenthesis
734
       around a lambda with ':' */
735
47
    PyObject *temp_fv_str = expr_as_unicode(e, PR_TEST + 1);
736
47
    if (!temp_fv_str) {
737
0
        return -1;
738
0
    }
739
47
    int result = append_interpolation_str(writer, temp_fv_str);
740
47
    Py_DECREF(temp_fv_str);
741
47
    return result;
742
47
}
743
744
static int
745
append_interpolation_conversion(PyUnicodeWriter *writer, int conversion)
746
139
{
747
139
    if (conversion < 0) {
748
88
        return 0;
749
88
    }
750
751
51
    const char *conversion_str;
752
51
    switch (conversion) {
753
49
    case 'a':
754
49
        conversion_str = "!a";
755
49
        break;
756
2
    case 'r':
757
2
        conversion_str = "!r";
758
2
        break;
759
0
    case 's':
760
0
        conversion_str = "!s";
761
0
        break;
762
0
    default:
763
0
        PyErr_SetString(PyExc_SystemError,
764
0
                        "unknown f-value conversion kind");
765
0
        return -1;
766
51
    }
767
51
    APPEND_STR(conversion_str);
768
51
    return 0;
769
51
}
770
771
static int
772
append_interpolation_format_spec(PyUnicodeWriter *writer, expr_ty e)
773
139
{
774
139
    if (e) {
775
4
        if (-1 == PyUnicodeWriter_WriteChar(writer, ':') ||
776
4
            -1 == append_fstring_element(writer, e, true))
777
0
        {
778
0
            return -1;
779
0
        }
780
4
    }
781
139
    return 0;
782
139
}
783
784
static int
785
append_interpolation(PyUnicodeWriter *writer, expr_ty e)
786
92
{
787
92
    if (-1 == append_interpolation_str(writer, e->v.Interpolation.str)) {
788
0
        return -1;
789
0
    }
790
791
92
    if (-1 == append_interpolation_conversion(writer, e->v.Interpolation.conversion)) {
792
0
        return -1;
793
0
    }
794
795
92
    if (-1 == append_interpolation_format_spec(writer, e->v.Interpolation.format_spec)) {
796
0
        return -1;
797
0
    }
798
799
92
    APPEND_STR_FINISH("}");
800
92
}
801
802
static int
803
append_formattedvalue(PyUnicodeWriter *writer, expr_ty e)
804
47
{
805
47
    if (-1 == append_interpolation_value(writer, e->v.FormattedValue.value)) {
806
0
        return -1;
807
0
    }
808
809
47
    if (-1 == append_interpolation_conversion(writer, e->v.FormattedValue.conversion)) {
810
0
        return -1;
811
0
    }
812
813
47
    if (-1 == append_interpolation_format_spec(writer, e->v.FormattedValue.format_spec)) {
814
0
        return -1;
815
0
    }
816
817
47
    APPEND_CHAR_FINISH('}');
818
47
}
819
820
static int
821
append_ast_constant(PyUnicodeWriter *writer, PyObject *constant)
822
25.4k
{
823
25.4k
    if (PyTuple_CheckExact(constant)) {
824
0
        Py_ssize_t i, elem_count;
825
826
0
        elem_count = PyTuple_GET_SIZE(constant);
827
0
        APPEND_CHAR('(');
828
0
        for (i = 0; i < elem_count; i++) {
829
0
            APPEND_STR_IF(i > 0, ", ");
830
0
            if (append_ast_constant(writer, PyTuple_GET_ITEM(constant, i)) < 0) {
831
0
                return -1;
832
0
            }
833
0
        }
834
835
0
        APPEND_STR_IF(elem_count == 1, ",");
836
0
        APPEND_CHAR_FINISH(')');
837
0
    }
838
25.4k
    return append_repr(writer, constant);
839
25.4k
}
840
841
static int
842
append_ast_attribute(PyUnicodeWriter *writer, expr_ty e)
843
22
{
844
22
    const char *period;
845
22
    expr_ty v = e->v.Attribute.value;
846
22
    APPEND_EXPR(v, PR_ATOM);
847
848
    /* Special case: integers require a space for attribute access to be
849
       unambiguous. */
850
22
    if (v->kind == Constant_kind && PyLong_CheckExact(v->v.Constant.value)) {
851
1
        period = " .";
852
1
    }
853
21
    else {
854
21
        period = ".";
855
21
    }
856
22
    APPEND_STR(period);
857
858
22
    return PyUnicodeWriter_WriteStr(writer, e->v.Attribute.attr);
859
22
}
860
861
static int
862
append_ast_slice(PyUnicodeWriter *writer, expr_ty e)
863
15
{
864
15
    if (e->v.Slice.lower) {
865
1
        APPEND_EXPR(e->v.Slice.lower, PR_TEST);
866
1
    }
867
868
15
    APPEND_CHAR(':');
869
870
15
    if (e->v.Slice.upper) {
871
4
        APPEND_EXPR(e->v.Slice.upper, PR_TEST);
872
4
    }
873
874
15
    if (e->v.Slice.step) {
875
11
        APPEND_CHAR(':');
876
11
        APPEND_EXPR(e->v.Slice.step, PR_TEST);
877
11
    }
878
15
    return 0;
879
15
}
880
881
static int
882
append_ast_subscript(PyUnicodeWriter *writer, expr_ty e)
883
18
{
884
18
    APPEND_EXPR(e->v.Subscript.value, PR_ATOM);
885
18
    APPEND_CHAR('[');
886
18
    APPEND_EXPR(e->v.Subscript.slice, PR_TUPLE);
887
18
    APPEND_CHAR_FINISH(']');
888
18
}
889
890
static int
891
append_ast_starred(PyUnicodeWriter *writer, expr_ty e)
892
56
{
893
56
    APPEND_CHAR('*');
894
56
    APPEND_EXPR(e->v.Starred.value, PR_EXPR);
895
56
    return 0;
896
56
}
897
898
static int
899
append_ast_yield(PyUnicodeWriter *writer, expr_ty e)
900
0
{
901
0
    if (!e->v.Yield.value) {
902
0
        APPEND_STR_FINISH("(yield)");
903
0
    }
904
905
0
    APPEND_STR("(yield ");
906
0
    APPEND_EXPR(e->v.Yield.value, PR_TEST);
907
0
    APPEND_CHAR_FINISH(')');
908
0
}
909
910
static int
911
append_ast_yield_from(PyUnicodeWriter *writer, expr_ty e)
912
0
{
913
0
    APPEND_STR("(yield from ");
914
0
    APPEND_EXPR(e->v.YieldFrom.value, PR_TEST);
915
0
    APPEND_CHAR_FINISH(')');
916
0
}
917
918
static int
919
append_ast_await(PyUnicodeWriter *writer, expr_ty e, int level)
920
0
{
921
0
    APPEND_STR_IF(level > PR_AWAIT, "(");
922
0
    APPEND_STR("await ");
923
0
    APPEND_EXPR(e->v.Await.value, PR_ATOM);
924
0
    APPEND_STR_IF(level > PR_AWAIT, ")");
925
0
    return 0;
926
0
}
927
928
static int
929
append_named_expr(PyUnicodeWriter *writer, expr_ty e, int level)
930
0
{
931
0
    APPEND_STR_IF(level > PR_TUPLE, "(");
932
0
    APPEND_EXPR(e->v.NamedExpr.target, PR_ATOM);
933
0
    APPEND_STR(" := ");
934
0
    APPEND_EXPR(e->v.NamedExpr.value, PR_ATOM);
935
0
    APPEND_STR_IF(level > PR_TUPLE, ")");
936
0
    return 0;
937
0
}
938
939
static int
940
append_ast_expr(PyUnicodeWriter *writer, expr_ty e, int level)
941
62.6k
{
942
62.6k
    switch (e->kind) {
943
79
    case BoolOp_kind:
944
79
        return append_ast_boolop(writer, e, level);
945
24.5k
    case BinOp_kind:
946
24.5k
        return append_ast_binop(writer, e, level);
947
8.83k
    case UnaryOp_kind:
948
8.83k
        return append_ast_unaryop(writer, e, level);
949
1
    case Lambda_kind:
950
1
        return append_ast_lambda(writer, e, level);
951
5
    case IfExp_kind:
952
5
        return append_ast_ifexp(writer, e, level);
953
4
    case Dict_kind:
954
4
        return append_ast_dict(writer, e);
955
9
    case Set_kind:
956
9
        return append_ast_set(writer, e);
957
0
    case GeneratorExp_kind:
958
0
        return append_ast_genexp(writer, e);
959
0
    case ListComp_kind:
960
0
        return append_ast_listcomp(writer, e);
961
0
    case SetComp_kind:
962
0
        return append_ast_setcomp(writer, e);
963
0
    case DictComp_kind:
964
0
        return append_ast_dictcomp(writer, e);
965
0
    case Yield_kind:
966
0
        return append_ast_yield(writer, e);
967
0
    case YieldFrom_kind:
968
0
        return append_ast_yield_from(writer, e);
969
0
    case Await_kind:
970
0
        return append_ast_await(writer, e, level);
971
680
    case Compare_kind:
972
680
        return append_ast_compare(writer, e, level);
973
12
    case Call_kind:
974
12
        return append_ast_call(writer, e);
975
25.4k
    case Constant_kind:
976
25.4k
        if (e->v.Constant.value == Py_Ellipsis) {
977
6
            APPEND_STR_FINISH("...");
978
6
        }
979
25.4k
        if (e->v.Constant.kind != NULL
980
1
            && -1 == PyUnicodeWriter_WriteStr(writer, e->v.Constant.kind)) {
981
0
            return -1;
982
0
        }
983
25.4k
        return append_ast_constant(writer, e->v.Constant.value);
984
57
    case JoinedStr_kind:
985
57
        return append_joinedstr(writer, e, false);
986
51
    case TemplateStr_kind:
987
51
        return append_templatestr(writer, e);
988
0
    case FormattedValue_kind:
989
0
        return append_formattedvalue(writer, e);
990
0
    case Interpolation_kind:
991
0
        return append_interpolation(writer, e);
992
    /* The following exprs can be assignment targets. */
993
22
    case Attribute_kind:
994
22
        return append_ast_attribute(writer, e);
995
18
    case Subscript_kind:
996
18
        return append_ast_subscript(writer, e);
997
56
    case Starred_kind:
998
56
        return append_ast_starred(writer, e);
999
15
    case Slice_kind:
1000
15
        return append_ast_slice(writer, e);
1001
2.77k
    case Name_kind:
1002
2.77k
        return PyUnicodeWriter_WriteStr(writer, e->v.Name.id);
1003
10
    case List_kind:
1004
10
        return append_ast_list(writer, e);
1005
7
    case Tuple_kind:
1006
7
        return append_ast_tuple(writer, e, level);
1007
0
    case NamedExpr_kind:
1008
0
        return append_named_expr(writer, e, level);
1009
    // No default so compiler emits a warning for unhandled cases
1010
62.6k
    }
1011
0
    PyErr_SetString(PyExc_SystemError,
1012
0
                    "unknown expression kind");
1013
0
    return -1;
1014
62.6k
}
1015
1016
static PyObject *
1017
expr_as_unicode(expr_ty e, int level)
1018
1.48k
{
1019
1.48k
    PyUnicodeWriter *writer = PyUnicodeWriter_Create(256);
1020
1.48k
    if (writer == NULL) {
1021
0
        return NULL;
1022
0
    }
1023
1024
1.48k
    if (-1 == append_ast_expr(writer, e, level)) {
1025
0
        PyUnicodeWriter_Discard(writer);
1026
0
        return NULL;
1027
0
    }
1028
1.48k
    return PyUnicodeWriter_Finish(writer);
1029
1.48k
}
1030
1031
PyObject *
1032
_PyAST_ExprAsUnicode(expr_ty e)
1033
1.43k
{
1034
1.43k
    return expr_as_unicode(e, PR_TEST);
1035
1.43k
}