blob: 718362d479229c56e9abe221bdb25e9137768823 [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);
Georg Brandl2daf6ae2012-02-20 19:54:16 +010073extern void _PyRandom_Init(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000074
Mark Hammond8d98d2c2003-04-19 15:41:53 +000075#ifdef WITH_THREAD
76extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
77extern void _PyGILState_Fini(void);
78#endif /* WITH_THREAD */
79
Guido van Rossum82598051997-03-05 00:20:32 +000080int Py_DebugFlag; /* Needed by parser.c */
81int Py_VerboseFlag; /* Needed by import.c */
Georg Brandl8aa7e992010-12-28 18:30:18 +000082int Py_QuietFlag; /* Needed by sysmodule.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000083int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl0b2489e2011-05-15 08:49:12 +020084int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000085int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +000086int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Christian Heimes790c8232008-01-07 21:14:23 +000087int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000088int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000089int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000090int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Christian Heimes8dc226f2008-05-06 23:45:46 +000091int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Antoine Pitrou05608432009-01-09 18:53:14 +000092int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
Georg Brandl2daf6ae2012-02-20 19:54:16 +010093int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000094
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +020095PyThreadState *_Py_Finalizing = NULL;
96
Christian Heimes33fe8092008-04-13 13:53:33 +000097/* PyModule_GetWarningsModule is no longer necessary as of 2.6
98since _warnings is builtin. This API should not be used. */
99PyObject *
100PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +0000101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +0000103}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000104
Guido van Rossum25ce5661997-08-02 03:10:38 +0000105static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000106
Thomas Wouters7e474022000-07-16 12:04:32 +0000107/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000108
109int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000110Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 return initialized;
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000113}
114
Guido van Rossum25ce5661997-08-02 03:10:38 +0000115/* Global initializations. Can be undone by Py_Finalize(). Don't
116 call this twice without an intervening Py_Finalize() call. When
117 initializations fail, a fatal error is issued and the function does
118 not return. On return, the first thread and interpreter state have
119 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000120
Guido van Rossum25ce5661997-08-02 03:10:38 +0000121 Locking: you must hold the interpreter lock while calling this.
122 (If the lock has not yet been initialized, that's equivalent to
123 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000124
Guido van Rossum25ce5661997-08-02 03:10:38 +0000125*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000126
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000127static int
128add_flag(int flag, const char *envs)
129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 int env = atoi(envs);
131 if (flag < env)
132 flag = env;
133 if (flag < 1)
134 flag = 1;
135 return flag;
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000136}
137
Christian Heimes5833a2f2008-10-30 21:40:04 +0000138static char*
Victor Stinner94908bb2010-08-18 21:23:25 +0000139get_codec_name(const char *encoding)
Christian Heimes5833a2f2008-10-30 21:40:04 +0000140{
Victor Stinner94908bb2010-08-18 21:23:25 +0000141 char *name_utf8, *name_str;
Victor Stinner386fe712010-05-19 00:34:15 +0000142 PyObject *codec, *name = NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000143
Victor Stinner94908bb2010-08-18 21:23:25 +0000144 codec = _PyCodec_Lookup(encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 if (!codec)
146 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 name = PyObject_GetAttrString(codec, "name");
149 Py_CLEAR(codec);
150 if (!name)
151 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000152
Victor Stinner94908bb2010-08-18 21:23:25 +0000153 name_utf8 = _PyUnicode_AsString(name);
Victor Stinner9c4efe52011-03-20 23:23:22 +0100154 if (name_utf8 == NULL)
Victor Stinner386fe712010-05-19 00:34:15 +0000155 goto error;
Victor Stinner94908bb2010-08-18 21:23:25 +0000156 name_str = strdup(name_utf8);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 Py_DECREF(name);
Victor Stinner94908bb2010-08-18 21:23:25 +0000158 if (name_str == NULL) {
159 PyErr_NoMemory();
160 return NULL;
161 }
162 return name_str;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000163
164error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 Py_XDECREF(codec);
Victor Stinner386fe712010-05-19 00:34:15 +0000166 Py_XDECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000168}
Victor Stinner94908bb2010-08-18 21:23:25 +0000169
170#if defined(HAVE_LANGINFO_H) && defined(CODESET)
171static char*
172get_codeset(void)
173{
174 char* codeset = nl_langinfo(CODESET);
175 if (!codeset || codeset[0] == '\0') {
176 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
177 return NULL;
178 }
179 return get_codec_name(codeset);
180}
Christian Heimes5833a2f2008-10-30 21:40:04 +0000181#endif
182
Guido van Rossuma027efa1997-05-05 20:56:21 +0000183void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000184Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 PyInterpreterState *interp;
187 PyThreadState *tstate;
188 PyObject *bimod, *sysmod, *pstderr;
189 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (initialized)
193 return;
194 initialized = 1;
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200195 _Py_Finalizing = NULL;
Tim Petersd08e3822003-04-17 15:24:21 +0000196
Andrew M. Kuchlingb2ceb3a2010-02-22 23:26:10 +0000197#if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 /* Set up the LC_CTYPE locale, so we can obtain
199 the locale's charset without having to switch
200 locales. */
201 setlocale(LC_CTYPE, "");
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000202#endif
203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
205 Py_DebugFlag = add_flag(Py_DebugFlag, p);
206 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
207 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
208 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
209 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
210 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
211 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100212 /* The variable is only tested for existence here; _PyRandom_Init will
213 check its value further. */
214 if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
215 Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
216
217 _PyRandom_Init();
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 interp = PyInterpreterState_New();
220 if (interp == NULL)
221 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 tstate = PyThreadState_New(interp);
224 if (tstate == NULL)
225 Py_FatalError("Py_Initialize: can't make first thread");
226 (void) PyThreadState_Swap(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000227
Victor Stinner6961bd62010-08-17 22:26:51 +0000228#ifdef WITH_THREAD
Antoine Pitroub0b384b2010-09-20 20:13:48 +0000229 /* We can't call _PyEval_FiniThreads() in Py_Finalize because
230 destroying the GIL might fail when it is being referenced from
231 another running thread (see issue #9901).
232 Instead we destroy the previously created GIL here, which ensures
233 that we can call Py_Initialize / Py_Finalize multiple times. */
234 _PyEval_FiniThreads();
235
236 /* Auto-thread-state API */
Victor Stinner6961bd62010-08-17 22:26:51 +0000237 _PyGILState_Init(interp, tstate);
238#endif /* WITH_THREAD */
239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 if (!_PyFrame_Init())
243 Py_FatalError("Py_Initialize: can't init frames");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (!_PyLong_Init())
246 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (!PyByteArray_Init())
249 Py_FatalError("Py_Initialize: can't init bytearray");
Neal Norwitz6968b052007-02-27 19:02:19 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 _PyFloat_Init();
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 interp->modules = PyDict_New();
254 if (interp->modules == NULL)
255 Py_FatalError("Py_Initialize: can't make modules dictionary");
256 interp->modules_reloading = PyDict_New();
257 if (interp->modules_reloading == NULL)
258 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 /* Init Unicode implementation; relies on the codec registry */
261 _PyUnicode_Init();
Guido van Rossumc94044c2000-03-10 23:03:54 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 bimod = _PyBuiltin_Init();
264 if (bimod == NULL)
265 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Victor Stinner49d3f252010-10-17 01:24:53 +0000266 _PyImport_FixupBuiltin(bimod, "builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 interp->builtins = PyModule_GetDict(bimod);
268 if (interp->builtins == NULL)
269 Py_FatalError("Py_Initialize: can't initialize builtins dict");
270 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* initialize builtin exceptions */
273 _PyExc_Init();
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 sysmod = _PySys_Init();
276 if (sysmod == NULL)
277 Py_FatalError("Py_Initialize: can't initialize sys");
278 interp->sysdict = PyModule_GetDict(sysmod);
279 if (interp->sysdict == NULL)
280 Py_FatalError("Py_Initialize: can't initialize sys dict");
281 Py_INCREF(interp->sysdict);
Victor Stinner49d3f252010-10-17 01:24:53 +0000282 _PyImport_FixupBuiltin(sysmod, "sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 PySys_SetPath(Py_GetPath());
284 PyDict_SetItemString(interp->sysdict, "modules",
285 interp->modules);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 /* Set up a preliminary stderr printer until we have enough
288 infrastructure for the io module in place. */
289 pstderr = PyFile_NewStdPrinter(fileno(stderr));
290 if (pstderr == NULL)
291 Py_FatalError("Py_Initialize: can't set preliminary stderr");
292 PySys_SetObject("stderr", pstderr);
293 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000294 Py_DECREF(pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 _PyImport_Init();
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 _PyImportHooks_Init();
Just van Rossum52e14d62002-12-30 22:08:05 +0000299
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000300 /* Initialize _warnings. */
301 _PyWarnings_Init();
302
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000303 _PyTime_Init();
304
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200305 if (initfsencoding(interp) < 0)
306 Py_FatalError("Py_Initialize: unable to load the file system codec");
Martin v. Löwis011e8422009-05-05 04:43:17 +0000307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 if (install_sigs)
309 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 initmain(); /* Module __main__ */
312 if (initstdio() < 0)
313 Py_FatalError(
314 "Py_Initialize: can't initialize sys standard streams");
315
Antoine Pitroucf9f9802010-11-10 13:55:25 +0000316 /* Initialize warnings. */
317 if (PySys_HasWarnOptions()) {
318 PyObject *warnings_module = PyImport_ImportModule("warnings");
319 if (warnings_module == NULL) {
320 fprintf(stderr, "'import warnings' failed; traceback:\n");
321 PyErr_Print();
322 }
323 Py_XDECREF(warnings_module);
324 }
325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 if (!Py_NoSiteFlag)
327 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000328}
329
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000330void
331Py_Initialize(void)
332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 Py_InitializeEx(1);
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000334}
335
336
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000337#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000339#endif
340
Guido van Rossume8432ac2007-07-09 15:04:50 +0000341/* Flush stdout and stderr */
342
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100343static int
344file_is_closed(PyObject *fobj)
345{
346 int r;
347 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
348 if (tmp == NULL) {
349 PyErr_Clear();
350 return 0;
351 }
352 r = PyObject_IsTrue(tmp);
353 Py_DECREF(tmp);
354 if (r < 0)
355 PyErr_Clear();
356 return r > 0;
357}
358
Neal Norwitz2bad9702007-08-27 06:19:22 +0000359static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000360flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 PyObject *fout = PySys_GetObject("stdout");
363 PyObject *ferr = PySys_GetObject("stderr");
364 PyObject *tmp;
Guido van Rossume8432ac2007-07-09 15:04:50 +0000365
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100366 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 tmp = PyObject_CallMethod(fout, "flush", "");
368 if (tmp == NULL)
Antoine Pitroubddc9fe2010-08-08 20:46:42 +0000369 PyErr_WriteUnraisable(fout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 else
371 Py_DECREF(tmp);
372 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000373
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100374 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 tmp = PyObject_CallMethod(ferr, "flush", "");
376 if (tmp == NULL)
377 PyErr_Clear();
378 else
379 Py_DECREF(tmp);
380 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000381}
382
Guido van Rossum25ce5661997-08-02 03:10:38 +0000383/* Undo the effect of Py_Initialize().
384
385 Beware: if multiple interpreter and/or thread states exist, these
386 are not wiped out; only the current thread and interpreter state
387 are deleted. But since everything else is deleted, those other
388 interpreter and thread states should no longer be used.
389
390 (XXX We should do better, e.g. wipe out all interpreters and
391 threads.)
392
393 Locking: as above.
394
395*/
396
397void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000398Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 PyInterpreterState *interp;
401 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (!initialized)
404 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 wait_for_thread_shutdown();
Antoine Pitrou011bd622009-10-20 21:52:47 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 /* The interpreter is still entirely intact at this point, and the
409 * exit funcs may be relying on that. In particular, if some thread
410 * or exit func is still waiting to do an import, the import machinery
411 * expects Py_IsInitialized() to return true. So don't say the
412 * interpreter is uninitialized until after the exit funcs have run.
413 * Note that Threading.py uses an exit func to do a join on all the
414 * threads created thru it, so this also protects pending imports in
415 * the threads created via Threading.
416 */
417 call_py_exitfuncs();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 /* Get current thread state and interpreter pointer */
420 tstate = PyThreadState_GET();
421 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000422
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200423 /* Remaining threads (e.g. daemon threads) will automatically exit
424 after taking the GIL (in PyEval_RestoreThread()). */
425 _Py_Finalizing = tstate;
426 initialized = 0;
427
428 /* Flush stdout+stderr */
429 flush_std_files();
430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 /* Disable signal handling */
432 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 /* Clear type lookup cache */
435 PyType_ClearCache();
Christian Heimes26855632008-01-27 23:50:43 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 /* Collect garbage. This may call finalizers; it's nice to call these
438 * before all modules are destroyed.
439 * XXX If a __del__ or weakref callback is triggered here, and tries to
440 * XXX import a module, bad things can happen, because Python no
441 * XXX longer believes it's initialized.
442 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
443 * XXX is easy to provoke that way. I've also seen, e.g.,
444 * XXX Exception exceptions.ImportError: 'No module named sha'
445 * XXX in <function callback at 0x008F5718> ignored
446 * XXX but I'm unclear on exactly how that one happens. In any case,
447 * XXX I haven't seen a real-life report of either of these.
448 */
449 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000450#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 /* With COUNT_ALLOCS, it helps to run GC multiple times:
452 each collection might release some types from the type
453 list, so they become garbage. */
454 while (PyGC_Collect() > 0)
455 /* nothing */;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000456#endif
Antoine Pitrou696e0352010-08-08 22:18:46 +0000457 /* We run this while most interpreter state is still alive, so that
458 debug information can be printed out */
459 _PyGC_Fini();
Guido van Rossume13ddc92003-04-17 17:29:22 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 /* Destroy all modules */
462 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 /* Flush stdout+stderr (again, in case more was printed) */
465 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 /* Collect final garbage. This disposes of cycles created by
468 * new-style class definitions, for example.
469 * XXX This is disabled because it caused too many problems. If
470 * XXX a __del__ or weakref callback triggers here, Python code has
471 * XXX a hard time running, because even the sys module has been
472 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
473 * XXX One symptom is a sequence of information-free messages
474 * XXX coming from threads (if a __del__ or callback is invoked,
475 * XXX other threads can execute too, and any exception they encounter
476 * XXX triggers a comedy of errors as subsystem after subsystem
477 * XXX fails to find what it *expects* to find in sys to help report
478 * XXX the exception and consequent unexpected failures). I've also
479 * XXX seen segfaults then, after adding print statements to the
480 * XXX Python code getting called.
481 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000482#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000484#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
487 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000490#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000492#endif
493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000495
Tim Peters9cf25ce2003-04-17 15:21:01 +0000496#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 /* Display all objects still alive -- this can invoke arbitrary
498 * __repr__ overrides, so requires a mostly-intact interpreter.
499 * Alas, a lot of stuff may still be alive now that will be cleaned
500 * up later.
501 */
502 if (Py_GETENV("PYTHONDUMPREFS"))
503 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000504#endif /* Py_TRACE_REFS */
505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 /* Clear interpreter state */
507 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 /* Now we decref the exception classes. After this point nothing
510 can raise an exception. That's okay, because each Fini() method
511 below has been checked to make sure no exceptions are ever
512 raised.
513 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 /* Cleanup auto-thread-state */
Christian Heimes7d2ff882007-11-30 14:35:04 +0000518#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 _PyGILState_Fini();
Christian Heimes7d2ff882007-11-30 14:35:04 +0000520#endif /* WITH_THREAD */
521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 /* Delete current thread */
523 PyThreadState_Swap(NULL);
524 PyInterpreterState_Delete(interp);
Barry Warsawf242aa02000-05-25 23:09:49 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 /* Sundry finalizers */
527 PyMethod_Fini();
528 PyFrame_Fini();
529 PyCFunction_Fini();
530 PyTuple_Fini();
531 PyList_Fini();
532 PySet_Fini();
533 PyBytes_Fini();
534 PyByteArray_Fini();
535 PyLong_Fini();
536 PyFloat_Fini();
537 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 /* Cleanup Unicode implementation */
540 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 /* reset file system default encoding */
Victor Stinnerb744ba12010-05-15 12:27:16 +0000543 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 free((char*)Py_FileSystemDefaultEncoding);
545 Py_FileSystemDefaultEncoding = NULL;
546 }
Christian Heimesc8967002007-11-30 10:18:26 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 /* XXX Still allocated:
549 - various static ad-hoc pointers to interned strings
550 - int and float free list blocks
551 - whatever various modules and libraries allocate
552 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000555
Tim Peters269b2a62003-04-17 19:52:29 +0000556#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* Display addresses (& refcnts) of all objects still alive.
558 * An address can be used to find the repr of the object, printed
559 * above by _Py_PrintReferences.
560 */
561 if (Py_GETENV("PYTHONDUMPREFS"))
562 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000563#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000564#ifdef PYMALLOC_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 if (Py_GETENV("PYTHONMALLOCSTATS"))
566 _PyObject_DebugMallocStats();
Tim Peters0e871182002-04-13 08:29:14 +0000567#endif
568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000570}
571
572/* Create and initialize a new interpreter and thread, and return the
573 new thread. This requires that Py_Initialize() has been called
574 first.
575
576 Unsuccessful initialization yields a NULL pointer. Note that *no*
577 exception information is available even in this case -- the
578 exception information is held in the thread, and there is no
579 thread.
580
581 Locking: as above.
582
583*/
584
585PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000586Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 PyInterpreterState *interp;
589 PyThreadState *tstate, *save_tstate;
590 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 if (!initialized)
593 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 interp = PyInterpreterState_New();
596 if (interp == NULL)
597 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 tstate = PyThreadState_New(interp);
600 if (tstate == NULL) {
601 PyInterpreterState_Delete(interp);
602 return NULL;
603 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 interp->modules = PyDict_New();
610 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000611
Victor Stinner49d3f252010-10-17 01:24:53 +0000612 bimod = _PyImport_FindBuiltin("builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 if (bimod != NULL) {
614 interp->builtins = PyModule_GetDict(bimod);
615 if (interp->builtins == NULL)
616 goto handle_error;
617 Py_INCREF(interp->builtins);
618 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 /* initialize builtin exceptions */
621 _PyExc_Init();
Christian Heimes6a27efa2008-10-30 21:48:26 +0000622
Victor Stinner49d3f252010-10-17 01:24:53 +0000623 sysmod = _PyImport_FindBuiltin("sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 if (bimod != NULL && sysmod != NULL) {
625 PyObject *pstderr;
626 interp->sysdict = PyModule_GetDict(sysmod);
627 if (interp->sysdict == NULL)
628 goto handle_error;
629 Py_INCREF(interp->sysdict);
630 PySys_SetPath(Py_GetPath());
631 PyDict_SetItemString(interp->sysdict, "modules",
632 interp->modules);
633 /* Set up a preliminary stderr printer until we have enough
634 infrastructure for the io module in place. */
635 pstderr = PyFile_NewStdPrinter(fileno(stderr));
636 if (pstderr == NULL)
637 Py_FatalError("Py_Initialize: can't set preliminary stderr");
638 PySys_SetObject("stderr", pstderr);
639 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000640 Py_DECREF(pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 _PyImportHooks_Init();
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200643
644 if (initfsencoding(interp) < 0)
645 goto handle_error;
646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 if (initstdio() < 0)
648 Py_FatalError(
649 "Py_Initialize: can't initialize sys standard streams");
650 initmain();
651 if (!Py_NoSiteFlag)
652 initsite();
653 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if (!PyErr_Occurred())
656 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000657
Thomas Wouters89f507f2006-12-13 04:49:30 +0000658handle_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000660
Victor Stinner11889352011-04-27 00:20:27 +0200661 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 PyThreadState_Clear(tstate);
663 PyThreadState_Swap(save_tstate);
664 PyThreadState_Delete(tstate);
665 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000668}
669
670/* Delete an interpreter and its last thread. This requires that the
671 given thread state is current, that the thread has no remaining
672 frames, and that it is its interpreter's only remaining thread.
673 It is a fatal error to violate these constraints.
674
675 (Py_Finalize() doesn't have these constraints -- it zaps
676 everything, regardless.)
677
678 Locking: as above.
679
680*/
681
682void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000683Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 if (tstate != PyThreadState_GET())
688 Py_FatalError("Py_EndInterpreter: thread is not current");
689 if (tstate->frame != NULL)
690 Py_FatalError("Py_EndInterpreter: thread still has a frame");
691 if (tstate != interp->tstate_head || tstate->next != NULL)
692 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 PyImport_Cleanup();
695 PyInterpreterState_Clear(interp);
696 PyThreadState_Swap(NULL);
697 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000698}
699
Martin v. Löwis790465f2008-04-05 20:41:37 +0000700static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000701
702void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000703Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 if (pn && *pn)
706 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000707}
708
Martin v. Löwis790465f2008-04-05 20:41:37 +0000709wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000710Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000713}
714
Martin v. Löwis790465f2008-04-05 20:41:37 +0000715static wchar_t *default_home = NULL;
716static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000717
718void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000719Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000722}
723
Martin v. Löwis790465f2008-04-05 20:41:37 +0000724wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000725Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 wchar_t *home = default_home;
728 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
729 char* chome = Py_GETENV("PYTHONHOME");
730 if (chome) {
731 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
732 if (r != (size_t)-1 && r <= PATH_MAX)
733 home = env_home;
734 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 }
737 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000738}
739
Guido van Rossum6135a871995-01-09 17:53:26 +0000740/* Create __main__ module */
741
742static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000743initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 PyObject *m, *d;
746 m = PyImport_AddModule("__main__");
747 if (m == NULL)
748 Py_FatalError("can't create __main__ module");
749 d = PyModule_GetDict(m);
750 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
751 PyObject *bimod = PyImport_ImportModule("builtins");
752 if (bimod == NULL ||
753 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
754 Py_FatalError("can't add __builtins__ to __main__");
755 Py_DECREF(bimod);
756 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000757}
758
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200759static int
760initfsencoding(PyInterpreterState *interp)
Victor Stinnerb744ba12010-05-15 12:27:16 +0000761{
762 PyObject *codec;
763#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94908bb2010-08-18 21:23:25 +0000764 char *codeset = NULL;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000765
Victor Stinner7f84ab52010-06-11 00:36:33 +0000766 if (Py_FileSystemDefaultEncoding == NULL) {
Victor Stinner8f6b6b02010-10-13 22:02:27 +0000767 /* On Unix, set the file system encoding according to the
768 user's preference, if the CODESET names a well-known
769 Python codec, and Py_FileSystemDefaultEncoding isn't
Victor Stinnere4743092010-10-19 00:05:51 +0000770 initialized by other means. */
Victor Stinner8f6b6b02010-10-13 22:02:27 +0000771 codeset = get_codeset();
Victor Stinnere4743092010-10-19 00:05:51 +0000772 if (codeset == NULL)
773 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
Victor Stinnerb744ba12010-05-15 12:27:16 +0000774
Victor Stinnere4743092010-10-19 00:05:51 +0000775 Py_FileSystemDefaultEncoding = codeset;
776 Py_HasFileSystemDefaultEncoding = 0;
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200777 interp->fscodec_initialized = 1;
778 return 0;
Victor Stinner7f84ab52010-06-11 00:36:33 +0000779 }
Victor Stinnerb744ba12010-05-15 12:27:16 +0000780#endif
781
782 /* the encoding is mbcs, utf-8 or ascii */
783 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
784 if (!codec) {
785 /* Such error can only occurs in critical situations: no more
786 * memory, import a module of the standard library failed,
787 * etc. */
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200788 return -1;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000789 }
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200790 Py_DECREF(codec);
791 interp->fscodec_initialized = 1;
792 return 0;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000793}
794
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000795/* Import the site module (not into __main__ though) */
796
797static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000798initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 PyObject *m;
801 m = PyImport_ImportModule("site");
802 if (m == NULL) {
803 PyErr_Print();
804 Py_Finalize();
805 exit(1);
806 }
807 else {
808 Py_DECREF(m);
809 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000810}
811
Antoine Pitrou05608432009-01-09 18:53:14 +0000812static PyObject*
813create_stdio(PyObject* io,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 int fd, int write_mode, char* name,
815 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +0000816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
818 const char* mode;
Victor Stinner02bfdb32011-02-23 12:10:23 +0000819 const char* newline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 PyObject *line_buffering;
821 int buffering, isatty;
Antoine Pitrou05608432009-01-09 18:53:14 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 /* stdin is always opened in buffered mode, first because it shouldn't
824 make a difference in common use cases, second because TextIOWrapper
825 depends on the presence of a read1() method which only exists on
826 buffered streams.
827 */
828 if (Py_UnbufferedStdioFlag && write_mode)
829 buffering = 0;
830 else
831 buffering = -1;
832 if (write_mode)
833 mode = "wb";
834 else
835 mode = "rb";
836 buf = PyObject_CallMethod(io, "open", "isiOOOi",
837 fd, mode, buffering,
838 Py_None, Py_None, Py_None, 0);
839 if (buf == NULL)
840 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 if (buffering) {
843 raw = PyObject_GetAttrString(buf, "raw");
844 if (raw == NULL)
845 goto error;
846 }
847 else {
848 raw = buf;
849 Py_INCREF(raw);
850 }
Antoine Pitrou05608432009-01-09 18:53:14 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 text = PyUnicode_FromString(name);
853 if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0)
854 goto error;
855 res = PyObject_CallMethod(raw, "isatty", "");
856 if (res == NULL)
857 goto error;
858 isatty = PyObject_IsTrue(res);
859 Py_DECREF(res);
860 if (isatty == -1)
861 goto error;
862 if (isatty || Py_UnbufferedStdioFlag)
863 line_buffering = Py_True;
864 else
865 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 Py_CLEAR(raw);
868 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +0000869
Victor Stinner02bfdb32011-02-23 12:10:23 +0000870 newline = "\n";
871#ifdef MS_WINDOWS
872 if (!write_mode) {
873 /* translate \r\n to \n for sys.stdin on Windows */
874 newline = NULL;
875 }
876#endif
877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO",
879 buf, encoding, errors,
Victor Stinner02bfdb32011-02-23 12:10:23 +0000880 newline, line_buffering);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 Py_CLEAR(buf);
882 if (stream == NULL)
883 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 if (write_mode)
886 mode = "w";
887 else
888 mode = "r";
889 text = PyUnicode_FromString(mode);
890 if (!text || PyObject_SetAttrString(stream, "mode", text) < 0)
891 goto error;
892 Py_CLEAR(text);
893 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +0000894
895error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 Py_XDECREF(buf);
897 Py_XDECREF(stream);
898 Py_XDECREF(text);
899 Py_XDECREF(raw);
900 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +0000901}
902
Antoine Pitrou11942a52011-11-28 19:08:36 +0100903static int
904is_valid_fd(int fd)
905{
906 int dummy_fd;
907 if (fd < 0 || !_PyVerify_fd(fd))
908 return 0;
909 dummy_fd = dup(fd);
910 if (dummy_fd < 0)
911 return 0;
912 close(dummy_fd);
913 return 1;
914}
915
Georg Brandl1a3284e2007-12-02 09:40:06 +0000916/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000917static int
918initstdio(void)
919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 PyObject *iomod = NULL, *wrapper;
921 PyObject *bimod = NULL;
922 PyObject *m;
923 PyObject *std = NULL;
924 int status = 0, fd;
925 PyObject * encoding_attr;
926 char *encoding = NULL, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* Hack to avoid a nasty recursion issue when Python is invoked
929 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
930 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
931 goto error;
932 }
933 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
936 goto error;
937 }
938 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 if (!(bimod = PyImport_ImportModule("builtins"))) {
941 goto error;
942 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 if (!(iomod = PyImport_ImportModule("io"))) {
945 goto error;
946 }
947 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
948 goto error;
949 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 /* Set builtins.open */
952 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000953 Py_DECREF(wrapper);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 goto error;
955 }
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000956 Py_DECREF(wrapper);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 encoding = Py_GETENV("PYTHONIOENCODING");
959 errors = NULL;
960 if (encoding) {
961 encoding = strdup(encoding);
962 errors = strchr(encoding, ':');
963 if (errors) {
964 *errors = '\0';
965 errors++;
966 }
967 }
Martin v. Löwis0f599892008-06-02 11:13:03 +0000968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 /* Set sys.stdin */
970 fd = fileno(stdin);
971 /* Under some conditions stdin, stdout and stderr may not be connected
972 * and fileno() may point to an invalid file descriptor. For example
973 * GUI apps don't have valid standard streams by default.
974 */
Antoine Pitrou11942a52011-11-28 19:08:36 +0100975 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 std = Py_None;
977 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 }
979 else {
980 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
981 if (std == NULL)
982 goto error;
983 } /* if (fd < 0) */
984 PySys_SetObject("__stdin__", std);
985 PySys_SetObject("stdin", std);
986 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 /* Set sys.stdout */
989 fd = fileno(stdout);
Antoine Pitrou11942a52011-11-28 19:08:36 +0100990 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 std = Py_None;
992 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 }
994 else {
995 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
996 if (std == NULL)
997 goto error;
998 } /* if (fd < 0) */
999 PySys_SetObject("__stdout__", std);
1000 PySys_SetObject("stdout", std);
1001 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001002
Guido van Rossum98297ee2007-11-06 21:34:58 +00001003#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 /* Set sys.stderr, replaces the preliminary stderr */
1005 fd = fileno(stderr);
Antoine Pitrou11942a52011-11-28 19:08:36 +01001006 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 std = Py_None;
1008 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 }
1010 else {
1011 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1012 if (std == NULL)
1013 goto error;
1014 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 /* Same as hack above, pre-import stderr's codec to avoid recursion
1017 when import.c tries to write to stderr in verbose mode. */
1018 encoding_attr = PyObject_GetAttrString(std, "encoding");
1019 if (encoding_attr != NULL) {
1020 const char * encoding;
1021 encoding = _PyUnicode_AsString(encoding_attr);
1022 if (encoding != NULL) {
Antoine Pitrou2fabfac2012-01-18 15:14:46 +01001023 PyObject *codec_info = _PyCodec_Lookup(encoding);
1024 Py_XDECREF(codec_info);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 }
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +00001026 Py_DECREF(encoding_attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 }
1028 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PySys_SetObject("__stderr__", std);
1031 PySys_SetObject("stderr", std);
1032 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001033#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001036 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 status = -1;
1038 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 if (encoding)
1041 free(encoding);
1042 Py_XDECREF(bimod);
1043 Py_XDECREF(iomod);
1044 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001045}
1046
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001047/* Parse input from a file and execute it */
1048
1049int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001050PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 if (filename == NULL)
1054 filename = "???";
1055 if (Py_FdIsInteractive(fp, filename)) {
1056 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
1057 if (closeit)
1058 fclose(fp);
1059 return err;
1060 }
1061 else
1062 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001063}
1064
1065int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001066PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 PyObject *v;
1069 int ret;
1070 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 if (flags == NULL) {
1073 flags = &local_flags;
1074 local_flags.cf_flags = 0;
1075 }
1076 v = PySys_GetObject("ps1");
1077 if (v == NULL) {
1078 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
1079 Py_XDECREF(v);
1080 }
1081 v = PySys_GetObject("ps2");
1082 if (v == NULL) {
1083 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1084 Py_XDECREF(v);
1085 }
1086 for (;;) {
1087 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
1088 PRINT_TOTAL_REFS();
1089 if (ret == E_EOF)
1090 return 0;
1091 /*
1092 if (ret == E_NOMEM)
1093 return -1;
1094 */
1095 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001096}
1097
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001098/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001099static int PARSER_FLAGS(PyCompilerFlags *flags)
1100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 int parser_flags = 0;
1102 if (!flags)
1103 return 0;
1104 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1105 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1106 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1107 parser_flags |= PyPARSE_IGNORE_COOKIE;
1108 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1109 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1110 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001111}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001112
Thomas Wouters89f507f2006-12-13 04:49:30 +00001113#if 0
1114/* Keep an example of flags with future keyword support. */
1115#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1117 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1118 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1119 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001120#endif
1121
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001122int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001123PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 PyObject *m, *d, *v, *w, *oenc = NULL;
1126 mod_ty mod;
1127 PyArena *arena;
1128 char *ps1 = "", *ps2 = "", *enc = NULL;
1129 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +00001130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (fp == stdin) {
1132 /* Fetch encoding from sys.stdin */
1133 v = PySys_GetObject("stdin");
1134 if (v == NULL || v == Py_None)
1135 return -1;
1136 oenc = PyObject_GetAttrString(v, "encoding");
1137 if (!oenc)
1138 return -1;
1139 enc = _PyUnicode_AsString(oenc);
Victor Stinner386fe712010-05-19 00:34:15 +00001140 if (enc == NULL)
1141 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 }
1143 v = PySys_GetObject("ps1");
1144 if (v != NULL) {
1145 v = PyObject_Str(v);
1146 if (v == NULL)
1147 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001148 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +00001150 if (ps1 == NULL) {
1151 PyErr_Clear();
1152 ps1 = "";
1153 }
1154 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 }
1156 w = PySys_GetObject("ps2");
1157 if (w != NULL) {
1158 w = PyObject_Str(w);
1159 if (w == NULL)
1160 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001161 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +00001163 if (ps2 == NULL) {
1164 PyErr_Clear();
1165 ps2 = "";
1166 }
1167 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 }
1169 arena = PyArena_New();
1170 if (arena == NULL) {
1171 Py_XDECREF(v);
1172 Py_XDECREF(w);
1173 Py_XDECREF(oenc);
1174 return -1;
1175 }
1176 mod = PyParser_ASTFromFile(fp, filename, enc,
1177 Py_single_input, ps1, ps2,
1178 flags, &errcode, arena);
1179 Py_XDECREF(v);
1180 Py_XDECREF(w);
1181 Py_XDECREF(oenc);
1182 if (mod == NULL) {
1183 PyArena_Free(arena);
1184 if (errcode == E_EOF) {
1185 PyErr_Clear();
1186 return E_EOF;
1187 }
1188 PyErr_Print();
1189 return -1;
1190 }
1191 m = PyImport_AddModule("__main__");
1192 if (m == NULL) {
1193 PyArena_Free(arena);
1194 return -1;
1195 }
1196 d = PyModule_GetDict(m);
1197 v = run_mod(mod, filename, d, d, flags, arena);
1198 PyArena_Free(arena);
1199 flush_io();
1200 if (v == NULL) {
1201 PyErr_Print();
1202 return -1;
1203 }
1204 Py_DECREF(v);
1205 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001206}
1207
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001208/* Check whether a file maybe a pyc file: Look at the extension,
1209 the file type, and, if we may close it, at the first few bytes. */
1210
1211static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001212maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1215 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 /* Only look into the file if we are allowed to close it, since
1218 it then should also be seekable. */
1219 if (closeit) {
1220 /* Read only two bytes of the magic. If the file was opened in
1221 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1222 be read as they are on disk. */
1223 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1224 unsigned char buf[2];
1225 /* Mess: In case of -x, the stream is NOT at its start now,
1226 and ungetc() was used to push back the first newline,
1227 which makes the current stream position formally undefined,
1228 and a x-platform nightmare.
1229 Unfortunately, we have no direct way to know whether -x
1230 was specified. So we use a terrible hack: if the current
1231 stream position is not 0, we assume -x was specified, and
1232 give up. Bug 132850 on SourceForge spells out the
1233 hopelessness of trying anything else (fseek and ftell
1234 don't work predictably x-platform for text-mode files).
1235 */
1236 int ispyc = 0;
1237 if (ftell(fp) == 0) {
1238 if (fread(buf, 1, 2, fp) == 2 &&
1239 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1240 ispyc = 1;
1241 rewind(fp);
1242 }
1243 return ispyc;
1244 }
1245 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001246}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001247
Guido van Rossum0df002c2000-08-27 19:21:52 +00001248int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001249PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 PyObject *m, *d, *v;
1253 const char *ext;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001254 int set_file_name = 0, ret;
1255 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 m = PyImport_AddModule("__main__");
1258 if (m == NULL)
1259 return -1;
1260 d = PyModule_GetDict(m);
1261 if (PyDict_GetItemString(d, "__file__") == NULL) {
1262 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001263 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 if (f == NULL)
1265 return -1;
1266 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1267 Py_DECREF(f);
1268 return -1;
1269 }
Barry Warsaw916048d2011-09-20 14:45:44 -04001270 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1271 Py_DECREF(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 return -1;
Barry Warsaw916048d2011-09-20 14:45:44 -04001273 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 set_file_name = 1;
1275 Py_DECREF(f);
1276 }
1277 len = strlen(filename);
1278 ext = filename + len - (len > 4 ? 4 : 0);
1279 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1280 /* Try to run a pyc file. First, re-open in binary */
1281 if (closeit)
1282 fclose(fp);
1283 if ((fp = fopen(filename, "rb")) == NULL) {
1284 fprintf(stderr, "python: Can't reopen .pyc file\n");
1285 ret = -1;
1286 goto done;
1287 }
1288 /* Turn on optimization if a .pyo file is given */
1289 if (strcmp(ext, ".pyo") == 0)
1290 Py_OptimizeFlag = 1;
1291 v = run_pyc_file(fp, filename, d, d, flags);
1292 } else {
1293 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1294 closeit, flags);
1295 }
1296 flush_io();
1297 if (v == NULL) {
1298 PyErr_Print();
1299 ret = -1;
1300 goto done;
1301 }
1302 Py_DECREF(v);
1303 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001304 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1306 PyErr_Clear();
1307 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001308}
1309
1310int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001311PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 PyObject *m, *d, *v;
1314 m = PyImport_AddModule("__main__");
1315 if (m == NULL)
1316 return -1;
1317 d = PyModule_GetDict(m);
1318 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1319 if (v == NULL) {
1320 PyErr_Print();
1321 return -1;
1322 }
1323 Py_DECREF(v);
1324 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001325}
1326
Barry Warsaw035574d1997-08-29 22:07:17 +00001327static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001328parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 long hold;
1332 PyObject *v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 /* old style errors */
1335 if (PyTuple_Check(err))
1336 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1337 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 /* new style errors. `err' is an instance */
Barry Warsaw035574d1997-08-29 22:07:17 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 if (! (v = PyObject_GetAttrString(err, "msg")))
1342 goto finally;
1343 *message = v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 if (!(v = PyObject_GetAttrString(err, "filename")))
1346 goto finally;
1347 if (v == Py_None)
1348 *filename = NULL;
1349 else if (! (*filename = _PyUnicode_AsString(v)))
1350 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 Py_DECREF(v);
1353 if (!(v = PyObject_GetAttrString(err, "lineno")))
1354 goto finally;
1355 hold = PyLong_AsLong(v);
1356 Py_DECREF(v);
1357 v = NULL;
1358 if (hold < 0 && PyErr_Occurred())
1359 goto finally;
1360 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 if (!(v = PyObject_GetAttrString(err, "offset")))
1363 goto finally;
1364 if (v == Py_None) {
1365 *offset = -1;
1366 Py_DECREF(v);
1367 v = NULL;
1368 } else {
1369 hold = PyLong_AsLong(v);
1370 Py_DECREF(v);
1371 v = NULL;
1372 if (hold < 0 && PyErr_Occurred())
1373 goto finally;
1374 *offset = (int)hold;
1375 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (!(v = PyObject_GetAttrString(err, "text")))
1378 goto finally;
1379 if (v == Py_None)
1380 *text = NULL;
1381 else if (!PyUnicode_Check(v) ||
1382 !(*text = _PyUnicode_AsString(v)))
1383 goto finally;
1384 Py_DECREF(v);
1385 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001386
1387finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 Py_XDECREF(v);
1389 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001390}
1391
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001392void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001393PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001396}
1397
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001398static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001399print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 char *nl;
1402 if (offset >= 0) {
Benjamin Petersona95e9772010-10-29 03:28:14 +00001403 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1404 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 for (;;) {
1406 nl = strchr(text, '\n');
1407 if (nl == NULL || nl-text >= offset)
1408 break;
1409 offset -= (int)(nl+1-text);
1410 text = nl+1;
1411 }
1412 while (*text == ' ' || *text == '\t') {
1413 text++;
1414 offset--;
1415 }
1416 }
1417 PyFile_WriteString(" ", f);
1418 PyFile_WriteString(text, f);
1419 if (*text == '\0' || text[strlen(text)-1] != '\n')
1420 PyFile_WriteString("\n", f);
1421 if (offset == -1)
1422 return;
1423 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +00001424 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001427}
1428
Guido van Rossum66e8e862001-03-23 17:54:43 +00001429static void
1430handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 PyObject *exception, *value, *tb;
1433 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (Py_InspectFlag)
1436 /* Don't exit if -i flag was given. This flag is set to 0
1437 * when entering interactive mode for inspecting. */
1438 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 PyErr_Fetch(&exception, &value, &tb);
1441 fflush(stdout);
1442 if (value == NULL || value == Py_None)
1443 goto done;
1444 if (PyExceptionInstance_Check(value)) {
1445 /* The error code should be in the `code' attribute. */
1446 PyObject *code = PyObject_GetAttrString(value, "code");
1447 if (code) {
1448 Py_DECREF(value);
1449 value = code;
1450 if (value == Py_None)
1451 goto done;
1452 }
1453 /* If we failed to dig out the 'code' attribute,
1454 just let the else clause below print the error. */
1455 }
1456 if (PyLong_Check(value))
1457 exitcode = (int)PyLong_AsLong(value);
1458 else {
Victor Stinnere9fb3192010-05-17 08:58:51 +00001459 PyObject *sys_stderr = PySys_GetObject("stderr");
Victor Stinner7126dbc2010-05-21 23:45:42 +00001460 if (sys_stderr != NULL && sys_stderr != Py_None) {
1461 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1462 } else {
1463 PyObject_Print(value, stderr, Py_PRINT_RAW);
1464 fflush(stderr);
1465 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 PySys_WriteStderr("\n");
1467 exitcode = 1;
1468 }
Tim Peterscf615b52003-04-19 18:47:02 +00001469 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 /* Restore and clear the exception info, in order to properly decref
1471 * the exception, value, and traceback. If we just exit instead,
1472 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1473 * some finalizers from running.
1474 */
1475 PyErr_Restore(exception, value, tb);
1476 PyErr_Clear();
1477 Py_Exit(exitcode);
1478 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001479}
1480
1481void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001482PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1487 handle_system_exit();
1488 }
1489 PyErr_Fetch(&exception, &v, &tb);
1490 if (exception == NULL)
1491 return;
1492 PyErr_NormalizeException(&exception, &v, &tb);
1493 if (tb == NULL) {
1494 tb = Py_None;
1495 Py_INCREF(tb);
1496 }
1497 PyException_SetTraceback(v, tb);
1498 if (exception == NULL)
1499 return;
1500 /* Now we know v != NULL too */
1501 if (set_sys_last_vars) {
1502 PySys_SetObject("last_type", exception);
1503 PySys_SetObject("last_value", v);
1504 PySys_SetObject("last_traceback", tb);
1505 }
1506 hook = PySys_GetObject("excepthook");
1507 if (hook) {
1508 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1509 PyObject *result = PyEval_CallObject(hook, args);
1510 if (result == NULL) {
1511 PyObject *exception2, *v2, *tb2;
1512 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1513 handle_system_exit();
1514 }
1515 PyErr_Fetch(&exception2, &v2, &tb2);
1516 PyErr_NormalizeException(&exception2, &v2, &tb2);
1517 /* It should not be possible for exception2 or v2
1518 to be NULL. However PyErr_Display() can't
1519 tolerate NULLs, so just be safe. */
1520 if (exception2 == NULL) {
1521 exception2 = Py_None;
1522 Py_INCREF(exception2);
1523 }
1524 if (v2 == NULL) {
1525 v2 = Py_None;
1526 Py_INCREF(v2);
1527 }
1528 fflush(stdout);
1529 PySys_WriteStderr("Error in sys.excepthook:\n");
1530 PyErr_Display(exception2, v2, tb2);
1531 PySys_WriteStderr("\nOriginal exception was:\n");
1532 PyErr_Display(exception, v, tb);
1533 Py_DECREF(exception2);
1534 Py_DECREF(v2);
1535 Py_XDECREF(tb2);
1536 }
1537 Py_XDECREF(result);
1538 Py_XDECREF(args);
1539 } else {
1540 PySys_WriteStderr("sys.excepthook is missing\n");
1541 PyErr_Display(exception, v, tb);
1542 }
1543 Py_XDECREF(exception);
1544 Py_XDECREF(v);
1545 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001546}
1547
Benjamin Petersone6528212008-07-15 15:32:09 +00001548static void
1549print_exception(PyObject *f, PyObject *value)
1550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 int err = 0;
1552 PyObject *type, *tb;
Benjamin Petersone6528212008-07-15 15:32:09 +00001553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 if (!PyExceptionInstance_Check(value)) {
1555 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1556 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1557 PyFile_WriteString(" found\n", f);
1558 return;
1559 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 Py_INCREF(value);
1562 fflush(stdout);
1563 type = (PyObject *) Py_TYPE(value);
1564 tb = PyException_GetTraceback(value);
1565 if (tb && tb != Py_None)
1566 err = PyTraceBack_Print(tb, f);
1567 if (err == 0 &&
1568 PyObject_HasAttrString(value, "print_file_and_line"))
1569 {
1570 PyObject *message;
1571 const char *filename, *text;
1572 int lineno, offset;
1573 if (!parse_syntax_error(value, &message, &filename,
1574 &lineno, &offset, &text))
1575 PyErr_Clear();
1576 else {
1577 char buf[10];
1578 PyFile_WriteString(" File \"", f);
1579 if (filename == NULL)
1580 PyFile_WriteString("<string>", f);
1581 else
1582 PyFile_WriteString(filename, f);
1583 PyFile_WriteString("\", line ", f);
1584 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1585 PyFile_WriteString(buf, f);
1586 PyFile_WriteString("\n", f);
1587 if (text != NULL)
1588 print_error_text(f, offset, text);
1589 Py_DECREF(value);
1590 value = message;
1591 /* Can't be bothered to check all those
1592 PyFile_WriteString() calls */
1593 if (PyErr_Occurred())
1594 err = -1;
1595 }
1596 }
1597 if (err) {
1598 /* Don't do anything else */
1599 }
1600 else {
1601 PyObject* moduleName;
1602 char* className;
1603 assert(PyExceptionClass_Check(type));
1604 className = PyExceptionClass_Name(type);
1605 if (className != NULL) {
1606 char *dot = strrchr(className, '.');
1607 if (dot != NULL)
1608 className = dot+1;
1609 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 moduleName = PyObject_GetAttrString(type, "__module__");
1612 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1613 {
1614 Py_DECREF(moduleName);
1615 err = PyFile_WriteString("<unknown>", f);
1616 }
1617 else {
1618 char* modstr = _PyUnicode_AsString(moduleName);
1619 if (modstr && strcmp(modstr, "builtins"))
1620 {
1621 err = PyFile_WriteString(modstr, f);
1622 err += PyFile_WriteString(".", f);
1623 }
1624 Py_DECREF(moduleName);
1625 }
1626 if (err == 0) {
1627 if (className == NULL)
1628 err = PyFile_WriteString("<unknown>", f);
1629 else
1630 err = PyFile_WriteString(className, f);
1631 }
1632 }
1633 if (err == 0 && (value != Py_None)) {
1634 PyObject *s = PyObject_Str(value);
1635 /* only print colon if the str() of the
1636 object is not the empty string
1637 */
1638 if (s == NULL)
1639 err = -1;
1640 else if (!PyUnicode_Check(s) ||
1641 PyUnicode_GetSize(s) != 0)
1642 err = PyFile_WriteString(": ", f);
1643 if (err == 0)
1644 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1645 Py_XDECREF(s);
1646 }
1647 /* try to write a newline in any case */
1648 err += PyFile_WriteString("\n", f);
1649 Py_XDECREF(tb);
1650 Py_DECREF(value);
1651 /* If an error happened here, don't show it.
1652 XXX This is wrong, but too many callers rely on this behavior. */
1653 if (err != 0)
1654 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001655}
1656
1657static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 "\nThe above exception was the direct cause "
1659 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001660
1661static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 "\nDuring handling of the above exception, "
1663 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001664
1665static void
1666print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 int err = 0, res;
1669 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 if (seen != NULL) {
1672 /* Exception chaining */
1673 if (PySet_Add(seen, value) == -1)
1674 PyErr_Clear();
1675 else if (PyExceptionInstance_Check(value)) {
1676 cause = PyException_GetCause(value);
1677 context = PyException_GetContext(value);
1678 if (cause) {
1679 res = PySet_Contains(seen, cause);
1680 if (res == -1)
1681 PyErr_Clear();
1682 if (res == 0) {
1683 print_exception_recursive(
1684 f, cause, seen);
1685 err |= PyFile_WriteString(
1686 cause_message, f);
1687 }
1688 }
1689 else if (context) {
1690 res = PySet_Contains(seen, context);
1691 if (res == -1)
1692 PyErr_Clear();
1693 if (res == 0) {
1694 print_exception_recursive(
1695 f, context, seen);
1696 err |= PyFile_WriteString(
1697 context_message, f);
1698 }
1699 }
1700 Py_XDECREF(context);
1701 Py_XDECREF(cause);
1702 }
1703 }
1704 print_exception(f, value);
1705 if (err != 0)
1706 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001707}
1708
Thomas Wouters477c8d52006-05-27 19:21:47 +00001709void
1710PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 PyObject *seen;
1713 PyObject *f = PySys_GetObject("stderr");
1714 if (f == Py_None) {
1715 /* pass */
1716 }
1717 else if (f == NULL) {
1718 _PyObject_Dump(value);
1719 fprintf(stderr, "lost sys.stderr\n");
1720 }
1721 else {
1722 /* We choose to ignore seen being possibly NULL, and report
1723 at least the main exception (it could be a MemoryError).
1724 */
1725 seen = PySet_New(NULL);
1726 if (seen == NULL)
1727 PyErr_Clear();
1728 print_exception_recursive(f, value, seen);
1729 Py_XDECREF(seen);
1730 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001731}
1732
Guido van Rossum82598051997-03-05 00:20:32 +00001733PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001734PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 PyObject *ret = NULL;
1738 mod_ty mod;
1739 PyArena *arena = PyArena_New();
1740 if (arena == NULL)
1741 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1744 if (mod != NULL)
1745 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1746 PyArena_Free(arena);
1747 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001748}
1749
1750PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001751PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 PyObject *ret;
1755 mod_ty mod;
1756 PyArena *arena = PyArena_New();
1757 if (arena == NULL)
1758 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1761 flags, NULL, arena);
1762 if (closeit)
1763 fclose(fp);
1764 if (mod == NULL) {
1765 PyArena_Free(arena);
1766 return NULL;
1767 }
1768 ret = run_mod(mod, filename, globals, locals, flags, arena);
1769 PyArena_Free(arena);
1770 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001771}
1772
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001773static void
1774flush_io(void)
1775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 PyObject *f, *r;
1777 PyObject *type, *value, *traceback;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 /* Save the current exception */
1780 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 f = PySys_GetObject("stderr");
1783 if (f != NULL) {
1784 r = PyObject_CallMethod(f, "flush", "");
1785 if (r)
1786 Py_DECREF(r);
1787 else
1788 PyErr_Clear();
1789 }
1790 f = PySys_GetObject("stdout");
1791 if (f != NULL) {
1792 r = PyObject_CallMethod(f, "flush", "");
1793 if (r)
1794 Py_DECREF(r);
1795 else
1796 PyErr_Clear();
1797 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001800}
1801
Guido van Rossum82598051997-03-05 00:20:32 +00001802static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 PyCodeObject *co;
1807 PyObject *v;
1808 co = PyAST_Compile(mod, filename, flags, arena);
1809 if (co == NULL)
1810 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001811 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 Py_DECREF(co);
1813 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001814}
1815
Guido van Rossum82598051997-03-05 00:20:32 +00001816static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001817run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 PyCodeObject *co;
1821 PyObject *v;
1822 long magic;
1823 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 magic = PyMarshal_ReadLongFromFile(fp);
1826 if (magic != PyImport_GetMagicNumber()) {
1827 PyErr_SetString(PyExc_RuntimeError,
1828 "Bad magic number in .pyc file");
1829 return NULL;
1830 }
1831 (void) PyMarshal_ReadLongFromFile(fp);
1832 v = PyMarshal_ReadLastObjectFromFile(fp);
1833 fclose(fp);
1834 if (v == NULL || !PyCode_Check(v)) {
1835 Py_XDECREF(v);
1836 PyErr_SetString(PyExc_RuntimeError,
1837 "Bad code object in .pyc file");
1838 return NULL;
1839 }
1840 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001841 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 if (v && flags)
1843 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1844 Py_DECREF(co);
1845 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001846}
1847
Guido van Rossum82598051997-03-05 00:20:32 +00001848PyObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00001849Py_CompileStringExFlags(const char *str, const char *filename, int start,
1850 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 PyCodeObject *co;
1853 mod_ty mod;
1854 PyArena *arena = PyArena_New();
1855 if (arena == NULL)
1856 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1859 if (mod == NULL) {
1860 PyArena_Free(arena);
1861 return NULL;
1862 }
1863 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1864 PyObject *result = PyAST_mod2obj(mod);
1865 PyArena_Free(arena);
1866 return result;
1867 }
Georg Brandl8334fd92010-12-04 10:26:46 +00001868 co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 PyArena_Free(arena);
1870 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001871}
1872
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001873/* For use in Py_LIMITED_API */
1874#undef Py_CompileString
1875PyObject *
1876PyCompileString(const char *str, const char *filename, int start)
1877{
1878 return Py_CompileStringFlags(str, filename, start, NULL);
1879}
1880
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001881struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001882Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 struct symtable *st;
1885 mod_ty mod;
1886 PyCompilerFlags flags;
1887 PyArena *arena = PyArena_New();
1888 if (arena == NULL)
1889 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 flags.cf_flags = 0;
1892 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1893 if (mod == NULL) {
1894 PyArena_Free(arena);
1895 return NULL;
1896 }
1897 st = PySymtable_Build(mod, filename, 0);
1898 PyArena_Free(arena);
1899 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001900}
1901
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902/* Preferred access to parser is through AST. */
1903mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001904PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 mod_ty mod;
1908 PyCompilerFlags localflags;
1909 perrdetail err;
1910 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1913 &_PyParser_Grammar, start, &err,
1914 &iflags);
1915 if (flags == NULL) {
1916 localflags.cf_flags = 0;
1917 flags = &localflags;
1918 }
1919 if (n) {
1920 flags->cf_flags |= iflags & PyCF_MASK;
1921 mod = PyAST_FromNode(n, flags, filename, arena);
1922 PyNode_Free(n);
1923 return mod;
1924 }
1925 else {
1926 err_input(&err);
1927 return NULL;
1928 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929}
1930
1931mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001932PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 int start, char *ps1,
1934 char *ps2, PyCompilerFlags *flags, int *errcode,
1935 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 mod_ty mod;
1938 PyCompilerFlags localflags;
1939 perrdetail err;
1940 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
1943 &_PyParser_Grammar,
1944 start, ps1, ps2, &err, &iflags);
1945 if (flags == NULL) {
1946 localflags.cf_flags = 0;
1947 flags = &localflags;
1948 }
1949 if (n) {
1950 flags->cf_flags |= iflags & PyCF_MASK;
1951 mod = PyAST_FromNode(n, flags, filename, arena);
1952 PyNode_Free(n);
1953 return mod;
1954 }
1955 else {
1956 err_input(&err);
1957 if (errcode)
1958 *errcode = err.error;
1959 return NULL;
1960 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961}
1962
Guido van Rossuma110aa61994-08-29 12:50:44 +00001963/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001964
Guido van Rossuma110aa61994-08-29 12:50:44 +00001965node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001966PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 perrdetail err;
1969 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1970 &_PyParser_Grammar,
1971 start, NULL, NULL, &err, flags);
1972 if (n == NULL)
1973 err_input(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001976}
1977
Guido van Rossuma110aa61994-08-29 12:50:44 +00001978/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001979
Guido van Rossuma110aa61994-08-29 12:50:44 +00001980node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001981PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 perrdetail err;
1984 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1985 start, &err, flags);
1986 if (n == NULL)
1987 err_input(&err);
1988 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001989}
1990
1991node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001992PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 perrdetail err;
1996 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1997 &_PyParser_Grammar, start, &err, flags);
1998 if (n == NULL)
1999 err_input(&err);
2000 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00002001}
2002
2003node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002004PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00002007}
2008
Guido van Rossum66ebd912003-04-17 16:02:26 +00002009/* May want to move a more generalized form of this to parsetok.c or
2010 even parser modules. */
2011
2012void
2013PyParser_SetError(perrdetail *err)
2014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00002016}
2017
Guido van Rossuma110aa61994-08-29 12:50:44 +00002018/* Set the error appropriate to the given input error code (see errcode.h) */
2019
2020static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002021err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00002022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 PyObject *v, *w, *errtype, *errtext;
2024 PyObject *msg_obj = NULL;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002025 PyObject *filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 char *msg = NULL;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 errtype = PyExc_SyntaxError;
2029 switch (err->error) {
2030 case E_ERROR:
2031 return;
2032 case E_SYNTAX:
2033 errtype = PyExc_IndentationError;
2034 if (err->expected == INDENT)
2035 msg = "expected an indented block";
2036 else if (err->token == INDENT)
2037 msg = "unexpected indent";
2038 else if (err->token == DEDENT)
2039 msg = "unexpected unindent";
2040 else {
2041 errtype = PyExc_SyntaxError;
2042 msg = "invalid syntax";
2043 }
2044 break;
2045 case E_TOKEN:
2046 msg = "invalid token";
2047 break;
2048 case E_EOFS:
2049 msg = "EOF while scanning triple-quoted string literal";
2050 break;
2051 case E_EOLS:
2052 msg = "EOL while scanning string literal";
2053 break;
2054 case E_INTR:
2055 if (!PyErr_Occurred())
2056 PyErr_SetNone(PyExc_KeyboardInterrupt);
2057 goto cleanup;
2058 case E_NOMEM:
2059 PyErr_NoMemory();
2060 goto cleanup;
2061 case E_EOF:
2062 msg = "unexpected EOF while parsing";
2063 break;
2064 case E_TABSPACE:
2065 errtype = PyExc_TabError;
2066 msg = "inconsistent use of tabs and spaces in indentation";
2067 break;
2068 case E_OVERFLOW:
2069 msg = "expression too long";
2070 break;
2071 case E_DEDENT:
2072 errtype = PyExc_IndentationError;
2073 msg = "unindent does not match any outer indentation level";
2074 break;
2075 case E_TOODEEP:
2076 errtype = PyExc_IndentationError;
2077 msg = "too many levels of indentation";
2078 break;
2079 case E_DECODE: {
2080 PyObject *type, *value, *tb;
2081 PyErr_Fetch(&type, &value, &tb);
2082 msg = "unknown decode error";
2083 if (value != NULL)
2084 msg_obj = PyObject_Str(value);
2085 Py_XDECREF(type);
2086 Py_XDECREF(value);
2087 Py_XDECREF(tb);
2088 break;
2089 }
2090 case E_LINECONT:
2091 msg = "unexpected character after line continuation character";
2092 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00002093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 case E_IDENTIFIER:
2095 msg = "invalid character in identifier";
2096 break;
2097 default:
2098 fprintf(stderr, "error=%d\n", err->error);
2099 msg = "unknown parsing error";
2100 break;
2101 }
2102 /* err->text may not be UTF-8 in case of decoding errors.
2103 Explicitly convert to an object. */
2104 if (!err->text) {
2105 errtext = Py_None;
2106 Py_INCREF(Py_None);
2107 } else {
2108 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2109 "replace");
2110 }
Victor Stinner2f2ed1f2010-10-16 13:42:53 +00002111 if (err->filename != NULL)
2112 filename = PyUnicode_DecodeFSDefault(err->filename);
2113 else {
2114 Py_INCREF(Py_None);
2115 filename = Py_None;
2116 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002117 if (filename != NULL)
2118 v = Py_BuildValue("(NiiN)", filename,
2119 err->lineno, err->offset, errtext);
2120 else
2121 v = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 if (v != NULL) {
2123 if (msg_obj)
2124 w = Py_BuildValue("(OO)", msg_obj, v);
2125 else
2126 w = Py_BuildValue("(sO)", msg, v);
2127 } else
2128 w = NULL;
2129 Py_XDECREF(v);
2130 PyErr_SetObject(errtype, w);
2131 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002132cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 Py_XDECREF(msg_obj);
2134 if (err->text != NULL) {
2135 PyObject_FREE(err->text);
2136 err->text = NULL;
2137 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002138}
2139
2140/* Print fatal error message and abort */
2141
2142void
Tim Peters7c321a82002-07-09 02:57:01 +00002143Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 fprintf(stderr, "Fatal Python error: %s\n", msg);
2146 fflush(stderr); /* it helps in Windows debug build */
2147 if (PyErr_Occurred()) {
Victor Stinner55a5c782010-06-08 21:00:13 +00002148 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002150#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 {
2152 size_t len = strlen(msg);
2153 WCHAR* buffer;
2154 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 /* Convert the message to wchar_t. This uses a simple one-to-one
2157 conversion, assuming that the this error message actually uses ASCII
2158 only. If this ceases to be true, we will have to convert. */
2159 buffer = alloca( (len+1) * (sizeof *buffer));
2160 for( i=0; i<=len; ++i)
2161 buffer[i] = msg[i];
2162 OutputDebugStringW(L"Fatal Python error: ");
2163 OutputDebugStringW(buffer);
2164 OutputDebugStringW(L"\n");
2165 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002166#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002168#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002169#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002171}
2172
2173/* Clean up and exit */
2174
Guido van Rossuma110aa61994-08-29 12:50:44 +00002175#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002176#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002177#endif
2178
Collin Winter670e6922007-03-21 02:57:17 +00002179static void (*pyexitfunc)(void) = NULL;
2180/* For the atexit module. */
2181void _Py_PyAtExit(void (*func)(void))
2182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002184}
2185
2186static void
2187call_py_exitfuncs(void)
2188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 if (pyexitfunc == NULL)
2190 return;
Collin Winter670e6922007-03-21 02:57:17 +00002191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 (*pyexitfunc)();
2193 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002194}
2195
Antoine Pitrou011bd622009-10-20 21:52:47 +00002196/* Wait until threading._shutdown completes, provided
2197 the threading module was imported in the first place.
2198 The shutdown routine will wait until all non-daemon
2199 "threading" threads have completed. */
2200static void
2201wait_for_thread_shutdown(void)
2202{
2203#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 PyObject *result;
2205 PyThreadState *tstate = PyThreadState_GET();
2206 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2207 "threading");
2208 if (threading == NULL) {
2209 /* threading not imported */
2210 PyErr_Clear();
2211 return;
2212 }
2213 result = PyObject_CallMethod(threading, "_shutdown", "");
2214 if (result == NULL) {
2215 PyErr_WriteUnraisable(threading);
2216 }
2217 else {
2218 Py_DECREF(result);
2219 }
2220 Py_DECREF(threading);
Antoine Pitrou011bd622009-10-20 21:52:47 +00002221#endif
2222}
2223
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002224#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002225static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002226static int nexitfuncs = 0;
2227
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002228int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 if (nexitfuncs >= NEXITFUNCS)
2231 return -1;
2232 exitfuncs[nexitfuncs++] = func;
2233 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002234}
2235
Guido van Rossumcc283f51997-08-05 02:22:03 +00002236static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002237call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 while (nexitfuncs > 0)
2240 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 fflush(stdout);
2243 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002244}
2245
2246void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002247Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002252}
2253
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002254static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002255initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002256{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002257#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002259#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002260#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002262#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002263#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002265#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002267}
2268
Guido van Rossum7433b121997-02-14 19:45:36 +00002269
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002270/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2271 *
2272 * All of the code in this function must only use async-signal-safe functions,
2273 * listed at `man 7 signal` or
2274 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2275 */
2276void
2277_Py_RestoreSignals(void)
2278{
2279#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 PyOS_setsig(SIGPIPE, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002281#endif
2282#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 PyOS_setsig(SIGXFZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002284#endif
2285#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 PyOS_setsig(SIGXFSZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002287#endif
2288}
2289
2290
Guido van Rossum7433b121997-02-14 19:45:36 +00002291/*
2292 * The file descriptor fd is considered ``interactive'' if either
2293 * a) isatty(fd) is TRUE, or
2294 * b) the -i flag was given, and the filename associated with
2295 * the descriptor is NULL or "<stdin>" or "???".
2296 */
2297int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002298Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 if (isatty((int)fileno(fp)))
2301 return 1;
2302 if (!Py_InteractiveFlag)
2303 return 0;
2304 return (filename == NULL) ||
2305 (strcmp(filename, "<stdin>") == 0) ||
2306 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002307}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002308
2309
Tim Petersd08e3822003-04-17 15:24:21 +00002310#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002311#if defined(WIN32) && defined(_MSC_VER)
2312
2313/* Stack checking for Microsoft C */
2314
2315#include <malloc.h>
2316#include <excpt.h>
2317
Fred Drakee8de31c2000-08-31 05:38:39 +00002318/*
2319 * Return non-zero when we run out of memory on the stack; zero otherwise.
2320 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002321int
Fred Drake399739f2000-08-31 05:52:44 +00002322PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 __try {
2325 /* alloca throws a stack overflow exception if there's
2326 not enough space left on the stack */
2327 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2328 return 0;
2329 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2330 EXCEPTION_EXECUTE_HANDLER :
2331 EXCEPTION_CONTINUE_SEARCH) {
2332 int errcode = _resetstkoflw();
2333 if (errcode == 0)
2334 {
2335 Py_FatalError("Could not reset the stack!");
2336 }
2337 }
2338 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002339}
2340
2341#endif /* WIN32 && _MSC_VER */
2342
2343/* Alternate implementations can be added here... */
2344
2345#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002346
2347
2348/* Wrappers around sigaction() or signal(). */
2349
2350PyOS_sighandler_t
2351PyOS_getsig(int sig)
2352{
2353#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 struct sigaction context;
2355 if (sigaction(sig, NULL, &context) == -1)
2356 return SIG_ERR;
2357 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002358#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002360/* Special signal handling for the secure CRT in Visual Studio 2005 */
2361#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 switch (sig) {
2363 /* Only these signals are valid */
2364 case SIGINT:
2365 case SIGILL:
2366 case SIGFPE:
2367 case SIGSEGV:
2368 case SIGTERM:
2369 case SIGBREAK:
2370 case SIGABRT:
2371 break;
2372 /* Don't call signal() with other values or it will assert */
2373 default:
2374 return SIG_ERR;
2375 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002376#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 handler = signal(sig, SIG_IGN);
2378 if (handler != SIG_ERR)
2379 signal(sig, handler);
2380 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002381#endif
2382}
2383
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002384/*
2385 * All of the code in this function must only use async-signal-safe functions,
2386 * listed at `man 7 signal` or
2387 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2388 */
Guido van Rossum6f256182000-09-16 16:32:19 +00002389PyOS_sighandler_t
2390PyOS_setsig(int sig, PyOS_sighandler_t handler)
2391{
2392#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 /* Some code in Modules/signalmodule.c depends on sigaction() being
2394 * used here if HAVE_SIGACTION is defined. Fix that if this code
2395 * changes to invalidate that assumption.
2396 */
2397 struct sigaction context, ocontext;
2398 context.sa_handler = handler;
2399 sigemptyset(&context.sa_mask);
2400 context.sa_flags = 0;
2401 if (sigaction(sig, &context, &ocontext) == -1)
2402 return SIG_ERR;
2403 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002404#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 PyOS_sighandler_t oldhandler;
2406 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002407#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002409#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002411#endif
2412}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002413
2414/* Deprecated C API functions still provided for binary compatiblity */
2415
2416#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002417PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421}
2422
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002423#undef PyParser_SimpleParseString
2424PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002425PyParser_SimpleParseString(const char *str, int start)
2426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002429
2430#undef PyRun_AnyFile
2431PyAPI_FUNC(int)
2432PyRun_AnyFile(FILE *fp, const char *name)
2433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002435}
2436
2437#undef PyRun_AnyFileEx
2438PyAPI_FUNC(int)
2439PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002442}
2443
2444#undef PyRun_AnyFileFlags
2445PyAPI_FUNC(int)
2446PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002449}
2450
2451#undef PyRun_File
2452PyAPI_FUNC(PyObject *)
2453PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002456}
2457
2458#undef PyRun_FileEx
2459PyAPI_FUNC(PyObject *)
2460PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002463}
2464
2465#undef PyRun_FileFlags
2466PyAPI_FUNC(PyObject *)
2467PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002471}
2472
2473#undef PyRun_SimpleFile
2474PyAPI_FUNC(int)
2475PyRun_SimpleFile(FILE *f, const char *p)
2476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002478}
2479
2480#undef PyRun_SimpleFileEx
2481PyAPI_FUNC(int)
2482PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002485}
2486
2487
2488#undef PyRun_String
2489PyAPI_FUNC(PyObject *)
2490PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002493}
2494
2495#undef PyRun_SimpleString
2496PyAPI_FUNC(int)
2497PyRun_SimpleString(const char *s)
2498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002500}
2501
2502#undef Py_CompileString
2503PyAPI_FUNC(PyObject *)
2504Py_CompileString(const char *str, const char *p, int s)
2505{
Georg Brandl8334fd92010-12-04 10:26:46 +00002506 return Py_CompileStringExFlags(str, p, s, NULL, -1);
2507}
2508
2509#undef Py_CompileStringFlags
2510PyAPI_FUNC(PyObject *)
2511Py_CompileStringFlags(const char *str, const char *p, int s,
2512 PyCompilerFlags *flags)
2513{
2514 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002515}
2516
2517#undef PyRun_InteractiveOne
2518PyAPI_FUNC(int)
2519PyRun_InteractiveOne(FILE *f, const char *p)
2520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002522}
2523
2524#undef PyRun_InteractiveLoop
2525PyAPI_FUNC(int)
2526PyRun_InteractiveLoop(FILE *f, const char *p)
2527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002529}
2530
2531#ifdef __cplusplus
2532}
2533#endif