blob: 7fbf06e68a1faba77b3f6929a13fff85bc3385c3 [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 */
Martin Panter3263f682016-02-28 03:16:11 +0000769 if (s == NULL) {
770 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 err = -1;
Martin Panter3263f682016-02-28 03:16:11 +0000772 PyFile_WriteString(": <exception str() failed>", f);
773 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 else if (!PyUnicode_Check(s) ||
Victor Stinnere251d6d2011-11-20 19:20:00 +0100775 PyUnicode_GetLength(s) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 err = PyFile_WriteString(": ", f);
777 if (err == 0)
778 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
779 Py_XDECREF(s);
780 }
781 /* try to write a newline in any case */
Martin Panter3263f682016-02-28 03:16:11 +0000782 if (err < 0) {
783 PyErr_Clear();
784 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 err += PyFile_WriteString("\n", f);
786 Py_XDECREF(tb);
787 Py_DECREF(value);
788 /* If an error happened here, don't show it.
789 XXX This is wrong, but too many callers rely on this behavior. */
790 if (err != 0)
791 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +0000792}
793
794static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 "\nThe above exception was the direct cause "
796 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +0000797
798static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 "\nDuring handling of the above exception, "
800 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +0000801
802static void
803print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 int err = 0, res;
806 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 if (seen != NULL) {
809 /* Exception chaining */
810 if (PySet_Add(seen, value) == -1)
811 PyErr_Clear();
812 else if (PyExceptionInstance_Check(value)) {
813 cause = PyException_GetCause(value);
814 context = PyException_GetContext(value);
Benjamin Petersond5a1c442012-05-14 22:09:31 -0700815 if (cause) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 res = PySet_Contains(seen, cause);
817 if (res == -1)
818 PyErr_Clear();
819 if (res == 0) {
820 print_exception_recursive(
821 f, cause, seen);
822 err |= PyFile_WriteString(
823 cause_message, f);
824 }
825 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -0700826 else if (context &&
827 !((PyBaseExceptionObject *)value)->suppress_context) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 res = PySet_Contains(seen, context);
829 if (res == -1)
830 PyErr_Clear();
831 if (res == 0) {
832 print_exception_recursive(
833 f, context, seen);
834 err |= PyFile_WriteString(
835 context_message, f);
836 }
837 }
838 Py_XDECREF(context);
839 Py_XDECREF(cause);
840 }
841 }
842 print_exception(f, value);
843 if (err != 0)
844 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +0000845}
846
Thomas Wouters477c8d52006-05-27 19:21:47 +0000847void
848PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 PyObject *seen;
Victor Stinnerbd303c12013-11-07 23:07:29 +0100851 PyObject *f = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrou24201d42013-10-13 21:53:13 +0200852 if (PyExceptionInstance_Check(value)
853 && tb != NULL && PyTraceBack_Check(tb)) {
854 /* Put the traceback on the exception, otherwise it won't get
855 displayed. See issue #18776. */
856 PyObject *cur_tb = PyException_GetTraceback(value);
857 if (cur_tb == NULL)
858 PyException_SetTraceback(value, tb);
859 else
860 Py_DECREF(cur_tb);
861 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 if (f == Py_None) {
863 /* pass */
864 }
865 else if (f == NULL) {
866 _PyObject_Dump(value);
867 fprintf(stderr, "lost sys.stderr\n");
868 }
869 else {
870 /* We choose to ignore seen being possibly NULL, and report
871 at least the main exception (it could be a MemoryError).
872 */
873 seen = PySet_New(NULL);
874 if (seen == NULL)
875 PyErr_Clear();
876 print_exception_recursive(f, value, seen);
877 Py_XDECREF(seen);
878 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000879}
880
Guido van Rossum82598051997-03-05 00:20:32 +0000881PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000882PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 PyObject *ret = NULL;
886 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +0100887 PyArena *arena;
Victor Stinner95701bd2013-11-06 18:41:07 +0100888 PyObject *filename;
889
890 filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
891 if (filename == NULL)
892 return NULL;
893
894 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 if (arena == NULL)
896 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000897
Victor Stinner95701bd2013-11-06 18:41:07 +0100898 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 if (mod != NULL)
Victor Stinner95701bd2013-11-06 18:41:07 +0100900 ret = run_mod(mod, filename, globals, locals, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyArena_Free(arena);
902 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000903}
904
905PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +0100906PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000908{
Victor Stinner95701bd2013-11-06 18:41:07 +0100909 PyObject *ret = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +0100911 PyArena *arena = NULL;
912 PyObject *filename;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000913
Victor Stinner95701bd2013-11-06 18:41:07 +0100914 filename = PyUnicode_DecodeFSDefault(filename_str);
915 if (filename == NULL)
916 goto exit;
917
918 arena = PyArena_New();
919 if (arena == NULL)
920 goto exit;
921
922 mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
923 flags, NULL, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 if (closeit)
925 fclose(fp);
926 if (mod == NULL) {
Victor Stinner95701bd2013-11-06 18:41:07 +0100927 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 }
929 ret = run_mod(mod, filename, globals, locals, flags, arena);
Victor Stinner95701bd2013-11-06 18:41:07 +0100930
931exit:
932 Py_XDECREF(filename);
933 if (arena != NULL)
934 PyArena_Free(arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000936}
937
Guido van Rossum6c193fa2007-12-05 05:14:58 +0000938static void
939flush_io(void)
940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyObject *f, *r;
942 PyObject *type, *value, *traceback;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 /* Save the current exception */
945 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +0000946
Victor Stinnerbd303c12013-11-07 23:07:29 +0100947 f = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200949 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 if (r)
951 Py_DECREF(r);
952 else
953 PyErr_Clear();
954 }
Victor Stinnerbd303c12013-11-07 23:07:29 +0100955 f = _PySys_GetObjectId(&PyId_stdout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200957 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (r)
959 Py_DECREF(r);
960 else
961 PyErr_Clear();
962 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +0000965}
966
Guido van Rossum82598051997-03-05 00:20:32 +0000967static PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +0100968run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
969 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyCodeObject *co;
972 PyObject *v;
Victor Stinner95701bd2013-11-06 18:41:07 +0100973 co = PyAST_CompileObject(mod, filename, flags, -1, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 if (co == NULL)
975 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000976 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 Py_DECREF(co);
978 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000979}
980
Guido van Rossum82598051997-03-05 00:20:32 +0000981static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000982run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +0000984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 PyCodeObject *co;
986 PyObject *v;
987 long magic;
988 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 magic = PyMarshal_ReadLongFromFile(fp);
991 if (magic != PyImport_GetMagicNumber()) {
Victor Stinner5200f552015-03-18 13:56:25 +0100992 if (!PyErr_Occurred())
993 PyErr_SetString(PyExc_RuntimeError,
994 "Bad magic number in .pyc file");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 return NULL;
996 }
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100997 /* Skip mtime and size */
998 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 (void) PyMarshal_ReadLongFromFile(fp);
Victor Stinner5200f552015-03-18 13:56:25 +01001000 if (PyErr_Occurred())
1001 return NULL;
1002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 v = PyMarshal_ReadLastObjectFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 if (v == NULL || !PyCode_Check(v)) {
1005 Py_XDECREF(v);
1006 PyErr_SetString(PyExc_RuntimeError,
1007 "Bad code object in .pyc file");
1008 return NULL;
1009 }
1010 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001011 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 if (v && flags)
1013 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1014 Py_DECREF(co);
1015 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001016}
1017
Guido van Rossum82598051997-03-05 00:20:32 +00001018PyObject *
Victor Stinner14e461d2013-08-26 22:28:21 +02001019Py_CompileStringObject(const char *str, PyObject *filename, int start,
1020 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 PyCodeObject *co;
1023 mod_ty mod;
1024 PyArena *arena = PyArena_New();
1025 if (arena == NULL)
1026 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001027
Victor Stinner14e461d2013-08-26 22:28:21 +02001028 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 if (mod == NULL) {
1030 PyArena_Free(arena);
1031 return NULL;
1032 }
1033 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1034 PyObject *result = PyAST_mod2obj(mod);
1035 PyArena_Free(arena);
1036 return result;
1037 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001038 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 PyArena_Free(arena);
1040 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001041}
1042
Victor Stinner14e461d2013-08-26 22:28:21 +02001043PyObject *
1044Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
1045 PyCompilerFlags *flags, int optimize)
1046{
1047 PyObject *filename, *co;
1048 filename = PyUnicode_DecodeFSDefault(filename_str);
1049 if (filename == NULL)
1050 return NULL;
1051 co = Py_CompileStringObject(str, filename, start, flags, optimize);
1052 Py_DECREF(filename);
1053 return co;
1054}
1055
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001056/* For use in Py_LIMITED_API */
1057#undef Py_CompileString
1058PyObject *
1059PyCompileString(const char *str, const char *filename, int start)
1060{
1061 return Py_CompileStringFlags(str, filename, start, NULL);
1062}
1063
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001064struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +02001065Py_SymtableStringObject(const char *str, PyObject *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 struct symtable *st;
1068 mod_ty mod;
1069 PyCompilerFlags flags;
Victor Stinner14e461d2013-08-26 22:28:21 +02001070 PyArena *arena;
1071
1072 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 if (arena == NULL)
1074 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 flags.cf_flags = 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02001077 mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 if (mod == NULL) {
1079 PyArena_Free(arena);
1080 return NULL;
1081 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001082 st = PySymtable_BuildObject(mod, filename, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 PyArena_Free(arena);
1084 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001085}
1086
Victor Stinner14e461d2013-08-26 22:28:21 +02001087struct symtable *
1088Py_SymtableString(const char *str, const char *filename_str, int start)
1089{
1090 PyObject *filename;
1091 struct symtable *st;
1092
1093 filename = PyUnicode_DecodeFSDefault(filename_str);
1094 if (filename == NULL)
1095 return NULL;
1096 st = Py_SymtableStringObject(str, filename, start);
1097 Py_DECREF(filename);
1098 return st;
1099}
1100
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101/* Preferred access to parser is through AST. */
1102mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02001103PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
1104 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 mod_ty mod;
1107 PyCompilerFlags localflags;
1108 perrdetail err;
1109 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001110
Victor Stinner14e461d2013-08-26 22:28:21 +02001111 node *n = PyParser_ParseStringObject(s, filename,
1112 &_PyParser_Grammar, start, &err,
1113 &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 if (flags == NULL) {
1115 localflags.cf_flags = 0;
1116 flags = &localflags;
1117 }
1118 if (n) {
1119 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02001120 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 }
1123 else {
1124 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001125 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001127 err_free(&err);
1128 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129}
1130
1131mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02001132PyParser_ASTFromString(const char *s, const char *filename_str, int start,
1133 PyCompilerFlags *flags, PyArena *arena)
1134{
1135 PyObject *filename;
1136 mod_ty mod;
1137 filename = PyUnicode_DecodeFSDefault(filename_str);
1138 if (filename == NULL)
1139 return NULL;
1140 mod = PyParser_ASTFromStringObject(s, filename, start, flags, arena);
1141 Py_DECREF(filename);
1142 return mod;
1143}
1144
1145mod_ty
1146PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
1147 int start, char *ps1,
1148 char *ps2, PyCompilerFlags *flags, int *errcode,
1149 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 mod_ty mod;
1152 PyCompilerFlags localflags;
1153 perrdetail err;
1154 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001155
Victor Stinner14e461d2013-08-26 22:28:21 +02001156 node *n = PyParser_ParseFileObject(fp, filename, enc,
1157 &_PyParser_Grammar,
1158 start, ps1, ps2, &err, &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 if (flags == NULL) {
1160 localflags.cf_flags = 0;
1161 flags = &localflags;
1162 }
1163 if (n) {
1164 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02001165 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 }
1168 else {
1169 err_input(&err);
1170 if (errcode)
1171 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02001172 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001174 err_free(&err);
1175 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176}
1177
Victor Stinner14e461d2013-08-26 22:28:21 +02001178mod_ty
1179PyParser_ASTFromFile(FILE *fp, const char *filename_str, const char* enc,
1180 int start, char *ps1,
1181 char *ps2, PyCompilerFlags *flags, int *errcode,
1182 PyArena *arena)
1183{
1184 mod_ty mod;
1185 PyObject *filename;
1186 filename = PyUnicode_DecodeFSDefault(filename_str);
1187 if (filename == NULL)
1188 return NULL;
1189 mod = PyParser_ASTFromFileObject(fp, filename, enc, start, ps1, ps2,
1190 flags, errcode, arena);
1191 Py_DECREF(filename);
1192 return mod;
1193}
1194
Guido van Rossuma110aa61994-08-29 12:50:44 +00001195/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001196
Guido van Rossuma110aa61994-08-29 12:50:44 +00001197node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001198PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 perrdetail err;
1201 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1202 &_PyParser_Grammar,
1203 start, NULL, NULL, &err, flags);
1204 if (n == NULL)
1205 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001206 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001209}
1210
Guido van Rossuma110aa61994-08-29 12:50:44 +00001211/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001212
Guido van Rossuma110aa61994-08-29 12:50:44 +00001213node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001214PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 perrdetail err;
1217 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1218 start, &err, flags);
1219 if (n == NULL)
1220 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001221 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001223}
1224
1225node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001226PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 perrdetail err;
1230 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1231 &_PyParser_Grammar, start, &err, flags);
1232 if (n == NULL)
1233 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001234 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001236}
1237
1238node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001239PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001242}
1243
Guido van Rossum66ebd912003-04-17 16:02:26 +00001244/* May want to move a more generalized form of this to parsetok.c or
1245 even parser modules. */
1246
1247void
Victor Stinner7f2fee32011-04-05 00:39:01 +02001248PyParser_ClearError(perrdetail *err)
1249{
1250 err_free(err);
1251}
1252
1253void
Guido van Rossum66ebd912003-04-17 16:02:26 +00001254PyParser_SetError(perrdetail *err)
1255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00001257}
1258
Victor Stinner7f2fee32011-04-05 00:39:01 +02001259static void
1260err_free(perrdetail *err)
1261{
1262 Py_CLEAR(err->filename);
1263}
1264
Guido van Rossuma110aa61994-08-29 12:50:44 +00001265/* Set the error appropriate to the given input error code (see errcode.h) */
1266
1267static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001268err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 PyObject *v, *w, *errtype, *errtext;
1271 PyObject *msg_obj = NULL;
1272 char *msg = NULL;
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001273 int offset = err->offset;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 errtype = PyExc_SyntaxError;
1276 switch (err->error) {
1277 case E_ERROR:
1278 return;
1279 case E_SYNTAX:
1280 errtype = PyExc_IndentationError;
1281 if (err->expected == INDENT)
1282 msg = "expected an indented block";
1283 else if (err->token == INDENT)
1284 msg = "unexpected indent";
1285 else if (err->token == DEDENT)
1286 msg = "unexpected unindent";
1287 else {
1288 errtype = PyExc_SyntaxError;
1289 msg = "invalid syntax";
1290 }
1291 break;
1292 case E_TOKEN:
1293 msg = "invalid token";
1294 break;
1295 case E_EOFS:
1296 msg = "EOF while scanning triple-quoted string literal";
1297 break;
1298 case E_EOLS:
1299 msg = "EOL while scanning string literal";
1300 break;
1301 case E_INTR:
1302 if (!PyErr_Occurred())
1303 PyErr_SetNone(PyExc_KeyboardInterrupt);
1304 goto cleanup;
1305 case E_NOMEM:
1306 PyErr_NoMemory();
1307 goto cleanup;
1308 case E_EOF:
1309 msg = "unexpected EOF while parsing";
1310 break;
1311 case E_TABSPACE:
1312 errtype = PyExc_TabError;
1313 msg = "inconsistent use of tabs and spaces in indentation";
1314 break;
1315 case E_OVERFLOW:
1316 msg = "expression too long";
1317 break;
1318 case E_DEDENT:
1319 errtype = PyExc_IndentationError;
1320 msg = "unindent does not match any outer indentation level";
1321 break;
1322 case E_TOODEEP:
1323 errtype = PyExc_IndentationError;
1324 msg = "too many levels of indentation";
1325 break;
1326 case E_DECODE: {
1327 PyObject *type, *value, *tb;
1328 PyErr_Fetch(&type, &value, &tb);
1329 msg = "unknown decode error";
1330 if (value != NULL)
1331 msg_obj = PyObject_Str(value);
1332 Py_XDECREF(type);
1333 Py_XDECREF(value);
1334 Py_XDECREF(tb);
1335 break;
1336 }
1337 case E_LINECONT:
1338 msg = "unexpected character after line continuation character";
1339 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 case E_IDENTIFIER:
1342 msg = "invalid character in identifier";
1343 break;
Meador Ingefa21bf02012-01-19 01:08:41 -06001344 case E_BADSINGLE:
1345 msg = "multiple statements found while compiling a single statement";
1346 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 default:
1348 fprintf(stderr, "error=%d\n", err->error);
1349 msg = "unknown parsing error";
1350 break;
1351 }
1352 /* err->text may not be UTF-8 in case of decoding errors.
1353 Explicitly convert to an object. */
1354 if (!err->text) {
1355 errtext = Py_None;
1356 Py_INCREF(Py_None);
1357 } else {
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001358 errtext = PyUnicode_DecodeUTF8(err->text, err->offset,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 "replace");
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001360 if (errtext != NULL) {
1361 Py_ssize_t len = strlen(err->text);
1362 offset = (int)PyUnicode_GET_LENGTH(errtext);
1363 if (len != err->offset) {
1364 Py_DECREF(errtext);
1365 errtext = PyUnicode_DecodeUTF8(err->text, len,
1366 "replace");
1367 }
1368 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001370 v = Py_BuildValue("(OiiN)", err->filename,
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001371 err->lineno, offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 if (v != NULL) {
1373 if (msg_obj)
1374 w = Py_BuildValue("(OO)", msg_obj, v);
1375 else
1376 w = Py_BuildValue("(sO)", msg, v);
1377 } else
1378 w = NULL;
1379 Py_XDECREF(v);
1380 PyErr_SetObject(errtype, w);
1381 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00001382cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 Py_XDECREF(msg_obj);
1384 if (err->text != NULL) {
1385 PyObject_FREE(err->text);
1386 err->text = NULL;
1387 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001388}
1389
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001390
Zachary Warec4821d62014-11-21 23:35:12 -06001391#if defined(USE_STACKCHECK)
1392#if defined(WIN32) && defined(_MSC_VER)
1393
1394/* Stack checking for Microsoft C */
1395
1396#include <malloc.h>
1397#include <excpt.h>
1398
1399/*
1400 * Return non-zero when we run out of memory on the stack; zero otherwise.
1401 */
1402int
1403PyOS_CheckStack(void)
1404{
1405 __try {
1406 /* alloca throws a stack overflow exception if there's
1407 not enough space left on the stack */
1408 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1409 return 0;
1410 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1411 EXCEPTION_EXECUTE_HANDLER :
1412 EXCEPTION_CONTINUE_SEARCH) {
1413 int errcode = _resetstkoflw();
1414 if (errcode == 0)
1415 {
1416 Py_FatalError("Could not reset the stack!");
1417 }
1418 }
1419 return 1;
1420}
1421
1422#endif /* WIN32 && _MSC_VER */
1423
1424/* Alternate implementations can be added here... */
1425
1426#endif /* USE_STACKCHECK */
1427
Martin Panter46f50722016-05-26 05:35:26 +00001428/* Deprecated C API functions still provided for binary compatibility */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429
1430#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001431PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435}
1436
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001437#undef PyParser_SimpleParseString
1438PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439PyParser_SimpleParseString(const char *str, int start)
1440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001443
1444#undef PyRun_AnyFile
1445PyAPI_FUNC(int)
1446PyRun_AnyFile(FILE *fp, const char *name)
1447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001449}
1450
1451#undef PyRun_AnyFileEx
1452PyAPI_FUNC(int)
1453PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001456}
1457
1458#undef PyRun_AnyFileFlags
1459PyAPI_FUNC(int)
1460PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001463}
1464
1465#undef PyRun_File
1466PyAPI_FUNC(PyObject *)
1467PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001470}
1471
1472#undef PyRun_FileEx
1473PyAPI_FUNC(PyObject *)
1474PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001477}
1478
1479#undef PyRun_FileFlags
1480PyAPI_FUNC(PyObject *)
1481PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001485}
1486
1487#undef PyRun_SimpleFile
1488PyAPI_FUNC(int)
1489PyRun_SimpleFile(FILE *f, const char *p)
1490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001492}
1493
1494#undef PyRun_SimpleFileEx
1495PyAPI_FUNC(int)
1496PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001499}
1500
1501
1502#undef PyRun_String
1503PyAPI_FUNC(PyObject *)
1504PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001507}
1508
1509#undef PyRun_SimpleString
1510PyAPI_FUNC(int)
1511PyRun_SimpleString(const char *s)
1512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001514}
1515
1516#undef Py_CompileString
1517PyAPI_FUNC(PyObject *)
1518Py_CompileString(const char *str, const char *p, int s)
1519{
Georg Brandl8334fd92010-12-04 10:26:46 +00001520 return Py_CompileStringExFlags(str, p, s, NULL, -1);
1521}
1522
1523#undef Py_CompileStringFlags
1524PyAPI_FUNC(PyObject *)
1525Py_CompileStringFlags(const char *str, const char *p, int s,
1526 PyCompilerFlags *flags)
1527{
1528 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001529}
1530
1531#undef PyRun_InteractiveOne
1532PyAPI_FUNC(int)
1533PyRun_InteractiveOne(FILE *f, const char *p)
1534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001536}
1537
1538#undef PyRun_InteractiveLoop
1539PyAPI_FUNC(int)
1540PyRun_InteractiveLoop(FILE *f, const char *p)
1541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001543}
1544
1545#ifdef __cplusplus
1546}
1547#endif