Coverage Report

Created: 2025-11-30 06:38

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
628k
#define ENTER_RECURSIVE() \
30
628k
if (Py_EnterRecursiveCall(" during compilation")) { \
31
0
    return 0; \
32
0
}
33
34
628k
#define LEAVE_RECURSIVE() Py_LeaveRecursiveCall();
35
36
static ControlFlowInFinallyContext*
37
get_cf_finally_top(_PyASTPreprocessState *state)
38
20.2k
{
39
20.2k
    int idx = state->cf_finally_used;
40
20.2k
    return ((ControlFlowInFinallyContext*)state->cf_finally.array) + idx;
41
20.2k
}
42
43
static int
44
push_cf_context(_PyASTPreprocessState *state, stmt_ty node, bool finally, bool funcdef, bool loop)
45
12.7k
{
46
12.7k
    if (_Py_CArray_EnsureCapacity(&state->cf_finally, state->cf_finally_used+1) < 0) {
47
0
        return 0;
48
0
    }
49
50
12.7k
    state->cf_finally_used++;
51
12.7k
    ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
52
53
12.7k
    ctx->in_finally = finally;
54
12.7k
    ctx->in_funcdef = funcdef;
55
12.7k
    ctx->in_loop = loop;
56
12.7k
    return 1;
57
12.7k
}
58
59
static void
60
pop_cf_context(_PyASTPreprocessState *state)
61
12.7k
{
62
12.7k
    assert(state->cf_finally_used > 0);
63
12.7k
    state->cf_finally_used--;
64
12.7k
}
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.18k
{
84
7.18k
    if (state->enable_warnings && state->cf_finally_used > 0) {
85
6.70k
        ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
86
6.70k
        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.70k
    }
92
7.18k
    return 1;
93
7.18k
}
94
95
static int
96
before_loop_exit(_PyASTPreprocessState *state, stmt_ty node_, const char *kw)
97
1.25k
{
98
1.25k
    if (state->enable_warnings && state->cf_finally_used > 0) {
99
757
        ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
100
757
        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
757
    }
106
1.25k
    return 1;
107
1.25k
}
108
109
#define PUSH_CONTEXT(S, N, FINALLY, FUNCDEF, LOOP) \
110
12.7k
    if (!push_cf_context((S), (N), (FINALLY), (FUNCDEF), (LOOP))) { \
111
0
        return 0; \
112
0
    }
113
114
12.7k
#define POP_CONTEXT(S) pop_cf_context(S)
115
116
2.35k
#define BEFORE_FINALLY(S, N)    PUSH_CONTEXT((S), (N), true, false, false)
117
2.35k
#define AFTER_FINALLY(S)        POP_CONTEXT(S)
118
8.07k
#define BEFORE_FUNC_BODY(S, N)  PUSH_CONTEXT((S), (N), false, true, false)
119
8.07k
#define AFTER_FUNC_BODY(S)      POP_CONTEXT(S)
120
2.32k
#define BEFORE_LOOP_BODY(S, N)  PUSH_CONTEXT((S), (N), false, false, true)
121
2.32k
#define AFTER_LOOP_BODY(S)      POP_CONTEXT(S)
122
123
#define BEFORE_RETURN(S, N) \
124
7.18k
    if (!before_return((S), (N))) { \
125
0
        return 0; \
126
0
    }
127
128
#define BEFORE_LOOP_EXIT(S, N, KW) \
129
1.25k
    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
649
{
160
649
    Py_ssize_t n = asdl_seq_LEN(elts);
161
1.98k
    for (Py_ssize_t i = 0; i < n; i++) {
162
1.33k
        expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
163
1.33k
        if (e->kind == Starred_kind) {
164
0
            return 1;
165
0
        }
166
1.33k
    }
167
649
    return 0;
168
649
}
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.8k
    while (pos < size) {
180
12.2k
        if (PyUnicode_READ(kind, data, pos) != '%') {
181
11.0k
            pos++;
182
11.0k
        }
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.21k
        else {
188
1.21k
            break;
189
1.21k
        }
190
12.2k
    }
191
1.74k
    *ppos = pos;
192
1.74k
    if (pos == start) {
193
721
        return NULL;
194
721
    }
195
1.02k
    PyObject *str = PyUnicode_Substring(fmt, start, pos);
196
    /* str = str.replace('%%', '%') */
197
1.02k
    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.02k
    if (!str) {
203
0
        return NULL;
204
0
    }
205
206
1.02k
    if (_PyArena_AddPyObject(arena, str) < 0) {
207
0
        Py_DECREF(str);
208
0
        return NULL;
209
0
    }
210
1.02k
    return _PyAST_Constant(str, NULL, -1, -1, -1, -1, arena);
211
1.02k
}
212
213
62
#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.21k
{
219
1.21k
    Py_ssize_t pos = *ppos, len = PyUnicode_GET_LENGTH(fmt);
220
1.21k
    Py_UCS4 ch;
221
222
1.31k
#define NEXTC do {                      \
223
1.31k
    if (pos >= len) {                   \
224
0
        return 0;                       \
225
0
    }                                   \
226
1.31k
    ch = PyUnicode_READ_CHAR(fmt, pos); \
227
1.31k
    pos++;                              \
228
1.31k
} while (0)
229
230
1.21k
    *flags = 0;
231
1.26k
    while (1) {
232
1.26k
        NEXTC;
233
1.26k
        switch (ch) {
234
9
            case '-': *flags |= F_LJUST; continue;
235
0
            case '+': *flags |= F_SIGN; continue;
236
0
            case ' ': *flags |= F_BLANK; continue;
237
22
            case '#': *flags |= F_ALT; continue;
238
20
            case '0': *flags |= F_ZERO; continue;
239
1.26k
        }
240
1.21k
        break;
241
1.26k
    }
242
1.21k
    if ('0' <= ch && ch <= '9') {
243
30
        *width = 0;
244
30
        int digits = 0;
245
75
        while ('0' <= ch && ch <= '9') {
246
48
            *width = *width * 10 + (ch - '0');
247
48
            NEXTC;
248
48
            if (++digits >= MAXDIGITS) {
249
3
                return 0;
250
3
            }
251
48
        }
252
30
    }
253
254
1.21k
    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.21k
    *spec = ch;
269
1.21k
    *ppos = pos;
270
1.21k
    return 1;
271
272
1.21k
#undef NEXTC
273
1.21k
}
274
275
static expr_ty
276
parse_format(PyObject *fmt, Py_ssize_t *ppos, expr_ty arg, PyArena *arena)
277
1.21k
{
278
1.21k
    int spec, flags, width = -1, prec = -1;
279
1.21k
    if (!simple_format_arg_parse(fmt, ppos, &spec, &flags, &width, &prec)) {
280
        // Unsupported format.
281
3
        return NULL;
282
3
    }
283
1.21k
    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
121
    return NULL;
316
1.21k
}
317
318
static int
319
optimize_format(expr_ty node, PyObject *fmt, asdl_expr_seq *elts, PyArena *arena)
320
649
{
321
649
    Py_ssize_t pos = 0;
322
649
    Py_ssize_t cnt = 0;
323
649
    asdl_expr_seq *seq = _Py_asdl_expr_seq_new(asdl_seq_LEN(elts) * 2 + 1, arena);
324
649
    if (!seq) {
325
0
        return 0;
326
0
    }
327
649
    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.02k
            asdl_seq_SET(seq, seq->size++, lit);
333
1.02k
        }
334
721
        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.21k
        if (cnt >= asdl_seq_LEN(elts)) {
342
            // More format units than items.
343
0
            return 1;
344
0
        }
345
1.21k
        assert(PyUnicode_READ_CHAR(fmt, pos) == '%');
346
1.21k
        pos++;
347
1.21k
        expr_ty expr = parse_format(fmt, &pos, asdl_seq_GET(elts, cnt), arena);
348
1.21k
        cnt++;
349
1.21k
        if (!expr) {
350
124
            return !PyErr_Occurred();
351
124
        }
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.5k
{
373
27.5k
    if (state->syntax_check_only) {
374
21.9k
        return 1;
375
21.9k
    }
376
5.61k
    expr_ty lhs, rhs;
377
5.61k
    lhs = node->v.BinOp.left;
378
5.61k
    rhs = node->v.BinOp.right;
379
5.61k
    if (lhs->kind != Constant_kind) {
380
3.86k
        return 1;
381
3.86k
    }
382
1.74k
    PyObject *lv = lhs->v.Constant.value;
383
384
1.74k
    if (node->v.BinOp.op == Mod &&
385
1.22k
        rhs->kind == Tuple_kind &&
386
1.74k
        PyUnicode_Check(lv) &&
387
649
        !has_starred(rhs->v.Tuple.elts))
388
649
    {
389
649
        return optimize_format(node, lv, rhs->v.Tuple.elts, arena);
390
649
    }
391
392
1.10k
    return 1;
393
1.74k
}
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
368k
    if (!FUNC((ARG), ctx_, state)) \
410
368k
        return 0;
411
412
#define CALL_OPT(FUNC, TYPE, ARG) \
413
101k
    if ((ARG) != NULL && !FUNC((ARG), ctx_, state)) \
414
101k
        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
598k
    for (i = 0; i < asdl_seq_LEN(seq); i++) { \
420
335k
        TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
421
335k
        if (elt != NULL && !FUNC(elt, ctx_, state)) \
422
335k
            return 0; \
423
335k
    } \
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.3k
{
469
16.3k
    int docstring = _PyAST_GetDocString(stmts) != NULL;
470
16.3k
    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.3k
    CALL_SEQ(astfold_stmt, stmt, stmts);
478
16.3k
    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.3k
    return 1;
494
16.3k
}
495
496
static int
497
astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
498
6.54k
{
499
6.54k
    switch (node_->kind) {
500
6.45k
    case Module_kind:
501
6.45k
        CALL(astfold_body, asdl_seq, node_->v.Module.body);
502
6.45k
        break;
503
0
    case Interactive_kind:
504
0
        CALL_SEQ(astfold_stmt, stmt, node_->v.Interactive.body);
505
0
        break;
506
86
    case Expression_kind:
507
86
        CALL(astfold_expr, expr_ty, node_->v.Expression.body);
508
86
        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.54k
    }
515
6.54k
    return 1;
516
6.54k
}
517
518
static int
519
astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
520
508k
{
521
508k
    ENTER_RECURSIVE();
522
508k
    switch (node_->kind) {
523
2.89k
    case BoolOp_kind:
524
2.89k
        CALL_SEQ(astfold_expr, expr, node_->v.BoolOp.values);
525
2.89k
        break;
526
27.5k
    case BinOp_kind:
527
27.5k
        CALL(astfold_expr, expr_ty, node_->v.BinOp.left);
528
27.5k
        CALL(astfold_expr, expr_ty, node_->v.BinOp.right);
529
27.5k
        CALL(fold_binop, expr_ty, node_);
530
27.5k
        break;
531
58.8k
    case UnaryOp_kind:
532
58.8k
        CALL(astfold_expr, expr_ty, node_->v.UnaryOp.operand);
533
58.8k
        break;
534
1.11k
    case Lambda_kind:
535
1.11k
        CALL(astfold_arguments, arguments_ty, node_->v.Lambda.args);
536
1.11k
        CALL(astfold_expr, expr_ty, node_->v.Lambda.body);
537
1.11k
        break;
538
491
    case IfExp_kind:
539
491
        CALL(astfold_expr, expr_ty, node_->v.IfExp.test);
540
491
        CALL(astfold_expr, expr_ty, node_->v.IfExp.body);
541
491
        CALL(astfold_expr, expr_ty, node_->v.IfExp.orelse);
542
491
        break;
543
1.96k
    case Dict_kind:
544
1.96k
        CALL_SEQ(astfold_expr, expr, node_->v.Dict.keys);
545
1.96k
        CALL_SEQ(astfold_expr, expr, node_->v.Dict.values);
546
1.96k
        break;
547
513
    case Set_kind:
548
513
        CALL_SEQ(astfold_expr, expr, node_->v.Set.elts);
549
513
        break;
550
561
    case ListComp_kind:
551
561
        CALL(astfold_expr, expr_ty, node_->v.ListComp.elt);
552
561
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.ListComp.generators);
553
561
        break;
554
173
    case SetComp_kind:
555
173
        CALL(astfold_expr, expr_ty, node_->v.SetComp.elt);
556
173
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.SetComp.generators);
557
173
        break;
558
712
    case DictComp_kind:
559
712
        CALL(astfold_expr, expr_ty, node_->v.DictComp.key);
560
712
        CALL(astfold_expr, expr_ty, node_->v.DictComp.value);
561
712
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.DictComp.generators);
562
712
        break;
563
690
    case GeneratorExp_kind:
564
690
        CALL(astfold_expr, expr_ty, node_->v.GeneratorExp.elt);
565
690
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.GeneratorExp.generators);
566
690
        break;
567
219
    case Await_kind:
568
219
        CALL(astfold_expr, expr_ty, node_->v.Await.value);
569
219
        break;
570
817
    case Yield_kind:
571
817
        CALL_OPT(astfold_expr, expr_ty, node_->v.Yield.value);
572
817
        break;
573
115
    case YieldFrom_kind:
574
115
        CALL(astfold_expr, expr_ty, node_->v.YieldFrom.value);
575
115
        break;
576
10.2k
    case Compare_kind:
577
10.2k
        CALL(astfold_expr, expr_ty, node_->v.Compare.left);
578
10.2k
        CALL_SEQ(astfold_expr, expr, node_->v.Compare.comparators);
579
10.2k
        break;
580
33.1k
    case Call_kind:
581
33.1k
        CALL(astfold_expr, expr_ty, node_->v.Call.func);
582
33.1k
        CALL_SEQ(astfold_expr, expr, node_->v.Call.args);
583
33.1k
        CALL_SEQ(astfold_keyword, keyword, node_->v.Call.keywords);
584
33.1k
        break;
585
12.0k
    case FormattedValue_kind:
586
12.0k
        CALL(astfold_expr, expr_ty, node_->v.FormattedValue.value);
587
12.0k
        CALL_OPT(astfold_expr, expr_ty, node_->v.FormattedValue.format_spec);
588
12.0k
        break;
589
1.81k
    case Interpolation_kind:
590
1.81k
        CALL(astfold_expr, expr_ty, node_->v.Interpolation.value);
591
1.81k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Interpolation.format_spec);
592
1.81k
        break;
593
7.00k
    case JoinedStr_kind:
594
7.00k
        CALL_SEQ(astfold_expr, expr, node_->v.JoinedStr.values);
595
7.00k
        break;
596
292
    case TemplateStr_kind:
597
292
        CALL_SEQ(astfold_expr, expr, node_->v.TemplateStr.values);
598
292
        break;
599
31.0k
    case Attribute_kind:
600
31.0k
        CALL(astfold_expr, expr_ty, node_->v.Attribute.value);
601
31.0k
        break;
602
5.64k
    case Subscript_kind:
603
5.64k
        CALL(astfold_expr, expr_ty, node_->v.Subscript.value);
604
5.64k
        CALL(astfold_expr, expr_ty, node_->v.Subscript.slice);
605
5.64k
        break;
606
1.00k
    case Starred_kind:
607
1.00k
        CALL(astfold_expr, expr_ty, node_->v.Starred.value);
608
1.00k
        break;
609
2.94k
    case Slice_kind:
610
2.94k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.lower);
611
2.94k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.upper);
612
2.94k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step);
613
2.94k
        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.4k
    case Tuple_kind:
618
12.4k
        CALL_SEQ(astfold_expr, expr, node_->v.Tuple.elts);
619
12.4k
        break;
620
174k
    case Name_kind:
621
174k
        if (state->syntax_check_only) {
622
61.1k
            break;
623
61.1k
        }
624
113k
        if (node_->v.Name.ctx == Load &&
625
93.5k
                _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
113k
        break;
630
113k
    case NamedExpr_kind:
631
314
        CALL(astfold_expr, expr_ty, node_->v.NamedExpr.value);
632
314
        break;
633
116k
    case Constant_kind:
634
        // Already a constant, nothing further to do
635
116k
        break;
636
    // No default case, so the compiler will emit a warning if new expression
637
    // kinds are added without being handled here
638
508k
    }
639
508k
    LEAVE_RECURSIVE();
640
508k
    return 1;
641
508k
}
642
643
static int
644
astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
645
5.75k
{
646
5.75k
    CALL(astfold_expr, expr_ty, node_->value);
647
5.75k
    return 1;
648
5.75k
}
649
650
static int
651
astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
652
2.27k
{
653
2.27k
    CALL(astfold_expr, expr_ty, node_->target);
654
2.27k
    CALL(astfold_expr, expr_ty, node_->iter);
655
2.27k
    CALL_SEQ(astfold_expr, expr, node_->ifs);
656
2.27k
    return 1;
657
2.27k
}
658
659
static int
660
astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
661
9.18k
{
662
9.18k
    CALL_SEQ(astfold_arg, arg, node_->posonlyargs);
663
9.18k
    CALL_SEQ(astfold_arg, arg, node_->args);
664
9.18k
    CALL_OPT(astfold_arg, arg_ty, node_->vararg);
665
9.18k
    CALL_SEQ(astfold_arg, arg, node_->kwonlyargs);
666
9.18k
    CALL_SEQ(astfold_expr, expr, node_->kw_defaults);
667
9.18k
    CALL_OPT(astfold_arg, arg_ty, node_->kwarg);
668
9.18k
    CALL_SEQ(astfold_expr, expr, node_->defaults);
669
9.18k
    return 1;
670
9.18k
}
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.7k
        CALL_OPT(astfold_expr, expr_ty, node_->annotation);
677
20.7k
    }
678
21.8k
    return 1;
679
21.8k
}
680
681
static int
682
astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
683
105k
{
684
105k
    ENTER_RECURSIVE();
685
105k
    switch (node_->kind) {
686
7.87k
    case FunctionDef_kind: {
687
7.87k
        CALL_SEQ(astfold_type_param, type_param, node_->v.FunctionDef.type_params);
688
7.87k
        CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args);
689
7.87k
        BEFORE_FUNC_BODY(state, node_);
690
7.87k
        CALL(astfold_body, asdl_seq, node_->v.FunctionDef.body);
691
7.87k
        AFTER_FUNC_BODY(state);
692
7.87k
        CALL_SEQ(astfold_expr, expr, node_->v.FunctionDef.decorator_list);
693
7.87k
        if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
694
7.39k
            CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns);
695
7.39k
        }
696
7.87k
        break;
697
7.87k
    }
698
7.87k
    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.83k
    case ClassDef_kind:
711
1.83k
        CALL_SEQ(astfold_type_param, type_param, node_->v.ClassDef.type_params);
712
1.83k
        CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.bases);
713
1.83k
        CALL_SEQ(astfold_keyword, keyword, node_->v.ClassDef.keywords);
714
1.83k
        CALL(astfold_body, asdl_seq, node_->v.ClassDef.body);
715
1.83k
        CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.decorator_list);
716
1.83k
        break;
717
7.18k
    case Return_kind:
718
7.18k
        BEFORE_RETURN(state, node_);
719
7.18k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Return.value);
720
7.18k
        break;
721
618
    case Delete_kind:
722
618
        CALL_SEQ(astfold_expr, expr, node_->v.Delete.targets);
723
618
        break;
724
18.5k
    case Assign_kind:
725
18.5k
        CALL_SEQ(astfold_expr, expr, node_->v.Assign.targets);
726
18.5k
        CALL(astfold_expr, expr_ty, node_->v.Assign.value);
727
18.5k
        break;
728
1.18k
    case AugAssign_kind:
729
1.18k
        CALL(astfold_expr, expr_ty, node_->v.AugAssign.target);
730
1.18k
        CALL(astfold_expr, expr_ty, node_->v.AugAssign.value);
731
1.18k
        break;
732
1.19k
    case AnnAssign_kind:
733
1.19k
        CALL(astfold_expr, expr_ty, node_->v.AnnAssign.target);
734
1.19k
        if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
735
1.08k
            CALL(astfold_expr, expr_ty, node_->v.AnnAssign.annotation);
736
1.08k
        }
737
1.19k
        CALL_OPT(astfold_expr, expr_ty, node_->v.AnnAssign.value);
738
1.19k
        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.55k
    case For_kind: {
745
1.55k
        CALL(astfold_expr, expr_ty, node_->v.For.target);
746
1.55k
        CALL(astfold_expr, expr_ty, node_->v.For.iter);
747
1.55k
        BEFORE_LOOP_BODY(state, node_);
748
1.55k
        CALL_SEQ(astfold_stmt, stmt, node_->v.For.body);
749
1.55k
        AFTER_LOOP_BODY(state);
750
1.55k
        CALL_SEQ(astfold_stmt, stmt, node_->v.For.orelse);
751
1.55k
        break;
752
1.55k
    }
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
697
    case While_kind: {
763
697
        CALL(astfold_expr, expr_ty, node_->v.While.test);
764
697
        BEFORE_LOOP_BODY(state, node_);
765
697
        CALL_SEQ(astfold_stmt, stmt, node_->v.While.body);
766
697
        AFTER_LOOP_BODY(state);
767
697
        CALL_SEQ(astfold_stmt, stmt, node_->v.While.orelse);
768
697
        break;
769
697
    }
770
11.0k
    case If_kind:
771
11.0k
        CALL(astfold_expr, expr_ty, node_->v.If.test);
772
11.0k
        CALL_SEQ(astfold_stmt, stmt, node_->v.If.body);
773
11.0k
        CALL_SEQ(astfold_stmt, stmt, node_->v.If.orelse);
774
11.0k
        break;
775
338
    case With_kind:
776
338
        CALL_SEQ(astfold_withitem, withitem, node_->v.With.items);
777
338
        CALL_SEQ(astfold_stmt, stmt, node_->v.With.body);
778
338
        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.32k
    case Raise_kind:
784
3.32k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.exc);
785
3.32k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.cause);
786
3.32k
        break;
787
1.92k
    case Try_kind: {
788
1.92k
        CALL_SEQ(astfold_stmt, stmt, node_->v.Try.body);
789
1.92k
        CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.Try.handlers);
790
1.92k
        CALL_SEQ(astfold_stmt, stmt, node_->v.Try.orelse);
791
1.92k
        BEFORE_FINALLY(state, node_);
792
1.92k
        CALL_SEQ(astfold_stmt, stmt, node_->v.Try.finalbody);
793
1.92k
        AFTER_FINALLY(state);
794
1.92k
        break;
795
1.92k
    }
796
430
    case TryStar_kind: {
797
430
        CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.body);
798
430
        CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.TryStar.handlers);
799
430
        CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.orelse);
800
430
        BEFORE_FINALLY(state, node_);
801
430
        CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.finalbody);
802
430
        AFTER_FINALLY(state);
803
430
        break;
804
430
    }
805
477
    case Assert_kind:
806
477
        CALL(astfold_expr, expr_ty, node_->v.Assert.test);
807
477
        CALL_OPT(astfold_expr, expr_ty, node_->v.Assert.msg);
808
477
        break;
809
40.5k
    case Expr_kind:
810
40.5k
        CALL(astfold_expr, expr_ty, node_->v.Expr.value);
811
40.5k
        break;
812
275
    case Match_kind:
813
275
        CALL(astfold_expr, expr_ty, node_->v.Match.subject);
814
275
        CALL_SEQ(astfold_match_case, match_case, node_->v.Match.cases);
815
275
        break;
816
720
    case Break_kind:
817
720
        BEFORE_LOOP_EXIT(state, node_, "break");
818
720
        break;
819
534
    case Continue_kind:
820
534
        BEFORE_LOOP_EXIT(state, node_, "continue");
821
534
        break;
822
    // The following statements don't contain any subexpressions to be folded
823
1.60k
    case Import_kind:
824
2.97k
    case ImportFrom_kind:
825
3.31k
    case Global_kind:
826
3.64k
    case Nonlocal_kind:
827
4.27k
    case Pass_kind:
828
4.27k
        break;
829
    // No default case, so the compiler will emit a warning if new statement
830
    // kinds are added without being handled here
831
105k
    }
832
105k
    LEAVE_RECURSIVE();
833
105k
    return 1;
834
105k
}
835
836
static int
837
astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
838
2.79k
{
839
2.79k
    switch (node_->kind) {
840
2.79k
    case ExceptHandler_kind:
841
2.79k
        CALL_OPT(astfold_expr, expr_ty, node_->v.ExceptHandler.type);
842
2.79k
        CALL_SEQ(astfold_stmt, stmt, node_->v.ExceptHandler.body);
843
2.79k
        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.79k
    }
847
2.79k
    return 1;
848
2.79k
}
849
850
static int
851
astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
852
1.83k
{
853
1.83k
    CALL(astfold_expr, expr_ty, node_->context_expr);
854
1.83k
    CALL_OPT(astfold_expr, expr_ty, node_->optional_vars);
855
1.83k
    return 1;
856
1.83k
}
857
858
static int
859
fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTPreprocessState *state)
860
673
{
861
673
    if (state->syntax_check_only) {
862
657
        return 1;
863
657
    }
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
547
        case MatchValue_kind:
908
547
            CALL(fold_const_match_patterns, expr_ty, node_->v.MatchValue.value);
909
547
            break;
910
290
        case MatchSingleton_kind:
911
290
            break;
912
502
        case MatchSequence_kind:
913
502
            CALL_SEQ(astfold_pattern, pattern, node_->v.MatchSequence.patterns);
914
502
            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
304
        case MatchStar_kind:
925
304
            break;
926
8.82k
        case MatchAs_kind:
927
8.82k
            if (node_->v.MatchAs.pattern) {
928
76
                CALL(astfold_pattern, pattern_ty, node_->v.MatchAs.pattern);
929
76
            }
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
766
{
944
766
    CALL(astfold_pattern, expr_ty, node_->pattern);
945
766
    CALL_OPT(astfold_expr, expr_ty, node_->guard);
946
766
    CALL_SEQ(astfold_stmt, stmt, node_->body);
947
766
    return 1;
948
766
}
949
950
static int
951
astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
952
5.93k
{
953
5.93k
    switch (node_->kind) {
954
4.81k
        case TypeVar_kind:
955
4.81k
            CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound);
956
4.81k
            CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.default_value);
957
4.81k
            break;
958
443
        case ParamSpec_kind:
959
443
            CALL_OPT(astfold_expr, expr_ty, node_->v.ParamSpec.default_value);
960
443
            break;
961
672
        case TypeVarTuple_kind:
962
672
            CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVarTuple.default_value);
963
672
            break;
964
5.93k
    }
965
5.93k
    return 1;
966
5.93k
}
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.54k
{
977
6.54k
    _PyASTPreprocessState state;
978
6.54k
    memset(&state, 0, sizeof(_PyASTPreprocessState));
979
6.54k
    state.filename = filename;
980
6.54k
    state.module = module;
981
6.54k
    state.optimize = optimize;
982
6.54k
    state.ff_features = ff_features;
983
6.54k
    state.syntax_check_only = syntax_check_only;
984
6.54k
    state.enable_warnings = enable_warnings;
985
6.54k
    if (_Py_CArray_Init(&state.cf_finally, sizeof(ControlFlowInFinallyContext), 20) < 0) {
986
0
        return -1;
987
0
    }
988
989
6.54k
    int ret = astfold_mod(mod, arena, &state);
990
6.54k
    assert(ret || PyErr_Occurred());
991
992
6.54k
    _Py_CArray_Fini(&state.cf_finally);
993
6.54k
    return ret;
994
6.54k
}