blob: 26f74c80d032708bd33e542c1395edfc4bb18d5c [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
Eric Snowc7ec9982017-05-23 23:00:52 -07002/* 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 */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000010
Guido van Rossum82598051997-03-05 00:20:32 +000011#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "Python-ast.h"
Guido van Rossumd8faa362007-04-27 19:54:29 +000014#undef Yield /* undefine macro conflicting with winbase.h */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060015#include "internal/pystate.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000016#include "grammar.h"
17#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000018#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000019#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000020#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000022#include "symtable.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000023#include "ast.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000024#include "marshal.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000025#include "osdefs.h"
Victor Stinner518e6102014-03-18 02:06:38 +010026#include <locale.h>
Guido van Rossum1984f1e1992-08-04 12:41:02 +000027
Thomas Wouters0e3f5912006-08-11 14:57:12 +000028#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000029#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000030#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000031
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000032#ifdef MS_WINDOWS
Martin v. Löwis5c88d812009-01-02 20:47:48 +000033#include "malloc.h" /* for alloca */
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000034#endif
Martin v. Löwis5c88d812009-01-02 20:47:48 +000035
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000036#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000037#undef BYTE
38#include "windows.h"
39#endif
40
Victor Stinnerbd303c12013-11-07 23:07:29 +010041_Py_IDENTIFIER(builtins);
Victor Stinner09054372013-11-06 22:41:44 +010042_Py_IDENTIFIER(excepthook);
Victor Stinner3f36a572013-11-12 21:39:02 +010043_Py_IDENTIFIER(flush);
Victor Stinnerbd303c12013-11-07 23:07:29 +010044_Py_IDENTIFIER(last_traceback);
Victor Stinner09054372013-11-06 22:41:44 +010045_Py_IDENTIFIER(last_type);
46_Py_IDENTIFIER(last_value);
Victor Stinnerbd303c12013-11-07 23:07:29 +010047_Py_IDENTIFIER(ps1);
48_Py_IDENTIFIER(ps2);
49_Py_IDENTIFIER(stdin);
50_Py_IDENTIFIER(stdout);
51_Py_IDENTIFIER(stderr);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +010052_Py_static_string(PyId_string, "<string>");
Victor Stinner09054372013-11-06 22:41:44 +010053
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054#ifdef __cplusplus
55extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000056#endif
57
Guido van Rossum82598051997-03-05 00:20:32 +000058extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000059
Guido van Rossumb73cc041993-11-01 16:28:59 +000060/* Forward */
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000061static void flush_io(void);
Victor Stinner95701bd2013-11-06 18:41:07 +010062static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000064static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static void err_input(perrdetail *);
Victor Stinner7f2fee32011-04-05 00:39:01 +020067static void err_free(perrdetail *);
xdegayee0582a32017-11-12 16:50:48 +010068static int PyRun_InteractiveOneObjectEx(FILE *, PyObject *, PyCompilerFlags *);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000069
Guido van Rossum1984f1e1992-08-04 12:41:02 +000070/* Parse input from a file and execute it */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000071int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000072PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +000074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 if (filename == NULL)
76 filename = "???";
77 if (Py_FdIsInteractive(fp, filename)) {
78 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
79 if (closeit)
80 fclose(fp);
81 return err;
82 }
83 else
84 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000085}
86
87int
Victor Stinner95701bd2013-11-06 18:41:07 +010088PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +000089{
Victor Stinner95701bd2013-11-06 18:41:07 +010090 PyObject *filename, *v;
91 int ret, err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 PyCompilerFlags local_flags;
xdegayee0582a32017-11-12 16:50:48 +010093 int nomem_count = 0;
Victor Stinner25420fe2017-11-20 18:12:22 -080094#ifdef Py_REF_DEBUG
95 int show_ref_count = PyThreadState_GET()->interp->core_config.show_ref_count;
96#endif
Jeremy Hylton9f324e92001-03-01 22:59:14 +000097
Victor Stinner95701bd2013-11-06 18:41:07 +010098 filename = PyUnicode_DecodeFSDefault(filename_str);
99 if (filename == NULL) {
100 PyErr_Print();
101 return -1;
102 }
103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 if (flags == NULL) {
105 flags = &local_flags;
106 local_flags.cf_flags = 0;
107 }
Victor Stinner09054372013-11-06 22:41:44 +0100108 v = _PySys_GetObjectId(&PyId_ps1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 if (v == NULL) {
Victor Stinner09054372013-11-06 22:41:44 +0100110 _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> "));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 Py_XDECREF(v);
112 }
Victor Stinner09054372013-11-06 22:41:44 +0100113 v = _PySys_GetObjectId(&PyId_ps2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 if (v == NULL) {
Victor Stinner09054372013-11-06 22:41:44 +0100115 _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... "));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 Py_XDECREF(v);
117 }
xdegayee0582a32017-11-12 16:50:48 +0100118 err = 0;
119 do {
120 ret = PyRun_InteractiveOneObjectEx(fp, filename, flags);
121 if (ret == -1 && PyErr_Occurred()) {
122 /* Prevent an endless loop after multiple consecutive MemoryErrors
123 * while still allowing an interactive command to fail with a
124 * MemoryError. */
125 if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
126 if (++nomem_count > 16) {
127 PyErr_Clear();
128 err = -1;
129 break;
130 }
131 } else {
132 nomem_count = 0;
133 }
134 PyErr_Print();
135 flush_io();
136 } else {
137 nomem_count = 0;
138 }
Eric Snowdae02762017-09-14 00:35:58 -0700139#ifdef Py_REF_DEBUG
Victor Stinner25420fe2017-11-20 18:12:22 -0800140 if (show_ref_count) {
Eric Snowdae02762017-09-14 00:35:58 -0700141 _PyDebug_PrintTotalRefs();
Victor Stinner25420fe2017-11-20 18:12:22 -0800142 }
Eric Snowdae02762017-09-14 00:35:58 -0700143#endif
xdegayee0582a32017-11-12 16:50:48 +0100144 } while (ret != E_EOF);
Victor Stinner95701bd2013-11-06 18:41:07 +0100145 Py_DECREF(filename);
146 return err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000147}
148
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000149/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000150static int PARSER_FLAGS(PyCompilerFlags *flags)
151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 int parser_flags = 0;
153 if (!flags)
154 return 0;
155 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
156 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
157 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
158 parser_flags |= PyPARSE_IGNORE_COOKIE;
159 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
160 parser_flags |= PyPARSE_BARRY_AS_BDFL;
161 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000162}
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000163
Thomas Wouters89f507f2006-12-13 04:49:30 +0000164#if 0
165/* Keep an example of flags with future keyword support. */
166#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
168 PyPARSE_DONT_IMPLY_DEDENT : 0) \
169 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
170 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000171#endif
172
xdegayee0582a32017-11-12 16:50:48 +0100173/* A PyRun_InteractiveOneObject() auxiliary function that does not print the
174 * error on failure. */
175static int
176PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
177 PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000178{
Victor Stinner95701bd2013-11-06 18:41:07 +0100179 PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 mod_ty mod;
181 PyArena *arena;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200182 const char *ps1 = "", *ps2 = "", *enc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 int errcode = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200184 _Py_IDENTIFIER(encoding);
Victor Stinner95701bd2013-11-06 18:41:07 +0100185 _Py_IDENTIFIER(__main__);
186
187 mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */
188 if (mod_name == NULL) {
Victor Stinner95701bd2013-11-06 18:41:07 +0100189 return -1;
190 }
Tim Petersfe2127d2001-07-16 05:37:24 +0000191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (fp == stdin) {
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -0400193 /* Fetch encoding from sys.stdin if possible. */
Victor Stinnerbd303c12013-11-07 23:07:29 +0100194 v = _PySys_GetObjectId(&PyId_stdin);
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -0400195 if (v && v != Py_None) {
196 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
197 if (oenc)
Serhiy Storchaka06515832016-11-20 09:13:07 +0200198 enc = PyUnicode_AsUTF8(oenc);
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -0400199 if (!enc)
200 PyErr_Clear();
201 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 }
Victor Stinner09054372013-11-06 22:41:44 +0100203 v = _PySys_GetObjectId(&PyId_ps1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (v != NULL) {
205 v = PyObject_Str(v);
206 if (v == NULL)
207 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +0000208 else if (PyUnicode_Check(v)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200209 ps1 = PyUnicode_AsUTF8(v);
Victor Stinner386fe712010-05-19 00:34:15 +0000210 if (ps1 == NULL) {
211 PyErr_Clear();
212 ps1 = "";
213 }
214 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 }
Victor Stinner09054372013-11-06 22:41:44 +0100216 w = _PySys_GetObjectId(&PyId_ps2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (w != NULL) {
218 w = PyObject_Str(w);
219 if (w == NULL)
220 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +0000221 else if (PyUnicode_Check(w)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200222 ps2 = PyUnicode_AsUTF8(w);
Victor Stinner386fe712010-05-19 00:34:15 +0000223 if (ps2 == NULL) {
224 PyErr_Clear();
225 ps2 = "";
226 }
227 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 }
229 arena = PyArena_New();
230 if (arena == NULL) {
231 Py_XDECREF(v);
232 Py_XDECREF(w);
233 Py_XDECREF(oenc);
234 return -1;
235 }
Victor Stinner95701bd2013-11-06 18:41:07 +0100236 mod = PyParser_ASTFromFileObject(fp, filename, enc,
237 Py_single_input, ps1, ps2,
238 flags, &errcode, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 Py_XDECREF(v);
240 Py_XDECREF(w);
241 Py_XDECREF(oenc);
242 if (mod == NULL) {
243 PyArena_Free(arena);
244 if (errcode == E_EOF) {
245 PyErr_Clear();
246 return E_EOF;
247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 return -1;
249 }
Victor Stinner95701bd2013-11-06 18:41:07 +0100250 m = PyImport_AddModuleObject(mod_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 if (m == NULL) {
252 PyArena_Free(arena);
253 return -1;
254 }
255 d = PyModule_GetDict(m);
256 v = run_mod(mod, filename, d, d, flags, arena);
257 PyArena_Free(arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 return -1;
260 }
261 Py_DECREF(v);
Antoine Pitrou9845c7e2014-05-11 13:42:17 +0200262 flush_io();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000264}
265
Victor Stinner95701bd2013-11-06 18:41:07 +0100266int
xdegayee0582a32017-11-12 16:50:48 +0100267PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
268{
269 int res;
270
271 res = PyRun_InteractiveOneObjectEx(fp, filename, flags);
272 if (res == -1) {
273 PyErr_Print();
274 flush_io();
275 }
276 return res;
277}
278
279int
Victor Stinner95701bd2013-11-06 18:41:07 +0100280PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
281{
282 PyObject *filename;
283 int res;
284
285 filename = PyUnicode_DecodeFSDefault(filename_str);
286 if (filename == NULL) {
287 PyErr_Print();
288 return -1;
289 }
290 res = PyRun_InteractiveOneObject(fp, filename, flags);
291 Py_DECREF(filename);
292 return res;
293}
294
295
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000296/* Check whether a file maybe a pyc file: Look at the extension,
297 the file type, and, if we may close it, at the first few bytes. */
298
299static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000300maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000301{
Brett Cannonf299abd2015-04-13 14:21:02 -0400302 if (strcmp(ext, ".pyc") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 /* Only look into the file if we are allowed to close it, since
306 it then should also be seekable. */
307 if (closeit) {
308 /* Read only two bytes of the magic. If the file was opened in
309 text mode, the bytes 3 and 4 of the magic (\r\n) might not
310 be read as they are on disk. */
311 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
312 unsigned char buf[2];
313 /* Mess: In case of -x, the stream is NOT at its start now,
314 and ungetc() was used to push back the first newline,
315 which makes the current stream position formally undefined,
316 and a x-platform nightmare.
317 Unfortunately, we have no direct way to know whether -x
318 was specified. So we use a terrible hack: if the current
319 stream position is not 0, we assume -x was specified, and
320 give up. Bug 132850 on SourceForge spells out the
321 hopelessness of trying anything else (fseek and ftell
322 don't work predictably x-platform for text-mode files).
323 */
324 int ispyc = 0;
325 if (ftell(fp) == 0) {
326 if (fread(buf, 1, 2, fp) == 2 &&
327 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
328 ispyc = 1;
329 rewind(fp);
330 }
331 return ispyc;
332 }
333 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000334}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000335
Antoine Pitrou32d483c2013-07-30 21:01:23 +0200336static int
337set_main_loader(PyObject *d, const char *filename, const char *loader_name)
Nick Coghlan85e729e2012-07-15 18:09:52 +1000338{
339 PyInterpreterState *interp;
340 PyThreadState *tstate;
Eric Snow32439d62015-05-02 19:15:18 -0600341 PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader;
Nick Coghlanb7a58942012-07-15 23:21:08 +1000342 int result = 0;
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200343
344 filename_obj = PyUnicode_DecodeFSDefault(filename);
345 if (filename_obj == NULL)
346 return -1;
Nick Coghlan85e729e2012-07-15 18:09:52 +1000347 /* Get current thread state and interpreter pointer */
348 tstate = PyThreadState_GET();
349 interp = tstate->interp;
Eric Snow32439d62015-05-02 19:15:18 -0600350 bootstrap = PyObject_GetAttrString(interp->importlib,
351 "_bootstrap_external");
352 if (bootstrap != NULL) {
353 loader_type = PyObject_GetAttrString(bootstrap, loader_name);
354 Py_DECREF(bootstrap);
355 }
Nick Coghlan3f94cbf2012-07-15 19:10:39 +1000356 if (loader_type == NULL) {
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200357 Py_DECREF(filename_obj);
Nick Coghlan3f94cbf2012-07-15 19:10:39 +1000358 return -1;
359 }
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200360 loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
Nick Coghlanb7a58942012-07-15 23:21:08 +1000361 Py_DECREF(loader_type);
362 if (loader == NULL) {
Nick Coghlan85e729e2012-07-15 18:09:52 +1000363 return -1;
364 }
Nick Coghlanb7a58942012-07-15 23:21:08 +1000365 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
366 result = -1;
367 }
Nick Coghlan85e729e2012-07-15 18:09:52 +1000368 Py_DECREF(loader);
Nick Coghlanb7a58942012-07-15 23:21:08 +1000369 return result;
Nick Coghlan85e729e2012-07-15 18:09:52 +1000370}
371
372int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000373PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 PyObject *m, *d, *v;
377 const char *ext;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100378 int set_file_name = 0, ret = -1;
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000379 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 m = PyImport_AddModule("__main__");
382 if (m == NULL)
383 return -1;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100384 Py_INCREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 d = PyModule_GetDict(m);
386 if (PyDict_GetItemString(d, "__file__") == NULL) {
387 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000388 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (f == NULL)
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100390 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (PyDict_SetItemString(d, "__file__", f) < 0) {
392 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100393 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 }
Barry Warsaw916048d2011-09-20 14:45:44 -0400395 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
396 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100397 goto done;
Barry Warsaw916048d2011-09-20 14:45:44 -0400398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 set_file_name = 1;
400 Py_DECREF(f);
401 }
402 len = strlen(filename);
403 ext = filename + len - (len > 4 ? 4 : 0);
404 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Christian Heimes04ac4c12012-09-11 15:47:28 +0200405 FILE *pyc_fp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 /* Try to run a pyc file. First, re-open in binary */
407 if (closeit)
408 fclose(fp);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200409 if ((pyc_fp = _Py_fopen(filename, "rb")) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 fprintf(stderr, "python: Can't reopen .pyc file\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 goto done;
412 }
Nick Coghlan85e729e2012-07-15 18:09:52 +1000413
414 if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
415 fprintf(stderr, "python: failed to set __main__.__loader__\n");
416 ret = -1;
Christian Heimes04ac4c12012-09-11 15:47:28 +0200417 fclose(pyc_fp);
Nick Coghlan85e729e2012-07-15 18:09:52 +1000418 goto done;
419 }
Christian Heimes04ac4c12012-09-11 15:47:28 +0200420 v = run_pyc_file(pyc_fp, filename, d, d, flags);
421 fclose(pyc_fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 } else {
Nick Coghlan85e729e2012-07-15 18:09:52 +1000423 /* When running from stdin, leave __main__.__loader__ alone */
424 if (strcmp(filename, "<stdin>") != 0 &&
425 set_main_loader(d, filename, "SourceFileLoader") < 0) {
426 fprintf(stderr, "python: failed to set __main__.__loader__\n");
427 ret = -1;
428 goto done;
429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
431 closeit, flags);
432 }
433 flush_io();
434 if (v == NULL) {
435 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 goto done;
437 }
438 Py_DECREF(v);
439 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000440 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (set_file_name && PyDict_DelItemString(d, "__file__"))
442 PyErr_Clear();
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100443 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000445}
446
447int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000448PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 PyObject *m, *d, *v;
451 m = PyImport_AddModule("__main__");
452 if (m == NULL)
453 return -1;
454 d = PyModule_GetDict(m);
455 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
456 if (v == NULL) {
457 PyErr_Print();
458 return -1;
459 }
460 Py_DECREF(v);
461 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000462}
463
Barry Warsaw035574d1997-08-29 22:07:17 +0000464static int
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100465parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
466 int *lineno, int *offset, PyObject **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000467{
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300468 int hold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 PyObject *v;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200470 _Py_IDENTIFIER(msg);
471 _Py_IDENTIFIER(filename);
472 _Py_IDENTIFIER(lineno);
473 _Py_IDENTIFIER(offset);
474 _Py_IDENTIFIER(text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000475
Benjamin Peterson80d50422012-04-03 00:30:38 -0400476 *message = NULL;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100477 *filename = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 /* new style errors. `err' is an instance */
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400480 *message = _PyObject_GetAttrId(err, &PyId_msg);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400481 if (!*message)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +0000483
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400484 v = _PyObject_GetAttrId(err, &PyId_filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400485 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400487 if (v == Py_None) {
488 Py_DECREF(v);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100489 *filename = _PyUnicode_FromId(&PyId_string);
490 if (*filename == NULL)
491 goto finally;
492 Py_INCREF(*filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400493 }
494 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100495 *filename = v;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400496 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000497
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400498 v = _PyObject_GetAttrId(err, &PyId_lineno);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400499 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 goto finally;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300501 hold = _PyLong_AsInt(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (hold < 0 && PyErr_Occurred())
504 goto finally;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300505 *lineno = hold;
Barry Warsaw035574d1997-08-29 22:07:17 +0000506
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400507 v = _PyObject_GetAttrId(err, &PyId_offset);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400508 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 goto finally;
510 if (v == Py_None) {
511 *offset = -1;
512 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 } else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300514 hold = _PyLong_AsInt(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 if (hold < 0 && PyErr_Occurred())
517 goto finally;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300518 *offset = hold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000520
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400521 v = _PyObject_GetAttrId(err, &PyId_text);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400522 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400524 if (v == Py_None) {
525 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 *text = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400527 }
528 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100529 *text = v;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400530 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +0000532
533finally:
Benjamin Peterson80d50422012-04-03 00:30:38 -0400534 Py_XDECREF(*message);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100535 Py_XDECREF(*filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +0000537}
538
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000539void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000540PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +0000543}
544
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000545static void
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100546print_error_text(PyObject *f, int offset, PyObject *text_obj)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000547{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200548 const char *text;
549 const char *nl;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100550
Serhiy Storchaka06515832016-11-20 09:13:07 +0200551 text = PyUnicode_AsUTF8(text_obj);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100552 if (text == NULL)
553 return;
554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 if (offset >= 0) {
Victor Stinner98ea54c2014-08-15 23:30:40 +0200556 if (offset > 0 && (size_t)offset == strlen(text) && text[offset - 1] == '\n')
Benjamin Petersona95e9772010-10-29 03:28:14 +0000557 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 for (;;) {
559 nl = strchr(text, '\n');
560 if (nl == NULL || nl-text >= offset)
561 break;
562 offset -= (int)(nl+1-text);
563 text = nl+1;
564 }
Martin Panterca3263c2016-12-11 00:18:36 +0000565 while (*text == ' ' || *text == '\t' || *text == '\f') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 text++;
567 offset--;
568 }
569 }
570 PyFile_WriteString(" ", f);
571 PyFile_WriteString(text, f);
572 if (*text == '\0' || text[strlen(text)-1] != '\n')
573 PyFile_WriteString("\n", f);
574 if (offset == -1)
575 return;
576 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +0000577 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000580}
581
Guido van Rossum66e8e862001-03-23 17:54:43 +0000582static void
583handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 PyObject *exception, *value, *tb;
586 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 if (Py_InspectFlag)
589 /* Don't exit if -i flag was given. This flag is set to 0
590 * when entering interactive mode for inspecting. */
591 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 PyErr_Fetch(&exception, &value, &tb);
594 fflush(stdout);
595 if (value == NULL || value == Py_None)
596 goto done;
597 if (PyExceptionInstance_Check(value)) {
598 /* The error code should be in the `code' attribute. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200599 _Py_IDENTIFIER(code);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200600 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 if (code) {
602 Py_DECREF(value);
603 value = code;
604 if (value == Py_None)
605 goto done;
606 }
607 /* If we failed to dig out the 'code' attribute,
608 just let the else clause below print the error. */
609 }
610 if (PyLong_Check(value))
611 exitcode = (int)PyLong_AsLong(value);
612 else {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100613 PyObject *sys_stderr = _PySys_GetObjectId(&PyId_stderr);
Nick Coghland979e432014-02-09 10:43:21 +1000614 /* We clear the exception here to avoid triggering the assertion
615 * in PyObject_Str that ensures it won't silently lose exception
616 * details.
617 */
618 PyErr_Clear();
Victor Stinner7126dbc2010-05-21 23:45:42 +0000619 if (sys_stderr != NULL && sys_stderr != Py_None) {
620 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
621 } else {
622 PyObject_Print(value, stderr, Py_PRINT_RAW);
623 fflush(stderr);
624 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 PySys_WriteStderr("\n");
626 exitcode = 1;
627 }
Tim Peterscf615b52003-04-19 18:47:02 +0000628 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 /* Restore and clear the exception info, in order to properly decref
630 * the exception, value, and traceback. If we just exit instead,
631 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
632 * some finalizers from running.
633 */
634 PyErr_Restore(exception, value, tb);
635 PyErr_Clear();
636 Py_Exit(exitcode);
637 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000638}
639
640void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000641PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
646 handle_system_exit();
647 }
648 PyErr_Fetch(&exception, &v, &tb);
649 if (exception == NULL)
650 return;
651 PyErr_NormalizeException(&exception, &v, &tb);
652 if (tb == NULL) {
653 tb = Py_None;
654 Py_INCREF(tb);
655 }
656 PyException_SetTraceback(v, tb);
657 if (exception == NULL)
658 return;
659 /* Now we know v != NULL too */
660 if (set_sys_last_vars) {
xdegaye66caacf2017-10-23 18:08:41 +0200661 if (_PySys_SetObjectId(&PyId_last_type, exception) < 0) {
662 PyErr_Clear();
663 }
664 if (_PySys_SetObjectId(&PyId_last_value, v) < 0) {
665 PyErr_Clear();
666 }
667 if (_PySys_SetObjectId(&PyId_last_traceback, tb) < 0) {
668 PyErr_Clear();
669 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 }
Victor Stinner09054372013-11-06 22:41:44 +0100671 hook = _PySys_GetObjectId(&PyId_excepthook);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (hook) {
Victor Stinner71cb64a2016-08-20 00:57:43 +0200673 PyObject* stack[3];
674 PyObject *result;
675
676 stack[0] = exception;
677 stack[1] = v;
678 stack[2] = tb;
Victor Stinner559bb6a2016-08-22 22:48:54 +0200679 result = _PyObject_FastCall(hook, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 if (result == NULL) {
681 PyObject *exception2, *v2, *tb2;
682 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
683 handle_system_exit();
684 }
685 PyErr_Fetch(&exception2, &v2, &tb2);
686 PyErr_NormalizeException(&exception2, &v2, &tb2);
687 /* It should not be possible for exception2 or v2
688 to be NULL. However PyErr_Display() can't
689 tolerate NULLs, so just be safe. */
690 if (exception2 == NULL) {
691 exception2 = Py_None;
692 Py_INCREF(exception2);
693 }
694 if (v2 == NULL) {
695 v2 = Py_None;
696 Py_INCREF(v2);
697 }
698 fflush(stdout);
699 PySys_WriteStderr("Error in sys.excepthook:\n");
700 PyErr_Display(exception2, v2, tb2);
701 PySys_WriteStderr("\nOriginal exception was:\n");
702 PyErr_Display(exception, v, tb);
703 Py_DECREF(exception2);
704 Py_DECREF(v2);
705 Py_XDECREF(tb2);
706 }
707 Py_XDECREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 } else {
709 PySys_WriteStderr("sys.excepthook is missing\n");
710 PyErr_Display(exception, v, tb);
711 }
712 Py_XDECREF(exception);
713 Py_XDECREF(v);
714 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000715}
716
Benjamin Petersone6528212008-07-15 15:32:09 +0000717static void
718print_exception(PyObject *f, PyObject *value)
719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 int err = 0;
721 PyObject *type, *tb;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200722 _Py_IDENTIFIER(print_file_and_line);
Benjamin Petersone6528212008-07-15 15:32:09 +0000723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 if (!PyExceptionInstance_Check(value)) {
Victor Stinner52ce3b02013-12-09 02:10:08 +0100725 err = PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
726 err += PyFile_WriteString(Py_TYPE(value)->tp_name, f);
727 err += PyFile_WriteString(" found\n", f);
728 if (err)
729 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 return;
731 }
Benjamin Peterson26582602008-08-23 20:08:07 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 Py_INCREF(value);
734 fflush(stdout);
735 type = (PyObject *) Py_TYPE(value);
736 tb = PyException_GetTraceback(value);
737 if (tb && tb != Py_None)
738 err = PyTraceBack_Print(tb, f);
739 if (err == 0 &&
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200740 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100742 PyObject *message, *filename, *text;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 int lineno, offset;
744 if (!parse_syntax_error(value, &message, &filename,
745 &lineno, &offset, &text))
746 PyErr_Clear();
747 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100748 PyObject *line;
749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 Py_DECREF(value);
751 value = message;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100752
753 line = PyUnicode_FromFormat(" File \"%U\", line %d\n",
754 filename, lineno);
755 Py_DECREF(filename);
756 if (line != NULL) {
757 PyFile_WriteObject(line, f, Py_PRINT_RAW);
758 Py_DECREF(line);
759 }
760
761 if (text != NULL) {
762 print_error_text(f, offset, text);
763 Py_DECREF(text);
764 }
765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 /* Can't be bothered to check all those
767 PyFile_WriteString() calls */
768 if (PyErr_Occurred())
769 err = -1;
770 }
771 }
772 if (err) {
773 /* Don't do anything else */
774 }
775 else {
776 PyObject* moduleName;
777 char* className;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200778 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 assert(PyExceptionClass_Check(type));
780 className = PyExceptionClass_Name(type);
781 if (className != NULL) {
782 char *dot = strrchr(className, '.');
783 if (dot != NULL)
784 className = dot+1;
785 }
Benjamin Petersone6528212008-07-15 15:32:09 +0000786
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200787 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (moduleName == NULL || !PyUnicode_Check(moduleName))
789 {
Victor Stinner13b21bd2011-05-26 14:25:13 +0200790 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 err = PyFile_WriteString("<unknown>", f);
792 }
793 else {
Serhiy Storchakaf5894dd2016-11-16 15:40:39 +0200794 if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 {
Victor Stinner937114f2013-11-07 00:12:30 +0100796 err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 err += PyFile_WriteString(".", f);
798 }
799 Py_DECREF(moduleName);
800 }
801 if (err == 0) {
802 if (className == NULL)
803 err = PyFile_WriteString("<unknown>", f);
804 else
805 err = PyFile_WriteString(className, f);
806 }
807 }
808 if (err == 0 && (value != Py_None)) {
809 PyObject *s = PyObject_Str(value);
810 /* only print colon if the str() of the
811 object is not the empty string
812 */
Martin Panter3263f682016-02-28 03:16:11 +0000813 if (s == NULL) {
814 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 err = -1;
Martin Panter3263f682016-02-28 03:16:11 +0000816 PyFile_WriteString(": <exception str() failed>", f);
817 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 else if (!PyUnicode_Check(s) ||
Victor Stinnere251d6d2011-11-20 19:20:00 +0100819 PyUnicode_GetLength(s) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 err = PyFile_WriteString(": ", f);
821 if (err == 0)
822 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
823 Py_XDECREF(s);
824 }
825 /* try to write a newline in any case */
Martin Panter3263f682016-02-28 03:16:11 +0000826 if (err < 0) {
827 PyErr_Clear();
828 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 err += PyFile_WriteString("\n", f);
830 Py_XDECREF(tb);
831 Py_DECREF(value);
832 /* If an error happened here, don't show it.
833 XXX This is wrong, but too many callers rely on this behavior. */
834 if (err != 0)
835 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +0000836}
837
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200838static const char cause_message[] =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 "\nThe above exception was the direct cause "
840 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +0000841
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200842static const char context_message[] =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 "\nDuring handling of the above exception, "
844 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +0000845
846static void
847print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 int err = 0, res;
850 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 if (seen != NULL) {
853 /* Exception chaining */
Zane Bitterde860732017-10-17 17:29:39 -0400854 PyObject *value_id = PyLong_FromVoidPtr(value);
855 if (value_id == NULL || PySet_Add(seen, value_id) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 PyErr_Clear();
857 else if (PyExceptionInstance_Check(value)) {
Zane Bitterde860732017-10-17 17:29:39 -0400858 PyObject *check_id = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 cause = PyException_GetCause(value);
860 context = PyException_GetContext(value);
Benjamin Petersond5a1c442012-05-14 22:09:31 -0700861 if (cause) {
Zane Bitterde860732017-10-17 17:29:39 -0400862 check_id = PyLong_FromVoidPtr(cause);
863 if (check_id == NULL) {
864 res = -1;
865 } else {
866 res = PySet_Contains(seen, check_id);
867 Py_DECREF(check_id);
868 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 if (res == -1)
870 PyErr_Clear();
871 if (res == 0) {
872 print_exception_recursive(
873 f, cause, seen);
874 err |= PyFile_WriteString(
875 cause_message, f);
876 }
877 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -0700878 else if (context &&
879 !((PyBaseExceptionObject *)value)->suppress_context) {
Zane Bitterde860732017-10-17 17:29:39 -0400880 check_id = PyLong_FromVoidPtr(context);
881 if (check_id == NULL) {
882 res = -1;
883 } else {
884 res = PySet_Contains(seen, check_id);
885 Py_DECREF(check_id);
886 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 if (res == -1)
888 PyErr_Clear();
889 if (res == 0) {
890 print_exception_recursive(
891 f, context, seen);
892 err |= PyFile_WriteString(
893 context_message, f);
894 }
895 }
896 Py_XDECREF(context);
897 Py_XDECREF(cause);
898 }
Zane Bitterde860732017-10-17 17:29:39 -0400899 Py_XDECREF(value_id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 }
901 print_exception(f, value);
902 if (err != 0)
903 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +0000904}
905
Thomas Wouters477c8d52006-05-27 19:21:47 +0000906void
907PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 PyObject *seen;
Victor Stinnerbd303c12013-11-07 23:07:29 +0100910 PyObject *f = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrou24201d42013-10-13 21:53:13 +0200911 if (PyExceptionInstance_Check(value)
912 && tb != NULL && PyTraceBack_Check(tb)) {
913 /* Put the traceback on the exception, otherwise it won't get
914 displayed. See issue #18776. */
915 PyObject *cur_tb = PyException_GetTraceback(value);
916 if (cur_tb == NULL)
917 PyException_SetTraceback(value, tb);
918 else
919 Py_DECREF(cur_tb);
920 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 if (f == Py_None) {
922 /* pass */
923 }
924 else if (f == NULL) {
925 _PyObject_Dump(value);
926 fprintf(stderr, "lost sys.stderr\n");
927 }
928 else {
929 /* We choose to ignore seen being possibly NULL, and report
930 at least the main exception (it could be a MemoryError).
931 */
932 seen = PySet_New(NULL);
933 if (seen == NULL)
934 PyErr_Clear();
935 print_exception_recursive(f, value, seen);
936 Py_XDECREF(seen);
937 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000938}
939
Guido van Rossum82598051997-03-05 00:20:32 +0000940PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000941PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 PyObject *ret = NULL;
945 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +0100946 PyArena *arena;
Victor Stinner95701bd2013-11-06 18:41:07 +0100947 PyObject *filename;
948
949 filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
950 if (filename == NULL)
951 return NULL;
952
953 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 if (arena == NULL)
955 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000956
Victor Stinner95701bd2013-11-06 18:41:07 +0100957 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (mod != NULL)
Victor Stinner95701bd2013-11-06 18:41:07 +0100959 ret = run_mod(mod, filename, globals, locals, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 PyArena_Free(arena);
961 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000962}
963
964PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +0100965PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000967{
Victor Stinner95701bd2013-11-06 18:41:07 +0100968 PyObject *ret = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +0100970 PyArena *arena = NULL;
971 PyObject *filename;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000972
Victor Stinner95701bd2013-11-06 18:41:07 +0100973 filename = PyUnicode_DecodeFSDefault(filename_str);
974 if (filename == NULL)
975 goto exit;
976
977 arena = PyArena_New();
978 if (arena == NULL)
979 goto exit;
980
981 mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
982 flags, NULL, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 if (closeit)
984 fclose(fp);
985 if (mod == NULL) {
Victor Stinner95701bd2013-11-06 18:41:07 +0100986 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 }
988 ret = run_mod(mod, filename, globals, locals, flags, arena);
Victor Stinner95701bd2013-11-06 18:41:07 +0100989
990exit:
991 Py_XDECREF(filename);
992 if (arena != NULL)
993 PyArena_Free(arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000995}
996
Guido van Rossum6c193fa2007-12-05 05:14:58 +0000997static void
998flush_io(void)
999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 PyObject *f, *r;
1001 PyObject *type, *value, *traceback;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 /* Save the current exception */
1004 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001005
Victor Stinnerbd303c12013-11-07 23:07:29 +01001006 f = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 if (f != NULL) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001008 r = _PyObject_CallMethodId(f, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 if (r)
1010 Py_DECREF(r);
1011 else
1012 PyErr_Clear();
1013 }
Victor Stinnerbd303c12013-11-07 23:07:29 +01001014 f = _PySys_GetObjectId(&PyId_stdout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 if (f != NULL) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001016 r = _PyObject_CallMethodId(f, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 if (r)
1018 Py_DECREF(r);
1019 else
1020 PyErr_Clear();
1021 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001024}
1025
Guido van Rossum82598051997-03-05 00:20:32 +00001026static PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +01001027run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
1028 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PyCodeObject *co;
1031 PyObject *v;
Victor Stinner95701bd2013-11-06 18:41:07 +01001032 co = PyAST_CompileObject(mod, filename, flags, -1, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 if (co == NULL)
1034 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001035 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 Py_DECREF(co);
1037 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001038}
1039
Guido van Rossum82598051997-03-05 00:20:32 +00001040static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001041run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 PyCodeObject *co;
1045 PyObject *v;
1046 long magic;
1047 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 magic = PyMarshal_ReadLongFromFile(fp);
1050 if (magic != PyImport_GetMagicNumber()) {
Victor Stinner5200f552015-03-18 13:56:25 +01001051 if (!PyErr_Occurred())
1052 PyErr_SetString(PyExc_RuntimeError,
1053 "Bad magic number in .pyc file");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 return NULL;
1055 }
Benjamin Peterson42aa93b2017-12-09 10:26:52 -08001056 /* Skip the rest of the header. */
1057 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001058 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 (void) PyMarshal_ReadLongFromFile(fp);
Victor Stinner5200f552015-03-18 13:56:25 +01001060 if (PyErr_Occurred())
1061 return NULL;
1062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 v = PyMarshal_ReadLastObjectFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 if (v == NULL || !PyCode_Check(v)) {
1065 Py_XDECREF(v);
1066 PyErr_SetString(PyExc_RuntimeError,
1067 "Bad code object in .pyc file");
1068 return NULL;
1069 }
1070 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001071 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 if (v && flags)
1073 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1074 Py_DECREF(co);
1075 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001076}
1077
Guido van Rossum82598051997-03-05 00:20:32 +00001078PyObject *
Victor Stinner14e461d2013-08-26 22:28:21 +02001079Py_CompileStringObject(const char *str, PyObject *filename, int start,
1080 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 PyCodeObject *co;
1083 mod_ty mod;
1084 PyArena *arena = PyArena_New();
1085 if (arena == NULL)
1086 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001087
Victor Stinner14e461d2013-08-26 22:28:21 +02001088 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 if (mod == NULL) {
1090 PyArena_Free(arena);
1091 return NULL;
1092 }
1093 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1094 PyObject *result = PyAST_mod2obj(mod);
1095 PyArena_Free(arena);
1096 return result;
1097 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001098 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 PyArena_Free(arena);
1100 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001101}
1102
Victor Stinner14e461d2013-08-26 22:28:21 +02001103PyObject *
1104Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
1105 PyCompilerFlags *flags, int optimize)
1106{
1107 PyObject *filename, *co;
1108 filename = PyUnicode_DecodeFSDefault(filename_str);
1109 if (filename == NULL)
1110 return NULL;
1111 co = Py_CompileStringObject(str, filename, start, flags, optimize);
1112 Py_DECREF(filename);
1113 return co;
1114}
1115
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001116/* For use in Py_LIMITED_API */
1117#undef Py_CompileString
1118PyObject *
1119PyCompileString(const char *str, const char *filename, int start)
1120{
1121 return Py_CompileStringFlags(str, filename, start, NULL);
1122}
1123
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001124struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +02001125Py_SymtableStringObject(const char *str, PyObject *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 struct symtable *st;
1128 mod_ty mod;
1129 PyCompilerFlags flags;
Victor Stinner14e461d2013-08-26 22:28:21 +02001130 PyArena *arena;
1131
1132 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 if (arena == NULL)
1134 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 flags.cf_flags = 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02001137 mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 if (mod == NULL) {
1139 PyArena_Free(arena);
1140 return NULL;
1141 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001142 st = PySymtable_BuildObject(mod, filename, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 PyArena_Free(arena);
1144 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001145}
1146
Victor Stinner14e461d2013-08-26 22:28:21 +02001147struct symtable *
1148Py_SymtableString(const char *str, const char *filename_str, int start)
1149{
1150 PyObject *filename;
1151 struct symtable *st;
1152
1153 filename = PyUnicode_DecodeFSDefault(filename_str);
1154 if (filename == NULL)
1155 return NULL;
1156 st = Py_SymtableStringObject(str, filename, start);
1157 Py_DECREF(filename);
1158 return st;
1159}
1160
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161/* Preferred access to parser is through AST. */
1162mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02001163PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
1164 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 mod_ty mod;
1167 PyCompilerFlags localflags;
1168 perrdetail err;
1169 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001170
Victor Stinner14e461d2013-08-26 22:28:21 +02001171 node *n = PyParser_ParseStringObject(s, filename,
1172 &_PyParser_Grammar, start, &err,
1173 &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 if (flags == NULL) {
1175 localflags.cf_flags = 0;
1176 flags = &localflags;
1177 }
1178 if (n) {
1179 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02001180 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 }
1183 else {
1184 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001185 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001187 err_free(&err);
1188 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189}
1190
1191mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02001192PyParser_ASTFromString(const char *s, const char *filename_str, int start,
1193 PyCompilerFlags *flags, PyArena *arena)
1194{
1195 PyObject *filename;
1196 mod_ty mod;
1197 filename = PyUnicode_DecodeFSDefault(filename_str);
1198 if (filename == NULL)
1199 return NULL;
1200 mod = PyParser_ASTFromStringObject(s, filename, start, flags, arena);
1201 Py_DECREF(filename);
1202 return mod;
1203}
1204
1205mod_ty
1206PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001207 int start, const char *ps1,
1208 const char *ps2, PyCompilerFlags *flags, int *errcode,
Victor Stinner14e461d2013-08-26 22:28:21 +02001209 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 mod_ty mod;
1212 PyCompilerFlags localflags;
1213 perrdetail err;
1214 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001215
Victor Stinner14e461d2013-08-26 22:28:21 +02001216 node *n = PyParser_ParseFileObject(fp, filename, enc,
1217 &_PyParser_Grammar,
1218 start, ps1, ps2, &err, &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (flags == NULL) {
1220 localflags.cf_flags = 0;
1221 flags = &localflags;
1222 }
1223 if (n) {
1224 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02001225 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 }
1228 else {
1229 err_input(&err);
1230 if (errcode)
1231 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02001232 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001234 err_free(&err);
1235 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236}
1237
Victor Stinner14e461d2013-08-26 22:28:21 +02001238mod_ty
1239PyParser_ASTFromFile(FILE *fp, const char *filename_str, const char* enc,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001240 int start, const char *ps1,
1241 const char *ps2, PyCompilerFlags *flags, int *errcode,
Victor Stinner14e461d2013-08-26 22:28:21 +02001242 PyArena *arena)
1243{
1244 mod_ty mod;
1245 PyObject *filename;
1246 filename = PyUnicode_DecodeFSDefault(filename_str);
1247 if (filename == NULL)
1248 return NULL;
1249 mod = PyParser_ASTFromFileObject(fp, filename, enc, start, ps1, ps2,
1250 flags, errcode, arena);
1251 Py_DECREF(filename);
1252 return mod;
1253}
1254
Guido van Rossuma110aa61994-08-29 12:50:44 +00001255/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001256
Guido van Rossuma110aa61994-08-29 12:50:44 +00001257node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001258PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 perrdetail err;
1261 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1262 &_PyParser_Grammar,
1263 start, NULL, NULL, &err, flags);
1264 if (n == NULL)
1265 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001266 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001269}
1270
Guido van Rossuma110aa61994-08-29 12:50:44 +00001271/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001272
Guido van Rossuma110aa61994-08-29 12:50:44 +00001273node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001274PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 perrdetail err;
1277 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1278 start, &err, flags);
1279 if (n == NULL)
1280 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001281 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001283}
1284
1285node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001286PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 perrdetail err;
1290 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1291 &_PyParser_Grammar, start, &err, flags);
1292 if (n == NULL)
1293 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001294 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001296}
1297
1298node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001299PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001302}
1303
Guido van Rossum66ebd912003-04-17 16:02:26 +00001304/* May want to move a more generalized form of this to parsetok.c or
1305 even parser modules. */
1306
1307void
Victor Stinner7f2fee32011-04-05 00:39:01 +02001308PyParser_ClearError(perrdetail *err)
1309{
1310 err_free(err);
1311}
1312
1313void
Guido van Rossum66ebd912003-04-17 16:02:26 +00001314PyParser_SetError(perrdetail *err)
1315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00001317}
1318
Victor Stinner7f2fee32011-04-05 00:39:01 +02001319static void
1320err_free(perrdetail *err)
1321{
1322 Py_CLEAR(err->filename);
1323}
1324
Guido van Rossuma110aa61994-08-29 12:50:44 +00001325/* Set the error appropriate to the given input error code (see errcode.h) */
1326
1327static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001328err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 PyObject *v, *w, *errtype, *errtext;
1331 PyObject *msg_obj = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001332 const char *msg = NULL;
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001333 int offset = err->offset;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 errtype = PyExc_SyntaxError;
1336 switch (err->error) {
1337 case E_ERROR:
1338 return;
1339 case E_SYNTAX:
1340 errtype = PyExc_IndentationError;
1341 if (err->expected == INDENT)
1342 msg = "expected an indented block";
1343 else if (err->token == INDENT)
1344 msg = "unexpected indent";
1345 else if (err->token == DEDENT)
1346 msg = "unexpected unindent";
1347 else {
1348 errtype = PyExc_SyntaxError;
1349 msg = "invalid syntax";
1350 }
1351 break;
1352 case E_TOKEN:
1353 msg = "invalid token";
1354 break;
1355 case E_EOFS:
1356 msg = "EOF while scanning triple-quoted string literal";
1357 break;
1358 case E_EOLS:
1359 msg = "EOL while scanning string literal";
1360 break;
1361 case E_INTR:
1362 if (!PyErr_Occurred())
1363 PyErr_SetNone(PyExc_KeyboardInterrupt);
1364 goto cleanup;
1365 case E_NOMEM:
1366 PyErr_NoMemory();
1367 goto cleanup;
1368 case E_EOF:
1369 msg = "unexpected EOF while parsing";
1370 break;
1371 case E_TABSPACE:
1372 errtype = PyExc_TabError;
1373 msg = "inconsistent use of tabs and spaces in indentation";
1374 break;
1375 case E_OVERFLOW:
1376 msg = "expression too long";
1377 break;
1378 case E_DEDENT:
1379 errtype = PyExc_IndentationError;
1380 msg = "unindent does not match any outer indentation level";
1381 break;
1382 case E_TOODEEP:
1383 errtype = PyExc_IndentationError;
1384 msg = "too many levels of indentation";
1385 break;
1386 case E_DECODE: {
1387 PyObject *type, *value, *tb;
1388 PyErr_Fetch(&type, &value, &tb);
1389 msg = "unknown decode error";
1390 if (value != NULL)
1391 msg_obj = PyObject_Str(value);
1392 Py_XDECREF(type);
1393 Py_XDECREF(value);
1394 Py_XDECREF(tb);
1395 break;
1396 }
1397 case E_LINECONT:
1398 msg = "unexpected character after line continuation character";
1399 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 case E_IDENTIFIER:
1402 msg = "invalid character in identifier";
1403 break;
Meador Ingefa21bf02012-01-19 01:08:41 -06001404 case E_BADSINGLE:
1405 msg = "multiple statements found while compiling a single statement";
1406 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 default:
1408 fprintf(stderr, "error=%d\n", err->error);
1409 msg = "unknown parsing error";
1410 break;
1411 }
1412 /* err->text may not be UTF-8 in case of decoding errors.
1413 Explicitly convert to an object. */
1414 if (!err->text) {
1415 errtext = Py_None;
1416 Py_INCREF(Py_None);
1417 } else {
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001418 errtext = PyUnicode_DecodeUTF8(err->text, err->offset,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 "replace");
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001420 if (errtext != NULL) {
1421 Py_ssize_t len = strlen(err->text);
1422 offset = (int)PyUnicode_GET_LENGTH(errtext);
1423 if (len != err->offset) {
1424 Py_DECREF(errtext);
1425 errtext = PyUnicode_DecodeUTF8(err->text, len,
1426 "replace");
1427 }
1428 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001430 v = Py_BuildValue("(OiiN)", err->filename,
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001431 err->lineno, offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 if (v != NULL) {
1433 if (msg_obj)
1434 w = Py_BuildValue("(OO)", msg_obj, v);
1435 else
1436 w = Py_BuildValue("(sO)", msg, v);
1437 } else
1438 w = NULL;
1439 Py_XDECREF(v);
1440 PyErr_SetObject(errtype, w);
1441 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00001442cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 Py_XDECREF(msg_obj);
1444 if (err->text != NULL) {
1445 PyObject_FREE(err->text);
1446 err->text = NULL;
1447 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001448}
1449
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001450
Zachary Warec4821d62014-11-21 23:35:12 -06001451#if defined(USE_STACKCHECK)
1452#if defined(WIN32) && defined(_MSC_VER)
1453
1454/* Stack checking for Microsoft C */
1455
1456#include <malloc.h>
1457#include <excpt.h>
1458
1459/*
1460 * Return non-zero when we run out of memory on the stack; zero otherwise.
1461 */
1462int
1463PyOS_CheckStack(void)
1464{
1465 __try {
1466 /* alloca throws a stack overflow exception if there's
1467 not enough space left on the stack */
1468 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1469 return 0;
1470 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1471 EXCEPTION_EXECUTE_HANDLER :
1472 EXCEPTION_CONTINUE_SEARCH) {
1473 int errcode = _resetstkoflw();
1474 if (errcode == 0)
1475 {
1476 Py_FatalError("Could not reset the stack!");
1477 }
1478 }
1479 return 1;
1480}
1481
1482#endif /* WIN32 && _MSC_VER */
1483
1484/* Alternate implementations can be added here... */
1485
1486#endif /* USE_STACKCHECK */
1487
Martin Panter46f50722016-05-26 05:35:26 +00001488/* Deprecated C API functions still provided for binary compatibility */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489
1490#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001491PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495}
1496
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001497#undef PyParser_SimpleParseString
1498PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499PyParser_SimpleParseString(const char *str, int start)
1500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001503
1504#undef PyRun_AnyFile
1505PyAPI_FUNC(int)
1506PyRun_AnyFile(FILE *fp, const char *name)
1507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001509}
1510
1511#undef PyRun_AnyFileEx
1512PyAPI_FUNC(int)
1513PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001516}
1517
1518#undef PyRun_AnyFileFlags
1519PyAPI_FUNC(int)
1520PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001523}
1524
1525#undef PyRun_File
1526PyAPI_FUNC(PyObject *)
1527PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001530}
1531
1532#undef PyRun_FileEx
1533PyAPI_FUNC(PyObject *)
1534PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001537}
1538
1539#undef PyRun_FileFlags
1540PyAPI_FUNC(PyObject *)
1541PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001545}
1546
1547#undef PyRun_SimpleFile
1548PyAPI_FUNC(int)
1549PyRun_SimpleFile(FILE *f, const char *p)
1550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001552}
1553
1554#undef PyRun_SimpleFileEx
1555PyAPI_FUNC(int)
1556PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001559}
1560
1561
1562#undef PyRun_String
1563PyAPI_FUNC(PyObject *)
1564PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001567}
1568
1569#undef PyRun_SimpleString
1570PyAPI_FUNC(int)
1571PyRun_SimpleString(const char *s)
1572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001574}
1575
1576#undef Py_CompileString
1577PyAPI_FUNC(PyObject *)
1578Py_CompileString(const char *str, const char *p, int s)
1579{
Georg Brandl8334fd92010-12-04 10:26:46 +00001580 return Py_CompileStringExFlags(str, p, s, NULL, -1);
1581}
1582
1583#undef Py_CompileStringFlags
1584PyAPI_FUNC(PyObject *)
1585Py_CompileStringFlags(const char *str, const char *p, int s,
1586 PyCompilerFlags *flags)
1587{
1588 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001589}
1590
1591#undef PyRun_InteractiveOne
1592PyAPI_FUNC(int)
1593PyRun_InteractiveOne(FILE *f, const char *p)
1594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001596}
1597
1598#undef PyRun_InteractiveLoop
1599PyAPI_FUNC(int)
1600PyRun_InteractiveLoop(FILE *f, const char *p)
1601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001603}
1604
1605#ifdef __cplusplus
1606}
1607#endif