blob: 460946804d2c222c066354059121351f8f523d81 [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 Stinner3cbf14b2011-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 *);
65static void initsigs(void);
Collin Winter670e6922007-03-21 02:57:17 +000066static void call_py_exitfuncs(void);
Antoine Pitrou011bd622009-10-20 21:52:47 +000067static void wait_for_thread_shutdown(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000068static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000069extern void _PyUnicode_Init(void);
70extern void _PyUnicode_Fini(void);
Guido van Rossumddefaf32007-01-14 03:31:43 +000071extern int _PyLong_Init(void);
72extern void PyLong_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000073
Mark Hammond8d98d2c2003-04-19 15:41:53 +000074#ifdef WITH_THREAD
75extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
76extern void _PyGILState_Fini(void);
77#endif /* WITH_THREAD */
78
Guido van Rossum82598051997-03-05 00:20:32 +000079int Py_DebugFlag; /* Needed by parser.c */
80int Py_VerboseFlag; /* Needed by import.c */
Georg Brandl8aa7e992010-12-28 18:30:18 +000081int Py_QuietFlag; /* Needed by sysmodule.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000082int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl0b2489e2011-05-15 08:49:12 +020083int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000084int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +000085int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Christian Heimes790c8232008-01-07 21:14:23 +000086int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000087int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000088int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000089int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Christian Heimes8dc226f2008-05-06 23:45:46 +000090int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Antoine Pitrou05608432009-01-09 18:53:14 +000091int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
Georg Brandl2daf6ae2012-02-20 19:54:16 +010092int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000093
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +020094PyThreadState *_Py_Finalizing = NULL;
95
Christian Heimes33fe8092008-04-13 13:53:33 +000096/* PyModule_GetWarningsModule is no longer necessary as of 2.6
97since _warnings is builtin. This API should not be used. */
98PyObject *
99PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +0000102}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000103
Guido van Rossum25ce5661997-08-02 03:10:38 +0000104static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000105
Thomas Wouters7e474022000-07-16 12:04:32 +0000106/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000107
108int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000109Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 return initialized;
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000112}
113
Guido van Rossum25ce5661997-08-02 03:10:38 +0000114/* Global initializations. Can be undone by Py_Finalize(). Don't
115 call this twice without an intervening Py_Finalize() call. When
116 initializations fail, a fatal error is issued and the function does
117 not return. On return, the first thread and interpreter state have
118 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000119
Guido van Rossum25ce5661997-08-02 03:10:38 +0000120 Locking: you must hold the interpreter lock while calling this.
121 (If the lock has not yet been initialized, that's equivalent to
122 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000123
Guido van Rossum25ce5661997-08-02 03:10:38 +0000124*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000125
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000126static int
127add_flag(int flag, const char *envs)
128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 int env = atoi(envs);
130 if (flag < env)
131 flag = env;
132 if (flag < 1)
133 flag = 1;
134 return flag;
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000135}
136
Christian Heimes5833a2f2008-10-30 21:40:04 +0000137static char*
Victor Stinner94908bb2010-08-18 21:23:25 +0000138get_codec_name(const char *encoding)
Christian Heimes5833a2f2008-10-30 21:40:04 +0000139{
Victor Stinner94908bb2010-08-18 21:23:25 +0000140 char *name_utf8, *name_str;
Victor Stinner386fe712010-05-19 00:34:15 +0000141 PyObject *codec, *name = NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000142
Victor Stinner94908bb2010-08-18 21:23:25 +0000143 codec = _PyCodec_Lookup(encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 if (!codec)
145 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 name = PyObject_GetAttrString(codec, "name");
148 Py_CLEAR(codec);
149 if (!name)
150 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000151
Victor Stinner94908bb2010-08-18 21:23:25 +0000152 name_utf8 = _PyUnicode_AsString(name);
Victor Stinner9c4efe52011-03-20 23:23:22 +0100153 if (name_utf8 == NULL)
Victor Stinner386fe712010-05-19 00:34:15 +0000154 goto error;
Victor Stinner94908bb2010-08-18 21:23:25 +0000155 name_str = strdup(name_utf8);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 Py_DECREF(name);
Victor Stinner94908bb2010-08-18 21:23:25 +0000157 if (name_str == NULL) {
158 PyErr_NoMemory();
159 return NULL;
160 }
161 return name_str;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000162
163error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 Py_XDECREF(codec);
Victor Stinner386fe712010-05-19 00:34:15 +0000165 Py_XDECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000167}
Victor Stinner94908bb2010-08-18 21:23:25 +0000168
169#if defined(HAVE_LANGINFO_H) && defined(CODESET)
170static char*
171get_codeset(void)
172{
173 char* codeset = nl_langinfo(CODESET);
174 if (!codeset || codeset[0] == '\0') {
175 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
176 return NULL;
177 }
178 return get_codec_name(codeset);
179}
Christian Heimes5833a2f2008-10-30 21:40:04 +0000180#endif
181
Guido van Rossuma027efa1997-05-05 20:56:21 +0000182void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000183Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 PyInterpreterState *interp;
186 PyThreadState *tstate;
187 PyObject *bimod, *sysmod, *pstderr;
188 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 if (initialized)
192 return;
193 initialized = 1;
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200194 _Py_Finalizing = NULL;
Tim Petersd08e3822003-04-17 15:24:21 +0000195
Andrew M. Kuchlingb2ceb3a2010-02-22 23:26:10 +0000196#if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 /* Set up the LC_CTYPE locale, so we can obtain
198 the locale's charset without having to switch
199 locales. */
200 setlocale(LC_CTYPE, "");
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000201#endif
202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
204 Py_DebugFlag = add_flag(Py_DebugFlag, p);
205 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
206 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
207 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
208 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
209 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
210 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100211 /* The variable is only tested for existence here; _PyRandom_Init will
212 check its value further. */
213 if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
214 Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
215
216 _PyRandom_Init();
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 interp = PyInterpreterState_New();
219 if (interp == NULL)
220 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 tstate = PyThreadState_New(interp);
223 if (tstate == NULL)
224 Py_FatalError("Py_Initialize: can't make first thread");
225 (void) PyThreadState_Swap(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000226
Victor Stinner6961bd62010-08-17 22:26:51 +0000227#ifdef WITH_THREAD
Antoine Pitroub0b384b2010-09-20 20:13:48 +0000228 /* We can't call _PyEval_FiniThreads() in Py_Finalize because
229 destroying the GIL might fail when it is being referenced from
230 another running thread (see issue #9901).
231 Instead we destroy the previously created GIL here, which ensures
232 that we can call Py_Initialize / Py_Finalize multiple times. */
233 _PyEval_FiniThreads();
234
235 /* Auto-thread-state API */
Victor Stinner6961bd62010-08-17 22:26:51 +0000236 _PyGILState_Init(interp, tstate);
237#endif /* WITH_THREAD */
238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 if (!_PyFrame_Init())
242 Py_FatalError("Py_Initialize: can't init frames");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 if (!_PyLong_Init())
245 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (!PyByteArray_Init())
248 Py_FatalError("Py_Initialize: can't init bytearray");
Neal Norwitz6968b052007-02-27 19:02:19 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 _PyFloat_Init();
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 interp->modules = PyDict_New();
253 if (interp->modules == NULL)
254 Py_FatalError("Py_Initialize: can't make modules dictionary");
255 interp->modules_reloading = PyDict_New();
256 if (interp->modules_reloading == NULL)
257 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 /* Init Unicode implementation; relies on the codec registry */
260 _PyUnicode_Init();
Guido van Rossumc94044c2000-03-10 23:03:54 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 bimod = _PyBuiltin_Init();
263 if (bimod == NULL)
264 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Victor Stinner49d3f252010-10-17 01:24:53 +0000265 _PyImport_FixupBuiltin(bimod, "builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 interp->builtins = PyModule_GetDict(bimod);
267 if (interp->builtins == NULL)
268 Py_FatalError("Py_Initialize: can't initialize builtins dict");
269 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* initialize builtin exceptions */
272 _PyExc_Init();
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 sysmod = _PySys_Init();
275 if (sysmod == NULL)
276 Py_FatalError("Py_Initialize: can't initialize sys");
277 interp->sysdict = PyModule_GetDict(sysmod);
278 if (interp->sysdict == NULL)
279 Py_FatalError("Py_Initialize: can't initialize sys dict");
280 Py_INCREF(interp->sysdict);
Victor Stinner49d3f252010-10-17 01:24:53 +0000281 _PyImport_FixupBuiltin(sysmod, "sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 PySys_SetPath(Py_GetPath());
283 PyDict_SetItemString(interp->sysdict, "modules",
284 interp->modules);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 /* Set up a preliminary stderr printer until we have enough
287 infrastructure for the io module in place. */
288 pstderr = PyFile_NewStdPrinter(fileno(stderr));
289 if (pstderr == NULL)
290 Py_FatalError("Py_Initialize: can't set preliminary stderr");
291 PySys_SetObject("stderr", pstderr);
292 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000293 Py_DECREF(pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 _PyImport_Init();
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 _PyImportHooks_Init();
Just van Rossum52e14d62002-12-30 22:08:05 +0000298
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000299 /* Initialize _warnings. */
300 _PyWarnings_Init();
301
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000302 _PyTime_Init();
303
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200304 if (initfsencoding(interp) < 0)
305 Py_FatalError("Py_Initialize: unable to load the file system codec");
Martin v. Löwis011e8422009-05-05 04:43:17 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 if (install_sigs)
308 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 initmain(); /* Module __main__ */
311 if (initstdio() < 0)
312 Py_FatalError(
313 "Py_Initialize: can't initialize sys standard streams");
314
Antoine Pitroucf9f9802010-11-10 13:55:25 +0000315 /* Initialize warnings. */
316 if (PySys_HasWarnOptions()) {
317 PyObject *warnings_module = PyImport_ImportModule("warnings");
318 if (warnings_module == NULL) {
319 fprintf(stderr, "'import warnings' failed; traceback:\n");
320 PyErr_Print();
321 }
322 Py_XDECREF(warnings_module);
323 }
324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (!Py_NoSiteFlag)
326 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000327}
328
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000329void
330Py_Initialize(void)
331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 Py_InitializeEx(1);
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000333}
334
335
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000336#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000337extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000338#endif
339
Guido van Rossume8432ac2007-07-09 15:04:50 +0000340/* Flush stdout and stderr */
341
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100342static int
343file_is_closed(PyObject *fobj)
344{
345 int r;
346 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
347 if (tmp == NULL) {
348 PyErr_Clear();
349 return 0;
350 }
351 r = PyObject_IsTrue(tmp);
352 Py_DECREF(tmp);
353 if (r < 0)
354 PyErr_Clear();
355 return r > 0;
356}
357
Neal Norwitz2bad9702007-08-27 06:19:22 +0000358static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000359flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 PyObject *fout = PySys_GetObject("stdout");
362 PyObject *ferr = PySys_GetObject("stderr");
363 PyObject *tmp;
Guido van Rossume8432ac2007-07-09 15:04:50 +0000364
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100365 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 tmp = PyObject_CallMethod(fout, "flush", "");
367 if (tmp == NULL)
Antoine Pitroubddc9fe2010-08-08 20:46:42 +0000368 PyErr_WriteUnraisable(fout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 else
370 Py_DECREF(tmp);
371 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000372
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100373 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 tmp = PyObject_CallMethod(ferr, "flush", "");
375 if (tmp == NULL)
376 PyErr_Clear();
377 else
378 Py_DECREF(tmp);
379 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000380}
381
Guido van Rossum25ce5661997-08-02 03:10:38 +0000382/* Undo the effect of Py_Initialize().
383
384 Beware: if multiple interpreter and/or thread states exist, these
385 are not wiped out; only the current thread and interpreter state
386 are deleted. But since everything else is deleted, those other
387 interpreter and thread states should no longer be used.
388
389 (XXX We should do better, e.g. wipe out all interpreters and
390 threads.)
391
392 Locking: as above.
393
394*/
395
396void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000397Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 PyInterpreterState *interp;
400 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (!initialized)
403 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 wait_for_thread_shutdown();
Antoine Pitrou011bd622009-10-20 21:52:47 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* The interpreter is still entirely intact at this point, and the
408 * exit funcs may be relying on that. In particular, if some thread
409 * or exit func is still waiting to do an import, the import machinery
410 * expects Py_IsInitialized() to return true. So don't say the
411 * interpreter is uninitialized until after the exit funcs have run.
412 * Note that Threading.py uses an exit func to do a join on all the
413 * threads created thru it, so this also protects pending imports in
414 * the threads created via Threading.
415 */
416 call_py_exitfuncs();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 /* Get current thread state and interpreter pointer */
419 tstate = PyThreadState_GET();
420 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000421
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200422 /* Remaining threads (e.g. daemon threads) will automatically exit
423 after taking the GIL (in PyEval_RestoreThread()). */
424 _Py_Finalizing = tstate;
425 initialized = 0;
426
427 /* Flush stdout+stderr */
428 flush_std_files();
429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 /* Disable signal handling */
431 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 /* Clear type lookup cache */
434 PyType_ClearCache();
Christian Heimes26855632008-01-27 23:50:43 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 /* Collect garbage. This may call finalizers; it's nice to call these
437 * before all modules are destroyed.
438 * XXX If a __del__ or weakref callback is triggered here, and tries to
439 * XXX import a module, bad things can happen, because Python no
440 * XXX longer believes it's initialized.
441 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
442 * XXX is easy to provoke that way. I've also seen, e.g.,
443 * XXX Exception exceptions.ImportError: 'No module named sha'
444 * XXX in <function callback at 0x008F5718> ignored
445 * XXX but I'm unclear on exactly how that one happens. In any case,
446 * XXX I haven't seen a real-life report of either of these.
447 */
448 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000449#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 /* With COUNT_ALLOCS, it helps to run GC multiple times:
451 each collection might release some types from the type
452 list, so they become garbage. */
453 while (PyGC_Collect() > 0)
454 /* nothing */;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455#endif
Antoine Pitrou696e0352010-08-08 22:18:46 +0000456 /* We run this while most interpreter state is still alive, so that
457 debug information can be printed out */
458 _PyGC_Fini();
Guido van Rossume13ddc92003-04-17 17:29:22 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 /* Destroy all modules */
461 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 /* Flush stdout+stderr (again, in case more was printed) */
464 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 /* Collect final garbage. This disposes of cycles created by
467 * new-style class definitions, for example.
468 * XXX This is disabled because it caused too many problems. If
469 * XXX a __del__ or weakref callback triggers here, Python code has
470 * XXX a hard time running, because even the sys module has been
471 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
472 * XXX One symptom is a sequence of information-free messages
473 * XXX coming from threads (if a __del__ or callback is invoked,
474 * XXX other threads can execute too, and any exception they encounter
475 * XXX triggers a comedy of errors as subsystem after subsystem
476 * XXX fails to find what it *expects* to find in sys to help report
477 * XXX the exception and consequent unexpected failures). I've also
478 * XXX seen segfaults then, after adding print statements to the
479 * XXX Python code getting called.
480 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000481#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000483#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
486 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000489#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000491#endif
492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000494
Tim Peters9cf25ce2003-04-17 15:21:01 +0000495#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 /* Display all objects still alive -- this can invoke arbitrary
497 * __repr__ overrides, so requires a mostly-intact interpreter.
498 * Alas, a lot of stuff may still be alive now that will be cleaned
499 * up later.
500 */
501 if (Py_GETENV("PYTHONDUMPREFS"))
502 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000503#endif /* Py_TRACE_REFS */
504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 /* Clear interpreter state */
506 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 /* Now we decref the exception classes. After this point nothing
509 can raise an exception. That's okay, because each Fini() method
510 below has been checked to make sure no exceptions are ever
511 raised.
512 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 /* Cleanup auto-thread-state */
Christian Heimes7d2ff882007-11-30 14:35:04 +0000517#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 _PyGILState_Fini();
Christian Heimes7d2ff882007-11-30 14:35:04 +0000519#endif /* WITH_THREAD */
520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 /* Delete current thread */
522 PyThreadState_Swap(NULL);
523 PyInterpreterState_Delete(interp);
Barry Warsawf242aa02000-05-25 23:09:49 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 /* Sundry finalizers */
526 PyMethod_Fini();
527 PyFrame_Fini();
528 PyCFunction_Fini();
529 PyTuple_Fini();
530 PyList_Fini();
531 PySet_Fini();
532 PyBytes_Fini();
533 PyByteArray_Fini();
534 PyLong_Fini();
535 PyFloat_Fini();
536 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 /* Cleanup Unicode implementation */
539 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 /* reset file system default encoding */
Victor Stinnerb744ba12010-05-15 12:27:16 +0000542 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 free((char*)Py_FileSystemDefaultEncoding);
544 Py_FileSystemDefaultEncoding = NULL;
545 }
Christian Heimesc8967002007-11-30 10:18:26 +0000546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 /* XXX Still allocated:
548 - various static ad-hoc pointers to interned strings
549 - int and float free list blocks
550 - whatever various modules and libraries allocate
551 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000554
Tim Peters269b2a62003-04-17 19:52:29 +0000555#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* Display addresses (& refcnts) of all objects still alive.
557 * An address can be used to find the repr of the object, printed
558 * above by _Py_PrintReferences.
559 */
560 if (Py_GETENV("PYTHONDUMPREFS"))
561 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000562#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000563#ifdef PYMALLOC_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 if (Py_GETENV("PYTHONMALLOCSTATS"))
565 _PyObject_DebugMallocStats();
Tim Peters0e871182002-04-13 08:29:14 +0000566#endif
567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000569}
570
571/* Create and initialize a new interpreter and thread, and return the
572 new thread. This requires that Py_Initialize() has been called
573 first.
574
575 Unsuccessful initialization yields a NULL pointer. Note that *no*
576 exception information is available even in this case -- the
577 exception information is held in the thread, and there is no
578 thread.
579
580 Locking: as above.
581
582*/
583
584PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000585Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 PyInterpreterState *interp;
588 PyThreadState *tstate, *save_tstate;
589 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 if (!initialized)
592 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 interp = PyInterpreterState_New();
595 if (interp == NULL)
596 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 tstate = PyThreadState_New(interp);
599 if (tstate == NULL) {
600 PyInterpreterState_Delete(interp);
601 return NULL;
602 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 interp->modules = PyDict_New();
609 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000610
Victor Stinner49d3f252010-10-17 01:24:53 +0000611 bimod = _PyImport_FindBuiltin("builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 if (bimod != NULL) {
613 interp->builtins = PyModule_GetDict(bimod);
614 if (interp->builtins == NULL)
615 goto handle_error;
616 Py_INCREF(interp->builtins);
617 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 /* initialize builtin exceptions */
620 _PyExc_Init();
Christian Heimes6a27efa2008-10-30 21:48:26 +0000621
Victor Stinner49d3f252010-10-17 01:24:53 +0000622 sysmod = _PyImport_FindBuiltin("sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 if (bimod != NULL && sysmod != NULL) {
624 PyObject *pstderr;
625 interp->sysdict = PyModule_GetDict(sysmod);
626 if (interp->sysdict == NULL)
627 goto handle_error;
628 Py_INCREF(interp->sysdict);
629 PySys_SetPath(Py_GetPath());
630 PyDict_SetItemString(interp->sysdict, "modules",
631 interp->modules);
632 /* Set up a preliminary stderr printer until we have enough
633 infrastructure for the io module in place. */
634 pstderr = PyFile_NewStdPrinter(fileno(stderr));
635 if (pstderr == NULL)
636 Py_FatalError("Py_Initialize: can't set preliminary stderr");
637 PySys_SetObject("stderr", pstderr);
638 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000639 Py_DECREF(pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 _PyImportHooks_Init();
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200642
643 if (initfsencoding(interp) < 0)
644 goto handle_error;
645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 if (initstdio() < 0)
647 Py_FatalError(
648 "Py_Initialize: can't initialize sys standard streams");
649 initmain();
650 if (!Py_NoSiteFlag)
651 initsite();
652 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 if (!PyErr_Occurred())
655 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000656
Thomas Wouters89f507f2006-12-13 04:49:30 +0000657handle_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000659
Victor Stinner11889352011-04-27 00:20:27 +0200660 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 PyThreadState_Clear(tstate);
662 PyThreadState_Swap(save_tstate);
663 PyThreadState_Delete(tstate);
664 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000667}
668
669/* Delete an interpreter and its last thread. This requires that the
670 given thread state is current, that the thread has no remaining
671 frames, and that it is its interpreter's only remaining thread.
672 It is a fatal error to violate these constraints.
673
674 (Py_Finalize() doesn't have these constraints -- it zaps
675 everything, regardless.)
676
677 Locking: as above.
678
679*/
680
681void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000682Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (tstate != PyThreadState_GET())
687 Py_FatalError("Py_EndInterpreter: thread is not current");
688 if (tstate->frame != NULL)
689 Py_FatalError("Py_EndInterpreter: thread still has a frame");
690 if (tstate != interp->tstate_head || tstate->next != NULL)
691 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyImport_Cleanup();
694 PyInterpreterState_Clear(interp);
695 PyThreadState_Swap(NULL);
696 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000697}
698
Antoine Pitrou01cca5e2012-07-05 20:56:30 +0200699#ifdef MS_WINDOWS
Martin v. Löwis790465f2008-04-05 20:41:37 +0000700static wchar_t *progname = L"python";
Antoine Pitrou01cca5e2012-07-05 20:56:30 +0200701#else
702static wchar_t *progname = L"python3";
703#endif
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000704
705void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000706Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 if (pn && *pn)
709 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000710}
711
Martin v. Löwis790465f2008-04-05 20:41:37 +0000712wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000713Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000716}
717
Martin v. Löwis790465f2008-04-05 20:41:37 +0000718static wchar_t *default_home = NULL;
719static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000720
721void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000722Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000725}
726
Martin v. Löwis790465f2008-04-05 20:41:37 +0000727wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000728Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 wchar_t *home = default_home;
731 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
732 char* chome = Py_GETENV("PYTHONHOME");
733 if (chome) {
734 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
735 if (r != (size_t)-1 && r <= PATH_MAX)
736 home = env_home;
737 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 }
740 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000741}
742
Guido van Rossum6135a871995-01-09 17:53:26 +0000743/* Create __main__ module */
744
745static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000746initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 PyObject *m, *d;
749 m = PyImport_AddModule("__main__");
750 if (m == NULL)
751 Py_FatalError("can't create __main__ module");
752 d = PyModule_GetDict(m);
753 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
754 PyObject *bimod = PyImport_ImportModule("builtins");
755 if (bimod == NULL ||
756 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
757 Py_FatalError("can't add __builtins__ to __main__");
758 Py_DECREF(bimod);
759 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000760}
761
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200762static int
763initfsencoding(PyInterpreterState *interp)
Victor Stinnerb744ba12010-05-15 12:27:16 +0000764{
765 PyObject *codec;
766#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94908bb2010-08-18 21:23:25 +0000767 char *codeset = NULL;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000768
Victor Stinner7f84ab52010-06-11 00:36:33 +0000769 if (Py_FileSystemDefaultEncoding == NULL) {
Victor Stinner8f6b6b02010-10-13 22:02:27 +0000770 /* On Unix, set the file system encoding according to the
771 user's preference, if the CODESET names a well-known
772 Python codec, and Py_FileSystemDefaultEncoding isn't
Victor Stinnere4743092010-10-19 00:05:51 +0000773 initialized by other means. */
Victor Stinner8f6b6b02010-10-13 22:02:27 +0000774 codeset = get_codeset();
Victor Stinnere4743092010-10-19 00:05:51 +0000775 if (codeset == NULL)
776 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
Victor Stinnerb744ba12010-05-15 12:27:16 +0000777
Victor Stinnere4743092010-10-19 00:05:51 +0000778 Py_FileSystemDefaultEncoding = codeset;
779 Py_HasFileSystemDefaultEncoding = 0;
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200780 interp->fscodec_initialized = 1;
781 return 0;
Victor Stinner7f84ab52010-06-11 00:36:33 +0000782 }
Victor Stinnerb744ba12010-05-15 12:27:16 +0000783#endif
784
785 /* the encoding is mbcs, utf-8 or ascii */
786 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
787 if (!codec) {
788 /* Such error can only occurs in critical situations: no more
789 * memory, import a module of the standard library failed,
790 * etc. */
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200791 return -1;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000792 }
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200793 Py_DECREF(codec);
794 interp->fscodec_initialized = 1;
795 return 0;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000796}
797
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000798/* Import the site module (not into __main__ though) */
799
800static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000801initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 PyObject *m;
804 m = PyImport_ImportModule("site");
805 if (m == NULL) {
806 PyErr_Print();
807 Py_Finalize();
808 exit(1);
809 }
810 else {
811 Py_DECREF(m);
812 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000813}
814
Antoine Pitrou05608432009-01-09 18:53:14 +0000815static PyObject*
816create_stdio(PyObject* io,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 int fd, int write_mode, char* name,
818 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +0000819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
821 const char* mode;
Victor Stinner02bfdb32011-02-23 12:10:23 +0000822 const char* newline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 PyObject *line_buffering;
824 int buffering, isatty;
Antoine Pitrou05608432009-01-09 18:53:14 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 /* stdin is always opened in buffered mode, first because it shouldn't
827 make a difference in common use cases, second because TextIOWrapper
828 depends on the presence of a read1() method which only exists on
829 buffered streams.
830 */
831 if (Py_UnbufferedStdioFlag && write_mode)
832 buffering = 0;
833 else
834 buffering = -1;
835 if (write_mode)
836 mode = "wb";
837 else
838 mode = "rb";
839 buf = PyObject_CallMethod(io, "open", "isiOOOi",
840 fd, mode, buffering,
841 Py_None, Py_None, Py_None, 0);
842 if (buf == NULL)
843 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 if (buffering) {
846 raw = PyObject_GetAttrString(buf, "raw");
847 if (raw == NULL)
848 goto error;
849 }
850 else {
851 raw = buf;
852 Py_INCREF(raw);
853 }
Antoine Pitrou05608432009-01-09 18:53:14 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 text = PyUnicode_FromString(name);
856 if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0)
857 goto error;
858 res = PyObject_CallMethod(raw, "isatty", "");
859 if (res == NULL)
860 goto error;
861 isatty = PyObject_IsTrue(res);
862 Py_DECREF(res);
863 if (isatty == -1)
864 goto error;
865 if (isatty || Py_UnbufferedStdioFlag)
866 line_buffering = Py_True;
867 else
868 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 Py_CLEAR(raw);
871 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +0000872
Victor Stinner02bfdb32011-02-23 12:10:23 +0000873 newline = "\n";
874#ifdef MS_WINDOWS
875 if (!write_mode) {
876 /* translate \r\n to \n for sys.stdin on Windows */
877 newline = NULL;
878 }
879#endif
880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO",
882 buf, encoding, errors,
Victor Stinner02bfdb32011-02-23 12:10:23 +0000883 newline, line_buffering);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 Py_CLEAR(buf);
885 if (stream == NULL)
886 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (write_mode)
889 mode = "w";
890 else
891 mode = "r";
892 text = PyUnicode_FromString(mode);
893 if (!text || PyObject_SetAttrString(stream, "mode", text) < 0)
894 goto error;
895 Py_CLEAR(text);
896 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +0000897
898error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 Py_XDECREF(buf);
900 Py_XDECREF(stream);
901 Py_XDECREF(text);
902 Py_XDECREF(raw);
903 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +0000904}
905
Antoine Pitrou11942a52011-11-28 19:08:36 +0100906static int
907is_valid_fd(int fd)
908{
909 int dummy_fd;
910 if (fd < 0 || !_PyVerify_fd(fd))
911 return 0;
912 dummy_fd = dup(fd);
913 if (dummy_fd < 0)
914 return 0;
915 close(dummy_fd);
916 return 1;
917}
918
Georg Brandl1a3284e2007-12-02 09:40:06 +0000919/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000920static int
921initstdio(void)
922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 PyObject *iomod = NULL, *wrapper;
924 PyObject *bimod = NULL;
925 PyObject *m;
926 PyObject *std = NULL;
927 int status = 0, fd;
928 PyObject * encoding_attr;
929 char *encoding = NULL, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 /* Hack to avoid a nasty recursion issue when Python is invoked
932 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
933 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
934 goto error;
935 }
936 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
939 goto error;
940 }
941 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 if (!(bimod = PyImport_ImportModule("builtins"))) {
944 goto error;
945 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 if (!(iomod = PyImport_ImportModule("io"))) {
948 goto error;
949 }
950 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
951 goto error;
952 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 /* Set builtins.open */
955 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000956 Py_DECREF(wrapper);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 goto error;
958 }
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000959 Py_DECREF(wrapper);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 encoding = Py_GETENV("PYTHONIOENCODING");
962 errors = NULL;
963 if (encoding) {
964 encoding = strdup(encoding);
965 errors = strchr(encoding, ':');
966 if (errors) {
967 *errors = '\0';
968 errors++;
969 }
970 }
Martin v. Löwis0f599892008-06-02 11:13:03 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 /* Set sys.stdin */
973 fd = fileno(stdin);
974 /* Under some conditions stdin, stdout and stderr may not be connected
975 * and fileno() may point to an invalid file descriptor. For example
976 * GUI apps don't have valid standard streams by default.
977 */
Antoine Pitrou11942a52011-11-28 19:08:36 +0100978 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 std = Py_None;
980 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 }
982 else {
983 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
984 if (std == NULL)
985 goto error;
986 } /* if (fd < 0) */
987 PySys_SetObject("__stdin__", std);
988 PySys_SetObject("stdin", std);
989 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 /* Set sys.stdout */
992 fd = fileno(stdout);
Antoine Pitrou11942a52011-11-28 19:08:36 +0100993 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 std = Py_None;
995 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 }
997 else {
998 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
999 if (std == NULL)
1000 goto error;
1001 } /* if (fd < 0) */
1002 PySys_SetObject("__stdout__", std);
1003 PySys_SetObject("stdout", std);
1004 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001005
Guido van Rossum98297ee2007-11-06 21:34:58 +00001006#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 /* Set sys.stderr, replaces the preliminary stderr */
1008 fd = fileno(stderr);
Antoine Pitrou11942a52011-11-28 19:08:36 +01001009 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 std = Py_None;
1011 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 }
1013 else {
1014 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1015 if (std == NULL)
1016 goto error;
1017 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +00001018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 /* Same as hack above, pre-import stderr's codec to avoid recursion
1020 when import.c tries to write to stderr in verbose mode. */
1021 encoding_attr = PyObject_GetAttrString(std, "encoding");
1022 if (encoding_attr != NULL) {
1023 const char * encoding;
1024 encoding = _PyUnicode_AsString(encoding_attr);
1025 if (encoding != NULL) {
Antoine Pitrou2fabfac2012-01-18 15:14:46 +01001026 PyObject *codec_info = _PyCodec_Lookup(encoding);
1027 Py_XDECREF(codec_info);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 }
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +00001029 Py_DECREF(encoding_attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 }
1031 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +00001032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 PySys_SetObject("__stderr__", std);
1034 PySys_SetObject("stderr", std);
1035 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001036#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001039 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 status = -1;
1041 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 if (encoding)
1044 free(encoding);
1045 Py_XDECREF(bimod);
1046 Py_XDECREF(iomod);
1047 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001048}
1049
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001050/* Parse input from a file and execute it */
1051
1052int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001053PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 if (filename == NULL)
1057 filename = "???";
1058 if (Py_FdIsInteractive(fp, filename)) {
1059 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
1060 if (closeit)
1061 fclose(fp);
1062 return err;
1063 }
1064 else
1065 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001066}
1067
1068int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001069PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 PyObject *v;
1072 int ret;
1073 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 if (flags == NULL) {
1076 flags = &local_flags;
1077 local_flags.cf_flags = 0;
1078 }
1079 v = PySys_GetObject("ps1");
1080 if (v == NULL) {
1081 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
1082 Py_XDECREF(v);
1083 }
1084 v = PySys_GetObject("ps2");
1085 if (v == NULL) {
1086 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1087 Py_XDECREF(v);
1088 }
1089 for (;;) {
1090 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
1091 PRINT_TOTAL_REFS();
1092 if (ret == E_EOF)
1093 return 0;
1094 /*
1095 if (ret == E_NOMEM)
1096 return -1;
1097 */
1098 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001099}
1100
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001101/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001102static int PARSER_FLAGS(PyCompilerFlags *flags)
1103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 int parser_flags = 0;
1105 if (!flags)
1106 return 0;
1107 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1108 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1109 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1110 parser_flags |= PyPARSE_IGNORE_COOKIE;
1111 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1112 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1113 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001114}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001115
Thomas Wouters89f507f2006-12-13 04:49:30 +00001116#if 0
1117/* Keep an example of flags with future keyword support. */
1118#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1120 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1121 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1122 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001123#endif
1124
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001125int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001126PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 PyObject *m, *d, *v, *w, *oenc = NULL;
1129 mod_ty mod;
1130 PyArena *arena;
1131 char *ps1 = "", *ps2 = "", *enc = NULL;
1132 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 if (fp == stdin) {
1135 /* Fetch encoding from sys.stdin */
1136 v = PySys_GetObject("stdin");
1137 if (v == NULL || v == Py_None)
1138 return -1;
1139 oenc = PyObject_GetAttrString(v, "encoding");
1140 if (!oenc)
1141 return -1;
1142 enc = _PyUnicode_AsString(oenc);
Victor Stinner386fe712010-05-19 00:34:15 +00001143 if (enc == NULL)
1144 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 }
1146 v = PySys_GetObject("ps1");
1147 if (v != NULL) {
1148 v = PyObject_Str(v);
1149 if (v == NULL)
1150 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001151 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +00001153 if (ps1 == NULL) {
1154 PyErr_Clear();
1155 ps1 = "";
1156 }
1157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 }
1159 w = PySys_GetObject("ps2");
1160 if (w != NULL) {
1161 w = PyObject_Str(w);
1162 if (w == NULL)
1163 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001164 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +00001166 if (ps2 == NULL) {
1167 PyErr_Clear();
1168 ps2 = "";
1169 }
1170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 }
1172 arena = PyArena_New();
1173 if (arena == NULL) {
1174 Py_XDECREF(v);
1175 Py_XDECREF(w);
1176 Py_XDECREF(oenc);
1177 return -1;
1178 }
1179 mod = PyParser_ASTFromFile(fp, filename, enc,
1180 Py_single_input, ps1, ps2,
1181 flags, &errcode, arena);
1182 Py_XDECREF(v);
1183 Py_XDECREF(w);
1184 Py_XDECREF(oenc);
1185 if (mod == NULL) {
1186 PyArena_Free(arena);
1187 if (errcode == E_EOF) {
1188 PyErr_Clear();
1189 return E_EOF;
1190 }
1191 PyErr_Print();
1192 return -1;
1193 }
1194 m = PyImport_AddModule("__main__");
1195 if (m == NULL) {
1196 PyArena_Free(arena);
1197 return -1;
1198 }
1199 d = PyModule_GetDict(m);
1200 v = run_mod(mod, filename, d, d, flags, arena);
1201 PyArena_Free(arena);
1202 flush_io();
1203 if (v == NULL) {
1204 PyErr_Print();
1205 return -1;
1206 }
1207 Py_DECREF(v);
1208 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001209}
1210
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001211/* Check whether a file maybe a pyc file: Look at the extension,
1212 the file type, and, if we may close it, at the first few bytes. */
1213
1214static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001215maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1218 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 /* Only look into the file if we are allowed to close it, since
1221 it then should also be seekable. */
1222 if (closeit) {
1223 /* Read only two bytes of the magic. If the file was opened in
1224 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1225 be read as they are on disk. */
1226 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1227 unsigned char buf[2];
1228 /* Mess: In case of -x, the stream is NOT at its start now,
1229 and ungetc() was used to push back the first newline,
1230 which makes the current stream position formally undefined,
1231 and a x-platform nightmare.
1232 Unfortunately, we have no direct way to know whether -x
1233 was specified. So we use a terrible hack: if the current
1234 stream position is not 0, we assume -x was specified, and
1235 give up. Bug 132850 on SourceForge spells out the
1236 hopelessness of trying anything else (fseek and ftell
1237 don't work predictably x-platform for text-mode files).
1238 */
1239 int ispyc = 0;
1240 if (ftell(fp) == 0) {
1241 if (fread(buf, 1, 2, fp) == 2 &&
1242 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1243 ispyc = 1;
1244 rewind(fp);
1245 }
1246 return ispyc;
1247 }
1248 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001249}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001250
Guido van Rossum0df002c2000-08-27 19:21:52 +00001251int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001252PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 PyObject *m, *d, *v;
1256 const char *ext;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001257 int set_file_name = 0, ret;
1258 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 m = PyImport_AddModule("__main__");
1261 if (m == NULL)
1262 return -1;
1263 d = PyModule_GetDict(m);
1264 if (PyDict_GetItemString(d, "__file__") == NULL) {
1265 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001266 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 if (f == NULL)
1268 return -1;
1269 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1270 Py_DECREF(f);
1271 return -1;
1272 }
Barry Warsaw916048d2011-09-20 14:45:44 -04001273 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1274 Py_DECREF(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 return -1;
Barry Warsaw916048d2011-09-20 14:45:44 -04001276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 set_file_name = 1;
1278 Py_DECREF(f);
1279 }
1280 len = strlen(filename);
1281 ext = filename + len - (len > 4 ? 4 : 0);
1282 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1283 /* Try to run a pyc file. First, re-open in binary */
1284 if (closeit)
1285 fclose(fp);
1286 if ((fp = fopen(filename, "rb")) == NULL) {
1287 fprintf(stderr, "python: Can't reopen .pyc file\n");
1288 ret = -1;
1289 goto done;
1290 }
1291 /* Turn on optimization if a .pyo file is given */
1292 if (strcmp(ext, ".pyo") == 0)
1293 Py_OptimizeFlag = 1;
1294 v = run_pyc_file(fp, filename, d, d, flags);
1295 } else {
1296 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1297 closeit, flags);
1298 }
1299 flush_io();
1300 if (v == NULL) {
1301 PyErr_Print();
1302 ret = -1;
1303 goto done;
1304 }
1305 Py_DECREF(v);
1306 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001307 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1309 PyErr_Clear();
1310 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001311}
1312
1313int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001314PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 PyObject *m, *d, *v;
1317 m = PyImport_AddModule("__main__");
1318 if (m == NULL)
1319 return -1;
1320 d = PyModule_GetDict(m);
1321 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1322 if (v == NULL) {
1323 PyErr_Print();
1324 return -1;
1325 }
1326 Py_DECREF(v);
1327 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001328}
1329
Barry Warsaw035574d1997-08-29 22:07:17 +00001330static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001331parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 long hold;
1335 PyObject *v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 /* old style errors */
1338 if (PyTuple_Check(err))
1339 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1340 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001341
Benjamin Peterson80d50422012-04-03 00:30:38 -04001342 *message = NULL;
1343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 /* new style errors. `err' is an instance */
Benjamin Peterson80d50422012-04-03 00:30:38 -04001345 *message = PyObject_GetAttrString(err, "msg");
1346 if (!*message)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001348
Benjamin Peterson80d50422012-04-03 00:30:38 -04001349 v = PyObject_GetAttrString(err, "filename");
1350 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001352 if (v == Py_None) {
1353 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 *filename = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001355 }
1356 else {
1357 *filename = _PyUnicode_AsString(v);
1358 Py_DECREF(v);
1359 if (!*filename)
1360 goto finally;
1361 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001362
Benjamin Peterson80d50422012-04-03 00:30:38 -04001363 v = PyObject_GetAttrString(err, "lineno");
1364 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 goto finally;
1366 hold = PyLong_AsLong(v);
1367 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 if (hold < 0 && PyErr_Occurred())
1369 goto finally;
1370 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001371
Benjamin Peterson80d50422012-04-03 00:30:38 -04001372 v = PyObject_GetAttrString(err, "offset");
1373 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 goto finally;
1375 if (v == Py_None) {
1376 *offset = -1;
1377 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 } else {
1379 hold = PyLong_AsLong(v);
1380 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 if (hold < 0 && PyErr_Occurred())
1382 goto finally;
1383 *offset = (int)hold;
1384 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001385
Benjamin Peterson80d50422012-04-03 00:30:38 -04001386 v = PyObject_GetAttrString(err, "text");
1387 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001389 if (v == Py_None) {
1390 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 *text = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001392 }
1393 else {
1394 *text = _PyUnicode_AsString(v);
1395 Py_DECREF(v);
1396 if (!*text)
1397 goto finally;
1398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001400
1401finally:
Benjamin Peterson80d50422012-04-03 00:30:38 -04001402 Py_XDECREF(*message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001404}
1405
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001406void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001407PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001410}
1411
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001412static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001413print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 char *nl;
1416 if (offset >= 0) {
Benjamin Petersona95e9772010-10-29 03:28:14 +00001417 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1418 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 for (;;) {
1420 nl = strchr(text, '\n');
1421 if (nl == NULL || nl-text >= offset)
1422 break;
1423 offset -= (int)(nl+1-text);
1424 text = nl+1;
1425 }
1426 while (*text == ' ' || *text == '\t') {
1427 text++;
1428 offset--;
1429 }
1430 }
1431 PyFile_WriteString(" ", f);
1432 PyFile_WriteString(text, f);
1433 if (*text == '\0' || text[strlen(text)-1] != '\n')
1434 PyFile_WriteString("\n", f);
1435 if (offset == -1)
1436 return;
1437 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +00001438 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001441}
1442
Guido van Rossum66e8e862001-03-23 17:54:43 +00001443static void
1444handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 PyObject *exception, *value, *tb;
1447 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 if (Py_InspectFlag)
1450 /* Don't exit if -i flag was given. This flag is set to 0
1451 * when entering interactive mode for inspecting. */
1452 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 PyErr_Fetch(&exception, &value, &tb);
1455 fflush(stdout);
1456 if (value == NULL || value == Py_None)
1457 goto done;
1458 if (PyExceptionInstance_Check(value)) {
1459 /* The error code should be in the `code' attribute. */
1460 PyObject *code = PyObject_GetAttrString(value, "code");
1461 if (code) {
1462 Py_DECREF(value);
1463 value = code;
1464 if (value == Py_None)
1465 goto done;
1466 }
1467 /* If we failed to dig out the 'code' attribute,
1468 just let the else clause below print the error. */
1469 }
1470 if (PyLong_Check(value))
1471 exitcode = (int)PyLong_AsLong(value);
1472 else {
Victor Stinnere9fb3192010-05-17 08:58:51 +00001473 PyObject *sys_stderr = PySys_GetObject("stderr");
Victor Stinner7126dbc2010-05-21 23:45:42 +00001474 if (sys_stderr != NULL && sys_stderr != Py_None) {
1475 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1476 } else {
1477 PyObject_Print(value, stderr, Py_PRINT_RAW);
1478 fflush(stderr);
1479 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 PySys_WriteStderr("\n");
1481 exitcode = 1;
1482 }
Tim Peterscf615b52003-04-19 18:47:02 +00001483 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 /* Restore and clear the exception info, in order to properly decref
1485 * the exception, value, and traceback. If we just exit instead,
1486 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1487 * some finalizers from running.
1488 */
1489 PyErr_Restore(exception, value, tb);
1490 PyErr_Clear();
1491 Py_Exit(exitcode);
1492 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001493}
1494
1495void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001496PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1501 handle_system_exit();
1502 }
1503 PyErr_Fetch(&exception, &v, &tb);
1504 if (exception == NULL)
1505 return;
1506 PyErr_NormalizeException(&exception, &v, &tb);
1507 if (tb == NULL) {
1508 tb = Py_None;
1509 Py_INCREF(tb);
1510 }
1511 PyException_SetTraceback(v, tb);
1512 if (exception == NULL)
1513 return;
1514 /* Now we know v != NULL too */
1515 if (set_sys_last_vars) {
1516 PySys_SetObject("last_type", exception);
1517 PySys_SetObject("last_value", v);
1518 PySys_SetObject("last_traceback", tb);
1519 }
1520 hook = PySys_GetObject("excepthook");
1521 if (hook) {
1522 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1523 PyObject *result = PyEval_CallObject(hook, args);
1524 if (result == NULL) {
1525 PyObject *exception2, *v2, *tb2;
1526 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1527 handle_system_exit();
1528 }
1529 PyErr_Fetch(&exception2, &v2, &tb2);
1530 PyErr_NormalizeException(&exception2, &v2, &tb2);
1531 /* It should not be possible for exception2 or v2
1532 to be NULL. However PyErr_Display() can't
1533 tolerate NULLs, so just be safe. */
1534 if (exception2 == NULL) {
1535 exception2 = Py_None;
1536 Py_INCREF(exception2);
1537 }
1538 if (v2 == NULL) {
1539 v2 = Py_None;
1540 Py_INCREF(v2);
1541 }
1542 fflush(stdout);
1543 PySys_WriteStderr("Error in sys.excepthook:\n");
1544 PyErr_Display(exception2, v2, tb2);
1545 PySys_WriteStderr("\nOriginal exception was:\n");
1546 PyErr_Display(exception, v, tb);
1547 Py_DECREF(exception2);
1548 Py_DECREF(v2);
1549 Py_XDECREF(tb2);
1550 }
1551 Py_XDECREF(result);
1552 Py_XDECREF(args);
1553 } else {
1554 PySys_WriteStderr("sys.excepthook is missing\n");
1555 PyErr_Display(exception, v, tb);
1556 }
1557 Py_XDECREF(exception);
1558 Py_XDECREF(v);
1559 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001560}
1561
Benjamin Petersone6528212008-07-15 15:32:09 +00001562static void
1563print_exception(PyObject *f, PyObject *value)
1564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 int err = 0;
1566 PyObject *type, *tb;
Benjamin Petersone6528212008-07-15 15:32:09 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (!PyExceptionInstance_Check(value)) {
1569 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1570 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1571 PyFile_WriteString(" found\n", f);
1572 return;
1573 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 Py_INCREF(value);
1576 fflush(stdout);
1577 type = (PyObject *) Py_TYPE(value);
1578 tb = PyException_GetTraceback(value);
1579 if (tb && tb != Py_None)
1580 err = PyTraceBack_Print(tb, f);
1581 if (err == 0 &&
1582 PyObject_HasAttrString(value, "print_file_and_line"))
1583 {
1584 PyObject *message;
1585 const char *filename, *text;
1586 int lineno, offset;
1587 if (!parse_syntax_error(value, &message, &filename,
1588 &lineno, &offset, &text))
1589 PyErr_Clear();
1590 else {
1591 char buf[10];
1592 PyFile_WriteString(" File \"", f);
1593 if (filename == NULL)
1594 PyFile_WriteString("<string>", f);
1595 else
1596 PyFile_WriteString(filename, f);
1597 PyFile_WriteString("\", line ", f);
1598 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1599 PyFile_WriteString(buf, f);
1600 PyFile_WriteString("\n", f);
1601 if (text != NULL)
1602 print_error_text(f, offset, text);
1603 Py_DECREF(value);
1604 value = message;
1605 /* Can't be bothered to check all those
1606 PyFile_WriteString() calls */
1607 if (PyErr_Occurred())
1608 err = -1;
1609 }
1610 }
1611 if (err) {
1612 /* Don't do anything else */
1613 }
1614 else {
1615 PyObject* moduleName;
1616 char* className;
1617 assert(PyExceptionClass_Check(type));
1618 className = PyExceptionClass_Name(type);
1619 if (className != NULL) {
1620 char *dot = strrchr(className, '.');
1621 if (dot != NULL)
1622 className = dot+1;
1623 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 moduleName = PyObject_GetAttrString(type, "__module__");
1626 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1627 {
1628 Py_DECREF(moduleName);
1629 err = PyFile_WriteString("<unknown>", f);
1630 }
1631 else {
1632 char* modstr = _PyUnicode_AsString(moduleName);
1633 if (modstr && strcmp(modstr, "builtins"))
1634 {
1635 err = PyFile_WriteString(modstr, f);
1636 err += PyFile_WriteString(".", f);
1637 }
1638 Py_DECREF(moduleName);
1639 }
1640 if (err == 0) {
1641 if (className == NULL)
1642 err = PyFile_WriteString("<unknown>", f);
1643 else
1644 err = PyFile_WriteString(className, f);
1645 }
1646 }
1647 if (err == 0 && (value != Py_None)) {
1648 PyObject *s = PyObject_Str(value);
1649 /* only print colon if the str() of the
1650 object is not the empty string
1651 */
1652 if (s == NULL)
1653 err = -1;
1654 else if (!PyUnicode_Check(s) ||
1655 PyUnicode_GetSize(s) != 0)
1656 err = PyFile_WriteString(": ", f);
1657 if (err == 0)
1658 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1659 Py_XDECREF(s);
1660 }
1661 /* try to write a newline in any case */
1662 err += PyFile_WriteString("\n", f);
1663 Py_XDECREF(tb);
1664 Py_DECREF(value);
1665 /* If an error happened here, don't show it.
1666 XXX This is wrong, but too many callers rely on this behavior. */
1667 if (err != 0)
1668 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001669}
1670
1671static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 "\nThe above exception was the direct cause "
1673 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001674
1675static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 "\nDuring handling of the above exception, "
1677 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001678
1679static void
1680print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 int err = 0, res;
1683 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 if (seen != NULL) {
1686 /* Exception chaining */
1687 if (PySet_Add(seen, value) == -1)
1688 PyErr_Clear();
1689 else if (PyExceptionInstance_Check(value)) {
1690 cause = PyException_GetCause(value);
1691 context = PyException_GetContext(value);
1692 if (cause) {
1693 res = PySet_Contains(seen, cause);
1694 if (res == -1)
1695 PyErr_Clear();
1696 if (res == 0) {
1697 print_exception_recursive(
1698 f, cause, seen);
1699 err |= PyFile_WriteString(
1700 cause_message, f);
1701 }
1702 }
1703 else if (context) {
1704 res = PySet_Contains(seen, context);
1705 if (res == -1)
1706 PyErr_Clear();
1707 if (res == 0) {
1708 print_exception_recursive(
1709 f, context, seen);
1710 err |= PyFile_WriteString(
1711 context_message, f);
1712 }
1713 }
1714 Py_XDECREF(context);
1715 Py_XDECREF(cause);
1716 }
1717 }
1718 print_exception(f, value);
1719 if (err != 0)
1720 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001721}
1722
Thomas Wouters477c8d52006-05-27 19:21:47 +00001723void
1724PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 PyObject *seen;
1727 PyObject *f = PySys_GetObject("stderr");
1728 if (f == Py_None) {
1729 /* pass */
1730 }
1731 else if (f == NULL) {
1732 _PyObject_Dump(value);
1733 fprintf(stderr, "lost sys.stderr\n");
1734 }
1735 else {
1736 /* We choose to ignore seen being possibly NULL, and report
1737 at least the main exception (it could be a MemoryError).
1738 */
1739 seen = PySet_New(NULL);
1740 if (seen == NULL)
1741 PyErr_Clear();
1742 print_exception_recursive(f, value, seen);
1743 Py_XDECREF(seen);
1744 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001745}
1746
Guido van Rossum82598051997-03-05 00:20:32 +00001747PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001748PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 PyObject *ret = NULL;
1752 mod_ty mod;
1753 PyArena *arena = PyArena_New();
1754 if (arena == NULL)
1755 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1758 if (mod != NULL)
1759 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1760 PyArena_Free(arena);
1761 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001762}
1763
1764PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001765PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 PyObject *ret;
1769 mod_ty mod;
1770 PyArena *arena = PyArena_New();
1771 if (arena == NULL)
1772 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1775 flags, NULL, arena);
1776 if (closeit)
1777 fclose(fp);
1778 if (mod == NULL) {
1779 PyArena_Free(arena);
1780 return NULL;
1781 }
1782 ret = run_mod(mod, filename, globals, locals, flags, arena);
1783 PyArena_Free(arena);
1784 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001785}
1786
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001787static void
1788flush_io(void)
1789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 PyObject *f, *r;
1791 PyObject *type, *value, *traceback;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 /* Save the current exception */
1794 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 f = PySys_GetObject("stderr");
1797 if (f != NULL) {
1798 r = PyObject_CallMethod(f, "flush", "");
1799 if (r)
1800 Py_DECREF(r);
1801 else
1802 PyErr_Clear();
1803 }
1804 f = PySys_GetObject("stdout");
1805 if (f != NULL) {
1806 r = PyObject_CallMethod(f, "flush", "");
1807 if (r)
1808 Py_DECREF(r);
1809 else
1810 PyErr_Clear();
1811 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001814}
1815
Guido van Rossum82598051997-03-05 00:20:32 +00001816static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 PyCodeObject *co;
1821 PyObject *v;
1822 co = PyAST_Compile(mod, filename, flags, arena);
1823 if (co == NULL)
1824 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001825 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 Py_DECREF(co);
1827 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001828}
1829
Guido van Rossum82598051997-03-05 00:20:32 +00001830static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001831run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 PyCodeObject *co;
1835 PyObject *v;
1836 long magic;
1837 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 magic = PyMarshal_ReadLongFromFile(fp);
1840 if (magic != PyImport_GetMagicNumber()) {
1841 PyErr_SetString(PyExc_RuntimeError,
1842 "Bad magic number in .pyc file");
1843 return NULL;
1844 }
1845 (void) PyMarshal_ReadLongFromFile(fp);
1846 v = PyMarshal_ReadLastObjectFromFile(fp);
1847 fclose(fp);
1848 if (v == NULL || !PyCode_Check(v)) {
1849 Py_XDECREF(v);
1850 PyErr_SetString(PyExc_RuntimeError,
1851 "Bad code object in .pyc file");
1852 return NULL;
1853 }
1854 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001855 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 if (v && flags)
1857 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1858 Py_DECREF(co);
1859 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001860}
1861
Guido van Rossum82598051997-03-05 00:20:32 +00001862PyObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00001863Py_CompileStringExFlags(const char *str, const char *filename, int start,
1864 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 PyCodeObject *co;
1867 mod_ty mod;
1868 PyArena *arena = PyArena_New();
1869 if (arena == NULL)
1870 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1873 if (mod == NULL) {
1874 PyArena_Free(arena);
1875 return NULL;
1876 }
1877 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1878 PyObject *result = PyAST_mod2obj(mod);
1879 PyArena_Free(arena);
1880 return result;
1881 }
Georg Brandl8334fd92010-12-04 10:26:46 +00001882 co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 PyArena_Free(arena);
1884 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001885}
1886
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001887/* For use in Py_LIMITED_API */
1888#undef Py_CompileString
1889PyObject *
1890PyCompileString(const char *str, const char *filename, int start)
1891{
1892 return Py_CompileStringFlags(str, filename, start, NULL);
1893}
1894
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001895struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001896Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 struct symtable *st;
1899 mod_ty mod;
1900 PyCompilerFlags flags;
1901 PyArena *arena = PyArena_New();
1902 if (arena == NULL)
1903 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 flags.cf_flags = 0;
1906 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1907 if (mod == NULL) {
1908 PyArena_Free(arena);
1909 return NULL;
1910 }
1911 st = PySymtable_Build(mod, filename, 0);
1912 PyArena_Free(arena);
1913 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001914}
1915
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916/* Preferred access to parser is through AST. */
1917mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001918PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 mod_ty mod;
1922 PyCompilerFlags localflags;
1923 perrdetail err;
1924 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1927 &_PyParser_Grammar, start, &err,
1928 &iflags);
1929 if (flags == NULL) {
1930 localflags.cf_flags = 0;
1931 flags = &localflags;
1932 }
1933 if (n) {
1934 flags->cf_flags |= iflags & PyCF_MASK;
1935 mod = PyAST_FromNode(n, flags, filename, arena);
1936 PyNode_Free(n);
1937 return mod;
1938 }
1939 else {
1940 err_input(&err);
1941 return NULL;
1942 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943}
1944
1945mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001946PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 int start, char *ps1,
1948 char *ps2, PyCompilerFlags *flags, int *errcode,
1949 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 mod_ty mod;
1952 PyCompilerFlags localflags;
1953 perrdetail err;
1954 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
1957 &_PyParser_Grammar,
1958 start, ps1, ps2, &err, &iflags);
1959 if (flags == NULL) {
1960 localflags.cf_flags = 0;
1961 flags = &localflags;
1962 }
1963 if (n) {
1964 flags->cf_flags |= iflags & PyCF_MASK;
1965 mod = PyAST_FromNode(n, flags, filename, arena);
1966 PyNode_Free(n);
1967 return mod;
1968 }
1969 else {
1970 err_input(&err);
1971 if (errcode)
1972 *errcode = err.error;
1973 return NULL;
1974 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975}
1976
Guido van Rossuma110aa61994-08-29 12:50:44 +00001977/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001978
Guido van Rossuma110aa61994-08-29 12:50:44 +00001979node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001980PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 perrdetail err;
1983 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1984 &_PyParser_Grammar,
1985 start, NULL, NULL, &err, flags);
1986 if (n == NULL)
1987 err_input(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001990}
1991
Guido van Rossuma110aa61994-08-29 12:50:44 +00001992/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001993
Guido van Rossuma110aa61994-08-29 12:50:44 +00001994node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001995PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 perrdetail err;
1998 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1999 start, &err, flags);
2000 if (n == NULL)
2001 err_input(&err);
2002 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00002003}
2004
2005node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002006PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 perrdetail err;
2010 node *n = PyParser_ParseStringFlagsFilename(str, filename,
2011 &_PyParser_Grammar, start, &err, flags);
2012 if (n == NULL)
2013 err_input(&err);
2014 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00002015}
2016
2017node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002018PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00002021}
2022
Guido van Rossum66ebd912003-04-17 16:02:26 +00002023/* May want to move a more generalized form of this to parsetok.c or
2024 even parser modules. */
2025
2026void
2027PyParser_SetError(perrdetail *err)
2028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00002030}
2031
Guido van Rossuma110aa61994-08-29 12:50:44 +00002032/* Set the error appropriate to the given input error code (see errcode.h) */
2033
2034static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002035err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00002036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 PyObject *v, *w, *errtype, *errtext;
2038 PyObject *msg_obj = NULL;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002039 PyObject *filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 char *msg = NULL;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 errtype = PyExc_SyntaxError;
2043 switch (err->error) {
2044 case E_ERROR:
2045 return;
2046 case E_SYNTAX:
2047 errtype = PyExc_IndentationError;
2048 if (err->expected == INDENT)
2049 msg = "expected an indented block";
2050 else if (err->token == INDENT)
2051 msg = "unexpected indent";
2052 else if (err->token == DEDENT)
2053 msg = "unexpected unindent";
2054 else {
2055 errtype = PyExc_SyntaxError;
2056 msg = "invalid syntax";
2057 }
2058 break;
2059 case E_TOKEN:
2060 msg = "invalid token";
2061 break;
2062 case E_EOFS:
2063 msg = "EOF while scanning triple-quoted string literal";
2064 break;
2065 case E_EOLS:
2066 msg = "EOL while scanning string literal";
2067 break;
2068 case E_INTR:
2069 if (!PyErr_Occurred())
2070 PyErr_SetNone(PyExc_KeyboardInterrupt);
2071 goto cleanup;
2072 case E_NOMEM:
2073 PyErr_NoMemory();
2074 goto cleanup;
2075 case E_EOF:
2076 msg = "unexpected EOF while parsing";
2077 break;
2078 case E_TABSPACE:
2079 errtype = PyExc_TabError;
2080 msg = "inconsistent use of tabs and spaces in indentation";
2081 break;
2082 case E_OVERFLOW:
2083 msg = "expression too long";
2084 break;
2085 case E_DEDENT:
2086 errtype = PyExc_IndentationError;
2087 msg = "unindent does not match any outer indentation level";
2088 break;
2089 case E_TOODEEP:
2090 errtype = PyExc_IndentationError;
2091 msg = "too many levels of indentation";
2092 break;
2093 case E_DECODE: {
2094 PyObject *type, *value, *tb;
2095 PyErr_Fetch(&type, &value, &tb);
2096 msg = "unknown decode error";
2097 if (value != NULL)
2098 msg_obj = PyObject_Str(value);
2099 Py_XDECREF(type);
2100 Py_XDECREF(value);
2101 Py_XDECREF(tb);
2102 break;
2103 }
2104 case E_LINECONT:
2105 msg = "unexpected character after line continuation character";
2106 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00002107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 case E_IDENTIFIER:
2109 msg = "invalid character in identifier";
2110 break;
2111 default:
2112 fprintf(stderr, "error=%d\n", err->error);
2113 msg = "unknown parsing error";
2114 break;
2115 }
2116 /* err->text may not be UTF-8 in case of decoding errors.
2117 Explicitly convert to an object. */
2118 if (!err->text) {
2119 errtext = Py_None;
2120 Py_INCREF(Py_None);
2121 } else {
2122 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2123 "replace");
2124 }
Victor Stinner2f2ed1f2010-10-16 13:42:53 +00002125 if (err->filename != NULL)
2126 filename = PyUnicode_DecodeFSDefault(err->filename);
2127 else {
2128 Py_INCREF(Py_None);
2129 filename = Py_None;
2130 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002131 if (filename != NULL)
2132 v = Py_BuildValue("(NiiN)", filename,
2133 err->lineno, err->offset, errtext);
2134 else
2135 v = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 if (v != NULL) {
2137 if (msg_obj)
2138 w = Py_BuildValue("(OO)", msg_obj, v);
2139 else
2140 w = Py_BuildValue("(sO)", msg, v);
2141 } else
2142 w = NULL;
2143 Py_XDECREF(v);
2144 PyErr_SetObject(errtype, w);
2145 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002146cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 Py_XDECREF(msg_obj);
2148 if (err->text != NULL) {
2149 PyObject_FREE(err->text);
2150 err->text = NULL;
2151 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002152}
2153
2154/* Print fatal error message and abort */
2155
2156void
Tim Peters7c321a82002-07-09 02:57:01 +00002157Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 fprintf(stderr, "Fatal Python error: %s\n", msg);
2160 fflush(stderr); /* it helps in Windows debug build */
2161 if (PyErr_Occurred()) {
Victor Stinner55a5c782010-06-08 21:00:13 +00002162 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002164#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 {
2166 size_t len = strlen(msg);
2167 WCHAR* buffer;
2168 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 /* Convert the message to wchar_t. This uses a simple one-to-one
2171 conversion, assuming that the this error message actually uses ASCII
2172 only. If this ceases to be true, we will have to convert. */
2173 buffer = alloca( (len+1) * (sizeof *buffer));
2174 for( i=0; i<=len; ++i)
2175 buffer[i] = msg[i];
2176 OutputDebugStringW(L"Fatal Python error: ");
2177 OutputDebugStringW(buffer);
2178 OutputDebugStringW(L"\n");
2179 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002180#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002182#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002183#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002185}
2186
2187/* Clean up and exit */
2188
Guido van Rossuma110aa61994-08-29 12:50:44 +00002189#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002190#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002191#endif
2192
Collin Winter670e6922007-03-21 02:57:17 +00002193static void (*pyexitfunc)(void) = NULL;
2194/* For the atexit module. */
2195void _Py_PyAtExit(void (*func)(void))
2196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002198}
2199
2200static void
2201call_py_exitfuncs(void)
2202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 if (pyexitfunc == NULL)
2204 return;
Collin Winter670e6922007-03-21 02:57:17 +00002205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 (*pyexitfunc)();
2207 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002208}
2209
Antoine Pitrou011bd622009-10-20 21:52:47 +00002210/* Wait until threading._shutdown completes, provided
2211 the threading module was imported in the first place.
2212 The shutdown routine will wait until all non-daemon
2213 "threading" threads have completed. */
2214static void
2215wait_for_thread_shutdown(void)
2216{
2217#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 PyObject *result;
2219 PyThreadState *tstate = PyThreadState_GET();
2220 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2221 "threading");
2222 if (threading == NULL) {
2223 /* threading not imported */
2224 PyErr_Clear();
2225 return;
2226 }
2227 result = PyObject_CallMethod(threading, "_shutdown", "");
2228 if (result == NULL) {
2229 PyErr_WriteUnraisable(threading);
2230 }
2231 else {
2232 Py_DECREF(result);
2233 }
2234 Py_DECREF(threading);
Antoine Pitrou011bd622009-10-20 21:52:47 +00002235#endif
2236}
2237
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002238#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002239static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002240static int nexitfuncs = 0;
2241
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002242int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 if (nexitfuncs >= NEXITFUNCS)
2245 return -1;
2246 exitfuncs[nexitfuncs++] = func;
2247 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002248}
2249
Guido van Rossumcc283f51997-08-05 02:22:03 +00002250static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002251call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 while (nexitfuncs > 0)
2254 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 fflush(stdout);
2257 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002258}
2259
2260void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002261Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002266}
2267
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002268static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002269initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002270{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002271#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002273#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002274#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002276#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002277#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002279#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002281}
2282
Guido van Rossum7433b121997-02-14 19:45:36 +00002283
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002284/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2285 *
2286 * All of the code in this function must only use async-signal-safe functions,
2287 * listed at `man 7 signal` or
2288 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2289 */
2290void
2291_Py_RestoreSignals(void)
2292{
2293#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 PyOS_setsig(SIGPIPE, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002295#endif
2296#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 PyOS_setsig(SIGXFZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002298#endif
2299#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 PyOS_setsig(SIGXFSZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002301#endif
2302}
2303
2304
Guido van Rossum7433b121997-02-14 19:45:36 +00002305/*
2306 * The file descriptor fd is considered ``interactive'' if either
2307 * a) isatty(fd) is TRUE, or
2308 * b) the -i flag was given, and the filename associated with
2309 * the descriptor is NULL or "<stdin>" or "???".
2310 */
2311int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002312Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 if (isatty((int)fileno(fp)))
2315 return 1;
2316 if (!Py_InteractiveFlag)
2317 return 0;
2318 return (filename == NULL) ||
2319 (strcmp(filename, "<stdin>") == 0) ||
2320 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002321}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002322
2323
Tim Petersd08e3822003-04-17 15:24:21 +00002324#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002325#if defined(WIN32) && defined(_MSC_VER)
2326
2327/* Stack checking for Microsoft C */
2328
2329#include <malloc.h>
2330#include <excpt.h>
2331
Fred Drakee8de31c2000-08-31 05:38:39 +00002332/*
2333 * Return non-zero when we run out of memory on the stack; zero otherwise.
2334 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002335int
Fred Drake399739f2000-08-31 05:52:44 +00002336PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 __try {
2339 /* alloca throws a stack overflow exception if there's
2340 not enough space left on the stack */
2341 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2342 return 0;
2343 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2344 EXCEPTION_EXECUTE_HANDLER :
2345 EXCEPTION_CONTINUE_SEARCH) {
2346 int errcode = _resetstkoflw();
2347 if (errcode == 0)
2348 {
2349 Py_FatalError("Could not reset the stack!");
2350 }
2351 }
2352 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002353}
2354
2355#endif /* WIN32 && _MSC_VER */
2356
2357/* Alternate implementations can be added here... */
2358
2359#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002360
2361
2362/* Wrappers around sigaction() or signal(). */
2363
2364PyOS_sighandler_t
2365PyOS_getsig(int sig)
2366{
2367#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 struct sigaction context;
2369 if (sigaction(sig, NULL, &context) == -1)
2370 return SIG_ERR;
2371 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002372#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002374/* Special signal handling for the secure CRT in Visual Studio 2005 */
2375#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 switch (sig) {
2377 /* Only these signals are valid */
2378 case SIGINT:
2379 case SIGILL:
2380 case SIGFPE:
2381 case SIGSEGV:
2382 case SIGTERM:
2383 case SIGBREAK:
2384 case SIGABRT:
2385 break;
2386 /* Don't call signal() with other values or it will assert */
2387 default:
2388 return SIG_ERR;
2389 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002390#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 handler = signal(sig, SIG_IGN);
2392 if (handler != SIG_ERR)
2393 signal(sig, handler);
2394 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002395#endif
2396}
2397
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002398/*
2399 * All of the code in this function must only use async-signal-safe functions,
2400 * listed at `man 7 signal` or
2401 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2402 */
Guido van Rossum6f256182000-09-16 16:32:19 +00002403PyOS_sighandler_t
2404PyOS_setsig(int sig, PyOS_sighandler_t handler)
2405{
2406#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 /* Some code in Modules/signalmodule.c depends on sigaction() being
2408 * used here if HAVE_SIGACTION is defined. Fix that if this code
2409 * changes to invalidate that assumption.
2410 */
2411 struct sigaction context, ocontext;
2412 context.sa_handler = handler;
2413 sigemptyset(&context.sa_mask);
2414 context.sa_flags = 0;
2415 if (sigaction(sig, &context, &ocontext) == -1)
2416 return SIG_ERR;
2417 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002418#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 PyOS_sighandler_t oldhandler;
2420 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002421#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002423#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002425#endif
2426}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427
2428/* Deprecated C API functions still provided for binary compatiblity */
2429
2430#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002431PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435}
2436
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002437#undef PyParser_SimpleParseString
2438PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439PyParser_SimpleParseString(const char *str, int start)
2440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002443
2444#undef PyRun_AnyFile
2445PyAPI_FUNC(int)
2446PyRun_AnyFile(FILE *fp, const char *name)
2447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002449}
2450
2451#undef PyRun_AnyFileEx
2452PyAPI_FUNC(int)
2453PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002456}
2457
2458#undef PyRun_AnyFileFlags
2459PyAPI_FUNC(int)
2460PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002463}
2464
2465#undef PyRun_File
2466PyAPI_FUNC(PyObject *)
2467PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002470}
2471
2472#undef PyRun_FileEx
2473PyAPI_FUNC(PyObject *)
2474PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002477}
2478
2479#undef PyRun_FileFlags
2480PyAPI_FUNC(PyObject *)
2481PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002485}
2486
2487#undef PyRun_SimpleFile
2488PyAPI_FUNC(int)
2489PyRun_SimpleFile(FILE *f, const char *p)
2490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002492}
2493
2494#undef PyRun_SimpleFileEx
2495PyAPI_FUNC(int)
2496PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002499}
2500
2501
2502#undef PyRun_String
2503PyAPI_FUNC(PyObject *)
2504PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002507}
2508
2509#undef PyRun_SimpleString
2510PyAPI_FUNC(int)
2511PyRun_SimpleString(const char *s)
2512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002514}
2515
2516#undef Py_CompileString
2517PyAPI_FUNC(PyObject *)
2518Py_CompileString(const char *str, const char *p, int s)
2519{
Georg Brandl8334fd92010-12-04 10:26:46 +00002520 return Py_CompileStringExFlags(str, p, s, NULL, -1);
2521}
2522
2523#undef Py_CompileStringFlags
2524PyAPI_FUNC(PyObject *)
2525Py_CompileStringFlags(const char *str, const char *p, int s,
2526 PyCompilerFlags *flags)
2527{
2528 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002529}
2530
2531#undef PyRun_InteractiveOne
2532PyAPI_FUNC(int)
2533PyRun_InteractiveOne(FILE *f, const char *p)
2534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002536}
2537
2538#undef PyRun_InteractiveLoop
2539PyAPI_FUNC(int)
2540PyRun_InteractiveLoop(FILE *f, const char *p)
2541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002543}
2544
2545#ifdef __cplusplus
2546}
2547#endif