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