blob: ebedd123f39516a159a196915eb4ba612c8399db [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Guido van Rossumd8faa362007-04-27 19:54:29 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00008#include "grammar.h"
9#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000010#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000014#include "symtable.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015#include "ast.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000016#include "marshal.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000017#include "osdefs.h"
Victor Stinner518e6102014-03-18 02:06:38 +010018#include <locale.h>
Guido van Rossum1984f1e1992-08-04 12:41:02 +000019
Thomas Wouters0e3f5912006-08-11 14:57:12 +000020#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000021#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000022#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000023
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000024#ifdef MS_WINDOWS
Martin v. Löwis5c88d812009-01-02 20:47:48 +000025#include "malloc.h" /* for alloca */
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000026#endif
Martin v. Löwis5c88d812009-01-02 20:47:48 +000027
Martin v. Löwis73d538b2003-03-05 15:13:47 +000028#ifdef HAVE_LANGINFO_H
Martin v. Löwis73d538b2003-03-05 15:13:47 +000029#include <langinfo.h>
30#endif
31
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000032#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000033#undef BYTE
34#include "windows.h"
35#endif
36
Victor Stinnerbd303c12013-11-07 23:07:29 +010037_Py_IDENTIFIER(builtins);
Victor Stinner09054372013-11-06 22:41:44 +010038_Py_IDENTIFIER(excepthook);
Victor Stinner3f36a572013-11-12 21:39:02 +010039_Py_IDENTIFIER(flush);
Victor Stinnerbd303c12013-11-07 23:07:29 +010040_Py_IDENTIFIER(last_traceback);
Victor Stinner09054372013-11-06 22:41:44 +010041_Py_IDENTIFIER(last_type);
42_Py_IDENTIFIER(last_value);
Victor Stinnerbd303c12013-11-07 23:07:29 +010043_Py_IDENTIFIER(ps1);
44_Py_IDENTIFIER(ps2);
45_Py_IDENTIFIER(stdin);
46_Py_IDENTIFIER(stdout);
47_Py_IDENTIFIER(stderr);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +010048_Py_static_string(PyId_string, "<string>");
Victor Stinner09054372013-11-06 22:41:44 +010049
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000050#ifdef __cplusplus
51extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000052#endif
53
Guido van Rossum82598051997-03-05 00:20:32 +000054extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000055
Guido van Rossumb73cc041993-11-01 16:28:59 +000056/* Forward */
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000057static void flush_io(void);
Victor Stinner95701bd2013-11-06 18:41:07 +010058static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000060static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000062static void err_input(perrdetail *);
Victor Stinner7f2fee32011-04-05 00:39:01 +020063static void err_free(perrdetail *);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000064
Guido van Rossum1984f1e1992-08-04 12:41:02 +000065/* Parse input from a file and execute it */
66
67int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000068PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +000070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (filename == NULL)
72 filename = "???";
73 if (Py_FdIsInteractive(fp, filename)) {
74 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
75 if (closeit)
76 fclose(fp);
77 return err;
78 }
79 else
80 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000081}
82
83int
Victor Stinner95701bd2013-11-06 18:41:07 +010084PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +000085{
Victor Stinner95701bd2013-11-06 18:41:07 +010086 PyObject *filename, *v;
87 int ret, err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +000089
Victor Stinner95701bd2013-11-06 18:41:07 +010090 filename = PyUnicode_DecodeFSDefault(filename_str);
91 if (filename == NULL) {
92 PyErr_Print();
93 return -1;
94 }
95
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (flags == NULL) {
97 flags = &local_flags;
98 local_flags.cf_flags = 0;
99 }
Victor Stinner09054372013-11-06 22:41:44 +0100100 v = _PySys_GetObjectId(&PyId_ps1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 if (v == NULL) {
Victor Stinner09054372013-11-06 22:41:44 +0100102 _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> "));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 Py_XDECREF(v);
104 }
Victor Stinner09054372013-11-06 22:41:44 +0100105 v = _PySys_GetObjectId(&PyId_ps2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 if (v == NULL) {
Victor Stinner09054372013-11-06 22:41:44 +0100107 _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... "));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 Py_XDECREF(v);
109 }
Victor Stinner95701bd2013-11-06 18:41:07 +0100110 err = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 for (;;) {
Victor Stinner95701bd2013-11-06 18:41:07 +0100112 ret = PyRun_InteractiveOneObject(fp, filename, flags);
Nick Coghland6009512014-11-20 21:39:37 +1000113 _PY_DEBUG_PRINT_TOTAL_REFS();
Victor Stinner95701bd2013-11-06 18:41:07 +0100114 if (ret == E_EOF) {
115 err = 0;
116 break;
117 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 /*
119 if (ret == E_NOMEM)
Victor Stinner95701bd2013-11-06 18:41:07 +0100120 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 */
122 }
Victor Stinner95701bd2013-11-06 18:41:07 +0100123 Py_DECREF(filename);
124 return err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000125}
126
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000127/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000128static int PARSER_FLAGS(PyCompilerFlags *flags)
129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 int parser_flags = 0;
131 if (!flags)
132 return 0;
133 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
134 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
135 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
136 parser_flags |= PyPARSE_IGNORE_COOKIE;
137 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
138 parser_flags |= PyPARSE_BARRY_AS_BDFL;
139 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000140}
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000141
Thomas Wouters89f507f2006-12-13 04:49:30 +0000142#if 0
143/* Keep an example of flags with future keyword support. */
144#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
146 PyPARSE_DONT_IMPLY_DEDENT : 0) \
147 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
148 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000149#endif
150
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000151int
Victor Stinner95701bd2013-11-06 18:41:07 +0100152PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000153{
Victor Stinner95701bd2013-11-06 18:41:07 +0100154 PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 mod_ty mod;
156 PyArena *arena;
157 char *ps1 = "", *ps2 = "", *enc = NULL;
158 int errcode = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200159 _Py_IDENTIFIER(encoding);
Victor Stinner95701bd2013-11-06 18:41:07 +0100160 _Py_IDENTIFIER(__main__);
161
162 mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */
163 if (mod_name == NULL) {
164 PyErr_Print();
165 return -1;
166 }
Tim Petersfe2127d2001-07-16 05:37:24 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if (fp == stdin) {
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -0400169 /* Fetch encoding from sys.stdin if possible. */
Victor Stinnerbd303c12013-11-07 23:07:29 +0100170 v = _PySys_GetObjectId(&PyId_stdin);
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -0400171 if (v && v != Py_None) {
172 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
173 if (oenc)
174 enc = _PyUnicode_AsString(oenc);
175 if (!enc)
176 PyErr_Clear();
177 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 }
Victor Stinner09054372013-11-06 22:41:44 +0100179 v = _PySys_GetObjectId(&PyId_ps1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 if (v != NULL) {
181 v = PyObject_Str(v);
182 if (v == NULL)
183 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +0000184 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +0000186 if (ps1 == NULL) {
187 PyErr_Clear();
188 ps1 = "";
189 }
190 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 }
Victor Stinner09054372013-11-06 22:41:44 +0100192 w = _PySys_GetObjectId(&PyId_ps2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (w != NULL) {
194 w = PyObject_Str(w);
195 if (w == NULL)
196 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +0000197 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +0000199 if (ps2 == NULL) {
200 PyErr_Clear();
201 ps2 = "";
202 }
203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 }
205 arena = PyArena_New();
206 if (arena == NULL) {
207 Py_XDECREF(v);
208 Py_XDECREF(w);
209 Py_XDECREF(oenc);
210 return -1;
211 }
Victor Stinner95701bd2013-11-06 18:41:07 +0100212 mod = PyParser_ASTFromFileObject(fp, filename, enc,
213 Py_single_input, ps1, ps2,
214 flags, &errcode, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 Py_XDECREF(v);
216 Py_XDECREF(w);
217 Py_XDECREF(oenc);
218 if (mod == NULL) {
219 PyArena_Free(arena);
220 if (errcode == E_EOF) {
221 PyErr_Clear();
222 return E_EOF;
223 }
224 PyErr_Print();
225 return -1;
226 }
Victor Stinner95701bd2013-11-06 18:41:07 +0100227 m = PyImport_AddModuleObject(mod_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (m == NULL) {
229 PyArena_Free(arena);
230 return -1;
231 }
232 d = PyModule_GetDict(m);
233 v = run_mod(mod, filename, d, d, flags, arena);
234 PyArena_Free(arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 if (v == NULL) {
236 PyErr_Print();
Antoine Pitrou9845c7e2014-05-11 13:42:17 +0200237 flush_io();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 return -1;
239 }
240 Py_DECREF(v);
Antoine Pitrou9845c7e2014-05-11 13:42:17 +0200241 flush_io();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000243}
244
Victor Stinner95701bd2013-11-06 18:41:07 +0100245int
246PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
247{
248 PyObject *filename;
249 int res;
250
251 filename = PyUnicode_DecodeFSDefault(filename_str);
252 if (filename == NULL) {
253 PyErr_Print();
254 return -1;
255 }
256 res = PyRun_InteractiveOneObject(fp, filename, flags);
257 Py_DECREF(filename);
258 return res;
259}
260
261
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000262/* Check whether a file maybe a pyc file: Look at the extension,
263 the file type, and, if we may close it, at the first few bytes. */
264
265static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000266maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000267{
Brett Cannonf299abd2015-04-13 14:21:02 -0400268 if (strcmp(ext, ".pyc") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* Only look into the file if we are allowed to close it, since
272 it then should also be seekable. */
273 if (closeit) {
274 /* Read only two bytes of the magic. If the file was opened in
275 text mode, the bytes 3 and 4 of the magic (\r\n) might not
276 be read as they are on disk. */
277 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
278 unsigned char buf[2];
279 /* Mess: In case of -x, the stream is NOT at its start now,
280 and ungetc() was used to push back the first newline,
281 which makes the current stream position formally undefined,
282 and a x-platform nightmare.
283 Unfortunately, we have no direct way to know whether -x
284 was specified. So we use a terrible hack: if the current
285 stream position is not 0, we assume -x was specified, and
286 give up. Bug 132850 on SourceForge spells out the
287 hopelessness of trying anything else (fseek and ftell
288 don't work predictably x-platform for text-mode files).
289 */
290 int ispyc = 0;
291 if (ftell(fp) == 0) {
292 if (fread(buf, 1, 2, fp) == 2 &&
293 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
294 ispyc = 1;
295 rewind(fp);
296 }
297 return ispyc;
298 }
299 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000300}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000301
Antoine Pitrou32d483c2013-07-30 21:01:23 +0200302static int
303set_main_loader(PyObject *d, const char *filename, const char *loader_name)
Nick Coghlan85e729e2012-07-15 18:09:52 +1000304{
305 PyInterpreterState *interp;
306 PyThreadState *tstate;
Eric Snow32439d62015-05-02 19:15:18 -0600307 PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader;
Nick Coghlanb7a58942012-07-15 23:21:08 +1000308 int result = 0;
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200309
310 filename_obj = PyUnicode_DecodeFSDefault(filename);
311 if (filename_obj == NULL)
312 return -1;
Nick Coghlan85e729e2012-07-15 18:09:52 +1000313 /* Get current thread state and interpreter pointer */
314 tstate = PyThreadState_GET();
315 interp = tstate->interp;
Eric Snow32439d62015-05-02 19:15:18 -0600316 bootstrap = PyObject_GetAttrString(interp->importlib,
317 "_bootstrap_external");
318 if (bootstrap != NULL) {
319 loader_type = PyObject_GetAttrString(bootstrap, loader_name);
320 Py_DECREF(bootstrap);
321 }
Nick Coghlan3f94cbf2012-07-15 19:10:39 +1000322 if (loader_type == NULL) {
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200323 Py_DECREF(filename_obj);
Nick Coghlan3f94cbf2012-07-15 19:10:39 +1000324 return -1;
325 }
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200326 loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
Nick Coghlanb7a58942012-07-15 23:21:08 +1000327 Py_DECREF(loader_type);
328 if (loader == NULL) {
Nick Coghlan85e729e2012-07-15 18:09:52 +1000329 return -1;
330 }
Nick Coghlanb7a58942012-07-15 23:21:08 +1000331 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
332 result = -1;
333 }
Nick Coghlan85e729e2012-07-15 18:09:52 +1000334 Py_DECREF(loader);
Nick Coghlanb7a58942012-07-15 23:21:08 +1000335 return result;
Nick Coghlan85e729e2012-07-15 18:09:52 +1000336}
337
338int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000339PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 PyObject *m, *d, *v;
343 const char *ext;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100344 int set_file_name = 0, ret = -1;
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000345 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 m = PyImport_AddModule("__main__");
348 if (m == NULL)
349 return -1;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100350 Py_INCREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 d = PyModule_GetDict(m);
352 if (PyDict_GetItemString(d, "__file__") == NULL) {
353 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000354 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 if (f == NULL)
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100356 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 if (PyDict_SetItemString(d, "__file__", f) < 0) {
358 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100359 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 }
Barry Warsaw916048d2011-09-20 14:45:44 -0400361 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
362 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100363 goto done;
Barry Warsaw916048d2011-09-20 14:45:44 -0400364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 set_file_name = 1;
366 Py_DECREF(f);
367 }
368 len = strlen(filename);
369 ext = filename + len - (len > 4 ? 4 : 0);
370 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Christian Heimes04ac4c12012-09-11 15:47:28 +0200371 FILE *pyc_fp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 /* Try to run a pyc file. First, re-open in binary */
373 if (closeit)
374 fclose(fp);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200375 if ((pyc_fp = _Py_fopen(filename, "rb")) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 fprintf(stderr, "python: Can't reopen .pyc file\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 goto done;
378 }
Nick Coghlan85e729e2012-07-15 18:09:52 +1000379
380 if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
381 fprintf(stderr, "python: failed to set __main__.__loader__\n");
382 ret = -1;
Christian Heimes04ac4c12012-09-11 15:47:28 +0200383 fclose(pyc_fp);
Nick Coghlan85e729e2012-07-15 18:09:52 +1000384 goto done;
385 }
Christian Heimes04ac4c12012-09-11 15:47:28 +0200386 v = run_pyc_file(pyc_fp, filename, d, d, flags);
387 fclose(pyc_fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 } else {
Nick Coghlan85e729e2012-07-15 18:09:52 +1000389 /* When running from stdin, leave __main__.__loader__ alone */
390 if (strcmp(filename, "<stdin>") != 0 &&
391 set_main_loader(d, filename, "SourceFileLoader") < 0) {
392 fprintf(stderr, "python: failed to set __main__.__loader__\n");
393 ret = -1;
394 goto done;
395 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
397 closeit, flags);
398 }
399 flush_io();
400 if (v == NULL) {
401 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 goto done;
403 }
404 Py_DECREF(v);
405 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000406 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if (set_file_name && PyDict_DelItemString(d, "__file__"))
408 PyErr_Clear();
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100409 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000411}
412
413int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000414PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 PyObject *m, *d, *v;
417 m = PyImport_AddModule("__main__");
418 if (m == NULL)
419 return -1;
420 d = PyModule_GetDict(m);
421 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
422 if (v == NULL) {
423 PyErr_Print();
424 return -1;
425 }
426 Py_DECREF(v);
427 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000428}
429
Barry Warsaw035574d1997-08-29 22:07:17 +0000430static int
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100431parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
432 int *lineno, int *offset, PyObject **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 long hold;
435 PyObject *v;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200436 _Py_IDENTIFIER(msg);
437 _Py_IDENTIFIER(filename);
438 _Py_IDENTIFIER(lineno);
439 _Py_IDENTIFIER(offset);
440 _Py_IDENTIFIER(text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000441
Benjamin Peterson80d50422012-04-03 00:30:38 -0400442 *message = NULL;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100443 *filename = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 /* new style errors. `err' is an instance */
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400446 *message = _PyObject_GetAttrId(err, &PyId_msg);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400447 if (!*message)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +0000449
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400450 v = _PyObject_GetAttrId(err, &PyId_filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400451 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400453 if (v == Py_None) {
454 Py_DECREF(v);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100455 *filename = _PyUnicode_FromId(&PyId_string);
456 if (*filename == NULL)
457 goto finally;
458 Py_INCREF(*filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400459 }
460 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100461 *filename = v;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400462 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000463
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400464 v = _PyObject_GetAttrId(err, &PyId_lineno);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400465 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 goto finally;
467 hold = PyLong_AsLong(v);
468 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 if (hold < 0 && PyErr_Occurred())
470 goto finally;
471 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +0000472
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400473 v = _PyObject_GetAttrId(err, &PyId_offset);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400474 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 goto finally;
476 if (v == Py_None) {
477 *offset = -1;
478 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 } else {
480 hold = PyLong_AsLong(v);
481 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (hold < 0 && PyErr_Occurred())
483 goto finally;
484 *offset = (int)hold;
485 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000486
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400487 v = _PyObject_GetAttrId(err, &PyId_text);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400488 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400490 if (v == Py_None) {
491 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 *text = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400493 }
494 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100495 *text = v;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400496 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +0000498
499finally:
Benjamin Peterson80d50422012-04-03 00:30:38 -0400500 Py_XDECREF(*message);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100501 Py_XDECREF(*filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +0000503}
504
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000505void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000506PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +0000509}
510
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000511static void
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100512print_error_text(PyObject *f, int offset, PyObject *text_obj)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000513{
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100514 char *text;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 char *nl;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100516
517 text = _PyUnicode_AsString(text_obj);
518 if (text == NULL)
519 return;
520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 if (offset >= 0) {
Victor Stinner98ea54c2014-08-15 23:30:40 +0200522 if (offset > 0 && (size_t)offset == strlen(text) && text[offset - 1] == '\n')
Benjamin Petersona95e9772010-10-29 03:28:14 +0000523 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 for (;;) {
525 nl = strchr(text, '\n');
526 if (nl == NULL || nl-text >= offset)
527 break;
528 offset -= (int)(nl+1-text);
529 text = nl+1;
530 }
531 while (*text == ' ' || *text == '\t') {
532 text++;
533 offset--;
534 }
535 }
536 PyFile_WriteString(" ", f);
537 PyFile_WriteString(text, f);
538 if (*text == '\0' || text[strlen(text)-1] != '\n')
539 PyFile_WriteString("\n", f);
540 if (offset == -1)
541 return;
542 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +0000543 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000546}
547
Guido van Rossum66e8e862001-03-23 17:54:43 +0000548static void
549handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 PyObject *exception, *value, *tb;
552 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (Py_InspectFlag)
555 /* Don't exit if -i flag was given. This flag is set to 0
556 * when entering interactive mode for inspecting. */
557 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 PyErr_Fetch(&exception, &value, &tb);
560 fflush(stdout);
561 if (value == NULL || value == Py_None)
562 goto done;
563 if (PyExceptionInstance_Check(value)) {
564 /* The error code should be in the `code' attribute. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200565 _Py_IDENTIFIER(code);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200566 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (code) {
568 Py_DECREF(value);
569 value = code;
570 if (value == Py_None)
571 goto done;
572 }
573 /* If we failed to dig out the 'code' attribute,
574 just let the else clause below print the error. */
575 }
576 if (PyLong_Check(value))
577 exitcode = (int)PyLong_AsLong(value);
578 else {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100579 PyObject *sys_stderr = _PySys_GetObjectId(&PyId_stderr);
Nick Coghland979e432014-02-09 10:43:21 +1000580 /* We clear the exception here to avoid triggering the assertion
581 * in PyObject_Str that ensures it won't silently lose exception
582 * details.
583 */
584 PyErr_Clear();
Victor Stinner7126dbc2010-05-21 23:45:42 +0000585 if (sys_stderr != NULL && sys_stderr != Py_None) {
586 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
587 } else {
588 PyObject_Print(value, stderr, Py_PRINT_RAW);
589 fflush(stderr);
590 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PySys_WriteStderr("\n");
592 exitcode = 1;
593 }
Tim Peterscf615b52003-04-19 18:47:02 +0000594 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 /* Restore and clear the exception info, in order to properly decref
596 * the exception, value, and traceback. If we just exit instead,
597 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
598 * some finalizers from running.
599 */
600 PyErr_Restore(exception, value, tb);
601 PyErr_Clear();
602 Py_Exit(exitcode);
603 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000604}
605
606void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000607PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
612 handle_system_exit();
613 }
614 PyErr_Fetch(&exception, &v, &tb);
615 if (exception == NULL)
616 return;
617 PyErr_NormalizeException(&exception, &v, &tb);
618 if (tb == NULL) {
619 tb = Py_None;
620 Py_INCREF(tb);
621 }
622 PyException_SetTraceback(v, tb);
623 if (exception == NULL)
624 return;
625 /* Now we know v != NULL too */
626 if (set_sys_last_vars) {
Victor Stinner09054372013-11-06 22:41:44 +0100627 _PySys_SetObjectId(&PyId_last_type, exception);
628 _PySys_SetObjectId(&PyId_last_value, v);
629 _PySys_SetObjectId(&PyId_last_traceback, tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 }
Victor Stinner09054372013-11-06 22:41:44 +0100631 hook = _PySys_GetObjectId(&PyId_excepthook);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 if (hook) {
633 PyObject *args = PyTuple_Pack(3, exception, v, tb);
634 PyObject *result = PyEval_CallObject(hook, args);
635 if (result == NULL) {
636 PyObject *exception2, *v2, *tb2;
637 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
638 handle_system_exit();
639 }
640 PyErr_Fetch(&exception2, &v2, &tb2);
641 PyErr_NormalizeException(&exception2, &v2, &tb2);
642 /* It should not be possible for exception2 or v2
643 to be NULL. However PyErr_Display() can't
644 tolerate NULLs, so just be safe. */
645 if (exception2 == NULL) {
646 exception2 = Py_None;
647 Py_INCREF(exception2);
648 }
649 if (v2 == NULL) {
650 v2 = Py_None;
651 Py_INCREF(v2);
652 }
653 fflush(stdout);
654 PySys_WriteStderr("Error in sys.excepthook:\n");
655 PyErr_Display(exception2, v2, tb2);
656 PySys_WriteStderr("\nOriginal exception was:\n");
657 PyErr_Display(exception, v, tb);
658 Py_DECREF(exception2);
659 Py_DECREF(v2);
660 Py_XDECREF(tb2);
661 }
662 Py_XDECREF(result);
663 Py_XDECREF(args);
664 } else {
665 PySys_WriteStderr("sys.excepthook is missing\n");
666 PyErr_Display(exception, v, tb);
667 }
668 Py_XDECREF(exception);
669 Py_XDECREF(v);
670 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000671}
672
Benjamin Petersone6528212008-07-15 15:32:09 +0000673static void
674print_exception(PyObject *f, PyObject *value)
675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 int err = 0;
677 PyObject *type, *tb;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200678 _Py_IDENTIFIER(print_file_and_line);
Benjamin Petersone6528212008-07-15 15:32:09 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 if (!PyExceptionInstance_Check(value)) {
Victor Stinner52ce3b02013-12-09 02:10:08 +0100681 err = PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
682 err += PyFile_WriteString(Py_TYPE(value)->tp_name, f);
683 err += PyFile_WriteString(" found\n", f);
684 if (err)
685 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 return;
687 }
Benjamin Peterson26582602008-08-23 20:08:07 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 Py_INCREF(value);
690 fflush(stdout);
691 type = (PyObject *) Py_TYPE(value);
692 tb = PyException_GetTraceback(value);
693 if (tb && tb != Py_None)
694 err = PyTraceBack_Print(tb, f);
695 if (err == 0 &&
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200696 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100698 PyObject *message, *filename, *text;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 int lineno, offset;
700 if (!parse_syntax_error(value, &message, &filename,
701 &lineno, &offset, &text))
702 PyErr_Clear();
703 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100704 PyObject *line;
705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 Py_DECREF(value);
707 value = message;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100708
709 line = PyUnicode_FromFormat(" File \"%U\", line %d\n",
710 filename, lineno);
711 Py_DECREF(filename);
712 if (line != NULL) {
713 PyFile_WriteObject(line, f, Py_PRINT_RAW);
714 Py_DECREF(line);
715 }
716
717 if (text != NULL) {
718 print_error_text(f, offset, text);
719 Py_DECREF(text);
720 }
721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 /* Can't be bothered to check all those
723 PyFile_WriteString() calls */
724 if (PyErr_Occurred())
725 err = -1;
726 }
727 }
728 if (err) {
729 /* Don't do anything else */
730 }
731 else {
732 PyObject* moduleName;
733 char* className;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200734 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 assert(PyExceptionClass_Check(type));
736 className = PyExceptionClass_Name(type);
737 if (className != NULL) {
738 char *dot = strrchr(className, '.');
739 if (dot != NULL)
740 className = dot+1;
741 }
Benjamin Petersone6528212008-07-15 15:32:09 +0000742
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200743 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 if (moduleName == NULL || !PyUnicode_Check(moduleName))
745 {
Victor Stinner13b21bd2011-05-26 14:25:13 +0200746 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 err = PyFile_WriteString("<unknown>", f);
748 }
749 else {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100750 if (_PyUnicode_CompareWithId(moduleName, &PyId_builtins) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 {
Victor Stinner937114f2013-11-07 00:12:30 +0100752 err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 err += PyFile_WriteString(".", f);
754 }
755 Py_DECREF(moduleName);
756 }
757 if (err == 0) {
758 if (className == NULL)
759 err = PyFile_WriteString("<unknown>", f);
760 else
761 err = PyFile_WriteString(className, f);
762 }
763 }
764 if (err == 0 && (value != Py_None)) {
765 PyObject *s = PyObject_Str(value);
766 /* only print colon if the str() of the
767 object is not the empty string
768 */
769 if (s == NULL)
770 err = -1;
771 else if (!PyUnicode_Check(s) ||
Victor Stinnere251d6d2011-11-20 19:20:00 +0100772 PyUnicode_GetLength(s) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 err = PyFile_WriteString(": ", f);
774 if (err == 0)
775 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
776 Py_XDECREF(s);
777 }
778 /* try to write a newline in any case */
779 err += PyFile_WriteString("\n", f);
780 Py_XDECREF(tb);
781 Py_DECREF(value);
782 /* If an error happened here, don't show it.
783 XXX This is wrong, but too many callers rely on this behavior. */
784 if (err != 0)
785 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +0000786}
787
788static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 "\nThe above exception was the direct cause "
790 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +0000791
792static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 "\nDuring handling of the above exception, "
794 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +0000795
796static void
797print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 int err = 0, res;
800 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 if (seen != NULL) {
803 /* Exception chaining */
804 if (PySet_Add(seen, value) == -1)
805 PyErr_Clear();
806 else if (PyExceptionInstance_Check(value)) {
807 cause = PyException_GetCause(value);
808 context = PyException_GetContext(value);
Benjamin Petersond5a1c442012-05-14 22:09:31 -0700809 if (cause) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 res = PySet_Contains(seen, cause);
811 if (res == -1)
812 PyErr_Clear();
813 if (res == 0) {
814 print_exception_recursive(
815 f, cause, seen);
816 err |= PyFile_WriteString(
817 cause_message, f);
818 }
819 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -0700820 else if (context &&
821 !((PyBaseExceptionObject *)value)->suppress_context) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 res = PySet_Contains(seen, context);
823 if (res == -1)
824 PyErr_Clear();
825 if (res == 0) {
826 print_exception_recursive(
827 f, context, seen);
828 err |= PyFile_WriteString(
829 context_message, f);
830 }
831 }
832 Py_XDECREF(context);
833 Py_XDECREF(cause);
834 }
835 }
836 print_exception(f, value);
837 if (err != 0)
838 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +0000839}
840
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841void
842PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 PyObject *seen;
Victor Stinnerbd303c12013-11-07 23:07:29 +0100845 PyObject *f = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrou24201d42013-10-13 21:53:13 +0200846 if (PyExceptionInstance_Check(value)
847 && tb != NULL && PyTraceBack_Check(tb)) {
848 /* Put the traceback on the exception, otherwise it won't get
849 displayed. See issue #18776. */
850 PyObject *cur_tb = PyException_GetTraceback(value);
851 if (cur_tb == NULL)
852 PyException_SetTraceback(value, tb);
853 else
854 Py_DECREF(cur_tb);
855 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 if (f == Py_None) {
857 /* pass */
858 }
859 else if (f == NULL) {
860 _PyObject_Dump(value);
861 fprintf(stderr, "lost sys.stderr\n");
862 }
863 else {
864 /* We choose to ignore seen being possibly NULL, and report
865 at least the main exception (it could be a MemoryError).
866 */
867 seen = PySet_New(NULL);
868 if (seen == NULL)
869 PyErr_Clear();
870 print_exception_recursive(f, value, seen);
871 Py_XDECREF(seen);
872 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000873}
874
Guido van Rossum82598051997-03-05 00:20:32 +0000875PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000876PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 PyObject *ret = NULL;
880 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +0100881 PyArena *arena;
Victor Stinner95701bd2013-11-06 18:41:07 +0100882 PyObject *filename;
883
884 filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
885 if (filename == NULL)
886 return NULL;
887
888 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 if (arena == NULL)
890 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000891
Victor Stinner95701bd2013-11-06 18:41:07 +0100892 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 if (mod != NULL)
Victor Stinner95701bd2013-11-06 18:41:07 +0100894 ret = run_mod(mod, filename, globals, locals, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 PyArena_Free(arena);
896 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000897}
898
899PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +0100900PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000902{
Victor Stinner95701bd2013-11-06 18:41:07 +0100903 PyObject *ret = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +0100905 PyArena *arena = NULL;
906 PyObject *filename;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000907
Victor Stinner95701bd2013-11-06 18:41:07 +0100908 filename = PyUnicode_DecodeFSDefault(filename_str);
909 if (filename == NULL)
910 goto exit;
911
912 arena = PyArena_New();
913 if (arena == NULL)
914 goto exit;
915
916 mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
917 flags, NULL, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (closeit)
919 fclose(fp);
920 if (mod == NULL) {
Victor Stinner95701bd2013-11-06 18:41:07 +0100921 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 }
923 ret = run_mod(mod, filename, globals, locals, flags, arena);
Victor Stinner95701bd2013-11-06 18:41:07 +0100924
925exit:
926 Py_XDECREF(filename);
927 if (arena != NULL)
928 PyArena_Free(arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000930}
931
Guido van Rossum6c193fa2007-12-05 05:14:58 +0000932static void
933flush_io(void)
934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 PyObject *f, *r;
936 PyObject *type, *value, *traceback;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 /* Save the current exception */
939 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +0000940
Victor Stinnerbd303c12013-11-07 23:07:29 +0100941 f = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200943 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 if (r)
945 Py_DECREF(r);
946 else
947 PyErr_Clear();
948 }
Victor Stinnerbd303c12013-11-07 23:07:29 +0100949 f = _PySys_GetObjectId(&PyId_stdout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200951 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 if (r)
953 Py_DECREF(r);
954 else
955 PyErr_Clear();
956 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +0000959}
960
Guido van Rossum82598051997-03-05 00:20:32 +0000961static PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +0100962run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
963 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 PyCodeObject *co;
966 PyObject *v;
Victor Stinner95701bd2013-11-06 18:41:07 +0100967 co = PyAST_CompileObject(mod, filename, flags, -1, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 if (co == NULL)
969 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000970 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 Py_DECREF(co);
972 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000973}
974
Guido van Rossum82598051997-03-05 00:20:32 +0000975static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000976run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +0000978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 PyCodeObject *co;
980 PyObject *v;
981 long magic;
982 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 magic = PyMarshal_ReadLongFromFile(fp);
985 if (magic != PyImport_GetMagicNumber()) {
Victor Stinner5200f552015-03-18 13:56:25 +0100986 if (!PyErr_Occurred())
987 PyErr_SetString(PyExc_RuntimeError,
988 "Bad magic number in .pyc file");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 return NULL;
990 }
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100991 /* Skip mtime and size */
992 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 (void) PyMarshal_ReadLongFromFile(fp);
Victor Stinner5200f552015-03-18 13:56:25 +0100994 if (PyErr_Occurred())
995 return NULL;
996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 v = PyMarshal_ReadLastObjectFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 if (v == NULL || !PyCode_Check(v)) {
999 Py_XDECREF(v);
1000 PyErr_SetString(PyExc_RuntimeError,
1001 "Bad code object in .pyc file");
1002 return NULL;
1003 }
1004 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001005 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 if (v && flags)
1007 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1008 Py_DECREF(co);
1009 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001010}
1011
Guido van Rossum82598051997-03-05 00:20:32 +00001012PyObject *
Victor Stinner14e461d2013-08-26 22:28:21 +02001013Py_CompileStringObject(const char *str, PyObject *filename, int start,
1014 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 PyCodeObject *co;
1017 mod_ty mod;
1018 PyArena *arena = PyArena_New();
1019 if (arena == NULL)
1020 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001021
Victor Stinner14e461d2013-08-26 22:28:21 +02001022 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 if (mod == NULL) {
1024 PyArena_Free(arena);
1025 return NULL;
1026 }
1027 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1028 PyObject *result = PyAST_mod2obj(mod);
1029 PyArena_Free(arena);
1030 return result;
1031 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001032 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 PyArena_Free(arena);
1034 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001035}
1036
Victor Stinner14e461d2013-08-26 22:28:21 +02001037PyObject *
1038Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
1039 PyCompilerFlags *flags, int optimize)
1040{
1041 PyObject *filename, *co;
1042 filename = PyUnicode_DecodeFSDefault(filename_str);
1043 if (filename == NULL)
1044 return NULL;
1045 co = Py_CompileStringObject(str, filename, start, flags, optimize);
1046 Py_DECREF(filename);
1047 return co;
1048}
1049
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001050/* For use in Py_LIMITED_API */
1051#undef Py_CompileString
1052PyObject *
1053PyCompileString(const char *str, const char *filename, int start)
1054{
1055 return Py_CompileStringFlags(str, filename, start, NULL);
1056}
1057
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001058struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +02001059Py_SymtableStringObject(const char *str, PyObject *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 struct symtable *st;
1062 mod_ty mod;
1063 PyCompilerFlags flags;
Victor Stinner14e461d2013-08-26 22:28:21 +02001064 PyArena *arena;
1065
1066 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (arena == NULL)
1068 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 flags.cf_flags = 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02001071 mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 if (mod == NULL) {
1073 PyArena_Free(arena);
1074 return NULL;
1075 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001076 st = PySymtable_BuildObject(mod, filename, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 PyArena_Free(arena);
1078 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001079}
1080
Victor Stinner14e461d2013-08-26 22:28:21 +02001081struct symtable *
1082Py_SymtableString(const char *str, const char *filename_str, int start)
1083{
1084 PyObject *filename;
1085 struct symtable *st;
1086
1087 filename = PyUnicode_DecodeFSDefault(filename_str);
1088 if (filename == NULL)
1089 return NULL;
1090 st = Py_SymtableStringObject(str, filename, start);
1091 Py_DECREF(filename);
1092 return st;
1093}
1094
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095/* Preferred access to parser is through AST. */
1096mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02001097PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
1098 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 mod_ty mod;
1101 PyCompilerFlags localflags;
1102 perrdetail err;
1103 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001104
Victor Stinner14e461d2013-08-26 22:28:21 +02001105 node *n = PyParser_ParseStringObject(s, filename,
1106 &_PyParser_Grammar, start, &err,
1107 &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 if (flags == NULL) {
1109 localflags.cf_flags = 0;
1110 flags = &localflags;
1111 }
1112 if (n) {
1113 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02001114 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 }
1117 else {
1118 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001119 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001121 err_free(&err);
1122 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123}
1124
1125mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02001126PyParser_ASTFromString(const char *s, const char *filename_str, int start,
1127 PyCompilerFlags *flags, PyArena *arena)
1128{
1129 PyObject *filename;
1130 mod_ty mod;
1131 filename = PyUnicode_DecodeFSDefault(filename_str);
1132 if (filename == NULL)
1133 return NULL;
1134 mod = PyParser_ASTFromStringObject(s, filename, start, flags, arena);
1135 Py_DECREF(filename);
1136 return mod;
1137}
1138
1139mod_ty
1140PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
1141 int start, char *ps1,
1142 char *ps2, PyCompilerFlags *flags, int *errcode,
1143 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 mod_ty mod;
1146 PyCompilerFlags localflags;
1147 perrdetail err;
1148 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001149
Victor Stinner14e461d2013-08-26 22:28:21 +02001150 node *n = PyParser_ParseFileObject(fp, filename, enc,
1151 &_PyParser_Grammar,
1152 start, ps1, ps2, &err, &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 if (flags == NULL) {
1154 localflags.cf_flags = 0;
1155 flags = &localflags;
1156 }
1157 if (n) {
1158 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02001159 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 }
1162 else {
1163 err_input(&err);
1164 if (errcode)
1165 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02001166 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001168 err_free(&err);
1169 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170}
1171
Victor Stinner14e461d2013-08-26 22:28:21 +02001172mod_ty
1173PyParser_ASTFromFile(FILE *fp, const char *filename_str, const char* enc,
1174 int start, char *ps1,
1175 char *ps2, PyCompilerFlags *flags, int *errcode,
1176 PyArena *arena)
1177{
1178 mod_ty mod;
1179 PyObject *filename;
1180 filename = PyUnicode_DecodeFSDefault(filename_str);
1181 if (filename == NULL)
1182 return NULL;
1183 mod = PyParser_ASTFromFileObject(fp, filename, enc, start, ps1, ps2,
1184 flags, errcode, arena);
1185 Py_DECREF(filename);
1186 return mod;
1187}
1188
Guido van Rossuma110aa61994-08-29 12:50:44 +00001189/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001190
Guido van Rossuma110aa61994-08-29 12:50:44 +00001191node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001192PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 perrdetail err;
1195 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1196 &_PyParser_Grammar,
1197 start, NULL, NULL, &err, flags);
1198 if (n == NULL)
1199 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001200 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001203}
1204
Guido van Rossuma110aa61994-08-29 12:50:44 +00001205/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001206
Guido van Rossuma110aa61994-08-29 12:50:44 +00001207node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001208PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 perrdetail err;
1211 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1212 start, &err, flags);
1213 if (n == NULL)
1214 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001215 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001217}
1218
1219node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001220PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 perrdetail err;
1224 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1225 &_PyParser_Grammar, start, &err, flags);
1226 if (n == NULL)
1227 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001228 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001230}
1231
1232node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001233PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001236}
1237
Guido van Rossum66ebd912003-04-17 16:02:26 +00001238/* May want to move a more generalized form of this to parsetok.c or
1239 even parser modules. */
1240
1241void
Victor Stinner7f2fee32011-04-05 00:39:01 +02001242PyParser_ClearError(perrdetail *err)
1243{
1244 err_free(err);
1245}
1246
1247void
Guido van Rossum66ebd912003-04-17 16:02:26 +00001248PyParser_SetError(perrdetail *err)
1249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00001251}
1252
Victor Stinner7f2fee32011-04-05 00:39:01 +02001253static void
1254err_free(perrdetail *err)
1255{
1256 Py_CLEAR(err->filename);
1257}
1258
Guido van Rossuma110aa61994-08-29 12:50:44 +00001259/* Set the error appropriate to the given input error code (see errcode.h) */
1260
1261static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001262err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 PyObject *v, *w, *errtype, *errtext;
1265 PyObject *msg_obj = NULL;
1266 char *msg = NULL;
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001267 int offset = err->offset;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 errtype = PyExc_SyntaxError;
1270 switch (err->error) {
1271 case E_ERROR:
1272 return;
1273 case E_SYNTAX:
1274 errtype = PyExc_IndentationError;
1275 if (err->expected == INDENT)
1276 msg = "expected an indented block";
1277 else if (err->token == INDENT)
1278 msg = "unexpected indent";
1279 else if (err->token == DEDENT)
1280 msg = "unexpected unindent";
1281 else {
1282 errtype = PyExc_SyntaxError;
1283 msg = "invalid syntax";
1284 }
1285 break;
1286 case E_TOKEN:
1287 msg = "invalid token";
1288 break;
1289 case E_EOFS:
1290 msg = "EOF while scanning triple-quoted string literal";
1291 break;
1292 case E_EOLS:
1293 msg = "EOL while scanning string literal";
1294 break;
1295 case E_INTR:
1296 if (!PyErr_Occurred())
1297 PyErr_SetNone(PyExc_KeyboardInterrupt);
1298 goto cleanup;
1299 case E_NOMEM:
1300 PyErr_NoMemory();
1301 goto cleanup;
1302 case E_EOF:
1303 msg = "unexpected EOF while parsing";
1304 break;
1305 case E_TABSPACE:
1306 errtype = PyExc_TabError;
1307 msg = "inconsistent use of tabs and spaces in indentation";
1308 break;
1309 case E_OVERFLOW:
1310 msg = "expression too long";
1311 break;
1312 case E_DEDENT:
1313 errtype = PyExc_IndentationError;
1314 msg = "unindent does not match any outer indentation level";
1315 break;
1316 case E_TOODEEP:
1317 errtype = PyExc_IndentationError;
1318 msg = "too many levels of indentation";
1319 break;
1320 case E_DECODE: {
1321 PyObject *type, *value, *tb;
1322 PyErr_Fetch(&type, &value, &tb);
1323 msg = "unknown decode error";
1324 if (value != NULL)
1325 msg_obj = PyObject_Str(value);
1326 Py_XDECREF(type);
1327 Py_XDECREF(value);
1328 Py_XDECREF(tb);
1329 break;
1330 }
1331 case E_LINECONT:
1332 msg = "unexpected character after line continuation character";
1333 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 case E_IDENTIFIER:
1336 msg = "invalid character in identifier";
1337 break;
Meador Ingefa21bf02012-01-19 01:08:41 -06001338 case E_BADSINGLE:
1339 msg = "multiple statements found while compiling a single statement";
1340 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 default:
1342 fprintf(stderr, "error=%d\n", err->error);
1343 msg = "unknown parsing error";
1344 break;
1345 }
1346 /* err->text may not be UTF-8 in case of decoding errors.
1347 Explicitly convert to an object. */
1348 if (!err->text) {
1349 errtext = Py_None;
1350 Py_INCREF(Py_None);
1351 } else {
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001352 errtext = PyUnicode_DecodeUTF8(err->text, err->offset,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 "replace");
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001354 if (errtext != NULL) {
1355 Py_ssize_t len = strlen(err->text);
1356 offset = (int)PyUnicode_GET_LENGTH(errtext);
1357 if (len != err->offset) {
1358 Py_DECREF(errtext);
1359 errtext = PyUnicode_DecodeUTF8(err->text, len,
1360 "replace");
1361 }
1362 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001364 v = Py_BuildValue("(OiiN)", err->filename,
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001365 err->lineno, offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 if (v != NULL) {
1367 if (msg_obj)
1368 w = Py_BuildValue("(OO)", msg_obj, v);
1369 else
1370 w = Py_BuildValue("(sO)", msg, v);
1371 } else
1372 w = NULL;
1373 Py_XDECREF(v);
1374 PyErr_SetObject(errtype, w);
1375 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00001376cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 Py_XDECREF(msg_obj);
1378 if (err->text != NULL) {
1379 PyObject_FREE(err->text);
1380 err->text = NULL;
1381 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001382}
1383
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001384
Zachary Warec4821d62014-11-21 23:35:12 -06001385#if defined(USE_STACKCHECK)
1386#if defined(WIN32) && defined(_MSC_VER)
1387
1388/* Stack checking for Microsoft C */
1389
1390#include <malloc.h>
1391#include <excpt.h>
1392
1393/*
1394 * Return non-zero when we run out of memory on the stack; zero otherwise.
1395 */
1396int
1397PyOS_CheckStack(void)
1398{
1399 __try {
1400 /* alloca throws a stack overflow exception if there's
1401 not enough space left on the stack */
1402 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1403 return 0;
1404 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1405 EXCEPTION_EXECUTE_HANDLER :
1406 EXCEPTION_CONTINUE_SEARCH) {
1407 int errcode = _resetstkoflw();
1408 if (errcode == 0)
1409 {
1410 Py_FatalError("Could not reset the stack!");
1411 }
1412 }
1413 return 1;
1414}
1415
1416#endif /* WIN32 && _MSC_VER */
1417
1418/* Alternate implementations can be added here... */
1419
1420#endif /* USE_STACKCHECK */
1421
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422/* Deprecated C API functions still provided for binary compatiblity */
1423
1424#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001425PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429}
1430
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001431#undef PyParser_SimpleParseString
1432PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433PyParser_SimpleParseString(const char *str, int start)
1434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001437
1438#undef PyRun_AnyFile
1439PyAPI_FUNC(int)
1440PyRun_AnyFile(FILE *fp, const char *name)
1441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001443}
1444
1445#undef PyRun_AnyFileEx
1446PyAPI_FUNC(int)
1447PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001450}
1451
1452#undef PyRun_AnyFileFlags
1453PyAPI_FUNC(int)
1454PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001457}
1458
1459#undef PyRun_File
1460PyAPI_FUNC(PyObject *)
1461PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001464}
1465
1466#undef PyRun_FileEx
1467PyAPI_FUNC(PyObject *)
1468PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001471}
1472
1473#undef PyRun_FileFlags
1474PyAPI_FUNC(PyObject *)
1475PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001479}
1480
1481#undef PyRun_SimpleFile
1482PyAPI_FUNC(int)
1483PyRun_SimpleFile(FILE *f, const char *p)
1484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001486}
1487
1488#undef PyRun_SimpleFileEx
1489PyAPI_FUNC(int)
1490PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001493}
1494
1495
1496#undef PyRun_String
1497PyAPI_FUNC(PyObject *)
1498PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001501}
1502
1503#undef PyRun_SimpleString
1504PyAPI_FUNC(int)
1505PyRun_SimpleString(const char *s)
1506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001508}
1509
1510#undef Py_CompileString
1511PyAPI_FUNC(PyObject *)
1512Py_CompileString(const char *str, const char *p, int s)
1513{
Georg Brandl8334fd92010-12-04 10:26:46 +00001514 return Py_CompileStringExFlags(str, p, s, NULL, -1);
1515}
1516
1517#undef Py_CompileStringFlags
1518PyAPI_FUNC(PyObject *)
1519Py_CompileStringFlags(const char *str, const char *p, int s,
1520 PyCompilerFlags *flags)
1521{
1522 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001523}
1524
1525#undef PyRun_InteractiveOne
1526PyAPI_FUNC(int)
1527PyRun_InteractiveOne(FILE *f, const char *p)
1528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001530}
1531
1532#undef PyRun_InteractiveLoop
1533PyAPI_FUNC(int)
1534PyRun_InteractiveLoop(FILE *f, const char *p)
1535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001537}
1538
1539#ifdef __cplusplus
1540}
1541#endif