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