Coverage Report

Created: 2026-02-26 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/ast.c
Line
Count
Source
1
/*
2
 * This file exposes PyAST_Validate interface to check the integrity
3
 * of the given abstract syntax tree (potentially constructed manually).
4
 */
5
#include "Python.h"
6
#include "pycore_ast.h"           // asdl_stmt_seq
7
#include "pycore_pystate.h"       // _PyThreadState_GET()
8
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
9
10
#include <stdbool.h>              // bool
11
12
13
0
#define ENTER_RECURSIVE() \
14
0
if (Py_EnterRecursiveCall(" during compilation")) { \
15
0
    return 0; \
16
0
}
17
18
0
#define LEAVE_RECURSIVE() Py_LeaveRecursiveCall();
19
20
static int validate_stmts(asdl_stmt_seq *);
21
static int validate_exprs(asdl_expr_seq *, expr_context_ty, int);
22
static int validate_patterns(asdl_pattern_seq *, int);
23
static int validate_type_params(asdl_type_param_seq *);
24
static int _validate_nonempty_seq(asdl_seq *, const char *, const char *);
25
static int validate_stmt(stmt_ty);
26
static int validate_expr(expr_ty, expr_context_ty);
27
static int validate_pattern(pattern_ty, int);
28
static int validate_typeparam(type_param_ty);
29
30
#define VALIDATE_POSITIONS(node) \
31
0
    if (node->lineno > node->end_lineno) { \
32
0
        PyErr_Format(PyExc_ValueError, \
33
0
                     "AST node line range (%d, %d) is not valid", \
34
0
                     node->lineno, node->end_lineno); \
35
0
        return 0; \
36
0
    } \
37
0
    if ((node->lineno < 0 && node->end_lineno != node->lineno) || \
38
0
        (node->col_offset < 0 && node->col_offset != node->end_col_offset)) { \
39
0
        PyErr_Format(PyExc_ValueError, \
40
0
                     "AST node column range (%d, %d) for line range (%d, %d) is not valid", \
41
0
                     node->col_offset, node->end_col_offset, node->lineno, node->end_lineno); \
42
0
        return 0; \
43
0
    } \
44
0
    if (node->lineno == node->end_lineno && node->col_offset > node->end_col_offset) { \
45
0
        PyErr_Format(PyExc_ValueError, \
46
0
                     "line %d, column %d-%d is not a valid range", \
47
0
                     node->lineno, node->col_offset, node->end_col_offset); \
48
0
        return 0; \
49
0
    }
50
51
static int
52
validate_name(PyObject *name)
53
0
{
54
0
    assert(!PyErr_Occurred());
55
0
    assert(PyUnicode_Check(name));
56
0
    static const char * const forbidden[] = {
57
0
        "None",
58
0
        "True",
59
0
        "False",
60
0
        NULL
61
0
    };
62
0
    for (int i = 0; forbidden[i] != NULL; i++) {
63
0
        if (_PyUnicode_EqualToASCIIString(name, forbidden[i])) {
64
0
            PyErr_Format(PyExc_ValueError, "identifier field can't represent '%s' constant", forbidden[i]);
65
0
            return 0;
66
0
        }
67
0
    }
68
0
    return 1;
69
0
}
70
71
static int
72
validate_comprehension(asdl_comprehension_seq *gens)
73
0
{
74
0
    assert(!PyErr_Occurred());
75
0
    if (!asdl_seq_LEN(gens)) {
76
0
        PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
77
0
        return 0;
78
0
    }
79
0
    for (Py_ssize_t i = 0; i < asdl_seq_LEN(gens); i++) {
80
0
        comprehension_ty comp = asdl_seq_GET(gens, i);
81
0
        if (!validate_expr(comp->target, Store) ||
82
0
            !validate_expr(comp->iter, Load) ||
83
0
            !validate_exprs(comp->ifs, Load, 0))
84
0
            return 0;
85
0
    }
86
0
    return 1;
87
0
}
88
89
static int
90
validate_keywords(asdl_keyword_seq *keywords)
91
0
{
92
0
    assert(!PyErr_Occurred());
93
0
    for (Py_ssize_t i = 0; i < asdl_seq_LEN(keywords); i++)
94
0
        if (!validate_expr((asdl_seq_GET(keywords, i))->value, Load))
95
0
            return 0;
96
0
    return 1;
97
0
}
98
99
static int
100
validate_args(asdl_arg_seq *args)
101
0
{
102
0
    assert(!PyErr_Occurred());
103
0
    for (Py_ssize_t i = 0; i < asdl_seq_LEN(args); i++) {
104
0
        arg_ty arg = asdl_seq_GET(args, i);
105
0
        VALIDATE_POSITIONS(arg);
106
0
        if (arg->annotation && !validate_expr(arg->annotation, Load))
107
0
            return 0;
108
0
    }
109
0
    return 1;
110
0
}
111
112
static const char *
113
expr_context_name(expr_context_ty ctx)
114
0
{
115
0
    switch (ctx) {
116
0
    case Load:
117
0
        return "Load";
118
0
    case Store:
119
0
        return "Store";
120
0
    case Del:
121
0
        return "Del";
122
    // No default case so compiler emits warning for unhandled cases
123
0
    }
124
0
    Py_UNREACHABLE();
125
0
}
126
127
static int
128
validate_arguments(arguments_ty args)
129
0
{
130
0
    assert(!PyErr_Occurred());
131
0
    if (!validate_args(args->posonlyargs) || !validate_args(args->args)) {
132
0
        return 0;
133
0
    }
134
0
    if (args->vararg && args->vararg->annotation
135
0
        && !validate_expr(args->vararg->annotation, Load)) {
136
0
            return 0;
137
0
    }
138
0
    if (!validate_args(args->kwonlyargs))
139
0
        return 0;
140
0
    if (args->kwarg && args->kwarg->annotation
141
0
        && !validate_expr(args->kwarg->annotation, Load)) {
142
0
            return 0;
143
0
    }
144
0
    if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) {
145
0
        PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
146
0
        return 0;
147
0
    }
148
0
    if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
149
0
        PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
150
0
                        "kw_defaults on arguments");
151
0
        return 0;
152
0
    }
153
0
    return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
154
0
}
155
156
static int
157
validate_constant(PyObject *value)
158
0
{
159
0
    assert(!PyErr_Occurred());
160
0
    if (value == Py_None || value == Py_Ellipsis)
161
0
        return 1;
162
163
0
    if (PyLong_CheckExact(value)
164
0
            || PyFloat_CheckExact(value)
165
0
            || PyComplex_CheckExact(value)
166
0
            || PyBool_Check(value)
167
0
            || PyUnicode_CheckExact(value)
168
0
            || PyBytes_CheckExact(value))
169
0
        return 1;
170
171
0
    if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
172
0
        ENTER_RECURSIVE();
173
174
0
        PyObject *it = PyObject_GetIter(value);
175
0
        if (it == NULL)
176
0
            return 0;
177
178
0
        while (1) {
179
0
            PyObject *item = PyIter_Next(it);
180
0
            if (item == NULL) {
181
0
                if (PyErr_Occurred()) {
182
0
                    Py_DECREF(it);
183
0
                    return 0;
184
0
                }
185
0
                break;
186
0
            }
187
188
0
            if (!validate_constant(item)) {
189
0
                Py_DECREF(it);
190
0
                Py_DECREF(item);
191
0
                return 0;
192
0
            }
193
0
            Py_DECREF(item);
194
0
        }
195
196
0
        Py_DECREF(it);
197
0
        LEAVE_RECURSIVE();
198
0
        return 1;
199
0
    }
200
201
0
    if (!PyErr_Occurred()) {
202
0
        PyErr_Format(PyExc_TypeError,
203
0
                     "got an invalid type in Constant: %s",
204
0
                     _PyType_Name(Py_TYPE(value)));
205
0
    }
206
0
    return 0;
207
0
}
208
209
static int
210
validate_expr(expr_ty exp, expr_context_ty ctx)
211
0
{
212
0
    assert(!PyErr_Occurred());
213
0
    VALIDATE_POSITIONS(exp);
214
0
    int ret = -1;
215
0
    ENTER_RECURSIVE();
216
0
    int check_ctx = 1;
217
0
    expr_context_ty actual_ctx;
218
219
    /* First check expression context. */
220
0
    switch (exp->kind) {
221
0
    case Attribute_kind:
222
0
        actual_ctx = exp->v.Attribute.ctx;
223
0
        break;
224
0
    case Subscript_kind:
225
0
        actual_ctx = exp->v.Subscript.ctx;
226
0
        break;
227
0
    case Starred_kind:
228
0
        actual_ctx = exp->v.Starred.ctx;
229
0
        break;
230
0
    case Name_kind:
231
0
        if (!validate_name(exp->v.Name.id)) {
232
0
            return 0;
233
0
        }
234
0
        actual_ctx = exp->v.Name.ctx;
235
0
        break;
236
0
    case List_kind:
237
0
        actual_ctx = exp->v.List.ctx;
238
0
        break;
239
0
    case Tuple_kind:
240
0
        actual_ctx = exp->v.Tuple.ctx;
241
0
        break;
242
0
    default:
243
0
        if (ctx != Load) {
244
0
            PyErr_Format(PyExc_ValueError, "expression which can't be "
245
0
                         "assigned to in %s context", expr_context_name(ctx));
246
0
            return 0;
247
0
        }
248
0
        check_ctx = 0;
249
        /* set actual_ctx to prevent gcc warning */
250
0
        actual_ctx = 0;
251
0
    }
252
0
    if (check_ctx && actual_ctx != ctx) {
253
0
        PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
254
0
                     expr_context_name(ctx), expr_context_name(actual_ctx));
255
0
        return 0;
256
0
    }
257
258
    /* Now validate expression. */
259
0
    switch (exp->kind) {
260
0
    case BoolOp_kind:
261
0
        if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
262
0
            PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
263
0
            return 0;
264
0
        }
265
0
        ret = validate_exprs(exp->v.BoolOp.values, Load, 0);
266
0
        break;
267
0
    case BinOp_kind:
268
0
        ret = validate_expr(exp->v.BinOp.left, Load) &&
269
0
            validate_expr(exp->v.BinOp.right, Load);
270
0
        break;
271
0
    case UnaryOp_kind:
272
0
        ret = validate_expr(exp->v.UnaryOp.operand, Load);
273
0
        break;
274
0
    case Lambda_kind:
275
0
        ret = validate_arguments(exp->v.Lambda.args) &&
276
0
            validate_expr(exp->v.Lambda.body, Load);
277
0
        break;
278
0
    case IfExp_kind:
279
0
        ret = validate_expr(exp->v.IfExp.test, Load) &&
280
0
            validate_expr(exp->v.IfExp.body, Load) &&
281
0
            validate_expr(exp->v.IfExp.orelse, Load);
282
0
        break;
283
0
    case Dict_kind:
284
0
        if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
285
0
            PyErr_SetString(PyExc_ValueError,
286
0
                            "Dict doesn't have the same number of keys as values");
287
0
            return 0;
288
0
        }
289
        /* null_ok=1 for keys expressions to allow dict unpacking to work in
290
           dict literals, i.e. ``{**{a:b}}`` */
291
0
        ret = validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
292
0
            validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
293
0
        break;
294
0
    case Set_kind:
295
0
        ret = validate_exprs(exp->v.Set.elts, Load, 0);
296
0
        break;
297
0
#define COMP(NAME) \
298
0
        case NAME ## _kind: \
299
0
            ret = validate_comprehension(exp->v.NAME.generators) && \
300
0
                validate_expr(exp->v.NAME.elt, Load); \
301
0
            break;
302
0
    COMP(ListComp)
303
0
    COMP(SetComp)
304
0
    COMP(GeneratorExp)
305
0
#undef COMP
306
0
    case DictComp_kind:
307
0
        ret = validate_comprehension(exp->v.DictComp.generators) &&
308
0
            validate_expr(exp->v.DictComp.key, Load);
309
0
        if (ret && exp->v.DictComp.value != NULL){
310
0
            ret = validate_expr(exp->v.DictComp.value, Load);
311
0
        }
312
0
        break;
313
0
    case Yield_kind:
314
0
        ret = !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
315
0
        break;
316
0
    case YieldFrom_kind:
317
0
        ret = validate_expr(exp->v.YieldFrom.value, Load);
318
0
        break;
319
0
    case Await_kind:
320
0
        ret = validate_expr(exp->v.Await.value, Load);
321
0
        break;
322
0
    case Compare_kind:
323
0
        if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
324
0
            PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
325
0
            return 0;
326
0
        }
327
0
        if (asdl_seq_LEN(exp->v.Compare.comparators) !=
328
0
            asdl_seq_LEN(exp->v.Compare.ops)) {
329
0
            PyErr_SetString(PyExc_ValueError, "Compare has a different number "
330
0
                            "of comparators and operands");
331
0
            return 0;
332
0
        }
333
0
        ret = validate_exprs(exp->v.Compare.comparators, Load, 0) &&
334
0
            validate_expr(exp->v.Compare.left, Load);
335
0
        break;
336
0
    case Call_kind:
337
0
        ret = validate_expr(exp->v.Call.func, Load) &&
338
0
            validate_exprs(exp->v.Call.args, Load, 0) &&
339
0
            validate_keywords(exp->v.Call.keywords);
340
0
        break;
341
0
    case Constant_kind:
342
0
        if (!validate_constant(exp->v.Constant.value)) {
343
0
            return 0;
344
0
        }
345
0
        ret = 1;
346
0
        break;
347
0
    case JoinedStr_kind:
348
0
        ret = validate_exprs(exp->v.JoinedStr.values, Load, 0);
349
0
        break;
350
0
    case TemplateStr_kind:
351
0
        ret = validate_exprs(exp->v.TemplateStr.values, Load, 0);
352
0
        break;
353
0
    case FormattedValue_kind:
354
0
        if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
355
0
            return 0;
356
0
        if (exp->v.FormattedValue.format_spec) {
357
0
            ret = validate_expr(exp->v.FormattedValue.format_spec, Load);
358
0
            break;
359
0
        }
360
0
        ret = 1;
361
0
        break;
362
0
    case Interpolation_kind:
363
0
        if (validate_expr(exp->v.Interpolation.value, Load) == 0)
364
0
            return 0;
365
0
        if (exp->v.Interpolation.format_spec) {
366
0
            ret = validate_expr(exp->v.Interpolation.format_spec, Load);
367
0
            break;
368
0
        }
369
0
        ret = 1;
370
0
        break;
371
0
    case Attribute_kind:
372
0
        ret = validate_expr(exp->v.Attribute.value, Load);
373
0
        break;
374
0
    case Subscript_kind:
375
0
        ret = validate_expr(exp->v.Subscript.slice, Load) &&
376
0
            validate_expr(exp->v.Subscript.value, Load);
377
0
        break;
378
0
    case Starred_kind:
379
0
        ret = validate_expr(exp->v.Starred.value, ctx);
380
0
        break;
381
0
    case Slice_kind:
382
0
        ret = (!exp->v.Slice.lower || validate_expr(exp->v.Slice.lower, Load)) &&
383
0
            (!exp->v.Slice.upper || validate_expr(exp->v.Slice.upper, Load)) &&
384
0
            (!exp->v.Slice.step || validate_expr(exp->v.Slice.step, Load));
385
0
        break;
386
0
    case List_kind:
387
0
        ret = validate_exprs(exp->v.List.elts, ctx, 0);
388
0
        break;
389
0
    case Tuple_kind:
390
0
        ret = validate_exprs(exp->v.Tuple.elts, ctx, 0);
391
0
        break;
392
0
    case NamedExpr_kind:
393
0
        if (exp->v.NamedExpr.target->kind != Name_kind) {
394
0
            PyErr_SetString(PyExc_TypeError,
395
0
                            "NamedExpr target must be a Name");
396
0
            return 0;
397
0
        }
398
0
        ret = validate_expr(exp->v.NamedExpr.value, Load);
399
0
        break;
400
    /* This last case doesn't have any checking. */
401
0
    case Name_kind:
402
0
        ret = 1;
403
0
        break;
404
    // No default case so compiler emits warning for unhandled cases
405
0
    }
406
0
    if (ret < 0) {
407
0
        PyErr_SetString(PyExc_SystemError, "unexpected expression");
408
0
        ret = 0;
409
0
    }
410
0
    LEAVE_RECURSIVE();
411
0
    return ret;
412
0
}
413
414
415
// Note: the ensure_literal_* functions are only used to validate a restricted
416
//       set of non-recursive literals that have already been checked with
417
//       validate_expr, so they don't accept the validator state
418
static int
419
ensure_literal_number(expr_ty exp, bool allow_real, bool allow_imaginary)
420
0
{
421
0
    assert(exp->kind == Constant_kind);
422
0
    PyObject *value = exp->v.Constant.value;
423
0
    return (allow_real && PyFloat_CheckExact(value)) ||
424
0
           (allow_real && PyLong_CheckExact(value)) ||
425
0
           (allow_imaginary && PyComplex_CheckExact(value));
426
0
}
427
428
static int
429
ensure_literal_negative(expr_ty exp, bool allow_real, bool allow_imaginary)
430
0
{
431
0
    assert(exp->kind == UnaryOp_kind);
432
    // Must be negation ...
433
0
    if (exp->v.UnaryOp.op != USub) {
434
0
        return 0;
435
0
    }
436
    // ... of a constant ...
437
0
    expr_ty operand = exp->v.UnaryOp.operand;
438
0
    if (operand->kind != Constant_kind) {
439
0
        return 0;
440
0
    }
441
    // ... number
442
0
    return ensure_literal_number(operand, allow_real, allow_imaginary);
443
0
}
444
445
static int
446
ensure_literal_complex(expr_ty exp)
447
0
{
448
0
    assert(exp->kind == BinOp_kind);
449
0
    expr_ty left = exp->v.BinOp.left;
450
0
    expr_ty right = exp->v.BinOp.right;
451
    // Ensure op is addition or subtraction
452
0
    if (exp->v.BinOp.op != Add && exp->v.BinOp.op != Sub) {
453
0
        return 0;
454
0
    }
455
    // Check LHS is a real number (potentially signed)
456
0
    switch (left->kind)
457
0
    {
458
0
        case Constant_kind:
459
0
            if (!ensure_literal_number(left, /*real=*/true, /*imaginary=*/false)) {
460
0
                return 0;
461
0
            }
462
0
            break;
463
0
        case UnaryOp_kind:
464
0
            if (!ensure_literal_negative(left, /*real=*/true, /*imaginary=*/false)) {
465
0
                return 0;
466
0
            }
467
0
            break;
468
0
        default:
469
0
            return 0;
470
0
    }
471
    // Check RHS is an imaginary number (no separate sign allowed)
472
0
    switch (right->kind)
473
0
    {
474
0
        case Constant_kind:
475
0
            if (!ensure_literal_number(right, /*real=*/false, /*imaginary=*/true)) {
476
0
                return 0;
477
0
            }
478
0
            break;
479
0
        default:
480
0
            return 0;
481
0
    }
482
0
    return 1;
483
0
}
484
485
static int
486
validate_pattern_match_value(expr_ty exp)
487
0
{
488
0
    assert(!PyErr_Occurred());
489
0
    if (!validate_expr(exp, Load)) {
490
0
        return 0;
491
0
    }
492
493
0
    switch (exp->kind)
494
0
    {
495
0
        case Constant_kind:
496
            /* Ellipsis and immutable sequences are not allowed.
497
               For True, False and None, MatchSingleton() should
498
               be used */
499
0
            if (!validate_expr(exp, Load)) {
500
0
                return 0;
501
0
            }
502
0
            PyObject *literal = exp->v.Constant.value;
503
0
            if (PyLong_CheckExact(literal) || PyFloat_CheckExact(literal) ||
504
0
                PyBytes_CheckExact(literal) || PyComplex_CheckExact(literal) ||
505
0
                PyUnicode_CheckExact(literal)) {
506
0
                return 1;
507
0
            }
508
0
            PyErr_SetString(PyExc_ValueError,
509
0
                            "unexpected constant inside of a literal pattern");
510
0
            return 0;
511
0
        case Attribute_kind:
512
            // Constants and attribute lookups are always permitted
513
0
            return 1;
514
0
        case UnaryOp_kind:
515
            // Negated numbers are permitted (whether real or imaginary)
516
            // Compiler will complain if AST folding doesn't create a constant
517
0
            if (ensure_literal_negative(exp, /*real=*/true, /*imaginary=*/true)) {
518
0
                return 1;
519
0
            }
520
0
            break;
521
0
        case BinOp_kind:
522
            // Complex literals are permitted
523
            // Compiler will complain if AST folding doesn't create a constant
524
0
            if (ensure_literal_complex(exp)) {
525
0
                return 1;
526
0
            }
527
0
            break;
528
0
        case JoinedStr_kind:
529
0
        case TemplateStr_kind:
530
            // Handled in the later stages
531
0
            return 1;
532
0
        default:
533
0
            break;
534
0
    }
535
0
    PyErr_SetString(PyExc_ValueError,
536
0
                    "patterns may only match literals and attribute lookups");
537
0
    return 0;
538
0
}
539
540
static int
541
validate_capture(PyObject *name)
542
0
{
543
0
    assert(!PyErr_Occurred());
544
0
    if (_PyUnicode_EqualToASCIIString(name, "_")) {
545
0
        PyErr_Format(PyExc_ValueError, "can't capture name '_' in patterns");
546
0
        return 0;
547
0
    }
548
0
    return validate_name(name);
549
0
}
550
551
static int
552
validate_pattern(pattern_ty p, int star_ok)
553
0
{
554
0
    assert(!PyErr_Occurred());
555
0
    VALIDATE_POSITIONS(p);
556
0
    int ret = -1;
557
0
    ENTER_RECURSIVE();
558
0
    switch (p->kind) {
559
0
        case MatchValue_kind:
560
0
            ret = validate_pattern_match_value(p->v.MatchValue.value);
561
0
            break;
562
0
        case MatchSingleton_kind:
563
0
            ret = p->v.MatchSingleton.value == Py_None || PyBool_Check(p->v.MatchSingleton.value);
564
0
            if (!ret) {
565
0
                PyErr_SetString(PyExc_ValueError,
566
0
                                "MatchSingleton can only contain True, False and None");
567
0
            }
568
0
            break;
569
0
        case MatchSequence_kind:
570
0
            ret = validate_patterns(p->v.MatchSequence.patterns, /*star_ok=*/1);
571
0
            break;
572
0
        case MatchMapping_kind:
573
0
            if (asdl_seq_LEN(p->v.MatchMapping.keys) != asdl_seq_LEN(p->v.MatchMapping.patterns)) {
574
0
                PyErr_SetString(PyExc_ValueError,
575
0
                                "MatchMapping doesn't have the same number of keys as patterns");
576
0
                ret = 0;
577
0
                break;
578
0
            }
579
580
0
            if (p->v.MatchMapping.rest && !validate_capture(p->v.MatchMapping.rest)) {
581
0
                ret = 0;
582
0
                break;
583
0
            }
584
585
0
            asdl_expr_seq *keys = p->v.MatchMapping.keys;
586
0
            for (Py_ssize_t i = 0; i < asdl_seq_LEN(keys); i++) {
587
0
                expr_ty key = asdl_seq_GET(keys, i);
588
0
                if (key->kind == Constant_kind) {
589
0
                    PyObject *literal = key->v.Constant.value;
590
0
                    if (literal == Py_None || PyBool_Check(literal)) {
591
                        /* validate_pattern_match_value will ensure the key
592
                           doesn't contain True, False and None but it is
593
                           syntactically valid, so we will pass those on in
594
                           a special case. */
595
0
                        continue;
596
0
                    }
597
0
                }
598
0
                if (!validate_pattern_match_value(key)) {
599
0
                    ret = 0;
600
0
                    break;
601
0
                }
602
0
            }
603
0
            if (ret == 0) {
604
0
                break;
605
0
            }
606
0
            ret = validate_patterns(p->v.MatchMapping.patterns, /*star_ok=*/0);
607
0
            break;
608
0
        case MatchClass_kind:
609
0
            if (asdl_seq_LEN(p->v.MatchClass.kwd_attrs) != asdl_seq_LEN(p->v.MatchClass.kwd_patterns)) {
610
0
                PyErr_SetString(PyExc_ValueError,
611
0
                                "MatchClass doesn't have the same number of keyword attributes as patterns");
612
0
                ret = 0;
613
0
                break;
614
0
            }
615
0
            if (!validate_expr(p->v.MatchClass.cls, Load)) {
616
0
                ret = 0;
617
0
                break;
618
0
            }
619
620
0
            expr_ty cls = p->v.MatchClass.cls;
621
0
            while (1) {
622
0
                if (cls->kind == Name_kind) {
623
0
                    break;
624
0
                }
625
0
                else if (cls->kind == Attribute_kind) {
626
0
                    cls = cls->v.Attribute.value;
627
0
                    continue;
628
0
                }
629
0
                else {
630
0
                    PyErr_SetString(PyExc_ValueError,
631
0
                                    "MatchClass cls field can only contain Name or Attribute nodes.");
632
0
                    ret = 0;
633
0
                    break;
634
0
                }
635
0
            }
636
0
            if (ret == 0) {
637
0
                break;
638
0
            }
639
640
0
            for (Py_ssize_t i = 0; i < asdl_seq_LEN(p->v.MatchClass.kwd_attrs); i++) {
641
0
                PyObject *identifier = asdl_seq_GET(p->v.MatchClass.kwd_attrs, i);
642
0
                if (!validate_name(identifier)) {
643
0
                    ret = 0;
644
0
                    break;
645
0
                }
646
0
            }
647
0
            if (ret == 0) {
648
0
                break;
649
0
            }
650
651
0
            if (!validate_patterns(p->v.MatchClass.patterns, /*star_ok=*/0)) {
652
0
                ret = 0;
653
0
                break;
654
0
            }
655
656
0
            ret = validate_patterns(p->v.MatchClass.kwd_patterns, /*star_ok=*/0);
657
0
            break;
658
0
        case MatchStar_kind:
659
0
            if (!star_ok) {
660
0
                PyErr_SetString(PyExc_ValueError, "can't use MatchStar here");
661
0
                ret = 0;
662
0
                break;
663
0
            }
664
0
            ret = p->v.MatchStar.name == NULL || validate_capture(p->v.MatchStar.name);
665
0
            break;
666
0
        case MatchAs_kind:
667
0
            if (p->v.MatchAs.name && !validate_capture(p->v.MatchAs.name)) {
668
0
                ret = 0;
669
0
                break;
670
0
            }
671
0
            if (p->v.MatchAs.pattern == NULL) {
672
0
                ret = 1;
673
0
            }
674
0
            else if (p->v.MatchAs.name == NULL) {
675
0
                PyErr_SetString(PyExc_ValueError,
676
0
                                "MatchAs must specify a target name if a pattern is given");
677
0
                ret = 0;
678
0
            }
679
0
            else {
680
0
                ret = validate_pattern(p->v.MatchAs.pattern, /*star_ok=*/0);
681
0
            }
682
0
            break;
683
0
        case MatchOr_kind:
684
0
            if (asdl_seq_LEN(p->v.MatchOr.patterns) < 2) {
685
0
                PyErr_SetString(PyExc_ValueError,
686
0
                                "MatchOr requires at least 2 patterns");
687
0
                ret = 0;
688
0
                break;
689
0
            }
690
0
            ret = validate_patterns(p->v.MatchOr.patterns, /*star_ok=*/0);
691
0
            break;
692
    // No default case, so the compiler will emit a warning if new pattern
693
    // kinds are added without being handled here
694
0
    }
695
0
    if (ret < 0) {
696
0
        PyErr_SetString(PyExc_SystemError, "unexpected pattern");
697
0
        ret = 0;
698
0
    }
699
0
    LEAVE_RECURSIVE();
700
0
    return ret;
701
0
}
702
703
static int
704
_validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
705
0
{
706
0
    if (asdl_seq_LEN(seq))
707
0
        return 1;
708
0
    PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
709
0
    return 0;
710
0
}
711
0
#define validate_nonempty_seq(seq, what, owner) _validate_nonempty_seq((asdl_seq*)seq, what, owner)
712
713
static int
714
validate_assignlist(asdl_expr_seq *targets, expr_context_ty ctx)
715
0
{
716
0
    assert(!PyErr_Occurred());
717
0
    return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
718
0
        validate_exprs(targets, ctx, 0);
719
0
}
720
721
static int
722
validate_body(asdl_stmt_seq *body, const char *owner)
723
0
{
724
0
    assert(!PyErr_Occurred());
725
0
    return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
726
0
}
727
728
static int
729
validate_stmt(stmt_ty stmt)
730
0
{
731
0
    assert(!PyErr_Occurred());
732
0
    VALIDATE_POSITIONS(stmt);
733
0
    int ret = -1;
734
0
    ENTER_RECURSIVE();
735
0
    switch (stmt->kind) {
736
0
    case FunctionDef_kind:
737
0
        ret = validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
738
0
            validate_type_params(stmt->v.FunctionDef.type_params) &&
739
0
            validate_arguments(stmt->v.FunctionDef.args) &&
740
0
            validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
741
0
            (!stmt->v.FunctionDef.returns ||
742
0
             validate_expr(stmt->v.FunctionDef.returns, Load));
743
0
        break;
744
0
    case ClassDef_kind:
745
0
        ret = validate_body(stmt->v.ClassDef.body, "ClassDef") &&
746
0
            validate_type_params(stmt->v.ClassDef.type_params) &&
747
0
            validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
748
0
            validate_keywords(stmt->v.ClassDef.keywords) &&
749
0
            validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
750
0
        break;
751
0
    case Return_kind:
752
0
        ret = !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
753
0
        break;
754
0
    case Delete_kind:
755
0
        ret = validate_assignlist(stmt->v.Delete.targets, Del);
756
0
        break;
757
0
    case Assign_kind:
758
0
        ret = validate_assignlist(stmt->v.Assign.targets, Store) &&
759
0
            validate_expr(stmt->v.Assign.value, Load);
760
0
        break;
761
0
    case AugAssign_kind:
762
0
        ret = validate_expr(stmt->v.AugAssign.target, Store) &&
763
0
            validate_expr(stmt->v.AugAssign.value, Load);
764
0
        break;
765
0
    case AnnAssign_kind:
766
0
        if (stmt->v.AnnAssign.target->kind != Name_kind &&
767
0
            stmt->v.AnnAssign.simple) {
768
0
            PyErr_SetString(PyExc_TypeError,
769
0
                            "AnnAssign with simple non-Name target");
770
0
            return 0;
771
0
        }
772
0
        ret = validate_expr(stmt->v.AnnAssign.target, Store) &&
773
0
               (!stmt->v.AnnAssign.value ||
774
0
                validate_expr(stmt->v.AnnAssign.value, Load)) &&
775
0
               validate_expr(stmt->v.AnnAssign.annotation, Load);
776
0
        break;
777
0
    case TypeAlias_kind:
778
0
        if (stmt->v.TypeAlias.name->kind != Name_kind) {
779
0
            PyErr_SetString(PyExc_TypeError,
780
0
                            "TypeAlias with non-Name name");
781
0
            return 0;
782
0
        }
783
0
        ret = validate_expr(stmt->v.TypeAlias.name, Store) &&
784
0
            validate_type_params(stmt->v.TypeAlias.type_params) &&
785
0
            validate_expr(stmt->v.TypeAlias.value, Load);
786
0
        break;
787
0
    case For_kind:
788
0
        ret = validate_expr(stmt->v.For.target, Store) &&
789
0
            validate_expr(stmt->v.For.iter, Load) &&
790
0
            validate_body(stmt->v.For.body, "For") &&
791
0
            validate_stmts(stmt->v.For.orelse);
792
0
        break;
793
0
    case AsyncFor_kind:
794
0
        ret = validate_expr(stmt->v.AsyncFor.target, Store) &&
795
0
            validate_expr(stmt->v.AsyncFor.iter, Load) &&
796
0
            validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
797
0
            validate_stmts(stmt->v.AsyncFor.orelse);
798
0
        break;
799
0
    case While_kind:
800
0
        ret = validate_expr(stmt->v.While.test, Load) &&
801
0
            validate_body(stmt->v.While.body, "While") &&
802
0
            validate_stmts(stmt->v.While.orelse);
803
0
        break;
804
0
    case If_kind:
805
0
        ret = validate_expr(stmt->v.If.test, Load) &&
806
0
            validate_body(stmt->v.If.body, "If") &&
807
0
            validate_stmts(stmt->v.If.orelse);
808
0
        break;
809
0
    case With_kind:
810
0
        if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
811
0
            return 0;
812
0
        for (Py_ssize_t i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
813
0
            withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
814
0
            if (!validate_expr(item->context_expr, Load) ||
815
0
                (item->optional_vars && !validate_expr(item->optional_vars, Store)))
816
0
                return 0;
817
0
        }
818
0
        ret = validate_body(stmt->v.With.body, "With");
819
0
        break;
820
0
    case AsyncWith_kind:
821
0
        if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
822
0
            return 0;
823
0
        for (Py_ssize_t i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
824
0
            withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
825
0
            if (!validate_expr(item->context_expr, Load) ||
826
0
                (item->optional_vars && !validate_expr(item->optional_vars, Store)))
827
0
                return 0;
828
0
        }
829
0
        ret = validate_body(stmt->v.AsyncWith.body, "AsyncWith");
830
0
        break;
831
0
    case Match_kind:
832
0
        if (!validate_expr(stmt->v.Match.subject, Load)
833
0
            || !validate_nonempty_seq(stmt->v.Match.cases, "cases", "Match")) {
834
0
            return 0;
835
0
        }
836
0
        for (Py_ssize_t i = 0; i < asdl_seq_LEN(stmt->v.Match.cases); i++) {
837
0
            match_case_ty m = asdl_seq_GET(stmt->v.Match.cases, i);
838
0
            if (!validate_pattern(m->pattern, /*star_ok=*/0)
839
0
                || (m->guard && !validate_expr(m->guard, Load))
840
0
                || !validate_body(m->body, "match_case")) {
841
0
                return 0;
842
0
            }
843
0
        }
844
0
        ret = 1;
845
0
        break;
846
0
    case Raise_kind:
847
0
        if (stmt->v.Raise.exc) {
848
0
            ret = validate_expr(stmt->v.Raise.exc, Load) &&
849
0
                (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
850
0
            break;
851
0
        }
852
0
        if (stmt->v.Raise.cause) {
853
0
            PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
854
0
            return 0;
855
0
        }
856
0
        ret = 1;
857
0
        break;
858
0
    case Try_kind:
859
0
        if (!validate_body(stmt->v.Try.body, "Try"))
860
0
            return 0;
861
0
        if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
862
0
            !asdl_seq_LEN(stmt->v.Try.finalbody)) {
863
0
            PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
864
0
            return 0;
865
0
        }
866
0
        if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
867
0
            asdl_seq_LEN(stmt->v.Try.orelse)) {
868
0
            PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
869
0
            return 0;
870
0
        }
871
0
        for (Py_ssize_t i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
872
0
            excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
873
0
            VALIDATE_POSITIONS(handler);
874
0
            if ((handler->v.ExceptHandler.type &&
875
0
                 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
876
0
                !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
877
0
                return 0;
878
0
        }
879
0
        ret = (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
880
0
                validate_stmts(stmt->v.Try.finalbody)) &&
881
0
            (!asdl_seq_LEN(stmt->v.Try.orelse) ||
882
0
             validate_stmts(stmt->v.Try.orelse));
883
0
        break;
884
0
    case TryStar_kind:
885
0
        if (!validate_body(stmt->v.TryStar.body, "TryStar"))
886
0
            return 0;
887
0
        if (!asdl_seq_LEN(stmt->v.TryStar.handlers) &&
888
0
            !asdl_seq_LEN(stmt->v.TryStar.finalbody)) {
889
0
            PyErr_SetString(PyExc_ValueError, "TryStar has neither except handlers nor finalbody");
890
0
            return 0;
891
0
        }
892
0
        if (!asdl_seq_LEN(stmt->v.TryStar.handlers) &&
893
0
            asdl_seq_LEN(stmt->v.TryStar.orelse)) {
894
0
            PyErr_SetString(PyExc_ValueError, "TryStar has orelse but no except handlers");
895
0
            return 0;
896
0
        }
897
0
        for (Py_ssize_t i = 0; i < asdl_seq_LEN(stmt->v.TryStar.handlers); i++) {
898
0
            excepthandler_ty handler = asdl_seq_GET(stmt->v.TryStar.handlers, i);
899
0
            if ((handler->v.ExceptHandler.type &&
900
0
                 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
901
0
                !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
902
0
                return 0;
903
0
        }
904
0
        ret = (!asdl_seq_LEN(stmt->v.TryStar.finalbody) ||
905
0
                validate_stmts(stmt->v.TryStar.finalbody)) &&
906
0
            (!asdl_seq_LEN(stmt->v.TryStar.orelse) ||
907
0
             validate_stmts(stmt->v.TryStar.orelse));
908
0
        break;
909
0
    case Assert_kind:
910
0
        ret = validate_expr(stmt->v.Assert.test, Load) &&
911
0
            (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
912
0
        break;
913
0
    case Import_kind:
914
0
        ret = validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
915
0
        break;
916
0
    case ImportFrom_kind:
917
0
        if (stmt->v.ImportFrom.level < 0) {
918
0
            PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
919
0
            return 0;
920
0
        }
921
0
        ret = validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
922
0
        break;
923
0
    case Global_kind:
924
0
        ret = validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
925
0
        break;
926
0
    case Nonlocal_kind:
927
0
        ret = validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
928
0
        break;
929
0
    case Expr_kind:
930
0
        ret = validate_expr(stmt->v.Expr.value, Load);
931
0
        break;
932
0
    case AsyncFunctionDef_kind:
933
0
        ret = validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
934
0
            validate_type_params(stmt->v.AsyncFunctionDef.type_params) &&
935
0
            validate_arguments(stmt->v.AsyncFunctionDef.args) &&
936
0
            validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
937
0
            (!stmt->v.AsyncFunctionDef.returns ||
938
0
             validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
939
0
        break;
940
0
    case Pass_kind:
941
0
    case Break_kind:
942
0
    case Continue_kind:
943
0
        ret = 1;
944
0
        break;
945
    // No default case so compiler emits warning for unhandled cases
946
0
    }
947
0
    if (ret < 0) {
948
0
        PyErr_SetString(PyExc_SystemError, "unexpected statement");
949
0
        ret = 0;
950
0
    }
951
0
    LEAVE_RECURSIVE();
952
0
    return ret;
953
0
}
954
955
static int
956
validate_stmts(asdl_stmt_seq *seq)
957
0
{
958
0
    assert(!PyErr_Occurred());
959
0
    for (Py_ssize_t i = 0; i < asdl_seq_LEN(seq); i++) {
960
0
        stmt_ty stmt = asdl_seq_GET(seq, i);
961
0
        if (stmt) {
962
0
            if (!validate_stmt(stmt))
963
0
                return 0;
964
0
        }
965
0
        else {
966
0
            PyErr_SetString(PyExc_ValueError,
967
0
                            "None disallowed in statement list");
968
0
            return 0;
969
0
        }
970
0
    }
971
0
    return 1;
972
0
}
973
974
static int
975
validate_exprs(asdl_expr_seq *exprs, expr_context_ty ctx, int null_ok)
976
0
{
977
0
    assert(!PyErr_Occurred());
978
0
    for (Py_ssize_t i = 0; i < asdl_seq_LEN(exprs); i++) {
979
0
        expr_ty expr = asdl_seq_GET(exprs, i);
980
0
        if (expr) {
981
0
            if (!validate_expr(expr, ctx))
982
0
                return 0;
983
0
        }
984
0
        else if (!null_ok) {
985
0
            PyErr_SetString(PyExc_ValueError,
986
0
                            "None disallowed in expression list");
987
0
            return 0;
988
0
        }
989
990
0
    }
991
0
    return 1;
992
0
}
993
994
static int
995
validate_patterns(asdl_pattern_seq *patterns, int star_ok)
996
0
{
997
0
    assert(!PyErr_Occurred());
998
0
    for (Py_ssize_t i = 0; i < asdl_seq_LEN(patterns); i++) {
999
0
        pattern_ty pattern = asdl_seq_GET(patterns, i);
1000
0
        if (!validate_pattern(pattern, star_ok)) {
1001
0
            return 0;
1002
0
        }
1003
0
    }
1004
0
    return 1;
1005
0
}
1006
1007
static int
1008
validate_typeparam(type_param_ty tp)
1009
0
{
1010
0
    VALIDATE_POSITIONS(tp);
1011
0
    int ret = -1;
1012
0
    switch (tp->kind) {
1013
0
        case TypeVar_kind:
1014
0
            ret = validate_name(tp->v.TypeVar.name) &&
1015
0
                (!tp->v.TypeVar.bound ||
1016
0
                 validate_expr(tp->v.TypeVar.bound, Load)) &&
1017
0
                (!tp->v.TypeVar.default_value ||
1018
0
                 validate_expr(tp->v.TypeVar.default_value, Load));
1019
0
            break;
1020
0
        case ParamSpec_kind:
1021
0
            ret = validate_name(tp->v.ParamSpec.name) &&
1022
0
                (!tp->v.ParamSpec.default_value ||
1023
0
                 validate_expr(tp->v.ParamSpec.default_value, Load));
1024
0
            break;
1025
0
        case TypeVarTuple_kind:
1026
0
            ret = validate_name(tp->v.TypeVarTuple.name) &&
1027
0
                (!tp->v.TypeVarTuple.default_value ||
1028
0
                 validate_expr(tp->v.TypeVarTuple.default_value, Load));
1029
0
            break;
1030
0
    }
1031
0
    return ret;
1032
0
}
1033
1034
static int
1035
validate_type_params(asdl_type_param_seq *tps)
1036
0
{
1037
0
    Py_ssize_t i;
1038
0
    for (i = 0; i < asdl_seq_LEN(tps); i++) {
1039
0
        type_param_ty tp = asdl_seq_GET(tps, i);
1040
0
        if (tp) {
1041
0
            if (!validate_typeparam(tp))
1042
0
                return 0;
1043
0
        }
1044
0
    }
1045
0
    return 1;
1046
0
}
1047
1048
int
1049
_PyAST_Validate(mod_ty mod)
1050
0
{
1051
0
    assert(!PyErr_Occurred());
1052
0
    int res = -1;
1053
1054
0
    switch (mod->kind) {
1055
0
    case Module_kind:
1056
0
        res = validate_stmts(mod->v.Module.body);
1057
0
        break;
1058
0
    case Interactive_kind:
1059
0
        res = validate_stmts(mod->v.Interactive.body);
1060
0
        break;
1061
0
    case Expression_kind:
1062
0
        res = validate_expr(mod->v.Expression.body, Load);
1063
0
        break;
1064
0
    case FunctionType_kind:
1065
0
        res = validate_exprs(mod->v.FunctionType.argtypes, Load, /*null_ok=*/0) &&
1066
0
              validate_expr(mod->v.FunctionType.returns, Load);
1067
0
        break;
1068
    // No default case so compiler emits warning for unhandled cases
1069
0
    }
1070
1071
0
    if (res < 0) {
1072
0
        PyErr_SetString(PyExc_SystemError, "impossible module node");
1073
0
        return 0;
1074
0
    }
1075
0
    return res;
1076
0
}
1077
1078
PyObject *
1079
_PyAST_GetDocString(asdl_stmt_seq *body)
1080
31.4k
{
1081
31.4k
    if (!asdl_seq_LEN(body)) {
1082
982
        return NULL;
1083
982
    }
1084
30.4k
    stmt_ty st = asdl_seq_GET(body, 0);
1085
30.4k
    if (st->kind != Expr_kind) {
1086
13.1k
        return NULL;
1087
13.1k
    }
1088
17.3k
    expr_ty e = st->v.Expr.value;
1089
17.3k
    if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
1090
3.26k
        return e->v.Constant.value;
1091
3.26k
    }
1092
14.0k
    return NULL;
1093
17.3k
}