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