Coverage Report

Created: 2026-02-26 06:53

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
64
_PyRun_SimpleStringFlagsWithName(const char *command, const char* name, PyCompilerFlags *flags) {
557
64
    PyObject *main_module = PyImport_AddModuleRef("__main__");
558
64
    if (main_module == NULL) {
559
0
        return -1;
560
0
    }
561
64
    PyObject *dict = PyModule_GetDict(main_module);  // borrowed ref
562
563
64
    PyObject *res = NULL;
564
64
    if (name == NULL) {
565
64
        res = PyRun_StringFlags(command, Py_file_input, dict, dict, flags);
566
64
    } 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
64
    Py_DECREF(main_module);
576
64
    if (res == NULL) {
577
0
        PyErr_Print();
578
0
        return -1;
579
0
    }
580
581
64
    Py_DECREF(res);
582
64
    return 0;
583
64
}
584
585
int
586
PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
587
64
{
588
64
    return _PyRun_SimpleStringFlagsWithName(command, NULL, flags);
589
64
}
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
        goto fallback;
1149
0
    }
1150
1151
0
    PyObject* result = PyObject_CallOneArg(print_exception_fn, value);
1152
1153
0
    Py_XDECREF(print_exception_fn);
1154
0
    if (result) {
1155
0
        Py_DECREF(result);
1156
0
        return;
1157
0
    }
1158
0
fallback:
1159
#ifdef Py_DEBUG
1160
     if (PyErr_Occurred()) {
1161
         PyErr_FormatUnraisable(
1162
             "Exception ignored in the internal traceback machinery");
1163
     }
1164
#endif
1165
0
    PyErr_Clear();
1166
0
    struct exception_print_context ctx;
1167
0
    ctx.file = file;
1168
1169
    /* We choose to ignore seen being possibly NULL, and report
1170
       at least the main exception (it could be a MemoryError).
1171
    */
1172
0
    ctx.seen = PySet_New(NULL);
1173
0
    if (ctx.seen == NULL) {
1174
0
        PyErr_Clear();
1175
0
    }
1176
0
    if (print_exception_recursive(&ctx, value) < 0) {
1177
0
        PyErr_Clear();
1178
0
        PyObject_Dump(value);
1179
0
        fprintf(stderr, "lost sys.stderr\n");
1180
0
    }
1181
0
    Py_XDECREF(ctx.seen);
1182
1183
    /* Call file.flush() */
1184
0
    if (_PyFile_Flush(file) < 0) {
1185
        /* Silently ignore file.flush() error */
1186
0
        PyErr_Clear();
1187
0
    }
1188
0
}
1189
1190
void
1191
PyErr_Display(PyObject *unused, PyObject *value, PyObject *tb)
1192
0
{
1193
0
    PyObject *file;
1194
0
    if (PySys_GetOptionalAttr(&_Py_ID(stderr), &file) < 0) {
1195
0
        PyObject *exc = PyErr_GetRaisedException();
1196
0
        PyObject_Dump(value);
1197
0
        fprintf(stderr, "lost sys.stderr\n");
1198
0
        PyObject_Dump(exc);
1199
0
        Py_DECREF(exc);
1200
0
        return;
1201
0
    }
1202
0
    if (file == NULL) {
1203
0
        PyObject_Dump(value);
1204
0
        fprintf(stderr, "lost sys.stderr\n");
1205
0
        return;
1206
0
    }
1207
0
    if (file == Py_None) {
1208
0
        Py_DECREF(file);
1209
0
        return;
1210
0
    }
1211
0
    _PyErr_Display(file, NULL, value, tb);
1212
0
    Py_DECREF(file);
1213
0
}
1214
1215
void _PyErr_DisplayException(PyObject *file, PyObject *exc)
1216
0
{
1217
0
    _PyErr_Display(file, NULL, exc, NULL);
1218
0
}
1219
1220
void PyErr_DisplayException(PyObject *exc)
1221
0
{
1222
0
    PyErr_Display(NULL, exc, NULL);
1223
0
}
1224
1225
static PyObject *
1226
_PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
1227
                           PyObject *globals, PyObject *locals, PyCompilerFlags *flags,
1228
                           int generate_new_source)
1229
408
{
1230
408
    PyObject *ret = NULL;
1231
408
    mod_ty mod;
1232
408
    PyArena *arena;
1233
1234
408
    arena = _PyArena_New();
1235
408
    if (arena == NULL)
1236
0
        return NULL;
1237
1238
408
    PyObject* source = NULL;
1239
408
    _Py_DECLARE_STR(anon_string, "<string>");
1240
1241
408
    if (name) {
1242
0
        source = PyUnicode_FromString(str);
1243
0
        if (!source) {
1244
0
            PyErr_Clear();
1245
0
        }
1246
408
    } else {
1247
408
        name = &_Py_STR(anon_string);
1248
408
    }
1249
408
    PyObject *module = NULL;
1250
408
    if (globals && PyDict_GetItemStringRef(globals, "__name__", &module) < 0) {
1251
0
        goto done;
1252
0
    }
1253
1254
408
    mod = _PyParser_ASTFromString(str, name, start, flags, arena, module);
1255
408
    Py_XDECREF(module);
1256
1257
408
    if (mod != NULL) {
1258
408
        ret = run_mod(mod, name, globals, locals, flags, arena, source, generate_new_source);
1259
408
    }
1260
1261
408
done:
1262
408
    Py_XDECREF(source);
1263
408
    _PyArena_Free(arena);
1264
408
    return ret;
1265
408
}
1266
1267
1268
PyObject *
1269
PyRun_StringFlags(const char *str, int start, PyObject *globals,
1270
408
                     PyObject *locals, PyCompilerFlags *flags) {
1271
1272
408
    return _PyRun_StringFlagsWithName(str, NULL, start, globals, locals, flags, 0);
1273
408
}
1274
1275
static PyObject *
1276
pyrun_file(FILE *fp, PyObject *filename, int start, PyObject *globals,
1277
           PyObject *locals, int closeit, PyCompilerFlags *flags)
1278
0
{
1279
0
    PyArena *arena = _PyArena_New();
1280
0
    if (arena == NULL) {
1281
0
        return NULL;
1282
0
    }
1283
1284
0
    mod_ty mod;
1285
0
    mod = _PyParser_ASTFromFile(fp, filename, NULL, start, NULL, NULL,
1286
0
                                flags, NULL, arena);
1287
1288
0
    if (closeit) {
1289
0
        fclose(fp);
1290
0
    }
1291
1292
0
    PyObject *ret;
1293
0
    if (mod != NULL) {
1294
0
        ret = run_mod(mod, filename, globals, locals, flags, arena, NULL, 0);
1295
0
    }
1296
0
    else {
1297
0
        ret = NULL;
1298
0
    }
1299
0
    _PyArena_Free(arena);
1300
1301
0
    return ret;
1302
0
}
1303
1304
1305
PyObject *
1306
PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1307
                  PyObject *locals, int closeit, PyCompilerFlags *flags)
1308
0
{
1309
0
    PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
1310
0
    if (filename_obj == NULL) {
1311
0
        return NULL;
1312
0
    }
1313
1314
0
    PyObject *res = pyrun_file(fp, filename_obj, start, globals,
1315
0
                               locals, closeit, flags);
1316
0
    Py_DECREF(filename_obj);
1317
0
    return res;
1318
1319
0
}
1320
1321
static void
1322
flush_io_stream(PyThreadState *tstate, PyObject *name)
1323
0
{
1324
0
    PyObject *f;
1325
0
    if (PySys_GetOptionalAttr(name, &f) < 0) {
1326
0
        PyErr_Clear();
1327
0
    }
1328
0
    if (f != NULL) {
1329
0
        if (_PyFile_Flush(f) < 0) {
1330
0
            PyErr_Clear();
1331
0
        }
1332
0
        Py_DECREF(f);
1333
0
    }
1334
0
}
1335
1336
static void
1337
flush_io(void)
1338
0
{
1339
0
    PyThreadState *tstate = _PyThreadState_GET();
1340
0
    PyObject *exc = _PyErr_GetRaisedException(tstate);
1341
0
    flush_io_stream(tstate, &_Py_ID(stderr));
1342
0
    flush_io_stream(tstate, &_Py_ID(stdout));
1343
0
    _PyErr_SetRaisedException(tstate, exc);
1344
0
}
1345
1346
static PyObject *
1347
run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, PyObject *locals)
1348
408
{
1349
    /* Set globals['__builtins__'] if it doesn't exist */
1350
408
    if (!globals || !PyDict_Check(globals)) {
1351
0
        PyErr_SetString(PyExc_SystemError, "globals must be a real dict");
1352
0
        return NULL;
1353
0
    }
1354
408
    int has_builtins = PyDict_ContainsString(globals, "__builtins__");
1355
408
    if (has_builtins < 0) {
1356
0
        return NULL;
1357
0
    }
1358
408
    if (!has_builtins) {
1359
32
        if (PyDict_SetItemString(globals, "__builtins__",
1360
32
                                 tstate->interp->builtins) < 0)
1361
0
        {
1362
0
            return NULL;
1363
0
        }
1364
32
    }
1365
1366
408
    return PyEval_EvalCode((PyObject*)co, globals, locals);
1367
408
}
1368
1369
static PyObject *
1370
get_interactive_filename(PyObject *filename, Py_ssize_t count)
1371
0
{
1372
0
    PyObject *result;
1373
0
    Py_ssize_t len = PyUnicode_GET_LENGTH(filename);
1374
1375
0
    if (len >= 2
1376
0
            && PyUnicode_ReadChar(filename, 0) == '<'
1377
0
            && PyUnicode_ReadChar(filename, len - 1) == '>') {
1378
0
        PyObject *middle = PyUnicode_Substring(filename, 1, len-1);
1379
0
        if (middle == NULL) {
1380
0
            return NULL;
1381
0
        }
1382
0
        result = PyUnicode_FromFormat("<%U-%d>", middle, count);
1383
0
        Py_DECREF(middle);
1384
0
    } else {
1385
0
        result = PyUnicode_FromFormat(
1386
0
            "%U-%d", filename, count);
1387
0
    }
1388
0
    return result;
1389
1390
0
}
1391
1392
static PyObject *
1393
run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
1394
            PyCompilerFlags *flags, PyArena *arena, PyObject* interactive_src,
1395
            int generate_new_source)
1396
408
{
1397
408
    PyThreadState *tstate = _PyThreadState_GET();
1398
408
    PyObject* interactive_filename = filename;
1399
408
    if (interactive_src) {
1400
0
        PyInterpreterState *interp = tstate->interp;
1401
0
        if (generate_new_source) {
1402
0
            interactive_filename = get_interactive_filename(
1403
0
                filename, interp->_interactive_src_count++);
1404
0
        } else {
1405
0
            Py_INCREF(interactive_filename);
1406
0
        }
1407
0
        if (interactive_filename == NULL) {
1408
0
            return NULL;
1409
0
        }
1410
0
    }
1411
408
    PyObject *module = NULL;
1412
408
    if (globals && PyDict_GetItemStringRef(globals, "__name__", &module) < 0) {
1413
0
        if (interactive_src) {
1414
0
            Py_DECREF(interactive_filename);
1415
0
        }
1416
0
        return NULL;
1417
0
    }
1418
1419
408
    PyCodeObject *co = _PyAST_Compile(mod, interactive_filename, flags, -1,
1420
408
                                      arena, module);
1421
408
    Py_XDECREF(module);
1422
408
    if (co == NULL) {
1423
0
        if (interactive_src) {
1424
0
            Py_DECREF(interactive_filename);
1425
0
        }
1426
0
        return NULL;
1427
0
    }
1428
1429
408
    if (interactive_src) {
1430
0
        PyObject *print_tb_func = PyImport_ImportModuleAttrString(
1431
0
            "linecache",
1432
0
            "_register_code");
1433
0
        if (print_tb_func == NULL) {
1434
0
            Py_DECREF(co);
1435
0
            Py_DECREF(interactive_filename);
1436
0
            return NULL;
1437
0
        }
1438
1439
0
        if (!PyCallable_Check(print_tb_func)) {
1440
0
            Py_DECREF(co);
1441
0
            Py_DECREF(interactive_filename);
1442
0
            Py_DECREF(print_tb_func);
1443
0
            PyErr_SetString(PyExc_ValueError, "linecache._register_code is not callable");
1444
0
            return NULL;
1445
0
        }
1446
1447
0
        PyObject* result = PyObject_CallFunction(
1448
0
            print_tb_func, "OOO",
1449
0
            co,
1450
0
            interactive_src,
1451
0
            filename
1452
0
        );
1453
1454
0
        Py_DECREF(interactive_filename);
1455
1456
0
        Py_XDECREF(print_tb_func);
1457
0
        Py_XDECREF(result);
1458
0
        if (!result) {
1459
0
            Py_DECREF(co);
1460
0
            return NULL;
1461
0
        }
1462
0
    }
1463
1464
408
    if (_PySys_Audit(tstate, "exec", "O", co) < 0) {
1465
0
        Py_DECREF(co);
1466
0
        return NULL;
1467
0
    }
1468
1469
408
    PyObject *v = run_eval_code_obj(tstate, co, globals, locals);
1470
408
    Py_DECREF(co);
1471
408
    return v;
1472
408
}
1473
1474
static PyObject *
1475
run_pyc_file(FILE *fp, PyObject *globals, PyObject *locals,
1476
             PyCompilerFlags *flags)
1477
0
{
1478
0
    PyThreadState *tstate = _PyThreadState_GET();
1479
0
    PyCodeObject *co;
1480
0
    PyObject *v;
1481
0
    long magic;
1482
0
    long PyImport_GetMagicNumber(void);
1483
1484
0
    magic = PyMarshal_ReadLongFromFile(fp);
1485
0
    if (magic != PyImport_GetMagicNumber()) {
1486
0
        if (!PyErr_Occurred())
1487
0
            PyErr_SetString(PyExc_RuntimeError,
1488
0
                       "Bad magic number in .pyc file");
1489
0
        goto error;
1490
0
    }
1491
    /* Skip the rest of the header. */
1492
0
    (void) PyMarshal_ReadLongFromFile(fp);
1493
0
    (void) PyMarshal_ReadLongFromFile(fp);
1494
0
    (void) PyMarshal_ReadLongFromFile(fp);
1495
0
    if (PyErr_Occurred()) {
1496
0
        goto error;
1497
0
    }
1498
0
    v = PyMarshal_ReadLastObjectFromFile(fp);
1499
0
    if (v == NULL || !PyCode_Check(v)) {
1500
0
        Py_XDECREF(v);
1501
0
        PyErr_SetString(PyExc_RuntimeError,
1502
0
                   "Bad code object in .pyc file");
1503
0
        goto error;
1504
0
    }
1505
0
    fclose(fp);
1506
0
    co = (PyCodeObject *)v;
1507
0
    v = run_eval_code_obj(tstate, co, globals, locals);
1508
0
    if (v && flags)
1509
0
        flags->cf_flags |= (co->co_flags & PyCF_MASK);
1510
0
    Py_DECREF(co);
1511
0
    return v;
1512
0
error:
1513
0
    fclose(fp);
1514
0
    return NULL;
1515
0
}
1516
1517
PyObject *
1518
Py_CompileStringObject(const char *str, PyObject *filename, int start,
1519
                       PyCompilerFlags *flags, int optimize)
1520
0
{
1521
0
    return _Py_CompileStringObjectWithModule(str, filename, start,
1522
0
                                             flags, optimize, NULL);
1523
0
}
1524
1525
PyObject *
1526
_Py_CompileStringObjectWithModule(const char *str, PyObject *filename, int start,
1527
                       PyCompilerFlags *flags, int optimize, PyObject *module)
1528
17.3k
{
1529
17.3k
    PyCodeObject *co;
1530
17.3k
    mod_ty mod;
1531
17.3k
    PyArena *arena = _PyArena_New();
1532
17.3k
    if (arena == NULL)
1533
0
        return NULL;
1534
1535
17.3k
    mod = _PyParser_ASTFromString(str, filename, start, flags, arena, module);
1536
17.3k
    if (mod == NULL) {
1537
12.1k
        _PyArena_Free(arena);
1538
12.1k
        return NULL;
1539
12.1k
    }
1540
5.19k
    if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1541
5.01k
        int syntax_check_only = ((flags->cf_flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST); /* unoptiomized AST */
1542
5.01k
        if (_PyCompile_AstPreprocess(mod, filename, flags, optimize, arena,
1543
5.01k
                                     syntax_check_only, module) < 0)
1544
12
        {
1545
12
            _PyArena_Free(arena);
1546
12
            return NULL;
1547
12
        }
1548
4.99k
        PyObject *result = PyAST_mod2obj(mod);
1549
4.99k
        _PyArena_Free(arena);
1550
4.99k
        return result;
1551
5.01k
    }
1552
180
    co = _PyAST_Compile(mod, filename, flags, optimize, arena, module);
1553
180
    _PyArena_Free(arena);
1554
180
    return (PyObject *)co;
1555
5.19k
}
1556
1557
PyObject *
1558
Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
1559
                        PyCompilerFlags *flags, int optimize)
1560
0
{
1561
0
    PyObject *filename, *co;
1562
0
    filename = PyUnicode_DecodeFSDefault(filename_str);
1563
0
    if (filename == NULL)
1564
0
        return NULL;
1565
0
    co = Py_CompileStringObject(str, filename, start, flags, optimize);
1566
0
    Py_DECREF(filename);
1567
0
    return co;
1568
0
}
1569
1570
int
1571
_PyObject_SupportedAsScript(PyObject *cmd)
1572
0
{
1573
0
    if (PyUnicode_Check(cmd)) {
1574
0
        return 1;
1575
0
    }
1576
0
    else if (PyBytes_Check(cmd)) {
1577
0
        return 1;
1578
0
    }
1579
0
    else if (PyByteArray_Check(cmd)) {
1580
0
        return 1;
1581
0
    }
1582
0
    else if (PyObject_CheckBuffer(cmd)) {
1583
0
        return 1;
1584
0
    }
1585
0
    else {
1586
0
        return 0;
1587
0
    }
1588
0
}
1589
1590
const char *
1591
_Py_SourceAsString(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
1592
17.6k
{
1593
17.6k
    const char *str;
1594
17.6k
    Py_ssize_t size;
1595
17.6k
    Py_buffer view;
1596
1597
17.6k
    *cmd_copy = NULL;
1598
17.6k
    if (PyUnicode_Check(cmd)) {
1599
336
        cf->cf_flags |= PyCF_IGNORE_COOKIE;
1600
336
        str = PyUnicode_AsUTF8AndSize(cmd, &size);
1601
336
        if (str == NULL)
1602
0
            return NULL;
1603
336
    }
1604
17.3k
    else if (PyBytes_Check(cmd)) {
1605
17.3k
        str = PyBytes_AS_STRING(cmd);
1606
17.3k
        size = PyBytes_GET_SIZE(cmd);
1607
17.3k
    }
1608
0
    else if (PyByteArray_Check(cmd)) {
1609
0
        str = PyByteArray_AS_STRING(cmd);
1610
0
        size = PyByteArray_GET_SIZE(cmd);
1611
0
    }
1612
0
    else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
1613
        /* Copy to NUL-terminated buffer. */
1614
0
        *cmd_copy = PyBytes_FromStringAndSize(
1615
0
            (const char *)view.buf, view.len);
1616
0
        PyBuffer_Release(&view);
1617
0
        if (*cmd_copy == NULL) {
1618
0
            return NULL;
1619
0
        }
1620
0
        str = PyBytes_AS_STRING(*cmd_copy);
1621
0
        size = PyBytes_GET_SIZE(*cmd_copy);
1622
0
    }
1623
0
    else {
1624
0
        PyErr_Format(PyExc_TypeError,
1625
0
            "%s() arg 1 must be a %s object",
1626
0
            funcname, what);
1627
0
        return NULL;
1628
0
    }
1629
1630
17.6k
    if (strlen(str) != (size_t)size) {
1631
6
        PyErr_SetString(PyExc_SyntaxError,
1632
6
            "source code string cannot contain null bytes");
1633
6
        Py_CLEAR(*cmd_copy);
1634
6
        return NULL;
1635
6
    }
1636
17.6k
    return str;
1637
17.6k
}
1638
1639
#if defined(USE_STACKCHECK)
1640
1641
/* Stack checking */
1642
1643
/*
1644
 * Return non-zero when we run out of memory on the stack; zero otherwise.
1645
 */
1646
int
1647
PyOS_CheckStack(void)
1648
{
1649
    PyThreadState *tstate = _PyThreadState_GET();
1650
    return _Py_ReachedRecursionLimit(tstate);
1651
}
1652
1653
#endif /* USE_STACKCHECK */
1654
1655
/* Deprecated C API functions still provided for binary compatibility */
1656
1657
#undef PyRun_AnyFile
1658
PyAPI_FUNC(int)
1659
PyRun_AnyFile(FILE *fp, const char *name)
1660
0
{
1661
0
    return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1662
0
}
1663
1664
#undef PyRun_AnyFileEx
1665
PyAPI_FUNC(int)
1666
PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1667
0
{
1668
0
    return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1669
0
}
1670
1671
#undef PyRun_AnyFileFlags
1672
PyAPI_FUNC(int)
1673
PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1674
0
{
1675
0
    return PyRun_AnyFileExFlags(fp, name, 0, flags);
1676
0
}
1677
1678
#undef PyRun_File
1679
PyAPI_FUNC(PyObject *)
1680
PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1681
0
{
1682
0
    return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1683
0
}
1684
1685
#undef PyRun_FileEx
1686
PyAPI_FUNC(PyObject *)
1687
PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1688
0
{
1689
0
    return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1690
0
}
1691
1692
#undef PyRun_FileFlags
1693
PyAPI_FUNC(PyObject *)
1694
PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1695
                PyCompilerFlags *flags)
1696
0
{
1697
0
    return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1698
0
}
1699
1700
#undef PyRun_SimpleFile
1701
PyAPI_FUNC(int)
1702
PyRun_SimpleFile(FILE *f, const char *p)
1703
0
{
1704
0
    return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1705
0
}
1706
1707
#undef PyRun_SimpleFileEx
1708
PyAPI_FUNC(int)
1709
PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1710
0
{
1711
0
    return PyRun_SimpleFileExFlags(f, p, c, NULL);
1712
0
}
1713
1714
1715
#undef PyRun_String
1716
PyAPI_FUNC(PyObject *)
1717
PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1718
0
{
1719
0
    return PyRun_StringFlags(str, s, g, l, NULL);
1720
0
}
1721
1722
#undef PyRun_SimpleString
1723
PyAPI_FUNC(int)
1724
PyRun_SimpleString(const char *s)
1725
0
{
1726
0
    return PyRun_SimpleStringFlags(s, NULL);
1727
0
}
1728
1729
#undef Py_CompileString
1730
PyAPI_FUNC(PyObject *)
1731
Py_CompileString(const char *str, const char *p, int s)
1732
0
{
1733
0
    return Py_CompileStringExFlags(str, p, s, NULL, -1);
1734
0
}
1735
1736
#undef Py_CompileStringFlags
1737
PyAPI_FUNC(PyObject *)
1738
Py_CompileStringFlags(const char *str, const char *p, int s,
1739
                      PyCompilerFlags *flags)
1740
0
{
1741
0
    return Py_CompileStringExFlags(str, p, s, flags, -1);
1742
0
}
1743
1744
#undef PyRun_InteractiveOne
1745
PyAPI_FUNC(int)
1746
PyRun_InteractiveOne(FILE *f, const char *p)
1747
0
{
1748
0
    return PyRun_InteractiveOneFlags(f, p, NULL);
1749
0
}
1750
1751
#undef PyRun_InteractiveLoop
1752
PyAPI_FUNC(int)
1753
PyRun_InteractiveLoop(FILE *f, const char *p)
1754
0
{
1755
    return PyRun_InteractiveLoopFlags(f, p, NULL);
1756
0
}