Coverage Report

Created: 2026-03-08 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/pythonrun.c
Line
Count
Source
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 (bootstrap,
6
 * pythonrun, shutdown)
7
 */
8
9
/* TODO: Cull includes following phase split */
10
11
#include "Python.h"
12
13
#include "pycore_ast.h"           // PyAST_mod2obj()
14
#include "pycore_audit.h"         // _PySys_Audit()
15
#include "pycore_ceval.h"         // _Py_EnterRecursiveCall()
16
#include "pycore_compile.h"       // _PyAST_Compile()
17
#include "pycore_fileutils.h"     // _PyFile_Flush
18
#include "pycore_import.h"        // _PyImport_GetImportlibExternalLoader()
19
#include "pycore_interp.h"        // PyInterpreterState.importlib
20
#include "pycore_object.h"        // _PyDebug_PrintTotalRefs()
21
#include "pycore_parser.h"        // _PyParser_ASTFromString()
22
#include "pycore_pyerrors.h"      // _PyErr_GetRaisedException()
23
#include "pycore_pylifecycle.h"   // _Py_FdIsInteractive()
24
#include "pycore_pystate.h"       // _PyInterpreterState_GET()
25
#include "pycore_pythonrun.h"     // export _PyRun_InteractiveLoopObject()
26
#include "pycore_sysmodule.h"     // _PySys_SetAttr()
27
#include "pycore_traceback.h"     // _PyTraceBack_Print()
28
#include "pycore_unicodeobject.h" // _PyUnicode_Equal()
29
30
#include "errcode.h"              // E_EOF
31
#include "marshal.h"              // PyMarshal_ReadLongFromFile()
32
33
#include <stdbool.h>
34
35
#ifdef MS_WINDOWS
36
#  include "malloc.h"             // alloca()
37
#endif
38
39
#ifdef MS_WINDOWS
40
#  undef BYTE
41
#  include "windows.h"
42
#endif
43
44
/* Forward */
45
static void flush_io(void);
46
static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
47
                          PyCompilerFlags *, PyArena *, PyObject*, int);
48
static PyObject *run_pyc_file(FILE *, PyObject *, PyObject *,
49
                              PyCompilerFlags *);
50
static int PyRun_InteractiveOneObjectEx(FILE *, PyObject *, PyCompilerFlags *);
51
static PyObject* pyrun_file(FILE *fp, PyObject *filename, int start,
52
                            PyObject *globals, PyObject *locals, int closeit,
53
                            PyCompilerFlags *flags);
54
static PyObject *
55
_PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
56
                           PyObject *globals, PyObject *locals, PyCompilerFlags *flags,
57
                           int generate_new_source);
58
59
int
60
_PyRun_AnyFileObject(FILE *fp, PyObject *filename, int closeit,
61
                     PyCompilerFlags *flags)
62
0
{
63
0
    int decref_filename = 0;
64
0
    if (filename == NULL) {
65
0
        filename = PyUnicode_FromString("???");
66
0
        if (filename == NULL) {
67
0
            PyErr_Print();
68
0
            return -1;
69
0
        }
70
0
        decref_filename = 1;
71
0
    }
72
73
0
    int res;
74
0
    if (_Py_FdIsInteractive(fp, filename)) {
75
0
        res = _PyRun_InteractiveLoopObject(fp, filename, flags);
76
0
        if (closeit) {
77
0
            fclose(fp);
78
0
        }
79
0
    }
80
0
    else {
81
0
        res = _PyRun_SimpleFileObject(fp, filename, closeit, flags);
82
0
    }
83
84
0
    if (decref_filename) {
85
0
        Py_DECREF(filename);
86
0
    }
87
0
    return res;
88
0
}
89
90
int
91
PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
92
                     PyCompilerFlags *flags)
93
0
{
94
0
    PyObject *filename_obj = NULL;
95
0
    if (filename != NULL) {
96
0
        filename_obj = PyUnicode_DecodeFSDefault(filename);
97
0
        if (filename_obj == NULL) {
98
0
            PyErr_Print();
99
0
            return -1;
100
0
        }
101
0
    }
102
0
    int res = _PyRun_AnyFileObject(fp, filename_obj, closeit, flags);
103
0
    Py_XDECREF(filename_obj);
104
0
    return res;
105
0
}
106
107
108
int
109
_PyRun_InteractiveLoopObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
110
0
{
111
0
    PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
112
0
    if (flags == NULL) {
113
0
        flags = &local_flags;
114
0
    }
115
116
0
    PyObject *v;
117
0
    if (PySys_GetOptionalAttr(&_Py_ID(ps1), &v) < 0) {
118
0
        PyErr_Print();
119
0
        return -1;
120
0
    }
121
0
    if (v == NULL) {
122
0
        v = PyUnicode_FromString(">>> ");
123
0
        if (v == NULL) {
124
0
            PyErr_Clear();
125
0
        }
126
0
        if (_PySys_SetAttr(&_Py_ID(ps1), v) < 0) {
127
0
            PyErr_Clear();
128
0
        }
129
0
    }
130
0
    Py_XDECREF(v);
131
0
    if (PySys_GetOptionalAttr(&_Py_ID(ps2), &v) < 0) {
132
0
        PyErr_Print();
133
0
        return -1;
134
0
    }
135
0
    if (v == NULL) {
136
0
        v = PyUnicode_FromString("... ");
137
0
        if (v == NULL) {
138
0
            PyErr_Clear();
139
0
        }
140
0
        if (_PySys_SetAttr(&_Py_ID(ps2), v) < 0) {
141
0
            PyErr_Clear();
142
0
        }
143
0
    }
144
0
    Py_XDECREF(v);
145
146
#ifdef Py_REF_DEBUG
147
    int show_ref_count = _Py_GetConfig()->show_ref_count;
148
#endif
149
0
    int err = 0;
150
0
    int ret;
151
0
    int nomem_count = 0;
152
0
    do {
153
0
        ret = PyRun_InteractiveOneObjectEx(fp, filename, flags);
154
0
        if (ret == -1 && PyErr_Occurred()) {
155
            /* Prevent an endless loop after multiple consecutive MemoryErrors
156
             * while still allowing an interactive command to fail with a
157
             * MemoryError. */
158
0
            if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
159
0
                if (++nomem_count > 16) {
160
0
                    PyErr_Clear();
161
0
                    err = -1;
162
0
                    break;
163
0
                }
164
0
            } else {
165
0
                nomem_count = 0;
166
0
            }
167
0
            PyErr_Print();
168
0
            flush_io();
169
0
        } else {
170
0
            nomem_count = 0;
171
0
        }
172
#ifdef Py_REF_DEBUG
173
        if (show_ref_count) {
174
            _PyDebug_PrintTotalRefs();
175
        }
176
#endif
177
0
    } while (ret != E_EOF);
178
0
    return err;
179
0
}
180
181
182
int
183
PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
184
0
{
185
0
    PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
186
0
    if (filename_obj == NULL) {
187
0
        PyErr_Print();
188
0
        return -1;
189
0
    }
190
191
0
    int err = _PyRun_InteractiveLoopObject(fp, filename_obj, flags);
192
0
    Py_DECREF(filename_obj);
193
0
    return err;
194
195
0
}
196
197
198
// Call _PyParser_ASTFromFile() with sys.stdin.encoding, sys.ps1 and sys.ps2
199
static int
200
pyrun_one_parse_ast(FILE *fp, PyObject *filename,
201
                    PyCompilerFlags *flags, PyArena *arena,
202
                    mod_ty *pmod, PyObject** interactive_src)
203
0
{
204
    // Get sys.stdin.encoding (as UTF-8)
205
0
    PyObject *attr;
206
0
    PyObject *encoding_obj = NULL;
207
0
    const char *encoding = NULL;
208
0
    if (fp == stdin) {
209
0
        if (PySys_GetOptionalAttr(&_Py_ID(stdin), &attr) < 0) {
210
0
            PyErr_Clear();
211
0
        }
212
0
        else if (attr != NULL && attr != Py_None) {
213
0
            if (PyObject_GetOptionalAttr(attr, &_Py_ID(encoding), &encoding_obj) < 0) {
214
0
                PyErr_Clear();
215
0
            }
216
0
            else if (encoding_obj && PyUnicode_Check(encoding_obj)) {
217
0
                encoding = PyUnicode_AsUTF8(encoding_obj);
218
0
                if (!encoding) {
219
0
                    PyErr_Clear();
220
0
                }
221
0
            }
222
0
        }
223
0
        Py_XDECREF(attr);
224
0
    }
225
226
    // Get sys.ps1 (as UTF-8)
227
0
    PyObject *ps1_obj = NULL;
228
0
    const char *ps1 = "";
229
0
    if (PySys_GetOptionalAttr(&_Py_ID(ps1), &attr) < 0) {
230
0
        PyErr_Clear();
231
0
    }
232
0
    else if (attr != NULL) {
233
0
        ps1_obj = PyObject_Str(attr);
234
0
        Py_DECREF(attr);
235
0
        if (ps1_obj == NULL) {
236
0
            PyErr_Clear();
237
0
        }
238
0
        else if (PyUnicode_Check(ps1_obj)) {
239
0
            ps1 = PyUnicode_AsUTF8(ps1_obj);
240
0
            if (ps1 == NULL) {
241
0
                PyErr_Clear();
242
0
                ps1 = "";
243
0
            }
244
0
        }
245
0
    }
246
247
    // Get sys.ps2 (as UTF-8)
248
0
    PyObject *ps2_obj = NULL;
249
0
    const char *ps2 = "";
250
0
    if (PySys_GetOptionalAttr(&_Py_ID(ps2), &attr) < 0) {
251
0
        PyErr_Clear();
252
0
    }
253
0
    else if (attr != NULL) {
254
0
        ps2_obj = PyObject_Str(attr);
255
0
        Py_DECREF(attr);
256
0
        if (ps2_obj == NULL) {
257
0
            PyErr_Clear();
258
0
        }
259
0
        else if (PyUnicode_Check(ps2_obj)) {
260
0
            ps2 = PyUnicode_AsUTF8(ps2_obj);
261
0
            if (ps2 == NULL) {
262
0
                PyErr_Clear();
263
0
                ps2 = "";
264
0
            }
265
0
        }
266
0
    }
267
268
0
    int errcode = 0;
269
0
    *pmod = _PyParser_InteractiveASTFromFile(fp, filename, encoding,
270
0
                                             Py_single_input, ps1, ps2,
271
0
                                             flags, &errcode, interactive_src, arena);
272
0
    Py_XDECREF(ps1_obj);
273
0
    Py_XDECREF(ps2_obj);
274
0
    Py_XDECREF(encoding_obj);
275
276
0
    if (*pmod == NULL) {
277
0
        if (errcode == E_EOF) {
278
0
            PyErr_Clear();
279
0
            return E_EOF;
280
0
        }
281
0
        return -1;
282
0
    }
283
0
    return 0;
284
0
}
285
286
287
/* A PyRun_InteractiveOneObject() auxiliary function that does not print the
288
 * error on failure. */
289
static int
290
PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
291
                             PyCompilerFlags *flags)
292
0
{
293
0
    PyArena *arena = _PyArena_New();
294
0
    if (arena == NULL) {
295
0
        return -1;
296
0
    }
297
298
0
    mod_ty mod;
299
0
    PyObject *interactive_src;
300
0
    int parse_res = pyrun_one_parse_ast(fp, filename, flags, arena, &mod, &interactive_src);
301
0
    if (parse_res != 0) {
302
0
        _PyArena_Free(arena);
303
0
        return parse_res;
304
0
    }
305
306
0
    PyObject *main_module = PyImport_AddModuleRef("__main__");
307
0
    if (main_module == NULL) {
308
0
        _PyArena_Free(arena);
309
0
        return -1;
310
0
    }
311
0
    PyObject *main_dict = PyModule_GetDict(main_module);  // borrowed ref
312
313
0
    PyObject *res = run_mod(mod, filename, main_dict, main_dict, flags, arena, interactive_src, 1);
314
0
    Py_INCREF(interactive_src);
315
0
    _PyArena_Free(arena);
316
0
    Py_DECREF(main_module);
317
0
    if (res == NULL) {
318
0
        PyThreadState *tstate = _PyThreadState_GET();
319
0
        PyObject *exc = _PyErr_GetRaisedException(tstate);
320
0
        if (PyType_IsSubtype(Py_TYPE(exc),
321
0
                             (PyTypeObject *) PyExc_SyntaxError))
322
0
        {
323
            /* fix "text" attribute */
324
0
            assert(interactive_src != NULL);
325
0
            PyObject *xs = PyUnicode_Splitlines(interactive_src, 1);
326
0
            if (xs == NULL) {
327
0
                goto error;
328
0
            }
329
0
            PyObject *exc_lineno = PyObject_GetAttr(exc, &_Py_ID(lineno));
330
0
            if (exc_lineno == NULL) {
331
0
                Py_DECREF(xs);
332
0
                goto error;
333
0
            }
334
0
            int n = PyLong_AsInt(exc_lineno);
335
0
            Py_DECREF(exc_lineno);
336
0
            if (n <= 0 || n > PyList_GET_SIZE(xs)) {
337
0
                Py_DECREF(xs);
338
0
                goto error;
339
0
            }
340
0
            PyObject *line = PyList_GET_ITEM(xs, n - 1);
341
0
            PyObject_SetAttr(exc, &_Py_ID(text), line);
342
0
            Py_DECREF(xs);
343
0
        }
344
0
error:
345
0
        Py_DECREF(interactive_src);
346
0
        _PyErr_SetRaisedException(tstate, exc);
347
0
        return -1;
348
0
    }
349
0
    Py_DECREF(interactive_src);
350
0
    Py_DECREF(res);
351
352
0
    flush_io();
353
0
    return 0;
354
0
}
355
356
int
357
PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
358
0
{
359
0
    int res;
360
361
0
    res = PyRun_InteractiveOneObjectEx(fp, filename, flags);
362
0
    if (res == -1) {
363
0
        PyErr_Print();
364
0
        flush_io();
365
0
    }
366
0
    return res;
367
0
}
368
369
int
370
PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
371
0
{
372
0
    PyObject *filename;
373
0
    int res;
374
375
0
    filename = PyUnicode_DecodeFSDefault(filename_str);
376
0
    if (filename == NULL) {
377
0
        PyErr_Print();
378
0
        return -1;
379
0
    }
380
0
    res = PyRun_InteractiveOneObject(fp, filename, flags);
381
0
    Py_DECREF(filename);
382
0
    return res;
383
0
}
384
385
386
/* Check whether a file maybe a pyc file: Look at the extension,
387
   the file type, and, if we may close it, at the first few bytes. */
388
389
static int
390
maybe_pyc_file(FILE *fp, PyObject *filename, int closeit)
391
0
{
392
0
    PyObject *ext = PyUnicode_FromString(".pyc");
393
0
    if (ext == NULL) {
394
0
        return -1;
395
0
    }
396
0
    Py_ssize_t endswith = PyUnicode_Tailmatch(filename, ext, 0, PY_SSIZE_T_MAX, +1);
397
0
    Py_DECREF(ext);
398
0
    if (endswith) {
399
0
        return 1;
400
0
    }
401
402
    /* Only look into the file if we are allowed to close it, since
403
       it then should also be seekable. */
404
0
    if (!closeit) {
405
0
        return 0;
406
0
    }
407
408
    /* Read only two bytes of the magic. If the file was opened in
409
       text mode, the bytes 3 and 4 of the magic (\r\n) might not
410
       be read as they are on disk. */
411
0
    unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
412
0
    unsigned char buf[2];
413
    /* Mess:  In case of -x, the stream is NOT at its start now,
414
       and ungetc() was used to push back the first newline,
415
       which makes the current stream position formally undefined,
416
       and a x-platform nightmare.
417
       Unfortunately, we have no direct way to know whether -x
418
       was specified.  So we use a terrible hack:  if the current
419
       stream position is not 0, we assume -x was specified, and
420
       give up.  Bug 132850 on SourceForge spells out the
421
       hopelessness of trying anything else (fseek and ftell
422
       don't work predictably x-platform for text-mode files).
423
    */
424
0
    int ispyc = 0;
425
0
    if (ftell(fp) == 0) {
426
0
        if (fread(buf, 1, 2, fp) == 2 &&
427
0
            ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
428
0
            ispyc = 1;
429
0
        rewind(fp);
430
0
    }
431
0
    return ispyc;
432
0
}
433
434
435
static int
436
set_main_loader(PyObject *d, PyObject *filename, const char *loader_name)
437
0
{
438
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
439
0
    PyObject *loader_type = _PyImport_GetImportlibExternalLoader(interp,
440
0
                                                                 loader_name);
441
0
    if (loader_type == NULL) {
442
0
        return -1;
443
0
    }
444
445
0
    PyObject *loader = PyObject_CallFunction(loader_type,
446
0
                                             "sO", "__main__", filename);
447
0
    Py_DECREF(loader_type);
448
0
    if (loader == NULL) {
449
0
        return -1;
450
0
    }
451
452
0
    if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
453
0
        Py_DECREF(loader);
454
0
        return -1;
455
0
    }
456
0
    Py_DECREF(loader);
457
0
    return 0;
458
0
}
459
460
461
int
462
_PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
463
                        PyCompilerFlags *flags)
464
0
{
465
0
    int ret = -1;
466
467
0
    PyObject *main_module = PyImport_AddModuleRef("__main__");
468
0
    if (main_module == NULL)
469
0
        return -1;
470
0
    PyObject *dict = PyModule_GetDict(main_module);  // borrowed ref
471
472
0
    int set_file_name = 0;
473
0
    int has_file = PyDict_ContainsString(dict, "__file__");
474
0
    if (has_file < 0) {
475
0
        goto done;
476
0
    }
477
0
    if (!has_file) {
478
0
        if (PyDict_SetItemString(dict, "__file__", filename) < 0) {
479
0
            goto done;
480
0
        }
481
0
        set_file_name = 1;
482
0
    }
483
484
0
    int pyc = maybe_pyc_file(fp, filename, closeit);
485
0
    if (pyc < 0) {
486
0
        goto done;
487
0
    }
488
489
0
    PyObject *v;
490
0
    if (pyc) {
491
0
        FILE *pyc_fp;
492
        /* Try to run a pyc file. First, re-open in binary */
493
0
        if (closeit) {
494
0
            fclose(fp);
495
0
        }
496
497
0
        pyc_fp = Py_fopen(filename, "rb");
498
0
        if (pyc_fp == NULL) {
499
0
            fprintf(stderr, "python: Can't reopen .pyc file\n");
500
0
            goto done;
501
0
        }
502
503
0
        if (set_main_loader(dict, filename, "SourcelessFileLoader") < 0) {
504
0
            fprintf(stderr, "python: failed to set __main__.__loader__\n");
505
0
            ret = -1;
506
0
            fclose(pyc_fp);
507
0
            goto done;
508
0
        }
509
0
        v = run_pyc_file(pyc_fp, dict, dict, flags);
510
0
    } else {
511
        /* When running from stdin, leave __main__.__loader__ alone */
512
0
        if ((!PyUnicode_Check(filename) || !PyUnicode_EqualToUTF8(filename, "<stdin>")) &&
513
0
            set_main_loader(dict, filename, "SourceFileLoader") < 0) {
514
0
            fprintf(stderr, "python: failed to set __main__.__loader__\n");
515
0
            ret = -1;
516
0
            goto done;
517
0
        }
518
0
        v = pyrun_file(fp, filename, Py_file_input, dict, dict,
519
0
                       closeit, flags);
520
0
    }
521
0
    flush_io();
522
0
    if (v == NULL) {
523
0
        Py_CLEAR(main_module);
524
0
        PyErr_Print();
525
0
        goto done;
526
0
    }
527
0
    Py_DECREF(v);
528
0
    ret = 0;
529
530
0
  done:
531
0
    if (set_file_name) {
532
0
        if (PyDict_PopString(dict, "__file__", NULL) < 0) {
533
0
            PyErr_Print();
534
0
        }
535
0
    }
536
0
    Py_XDECREF(main_module);
537
0
    return ret;
538
0
}
539
540
541
int
542
PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
543
                        PyCompilerFlags *flags)
544
0
{
545
0
    PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
546
0
    if (filename_obj == NULL) {
547
0
        return -1;
548
0
    }
549
0
    int res = _PyRun_SimpleFileObject(fp, filename_obj, closeit, flags);
550
0
    Py_DECREF(filename_obj);
551
0
    return res;
552
0
}
553
554
555
int
556
68
_PyRun_SimpleStringFlagsWithName(const char *command, const char* name, PyCompilerFlags *flags) {
557
68
    PyObject *main_module = PyImport_AddModuleRef("__main__");
558
68
    if (main_module == NULL) {
559
0
        return -1;
560
0
    }
561
68
    PyObject *dict = PyModule_GetDict(main_module);  // borrowed ref
562
563
68
    PyObject *res = NULL;
564
68
    if (name == NULL) {
565
68
        res = PyRun_StringFlags(command, Py_file_input, dict, dict, flags);
566
68
    } else {
567
0
        PyObject* the_name = PyUnicode_FromString(name);
568
0
        if (!the_name) {
569
0
            PyErr_Print();
570
0
            return -1;
571
0
        }
572
0
        res = _PyRun_StringFlagsWithName(command, the_name, Py_file_input, dict, dict, flags, 0);
573
0
        Py_DECREF(the_name);
574
0
    }
575
68
    Py_DECREF(main_module);
576
68
    if (res == NULL) {
577
0
        PyErr_Print();
578
0
        return -1;
579
0
    }
580
581
68
    Py_DECREF(res);
582
68
    return 0;
583
68
}
584
585
int
586
PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
587
68
{
588
68
    return _PyRun_SimpleStringFlagsWithName(command, NULL, flags);
589
68
}
590
591
static int
592
parse_exit_code(PyObject *code, int *exitcode_p)
593
0
{
594
0
    if (PyLong_Check(code)) {
595
        // gh-125842: Use a long long to avoid an overflow error when `long`
596
        // is 32-bit. We still truncate the result to an int.
597
0
        int exitcode = (int)PyLong_AsLongLong(code);
598
0
        if (exitcode == -1 && PyErr_Occurred()) {
599
            // On overflow or other error, clear the exception and use -1
600
            // as the exit code to match historical Python behavior.
601
0
            PyErr_Clear();
602
0
            *exitcode_p = -1;
603
0
            return 1;
604
0
        }
605
0
        *exitcode_p = exitcode;
606
0
        return 1;
607
0
    }
608
0
    else if (code == Py_None) {
609
0
        *exitcode_p = 0;
610
0
        return 1;
611
0
    }
612
0
    return 0;
613
0
}
614
615
int
616
_Py_HandleSystemExitAndKeyboardInterrupt(int *exitcode_p)
617
0
{
618
0
    if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
619
0
        _Py_atomic_store_int(&_PyRuntime.signals.unhandled_keyboard_interrupt, 1);
620
0
        return 0;
621
0
    }
622
623
0
    int inspect = _Py_GetConfig()->inspect;
624
0
    if (inspect) {
625
        /* Don't exit if -i flag was given. This flag is set to 0
626
         * when entering interactive mode for inspecting. */
627
0
        return 0;
628
0
    }
629
630
0
    if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
631
0
        return 0;
632
0
    }
633
634
0
    fflush(stdout);
635
636
0
    PyObject *exc = PyErr_GetRaisedException();
637
0
    assert(exc != NULL && PyExceptionInstance_Check(exc));
638
639
0
    PyObject *code = PyObject_GetAttr(exc, &_Py_ID(code));
640
0
    if (code == NULL) {
641
        // If the exception has no 'code' attribute, print the exception below
642
0
        PyErr_Clear();
643
0
    }
644
0
    else if (parse_exit_code(code, exitcode_p)) {
645
0
        Py_DECREF(code);
646
0
        Py_CLEAR(exc);
647
0
        return 1;
648
0
    }
649
0
    else {
650
        // If code is not an int or None, print it below
651
0
        Py_SETREF(exc, code);
652
0
    }
653
654
0
    PyObject *sys_stderr;
655
0
    if (PySys_GetOptionalAttr(&_Py_ID(stderr), &sys_stderr) < 0) {
656
0
        PyErr_Clear();
657
0
    }
658
0
    else if (sys_stderr != NULL && sys_stderr != Py_None) {
659
0
        if (PyFile_WriteObject(exc, sys_stderr, Py_PRINT_RAW) < 0) {
660
0
            PyErr_Clear();
661
0
        }
662
0
    }
663
0
    else {
664
0
        if (PyObject_Print(exc, stderr, Py_PRINT_RAW) < 0) {
665
0
            PyErr_Clear();
666
0
        }
667
0
        fflush(stderr);
668
0
    }
669
0
    PySys_WriteStderr("\n");
670
0
    Py_XDECREF(sys_stderr);
671
0
    Py_CLEAR(exc);
672
0
    *exitcode_p = 1;
673
0
    return 1;
674
0
}
675
676
677
static void
678
handle_system_exit(void)
679
0
{
680
0
    int exitcode;
681
0
    if (_Py_HandleSystemExitAndKeyboardInterrupt(&exitcode)) {
682
0
        Py_Exit(exitcode);
683
0
    }
684
0
}
685
686
687
static void
688
_PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
689
0
{
690
0
    PyObject *typ = NULL, *tb = NULL, *hook = NULL;
691
0
    handle_system_exit();
692
693
0
    PyObject *exc = _PyErr_GetRaisedException(tstate);
694
0
    if (exc == NULL) {
695
0
        goto done;
696
0
    }
697
0
    assert(PyExceptionInstance_Check(exc));
698
0
    typ = Py_NewRef(Py_TYPE(exc));
699
0
    tb = PyException_GetTraceback(exc);
700
0
    if (tb == NULL) {
701
0
        tb = Py_NewRef(Py_None);
702
0
    }
703
704
0
    if (set_sys_last_vars) {
705
0
        if (_PySys_SetAttr(&_Py_ID(last_exc), exc) < 0) {
706
0
            _PyErr_Clear(tstate);
707
0
        }
708
        /* Legacy version: */
709
0
        if (_PySys_SetAttr(&_Py_ID(last_type), typ) < 0) {
710
0
            _PyErr_Clear(tstate);
711
0
        }
712
0
        if (_PySys_SetAttr(&_Py_ID(last_value), exc) < 0) {
713
0
            _PyErr_Clear(tstate);
714
0
        }
715
0
        if (_PySys_SetAttr(&_Py_ID(last_traceback), tb) < 0) {
716
0
            _PyErr_Clear(tstate);
717
0
        }
718
0
    }
719
0
    if (PySys_GetOptionalAttr(&_Py_ID(excepthook), &hook) < 0) {
720
0
        PyErr_Clear();
721
0
    }
722
0
    if (_PySys_Audit(tstate, "sys.excepthook", "OOOO", hook ? hook : Py_None,
723
0
                     typ, exc, tb) < 0) {
724
0
        if (PyErr_ExceptionMatches(PyExc_RuntimeError)) {
725
0
            PyErr_Clear();
726
0
            goto done;
727
0
        }
728
0
        PyErr_FormatUnraisable("Exception ignored in audit hook");
729
0
    }
730
0
    if (hook) {
731
0
        PyObject* args[3] = {typ, exc, tb};
732
0
        PyObject *result = PyObject_Vectorcall(hook, args, 3, NULL);
733
0
        if (result == NULL) {
734
0
            handle_system_exit();
735
736
0
            PyObject *exc2 = _PyErr_GetRaisedException(tstate);
737
0
            assert(exc2 && PyExceptionInstance_Check(exc2));
738
0
            fflush(stdout);
739
0
            PySys_WriteStderr("Error in sys.excepthook:\n");
740
0
            PyErr_DisplayException(exc2);
741
0
            PySys_WriteStderr("\nOriginal exception was:\n");
742
0
            PyErr_DisplayException(exc);
743
0
            Py_DECREF(exc2);
744
0
        }
745
0
        else {
746
0
            Py_DECREF(result);
747
0
        }
748
0
    }
749
0
    else {
750
0
        PySys_WriteStderr("sys.excepthook is missing\n");
751
0
        PyErr_DisplayException(exc);
752
0
    }
753
754
0
done:
755
0
    Py_XDECREF(hook);
756
0
    Py_XDECREF(typ);
757
0
    Py_XDECREF(exc);
758
0
    Py_XDECREF(tb);
759
0
}
760
761
void
762
_PyErr_Print(PyThreadState *tstate)
763
0
{
764
0
    _PyErr_PrintEx(tstate, 1);
765
0
}
766
767
void
768
PyErr_PrintEx(int set_sys_last_vars)
769
0
{
770
0
    PyThreadState *tstate = _PyThreadState_GET();
771
0
    _PyErr_PrintEx(tstate, set_sys_last_vars);
772
0
}
773
774
void
775
PyErr_Print(void)
776
0
{
777
0
    PyErr_PrintEx(1);
778
0
}
779
780
struct exception_print_context
781
{
782
    PyObject *file;
783
    PyObject *seen;            // Prevent cycles in recursion
784
};
785
786
static int
787
print_exception_invalid_type(struct exception_print_context *ctx,
788
                             PyObject *value)
789
0
{
790
0
    PyObject *f = ctx->file;
791
0
    const char *const msg = "TypeError: print_exception(): Exception expected "
792
0
                            "for value, ";
793
0
    if (PyFile_WriteString(msg, f) < 0) {
794
0
        return -1;
795
0
    }
796
0
    if (PyFile_WriteString(Py_TYPE(value)->tp_name, f) < 0) {
797
0
        return -1;
798
0
    }
799
0
    if (PyFile_WriteString(" found\n", f) < 0) {
800
0
        return -1;
801
0
    }
802
0
    return 0;
803
0
}
804
805
static int
806
print_exception_traceback(struct exception_print_context *ctx, PyObject *value)
807
0
{
808
0
    PyObject *f = ctx->file;
809
0
    int err = 0;
810
811
0
    PyObject *tb = PyException_GetTraceback(value);
812
0
    if (tb && tb != Py_None) {
813
0
        const char *header = EXCEPTION_TB_HEADER;
814
0
        err = _PyTraceBack_Print(tb, header, f);
815
0
    }
816
0
    Py_XDECREF(tb);
817
0
    return err;
818
0
}
819
820
static int
821
print_exception_file_and_line(struct exception_print_context *ctx,
822
                              PyObject **value_p)
823
0
{
824
0
    PyObject *f = ctx->file;
825
826
0
    PyObject *tmp;
827
0
    int res = PyObject_GetOptionalAttr(*value_p, &_Py_ID(print_file_and_line), &tmp);
828
0
    if (res <= 0) {
829
0
        if (res < 0) {
830
0
            PyErr_Clear();
831
0
        }
832
0
        return 0;
833
0
    }
834
0
    Py_DECREF(tmp);
835
836
0
    PyObject *filename = NULL;
837
0
    Py_ssize_t lineno = 0;
838
0
    PyObject* v = PyObject_GetAttr(*value_p, &_Py_ID(filename));
839
0
    if (!v) {
840
0
        return -1;
841
0
    }
842
0
    if (v == Py_None) {
843
0
        Py_DECREF(v);
844
0
        _Py_DECLARE_STR(anon_string, "<string>");
845
0
        filename = Py_NewRef(&_Py_STR(anon_string));
846
0
    }
847
0
    else {
848
0
        filename = v;
849
0
    }
850
851
0
    PyObject *line = PyUnicode_FromFormat("  File \"%S\", line %zd\n",
852
0
                                          filename, lineno);
853
0
    Py_DECREF(filename);
854
0
    if (line == NULL) {
855
0
        goto error;
856
0
    }
857
0
    if (PyFile_WriteObject(line, f, Py_PRINT_RAW) < 0) {
858
0
        goto error;
859
0
    }
860
0
    Py_CLEAR(line);
861
862
0
    assert(!PyErr_Occurred());
863
0
    return 0;
864
865
0
error:
866
0
    Py_XDECREF(line);
867
0
    return -1;
868
0
}
869
870
/* Prints the message line: module.qualname[: str(exc)] */
871
static int
872
print_exception_message(struct exception_print_context *ctx, PyObject *type,
873
                        PyObject *value)
874
0
{
875
0
    PyObject *f = ctx->file;
876
877
0
    if (PyErr_GivenExceptionMatches(value, PyExc_MemoryError)) {
878
        // The Python APIs in this function require allocating memory
879
        // for various objects. If we're out of memory, we can't do that,
880
0
        return -1;
881
0
    }
882
883
0
    assert(PyExceptionClass_Check(type));
884
885
0
    PyObject *modulename = PyObject_GetAttr(type, &_Py_ID(__module__));
886
0
    if (modulename == NULL || !PyUnicode_Check(modulename)) {
887
0
        Py_XDECREF(modulename);
888
0
        PyErr_Clear();
889
0
        if (PyFile_WriteString("<unknown>.", f) < 0) {
890
0
            return -1;
891
0
        }
892
0
    }
893
0
    else {
894
0
        if (!_PyUnicode_Equal(modulename, &_Py_ID(builtins)) &&
895
0
            !_PyUnicode_Equal(modulename, &_Py_ID(__main__)))
896
0
        {
897
0
            int res = PyFile_WriteObject(modulename, f, Py_PRINT_RAW);
898
0
            Py_DECREF(modulename);
899
0
            if (res < 0) {
900
0
                return -1;
901
0
            }
902
0
            if (PyFile_WriteString(".", f) < 0) {
903
0
                return -1;
904
0
            }
905
0
        }
906
0
        else {
907
0
            Py_DECREF(modulename);
908
0
        }
909
0
    }
910
911
0
    PyObject *qualname = PyType_GetQualName((PyTypeObject *)type);
912
0
    if (qualname == NULL || !PyUnicode_Check(qualname)) {
913
0
        Py_XDECREF(qualname);
914
0
        PyErr_Clear();
915
0
        if (PyFile_WriteString("<unknown>", f) < 0) {
916
0
            return -1;
917
0
        }
918
0
    }
919
0
    else {
920
0
        int res = PyFile_WriteObject(qualname, f, Py_PRINT_RAW);
921
0
        Py_DECREF(qualname);
922
0
        if (res < 0) {
923
0
            return -1;
924
0
        }
925
0
    }
926
927
0
    if (Py_IsNone(value)) {
928
0
        return 0;
929
0
    }
930
931
0
    PyObject *s = PyObject_Str(value);
932
0
    if (s == NULL) {
933
0
        PyErr_Clear();
934
0
        if (PyFile_WriteString(": <exception str() failed>", f) < 0) {
935
0
            return -1;
936
0
        }
937
0
    }
938
0
    else {
939
        /* only print colon if the str() of the
940
           object is not the empty string
941
        */
942
0
        if (!PyUnicode_Check(s) || PyUnicode_GetLength(s) != 0) {
943
0
            if (PyFile_WriteString(": ", f) < 0) {
944
0
                Py_DECREF(s);
945
0
                return -1;
946
0
            }
947
0
        }
948
0
        int res = PyFile_WriteObject(s, f, Py_PRINT_RAW);
949
0
        Py_DECREF(s);
950
0
        if (res < 0) {
951
0
            return -1;
952
0
        }
953
0
    }
954
955
0
    return 0;
956
0
}
957
958
static int
959
print_exception(struct exception_print_context *ctx, PyObject *value)
960
0
{
961
0
    PyObject *f = ctx->file;
962
963
0
    if (!PyExceptionInstance_Check(value)) {
964
0
        return print_exception_invalid_type(ctx, value);
965
0
    }
966
967
0
    Py_INCREF(value);
968
0
    fflush(stdout);
969
970
0
    if (print_exception_traceback(ctx, value) < 0) {
971
0
        goto error;
972
0
    }
973
974
    /* grab the type and notes now because value can change below */
975
0
    PyObject *type = (PyObject *) Py_TYPE(value);
976
977
0
    if (print_exception_file_and_line(ctx, &value) < 0) {
978
0
        goto error;
979
0
    }
980
0
    if (print_exception_message(ctx, type, value) < 0) {
981
0
        goto error;
982
0
    }
983
0
    if (PyFile_WriteString("\n", f) < 0) {
984
0
        goto error;
985
0
    }
986
0
    Py_DECREF(value);
987
0
    assert(!PyErr_Occurred());
988
0
    return 0;
989
0
error:
990
0
    Py_DECREF(value);
991
0
    return -1;
992
0
}
993
994
static const char cause_message[] =
995
    "The above exception was the direct cause "
996
    "of the following exception:\n";
997
998
static const char context_message[] =
999
    "During handling of the above exception, "
1000
    "another exception occurred:\n";
1001
1002
static int
1003
print_exception_recursive(struct exception_print_context*, PyObject*);
1004
1005
static int
1006
print_chained(struct exception_print_context* ctx, PyObject *value,
1007
              const char * message, const char *tag)
1008
0
{
1009
0
    PyObject *f = ctx->file;
1010
0
    if (_Py_EnterRecursiveCall(" in print_chained")) {
1011
0
        return -1;
1012
0
    }
1013
0
    int res = print_exception_recursive(ctx, value);
1014
0
    _Py_LeaveRecursiveCall();
1015
0
    if (res < 0) {
1016
0
        return -1;
1017
0
    }
1018
1019
0
    if (PyFile_WriteString("\n", f) < 0) {
1020
0
        return -1;
1021
0
    }
1022
0
    if (PyFile_WriteString(message, f) < 0) {
1023
0
        return -1;
1024
0
    }
1025
0
    if (PyFile_WriteString("\n", f) < 0) {
1026
0
        return -1;
1027
0
    }
1028
0
    return 0;
1029
0
}
1030
1031
/* Return true if value is in seen or there was a lookup error.
1032
 * Return false if lookup succeeded and the item was not found.
1033
 * We suppress errors because this makes us err on the side of
1034
 * under-printing which is better than over-printing irregular
1035
 * exceptions (e.g., unhashable ones).
1036
 */
1037
static bool
1038
print_exception_seen_lookup(struct exception_print_context *ctx,
1039
                            PyObject *value)
1040
0
{
1041
0
    PyObject *check_id = PyLong_FromVoidPtr(value);
1042
0
    if (check_id == NULL) {
1043
0
        PyErr_Clear();
1044
0
        return true;
1045
0
    }
1046
1047
0
    int in_seen = PySet_Contains(ctx->seen, check_id);
1048
0
    Py_DECREF(check_id);
1049
0
    if (in_seen == -1) {
1050
0
        PyErr_Clear();
1051
0
        return true;
1052
0
    }
1053
1054
0
    if (in_seen == 1) {
1055
        /* value is in seen */
1056
0
        return true;
1057
0
    }
1058
0
    return false;
1059
0
}
1060
1061
static int
1062
print_exception_cause_and_context(struct exception_print_context *ctx,
1063
                                  PyObject *value)
1064
0
{
1065
0
    PyObject *value_id = PyLong_FromVoidPtr(value);
1066
0
    if (value_id == NULL || PySet_Add(ctx->seen, value_id) == -1) {
1067
0
        PyErr_Clear();
1068
0
        Py_XDECREF(value_id);
1069
0
        return 0;
1070
0
    }
1071
0
    Py_DECREF(value_id);
1072
1073
0
    if (!PyExceptionInstance_Check(value)) {
1074
0
        return 0;
1075
0
    }
1076
1077
0
    PyObject *cause = PyException_GetCause(value);
1078
0
    if (cause) {
1079
0
        int err = 0;
1080
0
        if (!print_exception_seen_lookup(ctx, cause)) {
1081
0
            err = print_chained(ctx, cause, cause_message, "cause");
1082
0
        }
1083
0
        Py_DECREF(cause);
1084
0
        return err;
1085
0
    }
1086
0
    if (((PyBaseExceptionObject *)value)->suppress_context) {
1087
0
        return 0;
1088
0
    }
1089
0
    PyObject *context = PyException_GetContext(value);
1090
0
    if (context) {
1091
0
        int err = 0;
1092
0
        if (!print_exception_seen_lookup(ctx, context)) {
1093
0
            err = print_chained(ctx, context, context_message, "context");
1094
0
        }
1095
0
        Py_DECREF(context);
1096
0
        return err;
1097
0
    }
1098
0
    return 0;
1099
0
}
1100
1101
static int
1102
print_exception_recursive(struct exception_print_context *ctx, PyObject *value)
1103
0
{
1104
0
    if (_Py_EnterRecursiveCall(" in print_exception_recursive")) {
1105
0
        return -1;
1106
0
    }
1107
0
    if (ctx->seen != NULL) {
1108
        /* Exception chaining */
1109
0
        if (print_exception_cause_and_context(ctx, value) < 0) {
1110
0
            goto error;
1111
0
        }
1112
0
    }
1113
0
    if (print_exception(ctx, value) < 0) {
1114
0
        goto error;
1115
0
    }
1116
0
    assert(!PyErr_Occurred());
1117
1118
0
    _Py_LeaveRecursiveCall();
1119
0
    return 0;
1120
0
error:
1121
0
    _Py_LeaveRecursiveCall();
1122
0
    return -1;
1123
0
}
1124
1125
void
1126
_PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
1127
0
{
1128
0
    assert(value != NULL);
1129
0
    assert(file != NULL && file != Py_None);
1130
0
    if (PyExceptionInstance_Check(value)
1131
0
        && tb != NULL && PyTraceBack_Check(tb)) {
1132
        /* Put the traceback on the exception, otherwise it won't get
1133
           displayed.  See issue #18776. */
1134
0
        PyObject *cur_tb = PyException_GetTraceback(value);
1135
0
        if (cur_tb == NULL) {
1136
0
            PyException_SetTraceback(value, tb);
1137
0
        }
1138
0
        else {
1139
0
            Py_DECREF(cur_tb);
1140
0
        }
1141
0
    }
1142
1143
    // Try first with the stdlib traceback module
1144
0
    PyObject *print_exception_fn = PyImport_ImportModuleAttrString(
1145
0
        "traceback",
1146
0
        "_print_exception_bltin");
1147
0
    if (print_exception_fn == NULL || !PyCallable_Check(print_exception_fn)) {
1148
0
        Py_XDECREF(print_exception_fn);
1149
0
        goto fallback;
1150
0
    }
1151
1152
0
    PyObject* result = PyObject_CallOneArg(print_exception_fn, value);
1153
1154
0
    Py_XDECREF(print_exception_fn);
1155
0
    if (result) {
1156
0
        Py_DECREF(result);
1157
0
        return;
1158
0
    }
1159
0
fallback:
1160
#ifdef Py_DEBUG
1161
     if (PyErr_Occurred()) {
1162
         PyErr_FormatUnraisable(
1163
             "Exception ignored in the internal traceback machinery");
1164
     }
1165
#endif
1166
0
    PyErr_Clear();
1167
0
    struct exception_print_context ctx;
1168
0
    ctx.file = file;
1169
1170
    /* We choose to ignore seen being possibly NULL, and report
1171
       at least the main exception (it could be a MemoryError).
1172
    */
1173
0
    ctx.seen = PySet_New(NULL);
1174
0
    if (ctx.seen == NULL) {
1175
0
        PyErr_Clear();
1176
0
    }
1177
0
    if (print_exception_recursive(&ctx, value) < 0) {
1178
0
        PyErr_Clear();
1179
0
        PyObject_Dump(value);
1180
0
        fprintf(stderr, "lost sys.stderr\n");
1181
0
    }
1182
0
    Py_XDECREF(ctx.seen);
1183
1184
    /* Call file.flush() */
1185
0
    if (_PyFile_Flush(file) < 0) {
1186
        /* Silently ignore file.flush() error */
1187
0
        PyErr_Clear();
1188
0
    }
1189
0
}
1190
1191
void
1192
PyErr_Display(PyObject *unused, PyObject *value, PyObject *tb)
1193
0
{
1194
0
    PyObject *file;
1195
0
    if (PySys_GetOptionalAttr(&_Py_ID(stderr), &file) < 0) {
1196
0
        PyObject *exc = PyErr_GetRaisedException();
1197
0
        PyObject_Dump(value);
1198
0
        fprintf(stderr, "lost sys.stderr\n");
1199
0
        PyObject_Dump(exc);
1200
0
        Py_DECREF(exc);
1201
0
        return;
1202
0
    }
1203
0
    if (file == NULL) {
1204
0
        PyObject_Dump(value);
1205
0
        fprintf(stderr, "lost sys.stderr\n");
1206
0
        return;
1207
0
    }
1208
0
    if (file == Py_None) {
1209
0
        Py_DECREF(file);
1210
0
        return;
1211
0
    }
1212
0
    _PyErr_Display(file, NULL, value, tb);
1213
0
    Py_DECREF(file);
1214
0
}
1215
1216
void _PyErr_DisplayException(PyObject *file, PyObject *exc)
1217
0
{
1218
0
    _PyErr_Display(file, NULL, exc, NULL);
1219
0
}
1220
1221
void PyErr_DisplayException(PyObject *exc)
1222
0
{
1223
0
    PyErr_Display(NULL, exc, NULL);
1224
0
}
1225
1226
static PyObject *
1227
_PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
1228
                           PyObject *globals, PyObject *locals, PyCompilerFlags *flags,
1229
                           int generate_new_source)
1230
624
{
1231
624
    PyObject *ret = NULL;
1232
624
    mod_ty mod;
1233
624
    PyArena *arena;
1234
1235
624
    arena = _PyArena_New();
1236
624
    if (arena == NULL)
1237
0
        return NULL;
1238
1239
624
    PyObject* source = NULL;
1240
624
    _Py_DECLARE_STR(anon_string, "<string>");
1241
1242
624
    if (name) {
1243
0
        source = PyUnicode_FromString(str);
1244
0
        if (!source) {
1245
0
            PyErr_Clear();
1246
0
        }
1247
624
    } else {
1248
624
        name = &_Py_STR(anon_string);
1249
624
    }
1250
624
    PyObject *module = NULL;
1251
624
    if (globals && PyDict_GetItemStringRef(globals, "__name__", &module) < 0) {
1252
0
        goto done;
1253
0
    }
1254
1255
624
    mod = _PyParser_ASTFromString(str, name, start, flags, arena, module);
1256
624
    Py_XDECREF(module);
1257
1258
624
    if (mod != NULL) {
1259
624
        ret = run_mod(mod, name, globals, locals, flags, arena, source, generate_new_source);
1260
624
    }
1261
1262
624
done:
1263
624
    Py_XDECREF(source);
1264
624
    _PyArena_Free(arena);
1265
624
    return ret;
1266
624
}
1267
1268
1269
PyObject *
1270
PyRun_StringFlags(const char *str, int start, PyObject *globals,
1271
624
                     PyObject *locals, PyCompilerFlags *flags) {
1272
1273
624
    return _PyRun_StringFlagsWithName(str, NULL, start, globals, locals, flags, 0);
1274
624
}
1275
1276
static PyObject *
1277
pyrun_file(FILE *fp, PyObject *filename, int start, PyObject *globals,
1278
           PyObject *locals, int closeit, PyCompilerFlags *flags)
1279
0
{
1280
0
    PyArena *arena = _PyArena_New();
1281
0
    if (arena == NULL) {
1282
0
        return NULL;
1283
0
    }
1284
1285
0
    mod_ty mod;
1286
0
    mod = _PyParser_ASTFromFile(fp, filename, NULL, start, NULL, NULL,
1287
0
                                flags, NULL, arena);
1288
1289
0
    if (closeit) {
1290
0
        fclose(fp);
1291
0
    }
1292
1293
0
    PyObject *ret;
1294
0
    if (mod != NULL) {
1295
0
        ret = run_mod(mod, filename, globals, locals, flags, arena, NULL, 0);
1296
0
    }
1297
0
    else {
1298
0
        ret = NULL;
1299
0
    }
1300
0
    _PyArena_Free(arena);
1301
1302
0
    return ret;
1303
0
}
1304
1305
1306
PyObject *
1307
PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1308
                  PyObject *locals, int closeit, PyCompilerFlags *flags)
1309
0
{
1310
0
    PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
1311
0
    if (filename_obj == NULL) {
1312
0
        return NULL;
1313
0
    }
1314
1315
0
    PyObject *res = pyrun_file(fp, filename_obj, start, globals,
1316
0
                               locals, closeit, flags);
1317
0
    Py_DECREF(filename_obj);
1318
0
    return res;
1319
1320
0
}
1321
1322
static void
1323
flush_io_stream(PyThreadState *tstate, PyObject *name)
1324
0
{
1325
0
    PyObject *f;
1326
0
    if (PySys_GetOptionalAttr(name, &f) < 0) {
1327
0
        PyErr_Clear();
1328
0
    }
1329
0
    if (f != NULL) {
1330
0
        if (_PyFile_Flush(f) < 0) {
1331
0
            PyErr_Clear();
1332
0
        }
1333
0
        Py_DECREF(f);
1334
0
    }
1335
0
}
1336
1337
static void
1338
flush_io(void)
1339
0
{
1340
0
    PyThreadState *tstate = _PyThreadState_GET();
1341
0
    PyObject *exc = _PyErr_GetRaisedException(tstate);
1342
0
    flush_io_stream(tstate, &_Py_ID(stderr));
1343
0
    flush_io_stream(tstate, &_Py_ID(stdout));
1344
0
    _PyErr_SetRaisedException(tstate, exc);
1345
0
}
1346
1347
static PyObject *
1348
run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, PyObject *locals)
1349
624
{
1350
    /* Set globals['__builtins__'] if it doesn't exist */
1351
624
    if (!globals || !PyAnyDict_Check(globals)) {
1352
0
        PyErr_SetString(PyExc_SystemError,
1353
0
                        "globals must be a real dict or a real frozendict");
1354
0
        return NULL;
1355
0
    }
1356
624
    int has_builtins = PyDict_ContainsString(globals, "__builtins__");
1357
624
    if (has_builtins < 0) {
1358
0
        return NULL;
1359
0
    }
1360
624
    if (!has_builtins) {
1361
34
        if (PyDict_SetItemString(globals, "__builtins__",
1362
34
                                 tstate->interp->builtins) < 0)
1363
0
        {
1364
0
            return NULL;
1365
0
        }
1366
34
    }
1367
1368
624
    return PyEval_EvalCode((PyObject*)co, globals, locals);
1369
624
}
1370
1371
static PyObject *
1372
get_interactive_filename(PyObject *filename, Py_ssize_t count)
1373
0
{
1374
0
    PyObject *result;
1375
0
    Py_ssize_t len = PyUnicode_GET_LENGTH(filename);
1376
1377
0
    if (len >= 2
1378
0
            && PyUnicode_ReadChar(filename, 0) == '<'
1379
0
            && PyUnicode_ReadChar(filename, len - 1) == '>') {
1380
0
        PyObject *middle = PyUnicode_Substring(filename, 1, len-1);
1381
0
        if (middle == NULL) {
1382
0
            return NULL;
1383
0
        }
1384
0
        result = PyUnicode_FromFormat("<%U-%d>", middle, count);
1385
0
        Py_DECREF(middle);
1386
0
    } else {
1387
0
        result = PyUnicode_FromFormat(
1388
0
            "%U-%d", filename, count);
1389
0
    }
1390
0
    return result;
1391
1392
0
}
1393
1394
static PyObject *
1395
run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
1396
            PyCompilerFlags *flags, PyArena *arena, PyObject* interactive_src,
1397
            int generate_new_source)
1398
624
{
1399
624
    PyThreadState *tstate = _PyThreadState_GET();
1400
624
    PyObject* interactive_filename = filename;
1401
624
    if (interactive_src) {
1402
0
        PyInterpreterState *interp = tstate->interp;
1403
0
        if (generate_new_source) {
1404
0
            interactive_filename = get_interactive_filename(
1405
0
                filename, interp->_interactive_src_count++);
1406
0
        } else {
1407
0
            Py_INCREF(interactive_filename);
1408
0
        }
1409
0
        if (interactive_filename == NULL) {
1410
0
            return NULL;
1411
0
        }
1412
0
    }
1413
624
    PyObject *module = NULL;
1414
624
    if (globals && PyDict_GetItemStringRef(globals, "__name__", &module) < 0) {
1415
0
        if (interactive_src) {
1416
0
            Py_DECREF(interactive_filename);
1417
0
        }
1418
0
        return NULL;
1419
0
    }
1420
1421
624
    PyCodeObject *co = _PyAST_Compile(mod, interactive_filename, flags, -1,
1422
624
                                      arena, module);
1423
624
    Py_XDECREF(module);
1424
624
    if (co == NULL) {
1425
0
        if (interactive_src) {
1426
0
            Py_DECREF(interactive_filename);
1427
0
        }
1428
0
        return NULL;
1429
0
    }
1430
1431
624
    if (interactive_src) {
1432
0
        PyObject *print_tb_func = PyImport_ImportModuleAttrString(
1433
0
            "linecache",
1434
0
            "_register_code");
1435
0
        if (print_tb_func == NULL) {
1436
0
            Py_DECREF(co);
1437
0
            Py_DECREF(interactive_filename);
1438
0
            return NULL;
1439
0
        }
1440
1441
0
        if (!PyCallable_Check(print_tb_func)) {
1442
0
            Py_DECREF(co);
1443
0
            Py_DECREF(interactive_filename);
1444
0
            Py_DECREF(print_tb_func);
1445
0
            PyErr_SetString(PyExc_ValueError, "linecache._register_code is not callable");
1446
0
            return NULL;
1447
0
        }
1448
1449
0
        PyObject* result = PyObject_CallFunction(
1450
0
            print_tb_func, "OOO",
1451
0
            co,
1452
0
            interactive_src,
1453
0
            filename
1454
0
        );
1455
1456
0
        Py_DECREF(interactive_filename);
1457
1458
0
        Py_XDECREF(print_tb_func);
1459
0
        Py_XDECREF(result);
1460
0
        if (!result) {
1461
0
            Py_DECREF(co);
1462
0
            return NULL;
1463
0
        }
1464
0
    }
1465
1466
624
    if (_PySys_Audit(tstate, "exec", "O", co) < 0) {
1467
0
        Py_DECREF(co);
1468
0
        return NULL;
1469
0
    }
1470
1471
624
    PyObject *v = run_eval_code_obj(tstate, co, globals, locals);
1472
624
    Py_DECREF(co);
1473
624
    return v;
1474
624
}
1475
1476
static PyObject *
1477
run_pyc_file(FILE *fp, PyObject *globals, PyObject *locals,
1478
             PyCompilerFlags *flags)
1479
0
{
1480
0
    PyThreadState *tstate = _PyThreadState_GET();
1481
0
    PyCodeObject *co;
1482
0
    PyObject *v;
1483
0
    long magic;
1484
0
    long PyImport_GetMagicNumber(void);
1485
1486
0
    magic = PyMarshal_ReadLongFromFile(fp);
1487
0
    if (magic != PyImport_GetMagicNumber()) {
1488
0
        if (!PyErr_Occurred())
1489
0
            PyErr_SetString(PyExc_RuntimeError,
1490
0
                       "Bad magic number in .pyc file");
1491
0
        goto error;
1492
0
    }
1493
    /* Skip the rest of the header. */
1494
0
    (void) PyMarshal_ReadLongFromFile(fp);
1495
0
    (void) PyMarshal_ReadLongFromFile(fp);
1496
0
    (void) PyMarshal_ReadLongFromFile(fp);
1497
0
    if (PyErr_Occurred()) {
1498
0
        goto error;
1499
0
    }
1500
0
    v = PyMarshal_ReadLastObjectFromFile(fp);
1501
0
    if (v == NULL || !PyCode_Check(v)) {
1502
0
        Py_XDECREF(v);
1503
0
        PyErr_SetString(PyExc_RuntimeError,
1504
0
                   "Bad code object in .pyc file");
1505
0
        goto error;
1506
0
    }
1507
0
    fclose(fp);
1508
0
    co = (PyCodeObject *)v;
1509
0
    v = run_eval_code_obj(tstate, co, globals, locals);
1510
0
    if (v && flags)
1511
0
        flags->cf_flags |= (co->co_flags & PyCF_MASK);
1512
0
    Py_DECREF(co);
1513
0
    return v;
1514
0
error:
1515
0
    fclose(fp);
1516
0
    return NULL;
1517
0
}
1518
1519
PyObject *
1520
Py_CompileStringObject(const char *str, PyObject *filename, int start,
1521
                       PyCompilerFlags *flags, int optimize)
1522
0
{
1523
0
    return _Py_CompileStringObjectWithModule(str, filename, start,
1524
0
                                             flags, optimize, NULL);
1525
0
}
1526
1527
PyObject *
1528
_Py_CompileStringObjectWithModule(const char *str, PyObject *filename, int start,
1529
                       PyCompilerFlags *flags, int optimize, PyObject *module)
1530
110k
{
1531
110k
    PyCodeObject *co;
1532
110k
    mod_ty mod;
1533
110k
    PyArena *arena = _PyArena_New();
1534
110k
    if (arena == NULL)
1535
0
        return NULL;
1536
1537
110k
    mod = _PyParser_ASTFromString(str, filename, start, flags, arena, module);
1538
110k
    if (mod == NULL) {
1539
104k
        _PyArena_Free(arena);
1540
104k
        return NULL;
1541
104k
    }
1542
5.70k
    if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1543
5.50k
        int syntax_check_only = ((flags->cf_flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST); /* unoptiomized AST */
1544
5.50k
        if (_PyCompile_AstPreprocess(mod, filename, flags, optimize, arena,
1545
5.50k
                                     syntax_check_only, module) < 0)
1546
15
        {
1547
15
            _PyArena_Free(arena);
1548
15
            return NULL;
1549
15
        }
1550
5.49k
        PyObject *result = PyAST_mod2obj(mod);
1551
5.49k
        _PyArena_Free(arena);
1552
5.49k
        return result;
1553
5.50k
    }
1554
196
    co = _PyAST_Compile(mod, filename, flags, optimize, arena, module);
1555
196
    _PyArena_Free(arena);
1556
196
    return (PyObject *)co;
1557
5.70k
}
1558
1559
PyObject *
1560
Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
1561
                        PyCompilerFlags *flags, int optimize)
1562
0
{
1563
0
    PyObject *filename, *co;
1564
0
    filename = PyUnicode_DecodeFSDefault(filename_str);
1565
0
    if (filename == NULL)
1566
0
        return NULL;
1567
0
    co = Py_CompileStringObject(str, filename, start, flags, optimize);
1568
0
    Py_DECREF(filename);
1569
0
    return co;
1570
0
}
1571
1572
int
1573
_PyObject_SupportedAsScript(PyObject *cmd)
1574
0
{
1575
0
    if (PyUnicode_Check(cmd)) {
1576
0
        return 1;
1577
0
    }
1578
0
    else if (PyBytes_Check(cmd)) {
1579
0
        return 1;
1580
0
    }
1581
0
    else if (PyByteArray_Check(cmd)) {
1582
0
        return 1;
1583
0
    }
1584
0
    else if (PyObject_CheckBuffer(cmd)) {
1585
0
        return 1;
1586
0
    }
1587
0
    else {
1588
0
        return 0;
1589
0
    }
1590
0
}
1591
1592
const char *
1593
_Py_SourceAsString(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
1594
110k
{
1595
110k
    const char *str;
1596
110k
    Py_ssize_t size;
1597
110k
    Py_buffer view;
1598
1599
110k
    *cmd_copy = NULL;
1600
110k
    if (PyUnicode_Check(cmd)) {
1601
92.6k
        cf->cf_flags |= PyCF_IGNORE_COOKIE;
1602
92.6k
        str = PyUnicode_AsUTF8AndSize(cmd, &size);
1603
92.6k
        if (str == NULL)
1604
0
            return NULL;
1605
92.6k
    }
1606
18.2k
    else if (PyBytes_Check(cmd)) {
1607
18.2k
        str = PyBytes_AS_STRING(cmd);
1608
18.2k
        size = PyBytes_GET_SIZE(cmd);
1609
18.2k
    }
1610
0
    else if (PyByteArray_Check(cmd)) {
1611
0
        str = PyByteArray_AS_STRING(cmd);
1612
0
        size = PyByteArray_GET_SIZE(cmd);
1613
0
    }
1614
0
    else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
1615
        /* Copy to NUL-terminated buffer. */
1616
0
        *cmd_copy = PyBytes_FromStringAndSize(
1617
0
            (const char *)view.buf, view.len);
1618
0
        PyBuffer_Release(&view);
1619
0
        if (*cmd_copy == NULL) {
1620
0
            return NULL;
1621
0
        }
1622
0
        str = PyBytes_AS_STRING(*cmd_copy);
1623
0
        size = PyBytes_GET_SIZE(*cmd_copy);
1624
0
    }
1625
0
    else {
1626
0
        PyErr_Format(PyExc_TypeError,
1627
0
            "%s() arg 1 must be a %s object",
1628
0
            funcname, what);
1629
0
        return NULL;
1630
0
    }
1631
1632
110k
    if (strlen(str) != (size_t)size) {
1633
5
        PyErr_SetString(PyExc_SyntaxError,
1634
5
            "source code string cannot contain null bytes");
1635
5
        Py_CLEAR(*cmd_copy);
1636
5
        return NULL;
1637
5
    }
1638
110k
    return str;
1639
110k
}
1640
1641
#if defined(USE_STACKCHECK)
1642
1643
/* Stack checking */
1644
1645
/*
1646
 * Return non-zero when we run out of memory on the stack; zero otherwise.
1647
 */
1648
int
1649
PyOS_CheckStack(void)
1650
{
1651
    PyThreadState *tstate = _PyThreadState_GET();
1652
    return _Py_ReachedRecursionLimit(tstate);
1653
}
1654
1655
#endif /* USE_STACKCHECK */
1656
1657
/* Deprecated C API functions still provided for binary compatibility */
1658
1659
#undef PyRun_AnyFile
1660
PyAPI_FUNC(int)
1661
PyRun_AnyFile(FILE *fp, const char *name)
1662
0
{
1663
0
    return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1664
0
}
1665
1666
#undef PyRun_AnyFileEx
1667
PyAPI_FUNC(int)
1668
PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1669
0
{
1670
0
    return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1671
0
}
1672
1673
#undef PyRun_AnyFileFlags
1674
PyAPI_FUNC(int)
1675
PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1676
0
{
1677
0
    return PyRun_AnyFileExFlags(fp, name, 0, flags);
1678
0
}
1679
1680
#undef PyRun_File
1681
PyAPI_FUNC(PyObject *)
1682
PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1683
0
{
1684
0
    return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1685
0
}
1686
1687
#undef PyRun_FileEx
1688
PyAPI_FUNC(PyObject *)
1689
PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1690
0
{
1691
0
    return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1692
0
}
1693
1694
#undef PyRun_FileFlags
1695
PyAPI_FUNC(PyObject *)
1696
PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1697
                PyCompilerFlags *flags)
1698
0
{
1699
0
    return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1700
0
}
1701
1702
#undef PyRun_SimpleFile
1703
PyAPI_FUNC(int)
1704
PyRun_SimpleFile(FILE *f, const char *p)
1705
0
{
1706
0
    return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1707
0
}
1708
1709
#undef PyRun_SimpleFileEx
1710
PyAPI_FUNC(int)
1711
PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1712
0
{
1713
0
    return PyRun_SimpleFileExFlags(f, p, c, NULL);
1714
0
}
1715
1716
1717
#undef PyRun_String
1718
PyAPI_FUNC(PyObject *)
1719
PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1720
0
{
1721
0
    return PyRun_StringFlags(str, s, g, l, NULL);
1722
0
}
1723
1724
#undef PyRun_SimpleString
1725
PyAPI_FUNC(int)
1726
PyRun_SimpleString(const char *s)
1727
0
{
1728
0
    return PyRun_SimpleStringFlags(s, NULL);
1729
0
}
1730
1731
#undef Py_CompileString
1732
PyAPI_FUNC(PyObject *)
1733
Py_CompileString(const char *str, const char *p, int s)
1734
0
{
1735
0
    return Py_CompileStringExFlags(str, p, s, NULL, -1);
1736
0
}
1737
1738
#undef Py_CompileStringFlags
1739
PyAPI_FUNC(PyObject *)
1740
Py_CompileStringFlags(const char *str, const char *p, int s,
1741
                      PyCompilerFlags *flags)
1742
0
{
1743
0
    return Py_CompileStringExFlags(str, p, s, flags, -1);
1744
0
}
1745
1746
#undef PyRun_InteractiveOne
1747
PyAPI_FUNC(int)
1748
PyRun_InteractiveOne(FILE *f, const char *p)
1749
0
{
1750
0
    return PyRun_InteractiveOneFlags(f, p, NULL);
1751
0
}
1752
1753
#undef PyRun_InteractiveLoop
1754
PyAPI_FUNC(int)
1755
PyRun_InteractiveLoop(FILE *f, const char *p)
1756
0
{
1757
    return PyRun_InteractiveLoopFlags(f, p, NULL);
1758
0
}