Coverage Report

Created: 2026-04-12 06:54

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
72
_PyRun_SimpleStringFlagsWithName(const char *command, const char* name, PyCompilerFlags *flags) {
557
72
    PyObject *main_module = PyImport_AddModuleRef("__main__");
558
72
    if (main_module == NULL) {
559
0
        return -1;
560
0
    }
561
72
    PyObject *dict = PyModule_GetDict(main_module);  // borrowed ref
562
563
72
    PyObject *res = NULL;
564
72
    if (name == NULL) {
565
72
        res = PyRun_StringFlags(command, Py_file_input, dict, dict, flags);
566
72
    } else {
567
0
        PyObject* the_name = PyUnicode_FromString(name);
568
0
        if (!the_name) {
569
0
            PyErr_Print();
570
0
            Py_DECREF(main_module);
571
0
            return -1;
572
0
        }
573
0
        res = _PyRun_StringFlagsWithName(command, the_name, Py_file_input, dict, dict, flags, 0);
574
0
        Py_DECREF(the_name);
575
0
    }
576
72
    Py_DECREF(main_module);
577
72
    if (res == NULL) {
578
0
        PyErr_Print();
579
0
        return -1;
580
0
    }
581
582
72
    Py_DECREF(res);
583
72
    return 0;
584
72
}
585
586
int
587
PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
588
72
{
589
72
    return _PyRun_SimpleStringFlagsWithName(command, NULL, flags);
590
72
}
591
592
static int
593
parse_exit_code(PyObject *code, int *exitcode_p)
594
0
{
595
0
    if (PyLong_Check(code)) {
596
        // gh-125842: Use a long long to avoid an overflow error when `long`
597
        // is 32-bit. We still truncate the result to an int.
598
0
        int exitcode = (int)PyLong_AsLongLong(code);
599
0
        if (exitcode == -1 && PyErr_Occurred()) {
600
            // On overflow or other error, clear the exception and use -1
601
            // as the exit code to match historical Python behavior.
602
0
            PyErr_Clear();
603
0
            *exitcode_p = -1;
604
0
            return 1;
605
0
        }
606
0
        *exitcode_p = exitcode;
607
0
        return 1;
608
0
    }
609
0
    else if (code == Py_None) {
610
0
        *exitcode_p = 0;
611
0
        return 1;
612
0
    }
613
0
    return 0;
614
0
}
615
616
int
617
_Py_HandleSystemExitAndKeyboardInterrupt(int *exitcode_p)
618
0
{
619
0
    if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
620
0
        _Py_atomic_store_int(&_PyRuntime.signals.unhandled_keyboard_interrupt, 1);
621
0
        return 0;
622
0
    }
623
624
0
    int inspect = _Py_GetConfig()->inspect;
625
0
    if (inspect) {
626
        /* Don't exit if -i flag was given. This flag is set to 0
627
         * when entering interactive mode for inspecting. */
628
0
        return 0;
629
0
    }
630
631
0
    if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
632
0
        return 0;
633
0
    }
634
635
0
    fflush(stdout);
636
637
0
    PyObject *exc = PyErr_GetRaisedException();
638
0
    assert(exc != NULL && PyExceptionInstance_Check(exc));
639
640
0
    PyObject *code = PyObject_GetAttr(exc, &_Py_ID(code));
641
0
    if (code == NULL) {
642
        // If the exception has no 'code' attribute, print the exception below
643
0
        PyErr_Clear();
644
0
    }
645
0
    else if (parse_exit_code(code, exitcode_p)) {
646
0
        Py_DECREF(code);
647
0
        Py_CLEAR(exc);
648
0
        return 1;
649
0
    }
650
0
    else {
651
        // If code is not an int or None, print it below
652
0
        Py_SETREF(exc, code);
653
0
    }
654
655
0
    PyObject *sys_stderr;
656
0
    if (PySys_GetOptionalAttr(&_Py_ID(stderr), &sys_stderr) < 0) {
657
0
        PyErr_Clear();
658
0
    }
659
0
    else if (sys_stderr != NULL && sys_stderr != Py_None) {
660
0
        if (PyFile_WriteObject(exc, sys_stderr, Py_PRINT_RAW) < 0) {
661
0
            PyErr_Clear();
662
0
        }
663
0
    }
664
0
    else {
665
0
        if (PyObject_Print(exc, stderr, Py_PRINT_RAW) < 0) {
666
0
            PyErr_Clear();
667
0
        }
668
0
        fflush(stderr);
669
0
    }
670
0
    PySys_WriteStderr("\n");
671
0
    Py_XDECREF(sys_stderr);
672
0
    Py_CLEAR(exc);
673
0
    *exitcode_p = 1;
674
0
    return 1;
675
0
}
676
677
678
static void
679
handle_system_exit(void)
680
0
{
681
0
    int exitcode;
682
0
    if (_Py_HandleSystemExitAndKeyboardInterrupt(&exitcode)) {
683
0
        Py_Exit(exitcode);
684
0
    }
685
0
}
686
687
688
static void
689
_PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
690
0
{
691
0
    PyObject *typ = NULL, *tb = NULL, *hook = NULL;
692
0
    handle_system_exit();
693
694
0
    PyObject *exc = _PyErr_GetRaisedException(tstate);
695
0
    if (exc == NULL) {
696
0
        goto done;
697
0
    }
698
0
    assert(PyExceptionInstance_Check(exc));
699
0
    typ = Py_NewRef(Py_TYPE(exc));
700
0
    tb = PyException_GetTraceback(exc);
701
0
    if (tb == NULL) {
702
0
        tb = Py_NewRef(Py_None);
703
0
    }
704
705
0
    if (set_sys_last_vars) {
706
0
        if (_PySys_SetAttr(&_Py_ID(last_exc), exc) < 0) {
707
0
            _PyErr_Clear(tstate);
708
0
        }
709
        /* Legacy version: */
710
0
        if (_PySys_SetAttr(&_Py_ID(last_type), typ) < 0) {
711
0
            _PyErr_Clear(tstate);
712
0
        }
713
0
        if (_PySys_SetAttr(&_Py_ID(last_value), exc) < 0) {
714
0
            _PyErr_Clear(tstate);
715
0
        }
716
0
        if (_PySys_SetAttr(&_Py_ID(last_traceback), tb) < 0) {
717
0
            _PyErr_Clear(tstate);
718
0
        }
719
0
    }
720
0
    if (PySys_GetOptionalAttr(&_Py_ID(excepthook), &hook) < 0) {
721
0
        PyErr_Clear();
722
0
    }
723
0
    if (_PySys_Audit(tstate, "sys.excepthook", "OOOO", hook ? hook : Py_None,
724
0
                     typ, exc, tb) < 0) {
725
0
        if (PyErr_ExceptionMatches(PyExc_RuntimeError)) {
726
0
            PyErr_Clear();
727
0
            goto done;
728
0
        }
729
0
        PyErr_FormatUnraisable("Exception ignored in audit hook");
730
0
    }
731
0
    if (hook) {
732
0
        PyObject* args[3] = {typ, exc, tb};
733
0
        PyObject *result = PyObject_Vectorcall(hook, args, 3, NULL);
734
0
        if (result == NULL) {
735
0
            handle_system_exit();
736
737
0
            PyObject *exc2 = _PyErr_GetRaisedException(tstate);
738
0
            assert(exc2 && PyExceptionInstance_Check(exc2));
739
0
            fflush(stdout);
740
0
            PySys_WriteStderr("Error in sys.excepthook:\n");
741
0
            PyErr_DisplayException(exc2);
742
0
            PySys_WriteStderr("\nOriginal exception was:\n");
743
0
            PyErr_DisplayException(exc);
744
0
            Py_DECREF(exc2);
745
0
        }
746
0
        else {
747
0
            Py_DECREF(result);
748
0
        }
749
0
    }
750
0
    else {
751
0
        PySys_WriteStderr("sys.excepthook is missing\n");
752
0
        PyErr_DisplayException(exc);
753
0
    }
754
755
0
done:
756
0
    Py_XDECREF(hook);
757
0
    Py_XDECREF(typ);
758
0
    Py_XDECREF(exc);
759
0
    Py_XDECREF(tb);
760
0
}
761
762
void
763
_PyErr_Print(PyThreadState *tstate)
764
0
{
765
0
    _PyErr_PrintEx(tstate, 1);
766
0
}
767
768
void
769
PyErr_PrintEx(int set_sys_last_vars)
770
0
{
771
0
    PyThreadState *tstate = _PyThreadState_GET();
772
0
    _PyErr_PrintEx(tstate, set_sys_last_vars);
773
0
}
774
775
void
776
PyErr_Print(void)
777
0
{
778
0
    PyErr_PrintEx(1);
779
0
}
780
781
struct exception_print_context
782
{
783
    PyObject *file;
784
    PyObject *seen;            // Prevent cycles in recursion
785
};
786
787
static int
788
print_exception_invalid_type(struct exception_print_context *ctx,
789
                             PyObject *value)
790
0
{
791
0
    PyObject *f = ctx->file;
792
0
    const char *const msg = "TypeError: print_exception(): Exception expected "
793
0
                            "for value, ";
794
0
    if (PyFile_WriteString(msg, f) < 0) {
795
0
        return -1;
796
0
    }
797
0
    if (PyFile_WriteString(Py_TYPE(value)->tp_name, f) < 0) {
798
0
        return -1;
799
0
    }
800
0
    if (PyFile_WriteString(" found\n", f) < 0) {
801
0
        return -1;
802
0
    }
803
0
    return 0;
804
0
}
805
806
static int
807
print_exception_traceback(struct exception_print_context *ctx, PyObject *value)
808
0
{
809
0
    PyObject *f = ctx->file;
810
0
    int err = 0;
811
812
0
    PyObject *tb = PyException_GetTraceback(value);
813
0
    if (tb && tb != Py_None) {
814
0
        const char *header = EXCEPTION_TB_HEADER;
815
0
        err = _PyTraceBack_Print(tb, header, f);
816
0
    }
817
0
    Py_XDECREF(tb);
818
0
    return err;
819
0
}
820
821
static int
822
print_exception_file_and_line(struct exception_print_context *ctx,
823
                              PyObject **value_p)
824
0
{
825
0
    PyObject *f = ctx->file;
826
827
0
    PyObject *tmp;
828
0
    int res = PyObject_GetOptionalAttr(*value_p, &_Py_ID(print_file_and_line), &tmp);
829
0
    if (res <= 0) {
830
0
        if (res < 0) {
831
0
            PyErr_Clear();
832
0
        }
833
0
        return 0;
834
0
    }
835
0
    Py_DECREF(tmp);
836
837
0
    PyObject *filename = NULL;
838
0
    Py_ssize_t lineno = 0;
839
0
    PyObject* v = PyObject_GetAttr(*value_p, &_Py_ID(filename));
840
0
    if (!v) {
841
0
        return -1;
842
0
    }
843
0
    if (v == Py_None) {
844
0
        Py_DECREF(v);
845
0
        _Py_DECLARE_STR(anon_string, "<string>");
846
0
        filename = Py_NewRef(&_Py_STR(anon_string));
847
0
    }
848
0
    else {
849
0
        filename = v;
850
0
    }
851
852
0
    PyObject *line = PyUnicode_FromFormat("  File \"%S\", line %zd\n",
853
0
                                          filename, lineno);
854
0
    Py_DECREF(filename);
855
0
    if (line == NULL) {
856
0
        goto error;
857
0
    }
858
0
    if (PyFile_WriteObject(line, f, Py_PRINT_RAW) < 0) {
859
0
        goto error;
860
0
    }
861
0
    Py_CLEAR(line);
862
863
0
    assert(!PyErr_Occurred());
864
0
    return 0;
865
866
0
error:
867
0
    Py_XDECREF(line);
868
0
    return -1;
869
0
}
870
871
/* Prints the message line: module.qualname[: str(exc)] */
872
static int
873
print_exception_message(struct exception_print_context *ctx, PyObject *type,
874
                        PyObject *value)
875
0
{
876
0
    PyObject *f = ctx->file;
877
878
0
    if (PyErr_GivenExceptionMatches(value, PyExc_MemoryError)) {
879
        // The Python APIs in this function require allocating memory
880
        // for various objects. If we're out of memory, we can't do that,
881
0
        return -1;
882
0
    }
883
884
0
    assert(PyExceptionClass_Check(type));
885
886
0
    PyObject *modulename = PyObject_GetAttr(type, &_Py_ID(__module__));
887
0
    if (modulename == NULL || !PyUnicode_Check(modulename)) {
888
0
        Py_XDECREF(modulename);
889
0
        PyErr_Clear();
890
0
        if (PyFile_WriteString("<unknown>.", f) < 0) {
891
0
            return -1;
892
0
        }
893
0
    }
894
0
    else {
895
0
        if (!_PyUnicode_Equal(modulename, &_Py_ID(builtins)) &&
896
0
            !_PyUnicode_Equal(modulename, &_Py_ID(__main__)))
897
0
        {
898
0
            int res = PyFile_WriteObject(modulename, f, Py_PRINT_RAW);
899
0
            Py_DECREF(modulename);
900
0
            if (res < 0) {
901
0
                return -1;
902
0
            }
903
0
            if (PyFile_WriteString(".", f) < 0) {
904
0
                return -1;
905
0
            }
906
0
        }
907
0
        else {
908
0
            Py_DECREF(modulename);
909
0
        }
910
0
    }
911
912
0
    PyObject *qualname = PyType_GetQualName((PyTypeObject *)type);
913
0
    if (qualname == NULL || !PyUnicode_Check(qualname)) {
914
0
        Py_XDECREF(qualname);
915
0
        PyErr_Clear();
916
0
        if (PyFile_WriteString("<unknown>", f) < 0) {
917
0
            return -1;
918
0
        }
919
0
    }
920
0
    else {
921
0
        int res = PyFile_WriteObject(qualname, f, Py_PRINT_RAW);
922
0
        Py_DECREF(qualname);
923
0
        if (res < 0) {
924
0
            return -1;
925
0
        }
926
0
    }
927
928
0
    if (Py_IsNone(value)) {
929
0
        return 0;
930
0
    }
931
932
0
    PyObject *s = PyObject_Str(value);
933
0
    if (s == NULL) {
934
0
        PyErr_Clear();
935
0
        if (PyFile_WriteString(": <exception str() failed>", f) < 0) {
936
0
            return -1;
937
0
        }
938
0
    }
939
0
    else {
940
        /* only print colon if the str() of the
941
           object is not the empty string
942
        */
943
0
        if (!PyUnicode_Check(s) || PyUnicode_GetLength(s) != 0) {
944
0
            if (PyFile_WriteString(": ", f) < 0) {
945
0
                Py_DECREF(s);
946
0
                return -1;
947
0
            }
948
0
        }
949
0
        int res = PyFile_WriteObject(s, f, Py_PRINT_RAW);
950
0
        Py_DECREF(s);
951
0
        if (res < 0) {
952
0
            return -1;
953
0
        }
954
0
    }
955
956
0
    return 0;
957
0
}
958
959
static int
960
print_exception(struct exception_print_context *ctx, PyObject *value)
961
0
{
962
0
    PyObject *f = ctx->file;
963
964
0
    if (!PyExceptionInstance_Check(value)) {
965
0
        return print_exception_invalid_type(ctx, value);
966
0
    }
967
968
0
    Py_INCREF(value);
969
0
    fflush(stdout);
970
971
0
    if (print_exception_traceback(ctx, value) < 0) {
972
0
        goto error;
973
0
    }
974
975
    /* grab the type and notes now because value can change below */
976
0
    PyObject *type = (PyObject *) Py_TYPE(value);
977
978
0
    if (print_exception_file_and_line(ctx, &value) < 0) {
979
0
        goto error;
980
0
    }
981
0
    if (print_exception_message(ctx, type, value) < 0) {
982
0
        goto error;
983
0
    }
984
0
    if (PyFile_WriteString("\n", f) < 0) {
985
0
        goto error;
986
0
    }
987
0
    Py_DECREF(value);
988
0
    assert(!PyErr_Occurred());
989
0
    return 0;
990
0
error:
991
0
    Py_DECREF(value);
992
0
    return -1;
993
0
}
994
995
static const char cause_message[] =
996
    "The above exception was the direct cause "
997
    "of the following exception:\n";
998
999
static const char context_message[] =
1000
    "During handling of the above exception, "
1001
    "another exception occurred:\n";
1002
1003
static int
1004
print_exception_recursive(struct exception_print_context*, PyObject*);
1005
1006
static int
1007
print_chained(struct exception_print_context* ctx, PyObject *value,
1008
              const char * message, const char *tag)
1009
0
{
1010
0
    PyObject *f = ctx->file;
1011
0
    if (_Py_EnterRecursiveCall(" in print_chained")) {
1012
0
        return -1;
1013
0
    }
1014
0
    int res = print_exception_recursive(ctx, value);
1015
0
    _Py_LeaveRecursiveCall();
1016
0
    if (res < 0) {
1017
0
        return -1;
1018
0
    }
1019
1020
0
    if (PyFile_WriteString("\n", f) < 0) {
1021
0
        return -1;
1022
0
    }
1023
0
    if (PyFile_WriteString(message, f) < 0) {
1024
0
        return -1;
1025
0
    }
1026
0
    if (PyFile_WriteString("\n", f) < 0) {
1027
0
        return -1;
1028
0
    }
1029
0
    return 0;
1030
0
}
1031
1032
/* Return true if value is in seen or there was a lookup error.
1033
 * Return false if lookup succeeded and the item was not found.
1034
 * We suppress errors because this makes us err on the side of
1035
 * under-printing which is better than over-printing irregular
1036
 * exceptions (e.g., unhashable ones).
1037
 */
1038
static bool
1039
print_exception_seen_lookup(struct exception_print_context *ctx,
1040
                            PyObject *value)
1041
0
{
1042
0
    PyObject *check_id = PyLong_FromVoidPtr(value);
1043
0
    if (check_id == NULL) {
1044
0
        PyErr_Clear();
1045
0
        return true;
1046
0
    }
1047
1048
0
    int in_seen = PySet_Contains(ctx->seen, check_id);
1049
0
    Py_DECREF(check_id);
1050
0
    if (in_seen == -1) {
1051
0
        PyErr_Clear();
1052
0
        return true;
1053
0
    }
1054
1055
0
    if (in_seen == 1) {
1056
        /* value is in seen */
1057
0
        return true;
1058
0
    }
1059
0
    return false;
1060
0
}
1061
1062
static int
1063
print_exception_cause_and_context(struct exception_print_context *ctx,
1064
                                  PyObject *value)
1065
0
{
1066
0
    PyObject *value_id = PyLong_FromVoidPtr(value);
1067
0
    if (value_id == NULL || PySet_Add(ctx->seen, value_id) == -1) {
1068
0
        PyErr_Clear();
1069
0
        Py_XDECREF(value_id);
1070
0
        return 0;
1071
0
    }
1072
0
    Py_DECREF(value_id);
1073
1074
0
    if (!PyExceptionInstance_Check(value)) {
1075
0
        return 0;
1076
0
    }
1077
1078
0
    PyObject *cause = PyException_GetCause(value);
1079
0
    if (cause) {
1080
0
        int err = 0;
1081
0
        if (!print_exception_seen_lookup(ctx, cause)) {
1082
0
            err = print_chained(ctx, cause, cause_message, "cause");
1083
0
        }
1084
0
        Py_DECREF(cause);
1085
0
        return err;
1086
0
    }
1087
0
    if (((PyBaseExceptionObject *)value)->suppress_context) {
1088
0
        return 0;
1089
0
    }
1090
0
    PyObject *context = PyException_GetContext(value);
1091
0
    if (context) {
1092
0
        int err = 0;
1093
0
        if (!print_exception_seen_lookup(ctx, context)) {
1094
0
            err = print_chained(ctx, context, context_message, "context");
1095
0
        }
1096
0
        Py_DECREF(context);
1097
0
        return err;
1098
0
    }
1099
0
    return 0;
1100
0
}
1101
1102
static int
1103
print_exception_recursive(struct exception_print_context *ctx, PyObject *value)
1104
0
{
1105
0
    if (_Py_EnterRecursiveCall(" in print_exception_recursive")) {
1106
0
        return -1;
1107
0
    }
1108
0
    if (ctx->seen != NULL) {
1109
        /* Exception chaining */
1110
0
        if (print_exception_cause_and_context(ctx, value) < 0) {
1111
0
            goto error;
1112
0
        }
1113
0
    }
1114
0
    if (print_exception(ctx, value) < 0) {
1115
0
        goto error;
1116
0
    }
1117
0
    assert(!PyErr_Occurred());
1118
1119
0
    _Py_LeaveRecursiveCall();
1120
0
    return 0;
1121
0
error:
1122
0
    _Py_LeaveRecursiveCall();
1123
0
    return -1;
1124
0
}
1125
1126
void
1127
_PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
1128
0
{
1129
0
    assert(value != NULL);
1130
0
    assert(file != NULL && file != Py_None);
1131
0
    if (PyExceptionInstance_Check(value)
1132
0
        && tb != NULL && PyTraceBack_Check(tb)) {
1133
        /* Put the traceback on the exception, otherwise it won't get
1134
           displayed.  See issue #18776. */
1135
0
        PyObject *cur_tb = PyException_GetTraceback(value);
1136
0
        if (cur_tb == NULL) {
1137
0
            PyException_SetTraceback(value, tb);
1138
0
        }
1139
0
        else {
1140
0
            Py_DECREF(cur_tb);
1141
0
        }
1142
0
    }
1143
1144
    // Try first with the stdlib traceback module
1145
0
    PyObject *print_exception_fn = PyImport_ImportModuleAttrString(
1146
0
        "traceback",
1147
0
        "_print_exception_bltin");
1148
0
    if (print_exception_fn == NULL || !PyCallable_Check(print_exception_fn)) {
1149
0
        Py_XDECREF(print_exception_fn);
1150
0
        goto fallback;
1151
0
    }
1152
1153
0
    PyObject* result = PyObject_CallOneArg(print_exception_fn, value);
1154
1155
0
    Py_XDECREF(print_exception_fn);
1156
0
    if (result) {
1157
0
        Py_DECREF(result);
1158
0
        return;
1159
0
    }
1160
0
fallback:
1161
#ifdef Py_DEBUG
1162
     if (PyErr_Occurred()) {
1163
         PyErr_FormatUnraisable(
1164
             "Exception ignored in the internal traceback machinery");
1165
     }
1166
#endif
1167
0
    PyErr_Clear();
1168
0
    struct exception_print_context ctx;
1169
0
    ctx.file = file;
1170
1171
    /* We choose to ignore seen being possibly NULL, and report
1172
       at least the main exception (it could be a MemoryError).
1173
    */
1174
0
    ctx.seen = PySet_New(NULL);
1175
0
    if (ctx.seen == NULL) {
1176
0
        PyErr_Clear();
1177
0
    }
1178
0
    if (print_exception_recursive(&ctx, value) < 0) {
1179
0
        PyErr_Clear();
1180
0
        PyObject_Dump(value);
1181
0
        fprintf(stderr, "lost sys.stderr\n");
1182
0
    }
1183
0
    Py_XDECREF(ctx.seen);
1184
1185
    /* Call file.flush() */
1186
0
    if (_PyFile_Flush(file) < 0) {
1187
        /* Silently ignore file.flush() error */
1188
0
        PyErr_Clear();
1189
0
    }
1190
0
}
1191
1192
void
1193
PyErr_Display(PyObject *unused, PyObject *value, PyObject *tb)
1194
0
{
1195
0
    PyObject *file;
1196
0
    if (PySys_GetOptionalAttr(&_Py_ID(stderr), &file) < 0) {
1197
0
        PyObject *exc = PyErr_GetRaisedException();
1198
0
        PyObject_Dump(value);
1199
0
        fprintf(stderr, "lost sys.stderr\n");
1200
0
        PyObject_Dump(exc);
1201
0
        Py_DECREF(exc);
1202
0
        return;
1203
0
    }
1204
0
    if (file == NULL) {
1205
0
        PyObject_Dump(value);
1206
0
        fprintf(stderr, "lost sys.stderr\n");
1207
0
        return;
1208
0
    }
1209
0
    if (file == Py_None) {
1210
0
        Py_DECREF(file);
1211
0
        return;
1212
0
    }
1213
0
    _PyErr_Display(file, NULL, value, tb);
1214
0
    Py_DECREF(file);
1215
0
}
1216
1217
void _PyErr_DisplayException(PyObject *file, PyObject *exc)
1218
0
{
1219
0
    _PyErr_Display(file, NULL, exc, NULL);
1220
0
}
1221
1222
void PyErr_DisplayException(PyObject *exc)
1223
0
{
1224
0
    PyErr_Display(NULL, exc, NULL);
1225
0
}
1226
1227
static PyObject *
1228
_PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
1229
                           PyObject *globals, PyObject *locals, PyCompilerFlags *flags,
1230
                           int generate_new_source)
1231
657
{
1232
657
    PyObject *ret = NULL;
1233
657
    mod_ty mod;
1234
657
    PyArena *arena;
1235
1236
657
    arena = _PyArena_New();
1237
657
    if (arena == NULL)
1238
0
        return NULL;
1239
1240
657
    PyObject* source = NULL;
1241
657
    _Py_DECLARE_STR(anon_string, "<string>");
1242
1243
657
    if (name) {
1244
0
        source = PyUnicode_FromString(str);
1245
0
        if (!source) {
1246
0
            PyErr_Clear();
1247
0
        }
1248
657
    } else {
1249
657
        name = &_Py_STR(anon_string);
1250
657
    }
1251
657
    PyObject *module = NULL;
1252
657
    if (globals && PyDict_GetItemStringRef(globals, "__name__", &module) < 0) {
1253
0
        goto done;
1254
0
    }
1255
1256
657
    mod = _PyParser_ASTFromString(str, name, start, flags, arena, module);
1257
657
    Py_XDECREF(module);
1258
1259
657
    if (mod != NULL) {
1260
657
        ret = run_mod(mod, name, globals, locals, flags, arena, source, generate_new_source);
1261
657
    }
1262
1263
657
done:
1264
657
    Py_XDECREF(source);
1265
657
    _PyArena_Free(arena);
1266
657
    return ret;
1267
657
}
1268
1269
1270
PyObject *
1271
PyRun_StringFlags(const char *str, int start, PyObject *globals,
1272
657
                     PyObject *locals, PyCompilerFlags *flags) {
1273
1274
657
    return _PyRun_StringFlagsWithName(str, NULL, start, globals, locals, flags, 0);
1275
657
}
1276
1277
static PyObject *
1278
pyrun_file(FILE *fp, PyObject *filename, int start, PyObject *globals,
1279
           PyObject *locals, int closeit, PyCompilerFlags *flags)
1280
0
{
1281
0
    PyArena *arena = _PyArena_New();
1282
0
    if (arena == NULL) {
1283
0
        return NULL;
1284
0
    }
1285
1286
0
    mod_ty mod;
1287
0
    mod = _PyParser_ASTFromFile(fp, filename, NULL, start, NULL, NULL,
1288
0
                                flags, NULL, arena);
1289
1290
0
    if (closeit) {
1291
0
        fclose(fp);
1292
0
    }
1293
1294
0
    PyObject *ret;
1295
0
    if (mod != NULL) {
1296
0
        ret = run_mod(mod, filename, globals, locals, flags, arena, NULL, 0);
1297
0
    }
1298
0
    else {
1299
0
        ret = NULL;
1300
0
    }
1301
0
    _PyArena_Free(arena);
1302
1303
0
    return ret;
1304
0
}
1305
1306
1307
PyObject *
1308
PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1309
                  PyObject *locals, int closeit, PyCompilerFlags *flags)
1310
0
{
1311
0
    PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
1312
0
    if (filename_obj == NULL) {
1313
0
        return NULL;
1314
0
    }
1315
1316
0
    PyObject *res = pyrun_file(fp, filename_obj, start, globals,
1317
0
                               locals, closeit, flags);
1318
0
    Py_DECREF(filename_obj);
1319
0
    return res;
1320
1321
0
}
1322
1323
static void
1324
flush_io_stream(PyThreadState *tstate, PyObject *name)
1325
0
{
1326
0
    PyObject *f;
1327
0
    if (PySys_GetOptionalAttr(name, &f) < 0) {
1328
0
        PyErr_Clear();
1329
0
    }
1330
0
    if (f != NULL) {
1331
0
        if (_PyFile_Flush(f) < 0) {
1332
0
            PyErr_Clear();
1333
0
        }
1334
0
        Py_DECREF(f);
1335
0
    }
1336
0
}
1337
1338
static void
1339
flush_io(void)
1340
0
{
1341
0
    PyThreadState *tstate = _PyThreadState_GET();
1342
0
    PyObject *exc = _PyErr_GetRaisedException(tstate);
1343
0
    flush_io_stream(tstate, &_Py_ID(stderr));
1344
0
    flush_io_stream(tstate, &_Py_ID(stdout));
1345
0
    _PyErr_SetRaisedException(tstate, exc);
1346
0
}
1347
1348
static PyObject *
1349
run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, PyObject *locals)
1350
657
{
1351
    /* Set globals['__builtins__'] if it doesn't exist */
1352
657
    if (!globals || !PyAnyDict_Check(globals)) {
1353
0
        PyErr_SetString(PyExc_SystemError,
1354
0
                        "globals must be a real dict or a real frozendict");
1355
0
        return NULL;
1356
0
    }
1357
657
    int has_builtins = PyDict_ContainsString(globals, "__builtins__");
1358
657
    if (has_builtins < 0) {
1359
0
        return NULL;
1360
0
    }
1361
657
    if (!has_builtins) {
1362
36
        if (PyDict_SetItemString(globals, "__builtins__",
1363
36
                                 tstate->interp->builtins) < 0)
1364
0
        {
1365
0
            return NULL;
1366
0
        }
1367
36
    }
1368
1369
657
    return PyEval_EvalCode((PyObject*)co, globals, locals);
1370
657
}
1371
1372
static PyObject *
1373
get_interactive_filename(PyObject *filename, Py_ssize_t count)
1374
0
{
1375
0
    PyObject *result;
1376
0
    Py_ssize_t len = PyUnicode_GET_LENGTH(filename);
1377
1378
0
    if (len >= 2
1379
0
            && PyUnicode_ReadChar(filename, 0) == '<'
1380
0
            && PyUnicode_ReadChar(filename, len - 1) == '>') {
1381
0
        PyObject *middle = PyUnicode_Substring(filename, 1, len-1);
1382
0
        if (middle == NULL) {
1383
0
            return NULL;
1384
0
        }
1385
0
        result = PyUnicode_FromFormat("<%U-%zd>", middle, count);
1386
0
        Py_DECREF(middle);
1387
0
    } else {
1388
0
        result = PyUnicode_FromFormat(
1389
0
            "%U-%zd", filename, count);
1390
0
    }
1391
0
    return result;
1392
1393
0
}
1394
1395
static PyObject *
1396
run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
1397
            PyCompilerFlags *flags, PyArena *arena, PyObject* interactive_src,
1398
            int generate_new_source)
1399
657
{
1400
657
    PyThreadState *tstate = _PyThreadState_GET();
1401
657
    PyObject* interactive_filename = filename;
1402
657
    if (interactive_src) {
1403
0
        PyInterpreterState *interp = tstate->interp;
1404
0
        if (generate_new_source) {
1405
0
            interactive_filename = get_interactive_filename(
1406
0
                filename, interp->_interactive_src_count++);
1407
0
        } else {
1408
0
            Py_INCREF(interactive_filename);
1409
0
        }
1410
0
        if (interactive_filename == NULL) {
1411
0
            return NULL;
1412
0
        }
1413
0
    }
1414
657
    PyObject *module = NULL;
1415
657
    if (globals && PyDict_GetItemStringRef(globals, "__name__", &module) < 0) {
1416
0
        if (interactive_src) {
1417
0
            Py_DECREF(interactive_filename);
1418
0
        }
1419
0
        return NULL;
1420
0
    }
1421
1422
657
    PyCodeObject *co = _PyAST_Compile(mod, interactive_filename, flags, -1,
1423
657
                                      arena, module);
1424
657
    Py_XDECREF(module);
1425
657
    if (co == NULL) {
1426
0
        if (interactive_src) {
1427
0
            Py_DECREF(interactive_filename);
1428
0
        }
1429
0
        return NULL;
1430
0
    }
1431
1432
657
    if (interactive_src) {
1433
0
        PyObject *print_tb_func = PyImport_ImportModuleAttrString(
1434
0
            "linecache",
1435
0
            "_register_code");
1436
0
        if (print_tb_func == NULL) {
1437
0
            Py_DECREF(co);
1438
0
            Py_DECREF(interactive_filename);
1439
0
            return NULL;
1440
0
        }
1441
1442
0
        if (!PyCallable_Check(print_tb_func)) {
1443
0
            Py_DECREF(co);
1444
0
            Py_DECREF(interactive_filename);
1445
0
            Py_DECREF(print_tb_func);
1446
0
            PyErr_SetString(PyExc_ValueError, "linecache._register_code is not callable");
1447
0
            return NULL;
1448
0
        }
1449
1450
0
        PyObject* result = PyObject_CallFunction(
1451
0
            print_tb_func, "OOO",
1452
0
            co,
1453
0
            interactive_src,
1454
0
            filename
1455
0
        );
1456
1457
0
        Py_DECREF(interactive_filename);
1458
1459
0
        Py_XDECREF(print_tb_func);
1460
0
        Py_XDECREF(result);
1461
0
        if (!result) {
1462
0
            Py_DECREF(co);
1463
0
            return NULL;
1464
0
        }
1465
0
    }
1466
1467
657
    if (_PySys_Audit(tstate, "exec", "O", co) < 0) {
1468
0
        Py_DECREF(co);
1469
0
        return NULL;
1470
0
    }
1471
1472
657
    PyObject *v = run_eval_code_obj(tstate, co, globals, locals);
1473
657
    Py_DECREF(co);
1474
657
    return v;
1475
657
}
1476
1477
static PyObject *
1478
run_pyc_file(FILE *fp, PyObject *globals, PyObject *locals,
1479
             PyCompilerFlags *flags)
1480
0
{
1481
0
    PyThreadState *tstate = _PyThreadState_GET();
1482
0
    PyCodeObject *co;
1483
0
    PyObject *v;
1484
0
    long magic;
1485
0
    long PyImport_GetMagicNumber(void);
1486
1487
0
    magic = PyMarshal_ReadLongFromFile(fp);
1488
0
    if (magic != PyImport_GetMagicNumber()) {
1489
0
        if (!PyErr_Occurred())
1490
0
            PyErr_SetString(PyExc_RuntimeError,
1491
0
                       "Bad magic number in .pyc file");
1492
0
        goto error;
1493
0
    }
1494
    /* Skip the rest of the header. */
1495
0
    (void) PyMarshal_ReadLongFromFile(fp);
1496
0
    (void) PyMarshal_ReadLongFromFile(fp);
1497
0
    (void) PyMarshal_ReadLongFromFile(fp);
1498
0
    if (PyErr_Occurred()) {
1499
0
        goto error;
1500
0
    }
1501
0
    v = PyMarshal_ReadLastObjectFromFile(fp);
1502
0
    if (v == NULL || !PyCode_Check(v)) {
1503
0
        Py_XDECREF(v);
1504
0
        PyErr_SetString(PyExc_RuntimeError,
1505
0
                   "Bad code object in .pyc file");
1506
0
        goto error;
1507
0
    }
1508
0
    fclose(fp);
1509
0
    co = (PyCodeObject *)v;
1510
0
    v = run_eval_code_obj(tstate, co, globals, locals);
1511
0
    if (v && flags)
1512
0
        flags->cf_flags |= (co->co_flags & PyCF_MASK);
1513
0
    Py_DECREF(co);
1514
0
    return v;
1515
0
error:
1516
0
    fclose(fp);
1517
0
    return NULL;
1518
0
}
1519
1520
PyObject *
1521
Py_CompileStringObject(const char *str, PyObject *filename, int start,
1522
                       PyCompilerFlags *flags, int optimize)
1523
0
{
1524
0
    return _Py_CompileStringObjectWithModule(str, filename, start,
1525
0
                                             flags, optimize, NULL);
1526
0
}
1527
1528
PyObject *
1529
_Py_CompileStringObjectWithModule(const char *str, PyObject *filename, int start,
1530
                       PyCompilerFlags *flags, int optimize, PyObject *module)
1531
101k
{
1532
101k
    PyCodeObject *co;
1533
101k
    mod_ty mod;
1534
101k
    PyArena *arena = _PyArena_New();
1535
101k
    if (arena == NULL)
1536
0
        return NULL;
1537
1538
101k
    mod = _PyParser_ASTFromString(str, filename, start, flags, arena, module);
1539
101k
    if (mod == NULL) {
1540
94.8k
        _PyArena_Free(arena);
1541
94.8k
        return NULL;
1542
94.8k
    }
1543
6.33k
    if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1544
6.13k
        int syntax_check_only = ((flags->cf_flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST); /* unoptiomized AST */
1545
6.13k
        if (_PyCompile_AstPreprocess(mod, filename, flags, optimize, arena,
1546
6.13k
                                     syntax_check_only, module) < 0)
1547
23
        {
1548
23
            _PyArena_Free(arena);
1549
23
            return NULL;
1550
23
        }
1551
6.10k
        PyObject *result = PyAST_mod2obj(mod);
1552
6.10k
        _PyArena_Free(arena);
1553
6.10k
        return result;
1554
6.13k
    }
1555
202
    co = _PyAST_Compile(mod, filename, flags, optimize, arena, module);
1556
202
    _PyArena_Free(arena);
1557
202
    return (PyObject *)co;
1558
6.33k
}
1559
1560
PyObject *
1561
Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
1562
                        PyCompilerFlags *flags, int optimize)
1563
0
{
1564
0
    PyObject *filename, *co;
1565
0
    filename = PyUnicode_DecodeFSDefault(filename_str);
1566
0
    if (filename == NULL)
1567
0
        return NULL;
1568
0
    co = Py_CompileStringObject(str, filename, start, flags, optimize);
1569
0
    Py_DECREF(filename);
1570
0
    return co;
1571
0
}
1572
1573
int
1574
_PyObject_SupportedAsScript(PyObject *cmd)
1575
0
{
1576
0
    if (PyUnicode_Check(cmd)) {
1577
0
        return 1;
1578
0
    }
1579
0
    else if (PyBytes_Check(cmd)) {
1580
0
        return 1;
1581
0
    }
1582
0
    else if (PyByteArray_Check(cmd)) {
1583
0
        return 1;
1584
0
    }
1585
0
    else if (PyObject_CheckBuffer(cmd)) {
1586
0
        return 1;
1587
0
    }
1588
0
    else {
1589
0
        return 0;
1590
0
    }
1591
0
}
1592
1593
const char *
1594
_Py_SourceAsString(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
1595
101k
{
1596
101k
    const char *str;
1597
101k
    Py_ssize_t size;
1598
101k
    Py_buffer view;
1599
1600
101k
    *cmd_copy = NULL;
1601
101k
    if (PyUnicode_Check(cmd)) {
1602
82.2k
        cf->cf_flags |= PyCF_IGNORE_COOKIE;
1603
82.2k
        str = PyUnicode_AsUTF8AndSize(cmd, &size);
1604
82.2k
        if (str == NULL)
1605
0
            return NULL;
1606
82.2k
    }
1607
19.4k
    else if (PyBytes_Check(cmd)) {
1608
19.4k
        str = PyBytes_AS_STRING(cmd);
1609
19.4k
        size = PyBytes_GET_SIZE(cmd);
1610
19.4k
    }
1611
0
    else if (PyByteArray_Check(cmd)) {
1612
0
        str = PyByteArray_AS_STRING(cmd);
1613
0
        size = PyByteArray_GET_SIZE(cmd);
1614
0
    }
1615
0
    else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
1616
        /* Copy to NUL-terminated buffer. */
1617
0
        *cmd_copy = PyBytes_FromStringAndSize(
1618
0
            (const char *)view.buf, view.len);
1619
0
        PyBuffer_Release(&view);
1620
0
        if (*cmd_copy == NULL) {
1621
0
            return NULL;
1622
0
        }
1623
0
        str = PyBytes_AS_STRING(*cmd_copy);
1624
0
        size = PyBytes_GET_SIZE(*cmd_copy);
1625
0
    }
1626
0
    else {
1627
0
        PyErr_Format(PyExc_TypeError,
1628
0
            "%s() arg 1 must be a %s object",
1629
0
            funcname, what);
1630
0
        return NULL;
1631
0
    }
1632
1633
101k
    if (strlen(str) != (size_t)size) {
1634
5
        PyErr_SetString(PyExc_SyntaxError,
1635
5
            "source code string cannot contain null bytes");
1636
5
        Py_CLEAR(*cmd_copy);
1637
5
        return NULL;
1638
5
    }
1639
101k
    return str;
1640
101k
}
1641
1642
#if defined(USE_STACKCHECK)
1643
1644
/* Stack checking */
1645
1646
/*
1647
 * Return non-zero when we run out of memory on the stack; zero otherwise.
1648
 */
1649
int
1650
PyOS_CheckStack(void)
1651
{
1652
    PyThreadState *tstate = _PyThreadState_GET();
1653
    return _Py_ReachedRecursionLimit(tstate);
1654
}
1655
1656
#endif /* USE_STACKCHECK */
1657
1658
/* Deprecated C API functions still provided for binary compatibility */
1659
1660
#undef PyRun_AnyFile
1661
PyAPI_FUNC(int)
1662
PyRun_AnyFile(FILE *fp, const char *name)
1663
0
{
1664
0
    return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1665
0
}
1666
1667
#undef PyRun_AnyFileEx
1668
PyAPI_FUNC(int)
1669
PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1670
0
{
1671
0
    return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1672
0
}
1673
1674
#undef PyRun_AnyFileFlags
1675
PyAPI_FUNC(int)
1676
PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1677
0
{
1678
0
    return PyRun_AnyFileExFlags(fp, name, 0, flags);
1679
0
}
1680
1681
#undef PyRun_File
1682
PyAPI_FUNC(PyObject *)
1683
PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1684
0
{
1685
0
    return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1686
0
}
1687
1688
#undef PyRun_FileEx
1689
PyAPI_FUNC(PyObject *)
1690
PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1691
0
{
1692
0
    return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1693
0
}
1694
1695
#undef PyRun_FileFlags
1696
PyAPI_FUNC(PyObject *)
1697
PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1698
                PyCompilerFlags *flags)
1699
0
{
1700
0
    return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1701
0
}
1702
1703
#undef PyRun_SimpleFile
1704
PyAPI_FUNC(int)
1705
PyRun_SimpleFile(FILE *f, const char *p)
1706
0
{
1707
0
    return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1708
0
}
1709
1710
#undef PyRun_SimpleFileEx
1711
PyAPI_FUNC(int)
1712
PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1713
0
{
1714
0
    return PyRun_SimpleFileExFlags(f, p, c, NULL);
1715
0
}
1716
1717
1718
#undef PyRun_String
1719
PyAPI_FUNC(PyObject *)
1720
PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1721
0
{
1722
0
    return PyRun_StringFlags(str, s, g, l, NULL);
1723
0
}
1724
1725
#undef PyRun_SimpleString
1726
PyAPI_FUNC(int)
1727
PyRun_SimpleString(const char *s)
1728
0
{
1729
0
    return PyRun_SimpleStringFlags(s, NULL);
1730
0
}
1731
1732
#undef Py_CompileString
1733
PyAPI_FUNC(PyObject *)
1734
Py_CompileString(const char *str, const char *p, int s)
1735
0
{
1736
0
    return Py_CompileStringExFlags(str, p, s, NULL, -1);
1737
0
}
1738
1739
#undef Py_CompileStringFlags
1740
PyAPI_FUNC(PyObject *)
1741
Py_CompileStringFlags(const char *str, const char *p, int s,
1742
                      PyCompilerFlags *flags)
1743
0
{
1744
0
    return Py_CompileStringExFlags(str, p, s, flags, -1);
1745
0
}
1746
1747
#undef PyRun_InteractiveOne
1748
PyAPI_FUNC(int)
1749
PyRun_InteractiveOne(FILE *f, const char *p)
1750
0
{
1751
0
    return PyRun_InteractiveOneFlags(f, p, NULL);
1752
0
}
1753
1754
#undef PyRun_InteractiveLoop
1755
PyAPI_FUNC(int)
1756
PyRun_InteractiveLoop(FILE *f, const char *p)
1757
0
{
1758
    return PyRun_InteractiveLoopFlags(f, p, NULL);
1759
0
}