blob: 0c267fc832694b459c7918aea383c56120c53828 [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);
Victor Stinner3a50e702011-10-18 21:21:00 +020070extern int _PyUnicode_Init(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000071extern 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 */
Victor Stinner3a50e702011-10-18 21:21:00 +0200264 if (_PyUnicode_Init() < 0)
265 Py_FatalError("Py_Initialize: can't initialize unicode");
Guido van Rossumc94044c2000-03-10 23:03:54 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 bimod = _PyBuiltin_Init();
268 if (bimod == NULL)
269 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Victor Stinner49d3f252010-10-17 01:24:53 +0000270 _PyImport_FixupBuiltin(bimod, "builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 interp->builtins = PyModule_GetDict(bimod);
272 if (interp->builtins == NULL)
273 Py_FatalError("Py_Initialize: can't initialize builtins dict");
274 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 /* initialize builtin exceptions */
277 _PyExc_Init();
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 sysmod = _PySys_Init();
280 if (sysmod == NULL)
281 Py_FatalError("Py_Initialize: can't initialize sys");
282 interp->sysdict = PyModule_GetDict(sysmod);
283 if (interp->sysdict == NULL)
284 Py_FatalError("Py_Initialize: can't initialize sys dict");
285 Py_INCREF(interp->sysdict);
Victor Stinner49d3f252010-10-17 01:24:53 +0000286 _PyImport_FixupBuiltin(sysmod, "sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 PySys_SetPath(Py_GetPath());
288 PyDict_SetItemString(interp->sysdict, "modules",
289 interp->modules);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 /* Set up a preliminary stderr printer until we have enough
292 infrastructure for the io module in place. */
293 pstderr = PyFile_NewStdPrinter(fileno(stderr));
294 if (pstderr == NULL)
295 Py_FatalError("Py_Initialize: can't set preliminary stderr");
296 PySys_SetObject("stderr", pstderr);
297 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000298 Py_DECREF(pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 _PyImport_Init();
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 _PyImportHooks_Init();
Just van Rossum52e14d62002-12-30 22:08:05 +0000303
Victor Stinner024e37a2011-03-31 01:31:06 +0200304 /* initialize the faulthandler module */
305 if (_PyFaulthandler_Init())
306 Py_FatalError("Py_Initialize: can't initialize faulthandler");
307
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000308 /* Initialize _warnings. */
309 _PyWarnings_Init();
310
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000311 _PyTime_Init();
312
Victor Stinner793b5312011-04-27 00:24:21 +0200313 if (initfsencoding(interp) < 0)
314 Py_FatalError("Py_Initialize: unable to load the file system codec");
Martin v. Löwis011e8422009-05-05 04:43:17 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 if (install_sigs)
317 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 initmain(); /* Module __main__ */
320 if (initstdio() < 0)
321 Py_FatalError(
322 "Py_Initialize: can't initialize sys standard streams");
323
Antoine Pitroucf9f9802010-11-10 13:55:25 +0000324 /* Initialize warnings. */
325 if (PySys_HasWarnOptions()) {
326 PyObject *warnings_module = PyImport_ImportModule("warnings");
327 if (warnings_module == NULL) {
328 fprintf(stderr, "'import warnings' failed; traceback:\n");
329 PyErr_Print();
330 }
331 Py_XDECREF(warnings_module);
332 }
333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 if (!Py_NoSiteFlag)
335 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000336}
337
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000338void
339Py_Initialize(void)
340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 Py_InitializeEx(1);
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000342}
343
344
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000345#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000347#endif
348
Guido van Rossume8432ac2007-07-09 15:04:50 +0000349/* Flush stdout and stderr */
350
Neal Norwitz2bad9702007-08-27 06:19:22 +0000351static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000352flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 PyObject *fout = PySys_GetObject("stdout");
355 PyObject *ferr = PySys_GetObject("stderr");
356 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200357 _Py_IDENTIFIER(flush);
Guido van Rossume8432ac2007-07-09 15:04:50 +0000358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (fout != NULL && fout != Py_None) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200360 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (tmp == NULL)
Antoine Pitroubddc9fe2010-08-08 20:46:42 +0000362 PyErr_WriteUnraisable(fout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 else
364 Py_DECREF(tmp);
365 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000366
Victor Stinner9467b212010-05-14 00:59:09 +0000367 if (ferr != NULL && ferr != Py_None) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200368 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 if (tmp == NULL)
370 PyErr_Clear();
371 else
372 Py_DECREF(tmp);
373 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000374}
375
Guido van Rossum25ce5661997-08-02 03:10:38 +0000376/* Undo the effect of Py_Initialize().
377
378 Beware: if multiple interpreter and/or thread states exist, these
379 are not wiped out; only the current thread and interpreter state
380 are deleted. But since everything else is deleted, those other
381 interpreter and thread states should no longer be used.
382
383 (XXX We should do better, e.g. wipe out all interpreters and
384 threads.)
385
386 Locking: as above.
387
388*/
389
390void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000391Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 PyInterpreterState *interp;
394 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 if (!initialized)
397 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 wait_for_thread_shutdown();
Antoine Pitrou011bd622009-10-20 21:52:47 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 /* The interpreter is still entirely intact at this point, and the
402 * exit funcs may be relying on that. In particular, if some thread
403 * or exit func is still waiting to do an import, the import machinery
404 * expects Py_IsInitialized() to return true. So don't say the
405 * interpreter is uninitialized until after the exit funcs have run.
406 * Note that Threading.py uses an exit func to do a join on all the
407 * threads created thru it, so this also protects pending imports in
408 * the threads created via Threading.
409 */
410 call_py_exitfuncs();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 /* Get current thread state and interpreter pointer */
413 tstate = PyThreadState_GET();
414 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000415
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200416 /* Remaining threads (e.g. daemon threads) will automatically exit
417 after taking the GIL (in PyEval_RestoreThread()). */
418 _Py_Finalizing = tstate;
419 initialized = 0;
420
421 /* Flush stdout+stderr */
422 flush_std_files();
423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 /* Disable signal handling */
425 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 /* Clear type lookup cache */
428 PyType_ClearCache();
Christian Heimes26855632008-01-27 23:50:43 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 /* Collect garbage. This may call finalizers; it's nice to call these
431 * before all modules are destroyed.
432 * XXX If a __del__ or weakref callback is triggered here, and tries to
433 * XXX import a module, bad things can happen, because Python no
434 * XXX longer believes it's initialized.
435 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
436 * XXX is easy to provoke that way. I've also seen, e.g.,
437 * XXX Exception exceptions.ImportError: 'No module named sha'
438 * XXX in <function callback at 0x008F5718> ignored
439 * XXX but I'm unclear on exactly how that one happens. In any case,
440 * XXX I haven't seen a real-life report of either of these.
441 */
442 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 /* With COUNT_ALLOCS, it helps to run GC multiple times:
445 each collection might release some types from the type
446 list, so they become garbage. */
447 while (PyGC_Collect() > 0)
448 /* nothing */;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000449#endif
Antoine Pitrou696e0352010-08-08 22:18:46 +0000450 /* We run this while most interpreter state is still alive, so that
451 debug information can be printed out */
452 _PyGC_Fini();
Guido van Rossume13ddc92003-04-17 17:29:22 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 /* Destroy all modules */
455 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 /* Flush stdout+stderr (again, in case more was printed) */
458 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 /* Collect final garbage. This disposes of cycles created by
461 * new-style class definitions, for example.
462 * XXX This is disabled because it caused too many problems. If
463 * XXX a __del__ or weakref callback triggers here, Python code has
464 * XXX a hard time running, because even the sys module has been
465 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
466 * XXX One symptom is a sequence of information-free messages
467 * XXX coming from threads (if a __del__ or callback is invoked,
468 * XXX other threads can execute too, and any exception they encounter
469 * XXX triggers a comedy of errors as subsystem after subsystem
470 * XXX fails to find what it *expects* to find in sys to help report
471 * XXX the exception and consequent unexpected failures). I've also
472 * XXX seen segfaults then, after adding print statements to the
473 * XXX Python code getting called.
474 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000475#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000477#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
480 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000481
Victor Stinner024e37a2011-03-31 01:31:06 +0200482 /* unload faulthandler module */
483 _PyFaulthandler_Fini();
484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000486#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000488#endif
489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000491
Tim Peters9cf25ce2003-04-17 15:21:01 +0000492#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 /* Display all objects still alive -- this can invoke arbitrary
494 * __repr__ overrides, so requires a mostly-intact interpreter.
495 * Alas, a lot of stuff may still be alive now that will be cleaned
496 * up later.
497 */
498 if (Py_GETENV("PYTHONDUMPREFS"))
499 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000500#endif /* Py_TRACE_REFS */
501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* Clear interpreter state */
503 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 /* Now we decref the exception classes. After this point nothing
506 can raise an exception. That's okay, because each Fini() method
507 below has been checked to make sure no exceptions are ever
508 raised.
509 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 /* Cleanup auto-thread-state */
Christian Heimes7d2ff882007-11-30 14:35:04 +0000514#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 _PyGILState_Fini();
Christian Heimes7d2ff882007-11-30 14:35:04 +0000516#endif /* WITH_THREAD */
517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 /* Delete current thread */
519 PyThreadState_Swap(NULL);
520 PyInterpreterState_Delete(interp);
Barry Warsawf242aa02000-05-25 23:09:49 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 /* Sundry finalizers */
523 PyMethod_Fini();
524 PyFrame_Fini();
525 PyCFunction_Fini();
526 PyTuple_Fini();
527 PyList_Fini();
528 PySet_Fini();
529 PyBytes_Fini();
530 PyByteArray_Fini();
531 PyLong_Fini();
532 PyFloat_Fini();
533 PyDict_Fini();
Antoine Pitrouf34a0cd2011-11-18 20:14:34 +0100534 PySlice_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 /* Cleanup Unicode implementation */
537 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 /* reset file system default encoding */
Victor Stinnerb744ba12010-05-15 12:27:16 +0000540 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 free((char*)Py_FileSystemDefaultEncoding);
542 Py_FileSystemDefaultEncoding = NULL;
543 }
Christian Heimesc8967002007-11-30 10:18:26 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 /* XXX Still allocated:
546 - various static ad-hoc pointers to interned strings
547 - int and float free list blocks
548 - whatever various modules and libraries allocate
549 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000552
Tim Peters269b2a62003-04-17 19:52:29 +0000553#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 /* Display addresses (& refcnts) of all objects still alive.
555 * An address can be used to find the repr of the object, printed
556 * above by _Py_PrintReferences.
557 */
558 if (Py_GETENV("PYTHONDUMPREFS"))
559 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000560#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000561#ifdef PYMALLOC_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 if (Py_GETENV("PYTHONMALLOCSTATS"))
563 _PyObject_DebugMallocStats();
Tim Peters0e871182002-04-13 08:29:14 +0000564#endif
565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000567}
568
569/* Create and initialize a new interpreter and thread, and return the
570 new thread. This requires that Py_Initialize() has been called
571 first.
572
573 Unsuccessful initialization yields a NULL pointer. Note that *no*
574 exception information is available even in this case -- the
575 exception information is held in the thread, and there is no
576 thread.
577
578 Locking: as above.
579
580*/
581
582PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000583Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 PyInterpreterState *interp;
586 PyThreadState *tstate, *save_tstate;
587 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 if (!initialized)
590 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 interp = PyInterpreterState_New();
593 if (interp == NULL)
594 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 tstate = PyThreadState_New(interp);
597 if (tstate == NULL) {
598 PyInterpreterState_Delete(interp);
599 return NULL;
600 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 interp->modules = PyDict_New();
607 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000608
Victor Stinner49d3f252010-10-17 01:24:53 +0000609 bimod = _PyImport_FindBuiltin("builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 if (bimod != NULL) {
611 interp->builtins = PyModule_GetDict(bimod);
612 if (interp->builtins == NULL)
613 goto handle_error;
614 Py_INCREF(interp->builtins);
615 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 /* initialize builtin exceptions */
618 _PyExc_Init();
Christian Heimes6a27efa2008-10-30 21:48:26 +0000619
Victor Stinner49d3f252010-10-17 01:24:53 +0000620 sysmod = _PyImport_FindBuiltin("sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 if (bimod != NULL && sysmod != NULL) {
622 PyObject *pstderr;
623 interp->sysdict = PyModule_GetDict(sysmod);
624 if (interp->sysdict == NULL)
625 goto handle_error;
626 Py_INCREF(interp->sysdict);
627 PySys_SetPath(Py_GetPath());
628 PyDict_SetItemString(interp->sysdict, "modules",
629 interp->modules);
630 /* Set up a preliminary stderr printer until we have enough
631 infrastructure for the io module in place. */
632 pstderr = PyFile_NewStdPrinter(fileno(stderr));
633 if (pstderr == NULL)
634 Py_FatalError("Py_Initialize: can't set preliminary stderr");
635 PySys_SetObject("stderr", pstderr);
636 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000637 Py_DECREF(pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 _PyImportHooks_Init();
Victor Stinner793b5312011-04-27 00:24:21 +0200640
641 if (initfsencoding(interp) < 0)
642 goto handle_error;
643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 if (initstdio() < 0)
645 Py_FatalError(
646 "Py_Initialize: can't initialize sys standard streams");
647 initmain();
648 if (!Py_NoSiteFlag)
649 initsite();
650 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 if (!PyErr_Occurred())
653 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000654
Thomas Wouters89f507f2006-12-13 04:49:30 +0000655handle_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000657
Victor Stinnerc40a3502011-04-27 00:20:27 +0200658 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 PyThreadState_Clear(tstate);
660 PyThreadState_Swap(save_tstate);
661 PyThreadState_Delete(tstate);
662 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000665}
666
667/* Delete an interpreter and its last thread. This requires that the
668 given thread state is current, that the thread has no remaining
669 frames, and that it is its interpreter's only remaining thread.
670 It is a fatal error to violate these constraints.
671
672 (Py_Finalize() doesn't have these constraints -- it zaps
673 everything, regardless.)
674
675 Locking: as above.
676
677*/
678
679void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000680Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 if (tstate != PyThreadState_GET())
685 Py_FatalError("Py_EndInterpreter: thread is not current");
686 if (tstate->frame != NULL)
687 Py_FatalError("Py_EndInterpreter: thread still has a frame");
688 if (tstate != interp->tstate_head || tstate->next != NULL)
689 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 PyImport_Cleanup();
692 PyInterpreterState_Clear(interp);
693 PyThreadState_Swap(NULL);
694 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000695}
696
Martin v. Löwis790465f2008-04-05 20:41:37 +0000697static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000698
699void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000700Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 if (pn && *pn)
703 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000704}
705
Martin v. Löwis790465f2008-04-05 20:41:37 +0000706wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000707Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000710}
711
Martin v. Löwis790465f2008-04-05 20:41:37 +0000712static wchar_t *default_home = NULL;
713static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000714
715void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000716Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000719}
720
Martin v. Löwis790465f2008-04-05 20:41:37 +0000721wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000722Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 wchar_t *home = default_home;
725 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
726 char* chome = Py_GETENV("PYTHONHOME");
727 if (chome) {
728 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
729 if (r != (size_t)-1 && r <= PATH_MAX)
730 home = env_home;
731 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 }
734 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000735}
736
Guido van Rossum6135a871995-01-09 17:53:26 +0000737/* Create __main__ module */
738
739static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000740initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 PyObject *m, *d;
743 m = PyImport_AddModule("__main__");
744 if (m == NULL)
745 Py_FatalError("can't create __main__ module");
746 d = PyModule_GetDict(m);
747 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
748 PyObject *bimod = PyImport_ImportModule("builtins");
749 if (bimod == NULL ||
750 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
751 Py_FatalError("can't add __builtins__ to __main__");
752 Py_DECREF(bimod);
753 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000754}
755
Victor Stinner793b5312011-04-27 00:24:21 +0200756static int
757initfsencoding(PyInterpreterState *interp)
Victor Stinnerb744ba12010-05-15 12:27:16 +0000758{
759 PyObject *codec;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000760
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200761 if (Py_FileSystemDefaultEncoding == NULL)
762 {
763 Py_FileSystemDefaultEncoding = get_locale_encoding();
764 if (Py_FileSystemDefaultEncoding == NULL)
Victor Stinnere4743092010-10-19 00:05:51 +0000765 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
Victor Stinnerb744ba12010-05-15 12:27:16 +0000766
Victor Stinnere4743092010-10-19 00:05:51 +0000767 Py_HasFileSystemDefaultEncoding = 0;
Victor Stinner793b5312011-04-27 00:24:21 +0200768 interp->fscodec_initialized = 1;
769 return 0;
Victor Stinner7f84ab52010-06-11 00:36:33 +0000770 }
Victor Stinnerb744ba12010-05-15 12:27:16 +0000771
772 /* the encoding is mbcs, utf-8 or ascii */
773 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
774 if (!codec) {
775 /* Such error can only occurs in critical situations: no more
776 * memory, import a module of the standard library failed,
777 * etc. */
Victor Stinner793b5312011-04-27 00:24:21 +0200778 return -1;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000779 }
Victor Stinner793b5312011-04-27 00:24:21 +0200780 Py_DECREF(codec);
781 interp->fscodec_initialized = 1;
782 return 0;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000783}
784
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000785/* Import the site module (not into __main__ though) */
786
787static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000788initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 PyObject *m;
791 m = PyImport_ImportModule("site");
792 if (m == NULL) {
793 PyErr_Print();
794 Py_Finalize();
795 exit(1);
796 }
797 else {
798 Py_DECREF(m);
799 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000800}
801
Antoine Pitrou05608432009-01-09 18:53:14 +0000802static PyObject*
803create_stdio(PyObject* io,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 int fd, int write_mode, char* name,
805 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +0000806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
808 const char* mode;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000809 const char* newline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 PyObject *line_buffering;
811 int buffering, isatty;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200812 _Py_IDENTIFIER(open);
813 _Py_IDENTIFIER(isatty);
814 _Py_IDENTIFIER(TextIOWrapper);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200815 _Py_IDENTIFIER(name);
816 _Py_IDENTIFIER(mode);
Antoine Pitrou05608432009-01-09 18:53:14 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 /* stdin is always opened in buffered mode, first because it shouldn't
819 make a difference in common use cases, second because TextIOWrapper
820 depends on the presence of a read1() method which only exists on
821 buffered streams.
822 */
823 if (Py_UnbufferedStdioFlag && write_mode)
824 buffering = 0;
825 else
826 buffering = -1;
827 if (write_mode)
828 mode = "wb";
829 else
830 mode = "rb";
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200831 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
832 fd, mode, buffering,
833 Py_None, Py_None, Py_None, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 if (buf == NULL)
835 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 if (buffering) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200838 _Py_IDENTIFIER(raw);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200839 raw = _PyObject_GetAttrId(buf, &PyId_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 if (raw == NULL)
841 goto error;
842 }
843 else {
844 raw = buf;
845 Py_INCREF(raw);
846 }
Antoine Pitrou05608432009-01-09 18:53:14 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 text = PyUnicode_FromString(name);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200849 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 goto error;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200851 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 if (res == NULL)
853 goto error;
854 isatty = PyObject_IsTrue(res);
855 Py_DECREF(res);
856 if (isatty == -1)
857 goto error;
858 if (isatty || Py_UnbufferedStdioFlag)
859 line_buffering = Py_True;
860 else
861 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 Py_CLEAR(raw);
864 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +0000865
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000866 newline = "\n";
867#ifdef MS_WINDOWS
868 if (!write_mode) {
869 /* translate \r\n to \n for sys.stdin on Windows */
870 newline = NULL;
871 }
872#endif
873
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200874 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
875 buf, encoding, errors,
876 newline, line_buffering);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 Py_CLEAR(buf);
878 if (stream == NULL)
879 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (write_mode)
882 mode = "w";
883 else
884 mode = "r";
885 text = PyUnicode_FromString(mode);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200886 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 goto error;
888 Py_CLEAR(text);
889 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +0000890
891error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 Py_XDECREF(buf);
893 Py_XDECREF(stream);
894 Py_XDECREF(text);
895 Py_XDECREF(raw);
896 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +0000897}
898
Georg Brandl1a3284e2007-12-02 09:40:06 +0000899/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000900static int
901initstdio(void)
902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 PyObject *iomod = NULL, *wrapper;
904 PyObject *bimod = NULL;
905 PyObject *m;
906 PyObject *std = NULL;
907 int status = 0, fd;
908 PyObject * encoding_attr;
909 char *encoding = NULL, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 /* Hack to avoid a nasty recursion issue when Python is invoked
912 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
913 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
914 goto error;
915 }
916 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
919 goto error;
920 }
921 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 if (!(bimod = PyImport_ImportModule("builtins"))) {
924 goto error;
925 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 if (!(iomod = PyImport_ImportModule("io"))) {
928 goto error;
929 }
930 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
931 goto error;
932 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 /* Set builtins.open */
935 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000936 Py_DECREF(wrapper);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 goto error;
938 }
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000939 Py_DECREF(wrapper);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 encoding = Py_GETENV("PYTHONIOENCODING");
942 errors = NULL;
943 if (encoding) {
944 encoding = strdup(encoding);
945 errors = strchr(encoding, ':');
946 if (errors) {
947 *errors = '\0';
948 errors++;
949 }
950 }
Martin v. Löwis0f599892008-06-02 11:13:03 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 /* Set sys.stdin */
953 fd = fileno(stdin);
954 /* Under some conditions stdin, stdout and stderr may not be connected
955 * and fileno() may point to an invalid file descriptor. For example
956 * GUI apps don't have valid standard streams by default.
957 */
958 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000959#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 std = Py_None;
961 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000962#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000964#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 }
966 else {
967 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
968 if (std == NULL)
969 goto error;
970 } /* if (fd < 0) */
971 PySys_SetObject("__stdin__", std);
972 PySys_SetObject("stdin", std);
973 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 /* Set sys.stdout */
976 fd = fileno(stdout);
977 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000978#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 std = Py_None;
980 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000981#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000983#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 }
985 else {
986 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
987 if (std == NULL)
988 goto error;
989 } /* if (fd < 0) */
990 PySys_SetObject("__stdout__", std);
991 PySys_SetObject("stdout", std);
992 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000993
Guido van Rossum98297ee2007-11-06 21:34:58 +0000994#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 /* Set sys.stderr, replaces the preliminary stderr */
996 fd = fileno(stderr);
997 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000998#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 std = Py_None;
1000 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +00001001#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +00001003#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 }
1005 else {
1006 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1007 if (std == NULL)
1008 goto error;
1009 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 /* Same as hack above, pre-import stderr's codec to avoid recursion
1012 when import.c tries to write to stderr in verbose mode. */
1013 encoding_attr = PyObject_GetAttrString(std, "encoding");
1014 if (encoding_attr != NULL) {
1015 const char * encoding;
1016 encoding = _PyUnicode_AsString(encoding_attr);
1017 if (encoding != NULL) {
1018 _PyCodec_Lookup(encoding);
1019 }
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +00001020 Py_DECREF(encoding_attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 }
1022 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 PySys_SetObject("__stderr__", std);
1025 PySys_SetObject("stderr", std);
1026 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001027#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001030 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 status = -1;
1032 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 if (encoding)
1035 free(encoding);
1036 Py_XDECREF(bimod);
1037 Py_XDECREF(iomod);
1038 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001039}
1040
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001041/* Parse input from a file and execute it */
1042
1043int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001044PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 if (filename == NULL)
1048 filename = "???";
1049 if (Py_FdIsInteractive(fp, filename)) {
1050 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
1051 if (closeit)
1052 fclose(fp);
1053 return err;
1054 }
1055 else
1056 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001057}
1058
1059int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001060PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 PyObject *v;
1063 int ret;
1064 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (flags == NULL) {
1067 flags = &local_flags;
1068 local_flags.cf_flags = 0;
1069 }
1070 v = PySys_GetObject("ps1");
1071 if (v == NULL) {
1072 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
1073 Py_XDECREF(v);
1074 }
1075 v = PySys_GetObject("ps2");
1076 if (v == NULL) {
1077 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1078 Py_XDECREF(v);
1079 }
1080 for (;;) {
1081 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
1082 PRINT_TOTAL_REFS();
1083 if (ret == E_EOF)
1084 return 0;
1085 /*
1086 if (ret == E_NOMEM)
1087 return -1;
1088 */
1089 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001090}
1091
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001092/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001093static int PARSER_FLAGS(PyCompilerFlags *flags)
1094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 int parser_flags = 0;
1096 if (!flags)
1097 return 0;
1098 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1099 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1100 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1101 parser_flags |= PyPARSE_IGNORE_COOKIE;
1102 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1103 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1104 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001105}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001106
Thomas Wouters89f507f2006-12-13 04:49:30 +00001107#if 0
1108/* Keep an example of flags with future keyword support. */
1109#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1111 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1112 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1113 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001114#endif
1115
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001116int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001117PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 PyObject *m, *d, *v, *w, *oenc = NULL;
1120 mod_ty mod;
1121 PyArena *arena;
1122 char *ps1 = "", *ps2 = "", *enc = NULL;
1123 int errcode = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001124 _Py_IDENTIFIER(encoding);
Tim Petersfe2127d2001-07-16 05:37:24 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 if (fp == stdin) {
1127 /* Fetch encoding from sys.stdin */
1128 v = PySys_GetObject("stdin");
1129 if (v == NULL || v == Py_None)
1130 return -1;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001131 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 if (!oenc)
1133 return -1;
1134 enc = _PyUnicode_AsString(oenc);
Victor Stinner386fe712010-05-19 00:34:15 +00001135 if (enc == NULL)
1136 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 }
1138 v = PySys_GetObject("ps1");
1139 if (v != NULL) {
1140 v = PyObject_Str(v);
1141 if (v == NULL)
1142 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001143 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +00001145 if (ps1 == NULL) {
1146 PyErr_Clear();
1147 ps1 = "";
1148 }
1149 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 }
1151 w = PySys_GetObject("ps2");
1152 if (w != NULL) {
1153 w = PyObject_Str(w);
1154 if (w == NULL)
1155 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001156 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +00001158 if (ps2 == NULL) {
1159 PyErr_Clear();
1160 ps2 = "";
1161 }
1162 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 }
1164 arena = PyArena_New();
1165 if (arena == NULL) {
1166 Py_XDECREF(v);
1167 Py_XDECREF(w);
1168 Py_XDECREF(oenc);
1169 return -1;
1170 }
1171 mod = PyParser_ASTFromFile(fp, filename, enc,
1172 Py_single_input, ps1, ps2,
1173 flags, &errcode, arena);
1174 Py_XDECREF(v);
1175 Py_XDECREF(w);
1176 Py_XDECREF(oenc);
1177 if (mod == NULL) {
1178 PyArena_Free(arena);
1179 if (errcode == E_EOF) {
1180 PyErr_Clear();
1181 return E_EOF;
1182 }
1183 PyErr_Print();
1184 return -1;
1185 }
1186 m = PyImport_AddModule("__main__");
1187 if (m == NULL) {
1188 PyArena_Free(arena);
1189 return -1;
1190 }
1191 d = PyModule_GetDict(m);
1192 v = run_mod(mod, filename, d, d, flags, arena);
1193 PyArena_Free(arena);
1194 flush_io();
1195 if (v == NULL) {
1196 PyErr_Print();
1197 return -1;
1198 }
1199 Py_DECREF(v);
1200 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001201}
1202
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001203/* Check whether a file maybe a pyc file: Look at the extension,
1204 the file type, and, if we may close it, at the first few bytes. */
1205
1206static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001207maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1210 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 /* Only look into the file if we are allowed to close it, since
1213 it then should also be seekable. */
1214 if (closeit) {
1215 /* Read only two bytes of the magic. If the file was opened in
1216 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1217 be read as they are on disk. */
1218 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1219 unsigned char buf[2];
1220 /* Mess: In case of -x, the stream is NOT at its start now,
1221 and ungetc() was used to push back the first newline,
1222 which makes the current stream position formally undefined,
1223 and a x-platform nightmare.
1224 Unfortunately, we have no direct way to know whether -x
1225 was specified. So we use a terrible hack: if the current
1226 stream position is not 0, we assume -x was specified, and
1227 give up. Bug 132850 on SourceForge spells out the
1228 hopelessness of trying anything else (fseek and ftell
1229 don't work predictably x-platform for text-mode files).
1230 */
1231 int ispyc = 0;
1232 if (ftell(fp) == 0) {
1233 if (fread(buf, 1, 2, fp) == 2 &&
1234 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1235 ispyc = 1;
1236 rewind(fp);
1237 }
1238 return ispyc;
1239 }
1240 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001241}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001242
Guido van Rossum0df002c2000-08-27 19:21:52 +00001243int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001244PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 PyObject *m, *d, *v;
1248 const char *ext;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001249 int set_file_name = 0, ret;
1250 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 m = PyImport_AddModule("__main__");
1253 if (m == NULL)
1254 return -1;
1255 d = PyModule_GetDict(m);
1256 if (PyDict_GetItemString(d, "__file__") == NULL) {
1257 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001258 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 if (f == NULL)
1260 return -1;
1261 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1262 Py_DECREF(f);
1263 return -1;
1264 }
Barry Warsaw916048d2011-09-20 14:45:44 -04001265 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1266 Py_DECREF(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 return -1;
Barry Warsaw916048d2011-09-20 14:45:44 -04001268 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 set_file_name = 1;
1270 Py_DECREF(f);
1271 }
1272 len = strlen(filename);
1273 ext = filename + len - (len > 4 ? 4 : 0);
1274 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1275 /* Try to run a pyc file. First, re-open in binary */
1276 if (closeit)
1277 fclose(fp);
1278 if ((fp = fopen(filename, "rb")) == NULL) {
1279 fprintf(stderr, "python: Can't reopen .pyc file\n");
1280 ret = -1;
1281 goto done;
1282 }
1283 /* Turn on optimization if a .pyo file is given */
1284 if (strcmp(ext, ".pyo") == 0)
1285 Py_OptimizeFlag = 1;
1286 v = run_pyc_file(fp, filename, d, d, flags);
1287 } else {
1288 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1289 closeit, flags);
1290 }
1291 flush_io();
1292 if (v == NULL) {
1293 PyErr_Print();
1294 ret = -1;
1295 goto done;
1296 }
1297 Py_DECREF(v);
1298 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001299 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1301 PyErr_Clear();
1302 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001303}
1304
1305int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001306PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 PyObject *m, *d, *v;
1309 m = PyImport_AddModule("__main__");
1310 if (m == NULL)
1311 return -1;
1312 d = PyModule_GetDict(m);
1313 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1314 if (v == NULL) {
1315 PyErr_Print();
1316 return -1;
1317 }
1318 Py_DECREF(v);
1319 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001320}
1321
Barry Warsaw035574d1997-08-29 22:07:17 +00001322static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001323parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 long hold;
1327 PyObject *v;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001328 _Py_IDENTIFIER(msg);
1329 _Py_IDENTIFIER(filename);
1330 _Py_IDENTIFIER(lineno);
1331 _Py_IDENTIFIER(offset);
1332 _Py_IDENTIFIER(text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 /* old style errors */
1335 if (PyTuple_Check(err))
1336 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1337 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 /* new style errors. `err' is an instance */
Barry Warsaw035574d1997-08-29 22:07:17 +00001340
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001341 if (! (v = _PyObject_GetAttrId(err, &PyId_msg)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 goto finally;
1343 *message = v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001344
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001345 if (!(v = _PyObject_GetAttrId(err, &PyId_filename)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 goto finally;
1347 if (v == Py_None)
1348 *filename = NULL;
1349 else if (! (*filename = _PyUnicode_AsString(v)))
1350 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 Py_DECREF(v);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001353 if (!(v = _PyObject_GetAttrId(err, &PyId_lineno)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 goto finally;
1355 hold = PyLong_AsLong(v);
1356 Py_DECREF(v);
1357 v = NULL;
1358 if (hold < 0 && PyErr_Occurred())
1359 goto finally;
1360 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001361
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001362 if (!(v = _PyObject_GetAttrId(err, &PyId_offset)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 goto finally;
1364 if (v == Py_None) {
1365 *offset = -1;
1366 Py_DECREF(v);
1367 v = NULL;
1368 } else {
1369 hold = PyLong_AsLong(v);
1370 Py_DECREF(v);
1371 v = NULL;
1372 if (hold < 0 && PyErr_Occurred())
1373 goto finally;
1374 *offset = (int)hold;
1375 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001376
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001377 if (!(v = _PyObject_GetAttrId(err, &PyId_text)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 goto finally;
1379 if (v == Py_None)
1380 *text = NULL;
1381 else if (!PyUnicode_Check(v) ||
1382 !(*text = _PyUnicode_AsString(v)))
1383 goto finally;
1384 Py_DECREF(v);
1385 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001386
1387finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 Py_XDECREF(v);
1389 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001390}
1391
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001392void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001393PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001396}
1397
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001398static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001399print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 char *nl;
1402 if (offset >= 0) {
Benjamin Petersona95e9772010-10-29 03:28:14 +00001403 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1404 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 for (;;) {
1406 nl = strchr(text, '\n');
1407 if (nl == NULL || nl-text >= offset)
1408 break;
1409 offset -= (int)(nl+1-text);
1410 text = nl+1;
1411 }
1412 while (*text == ' ' || *text == '\t') {
1413 text++;
1414 offset--;
1415 }
1416 }
1417 PyFile_WriteString(" ", f);
1418 PyFile_WriteString(text, f);
1419 if (*text == '\0' || text[strlen(text)-1] != '\n')
1420 PyFile_WriteString("\n", f);
1421 if (offset == -1)
1422 return;
1423 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +00001424 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001427}
1428
Guido van Rossum66e8e862001-03-23 17:54:43 +00001429static void
1430handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 PyObject *exception, *value, *tb;
1433 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (Py_InspectFlag)
1436 /* Don't exit if -i flag was given. This flag is set to 0
1437 * when entering interactive mode for inspecting. */
1438 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 PyErr_Fetch(&exception, &value, &tb);
1441 fflush(stdout);
1442 if (value == NULL || value == Py_None)
1443 goto done;
1444 if (PyExceptionInstance_Check(value)) {
1445 /* The error code should be in the `code' attribute. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001446 _Py_IDENTIFIER(code);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001447 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 if (code) {
1449 Py_DECREF(value);
1450 value = code;
1451 if (value == Py_None)
1452 goto done;
1453 }
1454 /* If we failed to dig out the 'code' attribute,
1455 just let the else clause below print the error. */
1456 }
1457 if (PyLong_Check(value))
1458 exitcode = (int)PyLong_AsLong(value);
1459 else {
Victor Stinnere9fb3192010-05-17 08:58:51 +00001460 PyObject *sys_stderr = PySys_GetObject("stderr");
Victor Stinner7126dbc2010-05-21 23:45:42 +00001461 if (sys_stderr != NULL && sys_stderr != Py_None) {
1462 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1463 } else {
1464 PyObject_Print(value, stderr, Py_PRINT_RAW);
1465 fflush(stderr);
1466 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 PySys_WriteStderr("\n");
1468 exitcode = 1;
1469 }
Tim Peterscf615b52003-04-19 18:47:02 +00001470 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 /* Restore and clear the exception info, in order to properly decref
1472 * the exception, value, and traceback. If we just exit instead,
1473 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1474 * some finalizers from running.
1475 */
1476 PyErr_Restore(exception, value, tb);
1477 PyErr_Clear();
1478 Py_Exit(exitcode);
1479 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001480}
1481
1482void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001483PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1488 handle_system_exit();
1489 }
1490 PyErr_Fetch(&exception, &v, &tb);
1491 if (exception == NULL)
1492 return;
1493 PyErr_NormalizeException(&exception, &v, &tb);
1494 if (tb == NULL) {
1495 tb = Py_None;
1496 Py_INCREF(tb);
1497 }
1498 PyException_SetTraceback(v, tb);
1499 if (exception == NULL)
1500 return;
1501 /* Now we know v != NULL too */
1502 if (set_sys_last_vars) {
1503 PySys_SetObject("last_type", exception);
1504 PySys_SetObject("last_value", v);
1505 PySys_SetObject("last_traceback", tb);
1506 }
1507 hook = PySys_GetObject("excepthook");
1508 if (hook) {
1509 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1510 PyObject *result = PyEval_CallObject(hook, args);
1511 if (result == NULL) {
1512 PyObject *exception2, *v2, *tb2;
1513 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1514 handle_system_exit();
1515 }
1516 PyErr_Fetch(&exception2, &v2, &tb2);
1517 PyErr_NormalizeException(&exception2, &v2, &tb2);
1518 /* It should not be possible for exception2 or v2
1519 to be NULL. However PyErr_Display() can't
1520 tolerate NULLs, so just be safe. */
1521 if (exception2 == NULL) {
1522 exception2 = Py_None;
1523 Py_INCREF(exception2);
1524 }
1525 if (v2 == NULL) {
1526 v2 = Py_None;
1527 Py_INCREF(v2);
1528 }
1529 fflush(stdout);
1530 PySys_WriteStderr("Error in sys.excepthook:\n");
1531 PyErr_Display(exception2, v2, tb2);
1532 PySys_WriteStderr("\nOriginal exception was:\n");
1533 PyErr_Display(exception, v, tb);
1534 Py_DECREF(exception2);
1535 Py_DECREF(v2);
1536 Py_XDECREF(tb2);
1537 }
1538 Py_XDECREF(result);
1539 Py_XDECREF(args);
1540 } else {
1541 PySys_WriteStderr("sys.excepthook is missing\n");
1542 PyErr_Display(exception, v, tb);
1543 }
1544 Py_XDECREF(exception);
1545 Py_XDECREF(v);
1546 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001547}
1548
Benjamin Petersone6528212008-07-15 15:32:09 +00001549static void
1550print_exception(PyObject *f, PyObject *value)
1551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 int err = 0;
1553 PyObject *type, *tb;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001554 _Py_IDENTIFIER(print_file_and_line);
Benjamin Petersone6528212008-07-15 15:32:09 +00001555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 if (!PyExceptionInstance_Check(value)) {
1557 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1558 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1559 PyFile_WriteString(" found\n", f);
1560 return;
1561 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 Py_INCREF(value);
1564 fflush(stdout);
1565 type = (PyObject *) Py_TYPE(value);
1566 tb = PyException_GetTraceback(value);
1567 if (tb && tb != Py_None)
1568 err = PyTraceBack_Print(tb, f);
1569 if (err == 0 &&
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001570 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 {
1572 PyObject *message;
1573 const char *filename, *text;
1574 int lineno, offset;
1575 if (!parse_syntax_error(value, &message, &filename,
1576 &lineno, &offset, &text))
1577 PyErr_Clear();
1578 else {
1579 char buf[10];
1580 PyFile_WriteString(" File \"", f);
1581 if (filename == NULL)
1582 PyFile_WriteString("<string>", f);
1583 else
1584 PyFile_WriteString(filename, f);
1585 PyFile_WriteString("\", line ", f);
1586 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1587 PyFile_WriteString(buf, f);
1588 PyFile_WriteString("\n", f);
1589 if (text != NULL)
1590 print_error_text(f, offset, text);
1591 Py_DECREF(value);
1592 value = message;
1593 /* Can't be bothered to check all those
1594 PyFile_WriteString() calls */
1595 if (PyErr_Occurred())
1596 err = -1;
1597 }
1598 }
1599 if (err) {
1600 /* Don't do anything else */
1601 }
1602 else {
1603 PyObject* moduleName;
1604 char* className;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001605 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 assert(PyExceptionClass_Check(type));
1607 className = PyExceptionClass_Name(type);
1608 if (className != NULL) {
1609 char *dot = strrchr(className, '.');
1610 if (dot != NULL)
1611 className = dot+1;
1612 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001613
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001614 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1616 {
Victor Stinner13b21bd2011-05-26 14:25:13 +02001617 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 err = PyFile_WriteString("<unknown>", f);
1619 }
1620 else {
1621 char* modstr = _PyUnicode_AsString(moduleName);
1622 if (modstr && strcmp(modstr, "builtins"))
1623 {
1624 err = PyFile_WriteString(modstr, f);
1625 err += PyFile_WriteString(".", f);
1626 }
1627 Py_DECREF(moduleName);
1628 }
1629 if (err == 0) {
1630 if (className == NULL)
1631 err = PyFile_WriteString("<unknown>", f);
1632 else
1633 err = PyFile_WriteString(className, f);
1634 }
1635 }
1636 if (err == 0 && (value != Py_None)) {
1637 PyObject *s = PyObject_Str(value);
1638 /* only print colon if the str() of the
1639 object is not the empty string
1640 */
1641 if (s == NULL)
1642 err = -1;
1643 else if (!PyUnicode_Check(s) ||
1644 PyUnicode_GetSize(s) != 0)
1645 err = PyFile_WriteString(": ", f);
1646 if (err == 0)
1647 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1648 Py_XDECREF(s);
1649 }
1650 /* try to write a newline in any case */
1651 err += PyFile_WriteString("\n", f);
1652 Py_XDECREF(tb);
1653 Py_DECREF(value);
1654 /* If an error happened here, don't show it.
1655 XXX This is wrong, but too many callers rely on this behavior. */
1656 if (err != 0)
1657 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001658}
1659
1660static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 "\nThe above exception was the direct cause "
1662 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001663
1664static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 "\nDuring handling of the above exception, "
1666 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001667
1668static void
1669print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 int err = 0, res;
1672 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00001673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 if (seen != NULL) {
1675 /* Exception chaining */
1676 if (PySet_Add(seen, value) == -1)
1677 PyErr_Clear();
1678 else if (PyExceptionInstance_Check(value)) {
1679 cause = PyException_GetCause(value);
1680 context = PyException_GetContext(value);
1681 if (cause) {
1682 res = PySet_Contains(seen, cause);
1683 if (res == -1)
1684 PyErr_Clear();
1685 if (res == 0) {
1686 print_exception_recursive(
1687 f, cause, seen);
1688 err |= PyFile_WriteString(
1689 cause_message, f);
1690 }
1691 }
1692 else if (context) {
1693 res = PySet_Contains(seen, context);
1694 if (res == -1)
1695 PyErr_Clear();
1696 if (res == 0) {
1697 print_exception_recursive(
1698 f, context, seen);
1699 err |= PyFile_WriteString(
1700 context_message, f);
1701 }
1702 }
1703 Py_XDECREF(context);
1704 Py_XDECREF(cause);
1705 }
1706 }
1707 print_exception(f, value);
1708 if (err != 0)
1709 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001710}
1711
Thomas Wouters477c8d52006-05-27 19:21:47 +00001712void
1713PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 PyObject *seen;
1716 PyObject *f = PySys_GetObject("stderr");
1717 if (f == Py_None) {
1718 /* pass */
1719 }
1720 else if (f == NULL) {
1721 _PyObject_Dump(value);
1722 fprintf(stderr, "lost sys.stderr\n");
1723 }
1724 else {
1725 /* We choose to ignore seen being possibly NULL, and report
1726 at least the main exception (it could be a MemoryError).
1727 */
1728 seen = PySet_New(NULL);
1729 if (seen == NULL)
1730 PyErr_Clear();
1731 print_exception_recursive(f, value, seen);
1732 Py_XDECREF(seen);
1733 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001734}
1735
Guido van Rossum82598051997-03-05 00:20:32 +00001736PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001737PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 PyObject *ret = NULL;
1741 mod_ty mod;
1742 PyArena *arena = PyArena_New();
1743 if (arena == NULL)
1744 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1747 if (mod != NULL)
1748 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1749 PyArena_Free(arena);
1750 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001751}
1752
1753PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001754PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 PyObject *ret;
1758 mod_ty mod;
1759 PyArena *arena = PyArena_New();
1760 if (arena == NULL)
1761 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1764 flags, NULL, arena);
1765 if (closeit)
1766 fclose(fp);
1767 if (mod == NULL) {
1768 PyArena_Free(arena);
1769 return NULL;
1770 }
1771 ret = run_mod(mod, filename, globals, locals, flags, arena);
1772 PyArena_Free(arena);
1773 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001774}
1775
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001776static void
1777flush_io(void)
1778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 PyObject *f, *r;
1780 PyObject *type, *value, *traceback;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001781 _Py_IDENTIFIER(flush);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 /* Save the current exception */
1784 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 f = PySys_GetObject("stderr");
1787 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001788 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 if (r)
1790 Py_DECREF(r);
1791 else
1792 PyErr_Clear();
1793 }
1794 f = PySys_GetObject("stdout");
1795 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001796 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 if (r)
1798 Py_DECREF(r);
1799 else
1800 PyErr_Clear();
1801 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001804}
1805
Guido van Rossum82598051997-03-05 00:20:32 +00001806static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 PyCodeObject *co;
1811 PyObject *v;
1812 co = PyAST_Compile(mod, filename, flags, arena);
1813 if (co == NULL)
1814 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001815 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 Py_DECREF(co);
1817 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001818}
1819
Guido van Rossum82598051997-03-05 00:20:32 +00001820static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001821run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 PyCodeObject *co;
1825 PyObject *v;
1826 long magic;
1827 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 magic = PyMarshal_ReadLongFromFile(fp);
1830 if (magic != PyImport_GetMagicNumber()) {
1831 PyErr_SetString(PyExc_RuntimeError,
1832 "Bad magic number in .pyc file");
1833 return NULL;
1834 }
1835 (void) PyMarshal_ReadLongFromFile(fp);
1836 v = PyMarshal_ReadLastObjectFromFile(fp);
1837 fclose(fp);
1838 if (v == NULL || !PyCode_Check(v)) {
1839 Py_XDECREF(v);
1840 PyErr_SetString(PyExc_RuntimeError,
1841 "Bad code object in .pyc file");
1842 return NULL;
1843 }
1844 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001845 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 if (v && flags)
1847 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1848 Py_DECREF(co);
1849 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001850}
1851
Guido van Rossum82598051997-03-05 00:20:32 +00001852PyObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00001853Py_CompileStringExFlags(const char *str, const char *filename, int start,
1854 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 PyCodeObject *co;
1857 mod_ty mod;
1858 PyArena *arena = PyArena_New();
1859 if (arena == NULL)
1860 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1863 if (mod == NULL) {
1864 PyArena_Free(arena);
1865 return NULL;
1866 }
1867 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1868 PyObject *result = PyAST_mod2obj(mod);
1869 PyArena_Free(arena);
1870 return result;
1871 }
Georg Brandl8334fd92010-12-04 10:26:46 +00001872 co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 PyArena_Free(arena);
1874 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001875}
1876
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001877/* For use in Py_LIMITED_API */
1878#undef Py_CompileString
1879PyObject *
1880PyCompileString(const char *str, const char *filename, int start)
1881{
1882 return Py_CompileStringFlags(str, filename, start, NULL);
1883}
1884
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001885struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001886Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 struct symtable *st;
1889 mod_ty mod;
1890 PyCompilerFlags flags;
1891 PyArena *arena = PyArena_New();
1892 if (arena == NULL)
1893 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 flags.cf_flags = 0;
1896 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1897 if (mod == NULL) {
1898 PyArena_Free(arena);
1899 return NULL;
1900 }
1901 st = PySymtable_Build(mod, filename, 0);
1902 PyArena_Free(arena);
1903 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001904}
1905
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906/* Preferred access to parser is through AST. */
1907mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001908PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 mod_ty mod;
1912 PyCompilerFlags localflags;
1913 perrdetail err;
1914 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1917 &_PyParser_Grammar, start, &err,
1918 &iflags);
1919 if (flags == NULL) {
1920 localflags.cf_flags = 0;
1921 flags = &localflags;
1922 }
1923 if (n) {
1924 flags->cf_flags |= iflags & PyCF_MASK;
1925 mod = PyAST_FromNode(n, flags, filename, arena);
1926 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 }
1928 else {
1929 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001930 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001932 err_free(&err);
1933 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934}
1935
1936mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001937PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 int start, char *ps1,
1939 char *ps2, PyCompilerFlags *flags, int *errcode,
1940 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 mod_ty mod;
1943 PyCompilerFlags localflags;
1944 perrdetail err;
1945 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
1948 &_PyParser_Grammar,
1949 start, ps1, ps2, &err, &iflags);
1950 if (flags == NULL) {
1951 localflags.cf_flags = 0;
1952 flags = &localflags;
1953 }
1954 if (n) {
1955 flags->cf_flags |= iflags & PyCF_MASK;
1956 mod = PyAST_FromNode(n, flags, filename, arena);
1957 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 }
1959 else {
1960 err_input(&err);
1961 if (errcode)
1962 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02001963 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001965 err_free(&err);
1966 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967}
1968
Guido van Rossuma110aa61994-08-29 12:50:44 +00001969/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001970
Guido van Rossuma110aa61994-08-29 12:50:44 +00001971node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001972PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 perrdetail err;
1975 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1976 &_PyParser_Grammar,
1977 start, NULL, NULL, &err, flags);
1978 if (n == NULL)
1979 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001980 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001983}
1984
Guido van Rossuma110aa61994-08-29 12:50:44 +00001985/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001986
Guido van Rossuma110aa61994-08-29 12:50:44 +00001987node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001988PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 perrdetail err;
1991 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1992 start, &err, flags);
1993 if (n == NULL)
1994 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001995 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001997}
1998
1999node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002000PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 perrdetail err;
2004 node *n = PyParser_ParseStringFlagsFilename(str, filename,
2005 &_PyParser_Grammar, start, &err, flags);
2006 if (n == NULL)
2007 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002008 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00002010}
2011
2012node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002013PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00002016}
2017
Guido van Rossum66ebd912003-04-17 16:02:26 +00002018/* May want to move a more generalized form of this to parsetok.c or
2019 even parser modules. */
2020
2021void
Victor Stinner7f2fee32011-04-05 00:39:01 +02002022PyParser_ClearError(perrdetail *err)
2023{
2024 err_free(err);
2025}
2026
2027void
Guido van Rossum66ebd912003-04-17 16:02:26 +00002028PyParser_SetError(perrdetail *err)
2029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00002031}
2032
Victor Stinner7f2fee32011-04-05 00:39:01 +02002033static void
2034err_free(perrdetail *err)
2035{
2036 Py_CLEAR(err->filename);
2037}
2038
Guido van Rossuma110aa61994-08-29 12:50:44 +00002039/* Set the error appropriate to the given input error code (see errcode.h) */
2040
2041static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002042err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00002043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 PyObject *v, *w, *errtype, *errtext;
2045 PyObject *msg_obj = NULL;
2046 char *msg = NULL;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 errtype = PyExc_SyntaxError;
2049 switch (err->error) {
2050 case E_ERROR:
2051 return;
2052 case E_SYNTAX:
2053 errtype = PyExc_IndentationError;
2054 if (err->expected == INDENT)
2055 msg = "expected an indented block";
2056 else if (err->token == INDENT)
2057 msg = "unexpected indent";
2058 else if (err->token == DEDENT)
2059 msg = "unexpected unindent";
2060 else {
2061 errtype = PyExc_SyntaxError;
2062 msg = "invalid syntax";
2063 }
2064 break;
2065 case E_TOKEN:
2066 msg = "invalid token";
2067 break;
2068 case E_EOFS:
2069 msg = "EOF while scanning triple-quoted string literal";
2070 break;
2071 case E_EOLS:
2072 msg = "EOL while scanning string literal";
2073 break;
2074 case E_INTR:
2075 if (!PyErr_Occurred())
2076 PyErr_SetNone(PyExc_KeyboardInterrupt);
2077 goto cleanup;
2078 case E_NOMEM:
2079 PyErr_NoMemory();
2080 goto cleanup;
2081 case E_EOF:
2082 msg = "unexpected EOF while parsing";
2083 break;
2084 case E_TABSPACE:
2085 errtype = PyExc_TabError;
2086 msg = "inconsistent use of tabs and spaces in indentation";
2087 break;
2088 case E_OVERFLOW:
2089 msg = "expression too long";
2090 break;
2091 case E_DEDENT:
2092 errtype = PyExc_IndentationError;
2093 msg = "unindent does not match any outer indentation level";
2094 break;
2095 case E_TOODEEP:
2096 errtype = PyExc_IndentationError;
2097 msg = "too many levels of indentation";
2098 break;
2099 case E_DECODE: {
2100 PyObject *type, *value, *tb;
2101 PyErr_Fetch(&type, &value, &tb);
2102 msg = "unknown decode error";
2103 if (value != NULL)
2104 msg_obj = PyObject_Str(value);
2105 Py_XDECREF(type);
2106 Py_XDECREF(value);
2107 Py_XDECREF(tb);
2108 break;
2109 }
2110 case E_LINECONT:
2111 msg = "unexpected character after line continuation character";
2112 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00002113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 case E_IDENTIFIER:
2115 msg = "invalid character in identifier";
2116 break;
2117 default:
2118 fprintf(stderr, "error=%d\n", err->error);
2119 msg = "unknown parsing error";
2120 break;
2121 }
2122 /* err->text may not be UTF-8 in case of decoding errors.
2123 Explicitly convert to an object. */
2124 if (!err->text) {
2125 errtext = Py_None;
2126 Py_INCREF(Py_None);
2127 } else {
2128 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2129 "replace");
2130 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002131 v = Py_BuildValue("(OiiN)", err->filename,
2132 err->lineno, err->offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 if (v != NULL) {
2134 if (msg_obj)
2135 w = Py_BuildValue("(OO)", msg_obj, v);
2136 else
2137 w = Py_BuildValue("(sO)", msg, v);
2138 } else
2139 w = NULL;
2140 Py_XDECREF(v);
2141 PyErr_SetObject(errtype, w);
2142 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002143cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 Py_XDECREF(msg_obj);
2145 if (err->text != NULL) {
2146 PyObject_FREE(err->text);
2147 err->text = NULL;
2148 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002149}
2150
2151/* Print fatal error message and abort */
2152
2153void
Tim Peters7c321a82002-07-09 02:57:01 +00002154Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002155{
Victor Stinner024e37a2011-03-31 01:31:06 +02002156 const int fd = fileno(stderr);
2157 PyThreadState *tstate;
2158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 fprintf(stderr, "Fatal Python error: %s\n", msg);
2160 fflush(stderr); /* it helps in Windows debug build */
2161 if (PyErr_Occurred()) {
Victor Stinner55a5c782010-06-08 21:00:13 +00002162 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 }
Victor Stinner024e37a2011-03-31 01:31:06 +02002164 else {
2165 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
2166 if (tstate != NULL) {
2167 fputc('\n', stderr);
2168 fflush(stderr);
Victor Stinner7bba62f2011-05-07 12:43:00 +02002169 _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
Victor Stinner024e37a2011-03-31 01:31:06 +02002170 }
Victor Stinnerd727e232011-04-01 12:13:55 +02002171 _PyFaulthandler_Fini();
Victor Stinner024e37a2011-03-31 01:31:06 +02002172 }
2173
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002174#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 {
2176 size_t len = strlen(msg);
2177 WCHAR* buffer;
2178 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 /* Convert the message to wchar_t. This uses a simple one-to-one
2181 conversion, assuming that the this error message actually uses ASCII
2182 only. If this ceases to be true, we will have to convert. */
2183 buffer = alloca( (len+1) * (sizeof *buffer));
2184 for( i=0; i<=len; ++i)
2185 buffer[i] = msg[i];
2186 OutputDebugStringW(L"Fatal Python error: ");
2187 OutputDebugStringW(buffer);
2188 OutputDebugStringW(L"\n");
2189 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002190#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002192#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002193#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002195}
2196
2197/* Clean up and exit */
2198
Guido van Rossuma110aa61994-08-29 12:50:44 +00002199#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002200#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002201#endif
2202
Collin Winter670e6922007-03-21 02:57:17 +00002203static void (*pyexitfunc)(void) = NULL;
2204/* For the atexit module. */
2205void _Py_PyAtExit(void (*func)(void))
2206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002208}
2209
2210static void
2211call_py_exitfuncs(void)
2212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 if (pyexitfunc == NULL)
2214 return;
Collin Winter670e6922007-03-21 02:57:17 +00002215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 (*pyexitfunc)();
2217 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002218}
2219
Antoine Pitrou011bd622009-10-20 21:52:47 +00002220/* Wait until threading._shutdown completes, provided
2221 the threading module was imported in the first place.
2222 The shutdown routine will wait until all non-daemon
2223 "threading" threads have completed. */
2224static void
2225wait_for_thread_shutdown(void)
2226{
2227#ifdef WITH_THREAD
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002228 _Py_IDENTIFIER(_shutdown);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 PyObject *result;
2230 PyThreadState *tstate = PyThreadState_GET();
2231 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2232 "threading");
2233 if (threading == NULL) {
2234 /* threading not imported */
2235 PyErr_Clear();
2236 return;
2237 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002238 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 if (result == NULL) {
2240 PyErr_WriteUnraisable(threading);
2241 }
2242 else {
2243 Py_DECREF(result);
2244 }
2245 Py_DECREF(threading);
Antoine Pitrou011bd622009-10-20 21:52:47 +00002246#endif
2247}
2248
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002249#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002250static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002251static int nexitfuncs = 0;
2252
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002253int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 if (nexitfuncs >= NEXITFUNCS)
2256 return -1;
2257 exitfuncs[nexitfuncs++] = func;
2258 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002259}
2260
Guido van Rossumcc283f51997-08-05 02:22:03 +00002261static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002262call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 while (nexitfuncs > 0)
2265 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 fflush(stdout);
2268 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002269}
2270
2271void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002272Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002277}
2278
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002279static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002280initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002281{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002282#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002284#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002285#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002287#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002288#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002290#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002292}
2293
Guido van Rossum7433b121997-02-14 19:45:36 +00002294
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002295/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2296 *
2297 * All of the code in this function must only use async-signal-safe functions,
2298 * listed at `man 7 signal` or
2299 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2300 */
2301void
2302_Py_RestoreSignals(void)
2303{
2304#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 PyOS_setsig(SIGPIPE, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002306#endif
2307#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 PyOS_setsig(SIGXFZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002309#endif
2310#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 PyOS_setsig(SIGXFSZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002312#endif
2313}
2314
2315
Guido van Rossum7433b121997-02-14 19:45:36 +00002316/*
2317 * The file descriptor fd is considered ``interactive'' if either
2318 * a) isatty(fd) is TRUE, or
2319 * b) the -i flag was given, and the filename associated with
2320 * the descriptor is NULL or "<stdin>" or "???".
2321 */
2322int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002323Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 if (isatty((int)fileno(fp)))
2326 return 1;
2327 if (!Py_InteractiveFlag)
2328 return 0;
2329 return (filename == NULL) ||
2330 (strcmp(filename, "<stdin>") == 0) ||
2331 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002332}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002333
2334
Tim Petersd08e3822003-04-17 15:24:21 +00002335#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002336#if defined(WIN32) && defined(_MSC_VER)
2337
2338/* Stack checking for Microsoft C */
2339
2340#include <malloc.h>
2341#include <excpt.h>
2342
Fred Drakee8de31c2000-08-31 05:38:39 +00002343/*
2344 * Return non-zero when we run out of memory on the stack; zero otherwise.
2345 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002346int
Fred Drake399739f2000-08-31 05:52:44 +00002347PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 __try {
2350 /* alloca throws a stack overflow exception if there's
2351 not enough space left on the stack */
2352 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2353 return 0;
2354 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2355 EXCEPTION_EXECUTE_HANDLER :
2356 EXCEPTION_CONTINUE_SEARCH) {
2357 int errcode = _resetstkoflw();
2358 if (errcode == 0)
2359 {
2360 Py_FatalError("Could not reset the stack!");
2361 }
2362 }
2363 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002364}
2365
2366#endif /* WIN32 && _MSC_VER */
2367
2368/* Alternate implementations can be added here... */
2369
2370#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002371
2372
2373/* Wrappers around sigaction() or signal(). */
2374
2375PyOS_sighandler_t
2376PyOS_getsig(int sig)
2377{
2378#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 struct sigaction context;
2380 if (sigaction(sig, NULL, &context) == -1)
2381 return SIG_ERR;
2382 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002383#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002385/* Special signal handling for the secure CRT in Visual Studio 2005 */
2386#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 switch (sig) {
2388 /* Only these signals are valid */
2389 case SIGINT:
2390 case SIGILL:
2391 case SIGFPE:
2392 case SIGSEGV:
2393 case SIGTERM:
2394 case SIGBREAK:
2395 case SIGABRT:
2396 break;
2397 /* Don't call signal() with other values or it will assert */
2398 default:
2399 return SIG_ERR;
2400 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002401#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 handler = signal(sig, SIG_IGN);
2403 if (handler != SIG_ERR)
2404 signal(sig, handler);
2405 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002406#endif
2407}
2408
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002409/*
2410 * All of the code in this function must only use async-signal-safe functions,
2411 * listed at `man 7 signal` or
2412 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2413 */
Guido van Rossum6f256182000-09-16 16:32:19 +00002414PyOS_sighandler_t
2415PyOS_setsig(int sig, PyOS_sighandler_t handler)
2416{
2417#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 /* Some code in Modules/signalmodule.c depends on sigaction() being
2419 * used here if HAVE_SIGACTION is defined. Fix that if this code
2420 * changes to invalidate that assumption.
2421 */
2422 struct sigaction context, ocontext;
2423 context.sa_handler = handler;
2424 sigemptyset(&context.sa_mask);
2425 context.sa_flags = 0;
2426 if (sigaction(sig, &context, &ocontext) == -1)
2427 return SIG_ERR;
2428 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002429#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 PyOS_sighandler_t oldhandler;
2431 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002432#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002434#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002436#endif
2437}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438
2439/* Deprecated C API functions still provided for binary compatiblity */
2440
2441#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002442PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002443PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446}
2447
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002448#undef PyParser_SimpleParseString
2449PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450PyParser_SimpleParseString(const char *str, int start)
2451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002454
2455#undef PyRun_AnyFile
2456PyAPI_FUNC(int)
2457PyRun_AnyFile(FILE *fp, const char *name)
2458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002460}
2461
2462#undef PyRun_AnyFileEx
2463PyAPI_FUNC(int)
2464PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002467}
2468
2469#undef PyRun_AnyFileFlags
2470PyAPI_FUNC(int)
2471PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002474}
2475
2476#undef PyRun_File
2477PyAPI_FUNC(PyObject *)
2478PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002481}
2482
2483#undef PyRun_FileEx
2484PyAPI_FUNC(PyObject *)
2485PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002488}
2489
2490#undef PyRun_FileFlags
2491PyAPI_FUNC(PyObject *)
2492PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002496}
2497
2498#undef PyRun_SimpleFile
2499PyAPI_FUNC(int)
2500PyRun_SimpleFile(FILE *f, const char *p)
2501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002503}
2504
2505#undef PyRun_SimpleFileEx
2506PyAPI_FUNC(int)
2507PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002510}
2511
2512
2513#undef PyRun_String
2514PyAPI_FUNC(PyObject *)
2515PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002518}
2519
2520#undef PyRun_SimpleString
2521PyAPI_FUNC(int)
2522PyRun_SimpleString(const char *s)
2523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002525}
2526
2527#undef Py_CompileString
2528PyAPI_FUNC(PyObject *)
2529Py_CompileString(const char *str, const char *p, int s)
2530{
Georg Brandl8334fd92010-12-04 10:26:46 +00002531 return Py_CompileStringExFlags(str, p, s, NULL, -1);
2532}
2533
2534#undef Py_CompileStringFlags
2535PyAPI_FUNC(PyObject *)
2536Py_CompileStringFlags(const char *str, const char *p, int s,
2537 PyCompilerFlags *flags)
2538{
2539 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002540}
2541
2542#undef PyRun_InteractiveOne
2543PyAPI_FUNC(int)
2544PyRun_InteractiveOne(FILE *f, const char *p)
2545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002547}
2548
2549#undef PyRun_InteractiveLoop
2550PyAPI_FUNC(int)
2551PyRun_InteractiveLoop(FILE *f, const char *p)
2552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002554}
2555
2556#ifdef __cplusplus
2557}
2558#endif