Coverage Report

Created: 2025-07-11 06:59

/src/Python-3.8.3/Python/pythonrun.c
Line
Count
Source (jump to first uncovered line)
1
2
/* Top level execution of Python code (including in __main__) */
3
4
/* To help control the interfaces between the startup, execution and
5
 * shutdown code, the phases are split across separate modules (boostrap,
6
 * pythonrun, shutdown)
7
 */
8
9
/* TODO: Cull includes following phase split */
10
11
#include "Python.h"
12
13
#include "Python-ast.h"
14
#undef Yield   /* undefine macro conflicting with <winbase.h> */
15
#include "pycore_pyerrors.h"
16
#include "pycore_pylifecycle.h"
17
#include "pycore_pystate.h"
18
#include "grammar.h"
19
#include "node.h"
20
#include "token.h"
21
#include "parsetok.h"
22
#include "errcode.h"
23
#include "code.h"
24
#include "symtable.h"
25
#include "ast.h"
26
#include "marshal.h"
27
#include "osdefs.h"
28
#include <locale.h>
29
30
#ifdef HAVE_SIGNAL_H
31
#include <signal.h>
32
#endif
33
34
#ifdef MS_WINDOWS
35
#include "malloc.h" /* for alloca */
36
#endif
37
38
#ifdef MS_WINDOWS
39
#undef BYTE
40
#include "windows.h"
41
#endif
42
43
_Py_IDENTIFIER(builtins);
44
_Py_IDENTIFIER(excepthook);
45
_Py_IDENTIFIER(flush);
46
_Py_IDENTIFIER(last_traceback);
47
_Py_IDENTIFIER(last_type);
48
_Py_IDENTIFIER(last_value);
49
_Py_IDENTIFIER(ps1);
50
_Py_IDENTIFIER(ps2);
51
_Py_IDENTIFIER(stdin);
52
_Py_IDENTIFIER(stdout);
53
_Py_IDENTIFIER(stderr);
54
_Py_static_string(PyId_string, "<string>");
55
56
#ifdef __cplusplus
57
extern "C" {
58
#endif
59
60
extern grammar _PyParser_Grammar; /* From graminit.c */
61
62
/* Forward */
63
static void flush_io(void);
64
static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
65
                          PyCompilerFlags *, PyArena *);
66
static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
67
                              PyCompilerFlags *);
68
static void err_input(perrdetail *);
69
static void err_free(perrdetail *);
70
static int PyRun_InteractiveOneObjectEx(FILE *, PyObject *, PyCompilerFlags *);
71
72
/* Parse input from a file and execute it */
73
int
74
PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
75
                     PyCompilerFlags *flags)
76
0
{
77
0
    if (filename == NULL)
78
0
        filename = "???";
79
0
    if (Py_FdIsInteractive(fp, filename)) {
80
0
        int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
81
0
        if (closeit)
82
0
            fclose(fp);
83
0
        return err;
84
0
    }
85
0
    else
86
0
        return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
87
0
}
88
89
int
90
PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
91
0
{
92
0
    PyObject *filename, *v;
93
0
    int ret, err;
94
0
    PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
95
0
    int nomem_count = 0;
96
#ifdef Py_REF_DEBUG
97
    int show_ref_count = _PyInterpreterState_Get()->config.show_ref_count;
98
#endif
99
100
0
    filename = PyUnicode_DecodeFSDefault(filename_str);
101
0
    if (filename == NULL) {
102
0
        PyErr_Print();
103
0
        return -1;
104
0
    }
105
106
0
    if (flags == NULL) {
107
0
        flags = &local_flags;
108
0
    }
109
0
    v = _PySys_GetObjectId(&PyId_ps1);
110
0
    if (v == NULL) {
111
0
        _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> "));
112
0
        Py_XDECREF(v);
113
0
    }
114
0
    v = _PySys_GetObjectId(&PyId_ps2);
115
0
    if (v == NULL) {
116
0
        _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... "));
117
0
        Py_XDECREF(v);
118
0
    }
119
0
    err = 0;
120
0
    do {
121
0
        ret = PyRun_InteractiveOneObjectEx(fp, filename, flags);
122
0
        if (ret == -1 && PyErr_Occurred()) {
123
            /* Prevent an endless loop after multiple consecutive MemoryErrors
124
             * while still allowing an interactive command to fail with a
125
             * MemoryError. */
126
0
            if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
127
0
                if (++nomem_count > 16) {
128
0
                    PyErr_Clear();
129
0
                    err = -1;
130
0
                    break;
131
0
                }
132
0
            } else {
133
0
                nomem_count = 0;
134
0
            }
135
0
            PyErr_Print();
136
0
            flush_io();
137
0
        } else {
138
0
            nomem_count = 0;
139
0
        }
140
#ifdef Py_REF_DEBUG
141
        if (show_ref_count) {
142
            _PyDebug_PrintTotalRefs();
143
        }
144
#endif
145
0
    } while (ret != E_EOF);
146
0
    Py_DECREF(filename);
147
0
    return err;
148
0
}
149
150
/* compute parser flags based on compiler flags */
151
static int PARSER_FLAGS(PyCompilerFlags *flags)
152
16
{
153
16
    int parser_flags = 0;
154
16
    if (!flags)
155
14
        return 0;
156
2
    if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
157
0
        parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
158
2
    if (flags->cf_flags & PyCF_IGNORE_COOKIE)
159
2
        parser_flags |= PyPARSE_IGNORE_COOKIE;
160
2
    if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
161
0
        parser_flags |= PyPARSE_BARRY_AS_BDFL;
162
2
    if (flags->cf_flags & PyCF_TYPE_COMMENTS)
163
0
        parser_flags |= PyPARSE_TYPE_COMMENTS;
164
2
    return parser_flags;
165
16
}
166
167
#if 0
168
/* Keep an example of flags with future keyword support. */
169
#define PARSER_FLAGS(flags) \
170
    ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
171
                  PyPARSE_DONT_IMPLY_DEDENT : 0) \
172
                | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
173
                   PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
174
#endif
175
176
/* A PyRun_InteractiveOneObject() auxiliary function that does not print the
177
 * error on failure. */
178
static int
179
PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
180
                             PyCompilerFlags *flags)
181
0
{
182
0
    PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
183
0
    mod_ty mod;
184
0
    PyArena *arena;
185
0
    const char *ps1 = "", *ps2 = "", *enc = NULL;
186
0
    int errcode = 0;
187
0
    _Py_IDENTIFIER(encoding);
188
0
    _Py_IDENTIFIER(__main__);
189
190
0
    mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */
191
0
    if (mod_name == NULL) {
192
0
        return -1;
193
0
    }
194
195
0
    if (fp == stdin) {
196
        /* Fetch encoding from sys.stdin if possible. */
197
0
        v = _PySys_GetObjectId(&PyId_stdin);
198
0
        if (v && v != Py_None) {
199
0
            oenc = _PyObject_GetAttrId(v, &PyId_encoding);
200
0
            if (oenc)
201
0
                enc = PyUnicode_AsUTF8(oenc);
202
0
            if (!enc)
203
0
                PyErr_Clear();
204
0
        }
205
0
    }
206
0
    v = _PySys_GetObjectId(&PyId_ps1);
207
0
    if (v != NULL) {
208
0
        v = PyObject_Str(v);
209
0
        if (v == NULL)
210
0
            PyErr_Clear();
211
0
        else if (PyUnicode_Check(v)) {
212
0
            ps1 = PyUnicode_AsUTF8(v);
213
0
            if (ps1 == NULL) {
214
0
                PyErr_Clear();
215
0
                ps1 = "";
216
0
            }
217
0
        }
218
0
    }
219
0
    w = _PySys_GetObjectId(&PyId_ps2);
220
0
    if (w != NULL) {
221
0
        w = PyObject_Str(w);
222
0
        if (w == NULL)
223
0
            PyErr_Clear();
224
0
        else if (PyUnicode_Check(w)) {
225
0
            ps2 = PyUnicode_AsUTF8(w);
226
0
            if (ps2 == NULL) {
227
0
                PyErr_Clear();
228
0
                ps2 = "";
229
0
            }
230
0
        }
231
0
    }
232
0
    arena = PyArena_New();
233
0
    if (arena == NULL) {
234
0
        Py_XDECREF(v);
235
0
        Py_XDECREF(w);
236
0
        Py_XDECREF(oenc);
237
0
        return -1;
238
0
    }
239
0
    mod = PyParser_ASTFromFileObject(fp, filename, enc,
240
0
                                     Py_single_input, ps1, ps2,
241
0
                                     flags, &errcode, arena);
242
0
    Py_XDECREF(v);
243
0
    Py_XDECREF(w);
244
0
    Py_XDECREF(oenc);
245
0
    if (mod == NULL) {
246
0
        PyArena_Free(arena);
247
0
        if (errcode == E_EOF) {
248
0
            PyErr_Clear();
249
0
            return E_EOF;
250
0
        }
251
0
        return -1;
252
0
    }
253
0
    m = PyImport_AddModuleObject(mod_name);
254
0
    if (m == NULL) {
255
0
        PyArena_Free(arena);
256
0
        return -1;
257
0
    }
258
0
    d = PyModule_GetDict(m);
259
0
    v = run_mod(mod, filename, d, d, flags, arena);
260
0
    PyArena_Free(arena);
261
0
    if (v == NULL) {
262
0
        return -1;
263
0
    }
264
0
    Py_DECREF(v);
265
0
    flush_io();
266
0
    return 0;
267
0
}
268
269
int
270
PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
271
0
{
272
0
    int res;
273
274
0
    res = PyRun_InteractiveOneObjectEx(fp, filename, flags);
275
0
    if (res == -1) {
276
0
        PyErr_Print();
277
0
        flush_io();
278
0
    }
279
0
    return res;
280
0
}
281
282
int
283
PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
284
0
{
285
0
    PyObject *filename;
286
0
    int res;
287
288
0
    filename = PyUnicode_DecodeFSDefault(filename_str);
289
0
    if (filename == NULL) {
290
0
        PyErr_Print();
291
0
        return -1;
292
0
    }
293
0
    res = PyRun_InteractiveOneObject(fp, filename, flags);
294
0
    Py_DECREF(filename);
295
0
    return res;
296
0
}
297
298
299
/* Check whether a file maybe a pyc file: Look at the extension,
300
   the file type, and, if we may close it, at the first few bytes. */
301
302
static int
303
maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
304
0
{
305
0
    if (strcmp(ext, ".pyc") == 0)
306
0
        return 1;
307
308
    /* Only look into the file if we are allowed to close it, since
309
       it then should also be seekable. */
310
0
    if (closeit) {
311
        /* Read only two bytes of the magic. If the file was opened in
312
           text mode, the bytes 3 and 4 of the magic (\r\n) might not
313
           be read as they are on disk. */
314
0
        unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
315
0
        unsigned char buf[2];
316
        /* Mess:  In case of -x, the stream is NOT at its start now,
317
           and ungetc() was used to push back the first newline,
318
           which makes the current stream position formally undefined,
319
           and a x-platform nightmare.
320
           Unfortunately, we have no direct way to know whether -x
321
           was specified.  So we use a terrible hack:  if the current
322
           stream position is not 0, we assume -x was specified, and
323
           give up.  Bug 132850 on SourceForge spells out the
324
           hopelessness of trying anything else (fseek and ftell
325
           don't work predictably x-platform for text-mode files).
326
        */
327
0
        int ispyc = 0;
328
0
        if (ftell(fp) == 0) {
329
0
            if (fread(buf, 1, 2, fp) == 2 &&
330
0
                ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
331
0
                ispyc = 1;
332
0
            rewind(fp);
333
0
        }
334
0
        return ispyc;
335
0
    }
336
0
    return 0;
337
0
}
338
339
static int
340
set_main_loader(PyObject *d, const char *filename, const char *loader_name)
341
0
{
342
0
    PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader;
343
0
    int result = 0;
344
345
0
    filename_obj = PyUnicode_DecodeFSDefault(filename);
346
0
    if (filename_obj == NULL)
347
0
        return -1;
348
0
    PyInterpreterState *interp = _PyInterpreterState_Get();
349
0
    bootstrap = PyObject_GetAttrString(interp->importlib,
350
0
                                       "_bootstrap_external");
351
0
    if (bootstrap != NULL) {
352
0
        loader_type = PyObject_GetAttrString(bootstrap, loader_name);
353
0
        Py_DECREF(bootstrap);
354
0
    }
355
0
    if (loader_type == NULL) {
356
0
        Py_DECREF(filename_obj);
357
0
        return -1;
358
0
    }
359
0
    loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
360
0
    Py_DECREF(loader_type);
361
0
    if (loader == NULL) {
362
0
        return -1;
363
0
    }
364
0
    if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
365
0
        result = -1;
366
0
    }
367
0
    Py_DECREF(loader);
368
0
    return result;
369
0
}
370
371
int
372
PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
373
                        PyCompilerFlags *flags)
374
0
{
375
0
    PyObject *m, *d, *v;
376
0
    const char *ext;
377
0
    int set_file_name = 0, ret = -1;
378
0
    size_t len;
379
380
0
    m = PyImport_AddModule("__main__");
381
0
    if (m == NULL)
382
0
        return -1;
383
0
    Py_INCREF(m);
384
0
    d = PyModule_GetDict(m);
385
0
    if (PyDict_GetItemString(d, "__file__") == NULL) {
386
0
        PyObject *f;
387
0
        f = PyUnicode_DecodeFSDefault(filename);
388
0
        if (f == NULL)
389
0
            goto done;
390
0
        if (PyDict_SetItemString(d, "__file__", f) < 0) {
391
0
            Py_DECREF(f);
392
0
            goto done;
393
0
        }
394
0
        if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
395
0
            Py_DECREF(f);
396
0
            goto done;
397
0
        }
398
0
        set_file_name = 1;
399
0
        Py_DECREF(f);
400
0
    }
401
0
    len = strlen(filename);
402
0
    ext = filename + len - (len > 4 ? 4 : 0);
403
0
    if (maybe_pyc_file(fp, filename, ext, closeit)) {
404
0
        FILE *pyc_fp;
405
        /* Try to run a pyc file. First, re-open in binary */
406
0
        if (closeit)
407
0
            fclose(fp);
408
0
        if ((pyc_fp = _Py_fopen(filename, "rb")) == NULL) {
409
0
            fprintf(stderr, "python: Can't reopen .pyc file\n");
410
0
            goto done;
411
0
        }
412
413
0
        if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
414
0
            fprintf(stderr, "python: failed to set __main__.__loader__\n");
415
0
            ret = -1;
416
0
            fclose(pyc_fp);
417
0
            goto done;
418
0
        }
419
0
        v = run_pyc_file(pyc_fp, filename, d, d, flags);
420
0
    } else {
421
        /* When running from stdin, leave __main__.__loader__ alone */
422
0
        if (strcmp(filename, "<stdin>") != 0 &&
423
0
            set_main_loader(d, filename, "SourceFileLoader") < 0) {
424
0
            fprintf(stderr, "python: failed to set __main__.__loader__\n");
425
0
            ret = -1;
426
0
            goto done;
427
0
        }
428
0
        v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
429
0
                              closeit, flags);
430
0
    }
431
0
    flush_io();
432
0
    if (v == NULL) {
433
0
        Py_CLEAR(m);
434
0
        PyErr_Print();
435
0
        goto done;
436
0
    }
437
0
    Py_DECREF(v);
438
0
    ret = 0;
439
0
  done:
440
0
    if (set_file_name) {
441
0
        if (PyDict_DelItemString(d, "__file__")) {
442
0
            PyErr_Clear();
443
0
        }
444
0
        if (PyDict_DelItemString(d, "__cached__")) {
445
0
            PyErr_Clear();
446
0
        }
447
0
    }
448
0
    Py_XDECREF(m);
449
0
    return ret;
450
0
}
451
452
int
453
PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
454
0
{
455
0
    PyObject *m, *d, *v;
456
0
    m = PyImport_AddModule("__main__");
457
0
    if (m == NULL)
458
0
        return -1;
459
0
    d = PyModule_GetDict(m);
460
0
    v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
461
0
    if (v == NULL) {
462
0
        PyErr_Print();
463
0
        return -1;
464
0
    }
465
0
    Py_DECREF(v);
466
0
    return 0;
467
0
}
468
469
static int
470
parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
471
                   int *lineno, int *offset, PyObject **text)
472
0
{
473
0
    int hold;
474
0
    PyObject *v;
475
0
    _Py_IDENTIFIER(msg);
476
0
    _Py_IDENTIFIER(filename);
477
0
    _Py_IDENTIFIER(lineno);
478
0
    _Py_IDENTIFIER(offset);
479
0
    _Py_IDENTIFIER(text);
480
481
0
    *message = NULL;
482
0
    *filename = NULL;
483
484
    /* new style errors.  `err' is an instance */
485
0
    *message = _PyObject_GetAttrId(err, &PyId_msg);
486
0
    if (!*message)
487
0
        goto finally;
488
489
0
    v = _PyObject_GetAttrId(err, &PyId_filename);
490
0
    if (!v)
491
0
        goto finally;
492
0
    if (v == Py_None) {
493
0
        Py_DECREF(v);
494
0
        *filename = _PyUnicode_FromId(&PyId_string);
495
0
        if (*filename == NULL)
496
0
            goto finally;
497
0
        Py_INCREF(*filename);
498
0
    }
499
0
    else {
500
0
        *filename = v;
501
0
    }
502
503
0
    v = _PyObject_GetAttrId(err, &PyId_lineno);
504
0
    if (!v)
505
0
        goto finally;
506
0
    hold = _PyLong_AsInt(v);
507
0
    Py_DECREF(v);
508
0
    if (hold < 0 && PyErr_Occurred())
509
0
        goto finally;
510
0
    *lineno = hold;
511
512
0
    v = _PyObject_GetAttrId(err, &PyId_offset);
513
0
    if (!v)
514
0
        goto finally;
515
0
    if (v == Py_None) {
516
0
        *offset = -1;
517
0
        Py_DECREF(v);
518
0
    } else {
519
0
        hold = _PyLong_AsInt(v);
520
0
        Py_DECREF(v);
521
0
        if (hold < 0 && PyErr_Occurred())
522
0
            goto finally;
523
0
        *offset = hold;
524
0
    }
525
526
0
    v = _PyObject_GetAttrId(err, &PyId_text);
527
0
    if (!v)
528
0
        goto finally;
529
0
    if (v == Py_None) {
530
0
        Py_DECREF(v);
531
0
        *text = NULL;
532
0
    }
533
0
    else {
534
0
        *text = v;
535
0
    }
536
0
    return 1;
537
538
0
finally:
539
0
    Py_XDECREF(*message);
540
0
    Py_XDECREF(*filename);
541
0
    return 0;
542
0
}
543
544
static void
545
print_error_text(PyObject *f, int offset, PyObject *text_obj)
546
0
{
547
0
    const char *text;
548
0
    const char *nl;
549
550
0
    text = PyUnicode_AsUTF8(text_obj);
551
0
    if (text == NULL)
552
0
        return;
553
554
0
    if (offset >= 0) {
555
0
        if (offset > 0 && (size_t)offset == strlen(text) && text[offset - 1] == '\n')
556
0
            offset--;
557
0
        for (;;) {
558
0
            nl = strchr(text, '\n');
559
0
            if (nl == NULL || nl-text >= offset)
560
0
                break;
561
0
            offset -= (int)(nl+1-text);
562
0
            text = nl+1;
563
0
        }
564
0
        while (*text == ' ' || *text == '\t' || *text == '\f') {
565
0
            text++;
566
0
            offset--;
567
0
        }
568
0
    }
569
0
    PyFile_WriteString("    ", f);
570
0
    PyFile_WriteString(text, f);
571
0
    if (*text == '\0' || text[strlen(text)-1] != '\n')
572
0
        PyFile_WriteString("\n", f);
573
0
    if (offset == -1)
574
0
        return;
575
0
    PyFile_WriteString("    ", f);
576
0
    while (--offset > 0)
577
0
        PyFile_WriteString(" ", f);
578
0
    PyFile_WriteString("^\n", f);
579
0
}
580
581
582
int
583
_Py_HandleSystemExit(int *exitcode_p)
584
0
{
585
0
    int inspect = _PyInterpreterState_GET_UNSAFE()->config.inspect;
586
0
    if (inspect) {
587
        /* Don't exit if -i flag was given. This flag is set to 0
588
         * when entering interactive mode for inspecting. */
589
0
        return 0;
590
0
    }
591
592
0
    if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
593
0
        return 0;
594
0
    }
595
596
0
    PyObject *exception, *value, *tb;
597
0
    PyErr_Fetch(&exception, &value, &tb);
598
599
0
    fflush(stdout);
600
601
0
    int exitcode = 0;
602
0
    if (value == NULL || value == Py_None) {
603
0
        goto done;
604
0
    }
605
606
0
    if (PyExceptionInstance_Check(value)) {
607
        /* The error code should be in the `code' attribute. */
608
0
        _Py_IDENTIFIER(code);
609
0
        PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
610
0
        if (code) {
611
0
            Py_DECREF(value);
612
0
            value = code;
613
0
            if (value == Py_None)
614
0
                goto done;
615
0
        }
616
        /* If we failed to dig out the 'code' attribute,
617
           just let the else clause below print the error. */
618
0
    }
619
620
0
    if (PyLong_Check(value)) {
621
0
        exitcode = (int)PyLong_AsLong(value);
622
0
    }
623
0
    else {
624
0
        PyObject *sys_stderr = _PySys_GetObjectId(&PyId_stderr);
625
        /* We clear the exception here to avoid triggering the assertion
626
         * in PyObject_Str that ensures it won't silently lose exception
627
         * details.
628
         */
629
0
        PyErr_Clear();
630
0
        if (sys_stderr != NULL && sys_stderr != Py_None) {
631
0
            PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
632
0
        } else {
633
0
            PyObject_Print(value, stderr, Py_PRINT_RAW);
634
0
            fflush(stderr);
635
0
        }
636
0
        PySys_WriteStderr("\n");
637
0
        exitcode = 1;
638
0
    }
639
640
0
 done:
641
    /* Restore and clear the exception info, in order to properly decref
642
     * the exception, value, and traceback.      If we just exit instead,
643
     * these leak, which confuses PYTHONDUMPREFS output, and may prevent
644
     * some finalizers from running.
645
     */
646
0
    PyErr_Restore(exception, value, tb);
647
0
    PyErr_Clear();
648
0
    *exitcode_p = exitcode;
649
0
    return 1;
650
0
}
651
652
653
static void
654
handle_system_exit(void)
655
0
{
656
0
    int exitcode;
657
0
    if (_Py_HandleSystemExit(&exitcode)) {
658
0
        Py_Exit(exitcode);
659
0
    }
660
0
}
661
662
663
static void
664
_PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
665
0
{
666
0
    PyObject *exception, *v, *tb, *hook;
667
668
0
    handle_system_exit();
669
670
0
    _PyErr_Fetch(tstate, &exception, &v, &tb);
671
0
    if (exception == NULL) {
672
0
        goto done;
673
0
    }
674
675
0
    _PyErr_NormalizeException(tstate, &exception, &v, &tb);
676
0
    if (tb == NULL) {
677
0
        tb = Py_None;
678
0
        Py_INCREF(tb);
679
0
    }
680
0
    PyException_SetTraceback(v, tb);
681
0
    if (exception == NULL) {
682
0
        goto done;
683
0
    }
684
685
    /* Now we know v != NULL too */
686
0
    if (set_sys_last_vars) {
687
0
        if (_PySys_SetObjectId(&PyId_last_type, exception) < 0) {
688
0
            _PyErr_Clear(tstate);
689
0
        }
690
0
        if (_PySys_SetObjectId(&PyId_last_value, v) < 0) {
691
0
            _PyErr_Clear(tstate);
692
0
        }
693
0
        if (_PySys_SetObjectId(&PyId_last_traceback, tb) < 0) {
694
0
            _PyErr_Clear(tstate);
695
0
        }
696
0
    }
697
0
    hook = _PySys_GetObjectId(&PyId_excepthook);
698
0
    if (PySys_Audit("sys.excepthook", "OOOO", hook ? hook : Py_None,
699
0
                    exception, v, tb) < 0) {
700
0
        if (PyErr_ExceptionMatches(PyExc_RuntimeError)) {
701
0
            PyErr_Clear();
702
0
            goto done;
703
0
        }
704
0
        _PyErr_WriteUnraisableMsg("in audit hook", NULL);
705
0
    }
706
0
    if (hook) {
707
0
        PyObject* stack[3];
708
0
        PyObject *result;
709
710
0
        stack[0] = exception;
711
0
        stack[1] = v;
712
0
        stack[2] = tb;
713
0
        result = _PyObject_FastCall(hook, stack, 3);
714
0
        if (result == NULL) {
715
0
            handle_system_exit();
716
717
0
            PyObject *exception2, *v2, *tb2;
718
0
            _PyErr_Fetch(tstate, &exception2, &v2, &tb2);
719
0
            _PyErr_NormalizeException(tstate, &exception2, &v2, &tb2);
720
            /* It should not be possible for exception2 or v2
721
               to be NULL. However PyErr_Display() can't
722
               tolerate NULLs, so just be safe. */
723
0
            if (exception2 == NULL) {
724
0
                exception2 = Py_None;
725
0
                Py_INCREF(exception2);
726
0
            }
727
0
            if (v2 == NULL) {
728
0
                v2 = Py_None;
729
0
                Py_INCREF(v2);
730
0
            }
731
0
            fflush(stdout);
732
0
            PySys_WriteStderr("Error in sys.excepthook:\n");
733
0
            PyErr_Display(exception2, v2, tb2);
734
0
            PySys_WriteStderr("\nOriginal exception was:\n");
735
0
            PyErr_Display(exception, v, tb);
736
0
            Py_DECREF(exception2);
737
0
            Py_DECREF(v2);
738
0
            Py_XDECREF(tb2);
739
0
        }
740
0
        Py_XDECREF(result);
741
0
    }
742
0
    else {
743
0
        PySys_WriteStderr("sys.excepthook is missing\n");
744
0
        PyErr_Display(exception, v, tb);
745
0
    }
746
747
0
done:
748
0
    Py_XDECREF(exception);
749
0
    Py_XDECREF(v);
750
0
    Py_XDECREF(tb);
751
0
}
752
753
void
754
_PyErr_Print(PyThreadState *tstate)
755
0
{
756
0
    _PyErr_PrintEx(tstate, 1);
757
0
}
758
759
void
760
PyErr_PrintEx(int set_sys_last_vars)
761
0
{
762
0
    PyThreadState *tstate = _PyThreadState_GET();
763
0
    _PyErr_PrintEx(tstate, set_sys_last_vars);
764
0
}
765
766
void
767
PyErr_Print(void)
768
0
{
769
0
    PyErr_PrintEx(1);
770
0
}
771
772
static void
773
print_exception(PyObject *f, PyObject *value)
774
0
{
775
0
    int err = 0;
776
0
    PyObject *type, *tb;
777
0
    _Py_IDENTIFIER(print_file_and_line);
778
779
0
    if (!PyExceptionInstance_Check(value)) {
780
0
        err = PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
781
0
        err += PyFile_WriteString(Py_TYPE(value)->tp_name, f);
782
0
        err += PyFile_WriteString(" found\n", f);
783
0
        if (err)
784
0
            PyErr_Clear();
785
0
        return;
786
0
    }
787
788
0
    Py_INCREF(value);
789
0
    fflush(stdout);
790
0
    type = (PyObject *) Py_TYPE(value);
791
0
    tb = PyException_GetTraceback(value);
792
0
    if (tb && tb != Py_None)
793
0
        err = PyTraceBack_Print(tb, f);
794
0
    if (err == 0 &&
795
0
        _PyObject_HasAttrId(value, &PyId_print_file_and_line))
796
0
    {
797
0
        PyObject *message, *filename, *text;
798
0
        int lineno, offset;
799
0
        if (!parse_syntax_error(value, &message, &filename,
800
0
                                &lineno, &offset, &text))
801
0
            PyErr_Clear();
802
0
        else {
803
0
            PyObject *line;
804
805
0
            Py_DECREF(value);
806
0
            value = message;
807
808
0
            line = PyUnicode_FromFormat("  File \"%S\", line %d\n",
809
0
                                          filename, lineno);
810
0
            Py_DECREF(filename);
811
0
            if (line != NULL) {
812
0
                PyFile_WriteObject(line, f, Py_PRINT_RAW);
813
0
                Py_DECREF(line);
814
0
            }
815
816
0
            if (text != NULL) {
817
0
                print_error_text(f, offset, text);
818
0
                Py_DECREF(text);
819
0
            }
820
821
            /* Can't be bothered to check all those
822
               PyFile_WriteString() calls */
823
0
            if (PyErr_Occurred())
824
0
                err = -1;
825
0
        }
826
0
    }
827
0
    if (err) {
828
        /* Don't do anything else */
829
0
    }
830
0
    else {
831
0
        PyObject* moduleName;
832
0
        const char *className;
833
0
        _Py_IDENTIFIER(__module__);
834
0
        assert(PyExceptionClass_Check(type));
835
0
        className = PyExceptionClass_Name(type);
836
0
        if (className != NULL) {
837
0
            const char *dot = strrchr(className, '.');
838
0
            if (dot != NULL)
839
0
                className = dot+1;
840
0
        }
841
842
0
        moduleName = _PyObject_GetAttrId(type, &PyId___module__);
843
0
        if (moduleName == NULL || !PyUnicode_Check(moduleName))
844
0
        {
845
0
            Py_XDECREF(moduleName);
846
0
            err = PyFile_WriteString("<unknown>", f);
847
0
        }
848
0
        else {
849
0
            if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins))
850
0
            {
851
0
                err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
852
0
                err += PyFile_WriteString(".", f);
853
0
            }
854
0
            Py_DECREF(moduleName);
855
0
        }
856
0
        if (err == 0) {
857
0
            if (className == NULL)
858
0
                      err = PyFile_WriteString("<unknown>", f);
859
0
            else
860
0
                      err = PyFile_WriteString(className, f);
861
0
        }
862
0
    }
863
0
    if (err == 0 && (value != Py_None)) {
864
0
        PyObject *s = PyObject_Str(value);
865
        /* only print colon if the str() of the
866
           object is not the empty string
867
        */
868
0
        if (s == NULL) {
869
0
            PyErr_Clear();
870
0
            err = -1;
871
0
            PyFile_WriteString(": <exception str() failed>", f);
872
0
        }
873
0
        else if (!PyUnicode_Check(s) ||
874
0
            PyUnicode_GetLength(s) != 0)
875
0
            err = PyFile_WriteString(": ", f);
876
0
        if (err == 0)
877
0
          err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
878
0
        Py_XDECREF(s);
879
0
    }
880
    /* try to write a newline in any case */
881
0
    if (err < 0) {
882
0
        PyErr_Clear();
883
0
    }
884
0
    err += PyFile_WriteString("\n", f);
885
0
    Py_XDECREF(tb);
886
0
    Py_DECREF(value);
887
    /* If an error happened here, don't show it.
888
       XXX This is wrong, but too many callers rely on this behavior. */
889
0
    if (err != 0)
890
0
        PyErr_Clear();
891
0
}
892
893
static const char cause_message[] =
894
    "\nThe above exception was the direct cause "
895
    "of the following exception:\n\n";
896
897
static const char context_message[] =
898
    "\nDuring handling of the above exception, "
899
    "another exception occurred:\n\n";
900
901
static void
902
print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
903
0
{
904
0
    int err = 0, res;
905
0
    PyObject *cause, *context;
906
907
0
    if (seen != NULL) {
908
        /* Exception chaining */
909
0
        PyObject *value_id = PyLong_FromVoidPtr(value);
910
0
        if (value_id == NULL || PySet_Add(seen, value_id) == -1)
911
0
            PyErr_Clear();
912
0
        else if (PyExceptionInstance_Check(value)) {
913
0
            PyObject *check_id = NULL;
914
0
            cause = PyException_GetCause(value);
915
0
            context = PyException_GetContext(value);
916
0
            if (cause) {
917
0
                check_id = PyLong_FromVoidPtr(cause);
918
0
                if (check_id == NULL) {
919
0
                    res = -1;
920
0
                } else {
921
0
                    res = PySet_Contains(seen, check_id);
922
0
                    Py_DECREF(check_id);
923
0
                }
924
0
                if (res == -1)
925
0
                    PyErr_Clear();
926
0
                if (res == 0) {
927
0
                    print_exception_recursive(
928
0
                        f, cause, seen);
929
0
                    err |= PyFile_WriteString(
930
0
                        cause_message, f);
931
0
                }
932
0
            }
933
0
            else if (context &&
934
0
                !((PyBaseExceptionObject *)value)->suppress_context) {
935
0
                check_id = PyLong_FromVoidPtr(context);
936
0
                if (check_id == NULL) {
937
0
                    res = -1;
938
0
                } else {
939
0
                    res = PySet_Contains(seen, check_id);
940
0
                    Py_DECREF(check_id);
941
0
                }
942
0
                if (res == -1)
943
0
                    PyErr_Clear();
944
0
                if (res == 0) {
945
0
                    print_exception_recursive(
946
0
                        f, context, seen);
947
0
                    err |= PyFile_WriteString(
948
0
                        context_message, f);
949
0
                }
950
0
            }
951
0
            Py_XDECREF(context);
952
0
            Py_XDECREF(cause);
953
0
        }
954
0
        Py_XDECREF(value_id);
955
0
    }
956
0
    print_exception(f, value);
957
0
    if (err != 0)
958
0
        PyErr_Clear();
959
0
}
960
961
void
962
_PyErr_Display(PyObject *file, PyObject *exception, PyObject *value, PyObject *tb)
963
0
{
964
0
    assert(file != NULL && file != Py_None);
965
966
0
    PyObject *seen;
967
0
    if (PyExceptionInstance_Check(value)
968
0
        && tb != NULL && PyTraceBack_Check(tb)) {
969
        /* Put the traceback on the exception, otherwise it won't get
970
           displayed.  See issue #18776. */
971
0
        PyObject *cur_tb = PyException_GetTraceback(value);
972
0
        if (cur_tb == NULL)
973
0
            PyException_SetTraceback(value, tb);
974
0
        else
975
0
            Py_DECREF(cur_tb);
976
0
    }
977
978
    /* We choose to ignore seen being possibly NULL, and report
979
       at least the main exception (it could be a MemoryError).
980
    */
981
0
    seen = PySet_New(NULL);
982
0
    if (seen == NULL) {
983
0
        PyErr_Clear();
984
0
    }
985
0
    print_exception_recursive(file, value, seen);
986
0
    Py_XDECREF(seen);
987
988
    /* Call file.flush() */
989
0
    PyObject *res = _PyObject_CallMethodId(file, &PyId_flush, NULL);
990
0
    if (!res) {
991
        /* Silently ignore file.flush() error */
992
0
        PyErr_Clear();
993
0
    }
994
0
    else {
995
0
        Py_DECREF(res);
996
0
    }
997
0
}
998
999
void
1000
PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1001
0
{
1002
0
    PyObject *file = _PySys_GetObjectId(&PyId_stderr);
1003
0
    if (file == NULL) {
1004
0
        _PyObject_Dump(value);
1005
0
        fprintf(stderr, "lost sys.stderr\n");
1006
0
        return;
1007
0
    }
1008
0
    if (file == Py_None) {
1009
0
        return;
1010
0
    }
1011
1012
0
    _PyErr_Display(file, exception, value, tb);
1013
0
}
1014
1015
PyObject *
1016
PyRun_StringFlags(const char *str, int start, PyObject *globals,
1017
                  PyObject *locals, PyCompilerFlags *flags)
1018
2
{
1019
2
    PyObject *ret = NULL;
1020
2
    mod_ty mod;
1021
2
    PyArena *arena;
1022
2
    PyObject *filename;
1023
1024
2
    filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
1025
2
    if (filename == NULL)
1026
0
        return NULL;
1027
1028
2
    arena = PyArena_New();
1029
2
    if (arena == NULL)
1030
0
        return NULL;
1031
1032
2
    mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
1033
2
    if (mod != NULL)
1034
2
        ret = run_mod(mod, filename, globals, locals, flags, arena);
1035
2
    PyArena_Free(arena);
1036
2
    return ret;
1037
2
}
1038
1039
PyObject *
1040
PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
1041
                  PyObject *locals, int closeit, PyCompilerFlags *flags)
1042
0
{
1043
0
    PyObject *ret = NULL;
1044
0
    mod_ty mod;
1045
0
    PyArena *arena = NULL;
1046
0
    PyObject *filename;
1047
1048
0
    filename = PyUnicode_DecodeFSDefault(filename_str);
1049
0
    if (filename == NULL)
1050
0
        goto exit;
1051
1052
0
    arena = PyArena_New();
1053
0
    if (arena == NULL)
1054
0
        goto exit;
1055
1056
0
    mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
1057
0
                                     flags, NULL, arena);
1058
0
    if (closeit)
1059
0
        fclose(fp);
1060
0
    if (mod == NULL) {
1061
0
        goto exit;
1062
0
    }
1063
0
    ret = run_mod(mod, filename, globals, locals, flags, arena);
1064
1065
0
exit:
1066
0
    Py_XDECREF(filename);
1067
0
    if (arena != NULL)
1068
0
        PyArena_Free(arena);
1069
0
    return ret;
1070
0
}
1071
1072
static void
1073
flush_io(void)
1074
0
{
1075
0
    PyObject *f, *r;
1076
0
    PyObject *type, *value, *traceback;
1077
1078
    /* Save the current exception */
1079
0
    PyErr_Fetch(&type, &value, &traceback);
1080
1081
0
    f = _PySys_GetObjectId(&PyId_stderr);
1082
0
    if (f != NULL) {
1083
0
        r = _PyObject_CallMethodId(f, &PyId_flush, NULL);
1084
0
        if (r)
1085
0
            Py_DECREF(r);
1086
0
        else
1087
0
            PyErr_Clear();
1088
0
    }
1089
0
    f = _PySys_GetObjectId(&PyId_stdout);
1090
0
    if (f != NULL) {
1091
0
        r = _PyObject_CallMethodId(f, &PyId_flush, NULL);
1092
0
        if (r)
1093
0
            Py_DECREF(r);
1094
0
        else
1095
0
            PyErr_Clear();
1096
0
    }
1097
1098
0
    PyErr_Restore(type, value, traceback);
1099
0
}
1100
1101
static PyObject *
1102
run_eval_code_obj(PyCodeObject *co, PyObject *globals, PyObject *locals)
1103
2
{
1104
2
    PyObject *v;
1105
    /*
1106
     * We explicitly re-initialize _Py_UnhandledKeyboardInterrupt every eval
1107
     * _just in case_ someone is calling into an embedded Python where they
1108
     * don't care about an uncaught KeyboardInterrupt exception (why didn't they
1109
     * leave config.install_signal_handlers set to 0?!?) but then later call
1110
     * Py_Main() itself (which _checks_ this flag and dies with a signal after
1111
     * its interpreter exits).  We don't want a previous embedded interpreter's
1112
     * uncaught exception to trigger an unexplained signal exit from a future
1113
     * Py_Main() based one.
1114
     */
1115
2
    _Py_UnhandledKeyboardInterrupt = 0;
1116
1117
    /* Set globals['__builtins__'] if it doesn't exist */
1118
2
    if (globals != NULL && PyDict_GetItemString(globals, "__builtins__") == NULL) {
1119
0
        PyInterpreterState *interp = _PyInterpreterState_Get();
1120
0
        if (PyDict_SetItemString(globals, "__builtins__", interp->builtins) < 0) {
1121
0
            return NULL;
1122
0
        }
1123
0
    }
1124
1125
2
    v = PyEval_EvalCode((PyObject*)co, globals, locals);
1126
2
    if (!v && PyErr_Occurred() == PyExc_KeyboardInterrupt) {
1127
0
        _Py_UnhandledKeyboardInterrupt = 1;
1128
0
    }
1129
2
    return v;
1130
2
}
1131
1132
static PyObject *
1133
run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
1134
            PyCompilerFlags *flags, PyArena *arena)
1135
2
{
1136
2
    PyCodeObject *co;
1137
2
    PyObject *v;
1138
2
    co = PyAST_CompileObject(mod, filename, flags, -1, arena);
1139
2
    if (co == NULL)
1140
0
        return NULL;
1141
1142
2
    if (PySys_Audit("exec", "O", co) < 0) {
1143
0
        Py_DECREF(co);
1144
0
        return NULL;
1145
0
    }
1146
1147
2
    v = run_eval_code_obj(co, globals, locals);
1148
2
    Py_DECREF(co);
1149
2
    return v;
1150
2
}
1151
1152
static PyObject *
1153
run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1154
             PyObject *locals, PyCompilerFlags *flags)
1155
0
{
1156
0
    PyCodeObject *co;
1157
0
    PyObject *v;
1158
0
    long magic;
1159
0
    long PyImport_GetMagicNumber(void);
1160
1161
0
    magic = PyMarshal_ReadLongFromFile(fp);
1162
0
    if (magic != PyImport_GetMagicNumber()) {
1163
0
        if (!PyErr_Occurred())
1164
0
            PyErr_SetString(PyExc_RuntimeError,
1165
0
                       "Bad magic number in .pyc file");
1166
0
        goto error;
1167
0
    }
1168
    /* Skip the rest of the header. */
1169
0
    (void) PyMarshal_ReadLongFromFile(fp);
1170
0
    (void) PyMarshal_ReadLongFromFile(fp);
1171
0
    (void) PyMarshal_ReadLongFromFile(fp);
1172
0
    if (PyErr_Occurred()) {
1173
0
        goto error;
1174
0
    }
1175
0
    v = PyMarshal_ReadLastObjectFromFile(fp);
1176
0
    if (v == NULL || !PyCode_Check(v)) {
1177
0
        Py_XDECREF(v);
1178
0
        PyErr_SetString(PyExc_RuntimeError,
1179
0
                   "Bad code object in .pyc file");
1180
0
        goto error;
1181
0
    }
1182
0
    fclose(fp);
1183
0
    co = (PyCodeObject *)v;
1184
0
    v = run_eval_code_obj(co, globals, locals);
1185
0
    if (v && flags)
1186
0
        flags->cf_flags |= (co->co_flags & PyCF_MASK);
1187
0
    Py_DECREF(co);
1188
0
    return v;
1189
0
error:
1190
0
    fclose(fp);
1191
0
    return NULL;
1192
0
}
1193
1194
PyObject *
1195
Py_CompileStringObject(const char *str, PyObject *filename, int start,
1196
                       PyCompilerFlags *flags, int optimize)
1197
14
{
1198
14
    PyCodeObject *co;
1199
14
    mod_ty mod;
1200
14
    PyArena *arena = PyArena_New();
1201
14
    if (arena == NULL)
1202
0
        return NULL;
1203
1204
14
    mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
1205
14
    if (mod == NULL) {
1206
0
        PyArena_Free(arena);
1207
0
        return NULL;
1208
0
    }
1209
14
    if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1210
0
        PyObject *result = PyAST_mod2obj(mod);
1211
0
        PyArena_Free(arena);
1212
0
        return result;
1213
0
    }
1214
14
    co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
1215
14
    PyArena_Free(arena);
1216
14
    return (PyObject *)co;
1217
14
}
1218
1219
PyObject *
1220
Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
1221
                        PyCompilerFlags *flags, int optimize)
1222
14
{
1223
14
    PyObject *filename, *co;
1224
14
    filename = PyUnicode_DecodeFSDefault(filename_str);
1225
14
    if (filename == NULL)
1226
0
        return NULL;
1227
14
    co = Py_CompileStringObject(str, filename, start, flags, optimize);
1228
14
    Py_DECREF(filename);
1229
14
    return co;
1230
14
}
1231
1232
/* For use in Py_LIMITED_API */
1233
#undef Py_CompileString
1234
PyObject *
1235
PyCompileString(const char *str, const char *filename, int start)
1236
0
{
1237
0
    return Py_CompileStringFlags(str, filename, start, NULL);
1238
0
}
1239
1240
const char *
1241
_Py_SourceAsString(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
1242
2
{
1243
2
    const char *str;
1244
2
    Py_ssize_t size;
1245
2
    Py_buffer view;
1246
1247
2
    *cmd_copy = NULL;
1248
2
    if (PyUnicode_Check(cmd)) {
1249
2
        cf->cf_flags |= PyCF_IGNORE_COOKIE;
1250
2
        str = PyUnicode_AsUTF8AndSize(cmd, &size);
1251
2
        if (str == NULL)
1252
0
            return NULL;
1253
2
    }
1254
0
    else if (PyBytes_Check(cmd)) {
1255
0
        str = PyBytes_AS_STRING(cmd);
1256
0
        size = PyBytes_GET_SIZE(cmd);
1257
0
    }
1258
0
    else if (PyByteArray_Check(cmd)) {
1259
0
        str = PyByteArray_AS_STRING(cmd);
1260
0
        size = PyByteArray_GET_SIZE(cmd);
1261
0
    }
1262
0
    else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
1263
        /* Copy to NUL-terminated buffer. */
1264
0
        *cmd_copy = PyBytes_FromStringAndSize(
1265
0
            (const char *)view.buf, view.len);
1266
0
        PyBuffer_Release(&view);
1267
0
        if (*cmd_copy == NULL) {
1268
0
            return NULL;
1269
0
        }
1270
0
        str = PyBytes_AS_STRING(*cmd_copy);
1271
0
        size = PyBytes_GET_SIZE(*cmd_copy);
1272
0
    }
1273
0
    else {
1274
0
        PyErr_Format(PyExc_TypeError,
1275
0
            "%s() arg 1 must be a %s object",
1276
0
            funcname, what);
1277
0
        return NULL;
1278
0
    }
1279
1280
2
    if (strlen(str) != (size_t)size) {
1281
0
        PyErr_SetString(PyExc_ValueError,
1282
0
            "source code string cannot contain null bytes");
1283
0
        Py_CLEAR(*cmd_copy);
1284
0
        return NULL;
1285
0
    }
1286
2
    return str;
1287
2
}
1288
1289
struct symtable *
1290
Py_SymtableStringObject(const char *str, PyObject *filename, int start)
1291
0
{
1292
0
    PyCompilerFlags flags = _PyCompilerFlags_INIT;
1293
0
    return _Py_SymtableStringObjectFlags(str, filename, start, &flags);
1294
0
}
1295
1296
struct symtable *
1297
_Py_SymtableStringObjectFlags(const char *str, PyObject *filename, int start, PyCompilerFlags *flags)
1298
0
{
1299
0
    struct symtable *st;
1300
0
    mod_ty mod;
1301
0
    PyArena *arena;
1302
1303
0
    arena = PyArena_New();
1304
0
    if (arena == NULL)
1305
0
        return NULL;
1306
1307
0
    mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
1308
0
    if (mod == NULL) {
1309
0
        PyArena_Free(arena);
1310
0
        return NULL;
1311
0
    }
1312
0
    st = PySymtable_BuildObject(mod, filename, 0);
1313
0
    PyArena_Free(arena);
1314
0
    return st;
1315
0
}
1316
1317
struct symtable *
1318
Py_SymtableString(const char *str, const char *filename_str, int start)
1319
0
{
1320
0
    PyObject *filename;
1321
0
    struct symtable *st;
1322
1323
0
    filename = PyUnicode_DecodeFSDefault(filename_str);
1324
0
    if (filename == NULL)
1325
0
        return NULL;
1326
0
    st = Py_SymtableStringObject(str, filename, start);
1327
0
    Py_DECREF(filename);
1328
0
    return st;
1329
0
}
1330
1331
/* Preferred access to parser is through AST. */
1332
mod_ty
1333
PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
1334
                             PyCompilerFlags *flags, PyArena *arena)
1335
16
{
1336
16
    mod_ty mod;
1337
16
    PyCompilerFlags localflags = _PyCompilerFlags_INIT;
1338
16
    perrdetail err;
1339
16
    int iflags = PARSER_FLAGS(flags);
1340
16
    if (flags && flags->cf_feature_version < 7)
1341
0
        iflags |= PyPARSE_ASYNC_HACKS;
1342
1343
16
    node *n = PyParser_ParseStringObject(s, filename,
1344
16
                                         &_PyParser_Grammar, start, &err,
1345
16
                                         &iflags);
1346
16
    if (flags == NULL) {
1347
14
        flags = &localflags;
1348
14
    }
1349
16
    if (n) {
1350
16
        flags->cf_flags |= iflags & PyCF_MASK;
1351
16
        mod = PyAST_FromNodeObject(n, flags, filename, arena);
1352
16
        PyNode_Free(n);
1353
16
    }
1354
0
    else {
1355
0
        err_input(&err);
1356
0
        mod = NULL;
1357
0
    }
1358
16
    err_free(&err);
1359
16
    return mod;
1360
16
}
1361
1362
mod_ty
1363
PyParser_ASTFromString(const char *s, const char *filename_str, int start,
1364
                       PyCompilerFlags *flags, PyArena *arena)
1365
0
{
1366
0
    PyObject *filename;
1367
0
    mod_ty mod;
1368
0
    filename = PyUnicode_DecodeFSDefault(filename_str);
1369
0
    if (filename == NULL)
1370
0
        return NULL;
1371
0
    mod = PyParser_ASTFromStringObject(s, filename, start, flags, arena);
1372
0
    Py_DECREF(filename);
1373
0
    return mod;
1374
0
}
1375
1376
mod_ty
1377
PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
1378
                           int start, const char *ps1,
1379
                           const char *ps2, PyCompilerFlags *flags, int *errcode,
1380
                           PyArena *arena)
1381
0
{
1382
0
    mod_ty mod;
1383
0
    PyCompilerFlags localflags = _PyCompilerFlags_INIT;
1384
0
    perrdetail err;
1385
0
    int iflags = PARSER_FLAGS(flags);
1386
1387
0
    node *n = PyParser_ParseFileObject(fp, filename, enc,
1388
0
                                       &_PyParser_Grammar,
1389
0
                                       start, ps1, ps2, &err, &iflags);
1390
0
    if (flags == NULL) {
1391
0
        flags = &localflags;
1392
0
    }
1393
0
    if (n) {
1394
0
        flags->cf_flags |= iflags & PyCF_MASK;
1395
0
        mod = PyAST_FromNodeObject(n, flags, filename, arena);
1396
0
        PyNode_Free(n);
1397
0
    }
1398
0
    else {
1399
0
        err_input(&err);
1400
0
        if (errcode)
1401
0
            *errcode = err.error;
1402
0
        mod = NULL;
1403
0
    }
1404
0
    err_free(&err);
1405
0
    return mod;
1406
0
}
1407
1408
mod_ty
1409
PyParser_ASTFromFile(FILE *fp, const char *filename_str, const char* enc,
1410
                     int start, const char *ps1,
1411
                     const char *ps2, PyCompilerFlags *flags, int *errcode,
1412
                     PyArena *arena)
1413
0
{
1414
0
    mod_ty mod;
1415
0
    PyObject *filename;
1416
0
    filename = PyUnicode_DecodeFSDefault(filename_str);
1417
0
    if (filename == NULL)
1418
0
        return NULL;
1419
0
    mod = PyParser_ASTFromFileObject(fp, filename, enc, start, ps1, ps2,
1420
0
                                     flags, errcode, arena);
1421
0
    Py_DECREF(filename);
1422
0
    return mod;
1423
0
}
1424
1425
/* Simplified interface to parsefile -- return node or set exception */
1426
1427
node *
1428
PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1429
0
{
1430
0
    perrdetail err;
1431
0
    node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1432
0
                                      &_PyParser_Grammar,
1433
0
                                      start, NULL, NULL, &err, flags);
1434
0
    if (n == NULL)
1435
0
        err_input(&err);
1436
0
    err_free(&err);
1437
1438
0
    return n;
1439
0
}
1440
1441
/* Simplified interface to parsestring -- return node or set exception */
1442
1443
node *
1444
PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1445
0
{
1446
0
    perrdetail err;
1447
0
    node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1448
0
                                        start, &err, flags);
1449
0
    if (n == NULL)
1450
0
        err_input(&err);
1451
0
    err_free(&err);
1452
0
    return n;
1453
0
}
1454
1455
node *
1456
PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1457
                                        int start, int flags)
1458
0
{
1459
0
    perrdetail err;
1460
0
    node *n = PyParser_ParseStringFlagsFilename(str, filename,
1461
0
                            &_PyParser_Grammar, start, &err, flags);
1462
0
    if (n == NULL)
1463
0
        err_input(&err);
1464
0
    err_free(&err);
1465
0
    return n;
1466
0
}
1467
1468
/* May want to move a more generalized form of this to parsetok.c or
1469
   even parser modules. */
1470
1471
void
1472
PyParser_ClearError(perrdetail *err)
1473
0
{
1474
0
    err_free(err);
1475
0
}
1476
1477
void
1478
PyParser_SetError(perrdetail *err)
1479
0
{
1480
0
    err_input(err);
1481
0
}
1482
1483
static void
1484
err_free(perrdetail *err)
1485
16
{
1486
16
    Py_CLEAR(err->filename);
1487
16
}
1488
1489
/* Set the error appropriate to the given input error code (see errcode.h) */
1490
1491
static void
1492
err_input(perrdetail *err)
1493
0
{
1494
0
    PyObject *v, *w, *errtype, *errtext;
1495
0
    PyObject *msg_obj = NULL;
1496
0
    const char *msg = NULL;
1497
0
    int offset = err->offset;
1498
1499
0
    errtype = PyExc_SyntaxError;
1500
0
    switch (err->error) {
1501
0
    case E_ERROR:
1502
0
        goto cleanup;
1503
0
    case E_SYNTAX:
1504
0
        errtype = PyExc_IndentationError;
1505
0
        if (err->expected == INDENT)
1506
0
            msg = "expected an indented block";
1507
0
        else if (err->token == INDENT)
1508
0
            msg = "unexpected indent";
1509
0
        else if (err->token == DEDENT)
1510
0
            msg = "unexpected unindent";
1511
0
        else if (err->expected == NOTEQUAL) {
1512
0
            errtype = PyExc_SyntaxError;
1513
0
            msg = "with Barry as BDFL, use '<>' instead of '!='";
1514
0
        }
1515
0
        else {
1516
0
            errtype = PyExc_SyntaxError;
1517
0
            msg = "invalid syntax";
1518
0
        }
1519
0
        break;
1520
0
    case E_TOKEN:
1521
0
        msg = "invalid token";
1522
0
        break;
1523
0
    case E_EOFS:
1524
0
        msg = "EOF while scanning triple-quoted string literal";
1525
0
        break;
1526
0
    case E_EOLS:
1527
0
        msg = "EOL while scanning string literal";
1528
0
        break;
1529
0
    case E_INTR:
1530
0
        if (!PyErr_Occurred())
1531
0
            PyErr_SetNone(PyExc_KeyboardInterrupt);
1532
0
        goto cleanup;
1533
0
    case E_NOMEM:
1534
0
        PyErr_NoMemory();
1535
0
        goto cleanup;
1536
0
    case E_EOF:
1537
0
        msg = "unexpected EOF while parsing";
1538
0
        break;
1539
0
    case E_TABSPACE:
1540
0
        errtype = PyExc_TabError;
1541
0
        msg = "inconsistent use of tabs and spaces in indentation";
1542
0
        break;
1543
0
    case E_OVERFLOW:
1544
0
        msg = "expression too long";
1545
0
        break;
1546
0
    case E_DEDENT:
1547
0
        errtype = PyExc_IndentationError;
1548
0
        msg = "unindent does not match any outer indentation level";
1549
0
        break;
1550
0
    case E_TOODEEP:
1551
0
        errtype = PyExc_IndentationError;
1552
0
        msg = "too many levels of indentation";
1553
0
        break;
1554
0
    case E_DECODE: {
1555
0
        PyObject *type, *value, *tb;
1556
0
        PyErr_Fetch(&type, &value, &tb);
1557
0
        msg = "unknown decode error";
1558
0
        if (value != NULL)
1559
0
            msg_obj = PyObject_Str(value);
1560
0
        Py_XDECREF(type);
1561
0
        Py_XDECREF(value);
1562
0
        Py_XDECREF(tb);
1563
0
        break;
1564
0
    }
1565
0
    case E_LINECONT:
1566
0
        msg = "unexpected character after line continuation character";
1567
0
        break;
1568
1569
0
    case E_IDENTIFIER:
1570
0
        msg = "invalid character in identifier";
1571
0
        break;
1572
0
    case E_BADSINGLE:
1573
0
        msg = "multiple statements found while compiling a single statement";
1574
0
        break;
1575
0
    default:
1576
0
        fprintf(stderr, "error=%d\n", err->error);
1577
0
        msg = "unknown parsing error";
1578
0
        break;
1579
0
    }
1580
    /* err->text may not be UTF-8 in case of decoding errors.
1581
       Explicitly convert to an object. */
1582
0
    if (!err->text) {
1583
0
        errtext = Py_None;
1584
0
        Py_INCREF(Py_None);
1585
0
    } else {
1586
0
        errtext = PyUnicode_DecodeUTF8(err->text, err->offset,
1587
0
                                       "replace");
1588
0
        if (errtext != NULL) {
1589
0
            Py_ssize_t len = strlen(err->text);
1590
0
            offset = (int)PyUnicode_GET_LENGTH(errtext);
1591
0
            if (len != err->offset) {
1592
0
                Py_DECREF(errtext);
1593
0
                errtext = PyUnicode_DecodeUTF8(err->text, len,
1594
0
                                               "replace");
1595
0
            }
1596
0
        }
1597
0
    }
1598
0
    v = Py_BuildValue("(OiiN)", err->filename,
1599
0
                      err->lineno, offset, errtext);
1600
0
    if (v != NULL) {
1601
0
        if (msg_obj)
1602
0
            w = Py_BuildValue("(OO)", msg_obj, v);
1603
0
        else
1604
0
            w = Py_BuildValue("(sO)", msg, v);
1605
0
    } else
1606
0
        w = NULL;
1607
0
    Py_XDECREF(v);
1608
0
    PyErr_SetObject(errtype, w);
1609
0
    Py_XDECREF(w);
1610
0
cleanup:
1611
0
    Py_XDECREF(msg_obj);
1612
0
    if (err->text != NULL) {
1613
0
        PyObject_FREE(err->text);
1614
0
        err->text = NULL;
1615
0
    }
1616
0
}
1617
1618
1619
#if defined(USE_STACKCHECK)
1620
#if defined(WIN32) && defined(_MSC_VER)
1621
1622
/* Stack checking for Microsoft C */
1623
1624
#include <malloc.h>
1625
#include <excpt.h>
1626
1627
/*
1628
 * Return non-zero when we run out of memory on the stack; zero otherwise.
1629
 */
1630
int
1631
PyOS_CheckStack(void)
1632
{
1633
    __try {
1634
        /* alloca throws a stack overflow exception if there's
1635
           not enough space left on the stack */
1636
        alloca(PYOS_STACK_MARGIN * sizeof(void*));
1637
        return 0;
1638
    } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1639
                    EXCEPTION_EXECUTE_HANDLER :
1640
            EXCEPTION_CONTINUE_SEARCH) {
1641
        int errcode = _resetstkoflw();
1642
        if (errcode == 0)
1643
        {
1644
            Py_FatalError("Could not reset the stack!");
1645
        }
1646
    }
1647
    return 1;
1648
}
1649
1650
#endif /* WIN32 && _MSC_VER */
1651
1652
/* Alternate implementations can be added here... */
1653
1654
#endif /* USE_STACKCHECK */
1655
1656
/* Deprecated C API functions still provided for binary compatibility */
1657
1658
#undef PyParser_SimpleParseFile
1659
PyAPI_FUNC(node *)
1660
PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1661
0
{
1662
0
    return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1663
0
}
1664
1665
#undef PyParser_SimpleParseString
1666
PyAPI_FUNC(node *)
1667
PyParser_SimpleParseString(const char *str, int start)
1668
0
{
1669
0
    return PyParser_SimpleParseStringFlags(str, start, 0);
1670
0
}
1671
1672
#undef PyRun_AnyFile
1673
PyAPI_FUNC(int)
1674
PyRun_AnyFile(FILE *fp, const char *name)
1675
0
{
1676
0
    return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1677
0
}
1678
1679
#undef PyRun_AnyFileEx
1680
PyAPI_FUNC(int)
1681
PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1682
0
{
1683
0
    return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1684
0
}
1685
1686
#undef PyRun_AnyFileFlags
1687
PyAPI_FUNC(int)
1688
PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1689
0
{
1690
0
    return PyRun_AnyFileExFlags(fp, name, 0, flags);
1691
0
}
1692
1693
#undef PyRun_File
1694
PyAPI_FUNC(PyObject *)
1695
PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1696
0
{
1697
0
    return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1698
0
}
1699
1700
#undef PyRun_FileEx
1701
PyAPI_FUNC(PyObject *)
1702
PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1703
0
{
1704
0
    return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1705
0
}
1706
1707
#undef PyRun_FileFlags
1708
PyAPI_FUNC(PyObject *)
1709
PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1710
                PyCompilerFlags *flags)
1711
0
{
1712
0
    return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1713
0
}
1714
1715
#undef PyRun_SimpleFile
1716
PyAPI_FUNC(int)
1717
PyRun_SimpleFile(FILE *f, const char *p)
1718
0
{
1719
0
    return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1720
0
}
1721
1722
#undef PyRun_SimpleFileEx
1723
PyAPI_FUNC(int)
1724
PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1725
0
{
1726
0
    return PyRun_SimpleFileExFlags(f, p, c, NULL);
1727
0
}
1728
1729
1730
#undef PyRun_String
1731
PyAPI_FUNC(PyObject *)
1732
PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1733
0
{
1734
0
    return PyRun_StringFlags(str, s, g, l, NULL);
1735
0
}
1736
1737
#undef PyRun_SimpleString
1738
PyAPI_FUNC(int)
1739
PyRun_SimpleString(const char *s)
1740
0
{
1741
0
    return PyRun_SimpleStringFlags(s, NULL);
1742
0
}
1743
1744
#undef Py_CompileString
1745
PyAPI_FUNC(PyObject *)
1746
Py_CompileString(const char *str, const char *p, int s)
1747
0
{
1748
0
    return Py_CompileStringExFlags(str, p, s, NULL, -1);
1749
0
}
1750
1751
#undef Py_CompileStringFlags
1752
PyAPI_FUNC(PyObject *)
1753
Py_CompileStringFlags(const char *str, const char *p, int s,
1754
                      PyCompilerFlags *flags)
1755
0
{
1756
0
    return Py_CompileStringExFlags(str, p, s, flags, -1);
1757
0
}
1758
1759
#undef PyRun_InteractiveOne
1760
PyAPI_FUNC(int)
1761
PyRun_InteractiveOne(FILE *f, const char *p)
1762
0
{
1763
0
    return PyRun_InteractiveOneFlags(f, p, NULL);
1764
0
}
1765
1766
#undef PyRun_InteractiveLoop
1767
PyAPI_FUNC(int)
1768
PyRun_InteractiveLoop(FILE *f, const char *p)
1769
0
{
1770
0
    return PyRun_InteractiveLoopFlags(f, p, NULL);
1771
0
}
1772
1773
#ifdef __cplusplus
1774
}
1775
#endif