Coverage Report

Created: 2025-11-24 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/ast_preprocess.c
Line
Count
Source
1
/* AST pre-processing */
2
#include "Python.h"
3
#include "pycore_ast.h"           // _PyAST_GetDocString()
4
#include "pycore_c_array.h"       // _Py_CArray_EnsureCapacity()
5
#include "pycore_format.h"        // F_LJUST
6
#include "pycore_runtime.h"       // _Py_STR()
7
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
8
9
10
/* See PEP 765 */
11
typedef struct {
12
    bool in_finally;
13
    bool in_funcdef;
14
    bool in_loop;
15
} ControlFlowInFinallyContext;
16
17
typedef struct {
18
    PyObject *filename;
19
    PyObject *module;
20
    int optimize;
21
    int ff_features;
22
    int syntax_check_only;
23
    int enable_warnings;
24
25
    _Py_c_array_t cf_finally;       /* context for PEP 765 check */
26
    int cf_finally_used;
27
} _PyASTPreprocessState;
28
29
629k
#define ENTER_RECURSIVE() \
30
629k
if (Py_EnterRecursiveCall(" during compilation")) { \
31
0
    return 0; \
32
0
}
33
34
629k
#define LEAVE_RECURSIVE() Py_LeaveRecursiveCall();
35
36
static ControlFlowInFinallyContext*
37
get_cf_finally_top(_PyASTPreprocessState *state)
38
20.1k
{
39
20.1k
    int idx = state->cf_finally_used;
40
20.1k
    return ((ControlFlowInFinallyContext*)state->cf_finally.array) + idx;
41
20.1k
}
42
43
static int
44
push_cf_context(_PyASTPreprocessState *state, stmt_ty node, bool finally, bool funcdef, bool loop)
45
12.5k
{
46
12.5k
    if (_Py_CArray_EnsureCapacity(&state->cf_finally, state->cf_finally_used+1) < 0) {
47
0
        return 0;
48
0
    }
49
50
12.5k
    state->cf_finally_used++;
51
12.5k
    ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
52
53
12.5k
    ctx->in_finally = finally;
54
12.5k
    ctx->in_funcdef = funcdef;
55
12.5k
    ctx->in_loop = loop;
56
12.5k
    return 1;
57
12.5k
}
58
59
static void
60
pop_cf_context(_PyASTPreprocessState *state)
61
12.5k
{
62
12.5k
    assert(state->cf_finally_used > 0);
63
12.5k
    state->cf_finally_used--;
64
12.5k
}
65
66
static int
67
control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTPreprocessState *state)
68
0
{
69
0
    PyObject *msg = PyUnicode_FromFormat("'%s' in a 'finally' block", kw);
70
0
    if (msg == NULL) {
71
0
        return 0;
72
0
    }
73
0
    int ret = _PyErr_EmitSyntaxWarning(msg, state->filename, n->lineno,
74
0
                                       n->col_offset + 1, n->end_lineno,
75
0
                                       n->end_col_offset + 1,
76
0
                                       state->module);
77
0
    Py_DECREF(msg);
78
0
    return ret < 0 ? 0 : 1;
79
0
}
80
81
static int
82
before_return(_PyASTPreprocessState *state, stmt_ty node_)
83
7.28k
{
84
7.28k
    if (state->enable_warnings && state->cf_finally_used > 0) {
85
6.79k
        ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
86
6.79k
        if (ctx->in_finally && ! ctx->in_funcdef) {
87
0
            if (!control_flow_in_finally_warning("return", node_, state)) {
88
0
                return 0;
89
0
            }
90
0
        }
91
6.79k
    }
92
7.28k
    return 1;
93
7.28k
}
94
95
static int
96
before_loop_exit(_PyASTPreprocessState *state, stmt_ty node_, const char *kw)
97
1.26k
{
98
1.26k
    if (state->enable_warnings && state->cf_finally_used > 0) {
99
769
        ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
100
769
        if (ctx->in_finally && ! ctx->in_loop) {
101
0
            if (!control_flow_in_finally_warning(kw, node_, state)) {
102
0
                return 0;
103
0
            }
104
0
        }
105
769
    }
106
1.26k
    return 1;
107
1.26k
}
108
109
#define PUSH_CONTEXT(S, N, FINALLY, FUNCDEF, LOOP) \
110
12.5k
    if (!push_cf_context((S), (N), (FINALLY), (FUNCDEF), (LOOP))) { \
111
0
        return 0; \
112
0
    }
113
114
12.5k
#define POP_CONTEXT(S) pop_cf_context(S)
115
116
2.22k
#define BEFORE_FINALLY(S, N)    PUSH_CONTEXT((S), (N), true, false, false)
117
2.22k
#define AFTER_FINALLY(S)        POP_CONTEXT(S)
118
7.97k
#define BEFORE_FUNC_BODY(S, N)  PUSH_CONTEXT((S), (N), false, true, false)
119
7.97k
#define AFTER_FUNC_BODY(S)      POP_CONTEXT(S)
120
2.37k
#define BEFORE_LOOP_BODY(S, N)  PUSH_CONTEXT((S), (N), false, false, true)
121
2.37k
#define AFTER_LOOP_BODY(S)      POP_CONTEXT(S)
122
123
#define BEFORE_RETURN(S, N) \
124
7.28k
    if (!before_return((S), (N))) { \
125
0
        return 0; \
126
0
    }
127
128
#define BEFORE_LOOP_EXIT(S, N, KW) \
129
1.26k
    if (!before_loop_exit((S), (N), (KW))) { \
130
0
        return 0; \
131
0
    }
132
133
static int
134
make_const(expr_ty node, PyObject *val, PyArena *arena)
135
0
{
136
    // Even if no new value was calculated, make_const may still
137
    // need to clear an error (e.g. for division by zero)
138
0
    if (val == NULL) {
139
0
        if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
140
0
            return 0;
141
0
        }
142
0
        PyErr_Clear();
143
0
        return 1;
144
0
    }
145
0
    if (_PyArena_AddPyObject(arena, val) < 0) {
146
0
        Py_DECREF(val);
147
0
        return 0;
148
0
    }
149
0
    node->kind = Constant_kind;
150
0
    node->v.Constant.kind = NULL;
151
0
    node->v.Constant.value = val;
152
0
    return 1;
153
0
}
154
155
525
#define COPY_NODE(TO, FROM) (memcpy((TO), (FROM), sizeof(struct _expr)))
156
157
static int
158
has_starred(asdl_expr_seq *elts)
159
656
{
160
656
    Py_ssize_t n = asdl_seq_LEN(elts);
161
2.00k
    for (Py_ssize_t i = 0; i < n; i++) {
162
1.35k
        expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
163
1.35k
        if (e->kind == Starred_kind) {
164
0
            return 1;
165
0
        }
166
1.35k
    }
167
656
    return 0;
168
656
}
169
170
static expr_ty
171
parse_literal(PyObject *fmt, Py_ssize_t *ppos, PyArena *arena)
172
1.74k
{
173
1.74k
    const void *data = PyUnicode_DATA(fmt);
174
1.74k
    int kind = PyUnicode_KIND(fmt);
175
1.74k
    Py_ssize_t size = PyUnicode_GET_LENGTH(fmt);
176
1.74k
    Py_ssize_t start, pos;
177
1.74k
    int has_percents = 0;
178
1.74k
    start = pos = *ppos;
179
12.6k
    while (pos < size) {
180
12.1k
        if (PyUnicode_READ(kind, data, pos) != '%') {
181
10.9k
            pos++;
182
10.9k
        }
183
1.22k
        else if (pos+1 < size && PyUnicode_READ(kind, data, pos+1) == '%') {
184
4
            has_percents = 1;
185
4
            pos += 2;
186
4
        }
187
1.22k
        else {
188
1.22k
            break;
189
1.22k
        }
190
12.1k
    }
191
1.74k
    *ppos = pos;
192
1.74k
    if (pos == start) {
193
730
        return NULL;
194
730
    }
195
1.01k
    PyObject *str = PyUnicode_Substring(fmt, start, pos);
196
    /* str = str.replace('%%', '%') */
197
1.01k
    if (str && has_percents) {
198
3
        _Py_DECLARE_STR(dbl_percent, "%%");
199
3
        Py_SETREF(str, PyUnicode_Replace(str, &_Py_STR(dbl_percent),
200
3
                                         _Py_LATIN1_CHR('%'), -1));
201
3
    }
202
1.01k
    if (!str) {
203
0
        return NULL;
204
0
    }
205
206
1.01k
    if (_PyArena_AddPyObject(arena, str) < 0) {
207
0
        Py_DECREF(str);
208
0
        return NULL;
209
0
    }
210
1.01k
    return _PyAST_Constant(str, NULL, -1, -1, -1, -1, arena);
211
1.01k
}
212
213
66
#define MAXDIGITS 3
214
215
static int
216
simple_format_arg_parse(PyObject *fmt, Py_ssize_t *ppos,
217
                        int *spec, int *flags, int *width, int *prec)
218
1.22k
{
219
1.22k
    Py_ssize_t pos = *ppos, len = PyUnicode_GET_LENGTH(fmt);
220
1.22k
    Py_UCS4 ch;
221
222
1.34k
#define NEXTC do {                      \
223
1.34k
    if (pos >= len) {                   \
224
0
        return 0;                       \
225
0
    }                                   \
226
1.34k
    ch = PyUnicode_READ_CHAR(fmt, pos); \
227
1.34k
    pos++;                              \
228
1.34k
} while (0)
229
230
1.22k
    *flags = 0;
231
1.28k
    while (1) {
232
1.28k
        NEXTC;
233
1.28k
        switch (ch) {
234
9
            case '-': *flags |= F_LJUST; continue;
235
0
            case '+': *flags |= F_SIGN; continue;
236
0
            case ' ': *flags |= F_BLANK; continue;
237
28
            case '#': *flags |= F_ALT; continue;
238
26
            case '0': *flags |= F_ZERO; continue;
239
1.28k
        }
240
1.22k
        break;
241
1.28k
    }
242
1.22k
    if ('0' <= ch && ch <= '9') {
243
34
        *width = 0;
244
34
        int digits = 0;
245
83
        while ('0' <= ch && ch <= '9') {
246
52
            *width = *width * 10 + (ch - '0');
247
52
            NEXTC;
248
52
            if (++digits >= MAXDIGITS) {
249
3
                return 0;
250
3
            }
251
52
        }
252
34
    }
253
254
1.22k
    if (ch == '.') {
255
1
        NEXTC;
256
1
        *prec = 0;
257
1
        if ('0' <= ch && ch <= '9') {
258
1
            int digits = 0;
259
3
            while ('0' <= ch && ch <= '9') {
260
2
                *prec = *prec * 10 + (ch - '0');
261
2
                NEXTC;
262
2
                if (++digits >= MAXDIGITS) {
263
0
                    return 0;
264
0
                }
265
2
            }
266
1
        }
267
1
    }
268
1.22k
    *spec = ch;
269
1.22k
    *ppos = pos;
270
1.22k
    return 1;
271
272
1.22k
#undef NEXTC
273
1.22k
}
274
275
static expr_ty
276
parse_format(PyObject *fmt, Py_ssize_t *ppos, expr_ty arg, PyArena *arena)
277
1.22k
{
278
1.22k
    int spec, flags, width = -1, prec = -1;
279
1.22k
    if (!simple_format_arg_parse(fmt, ppos, &spec, &flags, &width, &prec)) {
280
        // Unsupported format.
281
3
        return NULL;
282
3
    }
283
1.22k
    if (spec == 's' || spec == 'r' || spec == 'a') {
284
1.09k
        char buf[1 + MAXDIGITS + 1 + MAXDIGITS + 1], *p = buf;
285
1.09k
        if (!(flags & F_LJUST) && width > 0) {
286
2
            *p++ = '>';
287
2
        }
288
1.09k
        if (width >= 0) {
289
11
            p += snprintf(p, MAXDIGITS + 1, "%d", width);
290
11
        }
291
1.09k
        if (prec >= 0) {
292
1
            p += snprintf(p, MAXDIGITS + 2, ".%d", prec);
293
1
        }
294
1.09k
        expr_ty format_spec = NULL;
295
1.09k
        if (p != buf) {
296
12
            PyObject *str = PyUnicode_FromString(buf);
297
12
            if (str == NULL) {
298
0
                return NULL;
299
0
            }
300
12
            if (_PyArena_AddPyObject(arena, str) < 0) {
301
0
                Py_DECREF(str);
302
0
                return NULL;
303
0
            }
304
12
            format_spec = _PyAST_Constant(str, NULL, -1, -1, -1, -1, arena);
305
12
            if (format_spec == NULL) {
306
0
                return NULL;
307
0
            }
308
12
        }
309
1.09k
        return _PyAST_FormattedValue(arg, spec, format_spec,
310
1.09k
                                     arg->lineno, arg->col_offset,
311
1.09k
                                     arg->end_lineno, arg->end_col_offset,
312
1.09k
                                     arena);
313
1.09k
    }
314
    // Unsupported format.
315
128
    return NULL;
316
1.22k
}
317
318
static int
319
optimize_format(expr_ty node, PyObject *fmt, asdl_expr_seq *elts, PyArena *arena)
320
656
{
321
656
    Py_ssize_t pos = 0;
322
656
    Py_ssize_t cnt = 0;
323
656
    asdl_expr_seq *seq = _Py_asdl_expr_seq_new(asdl_seq_LEN(elts) * 2 + 1, arena);
324
656
    if (!seq) {
325
0
        return 0;
326
0
    }
327
656
    seq->size = 0;
328
329
1.74k
    while (1) {
330
1.74k
        expr_ty lit = parse_literal(fmt, &pos, arena);
331
1.74k
        if (lit) {
332
1.01k
            asdl_seq_SET(seq, seq->size++, lit);
333
1.01k
        }
334
730
        else if (PyErr_Occurred()) {
335
0
            return 0;
336
0
        }
337
338
1.74k
        if (pos >= PyUnicode_GET_LENGTH(fmt)) {
339
525
            break;
340
525
        }
341
1.22k
        if (cnt >= asdl_seq_LEN(elts)) {
342
            // More format units than items.
343
0
            return 1;
344
0
        }
345
1.22k
        assert(PyUnicode_READ_CHAR(fmt, pos) == '%');
346
1.22k
        pos++;
347
1.22k
        expr_ty expr = parse_format(fmt, &pos, asdl_seq_GET(elts, cnt), arena);
348
1.22k
        cnt++;
349
1.22k
        if (!expr) {
350
131
            return !PyErr_Occurred();
351
131
        }
352
1.09k
        asdl_seq_SET(seq, seq->size++, expr);
353
1.09k
    }
354
525
    if (cnt < asdl_seq_LEN(elts)) {
355
        // More items than format units.
356
0
        return 1;
357
0
    }
358
525
    expr_ty res = _PyAST_JoinedStr(seq,
359
525
                                   node->lineno, node->col_offset,
360
525
                                   node->end_lineno, node->end_col_offset,
361
525
                                   arena);
362
525
    if (!res) {
363
0
        return 0;
364
0
    }
365
525
    COPY_NODE(node, res);
366
//     PySys_FormatStderr("format = %R\n", fmt);
367
525
    return 1;
368
525
}
369
370
static int
371
fold_binop(expr_ty node, PyArena *arena, _PyASTPreprocessState *state)
372
27.9k
{
373
27.9k
    if (state->syntax_check_only) {
374
22.0k
        return 1;
375
22.0k
    }
376
5.88k
    expr_ty lhs, rhs;
377
5.88k
    lhs = node->v.BinOp.left;
378
5.88k
    rhs = node->v.BinOp.right;
379
5.88k
    if (lhs->kind != Constant_kind) {
380
4.10k
        return 1;
381
4.10k
    }
382
1.78k
    PyObject *lv = lhs->v.Constant.value;
383
384
1.78k
    if (node->v.BinOp.op == Mod &&
385
1.23k
        rhs->kind == Tuple_kind &&
386
1.78k
        PyUnicode_Check(lv) &&
387
656
        !has_starred(rhs->v.Tuple.elts))
388
656
    {
389
656
        return optimize_format(node, lv, rhs->v.Tuple.elts, arena);
390
656
    }
391
392
1.13k
    return 1;
393
1.78k
}
394
395
static int astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
396
static int astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
397
static int astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
398
static int astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
399
static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
400
static int astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
401
static int astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
402
static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
403
static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
404
static int astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
405
static int astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
406
static int astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
407
408
#define CALL(FUNC, TYPE, ARG) \
409
367k
    if (!FUNC((ARG), ctx_, state)) \
410
367k
        return 0;
411
412
#define CALL_OPT(FUNC, TYPE, ARG) \
413
103k
    if ((ARG) != NULL && !FUNC((ARG), ctx_, state)) \
414
103k
        return 0;
415
416
263k
#define CALL_SEQ(FUNC, TYPE, ARG) { \
417
263k
    Py_ssize_t i; \
418
263k
    asdl_ ## TYPE ## _seq *seq = (ARG); /* avoid variable capture */ \
419
599k
    for (i = 0; i < asdl_seq_LEN(seq); i++) { \
420
336k
        TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
421
336k
        if (elt != NULL && !FUNC(elt, ctx_, state)) \
422
336k
            return 0; \
423
336k
    } \
424
263k
}
425
426
427
static int
428
stmt_seq_remove_item(asdl_stmt_seq *stmts, Py_ssize_t idx)
429
0
{
430
0
    if (idx >= asdl_seq_LEN(stmts)) {
431
0
        return 0;
432
0
    }
433
0
    for (Py_ssize_t i = idx; i < asdl_seq_LEN(stmts) - 1; i++) {
434
0
        stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, i+1);
435
0
        asdl_seq_SET(stmts, i, st);
436
0
    }
437
0
    stmts->size--;
438
0
    return 1;
439
0
}
440
441
static int
442
remove_docstring(asdl_stmt_seq *stmts, Py_ssize_t idx, PyArena *ctx_)
443
0
{
444
0
    assert(_PyAST_GetDocString(stmts) != NULL);
445
    // In case there's just the docstring in the body, replace it with `pass`
446
    // keyword, so body won't be empty.
447
0
    if (asdl_seq_LEN(stmts) == 1) {
448
0
        stmt_ty docstring = (stmt_ty)asdl_seq_GET(stmts, 0);
449
0
        stmt_ty pass = _PyAST_Pass(
450
0
            docstring->lineno, docstring->col_offset,
451
            // we know that `pass` always takes 4 chars and a single line,
452
            // while docstring can span on multiple lines
453
0
            docstring->lineno, docstring->col_offset + 4,
454
0
            ctx_
455
0
        );
456
0
        if (pass == NULL) {
457
0
            return 0;
458
0
        }
459
0
        asdl_seq_SET(stmts, 0, pass);
460
0
        return 1;
461
0
    }
462
    // In case there are more than 1 body items, just remove the docstring.
463
0
    return stmt_seq_remove_item(stmts, idx);
464
0
}
465
466
static int
467
astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTPreprocessState *state)
468
16.2k
{
469
16.2k
    int docstring = _PyAST_GetDocString(stmts) != NULL;
470
16.2k
    if (docstring && (state->optimize >= 2)) {
471
        /* remove the docstring */
472
0
        if (!remove_docstring(stmts, 0, ctx_)) {
473
0
            return 0;
474
0
        }
475
0
        docstring = 0;
476
0
    }
477
16.2k
    CALL_SEQ(astfold_stmt, stmt, stmts);
478
16.2k
    if (!docstring && _PyAST_GetDocString(stmts) != NULL) {
479
0
        stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
480
0
        asdl_expr_seq *values = _Py_asdl_expr_seq_new(1, ctx_);
481
0
        if (!values) {
482
0
            return 0;
483
0
        }
484
0
        asdl_seq_SET(values, 0, st->v.Expr.value);
485
0
        expr_ty expr = _PyAST_JoinedStr(values, st->lineno, st->col_offset,
486
0
                                        st->end_lineno, st->end_col_offset,
487
0
                                        ctx_);
488
0
        if (!expr) {
489
0
            return 0;
490
0
        }
491
0
        st->v.Expr.value = expr;
492
0
    }
493
16.2k
    return 1;
494
16.2k
}
495
496
static int
497
astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
498
6.51k
{
499
6.51k
    switch (node_->kind) {
500
6.43k
    case Module_kind:
501
6.43k
        CALL(astfold_body, asdl_seq, node_->v.Module.body);
502
6.43k
        break;
503
0
    case Interactive_kind:
504
0
        CALL_SEQ(astfold_stmt, stmt, node_->v.Interactive.body);
505
0
        break;
506
85
    case Expression_kind:
507
85
        CALL(astfold_expr, expr_ty, node_->v.Expression.body);
508
85
        break;
509
    // The following top level nodes don't participate in constant folding
510
0
    case FunctionType_kind:
511
0
        break;
512
    // No default case, so the compiler will emit a warning if new top level
513
    // compilation nodes are added without being handled here
514
6.51k
    }
515
6.51k
    return 1;
516
6.51k
}
517
518
static int
519
astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
520
509k
{
521
509k
    ENTER_RECURSIVE();
522
509k
    switch (node_->kind) {
523
2.90k
    case BoolOp_kind:
524
2.90k
        CALL_SEQ(astfold_expr, expr, node_->v.BoolOp.values);
525
2.90k
        break;
526
27.9k
    case BinOp_kind:
527
27.9k
        CALL(astfold_expr, expr_ty, node_->v.BinOp.left);
528
27.9k
        CALL(astfold_expr, expr_ty, node_->v.BinOp.right);
529
27.9k
        CALL(fold_binop, expr_ty, node_);
530
27.9k
        break;
531
56.6k
    case UnaryOp_kind:
532
56.6k
        CALL(astfold_expr, expr_ty, node_->v.UnaryOp.operand);
533
56.6k
        break;
534
1.10k
    case Lambda_kind:
535
1.10k
        CALL(astfold_arguments, arguments_ty, node_->v.Lambda.args);
536
1.10k
        CALL(astfold_expr, expr_ty, node_->v.Lambda.body);
537
1.10k
        break;
538
494
    case IfExp_kind:
539
494
        CALL(astfold_expr, expr_ty, node_->v.IfExp.test);
540
494
        CALL(astfold_expr, expr_ty, node_->v.IfExp.body);
541
494
        CALL(astfold_expr, expr_ty, node_->v.IfExp.orelse);
542
494
        break;
543
1.88k
    case Dict_kind:
544
1.88k
        CALL_SEQ(astfold_expr, expr, node_->v.Dict.keys);
545
1.88k
        CALL_SEQ(astfold_expr, expr, node_->v.Dict.values);
546
1.88k
        break;
547
519
    case Set_kind:
548
519
        CALL_SEQ(astfold_expr, expr, node_->v.Set.elts);
549
519
        break;
550
563
    case ListComp_kind:
551
563
        CALL(astfold_expr, expr_ty, node_->v.ListComp.elt);
552
563
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.ListComp.generators);
553
563
        break;
554
174
    case SetComp_kind:
555
174
        CALL(astfold_expr, expr_ty, node_->v.SetComp.elt);
556
174
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.SetComp.generators);
557
174
        break;
558
711
    case DictComp_kind:
559
711
        CALL(astfold_expr, expr_ty, node_->v.DictComp.key);
560
711
        CALL(astfold_expr, expr_ty, node_->v.DictComp.value);
561
711
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.DictComp.generators);
562
711
        break;
563
699
    case GeneratorExp_kind:
564
699
        CALL(astfold_expr, expr_ty, node_->v.GeneratorExp.elt);
565
699
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.GeneratorExp.generators);
566
699
        break;
567
220
    case Await_kind:
568
220
        CALL(astfold_expr, expr_ty, node_->v.Await.value);
569
220
        break;
570
818
    case Yield_kind:
571
818
        CALL_OPT(astfold_expr, expr_ty, node_->v.Yield.value);
572
818
        break;
573
110
    case YieldFrom_kind:
574
110
        CALL(astfold_expr, expr_ty, node_->v.YieldFrom.value);
575
110
        break;
576
10.4k
    case Compare_kind:
577
10.4k
        CALL(astfold_expr, expr_ty, node_->v.Compare.left);
578
10.4k
        CALL_SEQ(astfold_expr, expr, node_->v.Compare.comparators);
579
10.4k
        break;
580
33.4k
    case Call_kind:
581
33.4k
        CALL(astfold_expr, expr_ty, node_->v.Call.func);
582
33.4k
        CALL_SEQ(astfold_expr, expr, node_->v.Call.args);
583
33.4k
        CALL_SEQ(astfold_keyword, keyword, node_->v.Call.keywords);
584
33.4k
        break;
585
12.1k
    case FormattedValue_kind:
586
12.1k
        CALL(astfold_expr, expr_ty, node_->v.FormattedValue.value);
587
12.1k
        CALL_OPT(astfold_expr, expr_ty, node_->v.FormattedValue.format_spec);
588
12.1k
        break;
589
1.53k
    case Interpolation_kind:
590
1.53k
        CALL(astfold_expr, expr_ty, node_->v.Interpolation.value);
591
1.53k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Interpolation.format_spec);
592
1.53k
        break;
593
7.10k
    case JoinedStr_kind:
594
7.10k
        CALL_SEQ(astfold_expr, expr, node_->v.JoinedStr.values);
595
7.10k
        break;
596
290
    case TemplateStr_kind:
597
290
        CALL_SEQ(astfold_expr, expr, node_->v.TemplateStr.values);
598
290
        break;
599
31.1k
    case Attribute_kind:
600
31.1k
        CALL(astfold_expr, expr_ty, node_->v.Attribute.value);
601
31.1k
        break;
602
5.82k
    case Subscript_kind:
603
5.82k
        CALL(astfold_expr, expr_ty, node_->v.Subscript.value);
604
5.82k
        CALL(astfold_expr, expr_ty, node_->v.Subscript.slice);
605
5.82k
        break;
606
982
    case Starred_kind:
607
982
        CALL(astfold_expr, expr_ty, node_->v.Starred.value);
608
982
        break;
609
3.41k
    case Slice_kind:
610
3.41k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.lower);
611
3.41k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.upper);
612
3.41k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step);
613
3.41k
        break;
614
3.00k
    case List_kind:
615
3.00k
        CALL_SEQ(astfold_expr, expr, node_->v.List.elts);
616
3.00k
        break;
617
12.3k
    case Tuple_kind:
618
12.3k
        CALL_SEQ(astfold_expr, expr, node_->v.Tuple.elts);
619
12.3k
        break;
620
176k
    case Name_kind:
621
176k
        if (state->syntax_check_only) {
622
60.8k
            break;
623
60.8k
        }
624
115k
        if (node_->v.Name.ctx == Load &&
625
95.3k
                _PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) {
626
0
            LEAVE_RECURSIVE();
627
0
            return make_const(node_, PyBool_FromLong(!state->optimize), ctx_);
628
0
        }
629
115k
        break;
630
115k
    case NamedExpr_kind:
631
314
        CALL(astfold_expr, expr_ty, node_->v.NamedExpr.value);
632
314
        break;
633
117k
    case Constant_kind:
634
        // Already a constant, nothing further to do
635
117k
        break;
636
    // No default case, so the compiler will emit a warning if new expression
637
    // kinds are added without being handled here
638
509k
    }
639
509k
    LEAVE_RECURSIVE();
640
509k
    return 1;
641
509k
}
642
643
static int
644
astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
645
5.78k
{
646
5.78k
    CALL(astfold_expr, expr_ty, node_->value);
647
5.78k
    return 1;
648
5.78k
}
649
650
static int
651
astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
652
2.28k
{
653
2.28k
    CALL(astfold_expr, expr_ty, node_->target);
654
2.28k
    CALL(astfold_expr, expr_ty, node_->iter);
655
2.28k
    CALL_SEQ(astfold_expr, expr, node_->ifs);
656
2.28k
    return 1;
657
2.28k
}
658
659
static int
660
astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
661
9.08k
{
662
9.08k
    CALL_SEQ(astfold_arg, arg, node_->posonlyargs);
663
9.08k
    CALL_SEQ(astfold_arg, arg, node_->args);
664
9.08k
    CALL_OPT(astfold_arg, arg_ty, node_->vararg);
665
9.08k
    CALL_SEQ(astfold_arg, arg, node_->kwonlyargs);
666
9.08k
    CALL_SEQ(astfold_expr, expr, node_->kw_defaults);
667
9.08k
    CALL_OPT(astfold_arg, arg_ty, node_->kwarg);
668
9.08k
    CALL_SEQ(astfold_expr, expr, node_->defaults);
669
9.08k
    return 1;
670
9.08k
}
671
672
static int
673
astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
674
21.8k
{
675
21.8k
    if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
676
20.9k
        CALL_OPT(astfold_expr, expr_ty, node_->annotation);
677
20.9k
    }
678
21.8k
    return 1;
679
21.8k
}
680
681
static int
682
astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
683
104k
{
684
104k
    ENTER_RECURSIVE();
685
104k
    switch (node_->kind) {
686
7.78k
    case FunctionDef_kind: {
687
7.78k
        CALL_SEQ(astfold_type_param, type_param, node_->v.FunctionDef.type_params);
688
7.78k
        CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args);
689
7.78k
        BEFORE_FUNC_BODY(state, node_);
690
7.78k
        CALL(astfold_body, asdl_seq, node_->v.FunctionDef.body);
691
7.78k
        AFTER_FUNC_BODY(state);
692
7.78k
        CALL_SEQ(astfold_expr, expr, node_->v.FunctionDef.decorator_list);
693
7.78k
        if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
694
7.49k
            CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns);
695
7.49k
        }
696
7.78k
        break;
697
7.78k
    }
698
7.78k
    case AsyncFunctionDef_kind: {
699
193
        CALL_SEQ(astfold_type_param, type_param, node_->v.AsyncFunctionDef.type_params);
700
193
        CALL(astfold_arguments, arguments_ty, node_->v.AsyncFunctionDef.args);
701
193
        BEFORE_FUNC_BODY(state, node_);
702
193
        CALL(astfold_body, asdl_seq, node_->v.AsyncFunctionDef.body);
703
193
        AFTER_FUNC_BODY(state);
704
193
        CALL_SEQ(astfold_expr, expr, node_->v.AsyncFunctionDef.decorator_list);
705
193
        if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
706
175
            CALL_OPT(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.returns);
707
175
        }
708
193
        break;
709
193
    }
710
1.84k
    case ClassDef_kind:
711
1.84k
        CALL_SEQ(astfold_type_param, type_param, node_->v.ClassDef.type_params);
712
1.84k
        CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.bases);
713
1.84k
        CALL_SEQ(astfold_keyword, keyword, node_->v.ClassDef.keywords);
714
1.84k
        CALL(astfold_body, asdl_seq, node_->v.ClassDef.body);
715
1.84k
        CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.decorator_list);
716
1.84k
        break;
717
7.28k
    case Return_kind:
718
7.28k
        BEFORE_RETURN(state, node_);
719
7.28k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Return.value);
720
7.28k
        break;
721
619
    case Delete_kind:
722
619
        CALL_SEQ(astfold_expr, expr, node_->v.Delete.targets);
723
619
        break;
724
18.8k
    case Assign_kind:
725
18.8k
        CALL_SEQ(astfold_expr, expr, node_->v.Assign.targets);
726
18.8k
        CALL(astfold_expr, expr_ty, node_->v.Assign.value);
727
18.8k
        break;
728
1.25k
    case AugAssign_kind:
729
1.25k
        CALL(astfold_expr, expr_ty, node_->v.AugAssign.target);
730
1.25k
        CALL(astfold_expr, expr_ty, node_->v.AugAssign.value);
731
1.25k
        break;
732
1.14k
    case AnnAssign_kind:
733
1.14k
        CALL(astfold_expr, expr_ty, node_->v.AnnAssign.target);
734
1.14k
        if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
735
1.05k
            CALL(astfold_expr, expr_ty, node_->v.AnnAssign.annotation);
736
1.05k
        }
737
1.14k
        CALL_OPT(astfold_expr, expr_ty, node_->v.AnnAssign.value);
738
1.14k
        break;
739
75
    case TypeAlias_kind:
740
75
        CALL(astfold_expr, expr_ty, node_->v.TypeAlias.name);
741
75
        CALL_SEQ(astfold_type_param, type_param, node_->v.TypeAlias.type_params);
742
75
        CALL(astfold_expr, expr_ty, node_->v.TypeAlias.value);
743
75
        break;
744
1.59k
    case For_kind: {
745
1.59k
        CALL(astfold_expr, expr_ty, node_->v.For.target);
746
1.59k
        CALL(astfold_expr, expr_ty, node_->v.For.iter);
747
1.59k
        BEFORE_LOOP_BODY(state, node_);
748
1.59k
        CALL_SEQ(astfold_stmt, stmt, node_->v.For.body);
749
1.59k
        AFTER_LOOP_BODY(state);
750
1.59k
        CALL_SEQ(astfold_stmt, stmt, node_->v.For.orelse);
751
1.59k
        break;
752
1.59k
    }
753
72
    case AsyncFor_kind: {
754
72
        CALL(astfold_expr, expr_ty, node_->v.AsyncFor.target);
755
72
        CALL(astfold_expr, expr_ty, node_->v.AsyncFor.iter);
756
72
        BEFORE_LOOP_BODY(state, node_);
757
72
        CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.body);
758
72
        AFTER_LOOP_BODY(state);
759
72
        CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.orelse);
760
72
        break;
761
72
    }
762
706
    case While_kind: {
763
706
        CALL(astfold_expr, expr_ty, node_->v.While.test);
764
706
        BEFORE_LOOP_BODY(state, node_);
765
706
        CALL_SEQ(astfold_stmt, stmt, node_->v.While.body);
766
706
        AFTER_LOOP_BODY(state);
767
706
        CALL_SEQ(astfold_stmt, stmt, node_->v.While.orelse);
768
706
        break;
769
706
    }
770
11.2k
    case If_kind:
771
11.2k
        CALL(astfold_expr, expr_ty, node_->v.If.test);
772
11.2k
        CALL_SEQ(astfold_stmt, stmt, node_->v.If.body);
773
11.2k
        CALL_SEQ(astfold_stmt, stmt, node_->v.If.orelse);
774
11.2k
        break;
775
349
    case With_kind:
776
349
        CALL_SEQ(astfold_withitem, withitem, node_->v.With.items);
777
349
        CALL_SEQ(astfold_stmt, stmt, node_->v.With.body);
778
349
        break;
779
222
    case AsyncWith_kind:
780
222
        CALL_SEQ(astfold_withitem, withitem, node_->v.AsyncWith.items);
781
222
        CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncWith.body);
782
222
        break;
783
3.29k
    case Raise_kind:
784
3.29k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.exc);
785
3.29k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.cause);
786
3.29k
        break;
787
1.79k
    case Try_kind: {
788
1.79k
        CALL_SEQ(astfold_stmt, stmt, node_->v.Try.body);
789
1.79k
        CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.Try.handlers);
790
1.79k
        CALL_SEQ(astfold_stmt, stmt, node_->v.Try.orelse);
791
1.79k
        BEFORE_FINALLY(state, node_);
792
1.79k
        CALL_SEQ(astfold_stmt, stmt, node_->v.Try.finalbody);
793
1.79k
        AFTER_FINALLY(state);
794
1.79k
        break;
795
1.79k
    }
796
431
    case TryStar_kind: {
797
431
        CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.body);
798
431
        CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.TryStar.handlers);
799
431
        CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.orelse);
800
431
        BEFORE_FINALLY(state, node_);
801
431
        CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.finalbody);
802
431
        AFTER_FINALLY(state);
803
431
        break;
804
431
    }
805
502
    case Assert_kind:
806
502
        CALL(astfold_expr, expr_ty, node_->v.Assert.test);
807
502
        CALL_OPT(astfold_expr, expr_ty, node_->v.Assert.msg);
808
502
        break;
809
39.7k
    case Expr_kind:
810
39.7k
        CALL(astfold_expr, expr_ty, node_->v.Expr.value);
811
39.7k
        break;
812
274
    case Match_kind:
813
274
        CALL(astfold_expr, expr_ty, node_->v.Match.subject);
814
274
        CALL_SEQ(astfold_match_case, match_case, node_->v.Match.cases);
815
274
        break;
816
734
    case Break_kind:
817
734
        BEFORE_LOOP_EXIT(state, node_, "break");
818
734
        break;
819
533
    case Continue_kind:
820
533
        BEFORE_LOOP_EXIT(state, node_, "continue");
821
533
        break;
822
    // The following statements don't contain any subexpressions to be folded
823
1.59k
    case Import_kind:
824
2.95k
    case ImportFrom_kind:
825
3.30k
    case Global_kind:
826
3.51k
    case Nonlocal_kind:
827
4.14k
    case Pass_kind:
828
4.14k
        break;
829
    // No default case, so the compiler will emit a warning if new statement
830
    // kinds are added without being handled here
831
104k
    }
832
104k
    LEAVE_RECURSIVE();
833
104k
    return 1;
834
104k
}
835
836
static int
837
astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
838
2.66k
{
839
2.66k
    switch (node_->kind) {
840
2.66k
    case ExceptHandler_kind:
841
2.66k
        CALL_OPT(astfold_expr, expr_ty, node_->v.ExceptHandler.type);
842
2.66k
        CALL_SEQ(astfold_stmt, stmt, node_->v.ExceptHandler.body);
843
2.66k
        break;
844
    // No default case, so the compiler will emit a warning if new handler
845
    // kinds are added without being handled here
846
2.66k
    }
847
2.66k
    return 1;
848
2.66k
}
849
850
static int
851
astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
852
1.84k
{
853
1.84k
    CALL(astfold_expr, expr_ty, node_->context_expr);
854
1.84k
    CALL_OPT(astfold_expr, expr_ty, node_->optional_vars);
855
1.84k
    return 1;
856
1.84k
}
857
858
static int
859
fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTPreprocessState *state)
860
674
{
861
674
    if (state->syntax_check_only) {
862
658
        return 1;
863
658
    }
864
16
    switch (node->kind)
865
16
    {
866
0
        case UnaryOp_kind:
867
0
        {
868
0
            if (node->v.UnaryOp.op == USub &&
869
0
                node->v.UnaryOp.operand->kind == Constant_kind)
870
0
            {
871
0
                PyObject *operand = node->v.UnaryOp.operand->v.Constant.value;
872
0
                PyObject *folded = PyNumber_Negative(operand);
873
0
                return make_const(node, folded, ctx_);
874
0
            }
875
0
            break;
876
0
        }
877
0
        case BinOp_kind:
878
0
        {
879
0
            operator_ty op = node->v.BinOp.op;
880
0
            if ((op == Add || op == Sub) &&
881
0
                node->v.BinOp.right->kind == Constant_kind)
882
0
            {
883
0
                CALL(fold_const_match_patterns, expr_ty, node->v.BinOp.left);
884
0
                if (node->v.BinOp.left->kind == Constant_kind) {
885
0
                    PyObject *left = node->v.BinOp.left->v.Constant.value;
886
0
                    PyObject *right = node->v.BinOp.right->v.Constant.value;
887
0
                    PyObject *folded = op == Add ? PyNumber_Add(left, right) : PyNumber_Subtract(left, right);
888
0
                    return make_const(node, folded, ctx_);
889
0
                }
890
0
            }
891
0
            break;
892
0
        }
893
16
        default:
894
16
            break;
895
16
    }
896
16
    return 1;
897
16
}
898
899
static int
900
astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
901
14.8k
{
902
    // Currently, this is really only used to form complex/negative numeric
903
    // constants in MatchValue and MatchMapping nodes
904
    // We still recurse into all subexpressions and subpatterns anyway
905
14.8k
    ENTER_RECURSIVE();
906
14.8k
    switch (node_->kind) {
907
548
        case MatchValue_kind:
908
548
            CALL(fold_const_match_patterns, expr_ty, node_->v.MatchValue.value);
909
548
            break;
910
295
        case MatchSingleton_kind:
911
295
            break;
912
501
        case MatchSequence_kind:
913
501
            CALL_SEQ(astfold_pattern, pattern, node_->v.MatchSequence.patterns);
914
501
            break;
915
403
        case MatchMapping_kind:
916
403
            CALL_SEQ(fold_const_match_patterns, expr, node_->v.MatchMapping.keys);
917
403
            CALL_SEQ(astfold_pattern, pattern, node_->v.MatchMapping.patterns);
918
403
            break;
919
1.21k
        case MatchClass_kind:
920
1.21k
            CALL(astfold_expr, expr_ty, node_->v.MatchClass.cls);
921
1.21k
            CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.patterns);
922
1.21k
            CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.kwd_patterns);
923
1.21k
            break;
924
295
        case MatchStar_kind:
925
295
            break;
926
8.82k
        case MatchAs_kind:
927
8.82k
            if (node_->v.MatchAs.pattern) {
928
77
                CALL(astfold_pattern, pattern_ty, node_->v.MatchAs.pattern);
929
77
            }
930
8.82k
            break;
931
8.82k
        case MatchOr_kind:
932
2.74k
            CALL_SEQ(astfold_pattern, pattern, node_->v.MatchOr.patterns);
933
2.74k
            break;
934
    // No default case, so the compiler will emit a warning if new pattern
935
    // kinds are added without being handled here
936
14.8k
    }
937
14.8k
    LEAVE_RECURSIVE();
938
14.8k
    return 1;
939
14.8k
}
940
941
static int
942
astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
943
765
{
944
765
    CALL(astfold_pattern, expr_ty, node_->pattern);
945
765
    CALL_OPT(astfold_expr, expr_ty, node_->guard);
946
765
    CALL_SEQ(astfold_stmt, stmt, node_->body);
947
765
    return 1;
948
765
}
949
950
static int
951
astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
952
5.90k
{
953
5.90k
    switch (node_->kind) {
954
4.80k
        case TypeVar_kind:
955
4.80k
            CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound);
956
4.80k
            CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.default_value);
957
4.80k
            break;
958
433
        case ParamSpec_kind:
959
433
            CALL_OPT(astfold_expr, expr_ty, node_->v.ParamSpec.default_value);
960
433
            break;
961
669
        case TypeVarTuple_kind:
962
669
            CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVarTuple.default_value);
963
669
            break;
964
5.90k
    }
965
5.90k
    return 1;
966
5.90k
}
967
968
#undef CALL
969
#undef CALL_OPT
970
#undef CALL_SEQ
971
972
int
973
_PyAST_Preprocess(mod_ty mod, PyArena *arena, PyObject *filename, int optimize,
974
                  int ff_features, int syntax_check_only, int enable_warnings,
975
                  PyObject *module)
976
6.51k
{
977
6.51k
    _PyASTPreprocessState state;
978
6.51k
    memset(&state, 0, sizeof(_PyASTPreprocessState));
979
6.51k
    state.filename = filename;
980
6.51k
    state.module = module;
981
6.51k
    state.optimize = optimize;
982
6.51k
    state.ff_features = ff_features;
983
6.51k
    state.syntax_check_only = syntax_check_only;
984
6.51k
    state.enable_warnings = enable_warnings;
985
6.51k
    if (_Py_CArray_Init(&state.cf_finally, sizeof(ControlFlowInFinallyContext), 20) < 0) {
986
0
        return -1;
987
0
    }
988
989
6.51k
    int ret = astfold_mod(mod, arena, &state);
990
6.51k
    assert(ret || PyErr_Occurred());
991
992
6.51k
    _Py_CArray_Fini(&state.cf_finally);
993
6.51k
    return ret;
994
6.51k
}