blob: 0ef36bbb8cb776ace00e50c0b52d397913c82c0c [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Guido van Rossumd8faa362007-04-27 19:54:29 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00008#include "grammar.h"
9#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000010#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000014#include "symtable.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015#include "ast.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000016#include "marshal.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000017#include "osdefs.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000018
Thomas Wouters0e3f5912006-08-11 14:57:12 +000019#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000020#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000021#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000022
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000023#ifdef MS_WINDOWS
Martin v. Löwis5c88d812009-01-02 20:47:48 +000024#include "malloc.h" /* for alloca */
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000025#endif
Martin v. Löwis5c88d812009-01-02 20:47:48 +000026
Martin v. Löwis73d538b2003-03-05 15:13:47 +000027#ifdef HAVE_LANGINFO_H
28#include <locale.h>
29#include <langinfo.h>
30#endif
31
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000032#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000033#undef BYTE
34#include "windows.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000035#define PATH_MAX MAXPATHLEN
Guido van Rossuma44823b1995-03-14 15:01:17 +000036#endif
37
Neal Norwitz4281cef2006-03-04 19:58:13 +000038#ifndef Py_REF_DEBUG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000040#else /* Py_REF_DEBUG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041#define PRINT_TOTAL_REFS() fprintf(stderr, \
42 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
43 _Py_GetRefTotal())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000044#endif
45
46#ifdef __cplusplus
47extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000048#endif
49
Martin v. Löwis790465f2008-04-05 20:41:37 +000050extern wchar_t *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000051
Guido van Rossum82598051997-03-05 00:20:32 +000052extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000053
Guido van Rossumb73cc041993-11-01 16:28:59 +000054/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000055static void initmain(void);
Victor Stinner793b5312011-04-27 00:24:21 +020056static int initfsencoding(PyInterpreterState *interp);
Tim Petersdbd9ba62000-07-09 03:09:57 +000057static void initsite(void);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000058static int initstdio(void);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000059static void flush_io(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000060static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000062static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000064static void err_input(perrdetail *);
Victor Stinner7f2fee32011-04-05 00:39:01 +020065static void err_free(perrdetail *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static void initsigs(void);
Collin Winter670e6922007-03-21 02:57:17 +000067static void call_py_exitfuncs(void);
Antoine Pitrou011bd622009-10-20 21:52:47 +000068static void wait_for_thread_shutdown(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000069static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000070extern void _PyUnicode_Init(void);
71extern void _PyUnicode_Fini(void);
Guido van Rossumddefaf32007-01-14 03:31:43 +000072extern int _PyLong_Init(void);
73extern void PyLong_Fini(void);
Victor Stinner024e37a2011-03-31 01:31:06 +020074extern int _PyFaulthandler_Init(void);
75extern void _PyFaulthandler_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000076
Mark Hammond8d98d2c2003-04-19 15:41:53 +000077#ifdef WITH_THREAD
78extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
79extern void _PyGILState_Fini(void);
80#endif /* WITH_THREAD */
81
Guido van Rossum82598051997-03-05 00:20:32 +000082int Py_DebugFlag; /* Needed by parser.c */
83int Py_VerboseFlag; /* Needed by import.c */
Georg Brandl8aa7e992010-12-28 18:30:18 +000084int Py_QuietFlag; /* Needed by sysmodule.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000085int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl0b2489e2011-05-15 08:49:12 +020086int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000087int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +000088int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Christian Heimes790c8232008-01-07 21:14:23 +000089int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000090int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000091int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000092int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Christian Heimes8dc226f2008-05-06 23:45:46 +000093int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Antoine Pitrou05608432009-01-09 18:53:14 +000094int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000095
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +020096PyThreadState *_Py_Finalizing = NULL;
97
Christian Heimes33fe8092008-04-13 13:53:33 +000098/* PyModule_GetWarningsModule is no longer necessary as of 2.6
99since _warnings is builtin. This API should not be used. */
100PyObject *
101PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +0000102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +0000104}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000105
Guido van Rossum25ce5661997-08-02 03:10:38 +0000106static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000107
Thomas Wouters7e474022000-07-16 12:04:32 +0000108/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000109
110int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000111Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 return initialized;
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000114}
115
Guido van Rossum25ce5661997-08-02 03:10:38 +0000116/* Global initializations. Can be undone by Py_Finalize(). Don't
117 call this twice without an intervening Py_Finalize() call. When
118 initializations fail, a fatal error is issued and the function does
119 not return. On return, the first thread and interpreter state have
120 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000121
Guido van Rossum25ce5661997-08-02 03:10:38 +0000122 Locking: you must hold the interpreter lock while calling this.
123 (If the lock has not yet been initialized, that's equivalent to
124 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000125
Guido van Rossum25ce5661997-08-02 03:10:38 +0000126*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000127
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000128static int
129add_flag(int flag, const char *envs)
130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 int env = atoi(envs);
132 if (flag < env)
133 flag = env;
134 if (flag < 1)
135 flag = 1;
136 return flag;
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000137}
138
Christian Heimes5833a2f2008-10-30 21:40:04 +0000139static char*
Victor Stinner94908bb2010-08-18 21:23:25 +0000140get_codec_name(const char *encoding)
Christian Heimes5833a2f2008-10-30 21:40:04 +0000141{
Victor Stinner94908bb2010-08-18 21:23:25 +0000142 char *name_utf8, *name_str;
Victor Stinner386fe712010-05-19 00:34:15 +0000143 PyObject *codec, *name = NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000144
Victor Stinner94908bb2010-08-18 21:23:25 +0000145 codec = _PyCodec_Lookup(encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 if (!codec)
147 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 name = PyObject_GetAttrString(codec, "name");
150 Py_CLEAR(codec);
151 if (!name)
152 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000153
Victor Stinner94908bb2010-08-18 21:23:25 +0000154 name_utf8 = _PyUnicode_AsString(name);
Victor Stinner4ca28092011-03-20 23:09:03 +0100155 if (name_utf8 == NULL)
Victor Stinner386fe712010-05-19 00:34:15 +0000156 goto error;
Victor Stinner94908bb2010-08-18 21:23:25 +0000157 name_str = strdup(name_utf8);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 Py_DECREF(name);
Victor Stinner94908bb2010-08-18 21:23:25 +0000159 if (name_str == NULL) {
160 PyErr_NoMemory();
161 return NULL;
162 }
163 return name_str;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000164
165error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 Py_XDECREF(codec);
Victor Stinner386fe712010-05-19 00:34:15 +0000167 Py_XDECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000169}
Victor Stinner94908bb2010-08-18 21:23:25 +0000170
Victor Stinner94908bb2010-08-18 21:23:25 +0000171static char*
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200172get_locale_encoding(void)
Victor Stinner94908bb2010-08-18 21:23:25 +0000173{
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200174#ifdef MS_WINDOWS
175 char codepage[100];
176 PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
177 return get_codec_name(codepage);
178#elif defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94908bb2010-08-18 21:23:25 +0000179 char* codeset = nl_langinfo(CODESET);
180 if (!codeset || codeset[0] == '\0') {
181 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
182 return NULL;
183 }
184 return get_codec_name(codeset);
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200185#else
186 PyErr_SetNone(PyExc_NotImplementedError);
187 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000188#endif
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200189}
Christian Heimes5833a2f2008-10-30 21:40:04 +0000190
Guido van Rossuma027efa1997-05-05 20:56:21 +0000191void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000192Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 PyInterpreterState *interp;
195 PyThreadState *tstate;
196 PyObject *bimod, *sysmod, *pstderr;
197 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (initialized)
201 return;
202 initialized = 1;
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200203 _Py_Finalizing = NULL;
Tim Petersd08e3822003-04-17 15:24:21 +0000204
Andrew M. Kuchlingb2ceb3a2010-02-22 23:26:10 +0000205#if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 /* Set up the LC_CTYPE locale, so we can obtain
207 the locale's charset without having to switch
208 locales. */
209 setlocale(LC_CTYPE, "");
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000210#endif
211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
213 Py_DebugFlag = add_flag(Py_DebugFlag, p);
214 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
215 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
216 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
217 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
218 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
219 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 interp = PyInterpreterState_New();
222 if (interp == NULL)
223 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 tstate = PyThreadState_New(interp);
226 if (tstate == NULL)
227 Py_FatalError("Py_Initialize: can't make first thread");
228 (void) PyThreadState_Swap(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000229
Victor Stinner6961bd62010-08-17 22:26:51 +0000230#ifdef WITH_THREAD
Antoine Pitroub0b384b2010-09-20 20:13:48 +0000231 /* We can't call _PyEval_FiniThreads() in Py_Finalize because
232 destroying the GIL might fail when it is being referenced from
233 another running thread (see issue #9901).
234 Instead we destroy the previously created GIL here, which ensures
235 that we can call Py_Initialize / Py_Finalize multiple times. */
236 _PyEval_FiniThreads();
237
238 /* Auto-thread-state API */
Victor Stinner6961bd62010-08-17 22:26:51 +0000239 _PyGILState_Init(interp, tstate);
240#endif /* WITH_THREAD */
241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 if (!_PyFrame_Init())
245 Py_FatalError("Py_Initialize: can't init frames");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (!_PyLong_Init())
248 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 if (!PyByteArray_Init())
251 Py_FatalError("Py_Initialize: can't init bytearray");
Neal Norwitz6968b052007-02-27 19:02:19 +0000252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 _PyFloat_Init();
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 interp->modules = PyDict_New();
256 if (interp->modules == NULL)
257 Py_FatalError("Py_Initialize: can't make modules dictionary");
258 interp->modules_reloading = PyDict_New();
259 if (interp->modules_reloading == NULL)
260 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 /* Init Unicode implementation; relies on the codec registry */
263 _PyUnicode_Init();
Guido van Rossumc94044c2000-03-10 23:03:54 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 bimod = _PyBuiltin_Init();
266 if (bimod == NULL)
267 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Victor Stinner49d3f252010-10-17 01:24:53 +0000268 _PyImport_FixupBuiltin(bimod, "builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 interp->builtins = PyModule_GetDict(bimod);
270 if (interp->builtins == NULL)
271 Py_FatalError("Py_Initialize: can't initialize builtins dict");
272 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 /* initialize builtin exceptions */
275 _PyExc_Init();
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 sysmod = _PySys_Init();
278 if (sysmod == NULL)
279 Py_FatalError("Py_Initialize: can't initialize sys");
280 interp->sysdict = PyModule_GetDict(sysmod);
281 if (interp->sysdict == NULL)
282 Py_FatalError("Py_Initialize: can't initialize sys dict");
283 Py_INCREF(interp->sysdict);
Victor Stinner49d3f252010-10-17 01:24:53 +0000284 _PyImport_FixupBuiltin(sysmod, "sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 PySys_SetPath(Py_GetPath());
286 PyDict_SetItemString(interp->sysdict, "modules",
287 interp->modules);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 /* Set up a preliminary stderr printer until we have enough
290 infrastructure for the io module in place. */
291 pstderr = PyFile_NewStdPrinter(fileno(stderr));
292 if (pstderr == NULL)
293 Py_FatalError("Py_Initialize: can't set preliminary stderr");
294 PySys_SetObject("stderr", pstderr);
295 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000296 Py_DECREF(pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 _PyImport_Init();
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 _PyImportHooks_Init();
Just van Rossum52e14d62002-12-30 22:08:05 +0000301
Victor Stinner024e37a2011-03-31 01:31:06 +0200302 /* initialize the faulthandler module */
303 if (_PyFaulthandler_Init())
304 Py_FatalError("Py_Initialize: can't initialize faulthandler");
305
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000306 /* Initialize _warnings. */
307 _PyWarnings_Init();
308
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000309 _PyTime_Init();
310
Victor Stinner793b5312011-04-27 00:24:21 +0200311 if (initfsencoding(interp) < 0)
312 Py_FatalError("Py_Initialize: unable to load the file system codec");
Martin v. Löwis011e8422009-05-05 04:43:17 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 if (install_sigs)
315 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 initmain(); /* Module __main__ */
318 if (initstdio() < 0)
319 Py_FatalError(
320 "Py_Initialize: can't initialize sys standard streams");
321
Antoine Pitroucf9f9802010-11-10 13:55:25 +0000322 /* Initialize warnings. */
323 if (PySys_HasWarnOptions()) {
324 PyObject *warnings_module = PyImport_ImportModule("warnings");
325 if (warnings_module == NULL) {
326 fprintf(stderr, "'import warnings' failed; traceback:\n");
327 PyErr_Print();
328 }
329 Py_XDECREF(warnings_module);
330 }
331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 if (!Py_NoSiteFlag)
333 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000334}
335
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000336void
337Py_Initialize(void)
338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 Py_InitializeEx(1);
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000340}
341
342
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000343#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000344extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000345#endif
346
Guido van Rossume8432ac2007-07-09 15:04:50 +0000347/* Flush stdout and stderr */
348
Neal Norwitz2bad9702007-08-27 06:19:22 +0000349static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000350flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 PyObject *fout = PySys_GetObject("stdout");
353 PyObject *ferr = PySys_GetObject("stderr");
354 PyObject *tmp;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200355 _Py_identifier(flush);
Guido van Rossume8432ac2007-07-09 15:04:50 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 if (fout != NULL && fout != Py_None) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200358 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (tmp == NULL)
Antoine Pitroubddc9fe2010-08-08 20:46:42 +0000360 PyErr_WriteUnraisable(fout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 else
362 Py_DECREF(tmp);
363 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000364
Victor Stinner9467b212010-05-14 00:59:09 +0000365 if (ferr != NULL && ferr != Py_None) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200366 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 if (tmp == NULL)
368 PyErr_Clear();
369 else
370 Py_DECREF(tmp);
371 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000372}
373
Guido van Rossum25ce5661997-08-02 03:10:38 +0000374/* Undo the effect of Py_Initialize().
375
376 Beware: if multiple interpreter and/or thread states exist, these
377 are not wiped out; only the current thread and interpreter state
378 are deleted. But since everything else is deleted, those other
379 interpreter and thread states should no longer be used.
380
381 (XXX We should do better, e.g. wipe out all interpreters and
382 threads.)
383
384 Locking: as above.
385
386*/
387
388void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000389Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 PyInterpreterState *interp;
392 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 if (!initialized)
395 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 wait_for_thread_shutdown();
Antoine Pitrou011bd622009-10-20 21:52:47 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 /* The interpreter is still entirely intact at this point, and the
400 * exit funcs may be relying on that. In particular, if some thread
401 * or exit func is still waiting to do an import, the import machinery
402 * expects Py_IsInitialized() to return true. So don't say the
403 * interpreter is uninitialized until after the exit funcs have run.
404 * Note that Threading.py uses an exit func to do a join on all the
405 * threads created thru it, so this also protects pending imports in
406 * the threads created via Threading.
407 */
408 call_py_exitfuncs();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* Get current thread state and interpreter pointer */
411 tstate = PyThreadState_GET();
412 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000413
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200414 /* Remaining threads (e.g. daemon threads) will automatically exit
415 after taking the GIL (in PyEval_RestoreThread()). */
416 _Py_Finalizing = tstate;
417 initialized = 0;
418
419 /* Flush stdout+stderr */
420 flush_std_files();
421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 /* Disable signal handling */
423 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 /* Clear type lookup cache */
426 PyType_ClearCache();
Christian Heimes26855632008-01-27 23:50:43 +0000427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 /* Collect garbage. This may call finalizers; it's nice to call these
429 * before all modules are destroyed.
430 * XXX If a __del__ or weakref callback is triggered here, and tries to
431 * XXX import a module, bad things can happen, because Python no
432 * XXX longer believes it's initialized.
433 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
434 * XXX is easy to provoke that way. I've also seen, e.g.,
435 * XXX Exception exceptions.ImportError: 'No module named sha'
436 * XXX in <function callback at 0x008F5718> ignored
437 * XXX but I'm unclear on exactly how that one happens. In any case,
438 * XXX I haven't seen a real-life report of either of these.
439 */
440 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000441#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 /* With COUNT_ALLOCS, it helps to run GC multiple times:
443 each collection might release some types from the type
444 list, so they become garbage. */
445 while (PyGC_Collect() > 0)
446 /* nothing */;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000447#endif
Antoine Pitrou696e0352010-08-08 22:18:46 +0000448 /* We run this while most interpreter state is still alive, so that
449 debug information can be printed out */
450 _PyGC_Fini();
Guido van Rossume13ddc92003-04-17 17:29:22 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 /* Destroy all modules */
453 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 /* Flush stdout+stderr (again, in case more was printed) */
456 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 /* Collect final garbage. This disposes of cycles created by
459 * new-style class definitions, for example.
460 * XXX This is disabled because it caused too many problems. If
461 * XXX a __del__ or weakref callback triggers here, Python code has
462 * XXX a hard time running, because even the sys module has been
463 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
464 * XXX One symptom is a sequence of information-free messages
465 * XXX coming from threads (if a __del__ or callback is invoked,
466 * XXX other threads can execute too, and any exception they encounter
467 * XXX triggers a comedy of errors as subsystem after subsystem
468 * XXX fails to find what it *expects* to find in sys to help report
469 * XXX the exception and consequent unexpected failures). I've also
470 * XXX seen segfaults then, after adding print statements to the
471 * XXX Python code getting called.
472 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000473#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000475#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
478 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000479
Victor Stinner024e37a2011-03-31 01:31:06 +0200480 /* unload faulthandler module */
481 _PyFaulthandler_Fini();
482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000484#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000486#endif
487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000489
Tim Peters9cf25ce2003-04-17 15:21:01 +0000490#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 /* Display all objects still alive -- this can invoke arbitrary
492 * __repr__ overrides, so requires a mostly-intact interpreter.
493 * Alas, a lot of stuff may still be alive now that will be cleaned
494 * up later.
495 */
496 if (Py_GETENV("PYTHONDUMPREFS"))
497 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000498#endif /* Py_TRACE_REFS */
499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 /* Clear interpreter state */
501 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 /* Now we decref the exception classes. After this point nothing
504 can raise an exception. That's okay, because each Fini() method
505 below has been checked to make sure no exceptions are ever
506 raised.
507 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 /* Cleanup auto-thread-state */
Christian Heimes7d2ff882007-11-30 14:35:04 +0000512#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 _PyGILState_Fini();
Christian Heimes7d2ff882007-11-30 14:35:04 +0000514#endif /* WITH_THREAD */
515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 /* Delete current thread */
517 PyThreadState_Swap(NULL);
518 PyInterpreterState_Delete(interp);
Barry Warsawf242aa02000-05-25 23:09:49 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 /* Sundry finalizers */
521 PyMethod_Fini();
522 PyFrame_Fini();
523 PyCFunction_Fini();
524 PyTuple_Fini();
525 PyList_Fini();
526 PySet_Fini();
527 PyBytes_Fini();
528 PyByteArray_Fini();
529 PyLong_Fini();
530 PyFloat_Fini();
531 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 /* Cleanup Unicode implementation */
534 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 /* reset file system default encoding */
Victor Stinnerb744ba12010-05-15 12:27:16 +0000537 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 free((char*)Py_FileSystemDefaultEncoding);
539 Py_FileSystemDefaultEncoding = NULL;
540 }
Christian Heimesc8967002007-11-30 10:18:26 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 /* XXX Still allocated:
543 - various static ad-hoc pointers to interned strings
544 - int and float free list blocks
545 - whatever various modules and libraries allocate
546 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000549
Tim Peters269b2a62003-04-17 19:52:29 +0000550#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 /* Display addresses (& refcnts) of all objects still alive.
552 * An address can be used to find the repr of the object, printed
553 * above by _Py_PrintReferences.
554 */
555 if (Py_GETENV("PYTHONDUMPREFS"))
556 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000557#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000558#ifdef PYMALLOC_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 if (Py_GETENV("PYTHONMALLOCSTATS"))
560 _PyObject_DebugMallocStats();
Tim Peters0e871182002-04-13 08:29:14 +0000561#endif
562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000564}
565
566/* Create and initialize a new interpreter and thread, and return the
567 new thread. This requires that Py_Initialize() has been called
568 first.
569
570 Unsuccessful initialization yields a NULL pointer. Note that *no*
571 exception information is available even in this case -- the
572 exception information is held in the thread, and there is no
573 thread.
574
575 Locking: as above.
576
577*/
578
579PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000580Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 PyInterpreterState *interp;
583 PyThreadState *tstate, *save_tstate;
584 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 if (!initialized)
587 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 interp = PyInterpreterState_New();
590 if (interp == NULL)
591 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 tstate = PyThreadState_New(interp);
594 if (tstate == NULL) {
595 PyInterpreterState_Delete(interp);
596 return NULL;
597 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 interp->modules = PyDict_New();
604 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000605
Victor Stinner49d3f252010-10-17 01:24:53 +0000606 bimod = _PyImport_FindBuiltin("builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 if (bimod != NULL) {
608 interp->builtins = PyModule_GetDict(bimod);
609 if (interp->builtins == NULL)
610 goto handle_error;
611 Py_INCREF(interp->builtins);
612 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 /* initialize builtin exceptions */
615 _PyExc_Init();
Christian Heimes6a27efa2008-10-30 21:48:26 +0000616
Victor Stinner49d3f252010-10-17 01:24:53 +0000617 sysmod = _PyImport_FindBuiltin("sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 if (bimod != NULL && sysmod != NULL) {
619 PyObject *pstderr;
620 interp->sysdict = PyModule_GetDict(sysmod);
621 if (interp->sysdict == NULL)
622 goto handle_error;
623 Py_INCREF(interp->sysdict);
624 PySys_SetPath(Py_GetPath());
625 PyDict_SetItemString(interp->sysdict, "modules",
626 interp->modules);
627 /* Set up a preliminary stderr printer until we have enough
628 infrastructure for the io module in place. */
629 pstderr = PyFile_NewStdPrinter(fileno(stderr));
630 if (pstderr == NULL)
631 Py_FatalError("Py_Initialize: can't set preliminary stderr");
632 PySys_SetObject("stderr", pstderr);
633 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000634 Py_DECREF(pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 _PyImportHooks_Init();
Victor Stinner793b5312011-04-27 00:24:21 +0200637
638 if (initfsencoding(interp) < 0)
639 goto handle_error;
640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 if (initstdio() < 0)
642 Py_FatalError(
643 "Py_Initialize: can't initialize sys standard streams");
644 initmain();
645 if (!Py_NoSiteFlag)
646 initsite();
647 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 if (!PyErr_Occurred())
650 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000651
Thomas Wouters89f507f2006-12-13 04:49:30 +0000652handle_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000654
Victor Stinnerc40a3502011-04-27 00:20:27 +0200655 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 PyThreadState_Clear(tstate);
657 PyThreadState_Swap(save_tstate);
658 PyThreadState_Delete(tstate);
659 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000662}
663
664/* Delete an interpreter and its last thread. This requires that the
665 given thread state is current, that the thread has no remaining
666 frames, and that it is its interpreter's only remaining thread.
667 It is a fatal error to violate these constraints.
668
669 (Py_Finalize() doesn't have these constraints -- it zaps
670 everything, regardless.)
671
672 Locking: as above.
673
674*/
675
676void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000677Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 if (tstate != PyThreadState_GET())
682 Py_FatalError("Py_EndInterpreter: thread is not current");
683 if (tstate->frame != NULL)
684 Py_FatalError("Py_EndInterpreter: thread still has a frame");
685 if (tstate != interp->tstate_head || tstate->next != NULL)
686 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyImport_Cleanup();
689 PyInterpreterState_Clear(interp);
690 PyThreadState_Swap(NULL);
691 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000692}
693
Martin v. Löwis790465f2008-04-05 20:41:37 +0000694static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000695
696void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000697Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 if (pn && *pn)
700 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000701}
702
Martin v. Löwis790465f2008-04-05 20:41:37 +0000703wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000704Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000707}
708
Martin v. Löwis790465f2008-04-05 20:41:37 +0000709static wchar_t *default_home = NULL;
710static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000711
712void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000713Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000716}
717
Martin v. Löwis790465f2008-04-05 20:41:37 +0000718wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000719Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 wchar_t *home = default_home;
722 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
723 char* chome = Py_GETENV("PYTHONHOME");
724 if (chome) {
725 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
726 if (r != (size_t)-1 && r <= PATH_MAX)
727 home = env_home;
728 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 }
731 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000732}
733
Guido van Rossum6135a871995-01-09 17:53:26 +0000734/* Create __main__ module */
735
736static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000737initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 PyObject *m, *d;
740 m = PyImport_AddModule("__main__");
741 if (m == NULL)
742 Py_FatalError("can't create __main__ module");
743 d = PyModule_GetDict(m);
744 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
745 PyObject *bimod = PyImport_ImportModule("builtins");
746 if (bimod == NULL ||
747 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
748 Py_FatalError("can't add __builtins__ to __main__");
749 Py_DECREF(bimod);
750 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000751}
752
Victor Stinner793b5312011-04-27 00:24:21 +0200753static int
754initfsencoding(PyInterpreterState *interp)
Victor Stinnerb744ba12010-05-15 12:27:16 +0000755{
756 PyObject *codec;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000757
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200758 if (Py_FileSystemDefaultEncoding == NULL)
759 {
760 Py_FileSystemDefaultEncoding = get_locale_encoding();
761 if (Py_FileSystemDefaultEncoding == NULL)
Victor Stinnere4743092010-10-19 00:05:51 +0000762 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
Victor Stinnerb744ba12010-05-15 12:27:16 +0000763
Victor Stinnere4743092010-10-19 00:05:51 +0000764 Py_HasFileSystemDefaultEncoding = 0;
Victor Stinner793b5312011-04-27 00:24:21 +0200765 interp->fscodec_initialized = 1;
766 return 0;
Victor Stinner7f84ab52010-06-11 00:36:33 +0000767 }
Victor Stinnerb744ba12010-05-15 12:27:16 +0000768
769 /* the encoding is mbcs, utf-8 or ascii */
770 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
771 if (!codec) {
772 /* Such error can only occurs in critical situations: no more
773 * memory, import a module of the standard library failed,
774 * etc. */
Victor Stinner793b5312011-04-27 00:24:21 +0200775 return -1;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000776 }
Victor Stinner793b5312011-04-27 00:24:21 +0200777 Py_DECREF(codec);
778 interp->fscodec_initialized = 1;
779 return 0;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000780}
781
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000782/* Import the site module (not into __main__ though) */
783
784static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000785initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyObject *m;
788 m = PyImport_ImportModule("site");
789 if (m == NULL) {
790 PyErr_Print();
791 Py_Finalize();
792 exit(1);
793 }
794 else {
795 Py_DECREF(m);
796 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000797}
798
Antoine Pitrou05608432009-01-09 18:53:14 +0000799static PyObject*
800create_stdio(PyObject* io,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 int fd, int write_mode, char* name,
802 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +0000803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
805 const char* mode;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000806 const char* newline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 PyObject *line_buffering;
808 int buffering, isatty;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200809 _Py_identifier(open);
810 _Py_identifier(isatty);
811 _Py_identifier(TextIOWrapper);
Antoine Pitrou05608432009-01-09 18:53:14 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 /* stdin is always opened in buffered mode, first because it shouldn't
814 make a difference in common use cases, second because TextIOWrapper
815 depends on the presence of a read1() method which only exists on
816 buffered streams.
817 */
818 if (Py_UnbufferedStdioFlag && write_mode)
819 buffering = 0;
820 else
821 buffering = -1;
822 if (write_mode)
823 mode = "wb";
824 else
825 mode = "rb";
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200826 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
827 fd, mode, buffering,
828 Py_None, Py_None, Py_None, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 if (buf == NULL)
830 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 if (buffering) {
833 raw = PyObject_GetAttrString(buf, "raw");
834 if (raw == NULL)
835 goto error;
836 }
837 else {
838 raw = buf;
839 Py_INCREF(raw);
840 }
Antoine Pitrou05608432009-01-09 18:53:14 +0000841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 text = PyUnicode_FromString(name);
843 if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0)
844 goto error;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200845 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 if (res == NULL)
847 goto error;
848 isatty = PyObject_IsTrue(res);
849 Py_DECREF(res);
850 if (isatty == -1)
851 goto error;
852 if (isatty || Py_UnbufferedStdioFlag)
853 line_buffering = Py_True;
854 else
855 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 Py_CLEAR(raw);
858 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +0000859
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000860 newline = "\n";
861#ifdef MS_WINDOWS
862 if (!write_mode) {
863 /* translate \r\n to \n for sys.stdin on Windows */
864 newline = NULL;
865 }
866#endif
867
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200868 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
869 buf, encoding, errors,
870 newline, line_buffering);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 Py_CLEAR(buf);
872 if (stream == NULL)
873 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (write_mode)
876 mode = "w";
877 else
878 mode = "r";
879 text = PyUnicode_FromString(mode);
880 if (!text || PyObject_SetAttrString(stream, "mode", text) < 0)
881 goto error;
882 Py_CLEAR(text);
883 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +0000884
885error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 Py_XDECREF(buf);
887 Py_XDECREF(stream);
888 Py_XDECREF(text);
889 Py_XDECREF(raw);
890 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +0000891}
892
Georg Brandl1a3284e2007-12-02 09:40:06 +0000893/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000894static int
895initstdio(void)
896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 PyObject *iomod = NULL, *wrapper;
898 PyObject *bimod = NULL;
899 PyObject *m;
900 PyObject *std = NULL;
901 int status = 0, fd;
902 PyObject * encoding_attr;
903 char *encoding = NULL, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 /* Hack to avoid a nasty recursion issue when Python is invoked
906 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
907 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
908 goto error;
909 }
910 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
913 goto error;
914 }
915 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 if (!(bimod = PyImport_ImportModule("builtins"))) {
918 goto error;
919 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 if (!(iomod = PyImport_ImportModule("io"))) {
922 goto error;
923 }
924 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
925 goto error;
926 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* Set builtins.open */
929 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000930 Py_DECREF(wrapper);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 goto error;
932 }
Antoine Pitrou5a96b522010-11-20 19:50:57 +0000933 Py_DECREF(wrapper);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 encoding = Py_GETENV("PYTHONIOENCODING");
936 errors = NULL;
937 if (encoding) {
938 encoding = strdup(encoding);
939 errors = strchr(encoding, ':');
940 if (errors) {
941 *errors = '\0';
942 errors++;
943 }
944 }
Martin v. Löwis0f599892008-06-02 11:13:03 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 /* Set sys.stdin */
947 fd = fileno(stdin);
948 /* Under some conditions stdin, stdout and stderr may not be connected
949 * and fileno() may point to an invalid file descriptor. For example
950 * GUI apps don't have valid standard streams by default.
951 */
952 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000953#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 std = Py_None;
955 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000956#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000958#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 }
960 else {
961 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
962 if (std == NULL)
963 goto error;
964 } /* if (fd < 0) */
965 PySys_SetObject("__stdin__", std);
966 PySys_SetObject("stdin", std);
967 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 /* Set sys.stdout */
970 fd = fileno(stdout);
971 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000972#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 std = Py_None;
974 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000975#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000977#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 }
979 else {
980 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
981 if (std == NULL)
982 goto error;
983 } /* if (fd < 0) */
984 PySys_SetObject("__stdout__", std);
985 PySys_SetObject("stdout", std);
986 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000987
Guido van Rossum98297ee2007-11-06 21:34:58 +0000988#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 /* Set sys.stderr, replaces the preliminary stderr */
990 fd = fileno(stderr);
991 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000992#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 std = Py_None;
994 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000995#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000997#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 }
999 else {
1000 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1001 if (std == NULL)
1002 goto error;
1003 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 /* Same as hack above, pre-import stderr's codec to avoid recursion
1006 when import.c tries to write to stderr in verbose mode. */
1007 encoding_attr = PyObject_GetAttrString(std, "encoding");
1008 if (encoding_attr != NULL) {
1009 const char * encoding;
1010 encoding = _PyUnicode_AsString(encoding_attr);
1011 if (encoding != NULL) {
1012 _PyCodec_Lookup(encoding);
1013 }
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +00001014 Py_DECREF(encoding_attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 }
1016 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 PySys_SetObject("__stderr__", std);
1019 PySys_SetObject("stderr", std);
1020 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001021#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001024 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 status = -1;
1026 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 if (encoding)
1029 free(encoding);
1030 Py_XDECREF(bimod);
1031 Py_XDECREF(iomod);
1032 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001033}
1034
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001035/* Parse input from a file and execute it */
1036
1037int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001038PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 if (filename == NULL)
1042 filename = "???";
1043 if (Py_FdIsInteractive(fp, filename)) {
1044 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
1045 if (closeit)
1046 fclose(fp);
1047 return err;
1048 }
1049 else
1050 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001051}
1052
1053int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001054PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 PyObject *v;
1057 int ret;
1058 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 if (flags == NULL) {
1061 flags = &local_flags;
1062 local_flags.cf_flags = 0;
1063 }
1064 v = PySys_GetObject("ps1");
1065 if (v == NULL) {
1066 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
1067 Py_XDECREF(v);
1068 }
1069 v = PySys_GetObject("ps2");
1070 if (v == NULL) {
1071 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1072 Py_XDECREF(v);
1073 }
1074 for (;;) {
1075 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
1076 PRINT_TOTAL_REFS();
1077 if (ret == E_EOF)
1078 return 0;
1079 /*
1080 if (ret == E_NOMEM)
1081 return -1;
1082 */
1083 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001084}
1085
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001086/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001087static int PARSER_FLAGS(PyCompilerFlags *flags)
1088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 int parser_flags = 0;
1090 if (!flags)
1091 return 0;
1092 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1093 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1094 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1095 parser_flags |= PyPARSE_IGNORE_COOKIE;
1096 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1097 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1098 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001099}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001100
Thomas Wouters89f507f2006-12-13 04:49:30 +00001101#if 0
1102/* Keep an example of flags with future keyword support. */
1103#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1105 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1106 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1107 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001108#endif
1109
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001110int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001111PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 PyObject *m, *d, *v, *w, *oenc = NULL;
1114 mod_ty mod;
1115 PyArena *arena;
1116 char *ps1 = "", *ps2 = "", *enc = NULL;
1117 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +00001118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 if (fp == stdin) {
1120 /* Fetch encoding from sys.stdin */
1121 v = PySys_GetObject("stdin");
1122 if (v == NULL || v == Py_None)
1123 return -1;
1124 oenc = PyObject_GetAttrString(v, "encoding");
1125 if (!oenc)
1126 return -1;
1127 enc = _PyUnicode_AsString(oenc);
Victor Stinner386fe712010-05-19 00:34:15 +00001128 if (enc == NULL)
1129 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 }
1131 v = PySys_GetObject("ps1");
1132 if (v != NULL) {
1133 v = PyObject_Str(v);
1134 if (v == NULL)
1135 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001136 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +00001138 if (ps1 == NULL) {
1139 PyErr_Clear();
1140 ps1 = "";
1141 }
1142 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 }
1144 w = PySys_GetObject("ps2");
1145 if (w != NULL) {
1146 w = PyObject_Str(w);
1147 if (w == NULL)
1148 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001149 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +00001151 if (ps2 == NULL) {
1152 PyErr_Clear();
1153 ps2 = "";
1154 }
1155 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 }
1157 arena = PyArena_New();
1158 if (arena == NULL) {
1159 Py_XDECREF(v);
1160 Py_XDECREF(w);
1161 Py_XDECREF(oenc);
1162 return -1;
1163 }
1164 mod = PyParser_ASTFromFile(fp, filename, enc,
1165 Py_single_input, ps1, ps2,
1166 flags, &errcode, arena);
1167 Py_XDECREF(v);
1168 Py_XDECREF(w);
1169 Py_XDECREF(oenc);
1170 if (mod == NULL) {
1171 PyArena_Free(arena);
1172 if (errcode == E_EOF) {
1173 PyErr_Clear();
1174 return E_EOF;
1175 }
1176 PyErr_Print();
1177 return -1;
1178 }
1179 m = PyImport_AddModule("__main__");
1180 if (m == NULL) {
1181 PyArena_Free(arena);
1182 return -1;
1183 }
1184 d = PyModule_GetDict(m);
1185 v = run_mod(mod, filename, d, d, flags, arena);
1186 PyArena_Free(arena);
1187 flush_io();
1188 if (v == NULL) {
1189 PyErr_Print();
1190 return -1;
1191 }
1192 Py_DECREF(v);
1193 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001194}
1195
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001196/* Check whether a file maybe a pyc file: Look at the extension,
1197 the file type, and, if we may close it, at the first few bytes. */
1198
1199static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001200maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1203 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 /* Only look into the file if we are allowed to close it, since
1206 it then should also be seekable. */
1207 if (closeit) {
1208 /* Read only two bytes of the magic. If the file was opened in
1209 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1210 be read as they are on disk. */
1211 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1212 unsigned char buf[2];
1213 /* Mess: In case of -x, the stream is NOT at its start now,
1214 and ungetc() was used to push back the first newline,
1215 which makes the current stream position formally undefined,
1216 and a x-platform nightmare.
1217 Unfortunately, we have no direct way to know whether -x
1218 was specified. So we use a terrible hack: if the current
1219 stream position is not 0, we assume -x was specified, and
1220 give up. Bug 132850 on SourceForge spells out the
1221 hopelessness of trying anything else (fseek and ftell
1222 don't work predictably x-platform for text-mode files).
1223 */
1224 int ispyc = 0;
1225 if (ftell(fp) == 0) {
1226 if (fread(buf, 1, 2, fp) == 2 &&
1227 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1228 ispyc = 1;
1229 rewind(fp);
1230 }
1231 return ispyc;
1232 }
1233 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001234}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001235
Guido van Rossum0df002c2000-08-27 19:21:52 +00001236int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001237PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 PyObject *m, *d, *v;
1241 const char *ext;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001242 int set_file_name = 0, ret;
1243 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 m = PyImport_AddModule("__main__");
1246 if (m == NULL)
1247 return -1;
1248 d = PyModule_GetDict(m);
1249 if (PyDict_GetItemString(d, "__file__") == NULL) {
1250 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001251 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 if (f == NULL)
1253 return -1;
1254 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1255 Py_DECREF(f);
1256 return -1;
1257 }
Barry Warsaw916048d2011-09-20 14:45:44 -04001258 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1259 Py_DECREF(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 return -1;
Barry Warsaw916048d2011-09-20 14:45:44 -04001261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 set_file_name = 1;
1263 Py_DECREF(f);
1264 }
1265 len = strlen(filename);
1266 ext = filename + len - (len > 4 ? 4 : 0);
1267 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1268 /* Try to run a pyc file. First, re-open in binary */
1269 if (closeit)
1270 fclose(fp);
1271 if ((fp = fopen(filename, "rb")) == NULL) {
1272 fprintf(stderr, "python: Can't reopen .pyc file\n");
1273 ret = -1;
1274 goto done;
1275 }
1276 /* Turn on optimization if a .pyo file is given */
1277 if (strcmp(ext, ".pyo") == 0)
1278 Py_OptimizeFlag = 1;
1279 v = run_pyc_file(fp, filename, d, d, flags);
1280 } else {
1281 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1282 closeit, flags);
1283 }
1284 flush_io();
1285 if (v == NULL) {
1286 PyErr_Print();
1287 ret = -1;
1288 goto done;
1289 }
1290 Py_DECREF(v);
1291 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001292 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1294 PyErr_Clear();
1295 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001296}
1297
1298int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001299PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 PyObject *m, *d, *v;
1302 m = PyImport_AddModule("__main__");
1303 if (m == NULL)
1304 return -1;
1305 d = PyModule_GetDict(m);
1306 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1307 if (v == NULL) {
1308 PyErr_Print();
1309 return -1;
1310 }
1311 Py_DECREF(v);
1312 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001313}
1314
Barry Warsaw035574d1997-08-29 22:07:17 +00001315static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001316parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 long hold;
1320 PyObject *v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 /* old style errors */
1323 if (PyTuple_Check(err))
1324 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1325 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* new style errors. `err' is an instance */
Barry Warsaw035574d1997-08-29 22:07:17 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 if (! (v = PyObject_GetAttrString(err, "msg")))
1330 goto finally;
1331 *message = v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (!(v = PyObject_GetAttrString(err, "filename")))
1334 goto finally;
1335 if (v == Py_None)
1336 *filename = NULL;
1337 else if (! (*filename = _PyUnicode_AsString(v)))
1338 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 Py_DECREF(v);
1341 if (!(v = PyObject_GetAttrString(err, "lineno")))
1342 goto finally;
1343 hold = PyLong_AsLong(v);
1344 Py_DECREF(v);
1345 v = NULL;
1346 if (hold < 0 && PyErr_Occurred())
1347 goto finally;
1348 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 if (!(v = PyObject_GetAttrString(err, "offset")))
1351 goto finally;
1352 if (v == Py_None) {
1353 *offset = -1;
1354 Py_DECREF(v);
1355 v = NULL;
1356 } else {
1357 hold = PyLong_AsLong(v);
1358 Py_DECREF(v);
1359 v = NULL;
1360 if (hold < 0 && PyErr_Occurred())
1361 goto finally;
1362 *offset = (int)hold;
1363 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 if (!(v = PyObject_GetAttrString(err, "text")))
1366 goto finally;
1367 if (v == Py_None)
1368 *text = NULL;
1369 else if (!PyUnicode_Check(v) ||
1370 !(*text = _PyUnicode_AsString(v)))
1371 goto finally;
1372 Py_DECREF(v);
1373 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001374
1375finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 Py_XDECREF(v);
1377 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001378}
1379
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001380void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001381PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001384}
1385
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001386static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001387print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 char *nl;
1390 if (offset >= 0) {
Benjamin Petersona95e9772010-10-29 03:28:14 +00001391 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1392 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 for (;;) {
1394 nl = strchr(text, '\n');
1395 if (nl == NULL || nl-text >= offset)
1396 break;
1397 offset -= (int)(nl+1-text);
1398 text = nl+1;
1399 }
1400 while (*text == ' ' || *text == '\t') {
1401 text++;
1402 offset--;
1403 }
1404 }
1405 PyFile_WriteString(" ", f);
1406 PyFile_WriteString(text, f);
1407 if (*text == '\0' || text[strlen(text)-1] != '\n')
1408 PyFile_WriteString("\n", f);
1409 if (offset == -1)
1410 return;
1411 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +00001412 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001415}
1416
Guido van Rossum66e8e862001-03-23 17:54:43 +00001417static void
1418handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 PyObject *exception, *value, *tb;
1421 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 if (Py_InspectFlag)
1424 /* Don't exit if -i flag was given. This flag is set to 0
1425 * when entering interactive mode for inspecting. */
1426 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 PyErr_Fetch(&exception, &value, &tb);
1429 fflush(stdout);
1430 if (value == NULL || value == Py_None)
1431 goto done;
1432 if (PyExceptionInstance_Check(value)) {
1433 /* The error code should be in the `code' attribute. */
1434 PyObject *code = PyObject_GetAttrString(value, "code");
1435 if (code) {
1436 Py_DECREF(value);
1437 value = code;
1438 if (value == Py_None)
1439 goto done;
1440 }
1441 /* If we failed to dig out the 'code' attribute,
1442 just let the else clause below print the error. */
1443 }
1444 if (PyLong_Check(value))
1445 exitcode = (int)PyLong_AsLong(value);
1446 else {
Victor Stinnere9fb3192010-05-17 08:58:51 +00001447 PyObject *sys_stderr = PySys_GetObject("stderr");
Victor Stinner7126dbc2010-05-21 23:45:42 +00001448 if (sys_stderr != NULL && sys_stderr != Py_None) {
1449 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1450 } else {
1451 PyObject_Print(value, stderr, Py_PRINT_RAW);
1452 fflush(stderr);
1453 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 PySys_WriteStderr("\n");
1455 exitcode = 1;
1456 }
Tim Peterscf615b52003-04-19 18:47:02 +00001457 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 /* Restore and clear the exception info, in order to properly decref
1459 * the exception, value, and traceback. If we just exit instead,
1460 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1461 * some finalizers from running.
1462 */
1463 PyErr_Restore(exception, value, tb);
1464 PyErr_Clear();
1465 Py_Exit(exitcode);
1466 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001467}
1468
1469void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001470PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1475 handle_system_exit();
1476 }
1477 PyErr_Fetch(&exception, &v, &tb);
1478 if (exception == NULL)
1479 return;
1480 PyErr_NormalizeException(&exception, &v, &tb);
1481 if (tb == NULL) {
1482 tb = Py_None;
1483 Py_INCREF(tb);
1484 }
1485 PyException_SetTraceback(v, tb);
1486 if (exception == NULL)
1487 return;
1488 /* Now we know v != NULL too */
1489 if (set_sys_last_vars) {
1490 PySys_SetObject("last_type", exception);
1491 PySys_SetObject("last_value", v);
1492 PySys_SetObject("last_traceback", tb);
1493 }
1494 hook = PySys_GetObject("excepthook");
1495 if (hook) {
1496 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1497 PyObject *result = PyEval_CallObject(hook, args);
1498 if (result == NULL) {
1499 PyObject *exception2, *v2, *tb2;
1500 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1501 handle_system_exit();
1502 }
1503 PyErr_Fetch(&exception2, &v2, &tb2);
1504 PyErr_NormalizeException(&exception2, &v2, &tb2);
1505 /* It should not be possible for exception2 or v2
1506 to be NULL. However PyErr_Display() can't
1507 tolerate NULLs, so just be safe. */
1508 if (exception2 == NULL) {
1509 exception2 = Py_None;
1510 Py_INCREF(exception2);
1511 }
1512 if (v2 == NULL) {
1513 v2 = Py_None;
1514 Py_INCREF(v2);
1515 }
1516 fflush(stdout);
1517 PySys_WriteStderr("Error in sys.excepthook:\n");
1518 PyErr_Display(exception2, v2, tb2);
1519 PySys_WriteStderr("\nOriginal exception was:\n");
1520 PyErr_Display(exception, v, tb);
1521 Py_DECREF(exception2);
1522 Py_DECREF(v2);
1523 Py_XDECREF(tb2);
1524 }
1525 Py_XDECREF(result);
1526 Py_XDECREF(args);
1527 } else {
1528 PySys_WriteStderr("sys.excepthook is missing\n");
1529 PyErr_Display(exception, v, tb);
1530 }
1531 Py_XDECREF(exception);
1532 Py_XDECREF(v);
1533 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001534}
1535
Benjamin Petersone6528212008-07-15 15:32:09 +00001536static void
1537print_exception(PyObject *f, PyObject *value)
1538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 int err = 0;
1540 PyObject *type, *tb;
Benjamin Petersone6528212008-07-15 15:32:09 +00001541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 if (!PyExceptionInstance_Check(value)) {
1543 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1544 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1545 PyFile_WriteString(" found\n", f);
1546 return;
1547 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 Py_INCREF(value);
1550 fflush(stdout);
1551 type = (PyObject *) Py_TYPE(value);
1552 tb = PyException_GetTraceback(value);
1553 if (tb && tb != Py_None)
1554 err = PyTraceBack_Print(tb, f);
1555 if (err == 0 &&
1556 PyObject_HasAttrString(value, "print_file_and_line"))
1557 {
1558 PyObject *message;
1559 const char *filename, *text;
1560 int lineno, offset;
1561 if (!parse_syntax_error(value, &message, &filename,
1562 &lineno, &offset, &text))
1563 PyErr_Clear();
1564 else {
1565 char buf[10];
1566 PyFile_WriteString(" File \"", f);
1567 if (filename == NULL)
1568 PyFile_WriteString("<string>", f);
1569 else
1570 PyFile_WriteString(filename, f);
1571 PyFile_WriteString("\", line ", f);
1572 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1573 PyFile_WriteString(buf, f);
1574 PyFile_WriteString("\n", f);
1575 if (text != NULL)
1576 print_error_text(f, offset, text);
1577 Py_DECREF(value);
1578 value = message;
1579 /* Can't be bothered to check all those
1580 PyFile_WriteString() calls */
1581 if (PyErr_Occurred())
1582 err = -1;
1583 }
1584 }
1585 if (err) {
1586 /* Don't do anything else */
1587 }
1588 else {
1589 PyObject* moduleName;
1590 char* className;
1591 assert(PyExceptionClass_Check(type));
1592 className = PyExceptionClass_Name(type);
1593 if (className != NULL) {
1594 char *dot = strrchr(className, '.');
1595 if (dot != NULL)
1596 className = dot+1;
1597 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 moduleName = PyObject_GetAttrString(type, "__module__");
1600 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1601 {
Victor Stinner13b21bd2011-05-26 14:25:13 +02001602 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 err = PyFile_WriteString("<unknown>", f);
1604 }
1605 else {
1606 char* modstr = _PyUnicode_AsString(moduleName);
1607 if (modstr && strcmp(modstr, "builtins"))
1608 {
1609 err = PyFile_WriteString(modstr, f);
1610 err += PyFile_WriteString(".", f);
1611 }
1612 Py_DECREF(moduleName);
1613 }
1614 if (err == 0) {
1615 if (className == NULL)
1616 err = PyFile_WriteString("<unknown>", f);
1617 else
1618 err = PyFile_WriteString(className, f);
1619 }
1620 }
1621 if (err == 0 && (value != Py_None)) {
1622 PyObject *s = PyObject_Str(value);
1623 /* only print colon if the str() of the
1624 object is not the empty string
1625 */
1626 if (s == NULL)
1627 err = -1;
1628 else if (!PyUnicode_Check(s) ||
1629 PyUnicode_GetSize(s) != 0)
1630 err = PyFile_WriteString(": ", f);
1631 if (err == 0)
1632 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1633 Py_XDECREF(s);
1634 }
1635 /* try to write a newline in any case */
1636 err += PyFile_WriteString("\n", f);
1637 Py_XDECREF(tb);
1638 Py_DECREF(value);
1639 /* If an error happened here, don't show it.
1640 XXX This is wrong, but too many callers rely on this behavior. */
1641 if (err != 0)
1642 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001643}
1644
1645static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 "\nThe above exception was the direct cause "
1647 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001648
1649static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 "\nDuring handling of the above exception, "
1651 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001652
1653static void
1654print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 int err = 0, res;
1657 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if (seen != NULL) {
1660 /* Exception chaining */
1661 if (PySet_Add(seen, value) == -1)
1662 PyErr_Clear();
1663 else if (PyExceptionInstance_Check(value)) {
1664 cause = PyException_GetCause(value);
1665 context = PyException_GetContext(value);
1666 if (cause) {
1667 res = PySet_Contains(seen, cause);
1668 if (res == -1)
1669 PyErr_Clear();
1670 if (res == 0) {
1671 print_exception_recursive(
1672 f, cause, seen);
1673 err |= PyFile_WriteString(
1674 cause_message, f);
1675 }
1676 }
1677 else if (context) {
1678 res = PySet_Contains(seen, context);
1679 if (res == -1)
1680 PyErr_Clear();
1681 if (res == 0) {
1682 print_exception_recursive(
1683 f, context, seen);
1684 err |= PyFile_WriteString(
1685 context_message, f);
1686 }
1687 }
1688 Py_XDECREF(context);
1689 Py_XDECREF(cause);
1690 }
1691 }
1692 print_exception(f, value);
1693 if (err != 0)
1694 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001695}
1696
Thomas Wouters477c8d52006-05-27 19:21:47 +00001697void
1698PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 PyObject *seen;
1701 PyObject *f = PySys_GetObject("stderr");
1702 if (f == Py_None) {
1703 /* pass */
1704 }
1705 else if (f == NULL) {
1706 _PyObject_Dump(value);
1707 fprintf(stderr, "lost sys.stderr\n");
1708 }
1709 else {
1710 /* We choose to ignore seen being possibly NULL, and report
1711 at least the main exception (it could be a MemoryError).
1712 */
1713 seen = PySet_New(NULL);
1714 if (seen == NULL)
1715 PyErr_Clear();
1716 print_exception_recursive(f, value, seen);
1717 Py_XDECREF(seen);
1718 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001719}
1720
Guido van Rossum82598051997-03-05 00:20:32 +00001721PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001722PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 PyObject *ret = NULL;
1726 mod_ty mod;
1727 PyArena *arena = PyArena_New();
1728 if (arena == NULL)
1729 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1732 if (mod != NULL)
1733 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1734 PyArena_Free(arena);
1735 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001736}
1737
1738PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001739PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 PyObject *ret;
1743 mod_ty mod;
1744 PyArena *arena = PyArena_New();
1745 if (arena == NULL)
1746 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1749 flags, NULL, arena);
1750 if (closeit)
1751 fclose(fp);
1752 if (mod == NULL) {
1753 PyArena_Free(arena);
1754 return NULL;
1755 }
1756 ret = run_mod(mod, filename, globals, locals, flags, arena);
1757 PyArena_Free(arena);
1758 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001759}
1760
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001761static void
1762flush_io(void)
1763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 PyObject *f, *r;
1765 PyObject *type, *value, *traceback;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001766 _Py_identifier(flush);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 /* Save the current exception */
1769 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 f = PySys_GetObject("stderr");
1772 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001773 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 if (r)
1775 Py_DECREF(r);
1776 else
1777 PyErr_Clear();
1778 }
1779 f = PySys_GetObject("stdout");
1780 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001781 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 if (r)
1783 Py_DECREF(r);
1784 else
1785 PyErr_Clear();
1786 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001789}
1790
Guido van Rossum82598051997-03-05 00:20:32 +00001791static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 PyCodeObject *co;
1796 PyObject *v;
1797 co = PyAST_Compile(mod, filename, flags, arena);
1798 if (co == NULL)
1799 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001800 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 Py_DECREF(co);
1802 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001803}
1804
Guido van Rossum82598051997-03-05 00:20:32 +00001805static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001806run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 PyCodeObject *co;
1810 PyObject *v;
1811 long magic;
1812 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 magic = PyMarshal_ReadLongFromFile(fp);
1815 if (magic != PyImport_GetMagicNumber()) {
1816 PyErr_SetString(PyExc_RuntimeError,
1817 "Bad magic number in .pyc file");
1818 return NULL;
1819 }
1820 (void) PyMarshal_ReadLongFromFile(fp);
1821 v = PyMarshal_ReadLastObjectFromFile(fp);
1822 fclose(fp);
1823 if (v == NULL || !PyCode_Check(v)) {
1824 Py_XDECREF(v);
1825 PyErr_SetString(PyExc_RuntimeError,
1826 "Bad code object in .pyc file");
1827 return NULL;
1828 }
1829 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001830 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 if (v && flags)
1832 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1833 Py_DECREF(co);
1834 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001835}
1836
Guido van Rossum82598051997-03-05 00:20:32 +00001837PyObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00001838Py_CompileStringExFlags(const char *str, const char *filename, int start,
1839 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 PyCodeObject *co;
1842 mod_ty mod;
1843 PyArena *arena = PyArena_New();
1844 if (arena == NULL)
1845 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1848 if (mod == NULL) {
1849 PyArena_Free(arena);
1850 return NULL;
1851 }
1852 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1853 PyObject *result = PyAST_mod2obj(mod);
1854 PyArena_Free(arena);
1855 return result;
1856 }
Georg Brandl8334fd92010-12-04 10:26:46 +00001857 co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 PyArena_Free(arena);
1859 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001860}
1861
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001862/* For use in Py_LIMITED_API */
1863#undef Py_CompileString
1864PyObject *
1865PyCompileString(const char *str, const char *filename, int start)
1866{
1867 return Py_CompileStringFlags(str, filename, start, NULL);
1868}
1869
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001870struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001871Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 struct symtable *st;
1874 mod_ty mod;
1875 PyCompilerFlags flags;
1876 PyArena *arena = PyArena_New();
1877 if (arena == NULL)
1878 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 flags.cf_flags = 0;
1881 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1882 if (mod == NULL) {
1883 PyArena_Free(arena);
1884 return NULL;
1885 }
1886 st = PySymtable_Build(mod, filename, 0);
1887 PyArena_Free(arena);
1888 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001889}
1890
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891/* Preferred access to parser is through AST. */
1892mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001893PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 mod_ty mod;
1897 PyCompilerFlags localflags;
1898 perrdetail err;
1899 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1902 &_PyParser_Grammar, start, &err,
1903 &iflags);
1904 if (flags == NULL) {
1905 localflags.cf_flags = 0;
1906 flags = &localflags;
1907 }
1908 if (n) {
1909 flags->cf_flags |= iflags & PyCF_MASK;
1910 mod = PyAST_FromNode(n, flags, filename, arena);
1911 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 }
1913 else {
1914 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001915 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001917 err_free(&err);
1918 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919}
1920
1921mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001922PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 int start, char *ps1,
1924 char *ps2, PyCompilerFlags *flags, int *errcode,
1925 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 mod_ty mod;
1928 PyCompilerFlags localflags;
1929 perrdetail err;
1930 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
1933 &_PyParser_Grammar,
1934 start, ps1, ps2, &err, &iflags);
1935 if (flags == NULL) {
1936 localflags.cf_flags = 0;
1937 flags = &localflags;
1938 }
1939 if (n) {
1940 flags->cf_flags |= iflags & PyCF_MASK;
1941 mod = PyAST_FromNode(n, flags, filename, arena);
1942 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 }
1944 else {
1945 err_input(&err);
1946 if (errcode)
1947 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02001948 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001950 err_free(&err);
1951 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952}
1953
Guido van Rossuma110aa61994-08-29 12:50:44 +00001954/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001955
Guido van Rossuma110aa61994-08-29 12:50:44 +00001956node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001957PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 perrdetail err;
1960 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1961 &_PyParser_Grammar,
1962 start, NULL, NULL, &err, flags);
1963 if (n == NULL)
1964 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001965 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001968}
1969
Guido van Rossuma110aa61994-08-29 12:50:44 +00001970/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001971
Guido van Rossuma110aa61994-08-29 12:50:44 +00001972node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001973PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 perrdetail err;
1976 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1977 start, &err, flags);
1978 if (n == NULL)
1979 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001980 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001982}
1983
1984node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001985PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 perrdetail err;
1989 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1990 &_PyParser_Grammar, start, &err, flags);
1991 if (n == NULL)
1992 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02001993 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001995}
1996
1997node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001998PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00002001}
2002
Guido van Rossum66ebd912003-04-17 16:02:26 +00002003/* May want to move a more generalized form of this to parsetok.c or
2004 even parser modules. */
2005
2006void
Victor Stinner7f2fee32011-04-05 00:39:01 +02002007PyParser_ClearError(perrdetail *err)
2008{
2009 err_free(err);
2010}
2011
2012void
Guido van Rossum66ebd912003-04-17 16:02:26 +00002013PyParser_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
Victor Stinner7f2fee32011-04-05 00:39:01 +02002018static void
2019err_free(perrdetail *err)
2020{
2021 Py_CLEAR(err->filename);
2022}
2023
Guido van Rossuma110aa61994-08-29 12:50:44 +00002024/* Set the error appropriate to the given input error code (see errcode.h) */
2025
2026static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002027err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00002028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 PyObject *v, *w, *errtype, *errtext;
2030 PyObject *msg_obj = NULL;
2031 char *msg = NULL;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 errtype = PyExc_SyntaxError;
2034 switch (err->error) {
2035 case E_ERROR:
2036 return;
2037 case E_SYNTAX:
2038 errtype = PyExc_IndentationError;
2039 if (err->expected == INDENT)
2040 msg = "expected an indented block";
2041 else if (err->token == INDENT)
2042 msg = "unexpected indent";
2043 else if (err->token == DEDENT)
2044 msg = "unexpected unindent";
2045 else {
2046 errtype = PyExc_SyntaxError;
2047 msg = "invalid syntax";
2048 }
2049 break;
2050 case E_TOKEN:
2051 msg = "invalid token";
2052 break;
2053 case E_EOFS:
2054 msg = "EOF while scanning triple-quoted string literal";
2055 break;
2056 case E_EOLS:
2057 msg = "EOL while scanning string literal";
2058 break;
2059 case E_INTR:
2060 if (!PyErr_Occurred())
2061 PyErr_SetNone(PyExc_KeyboardInterrupt);
2062 goto cleanup;
2063 case E_NOMEM:
2064 PyErr_NoMemory();
2065 goto cleanup;
2066 case E_EOF:
2067 msg = "unexpected EOF while parsing";
2068 break;
2069 case E_TABSPACE:
2070 errtype = PyExc_TabError;
2071 msg = "inconsistent use of tabs and spaces in indentation";
2072 break;
2073 case E_OVERFLOW:
2074 msg = "expression too long";
2075 break;
2076 case E_DEDENT:
2077 errtype = PyExc_IndentationError;
2078 msg = "unindent does not match any outer indentation level";
2079 break;
2080 case E_TOODEEP:
2081 errtype = PyExc_IndentationError;
2082 msg = "too many levels of indentation";
2083 break;
2084 case E_DECODE: {
2085 PyObject *type, *value, *tb;
2086 PyErr_Fetch(&type, &value, &tb);
2087 msg = "unknown decode error";
2088 if (value != NULL)
2089 msg_obj = PyObject_Str(value);
2090 Py_XDECREF(type);
2091 Py_XDECREF(value);
2092 Py_XDECREF(tb);
2093 break;
2094 }
2095 case E_LINECONT:
2096 msg = "unexpected character after line continuation character";
2097 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00002098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 case E_IDENTIFIER:
2100 msg = "invalid character in identifier";
2101 break;
2102 default:
2103 fprintf(stderr, "error=%d\n", err->error);
2104 msg = "unknown parsing error";
2105 break;
2106 }
2107 /* err->text may not be UTF-8 in case of decoding errors.
2108 Explicitly convert to an object. */
2109 if (!err->text) {
2110 errtext = Py_None;
2111 Py_INCREF(Py_None);
2112 } else {
2113 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2114 "replace");
2115 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002116 v = Py_BuildValue("(OiiN)", err->filename,
2117 err->lineno, err->offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 if (v != NULL) {
2119 if (msg_obj)
2120 w = Py_BuildValue("(OO)", msg_obj, v);
2121 else
2122 w = Py_BuildValue("(sO)", msg, v);
2123 } else
2124 w = NULL;
2125 Py_XDECREF(v);
2126 PyErr_SetObject(errtype, w);
2127 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002128cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 Py_XDECREF(msg_obj);
2130 if (err->text != NULL) {
2131 PyObject_FREE(err->text);
2132 err->text = NULL;
2133 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002134}
2135
2136/* Print fatal error message and abort */
2137
2138void
Tim Peters7c321a82002-07-09 02:57:01 +00002139Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002140{
Victor Stinner024e37a2011-03-31 01:31:06 +02002141 const int fd = fileno(stderr);
2142 PyThreadState *tstate;
2143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 fprintf(stderr, "Fatal Python error: %s\n", msg);
2145 fflush(stderr); /* it helps in Windows debug build */
2146 if (PyErr_Occurred()) {
Victor Stinner55a5c782010-06-08 21:00:13 +00002147 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 }
Victor Stinner024e37a2011-03-31 01:31:06 +02002149 else {
2150 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
2151 if (tstate != NULL) {
2152 fputc('\n', stderr);
2153 fflush(stderr);
Victor Stinner7bba62f2011-05-07 12:43:00 +02002154 _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
Victor Stinner024e37a2011-03-31 01:31:06 +02002155 }
Victor Stinnerd727e232011-04-01 12:13:55 +02002156 _PyFaulthandler_Fini();
Victor Stinner024e37a2011-03-31 01:31:06 +02002157 }
2158
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002159#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 {
2161 size_t len = strlen(msg);
2162 WCHAR* buffer;
2163 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 /* Convert the message to wchar_t. This uses a simple one-to-one
2166 conversion, assuming that the this error message actually uses ASCII
2167 only. If this ceases to be true, we will have to convert. */
2168 buffer = alloca( (len+1) * (sizeof *buffer));
2169 for( i=0; i<=len; ++i)
2170 buffer[i] = msg[i];
2171 OutputDebugStringW(L"Fatal Python error: ");
2172 OutputDebugStringW(buffer);
2173 OutputDebugStringW(L"\n");
2174 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002175#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002177#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002178#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002180}
2181
2182/* Clean up and exit */
2183
Guido van Rossuma110aa61994-08-29 12:50:44 +00002184#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002185#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002186#endif
2187
Collin Winter670e6922007-03-21 02:57:17 +00002188static void (*pyexitfunc)(void) = NULL;
2189/* For the atexit module. */
2190void _Py_PyAtExit(void (*func)(void))
2191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002193}
2194
2195static void
2196call_py_exitfuncs(void)
2197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 if (pyexitfunc == NULL)
2199 return;
Collin Winter670e6922007-03-21 02:57:17 +00002200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 (*pyexitfunc)();
2202 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002203}
2204
Antoine Pitrou011bd622009-10-20 21:52:47 +00002205/* Wait until threading._shutdown completes, provided
2206 the threading module was imported in the first place.
2207 The shutdown routine will wait until all non-daemon
2208 "threading" threads have completed. */
2209static void
2210wait_for_thread_shutdown(void)
2211{
2212#ifdef WITH_THREAD
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002213 _Py_identifier(_shutdown);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 PyObject *result;
2215 PyThreadState *tstate = PyThreadState_GET();
2216 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2217 "threading");
2218 if (threading == NULL) {
2219 /* threading not imported */
2220 PyErr_Clear();
2221 return;
2222 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002223 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 if (result == NULL) {
2225 PyErr_WriteUnraisable(threading);
2226 }
2227 else {
2228 Py_DECREF(result);
2229 }
2230 Py_DECREF(threading);
Antoine Pitrou011bd622009-10-20 21:52:47 +00002231#endif
2232}
2233
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002234#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002235static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002236static int nexitfuncs = 0;
2237
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002238int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 if (nexitfuncs >= NEXITFUNCS)
2241 return -1;
2242 exitfuncs[nexitfuncs++] = func;
2243 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002244}
2245
Guido van Rossumcc283f51997-08-05 02:22:03 +00002246static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002247call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 while (nexitfuncs > 0)
2250 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 fflush(stdout);
2253 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002254}
2255
2256void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002257Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002262}
2263
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002264static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002265initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002266{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002267#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002269#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002270#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002272#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002273#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002275#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002277}
2278
Guido van Rossum7433b121997-02-14 19:45:36 +00002279
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002280/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2281 *
2282 * All of the code in this function must only use async-signal-safe functions,
2283 * listed at `man 7 signal` or
2284 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2285 */
2286void
2287_Py_RestoreSignals(void)
2288{
2289#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 PyOS_setsig(SIGPIPE, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002291#endif
2292#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 PyOS_setsig(SIGXFZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002294#endif
2295#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 PyOS_setsig(SIGXFSZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002297#endif
2298}
2299
2300
Guido van Rossum7433b121997-02-14 19:45:36 +00002301/*
2302 * The file descriptor fd is considered ``interactive'' if either
2303 * a) isatty(fd) is TRUE, or
2304 * b) the -i flag was given, and the filename associated with
2305 * the descriptor is NULL or "<stdin>" or "???".
2306 */
2307int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002308Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 if (isatty((int)fileno(fp)))
2311 return 1;
2312 if (!Py_InteractiveFlag)
2313 return 0;
2314 return (filename == NULL) ||
2315 (strcmp(filename, "<stdin>") == 0) ||
2316 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002317}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002318
2319
Tim Petersd08e3822003-04-17 15:24:21 +00002320#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002321#if defined(WIN32) && defined(_MSC_VER)
2322
2323/* Stack checking for Microsoft C */
2324
2325#include <malloc.h>
2326#include <excpt.h>
2327
Fred Drakee8de31c2000-08-31 05:38:39 +00002328/*
2329 * Return non-zero when we run out of memory on the stack; zero otherwise.
2330 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002331int
Fred Drake399739f2000-08-31 05:52:44 +00002332PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 __try {
2335 /* alloca throws a stack overflow exception if there's
2336 not enough space left on the stack */
2337 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2338 return 0;
2339 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2340 EXCEPTION_EXECUTE_HANDLER :
2341 EXCEPTION_CONTINUE_SEARCH) {
2342 int errcode = _resetstkoflw();
2343 if (errcode == 0)
2344 {
2345 Py_FatalError("Could not reset the stack!");
2346 }
2347 }
2348 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002349}
2350
2351#endif /* WIN32 && _MSC_VER */
2352
2353/* Alternate implementations can be added here... */
2354
2355#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002356
2357
2358/* Wrappers around sigaction() or signal(). */
2359
2360PyOS_sighandler_t
2361PyOS_getsig(int sig)
2362{
2363#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 struct sigaction context;
2365 if (sigaction(sig, NULL, &context) == -1)
2366 return SIG_ERR;
2367 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002368#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002370/* Special signal handling for the secure CRT in Visual Studio 2005 */
2371#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 switch (sig) {
2373 /* Only these signals are valid */
2374 case SIGINT:
2375 case SIGILL:
2376 case SIGFPE:
2377 case SIGSEGV:
2378 case SIGTERM:
2379 case SIGBREAK:
2380 case SIGABRT:
2381 break;
2382 /* Don't call signal() with other values or it will assert */
2383 default:
2384 return SIG_ERR;
2385 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002386#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 handler = signal(sig, SIG_IGN);
2388 if (handler != SIG_ERR)
2389 signal(sig, handler);
2390 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002391#endif
2392}
2393
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002394/*
2395 * All of the code in this function must only use async-signal-safe functions,
2396 * listed at `man 7 signal` or
2397 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2398 */
Guido van Rossum6f256182000-09-16 16:32:19 +00002399PyOS_sighandler_t
2400PyOS_setsig(int sig, PyOS_sighandler_t handler)
2401{
2402#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 /* Some code in Modules/signalmodule.c depends on sigaction() being
2404 * used here if HAVE_SIGACTION is defined. Fix that if this code
2405 * changes to invalidate that assumption.
2406 */
2407 struct sigaction context, ocontext;
2408 context.sa_handler = handler;
2409 sigemptyset(&context.sa_mask);
2410 context.sa_flags = 0;
2411 if (sigaction(sig, &context, &ocontext) == -1)
2412 return SIG_ERR;
2413 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002414#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 PyOS_sighandler_t oldhandler;
2416 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002417#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002419#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002421#endif
2422}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423
2424/* Deprecated C API functions still provided for binary compatiblity */
2425
2426#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002427PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431}
2432
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002433#undef PyParser_SimpleParseString
2434PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435PyParser_SimpleParseString(const char *str, int start)
2436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002439
2440#undef PyRun_AnyFile
2441PyAPI_FUNC(int)
2442PyRun_AnyFile(FILE *fp, const char *name)
2443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002445}
2446
2447#undef PyRun_AnyFileEx
2448PyAPI_FUNC(int)
2449PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002452}
2453
2454#undef PyRun_AnyFileFlags
2455PyAPI_FUNC(int)
2456PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002459}
2460
2461#undef PyRun_File
2462PyAPI_FUNC(PyObject *)
2463PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002466}
2467
2468#undef PyRun_FileEx
2469PyAPI_FUNC(PyObject *)
2470PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002473}
2474
2475#undef PyRun_FileFlags
2476PyAPI_FUNC(PyObject *)
2477PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002481}
2482
2483#undef PyRun_SimpleFile
2484PyAPI_FUNC(int)
2485PyRun_SimpleFile(FILE *f, const char *p)
2486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002488}
2489
2490#undef PyRun_SimpleFileEx
2491PyAPI_FUNC(int)
2492PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002495}
2496
2497
2498#undef PyRun_String
2499PyAPI_FUNC(PyObject *)
2500PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002503}
2504
2505#undef PyRun_SimpleString
2506PyAPI_FUNC(int)
2507PyRun_SimpleString(const char *s)
2508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002510}
2511
2512#undef Py_CompileString
2513PyAPI_FUNC(PyObject *)
2514Py_CompileString(const char *str, const char *p, int s)
2515{
Georg Brandl8334fd92010-12-04 10:26:46 +00002516 return Py_CompileStringExFlags(str, p, s, NULL, -1);
2517}
2518
2519#undef Py_CompileStringFlags
2520PyAPI_FUNC(PyObject *)
2521Py_CompileStringFlags(const char *str, const char *p, int s,
2522 PyCompilerFlags *flags)
2523{
2524 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002525}
2526
2527#undef PyRun_InteractiveOne
2528PyAPI_FUNC(int)
2529PyRun_InteractiveOne(FILE *f, const char *p)
2530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002532}
2533
2534#undef PyRun_InteractiveLoop
2535PyAPI_FUNC(int)
2536PyRun_InteractiveLoop(FILE *f, const char *p)
2537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002539}
2540
2541#ifdef __cplusplus
2542}
2543#endif