Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Parser/pegen_errors.c
Line
Count
Source
1
#include <Python.h>
2
#include <errcode.h>
3
4
#include "pycore_pyerrors.h"      // _PyErr_ProgramDecodedTextObject()
5
#include "pycore_runtime.h"       // _Py_ID()
6
#include "pycore_tuple.h"         // _PyTuple_FromPair
7
#include "pegen.h"
8
#include "tokenizer/tokenizer.h"
9
10
// TOKENIZER ERRORS
11
12
void *
13
_PyPegen_raise_incomplete_input(Parser *p)
14
0
{
15
0
    if ((p->flags & PyPARSE_ALLOW_INCOMPLETE_INPUT) && !PyErr_Occurred()) {
16
0
        return _PyPegen_raise_error(p, PyExc_IncompleteInputError, 0,
17
0
                                    "incomplete input");
18
0
    }
19
0
    return NULL;
20
0
}
21
22
static inline void
23
2.10k
raise_unclosed_parentheses_error(Parser *p) {
24
2.10k
       _PyTokenizer_Info info = _PyTokenizer_GetInfo(p->tok);
25
2.10k
       int error_lineno = info.delimiter_loc.lineno;
26
2.10k
       int error_col = info.delimiter_loc.byte_col;
27
2.10k
       RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError,
28
2.10k
                                  error_lineno, error_col, error_lineno, -1,
29
2.10k
                                  "'%c' was never closed",
30
2.10k
                                  info.delimiter);
31
2.10k
}
32
33
int
34
_Pypegen_tokenizer_error(Parser *p)
35
4.02k
{
36
4.02k
    if (PyErr_Occurred()) {
37
1.93k
        return -1;
38
1.93k
    }
39
40
2.09k
    const char *msg = NULL;
41
2.09k
    PyObject* errtype = PyExc_SyntaxError;
42
2.09k
    Py_ssize_t col_offset = -1;
43
2.09k
    p->error_indicator = 1;
44
2.09k
    _PyTokenizer_Info info = _PyTokenizer_GetInfo(p->tok);
45
2.09k
    if ((p->flags & PyPARSE_ALLOW_INCOMPLETE_INPUT) &&
46
0
        (info.status == E_EOF || info.status == E_INCOMPLETE))
47
0
    {
48
0
        _PyPegen_raise_incomplete_input(p);
49
0
        return -1;
50
0
    }
51
2.09k
    switch (info.status) {
52
0
        case E_INCOMPLETE:
53
            // The diagnostic pass rejected this unfinished token. Let the
54
            // parser report the syntax error at its original failure point.
55
0
            return -1;
56
0
        case E_TOKEN:
57
0
            msg = "invalid token";
58
0
            break;
59
2.05k
        case E_EOF:
60
2.05k
            if (info.level) {
61
2.00k
                raise_unclosed_parentheses_error(p);
62
2.00k
            } else {
63
45
                RAISE_SYNTAX_ERROR("unexpected EOF while parsing");
64
45
            }
65
2.05k
            return -1;
66
6
        case E_DEDENT:
67
6
            RAISE_INDENTATION_ERROR("unindent does not match any outer indentation level");
68
6
            return -1;
69
0
        case E_INTR:
70
0
            if (!PyErr_Occurred()) {
71
0
                PyErr_SetNone(PyExc_KeyboardInterrupt);
72
0
            }
73
0
            return -1;
74
0
        case E_NOMEM:
75
0
            PyErr_NoMemory();
76
0
            return -1;
77
1
        case E_TABSPACE:
78
1
            errtype = PyExc_TabError;
79
1
            msg = "inconsistent use of tabs and spaces in indentation";
80
1
            break;
81
0
        case E_TOODEEP:
82
0
            errtype = PyExc_IndentationError;
83
0
            msg = "too many levels of indentation";
84
0
            break;
85
33
        case E_LINECONT: {
86
33
            col_offset = info.cursor - info.line_span.start - 1;
87
33
            msg = "unexpected character after line continuation character";
88
33
            break;
89
0
        }
90
0
        case E_COLUMNOVERFLOW:
91
0
            PyErr_SetString(PyExc_OverflowError,
92
0
                    "Parser column offset overflow - source line is too big");
93
0
            return -1;
94
0
        default:
95
0
            msg = "unknown parsing error";
96
2.09k
    }
97
98
34
    RAISE_ERROR_KNOWN_LOCATION(p, errtype, info.location.lineno,
99
34
                               col_offset >= 0 ? col_offset : 0,
100
34
                               info.location.lineno, -1, msg);
101
34
    return -1;
102
2.09k
}
103
104
int
105
_Pypegen_raise_decode_error(Parser *p)
106
123
{
107
123
    return _Pypegen_raise_decode_error_known_location(p, NULL);
108
123
}
109
110
int
111
_Pypegen_raise_decode_error_known_location(Parser *p, expr_ty a)
112
131
{
113
131
    assert(PyErr_Occurred());
114
131
    const char *errtype = NULL;
115
131
    if (PyErr_ExceptionMatches(PyExc_UnicodeError)) {
116
108
        errtype = "unicode error";
117
108
    }
118
23
    else if (PyErr_ExceptionMatches(PyExc_ValueError)) {
119
4
        errtype = "value error";
120
4
    }
121
131
    if (errtype) {
122
112
        PyObject *type;
123
112
        PyObject *value;
124
112
        PyObject *tback;
125
112
        PyObject *errstr;
126
112
        PyErr_Fetch(&type, &value, &tback);
127
112
        errstr = PyObject_Str(value);
128
112
        if (errstr) {
129
112
            if (a != NULL) {
130
8
                RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "(%s) %U", errtype, errstr);
131
8
            }
132
104
            else {
133
104
                RAISE_SYNTAX_ERROR("(%s) %U", errtype, errstr);
134
104
            }
135
112
            Py_DECREF(errstr);
136
112
        }
137
0
        else {
138
0
            PyErr_Clear();
139
0
            if (a != NULL) {
140
0
                RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "(%s) unknown error", errtype);
141
0
            }
142
0
            else {
143
0
                RAISE_SYNTAX_ERROR("(%s) unknown error", errtype);
144
0
            }
145
0
        }
146
112
        Py_XDECREF(type);
147
112
        Py_XDECREF(value);
148
112
        Py_XDECREF(tback);
149
112
    }
150
151
131
    return -1;
152
131
}
153
154
static int
155
51.4k
_PyPegen_tokenize_full_source_to_check_for_errors(Parser *p) {
156
    // Tokenize the whole input to see if there are any tokenization
157
    // errors such as mismatching parentheses. These will get priority
158
    // over generic syntax errors only if the line number of the error is
159
    // before the one that we had for the generic error.
160
161
    // We don't want to tokenize to the end for interactive input
162
51.4k
    if (_PyTokenizer_IsInteractive(p->tok)) {
163
0
        return 0;
164
0
    }
165
166
51.4k
    PyObject *type, *value, *traceback;
167
51.4k
    PyErr_Fetch(&type, &value, &traceback);
168
169
51.4k
    Token *current_token = p->known_err_token != NULL ? p->known_err_token : p->tokens[p->fill - 1];
170
51.4k
    Py_ssize_t current_err_line = current_token->lineno;
171
172
51.4k
    int ret = 0;
173
51.4k
    struct token new_token;
174
51.4k
    _PyToken_Init(&new_token);
175
176
224k
    for (;;) {
177
224k
        _PyTokenizer_Get(p->tok, &new_token);
178
224k
        switch (new_token.type) {
179
3.15k
            case ERRORTOKEN: {
180
3.15k
                if (PyErr_Occurred()) {
181
665
                    ret = -1;
182
665
                    goto exit;
183
665
                }
184
2.49k
                _PyTokenizer_Info info = _PyTokenizer_GetInfo(p->tok);
185
2.49k
                if (info.level != 0) {
186
2.47k
                    int error_lineno = info.delimiter_loc.lineno;
187
2.47k
                    if (current_err_line > error_lineno) {
188
98
                        raise_unclosed_parentheses_error(p);
189
98
                        ret = -1;
190
98
                        goto exit;
191
98
                    }
192
2.47k
                }
193
2.39k
                break;
194
2.49k
            }
195
48.3k
            case ENDMARKER:
196
48.3k
                break;
197
173k
            default:
198
173k
                continue;
199
224k
        }
200
50.7k
        break;
201
224k
    }
202
203
204
51.4k
exit:
205
51.4k
    _PyToken_Free(&new_token);
206
51.4k
    _PyTokenizer_Info info = _PyTokenizer_GetInfo(p->tok);
207
    // Preserve expression errors over later formatted-string errors.
208
51.4k
    if (PyErr_Occurred() && !info.in_formatted_string) {
209
543
        Py_XDECREF(value);
210
543
        Py_XDECREF(type);
211
543
        Py_XDECREF(traceback);
212
50.9k
    } else {
213
50.9k
        PyErr_Restore(type, value, traceback);
214
50.9k
    }
215
51.4k
    return ret;
216
51.4k
}
217
218
// PARSER ERRORS
219
220
void *
221
_PyPegen_raise_error(Parser *p, PyObject *errtype, int use_mark, const char *errmsg, ...)
222
1.18k
{
223
    // Bail out if we already have an error set.
224
1.18k
    if (p->error_indicator && PyErr_Occurred()) {
225
242
        return NULL;
226
242
    }
227
940
    if (p->fill == 0) {
228
0
        va_list va;
229
0
        va_start(va, errmsg);
230
0
        _PyPegen_raise_error_known_location(p, errtype, 0, 0, 0, -1, errmsg, va);
231
0
        va_end(va);
232
0
        return NULL;
233
0
    }
234
940
    if (use_mark && p->mark == p->fill && _PyPegen_fill_token(p) < 0) {
235
0
        p->error_indicator = 1;
236
0
        return NULL;
237
0
    }
238
940
    Token *t = p->known_err_token != NULL
239
940
                   ? p->known_err_token
240
940
                   : p->tokens[use_mark ? p->mark : p->fill - 1];
241
940
    Py_ssize_t col_offset;
242
940
    Py_ssize_t end_col_offset = -1;
243
940
    if (t->col_offset == -1) {
244
242
        _PyTokenizer_Info info = _PyTokenizer_GetInfo(p->tok);
245
242
        if (info.diagnostic.location.lineno != 0) {
246
0
            col_offset = info.diagnostic.location.byte_col;
247
242
        } else if (info.cursor == info.input_span.start) {
248
5
            col_offset = 0;
249
237
        } else {
250
237
            col_offset = Py_SAFE_DOWNCAST(
251
237
                info.cursor - info.line_span.start, intptr_t, int);
252
237
        }
253
698
    } else {
254
698
        col_offset = t->col_offset + 1;
255
698
    }
256
257
940
    if (t->end_col_offset != -1) {
258
698
        end_col_offset = t->end_col_offset + 1;
259
698
    }
260
261
940
    va_list va;
262
940
    va_start(va, errmsg);
263
940
    _PyPegen_raise_error_known_location(p, errtype, t->lineno, col_offset, t->end_lineno, end_col_offset, errmsg, va);
264
940
    va_end(va);
265
266
940
    return NULL;
267
940
}
268
269
static PyObject *
270
get_error_line_from_source(Parser *p, Py_ssize_t lineno)
271
319
{
272
319
    if (_PyTokenizer_RetainedSource(p->tok) == NULL) {
273
0
        return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
274
0
    }
275
319
    Py_ssize_t relative_lineno = p->starting_lineno ? lineno - p->starting_lineno + 1 : lineno;
276
319
    Py_ssize_t len;
277
319
    const char *line = _PyTokenizer_LineView(p->tok, relative_lineno, &len);
278
319
    return PyUnicode_DecodeUTF8(line, len, "replace");
279
319
}
280
281
void *
282
_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
283
                                    Py_ssize_t lineno, Py_ssize_t col_offset,
284
                                    Py_ssize_t end_lineno, Py_ssize_t end_col_offset,
285
                                    const char *errmsg, va_list va)
286
54.5k
{
287
    // Bail out if we already have an error set.
288
54.5k
    if (p->error_indicator && PyErr_Occurred()) {
289
662
        return NULL;
290
662
    }
291
53.8k
    PyObject *value = NULL;
292
53.8k
    PyObject *errstr = NULL;
293
53.8k
    PyObject *error_line = NULL;
294
53.8k
    PyObject *tmp = NULL;
295
53.8k
    p->error_indicator = 1;
296
53.8k
    _PyTokenizer_Info info = _PyTokenizer_GetInfo(p->tok);
297
53.8k
    _PyTok_Loc location = info.location;
298
53.8k
    _PyTok_Span text_span = info.line_span;
299
300
53.8k
    if (errtype == PyExc_IncompleteInputError &&
301
0
        info.diagnostic.location.lineno != 0) {
302
0
        location = info.diagnostic.location;
303
0
        text_span = info.diagnostic.text_span;
304
0
        lineno = end_lineno = location.lineno;
305
0
        col_offset = location.byte_col;
306
0
        end_col_offset = -1;
307
0
    }
308
309
53.8k
    if (end_lineno == CURRENT_POS) {
310
34
        end_lineno = location.lineno;
311
34
    }
312
53.8k
    if (end_col_offset == CURRENT_POS) {
313
34
        end_col_offset = location.byte_col;
314
34
    }
315
316
53.8k
    errstr = PyUnicode_FromFormatV(errmsg, va);
317
53.8k
    if (!errstr) {
318
0
        goto error;
319
0
    }
320
321
53.8k
    if (info.is_interactive && _PyTokenizer_RetainedSource(p->tok) != NULL) {
322
0
        error_line = get_error_line_from_source(p, lineno);
323
0
    }
324
53.8k
    else if (p->start_rule == Py_file_input) {
325
53.8k
        error_line = _PyErr_ProgramDecodedTextObject(info.filename,
326
53.8k
                                                     (int) lineno, info.encoding);
327
53.8k
    }
328
329
53.8k
    if (!error_line) {
330
        /* PyErr_ProgramTextObject was not called or returned NULL. If it was not called,
331
           then we need to find the error line from some other source, because
332
           p->start_rule != Py_file_input. If it returned NULL, then it either unexpectedly
333
           failed or we're parsing from a string or the REPL. There's a third edge case where
334
           we're actually parsing from a file, which has an E_EOF SyntaxError and in that case
335
           `PyErr_ProgramTextObject` fails because lineno points to last_file_line + 1, which
336
           does not physically exist */
337
53.8k
        assert(!info.is_file || info.status == E_EOF);
338
339
53.8k
        if (location.lineno <= lineno &&
340
53.5k
                info.input_span.end > info.input_span.start) {
341
53.5k
            Py_ssize_t size;
342
53.5k
            const char *line = _PyTokenizer_SpanView(
343
53.5k
                p->tok, text_span, &size);
344
53.5k
            if (errtype == PyExc_IncompleteInputError && size > 0 &&
345
0
                (info.implicit_newline == _PyTokenizer_APPEND_NEWLINE ||
346
0
                 (info.implicit_newline == _PyTokenizer_IMPLY_NEWLINE &&
347
0
                  p->start_rule != Py_file_input))) {
348
0
                size--;
349
0
            }
350
53.5k
            error_line = PyUnicode_DecodeUTF8(line, size, "replace");
351
53.5k
        }
352
277
        else if (!info.is_file) {
353
277
            error_line = get_error_line_from_source(p, lineno);
354
277
        }
355
0
        else {
356
0
            error_line = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
357
0
        }
358
53.8k
        if (!error_line) {
359
0
            goto error;
360
0
        }
361
53.8k
    }
362
363
53.8k
    Py_ssize_t col_number = col_offset;
364
53.8k
    Py_ssize_t end_col_number = end_col_offset;
365
366
53.8k
    col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
367
53.8k
    if (col_number < 0) {
368
0
        goto error;
369
0
    }
370
371
53.8k
    if (end_col_offset > 0) {
372
51.4k
        PyObject *end_line = end_lineno != lineno && _PyTokenizer_RetainedSource(p->tok) != NULL
373
51.4k
            ? get_error_line_from_source(p, end_lineno) : Py_NewRef(error_line);
374
51.4k
        if (end_line == NULL) {
375
0
            goto error;
376
0
        }
377
51.4k
        end_col_number = _PyPegen_byte_offset_to_character_offset(end_line, end_col_offset);
378
51.4k
        Py_DECREF(end_line);
379
51.4k
        if (end_col_number < 0) {
380
0
            goto error;
381
0
        }
382
51.4k
    }
383
384
53.8k
    tmp = Py_BuildValue("(OnnNnn)", info.filename, lineno, col_number, error_line, end_lineno, end_col_number);
385
53.8k
    error_line = NULL;
386
53.8k
    if (!tmp) {
387
0
        goto error;
388
0
    }
389
53.8k
    value = _PyTuple_FromPair(errstr, tmp);
390
53.8k
    Py_DECREF(tmp);
391
53.8k
    if (!value) {
392
0
        goto error;
393
0
    }
394
53.8k
    PyErr_SetObject(errtype, value);
395
396
53.8k
    Py_DECREF(errstr);
397
53.8k
    Py_DECREF(value);
398
53.8k
    return NULL;
399
400
0
error:
401
0
    Py_XDECREF(errstr);
402
0
    Py_XDECREF(error_line);
403
0
    return NULL;
404
53.8k
}
405
406
void
407
55.7k
_Pypegen_set_syntax_error(Parser* p, Token* last_token) {
408
55.7k
    _PyTokenizer_Info info = _PyTokenizer_GetInfo(p->tok);
409
    // Existing syntax error
410
55.7k
    if (PyErr_Occurred()) {
411
        // Prioritize tokenizer errors to custom syntax errors raised
412
        // on the second phase only if the errors come from the parser.
413
6.17k
        int is_tok_ok = (info.status == E_DONE || info.status == E_OK);
414
6.17k
        if (is_tok_ok && PyErr_ExceptionMatches(PyExc_SyntaxError)) {
415
1.98k
            _PyPegen_tokenize_full_source_to_check_for_errors(p);
416
1.98k
        }
417
        // Propagate the existing syntax error.
418
6.17k
        return;
419
6.17k
    }
420
    // Initialization error
421
49.5k
    if (p->fill == 0) {
422
0
        RAISE_SYNTAX_ERROR("error at start before reading any input");
423
0
    }
424
    // Parser encountered EOF (End of File) unexpectedtly
425
49.5k
    if (last_token->type == ERRORTOKEN && info.status == E_EOF) {
426
0
        if (info.level) {
427
0
            raise_unclosed_parentheses_error(p);
428
0
        } else {
429
0
            RAISE_SYNTAX_ERROR("unexpected EOF while parsing");
430
0
        }
431
0
        return;
432
0
    }
433
    // Indentation error in the tokenizer
434
49.5k
    if (last_token->type == INDENT || last_token->type == DEDENT) {
435
72
        RAISE_INDENTATION_ERROR(last_token->type == INDENT ? "unexpected indent" : "unexpected unindent");
436
72
        return;
437
72
    }
438
    // Unknown error (generic case)
439
440
    // Use the last token we found on the first pass to avoid reporting
441
    // incorrect locations for generic syntax errors just because we reached
442
    // further away when trying to find specific syntax errors in the second
443
    // pass.
444
49.4k
    RAISE_SYNTAX_ERROR_KNOWN_LOCATION(last_token, "invalid syntax");
445
    // _PyPegen_tokenize_full_source_to_check_for_errors will override the existing
446
    // generic SyntaxError we just raised if errors are found.
447
49.4k
    _PyPegen_tokenize_full_source_to_check_for_errors(p);
448
49.4k
}
449
450
void
451
_Pypegen_stack_overflow(Parser *p)
452
57
{
453
57
    p->error_indicator = 1;
454
57
    PyErr_SetString(PyExc_MemoryError,
455
57
        "Parser stack overflowed - Python source too complex to parse");
456
57
}