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