blob: 4f8417a31f3f94d237cf5c0192f3bf787336b71d [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00008#include "grammar.h"
9#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000010#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000014#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000015#include "symtable.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000016#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "ast.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000018#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000019#include "marshal.h"
Antoine Pitrouefb60c02009-10-20 21:29:37 +000020#include "abstract.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000021
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000022#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000023#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000024#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000025
Benjamin Peterson796798b2009-01-02 20:47:27 +000026#ifdef MS_WINDOWS
Martin v. Löwis5344c992009-01-02 20:32:55 +000027#include "malloc.h" /* for alloca */
Benjamin Peterson796798b2009-01-02 20:47:27 +000028#endif
Martin v. Löwis5344c992009-01-02 20:32:55 +000029
Martin v. Löwis73d538b2003-03-05 15:13:47 +000030#ifdef HAVE_LANGINFO_H
31#include <locale.h>
32#include <langinfo.h>
33#endif
34
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000035#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000036#undef BYTE
37#include "windows.h"
38#endif
39
Neal Norwitz4281cef2006-03-04 19:58:13 +000040#ifndef Py_REF_DEBUG
Tim Peters62e97f02006-03-28 21:44:32 +000041#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000042#else /* Py_REF_DEBUG */
Tim Peters62e97f02006-03-28 21:44:32 +000043#define PRINT_TOTAL_REFS() fprintf(stderr, \
44 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
Armin Rigoe1709372006-04-12 17:06:05 +000045 _Py_GetRefTotal())
Neal Norwitz4281cef2006-03-04 19:58:13 +000046#endif
47
Anthony Baxterac6bd462006-04-13 02:06:09 +000048#ifdef __cplusplus
49extern "C" {
50#endif
51
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000052extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000053
Guido van Rossum82598051997-03-05 00:20:32 +000054extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000055
Guido van Rossumb73cc041993-11-01 16:28:59 +000056/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000057static void initmain(void);
58static void initsite(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000059static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Neal Norwitzadb69fc2005-12-17 20:54:49 +000060 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000061static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000062 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000063static void err_input(perrdetail *);
64static void initsigs(void);
Antoine Pitrouefb60c02009-10-20 21:29:37 +000065static void wait_for_thread_shutdown(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static void call_sys_exitfunc(void);
67static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068extern void _PyUnicode_Init(void);
69extern void _PyUnicode_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000070
Mark Hammond8d98d2c2003-04-19 15:41:53 +000071#ifdef WITH_THREAD
72extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
73extern void _PyGILState_Fini(void);
74#endif /* WITH_THREAD */
75
Guido van Rossum82598051997-03-05 00:20:32 +000076int Py_DebugFlag; /* Needed by parser.c */
77int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000078int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl49aafc92007-03-07 00:34:46 +000079int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000080int Py_NoSiteFlag; /* Suppress 'import site' */
Christian Heimes1a6387e2008-03-26 12:49:49 +000081int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Georg Brandl2da0fce2008-01-07 17:09:35 +000082int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000083int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000084int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000085int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000086int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Tim Peters3caca232001-12-06 06:23:26 +000087/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
88 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
89 true divisions (which they will be in 2.3). */
90int _Py_QnewFlag = 0;
Christian Heimesaf748c32008-05-06 22:41:46 +000091int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000092
Brett Cannone9746892008-04-12 23:44:07 +000093/* PyModule_GetWarningsModule is no longer necessary as of 2.6
94since _warnings is builtin. This API should not be used. */
95PyObject *
96PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000097{
Brett Cannone9746892008-04-12 23:44:07 +000098 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +000099}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000100
Guido van Rossum25ce5661997-08-02 03:10:38 +0000101static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000102
Thomas Wouters7e474022000-07-16 12:04:32 +0000103/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000104
105int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000106Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000107{
108 return initialized;
109}
110
Guido van Rossum25ce5661997-08-02 03:10:38 +0000111/* Global initializations. Can be undone by Py_Finalize(). Don't
112 call this twice without an intervening Py_Finalize() call. When
113 initializations fail, a fatal error is issued and the function does
114 not return. On return, the first thread and interpreter state have
115 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000116
Guido van Rossum25ce5661997-08-02 03:10:38 +0000117 Locking: you must hold the interpreter lock while calling this.
118 (If the lock has not yet been initialized, that's equivalent to
119 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000120
Guido van Rossum25ce5661997-08-02 03:10:38 +0000121*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000122
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000123static int
124add_flag(int flag, const char *envs)
125{
126 int env = atoi(envs);
127 if (flag < env)
128 flag = env;
129 if (flag < 1)
130 flag = 1;
131 return flag;
132}
133
Guido van Rossuma027efa1997-05-05 20:56:21 +0000134void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000135Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000136{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000137 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000138 PyThreadState *tstate;
139 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000140 char *p;
Neal Norwitz18aa3882008-08-24 05:04:52 +0000141 char *icodeset = NULL; /* On Windows, input codeset may theoretically
142 differ from output codeset. */
Martin v. Löwis99815892008-06-01 07:20:46 +0000143 char *codeset = NULL;
144 char *errors = NULL;
145 int free_codeset = 0;
146 int overridden = 0;
Martin v. Löwisb12d8572008-06-01 08:06:17 +0000147 PyObject *sys_stream, *sys_isatty;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000148#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwis99815892008-06-01 07:20:46 +0000149 char *saved_locale, *loc_codeset;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000150#endif
Martin v. Löwis99815892008-06-01 07:20:46 +0000151#ifdef MS_WINDOWS
152 char ibuf[128];
153 char buf[128];
154#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000155 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000156
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000157 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000158 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000159 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000160
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000161 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000162 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000163 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000164 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000165 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000166 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Georg Brandl2da0fce2008-01-07 17:09:35 +0000167 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
168 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000169
Guido van Rossuma027efa1997-05-05 20:56:21 +0000170 interp = PyInterpreterState_New();
171 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000172 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000173
Guido van Rossuma027efa1997-05-05 20:56:21 +0000174 tstate = PyThreadState_New(interp);
175 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000176 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000177 (void) PyThreadState_Swap(tstate);
178
Guido van Rossum70d893a2001-08-16 08:21:42 +0000179 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000180
Neal Norwitzb2501f42002-12-31 03:42:13 +0000181 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000182 Py_FatalError("Py_Initialize: can't init frames");
183
Neal Norwitzb2501f42002-12-31 03:42:13 +0000184 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000185 Py_FatalError("Py_Initialize: can't init ints");
186
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000187 if (!_PyLong_Init())
188 Py_FatalError("Py_Initialize: can't init longs");
189
Christian Heimes3497f942008-05-26 12:29:14 +0000190 if (!PyByteArray_Init())
Christian Heimes1a6387e2008-03-26 12:49:49 +0000191 Py_FatalError("Py_Initialize: can't init bytearray");
192
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000193 _PyFloat_Init();
194
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195 interp->modules = PyDict_New();
196 if (interp->modules == NULL)
197 Py_FatalError("Py_Initialize: can't make modules dictionary");
Collin Winter276887b2007-03-12 16:11:39 +0000198 interp->modules_reloading = PyDict_New();
199 if (interp->modules_reloading == NULL)
200 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000201
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000202#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000203 /* Init Unicode implementation; relies on the codec registry */
204 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000205#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000206
Barry Warsawf242aa02000-05-25 23:09:49 +0000207 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000208 if (bimod == NULL)
209 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000210 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000211 if (interp->builtins == NULL)
212 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000213 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000214
215 sysmod = _PySys_Init();
216 if (sysmod == NULL)
217 Py_FatalError("Py_Initialize: can't initialize sys");
218 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000219 if (interp->sysdict == NULL)
220 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000221 Py_INCREF(interp->sysdict);
222 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000223 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000224 PyDict_SetItemString(interp->sysdict, "modules",
225 interp->modules);
226
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000227 _PyImport_Init();
228
Barry Warsawf242aa02000-05-25 23:09:49 +0000229 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000230 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000231 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000232
Barry Warsaw035574d1997-08-29 22:07:17 +0000233 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000234 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000235
Just van Rossum52e14d62002-12-30 22:08:05 +0000236 _PyImportHooks_Init();
237
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000238 if (install_sigs)
239 initsigs(); /* Signal handling stuff, including initintr() */
Brett Cannone9746892008-04-12 23:44:07 +0000240
Georg Brandl3c0fd562008-07-05 10:07:18 +0000241 /* Initialize warnings. */
242 _PyWarnings_Init();
243 if (PySys_HasWarnOptions()) {
244 PyObject *warnings_module = PyImport_ImportModule("warnings");
245 if (!warnings_module)
246 PyErr_Clear();
247 Py_XDECREF(warnings_module);
248 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000249
250 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000251 if (!Py_NoSiteFlag)
252 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000253
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000254 /* auto-thread-state API, if available */
255#ifdef WITH_THREAD
256 _PyGILState_Init(interp, tstate);
257#endif /* WITH_THREAD */
258
Martin v. Löwis99815892008-06-01 07:20:46 +0000259 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
260 p = icodeset = codeset = strdup(p);
261 free_codeset = 1;
262 errors = strchr(p, ':');
263 if (errors) {
264 *errors = '\0';
265 errors++;
266 }
267 overridden = 1;
268 }
269
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000270#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
271 /* On Unix, set the file system encoding according to the
272 user's preference, if the CODESET names a well-known
273 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000274 initialized by other means. Also set the encoding of
Martin v. Löwis99815892008-06-01 07:20:46 +0000275 stdin and stdout if these are terminals, unless overridden. */
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000276
Martin v. Löwis99815892008-06-01 07:20:46 +0000277 if (!overridden || !Py_FileSystemDefaultEncoding) {
278 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
279 setlocale(LC_CTYPE, "");
280 loc_codeset = nl_langinfo(CODESET);
281 if (loc_codeset && *loc_codeset) {
282 PyObject *enc = PyCodec_Encoder(loc_codeset);
283 if (enc) {
284 loc_codeset = strdup(loc_codeset);
285 Py_DECREF(enc);
286 } else {
287 loc_codeset = NULL;
288 PyErr_Clear();
289 }
290 } else
291 loc_codeset = NULL;
292 setlocale(LC_CTYPE, saved_locale);
293 free(saved_locale);
294
295 if (!overridden) {
296 codeset = icodeset = loc_codeset;
297 free_codeset = 1;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000298 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000299
300 /* Initialize Py_FileSystemDefaultEncoding from
301 locale even if PYTHONIOENCODING is set. */
302 if (!Py_FileSystemDefaultEncoding) {
303 Py_FileSystemDefaultEncoding = loc_codeset;
304 if (!overridden)
305 free_codeset = 0;
306 }
307 }
308#endif
309
310#ifdef MS_WINDOWS
311 if (!overridden) {
312 icodeset = ibuf;
Martin v. Löwis6495c8d2008-06-01 08:19:02 +0000313 codeset = buf;
Martin v. Löwis99815892008-06-01 07:20:46 +0000314 sprintf(ibuf, "cp%d", GetConsoleCP());
315 sprintf(buf, "cp%d", GetConsoleOutputCP());
316 }
317#endif
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000318
319 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000320 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000321 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
322 if (!sys_isatty)
323 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000324 if ((overridden ||
325 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000326 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000327 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000328 Py_FatalError("Cannot set codeset of stdin");
329 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000330 Py_XDECREF(sys_isatty);
331
332 sys_stream = PySys_GetObject("stdout");
333 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
334 if (!sys_isatty)
335 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000336 if ((overridden ||
337 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000338 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000339 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000340 Py_FatalError("Cannot set codeset of stdout");
341 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000342 Py_XDECREF(sys_isatty);
343
Martin v. Löwisea62d252006-04-03 10:56:49 +0000344 sys_stream = PySys_GetObject("stderr");
345 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
346 if (!sys_isatty)
347 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000348 if((overridden ||
349 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000350 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000351 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisea62d252006-04-03 10:56:49 +0000352 Py_FatalError("Cannot set codeset of stderr");
353 }
354 Py_XDECREF(sys_isatty);
355
Martin v. Löwis99815892008-06-01 07:20:46 +0000356 if (free_codeset)
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000357 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000358 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000359}
360
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000361void
362Py_Initialize(void)
363{
364 Py_InitializeEx(1);
365}
366
367
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000368#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000369extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000370#endif
371
Guido van Rossum25ce5661997-08-02 03:10:38 +0000372/* Undo the effect of Py_Initialize().
373
374 Beware: if multiple interpreter and/or thread states exist, these
375 are not wiped out; only the current thread and interpreter state
376 are deleted. But since everything else is deleted, those other
377 interpreter and thread states should no longer be used.
378
379 (XXX We should do better, e.g. wipe out all interpreters and
380 threads.)
381
382 Locking: as above.
383
384*/
385
386void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000387Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000388{
389 PyInterpreterState *interp;
390 PyThreadState *tstate;
391
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000392 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000393 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000394
Antoine Pitrouefb60c02009-10-20 21:29:37 +0000395 wait_for_thread_shutdown();
396
Tim Peters384fd102001-01-21 03:40:37 +0000397 /* The interpreter is still entirely intact at this point, and the
398 * exit funcs may be relying on that. In particular, if some thread
399 * or exit func is still waiting to do an import, the import machinery
400 * expects Py_IsInitialized() to return true. So don't say the
401 * interpreter is uninitialized until after the exit funcs have run.
402 * Note that Threading.py uses an exit func to do a join on all the
403 * threads created thru it, so this also protects pending imports in
404 * the threads created via Threading.
405 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000406 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000407 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000408
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000409 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000410 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000411 interp = tstate->interp;
412
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000413 /* Disable signal handling */
414 PyOS_FiniInterrupts();
415
Christian Heimes908caac2008-01-27 23:34:59 +0000416 /* Clear type lookup cache */
417 PyType_ClearCache();
418
Guido van Rossume13ddc92003-04-17 17:29:22 +0000419 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000420 * before all modules are destroyed.
421 * XXX If a __del__ or weakref callback is triggered here, and tries to
422 * XXX import a module, bad things can happen, because Python no
423 * XXX longer believes it's initialized.
424 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
425 * XXX is easy to provoke that way. I've also seen, e.g.,
426 * XXX Exception exceptions.ImportError: 'No module named sha'
427 * XXX in <function callback at 0x008F5718> ignored
428 * XXX but I'm unclear on exactly how that one happens. In any case,
429 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000430 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000431 PyGC_Collect();
Martin v. Löwis45294a92006-04-18 06:24:08 +0000432#ifdef COUNT_ALLOCS
433 /* With COUNT_ALLOCS, it helps to run GC multiple times:
434 each collection might release some types from the type
435 list, so they become garbage. */
436 while (PyGC_Collect() > 0)
437 /* nothing */;
438#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000439
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000440 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000441 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000442
Guido van Rossume13ddc92003-04-17 17:29:22 +0000443 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000444 * new-style class definitions, for example.
445 * XXX This is disabled because it caused too many problems. If
446 * XXX a __del__ or weakref callback triggers here, Python code has
447 * XXX a hard time running, because even the sys module has been
448 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
449 * XXX One symptom is a sequence of information-free messages
450 * XXX coming from threads (if a __del__ or callback is invoked,
451 * XXX other threads can execute too, and any exception they encounter
452 * XXX triggers a comedy of errors as subsystem after subsystem
453 * XXX fails to find what it *expects* to find in sys to help report
454 * XXX the exception and consequent unexpected failures). I've also
455 * XXX seen segfaults then, after adding print statements to the
456 * XXX Python code getting called.
457 */
458#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000459 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000460#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000461
Guido van Rossum1707aad1997-12-08 23:43:45 +0000462 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
463 _PyImport_Fini();
464
465 /* Debugging stuff */
466#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000467 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000468#endif
469
Tim Peters62e97f02006-03-28 21:44:32 +0000470 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000471
Tim Peters9cf25ce2003-04-17 15:21:01 +0000472#ifdef Py_TRACE_REFS
473 /* Display all objects still alive -- this can invoke arbitrary
474 * __repr__ overrides, so requires a mostly-intact interpreter.
475 * Alas, a lot of stuff may still be alive now that will be cleaned
476 * up later.
477 */
Tim Peters269b2a62003-04-17 19:52:29 +0000478 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000479 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000480#endif /* Py_TRACE_REFS */
481
Guido van Rossumd922fa42003-04-15 14:10:09 +0000482 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000483 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000484
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000485 /* Now we decref the exception classes. After this point nothing
486 can raise an exception. That's okay, because each Fini() method
487 below has been checked to make sure no exceptions are ever
488 raised.
489 */
490
491 _PyExc_Fini();
492
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000493 /* Cleanup auto-thread-state */
494#ifdef WITH_THREAD
495 _PyGILState_Fini();
496#endif /* WITH_THREAD */
497
Guido van Rossumd922fa42003-04-15 14:10:09 +0000498 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000499 PyThreadState_Swap(NULL);
500 PyInterpreterState_Delete(interp);
501
Guido van Rossumd922fa42003-04-15 14:10:09 +0000502 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000503 PyMethod_Fini();
504 PyFrame_Fini();
505 PyCFunction_Fini();
506 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000507 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000508 PySet_Fini();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000509 PyString_Fini();
Christian Heimes3497f942008-05-26 12:29:14 +0000510 PyByteArray_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000511 PyInt_Fini();
512 PyFloat_Fini();
Christian Heimesf75dbef2008-02-08 00:11:31 +0000513 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000514
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000515#ifdef Py_USING_UNICODE
516 /* Cleanup Unicode implementation */
517 _PyUnicode_Fini();
518#endif
519
Guido van Rossumcc283f51997-08-05 02:22:03 +0000520 /* XXX Still allocated:
521 - various static ad-hoc pointers to interned strings
522 - int and float free list blocks
523 - whatever various modules and libraries allocate
524 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000525
526 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000527
Tim Peters269b2a62003-04-17 19:52:29 +0000528#ifdef Py_TRACE_REFS
529 /* Display addresses (& refcnts) of all objects still alive.
530 * An address can be used to find the repr of the object, printed
531 * above by _Py_PrintReferences.
532 */
533 if (Py_GETENV("PYTHONDUMPREFS"))
534 _Py_PrintReferenceAddresses(stderr);
535#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000536#ifdef PYMALLOC_DEBUG
537 if (Py_GETENV("PYTHONMALLOCSTATS"))
538 _PyObject_DebugMallocStats();
539#endif
540
Guido van Rossumcc283f51997-08-05 02:22:03 +0000541 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000542}
543
544/* Create and initialize a new interpreter and thread, and return the
545 new thread. This requires that Py_Initialize() has been called
546 first.
547
548 Unsuccessful initialization yields a NULL pointer. Note that *no*
549 exception information is available even in this case -- the
550 exception information is held in the thread, and there is no
551 thread.
552
553 Locking: as above.
554
555*/
556
557PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000558Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000559{
560 PyInterpreterState *interp;
561 PyThreadState *tstate, *save_tstate;
562 PyObject *bimod, *sysmod;
563
564 if (!initialized)
565 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
566
567 interp = PyInterpreterState_New();
568 if (interp == NULL)
569 return NULL;
570
571 tstate = PyThreadState_New(interp);
572 if (tstate == NULL) {
573 PyInterpreterState_Delete(interp);
574 return NULL;
575 }
576
577 save_tstate = PyThreadState_Swap(tstate);
578
579 /* XXX The following is lax in error checking */
580
581 interp->modules = PyDict_New();
Collin Winter276887b2007-03-12 16:11:39 +0000582 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000583
584 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
585 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000586 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000587 if (interp->builtins == NULL)
588 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000589 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000590 }
591 sysmod = _PyImport_FindExtension("sys", "sys");
592 if (bimod != NULL && sysmod != NULL) {
593 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000594 if (interp->sysdict == NULL)
595 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000596 Py_INCREF(interp->sysdict);
597 PySys_SetPath(Py_GetPath());
598 PyDict_SetItemString(interp->sysdict, "modules",
599 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000600 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000601 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000602 if (!Py_NoSiteFlag)
603 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000604 }
605
606 if (!PyErr_Occurred())
607 return tstate;
608
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000609handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000610 /* Oops, it didn't work. Undo it all. */
611
612 PyErr_Print();
613 PyThreadState_Clear(tstate);
614 PyThreadState_Swap(save_tstate);
615 PyThreadState_Delete(tstate);
616 PyInterpreterState_Delete(interp);
617
618 return NULL;
619}
620
621/* Delete an interpreter and its last thread. This requires that the
622 given thread state is current, that the thread has no remaining
623 frames, and that it is its interpreter's only remaining thread.
624 It is a fatal error to violate these constraints.
625
626 (Py_Finalize() doesn't have these constraints -- it zaps
627 everything, regardless.)
628
629 Locking: as above.
630
631*/
632
633void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000634Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000635{
636 PyInterpreterState *interp = tstate->interp;
637
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000638 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000639 Py_FatalError("Py_EndInterpreter: thread is not current");
640 if (tstate->frame != NULL)
641 Py_FatalError("Py_EndInterpreter: thread still has a frame");
642 if (tstate != interp->tstate_head || tstate->next != NULL)
643 Py_FatalError("Py_EndInterpreter: not the last thread");
644
645 PyImport_Cleanup();
646 PyInterpreterState_Clear(interp);
647 PyThreadState_Swap(NULL);
648 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000649}
650
651static char *progname = "python";
652
653void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000654Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000655{
656 if (pn && *pn)
657 progname = pn;
658}
659
660char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000661Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000662{
663 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000664}
665
Guido van Rossuma61691e1998-02-06 22:27:24 +0000666static char *default_home = NULL;
667
668void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000669Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000670{
671 default_home = home;
672}
673
674char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000675Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000676{
677 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000678 if (home == NULL && !Py_IgnoreEnvironmentFlag)
679 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000680 return home;
681}
682
Guido van Rossum6135a871995-01-09 17:53:26 +0000683/* Create __main__ module */
684
685static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000686initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000687{
Guido van Rossum82598051997-03-05 00:20:32 +0000688 PyObject *m, *d;
689 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000690 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000691 Py_FatalError("can't create __main__ module");
692 d = PyModule_GetDict(m);
693 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000694 PyObject *bimod = PyImport_ImportModule("__builtin__");
695 if (bimod == NULL ||
696 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000697 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000698 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000699 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000700}
701
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000702/* Import the site module (not into __main__ though) */
703
704static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000705initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000706{
707 PyObject *m, *f;
708 m = PyImport_ImportModule("site");
709 if (m == NULL) {
710 f = PySys_GetObject("stderr");
711 if (Py_VerboseFlag) {
712 PyFile_WriteString(
713 "'import site' failed; traceback:\n", f);
714 PyErr_Print();
715 }
716 else {
717 PyFile_WriteString(
718 "'import site' failed; use -v for traceback\n", f);
719 PyErr_Clear();
720 }
721 }
722 else {
723 Py_DECREF(m);
724 }
725}
726
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000727/* Parse input from a file and execute it */
728
729int
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000730PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000731 PyCompilerFlags *flags)
732{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000733 if (filename == NULL)
734 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000735 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000736 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000737 if (closeit)
738 fclose(fp);
739 return err;
740 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000741 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000742 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000743}
744
745int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000746PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000747{
Guido van Rossum82598051997-03-05 00:20:32 +0000748 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000749 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000750 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000751
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000752 if (flags == NULL) {
753 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000754 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000755 }
Guido van Rossum82598051997-03-05 00:20:32 +0000756 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000757 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000758 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
Guido van Rossum82598051997-03-05 00:20:32 +0000759 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000760 }
Guido van Rossum82598051997-03-05 00:20:32 +0000761 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000762 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000763 PySys_SetObject("ps2", v = PyString_FromString("... "));
Guido van Rossum82598051997-03-05 00:20:32 +0000764 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000765 }
766 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000767 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Tim Peters62e97f02006-03-28 21:44:32 +0000768 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000769 if (ret == E_EOF)
770 return 0;
771 /*
772 if (ret == E_NOMEM)
773 return -1;
774 */
775 }
776}
777
Eric Smith7c478942008-03-18 23:45:49 +0000778#if 0
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000779/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000780#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000781 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Neal Norwitzca460d92006-09-06 06:28:06 +0000782 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Eric Smith7c478942008-03-18 23:45:49 +0000783#endif
784#if 1
Neal Norwitzca460d92006-09-06 06:28:06 +0000785/* Keep an example of flags with future keyword support. */
786#define PARSER_FLAGS(flags) \
787 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000788 PyPARSE_DONT_IMPLY_DEDENT : 0) \
Christian Heimes3c608332008-03-26 22:01:37 +0000789 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
790 PyPARSE_PRINT_IS_FUNCTION : 0) \
791 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
792 PyPARSE_UNICODE_LITERALS : 0) \
793 ) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000794#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000795
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000796int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000797PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000798{
Guido van Rossum82598051997-03-05 00:20:32 +0000799 PyObject *m, *d, *v, *w;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000801 PyArena *arena;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000802 char *ps1 = "", *ps2 = "";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000804
Guido van Rossum82598051997-03-05 00:20:32 +0000805 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000806 if (v != NULL) {
807 v = PyObject_Str(v);
808 if (v == NULL)
809 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000810 else if (PyString_Check(v))
811 ps1 = PyString_AsString(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000812 }
Guido van Rossum82598051997-03-05 00:20:32 +0000813 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000814 if (w != NULL) {
815 w = PyObject_Str(w);
816 if (w == NULL)
817 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000818 else if (PyString_Check(w))
819 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000820 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000821 arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000822 if (arena == NULL) {
823 Py_XDECREF(v);
824 Py_XDECREF(w);
825 return -1;
826 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000827 mod = PyParser_ASTFromFile(fp, filename,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000829 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000830 Py_XDECREF(v);
831 Py_XDECREF(w);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000833 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 if (errcode == E_EOF) {
835 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000836 return E_EOF;
837 }
Guido van Rossum82598051997-03-05 00:20:32 +0000838 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000840 }
Guido van Rossum82598051997-03-05 00:20:32 +0000841 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000842 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000843 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000844 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000845 }
Guido van Rossum82598051997-03-05 00:20:32 +0000846 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000847 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000848 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000849 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000850 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000851 return -1;
852 }
Guido van Rossum82598051997-03-05 00:20:32 +0000853 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000854 if (Py_FlushLine())
855 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000856 return 0;
857}
858
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000859/* Check whether a file maybe a pyc file: Look at the extension,
860 the file type, and, if we may close it, at the first few bytes. */
861
862static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000863maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000864{
865 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
866 return 1;
867
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000868 /* Only look into the file if we are allowed to close it, since
869 it then should also be seekable. */
870 if (closeit) {
871 /* Read only two bytes of the magic. If the file was opened in
872 text mode, the bytes 3 and 4 of the magic (\r\n) might not
873 be read as they are on disk. */
874 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
875 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000876 /* Mess: In case of -x, the stream is NOT at its start now,
877 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000878 which makes the current stream position formally undefined,
879 and a x-platform nightmare.
880 Unfortunately, we have no direct way to know whether -x
881 was specified. So we use a terrible hack: if the current
882 stream position is not 0, we assume -x was specified, and
883 give up. Bug 132850 on SourceForge spells out the
884 hopelessness of trying anything else (fseek and ftell
885 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000886 */
Tim Peters3e876562001-02-11 04:35:39 +0000887 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000888 if (ftell(fp) == 0) {
889 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000890 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000891 ispyc = 1;
892 rewind(fp);
893 }
Tim Peters3e876562001-02-11 04:35:39 +0000894 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000895 }
896 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000897}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000898
Guido van Rossum0df002c2000-08-27 19:21:52 +0000899int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000900PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000901 PyCompilerFlags *flags)
902{
Guido van Rossum82598051997-03-05 00:20:32 +0000903 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000904 const char *ext;
Matthias Kloseedb5e1e2009-04-04 14:18:13 +0000905 int set_file_name = 0, ret, len;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000906
Guido van Rossum82598051997-03-05 00:20:32 +0000907 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000908 if (m == NULL)
909 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000910 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000911 if (PyDict_GetItemString(d, "__file__") == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000912 PyObject *f = PyString_FromString(filename);
Fred Drake8ed02042002-10-17 21:24:58 +0000913 if (f == NULL)
914 return -1;
915 if (PyDict_SetItemString(d, "__file__", f) < 0) {
916 Py_DECREF(f);
917 return -1;
918 }
Georg Brandlaa2321b2007-03-07 00:40:28 +0000919 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +0000920 Py_DECREF(f);
921 }
Matthias Kloseedb5e1e2009-04-04 14:18:13 +0000922 len = strlen(filename);
923 ext = filename + len - (len > 4 ? 4 : 0);
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000924 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000925 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000926 if (closeit)
927 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000928 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000929 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000930 ret = -1;
931 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000932 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000933 /* Turn on optimization if a .pyo file is given */
934 if (strcmp(ext, ".pyo") == 0)
935 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000936 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000937 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000938 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000939 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000940 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000941 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000942 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000943 ret = -1;
944 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000945 }
Guido van Rossum82598051997-03-05 00:20:32 +0000946 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000947 if (Py_FlushLine())
948 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000949 ret = 0;
950 done:
951 if (set_file_name && PyDict_DelItemString(d, "__file__"))
952 PyErr_Clear();
953 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000954}
955
956int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000957PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000958{
Guido van Rossum82598051997-03-05 00:20:32 +0000959 PyObject *m, *d, *v;
960 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000961 if (m == NULL)
962 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000963 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000964 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000965 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000966 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000967 return -1;
968 }
Guido van Rossum82598051997-03-05 00:20:32 +0000969 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000970 if (Py_FlushLine())
971 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000972 return 0;
973}
974
Barry Warsaw035574d1997-08-29 22:07:17 +0000975static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000976parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
977 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000978{
979 long hold;
980 PyObject *v;
981
982 /* old style errors */
983 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000984 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000985 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000986
987 /* new style errors. `err' is an instance */
988
989 if (! (v = PyObject_GetAttrString(err, "msg")))
990 goto finally;
991 *message = v;
992
993 if (!(v = PyObject_GetAttrString(err, "filename")))
994 goto finally;
995 if (v == Py_None)
996 *filename = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000997 else if (! (*filename = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +0000998 goto finally;
999
1000 Py_DECREF(v);
1001 if (!(v = PyObject_GetAttrString(err, "lineno")))
1002 goto finally;
1003 hold = PyInt_AsLong(v);
1004 Py_DECREF(v);
1005 v = NULL;
1006 if (hold < 0 && PyErr_Occurred())
1007 goto finally;
1008 *lineno = (int)hold;
1009
1010 if (!(v = PyObject_GetAttrString(err, "offset")))
1011 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001012 if (v == Py_None) {
1013 *offset = -1;
1014 Py_DECREF(v);
1015 v = NULL;
1016 } else {
1017 hold = PyInt_AsLong(v);
1018 Py_DECREF(v);
1019 v = NULL;
1020 if (hold < 0 && PyErr_Occurred())
1021 goto finally;
1022 *offset = (int)hold;
1023 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001024
1025 if (!(v = PyObject_GetAttrString(err, "text")))
1026 goto finally;
1027 if (v == Py_None)
1028 *text = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001029 else if (! (*text = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001030 goto finally;
1031 Py_DECREF(v);
1032 return 1;
1033
1034finally:
1035 Py_XDECREF(v);
1036 return 0;
1037}
1038
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001039void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001040PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001041{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001042 PyErr_PrintEx(1);
1043}
1044
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001045static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001046print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001047{
1048 char *nl;
1049 if (offset >= 0) {
1050 if (offset > 0 && offset == (int)strlen(text))
1051 offset--;
1052 for (;;) {
1053 nl = strchr(text, '\n');
1054 if (nl == NULL || nl-text >= offset)
1055 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001056 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001057 text = nl+1;
1058 }
1059 while (*text == ' ' || *text == '\t') {
1060 text++;
1061 offset--;
1062 }
1063 }
1064 PyFile_WriteString(" ", f);
1065 PyFile_WriteString(text, f);
1066 if (*text == '\0' || text[strlen(text)-1] != '\n')
1067 PyFile_WriteString("\n", f);
1068 if (offset == -1)
1069 return;
1070 PyFile_WriteString(" ", f);
1071 offset--;
1072 while (offset > 0) {
1073 PyFile_WriteString(" ", f);
1074 offset--;
1075 }
1076 PyFile_WriteString("^\n", f);
1077}
1078
Guido van Rossum66e8e862001-03-23 17:54:43 +00001079static void
1080handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001081{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001082 PyObject *exception, *value, *tb;
1083 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001084
Georg Brandl49aafc92007-03-07 00:34:46 +00001085 if (Py_InspectFlag)
1086 /* Don't exit if -i flag was given. This flag is set to 0
1087 * when entering interactive mode for inspecting. */
1088 return;
1089
Guido van Rossum66e8e862001-03-23 17:54:43 +00001090 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001091 if (Py_FlushLine())
1092 PyErr_Clear();
1093 fflush(stdout);
1094 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001095 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001096 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001097 /* The error code should be in the `code' attribute. */
1098 PyObject *code = PyObject_GetAttrString(value, "code");
1099 if (code) {
1100 Py_DECREF(value);
1101 value = code;
1102 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001103 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001104 }
1105 /* If we failed to dig out the 'code' attribute,
1106 just let the else clause below print the error. */
1107 }
1108 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001109 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001110 else {
1111 PyObject_Print(value, stderr, Py_PRINT_RAW);
1112 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001113 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001114 }
Tim Peterscf615b52003-04-19 18:47:02 +00001115 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001116 /* Restore and clear the exception info, in order to properly decref
1117 * the exception, value, and traceback. If we just exit instead,
1118 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1119 * some finalizers from running.
1120 */
Tim Peterscf615b52003-04-19 18:47:02 +00001121 PyErr_Restore(exception, value, tb);
1122 PyErr_Clear();
1123 Py_Exit(exitcode);
1124 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001125}
1126
1127void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001128PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001129{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001130 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001131
1132 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1133 handle_system_exit();
1134 }
Guido van Rossum82598051997-03-05 00:20:32 +00001135 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 if (exception == NULL)
1137 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001138 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001139 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001140 return;
Georg Brandl3c0fd562008-07-05 10:07:18 +00001141 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001142 if (set_sys_last_vars) {
1143 PySys_SetObject("last_type", exception);
1144 PySys_SetObject("last_value", v);
1145 PySys_SetObject("last_traceback", tb);
1146 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001147 hook = PySys_GetObject("excepthook");
1148 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001149 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001150 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001151 PyObject *result = PyEval_CallObject(hook, args);
1152 if (result == NULL) {
1153 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001154 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1155 handle_system_exit();
1156 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001157 PyErr_Fetch(&exception2, &v2, &tb2);
1158 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001159 /* It should not be possible for exception2 or v2
1160 to be NULL. However PyErr_Display() can't
1161 tolerate NULLs, so just be safe. */
1162 if (exception2 == NULL) {
1163 exception2 = Py_None;
1164 Py_INCREF(exception2);
1165 }
1166 if (v2 == NULL) {
1167 v2 = Py_None;
1168 Py_INCREF(v2);
1169 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001170 if (Py_FlushLine())
1171 PyErr_Clear();
1172 fflush(stdout);
1173 PySys_WriteStderr("Error in sys.excepthook:\n");
1174 PyErr_Display(exception2, v2, tb2);
1175 PySys_WriteStderr("\nOriginal exception was:\n");
1176 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001177 Py_DECREF(exception2);
1178 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001179 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001180 }
1181 Py_XDECREF(result);
1182 Py_XDECREF(args);
1183 } else {
1184 PySys_WriteStderr("sys.excepthook is missing\n");
1185 PyErr_Display(exception, v, tb);
1186 }
1187 Py_XDECREF(exception);
1188 Py_XDECREF(v);
1189 Py_XDECREF(tb);
1190}
1191
Richard Jones7b9558d2006-05-27 12:29:24 +00001192void
1193PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001194{
1195 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001196 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001197 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001198 if (f == NULL)
1199 fprintf(stderr, "lost sys.stderr\n");
1200 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001201 if (Py_FlushLine())
1202 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001203 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001204 if (tb && tb != Py_None)
1205 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001206 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001207 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001208 {
Guido van Rossum82598051997-03-05 00:20:32 +00001209 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001210 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001211 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001212 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001213 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001214 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001215 else {
1216 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001217 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001218 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001219 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001220 else
Guido van Rossum82598051997-03-05 00:20:32 +00001221 PyFile_WriteString(filename, f);
1222 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001223 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001224 PyFile_WriteString(buf, f);
1225 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001226 if (text != NULL)
1227 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001228 Py_DECREF(value);
1229 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001230 /* Can't be bothered to check all those
1231 PyFile_WriteString() calls */
1232 if (PyErr_Occurred())
1233 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001234 }
1235 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001236 if (err) {
1237 /* Don't do anything else */
1238 }
Brett Cannonbf364092006-03-01 04:25:17 +00001239 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001240 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001241 char* className = PyExceptionClass_Name(exception);
1242 if (className != NULL) {
1243 char *dot = strrchr(className, '.');
1244 if (dot != NULL)
1245 className = dot+1;
1246 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001247
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001248 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001249 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001250 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001251 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001252 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001253 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001254 {
1255 err = PyFile_WriteString(modstr, f);
1256 err += PyFile_WriteString(".", f);
1257 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001258 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001259 }
1260 if (err == 0) {
1261 if (className == NULL)
1262 err = PyFile_WriteString("<unknown>", f);
1263 else
Brett Cannonbf364092006-03-01 04:25:17 +00001264 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001265 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001266 }
1267 else
1268 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001269 if (err == 0 && (value != Py_None)) {
1270 PyObject *s = PyObject_Str(value);
1271 /* only print colon if the str() of the
1272 object is not the empty string
1273 */
1274 if (s == NULL)
1275 err = -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001276 else if (!PyString_Check(s) ||
1277 PyString_GET_SIZE(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001278 err = PyFile_WriteString(": ", f);
1279 if (err == 0)
1280 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1281 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001282 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001283 /* try to write a newline in any case */
1284 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001285 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001286 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001287 /* If an error happened here, don't show it.
1288 XXX This is wrong, but too many callers rely on this behavior. */
1289 if (err != 0)
1290 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001291}
1292
Guido van Rossum82598051997-03-05 00:20:32 +00001293PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001294PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001296{
Neal Norwitze92fba02006-03-04 18:52:26 +00001297 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001298 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001299 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001300 if (arena == NULL)
1301 return NULL;
1302
1303 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001304 if (mod != NULL)
1305 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001306 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001308}
1309
1310PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001311PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001312 PyObject *locals, int closeit, PyCompilerFlags *flags)
1313{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001315 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001316 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001317 if (arena == NULL)
1318 return NULL;
1319
1320 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1321 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001322 if (closeit)
1323 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001324 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001325 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001327 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001328 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001329 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001331}
1332
Guido van Rossum82598051997-03-05 00:20:32 +00001333static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001335 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001336{
Guido van Rossum82598051997-03-05 00:20:32 +00001337 PyCodeObject *co;
1338 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001339 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001340 if (co == NULL)
1341 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001342 v = PyEval_EvalCode(co, globals, locals);
1343 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001344 return v;
1345}
1346
Guido van Rossum82598051997-03-05 00:20:32 +00001347static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001348run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001350{
Guido van Rossum82598051997-03-05 00:20:32 +00001351 PyCodeObject *co;
1352 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001353 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001354 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001355
Guido van Rossum82598051997-03-05 00:20:32 +00001356 magic = PyMarshal_ReadLongFromFile(fp);
1357 if (magic != PyImport_GetMagicNumber()) {
1358 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001359 "Bad magic number in .pyc file");
1360 return NULL;
1361 }
Guido van Rossum82598051997-03-05 00:20:32 +00001362 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001363 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001364 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001365 if (v == NULL || !PyCode_Check(v)) {
1366 Py_XDECREF(v);
1367 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001368 "Bad code object in .pyc file");
1369 return NULL;
1370 }
Guido van Rossum82598051997-03-05 00:20:32 +00001371 co = (PyCodeObject *)v;
1372 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001373 if (v && flags)
1374 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001375 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001376 return v;
1377}
1378
Guido van Rossum82598051997-03-05 00:20:32 +00001379PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001380Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001381 PyCompilerFlags *flags)
1382{
Guido van Rossum82598051997-03-05 00:20:32 +00001383 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001384 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001385 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001386 if (arena == NULL)
1387 return NULL;
1388
1389 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001390 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001391 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001392 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001393 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001394 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001395 PyObject *result = PyAST_mod2obj(mod);
1396 PyArena_Free(arena);
1397 return result;
1398 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001399 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001400 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001401 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001402}
1403
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001404struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001405Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001406{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001407 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001408 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001409 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001410 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001411 if (arena == NULL)
1412 return NULL;
1413
Christian Heimes7f23d862008-03-26 22:51:58 +00001414 flags.cf_flags = 0;
1415
Christian Heimes3c608332008-03-26 22:01:37 +00001416 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001417 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001418 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001419 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001420 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001422 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001423 return st;
1424}
1425
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426/* Preferred access to parser is through AST. */
1427mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001428PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001429 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001432 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001434 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001435
1436 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001437 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001438 &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001439 if (flags == NULL) {
1440 localflags.cf_flags = 0;
1441 flags = &localflags;
1442 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001444 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001445 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 PyNode_Free(n);
1447 return mod;
1448 }
1449 else {
1450 err_input(&err);
1451 return NULL;
1452 }
1453}
1454
1455mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001456PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001457 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001458 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001461 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001463 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001464
Christian Heimes3c608332008-03-26 22:01:37 +00001465 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1466 start, ps1, ps2, &err, &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001467 if (flags == NULL) {
1468 localflags.cf_flags = 0;
1469 flags = &localflags;
1470 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001472 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001473 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 PyNode_Free(n);
1475 return mod;
1476 }
1477 else {
1478 err_input(&err);
1479 if (errcode)
1480 *errcode = err.error;
1481 return NULL;
1482 }
1483}
1484
Guido van Rossuma110aa61994-08-29 12:50:44 +00001485/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001486
Guido van Rossuma110aa61994-08-29 12:50:44 +00001487node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001488PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001489{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001490 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001491 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1492 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001493 if (n == NULL)
1494 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001495
Guido van Rossuma110aa61994-08-29 12:50:44 +00001496 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001497}
1498
Guido van Rossuma110aa61994-08-29 12:50:44 +00001499/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001500
Guido van Rossuma110aa61994-08-29 12:50:44 +00001501node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001502PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001503{
Tim Petersfe2127d2001-07-16 05:37:24 +00001504 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001505 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1506 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001507 if (n == NULL)
1508 err_input(&err);
1509 return n;
1510}
1511
1512node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001513PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001514 int start, int flags)
1515{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001516 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001517 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1518 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001519 if (n == NULL)
1520 err_input(&err);
1521 return n;
1522}
1523
1524node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001525PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001526{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001527 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001528}
1529
Guido van Rossum66ebd912003-04-17 16:02:26 +00001530/* May want to move a more generalized form of this to parsetok.c or
1531 even parser modules. */
1532
1533void
1534PyParser_SetError(perrdetail *err)
1535{
1536 err_input(err);
1537}
1538
Guido van Rossuma110aa61994-08-29 12:50:44 +00001539/* Set the error appropriate to the given input error code (see errcode.h) */
1540
1541static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001542err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001543{
Fred Drake85f36392000-07-11 17:53:00 +00001544 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001545 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001546 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001547 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001548 switch (err->error) {
1549 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001550 errtype = PyExc_IndentationError;
1551 if (err->expected == INDENT)
1552 msg = "expected an indented block";
1553 else if (err->token == INDENT)
1554 msg = "unexpected indent";
1555 else if (err->token == DEDENT)
1556 msg = "unexpected unindent";
1557 else {
1558 errtype = PyExc_SyntaxError;
1559 msg = "invalid syntax";
1560 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001561 break;
1562 case E_TOKEN:
1563 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001564 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001565 case E_EOFS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001566 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001567 break;
1568 case E_EOLS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001569 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001570 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001571 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001572 if (!PyErr_Occurred())
1573 PyErr_SetNone(PyExc_KeyboardInterrupt);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001574 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001575 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001576 PyErr_NoMemory();
Georg Brandl1ad108d2008-07-19 10:08:55 +00001577 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001578 case E_EOF:
1579 msg = "unexpected EOF while parsing";
1580 break;
Fred Drake85f36392000-07-11 17:53:00 +00001581 case E_TABSPACE:
1582 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001583 msg = "inconsistent use of tabs and spaces in indentation";
1584 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001585 case E_OVERFLOW:
1586 msg = "expression too long";
1587 break;
Fred Drake85f36392000-07-11 17:53:00 +00001588 case E_DEDENT:
1589 errtype = PyExc_IndentationError;
1590 msg = "unindent does not match any outer indentation level";
1591 break;
1592 case E_TOODEEP:
1593 errtype = PyExc_IndentationError;
1594 msg = "too many levels of indentation";
1595 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001596 case E_DECODE: {
1597 PyObject *type, *value, *tb;
1598 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001599 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001600 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001601 if (u != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001602 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001603 }
1604 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001605 if (msg == NULL)
1606 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001607 Py_XDECREF(type);
1608 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001609 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001610 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001611 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001612 case E_LINECONT:
1613 msg = "unexpected character after line continuation character";
1614 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001615 default:
1616 fprintf(stderr, "error=%d\n", err->error);
1617 msg = "unknown parsing error";
1618 break;
1619 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620 v = Py_BuildValue("(ziiz)", err->filename,
1621 err->lineno, err->offset, err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622 w = NULL;
1623 if (v != NULL)
1624 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001625 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001626 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001627 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001628 Py_XDECREF(w);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001629cleanup:
1630 if (err->text != NULL) {
1631 PyObject_FREE(err->text);
1632 err->text = NULL;
1633 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001634}
1635
1636/* Print fatal error message and abort */
1637
1638void
Tim Peters7c321a82002-07-09 02:57:01 +00001639Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001640{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001641 fprintf(stderr, "Fatal Python error: %s\n", msg);
Jesse Noller42f9b4e2009-03-31 22:20:35 +00001642 fflush(stderr); /* it helps in Windows debug build */
1643
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001644#ifdef MS_WINDOWS
Martin v. Löwis5344c992009-01-02 20:32:55 +00001645 {
1646 size_t len = strlen(msg);
1647 WCHAR* buffer;
1648 size_t i;
1649
1650 /* Convert the message to wchar_t. This uses a simple one-to-one
1651 conversion, assuming that the this error message actually uses ASCII
1652 only. If this ceases to be true, we will have to convert. */
1653 buffer = alloca( (len+1) * (sizeof *buffer));
1654 for( i=0; i<=len; ++i)
1655 buffer[i] = msg[i];
1656 OutputDebugStringW(L"Fatal Python error: ");
1657 OutputDebugStringW(buffer);
1658 OutputDebugStringW(L"\n");
1659 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00001660#ifdef _DEBUG
1661 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001662#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001663#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001664 abort();
1665}
1666
1667/* Clean up and exit */
1668
Guido van Rossuma110aa61994-08-29 12:50:44 +00001669#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001670#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001671#endif
1672
Antoine Pitrouefb60c02009-10-20 21:29:37 +00001673/* Wait until threading._shutdown completes, provided
1674 the threading module was imported in the first place.
1675 The shutdown routine will wait until all non-daemon
1676 "threading" threads have completed. */
1677static void
1678wait_for_thread_shutdown(void)
1679{
1680#ifdef WITH_THREAD
1681 PyObject *result;
1682 PyThreadState *tstate = PyThreadState_GET();
1683 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
1684 "threading");
1685 if (threading == NULL) {
1686 /* threading not imported */
1687 PyErr_Clear();
1688 return;
1689 }
1690 result = PyObject_CallMethod(threading, "_shutdown", "");
1691 if (result == NULL)
1692 PyErr_WriteUnraisable(threading);
1693 else
1694 Py_DECREF(result);
1695 Py_DECREF(threading);
1696#endif
1697}
1698
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001699#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001700static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001701static int nexitfuncs = 0;
1702
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001703int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001704{
1705 if (nexitfuncs >= NEXITFUNCS)
1706 return -1;
1707 exitfuncs[nexitfuncs++] = func;
1708 return 0;
1709}
1710
Guido van Rossumcc283f51997-08-05 02:22:03 +00001711static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001712call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001713{
Guido van Rossum82598051997-03-05 00:20:32 +00001714 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001715
1716 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001717 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001718 Py_INCREF(exitfunc);
1719 PySys_SetObject("exitfunc", (PyObject *)NULL);
1720 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001721 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001722 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1723 PySys_WriteStderr("Error in sys.exitfunc:\n");
1724 }
Guido van Rossum82598051997-03-05 00:20:32 +00001725 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001726 }
Guido van Rossum82598051997-03-05 00:20:32 +00001727 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001728 }
1729
Guido van Rossum0829c751998-02-28 04:31:39 +00001730 if (Py_FlushLine())
1731 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001732}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001733
Guido van Rossumcc283f51997-08-05 02:22:03 +00001734static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001735call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001736{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001737 while (nexitfuncs > 0)
1738 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001739
1740 fflush(stdout);
1741 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001742}
1743
1744void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001745Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001746{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001747 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001748
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001749 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001750}
1751
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001752static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001753initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001754{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001755#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001756 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001757#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001758#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001759 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001760#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001761#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001762 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001763#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001764 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001765}
1766
Guido van Rossum7433b121997-02-14 19:45:36 +00001767
1768/*
1769 * The file descriptor fd is considered ``interactive'' if either
1770 * a) isatty(fd) is TRUE, or
1771 * b) the -i flag was given, and the filename associated with
1772 * the descriptor is NULL or "<stdin>" or "???".
1773 */
1774int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001775Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001776{
1777 if (isatty((int)fileno(fp)))
1778 return 1;
1779 if (!Py_InteractiveFlag)
1780 return 0;
1781 return (filename == NULL) ||
1782 (strcmp(filename, "<stdin>") == 0) ||
1783 (strcmp(filename, "???") == 0);
1784}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001785
1786
Tim Petersd08e3822003-04-17 15:24:21 +00001787#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001788#if defined(WIN32) && defined(_MSC_VER)
1789
1790/* Stack checking for Microsoft C */
1791
1792#include <malloc.h>
1793#include <excpt.h>
1794
Fred Drakee8de31c2000-08-31 05:38:39 +00001795/*
1796 * Return non-zero when we run out of memory on the stack; zero otherwise.
1797 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001798int
Fred Drake399739f2000-08-31 05:52:44 +00001799PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001800{
1801 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001802 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001803 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001804 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001805 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001806 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1807 EXCEPTION_EXECUTE_HANDLER :
1808 EXCEPTION_CONTINUE_SEARCH) {
1809 int errcode = _resetstkoflw();
Amaury Forgeot d'Arcdc619012008-11-22 20:01:18 +00001810 if (errcode == 0)
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001811 {
1812 Py_FatalError("Could not reset the stack!");
1813 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001814 }
1815 return 1;
1816}
1817
1818#endif /* WIN32 && _MSC_VER */
1819
1820/* Alternate implementations can be added here... */
1821
1822#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001823
1824
1825/* Wrappers around sigaction() or signal(). */
1826
1827PyOS_sighandler_t
1828PyOS_getsig(int sig)
1829{
1830#ifdef HAVE_SIGACTION
1831 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001832 if (sigaction(sig, NULL, &context) == -1)
1833 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001834 return context.sa_handler;
1835#else
1836 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001837/* Special signal handling for the secure CRT in Visual Studio 2005 */
1838#if defined(_MSC_VER) && _MSC_VER >= 1400
1839 switch (sig) {
1840 /* Only these signals are valid */
1841 case SIGINT:
1842 case SIGILL:
1843 case SIGFPE:
1844 case SIGSEGV:
1845 case SIGTERM:
1846 case SIGBREAK:
1847 case SIGABRT:
1848 break;
1849 /* Don't call signal() with other values or it will assert */
1850 default:
1851 return SIG_ERR;
1852 }
1853#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001854 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001855 if (handler != SIG_ERR)
1856 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001857 return handler;
1858#endif
1859}
1860
1861PyOS_sighandler_t
1862PyOS_setsig(int sig, PyOS_sighandler_t handler)
1863{
1864#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001865 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001866 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001867 sigemptyset(&context.sa_mask);
1868 context.sa_flags = 0;
1869 if (sigaction(sig, &context, &ocontext) == -1)
1870 return SIG_ERR;
1871 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001872#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001873 PyOS_sighandler_t oldhandler;
1874 oldhandler = signal(sig, handler);
1875#ifdef HAVE_SIGINTERRUPT
1876 siginterrupt(sig, 1);
1877#endif
1878 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001879#endif
1880}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881
1882/* Deprecated C API functions still provided for binary compatiblity */
1883
1884#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001885PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1887{
1888 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1889}
1890
Thomas Heller1b046642006-04-18 18:51:06 +00001891#undef PyParser_SimpleParseString
1892PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893PyParser_SimpleParseString(const char *str, int start)
1894{
1895 return PyParser_SimpleParseStringFlags(str, start, 0);
1896}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001897
Thomas Heller1b046642006-04-18 18:51:06 +00001898#undef PyRun_AnyFile
1899PyAPI_FUNC(int)
1900PyRun_AnyFile(FILE *fp, const char *name)
1901{
1902 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1903}
1904
1905#undef PyRun_AnyFileEx
1906PyAPI_FUNC(int)
1907PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1908{
1909 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1910}
1911
1912#undef PyRun_AnyFileFlags
1913PyAPI_FUNC(int)
1914PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1915{
1916 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1917}
1918
1919#undef PyRun_File
1920PyAPI_FUNC(PyObject *)
1921PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1922{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001923 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001924}
1925
1926#undef PyRun_FileEx
1927PyAPI_FUNC(PyObject *)
1928PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1929{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001930 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001931}
1932
1933#undef PyRun_FileFlags
1934PyAPI_FUNC(PyObject *)
1935PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1936 PyCompilerFlags *flags)
1937{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001938 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Heller1b046642006-04-18 18:51:06 +00001939}
1940
1941#undef PyRun_SimpleFile
1942PyAPI_FUNC(int)
1943PyRun_SimpleFile(FILE *f, const char *p)
1944{
1945 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1946}
1947
1948#undef PyRun_SimpleFileEx
1949PyAPI_FUNC(int)
1950PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1951{
1952 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1953}
1954
1955
1956#undef PyRun_String
1957PyAPI_FUNC(PyObject *)
1958PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1959{
1960 return PyRun_StringFlags(str, s, g, l, NULL);
1961}
1962
1963#undef PyRun_SimpleString
1964PyAPI_FUNC(int)
1965PyRun_SimpleString(const char *s)
1966{
1967 return PyRun_SimpleStringFlags(s, NULL);
1968}
1969
1970#undef Py_CompileString
1971PyAPI_FUNC(PyObject *)
1972Py_CompileString(const char *str, const char *p, int s)
1973{
1974 return Py_CompileStringFlags(str, p, s, NULL);
1975}
1976
1977#undef PyRun_InteractiveOne
1978PyAPI_FUNC(int)
1979PyRun_InteractiveOne(FILE *f, const char *p)
1980{
1981 return PyRun_InteractiveOneFlags(f, p, NULL);
1982}
1983
1984#undef PyRun_InteractiveLoop
1985PyAPI_FUNC(int)
1986PyRun_InteractiveLoop(FILE *f, const char *p)
1987{
1988 return PyRun_InteractiveLoopFlags(f, p, NULL);
1989}
1990
Anthony Baxterac6bd462006-04-13 02:06:09 +00001991#ifdef __cplusplus
1992}
1993#endif
1994