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