blob: a6e7c465681ee87a4e4ad8d3fbe8d7d3cd306912 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Guido van Rossumd8faa362007-04-27 19:54:29 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00008#include "grammar.h"
9#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000010#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000014#include "symtable.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015#include "ast.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000016#include "marshal.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000017#include "osdefs.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000018
Thomas Wouters0e3f5912006-08-11 14:57:12 +000019#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000020#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000021#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000022
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000023#ifdef MS_WINDOWS
Martin v. Löwis5c88d812009-01-02 20:47:48 +000024#include "malloc.h" /* for alloca */
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000025#endif
Martin v. Löwis5c88d812009-01-02 20:47:48 +000026
Martin v. Löwis73d538b2003-03-05 15:13:47 +000027#ifdef HAVE_LANGINFO_H
28#include <locale.h>
29#include <langinfo.h>
30#endif
31
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000032#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000033#undef BYTE
34#include "windows.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000035#define PATH_MAX MAXPATHLEN
Guido van Rossuma44823b1995-03-14 15:01:17 +000036#endif
37
Neal Norwitz4281cef2006-03-04 19:58:13 +000038#ifndef Py_REF_DEBUG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000040#else /* Py_REF_DEBUG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041#define PRINT_TOTAL_REFS() fprintf(stderr, \
42 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
43 _Py_GetRefTotal())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000044#endif
45
46#ifdef __cplusplus
47extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000048#endif
49
Martin v. Löwis790465f2008-04-05 20:41:37 +000050extern wchar_t *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000051
Guido van Rossum82598051997-03-05 00:20:32 +000052extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000053
Guido van Rossumb73cc041993-11-01 16:28:59 +000054/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000055static void initmain(void);
Victor Stinner793b5312011-04-27 00:24:21 +020056static int initfsencoding(PyInterpreterState *interp);
Tim Petersdbd9ba62000-07-09 03:09:57 +000057static void initsite(void);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000058static int initstdio(void);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000059static void flush_io(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000060static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000062static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000064static void err_input(perrdetail *);
Victor Stinner7f2fee32011-04-05 00:39:01 +020065static void err_free(perrdetail *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static void initsigs(void);
Collin Winter670e6922007-03-21 02:57:17 +000067static void call_py_exitfuncs(void);
Antoine Pitrou011bd622009-10-20 21:52:47 +000068static void wait_for_thread_shutdown(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000069static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000070extern void _PyUnicode_Init(void);
71extern void _PyUnicode_Fini(void);
Guido van Rossumddefaf32007-01-14 03:31:43 +000072extern int _PyLong_Init(void);
73extern void PyLong_Fini(void);
Victor Stinner024e37a2011-03-31 01:31:06 +020074extern int _PyFaulthandler_Init(void);
75extern void _PyFaulthandler_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000076
Mark Hammond8d98d2c2003-04-19 15:41:53 +000077#ifdef WITH_THREAD
78extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
79extern void _PyGILState_Fini(void);
80#endif /* WITH_THREAD */
81
Guido van Rossum82598051997-03-05 00:20:32 +000082int Py_DebugFlag; /* Needed by parser.c */
83int Py_VerboseFlag; /* Needed by import.c */
Georg Brandl8aa7e992010-12-28 18:30:18 +000084int Py_QuietFlag; /* Needed by sysmodule.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000085int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl0b2489e2011-05-15 08:49:12 +020086int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000087int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +000088int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Christian Heimes790c8232008-01-07 21:14:23 +000089int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000090int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000091int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000092int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Christian Heimes8dc226f2008-05-06 23:45:46 +000093int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Antoine Pitrou05608432009-01-09 18:53:14 +000094int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000095
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +020096PyThreadState *_Py_Finalizing = NULL;
97
Christian Heimes33fe8092008-04-13 13:53:33 +000098/* PyModule_GetWarningsModule is no longer necessary as of 2.6
99since _warnings is builtin. This API should not be used. */
100PyObject *
101PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +0000102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +0000104}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000105
Guido van Rossum25ce5661997-08-02 03:10:38 +0000106static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000107
Thomas Wouters7e474022000-07-16 12:04:32 +0000108/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000109
110int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000111Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 return initialized;
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000114}
115
Guido van Rossum25ce5661997-08-02 03:10:38 +0000116/* Global initializations. Can be undone by Py_Finalize(). Don't
117 call this twice without an intervening Py_Finalize() call. When
118 initializations fail, a fatal error is issued and the function does
119 not return. On return, the first thread and interpreter state have
120 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000121
Guido van Rossum25ce5661997-08-02 03:10:38 +0000122 Locking: you must hold the interpreter lock while calling this.
123 (If the lock has not yet been initialized, that's equivalent to
124 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000125
Guido van Rossum25ce5661997-08-02 03:10:38 +0000126*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000127
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000128static int
129add_flag(int flag, const char *envs)
130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 int env = atoi(envs);
132 if (flag < env)
133 flag = env;
134 if (flag < 1)
135 flag = 1;
136 return flag;
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000137}
138
Christian Heimes5833a2f2008-10-30 21:40:04 +0000139static char*
Victor Stinner94908bb2010-08-18 21:23:25 +0000140get_codec_name(const char *encoding)
Christian Heimes5833a2f2008-10-30 21:40:04 +0000141{
Victor Stinner94908bb2010-08-18 21:23:25 +0000142 char *name_utf8, *name_str;
Victor Stinner386fe712010-05-19 00:34:15 +0000143 PyObject *codec, *name = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200144 _Py_IDENTIFIER(name);
Christian Heimes5833a2f2008-10-30 21:40:04 +0000145
Victor Stinner94908bb2010-08-18 21:23:25 +0000146 codec = _PyCodec_Lookup(encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (!codec)
148 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000149
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200150 name = _PyObject_GetAttrId(codec, &PyId_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 Py_CLEAR(codec);
152 if (!name)
153 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000154
Victor Stinner94908bb2010-08-18 21:23:25 +0000155 name_utf8 = _PyUnicode_AsString(name);
Victor Stinner4ca28092011-03-20 23:09:03 +0100156 if (name_utf8 == NULL)
Victor Stinner386fe712010-05-19 00:34:15 +0000157 goto error;
Victor Stinner94908bb2010-08-18 21:23:25 +0000158 name_str = strdup(name_utf8);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 Py_DECREF(name);
Victor Stinner94908bb2010-08-18 21:23:25 +0000160 if (name_str == NULL) {
161 PyErr_NoMemory();
162 return NULL;
163 }
164 return name_str;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000165
166error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 Py_XDECREF(codec);
Victor Stinner386fe712010-05-19 00:34:15 +0000168 Py_XDECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000170}
Victor Stinner94908bb2010-08-18 21:23:25 +0000171
Victor Stinner94908bb2010-08-18 21:23:25 +0000172static char*
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200173get_locale_encoding(void)
Victor Stinner94908bb2010-08-18 21:23:25 +0000174{
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200175#ifdef MS_WINDOWS
176 char codepage[100];
177 PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
178 return get_codec_name(codepage);
179#elif defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94908bb2010-08-18 21:23:25 +0000180 char* codeset = nl_langinfo(CODESET);
181 if (!codeset || codeset[0] == '\0') {
182 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
183 return NULL;
184 }
185 return get_codec_name(codeset);
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200186#else
187 PyErr_SetNone(PyExc_NotImplementedError);
188 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000189#endif
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200190}
Christian Heimes5833a2f2008-10-30 21:40:04 +0000191
Guido van Rossuma027efa1997-05-05 20:56:21 +0000192void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000193Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 PyInterpreterState *interp;
196 PyThreadState *tstate;
197 PyObject *bimod, *sysmod, *pstderr;
198 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 if (initialized)
202 return;
203 initialized = 1;
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200204 _Py_Finalizing = NULL;
Tim Petersd08e3822003-04-17 15:24:21 +0000205
Andrew M. Kuchlingb2ceb3a2010-02-22 23:26:10 +0000206#if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 /* Set up the LC_CTYPE locale, so we can obtain
208 the locale's charset without having to switch
209 locales. */
210 setlocale(LC_CTYPE, "");
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000211#endif
212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
214 Py_DebugFlag = add_flag(Py_DebugFlag, p);
215 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
216 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
217 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
218 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
219 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
220 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 interp = PyInterpreterState_New();
223 if (interp == NULL)
224 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 tstate = PyThreadState_New(interp);
227 if (tstate == NULL)
228 Py_FatalError("Py_Initialize: can't make first thread");
229 (void) PyThreadState_Swap(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000230
Victor Stinner6961bd62010-08-17 22:26:51 +0000231#ifdef WITH_THREAD
Antoine Pitroub0b384b2010-09-20 20:13:48 +0000232 /* We can't call _PyEval_FiniThreads() in Py_Finalize because
233 destroying the GIL might fail when it is being referenced from
234 another running thread (see issue #9901).
235 Instead we destroy the previously created GIL here, which ensures
236 that we can call Py_Initialize / Py_Finalize multiple times. */
237 _PyEval_FiniThreads();
238
239 /* Auto-thread-state API */
Victor Stinner6961bd62010-08-17 22:26:51 +0000240 _PyGILState_Init(interp, tstate);
241#endif /* WITH_THREAD */
242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (!_PyFrame_Init())
246 Py_FatalError("Py_Initialize: can't init frames");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (!_PyLong_Init())
249 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 if (!PyByteArray_Init())
252 Py_FatalError("Py_Initialize: can't init bytearray");
Neal Norwitz6968b052007-02-27 19:02:19 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 _PyFloat_Init();
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 interp->modules = PyDict_New();
257 if (interp->modules == NULL)
258 Py_FatalError("Py_Initialize: can't make modules dictionary");
259 interp->modules_reloading = PyDict_New();
260 if (interp->modules_reloading == NULL)
261 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 /* Init Unicode implementation; relies on the codec registry */
264 _PyUnicode_Init();
Guido van Rossumc94044c2000-03-10 23:03:54 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 bimod = _PyBuiltin_Init();
267 if (bimod == NULL)
268 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Victor Stinner49d3f252010-10-17 01:24:53 +0000269 _PyImport_FixupBuiltin(bimod, "builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 interp->builtins = PyModule_GetDict(bimod);
271 if (interp->builtins == NULL)
272 Py_FatalError("Py_Initialize: can't initialize builtins dict");
273 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 /* initialize builtin exceptions */
276 _PyExc_Init();
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 sysmod = _PySys_Init();
279 if (sysmod == NULL)
280 Py_FatalError("Py_Initialize: can't initialize sys");
281 interp->sysdict = PyModule_GetDict(sysmod);
282 if (interp->sysdict == NULL)
283 Py_FatalError("Py_Initialize: can't initialize sys dict");
284 Py_INCREF(interp->sysdict);
Victor Stinner49d3f252010-10-17 01:24:53 +0000285 _PyImport_FixupBuiltin(sysmod, "sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 PySys_SetPath(Py_GetPath());
287 PyDict_SetItemString(interp->sysdict, "modules",
288 interp->modules);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 /* Set up a preliminary stderr printer until we have enough
291 infrastructure for the io module in place. */
292 pstderr = PyFile_NewStdPrinter(fileno(stderr));
293 if (pstderr == NULL)
294 Py_FatalError("Py_Initialize: can't set preliminary stderr");
295 PySys_SetObject("stderr", pstderr);
296 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000297 Py_DECREF(pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 _PyImport_Init();
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 _PyImportHooks_Init();
Just van Rossum52e14d62002-12-30 22:08:05 +0000302
Victor Stinner024e37a2011-03-31 01:31:06 +0200303 /* initialize the faulthandler module */
304 if (_PyFaulthandler_Init())
305 Py_FatalError("Py_Initialize: can't initialize faulthandler");
306
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000307 /* Initialize _warnings. */
308 _PyWarnings_Init();
309
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000310 _PyTime_Init();
311
Victor Stinner793b5312011-04-27 00:24:21 +0200312 if (initfsencoding(interp) < 0)
313 Py_FatalError("Py_Initialize: unable to load the file system codec");
Martin v. Löwis011e8422009-05-05 04:43:17 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 if (install_sigs)
316 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 initmain(); /* Module __main__ */
319 if (initstdio() < 0)
320 Py_FatalError(
321 "Py_Initialize: can't initialize sys standard streams");
322
Antoine Pitroucf9f9802010-11-10 13:55:25 +0000323 /* Initialize warnings. */
324 if (PySys_HasWarnOptions()) {
325 PyObject *warnings_module = PyImport_ImportModule("warnings");
326 if (warnings_module == NULL) {
327 fprintf(stderr, "'import warnings' failed; traceback:\n");
328 PyErr_Print();
329 }
330 Py_XDECREF(warnings_module);
331 }
332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 if (!Py_NoSiteFlag)
334 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000335}
336
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000337void
338Py_Initialize(void)
339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 Py_InitializeEx(1);
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000341}
342
343
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000344#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000345extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000346#endif
347
Guido van Rossume8432ac2007-07-09 15:04:50 +0000348/* Flush stdout and stderr */
349
Neal Norwitz2bad9702007-08-27 06:19:22 +0000350static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000351flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 PyObject *fout = PySys_GetObject("stdout");
354 PyObject *ferr = PySys_GetObject("stderr");
355 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200356 _Py_IDENTIFIER(flush);
Guido van Rossume8432ac2007-07-09 15:04:50 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 if (fout != NULL && fout != Py_None) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200359 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (tmp == NULL)
Antoine Pitroubddc9fe2010-08-08 20:46:42 +0000361 PyErr_WriteUnraisable(fout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 else
363 Py_DECREF(tmp);
364 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000365
Victor Stinner9467b212010-05-14 00:59:09 +0000366 if (ferr != NULL && ferr != Py_None) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200367 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (tmp == NULL)
369 PyErr_Clear();
370 else
371 Py_DECREF(tmp);
372 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000373}
374
Guido van Rossum25ce5661997-08-02 03:10:38 +0000375/* Undo the effect of Py_Initialize().
376
377 Beware: if multiple interpreter and/or thread states exist, these
378 are not wiped out; only the current thread and interpreter state
379 are deleted. But since everything else is deleted, those other
380 interpreter and thread states should no longer be used.
381
382 (XXX We should do better, e.g. wipe out all interpreters and
383 threads.)
384
385 Locking: as above.
386
387*/
388
389void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000390Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 PyInterpreterState *interp;
393 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if (!initialized)
396 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 wait_for_thread_shutdown();
Antoine Pitrou011bd622009-10-20 21:52:47 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 /* The interpreter is still entirely intact at this point, and the
401 * exit funcs may be relying on that. In particular, if some thread
402 * or exit func is still waiting to do an import, the import machinery
403 * expects Py_IsInitialized() to return true. So don't say the
404 * interpreter is uninitialized until after the exit funcs have run.
405 * Note that Threading.py uses an exit func to do a join on all the
406 * threads created thru it, so this also protects pending imports in
407 * the threads created via Threading.
408 */
409 call_py_exitfuncs();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 /* Get current thread state and interpreter pointer */
412 tstate = PyThreadState_GET();
413 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000414
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200415 /* Remaining threads (e.g. daemon threads) will automatically exit
416 after taking the GIL (in PyEval_RestoreThread()). */
417 _Py_Finalizing = tstate;
418 initialized = 0;
419
420 /* Flush stdout+stderr */
421 flush_std_files();
422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 /* Disable signal handling */
424 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 /* Clear type lookup cache */
427 PyType_ClearCache();
Christian Heimes26855632008-01-27 23:50:43 +0000428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 /* Collect garbage. This may call finalizers; it's nice to call these
430 * before all modules are destroyed.
431 * XXX If a __del__ or weakref callback is triggered here, and tries to
432 * XXX import a module, bad things can happen, because Python no
433 * XXX longer believes it's initialized.
434 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
435 * XXX is easy to provoke that way. I've also seen, e.g.,
436 * XXX Exception exceptions.ImportError: 'No module named sha'
437 * XXX in <function callback at 0x008F5718> ignored
438 * XXX but I'm unclear on exactly how that one happens. In any case,
439 * XXX I haven't seen a real-life report of either of these.
440 */
441 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000442#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 /* With COUNT_ALLOCS, it helps to run GC multiple times:
444 each collection might release some types from the type
445 list, so they become garbage. */
446 while (PyGC_Collect() > 0)
447 /* nothing */;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448#endif
Antoine Pitrou696e0352010-08-08 22:18:46 +0000449 /* We run this while most interpreter state is still alive, so that
450 debug information can be printed out */
451 _PyGC_Fini();
Guido van Rossume13ddc92003-04-17 17:29:22 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 /* Destroy all modules */
454 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 /* Flush stdout+stderr (again, in case more was printed) */
457 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 /* Collect final garbage. This disposes of cycles created by
460 * new-style class definitions, for example.
461 * XXX This is disabled because it caused too many problems. If
462 * XXX a __del__ or weakref callback triggers here, Python code has
463 * XXX a hard time running, because even the sys module has been
464 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
465 * XXX One symptom is a sequence of information-free messages
466 * XXX coming from threads (if a __del__ or callback is invoked,
467 * XXX other threads can execute too, and any exception they encounter
468 * XXX triggers a comedy of errors as subsystem after subsystem
469 * XXX fails to find what it *expects* to find in sys to help report
470 * XXX the exception and consequent unexpected failures). I've also
471 * XXX seen segfaults then, after adding print statements to the
472 * XXX Python code getting called.
473 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000474#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000476#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
479 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000480
Victor Stinner024e37a2011-03-31 01:31:06 +0200481 /* unload faulthandler module */
482 _PyFaulthandler_Fini();
483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000485#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000487#endif
488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000490
Tim Peters9cf25ce2003-04-17 15:21:01 +0000491#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 /* Display all objects still alive -- this can invoke arbitrary
493 * __repr__ overrides, so requires a mostly-intact interpreter.
494 * Alas, a lot of stuff may still be alive now that will be cleaned
495 * up later.
496 */
497 if (Py_GETENV("PYTHONDUMPREFS"))
498 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000499#endif /* Py_TRACE_REFS */
500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 /* Clear interpreter state */
502 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 /* Now we decref the exception classes. After this point nothing
505 can raise an exception. That's okay, because each Fini() method
506 below has been checked to make sure no exceptions are ever
507 raised.
508 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 /* Cleanup auto-thread-state */
Christian Heimes7d2ff882007-11-30 14:35:04 +0000513#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 _PyGILState_Fini();
Christian Heimes7d2ff882007-11-30 14:35:04 +0000515#endif /* WITH_THREAD */
516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 /* Delete current thread */
518 PyThreadState_Swap(NULL);
519 PyInterpreterState_Delete(interp);
Barry Warsawf242aa02000-05-25 23:09:49 +0000520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 /* Sundry finalizers */
522 PyMethod_Fini();
523 PyFrame_Fini();
524 PyCFunction_Fini();
525 PyTuple_Fini();
526 PyList_Fini();
527 PySet_Fini();
528 PyBytes_Fini();
529 PyByteArray_Fini();
530 PyLong_Fini();
531 PyFloat_Fini();
532 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 /* Cleanup Unicode implementation */
535 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 /* reset file system default encoding */
Victor Stinnerb744ba12010-05-15 12:27:16 +0000538 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 free((char*)Py_FileSystemDefaultEncoding);
540 Py_FileSystemDefaultEncoding = NULL;
541 }
Christian Heimesc8967002007-11-30 10:18:26 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 /* XXX Still allocated:
544 - various static ad-hoc pointers to interned strings
545 - int and float free list blocks
546 - whatever various modules and libraries allocate
547 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000550
Tim Peters269b2a62003-04-17 19:52:29 +0000551#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 /* Display addresses (& refcnts) of all objects still alive.
553 * An address can be used to find the repr of the object, printed
554 * above by _Py_PrintReferences.
555 */
556 if (Py_GETENV("PYTHONDUMPREFS"))
557 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000558#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000559#ifdef PYMALLOC_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 if (Py_GETENV("PYTHONMALLOCSTATS"))
561 _PyObject_DebugMallocStats();
Tim Peters0e871182002-04-13 08:29:14 +0000562#endif
563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000565}
566
567/* Create and initialize a new interpreter and thread, and return the
568 new thread. This requires that Py_Initialize() has been called
569 first.
570
571 Unsuccessful initialization yields a NULL pointer. Note that *no*
572 exception information is available even in this case -- the
573 exception information is held in the thread, and there is no
574 thread.
575
576 Locking: as above.
577
578*/
579
580PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000581Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 PyInterpreterState *interp;
584 PyThreadState *tstate, *save_tstate;
585 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 if (!initialized)
588 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 interp = PyInterpreterState_New();
591 if (interp == NULL)
592 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 tstate = PyThreadState_New(interp);
595 if (tstate == NULL) {
596 PyInterpreterState_Delete(interp);
597 return NULL;
598 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 interp->modules = PyDict_New();
605 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000606
Victor Stinner49d3f252010-10-17 01:24:53 +0000607 bimod = _PyImport_FindBuiltin("builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 if (bimod != NULL) {
609 interp->builtins = PyModule_GetDict(bimod);
610 if (interp->builtins == NULL)
611 goto handle_error;
612 Py_INCREF(interp->builtins);
613 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 /* initialize builtin exceptions */
616 _PyExc_Init();
Christian Heimes6a27efa2008-10-30 21:48:26 +0000617
Victor Stinner49d3f252010-10-17 01:24:53 +0000618 sysmod = _PyImport_FindBuiltin("sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 if (bimod != NULL && sysmod != NULL) {
620 PyObject *pstderr;
621 interp->sysdict = PyModule_GetDict(sysmod);
622 if (interp->sysdict == NULL)
623 goto handle_error;
624 Py_INCREF(interp->sysdict);
625 PySys_SetPath(Py_GetPath());
626 PyDict_SetItemString(interp->sysdict, "modules",
627 interp->modules);
628 /* Set up a preliminary stderr printer until we have enough
629 infrastructure for the io module in place. */
630 pstderr = PyFile_NewStdPrinter(fileno(stderr));
631 if (pstderr == NULL)
632 Py_FatalError("Py_Initialize: can't set preliminary stderr");
633 PySys_SetObject("stderr", pstderr);
634 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000635 Py_DECREF(pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 _PyImportHooks_Init();
Victor Stinner793b5312011-04-27 00:24:21 +0200638
639 if (initfsencoding(interp) < 0)
640 goto handle_error;
641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 if (initstdio() < 0)
643 Py_FatalError(
644 "Py_Initialize: can't initialize sys standard streams");
645 initmain();
646 if (!Py_NoSiteFlag)
647 initsite();
648 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (!PyErr_Occurred())
651 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000652
Thomas Wouters89f507f2006-12-13 04:49:30 +0000653handle_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000655
Victor Stinnerc40a3502011-04-27 00:20:27 +0200656 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 PyThreadState_Clear(tstate);
658 PyThreadState_Swap(save_tstate);
659 PyThreadState_Delete(tstate);
660 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000663}
664
665/* Delete an interpreter and its last thread. This requires that the
666 given thread state is current, that the thread has no remaining
667 frames, and that it is its interpreter's only remaining thread.
668 It is a fatal error to violate these constraints.
669
670 (Py_Finalize() doesn't have these constraints -- it zaps
671 everything, regardless.)
672
673 Locking: as above.
674
675*/
676
677void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000678Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 if (tstate != PyThreadState_GET())
683 Py_FatalError("Py_EndInterpreter: thread is not current");
684 if (tstate->frame != NULL)
685 Py_FatalError("Py_EndInterpreter: thread still has a frame");
686 if (tstate != interp->tstate_head || tstate->next != NULL)
687 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 PyImport_Cleanup();
690 PyInterpreterState_Clear(interp);
691 PyThreadState_Swap(NULL);
692 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000693}
694
Martin v. Löwis790465f2008-04-05 20:41:37 +0000695static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000696
697void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000698Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 if (pn && *pn)
701 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000702}
703
Martin v. Löwis790465f2008-04-05 20:41:37 +0000704wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000705Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000708}
709
Martin v. Löwis790465f2008-04-05 20:41:37 +0000710static wchar_t *default_home = NULL;
711static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000712
713void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000714Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000717}
718
Martin v. Löwis790465f2008-04-05 20:41:37 +0000719wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000720Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 wchar_t *home = default_home;
723 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
724 char* chome = Py_GETENV("PYTHONHOME");
725 if (chome) {
726 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
727 if (r != (size_t)-1 && r <= PATH_MAX)
728 home = env_home;
729 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 }
732 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000733}
734
Guido van Rossum6135a871995-01-09 17:53:26 +0000735/* Create __main__ module */
736
737static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000738initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 PyObject *m, *d;
741 m = PyImport_AddModule("__main__");
742 if (m == NULL)
743 Py_FatalError("can't create __main__ module");
744 d = PyModule_GetDict(m);
745 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
746 PyObject *bimod = PyImport_ImportModule("builtins");
747 if (bimod == NULL ||
748 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
749 Py_FatalError("can't add __builtins__ to __main__");
750 Py_DECREF(bimod);
751 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000752}
753
Victor Stinner793b5312011-04-27 00:24:21 +0200754static int
755initfsencoding(PyInterpreterState *interp)
Victor Stinnerb744ba12010-05-15 12:27:16 +0000756{
757 PyObject *codec;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000758
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200759 if (Py_FileSystemDefaultEncoding == NULL)
760 {
761 Py_FileSystemDefaultEncoding = get_locale_encoding();
762 if (Py_FileSystemDefaultEncoding == NULL)
Victor Stinnere4743092010-10-19 00:05:51 +0000763 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
Victor Stinnerb744ba12010-05-15 12:27:16 +0000764
Victor Stinnere4743092010-10-19 00:05:51 +0000765 Py_HasFileSystemDefaultEncoding = 0;
Victor Stinner793b5312011-04-27 00:24:21 +0200766 interp->fscodec_initialized = 1;
767 return 0;
Victor Stinner7f84ab52010-06-11 00:36:33 +0000768 }
Victor Stinnerb744ba12010-05-15 12:27:16 +0000769
770 /* the encoding is mbcs, utf-8 or ascii */
771 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
772 if (!codec) {
773 /* Such error can only occurs in critical situations: no more
774 * memory, import a module of the standard library failed,
775 * etc. */
Victor Stinner793b5312011-04-27 00:24:21 +0200776 return -1;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000777 }
Victor Stinner793b5312011-04-27 00:24:21 +0200778 Py_DECREF(codec);
779 interp->fscodec_initialized = 1;
780 return 0;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000781}
782
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000783/* Import the site module (not into __main__ though) */
784
785static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000786initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyObject *m;
789 m = PyImport_ImportModule("site");
790 if (m == NULL) {
791 PyErr_Print();
792 Py_Finalize();
793 exit(1);
794 }
795 else {
796 Py_DECREF(m);
797 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000798}
799
Antoine Pitrou05608432009-01-09 18:53:14 +0000800static PyObject*
801create_stdio(PyObject* io,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 int fd, int write_mode, char* name,
803 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +0000804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
806 const char* mode;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000807 const char* newline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 PyObject *line_buffering;
809 int buffering, isatty;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200810 _Py_IDENTIFIER(open);
811 _Py_IDENTIFIER(isatty);
812 _Py_IDENTIFIER(TextIOWrapper);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200813 _Py_IDENTIFIER(name);
814 _Py_IDENTIFIER(mode);
Antoine Pitrou05608432009-01-09 18:53:14 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 /* stdin is always opened in buffered mode, first because it shouldn't
817 make a difference in common use cases, second because TextIOWrapper
818 depends on the presence of a read1() method which only exists on
819 buffered streams.
820 */
821 if (Py_UnbufferedStdioFlag && write_mode)
822 buffering = 0;
823 else
824 buffering = -1;
825 if (write_mode)
826 mode = "wb";
827 else
828 mode = "rb";
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200829 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
830 fd, mode, buffering,
831 Py_None, Py_None, Py_None, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 if (buf == NULL)
833 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 if (buffering) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200836 _Py_IDENTIFIER(raw);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200837 raw = _PyObject_GetAttrId(buf, &PyId_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 if (raw == NULL)
839 goto error;
840 }
841 else {
842 raw = buf;
843 Py_INCREF(raw);
844 }
Antoine Pitrou05608432009-01-09 18:53:14 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 text = PyUnicode_FromString(name);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200847 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 goto error;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200849 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 if (res == NULL)
851 goto error;
852 isatty = PyObject_IsTrue(res);
853 Py_DECREF(res);
854 if (isatty == -1)
855 goto error;
856 if (isatty || Py_UnbufferedStdioFlag)
857 line_buffering = Py_True;
858 else
859 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 Py_CLEAR(raw);
862 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +0000863
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000864 newline = "\n";
865#ifdef MS_WINDOWS
866 if (!write_mode) {
867 /* translate \r\n to \n for sys.stdin on Windows */
868 newline = NULL;
869 }
870#endif
871
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200872 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
873 buf, encoding, errors,
874 newline, line_buffering);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 Py_CLEAR(buf);
876 if (stream == NULL)
877 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 if (write_mode)
880 mode = "w";
881 else
882 mode = "r";
883 text = PyUnicode_FromString(mode);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200884 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 goto error;
886 Py_CLEAR(text);
887 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +0000888
889error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 Py_XDECREF(buf);
891 Py_XDECREF(stream);
892 Py_XDECREF(text);
893 Py_XDECREF(raw);
894 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +0000895}
896
Georg Brandl1a3284e2007-12-02 09:40:06 +0000897/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000898static int
899initstdio(void)
900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyObject *iomod = NULL, *wrapper;
902 PyObject *bimod = NULL;
903 PyObject *m;
904 PyObject *std = NULL;
905 int status = 0, fd;
906 PyObject * encoding_attr;
907 char *encoding = NULL, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 /* Hack to avoid a nasty recursion issue when Python is invoked
910 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
911 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
912 goto error;
913 }
914 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
917 goto error;
918 }
919 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 if (!(bimod = PyImport_ImportModule("builtins"))) {
922 goto error;
923 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (!(iomod = PyImport_ImportModule("io"))) {
926 goto error;
927 }
928 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
929 goto error;
930 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 /* Set builtins.open */
933 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000934 Py_DECREF(wrapper);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 goto error;
936 }
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000937 Py_DECREF(wrapper);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 encoding = Py_GETENV("PYTHONIOENCODING");
940 errors = NULL;
941 if (encoding) {
942 encoding = strdup(encoding);
943 errors = strchr(encoding, ':');
944 if (errors) {
945 *errors = '\0';
946 errors++;
947 }
948 }
Martin v. Löwis0f599892008-06-02 11:13:03 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 /* Set sys.stdin */
951 fd = fileno(stdin);
952 /* Under some conditions stdin, stdout and stderr may not be connected
953 * and fileno() may point to an invalid file descriptor. For example
954 * GUI apps don't have valid standard streams by default.
955 */
956 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000957#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 std = Py_None;
959 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000960#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000962#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 }
964 else {
965 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
966 if (std == NULL)
967 goto error;
968 } /* if (fd < 0) */
969 PySys_SetObject("__stdin__", std);
970 PySys_SetObject("stdin", std);
971 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 /* Set sys.stdout */
974 fd = fileno(stdout);
975 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000976#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 std = Py_None;
978 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000979#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000981#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 }
983 else {
984 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
985 if (std == NULL)
986 goto error;
987 } /* if (fd < 0) */
988 PySys_SetObject("__stdout__", std);
989 PySys_SetObject("stdout", std);
990 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000991
Guido van Rossum98297ee2007-11-06 21:34:58 +0000992#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 /* Set sys.stderr, replaces the preliminary stderr */
994 fd = fileno(stderr);
995 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000996#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 std = Py_None;
998 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000999#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +00001001#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 }
1003 else {
1004 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1005 if (std == NULL)
1006 goto error;
1007 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 /* Same as hack above, pre-import stderr's codec to avoid recursion
1010 when import.c tries to write to stderr in verbose mode. */
1011 encoding_attr = PyObject_GetAttrString(std, "encoding");
1012 if (encoding_attr != NULL) {
1013 const char * encoding;
1014 encoding = _PyUnicode_AsString(encoding_attr);
1015 if (encoding != NULL) {
1016 _PyCodec_Lookup(encoding);
1017 }
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +00001018 Py_DECREF(encoding_attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 }
1020 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 PySys_SetObject("__stderr__", std);
1023 PySys_SetObject("stderr", std);
1024 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001025#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001028 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 status = -1;
1030 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 if (encoding)
1033 free(encoding);
1034 Py_XDECREF(bimod);
1035 Py_XDECREF(iomod);
1036 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001037}
1038
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001039/* Parse input from a file and execute it */
1040
1041int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001042PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 if (filename == NULL)
1046 filename = "???";
1047 if (Py_FdIsInteractive(fp, filename)) {
1048 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
1049 if (closeit)
1050 fclose(fp);
1051 return err;
1052 }
1053 else
1054 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001055}
1056
1057int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001058PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 PyObject *v;
1061 int ret;
1062 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 if (flags == NULL) {
1065 flags = &local_flags;
1066 local_flags.cf_flags = 0;
1067 }
1068 v = PySys_GetObject("ps1");
1069 if (v == NULL) {
1070 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
1071 Py_XDECREF(v);
1072 }
1073 v = PySys_GetObject("ps2");
1074 if (v == NULL) {
1075 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1076 Py_XDECREF(v);
1077 }
1078 for (;;) {
1079 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
1080 PRINT_TOTAL_REFS();
1081 if (ret == E_EOF)
1082 return 0;
1083 /*
1084 if (ret == E_NOMEM)
1085 return -1;
1086 */
1087 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001088}
1089
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001090/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001091static int PARSER_FLAGS(PyCompilerFlags *flags)
1092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 int parser_flags = 0;
1094 if (!flags)
1095 return 0;
1096 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1097 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1098 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1099 parser_flags |= PyPARSE_IGNORE_COOKIE;
1100 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1101 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1102 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001103}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001104
Thomas Wouters89f507f2006-12-13 04:49:30 +00001105#if 0
1106/* Keep an example of flags with future keyword support. */
1107#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1109 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1110 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1111 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001112#endif
1113
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001114int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001115PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 PyObject *m, *d, *v, *w, *oenc = NULL;
1118 mod_ty mod;
1119 PyArena *arena;
1120 char *ps1 = "", *ps2 = "", *enc = NULL;
1121 int errcode = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001122 _Py_IDENTIFIER(encoding);
Tim Petersfe2127d2001-07-16 05:37:24 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 if (fp == stdin) {
1125 /* Fetch encoding from sys.stdin */
1126 v = PySys_GetObject("stdin");
1127 if (v == NULL || v == Py_None)
1128 return -1;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001129 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (!oenc)
1131 return -1;
1132 enc = _PyUnicode_AsString(oenc);
Victor Stinner386fe712010-05-19 00:34:15 +00001133 if (enc == NULL)
1134 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 }
1136 v = PySys_GetObject("ps1");
1137 if (v != NULL) {
1138 v = PyObject_Str(v);
1139 if (v == NULL)
1140 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001141 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +00001143 if (ps1 == NULL) {
1144 PyErr_Clear();
1145 ps1 = "";
1146 }
1147 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 }
1149 w = PySys_GetObject("ps2");
1150 if (w != NULL) {
1151 w = PyObject_Str(w);
1152 if (w == NULL)
1153 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001154 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +00001156 if (ps2 == NULL) {
1157 PyErr_Clear();
1158 ps2 = "";
1159 }
1160 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 }
1162 arena = PyArena_New();
1163 if (arena == NULL) {
1164 Py_XDECREF(v);
1165 Py_XDECREF(w);
1166 Py_XDECREF(oenc);
1167 return -1;
1168 }
1169 mod = PyParser_ASTFromFile(fp, filename, enc,
1170 Py_single_input, ps1, ps2,
1171 flags, &errcode, arena);
1172 Py_XDECREF(v);
1173 Py_XDECREF(w);
1174 Py_XDECREF(oenc);
1175 if (mod == NULL) {
1176 PyArena_Free(arena);
1177 if (errcode == E_EOF) {
1178 PyErr_Clear();
1179 return E_EOF;
1180 }
1181 PyErr_Print();
1182 return -1;
1183 }
1184 m = PyImport_AddModule("__main__");
1185 if (m == NULL) {
1186 PyArena_Free(arena);
1187 return -1;
1188 }
1189 d = PyModule_GetDict(m);
1190 v = run_mod(mod, filename, d, d, flags, arena);
1191 PyArena_Free(arena);
1192 flush_io();
1193 if (v == NULL) {
1194 PyErr_Print();
1195 return -1;
1196 }
1197 Py_DECREF(v);
1198 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001199}
1200
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001201/* Check whether a file maybe a pyc file: Look at the extension,
1202 the file type, and, if we may close it, at the first few bytes. */
1203
1204static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001205maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1208 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 /* Only look into the file if we are allowed to close it, since
1211 it then should also be seekable. */
1212 if (closeit) {
1213 /* Read only two bytes of the magic. If the file was opened in
1214 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1215 be read as they are on disk. */
1216 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1217 unsigned char buf[2];
1218 /* Mess: In case of -x, the stream is NOT at its start now,
1219 and ungetc() was used to push back the first newline,
1220 which makes the current stream position formally undefined,
1221 and a x-platform nightmare.
1222 Unfortunately, we have no direct way to know whether -x
1223 was specified. So we use a terrible hack: if the current
1224 stream position is not 0, we assume -x was specified, and
1225 give up. Bug 132850 on SourceForge spells out the
1226 hopelessness of trying anything else (fseek and ftell
1227 don't work predictably x-platform for text-mode files).
1228 */
1229 int ispyc = 0;
1230 if (ftell(fp) == 0) {
1231 if (fread(buf, 1, 2, fp) == 2 &&
1232 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1233 ispyc = 1;
1234 rewind(fp);
1235 }
1236 return ispyc;
1237 }
1238 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001239}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001240
Guido van Rossum0df002c2000-08-27 19:21:52 +00001241int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001242PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 PyObject *m, *d, *v;
1246 const char *ext;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001247 int set_file_name = 0, ret;
1248 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 m = PyImport_AddModule("__main__");
1251 if (m == NULL)
1252 return -1;
1253 d = PyModule_GetDict(m);
1254 if (PyDict_GetItemString(d, "__file__") == NULL) {
1255 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001256 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if (f == NULL)
1258 return -1;
1259 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1260 Py_DECREF(f);
1261 return -1;
1262 }
Barry Warsaw916048d2011-09-20 14:45:44 -04001263 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1264 Py_DECREF(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 return -1;
Barry Warsaw916048d2011-09-20 14:45:44 -04001266 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 set_file_name = 1;
1268 Py_DECREF(f);
1269 }
1270 len = strlen(filename);
1271 ext = filename + len - (len > 4 ? 4 : 0);
1272 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1273 /* Try to run a pyc file. First, re-open in binary */
1274 if (closeit)
1275 fclose(fp);
1276 if ((fp = fopen(filename, "rb")) == NULL) {
1277 fprintf(stderr, "python: Can't reopen .pyc file\n");
1278 ret = -1;
1279 goto done;
1280 }
1281 /* Turn on optimization if a .pyo file is given */
1282 if (strcmp(ext, ".pyo") == 0)
1283 Py_OptimizeFlag = 1;
1284 v = run_pyc_file(fp, filename, d, d, flags);
1285 } else {
1286 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1287 closeit, flags);
1288 }
1289 flush_io();
1290 if (v == NULL) {
1291 PyErr_Print();
1292 ret = -1;
1293 goto done;
1294 }
1295 Py_DECREF(v);
1296 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001297 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1299 PyErr_Clear();
1300 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001301}
1302
1303int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001304PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 PyObject *m, *d, *v;
1307 m = PyImport_AddModule("__main__");
1308 if (m == NULL)
1309 return -1;
1310 d = PyModule_GetDict(m);
1311 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1312 if (v == NULL) {
1313 PyErr_Print();
1314 return -1;
1315 }
1316 Py_DECREF(v);
1317 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001318}
1319
Barry Warsaw035574d1997-08-29 22:07:17 +00001320static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001321parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 long hold;
1325 PyObject *v;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001326 _Py_IDENTIFIER(msg);
1327 _Py_IDENTIFIER(filename);
1328 _Py_IDENTIFIER(lineno);
1329 _Py_IDENTIFIER(offset);
1330 _Py_IDENTIFIER(text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 /* old style errors */
1333 if (PyTuple_Check(err))
1334 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1335 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 /* new style errors. `err' is an instance */
Barry Warsaw035574d1997-08-29 22:07:17 +00001338
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001339 if (! (v = _PyObject_GetAttrId(err, &PyId_msg)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 goto finally;
1341 *message = v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001342
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001343 if (!(v = _PyObject_GetAttrId(err, &PyId_filename)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 goto finally;
1345 if (v == Py_None)
1346 *filename = NULL;
1347 else if (! (*filename = _PyUnicode_AsString(v)))
1348 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 Py_DECREF(v);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001351 if (!(v = _PyObject_GetAttrId(err, &PyId_lineno)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 goto finally;
1353 hold = PyLong_AsLong(v);
1354 Py_DECREF(v);
1355 v = NULL;
1356 if (hold < 0 && PyErr_Occurred())
1357 goto finally;
1358 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001359
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001360 if (!(v = _PyObject_GetAttrId(err, &PyId_offset)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 goto finally;
1362 if (v == Py_None) {
1363 *offset = -1;
1364 Py_DECREF(v);
1365 v = NULL;
1366 } else {
1367 hold = PyLong_AsLong(v);
1368 Py_DECREF(v);
1369 v = NULL;
1370 if (hold < 0 && PyErr_Occurred())
1371 goto finally;
1372 *offset = (int)hold;
1373 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001374
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001375 if (!(v = _PyObject_GetAttrId(err, &PyId_text)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 goto finally;
1377 if (v == Py_None)
1378 *text = NULL;
1379 else if (!PyUnicode_Check(v) ||
1380 !(*text = _PyUnicode_AsString(v)))
1381 goto finally;
1382 Py_DECREF(v);
1383 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001384
1385finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 Py_XDECREF(v);
1387 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001388}
1389
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001390void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001391PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001394}
1395
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001396static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001397print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 char *nl;
1400 if (offset >= 0) {
Benjamin Petersona95e9772010-10-29 03:28:14 +00001401 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1402 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 for (;;) {
1404 nl = strchr(text, '\n');
1405 if (nl == NULL || nl-text >= offset)
1406 break;
1407 offset -= (int)(nl+1-text);
1408 text = nl+1;
1409 }
1410 while (*text == ' ' || *text == '\t') {
1411 text++;
1412 offset--;
1413 }
1414 }
1415 PyFile_WriteString(" ", f);
1416 PyFile_WriteString(text, f);
1417 if (*text == '\0' || text[strlen(text)-1] != '\n')
1418 PyFile_WriteString("\n", f);
1419 if (offset == -1)
1420 return;
1421 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +00001422 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001425}
1426
Guido van Rossum66e8e862001-03-23 17:54:43 +00001427static void
1428handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 PyObject *exception, *value, *tb;
1431 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 if (Py_InspectFlag)
1434 /* Don't exit if -i flag was given. This flag is set to 0
1435 * when entering interactive mode for inspecting. */
1436 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 PyErr_Fetch(&exception, &value, &tb);
1439 fflush(stdout);
1440 if (value == NULL || value == Py_None)
1441 goto done;
1442 if (PyExceptionInstance_Check(value)) {
1443 /* The error code should be in the `code' attribute. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001444 _Py_IDENTIFIER(code);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001445 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 if (code) {
1447 Py_DECREF(value);
1448 value = code;
1449 if (value == Py_None)
1450 goto done;
1451 }
1452 /* If we failed to dig out the 'code' attribute,
1453 just let the else clause below print the error. */
1454 }
1455 if (PyLong_Check(value))
1456 exitcode = (int)PyLong_AsLong(value);
1457 else {
Victor Stinnere9fb3192010-05-17 08:58:51 +00001458 PyObject *sys_stderr = PySys_GetObject("stderr");
Victor Stinner7126dbc2010-05-21 23:45:42 +00001459 if (sys_stderr != NULL && sys_stderr != Py_None) {
1460 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1461 } else {
1462 PyObject_Print(value, stderr, Py_PRINT_RAW);
1463 fflush(stderr);
1464 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 PySys_WriteStderr("\n");
1466 exitcode = 1;
1467 }
Tim Peterscf615b52003-04-19 18:47:02 +00001468 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 /* Restore and clear the exception info, in order to properly decref
1470 * the exception, value, and traceback. If we just exit instead,
1471 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1472 * some finalizers from running.
1473 */
1474 PyErr_Restore(exception, value, tb);
1475 PyErr_Clear();
1476 Py_Exit(exitcode);
1477 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001478}
1479
1480void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001481PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1486 handle_system_exit();
1487 }
1488 PyErr_Fetch(&exception, &v, &tb);
1489 if (exception == NULL)
1490 return;
1491 PyErr_NormalizeException(&exception, &v, &tb);
1492 if (tb == NULL) {
1493 tb = Py_None;
1494 Py_INCREF(tb);
1495 }
1496 PyException_SetTraceback(v, tb);
1497 if (exception == NULL)
1498 return;
1499 /* Now we know v != NULL too */
1500 if (set_sys_last_vars) {
1501 PySys_SetObject("last_type", exception);
1502 PySys_SetObject("last_value", v);
1503 PySys_SetObject("last_traceback", tb);
1504 }
1505 hook = PySys_GetObject("excepthook");
1506 if (hook) {
1507 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1508 PyObject *result = PyEval_CallObject(hook, args);
1509 if (result == NULL) {
1510 PyObject *exception2, *v2, *tb2;
1511 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1512 handle_system_exit();
1513 }
1514 PyErr_Fetch(&exception2, &v2, &tb2);
1515 PyErr_NormalizeException(&exception2, &v2, &tb2);
1516 /* It should not be possible for exception2 or v2
1517 to be NULL. However PyErr_Display() can't
1518 tolerate NULLs, so just be safe. */
1519 if (exception2 == NULL) {
1520 exception2 = Py_None;
1521 Py_INCREF(exception2);
1522 }
1523 if (v2 == NULL) {
1524 v2 = Py_None;
1525 Py_INCREF(v2);
1526 }
1527 fflush(stdout);
1528 PySys_WriteStderr("Error in sys.excepthook:\n");
1529 PyErr_Display(exception2, v2, tb2);
1530 PySys_WriteStderr("\nOriginal exception was:\n");
1531 PyErr_Display(exception, v, tb);
1532 Py_DECREF(exception2);
1533 Py_DECREF(v2);
1534 Py_XDECREF(tb2);
1535 }
1536 Py_XDECREF(result);
1537 Py_XDECREF(args);
1538 } else {
1539 PySys_WriteStderr("sys.excepthook is missing\n");
1540 PyErr_Display(exception, v, tb);
1541 }
1542 Py_XDECREF(exception);
1543 Py_XDECREF(v);
1544 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001545}
1546
Benjamin Petersone6528212008-07-15 15:32:09 +00001547static void
1548print_exception(PyObject *f, PyObject *value)
1549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 int err = 0;
1551 PyObject *type, *tb;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001552 _Py_IDENTIFIER(print_file_and_line);
Benjamin Petersone6528212008-07-15 15:32:09 +00001553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 if (!PyExceptionInstance_Check(value)) {
1555 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1556 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1557 PyFile_WriteString(" found\n", f);
1558 return;
1559 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 Py_INCREF(value);
1562 fflush(stdout);
1563 type = (PyObject *) Py_TYPE(value);
1564 tb = PyException_GetTraceback(value);
1565 if (tb && tb != Py_None)
1566 err = PyTraceBack_Print(tb, f);
1567 if (err == 0 &&
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001568 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 {
1570 PyObject *message;
1571 const char *filename, *text;
1572 int lineno, offset;
1573 if (!parse_syntax_error(value, &message, &filename,
1574 &lineno, &offset, &text))
1575 PyErr_Clear();
1576 else {
1577 char buf[10];
1578 PyFile_WriteString(" File \"", f);
1579 if (filename == NULL)
1580 PyFile_WriteString("<string>", f);
1581 else
1582 PyFile_WriteString(filename, f);
1583 PyFile_WriteString("\", line ", f);
1584 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1585 PyFile_WriteString(buf, f);
1586 PyFile_WriteString("\n", f);
1587 if (text != NULL)
1588 print_error_text(f, offset, text);
1589 Py_DECREF(value);
1590 value = message;
1591 /* Can't be bothered to check all those
1592 PyFile_WriteString() calls */
1593 if (PyErr_Occurred())
1594 err = -1;
1595 }
1596 }
1597 if (err) {
1598 /* Don't do anything else */
1599 }
1600 else {
1601 PyObject* moduleName;
1602 char* className;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001603 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 assert(PyExceptionClass_Check(type));
1605 className = PyExceptionClass_Name(type);
1606 if (className != NULL) {
1607 char *dot = strrchr(className, '.');
1608 if (dot != NULL)
1609 className = dot+1;
1610 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001611
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001612 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1614 {
Victor Stinner13b21bd2011-05-26 14:25:13 +02001615 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 err = PyFile_WriteString("<unknown>", f);
1617 }
1618 else {
1619 char* modstr = _PyUnicode_AsString(moduleName);
1620 if (modstr && strcmp(modstr, "builtins"))
1621 {
1622 err = PyFile_WriteString(modstr, f);
1623 err += PyFile_WriteString(".", f);
1624 }
1625 Py_DECREF(moduleName);
1626 }
1627 if (err == 0) {
1628 if (className == NULL)
1629 err = PyFile_WriteString("<unknown>", f);
1630 else
1631 err = PyFile_WriteString(className, f);
1632 }
1633 }
1634 if (err == 0 && (value != Py_None)) {
1635 PyObject *s = PyObject_Str(value);
1636 /* only print colon if the str() of the
1637 object is not the empty string
1638 */
1639 if (s == NULL)
1640 err = -1;
1641 else if (!PyUnicode_Check(s) ||
1642 PyUnicode_GetSize(s) != 0)
1643 err = PyFile_WriteString(": ", f);
1644 if (err == 0)
1645 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1646 Py_XDECREF(s);
1647 }
1648 /* try to write a newline in any case */
1649 err += PyFile_WriteString("\n", f);
1650 Py_XDECREF(tb);
1651 Py_DECREF(value);
1652 /* If an error happened here, don't show it.
1653 XXX This is wrong, but too many callers rely on this behavior. */
1654 if (err != 0)
1655 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001656}
1657
1658static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 "\nThe above exception was the direct cause "
1660 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001661
1662static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 "\nDuring handling of the above exception, "
1664 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001665
1666static void
1667print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 int err = 0, res;
1670 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 if (seen != NULL) {
1673 /* Exception chaining */
1674 if (PySet_Add(seen, value) == -1)
1675 PyErr_Clear();
1676 else if (PyExceptionInstance_Check(value)) {
1677 cause = PyException_GetCause(value);
1678 context = PyException_GetContext(value);
1679 if (cause) {
1680 res = PySet_Contains(seen, cause);
1681 if (res == -1)
1682 PyErr_Clear();
1683 if (res == 0) {
1684 print_exception_recursive(
1685 f, cause, seen);
1686 err |= PyFile_WriteString(
1687 cause_message, f);
1688 }
1689 }
1690 else if (context) {
1691 res = PySet_Contains(seen, context);
1692 if (res == -1)
1693 PyErr_Clear();
1694 if (res == 0) {
1695 print_exception_recursive(
1696 f, context, seen);
1697 err |= PyFile_WriteString(
1698 context_message, f);
1699 }
1700 }
1701 Py_XDECREF(context);
1702 Py_XDECREF(cause);
1703 }
1704 }
1705 print_exception(f, value);
1706 if (err != 0)
1707 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001708}
1709
Thomas Wouters477c8d52006-05-27 19:21:47 +00001710void
1711PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 PyObject *seen;
1714 PyObject *f = PySys_GetObject("stderr");
1715 if (f == Py_None) {
1716 /* pass */
1717 }
1718 else if (f == NULL) {
1719 _PyObject_Dump(value);
1720 fprintf(stderr, "lost sys.stderr\n");
1721 }
1722 else {
1723 /* We choose to ignore seen being possibly NULL, and report
1724 at least the main exception (it could be a MemoryError).
1725 */
1726 seen = PySet_New(NULL);
1727 if (seen == NULL)
1728 PyErr_Clear();
1729 print_exception_recursive(f, value, seen);
1730 Py_XDECREF(seen);
1731 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001732}
1733
Guido van Rossum82598051997-03-05 00:20:32 +00001734PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001735PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 PyObject *ret = NULL;
1739 mod_ty mod;
1740 PyArena *arena = PyArena_New();
1741 if (arena == NULL)
1742 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1745 if (mod != NULL)
1746 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1747 PyArena_Free(arena);
1748 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001749}
1750
1751PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001752PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 PyObject *ret;
1756 mod_ty mod;
1757 PyArena *arena = PyArena_New();
1758 if (arena == NULL)
1759 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1762 flags, NULL, arena);
1763 if (closeit)
1764 fclose(fp);
1765 if (mod == NULL) {
1766 PyArena_Free(arena);
1767 return NULL;
1768 }
1769 ret = run_mod(mod, filename, globals, locals, flags, arena);
1770 PyArena_Free(arena);
1771 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001772}
1773
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001774static void
1775flush_io(void)
1776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 PyObject *f, *r;
1778 PyObject *type, *value, *traceback;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001779 _Py_IDENTIFIER(flush);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 /* Save the current exception */
1782 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 f = PySys_GetObject("stderr");
1785 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001786 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (r)
1788 Py_DECREF(r);
1789 else
1790 PyErr_Clear();
1791 }
1792 f = PySys_GetObject("stdout");
1793 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001794 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 if (r)
1796 Py_DECREF(r);
1797 else
1798 PyErr_Clear();
1799 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001802}
1803
Guido van Rossum82598051997-03-05 00:20:32 +00001804static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 PyCodeObject *co;
1809 PyObject *v;
1810 co = PyAST_Compile(mod, filename, flags, arena);
1811 if (co == NULL)
1812 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001813 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 Py_DECREF(co);
1815 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001816}
1817
Guido van Rossum82598051997-03-05 00:20:32 +00001818static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001819run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 PyCodeObject *co;
1823 PyObject *v;
1824 long magic;
1825 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 magic = PyMarshal_ReadLongFromFile(fp);
1828 if (magic != PyImport_GetMagicNumber()) {
1829 PyErr_SetString(PyExc_RuntimeError,
1830 "Bad magic number in .pyc file");
1831 return NULL;
1832 }
1833 (void) PyMarshal_ReadLongFromFile(fp);
1834 v = PyMarshal_ReadLastObjectFromFile(fp);
1835 fclose(fp);
1836 if (v == NULL || !PyCode_Check(v)) {
1837 Py_XDECREF(v);
1838 PyErr_SetString(PyExc_RuntimeError,
1839 "Bad code object in .pyc file");
1840 return NULL;
1841 }
1842 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001843 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 if (v && flags)
1845 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1846 Py_DECREF(co);
1847 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001848}
1849
Guido van Rossum82598051997-03-05 00:20:32 +00001850PyObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00001851Py_CompileStringExFlags(const char *str, const char *filename, int start,
1852 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 PyCodeObject *co;
1855 mod_ty mod;
1856 PyArena *arena = PyArena_New();
1857 if (arena == NULL)
1858 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1861 if (mod == NULL) {
1862 PyArena_Free(arena);
1863 return NULL;
1864 }
1865 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1866 PyObject *result = PyAST_mod2obj(mod);
1867 PyArena_Free(arena);
1868 return result;
1869 }
Georg Brandl8334fd92010-12-04 10:26:46 +00001870 co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 PyArena_Free(arena);
1872 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001873}
1874
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001875/* For use in Py_LIMITED_API */
1876#undef Py_CompileString
1877PyObject *
1878PyCompileString(const char *str, const char *filename, int start)
1879{
1880 return Py_CompileStringFlags(str, filename, start, NULL);
1881}
1882
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001883struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001884Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 struct symtable *st;
1887 mod_ty mod;
1888 PyCompilerFlags flags;
1889 PyArena *arena = PyArena_New();
1890 if (arena == NULL)
1891 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 flags.cf_flags = 0;
1894 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1895 if (mod == NULL) {
1896 PyArena_Free(arena);
1897 return NULL;
1898 }
1899 st = PySymtable_Build(mod, filename, 0);
1900 PyArena_Free(arena);
1901 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001902}
1903
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904/* Preferred access to parser is through AST. */
1905mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001906PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 mod_ty mod;
1910 PyCompilerFlags localflags;
1911 perrdetail err;
1912 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1915 &_PyParser_Grammar, start, &err,
1916 &iflags);
1917 if (flags == NULL) {
1918 localflags.cf_flags = 0;
1919 flags = &localflags;
1920 }
1921 if (n) {
1922 flags->cf_flags |= iflags & PyCF_MASK;
1923 mod = PyAST_FromNode(n, flags, filename, arena);
1924 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 }
1926 else {
1927 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001928 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001930 err_free(&err);
1931 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932}
1933
1934mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001935PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 int start, char *ps1,
1937 char *ps2, PyCompilerFlags *flags, int *errcode,
1938 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 mod_ty mod;
1941 PyCompilerFlags localflags;
1942 perrdetail err;
1943 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
1946 &_PyParser_Grammar,
1947 start, ps1, ps2, &err, &iflags);
1948 if (flags == NULL) {
1949 localflags.cf_flags = 0;
1950 flags = &localflags;
1951 }
1952 if (n) {
1953 flags->cf_flags |= iflags & PyCF_MASK;
1954 mod = PyAST_FromNode(n, flags, filename, arena);
1955 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 }
1957 else {
1958 err_input(&err);
1959 if (errcode)
1960 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02001961 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001963 err_free(&err);
1964 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965}
1966
Guido van Rossuma110aa61994-08-29 12:50:44 +00001967/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001968
Guido van Rossuma110aa61994-08-29 12:50:44 +00001969node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001970PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 perrdetail err;
1973 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1974 &_PyParser_Grammar,
1975 start, NULL, NULL, &err, flags);
1976 if (n == NULL)
1977 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001978 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001981}
1982
Guido van Rossuma110aa61994-08-29 12:50:44 +00001983/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001984
Guido van Rossuma110aa61994-08-29 12:50:44 +00001985node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001986PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 perrdetail err;
1989 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1990 start, &err, flags);
1991 if (n == NULL)
1992 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001993 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001995}
1996
1997node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001998PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 perrdetail err;
2002 node *n = PyParser_ParseStringFlagsFilename(str, filename,
2003 &_PyParser_Grammar, start, &err, flags);
2004 if (n == NULL)
2005 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002006 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00002008}
2009
2010node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002011PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00002014}
2015
Guido van Rossum66ebd912003-04-17 16:02:26 +00002016/* May want to move a more generalized form of this to parsetok.c or
2017 even parser modules. */
2018
2019void
Victor Stinner7f2fee32011-04-05 00:39:01 +02002020PyParser_ClearError(perrdetail *err)
2021{
2022 err_free(err);
2023}
2024
2025void
Guido van Rossum66ebd912003-04-17 16:02:26 +00002026PyParser_SetError(perrdetail *err)
2027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00002029}
2030
Victor Stinner7f2fee32011-04-05 00:39:01 +02002031static void
2032err_free(perrdetail *err)
2033{
2034 Py_CLEAR(err->filename);
2035}
2036
Guido van Rossuma110aa61994-08-29 12:50:44 +00002037/* Set the error appropriate to the given input error code (see errcode.h) */
2038
2039static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002040err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00002041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 PyObject *v, *w, *errtype, *errtext;
2043 PyObject *msg_obj = NULL;
2044 char *msg = NULL;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 errtype = PyExc_SyntaxError;
2047 switch (err->error) {
2048 case E_ERROR:
2049 return;
2050 case E_SYNTAX:
2051 errtype = PyExc_IndentationError;
2052 if (err->expected == INDENT)
2053 msg = "expected an indented block";
2054 else if (err->token == INDENT)
2055 msg = "unexpected indent";
2056 else if (err->token == DEDENT)
2057 msg = "unexpected unindent";
2058 else {
2059 errtype = PyExc_SyntaxError;
2060 msg = "invalid syntax";
2061 }
2062 break;
2063 case E_TOKEN:
2064 msg = "invalid token";
2065 break;
2066 case E_EOFS:
2067 msg = "EOF while scanning triple-quoted string literal";
2068 break;
2069 case E_EOLS:
2070 msg = "EOL while scanning string literal";
2071 break;
2072 case E_INTR:
2073 if (!PyErr_Occurred())
2074 PyErr_SetNone(PyExc_KeyboardInterrupt);
2075 goto cleanup;
2076 case E_NOMEM:
2077 PyErr_NoMemory();
2078 goto cleanup;
2079 case E_EOF:
2080 msg = "unexpected EOF while parsing";
2081 break;
2082 case E_TABSPACE:
2083 errtype = PyExc_TabError;
2084 msg = "inconsistent use of tabs and spaces in indentation";
2085 break;
2086 case E_OVERFLOW:
2087 msg = "expression too long";
2088 break;
2089 case E_DEDENT:
2090 errtype = PyExc_IndentationError;
2091 msg = "unindent does not match any outer indentation level";
2092 break;
2093 case E_TOODEEP:
2094 errtype = PyExc_IndentationError;
2095 msg = "too many levels of indentation";
2096 break;
2097 case E_DECODE: {
2098 PyObject *type, *value, *tb;
2099 PyErr_Fetch(&type, &value, &tb);
2100 msg = "unknown decode error";
2101 if (value != NULL)
2102 msg_obj = PyObject_Str(value);
2103 Py_XDECREF(type);
2104 Py_XDECREF(value);
2105 Py_XDECREF(tb);
2106 break;
2107 }
2108 case E_LINECONT:
2109 msg = "unexpected character after line continuation character";
2110 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00002111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 case E_IDENTIFIER:
2113 msg = "invalid character in identifier";
2114 break;
2115 default:
2116 fprintf(stderr, "error=%d\n", err->error);
2117 msg = "unknown parsing error";
2118 break;
2119 }
2120 /* err->text may not be UTF-8 in case of decoding errors.
2121 Explicitly convert to an object. */
2122 if (!err->text) {
2123 errtext = Py_None;
2124 Py_INCREF(Py_None);
2125 } else {
2126 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2127 "replace");
2128 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002129 v = Py_BuildValue("(OiiN)", err->filename,
2130 err->lineno, err->offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 if (v != NULL) {
2132 if (msg_obj)
2133 w = Py_BuildValue("(OO)", msg_obj, v);
2134 else
2135 w = Py_BuildValue("(sO)", msg, v);
2136 } else
2137 w = NULL;
2138 Py_XDECREF(v);
2139 PyErr_SetObject(errtype, w);
2140 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002141cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 Py_XDECREF(msg_obj);
2143 if (err->text != NULL) {
2144 PyObject_FREE(err->text);
2145 err->text = NULL;
2146 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002147}
2148
2149/* Print fatal error message and abort */
2150
2151void
Tim Peters7c321a82002-07-09 02:57:01 +00002152Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002153{
Victor Stinner024e37a2011-03-31 01:31:06 +02002154 const int fd = fileno(stderr);
2155 PyThreadState *tstate;
2156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 fprintf(stderr, "Fatal Python error: %s\n", msg);
2158 fflush(stderr); /* it helps in Windows debug build */
2159 if (PyErr_Occurred()) {
Victor Stinner55a5c782010-06-08 21:00:13 +00002160 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 }
Victor Stinner024e37a2011-03-31 01:31:06 +02002162 else {
2163 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
2164 if (tstate != NULL) {
2165 fputc('\n', stderr);
2166 fflush(stderr);
Victor Stinner7bba62f2011-05-07 12:43:00 +02002167 _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
Victor Stinner024e37a2011-03-31 01:31:06 +02002168 }
Victor Stinnerd727e232011-04-01 12:13:55 +02002169 _PyFaulthandler_Fini();
Victor Stinner024e37a2011-03-31 01:31:06 +02002170 }
2171
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002172#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 {
2174 size_t len = strlen(msg);
2175 WCHAR* buffer;
2176 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 /* Convert the message to wchar_t. This uses a simple one-to-one
2179 conversion, assuming that the this error message actually uses ASCII
2180 only. If this ceases to be true, we will have to convert. */
2181 buffer = alloca( (len+1) * (sizeof *buffer));
2182 for( i=0; i<=len; ++i)
2183 buffer[i] = msg[i];
2184 OutputDebugStringW(L"Fatal Python error: ");
2185 OutputDebugStringW(buffer);
2186 OutputDebugStringW(L"\n");
2187 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002188#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002190#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002191#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002193}
2194
2195/* Clean up and exit */
2196
Guido van Rossuma110aa61994-08-29 12:50:44 +00002197#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002198#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002199#endif
2200
Collin Winter670e6922007-03-21 02:57:17 +00002201static void (*pyexitfunc)(void) = NULL;
2202/* For the atexit module. */
2203void _Py_PyAtExit(void (*func)(void))
2204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002206}
2207
2208static void
2209call_py_exitfuncs(void)
2210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 if (pyexitfunc == NULL)
2212 return;
Collin Winter670e6922007-03-21 02:57:17 +00002213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 (*pyexitfunc)();
2215 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002216}
2217
Antoine Pitrou011bd622009-10-20 21:52:47 +00002218/* Wait until threading._shutdown completes, provided
2219 the threading module was imported in the first place.
2220 The shutdown routine will wait until all non-daemon
2221 "threading" threads have completed. */
2222static void
2223wait_for_thread_shutdown(void)
2224{
2225#ifdef WITH_THREAD
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002226 _Py_IDENTIFIER(_shutdown);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 PyObject *result;
2228 PyThreadState *tstate = PyThreadState_GET();
2229 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2230 "threading");
2231 if (threading == NULL) {
2232 /* threading not imported */
2233 PyErr_Clear();
2234 return;
2235 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002236 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 if (result == NULL) {
2238 PyErr_WriteUnraisable(threading);
2239 }
2240 else {
2241 Py_DECREF(result);
2242 }
2243 Py_DECREF(threading);
Antoine Pitrou011bd622009-10-20 21:52:47 +00002244#endif
2245}
2246
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002247#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002248static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002249static int nexitfuncs = 0;
2250
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002251int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 if (nexitfuncs >= NEXITFUNCS)
2254 return -1;
2255 exitfuncs[nexitfuncs++] = func;
2256 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002257}
2258
Guido van Rossumcc283f51997-08-05 02:22:03 +00002259static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002260call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 while (nexitfuncs > 0)
2263 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 fflush(stdout);
2266 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002267}
2268
2269void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002270Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002275}
2276
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002277static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002278initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002279{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002280#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002282#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002283#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002285#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002286#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002288#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002290}
2291
Guido van Rossum7433b121997-02-14 19:45:36 +00002292
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002293/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2294 *
2295 * All of the code in this function must only use async-signal-safe functions,
2296 * listed at `man 7 signal` or
2297 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2298 */
2299void
2300_Py_RestoreSignals(void)
2301{
2302#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 PyOS_setsig(SIGPIPE, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002304#endif
2305#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 PyOS_setsig(SIGXFZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002307#endif
2308#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 PyOS_setsig(SIGXFSZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002310#endif
2311}
2312
2313
Guido van Rossum7433b121997-02-14 19:45:36 +00002314/*
2315 * The file descriptor fd is considered ``interactive'' if either
2316 * a) isatty(fd) is TRUE, or
2317 * b) the -i flag was given, and the filename associated with
2318 * the descriptor is NULL or "<stdin>" or "???".
2319 */
2320int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002321Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 if (isatty((int)fileno(fp)))
2324 return 1;
2325 if (!Py_InteractiveFlag)
2326 return 0;
2327 return (filename == NULL) ||
2328 (strcmp(filename, "<stdin>") == 0) ||
2329 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002330}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002331
2332
Tim Petersd08e3822003-04-17 15:24:21 +00002333#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002334#if defined(WIN32) && defined(_MSC_VER)
2335
2336/* Stack checking for Microsoft C */
2337
2338#include <malloc.h>
2339#include <excpt.h>
2340
Fred Drakee8de31c2000-08-31 05:38:39 +00002341/*
2342 * Return non-zero when we run out of memory on the stack; zero otherwise.
2343 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002344int
Fred Drake399739f2000-08-31 05:52:44 +00002345PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 __try {
2348 /* alloca throws a stack overflow exception if there's
2349 not enough space left on the stack */
2350 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2351 return 0;
2352 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2353 EXCEPTION_EXECUTE_HANDLER :
2354 EXCEPTION_CONTINUE_SEARCH) {
2355 int errcode = _resetstkoflw();
2356 if (errcode == 0)
2357 {
2358 Py_FatalError("Could not reset the stack!");
2359 }
2360 }
2361 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002362}
2363
2364#endif /* WIN32 && _MSC_VER */
2365
2366/* Alternate implementations can be added here... */
2367
2368#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002369
2370
2371/* Wrappers around sigaction() or signal(). */
2372
2373PyOS_sighandler_t
2374PyOS_getsig(int sig)
2375{
2376#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 struct sigaction context;
2378 if (sigaction(sig, NULL, &context) == -1)
2379 return SIG_ERR;
2380 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002381#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002383/* Special signal handling for the secure CRT in Visual Studio 2005 */
2384#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 switch (sig) {
2386 /* Only these signals are valid */
2387 case SIGINT:
2388 case SIGILL:
2389 case SIGFPE:
2390 case SIGSEGV:
2391 case SIGTERM:
2392 case SIGBREAK:
2393 case SIGABRT:
2394 break;
2395 /* Don't call signal() with other values or it will assert */
2396 default:
2397 return SIG_ERR;
2398 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002399#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 handler = signal(sig, SIG_IGN);
2401 if (handler != SIG_ERR)
2402 signal(sig, handler);
2403 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002404#endif
2405}
2406
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002407/*
2408 * All of the code in this function must only use async-signal-safe functions,
2409 * listed at `man 7 signal` or
2410 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2411 */
Guido van Rossum6f256182000-09-16 16:32:19 +00002412PyOS_sighandler_t
2413PyOS_setsig(int sig, PyOS_sighandler_t handler)
2414{
2415#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 /* Some code in Modules/signalmodule.c depends on sigaction() being
2417 * used here if HAVE_SIGACTION is defined. Fix that if this code
2418 * changes to invalidate that assumption.
2419 */
2420 struct sigaction context, ocontext;
2421 context.sa_handler = handler;
2422 sigemptyset(&context.sa_mask);
2423 context.sa_flags = 0;
2424 if (sigaction(sig, &context, &ocontext) == -1)
2425 return SIG_ERR;
2426 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002427#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 PyOS_sighandler_t oldhandler;
2429 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002430#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002432#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002434#endif
2435}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436
2437/* Deprecated C API functions still provided for binary compatiblity */
2438
2439#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002440PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002441PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444}
2445
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002446#undef PyParser_SimpleParseString
2447PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448PyParser_SimpleParseString(const char *str, int start)
2449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002452
2453#undef PyRun_AnyFile
2454PyAPI_FUNC(int)
2455PyRun_AnyFile(FILE *fp, const char *name)
2456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002458}
2459
2460#undef PyRun_AnyFileEx
2461PyAPI_FUNC(int)
2462PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002465}
2466
2467#undef PyRun_AnyFileFlags
2468PyAPI_FUNC(int)
2469PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002472}
2473
2474#undef PyRun_File
2475PyAPI_FUNC(PyObject *)
2476PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002479}
2480
2481#undef PyRun_FileEx
2482PyAPI_FUNC(PyObject *)
2483PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002486}
2487
2488#undef PyRun_FileFlags
2489PyAPI_FUNC(PyObject *)
2490PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002494}
2495
2496#undef PyRun_SimpleFile
2497PyAPI_FUNC(int)
2498PyRun_SimpleFile(FILE *f, const char *p)
2499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002501}
2502
2503#undef PyRun_SimpleFileEx
2504PyAPI_FUNC(int)
2505PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002508}
2509
2510
2511#undef PyRun_String
2512PyAPI_FUNC(PyObject *)
2513PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002516}
2517
2518#undef PyRun_SimpleString
2519PyAPI_FUNC(int)
2520PyRun_SimpleString(const char *s)
2521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002523}
2524
2525#undef Py_CompileString
2526PyAPI_FUNC(PyObject *)
2527Py_CompileString(const char *str, const char *p, int s)
2528{
Georg Brandl8334fd92010-12-04 10:26:46 +00002529 return Py_CompileStringExFlags(str, p, s, NULL, -1);
2530}
2531
2532#undef Py_CompileStringFlags
2533PyAPI_FUNC(PyObject *)
2534Py_CompileStringFlags(const char *str, const char *p, int s,
2535 PyCompilerFlags *flags)
2536{
2537 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002538}
2539
2540#undef PyRun_InteractiveOne
2541PyAPI_FUNC(int)
2542PyRun_InteractiveOne(FILE *f, const char *p)
2543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002545}
2546
2547#undef PyRun_InteractiveLoop
2548PyAPI_FUNC(int)
2549PyRun_InteractiveLoop(FILE *f, const char *p)
2550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002552}
2553
2554#ifdef __cplusplus
2555}
2556#endif