blob: f1d946a0b0f83dbf7a15214c9a77a46b724cac63 [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;
Miss Islington (bot)92e836c2019-06-12 17:36:03 -070094 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
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;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 }
Victor Stinner09054372013-11-06 22:41:44 +0100109 v = _PySys_GetObjectId(&PyId_ps1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 if (v == NULL) {
Victor Stinner09054372013-11-06 22:41:44 +0100111 _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> "));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 Py_XDECREF(v);
113 }
Victor Stinner09054372013-11-06 22:41:44 +0100114 v = _PySys_GetObjectId(&PyId_ps2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 if (v == NULL) {
Victor Stinner09054372013-11-06 22:41:44 +0100116 _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... "));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 Py_XDECREF(v);
118 }
xdegayee0582a32017-11-12 16:50:48 +0100119 err = 0;
120 do {
121 ret = PyRun_InteractiveOneObjectEx(fp, filename, flags);
122 if (ret == -1 && PyErr_Occurred()) {
123 /* Prevent an endless loop after multiple consecutive MemoryErrors
124 * while still allowing an interactive command to fail with a
125 * MemoryError. */
126 if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
127 if (++nomem_count > 16) {
128 PyErr_Clear();
129 err = -1;
130 break;
131 }
132 } else {
133 nomem_count = 0;
134 }
135 PyErr_Print();
136 flush_io();
137 } else {
138 nomem_count = 0;
139 }
Eric Snowdae02762017-09-14 00:35:58 -0700140#ifdef Py_REF_DEBUG
Victor Stinner25420fe2017-11-20 18:12:22 -0800141 if (show_ref_count) {
Eric Snowdae02762017-09-14 00:35:58 -0700142 _PyDebug_PrintTotalRefs();
Victor Stinner25420fe2017-11-20 18:12:22 -0800143 }
Eric Snowdae02762017-09-14 00:35:58 -0700144#endif
xdegayee0582a32017-11-12 16:50:48 +0100145 } while (ret != E_EOF);
Victor Stinner95701bd2013-11-06 18:41:07 +0100146 Py_DECREF(filename);
147 return err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000148}
149
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000150/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000151static int PARSER_FLAGS(PyCompilerFlags *flags)
152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 int parser_flags = 0;
154 if (!flags)
155 return 0;
156 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
157 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
158 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
159 parser_flags |= PyPARSE_IGNORE_COOKIE;
160 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
161 parser_flags |= PyPARSE_BARRY_AS_BDFL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800162 if (flags->cf_flags & PyCF_TYPE_COMMENTS)
163 parser_flags |= PyPARSE_TYPE_COMMENTS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000165}
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000166
Thomas Wouters89f507f2006-12-13 04:49:30 +0000167#if 0
168/* Keep an example of flags with future keyword support. */
169#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
171 PyPARSE_DONT_IMPLY_DEDENT : 0) \
172 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
173 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000174#endif
175
xdegayee0582a32017-11-12 16:50:48 +0100176/* A PyRun_InteractiveOneObject() auxiliary function that does not print the
177 * error on failure. */
178static int
179PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
180 PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000181{
Victor Stinner95701bd2013-11-06 18:41:07 +0100182 PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 mod_ty mod;
184 PyArena *arena;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200185 const char *ps1 = "", *ps2 = "", *enc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 int errcode = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200187 _Py_IDENTIFIER(encoding);
Victor Stinner95701bd2013-11-06 18:41:07 +0100188 _Py_IDENTIFIER(__main__);
189
190 mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */
191 if (mod_name == NULL) {
Victor Stinner95701bd2013-11-06 18:41:07 +0100192 return -1;
193 }
Tim Petersfe2127d2001-07-16 05:37:24 +0000194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 if (fp == stdin) {
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -0400196 /* Fetch encoding from sys.stdin if possible. */
Victor Stinnerbd303c12013-11-07 23:07:29 +0100197 v = _PySys_GetObjectId(&PyId_stdin);
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -0400198 if (v && v != Py_None) {
199 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
200 if (oenc)
Serhiy Storchaka06515832016-11-20 09:13:07 +0200201 enc = PyUnicode_AsUTF8(oenc);
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -0400202 if (!enc)
203 PyErr_Clear();
204 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 }
Victor Stinner09054372013-11-06 22:41:44 +0100206 v = _PySys_GetObjectId(&PyId_ps1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 if (v != NULL) {
208 v = PyObject_Str(v);
209 if (v == NULL)
210 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +0000211 else if (PyUnicode_Check(v)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200212 ps1 = PyUnicode_AsUTF8(v);
Victor Stinner386fe712010-05-19 00:34:15 +0000213 if (ps1 == NULL) {
214 PyErr_Clear();
215 ps1 = "";
216 }
217 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 }
Victor Stinner09054372013-11-06 22:41:44 +0100219 w = _PySys_GetObjectId(&PyId_ps2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 if (w != NULL) {
221 w = PyObject_Str(w);
222 if (w == NULL)
223 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +0000224 else if (PyUnicode_Check(w)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200225 ps2 = PyUnicode_AsUTF8(w);
Victor Stinner386fe712010-05-19 00:34:15 +0000226 if (ps2 == NULL) {
227 PyErr_Clear();
228 ps2 = "";
229 }
230 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 }
232 arena = PyArena_New();
233 if (arena == NULL) {
234 Py_XDECREF(v);
235 Py_XDECREF(w);
236 Py_XDECREF(oenc);
237 return -1;
238 }
Victor Stinner95701bd2013-11-06 18:41:07 +0100239 mod = PyParser_ASTFromFileObject(fp, filename, enc,
240 Py_single_input, ps1, ps2,
241 flags, &errcode, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 Py_XDECREF(v);
243 Py_XDECREF(w);
244 Py_XDECREF(oenc);
245 if (mod == NULL) {
246 PyArena_Free(arena);
247 if (errcode == E_EOF) {
248 PyErr_Clear();
249 return E_EOF;
250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 return -1;
252 }
Victor Stinner95701bd2013-11-06 18:41:07 +0100253 m = PyImport_AddModuleObject(mod_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 if (m == NULL) {
255 PyArena_Free(arena);
256 return -1;
257 }
258 d = PyModule_GetDict(m);
259 v = run_mod(mod, filename, d, d, flags, arena);
260 PyArena_Free(arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 return -1;
263 }
264 Py_DECREF(v);
Antoine Pitrou9845c7e2014-05-11 13:42:17 +0200265 flush_io();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267}
268
Victor Stinner95701bd2013-11-06 18:41:07 +0100269int
xdegayee0582a32017-11-12 16:50:48 +0100270PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
271{
272 int res;
273
274 res = PyRun_InteractiveOneObjectEx(fp, filename, flags);
275 if (res == -1) {
276 PyErr_Print();
277 flush_io();
278 }
279 return res;
280}
281
282int
Victor Stinner95701bd2013-11-06 18:41:07 +0100283PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
284{
285 PyObject *filename;
286 int res;
287
288 filename = PyUnicode_DecodeFSDefault(filename_str);
289 if (filename == NULL) {
290 PyErr_Print();
291 return -1;
292 }
293 res = PyRun_InteractiveOneObject(fp, filename, flags);
294 Py_DECREF(filename);
295 return res;
296}
297
298
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000299/* Check whether a file maybe a pyc file: Look at the extension,
300 the file type, and, if we may close it, at the first few bytes. */
301
302static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000303maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000304{
Brett Cannonf299abd2015-04-13 14:21:02 -0400305 if (strcmp(ext, ".pyc") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 /* Only look into the file if we are allowed to close it, since
309 it then should also be seekable. */
310 if (closeit) {
311 /* Read only two bytes of the magic. If the file was opened in
312 text mode, the bytes 3 and 4 of the magic (\r\n) might not
313 be read as they are on disk. */
314 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
315 unsigned char buf[2];
316 /* Mess: In case of -x, the stream is NOT at its start now,
317 and ungetc() was used to push back the first newline,
318 which makes the current stream position formally undefined,
319 and a x-platform nightmare.
320 Unfortunately, we have no direct way to know whether -x
321 was specified. So we use a terrible hack: if the current
322 stream position is not 0, we assume -x was specified, and
323 give up. Bug 132850 on SourceForge spells out the
324 hopelessness of trying anything else (fseek and ftell
325 don't work predictably x-platform for text-mode files).
326 */
327 int ispyc = 0;
328 if (ftell(fp) == 0) {
329 if (fread(buf, 1, 2, fp) == 2 &&
330 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
331 ispyc = 1;
332 rewind(fp);
333 }
334 return ispyc;
335 }
336 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000337}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000338
Antoine Pitrou32d483c2013-07-30 21:01:23 +0200339static int
340set_main_loader(PyObject *d, const char *filename, const char *loader_name)
Nick Coghlan85e729e2012-07-15 18:09:52 +1000341{
Eric Snow32439d62015-05-02 19:15:18 -0600342 PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader;
Nick Coghlanb7a58942012-07-15 23:21:08 +1000343 int result = 0;
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200344
345 filename_obj = PyUnicode_DecodeFSDefault(filename);
346 if (filename_obj == NULL)
347 return -1;
Victor Stinnercaba55b2018-08-03 15:33:52 +0200348 PyInterpreterState *interp = _PyInterpreterState_Get();
Eric Snow32439d62015-05-02 19:15:18 -0600349 bootstrap = PyObject_GetAttrString(interp->importlib,
350 "_bootstrap_external");
351 if (bootstrap != NULL) {
352 loader_type = PyObject_GetAttrString(bootstrap, loader_name);
353 Py_DECREF(bootstrap);
354 }
Nick Coghlan3f94cbf2012-07-15 19:10:39 +1000355 if (loader_type == NULL) {
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200356 Py_DECREF(filename_obj);
Nick Coghlan3f94cbf2012-07-15 19:10:39 +1000357 return -1;
358 }
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200359 loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
Nick Coghlanb7a58942012-07-15 23:21:08 +1000360 Py_DECREF(loader_type);
361 if (loader == NULL) {
Nick Coghlan85e729e2012-07-15 18:09:52 +1000362 return -1;
363 }
Nick Coghlanb7a58942012-07-15 23:21:08 +1000364 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
365 result = -1;
366 }
Nick Coghlan85e729e2012-07-15 18:09:52 +1000367 Py_DECREF(loader);
Nick Coghlanb7a58942012-07-15 23:21:08 +1000368 return result;
Nick Coghlan85e729e2012-07-15 18:09:52 +1000369}
370
371int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000372PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyObject *m, *d, *v;
376 const char *ext;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100377 int set_file_name = 0, ret = -1;
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000378 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 m = PyImport_AddModule("__main__");
381 if (m == NULL)
382 return -1;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100383 Py_INCREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 d = PyModule_GetDict(m);
385 if (PyDict_GetItemString(d, "__file__") == NULL) {
386 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000387 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (f == NULL)
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100389 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 if (PyDict_SetItemString(d, "__file__", f) < 0) {
391 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100392 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 }
Barry Warsaw916048d2011-09-20 14:45:44 -0400394 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
395 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100396 goto done;
Barry Warsaw916048d2011-09-20 14:45:44 -0400397 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 set_file_name = 1;
399 Py_DECREF(f);
400 }
401 len = strlen(filename);
402 ext = filename + len - (len > 4 ? 4 : 0);
403 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Christian Heimes04ac4c12012-09-11 15:47:28 +0200404 FILE *pyc_fp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 /* Try to run a pyc file. First, re-open in binary */
406 if (closeit)
407 fclose(fp);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200408 if ((pyc_fp = _Py_fopen(filename, "rb")) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 fprintf(stderr, "python: Can't reopen .pyc file\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 goto done;
411 }
Nick Coghlan85e729e2012-07-15 18:09:52 +1000412
413 if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
414 fprintf(stderr, "python: failed to set __main__.__loader__\n");
415 ret = -1;
Christian Heimes04ac4c12012-09-11 15:47:28 +0200416 fclose(pyc_fp);
Nick Coghlan85e729e2012-07-15 18:09:52 +1000417 goto done;
418 }
Christian Heimes04ac4c12012-09-11 15:47:28 +0200419 v = run_pyc_file(pyc_fp, filename, d, d, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 } else {
Nick Coghlan85e729e2012-07-15 18:09:52 +1000421 /* When running from stdin, leave __main__.__loader__ alone */
422 if (strcmp(filename, "<stdin>") != 0 &&
423 set_main_loader(d, filename, "SourceFileLoader") < 0) {
424 fprintf(stderr, "python: failed to set __main__.__loader__\n");
425 ret = -1;
426 goto done;
427 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
429 closeit, flags);
430 }
431 flush_io();
432 if (v == NULL) {
Zackery Spytzd8cba5d2018-07-03 13:47:22 -0600433 Py_CLEAR(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 goto done;
436 }
437 Py_DECREF(v);
438 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000439 done:
INADA Naoki82daa602018-11-29 20:01:27 +0900440 if (set_file_name) {
441 if (PyDict_DelItemString(d, "__file__")) {
442 PyErr_Clear();
443 }
444 if (PyDict_DelItemString(d, "__cached__")) {
445 PyErr_Clear();
446 }
447 }
Zackery Spytzd8cba5d2018-07-03 13:47:22 -0600448 Py_XDECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000450}
451
452int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000453PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 PyObject *m, *d, *v;
456 m = PyImport_AddModule("__main__");
457 if (m == NULL)
458 return -1;
459 d = PyModule_GetDict(m);
460 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
461 if (v == NULL) {
462 PyErr_Print();
463 return -1;
464 }
465 Py_DECREF(v);
466 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000467}
468
Barry Warsaw035574d1997-08-29 22:07:17 +0000469static int
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100470parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
471 int *lineno, int *offset, PyObject **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000472{
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300473 int hold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 PyObject *v;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200475 _Py_IDENTIFIER(msg);
476 _Py_IDENTIFIER(filename);
477 _Py_IDENTIFIER(lineno);
478 _Py_IDENTIFIER(offset);
479 _Py_IDENTIFIER(text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000480
Benjamin Peterson80d50422012-04-03 00:30:38 -0400481 *message = NULL;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100482 *filename = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 /* new style errors. `err' is an instance */
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400485 *message = _PyObject_GetAttrId(err, &PyId_msg);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400486 if (!*message)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +0000488
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400489 v = _PyObject_GetAttrId(err, &PyId_filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400490 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400492 if (v == Py_None) {
493 Py_DECREF(v);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100494 *filename = _PyUnicode_FromId(&PyId_string);
495 if (*filename == NULL)
496 goto finally;
497 Py_INCREF(*filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400498 }
499 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100500 *filename = v;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400501 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000502
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400503 v = _PyObject_GetAttrId(err, &PyId_lineno);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400504 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 goto finally;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300506 hold = _PyLong_AsInt(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 if (hold < 0 && PyErr_Occurred())
509 goto finally;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300510 *lineno = hold;
Barry Warsaw035574d1997-08-29 22:07:17 +0000511
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400512 v = _PyObject_GetAttrId(err, &PyId_offset);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400513 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 goto finally;
515 if (v == Py_None) {
516 *offset = -1;
517 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 } else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300519 hold = _PyLong_AsInt(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 if (hold < 0 && PyErr_Occurred())
522 goto finally;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300523 *offset = hold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000525
Benjamin Peterson0a9a6362012-04-03 00:35:36 -0400526 v = _PyObject_GetAttrId(err, &PyId_text);
Benjamin Peterson80d50422012-04-03 00:30:38 -0400527 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400529 if (v == Py_None) {
530 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 *text = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400532 }
533 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100534 *text = v;
Benjamin Peterson80d50422012-04-03 00:30:38 -0400535 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +0000537
538finally:
Benjamin Peterson80d50422012-04-03 00:30:38 -0400539 Py_XDECREF(*message);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100540 Py_XDECREF(*filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +0000542}
543
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000544static void
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100545print_error_text(PyObject *f, int offset, PyObject *text_obj)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000546{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200547 const char *text;
548 const char *nl;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100549
Serhiy Storchaka06515832016-11-20 09:13:07 +0200550 text = PyUnicode_AsUTF8(text_obj);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100551 if (text == NULL)
552 return;
553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (offset >= 0) {
Victor Stinner98ea54c2014-08-15 23:30:40 +0200555 if (offset > 0 && (size_t)offset == strlen(text) && text[offset - 1] == '\n')
Benjamin Petersona95e9772010-10-29 03:28:14 +0000556 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 for (;;) {
558 nl = strchr(text, '\n');
559 if (nl == NULL || nl-text >= offset)
560 break;
561 offset -= (int)(nl+1-text);
562 text = nl+1;
563 }
Martin Panterca3263c2016-12-11 00:18:36 +0000564 while (*text == ' ' || *text == '\t' || *text == '\f') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 text++;
566 offset--;
567 }
568 }
569 PyFile_WriteString(" ", f);
570 PyFile_WriteString(text, f);
571 if (*text == '\0' || text[strlen(text)-1] != '\n')
572 PyFile_WriteString("\n", f);
573 if (offset == -1)
574 return;
575 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +0000576 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000579}
580
Tim Peterscf615b52003-04-19 18:47:02 +0000581
Victor Stinner12083282019-05-17 23:05:29 +0200582int
583_Py_HandleSystemExit(int *exitcode_p)
584{
Victor Stinner331a6a52019-05-27 16:39:22 +0200585 int inspect = _PyInterpreterState_GET_UNSAFE()->config.inspect;
Victor Stinnerc96be812019-05-14 17:34:56 +0200586 if (inspect) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 /* Don't exit if -i flag was given. This flag is set to 0
588 * when entering interactive mode for inspecting. */
Victor Stinner12083282019-05-17 23:05:29 +0200589 return 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200590 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000591
Victor Stinner12083282019-05-17 23:05:29 +0200592 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
593 return 0;
594 }
595
596 PyObject *exception, *value, *tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 PyErr_Fetch(&exception, &value, &tb);
Victor Stinner12083282019-05-17 23:05:29 +0200598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 fflush(stdout);
Victor Stinner12083282019-05-17 23:05:29 +0200600
601 int exitcode = 0;
602 if (value == NULL || value == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 goto done;
Victor Stinner12083282019-05-17 23:05:29 +0200604 }
605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 if (PyExceptionInstance_Check(value)) {
607 /* The error code should be in the `code' attribute. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200608 _Py_IDENTIFIER(code);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200609 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 if (code) {
611 Py_DECREF(value);
612 value = code;
613 if (value == Py_None)
614 goto done;
615 }
616 /* If we failed to dig out the 'code' attribute,
617 just let the else clause below print the error. */
618 }
Victor Stinner12083282019-05-17 23:05:29 +0200619
620 if (PyLong_Check(value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 exitcode = (int)PyLong_AsLong(value);
Victor Stinner12083282019-05-17 23:05:29 +0200622 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 else {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100624 PyObject *sys_stderr = _PySys_GetObjectId(&PyId_stderr);
Nick Coghland979e432014-02-09 10:43:21 +1000625 /* We clear the exception here to avoid triggering the assertion
626 * in PyObject_Str that ensures it won't silently lose exception
627 * details.
628 */
629 PyErr_Clear();
Victor Stinner7126dbc2010-05-21 23:45:42 +0000630 if (sys_stderr != NULL && sys_stderr != Py_None) {
631 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
632 } else {
633 PyObject_Print(value, stderr, Py_PRINT_RAW);
634 fflush(stderr);
635 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 PySys_WriteStderr("\n");
637 exitcode = 1;
638 }
Victor Stinner12083282019-05-17 23:05:29 +0200639
Tim Peterscf615b52003-04-19 18:47:02 +0000640 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 /* Restore and clear the exception info, in order to properly decref
642 * the exception, value, and traceback. If we just exit instead,
643 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
644 * some finalizers from running.
645 */
646 PyErr_Restore(exception, value, tb);
647 PyErr_Clear();
Victor Stinner12083282019-05-17 23:05:29 +0200648 *exitcode_p = exitcode;
649 return 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000650}
651
Victor Stinner12083282019-05-17 23:05:29 +0200652
653static void
654handle_system_exit(void)
655{
656 int exitcode;
657 if (_Py_HandleSystemExit(&exitcode)) {
658 Py_Exit(exitcode);
659 }
660}
661
662
Victor Stinner438a12d2019-05-24 17:01:38 +0200663static void
664_PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000667
Victor Stinner12083282019-05-17 23:05:29 +0200668 handle_system_exit();
669
Victor Stinner438a12d2019-05-24 17:01:38 +0200670 _PyErr_Fetch(tstate, &exception, &v, &tb);
671 if (exception == NULL) {
672 goto done;
673 }
674
675 _PyErr_NormalizeException(tstate, &exception, &v, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 if (tb == NULL) {
677 tb = Py_None;
678 Py_INCREF(tb);
679 }
680 PyException_SetTraceback(v, tb);
Victor Stinner438a12d2019-05-24 17:01:38 +0200681 if (exception == NULL) {
682 goto done;
683 }
684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 /* Now we know v != NULL too */
686 if (set_sys_last_vars) {
xdegaye66caacf2017-10-23 18:08:41 +0200687 if (_PySys_SetObjectId(&PyId_last_type, exception) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +0200688 _PyErr_Clear(tstate);
xdegaye66caacf2017-10-23 18:08:41 +0200689 }
690 if (_PySys_SetObjectId(&PyId_last_value, v) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +0200691 _PyErr_Clear(tstate);
xdegaye66caacf2017-10-23 18:08:41 +0200692 }
693 if (_PySys_SetObjectId(&PyId_last_traceback, tb) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +0200694 _PyErr_Clear(tstate);
xdegaye66caacf2017-10-23 18:08:41 +0200695 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 }
Victor Stinner09054372013-11-06 22:41:44 +0100697 hook = _PySys_GetObjectId(&PyId_excepthook);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 if (hook) {
Victor Stinner71cb64a2016-08-20 00:57:43 +0200699 PyObject* stack[3];
700 PyObject *result;
701
702 stack[0] = exception;
703 stack[1] = v;
704 stack[2] = tb;
Victor Stinner559bb6a2016-08-22 22:48:54 +0200705 result = _PyObject_FastCall(hook, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (result == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200707 handle_system_exit();
708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 PyObject *exception2, *v2, *tb2;
Victor Stinner438a12d2019-05-24 17:01:38 +0200710 _PyErr_Fetch(tstate, &exception2, &v2, &tb2);
711 _PyErr_NormalizeException(tstate, &exception2, &v2, &tb2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 /* It should not be possible for exception2 or v2
713 to be NULL. However PyErr_Display() can't
714 tolerate NULLs, so just be safe. */
715 if (exception2 == NULL) {
716 exception2 = Py_None;
717 Py_INCREF(exception2);
718 }
719 if (v2 == NULL) {
720 v2 = Py_None;
721 Py_INCREF(v2);
722 }
723 fflush(stdout);
724 PySys_WriteStderr("Error in sys.excepthook:\n");
725 PyErr_Display(exception2, v2, tb2);
726 PySys_WriteStderr("\nOriginal exception was:\n");
727 PyErr_Display(exception, v, tb);
728 Py_DECREF(exception2);
729 Py_DECREF(v2);
730 Py_XDECREF(tb2);
731 }
732 Py_XDECREF(result);
Victor Stinner438a12d2019-05-24 17:01:38 +0200733 }
734 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 PySys_WriteStderr("sys.excepthook is missing\n");
736 PyErr_Display(exception, v, tb);
737 }
Victor Stinner438a12d2019-05-24 17:01:38 +0200738
739done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 Py_XDECREF(exception);
741 Py_XDECREF(v);
742 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000743}
744
Victor Stinner438a12d2019-05-24 17:01:38 +0200745void
746_PyErr_Print(PyThreadState *tstate)
747{
748 _PyErr_PrintEx(tstate, 1);
749}
750
751void
752PyErr_PrintEx(int set_sys_last_vars)
753{
754 PyThreadState *tstate = _PyThreadState_GET();
755 _PyErr_PrintEx(tstate, set_sys_last_vars);
756}
757
758void
759PyErr_Print(void)
760{
761 PyErr_PrintEx(1);
762}
763
Benjamin Petersone6528212008-07-15 15:32:09 +0000764static void
765print_exception(PyObject *f, PyObject *value)
766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 int err = 0;
768 PyObject *type, *tb;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200769 _Py_IDENTIFIER(print_file_and_line);
Benjamin Petersone6528212008-07-15 15:32:09 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 if (!PyExceptionInstance_Check(value)) {
Victor Stinner52ce3b02013-12-09 02:10:08 +0100772 err = PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
773 err += PyFile_WriteString(Py_TYPE(value)->tp_name, f);
774 err += PyFile_WriteString(" found\n", f);
775 if (err)
776 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 return;
778 }
Benjamin Peterson26582602008-08-23 20:08:07 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 Py_INCREF(value);
781 fflush(stdout);
782 type = (PyObject *) Py_TYPE(value);
783 tb = PyException_GetTraceback(value);
784 if (tb && tb != Py_None)
785 err = PyTraceBack_Print(tb, f);
786 if (err == 0 &&
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200787 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100789 PyObject *message, *filename, *text;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 int lineno, offset;
791 if (!parse_syntax_error(value, &message, &filename,
792 &lineno, &offset, &text))
793 PyErr_Clear();
794 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100795 PyObject *line;
796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 Py_DECREF(value);
798 value = message;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100799
Miss Islington (bot)2683ded2019-07-01 08:11:15 -0700800 line = PyUnicode_FromFormat(" File \"%S\", line %d\n",
Victor Stinnerefa7a0e2013-11-07 12:37:56 +0100801 filename, lineno);
802 Py_DECREF(filename);
803 if (line != NULL) {
804 PyFile_WriteObject(line, f, Py_PRINT_RAW);
805 Py_DECREF(line);
806 }
807
808 if (text != NULL) {
809 print_error_text(f, offset, text);
810 Py_DECREF(text);
811 }
812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 /* Can't be bothered to check all those
814 PyFile_WriteString() calls */
815 if (PyErr_Occurred())
816 err = -1;
817 }
818 }
819 if (err) {
820 /* Don't do anything else */
821 }
822 else {
823 PyObject* moduleName;
Serhiy Storchakaceeef102018-06-15 11:09:43 +0300824 const char *className;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200825 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 assert(PyExceptionClass_Check(type));
827 className = PyExceptionClass_Name(type);
828 if (className != NULL) {
Serhiy Storchakaceeef102018-06-15 11:09:43 +0300829 const char *dot = strrchr(className, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 if (dot != NULL)
831 className = dot+1;
832 }
Benjamin Petersone6528212008-07-15 15:32:09 +0000833
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200834 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 if (moduleName == NULL || !PyUnicode_Check(moduleName))
836 {
Victor Stinner13b21bd2011-05-26 14:25:13 +0200837 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 err = PyFile_WriteString("<unknown>", f);
839 }
840 else {
Serhiy Storchakaf5894dd2016-11-16 15:40:39 +0200841 if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 {
Victor Stinner937114f2013-11-07 00:12:30 +0100843 err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 err += PyFile_WriteString(".", f);
845 }
846 Py_DECREF(moduleName);
847 }
848 if (err == 0) {
849 if (className == NULL)
850 err = PyFile_WriteString("<unknown>", f);
851 else
852 err = PyFile_WriteString(className, f);
853 }
854 }
855 if (err == 0 && (value != Py_None)) {
856 PyObject *s = PyObject_Str(value);
857 /* only print colon if the str() of the
858 object is not the empty string
859 */
Martin Panter3263f682016-02-28 03:16:11 +0000860 if (s == NULL) {
861 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 err = -1;
Martin Panter3263f682016-02-28 03:16:11 +0000863 PyFile_WriteString(": <exception str() failed>", f);
864 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 else if (!PyUnicode_Check(s) ||
Victor Stinnere251d6d2011-11-20 19:20:00 +0100866 PyUnicode_GetLength(s) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 err = PyFile_WriteString(": ", f);
868 if (err == 0)
869 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
870 Py_XDECREF(s);
871 }
872 /* try to write a newline in any case */
Martin Panter3263f682016-02-28 03:16:11 +0000873 if (err < 0) {
874 PyErr_Clear();
875 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 err += PyFile_WriteString("\n", f);
877 Py_XDECREF(tb);
878 Py_DECREF(value);
879 /* If an error happened here, don't show it.
880 XXX This is wrong, but too many callers rely on this behavior. */
881 if (err != 0)
882 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +0000883}
884
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200885static const char cause_message[] =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 "\nThe above exception was the direct cause "
887 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +0000888
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200889static const char context_message[] =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 "\nDuring handling of the above exception, "
891 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +0000892
893static void
894print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 int err = 0, res;
897 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 if (seen != NULL) {
900 /* Exception chaining */
Zane Bitterde860732017-10-17 17:29:39 -0400901 PyObject *value_id = PyLong_FromVoidPtr(value);
902 if (value_id == NULL || PySet_Add(seen, value_id) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 PyErr_Clear();
904 else if (PyExceptionInstance_Check(value)) {
Zane Bitterde860732017-10-17 17:29:39 -0400905 PyObject *check_id = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 cause = PyException_GetCause(value);
907 context = PyException_GetContext(value);
Benjamin Petersond5a1c442012-05-14 22:09:31 -0700908 if (cause) {
Zane Bitterde860732017-10-17 17:29:39 -0400909 check_id = PyLong_FromVoidPtr(cause);
910 if (check_id == NULL) {
911 res = -1;
912 } else {
913 res = PySet_Contains(seen, check_id);
914 Py_DECREF(check_id);
915 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 if (res == -1)
917 PyErr_Clear();
918 if (res == 0) {
919 print_exception_recursive(
920 f, cause, seen);
921 err |= PyFile_WriteString(
922 cause_message, f);
923 }
924 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -0700925 else if (context &&
926 !((PyBaseExceptionObject *)value)->suppress_context) {
Zane Bitterde860732017-10-17 17:29:39 -0400927 check_id = PyLong_FromVoidPtr(context);
928 if (check_id == NULL) {
929 res = -1;
930 } else {
931 res = PySet_Contains(seen, check_id);
932 Py_DECREF(check_id);
933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 if (res == -1)
935 PyErr_Clear();
936 if (res == 0) {
937 print_exception_recursive(
938 f, context, seen);
939 err |= PyFile_WriteString(
940 context_message, f);
941 }
942 }
943 Py_XDECREF(context);
944 Py_XDECREF(cause);
945 }
Zane Bitterde860732017-10-17 17:29:39 -0400946 Py_XDECREF(value_id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 }
948 print_exception(f, value);
949 if (err != 0)
950 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +0000951}
952
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953void
Victor Stinnercd590a72019-05-28 00:39:52 +0200954_PyErr_Display(PyObject *file, PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000955{
Victor Stinnercd590a72019-05-28 00:39:52 +0200956 assert(file != NULL && file != Py_None);
957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 PyObject *seen;
Antoine Pitrou24201d42013-10-13 21:53:13 +0200959 if (PyExceptionInstance_Check(value)
960 && tb != NULL && PyTraceBack_Check(tb)) {
961 /* Put the traceback on the exception, otherwise it won't get
962 displayed. See issue #18776. */
963 PyObject *cur_tb = PyException_GetTraceback(value);
964 if (cur_tb == NULL)
965 PyException_SetTraceback(value, tb);
966 else
967 Py_DECREF(cur_tb);
968 }
Victor Stinnercd590a72019-05-28 00:39:52 +0200969
970 /* We choose to ignore seen being possibly NULL, and report
971 at least the main exception (it could be a MemoryError).
972 */
973 seen = PySet_New(NULL);
974 if (seen == NULL) {
975 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 }
Victor Stinnercd590a72019-05-28 00:39:52 +0200977 print_exception_recursive(file, value, seen);
978 Py_XDECREF(seen);
Victor Stinnera85a1d32019-05-28 16:01:17 +0200979
980 /* Call file.flush() */
981 PyObject *res = _PyObject_CallMethodId(file, &PyId_flush, NULL);
982 if (!res) {
983 /* Silently ignore file.flush() error */
984 PyErr_Clear();
985 }
986 else {
987 Py_DECREF(res);
988 }
Victor Stinnercd590a72019-05-28 00:39:52 +0200989}
990
991void
992PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
993{
994 PyObject *file = _PySys_GetObjectId(&PyId_stderr);
995 if (file == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 _PyObject_Dump(value);
997 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnercd590a72019-05-28 00:39:52 +0200998 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 }
Victor Stinnercd590a72019-05-28 00:39:52 +02001000 if (file == Py_None) {
1001 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 }
Victor Stinnercd590a72019-05-28 00:39:52 +02001003
1004 _PyErr_Display(file, exception, value, tb);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001005}
1006
Guido van Rossum82598051997-03-05 00:20:32 +00001007PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001008PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 PyObject *ret = NULL;
1012 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +01001013 PyArena *arena;
Victor Stinner95701bd2013-11-06 18:41:07 +01001014 PyObject *filename;
1015
1016 filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
1017 if (filename == NULL)
1018 return NULL;
1019
1020 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 if (arena == NULL)
1022 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001023
Victor Stinner95701bd2013-11-06 18:41:07 +01001024 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 if (mod != NULL)
Victor Stinner95701bd2013-11-06 18:41:07 +01001026 ret = run_mod(mod, filename, globals, locals, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 PyArena_Free(arena);
1028 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001029}
1030
1031PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +01001032PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001034{
Victor Stinner95701bd2013-11-06 18:41:07 +01001035 PyObject *ret = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +01001037 PyArena *arena = NULL;
1038 PyObject *filename;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001039
Victor Stinner95701bd2013-11-06 18:41:07 +01001040 filename = PyUnicode_DecodeFSDefault(filename_str);
1041 if (filename == NULL)
1042 goto exit;
1043
1044 arena = PyArena_New();
1045 if (arena == NULL)
1046 goto exit;
1047
1048 mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
1049 flags, NULL, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (closeit)
1051 fclose(fp);
1052 if (mod == NULL) {
Victor Stinner95701bd2013-11-06 18:41:07 +01001053 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 }
1055 ret = run_mod(mod, filename, globals, locals, flags, arena);
Victor Stinner95701bd2013-11-06 18:41:07 +01001056
1057exit:
1058 Py_XDECREF(filename);
1059 if (arena != NULL)
1060 PyArena_Free(arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001062}
1063
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001064static void
1065flush_io(void)
1066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 PyObject *f, *r;
1068 PyObject *type, *value, *traceback;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 /* Save the current exception */
1071 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001072
Victor Stinnerbd303c12013-11-07 23:07:29 +01001073 f = _PySys_GetObjectId(&PyId_stderr);
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 }
Victor Stinnerbd303c12013-11-07 23:07:29 +01001081 f = _PySys_GetObjectId(&PyId_stdout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 if (f != NULL) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001083 r = _PyObject_CallMethodId(f, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 if (r)
1085 Py_DECREF(r);
1086 else
1087 PyErr_Clear();
1088 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001091}
1092
Guido van Rossum82598051997-03-05 00:20:32 +00001093static PyObject *
Gregory P. Smith38f11cc2019-02-16 12:57:40 -08001094run_eval_code_obj(PyCodeObject *co, PyObject *globals, PyObject *locals)
1095{
1096 PyObject *v;
Gregory P. Smithd9bc5432019-02-20 17:35:54 -08001097 /*
1098 * We explicitly re-initialize _Py_UnhandledKeyboardInterrupt every eval
1099 * _just in case_ someone is calling into an embedded Python where they
1100 * don't care about an uncaught KeyboardInterrupt exception (why didn't they
1101 * leave config.install_signal_handlers set to 0?!?) but then later call
1102 * Py_Main() itself (which _checks_ this flag and dies with a signal after
1103 * its interpreter exits). We don't want a previous embedded interpreter's
1104 * uncaught exception to trigger an unexplained signal exit from a future
1105 * Py_Main() based one.
1106 */
1107 _Py_UnhandledKeyboardInterrupt = 0;
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001108
1109 /* Set globals['__builtins__'] if it doesn't exist */
1110 if (globals != NULL && PyDict_GetItemString(globals, "__builtins__") == NULL) {
1111 PyInterpreterState *interp = _PyInterpreterState_Get();
1112 if (PyDict_SetItemString(globals, "__builtins__", interp->builtins) < 0) {
1113 return NULL;
1114 }
1115 }
1116
Gregory P. Smith38f11cc2019-02-16 12:57:40 -08001117 v = PyEval_EvalCode((PyObject*)co, globals, locals);
1118 if (!v && PyErr_Occurred() == PyExc_KeyboardInterrupt) {
1119 _Py_UnhandledKeyboardInterrupt = 1;
1120 }
1121 return v;
1122}
1123
1124static PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +01001125run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
1126 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 PyCodeObject *co;
1129 PyObject *v;
Victor Stinner95701bd2013-11-06 18:41:07 +01001130 co = PyAST_CompileObject(mod, filename, flags, -1, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (co == NULL)
1132 return NULL;
Steve Dowerb82e17e2019-05-23 08:45:22 -07001133
1134 if (PySys_Audit("exec", "O", co) < 0) {
1135 Py_DECREF(co);
1136 return NULL;
1137 }
1138
Gregory P. Smith38f11cc2019-02-16 12:57:40 -08001139 v = run_eval_code_obj(co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 Py_DECREF(co);
1141 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001142}
1143
Guido van Rossum82598051997-03-05 00:20:32 +00001144static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001145run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 PyCodeObject *co;
1149 PyObject *v;
1150 long magic;
1151 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 magic = PyMarshal_ReadLongFromFile(fp);
1154 if (magic != PyImport_GetMagicNumber()) {
Victor Stinner5200f552015-03-18 13:56:25 +01001155 if (!PyErr_Occurred())
1156 PyErr_SetString(PyExc_RuntimeError,
1157 "Bad magic number in .pyc file");
Zackery Spytzea737752018-06-23 21:15:24 -06001158 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 }
Benjamin Peterson42aa93b2017-12-09 10:26:52 -08001160 /* Skip the rest of the header. */
1161 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001162 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 (void) PyMarshal_ReadLongFromFile(fp);
Zackery Spytzea737752018-06-23 21:15:24 -06001164 if (PyErr_Occurred()) {
1165 goto error;
1166 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 v = PyMarshal_ReadLastObjectFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 if (v == NULL || !PyCode_Check(v)) {
1169 Py_XDECREF(v);
1170 PyErr_SetString(PyExc_RuntimeError,
1171 "Bad code object in .pyc file");
Zackery Spytzea737752018-06-23 21:15:24 -06001172 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 }
Zackery Spytzea737752018-06-23 21:15:24 -06001174 fclose(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 co = (PyCodeObject *)v;
Gregory P. Smith38f11cc2019-02-16 12:57:40 -08001176 v = run_eval_code_obj(co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 if (v && flags)
1178 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1179 Py_DECREF(co);
1180 return v;
Zackery Spytzea737752018-06-23 21:15:24 -06001181error:
1182 fclose(fp);
1183 return NULL;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001184}
1185
Guido van Rossum82598051997-03-05 00:20:32 +00001186PyObject *
Victor Stinner14e461d2013-08-26 22:28:21 +02001187Py_CompileStringObject(const char *str, PyObject *filename, int start,
1188 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 PyCodeObject *co;
1191 mod_ty mod;
1192 PyArena *arena = PyArena_New();
1193 if (arena == NULL)
1194 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001195
Victor Stinner14e461d2013-08-26 22:28:21 +02001196 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 if (mod == NULL) {
1198 PyArena_Free(arena);
1199 return NULL;
1200 }
1201 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1202 PyObject *result = PyAST_mod2obj(mod);
1203 PyArena_Free(arena);
1204 return result;
1205 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001206 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 PyArena_Free(arena);
1208 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001209}
1210
Victor Stinner14e461d2013-08-26 22:28:21 +02001211PyObject *
1212Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
1213 PyCompilerFlags *flags, int optimize)
1214{
1215 PyObject *filename, *co;
1216 filename = PyUnicode_DecodeFSDefault(filename_str);
1217 if (filename == NULL)
1218 return NULL;
1219 co = Py_CompileStringObject(str, filename, start, flags, optimize);
1220 Py_DECREF(filename);
1221 return co;
1222}
1223
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001224/* For use in Py_LIMITED_API */
1225#undef Py_CompileString
1226PyObject *
1227PyCompileString(const char *str, const char *filename, int start)
1228{
1229 return Py_CompileStringFlags(str, filename, start, NULL);
1230}
1231
Dino Viehland41540692019-05-28 16:21:17 -07001232const char *
1233_Py_SourceAsString(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
1234{
1235 const char *str;
1236 Py_ssize_t size;
1237 Py_buffer view;
1238
1239 *cmd_copy = NULL;
1240 if (PyUnicode_Check(cmd)) {
1241 cf->cf_flags |= PyCF_IGNORE_COOKIE;
1242 str = PyUnicode_AsUTF8AndSize(cmd, &size);
1243 if (str == NULL)
1244 return NULL;
1245 }
1246 else if (PyBytes_Check(cmd)) {
1247 str = PyBytes_AS_STRING(cmd);
1248 size = PyBytes_GET_SIZE(cmd);
1249 }
1250 else if (PyByteArray_Check(cmd)) {
1251 str = PyByteArray_AS_STRING(cmd);
1252 size = PyByteArray_GET_SIZE(cmd);
1253 }
1254 else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
1255 /* Copy to NUL-terminated buffer. */
1256 *cmd_copy = PyBytes_FromStringAndSize(
1257 (const char *)view.buf, view.len);
1258 PyBuffer_Release(&view);
1259 if (*cmd_copy == NULL) {
1260 return NULL;
1261 }
1262 str = PyBytes_AS_STRING(*cmd_copy);
1263 size = PyBytes_GET_SIZE(*cmd_copy);
1264 }
1265 else {
1266 PyErr_Format(PyExc_TypeError,
1267 "%s() arg 1 must be a %s object",
1268 funcname, what);
1269 return NULL;
1270 }
1271
1272 if (strlen(str) != (size_t)size) {
1273 PyErr_SetString(PyExc_ValueError,
1274 "source code string cannot contain null bytes");
1275 Py_CLEAR(*cmd_copy);
1276 return NULL;
1277 }
1278 return str;
1279}
1280
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001281struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +02001282Py_SymtableStringObject(const char *str, PyObject *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001283{
Miss Islington (bot)92e836c2019-06-12 17:36:03 -07001284 PyCompilerFlags flags = _PyCompilerFlags_INIT;
Dino Viehland41540692019-05-28 16:21:17 -07001285 return _Py_SymtableStringObjectFlags(str, filename, start, &flags);
1286}
1287
1288struct symtable *
1289_Py_SymtableStringObjectFlags(const char *str, PyObject *filename, int start, PyCompilerFlags *flags)
1290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 struct symtable *st;
1292 mod_ty mod;
Victor Stinner14e461d2013-08-26 22:28:21 +02001293 PyArena *arena;
1294
1295 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 if (arena == NULL)
1297 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001298
Dino Viehland41540692019-05-28 16:21:17 -07001299 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 if (mod == NULL) {
1301 PyArena_Free(arena);
1302 return NULL;
1303 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001304 st = PySymtable_BuildObject(mod, filename, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 PyArena_Free(arena);
1306 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001307}
1308
Victor Stinner14e461d2013-08-26 22:28:21 +02001309struct symtable *
1310Py_SymtableString(const char *str, const char *filename_str, int start)
1311{
1312 PyObject *filename;
1313 struct symtable *st;
1314
1315 filename = PyUnicode_DecodeFSDefault(filename_str);
1316 if (filename == NULL)
1317 return NULL;
1318 st = Py_SymtableStringObject(str, filename, start);
1319 Py_DECREF(filename);
1320 return st;
1321}
1322
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323/* Preferred access to parser is through AST. */
1324mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02001325PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
1326 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 mod_ty mod;
Miss Islington (bot)92e836c2019-06-12 17:36:03 -07001329 PyCompilerFlags localflags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 perrdetail err;
1331 int iflags = PARSER_FLAGS(flags);
Guido van Rossum495da292019-03-07 12:38:08 -08001332 if (flags && flags->cf_feature_version < 7)
1333 iflags |= PyPARSE_ASYNC_HACKS;
Christian Heimes4d6ec852008-03-26 22:34:47 +00001334
Victor Stinner14e461d2013-08-26 22:28:21 +02001335 node *n = PyParser_ParseStringObject(s, filename,
1336 &_PyParser_Grammar, start, &err,
1337 &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (flags == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 flags = &localflags;
1340 }
1341 if (n) {
1342 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02001343 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 }
1346 else {
1347 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001348 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001350 err_free(&err);
1351 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352}
1353
1354mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02001355PyParser_ASTFromString(const char *s, const char *filename_str, int start,
1356 PyCompilerFlags *flags, PyArena *arena)
1357{
1358 PyObject *filename;
1359 mod_ty mod;
1360 filename = PyUnicode_DecodeFSDefault(filename_str);
1361 if (filename == NULL)
1362 return NULL;
1363 mod = PyParser_ASTFromStringObject(s, filename, start, flags, arena);
1364 Py_DECREF(filename);
1365 return mod;
1366}
1367
1368mod_ty
1369PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001370 int start, const char *ps1,
1371 const char *ps2, PyCompilerFlags *flags, int *errcode,
Victor Stinner14e461d2013-08-26 22:28:21 +02001372 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 mod_ty mod;
Miss Islington (bot)92e836c2019-06-12 17:36:03 -07001375 PyCompilerFlags localflags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 perrdetail err;
1377 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001378
Victor Stinner14e461d2013-08-26 22:28:21 +02001379 node *n = PyParser_ParseFileObject(fp, filename, enc,
1380 &_PyParser_Grammar,
1381 start, ps1, ps2, &err, &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 if (flags == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 flags = &localflags;
1384 }
1385 if (n) {
1386 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02001387 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 }
1390 else {
1391 err_input(&err);
1392 if (errcode)
1393 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02001394 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001396 err_free(&err);
1397 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398}
1399
Victor Stinner14e461d2013-08-26 22:28:21 +02001400mod_ty
1401PyParser_ASTFromFile(FILE *fp, const char *filename_str, const char* enc,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001402 int start, const char *ps1,
1403 const char *ps2, PyCompilerFlags *flags, int *errcode,
Victor Stinner14e461d2013-08-26 22:28:21 +02001404 PyArena *arena)
1405{
1406 mod_ty mod;
1407 PyObject *filename;
1408 filename = PyUnicode_DecodeFSDefault(filename_str);
1409 if (filename == NULL)
1410 return NULL;
1411 mod = PyParser_ASTFromFileObject(fp, filename, enc, start, ps1, ps2,
1412 flags, errcode, arena);
1413 Py_DECREF(filename);
1414 return mod;
1415}
1416
Guido van Rossuma110aa61994-08-29 12:50:44 +00001417/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001418
Guido van Rossuma110aa61994-08-29 12:50:44 +00001419node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001420PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 perrdetail err;
1423 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1424 &_PyParser_Grammar,
1425 start, NULL, NULL, &err, flags);
1426 if (n == NULL)
1427 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001428 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001431}
1432
Guido van Rossuma110aa61994-08-29 12:50:44 +00001433/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001434
Guido van Rossuma110aa61994-08-29 12:50:44 +00001435node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001436PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 perrdetail err;
1439 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1440 start, &err, flags);
1441 if (n == NULL)
1442 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001443 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001445}
1446
1447node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001448PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 perrdetail err;
1452 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1453 &_PyParser_Grammar, start, &err, flags);
1454 if (n == NULL)
1455 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001456 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001458}
1459
Guido van Rossum66ebd912003-04-17 16:02:26 +00001460/* May want to move a more generalized form of this to parsetok.c or
1461 even parser modules. */
1462
1463void
Victor Stinner7f2fee32011-04-05 00:39:01 +02001464PyParser_ClearError(perrdetail *err)
1465{
1466 err_free(err);
1467}
1468
1469void
Guido van Rossum66ebd912003-04-17 16:02:26 +00001470PyParser_SetError(perrdetail *err)
1471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00001473}
1474
Victor Stinner7f2fee32011-04-05 00:39:01 +02001475static void
1476err_free(perrdetail *err)
1477{
1478 Py_CLEAR(err->filename);
1479}
1480
Guido van Rossuma110aa61994-08-29 12:50:44 +00001481/* Set the error appropriate to the given input error code (see errcode.h) */
1482
1483static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001484err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 PyObject *v, *w, *errtype, *errtext;
1487 PyObject *msg_obj = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001488 const char *msg = NULL;
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001489 int offset = err->offset;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 errtype = PyExc_SyntaxError;
1492 switch (err->error) {
1493 case E_ERROR:
Serhiy Storchaka993030a2018-07-12 00:17:53 +03001494 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 case E_SYNTAX:
1496 errtype = PyExc_IndentationError;
1497 if (err->expected == INDENT)
1498 msg = "expected an indented block";
1499 else if (err->token == INDENT)
1500 msg = "unexpected indent";
1501 else if (err->token == DEDENT)
1502 msg = "unexpected unindent";
Serhiy Storchakaaba24ff2018-07-23 23:41:11 +03001503 else if (err->expected == NOTEQUAL) {
1504 errtype = PyExc_SyntaxError;
1505 msg = "with Barry as BDFL, use '<>' instead of '!='";
1506 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 else {
1508 errtype = PyExc_SyntaxError;
1509 msg = "invalid syntax";
1510 }
1511 break;
1512 case E_TOKEN:
1513 msg = "invalid token";
1514 break;
1515 case E_EOFS:
1516 msg = "EOF while scanning triple-quoted string literal";
1517 break;
1518 case E_EOLS:
1519 msg = "EOL while scanning string literal";
1520 break;
1521 case E_INTR:
1522 if (!PyErr_Occurred())
1523 PyErr_SetNone(PyExc_KeyboardInterrupt);
1524 goto cleanup;
1525 case E_NOMEM:
1526 PyErr_NoMemory();
1527 goto cleanup;
1528 case E_EOF:
1529 msg = "unexpected EOF while parsing";
1530 break;
1531 case E_TABSPACE:
1532 errtype = PyExc_TabError;
1533 msg = "inconsistent use of tabs and spaces in indentation";
1534 break;
1535 case E_OVERFLOW:
1536 msg = "expression too long";
1537 break;
1538 case E_DEDENT:
1539 errtype = PyExc_IndentationError;
1540 msg = "unindent does not match any outer indentation level";
1541 break;
1542 case E_TOODEEP:
1543 errtype = PyExc_IndentationError;
1544 msg = "too many levels of indentation";
1545 break;
1546 case E_DECODE: {
1547 PyObject *type, *value, *tb;
1548 PyErr_Fetch(&type, &value, &tb);
1549 msg = "unknown decode error";
1550 if (value != NULL)
1551 msg_obj = PyObject_Str(value);
1552 Py_XDECREF(type);
1553 Py_XDECREF(value);
1554 Py_XDECREF(tb);
1555 break;
1556 }
1557 case E_LINECONT:
1558 msg = "unexpected character after line continuation character";
1559 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 case E_IDENTIFIER:
1562 msg = "invalid character in identifier";
1563 break;
Meador Ingefa21bf02012-01-19 01:08:41 -06001564 case E_BADSINGLE:
1565 msg = "multiple statements found while compiling a single statement";
1566 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 default:
1568 fprintf(stderr, "error=%d\n", err->error);
1569 msg = "unknown parsing error";
1570 break;
1571 }
1572 /* err->text may not be UTF-8 in case of decoding errors.
1573 Explicitly convert to an object. */
1574 if (!err->text) {
1575 errtext = Py_None;
1576 Py_INCREF(Py_None);
1577 } else {
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001578 errtext = PyUnicode_DecodeUTF8(err->text, err->offset,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 "replace");
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001580 if (errtext != NULL) {
1581 Py_ssize_t len = strlen(err->text);
1582 offset = (int)PyUnicode_GET_LENGTH(errtext);
1583 if (len != err->offset) {
1584 Py_DECREF(errtext);
1585 errtext = PyUnicode_DecodeUTF8(err->text, len,
1586 "replace");
1587 }
1588 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001590 v = Py_BuildValue("(OiiN)", err->filename,
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02001591 err->lineno, offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 if (v != NULL) {
1593 if (msg_obj)
1594 w = Py_BuildValue("(OO)", msg_obj, v);
1595 else
1596 w = Py_BuildValue("(sO)", msg, v);
1597 } else
1598 w = NULL;
1599 Py_XDECREF(v);
1600 PyErr_SetObject(errtype, w);
1601 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00001602cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 Py_XDECREF(msg_obj);
1604 if (err->text != NULL) {
1605 PyObject_FREE(err->text);
1606 err->text = NULL;
1607 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001608}
1609
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001610
Zachary Warec4821d62014-11-21 23:35:12 -06001611#if defined(USE_STACKCHECK)
1612#if defined(WIN32) && defined(_MSC_VER)
1613
1614/* Stack checking for Microsoft C */
1615
1616#include <malloc.h>
1617#include <excpt.h>
1618
1619/*
1620 * Return non-zero when we run out of memory on the stack; zero otherwise.
1621 */
1622int
1623PyOS_CheckStack(void)
1624{
1625 __try {
1626 /* alloca throws a stack overflow exception if there's
1627 not enough space left on the stack */
1628 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1629 return 0;
1630 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1631 EXCEPTION_EXECUTE_HANDLER :
1632 EXCEPTION_CONTINUE_SEARCH) {
1633 int errcode = _resetstkoflw();
1634 if (errcode == 0)
1635 {
1636 Py_FatalError("Could not reset the stack!");
1637 }
1638 }
1639 return 1;
1640}
1641
1642#endif /* WIN32 && _MSC_VER */
1643
1644/* Alternate implementations can be added here... */
1645
1646#endif /* USE_STACKCHECK */
1647
Martin Panter46f50722016-05-26 05:35:26 +00001648/* Deprecated C API functions still provided for binary compatibility */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649
1650#undef PyParser_SimpleParseFile
Benjamin Petersone5024512018-09-12 12:06:42 -07001651node *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655}
1656
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001657#undef PyParser_SimpleParseString
Benjamin Petersone5024512018-09-12 12:06:42 -07001658node *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659PyParser_SimpleParseString(const char *str, int start)
1660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001662}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001663
1664#undef PyRun_AnyFile
Benjamin Petersone5024512018-09-12 12:06:42 -07001665int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001666PyRun_AnyFile(FILE *fp, const char *name)
1667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001669}
1670
1671#undef PyRun_AnyFileEx
Benjamin Petersone5024512018-09-12 12:06:42 -07001672int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001673PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001676}
1677
1678#undef PyRun_AnyFileFlags
Benjamin Petersone5024512018-09-12 12:06:42 -07001679int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001680PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001683}
1684
1685#undef PyRun_File
Benjamin Petersone5024512018-09-12 12:06:42 -07001686PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001687PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001690}
1691
1692#undef PyRun_FileEx
Benjamin Petersone5024512018-09-12 12:06:42 -07001693PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001694PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001697}
1698
1699#undef PyRun_FileFlags
Benjamin Petersone5024512018-09-12 12:06:42 -07001700PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001701PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001705}
1706
1707#undef PyRun_SimpleFile
Benjamin Petersone5024512018-09-12 12:06:42 -07001708int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001709PyRun_SimpleFile(FILE *f, const char *p)
1710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001712}
1713
1714#undef PyRun_SimpleFileEx
Benjamin Petersone5024512018-09-12 12:06:42 -07001715int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001716PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001719}
1720
1721
1722#undef PyRun_String
Benjamin Petersone5024512018-09-12 12:06:42 -07001723PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001724PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001727}
1728
1729#undef PyRun_SimpleString
Benjamin Petersone5024512018-09-12 12:06:42 -07001730int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001731PyRun_SimpleString(const char *s)
1732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001734}
1735
1736#undef Py_CompileString
Benjamin Petersone5024512018-09-12 12:06:42 -07001737PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001738Py_CompileString(const char *str, const char *p, int s)
1739{
Georg Brandl8334fd92010-12-04 10:26:46 +00001740 return Py_CompileStringExFlags(str, p, s, NULL, -1);
1741}
1742
1743#undef Py_CompileStringFlags
Benjamin Petersone5024512018-09-12 12:06:42 -07001744PyObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00001745Py_CompileStringFlags(const char *str, const char *p, int s,
1746 PyCompilerFlags *flags)
1747{
1748 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001749}
1750
1751#undef PyRun_InteractiveOne
Benjamin Petersone5024512018-09-12 12:06:42 -07001752int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001753PyRun_InteractiveOne(FILE *f, const char *p)
1754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001756}
1757
1758#undef PyRun_InteractiveLoop
Benjamin Petersone5024512018-09-12 12:06:42 -07001759int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001760PyRun_InteractiveLoop(FILE *f, const char *p)
1761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001763}
1764
1765#ifdef __cplusplus
1766}
1767#endif