blob: abc9ffc994cf34fd1da83bbf4228747b6c4c1385 [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
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100351static int
352file_is_closed(PyObject *fobj)
353{
354 int r;
355 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
356 if (tmp == NULL) {
357 PyErr_Clear();
358 return 0;
359 }
360 r = PyObject_IsTrue(tmp);
361 Py_DECREF(tmp);
362 if (r < 0)
363 PyErr_Clear();
364 return r > 0;
365}
366
Neal Norwitz2bad9702007-08-27 06:19:22 +0000367static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000368flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 PyObject *fout = PySys_GetObject("stdout");
371 PyObject *ferr = PySys_GetObject("stderr");
372 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200373 _Py_IDENTIFIER(flush);
Guido van Rossume8432ac2007-07-09 15:04:50 +0000374
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100375 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200376 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (tmp == NULL)
Antoine Pitroubddc9fe2010-08-08 20:46:42 +0000378 PyErr_WriteUnraisable(fout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 else
380 Py_DECREF(tmp);
381 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000382
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100383 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200384 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (tmp == NULL)
386 PyErr_Clear();
387 else
388 Py_DECREF(tmp);
389 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000390}
391
Guido van Rossum25ce5661997-08-02 03:10:38 +0000392/* Undo the effect of Py_Initialize().
393
394 Beware: if multiple interpreter and/or thread states exist, these
395 are not wiped out; only the current thread and interpreter state
396 are deleted. But since everything else is deleted, those other
397 interpreter and thread states should no longer be used.
398
399 (XXX We should do better, e.g. wipe out all interpreters and
400 threads.)
401
402 Locking: as above.
403
404*/
405
406void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000407Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 PyInterpreterState *interp;
410 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 if (!initialized)
413 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 wait_for_thread_shutdown();
Antoine Pitrou011bd622009-10-20 21:52:47 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 /* The interpreter is still entirely intact at this point, and the
418 * exit funcs may be relying on that. In particular, if some thread
419 * or exit func is still waiting to do an import, the import machinery
420 * expects Py_IsInitialized() to return true. So don't say the
421 * interpreter is uninitialized until after the exit funcs have run.
422 * Note that Threading.py uses an exit func to do a join on all the
423 * threads created thru it, so this also protects pending imports in
424 * the threads created via Threading.
425 */
426 call_py_exitfuncs();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 /* Get current thread state and interpreter pointer */
429 tstate = PyThreadState_GET();
430 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000431
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200432 /* Remaining threads (e.g. daemon threads) will automatically exit
433 after taking the GIL (in PyEval_RestoreThread()). */
434 _Py_Finalizing = tstate;
435 initialized = 0;
436
437 /* Flush stdout+stderr */
438 flush_std_files();
439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 /* Disable signal handling */
441 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 /* Clear type lookup cache */
444 PyType_ClearCache();
Christian Heimes26855632008-01-27 23:50:43 +0000445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 /* Collect garbage. This may call finalizers; it's nice to call these
447 * before all modules are destroyed.
448 * XXX If a __del__ or weakref callback is triggered here, and tries to
449 * XXX import a module, bad things can happen, because Python no
450 * XXX longer believes it's initialized.
451 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
452 * XXX is easy to provoke that way. I've also seen, e.g.,
453 * XXX Exception exceptions.ImportError: 'No module named sha'
454 * XXX in <function callback at 0x008F5718> ignored
455 * XXX but I'm unclear on exactly how that one happens. In any case,
456 * XXX I haven't seen a real-life report of either of these.
457 */
458 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000459#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 /* With COUNT_ALLOCS, it helps to run GC multiple times:
461 each collection might release some types from the type
462 list, so they become garbage. */
463 while (PyGC_Collect() > 0)
464 /* nothing */;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000465#endif
Antoine Pitrou696e0352010-08-08 22:18:46 +0000466 /* We run this while most interpreter state is still alive, so that
467 debug information can be printed out */
468 _PyGC_Fini();
Guido van Rossume13ddc92003-04-17 17:29:22 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 /* Destroy all modules */
471 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 /* Flush stdout+stderr (again, in case more was printed) */
474 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 /* Collect final garbage. This disposes of cycles created by
477 * new-style class definitions, for example.
478 * XXX This is disabled because it caused too many problems. If
479 * XXX a __del__ or weakref callback triggers here, Python code has
480 * XXX a hard time running, because even the sys module has been
481 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
482 * XXX One symptom is a sequence of information-free messages
483 * XXX coming from threads (if a __del__ or callback is invoked,
484 * XXX other threads can execute too, and any exception they encounter
485 * XXX triggers a comedy of errors as subsystem after subsystem
486 * XXX fails to find what it *expects* to find in sys to help report
487 * XXX the exception and consequent unexpected failures). I've also
488 * XXX seen segfaults then, after adding print statements to the
489 * XXX Python code getting called.
490 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000491#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000493#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
496 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000497
Victor Stinner024e37a2011-03-31 01:31:06 +0200498 /* unload faulthandler module */
499 _PyFaulthandler_Fini();
500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000502#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000504#endif
505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000507
Tim Peters9cf25ce2003-04-17 15:21:01 +0000508#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 /* Display all objects still alive -- this can invoke arbitrary
510 * __repr__ overrides, so requires a mostly-intact interpreter.
511 * Alas, a lot of stuff may still be alive now that will be cleaned
512 * up later.
513 */
514 if (Py_GETENV("PYTHONDUMPREFS"))
515 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000516#endif /* Py_TRACE_REFS */
517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 /* Clear interpreter state */
519 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 /* Now we decref the exception classes. After this point nothing
522 can raise an exception. That's okay, because each Fini() method
523 below has been checked to make sure no exceptions are ever
524 raised.
525 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 /* Cleanup auto-thread-state */
Christian Heimes7d2ff882007-11-30 14:35:04 +0000530#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 _PyGILState_Fini();
Christian Heimes7d2ff882007-11-30 14:35:04 +0000532#endif /* WITH_THREAD */
533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 /* Delete current thread */
535 PyThreadState_Swap(NULL);
536 PyInterpreterState_Delete(interp);
Barry Warsawf242aa02000-05-25 23:09:49 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 /* Sundry finalizers */
539 PyMethod_Fini();
540 PyFrame_Fini();
541 PyCFunction_Fini();
542 PyTuple_Fini();
543 PyList_Fini();
544 PySet_Fini();
545 PyBytes_Fini();
546 PyByteArray_Fini();
547 PyLong_Fini();
548 PyFloat_Fini();
549 PyDict_Fini();
Antoine Pitrouf34a0cd2011-11-18 20:14:34 +0100550 PySlice_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 /* Cleanup Unicode implementation */
553 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* reset file system default encoding */
Victor Stinnerb744ba12010-05-15 12:27:16 +0000556 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 free((char*)Py_FileSystemDefaultEncoding);
558 Py_FileSystemDefaultEncoding = NULL;
559 }
Christian Heimesc8967002007-11-30 10:18:26 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 /* XXX Still allocated:
562 - various static ad-hoc pointers to interned strings
563 - int and float free list blocks
564 - whatever various modules and libraries allocate
565 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000568
Tim Peters269b2a62003-04-17 19:52:29 +0000569#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 /* Display addresses (& refcnts) of all objects still alive.
571 * An address can be used to find the repr of the object, printed
572 * above by _Py_PrintReferences.
573 */
574 if (Py_GETENV("PYTHONDUMPREFS"))
575 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000576#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000577#ifdef PYMALLOC_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 if (Py_GETENV("PYTHONMALLOCSTATS"))
579 _PyObject_DebugMallocStats();
Tim Peters0e871182002-04-13 08:29:14 +0000580#endif
581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000583}
584
585/* Create and initialize a new interpreter and thread, and return the
586 new thread. This requires that Py_Initialize() has been called
587 first.
588
589 Unsuccessful initialization yields a NULL pointer. Note that *no*
590 exception information is available even in this case -- the
591 exception information is held in the thread, and there is no
592 thread.
593
594 Locking: as above.
595
596*/
597
598PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000599Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 PyInterpreterState *interp;
602 PyThreadState *tstate, *save_tstate;
603 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 if (!initialized)
606 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 interp = PyInterpreterState_New();
609 if (interp == NULL)
610 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 tstate = PyThreadState_New(interp);
613 if (tstate == NULL) {
614 PyInterpreterState_Delete(interp);
615 return NULL;
616 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 interp->modules = PyDict_New();
623 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000624
Victor Stinner49d3f252010-10-17 01:24:53 +0000625 bimod = _PyImport_FindBuiltin("builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 if (bimod != NULL) {
627 interp->builtins = PyModule_GetDict(bimod);
628 if (interp->builtins == NULL)
629 goto handle_error;
630 Py_INCREF(interp->builtins);
631 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 /* initialize builtin exceptions */
634 _PyExc_Init();
Christian Heimes6a27efa2008-10-30 21:48:26 +0000635
Victor Stinner49d3f252010-10-17 01:24:53 +0000636 sysmod = _PyImport_FindBuiltin("sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 if (bimod != NULL && sysmod != NULL) {
638 PyObject *pstderr;
639 interp->sysdict = PyModule_GetDict(sysmod);
640 if (interp->sysdict == NULL)
641 goto handle_error;
642 Py_INCREF(interp->sysdict);
643 PySys_SetPath(Py_GetPath());
644 PyDict_SetItemString(interp->sysdict, "modules",
645 interp->modules);
646 /* Set up a preliminary stderr printer until we have enough
647 infrastructure for the io module in place. */
648 pstderr = PyFile_NewStdPrinter(fileno(stderr));
649 if (pstderr == NULL)
650 Py_FatalError("Py_Initialize: can't set preliminary stderr");
651 PySys_SetObject("stderr", pstderr);
652 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000653 Py_DECREF(pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 _PyImportHooks_Init();
Victor Stinner793b5312011-04-27 00:24:21 +0200656
657 if (initfsencoding(interp) < 0)
658 goto handle_error;
659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 if (initstdio() < 0)
661 Py_FatalError(
662 "Py_Initialize: can't initialize sys standard streams");
663 initmain();
664 if (!Py_NoSiteFlag)
665 initsite();
666 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 if (!PyErr_Occurred())
669 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000670
Thomas Wouters89f507f2006-12-13 04:49:30 +0000671handle_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000673
Victor Stinnerc40a3502011-04-27 00:20:27 +0200674 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 PyThreadState_Clear(tstate);
676 PyThreadState_Swap(save_tstate);
677 PyThreadState_Delete(tstate);
678 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000681}
682
683/* Delete an interpreter and its last thread. This requires that the
684 given thread state is current, that the thread has no remaining
685 frames, and that it is its interpreter's only remaining thread.
686 It is a fatal error to violate these constraints.
687
688 (Py_Finalize() doesn't have these constraints -- it zaps
689 everything, regardless.)
690
691 Locking: as above.
692
693*/
694
695void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000696Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 if (tstate != PyThreadState_GET())
701 Py_FatalError("Py_EndInterpreter: thread is not current");
702 if (tstate->frame != NULL)
703 Py_FatalError("Py_EndInterpreter: thread still has a frame");
704 if (tstate != interp->tstate_head || tstate->next != NULL)
705 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 PyImport_Cleanup();
708 PyInterpreterState_Clear(interp);
709 PyThreadState_Swap(NULL);
710 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000711}
712
Martin v. Löwis790465f2008-04-05 20:41:37 +0000713static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000714
715void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000716Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 if (pn && *pn)
719 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000720}
721
Martin v. Löwis790465f2008-04-05 20:41:37 +0000722wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000723Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000726}
727
Martin v. Löwis790465f2008-04-05 20:41:37 +0000728static wchar_t *default_home = NULL;
729static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000730
731void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000732Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000735}
736
Martin v. Löwis790465f2008-04-05 20:41:37 +0000737wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000738Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 wchar_t *home = default_home;
741 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
742 char* chome = Py_GETENV("PYTHONHOME");
743 if (chome) {
744 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
745 if (r != (size_t)-1 && r <= PATH_MAX)
746 home = env_home;
747 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 }
750 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000751}
752
Guido van Rossum6135a871995-01-09 17:53:26 +0000753/* Create __main__ module */
754
755static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000756initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 PyObject *m, *d;
759 m = PyImport_AddModule("__main__");
760 if (m == NULL)
761 Py_FatalError("can't create __main__ module");
762 d = PyModule_GetDict(m);
763 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
764 PyObject *bimod = PyImport_ImportModule("builtins");
765 if (bimod == NULL ||
766 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
767 Py_FatalError("can't add __builtins__ to __main__");
768 Py_DECREF(bimod);
769 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000770}
771
Victor Stinner793b5312011-04-27 00:24:21 +0200772static int
773initfsencoding(PyInterpreterState *interp)
Victor Stinnerb744ba12010-05-15 12:27:16 +0000774{
775 PyObject *codec;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000776
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200777 if (Py_FileSystemDefaultEncoding == NULL)
778 {
779 Py_FileSystemDefaultEncoding = get_locale_encoding();
780 if (Py_FileSystemDefaultEncoding == NULL)
Victor Stinnere4743092010-10-19 00:05:51 +0000781 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
Victor Stinnerb744ba12010-05-15 12:27:16 +0000782
Victor Stinnere4743092010-10-19 00:05:51 +0000783 Py_HasFileSystemDefaultEncoding = 0;
Victor Stinner793b5312011-04-27 00:24:21 +0200784 interp->fscodec_initialized = 1;
785 return 0;
Victor Stinner7f84ab52010-06-11 00:36:33 +0000786 }
Victor Stinnerb744ba12010-05-15 12:27:16 +0000787
788 /* the encoding is mbcs, utf-8 or ascii */
789 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
790 if (!codec) {
791 /* Such error can only occurs in critical situations: no more
792 * memory, import a module of the standard library failed,
793 * etc. */
Victor Stinner793b5312011-04-27 00:24:21 +0200794 return -1;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000795 }
Victor Stinner793b5312011-04-27 00:24:21 +0200796 Py_DECREF(codec);
797 interp->fscodec_initialized = 1;
798 return 0;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000799}
800
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000801/* Import the site module (not into __main__ though) */
802
803static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000804initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 PyObject *m;
807 m = PyImport_ImportModule("site");
808 if (m == NULL) {
809 PyErr_Print();
810 Py_Finalize();
811 exit(1);
812 }
813 else {
814 Py_DECREF(m);
815 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000816}
817
Antoine Pitrou05608432009-01-09 18:53:14 +0000818static PyObject*
819create_stdio(PyObject* io,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 int fd, int write_mode, char* name,
821 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +0000822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
824 const char* mode;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000825 const char* newline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 PyObject *line_buffering;
827 int buffering, isatty;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200828 _Py_IDENTIFIER(open);
829 _Py_IDENTIFIER(isatty);
830 _Py_IDENTIFIER(TextIOWrapper);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200831 _Py_IDENTIFIER(name);
832 _Py_IDENTIFIER(mode);
Antoine Pitrou05608432009-01-09 18:53:14 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 /* stdin is always opened in buffered mode, first because it shouldn't
835 make a difference in common use cases, second because TextIOWrapper
836 depends on the presence of a read1() method which only exists on
837 buffered streams.
838 */
839 if (Py_UnbufferedStdioFlag && write_mode)
840 buffering = 0;
841 else
842 buffering = -1;
843 if (write_mode)
844 mode = "wb";
845 else
846 mode = "rb";
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200847 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
848 fd, mode, buffering,
849 Py_None, Py_None, Py_None, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 if (buf == NULL)
851 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 if (buffering) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200854 _Py_IDENTIFIER(raw);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200855 raw = _PyObject_GetAttrId(buf, &PyId_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 if (raw == NULL)
857 goto error;
858 }
859 else {
860 raw = buf;
861 Py_INCREF(raw);
862 }
Antoine Pitrou05608432009-01-09 18:53:14 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 text = PyUnicode_FromString(name);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200865 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 goto error;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200867 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 if (res == NULL)
869 goto error;
870 isatty = PyObject_IsTrue(res);
871 Py_DECREF(res);
872 if (isatty == -1)
873 goto error;
874 if (isatty || Py_UnbufferedStdioFlag)
875 line_buffering = Py_True;
876 else
877 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 Py_CLEAR(raw);
880 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +0000881
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000882 newline = "\n";
883#ifdef MS_WINDOWS
884 if (!write_mode) {
885 /* translate \r\n to \n for sys.stdin on Windows */
886 newline = NULL;
887 }
888#endif
889
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200890 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
891 buf, encoding, errors,
892 newline, line_buffering);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 Py_CLEAR(buf);
894 if (stream == NULL)
895 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 if (write_mode)
898 mode = "w";
899 else
900 mode = "r";
901 text = PyUnicode_FromString(mode);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200902 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 goto error;
904 Py_CLEAR(text);
905 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +0000906
907error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 Py_XDECREF(buf);
909 Py_XDECREF(stream);
910 Py_XDECREF(text);
911 Py_XDECREF(raw);
912 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +0000913}
914
Antoine Pitrou11942a52011-11-28 19:08:36 +0100915static int
916is_valid_fd(int fd)
917{
918 int dummy_fd;
919 if (fd < 0 || !_PyVerify_fd(fd))
920 return 0;
921 dummy_fd = dup(fd);
922 if (dummy_fd < 0)
923 return 0;
924 close(dummy_fd);
925 return 1;
926}
927
Georg Brandl1a3284e2007-12-02 09:40:06 +0000928/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000929static int
930initstdio(void)
931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 PyObject *iomod = NULL, *wrapper;
933 PyObject *bimod = NULL;
934 PyObject *m;
935 PyObject *std = NULL;
936 int status = 0, fd;
937 PyObject * encoding_attr;
938 char *encoding = NULL, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 /* Hack to avoid a nasty recursion issue when Python is invoked
941 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
942 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
943 goto error;
944 }
945 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
948 goto error;
949 }
950 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 if (!(bimod = PyImport_ImportModule("builtins"))) {
953 goto error;
954 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (!(iomod = PyImport_ImportModule("io"))) {
957 goto error;
958 }
959 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
960 goto error;
961 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 /* Set builtins.open */
964 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000965 Py_DECREF(wrapper);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 goto error;
967 }
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000968 Py_DECREF(wrapper);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 encoding = Py_GETENV("PYTHONIOENCODING");
971 errors = NULL;
972 if (encoding) {
973 encoding = strdup(encoding);
974 errors = strchr(encoding, ':');
975 if (errors) {
976 *errors = '\0';
977 errors++;
978 }
979 }
Martin v. Löwis0f599892008-06-02 11:13:03 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 /* Set sys.stdin */
982 fd = fileno(stdin);
983 /* Under some conditions stdin, stdout and stderr may not be connected
984 * and fileno() may point to an invalid file descriptor. For example
985 * GUI apps don't have valid standard streams by default.
986 */
Antoine Pitrou11942a52011-11-28 19:08:36 +0100987 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 std = Py_None;
989 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 }
991 else {
992 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
993 if (std == NULL)
994 goto error;
995 } /* if (fd < 0) */
996 PySys_SetObject("__stdin__", std);
997 PySys_SetObject("stdin", std);
998 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 /* Set sys.stdout */
1001 fd = fileno(stdout);
Antoine Pitrou11942a52011-11-28 19:08:36 +01001002 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 std = Py_None;
1004 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 }
1006 else {
1007 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
1008 if (std == NULL)
1009 goto error;
1010 } /* if (fd < 0) */
1011 PySys_SetObject("__stdout__", std);
1012 PySys_SetObject("stdout", std);
1013 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001014
Guido van Rossum98297ee2007-11-06 21:34:58 +00001015#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 /* Set sys.stderr, replaces the preliminary stderr */
1017 fd = fileno(stderr);
Antoine Pitrou11942a52011-11-28 19:08:36 +01001018 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 std = Py_None;
1020 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 }
1022 else {
1023 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1024 if (std == NULL)
1025 goto error;
1026 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 /* Same as hack above, pre-import stderr's codec to avoid recursion
1029 when import.c tries to write to stderr in verbose mode. */
1030 encoding_attr = PyObject_GetAttrString(std, "encoding");
1031 if (encoding_attr != NULL) {
1032 const char * encoding;
1033 encoding = _PyUnicode_AsString(encoding_attr);
1034 if (encoding != NULL) {
1035 _PyCodec_Lookup(encoding);
1036 }
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +00001037 Py_DECREF(encoding_attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 }
1039 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 PySys_SetObject("__stderr__", std);
1042 PySys_SetObject("stderr", std);
1043 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001044#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001047 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 status = -1;
1049 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 if (encoding)
1052 free(encoding);
1053 Py_XDECREF(bimod);
1054 Py_XDECREF(iomod);
1055 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001056}
1057
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001058/* Parse input from a file and execute it */
1059
1060int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001061PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 if (filename == NULL)
1065 filename = "???";
1066 if (Py_FdIsInteractive(fp, filename)) {
1067 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
1068 if (closeit)
1069 fclose(fp);
1070 return err;
1071 }
1072 else
1073 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001074}
1075
1076int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001077PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 PyObject *v;
1080 int ret;
1081 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 if (flags == NULL) {
1084 flags = &local_flags;
1085 local_flags.cf_flags = 0;
1086 }
1087 v = PySys_GetObject("ps1");
1088 if (v == NULL) {
1089 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
1090 Py_XDECREF(v);
1091 }
1092 v = PySys_GetObject("ps2");
1093 if (v == NULL) {
1094 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1095 Py_XDECREF(v);
1096 }
1097 for (;;) {
1098 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
1099 PRINT_TOTAL_REFS();
1100 if (ret == E_EOF)
1101 return 0;
1102 /*
1103 if (ret == E_NOMEM)
1104 return -1;
1105 */
1106 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001107}
1108
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001109/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001110static int PARSER_FLAGS(PyCompilerFlags *flags)
1111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 int parser_flags = 0;
1113 if (!flags)
1114 return 0;
1115 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1116 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1117 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1118 parser_flags |= PyPARSE_IGNORE_COOKIE;
1119 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1120 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1121 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001122}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001123
Thomas Wouters89f507f2006-12-13 04:49:30 +00001124#if 0
1125/* Keep an example of flags with future keyword support. */
1126#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1128 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1129 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1130 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001131#endif
1132
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001133int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001134PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 PyObject *m, *d, *v, *w, *oenc = NULL;
1137 mod_ty mod;
1138 PyArena *arena;
1139 char *ps1 = "", *ps2 = "", *enc = NULL;
1140 int errcode = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001141 _Py_IDENTIFIER(encoding);
Tim Petersfe2127d2001-07-16 05:37:24 +00001142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 if (fp == stdin) {
1144 /* Fetch encoding from sys.stdin */
1145 v = PySys_GetObject("stdin");
1146 if (v == NULL || v == Py_None)
1147 return -1;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001148 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (!oenc)
1150 return -1;
1151 enc = _PyUnicode_AsString(oenc);
Victor Stinner386fe712010-05-19 00:34:15 +00001152 if (enc == NULL)
1153 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 }
1155 v = PySys_GetObject("ps1");
1156 if (v != NULL) {
1157 v = PyObject_Str(v);
1158 if (v == NULL)
1159 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001160 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +00001162 if (ps1 == NULL) {
1163 PyErr_Clear();
1164 ps1 = "";
1165 }
1166 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 }
1168 w = PySys_GetObject("ps2");
1169 if (w != NULL) {
1170 w = PyObject_Str(w);
1171 if (w == NULL)
1172 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001173 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +00001175 if (ps2 == NULL) {
1176 PyErr_Clear();
1177 ps2 = "";
1178 }
1179 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 }
1181 arena = PyArena_New();
1182 if (arena == NULL) {
1183 Py_XDECREF(v);
1184 Py_XDECREF(w);
1185 Py_XDECREF(oenc);
1186 return -1;
1187 }
1188 mod = PyParser_ASTFromFile(fp, filename, enc,
1189 Py_single_input, ps1, ps2,
1190 flags, &errcode, arena);
1191 Py_XDECREF(v);
1192 Py_XDECREF(w);
1193 Py_XDECREF(oenc);
1194 if (mod == NULL) {
1195 PyArena_Free(arena);
1196 if (errcode == E_EOF) {
1197 PyErr_Clear();
1198 return E_EOF;
1199 }
1200 PyErr_Print();
1201 return -1;
1202 }
1203 m = PyImport_AddModule("__main__");
1204 if (m == NULL) {
1205 PyArena_Free(arena);
1206 return -1;
1207 }
1208 d = PyModule_GetDict(m);
1209 v = run_mod(mod, filename, d, d, flags, arena);
1210 PyArena_Free(arena);
1211 flush_io();
1212 if (v == NULL) {
1213 PyErr_Print();
1214 return -1;
1215 }
1216 Py_DECREF(v);
1217 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001218}
1219
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001220/* Check whether a file maybe a pyc file: Look at the extension,
1221 the file type, and, if we may close it, at the first few bytes. */
1222
1223static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001224maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1227 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 /* Only look into the file if we are allowed to close it, since
1230 it then should also be seekable. */
1231 if (closeit) {
1232 /* Read only two bytes of the magic. If the file was opened in
1233 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1234 be read as they are on disk. */
1235 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1236 unsigned char buf[2];
1237 /* Mess: In case of -x, the stream is NOT at its start now,
1238 and ungetc() was used to push back the first newline,
1239 which makes the current stream position formally undefined,
1240 and a x-platform nightmare.
1241 Unfortunately, we have no direct way to know whether -x
1242 was specified. So we use a terrible hack: if the current
1243 stream position is not 0, we assume -x was specified, and
1244 give up. Bug 132850 on SourceForge spells out the
1245 hopelessness of trying anything else (fseek and ftell
1246 don't work predictably x-platform for text-mode files).
1247 */
1248 int ispyc = 0;
1249 if (ftell(fp) == 0) {
1250 if (fread(buf, 1, 2, fp) == 2 &&
1251 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1252 ispyc = 1;
1253 rewind(fp);
1254 }
1255 return ispyc;
1256 }
1257 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001258}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001259
Guido van Rossum0df002c2000-08-27 19:21:52 +00001260int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001261PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 PyObject *m, *d, *v;
1265 const char *ext;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001266 int set_file_name = 0, ret;
1267 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 m = PyImport_AddModule("__main__");
1270 if (m == NULL)
1271 return -1;
1272 d = PyModule_GetDict(m);
1273 if (PyDict_GetItemString(d, "__file__") == NULL) {
1274 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001275 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 if (f == NULL)
1277 return -1;
1278 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1279 Py_DECREF(f);
1280 return -1;
1281 }
Barry Warsaw916048d2011-09-20 14:45:44 -04001282 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1283 Py_DECREF(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 return -1;
Barry Warsaw916048d2011-09-20 14:45:44 -04001285 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 set_file_name = 1;
1287 Py_DECREF(f);
1288 }
1289 len = strlen(filename);
1290 ext = filename + len - (len > 4 ? 4 : 0);
1291 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1292 /* Try to run a pyc file. First, re-open in binary */
1293 if (closeit)
1294 fclose(fp);
1295 if ((fp = fopen(filename, "rb")) == NULL) {
1296 fprintf(stderr, "python: Can't reopen .pyc file\n");
1297 ret = -1;
1298 goto done;
1299 }
1300 /* Turn on optimization if a .pyo file is given */
1301 if (strcmp(ext, ".pyo") == 0)
1302 Py_OptimizeFlag = 1;
1303 v = run_pyc_file(fp, filename, d, d, flags);
1304 } else {
1305 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1306 closeit, flags);
1307 }
1308 flush_io();
1309 if (v == NULL) {
1310 PyErr_Print();
1311 ret = -1;
1312 goto done;
1313 }
1314 Py_DECREF(v);
1315 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001316 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1318 PyErr_Clear();
1319 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001320}
1321
1322int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001323PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 PyObject *m, *d, *v;
1326 m = PyImport_AddModule("__main__");
1327 if (m == NULL)
1328 return -1;
1329 d = PyModule_GetDict(m);
1330 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1331 if (v == NULL) {
1332 PyErr_Print();
1333 return -1;
1334 }
1335 Py_DECREF(v);
1336 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001337}
1338
Barry Warsaw035574d1997-08-29 22:07:17 +00001339static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001340parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 long hold;
1344 PyObject *v;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001345 _Py_IDENTIFIER(msg);
1346 _Py_IDENTIFIER(filename);
1347 _Py_IDENTIFIER(lineno);
1348 _Py_IDENTIFIER(offset);
1349 _Py_IDENTIFIER(text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 /* old style errors */
1352 if (PyTuple_Check(err))
1353 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1354 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 /* new style errors. `err' is an instance */
Barry Warsaw035574d1997-08-29 22:07:17 +00001357
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001358 if (! (v = _PyObject_GetAttrId(err, &PyId_msg)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 goto finally;
1360 *message = v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001361
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001362 if (!(v = _PyObject_GetAttrId(err, &PyId_filename)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 goto finally;
1364 if (v == Py_None)
1365 *filename = NULL;
1366 else if (! (*filename = _PyUnicode_AsString(v)))
1367 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 Py_DECREF(v);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001370 if (!(v = _PyObject_GetAttrId(err, &PyId_lineno)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 goto finally;
1372 hold = PyLong_AsLong(v);
1373 Py_DECREF(v);
1374 v = NULL;
1375 if (hold < 0 && PyErr_Occurred())
1376 goto finally;
1377 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001378
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001379 if (!(v = _PyObject_GetAttrId(err, &PyId_offset)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 goto finally;
1381 if (v == Py_None) {
1382 *offset = -1;
1383 Py_DECREF(v);
1384 v = NULL;
1385 } else {
1386 hold = PyLong_AsLong(v);
1387 Py_DECREF(v);
1388 v = NULL;
1389 if (hold < 0 && PyErr_Occurred())
1390 goto finally;
1391 *offset = (int)hold;
1392 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001393
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001394 if (!(v = _PyObject_GetAttrId(err, &PyId_text)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 goto finally;
1396 if (v == Py_None)
1397 *text = NULL;
1398 else if (!PyUnicode_Check(v) ||
1399 !(*text = _PyUnicode_AsString(v)))
1400 goto finally;
1401 Py_DECREF(v);
1402 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001403
1404finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 Py_XDECREF(v);
1406 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001407}
1408
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001409void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001410PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001413}
1414
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001415static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001416print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 char *nl;
1419 if (offset >= 0) {
Benjamin Petersona95e9772010-10-29 03:28:14 +00001420 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1421 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 for (;;) {
1423 nl = strchr(text, '\n');
1424 if (nl == NULL || nl-text >= offset)
1425 break;
1426 offset -= (int)(nl+1-text);
1427 text = nl+1;
1428 }
1429 while (*text == ' ' || *text == '\t') {
1430 text++;
1431 offset--;
1432 }
1433 }
1434 PyFile_WriteString(" ", f);
1435 PyFile_WriteString(text, f);
1436 if (*text == '\0' || text[strlen(text)-1] != '\n')
1437 PyFile_WriteString("\n", f);
1438 if (offset == -1)
1439 return;
1440 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +00001441 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001444}
1445
Guido van Rossum66e8e862001-03-23 17:54:43 +00001446static void
1447handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 PyObject *exception, *value, *tb;
1450 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 if (Py_InspectFlag)
1453 /* Don't exit if -i flag was given. This flag is set to 0
1454 * when entering interactive mode for inspecting. */
1455 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 PyErr_Fetch(&exception, &value, &tb);
1458 fflush(stdout);
1459 if (value == NULL || value == Py_None)
1460 goto done;
1461 if (PyExceptionInstance_Check(value)) {
1462 /* The error code should be in the `code' attribute. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001463 _Py_IDENTIFIER(code);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001464 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 if (code) {
1466 Py_DECREF(value);
1467 value = code;
1468 if (value == Py_None)
1469 goto done;
1470 }
1471 /* If we failed to dig out the 'code' attribute,
1472 just let the else clause below print the error. */
1473 }
1474 if (PyLong_Check(value))
1475 exitcode = (int)PyLong_AsLong(value);
1476 else {
Victor Stinnere9fb3192010-05-17 08:58:51 +00001477 PyObject *sys_stderr = PySys_GetObject("stderr");
Victor Stinner7126dbc2010-05-21 23:45:42 +00001478 if (sys_stderr != NULL && sys_stderr != Py_None) {
1479 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1480 } else {
1481 PyObject_Print(value, stderr, Py_PRINT_RAW);
1482 fflush(stderr);
1483 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 PySys_WriteStderr("\n");
1485 exitcode = 1;
1486 }
Tim Peterscf615b52003-04-19 18:47:02 +00001487 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 /* Restore and clear the exception info, in order to properly decref
1489 * the exception, value, and traceback. If we just exit instead,
1490 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1491 * some finalizers from running.
1492 */
1493 PyErr_Restore(exception, value, tb);
1494 PyErr_Clear();
1495 Py_Exit(exitcode);
1496 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001497}
1498
1499void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001500PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1505 handle_system_exit();
1506 }
1507 PyErr_Fetch(&exception, &v, &tb);
1508 if (exception == NULL)
1509 return;
1510 PyErr_NormalizeException(&exception, &v, &tb);
1511 if (tb == NULL) {
1512 tb = Py_None;
1513 Py_INCREF(tb);
1514 }
1515 PyException_SetTraceback(v, tb);
1516 if (exception == NULL)
1517 return;
1518 /* Now we know v != NULL too */
1519 if (set_sys_last_vars) {
1520 PySys_SetObject("last_type", exception);
1521 PySys_SetObject("last_value", v);
1522 PySys_SetObject("last_traceback", tb);
1523 }
1524 hook = PySys_GetObject("excepthook");
1525 if (hook) {
1526 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1527 PyObject *result = PyEval_CallObject(hook, args);
1528 if (result == NULL) {
1529 PyObject *exception2, *v2, *tb2;
1530 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1531 handle_system_exit();
1532 }
1533 PyErr_Fetch(&exception2, &v2, &tb2);
1534 PyErr_NormalizeException(&exception2, &v2, &tb2);
1535 /* It should not be possible for exception2 or v2
1536 to be NULL. However PyErr_Display() can't
1537 tolerate NULLs, so just be safe. */
1538 if (exception2 == NULL) {
1539 exception2 = Py_None;
1540 Py_INCREF(exception2);
1541 }
1542 if (v2 == NULL) {
1543 v2 = Py_None;
1544 Py_INCREF(v2);
1545 }
1546 fflush(stdout);
1547 PySys_WriteStderr("Error in sys.excepthook:\n");
1548 PyErr_Display(exception2, v2, tb2);
1549 PySys_WriteStderr("\nOriginal exception was:\n");
1550 PyErr_Display(exception, v, tb);
1551 Py_DECREF(exception2);
1552 Py_DECREF(v2);
1553 Py_XDECREF(tb2);
1554 }
1555 Py_XDECREF(result);
1556 Py_XDECREF(args);
1557 } else {
1558 PySys_WriteStderr("sys.excepthook is missing\n");
1559 PyErr_Display(exception, v, tb);
1560 }
1561 Py_XDECREF(exception);
1562 Py_XDECREF(v);
1563 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001564}
1565
Benjamin Petersone6528212008-07-15 15:32:09 +00001566static void
1567print_exception(PyObject *f, PyObject *value)
1568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 int err = 0;
1570 PyObject *type, *tb;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001571 _Py_IDENTIFIER(print_file_and_line);
Benjamin Petersone6528212008-07-15 15:32:09 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 if (!PyExceptionInstance_Check(value)) {
1574 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1575 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1576 PyFile_WriteString(" found\n", f);
1577 return;
1578 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 Py_INCREF(value);
1581 fflush(stdout);
1582 type = (PyObject *) Py_TYPE(value);
1583 tb = PyException_GetTraceback(value);
1584 if (tb && tb != Py_None)
1585 err = PyTraceBack_Print(tb, f);
1586 if (err == 0 &&
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001587 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 {
1589 PyObject *message;
1590 const char *filename, *text;
1591 int lineno, offset;
1592 if (!parse_syntax_error(value, &message, &filename,
1593 &lineno, &offset, &text))
1594 PyErr_Clear();
1595 else {
1596 char buf[10];
1597 PyFile_WriteString(" File \"", f);
1598 if (filename == NULL)
1599 PyFile_WriteString("<string>", f);
1600 else
1601 PyFile_WriteString(filename, f);
1602 PyFile_WriteString("\", line ", f);
1603 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1604 PyFile_WriteString(buf, f);
1605 PyFile_WriteString("\n", f);
1606 if (text != NULL)
1607 print_error_text(f, offset, text);
1608 Py_DECREF(value);
1609 value = message;
1610 /* Can't be bothered to check all those
1611 PyFile_WriteString() calls */
1612 if (PyErr_Occurred())
1613 err = -1;
1614 }
1615 }
1616 if (err) {
1617 /* Don't do anything else */
1618 }
1619 else {
1620 PyObject* moduleName;
1621 char* className;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001622 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 assert(PyExceptionClass_Check(type));
1624 className = PyExceptionClass_Name(type);
1625 if (className != NULL) {
1626 char *dot = strrchr(className, '.');
1627 if (dot != NULL)
1628 className = dot+1;
1629 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001630
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001631 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1633 {
Victor Stinner13b21bd2011-05-26 14:25:13 +02001634 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 err = PyFile_WriteString("<unknown>", f);
1636 }
1637 else {
1638 char* modstr = _PyUnicode_AsString(moduleName);
1639 if (modstr && strcmp(modstr, "builtins"))
1640 {
1641 err = PyFile_WriteString(modstr, f);
1642 err += PyFile_WriteString(".", f);
1643 }
1644 Py_DECREF(moduleName);
1645 }
1646 if (err == 0) {
1647 if (className == NULL)
1648 err = PyFile_WriteString("<unknown>", f);
1649 else
1650 err = PyFile_WriteString(className, f);
1651 }
1652 }
1653 if (err == 0 && (value != Py_None)) {
1654 PyObject *s = PyObject_Str(value);
1655 /* only print colon if the str() of the
1656 object is not the empty string
1657 */
1658 if (s == NULL)
1659 err = -1;
1660 else if (!PyUnicode_Check(s) ||
Victor Stinnere251d6d2011-11-20 19:20:00 +01001661 PyUnicode_GetLength(s) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 err = PyFile_WriteString(": ", f);
1663 if (err == 0)
1664 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1665 Py_XDECREF(s);
1666 }
1667 /* try to write a newline in any case */
1668 err += PyFile_WriteString("\n", f);
1669 Py_XDECREF(tb);
1670 Py_DECREF(value);
1671 /* If an error happened here, don't show it.
1672 XXX This is wrong, but too many callers rely on this behavior. */
1673 if (err != 0)
1674 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001675}
1676
1677static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 "\nThe above exception was the direct cause "
1679 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001680
1681static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 "\nDuring handling of the above exception, "
1683 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001684
1685static void
1686print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 int err = 0, res;
1689 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 if (seen != NULL) {
1692 /* Exception chaining */
1693 if (PySet_Add(seen, value) == -1)
1694 PyErr_Clear();
1695 else if (PyExceptionInstance_Check(value)) {
1696 cause = PyException_GetCause(value);
1697 context = PyException_GetContext(value);
1698 if (cause) {
1699 res = PySet_Contains(seen, cause);
1700 if (res == -1)
1701 PyErr_Clear();
1702 if (res == 0) {
1703 print_exception_recursive(
1704 f, cause, seen);
1705 err |= PyFile_WriteString(
1706 cause_message, f);
1707 }
1708 }
1709 else if (context) {
1710 res = PySet_Contains(seen, context);
1711 if (res == -1)
1712 PyErr_Clear();
1713 if (res == 0) {
1714 print_exception_recursive(
1715 f, context, seen);
1716 err |= PyFile_WriteString(
1717 context_message, f);
1718 }
1719 }
1720 Py_XDECREF(context);
1721 Py_XDECREF(cause);
1722 }
1723 }
1724 print_exception(f, value);
1725 if (err != 0)
1726 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001727}
1728
Thomas Wouters477c8d52006-05-27 19:21:47 +00001729void
1730PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 PyObject *seen;
1733 PyObject *f = PySys_GetObject("stderr");
1734 if (f == Py_None) {
1735 /* pass */
1736 }
1737 else if (f == NULL) {
1738 _PyObject_Dump(value);
1739 fprintf(stderr, "lost sys.stderr\n");
1740 }
1741 else {
1742 /* We choose to ignore seen being possibly NULL, and report
1743 at least the main exception (it could be a MemoryError).
1744 */
1745 seen = PySet_New(NULL);
1746 if (seen == NULL)
1747 PyErr_Clear();
1748 print_exception_recursive(f, value, seen);
1749 Py_XDECREF(seen);
1750 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001751}
1752
Guido van Rossum82598051997-03-05 00:20:32 +00001753PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001754PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 PyObject *ret = NULL;
1758 mod_ty mod;
1759 PyArena *arena = PyArena_New();
1760 if (arena == NULL)
1761 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1764 if (mod != NULL)
1765 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1766 PyArena_Free(arena);
1767 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001768}
1769
1770PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001771PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 PyObject *ret;
1775 mod_ty mod;
1776 PyArena *arena = PyArena_New();
1777 if (arena == NULL)
1778 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1781 flags, NULL, arena);
1782 if (closeit)
1783 fclose(fp);
1784 if (mod == NULL) {
1785 PyArena_Free(arena);
1786 return NULL;
1787 }
1788 ret = run_mod(mod, filename, globals, locals, flags, arena);
1789 PyArena_Free(arena);
1790 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001791}
1792
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001793static void
1794flush_io(void)
1795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 PyObject *f, *r;
1797 PyObject *type, *value, *traceback;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001798 _Py_IDENTIFIER(flush);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 /* Save the current exception */
1801 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 f = PySys_GetObject("stderr");
1804 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001805 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 if (r)
1807 Py_DECREF(r);
1808 else
1809 PyErr_Clear();
1810 }
1811 f = PySys_GetObject("stdout");
1812 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001813 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 if (r)
1815 Py_DECREF(r);
1816 else
1817 PyErr_Clear();
1818 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001821}
1822
Guido van Rossum82598051997-03-05 00:20:32 +00001823static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 PyCodeObject *co;
1828 PyObject *v;
1829 co = PyAST_Compile(mod, filename, flags, arena);
1830 if (co == NULL)
1831 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001832 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 Py_DECREF(co);
1834 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001835}
1836
Guido van Rossum82598051997-03-05 00:20:32 +00001837static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001838run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 PyCodeObject *co;
1842 PyObject *v;
1843 long magic;
1844 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 magic = PyMarshal_ReadLongFromFile(fp);
1847 if (magic != PyImport_GetMagicNumber()) {
1848 PyErr_SetString(PyExc_RuntimeError,
1849 "Bad magic number in .pyc file");
1850 return NULL;
1851 }
1852 (void) PyMarshal_ReadLongFromFile(fp);
1853 v = PyMarshal_ReadLastObjectFromFile(fp);
1854 fclose(fp);
1855 if (v == NULL || !PyCode_Check(v)) {
1856 Py_XDECREF(v);
1857 PyErr_SetString(PyExc_RuntimeError,
1858 "Bad code object in .pyc file");
1859 return NULL;
1860 }
1861 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001862 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 if (v && flags)
1864 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1865 Py_DECREF(co);
1866 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001867}
1868
Guido van Rossum82598051997-03-05 00:20:32 +00001869PyObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00001870Py_CompileStringExFlags(const char *str, const char *filename, int start,
1871 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 PyCodeObject *co;
1874 mod_ty mod;
1875 PyArena *arena = PyArena_New();
1876 if (arena == NULL)
1877 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1880 if (mod == NULL) {
1881 PyArena_Free(arena);
1882 return NULL;
1883 }
1884 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1885 PyObject *result = PyAST_mod2obj(mod);
1886 PyArena_Free(arena);
1887 return result;
1888 }
Georg Brandl8334fd92010-12-04 10:26:46 +00001889 co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 PyArena_Free(arena);
1891 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001892}
1893
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001894/* For use in Py_LIMITED_API */
1895#undef Py_CompileString
1896PyObject *
1897PyCompileString(const char *str, const char *filename, int start)
1898{
1899 return Py_CompileStringFlags(str, filename, start, NULL);
1900}
1901
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001902struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001903Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 struct symtable *st;
1906 mod_ty mod;
1907 PyCompilerFlags flags;
1908 PyArena *arena = PyArena_New();
1909 if (arena == NULL)
1910 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 flags.cf_flags = 0;
1913 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1914 if (mod == NULL) {
1915 PyArena_Free(arena);
1916 return NULL;
1917 }
1918 st = PySymtable_Build(mod, filename, 0);
1919 PyArena_Free(arena);
1920 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001921}
1922
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923/* Preferred access to parser is through AST. */
1924mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001925PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 mod_ty mod;
1929 PyCompilerFlags localflags;
1930 perrdetail err;
1931 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1934 &_PyParser_Grammar, start, &err,
1935 &iflags);
1936 if (flags == NULL) {
1937 localflags.cf_flags = 0;
1938 flags = &localflags;
1939 }
1940 if (n) {
1941 flags->cf_flags |= iflags & PyCF_MASK;
1942 mod = PyAST_FromNode(n, flags, filename, arena);
1943 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 }
1945 else {
1946 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001947 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001949 err_free(&err);
1950 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951}
1952
1953mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001954PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 int start, char *ps1,
1956 char *ps2, PyCompilerFlags *flags, int *errcode,
1957 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 mod_ty mod;
1960 PyCompilerFlags localflags;
1961 perrdetail err;
1962 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
1965 &_PyParser_Grammar,
1966 start, ps1, ps2, &err, &iflags);
1967 if (flags == NULL) {
1968 localflags.cf_flags = 0;
1969 flags = &localflags;
1970 }
1971 if (n) {
1972 flags->cf_flags |= iflags & PyCF_MASK;
1973 mod = PyAST_FromNode(n, flags, filename, arena);
1974 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 }
1976 else {
1977 err_input(&err);
1978 if (errcode)
1979 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02001980 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001982 err_free(&err);
1983 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984}
1985
Guido van Rossuma110aa61994-08-29 12:50:44 +00001986/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001987
Guido van Rossuma110aa61994-08-29 12:50:44 +00001988node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001989PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 perrdetail err;
1992 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1993 &_PyParser_Grammar,
1994 start, NULL, NULL, &err, flags);
1995 if (n == NULL)
1996 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001997 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002000}
2001
Guido van Rossuma110aa61994-08-29 12:50:44 +00002002/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002003
Guido van Rossuma110aa61994-08-29 12:50:44 +00002004node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002005PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00002006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 perrdetail err;
2008 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
2009 start, &err, flags);
2010 if (n == NULL)
2011 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002012 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00002014}
2015
2016node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002017PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 perrdetail err;
2021 node *n = PyParser_ParseStringFlagsFilename(str, filename,
2022 &_PyParser_Grammar, start, &err, flags);
2023 if (n == NULL)
2024 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002025 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00002027}
2028
2029node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002030PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00002033}
2034
Guido van Rossum66ebd912003-04-17 16:02:26 +00002035/* May want to move a more generalized form of this to parsetok.c or
2036 even parser modules. */
2037
2038void
Victor Stinner7f2fee32011-04-05 00:39:01 +02002039PyParser_ClearError(perrdetail *err)
2040{
2041 err_free(err);
2042}
2043
2044void
Guido van Rossum66ebd912003-04-17 16:02:26 +00002045PyParser_SetError(perrdetail *err)
2046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00002048}
2049
Victor Stinner7f2fee32011-04-05 00:39:01 +02002050static void
2051err_free(perrdetail *err)
2052{
2053 Py_CLEAR(err->filename);
2054}
2055
Guido van Rossuma110aa61994-08-29 12:50:44 +00002056/* Set the error appropriate to the given input error code (see errcode.h) */
2057
2058static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002059err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00002060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 PyObject *v, *w, *errtype, *errtext;
2062 PyObject *msg_obj = NULL;
2063 char *msg = NULL;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 errtype = PyExc_SyntaxError;
2066 switch (err->error) {
2067 case E_ERROR:
2068 return;
2069 case E_SYNTAX:
2070 errtype = PyExc_IndentationError;
2071 if (err->expected == INDENT)
2072 msg = "expected an indented block";
2073 else if (err->token == INDENT)
2074 msg = "unexpected indent";
2075 else if (err->token == DEDENT)
2076 msg = "unexpected unindent";
2077 else {
2078 errtype = PyExc_SyntaxError;
2079 msg = "invalid syntax";
2080 }
2081 break;
2082 case E_TOKEN:
2083 msg = "invalid token";
2084 break;
2085 case E_EOFS:
2086 msg = "EOF while scanning triple-quoted string literal";
2087 break;
2088 case E_EOLS:
2089 msg = "EOL while scanning string literal";
2090 break;
2091 case E_INTR:
2092 if (!PyErr_Occurred())
2093 PyErr_SetNone(PyExc_KeyboardInterrupt);
2094 goto cleanup;
2095 case E_NOMEM:
2096 PyErr_NoMemory();
2097 goto cleanup;
2098 case E_EOF:
2099 msg = "unexpected EOF while parsing";
2100 break;
2101 case E_TABSPACE:
2102 errtype = PyExc_TabError;
2103 msg = "inconsistent use of tabs and spaces in indentation";
2104 break;
2105 case E_OVERFLOW:
2106 msg = "expression too long";
2107 break;
2108 case E_DEDENT:
2109 errtype = PyExc_IndentationError;
2110 msg = "unindent does not match any outer indentation level";
2111 break;
2112 case E_TOODEEP:
2113 errtype = PyExc_IndentationError;
2114 msg = "too many levels of indentation";
2115 break;
2116 case E_DECODE: {
2117 PyObject *type, *value, *tb;
2118 PyErr_Fetch(&type, &value, &tb);
2119 msg = "unknown decode error";
2120 if (value != NULL)
2121 msg_obj = PyObject_Str(value);
2122 Py_XDECREF(type);
2123 Py_XDECREF(value);
2124 Py_XDECREF(tb);
2125 break;
2126 }
2127 case E_LINECONT:
2128 msg = "unexpected character after line continuation character";
2129 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00002130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 case E_IDENTIFIER:
2132 msg = "invalid character in identifier";
2133 break;
2134 default:
2135 fprintf(stderr, "error=%d\n", err->error);
2136 msg = "unknown parsing error";
2137 break;
2138 }
2139 /* err->text may not be UTF-8 in case of decoding errors.
2140 Explicitly convert to an object. */
2141 if (!err->text) {
2142 errtext = Py_None;
2143 Py_INCREF(Py_None);
2144 } else {
2145 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2146 "replace");
2147 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002148 v = Py_BuildValue("(OiiN)", err->filename,
2149 err->lineno, err->offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 if (v != NULL) {
2151 if (msg_obj)
2152 w = Py_BuildValue("(OO)", msg_obj, v);
2153 else
2154 w = Py_BuildValue("(sO)", msg, v);
2155 } else
2156 w = NULL;
2157 Py_XDECREF(v);
2158 PyErr_SetObject(errtype, w);
2159 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002160cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 Py_XDECREF(msg_obj);
2162 if (err->text != NULL) {
2163 PyObject_FREE(err->text);
2164 err->text = NULL;
2165 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002166}
2167
2168/* Print fatal error message and abort */
2169
2170void
Tim Peters7c321a82002-07-09 02:57:01 +00002171Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002172{
Victor Stinner024e37a2011-03-31 01:31:06 +02002173 const int fd = fileno(stderr);
2174 PyThreadState *tstate;
2175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 fprintf(stderr, "Fatal Python error: %s\n", msg);
2177 fflush(stderr); /* it helps in Windows debug build */
2178 if (PyErr_Occurred()) {
Victor Stinner55a5c782010-06-08 21:00:13 +00002179 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 }
Victor Stinner024e37a2011-03-31 01:31:06 +02002181 else {
2182 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
2183 if (tstate != NULL) {
2184 fputc('\n', stderr);
2185 fflush(stderr);
Victor Stinner7bba62f2011-05-07 12:43:00 +02002186 _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
Victor Stinner024e37a2011-03-31 01:31:06 +02002187 }
Victor Stinnerd727e232011-04-01 12:13:55 +02002188 _PyFaulthandler_Fini();
Victor Stinner024e37a2011-03-31 01:31:06 +02002189 }
2190
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002191#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 {
2193 size_t len = strlen(msg);
2194 WCHAR* buffer;
2195 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 /* Convert the message to wchar_t. This uses a simple one-to-one
2198 conversion, assuming that the this error message actually uses ASCII
2199 only. If this ceases to be true, we will have to convert. */
2200 buffer = alloca( (len+1) * (sizeof *buffer));
2201 for( i=0; i<=len; ++i)
2202 buffer[i] = msg[i];
2203 OutputDebugStringW(L"Fatal Python error: ");
2204 OutputDebugStringW(buffer);
2205 OutputDebugStringW(L"\n");
2206 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002207#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002209#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002210#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002212}
2213
2214/* Clean up and exit */
2215
Guido van Rossuma110aa61994-08-29 12:50:44 +00002216#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002217#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002218#endif
2219
Collin Winter670e6922007-03-21 02:57:17 +00002220static void (*pyexitfunc)(void) = NULL;
2221/* For the atexit module. */
2222void _Py_PyAtExit(void (*func)(void))
2223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002225}
2226
2227static void
2228call_py_exitfuncs(void)
2229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 if (pyexitfunc == NULL)
2231 return;
Collin Winter670e6922007-03-21 02:57:17 +00002232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 (*pyexitfunc)();
2234 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002235}
2236
Antoine Pitrou011bd622009-10-20 21:52:47 +00002237/* Wait until threading._shutdown completes, provided
2238 the threading module was imported in the first place.
2239 The shutdown routine will wait until all non-daemon
2240 "threading" threads have completed. */
2241static void
2242wait_for_thread_shutdown(void)
2243{
2244#ifdef WITH_THREAD
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002245 _Py_IDENTIFIER(_shutdown);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 PyObject *result;
2247 PyThreadState *tstate = PyThreadState_GET();
2248 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2249 "threading");
2250 if (threading == NULL) {
2251 /* threading not imported */
2252 PyErr_Clear();
2253 return;
2254 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002255 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 if (result == NULL) {
2257 PyErr_WriteUnraisable(threading);
2258 }
2259 else {
2260 Py_DECREF(result);
2261 }
2262 Py_DECREF(threading);
Antoine Pitrou011bd622009-10-20 21:52:47 +00002263#endif
2264}
2265
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002266#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002267static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002268static int nexitfuncs = 0;
2269
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002270int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 if (nexitfuncs >= NEXITFUNCS)
2273 return -1;
2274 exitfuncs[nexitfuncs++] = func;
2275 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002276}
2277
Guido van Rossumcc283f51997-08-05 02:22:03 +00002278static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002279call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 while (nexitfuncs > 0)
2282 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 fflush(stdout);
2285 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002286}
2287
2288void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002289Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002294}
2295
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002296static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002297initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002298{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002299#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002301#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002302#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002304#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002305#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002307#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002309}
2310
Guido van Rossum7433b121997-02-14 19:45:36 +00002311
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002312/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2313 *
2314 * All of the code in this function must only use async-signal-safe functions,
2315 * listed at `man 7 signal` or
2316 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2317 */
2318void
2319_Py_RestoreSignals(void)
2320{
2321#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 PyOS_setsig(SIGPIPE, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002323#endif
2324#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 PyOS_setsig(SIGXFZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002326#endif
2327#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 PyOS_setsig(SIGXFSZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002329#endif
2330}
2331
2332
Guido van Rossum7433b121997-02-14 19:45:36 +00002333/*
2334 * The file descriptor fd is considered ``interactive'' if either
2335 * a) isatty(fd) is TRUE, or
2336 * b) the -i flag was given, and the filename associated with
2337 * the descriptor is NULL or "<stdin>" or "???".
2338 */
2339int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002340Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 if (isatty((int)fileno(fp)))
2343 return 1;
2344 if (!Py_InteractiveFlag)
2345 return 0;
2346 return (filename == NULL) ||
2347 (strcmp(filename, "<stdin>") == 0) ||
2348 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002349}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002350
2351
Tim Petersd08e3822003-04-17 15:24:21 +00002352#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002353#if defined(WIN32) && defined(_MSC_VER)
2354
2355/* Stack checking for Microsoft C */
2356
2357#include <malloc.h>
2358#include <excpt.h>
2359
Fred Drakee8de31c2000-08-31 05:38:39 +00002360/*
2361 * Return non-zero when we run out of memory on the stack; zero otherwise.
2362 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002363int
Fred Drake399739f2000-08-31 05:52:44 +00002364PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 __try {
2367 /* alloca throws a stack overflow exception if there's
2368 not enough space left on the stack */
2369 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2370 return 0;
2371 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2372 EXCEPTION_EXECUTE_HANDLER :
2373 EXCEPTION_CONTINUE_SEARCH) {
2374 int errcode = _resetstkoflw();
2375 if (errcode == 0)
2376 {
2377 Py_FatalError("Could not reset the stack!");
2378 }
2379 }
2380 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002381}
2382
2383#endif /* WIN32 && _MSC_VER */
2384
2385/* Alternate implementations can be added here... */
2386
2387#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002388
2389
2390/* Wrappers around sigaction() or signal(). */
2391
2392PyOS_sighandler_t
2393PyOS_getsig(int sig)
2394{
2395#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 struct sigaction context;
2397 if (sigaction(sig, NULL, &context) == -1)
2398 return SIG_ERR;
2399 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002400#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002402/* Special signal handling for the secure CRT in Visual Studio 2005 */
2403#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 switch (sig) {
2405 /* Only these signals are valid */
2406 case SIGINT:
2407 case SIGILL:
2408 case SIGFPE:
2409 case SIGSEGV:
2410 case SIGTERM:
2411 case SIGBREAK:
2412 case SIGABRT:
2413 break;
2414 /* Don't call signal() with other values or it will assert */
2415 default:
2416 return SIG_ERR;
2417 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002418#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 handler = signal(sig, SIG_IGN);
2420 if (handler != SIG_ERR)
2421 signal(sig, handler);
2422 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002423#endif
2424}
2425
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002426/*
2427 * All of the code in this function must only use async-signal-safe functions,
2428 * listed at `man 7 signal` or
2429 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2430 */
Guido van Rossum6f256182000-09-16 16:32:19 +00002431PyOS_sighandler_t
2432PyOS_setsig(int sig, PyOS_sighandler_t handler)
2433{
2434#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 /* Some code in Modules/signalmodule.c depends on sigaction() being
2436 * used here if HAVE_SIGACTION is defined. Fix that if this code
2437 * changes to invalidate that assumption.
2438 */
2439 struct sigaction context, ocontext;
2440 context.sa_handler = handler;
2441 sigemptyset(&context.sa_mask);
2442 context.sa_flags = 0;
2443 if (sigaction(sig, &context, &ocontext) == -1)
2444 return SIG_ERR;
2445 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002446#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 PyOS_sighandler_t oldhandler;
2448 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002449#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002451#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002453#endif
2454}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455
2456/* Deprecated C API functions still provided for binary compatiblity */
2457
2458#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002459PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463}
2464
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002465#undef PyParser_SimpleParseString
2466PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467PyParser_SimpleParseString(const char *str, int start)
2468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002471
2472#undef PyRun_AnyFile
2473PyAPI_FUNC(int)
2474PyRun_AnyFile(FILE *fp, const char *name)
2475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002477}
2478
2479#undef PyRun_AnyFileEx
2480PyAPI_FUNC(int)
2481PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002484}
2485
2486#undef PyRun_AnyFileFlags
2487PyAPI_FUNC(int)
2488PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002491}
2492
2493#undef PyRun_File
2494PyAPI_FUNC(PyObject *)
2495PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002498}
2499
2500#undef PyRun_FileEx
2501PyAPI_FUNC(PyObject *)
2502PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002505}
2506
2507#undef PyRun_FileFlags
2508PyAPI_FUNC(PyObject *)
2509PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002513}
2514
2515#undef PyRun_SimpleFile
2516PyAPI_FUNC(int)
2517PyRun_SimpleFile(FILE *f, const char *p)
2518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002520}
2521
2522#undef PyRun_SimpleFileEx
2523PyAPI_FUNC(int)
2524PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002527}
2528
2529
2530#undef PyRun_String
2531PyAPI_FUNC(PyObject *)
2532PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002535}
2536
2537#undef PyRun_SimpleString
2538PyAPI_FUNC(int)
2539PyRun_SimpleString(const char *s)
2540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002542}
2543
2544#undef Py_CompileString
2545PyAPI_FUNC(PyObject *)
2546Py_CompileString(const char *str, const char *p, int s)
2547{
Georg Brandl8334fd92010-12-04 10:26:46 +00002548 return Py_CompileStringExFlags(str, p, s, NULL, -1);
2549}
2550
2551#undef Py_CompileStringFlags
2552PyAPI_FUNC(PyObject *)
2553Py_CompileStringFlags(const char *str, const char *p, int s,
2554 PyCompilerFlags *flags)
2555{
2556 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002557}
2558
2559#undef PyRun_InteractiveOne
2560PyAPI_FUNC(int)
2561PyRun_InteractiveOne(FILE *f, const char *p)
2562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002564}
2565
2566#undef PyRun_InteractiveLoop
2567PyAPI_FUNC(int)
2568PyRun_InteractiveLoop(FILE *f, const char *p)
2569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002571}
2572
2573#ifdef __cplusplus
2574}
2575#endif