blob: ba1d1cf02f25846812604de6b2ce73001c0000f7 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
Eric Snowc7ec9982017-05-23 23:00:52 -07002/* Top level execution of Python code (including in __main__) */
3
4/* To help control the interfaces between the startup, execution and
5 * shutdown code, the phases are split across separate modules (boostrap,
6 * pythonrun, shutdown)
7 */
8
9/* TODO: Cull includes following phase split */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000010
Guido van Rossum82598051997-03-05 00:20:32 +000011#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "Python-ast.h"
Victor Stinner3bb183d2018-11-22 18:38:38 +010014#undef Yield /* undefine macro conflicting with <winbase.h> */
Victor Stinner438a12d2019-05-24 17:01:38 +020015#include "pycore_pyerrors.h"
Gregory P. Smith38f11cc2019-02-16 12:57:40 -080016#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010017#include "pycore_pystate.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000018#include "grammar.h"
19#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000020#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000021#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000022#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000023#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000024#include "symtable.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000025#include "ast.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000026#include "marshal.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000027#include "osdefs.h"
Victor Stinner518e6102014-03-18 02:06:38 +010028#include <locale.h>
Guido van Rossum1984f1e1992-08-04 12:41:02 +000029
Thomas Wouters0e3f5912006-08-11 14:57:12 +000030#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000031#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000032#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000033
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000034#ifdef MS_WINDOWS
Martin v. Löwis5c88d812009-01-02 20:47:48 +000035#include "malloc.h" /* for alloca */
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000036#endif
Martin v. Löwis5c88d812009-01-02 20:47:48 +000037
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000038#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000039#undef BYTE
40#include "windows.h"
41#endif
42
Victor Stinnerbd303c12013-11-07 23:07:29 +010043_Py_IDENTIFIER(builtins);
Victor Stinner09054372013-11-06 22:41:44 +010044_Py_IDENTIFIER(excepthook);
Victor Stinner3f36a572013-11-12 21:39:02 +010045_Py_IDENTIFIER(flush);
Victor Stinnerbd303c12013-11-07 23:07:29 +010046_Py_IDENTIFIER(last_traceback);
Victor Stinner09054372013-11-06 22:41:44 +010047_Py_IDENTIFIER(last_type);
48_Py_IDENTIFIER(last_value);
Victor Stinnerbd303c12013-11-07 23:07:29 +010049_Py_IDENTIFIER(ps1);
50_Py_IDENTIFIER(ps2);
51_Py_IDENTIFIER(stdin);
52_Py_IDENTIFIER(stdout);
53_Py_IDENTIFIER(stderr);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +010054_Py_static_string(PyId_string, "<string>");
Victor Stinner09054372013-11-06 22:41:44 +010055
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000056#ifdef __cplusplus
57extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000058#endif
59
Guido van Rossum82598051997-03-05 00:20:32 +000060extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000061
Guido van Rossumb73cc041993-11-01 16:28:59 +000062/* Forward */
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000063static void flush_io(void);
Victor Stinner95701bd2013-11-06 18:41:07 +010064static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000066static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000068static void err_input(perrdetail *);
Victor Stinner7f2fee32011-04-05 00:39:01 +020069static void err_free(perrdetail *);
xdegayee0582a32017-11-12 16:50:48 +010070static int PyRun_InteractiveOneObjectEx(FILE *, PyObject *, PyCompilerFlags *);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000071
Guido van Rossum1984f1e1992-08-04 12:41:02 +000072/* Parse input from a file and execute it */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000073int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000074PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +000076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 if (filename == NULL)
78 filename = "???";
79 if (Py_FdIsInteractive(fp, filename)) {
80 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
81 if (closeit)
82 fclose(fp);
83 return err;
84 }
85 else
86 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000087}
88
89int
Victor Stinner95701bd2013-11-06 18:41:07 +010090PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +000091{
Victor Stinner95701bd2013-11-06 18:41:07 +010092 PyObject *filename, *v;
93 int ret, err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 PyCompilerFlags local_flags;
xdegayee0582a32017-11-12 16:50:48 +010095 int nomem_count = 0;
Victor Stinner25420fe2017-11-20 18:12:22 -080096#ifdef Py_REF_DEBUG
Victor Stinner331a6a52019-05-27 16:39:22 +020097 int show_ref_count = _PyInterpreterState_Get()->config.show_ref_count;
Victor Stinner25420fe2017-11-20 18:12:22 -080098#endif
Jeremy Hylton9f324e92001-03-01 22:59:14 +000099
Victor Stinner95701bd2013-11-06 18:41:07 +0100100 filename = PyUnicode_DecodeFSDefault(filename_str);
101 if (filename == NULL) {
102 PyErr_Print();
103 return -1;
104 }
105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 if (flags == NULL) {
107 flags = &local_flags;
108 local_flags.cf_flags = 0;
Guido van Rossum495da292019-03-07 12:38:08 -0800109 local_flags.cf_feature_version = PY_MINOR_VERSION;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 }
Victor Stinner09054372013-11-06 22:41:44 +0100111 v = _PySys_GetObjectId(&PyId_ps1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 if (v == NULL) {
Victor Stinner09054372013-11-06 22:41:44 +0100113 _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> "));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 Py_XDECREF(v);
115 }
Victor Stinner09054372013-11-06 22:41:44 +0100116 v = _PySys_GetObjectId(&PyId_ps2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 if (v == NULL) {
Victor Stinner09054372013-11-06 22:41:44 +0100118 _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... "));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 Py_XDECREF(v);
120 }
xdegayee0582a32017-11-12 16:50:48 +0100121 err = 0;
122 do {
123 ret = PyRun_InteractiveOneObjectEx(fp, filename, flags);
124 if (ret == -1 && PyErr_Occurred()) {
125 /* Prevent an endless loop after multiple consecutive MemoryErrors
126 * while still allowing an interactive command to fail with a
127 * MemoryError. */
128 if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
129 if (++nomem_count > 16) {
130 PyErr_Clear();
131 err = -1;
132 break;
133 }
134 } else {
135 nomem_count = 0;
136 }
137 PyErr_Print();
138 flush_io();
139 } else {
140 nomem_count = 0;
141 }
Eric Snowdae02762017-09-14 00:35:58 -0700142#ifdef Py_REF_DEBUG
Victor Stinner25420fe2017-11-20 18:12:22 -0800143 if (show_ref_count) {
Eric Snowdae02762017-09-14 00:35:58 -0700144 _PyDebug_PrintTotalRefs();
Victor Stinner25420fe2017-11-20 18:12:22 -0800145 }
Eric Snowdae02762017-09-14 00:35:58 -0700146#endif
xdegayee0582a32017-11-12 16:50:48 +0100147 } while (ret != E_EOF);
Victor Stinner95701bd2013-11-06 18:41:07 +0100148 Py_DECREF(filename);
149 return err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000150}
151
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000152/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000153static int PARSER_FLAGS(PyCompilerFlags *flags)
154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 int parser_flags = 0;
156 if (!flags)
157 return 0;
158 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
159 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
160 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
161 parser_flags |= PyPARSE_IGNORE_COOKIE;
162 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
163 parser_flags |= PyPARSE_BARRY_AS_BDFL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800164 if (flags->cf_flags & PyCF_TYPE_COMMENTS)
165 parser_flags |= PyPARSE_TYPE_COMMENTS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000167}
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000168
Thomas Wouters89f507f2006-12-13 04:49:30 +0000169#if 0
170/* Keep an example of flags with future keyword support. */
171#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
173 PyPARSE_DONT_IMPLY_DEDENT : 0) \
174 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
175 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000176#endif
177
xdegayee0582a32017-11-12 16:50:48 +0100178/* A PyRun_InteractiveOneObject() auxiliary function that does not print the
179 * error on failure. */
180static int
181PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
182 PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000183{
Victor Stinner95701bd2013-11-06 18:41:07 +0100184 PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 mod_ty mod;
186 PyArena *arena;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200187 const char *ps1 = "", *ps2 = "", *enc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 int errcode = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200189 _Py_IDENTIFIER(encoding);
Victor Stinner95701bd2013-11-06 18:41:07 +0100190 _Py_IDENTIFIER(__main__);
191
192 mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */
193 if (mod_name == NULL) {
Victor Stinner95701bd2013-11-06 18:41:07 +0100194 return -1;
195 }
Tim Petersfe2127d2001-07-16 05:37:24 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (fp == stdin) {
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -0400198 /* Fetch encoding from sys.stdin if possible. */
Victor Stinnerbd303c12013-11-07 23:07:29 +0100199 v = _PySys_GetObjectId(&PyId_stdin);
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -0400200 if (v && v != Py_None) {
201 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
202 if (oenc)
Serhiy Storchaka06515832016-11-20 09:13:07 +0200203 enc = PyUnicode_AsUTF8(oenc);
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -0400204 if (!enc)
205 PyErr_Clear();
206 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 }
Victor Stinner09054372013-11-06 22:41:44 +0100208 v = _PySys_GetObjectId(&PyId_ps1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 if (v != NULL) {
210 v = PyObject_Str(v);
211 if (v == NULL)
212 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +0000213 else if (PyUnicode_Check(v)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200214 ps1 = PyUnicode_AsUTF8(v);
Victor Stinner386fe712010-05-19 00:34:15 +0000215 if (ps1 == NULL) {
216 PyErr_Clear();
217 ps1 = "";
218 }
219 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 }
Victor Stinner09054372013-11-06 22:41:44 +0100221 w = _PySys_GetObjectId(&PyId_ps2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 if (w != NULL) {
223 w = PyObject_Str(w);
224 if (w == NULL)
225 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +0000226 else if (PyUnicode_Check(w)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200227 ps2 = PyUnicode_AsUTF8(w);
Victor Stinner386fe712010-05-19 00:34:15 +0000228 if (ps2 == NULL) {
229 PyErr_Clear();
230 ps2 = "";
231 }
232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 }
234 arena = PyArena_New();
235 if (arena == NULL) {
236 Py_XDECREF(v);
237 Py_XDECREF(w);
238 Py_XDECREF(oenc);
239 return -1;
240 }
Victor Stinner95701bd2013-11-06 18:41:07 +0100241 mod = PyParser_ASTFromFileObject(fp, filename, enc,
242 Py_single_input, ps1, ps2,
243 flags, &errcode, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 Py_XDECREF(v);
245 Py_XDECREF(w);
246 Py_XDECREF(oenc);
247 if (mod == NULL) {
248 PyArena_Free(arena);
249 if (errcode == E_EOF) {
250 PyErr_Clear();
251 return E_EOF;
252 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 return -1;
254 }
Victor Stinner95701bd2013-11-06 18:41:07 +0100255 m = PyImport_AddModuleObject(mod_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 if (m == NULL) {
257 PyArena_Free(arena);
258 return -1;
259 }
260 d = PyModule_GetDict(m);
261 v = run_mod(mod, filename, d, d, flags, arena);
262 PyArena_Free(arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 return -1;
265 }
266 Py_DECREF(v);
Antoine Pitrou9845c7e2014-05-11 13:42:17 +0200267 flush_io();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000269}
270
Victor Stinner95701bd2013-11-06 18:41:07 +0100271int
xdegayee0582a32017-11-12 16:50:48 +0100272PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
273{
274 int res;
275
276 res = PyRun_InteractiveOneObjectEx(fp, filename, flags);
277 if (res == -1) {
278 PyErr_Print();
279 flush_io();
280 }
281 return res;
282}
283
284int
Victor Stinner95701bd2013-11-06 18:41:07 +0100285PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
286{
287 PyObject *filename;
288 int res;
289
290 filename = PyUnicode_DecodeFSDefault(filename_str);
291 if (filename == NULL) {
292 PyErr_Print();
293 return -1;
294 }
295 res = PyRun_InteractiveOneObject(fp, filename, flags);
296 Py_DECREF(filename);
297 return res;
298}
299
300
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000301/* Check whether a file maybe a pyc file: Look at the extension,
302 the file type, and, if we may close it, at the first few bytes. */
303
304static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000305maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000306{
Brett Cannonf299abd2015-04-13 14:21:02 -0400307 if (strcmp(ext, ".pyc") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* Only look into the file if we are allowed to close it, since
311 it then should also be seekable. */
312 if (closeit) {
313 /* Read only two bytes of the magic. If the file was opened in
314 text mode, the bytes 3 and 4 of the magic (\r\n) might not
315 be read as they are on disk. */
316 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
317 unsigned char buf[2];
318 /* Mess: In case of -x, the stream is NOT at its start now,
319 and ungetc() was used to push back the first newline,
320 which makes the current stream position formally undefined,
321 and a x-platform nightmare.
322 Unfortunately, we have no direct way to know whether -x
323 was specified. So we use a terrible hack: if the current
324 stream position is not 0, we assume -x was specified, and
325 give up. Bug 132850 on SourceForge spells out the
326 hopelessness of trying anything else (fseek and ftell
327 don't work predictably x-platform for text-mode files).
328 */
329 int ispyc = 0;
330 if (ftell(fp) == 0) {
331 if (fread(buf, 1, 2, fp) == 2 &&
332 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
333 ispyc = 1;
334 rewind(fp);
335 }
336 return ispyc;
337 }
338 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000339}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000340
Antoine Pitrou32d483c2013-07-30 21:01:23 +0200341static int
342set_main_loader(PyObject *d, const char *filename, const char *loader_name)
Nick Coghlan85e729e2012-07-15 18:09:52 +1000343{
Eric Snow32439d62015-05-02 19:15:18 -0600344 PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader;
Nick Coghlanb7a58942012-07-15 23:21:08 +1000345 int result = 0;
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200346
347 filename_obj = PyUnicode_DecodeFSDefault(filename);
348 if (filename_obj == NULL)
349 return -1;
Victor Stinnercaba55b2018-08-03 15:33:52 +0200350 PyInterpreterState *interp = _PyInterpreterState_Get();
Eric Snow32439d62015-05-02 19:15:18 -0600351 bootstrap = PyObject_GetAttrString(interp->importlib,
352 "_bootstrap_external");
353 if (bootstrap != NULL) {
354 loader_type = PyObject_GetAttrString(bootstrap, loader_name);
355 Py_DECREF(bootstrap);
356 }
Nick Coghlan3f94cbf2012-07-15 19:10:39 +1000357 if (loader_type == NULL) {
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200358 Py_DECREF(filename_obj);
Nick Coghlan3f94cbf2012-07-15 19:10:39 +1000359 return -1;
360 }
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200361 loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
Nick Coghlanb7a58942012-07-15 23:21:08 +1000362 Py_DECREF(loader_type);
363 if (loader == NULL) {
Nick Coghlan85e729e2012-07-15 18:09:52 +1000364 return -1;
365 }
Nick Coghlanb7a58942012-07-15 23:21:08 +1000366 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
367 result = -1;
368 }
Nick Coghlan85e729e2012-07-15 18:09:52 +1000369 Py_DECREF(loader);
Nick Coghlanb7a58942012-07-15 23:21:08 +1000370 return result;
Nick Coghlan85e729e2012-07-15 18:09:52 +1000371}
372
373int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000374PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 PyObject *m, *d, *v;
378 const char *ext;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100379 int set_file_name = 0, ret = -1;
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000380 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 m = PyImport_AddModule("__main__");
383 if (m == NULL)
384 return -1;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100385 Py_INCREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 d = PyModule_GetDict(m);
387 if (PyDict_GetItemString(d, "__file__") == NULL) {
388 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000389 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 if (f == NULL)
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100391 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (PyDict_SetItemString(d, "__file__", f) < 0) {
393 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100394 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 }
Barry Warsaw916048d2011-09-20 14:45:44 -0400396 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
397 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100398 goto done;
Barry Warsaw916048d2011-09-20 14:45:44 -0400399 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 set_file_name = 1;
401 Py_DECREF(f);
402 }
403 len = strlen(filename);
404 ext = filename + len - (len > 4 ? 4 : 0);
405 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Christian Heimes04ac4c12012-09-11 15:47:28 +0200406 FILE *pyc_fp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* Try to run a pyc file. First, re-open in binary */
408 if (closeit)
409 fclose(fp);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200410 if ((pyc_fp = _Py_fopen(filename, "rb")) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 fprintf(stderr, "python: Can't reopen .pyc file\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 goto done;
413 }
Nick Coghlan85e729e2012-07-15 18:09:52 +1000414
415 if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
416 fprintf(stderr, "python: failed to set __main__.__loader__\n");
417 ret = -1;
Christian Heimes04ac4c12012-09-11 15:47:28 +0200418 fclose(pyc_fp);
Nick Coghlan85e729e2012-07-15 18:09:52 +1000419 goto done;
420 }
Christian Heimes04ac4c12012-09-11 15:47:28 +0200421 v = run_pyc_file(pyc_fp, filename, d, d, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 } else {
Nick Coghlan85e729e2012-07-15 18:09:52 +1000423 /* When running from stdin, leave __main__.__loader__ alone */
424 if (strcmp(filename, "<stdin>") != 0 &&
425 set_main_loader(d, filename, "SourceFileLoader") < 0) {
426 fprintf(stderr, "python: failed to set __main__.__loader__\n");
427 ret = -1;
428 goto done;
429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
431 closeit, flags);
432 }
433 flush_io();
434 if (v == NULL) {
Zackery Spytzd8cba5d2018-07-03 13:47:22 -0600435 Py_CLEAR(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 goto done;
438 }
439 Py_DECREF(v);
440 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000441 done:
INADA Naoki82daa602018-11-29 20:01:27 +0900442 if (set_file_name) {
443 if (PyDict_DelItemString(d, "__file__")) {
444 PyErr_Clear();
445 }
446 if (PyDict_DelItemString(d, "__cached__")) {
447 PyErr_Clear();
448 }
449 }
Zackery Spytzd8cba5d2018-07-03 13:47:22 -0600450 Py_XDECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000452}
453
454int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000455PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 PyObject *m, *d, *v;
458 m = PyImport_AddModule("__main__");
459 if (m == NULL)
460 return -1;
461 d = PyModule_GetDict(m);
462 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
463 if (v == NULL) {
464 PyErr_Print();
465 return -1;
466 }
467 Py_DECREF(v);
468 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000469}
470
Barry Warsaw035574d1997-08-29 22:07:17 +0000471static int
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100472parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
473 int *lineno, int *offset, PyObject **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000474{
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300475 int hold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyObject *v;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200477 _Py_IDENTIFIER(msg);
478 _Py_IDENTIFIER(filename);
479 _Py_IDENTIFIER(lineno);
480 _Py_IDENTIFIER(offset);
481 _Py_IDENTIFIER(text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000482
Benjamin Peterson80d50422012-04-03 00:30:38 -0400483 *message = NULL;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100484 *filename = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 /* new style errors. `err' is an instance */
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400487 *message = _PyObject_GetAttrId(err, &PyId_msg);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400488 if (!*message)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +0000490
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400491 v = _PyObject_GetAttrId(err, &PyId_filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400492 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400494 if (v == Py_None) {
495 Py_DECREF(v);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100496 *filename = _PyUnicode_FromId(&PyId_string);
497 if (*filename == NULL)
498 goto finally;
499 Py_INCREF(*filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400500 }
501 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100502 *filename = v;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400503 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000504
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400505 v = _PyObject_GetAttrId(err, &PyId_lineno);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400506 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 goto finally;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300508 hold = _PyLong_AsInt(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 if (hold < 0 && PyErr_Occurred())
511 goto finally;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300512 *lineno = hold;
Barry Warsaw035574d1997-08-29 22:07:17 +0000513
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400514 v = _PyObject_GetAttrId(err, &PyId_offset);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400515 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 goto finally;
517 if (v == Py_None) {
518 *offset = -1;
519 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 } else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300521 hold = _PyLong_AsInt(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (hold < 0 && PyErr_Occurred())
524 goto finally;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300525 *offset = hold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000527
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400528 v = _PyObject_GetAttrId(err, &PyId_text);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400529 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400531 if (v == Py_None) {
532 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 *text = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400534 }
535 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100536 *text = v;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400537 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +0000539
540finally:
Benjamin Peterson80d50422012-04-03 00:30:38 -0400541 Py_XDECREF(*message);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100542 Py_XDECREF(*filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +0000544}
545
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000546static void
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100547print_error_text(PyObject *f, int offset, PyObject *text_obj)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000548{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200549 const char *text;
550 const char *nl;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100551
Serhiy Storchaka06515832016-11-20 09:13:07 +0200552 text = PyUnicode_AsUTF8(text_obj);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100553 if (text == NULL)
554 return;
555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 if (offset >= 0) {
Victor Stinner98ea54c2014-08-15 23:30:40 +0200557 if (offset > 0 && (size_t)offset == strlen(text) && text[offset - 1] == '\n')
Benjamin Petersona95e9772010-10-29 03:28:14 +0000558 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 for (;;) {
560 nl = strchr(text, '\n');
561 if (nl == NULL || nl-text >= offset)
562 break;
563 offset -= (int)(nl+1-text);
564 text = nl+1;
565 }
Martin Panterca3263c2016-12-11 00:18:36 +0000566 while (*text == ' ' || *text == '\t' || *text == '\f') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 text++;
568 offset--;
569 }
570 }
571 PyFile_WriteString(" ", f);
572 PyFile_WriteString(text, f);
573 if (*text == '\0' || text[strlen(text)-1] != '\n')
574 PyFile_WriteString("\n", f);
575 if (offset == -1)
576 return;
577 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +0000578 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000581}
582
Tim Peterscf615b52003-04-19 18:47:02 +0000583
Victor Stinner12083282019-05-17 23:05:29 +0200584int
585_Py_HandleSystemExit(int *exitcode_p)
586{
Victor Stinner331a6a52019-05-27 16:39:22 +0200587 int inspect = _PyInterpreterState_GET_UNSAFE()->config.inspect;
Victor Stinnerc96be812019-05-14 17:34:56 +0200588 if (inspect) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 /* Don't exit if -i flag was given. This flag is set to 0
590 * when entering interactive mode for inspecting. */
Victor Stinner12083282019-05-17 23:05:29 +0200591 return 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200592 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000593
Victor Stinner12083282019-05-17 23:05:29 +0200594 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
595 return 0;
596 }
597
598 PyObject *exception, *value, *tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 PyErr_Fetch(&exception, &value, &tb);
Victor Stinner12083282019-05-17 23:05:29 +0200600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 fflush(stdout);
Victor Stinner12083282019-05-17 23:05:29 +0200602
603 int exitcode = 0;
604 if (value == NULL || value == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 goto done;
Victor Stinner12083282019-05-17 23:05:29 +0200606 }
607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 if (PyExceptionInstance_Check(value)) {
609 /* The error code should be in the `code' attribute. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200610 _Py_IDENTIFIER(code);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200611 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 if (code) {
613 Py_DECREF(value);
614 value = code;
615 if (value == Py_None)
616 goto done;
617 }
618 /* If we failed to dig out the 'code' attribute,
619 just let the else clause below print the error. */
620 }
Victor Stinner12083282019-05-17 23:05:29 +0200621
622 if (PyLong_Check(value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 exitcode = (int)PyLong_AsLong(value);
Victor Stinner12083282019-05-17 23:05:29 +0200624 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 else {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100626 PyObject *sys_stderr = _PySys_GetObjectId(&PyId_stderr);
Nick Coghland979e432014-02-09 10:43:21 +1000627 /* We clear the exception here to avoid triggering the assertion
628 * in PyObject_Str that ensures it won't silently lose exception
629 * details.
630 */
631 PyErr_Clear();
Victor Stinner7126dbc2010-05-21 23:45:42 +0000632 if (sys_stderr != NULL && sys_stderr != Py_None) {
633 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
634 } else {
635 PyObject_Print(value, stderr, Py_PRINT_RAW);
636 fflush(stderr);
637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 PySys_WriteStderr("\n");
639 exitcode = 1;
640 }
Victor Stinner12083282019-05-17 23:05:29 +0200641
Tim Peterscf615b52003-04-19 18:47:02 +0000642 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 /* Restore and clear the exception info, in order to properly decref
644 * the exception, value, and traceback. If we just exit instead,
645 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
646 * some finalizers from running.
647 */
648 PyErr_Restore(exception, value, tb);
649 PyErr_Clear();
Victor Stinner12083282019-05-17 23:05:29 +0200650 *exitcode_p = exitcode;
651 return 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000652}
653
Victor Stinner12083282019-05-17 23:05:29 +0200654
655static void
656handle_system_exit(void)
657{
658 int exitcode;
659 if (_Py_HandleSystemExit(&exitcode)) {
660 Py_Exit(exitcode);
661 }
662}
663
664
Victor Stinner438a12d2019-05-24 17:01:38 +0200665static void
666_PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000669
Victor Stinner12083282019-05-17 23:05:29 +0200670 handle_system_exit();
671
Victor Stinner438a12d2019-05-24 17:01:38 +0200672 _PyErr_Fetch(tstate, &exception, &v, &tb);
673 if (exception == NULL) {
674 goto done;
675 }
676
677 _PyErr_NormalizeException(tstate, &exception, &v, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (tb == NULL) {
679 tb = Py_None;
680 Py_INCREF(tb);
681 }
682 PyException_SetTraceback(v, tb);
Victor Stinner438a12d2019-05-24 17:01:38 +0200683 if (exception == NULL) {
684 goto done;
685 }
686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 /* Now we know v != NULL too */
688 if (set_sys_last_vars) {
xdegaye66caacf2017-10-23 18:08:41 +0200689 if (_PySys_SetObjectId(&PyId_last_type, exception) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +0200690 _PyErr_Clear(tstate);
xdegaye66caacf2017-10-23 18:08:41 +0200691 }
692 if (_PySys_SetObjectId(&PyId_last_value, v) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +0200693 _PyErr_Clear(tstate);
xdegaye66caacf2017-10-23 18:08:41 +0200694 }
695 if (_PySys_SetObjectId(&PyId_last_traceback, tb) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +0200696 _PyErr_Clear(tstate);
xdegaye66caacf2017-10-23 18:08:41 +0200697 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 }
Victor Stinner09054372013-11-06 22:41:44 +0100699 hook = _PySys_GetObjectId(&PyId_excepthook);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 if (hook) {
Victor Stinner71cb64a2016-08-20 00:57:43 +0200701 PyObject* stack[3];
702 PyObject *result;
703
704 stack[0] = exception;
705 stack[1] = v;
706 stack[2] = tb;
Victor Stinner559bb6a2016-08-22 22:48:54 +0200707 result = _PyObject_FastCall(hook, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 if (result == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200709 handle_system_exit();
710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 PyObject *exception2, *v2, *tb2;
Victor Stinner438a12d2019-05-24 17:01:38 +0200712 _PyErr_Fetch(tstate, &exception2, &v2, &tb2);
713 _PyErr_NormalizeException(tstate, &exception2, &v2, &tb2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* It should not be possible for exception2 or v2
715 to be NULL. However PyErr_Display() can't
716 tolerate NULLs, so just be safe. */
717 if (exception2 == NULL) {
718 exception2 = Py_None;
719 Py_INCREF(exception2);
720 }
721 if (v2 == NULL) {
722 v2 = Py_None;
723 Py_INCREF(v2);
724 }
725 fflush(stdout);
726 PySys_WriteStderr("Error in sys.excepthook:\n");
727 PyErr_Display(exception2, v2, tb2);
728 PySys_WriteStderr("\nOriginal exception was:\n");
729 PyErr_Display(exception, v, tb);
730 Py_DECREF(exception2);
731 Py_DECREF(v2);
732 Py_XDECREF(tb2);
733 }
734 Py_XDECREF(result);
Victor Stinner438a12d2019-05-24 17:01:38 +0200735 }
736 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 PySys_WriteStderr("sys.excepthook is missing\n");
738 PyErr_Display(exception, v, tb);
739 }
Victor Stinner438a12d2019-05-24 17:01:38 +0200740
741done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 Py_XDECREF(exception);
743 Py_XDECREF(v);
744 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000745}
746
Victor Stinner438a12d2019-05-24 17:01:38 +0200747void
748_PyErr_Print(PyThreadState *tstate)
749{
750 _PyErr_PrintEx(tstate, 1);
751}
752
753void
754PyErr_PrintEx(int set_sys_last_vars)
755{
756 PyThreadState *tstate = _PyThreadState_GET();
757 _PyErr_PrintEx(tstate, set_sys_last_vars);
758}
759
760void
761PyErr_Print(void)
762{
763 PyErr_PrintEx(1);
764}
765
Benjamin Petersone6528212008-07-15 15:32:09 +0000766static void
767print_exception(PyObject *f, PyObject *value)
768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 int err = 0;
770 PyObject *type, *tb;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200771 _Py_IDENTIFIER(print_file_and_line);
Benjamin Petersone6528212008-07-15 15:32:09 +0000772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 if (!PyExceptionInstance_Check(value)) {
Victor Stinner52ce3b02013-12-09 02:10:08 +0100774 err = PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
775 err += PyFile_WriteString(Py_TYPE(value)->tp_name, f);
776 err += PyFile_WriteString(" found\n", f);
777 if (err)
778 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 return;
780 }
Benjamin Peterson26582602008-08-23 20:08:07 +0000781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 Py_INCREF(value);
783 fflush(stdout);
784 type = (PyObject *) Py_TYPE(value);
785 tb = PyException_GetTraceback(value);
786 if (tb && tb != Py_None)
787 err = PyTraceBack_Print(tb, f);
788 if (err == 0 &&
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200789 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100791 PyObject *message, *filename, *text;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 int lineno, offset;
793 if (!parse_syntax_error(value, &message, &filename,
794 &lineno, &offset, &text))
795 PyErr_Clear();
796 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100797 PyObject *line;
798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 Py_DECREF(value);
800 value = message;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100801
802 line = PyUnicode_FromFormat(" File \"%U\", line %d\n",
803 filename, lineno);
804 Py_DECREF(filename);
805 if (line != NULL) {
806 PyFile_WriteObject(line, f, Py_PRINT_RAW);
807 Py_DECREF(line);
808 }
809
810 if (text != NULL) {
811 print_error_text(f, offset, text);
812 Py_DECREF(text);
813 }
814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 /* Can't be bothered to check all those
816 PyFile_WriteString() calls */
817 if (PyErr_Occurred())
818 err = -1;
819 }
820 }
821 if (err) {
822 /* Don't do anything else */
823 }
824 else {
825 PyObject* moduleName;
Serhiy Storchakaceeef102018-06-15 11:09:43 +0300826 const char *className;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200827 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 assert(PyExceptionClass_Check(type));
829 className = PyExceptionClass_Name(type);
830 if (className != NULL) {
Serhiy Storchakaceeef102018-06-15 11:09:43 +0300831 const char *dot = strrchr(className, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 if (dot != NULL)
833 className = dot+1;
834 }
Benjamin Petersone6528212008-07-15 15:32:09 +0000835
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200836 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 if (moduleName == NULL || !PyUnicode_Check(moduleName))
838 {
Victor Stinner13b21bd2011-05-26 14:25:13 +0200839 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 err = PyFile_WriteString("<unknown>", f);
841 }
842 else {
Serhiy Storchakaf5894dd2016-11-16 15:40:39 +0200843 if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 {
Victor Stinner937114f2013-11-07 00:12:30 +0100845 err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 err += PyFile_WriteString(".", f);
847 }
848 Py_DECREF(moduleName);
849 }
850 if (err == 0) {
851 if (className == NULL)
852 err = PyFile_WriteString("<unknown>", f);
853 else
854 err = PyFile_WriteString(className, f);
855 }
856 }
857 if (err == 0 && (value != Py_None)) {
858 PyObject *s = PyObject_Str(value);
859 /* only print colon if the str() of the
860 object is not the empty string
861 */
Martin Panter3263f682016-02-28 03:16:11 +0000862 if (s == NULL) {
863 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 err = -1;
Martin Panter3263f682016-02-28 03:16:11 +0000865 PyFile_WriteString(": <exception str() failed>", f);
866 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 else if (!PyUnicode_Check(s) ||
Victor Stinnere251d6d2011-11-20 19:20:00 +0100868 PyUnicode_GetLength(s) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 err = PyFile_WriteString(": ", f);
870 if (err == 0)
871 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
872 Py_XDECREF(s);
873 }
874 /* try to write a newline in any case */
Martin Panter3263f682016-02-28 03:16:11 +0000875 if (err < 0) {
876 PyErr_Clear();
877 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 err += PyFile_WriteString("\n", f);
879 Py_XDECREF(tb);
880 Py_DECREF(value);
881 /* If an error happened here, don't show it.
882 XXX This is wrong, but too many callers rely on this behavior. */
883 if (err != 0)
884 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +0000885}
886
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200887static const char cause_message[] =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 "\nThe above exception was the direct cause "
889 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +0000890
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200891static const char context_message[] =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 "\nDuring handling of the above exception, "
893 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +0000894
895static void
896print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 int err = 0, res;
899 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 if (seen != NULL) {
902 /* Exception chaining */
Zane Bitterde860732017-10-17 17:29:39 -0400903 PyObject *value_id = PyLong_FromVoidPtr(value);
904 if (value_id == NULL || PySet_Add(seen, value_id) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 PyErr_Clear();
906 else if (PyExceptionInstance_Check(value)) {
Zane Bitterde860732017-10-17 17:29:39 -0400907 PyObject *check_id = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 cause = PyException_GetCause(value);
909 context = PyException_GetContext(value);
Benjamin Petersond5a1c442012-05-14 22:09:31 -0700910 if (cause) {
Zane Bitterde860732017-10-17 17:29:39 -0400911 check_id = PyLong_FromVoidPtr(cause);
912 if (check_id == NULL) {
913 res = -1;
914 } else {
915 res = PySet_Contains(seen, check_id);
916 Py_DECREF(check_id);
917 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (res == -1)
919 PyErr_Clear();
920 if (res == 0) {
921 print_exception_recursive(
922 f, cause, seen);
923 err |= PyFile_WriteString(
924 cause_message, f);
925 }
926 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -0700927 else if (context &&
928 !((PyBaseExceptionObject *)value)->suppress_context) {
Zane Bitterde860732017-10-17 17:29:39 -0400929 check_id = PyLong_FromVoidPtr(context);
930 if (check_id == NULL) {
931 res = -1;
932 } else {
933 res = PySet_Contains(seen, check_id);
934 Py_DECREF(check_id);
935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 if (res == -1)
937 PyErr_Clear();
938 if (res == 0) {
939 print_exception_recursive(
940 f, context, seen);
941 err |= PyFile_WriteString(
942 context_message, f);
943 }
944 }
945 Py_XDECREF(context);
946 Py_XDECREF(cause);
947 }
Zane Bitterde860732017-10-17 17:29:39 -0400948 Py_XDECREF(value_id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 }
950 print_exception(f, value);
951 if (err != 0)
952 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +0000953}
954
Thomas Wouters477c8d52006-05-27 19:21:47 +0000955void
Victor Stinnercd590a72019-05-28 00:39:52 +0200956_PyErr_Display(PyObject *file, PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000957{
Victor Stinnercd590a72019-05-28 00:39:52 +0200958 assert(file != NULL && file != Py_None);
959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 PyObject *seen;
Antoine Pitrou24201d42013-10-13 21:53:13 +0200961 if (PyExceptionInstance_Check(value)
962 && tb != NULL && PyTraceBack_Check(tb)) {
963 /* Put the traceback on the exception, otherwise it won't get
964 displayed. See issue #18776. */
965 PyObject *cur_tb = PyException_GetTraceback(value);
966 if (cur_tb == NULL)
967 PyException_SetTraceback(value, tb);
968 else
969 Py_DECREF(cur_tb);
970 }
Victor Stinnercd590a72019-05-28 00:39:52 +0200971
972 /* We choose to ignore seen being possibly NULL, and report
973 at least the main exception (it could be a MemoryError).
974 */
975 seen = PySet_New(NULL);
976 if (seen == NULL) {
977 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 }
Victor Stinnercd590a72019-05-28 00:39:52 +0200979 print_exception_recursive(file, value, seen);
980 Py_XDECREF(seen);
981}
982
983void
984PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
985{
986 PyObject *file = _PySys_GetObjectId(&PyId_stderr);
987 if (file == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 _PyObject_Dump(value);
989 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnercd590a72019-05-28 00:39:52 +0200990 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 }
Victor Stinnercd590a72019-05-28 00:39:52 +0200992 if (file == Py_None) {
993 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 }
Victor Stinnercd590a72019-05-28 00:39:52 +0200995
996 _PyErr_Display(file, exception, value, tb);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000997}
998
Guido van Rossum82598051997-03-05 00:20:32 +0000999PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001000PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 PyObject *ret = NULL;
1004 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +01001005 PyArena *arena;
Victor Stinner95701bd2013-11-06 18:41:07 +01001006 PyObject *filename;
1007
1008 filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
1009 if (filename == NULL)
1010 return NULL;
1011
1012 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 if (arena == NULL)
1014 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001015
Victor Stinner95701bd2013-11-06 18:41:07 +01001016 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 if (mod != NULL)
Victor Stinner95701bd2013-11-06 18:41:07 +01001018 ret = run_mod(mod, filename, globals, locals, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 PyArena_Free(arena);
1020 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001021}
1022
1023PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +01001024PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001026{
Victor Stinner95701bd2013-11-06 18:41:07 +01001027 PyObject *ret = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +01001029 PyArena *arena = NULL;
1030 PyObject *filename;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001031
Victor Stinner95701bd2013-11-06 18:41:07 +01001032 filename = PyUnicode_DecodeFSDefault(filename_str);
1033 if (filename == NULL)
1034 goto exit;
1035
1036 arena = PyArena_New();
1037 if (arena == NULL)
1038 goto exit;
1039
1040 mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
1041 flags, NULL, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 if (closeit)
1043 fclose(fp);
1044 if (mod == NULL) {
Victor Stinner95701bd2013-11-06 18:41:07 +01001045 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 }
1047 ret = run_mod(mod, filename, globals, locals, flags, arena);
Victor Stinner95701bd2013-11-06 18:41:07 +01001048
1049exit:
1050 Py_XDECREF(filename);
1051 if (arena != NULL)
1052 PyArena_Free(arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001054}
1055
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001056static void
1057flush_io(void)
1058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 PyObject *f, *r;
1060 PyObject *type, *value, *traceback;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 /* Save the current exception */
1063 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001064
Victor Stinnerbd303c12013-11-07 23:07:29 +01001065 f = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (f != NULL) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001067 r = _PyObject_CallMethodId(f, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (r)
1069 Py_DECREF(r);
1070 else
1071 PyErr_Clear();
1072 }
Victor Stinnerbd303c12013-11-07 23:07:29 +01001073 f = _PySys_GetObjectId(&PyId_stdout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (f != NULL) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001075 r = _PyObject_CallMethodId(f, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 if (r)
1077 Py_DECREF(r);
1078 else
1079 PyErr_Clear();
1080 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001083}
1084
Guido van Rossum82598051997-03-05 00:20:32 +00001085static PyObject *
Gregory P. Smith38f11cc2019-02-16 12:57:40 -08001086run_eval_code_obj(PyCodeObject *co, PyObject *globals, PyObject *locals)
1087{
1088 PyObject *v;
Gregory P. Smithd9bc5432019-02-20 17:35:54 -08001089 /*
1090 * We explicitly re-initialize _Py_UnhandledKeyboardInterrupt every eval
1091 * _just in case_ someone is calling into an embedded Python where they
1092 * don't care about an uncaught KeyboardInterrupt exception (why didn't they
1093 * leave config.install_signal_handlers set to 0?!?) but then later call
1094 * Py_Main() itself (which _checks_ this flag and dies with a signal after
1095 * its interpreter exits). We don't want a previous embedded interpreter's
1096 * uncaught exception to trigger an unexplained signal exit from a future
1097 * Py_Main() based one.
1098 */
1099 _Py_UnhandledKeyboardInterrupt = 0;
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001100
1101 /* Set globals['__builtins__'] if it doesn't exist */
1102 if (globals != NULL && PyDict_GetItemString(globals, "__builtins__") == NULL) {
1103 PyInterpreterState *interp = _PyInterpreterState_Get();
1104 if (PyDict_SetItemString(globals, "__builtins__", interp->builtins) < 0) {
1105 return NULL;
1106 }
1107 }
1108
Gregory P. Smith38f11cc2019-02-16 12:57:40 -08001109 v = PyEval_EvalCode((PyObject*)co, globals, locals);
1110 if (!v && PyErr_Occurred() == PyExc_KeyboardInterrupt) {
1111 _Py_UnhandledKeyboardInterrupt = 1;
1112 }
1113 return v;
1114}
1115
1116static PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +01001117run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
1118 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 PyCodeObject *co;
1121 PyObject *v;
Victor Stinner95701bd2013-11-06 18:41:07 +01001122 co = PyAST_CompileObject(mod, filename, flags, -1, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 if (co == NULL)
1124 return NULL;
Steve Dowerb82e17e2019-05-23 08:45:22 -07001125
1126 if (PySys_Audit("exec", "O", co) < 0) {
1127 Py_DECREF(co);
1128 return NULL;
1129 }
1130
Gregory P. Smith38f11cc2019-02-16 12:57:40 -08001131 v = run_eval_code_obj(co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 Py_DECREF(co);
1133 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001134}
1135
Guido van Rossum82598051997-03-05 00:20:32 +00001136static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001137run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 PyCodeObject *co;
1141 PyObject *v;
1142 long magic;
1143 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 magic = PyMarshal_ReadLongFromFile(fp);
1146 if (magic != PyImport_GetMagicNumber()) {
Victor Stinner5200f552015-03-18 13:56:25 +01001147 if (!PyErr_Occurred())
1148 PyErr_SetString(PyExc_RuntimeError,
1149 "Bad magic number in .pyc file");
Zackery Spytzea737752018-06-23 21:15:24 -06001150 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 }
Benjamin Peterson42aa93b2017-12-09 10:26:52 -08001152 /* Skip the rest of the header. */
1153 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001154 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 (void) PyMarshal_ReadLongFromFile(fp);
Zackery Spytzea737752018-06-23 21:15:24 -06001156 if (PyErr_Occurred()) {
1157 goto error;
1158 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 v = PyMarshal_ReadLastObjectFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 if (v == NULL || !PyCode_Check(v)) {
1161 Py_XDECREF(v);
1162 PyErr_SetString(PyExc_RuntimeError,
1163 "Bad code object in .pyc file");
Zackery Spytzea737752018-06-23 21:15:24 -06001164 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 }
Zackery Spytzea737752018-06-23 21:15:24 -06001166 fclose(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 co = (PyCodeObject *)v;
Gregory P. Smith38f11cc2019-02-16 12:57:40 -08001168 v = run_eval_code_obj(co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 if (v && flags)
1170 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1171 Py_DECREF(co);
1172 return v;
Zackery Spytzea737752018-06-23 21:15:24 -06001173error:
1174 fclose(fp);
1175 return NULL;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001176}
1177
Guido van Rossum82598051997-03-05 00:20:32 +00001178PyObject *
Victor Stinner14e461d2013-08-26 22:28:21 +02001179Py_CompileStringObject(const char *str, PyObject *filename, int start,
1180 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 PyCodeObject *co;
1183 mod_ty mod;
1184 PyArena *arena = PyArena_New();
1185 if (arena == NULL)
1186 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001187
Victor Stinner14e461d2013-08-26 22:28:21 +02001188 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (mod == NULL) {
1190 PyArena_Free(arena);
1191 return NULL;
1192 }
1193 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1194 PyObject *result = PyAST_mod2obj(mod);
1195 PyArena_Free(arena);
1196 return result;
1197 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001198 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 PyArena_Free(arena);
1200 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001201}
1202
Victor Stinner14e461d2013-08-26 22:28:21 +02001203PyObject *
1204Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
1205 PyCompilerFlags *flags, int optimize)
1206{
1207 PyObject *filename, *co;
1208 filename = PyUnicode_DecodeFSDefault(filename_str);
1209 if (filename == NULL)
1210 return NULL;
1211 co = Py_CompileStringObject(str, filename, start, flags, optimize);
1212 Py_DECREF(filename);
1213 return co;
1214}
1215
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001216/* For use in Py_LIMITED_API */
1217#undef Py_CompileString
1218PyObject *
1219PyCompileString(const char *str, const char *filename, int start)
1220{
1221 return Py_CompileStringFlags(str, filename, start, NULL);
1222}
1223
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001224struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +02001225Py_SymtableStringObject(const char *str, PyObject *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 struct symtable *st;
1228 mod_ty mod;
1229 PyCompilerFlags flags;
Victor Stinner14e461d2013-08-26 22:28:21 +02001230 PyArena *arena;
1231
1232 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 if (arena == NULL)
1234 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 flags.cf_flags = 0;
Guido van Rossum495da292019-03-07 12:38:08 -08001237 flags.cf_feature_version = PY_MINOR_VERSION;
Victor Stinner14e461d2013-08-26 22:28:21 +02001238 mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 if (mod == NULL) {
1240 PyArena_Free(arena);
1241 return NULL;
1242 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001243 st = PySymtable_BuildObject(mod, filename, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 PyArena_Free(arena);
1245 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001246}
1247
Victor Stinner14e461d2013-08-26 22:28:21 +02001248struct symtable *
1249Py_SymtableString(const char *str, const char *filename_str, int start)
1250{
1251 PyObject *filename;
1252 struct symtable *st;
1253
1254 filename = PyUnicode_DecodeFSDefault(filename_str);
1255 if (filename == NULL)
1256 return NULL;
1257 st = Py_SymtableStringObject(str, filename, start);
1258 Py_DECREF(filename);
1259 return st;
1260}
1261
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262/* Preferred access to parser is through AST. */
1263mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02001264PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
1265 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 mod_ty mod;
1268 PyCompilerFlags localflags;
1269 perrdetail err;
1270 int iflags = PARSER_FLAGS(flags);
Guido van Rossum495da292019-03-07 12:38:08 -08001271 if (flags && flags->cf_feature_version < 7)
1272 iflags |= PyPARSE_ASYNC_HACKS;
Christian Heimes4d6ec852008-03-26 22:34:47 +00001273
Victor Stinner14e461d2013-08-26 22:28:21 +02001274 node *n = PyParser_ParseStringObject(s, filename,
1275 &_PyParser_Grammar, start, &err,
1276 &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (flags == NULL) {
1278 localflags.cf_flags = 0;
Guido van Rossum495da292019-03-07 12:38:08 -08001279 localflags.cf_feature_version = PY_MINOR_VERSION;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 flags = &localflags;
1281 }
1282 if (n) {
1283 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02001284 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 }
1287 else {
1288 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001289 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001291 err_free(&err);
1292 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293}
1294
1295mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02001296PyParser_ASTFromString(const char *s, const char *filename_str, int start,
1297 PyCompilerFlags *flags, PyArena *arena)
1298{
1299 PyObject *filename;
1300 mod_ty mod;
1301 filename = PyUnicode_DecodeFSDefault(filename_str);
1302 if (filename == NULL)
1303 return NULL;
1304 mod = PyParser_ASTFromStringObject(s, filename, start, flags, arena);
1305 Py_DECREF(filename);
1306 return mod;
1307}
1308
1309mod_ty
1310PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001311 int start, const char *ps1,
1312 const char *ps2, PyCompilerFlags *flags, int *errcode,
Victor Stinner14e461d2013-08-26 22:28:21 +02001313 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 mod_ty mod;
1316 PyCompilerFlags localflags;
1317 perrdetail err;
1318 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001319
Victor Stinner14e461d2013-08-26 22:28:21 +02001320 node *n = PyParser_ParseFileObject(fp, filename, enc,
1321 &_PyParser_Grammar,
1322 start, ps1, ps2, &err, &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 if (flags == NULL) {
1324 localflags.cf_flags = 0;
Guido van Rossum495da292019-03-07 12:38:08 -08001325 localflags.cf_feature_version = PY_MINOR_VERSION;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 flags = &localflags;
1327 }
1328 if (n) {
1329 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02001330 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 }
1333 else {
1334 err_input(&err);
1335 if (errcode)
1336 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02001337 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001339 err_free(&err);
1340 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341}
1342
Victor Stinner14e461d2013-08-26 22:28:21 +02001343mod_ty
1344PyParser_ASTFromFile(FILE *fp, const char *filename_str, const char* enc,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001345 int start, const char *ps1,
1346 const char *ps2, PyCompilerFlags *flags, int *errcode,
Victor Stinner14e461d2013-08-26 22:28:21 +02001347 PyArena *arena)
1348{
1349 mod_ty mod;
1350 PyObject *filename;
1351 filename = PyUnicode_DecodeFSDefault(filename_str);
1352 if (filename == NULL)
1353 return NULL;
1354 mod = PyParser_ASTFromFileObject(fp, filename, enc, start, ps1, ps2,
1355 flags, errcode, arena);
1356 Py_DECREF(filename);
1357 return mod;
1358}
1359
Guido van Rossuma110aa61994-08-29 12:50:44 +00001360/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001361
Guido van Rossuma110aa61994-08-29 12:50:44 +00001362node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001363PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 perrdetail err;
1366 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1367 &_PyParser_Grammar,
1368 start, NULL, NULL, &err, flags);
1369 if (n == NULL)
1370 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001371 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001374}
1375
Guido van Rossuma110aa61994-08-29 12:50:44 +00001376/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001377
Guido van Rossuma110aa61994-08-29 12:50:44 +00001378node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001379PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 perrdetail err;
1382 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1383 start, &err, flags);
1384 if (n == NULL)
1385 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001386 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001388}
1389
1390node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001391PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 perrdetail err;
1395 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1396 &_PyParser_Grammar, start, &err, flags);
1397 if (n == NULL)
1398 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001399 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001401}
1402
Guido van Rossum66ebd912003-04-17 16:02:26 +00001403/* May want to move a more generalized form of this to parsetok.c or
1404 even parser modules. */
1405
1406void
Victor Stinner7f2fee32011-04-05 00:39:01 +02001407PyParser_ClearError(perrdetail *err)
1408{
1409 err_free(err);
1410}
1411
1412void
Guido van Rossum66ebd912003-04-17 16:02:26 +00001413PyParser_SetError(perrdetail *err)
1414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00001416}
1417
Victor Stinner7f2fee32011-04-05 00:39:01 +02001418static void
1419err_free(perrdetail *err)
1420{
1421 Py_CLEAR(err->filename);
1422}
1423
Guido van Rossuma110aa61994-08-29 12:50:44 +00001424/* Set the error appropriate to the given input error code (see errcode.h) */
1425
1426static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001427err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 PyObject *v, *w, *errtype, *errtext;
1430 PyObject *msg_obj = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001431 const char *msg = NULL;
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001432 int offset = err->offset;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 errtype = PyExc_SyntaxError;
1435 switch (err->error) {
1436 case E_ERROR:
Serhiy Storchaka993030a2018-07-12 00:17:53 +03001437 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 case E_SYNTAX:
1439 errtype = PyExc_IndentationError;
1440 if (err->expected == INDENT)
1441 msg = "expected an indented block";
1442 else if (err->token == INDENT)
1443 msg = "unexpected indent";
1444 else if (err->token == DEDENT)
1445 msg = "unexpected unindent";
Serhiy Storchakaaba24ff2018-07-23 23:41:11 +03001446 else if (err->expected == NOTEQUAL) {
1447 errtype = PyExc_SyntaxError;
1448 msg = "with Barry as BDFL, use '<>' instead of '!='";
1449 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 else {
1451 errtype = PyExc_SyntaxError;
1452 msg = "invalid syntax";
1453 }
1454 break;
1455 case E_TOKEN:
1456 msg = "invalid token";
1457 break;
1458 case E_EOFS:
1459 msg = "EOF while scanning triple-quoted string literal";
1460 break;
1461 case E_EOLS:
1462 msg = "EOL while scanning string literal";
1463 break;
1464 case E_INTR:
1465 if (!PyErr_Occurred())
1466 PyErr_SetNone(PyExc_KeyboardInterrupt);
1467 goto cleanup;
1468 case E_NOMEM:
1469 PyErr_NoMemory();
1470 goto cleanup;
1471 case E_EOF:
1472 msg = "unexpected EOF while parsing";
1473 break;
1474 case E_TABSPACE:
1475 errtype = PyExc_TabError;
1476 msg = "inconsistent use of tabs and spaces in indentation";
1477 break;
1478 case E_OVERFLOW:
1479 msg = "expression too long";
1480 break;
1481 case E_DEDENT:
1482 errtype = PyExc_IndentationError;
1483 msg = "unindent does not match any outer indentation level";
1484 break;
1485 case E_TOODEEP:
1486 errtype = PyExc_IndentationError;
1487 msg = "too many levels of indentation";
1488 break;
1489 case E_DECODE: {
1490 PyObject *type, *value, *tb;
1491 PyErr_Fetch(&type, &value, &tb);
1492 msg = "unknown decode error";
1493 if (value != NULL)
1494 msg_obj = PyObject_Str(value);
1495 Py_XDECREF(type);
1496 Py_XDECREF(value);
1497 Py_XDECREF(tb);
1498 break;
1499 }
1500 case E_LINECONT:
1501 msg = "unexpected character after line continuation character";
1502 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 case E_IDENTIFIER:
1505 msg = "invalid character in identifier";
1506 break;
Meador Ingefa21bf02012-01-19 01:08:41 -06001507 case E_BADSINGLE:
1508 msg = "multiple statements found while compiling a single statement";
1509 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 default:
1511 fprintf(stderr, "error=%d\n", err->error);
1512 msg = "unknown parsing error";
1513 break;
1514 }
1515 /* err->text may not be UTF-8 in case of decoding errors.
1516 Explicitly convert to an object. */
1517 if (!err->text) {
1518 errtext = Py_None;
1519 Py_INCREF(Py_None);
1520 } else {
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001521 errtext = PyUnicode_DecodeUTF8(err->text, err->offset,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 "replace");
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001523 if (errtext != NULL) {
1524 Py_ssize_t len = strlen(err->text);
1525 offset = (int)PyUnicode_GET_LENGTH(errtext);
1526 if (len != err->offset) {
1527 Py_DECREF(errtext);
1528 errtext = PyUnicode_DecodeUTF8(err->text, len,
1529 "replace");
1530 }
1531 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001533 v = Py_BuildValue("(OiiN)", err->filename,
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001534 err->lineno, offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 if (v != NULL) {
1536 if (msg_obj)
1537 w = Py_BuildValue("(OO)", msg_obj, v);
1538 else
1539 w = Py_BuildValue("(sO)", msg, v);
1540 } else
1541 w = NULL;
1542 Py_XDECREF(v);
1543 PyErr_SetObject(errtype, w);
1544 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00001545cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 Py_XDECREF(msg_obj);
1547 if (err->text != NULL) {
1548 PyObject_FREE(err->text);
1549 err->text = NULL;
1550 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001551}
1552
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001553
Zachary Warec4821d62014-11-21 23:35:12 -06001554#if defined(USE_STACKCHECK)
1555#if defined(WIN32) && defined(_MSC_VER)
1556
1557/* Stack checking for Microsoft C */
1558
1559#include <malloc.h>
1560#include <excpt.h>
1561
1562/*
1563 * Return non-zero when we run out of memory on the stack; zero otherwise.
1564 */
1565int
1566PyOS_CheckStack(void)
1567{
1568 __try {
1569 /* alloca throws a stack overflow exception if there's
1570 not enough space left on the stack */
1571 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1572 return 0;
1573 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1574 EXCEPTION_EXECUTE_HANDLER :
1575 EXCEPTION_CONTINUE_SEARCH) {
1576 int errcode = _resetstkoflw();
1577 if (errcode == 0)
1578 {
1579 Py_FatalError("Could not reset the stack!");
1580 }
1581 }
1582 return 1;
1583}
1584
1585#endif /* WIN32 && _MSC_VER */
1586
1587/* Alternate implementations can be added here... */
1588
1589#endif /* USE_STACKCHECK */
1590
Martin Panter46f50722016-05-26 05:35:26 +00001591/* Deprecated C API functions still provided for binary compatibility */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592
1593#undef PyParser_SimpleParseFile
Benjamin Petersone5024512018-09-12 12:06:42 -07001594node *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598}
1599
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001600#undef PyParser_SimpleParseString
Benjamin Petersone5024512018-09-12 12:06:42 -07001601node *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001602PyParser_SimpleParseString(const char *str, int start)
1603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001606
1607#undef PyRun_AnyFile
Benjamin Petersone5024512018-09-12 12:06:42 -07001608int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001609PyRun_AnyFile(FILE *fp, const char *name)
1610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001612}
1613
1614#undef PyRun_AnyFileEx
Benjamin Petersone5024512018-09-12 12:06:42 -07001615int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001616PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001619}
1620
1621#undef PyRun_AnyFileFlags
Benjamin Petersone5024512018-09-12 12:06:42 -07001622int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001623PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001626}
1627
1628#undef PyRun_File
Benjamin Petersone5024512018-09-12 12:06:42 -07001629PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001630PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001633}
1634
1635#undef PyRun_FileEx
Benjamin Petersone5024512018-09-12 12:06:42 -07001636PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001637PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001640}
1641
1642#undef PyRun_FileFlags
Benjamin Petersone5024512018-09-12 12:06:42 -07001643PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001644PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001648}
1649
1650#undef PyRun_SimpleFile
Benjamin Petersone5024512018-09-12 12:06:42 -07001651int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001652PyRun_SimpleFile(FILE *f, const char *p)
1653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001655}
1656
1657#undef PyRun_SimpleFileEx
Benjamin Petersone5024512018-09-12 12:06:42 -07001658int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001659PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001662}
1663
1664
1665#undef PyRun_String
Benjamin Petersone5024512018-09-12 12:06:42 -07001666PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001667PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001670}
1671
1672#undef PyRun_SimpleString
Benjamin Petersone5024512018-09-12 12:06:42 -07001673int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001674PyRun_SimpleString(const char *s)
1675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001677}
1678
1679#undef Py_CompileString
Benjamin Petersone5024512018-09-12 12:06:42 -07001680PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001681Py_CompileString(const char *str, const char *p, int s)
1682{
Georg Brandl8334fd92010-12-04 10:26:46 +00001683 return Py_CompileStringExFlags(str, p, s, NULL, -1);
1684}
1685
1686#undef Py_CompileStringFlags
Benjamin Petersone5024512018-09-12 12:06:42 -07001687PyObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00001688Py_CompileStringFlags(const char *str, const char *p, int s,
1689 PyCompilerFlags *flags)
1690{
1691 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001692}
1693
1694#undef PyRun_InteractiveOne
Benjamin Petersone5024512018-09-12 12:06:42 -07001695int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001696PyRun_InteractiveOne(FILE *f, const char *p)
1697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001699}
1700
1701#undef PyRun_InteractiveLoop
Benjamin Petersone5024512018-09-12 12:06:42 -07001702int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001703PyRun_InteractiveLoop(FILE *f, const char *p)
1704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001706}
1707
1708#ifdef __cplusplus
1709}
1710#endif