blob: 0f2f0501cde78af2cad81c3333123fed784c3ccb [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();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 /* Cleanup Unicode implementation */
536 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 /* reset file system default encoding */
Victor Stinnerb744ba12010-05-15 12:27:16 +0000539 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 free((char*)Py_FileSystemDefaultEncoding);
541 Py_FileSystemDefaultEncoding = NULL;
542 }
Christian Heimesc8967002007-11-30 10:18:26 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 /* XXX Still allocated:
545 - various static ad-hoc pointers to interned strings
546 - int and float free list blocks
547 - whatever various modules and libraries allocate
548 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000551
Tim Peters269b2a62003-04-17 19:52:29 +0000552#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 /* Display addresses (& refcnts) of all objects still alive.
554 * An address can be used to find the repr of the object, printed
555 * above by _Py_PrintReferences.
556 */
557 if (Py_GETENV("PYTHONDUMPREFS"))
558 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000559#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000560#ifdef PYMALLOC_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 if (Py_GETENV("PYTHONMALLOCSTATS"))
562 _PyObject_DebugMallocStats();
Tim Peters0e871182002-04-13 08:29:14 +0000563#endif
564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000566}
567
568/* Create and initialize a new interpreter and thread, and return the
569 new thread. This requires that Py_Initialize() has been called
570 first.
571
572 Unsuccessful initialization yields a NULL pointer. Note that *no*
573 exception information is available even in this case -- the
574 exception information is held in the thread, and there is no
575 thread.
576
577 Locking: as above.
578
579*/
580
581PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000582Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 PyInterpreterState *interp;
585 PyThreadState *tstate, *save_tstate;
586 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 if (!initialized)
589 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 interp = PyInterpreterState_New();
592 if (interp == NULL)
593 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 tstate = PyThreadState_New(interp);
596 if (tstate == NULL) {
597 PyInterpreterState_Delete(interp);
598 return NULL;
599 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 interp->modules = PyDict_New();
606 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000607
Victor Stinner49d3f252010-10-17 01:24:53 +0000608 bimod = _PyImport_FindBuiltin("builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 if (bimod != NULL) {
610 interp->builtins = PyModule_GetDict(bimod);
611 if (interp->builtins == NULL)
612 goto handle_error;
613 Py_INCREF(interp->builtins);
614 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 /* initialize builtin exceptions */
617 _PyExc_Init();
Christian Heimes6a27efa2008-10-30 21:48:26 +0000618
Victor Stinner49d3f252010-10-17 01:24:53 +0000619 sysmod = _PyImport_FindBuiltin("sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 if (bimod != NULL && sysmod != NULL) {
621 PyObject *pstderr;
622 interp->sysdict = PyModule_GetDict(sysmod);
623 if (interp->sysdict == NULL)
624 goto handle_error;
625 Py_INCREF(interp->sysdict);
626 PySys_SetPath(Py_GetPath());
627 PyDict_SetItemString(interp->sysdict, "modules",
628 interp->modules);
629 /* Set up a preliminary stderr printer until we have enough
630 infrastructure for the io module in place. */
631 pstderr = PyFile_NewStdPrinter(fileno(stderr));
632 if (pstderr == NULL)
633 Py_FatalError("Py_Initialize: can't set preliminary stderr");
634 PySys_SetObject("stderr", pstderr);
635 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000636 Py_DECREF(pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 _PyImportHooks_Init();
Victor Stinner793b5312011-04-27 00:24:21 +0200639
640 if (initfsencoding(interp) < 0)
641 goto handle_error;
642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 if (initstdio() < 0)
644 Py_FatalError(
645 "Py_Initialize: can't initialize sys standard streams");
646 initmain();
647 if (!Py_NoSiteFlag)
648 initsite();
649 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 if (!PyErr_Occurred())
652 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000653
Thomas Wouters89f507f2006-12-13 04:49:30 +0000654handle_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000656
Victor Stinnerc40a3502011-04-27 00:20:27 +0200657 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 PyThreadState_Clear(tstate);
659 PyThreadState_Swap(save_tstate);
660 PyThreadState_Delete(tstate);
661 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000664}
665
666/* Delete an interpreter and its last thread. This requires that the
667 given thread state is current, that the thread has no remaining
668 frames, and that it is its interpreter's only remaining thread.
669 It is a fatal error to violate these constraints.
670
671 (Py_Finalize() doesn't have these constraints -- it zaps
672 everything, regardless.)
673
674 Locking: as above.
675
676*/
677
678void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000679Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 if (tstate != PyThreadState_GET())
684 Py_FatalError("Py_EndInterpreter: thread is not current");
685 if (tstate->frame != NULL)
686 Py_FatalError("Py_EndInterpreter: thread still has a frame");
687 if (tstate != interp->tstate_head || tstate->next != NULL)
688 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 PyImport_Cleanup();
691 PyInterpreterState_Clear(interp);
692 PyThreadState_Swap(NULL);
693 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000694}
695
Martin v. Löwis790465f2008-04-05 20:41:37 +0000696static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000697
698void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000699Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 if (pn && *pn)
702 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000703}
704
Martin v. Löwis790465f2008-04-05 20:41:37 +0000705wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000706Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000709}
710
Martin v. Löwis790465f2008-04-05 20:41:37 +0000711static wchar_t *default_home = NULL;
712static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000713
714void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000715Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000718}
719
Martin v. Löwis790465f2008-04-05 20:41:37 +0000720wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000721Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 wchar_t *home = default_home;
724 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
725 char* chome = Py_GETENV("PYTHONHOME");
726 if (chome) {
727 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
728 if (r != (size_t)-1 && r <= PATH_MAX)
729 home = env_home;
730 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 }
733 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000734}
735
Guido van Rossum6135a871995-01-09 17:53:26 +0000736/* Create __main__ module */
737
738static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000739initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 PyObject *m, *d;
742 m = PyImport_AddModule("__main__");
743 if (m == NULL)
744 Py_FatalError("can't create __main__ module");
745 d = PyModule_GetDict(m);
746 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
747 PyObject *bimod = PyImport_ImportModule("builtins");
748 if (bimod == NULL ||
749 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
750 Py_FatalError("can't add __builtins__ to __main__");
751 Py_DECREF(bimod);
752 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000753}
754
Victor Stinner793b5312011-04-27 00:24:21 +0200755static int
756initfsencoding(PyInterpreterState *interp)
Victor Stinnerb744ba12010-05-15 12:27:16 +0000757{
758 PyObject *codec;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000759
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200760 if (Py_FileSystemDefaultEncoding == NULL)
761 {
762 Py_FileSystemDefaultEncoding = get_locale_encoding();
763 if (Py_FileSystemDefaultEncoding == NULL)
Victor Stinnere4743092010-10-19 00:05:51 +0000764 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
Victor Stinnerb744ba12010-05-15 12:27:16 +0000765
Victor Stinnere4743092010-10-19 00:05:51 +0000766 Py_HasFileSystemDefaultEncoding = 0;
Victor Stinner793b5312011-04-27 00:24:21 +0200767 interp->fscodec_initialized = 1;
768 return 0;
Victor Stinner7f84ab52010-06-11 00:36:33 +0000769 }
Victor Stinnerb744ba12010-05-15 12:27:16 +0000770
771 /* the encoding is mbcs, utf-8 or ascii */
772 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
773 if (!codec) {
774 /* Such error can only occurs in critical situations: no more
775 * memory, import a module of the standard library failed,
776 * etc. */
Victor Stinner793b5312011-04-27 00:24:21 +0200777 return -1;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000778 }
Victor Stinner793b5312011-04-27 00:24:21 +0200779 Py_DECREF(codec);
780 interp->fscodec_initialized = 1;
781 return 0;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000782}
783
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000784/* Import the site module (not into __main__ though) */
785
786static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000787initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 PyObject *m;
790 m = PyImport_ImportModule("site");
791 if (m == NULL) {
792 PyErr_Print();
793 Py_Finalize();
794 exit(1);
795 }
796 else {
797 Py_DECREF(m);
798 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000799}
800
Antoine Pitrou05608432009-01-09 18:53:14 +0000801static PyObject*
802create_stdio(PyObject* io,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 int fd, int write_mode, char* name,
804 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +0000805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
807 const char* mode;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000808 const char* newline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 PyObject *line_buffering;
810 int buffering, isatty;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200811 _Py_IDENTIFIER(open);
812 _Py_IDENTIFIER(isatty);
813 _Py_IDENTIFIER(TextIOWrapper);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200814 _Py_IDENTIFIER(name);
815 _Py_IDENTIFIER(mode);
Antoine Pitrou05608432009-01-09 18:53:14 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 /* stdin is always opened in buffered mode, first because it shouldn't
818 make a difference in common use cases, second because TextIOWrapper
819 depends on the presence of a read1() method which only exists on
820 buffered streams.
821 */
822 if (Py_UnbufferedStdioFlag && write_mode)
823 buffering = 0;
824 else
825 buffering = -1;
826 if (write_mode)
827 mode = "wb";
828 else
829 mode = "rb";
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200830 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
831 fd, mode, buffering,
832 Py_None, Py_None, Py_None, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 if (buf == NULL)
834 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 if (buffering) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200837 _Py_IDENTIFIER(raw);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200838 raw = _PyObject_GetAttrId(buf, &PyId_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 if (raw == NULL)
840 goto error;
841 }
842 else {
843 raw = buf;
844 Py_INCREF(raw);
845 }
Antoine Pitrou05608432009-01-09 18:53:14 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 text = PyUnicode_FromString(name);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200848 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 goto error;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200850 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 if (res == NULL)
852 goto error;
853 isatty = PyObject_IsTrue(res);
854 Py_DECREF(res);
855 if (isatty == -1)
856 goto error;
857 if (isatty || Py_UnbufferedStdioFlag)
858 line_buffering = Py_True;
859 else
860 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 Py_CLEAR(raw);
863 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +0000864
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000865 newline = "\n";
866#ifdef MS_WINDOWS
867 if (!write_mode) {
868 /* translate \r\n to \n for sys.stdin on Windows */
869 newline = NULL;
870 }
871#endif
872
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200873 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
874 buf, encoding, errors,
875 newline, line_buffering);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 Py_CLEAR(buf);
877 if (stream == NULL)
878 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 if (write_mode)
881 mode = "w";
882 else
883 mode = "r";
884 text = PyUnicode_FromString(mode);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200885 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 goto error;
887 Py_CLEAR(text);
888 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +0000889
890error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 Py_XDECREF(buf);
892 Py_XDECREF(stream);
893 Py_XDECREF(text);
894 Py_XDECREF(raw);
895 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +0000896}
897
Georg Brandl1a3284e2007-12-02 09:40:06 +0000898/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000899static int
900initstdio(void)
901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 PyObject *iomod = NULL, *wrapper;
903 PyObject *bimod = NULL;
904 PyObject *m;
905 PyObject *std = NULL;
906 int status = 0, fd;
907 PyObject * encoding_attr;
908 char *encoding = NULL, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 /* Hack to avoid a nasty recursion issue when Python is invoked
911 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
912 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
913 goto error;
914 }
915 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
918 goto error;
919 }
920 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 if (!(bimod = PyImport_ImportModule("builtins"))) {
923 goto error;
924 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 if (!(iomod = PyImport_ImportModule("io"))) {
927 goto error;
928 }
929 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
930 goto error;
931 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 /* Set builtins.open */
934 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000935 Py_DECREF(wrapper);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 goto error;
937 }
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000938 Py_DECREF(wrapper);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 encoding = Py_GETENV("PYTHONIOENCODING");
941 errors = NULL;
942 if (encoding) {
943 encoding = strdup(encoding);
944 errors = strchr(encoding, ':');
945 if (errors) {
946 *errors = '\0';
947 errors++;
948 }
949 }
Martin v. Löwis0f599892008-06-02 11:13:03 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 /* Set sys.stdin */
952 fd = fileno(stdin);
953 /* Under some conditions stdin, stdout and stderr may not be connected
954 * and fileno() may point to an invalid file descriptor. For example
955 * GUI apps don't have valid standard streams by default.
956 */
957 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000958#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 std = Py_None;
960 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000961#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000963#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 }
965 else {
966 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
967 if (std == NULL)
968 goto error;
969 } /* if (fd < 0) */
970 PySys_SetObject("__stdin__", std);
971 PySys_SetObject("stdin", std);
972 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 /* Set sys.stdout */
975 fd = fileno(stdout);
976 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000977#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 std = Py_None;
979 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000980#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000982#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 }
984 else {
985 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
986 if (std == NULL)
987 goto error;
988 } /* if (fd < 0) */
989 PySys_SetObject("__stdout__", std);
990 PySys_SetObject("stdout", std);
991 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000992
Guido van Rossum98297ee2007-11-06 21:34:58 +0000993#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 /* Set sys.stderr, replaces the preliminary stderr */
995 fd = fileno(stderr);
996 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000997#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 std = Py_None;
999 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +00001000#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +00001002#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 }
1004 else {
1005 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1006 if (std == NULL)
1007 goto error;
1008 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 /* Same as hack above, pre-import stderr's codec to avoid recursion
1011 when import.c tries to write to stderr in verbose mode. */
1012 encoding_attr = PyObject_GetAttrString(std, "encoding");
1013 if (encoding_attr != NULL) {
1014 const char * encoding;
1015 encoding = _PyUnicode_AsString(encoding_attr);
1016 if (encoding != NULL) {
1017 _PyCodec_Lookup(encoding);
1018 }
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +00001019 Py_DECREF(encoding_attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 }
1021 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PySys_SetObject("__stderr__", std);
1024 PySys_SetObject("stderr", std);
1025 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001026#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001029 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 status = -1;
1031 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 if (encoding)
1034 free(encoding);
1035 Py_XDECREF(bimod);
1036 Py_XDECREF(iomod);
1037 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001038}
1039
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001040/* Parse input from a file and execute it */
1041
1042int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001043PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 if (filename == NULL)
1047 filename = "???";
1048 if (Py_FdIsInteractive(fp, filename)) {
1049 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
1050 if (closeit)
1051 fclose(fp);
1052 return err;
1053 }
1054 else
1055 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001056}
1057
1058int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001059PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 PyObject *v;
1062 int ret;
1063 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 if (flags == NULL) {
1066 flags = &local_flags;
1067 local_flags.cf_flags = 0;
1068 }
1069 v = PySys_GetObject("ps1");
1070 if (v == NULL) {
1071 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
1072 Py_XDECREF(v);
1073 }
1074 v = PySys_GetObject("ps2");
1075 if (v == NULL) {
1076 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1077 Py_XDECREF(v);
1078 }
1079 for (;;) {
1080 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
1081 PRINT_TOTAL_REFS();
1082 if (ret == E_EOF)
1083 return 0;
1084 /*
1085 if (ret == E_NOMEM)
1086 return -1;
1087 */
1088 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001089}
1090
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001091/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001092static int PARSER_FLAGS(PyCompilerFlags *flags)
1093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 int parser_flags = 0;
1095 if (!flags)
1096 return 0;
1097 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1098 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1099 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1100 parser_flags |= PyPARSE_IGNORE_COOKIE;
1101 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1102 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1103 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001104}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001105
Thomas Wouters89f507f2006-12-13 04:49:30 +00001106#if 0
1107/* Keep an example of flags with future keyword support. */
1108#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1110 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1111 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1112 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001113#endif
1114
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001115int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001116PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 PyObject *m, *d, *v, *w, *oenc = NULL;
1119 mod_ty mod;
1120 PyArena *arena;
1121 char *ps1 = "", *ps2 = "", *enc = NULL;
1122 int errcode = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001123 _Py_IDENTIFIER(encoding);
Tim Petersfe2127d2001-07-16 05:37:24 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 if (fp == stdin) {
1126 /* Fetch encoding from sys.stdin */
1127 v = PySys_GetObject("stdin");
1128 if (v == NULL || v == Py_None)
1129 return -1;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001130 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (!oenc)
1132 return -1;
1133 enc = _PyUnicode_AsString(oenc);
Victor Stinner386fe712010-05-19 00:34:15 +00001134 if (enc == NULL)
1135 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 }
1137 v = PySys_GetObject("ps1");
1138 if (v != NULL) {
1139 v = PyObject_Str(v);
1140 if (v == NULL)
1141 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001142 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +00001144 if (ps1 == NULL) {
1145 PyErr_Clear();
1146 ps1 = "";
1147 }
1148 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 }
1150 w = PySys_GetObject("ps2");
1151 if (w != NULL) {
1152 w = PyObject_Str(w);
1153 if (w == NULL)
1154 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001155 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +00001157 if (ps2 == NULL) {
1158 PyErr_Clear();
1159 ps2 = "";
1160 }
1161 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 }
1163 arena = PyArena_New();
1164 if (arena == NULL) {
1165 Py_XDECREF(v);
1166 Py_XDECREF(w);
1167 Py_XDECREF(oenc);
1168 return -1;
1169 }
1170 mod = PyParser_ASTFromFile(fp, filename, enc,
1171 Py_single_input, ps1, ps2,
1172 flags, &errcode, arena);
1173 Py_XDECREF(v);
1174 Py_XDECREF(w);
1175 Py_XDECREF(oenc);
1176 if (mod == NULL) {
1177 PyArena_Free(arena);
1178 if (errcode == E_EOF) {
1179 PyErr_Clear();
1180 return E_EOF;
1181 }
1182 PyErr_Print();
1183 return -1;
1184 }
1185 m = PyImport_AddModule("__main__");
1186 if (m == NULL) {
1187 PyArena_Free(arena);
1188 return -1;
1189 }
1190 d = PyModule_GetDict(m);
1191 v = run_mod(mod, filename, d, d, flags, arena);
1192 PyArena_Free(arena);
1193 flush_io();
1194 if (v == NULL) {
1195 PyErr_Print();
1196 return -1;
1197 }
1198 Py_DECREF(v);
1199 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001200}
1201
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001202/* Check whether a file maybe a pyc file: Look at the extension,
1203 the file type, and, if we may close it, at the first few bytes. */
1204
1205static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001206maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1209 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 /* Only look into the file if we are allowed to close it, since
1212 it then should also be seekable. */
1213 if (closeit) {
1214 /* Read only two bytes of the magic. If the file was opened in
1215 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1216 be read as they are on disk. */
1217 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1218 unsigned char buf[2];
1219 /* Mess: In case of -x, the stream is NOT at its start now,
1220 and ungetc() was used to push back the first newline,
1221 which makes the current stream position formally undefined,
1222 and a x-platform nightmare.
1223 Unfortunately, we have no direct way to know whether -x
1224 was specified. So we use a terrible hack: if the current
1225 stream position is not 0, we assume -x was specified, and
1226 give up. Bug 132850 on SourceForge spells out the
1227 hopelessness of trying anything else (fseek and ftell
1228 don't work predictably x-platform for text-mode files).
1229 */
1230 int ispyc = 0;
1231 if (ftell(fp) == 0) {
1232 if (fread(buf, 1, 2, fp) == 2 &&
1233 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1234 ispyc = 1;
1235 rewind(fp);
1236 }
1237 return ispyc;
1238 }
1239 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001240}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001241
Guido van Rossum0df002c2000-08-27 19:21:52 +00001242int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001243PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 PyObject *m, *d, *v;
1247 const char *ext;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001248 int set_file_name = 0, ret;
1249 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 m = PyImport_AddModule("__main__");
1252 if (m == NULL)
1253 return -1;
1254 d = PyModule_GetDict(m);
1255 if (PyDict_GetItemString(d, "__file__") == NULL) {
1256 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001257 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 if (f == NULL)
1259 return -1;
1260 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1261 Py_DECREF(f);
1262 return -1;
1263 }
Barry Warsaw916048d2011-09-20 14:45:44 -04001264 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1265 Py_DECREF(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 return -1;
Barry Warsaw916048d2011-09-20 14:45:44 -04001267 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 set_file_name = 1;
1269 Py_DECREF(f);
1270 }
1271 len = strlen(filename);
1272 ext = filename + len - (len > 4 ? 4 : 0);
1273 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1274 /* Try to run a pyc file. First, re-open in binary */
1275 if (closeit)
1276 fclose(fp);
1277 if ((fp = fopen(filename, "rb")) == NULL) {
1278 fprintf(stderr, "python: Can't reopen .pyc file\n");
1279 ret = -1;
1280 goto done;
1281 }
1282 /* Turn on optimization if a .pyo file is given */
1283 if (strcmp(ext, ".pyo") == 0)
1284 Py_OptimizeFlag = 1;
1285 v = run_pyc_file(fp, filename, d, d, flags);
1286 } else {
1287 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1288 closeit, flags);
1289 }
1290 flush_io();
1291 if (v == NULL) {
1292 PyErr_Print();
1293 ret = -1;
1294 goto done;
1295 }
1296 Py_DECREF(v);
1297 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001298 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1300 PyErr_Clear();
1301 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001302}
1303
1304int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001305PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 PyObject *m, *d, *v;
1308 m = PyImport_AddModule("__main__");
1309 if (m == NULL)
1310 return -1;
1311 d = PyModule_GetDict(m);
1312 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1313 if (v == NULL) {
1314 PyErr_Print();
1315 return -1;
1316 }
1317 Py_DECREF(v);
1318 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001319}
1320
Barry Warsaw035574d1997-08-29 22:07:17 +00001321static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001322parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 long hold;
1326 PyObject *v;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001327 _Py_IDENTIFIER(msg);
1328 _Py_IDENTIFIER(filename);
1329 _Py_IDENTIFIER(lineno);
1330 _Py_IDENTIFIER(offset);
1331 _Py_IDENTIFIER(text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 /* old style errors */
1334 if (PyTuple_Check(err))
1335 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1336 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 /* new style errors. `err' is an instance */
Barry Warsaw035574d1997-08-29 22:07:17 +00001339
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001340 if (! (v = _PyObject_GetAttrId(err, &PyId_msg)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 goto finally;
1342 *message = v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001343
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001344 if (!(v = _PyObject_GetAttrId(err, &PyId_filename)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 goto finally;
1346 if (v == Py_None)
1347 *filename = NULL;
1348 else if (! (*filename = _PyUnicode_AsString(v)))
1349 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 Py_DECREF(v);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001352 if (!(v = _PyObject_GetAttrId(err, &PyId_lineno)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 goto finally;
1354 hold = PyLong_AsLong(v);
1355 Py_DECREF(v);
1356 v = NULL;
1357 if (hold < 0 && PyErr_Occurred())
1358 goto finally;
1359 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001360
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001361 if (!(v = _PyObject_GetAttrId(err, &PyId_offset)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 goto finally;
1363 if (v == Py_None) {
1364 *offset = -1;
1365 Py_DECREF(v);
1366 v = NULL;
1367 } else {
1368 hold = PyLong_AsLong(v);
1369 Py_DECREF(v);
1370 v = NULL;
1371 if (hold < 0 && PyErr_Occurred())
1372 goto finally;
1373 *offset = (int)hold;
1374 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001375
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001376 if (!(v = _PyObject_GetAttrId(err, &PyId_text)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 goto finally;
1378 if (v == Py_None)
1379 *text = NULL;
1380 else if (!PyUnicode_Check(v) ||
1381 !(*text = _PyUnicode_AsString(v)))
1382 goto finally;
1383 Py_DECREF(v);
1384 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001385
1386finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 Py_XDECREF(v);
1388 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001389}
1390
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001391void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001392PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001395}
1396
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001397static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001398print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 char *nl;
1401 if (offset >= 0) {
Benjamin Petersona95e9772010-10-29 03:28:14 +00001402 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1403 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 for (;;) {
1405 nl = strchr(text, '\n');
1406 if (nl == NULL || nl-text >= offset)
1407 break;
1408 offset -= (int)(nl+1-text);
1409 text = nl+1;
1410 }
1411 while (*text == ' ' || *text == '\t') {
1412 text++;
1413 offset--;
1414 }
1415 }
1416 PyFile_WriteString(" ", f);
1417 PyFile_WriteString(text, f);
1418 if (*text == '\0' || text[strlen(text)-1] != '\n')
1419 PyFile_WriteString("\n", f);
1420 if (offset == -1)
1421 return;
1422 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +00001423 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001426}
1427
Guido van Rossum66e8e862001-03-23 17:54:43 +00001428static void
1429handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 PyObject *exception, *value, *tb;
1432 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 if (Py_InspectFlag)
1435 /* Don't exit if -i flag was given. This flag is set to 0
1436 * when entering interactive mode for inspecting. */
1437 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 PyErr_Fetch(&exception, &value, &tb);
1440 fflush(stdout);
1441 if (value == NULL || value == Py_None)
1442 goto done;
1443 if (PyExceptionInstance_Check(value)) {
1444 /* The error code should be in the `code' attribute. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001445 _Py_IDENTIFIER(code);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001446 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 if (code) {
1448 Py_DECREF(value);
1449 value = code;
1450 if (value == Py_None)
1451 goto done;
1452 }
1453 /* If we failed to dig out the 'code' attribute,
1454 just let the else clause below print the error. */
1455 }
1456 if (PyLong_Check(value))
1457 exitcode = (int)PyLong_AsLong(value);
1458 else {
Victor Stinnere9fb3192010-05-17 08:58:51 +00001459 PyObject *sys_stderr = PySys_GetObject("stderr");
Victor Stinner7126dbc2010-05-21 23:45:42 +00001460 if (sys_stderr != NULL && sys_stderr != Py_None) {
1461 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1462 } else {
1463 PyObject_Print(value, stderr, Py_PRINT_RAW);
1464 fflush(stderr);
1465 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 PySys_WriteStderr("\n");
1467 exitcode = 1;
1468 }
Tim Peterscf615b52003-04-19 18:47:02 +00001469 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 /* Restore and clear the exception info, in order to properly decref
1471 * the exception, value, and traceback. If we just exit instead,
1472 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1473 * some finalizers from running.
1474 */
1475 PyErr_Restore(exception, value, tb);
1476 PyErr_Clear();
1477 Py_Exit(exitcode);
1478 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001479}
1480
1481void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001482PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1487 handle_system_exit();
1488 }
1489 PyErr_Fetch(&exception, &v, &tb);
1490 if (exception == NULL)
1491 return;
1492 PyErr_NormalizeException(&exception, &v, &tb);
1493 if (tb == NULL) {
1494 tb = Py_None;
1495 Py_INCREF(tb);
1496 }
1497 PyException_SetTraceback(v, tb);
1498 if (exception == NULL)
1499 return;
1500 /* Now we know v != NULL too */
1501 if (set_sys_last_vars) {
1502 PySys_SetObject("last_type", exception);
1503 PySys_SetObject("last_value", v);
1504 PySys_SetObject("last_traceback", tb);
1505 }
1506 hook = PySys_GetObject("excepthook");
1507 if (hook) {
1508 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1509 PyObject *result = PyEval_CallObject(hook, args);
1510 if (result == NULL) {
1511 PyObject *exception2, *v2, *tb2;
1512 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1513 handle_system_exit();
1514 }
1515 PyErr_Fetch(&exception2, &v2, &tb2);
1516 PyErr_NormalizeException(&exception2, &v2, &tb2);
1517 /* It should not be possible for exception2 or v2
1518 to be NULL. However PyErr_Display() can't
1519 tolerate NULLs, so just be safe. */
1520 if (exception2 == NULL) {
1521 exception2 = Py_None;
1522 Py_INCREF(exception2);
1523 }
1524 if (v2 == NULL) {
1525 v2 = Py_None;
1526 Py_INCREF(v2);
1527 }
1528 fflush(stdout);
1529 PySys_WriteStderr("Error in sys.excepthook:\n");
1530 PyErr_Display(exception2, v2, tb2);
1531 PySys_WriteStderr("\nOriginal exception was:\n");
1532 PyErr_Display(exception, v, tb);
1533 Py_DECREF(exception2);
1534 Py_DECREF(v2);
1535 Py_XDECREF(tb2);
1536 }
1537 Py_XDECREF(result);
1538 Py_XDECREF(args);
1539 } else {
1540 PySys_WriteStderr("sys.excepthook is missing\n");
1541 PyErr_Display(exception, v, tb);
1542 }
1543 Py_XDECREF(exception);
1544 Py_XDECREF(v);
1545 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001546}
1547
Benjamin Petersone6528212008-07-15 15:32:09 +00001548static void
1549print_exception(PyObject *f, PyObject *value)
1550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 int err = 0;
1552 PyObject *type, *tb;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001553 _Py_IDENTIFIER(print_file_and_line);
Benjamin Petersone6528212008-07-15 15:32:09 +00001554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 if (!PyExceptionInstance_Check(value)) {
1556 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1557 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1558 PyFile_WriteString(" found\n", f);
1559 return;
1560 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 Py_INCREF(value);
1563 fflush(stdout);
1564 type = (PyObject *) Py_TYPE(value);
1565 tb = PyException_GetTraceback(value);
1566 if (tb && tb != Py_None)
1567 err = PyTraceBack_Print(tb, f);
1568 if (err == 0 &&
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001569 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 {
1571 PyObject *message;
1572 const char *filename, *text;
1573 int lineno, offset;
1574 if (!parse_syntax_error(value, &message, &filename,
1575 &lineno, &offset, &text))
1576 PyErr_Clear();
1577 else {
1578 char buf[10];
1579 PyFile_WriteString(" File \"", f);
1580 if (filename == NULL)
1581 PyFile_WriteString("<string>", f);
1582 else
1583 PyFile_WriteString(filename, f);
1584 PyFile_WriteString("\", line ", f);
1585 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1586 PyFile_WriteString(buf, f);
1587 PyFile_WriteString("\n", f);
1588 if (text != NULL)
1589 print_error_text(f, offset, text);
1590 Py_DECREF(value);
1591 value = message;
1592 /* Can't be bothered to check all those
1593 PyFile_WriteString() calls */
1594 if (PyErr_Occurred())
1595 err = -1;
1596 }
1597 }
1598 if (err) {
1599 /* Don't do anything else */
1600 }
1601 else {
1602 PyObject* moduleName;
1603 char* className;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001604 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 assert(PyExceptionClass_Check(type));
1606 className = PyExceptionClass_Name(type);
1607 if (className != NULL) {
1608 char *dot = strrchr(className, '.');
1609 if (dot != NULL)
1610 className = dot+1;
1611 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001612
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001613 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1615 {
Victor Stinner13b21bd2011-05-26 14:25:13 +02001616 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 err = PyFile_WriteString("<unknown>", f);
1618 }
1619 else {
1620 char* modstr = _PyUnicode_AsString(moduleName);
1621 if (modstr && strcmp(modstr, "builtins"))
1622 {
1623 err = PyFile_WriteString(modstr, f);
1624 err += PyFile_WriteString(".", f);
1625 }
1626 Py_DECREF(moduleName);
1627 }
1628 if (err == 0) {
1629 if (className == NULL)
1630 err = PyFile_WriteString("<unknown>", f);
1631 else
1632 err = PyFile_WriteString(className, f);
1633 }
1634 }
1635 if (err == 0 && (value != Py_None)) {
1636 PyObject *s = PyObject_Str(value);
1637 /* only print colon if the str() of the
1638 object is not the empty string
1639 */
1640 if (s == NULL)
1641 err = -1;
1642 else if (!PyUnicode_Check(s) ||
1643 PyUnicode_GetSize(s) != 0)
1644 err = PyFile_WriteString(": ", f);
1645 if (err == 0)
1646 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1647 Py_XDECREF(s);
1648 }
1649 /* try to write a newline in any case */
1650 err += PyFile_WriteString("\n", f);
1651 Py_XDECREF(tb);
1652 Py_DECREF(value);
1653 /* If an error happened here, don't show it.
1654 XXX This is wrong, but too many callers rely on this behavior. */
1655 if (err != 0)
1656 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001657}
1658
1659static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 "\nThe above exception was the direct cause "
1661 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001662
1663static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 "\nDuring handling of the above exception, "
1665 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001666
1667static void
1668print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 int err = 0, res;
1671 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 if (seen != NULL) {
1674 /* Exception chaining */
1675 if (PySet_Add(seen, value) == -1)
1676 PyErr_Clear();
1677 else if (PyExceptionInstance_Check(value)) {
1678 cause = PyException_GetCause(value);
1679 context = PyException_GetContext(value);
1680 if (cause) {
1681 res = PySet_Contains(seen, cause);
1682 if (res == -1)
1683 PyErr_Clear();
1684 if (res == 0) {
1685 print_exception_recursive(
1686 f, cause, seen);
1687 err |= PyFile_WriteString(
1688 cause_message, f);
1689 }
1690 }
1691 else if (context) {
1692 res = PySet_Contains(seen, context);
1693 if (res == -1)
1694 PyErr_Clear();
1695 if (res == 0) {
1696 print_exception_recursive(
1697 f, context, seen);
1698 err |= PyFile_WriteString(
1699 context_message, f);
1700 }
1701 }
1702 Py_XDECREF(context);
1703 Py_XDECREF(cause);
1704 }
1705 }
1706 print_exception(f, value);
1707 if (err != 0)
1708 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001709}
1710
Thomas Wouters477c8d52006-05-27 19:21:47 +00001711void
1712PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 PyObject *seen;
1715 PyObject *f = PySys_GetObject("stderr");
1716 if (f == Py_None) {
1717 /* pass */
1718 }
1719 else if (f == NULL) {
1720 _PyObject_Dump(value);
1721 fprintf(stderr, "lost sys.stderr\n");
1722 }
1723 else {
1724 /* We choose to ignore seen being possibly NULL, and report
1725 at least the main exception (it could be a MemoryError).
1726 */
1727 seen = PySet_New(NULL);
1728 if (seen == NULL)
1729 PyErr_Clear();
1730 print_exception_recursive(f, value, seen);
1731 Py_XDECREF(seen);
1732 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001733}
1734
Guido van Rossum82598051997-03-05 00:20:32 +00001735PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001736PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 PyObject *ret = NULL;
1740 mod_ty mod;
1741 PyArena *arena = PyArena_New();
1742 if (arena == NULL)
1743 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1746 if (mod != NULL)
1747 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1748 PyArena_Free(arena);
1749 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001750}
1751
1752PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001753PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 PyObject *ret;
1757 mod_ty mod;
1758 PyArena *arena = PyArena_New();
1759 if (arena == NULL)
1760 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1763 flags, NULL, arena);
1764 if (closeit)
1765 fclose(fp);
1766 if (mod == NULL) {
1767 PyArena_Free(arena);
1768 return NULL;
1769 }
1770 ret = run_mod(mod, filename, globals, locals, flags, arena);
1771 PyArena_Free(arena);
1772 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001773}
1774
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001775static void
1776flush_io(void)
1777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 PyObject *f, *r;
1779 PyObject *type, *value, *traceback;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001780 _Py_IDENTIFIER(flush);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 /* Save the current exception */
1783 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 f = PySys_GetObject("stderr");
1786 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001787 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 if (r)
1789 Py_DECREF(r);
1790 else
1791 PyErr_Clear();
1792 }
1793 f = PySys_GetObject("stdout");
1794 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001795 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 if (r)
1797 Py_DECREF(r);
1798 else
1799 PyErr_Clear();
1800 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001803}
1804
Guido van Rossum82598051997-03-05 00:20:32 +00001805static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 PyCodeObject *co;
1810 PyObject *v;
1811 co = PyAST_Compile(mod, filename, flags, arena);
1812 if (co == NULL)
1813 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001814 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 Py_DECREF(co);
1816 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001817}
1818
Guido van Rossum82598051997-03-05 00:20:32 +00001819static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001820run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 PyCodeObject *co;
1824 PyObject *v;
1825 long magic;
1826 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 magic = PyMarshal_ReadLongFromFile(fp);
1829 if (magic != PyImport_GetMagicNumber()) {
1830 PyErr_SetString(PyExc_RuntimeError,
1831 "Bad magic number in .pyc file");
1832 return NULL;
1833 }
1834 (void) PyMarshal_ReadLongFromFile(fp);
1835 v = PyMarshal_ReadLastObjectFromFile(fp);
1836 fclose(fp);
1837 if (v == NULL || !PyCode_Check(v)) {
1838 Py_XDECREF(v);
1839 PyErr_SetString(PyExc_RuntimeError,
1840 "Bad code object in .pyc file");
1841 return NULL;
1842 }
1843 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001844 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 if (v && flags)
1846 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1847 Py_DECREF(co);
1848 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001849}
1850
Guido van Rossum82598051997-03-05 00:20:32 +00001851PyObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00001852Py_CompileStringExFlags(const char *str, const char *filename, int start,
1853 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 PyCodeObject *co;
1856 mod_ty mod;
1857 PyArena *arena = PyArena_New();
1858 if (arena == NULL)
1859 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1862 if (mod == NULL) {
1863 PyArena_Free(arena);
1864 return NULL;
1865 }
1866 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1867 PyObject *result = PyAST_mod2obj(mod);
1868 PyArena_Free(arena);
1869 return result;
1870 }
Georg Brandl8334fd92010-12-04 10:26:46 +00001871 co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 PyArena_Free(arena);
1873 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001874}
1875
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001876/* For use in Py_LIMITED_API */
1877#undef Py_CompileString
1878PyObject *
1879PyCompileString(const char *str, const char *filename, int start)
1880{
1881 return Py_CompileStringFlags(str, filename, start, NULL);
1882}
1883
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001884struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001885Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 struct symtable *st;
1888 mod_ty mod;
1889 PyCompilerFlags flags;
1890 PyArena *arena = PyArena_New();
1891 if (arena == NULL)
1892 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 flags.cf_flags = 0;
1895 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1896 if (mod == NULL) {
1897 PyArena_Free(arena);
1898 return NULL;
1899 }
1900 st = PySymtable_Build(mod, filename, 0);
1901 PyArena_Free(arena);
1902 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001903}
1904
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905/* Preferred access to parser is through AST. */
1906mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001907PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 mod_ty mod;
1911 PyCompilerFlags localflags;
1912 perrdetail err;
1913 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1916 &_PyParser_Grammar, start, &err,
1917 &iflags);
1918 if (flags == NULL) {
1919 localflags.cf_flags = 0;
1920 flags = &localflags;
1921 }
1922 if (n) {
1923 flags->cf_flags |= iflags & PyCF_MASK;
1924 mod = PyAST_FromNode(n, flags, filename, arena);
1925 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 }
1927 else {
1928 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001929 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001931 err_free(&err);
1932 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933}
1934
1935mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001936PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 int start, char *ps1,
1938 char *ps2, PyCompilerFlags *flags, int *errcode,
1939 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 mod_ty mod;
1942 PyCompilerFlags localflags;
1943 perrdetail err;
1944 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
1947 &_PyParser_Grammar,
1948 start, ps1, ps2, &err, &iflags);
1949 if (flags == NULL) {
1950 localflags.cf_flags = 0;
1951 flags = &localflags;
1952 }
1953 if (n) {
1954 flags->cf_flags |= iflags & PyCF_MASK;
1955 mod = PyAST_FromNode(n, flags, filename, arena);
1956 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 }
1958 else {
1959 err_input(&err);
1960 if (errcode)
1961 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02001962 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001964 err_free(&err);
1965 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966}
1967
Guido van Rossuma110aa61994-08-29 12:50:44 +00001968/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001969
Guido van Rossuma110aa61994-08-29 12:50:44 +00001970node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001971PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 perrdetail err;
1974 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1975 &_PyParser_Grammar,
1976 start, NULL, NULL, &err, flags);
1977 if (n == NULL)
1978 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001979 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001982}
1983
Guido van Rossuma110aa61994-08-29 12:50:44 +00001984/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001985
Guido van Rossuma110aa61994-08-29 12:50:44 +00001986node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001987PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 perrdetail err;
1990 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1991 start, &err, flags);
1992 if (n == NULL)
1993 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001994 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001996}
1997
1998node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001999PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 perrdetail err;
2003 node *n = PyParser_ParseStringFlagsFilename(str, filename,
2004 &_PyParser_Grammar, start, &err, flags);
2005 if (n == NULL)
2006 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002007 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00002009}
2010
2011node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002012PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00002015}
2016
Guido van Rossum66ebd912003-04-17 16:02:26 +00002017/* May want to move a more generalized form of this to parsetok.c or
2018 even parser modules. */
2019
2020void
Victor Stinner7f2fee32011-04-05 00:39:01 +02002021PyParser_ClearError(perrdetail *err)
2022{
2023 err_free(err);
2024}
2025
2026void
Guido van Rossum66ebd912003-04-17 16:02:26 +00002027PyParser_SetError(perrdetail *err)
2028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00002030}
2031
Victor Stinner7f2fee32011-04-05 00:39:01 +02002032static void
2033err_free(perrdetail *err)
2034{
2035 Py_CLEAR(err->filename);
2036}
2037
Guido van Rossuma110aa61994-08-29 12:50:44 +00002038/* Set the error appropriate to the given input error code (see errcode.h) */
2039
2040static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002041err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00002042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 PyObject *v, *w, *errtype, *errtext;
2044 PyObject *msg_obj = NULL;
2045 char *msg = NULL;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 errtype = PyExc_SyntaxError;
2048 switch (err->error) {
2049 case E_ERROR:
2050 return;
2051 case E_SYNTAX:
2052 errtype = PyExc_IndentationError;
2053 if (err->expected == INDENT)
2054 msg = "expected an indented block";
2055 else if (err->token == INDENT)
2056 msg = "unexpected indent";
2057 else if (err->token == DEDENT)
2058 msg = "unexpected unindent";
2059 else {
2060 errtype = PyExc_SyntaxError;
2061 msg = "invalid syntax";
2062 }
2063 break;
2064 case E_TOKEN:
2065 msg = "invalid token";
2066 break;
2067 case E_EOFS:
2068 msg = "EOF while scanning triple-quoted string literal";
2069 break;
2070 case E_EOLS:
2071 msg = "EOL while scanning string literal";
2072 break;
2073 case E_INTR:
2074 if (!PyErr_Occurred())
2075 PyErr_SetNone(PyExc_KeyboardInterrupt);
2076 goto cleanup;
2077 case E_NOMEM:
2078 PyErr_NoMemory();
2079 goto cleanup;
2080 case E_EOF:
2081 msg = "unexpected EOF while parsing";
2082 break;
2083 case E_TABSPACE:
2084 errtype = PyExc_TabError;
2085 msg = "inconsistent use of tabs and spaces in indentation";
2086 break;
2087 case E_OVERFLOW:
2088 msg = "expression too long";
2089 break;
2090 case E_DEDENT:
2091 errtype = PyExc_IndentationError;
2092 msg = "unindent does not match any outer indentation level";
2093 break;
2094 case E_TOODEEP:
2095 errtype = PyExc_IndentationError;
2096 msg = "too many levels of indentation";
2097 break;
2098 case E_DECODE: {
2099 PyObject *type, *value, *tb;
2100 PyErr_Fetch(&type, &value, &tb);
2101 msg = "unknown decode error";
2102 if (value != NULL)
2103 msg_obj = PyObject_Str(value);
2104 Py_XDECREF(type);
2105 Py_XDECREF(value);
2106 Py_XDECREF(tb);
2107 break;
2108 }
2109 case E_LINECONT:
2110 msg = "unexpected character after line continuation character";
2111 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00002112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 case E_IDENTIFIER:
2114 msg = "invalid character in identifier";
2115 break;
2116 default:
2117 fprintf(stderr, "error=%d\n", err->error);
2118 msg = "unknown parsing error";
2119 break;
2120 }
2121 /* err->text may not be UTF-8 in case of decoding errors.
2122 Explicitly convert to an object. */
2123 if (!err->text) {
2124 errtext = Py_None;
2125 Py_INCREF(Py_None);
2126 } else {
2127 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2128 "replace");
2129 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002130 v = Py_BuildValue("(OiiN)", err->filename,
2131 err->lineno, err->offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 if (v != NULL) {
2133 if (msg_obj)
2134 w = Py_BuildValue("(OO)", msg_obj, v);
2135 else
2136 w = Py_BuildValue("(sO)", msg, v);
2137 } else
2138 w = NULL;
2139 Py_XDECREF(v);
2140 PyErr_SetObject(errtype, w);
2141 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002142cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 Py_XDECREF(msg_obj);
2144 if (err->text != NULL) {
2145 PyObject_FREE(err->text);
2146 err->text = NULL;
2147 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002148}
2149
2150/* Print fatal error message and abort */
2151
2152void
Tim Peters7c321a82002-07-09 02:57:01 +00002153Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002154{
Victor Stinner024e37a2011-03-31 01:31:06 +02002155 const int fd = fileno(stderr);
2156 PyThreadState *tstate;
2157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 fprintf(stderr, "Fatal Python error: %s\n", msg);
2159 fflush(stderr); /* it helps in Windows debug build */
2160 if (PyErr_Occurred()) {
Victor Stinner55a5c782010-06-08 21:00:13 +00002161 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 }
Victor Stinner024e37a2011-03-31 01:31:06 +02002163 else {
2164 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
2165 if (tstate != NULL) {
2166 fputc('\n', stderr);
2167 fflush(stderr);
Victor Stinner7bba62f2011-05-07 12:43:00 +02002168 _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
Victor Stinner024e37a2011-03-31 01:31:06 +02002169 }
Victor Stinnerd727e232011-04-01 12:13:55 +02002170 _PyFaulthandler_Fini();
Victor Stinner024e37a2011-03-31 01:31:06 +02002171 }
2172
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002173#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 {
2175 size_t len = strlen(msg);
2176 WCHAR* buffer;
2177 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 /* Convert the message to wchar_t. This uses a simple one-to-one
2180 conversion, assuming that the this error message actually uses ASCII
2181 only. If this ceases to be true, we will have to convert. */
2182 buffer = alloca( (len+1) * (sizeof *buffer));
2183 for( i=0; i<=len; ++i)
2184 buffer[i] = msg[i];
2185 OutputDebugStringW(L"Fatal Python error: ");
2186 OutputDebugStringW(buffer);
2187 OutputDebugStringW(L"\n");
2188 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002189#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002191#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002192#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002194}
2195
2196/* Clean up and exit */
2197
Guido van Rossuma110aa61994-08-29 12:50:44 +00002198#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002199#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002200#endif
2201
Collin Winter670e6922007-03-21 02:57:17 +00002202static void (*pyexitfunc)(void) = NULL;
2203/* For the atexit module. */
2204void _Py_PyAtExit(void (*func)(void))
2205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002207}
2208
2209static void
2210call_py_exitfuncs(void)
2211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 if (pyexitfunc == NULL)
2213 return;
Collin Winter670e6922007-03-21 02:57:17 +00002214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 (*pyexitfunc)();
2216 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002217}
2218
Antoine Pitrou011bd622009-10-20 21:52:47 +00002219/* Wait until threading._shutdown completes, provided
2220 the threading module was imported in the first place.
2221 The shutdown routine will wait until all non-daemon
2222 "threading" threads have completed. */
2223static void
2224wait_for_thread_shutdown(void)
2225{
2226#ifdef WITH_THREAD
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002227 _Py_IDENTIFIER(_shutdown);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 PyObject *result;
2229 PyThreadState *tstate = PyThreadState_GET();
2230 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2231 "threading");
2232 if (threading == NULL) {
2233 /* threading not imported */
2234 PyErr_Clear();
2235 return;
2236 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002237 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 if (result == NULL) {
2239 PyErr_WriteUnraisable(threading);
2240 }
2241 else {
2242 Py_DECREF(result);
2243 }
2244 Py_DECREF(threading);
Antoine Pitrou011bd622009-10-20 21:52:47 +00002245#endif
2246}
2247
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002248#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002249static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002250static int nexitfuncs = 0;
2251
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002252int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 if (nexitfuncs >= NEXITFUNCS)
2255 return -1;
2256 exitfuncs[nexitfuncs++] = func;
2257 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002258}
2259
Guido van Rossumcc283f51997-08-05 02:22:03 +00002260static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002261call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 while (nexitfuncs > 0)
2264 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 fflush(stdout);
2267 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002268}
2269
2270void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002271Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002276}
2277
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002278static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002279initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002280{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002281#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002283#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002284#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002286#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002287#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002289#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002291}
2292
Guido van Rossum7433b121997-02-14 19:45:36 +00002293
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002294/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2295 *
2296 * All of the code in this function must only use async-signal-safe functions,
2297 * listed at `man 7 signal` or
2298 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2299 */
2300void
2301_Py_RestoreSignals(void)
2302{
2303#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 PyOS_setsig(SIGPIPE, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002305#endif
2306#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 PyOS_setsig(SIGXFZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002308#endif
2309#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 PyOS_setsig(SIGXFSZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002311#endif
2312}
2313
2314
Guido van Rossum7433b121997-02-14 19:45:36 +00002315/*
2316 * The file descriptor fd is considered ``interactive'' if either
2317 * a) isatty(fd) is TRUE, or
2318 * b) the -i flag was given, and the filename associated with
2319 * the descriptor is NULL or "<stdin>" or "???".
2320 */
2321int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002322Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 if (isatty((int)fileno(fp)))
2325 return 1;
2326 if (!Py_InteractiveFlag)
2327 return 0;
2328 return (filename == NULL) ||
2329 (strcmp(filename, "<stdin>") == 0) ||
2330 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002331}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002332
2333
Tim Petersd08e3822003-04-17 15:24:21 +00002334#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002335#if defined(WIN32) && defined(_MSC_VER)
2336
2337/* Stack checking for Microsoft C */
2338
2339#include <malloc.h>
2340#include <excpt.h>
2341
Fred Drakee8de31c2000-08-31 05:38:39 +00002342/*
2343 * Return non-zero when we run out of memory on the stack; zero otherwise.
2344 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002345int
Fred Drake399739f2000-08-31 05:52:44 +00002346PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 __try {
2349 /* alloca throws a stack overflow exception if there's
2350 not enough space left on the stack */
2351 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2352 return 0;
2353 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2354 EXCEPTION_EXECUTE_HANDLER :
2355 EXCEPTION_CONTINUE_SEARCH) {
2356 int errcode = _resetstkoflw();
2357 if (errcode == 0)
2358 {
2359 Py_FatalError("Could not reset the stack!");
2360 }
2361 }
2362 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002363}
2364
2365#endif /* WIN32 && _MSC_VER */
2366
2367/* Alternate implementations can be added here... */
2368
2369#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002370
2371
2372/* Wrappers around sigaction() or signal(). */
2373
2374PyOS_sighandler_t
2375PyOS_getsig(int sig)
2376{
2377#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 struct sigaction context;
2379 if (sigaction(sig, NULL, &context) == -1)
2380 return SIG_ERR;
2381 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002382#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002384/* Special signal handling for the secure CRT in Visual Studio 2005 */
2385#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 switch (sig) {
2387 /* Only these signals are valid */
2388 case SIGINT:
2389 case SIGILL:
2390 case SIGFPE:
2391 case SIGSEGV:
2392 case SIGTERM:
2393 case SIGBREAK:
2394 case SIGABRT:
2395 break;
2396 /* Don't call signal() with other values or it will assert */
2397 default:
2398 return SIG_ERR;
2399 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002400#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 handler = signal(sig, SIG_IGN);
2402 if (handler != SIG_ERR)
2403 signal(sig, handler);
2404 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002405#endif
2406}
2407
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002408/*
2409 * All of the code in this function must only use async-signal-safe functions,
2410 * listed at `man 7 signal` or
2411 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2412 */
Guido van Rossum6f256182000-09-16 16:32:19 +00002413PyOS_sighandler_t
2414PyOS_setsig(int sig, PyOS_sighandler_t handler)
2415{
2416#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 /* Some code in Modules/signalmodule.c depends on sigaction() being
2418 * used here if HAVE_SIGACTION is defined. Fix that if this code
2419 * changes to invalidate that assumption.
2420 */
2421 struct sigaction context, ocontext;
2422 context.sa_handler = handler;
2423 sigemptyset(&context.sa_mask);
2424 context.sa_flags = 0;
2425 if (sigaction(sig, &context, &ocontext) == -1)
2426 return SIG_ERR;
2427 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002428#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 PyOS_sighandler_t oldhandler;
2430 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002431#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002433#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002435#endif
2436}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437
2438/* Deprecated C API functions still provided for binary compatiblity */
2439
2440#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002441PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445}
2446
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002447#undef PyParser_SimpleParseString
2448PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449PyParser_SimpleParseString(const char *str, int start)
2450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002453
2454#undef PyRun_AnyFile
2455PyAPI_FUNC(int)
2456PyRun_AnyFile(FILE *fp, const char *name)
2457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002459}
2460
2461#undef PyRun_AnyFileEx
2462PyAPI_FUNC(int)
2463PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002466}
2467
2468#undef PyRun_AnyFileFlags
2469PyAPI_FUNC(int)
2470PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002473}
2474
2475#undef PyRun_File
2476PyAPI_FUNC(PyObject *)
2477PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002480}
2481
2482#undef PyRun_FileEx
2483PyAPI_FUNC(PyObject *)
2484PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002487}
2488
2489#undef PyRun_FileFlags
2490PyAPI_FUNC(PyObject *)
2491PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002495}
2496
2497#undef PyRun_SimpleFile
2498PyAPI_FUNC(int)
2499PyRun_SimpleFile(FILE *f, const char *p)
2500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002502}
2503
2504#undef PyRun_SimpleFileEx
2505PyAPI_FUNC(int)
2506PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002509}
2510
2511
2512#undef PyRun_String
2513PyAPI_FUNC(PyObject *)
2514PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002517}
2518
2519#undef PyRun_SimpleString
2520PyAPI_FUNC(int)
2521PyRun_SimpleString(const char *s)
2522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002524}
2525
2526#undef Py_CompileString
2527PyAPI_FUNC(PyObject *)
2528Py_CompileString(const char *str, const char *p, int s)
2529{
Georg Brandl8334fd92010-12-04 10:26:46 +00002530 return Py_CompileStringExFlags(str, p, s, NULL, -1);
2531}
2532
2533#undef Py_CompileStringFlags
2534PyAPI_FUNC(PyObject *)
2535Py_CompileStringFlags(const char *str, const char *p, int s,
2536 PyCompilerFlags *flags)
2537{
2538 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002539}
2540
2541#undef PyRun_InteractiveOne
2542PyAPI_FUNC(int)
2543PyRun_InteractiveOne(FILE *f, const char *p)
2544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002546}
2547
2548#undef PyRun_InteractiveLoop
2549PyAPI_FUNC(int)
2550PyRun_InteractiveLoop(FILE *f, const char *p)
2551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002553}
2554
2555#ifdef __cplusplus
2556}
2557#endif