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