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