blob: 30e5e6f0c373871346ada19d7379e843349089df [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
Ezio Melotti1f8898a2013-03-26 01:59:56 +020038#ifdef Py_REF_DEBUG
Antoine Pitrou208ac5c2013-04-24 20:17:53 +020039static
40void _print_total_refs(void) {
Ezio Melotti1f8898a2013-03-26 01:59:56 +020041 PyObject *xoptions, *key, *value;
42 xoptions = PySys_GetXOptions();
43 if (xoptions == NULL)
44 return;
45 key = PyUnicode_FromString("showrefcount");
46 if (key == NULL)
47 return;
48 value = PyDict_GetItem(xoptions, key);
49 Py_DECREF(key);
50 if (value == Py_True)
51 fprintf(stderr,
52 "[%" PY_FORMAT_SIZE_T "d refs, "
53 "%" PY_FORMAT_SIZE_T "d blocks]\n",
54 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
55}
56#endif
57
Neal Norwitz4281cef2006-03-04 19:58:13 +000058#ifndef Py_REF_DEBUG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000060#else /* Py_REF_DEBUG */
Ezio Melotti1f8898a2013-03-26 01:59:56 +020061#define PRINT_TOTAL_REFS() _print_total_refs()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000062#endif
63
64#ifdef __cplusplus
65extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000066#endif
67
Martin v. Löwis790465f2008-04-05 20:41:37 +000068extern wchar_t *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000069
Guido van Rossum82598051997-03-05 00:20:32 +000070extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000071
Guido van Rossumb73cc041993-11-01 16:28:59 +000072/* Forward */
Nick Coghlan85e729e2012-07-15 18:09:52 +100073static void initmain(PyInterpreterState *interp);
Victor Stinner793b5312011-04-27 00:24:21 +020074static int initfsencoding(PyInterpreterState *interp);
Tim Petersdbd9ba62000-07-09 03:09:57 +000075static void initsite(void);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000076static int initstdio(void);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000077static void flush_io(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000078static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000080static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000082static void err_input(perrdetail *);
Victor Stinner7f2fee32011-04-05 00:39:01 +020083static void err_free(perrdetail *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000084static void initsigs(void);
Collin Winter670e6922007-03-21 02:57:17 +000085static void call_py_exitfuncs(void);
Antoine Pitrou011bd622009-10-20 21:52:47 +000086static void wait_for_thread_shutdown(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000087static void call_ll_exitfuncs(void);
Victor Stinner3a50e702011-10-18 21:21:00 +020088extern int _PyUnicode_Init(void);
Victor Stinner26f91992013-07-17 01:22:45 +020089extern int _PyStructSequence_Init(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000090extern void _PyUnicode_Fini(void);
Guido van Rossumddefaf32007-01-14 03:31:43 +000091extern int _PyLong_Init(void);
92extern void PyLong_Fini(void);
Victor Stinner024e37a2011-03-31 01:31:06 +020093extern int _PyFaulthandler_Init(void);
94extern void _PyFaulthandler_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000095
Mark Hammond8d98d2c2003-04-19 15:41:53 +000096#ifdef WITH_THREAD
97extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
98extern void _PyGILState_Fini(void);
99#endif /* WITH_THREAD */
100
Guido van Rossum82598051997-03-05 00:20:32 +0000101int Py_DebugFlag; /* Needed by parser.c */
102int Py_VerboseFlag; /* Needed by import.c */
Georg Brandl8aa7e992010-12-28 18:30:18 +0000103int Py_QuietFlag; /* Needed by sysmodule.c */
Guido van Rossum7433b121997-02-14 19:45:36 +0000104int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl0b2489e2011-05-15 08:49:12 +0200105int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000106int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000107int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Christian Heimes790c8232008-01-07 21:14:23 +0000108int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +0000109int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +0000110int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000111int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Christian Heimes8dc226f2008-05-06 23:45:46 +0000112int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Antoine Pitrou05608432009-01-09 18:53:14 +0000113int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100114int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000115
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200116PyThreadState *_Py_Finalizing = NULL;
117
Christian Heimes33fe8092008-04-13 13:53:33 +0000118/* PyModule_GetWarningsModule is no longer necessary as of 2.6
119since _warnings is builtin. This API should not be used. */
120PyObject *
121PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +0000122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +0000124}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000125
Guido van Rossum25ce5661997-08-02 03:10:38 +0000126static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000127
Thomas Wouters7e474022000-07-16 12:04:32 +0000128/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000129
130int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000131Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 return initialized;
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000134}
135
Guido van Rossum25ce5661997-08-02 03:10:38 +0000136/* Global initializations. Can be undone by Py_Finalize(). Don't
137 call this twice without an intervening Py_Finalize() call. When
138 initializations fail, a fatal error is issued and the function does
139 not return. On return, the first thread and interpreter state have
140 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000141
Guido van Rossum25ce5661997-08-02 03:10:38 +0000142 Locking: you must hold the interpreter lock while calling this.
143 (If the lock has not yet been initialized, that's equivalent to
144 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000145
Guido van Rossum25ce5661997-08-02 03:10:38 +0000146*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000147
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000148static int
149add_flag(int flag, const char *envs)
150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 int env = atoi(envs);
152 if (flag < env)
153 flag = env;
154 if (flag < 1)
155 flag = 1;
156 return flag;
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000157}
158
Christian Heimes5833a2f2008-10-30 21:40:04 +0000159static char*
Victor Stinner94908bb2010-08-18 21:23:25 +0000160get_codec_name(const char *encoding)
Christian Heimes5833a2f2008-10-30 21:40:04 +0000161{
Victor Stinner94908bb2010-08-18 21:23:25 +0000162 char *name_utf8, *name_str;
Victor Stinner386fe712010-05-19 00:34:15 +0000163 PyObject *codec, *name = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200164 _Py_IDENTIFIER(name);
Christian Heimes5833a2f2008-10-30 21:40:04 +0000165
Victor Stinner94908bb2010-08-18 21:23:25 +0000166 codec = _PyCodec_Lookup(encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 if (!codec)
168 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000169
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200170 name = _PyObject_GetAttrId(codec, &PyId_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 Py_CLEAR(codec);
172 if (!name)
173 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000174
Victor Stinner94908bb2010-08-18 21:23:25 +0000175 name_utf8 = _PyUnicode_AsString(name);
Victor Stinner4ca28092011-03-20 23:09:03 +0100176 if (name_utf8 == NULL)
Victor Stinner386fe712010-05-19 00:34:15 +0000177 goto error;
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200178 name_str = _PyMem_RawStrdup(name_utf8);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 Py_DECREF(name);
Victor Stinner94908bb2010-08-18 21:23:25 +0000180 if (name_str == NULL) {
181 PyErr_NoMemory();
182 return NULL;
183 }
184 return name_str;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000185
186error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 Py_XDECREF(codec);
Victor Stinner386fe712010-05-19 00:34:15 +0000188 Py_XDECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000190}
Victor Stinner94908bb2010-08-18 21:23:25 +0000191
Victor Stinner94908bb2010-08-18 21:23:25 +0000192static char*
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200193get_locale_encoding(void)
Victor Stinner94908bb2010-08-18 21:23:25 +0000194{
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200195#ifdef MS_WINDOWS
196 char codepage[100];
197 PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
198 return get_codec_name(codepage);
199#elif defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94908bb2010-08-18 21:23:25 +0000200 char* codeset = nl_langinfo(CODESET);
201 if (!codeset || codeset[0] == '\0') {
202 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
203 return NULL;
204 }
205 return get_codec_name(codeset);
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200206#else
207 PyErr_SetNone(PyExc_NotImplementedError);
208 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000209#endif
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200210}
Christian Heimes5833a2f2008-10-30 21:40:04 +0000211
Brett Cannonfd074152012-04-14 14:10:13 -0400212static void
213import_init(PyInterpreterState *interp, PyObject *sysmod)
214{
215 PyObject *importlib;
216 PyObject *impmod;
217 PyObject *sys_modules;
218 PyObject *value;
219
220 /* Import _importlib through its frozen version, _frozen_importlib. */
Brett Cannonfd074152012-04-14 14:10:13 -0400221 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
222 Py_FatalError("Py_Initialize: can't import _frozen_importlib");
223 }
224 else if (Py_VerboseFlag) {
225 PySys_FormatStderr("import _frozen_importlib # frozen\n");
226 }
227 importlib = PyImport_AddModule("_frozen_importlib");
228 if (importlib == NULL) {
229 Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from "
230 "sys.modules");
231 }
232 interp->importlib = importlib;
233 Py_INCREF(interp->importlib);
234
235 /* Install _importlib as __import__ */
236 impmod = PyInit_imp();
237 if (impmod == NULL) {
238 Py_FatalError("Py_Initialize: can't import imp");
239 }
240 else if (Py_VerboseFlag) {
241 PySys_FormatStderr("import imp # builtin\n");
242 }
243 sys_modules = PyImport_GetModuleDict();
244 if (Py_VerboseFlag) {
245 PySys_FormatStderr("import sys # builtin\n");
246 }
Brett Cannon6f44d662012-04-15 16:08:47 -0400247 if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) {
248 Py_FatalError("Py_Initialize: can't save _imp to sys.modules");
Brett Cannonfd074152012-04-14 14:10:13 -0400249 }
250
Brett Cannone0d88a12012-04-25 20:54:04 -0400251 value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
Brett Cannonfd074152012-04-14 14:10:13 -0400252 if (value == NULL) {
253 PyErr_Print();
254 Py_FatalError("Py_Initialize: importlib install failed");
255 }
256 Py_DECREF(value);
Brett Cannonfc9ca272012-04-15 01:35:05 -0400257 Py_DECREF(impmod);
Brett Cannonfd074152012-04-14 14:10:13 -0400258
259 _PyImportZip_Init();
260}
261
262
Guido van Rossuma027efa1997-05-05 20:56:21 +0000263void
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200264_Py_InitializeEx_Private(int install_sigs, int install_importlib)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 PyInterpreterState *interp;
267 PyThreadState *tstate;
268 PyObject *bimod, *sysmod, *pstderr;
269 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 if (initialized)
273 return;
274 initialized = 1;
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200275 _Py_Finalizing = NULL;
Tim Petersd08e3822003-04-17 15:24:21 +0000276
Andrew M. Kuchlingb2ceb3a2010-02-22 23:26:10 +0000277#if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 /* Set up the LC_CTYPE locale, so we can obtain
279 the locale's charset without having to switch
280 locales. */
281 setlocale(LC_CTYPE, "");
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000282#endif
283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
285 Py_DebugFlag = add_flag(Py_DebugFlag, p);
286 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
287 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
288 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
289 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
290 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
291 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100292 /* The variable is only tested for existence here; _PyRandom_Init will
293 check its value further. */
294 if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
295 Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
296
297 _PyRandom_Init();
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 interp = PyInterpreterState_New();
300 if (interp == NULL)
301 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 tstate = PyThreadState_New(interp);
304 if (tstate == NULL)
305 Py_FatalError("Py_Initialize: can't make first thread");
306 (void) PyThreadState_Swap(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000307
Victor Stinner6961bd62010-08-17 22:26:51 +0000308#ifdef WITH_THREAD
Antoine Pitroub0b384b2010-09-20 20:13:48 +0000309 /* We can't call _PyEval_FiniThreads() in Py_Finalize because
310 destroying the GIL might fail when it is being referenced from
311 another running thread (see issue #9901).
312 Instead we destroy the previously created GIL here, which ensures
313 that we can call Py_Initialize / Py_Finalize multiple times. */
314 _PyEval_FiniThreads();
315
316 /* Auto-thread-state API */
Victor Stinner6961bd62010-08-17 22:26:51 +0000317 _PyGILState_Init(interp, tstate);
318#endif /* WITH_THREAD */
319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 if (!_PyFrame_Init())
323 Py_FatalError("Py_Initialize: can't init frames");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (!_PyLong_Init())
326 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 if (!PyByteArray_Init())
329 Py_FatalError("Py_Initialize: can't init bytearray");
Neal Norwitz6968b052007-02-27 19:02:19 +0000330
Victor Stinner1c8f0592013-07-22 22:24:54 +0200331 if (!_PyFloat_Init())
332 Py_FatalError("Py_Initialize: can't init float");
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 interp->modules = PyDict_New();
335 if (interp->modules == NULL)
336 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Init Unicode implementation; relies on the codec registry */
Victor Stinner3a50e702011-10-18 21:21:00 +0200339 if (_PyUnicode_Init() < 0)
340 Py_FatalError("Py_Initialize: can't initialize unicode");
Victor Stinner26f91992013-07-17 01:22:45 +0200341 if (_PyStructSequence_Init() < 0)
342 Py_FatalError("Py_Initialize: can't initialize structseq");
Guido van Rossumc94044c2000-03-10 23:03:54 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 bimod = _PyBuiltin_Init();
345 if (bimod == NULL)
346 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Victor Stinner49d3f252010-10-17 01:24:53 +0000347 _PyImport_FixupBuiltin(bimod, "builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 interp->builtins = PyModule_GetDict(bimod);
349 if (interp->builtins == NULL)
350 Py_FatalError("Py_Initialize: can't initialize builtins dict");
351 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 /* initialize builtin exceptions */
Brett Cannonfd074152012-04-14 14:10:13 -0400354 _PyExc_Init(bimod);
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 sysmod = _PySys_Init();
357 if (sysmod == NULL)
358 Py_FatalError("Py_Initialize: can't initialize sys");
359 interp->sysdict = PyModule_GetDict(sysmod);
360 if (interp->sysdict == NULL)
361 Py_FatalError("Py_Initialize: can't initialize sys dict");
362 Py_INCREF(interp->sysdict);
Victor Stinner49d3f252010-10-17 01:24:53 +0000363 _PyImport_FixupBuiltin(sysmod, "sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 PySys_SetPath(Py_GetPath());
365 PyDict_SetItemString(interp->sysdict, "modules",
366 interp->modules);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 /* Set up a preliminary stderr printer until we have enough
369 infrastructure for the io module in place. */
370 pstderr = PyFile_NewStdPrinter(fileno(stderr));
371 if (pstderr == NULL)
372 Py_FatalError("Py_Initialize: can't set preliminary stderr");
373 PySys_SetObject("stderr", pstderr);
374 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000375 Py_DECREF(pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 _PyImport_Init();
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 _PyImportHooks_Init();
Just van Rossum52e14d62002-12-30 22:08:05 +0000380
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000381 /* Initialize _warnings. */
382 _PyWarnings_Init();
383
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200384 if (!install_importlib)
385 return;
386
Brett Cannonfd074152012-04-14 14:10:13 -0400387 import_init(interp, sysmod);
388
Victor Stinnerd5698cb2012-07-31 02:55:49 +0200389 /* initialize the faulthandler module */
390 if (_PyFaulthandler_Init())
391 Py_FatalError("Py_Initialize: can't initialize faulthandler");
392
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000393 _PyTime_Init();
394
Victor Stinner793b5312011-04-27 00:24:21 +0200395 if (initfsencoding(interp) < 0)
396 Py_FatalError("Py_Initialize: unable to load the file system codec");
Martin v. Löwis011e8422009-05-05 04:43:17 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (install_sigs)
399 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000400
Nick Coghlan85e729e2012-07-15 18:09:52 +1000401 initmain(interp); /* Module __main__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (initstdio() < 0)
403 Py_FatalError(
404 "Py_Initialize: can't initialize sys standard streams");
405
Antoine Pitroucf9f9802010-11-10 13:55:25 +0000406 /* Initialize warnings. */
407 if (PySys_HasWarnOptions()) {
408 PyObject *warnings_module = PyImport_ImportModule("warnings");
409 if (warnings_module == NULL) {
410 fprintf(stderr, "'import warnings' failed; traceback:\n");
411 PyErr_Print();
412 }
413 Py_XDECREF(warnings_module);
414 }
415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 if (!Py_NoSiteFlag)
417 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000418}
419
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000420void
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200421Py_InitializeEx(int install_sigs)
422{
423 _Py_InitializeEx_Private(install_sigs, 1);
424}
425
426void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000427Py_Initialize(void)
428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 Py_InitializeEx(1);
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000430}
431
432
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000433#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000434extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000435#endif
436
Guido van Rossume8432ac2007-07-09 15:04:50 +0000437/* Flush stdout and stderr */
438
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100439static int
440file_is_closed(PyObject *fobj)
441{
442 int r;
443 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
444 if (tmp == NULL) {
445 PyErr_Clear();
446 return 0;
447 }
448 r = PyObject_IsTrue(tmp);
449 Py_DECREF(tmp);
450 if (r < 0)
451 PyErr_Clear();
452 return r > 0;
453}
454
Neal Norwitz2bad9702007-08-27 06:19:22 +0000455static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000456flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 PyObject *fout = PySys_GetObject("stdout");
459 PyObject *ferr = PySys_GetObject("stderr");
460 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200461 _Py_IDENTIFIER(flush);
Guido van Rossume8432ac2007-07-09 15:04:50 +0000462
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100463 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200464 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (tmp == NULL)
Antoine Pitroubddc9fe2010-08-08 20:46:42 +0000466 PyErr_WriteUnraisable(fout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 else
468 Py_DECREF(tmp);
469 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000470
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100471 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200472 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (tmp == NULL)
474 PyErr_Clear();
475 else
476 Py_DECREF(tmp);
477 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000478}
479
Guido van Rossum25ce5661997-08-02 03:10:38 +0000480/* Undo the effect of Py_Initialize().
481
482 Beware: if multiple interpreter and/or thread states exist, these
483 are not wiped out; only the current thread and interpreter state
484 are deleted. But since everything else is deleted, those other
485 interpreter and thread states should no longer be used.
486
487 (XXX We should do better, e.g. wipe out all interpreters and
488 threads.)
489
490 Locking: as above.
491
492*/
493
494void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000495Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 PyInterpreterState *interp;
498 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (!initialized)
501 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 wait_for_thread_shutdown();
Antoine Pitrou011bd622009-10-20 21:52:47 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 /* The interpreter is still entirely intact at this point, and the
506 * exit funcs may be relying on that. In particular, if some thread
507 * or exit func is still waiting to do an import, the import machinery
508 * expects Py_IsInitialized() to return true. So don't say the
509 * interpreter is uninitialized until after the exit funcs have run.
510 * Note that Threading.py uses an exit func to do a join on all the
511 * threads created thru it, so this also protects pending imports in
512 * the threads created via Threading.
513 */
514 call_py_exitfuncs();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 /* Get current thread state and interpreter pointer */
517 tstate = PyThreadState_GET();
518 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000519
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200520 /* Remaining threads (e.g. daemon threads) will automatically exit
521 after taking the GIL (in PyEval_RestoreThread()). */
522 _Py_Finalizing = tstate;
523 initialized = 0;
524
525 /* Flush stdout+stderr */
526 flush_std_files();
527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 /* Disable signal handling */
529 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 /* Collect garbage. This may call finalizers; it's nice to call these
532 * before all modules are destroyed.
533 * XXX If a __del__ or weakref callback is triggered here, and tries to
534 * XXX import a module, bad things can happen, because Python no
535 * XXX longer believes it's initialized.
536 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
537 * XXX is easy to provoke that way. I've also seen, e.g.,
538 * XXX Exception exceptions.ImportError: 'No module named sha'
539 * XXX in <function callback at 0x008F5718> ignored
540 * XXX but I'm unclear on exactly how that one happens. In any case,
541 * XXX I haven't seen a real-life report of either of these.
542 */
543 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000544#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 /* With COUNT_ALLOCS, it helps to run GC multiple times:
546 each collection might release some types from the type
547 list, so they become garbage. */
548 while (PyGC_Collect() > 0)
549 /* nothing */;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000550#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 /* Destroy all modules */
552 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 /* Flush stdout+stderr (again, in case more was printed) */
555 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* Collect final garbage. This disposes of cycles created by
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100558 * class definitions, for example.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 * XXX This is disabled because it caused too many problems. If
560 * XXX a __del__ or weakref callback triggers here, Python code has
561 * XXX a hard time running, because even the sys module has been
562 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
563 * XXX One symptom is a sequence of information-free messages
564 * XXX coming from threads (if a __del__ or callback is invoked,
565 * XXX other threads can execute too, and any exception they encounter
566 * XXX triggers a comedy of errors as subsystem after subsystem
567 * XXX fails to find what it *expects* to find in sys to help report
568 * XXX the exception and consequent unexpected failures). I've also
569 * XXX seen segfaults then, after adding print statements to the
570 * XXX Python code getting called.
571 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000572#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000574#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
577 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000578
Antoine Pitroufd417cc2013-05-05 08:12:42 +0200579 /* Cleanup typeobject.c's internal caches. */
580 _PyType_Fini();
581
Victor Stinner024e37a2011-03-31 01:31:06 +0200582 /* unload faulthandler module */
583 _PyFaulthandler_Fini();
584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000586#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000588#endif
589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000591
Tim Peters9cf25ce2003-04-17 15:21:01 +0000592#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 /* Display all objects still alive -- this can invoke arbitrary
594 * __repr__ overrides, so requires a mostly-intact interpreter.
595 * Alas, a lot of stuff may still be alive now that will be cleaned
596 * up later.
597 */
598 if (Py_GETENV("PYTHONDUMPREFS"))
599 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000600#endif /* Py_TRACE_REFS */
601
Antoine Pitroufd417cc2013-05-05 08:12:42 +0200602 /* Clear interpreter state and all thread states. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 /* Now we decref the exception classes. After this point nothing
606 can raise an exception. That's okay, because each Fini() method
607 below has been checked to make sure no exceptions are ever
608 raised.
609 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 /* Sundry finalizers */
614 PyMethod_Fini();
615 PyFrame_Fini();
616 PyCFunction_Fini();
617 PyTuple_Fini();
618 PyList_Fini();
619 PySet_Fini();
620 PyBytes_Fini();
621 PyByteArray_Fini();
622 PyLong_Fini();
623 PyFloat_Fini();
624 PyDict_Fini();
Antoine Pitrouf34a0cd2011-11-18 20:14:34 +0100625 PySlice_Fini();
Antoine Pitrou5f454a02013-05-06 21:15:57 +0200626 _PyGC_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 /* Cleanup Unicode implementation */
629 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 /* reset file system default encoding */
Victor Stinnerb744ba12010-05-15 12:27:16 +0000632 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200633 PyMem_RawFree((char*)Py_FileSystemDefaultEncoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 Py_FileSystemDefaultEncoding = NULL;
635 }
Christian Heimesc8967002007-11-30 10:18:26 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 /* XXX Still allocated:
638 - various static ad-hoc pointers to interned strings
639 - int and float free list blocks
640 - whatever various modules and libraries allocate
641 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000644
Victor Stinner51fa4582013-07-07 15:50:49 +0200645 /* Cleanup auto-thread-state */
646#ifdef WITH_THREAD
647 _PyGILState_Fini();
648#endif /* WITH_THREAD */
649
650 /* Delete current thread. After this, many C API calls become crashy. */
651 PyThreadState_Swap(NULL);
652 PyInterpreterState_Delete(interp);
653
Tim Peters269b2a62003-04-17 19:52:29 +0000654#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 /* Display addresses (& refcnts) of all objects still alive.
656 * An address can be used to find the repr of the object, printed
657 * above by _Py_PrintReferences.
658 */
659 if (Py_GETENV("PYTHONDUMPREFS"))
660 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000661#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000662#ifdef PYMALLOC_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 if (Py_GETENV("PYTHONMALLOCSTATS"))
David Malcolm49526f42012-06-22 14:55:41 -0400664 _PyObject_DebugMallocStats(stderr);
Tim Peters0e871182002-04-13 08:29:14 +0000665#endif
666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000668}
669
670/* Create and initialize a new interpreter and thread, and return the
671 new thread. This requires that Py_Initialize() has been called
672 first.
673
674 Unsuccessful initialization yields a NULL pointer. Note that *no*
675 exception information is available even in this case -- the
676 exception information is held in the thread, and there is no
677 thread.
678
679 Locking: as above.
680
681*/
682
683PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000684Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 PyInterpreterState *interp;
687 PyThreadState *tstate, *save_tstate;
688 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 if (!initialized)
691 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 interp = PyInterpreterState_New();
694 if (interp == NULL)
695 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 tstate = PyThreadState_New(interp);
698 if (tstate == NULL) {
699 PyInterpreterState_Delete(interp);
700 return NULL;
701 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 interp->modules = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000708
Victor Stinner49d3f252010-10-17 01:24:53 +0000709 bimod = _PyImport_FindBuiltin("builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 if (bimod != NULL) {
711 interp->builtins = PyModule_GetDict(bimod);
712 if (interp->builtins == NULL)
713 goto handle_error;
714 Py_INCREF(interp->builtins);
715 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 /* initialize builtin exceptions */
Brett Cannonfd074152012-04-14 14:10:13 -0400718 _PyExc_Init(bimod);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000719
Victor Stinner49d3f252010-10-17 01:24:53 +0000720 sysmod = _PyImport_FindBuiltin("sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 if (bimod != NULL && sysmod != NULL) {
722 PyObject *pstderr;
Brett Cannonfd074152012-04-14 14:10:13 -0400723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 interp->sysdict = PyModule_GetDict(sysmod);
725 if (interp->sysdict == NULL)
726 goto handle_error;
727 Py_INCREF(interp->sysdict);
728 PySys_SetPath(Py_GetPath());
729 PyDict_SetItemString(interp->sysdict, "modules",
730 interp->modules);
731 /* Set up a preliminary stderr printer until we have enough
732 infrastructure for the io module in place. */
733 pstderr = PyFile_NewStdPrinter(fileno(stderr));
734 if (pstderr == NULL)
735 Py_FatalError("Py_Initialize: can't set preliminary stderr");
736 PySys_SetObject("stderr", pstderr);
737 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000738 Py_DECREF(pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 _PyImportHooks_Init();
Victor Stinner793b5312011-04-27 00:24:21 +0200741
Brett Cannonfd074152012-04-14 14:10:13 -0400742 import_init(interp, sysmod);
743
Victor Stinner793b5312011-04-27 00:24:21 +0200744 if (initfsencoding(interp) < 0)
745 goto handle_error;
746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 if (initstdio() < 0)
748 Py_FatalError(
749 "Py_Initialize: can't initialize sys standard streams");
Nick Coghlan85e729e2012-07-15 18:09:52 +1000750 initmain(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 if (!Py_NoSiteFlag)
752 initsite();
753 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 if (!PyErr_Occurred())
756 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000757
Thomas Wouters89f507f2006-12-13 04:49:30 +0000758handle_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000760
Victor Stinnerc40a3502011-04-27 00:20:27 +0200761 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 PyThreadState_Clear(tstate);
763 PyThreadState_Swap(save_tstate);
764 PyThreadState_Delete(tstate);
765 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000768}
769
770/* Delete an interpreter and its last thread. This requires that the
771 given thread state is current, that the thread has no remaining
772 frames, and that it is its interpreter's only remaining thread.
773 It is a fatal error to violate these constraints.
774
775 (Py_Finalize() doesn't have these constraints -- it zaps
776 everything, regardless.)
777
778 Locking: as above.
779
780*/
781
782void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000783Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 if (tstate != PyThreadState_GET())
788 Py_FatalError("Py_EndInterpreter: thread is not current");
789 if (tstate->frame != NULL)
790 Py_FatalError("Py_EndInterpreter: thread still has a frame");
791 if (tstate != interp->tstate_head || tstate->next != NULL)
792 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 PyImport_Cleanup();
795 PyInterpreterState_Clear(interp);
796 PyThreadState_Swap(NULL);
797 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000798}
799
Antoine Pitrou01cca5e2012-07-05 20:56:30 +0200800#ifdef MS_WINDOWS
Martin v. Löwis790465f2008-04-05 20:41:37 +0000801static wchar_t *progname = L"python";
Antoine Pitrou01cca5e2012-07-05 20:56:30 +0200802#else
803static wchar_t *progname = L"python3";
804#endif
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000805
806void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000807Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 if (pn && *pn)
810 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000811}
812
Martin v. Löwis790465f2008-04-05 20:41:37 +0000813wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000814Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000817}
818
Martin v. Löwis790465f2008-04-05 20:41:37 +0000819static wchar_t *default_home = NULL;
820static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000821
822void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000823Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000826}
827
Martin v. Löwis790465f2008-04-05 20:41:37 +0000828wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000829Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 wchar_t *home = default_home;
832 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
833 char* chome = Py_GETENV("PYTHONHOME");
834 if (chome) {
835 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
836 if (r != (size_t)-1 && r <= PATH_MAX)
837 home = env_home;
838 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 }
841 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000842}
843
Guido van Rossum6135a871995-01-09 17:53:26 +0000844/* Create __main__ module */
845
846static void
Nick Coghlan85e729e2012-07-15 18:09:52 +1000847initmain(PyInterpreterState *interp)
Guido van Rossum6135a871995-01-09 17:53:26 +0000848{
Brett Cannon13853a62013-05-04 17:37:09 -0400849 PyObject *m, *d, *loader;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 m = PyImport_AddModule("__main__");
851 if (m == NULL)
852 Py_FatalError("can't create __main__ module");
853 d = PyModule_GetDict(m);
854 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
855 PyObject *bimod = PyImport_ImportModule("builtins");
Nick Coghlan85e729e2012-07-15 18:09:52 +1000856 if (bimod == NULL) {
857 Py_FatalError("Failed to retrieve builtins module");
858 }
859 if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
860 Py_FatalError("Failed to initialize __main__.__builtins__");
861 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 Py_DECREF(bimod);
863 }
Nick Coghlan85e729e2012-07-15 18:09:52 +1000864 /* Main is a little special - imp.is_builtin("__main__") will return
865 * False, but BuiltinImporter is still the most appropriate initial
866 * setting for its __loader__ attribute. A more suitable value will
867 * be set if __main__ gets further initialized later in the startup
868 * process.
869 */
Brett Cannon13853a62013-05-04 17:37:09 -0400870 loader = PyDict_GetItemString(d, "__loader__");
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400871 if (loader == NULL || loader == Py_None) {
Nick Coghlan85e729e2012-07-15 18:09:52 +1000872 PyObject *loader = PyObject_GetAttrString(interp->importlib,
873 "BuiltinImporter");
874 if (loader == NULL) {
875 Py_FatalError("Failed to retrieve BuiltinImporter");
876 }
877 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
878 Py_FatalError("Failed to initialize __main__.__loader__");
879 }
880 Py_DECREF(loader);
881 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000882}
883
Victor Stinner793b5312011-04-27 00:24:21 +0200884static int
885initfsencoding(PyInterpreterState *interp)
Victor Stinnerb744ba12010-05-15 12:27:16 +0000886{
887 PyObject *codec;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000888
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200889 if (Py_FileSystemDefaultEncoding == NULL)
890 {
891 Py_FileSystemDefaultEncoding = get_locale_encoding();
892 if (Py_FileSystemDefaultEncoding == NULL)
Victor Stinnere4743092010-10-19 00:05:51 +0000893 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
Victor Stinnerb744ba12010-05-15 12:27:16 +0000894
Victor Stinnere4743092010-10-19 00:05:51 +0000895 Py_HasFileSystemDefaultEncoding = 0;
Victor Stinner793b5312011-04-27 00:24:21 +0200896 interp->fscodec_initialized = 1;
897 return 0;
Victor Stinner7f84ab52010-06-11 00:36:33 +0000898 }
Victor Stinnerb744ba12010-05-15 12:27:16 +0000899
900 /* the encoding is mbcs, utf-8 or ascii */
901 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
902 if (!codec) {
903 /* Such error can only occurs in critical situations: no more
904 * memory, import a module of the standard library failed,
905 * etc. */
Victor Stinner793b5312011-04-27 00:24:21 +0200906 return -1;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000907 }
Victor Stinner793b5312011-04-27 00:24:21 +0200908 Py_DECREF(codec);
909 interp->fscodec_initialized = 1;
910 return 0;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000911}
912
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000913/* Import the site module (not into __main__ though) */
914
915static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000916initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 PyObject *m;
919 m = PyImport_ImportModule("site");
920 if (m == NULL) {
Victor Stinner62ce62a2013-07-22 22:53:28 +0200921 fprintf(stderr, "Failed to import the site module\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 PyErr_Print();
923 Py_Finalize();
924 exit(1);
925 }
926 else {
927 Py_DECREF(m);
928 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000929}
930
Antoine Pitrou05608432009-01-09 18:53:14 +0000931static PyObject*
932create_stdio(PyObject* io,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 int fd, int write_mode, char* name,
934 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +0000935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
937 const char* mode;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000938 const char* newline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 PyObject *line_buffering;
940 int buffering, isatty;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200941 _Py_IDENTIFIER(open);
942 _Py_IDENTIFIER(isatty);
943 _Py_IDENTIFIER(TextIOWrapper);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200944 _Py_IDENTIFIER(name);
945 _Py_IDENTIFIER(mode);
Antoine Pitrou05608432009-01-09 18:53:14 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 /* stdin is always opened in buffered mode, first because it shouldn't
948 make a difference in common use cases, second because TextIOWrapper
949 depends on the presence of a read1() method which only exists on
950 buffered streams.
951 */
952 if (Py_UnbufferedStdioFlag && write_mode)
953 buffering = 0;
954 else
955 buffering = -1;
956 if (write_mode)
957 mode = "wb";
958 else
959 mode = "rb";
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200960 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
961 fd, mode, buffering,
962 Py_None, Py_None, Py_None, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 if (buf == NULL)
964 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 if (buffering) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200967 _Py_IDENTIFIER(raw);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200968 raw = _PyObject_GetAttrId(buf, &PyId_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 if (raw == NULL)
970 goto error;
971 }
972 else {
973 raw = buf;
974 Py_INCREF(raw);
975 }
Antoine Pitrou05608432009-01-09 18:53:14 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 text = PyUnicode_FromString(name);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200978 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 goto error;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200980 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 if (res == NULL)
982 goto error;
983 isatty = PyObject_IsTrue(res);
984 Py_DECREF(res);
985 if (isatty == -1)
986 goto error;
987 if (isatty || Py_UnbufferedStdioFlag)
988 line_buffering = Py_True;
989 else
990 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +0000991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 Py_CLEAR(raw);
993 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +0000994
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000995#ifdef MS_WINDOWS
Victor Stinner7b3f0fa2012-08-04 01:28:00 +0200996 /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
997 newlines to "\n".
998 sys.stdout and sys.stderr: translate "\n" to "\r\n". */
999 newline = NULL;
1000#else
1001 /* sys.stdin: split lines at "\n".
1002 sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
1003 newline = "\n";
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00001004#endif
1005
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001006 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
1007 buf, encoding, errors,
1008 newline, line_buffering);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Py_CLEAR(buf);
1010 if (stream == NULL)
1011 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 if (write_mode)
1014 mode = "w";
1015 else
1016 mode = "r";
1017 text = PyUnicode_FromString(mode);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001018 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 goto error;
1020 Py_CLEAR(text);
1021 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +00001022
1023error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 Py_XDECREF(buf);
1025 Py_XDECREF(stream);
1026 Py_XDECREF(text);
1027 Py_XDECREF(raw);
1028 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +00001029}
1030
Antoine Pitrou11942a52011-11-28 19:08:36 +01001031static int
1032is_valid_fd(int fd)
1033{
1034 int dummy_fd;
1035 if (fd < 0 || !_PyVerify_fd(fd))
1036 return 0;
1037 dummy_fd = dup(fd);
1038 if (dummy_fd < 0)
1039 return 0;
1040 close(dummy_fd);
1041 return 1;
1042}
1043
Georg Brandl1a3284e2007-12-02 09:40:06 +00001044/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001045static int
1046initstdio(void)
1047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 PyObject *iomod = NULL, *wrapper;
1049 PyObject *bimod = NULL;
1050 PyObject *m;
1051 PyObject *std = NULL;
1052 int status = 0, fd;
1053 PyObject * encoding_attr;
1054 char *encoding = NULL, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 /* Hack to avoid a nasty recursion issue when Python is invoked
1057 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
1058 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
1059 goto error;
1060 }
1061 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
1064 goto error;
1065 }
1066 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (!(bimod = PyImport_ImportModule("builtins"))) {
1069 goto error;
1070 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 if (!(iomod = PyImport_ImportModule("io"))) {
1073 goto error;
1074 }
1075 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
1076 goto error;
1077 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 /* Set builtins.open */
1080 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
Antoine Pitrou5a96b522010-11-20 19:50:57 +00001081 Py_DECREF(wrapper);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 goto error;
1083 }
Antoine Pitrou5a96b522010-11-20 19:50:57 +00001084 Py_DECREF(wrapper);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 encoding = Py_GETENV("PYTHONIOENCODING");
1087 errors = NULL;
1088 if (encoding) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001089 encoding = _PyMem_Strdup(encoding);
1090 if (encoding == NULL) {
1091 PyErr_NoMemory();
1092 goto error;
1093 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 errors = strchr(encoding, ':');
1095 if (errors) {
1096 *errors = '\0';
1097 errors++;
1098 }
1099 }
Martin v. Löwis0f599892008-06-02 11:13:03 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 /* Set sys.stdin */
1102 fd = fileno(stdin);
1103 /* Under some conditions stdin, stdout and stderr may not be connected
1104 * and fileno() may point to an invalid file descriptor. For example
1105 * GUI apps don't have valid standard streams by default.
1106 */
Antoine Pitrou11942a52011-11-28 19:08:36 +01001107 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 std = Py_None;
1109 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 }
1111 else {
1112 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
1113 if (std == NULL)
1114 goto error;
1115 } /* if (fd < 0) */
1116 PySys_SetObject("__stdin__", std);
1117 PySys_SetObject("stdin", std);
1118 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 /* Set sys.stdout */
1121 fd = fileno(stdout);
Antoine Pitrou11942a52011-11-28 19:08:36 +01001122 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 std = Py_None;
1124 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 }
1126 else {
1127 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
1128 if (std == NULL)
1129 goto error;
1130 } /* if (fd < 0) */
1131 PySys_SetObject("__stdout__", std);
1132 PySys_SetObject("stdout", std);
1133 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001134
Guido van Rossum98297ee2007-11-06 21:34:58 +00001135#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 /* Set sys.stderr, replaces the preliminary stderr */
1137 fd = fileno(stderr);
Antoine Pitrou11942a52011-11-28 19:08:36 +01001138 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 std = Py_None;
1140 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 }
1142 else {
1143 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1144 if (std == NULL)
1145 goto error;
1146 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 /* Same as hack above, pre-import stderr's codec to avoid recursion
1149 when import.c tries to write to stderr in verbose mode. */
1150 encoding_attr = PyObject_GetAttrString(std, "encoding");
1151 if (encoding_attr != NULL) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001152 const char * std_encoding;
1153 std_encoding = _PyUnicode_AsString(encoding_attr);
1154 if (std_encoding != NULL) {
1155 PyObject *codec_info = _PyCodec_Lookup(std_encoding);
Antoine Pitrou2fabfac2012-01-18 15:14:46 +01001156 Py_XDECREF(codec_info);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 }
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +00001158 Py_DECREF(encoding_attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 }
1160 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 PySys_SetObject("__stderr__", std);
1163 PySys_SetObject("stderr", std);
1164 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001165#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001168 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 status = -1;
1170 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001171
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001172 PyMem_Free(encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 Py_XDECREF(bimod);
1174 Py_XDECREF(iomod);
1175 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001176}
1177
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001178/* Parse input from a file and execute it */
1179
1180int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001181PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 if (filename == NULL)
1185 filename = "???";
1186 if (Py_FdIsInteractive(fp, filename)) {
1187 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
1188 if (closeit)
1189 fclose(fp);
1190 return err;
1191 }
1192 else
1193 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001194}
1195
1196int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001197PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 PyObject *v;
1200 int ret;
1201 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 if (flags == NULL) {
1204 flags = &local_flags;
1205 local_flags.cf_flags = 0;
1206 }
1207 v = PySys_GetObject("ps1");
1208 if (v == NULL) {
1209 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
1210 Py_XDECREF(v);
1211 }
1212 v = PySys_GetObject("ps2");
1213 if (v == NULL) {
1214 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1215 Py_XDECREF(v);
1216 }
1217 for (;;) {
1218 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
1219 PRINT_TOTAL_REFS();
1220 if (ret == E_EOF)
1221 return 0;
1222 /*
1223 if (ret == E_NOMEM)
1224 return -1;
1225 */
1226 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001227}
1228
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001229/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001230static int PARSER_FLAGS(PyCompilerFlags *flags)
1231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 int parser_flags = 0;
1233 if (!flags)
1234 return 0;
1235 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1236 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1237 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1238 parser_flags |= PyPARSE_IGNORE_COOKIE;
1239 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1240 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1241 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001242}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001243
Thomas Wouters89f507f2006-12-13 04:49:30 +00001244#if 0
1245/* Keep an example of flags with future keyword support. */
1246#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1248 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1249 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1250 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001251#endif
1252
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001253int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001254PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 PyObject *m, *d, *v, *w, *oenc = NULL;
1257 mod_ty mod;
1258 PyArena *arena;
1259 char *ps1 = "", *ps2 = "", *enc = NULL;
1260 int errcode = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001261 _Py_IDENTIFIER(encoding);
Tim Petersfe2127d2001-07-16 05:37:24 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 if (fp == stdin) {
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -04001264 /* Fetch encoding from sys.stdin if possible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 v = PySys_GetObject("stdin");
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -04001266 if (v && v != Py_None) {
1267 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
1268 if (oenc)
1269 enc = _PyUnicode_AsString(oenc);
1270 if (!enc)
1271 PyErr_Clear();
1272 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 }
1274 v = PySys_GetObject("ps1");
1275 if (v != NULL) {
1276 v = PyObject_Str(v);
1277 if (v == NULL)
1278 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001279 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +00001281 if (ps1 == NULL) {
1282 PyErr_Clear();
1283 ps1 = "";
1284 }
1285 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 }
1287 w = PySys_GetObject("ps2");
1288 if (w != NULL) {
1289 w = PyObject_Str(w);
1290 if (w == NULL)
1291 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001292 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +00001294 if (ps2 == NULL) {
1295 PyErr_Clear();
1296 ps2 = "";
1297 }
1298 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 }
1300 arena = PyArena_New();
1301 if (arena == NULL) {
1302 Py_XDECREF(v);
1303 Py_XDECREF(w);
1304 Py_XDECREF(oenc);
1305 return -1;
1306 }
1307 mod = PyParser_ASTFromFile(fp, filename, enc,
1308 Py_single_input, ps1, ps2,
1309 flags, &errcode, arena);
1310 Py_XDECREF(v);
1311 Py_XDECREF(w);
1312 Py_XDECREF(oenc);
1313 if (mod == NULL) {
1314 PyArena_Free(arena);
1315 if (errcode == E_EOF) {
1316 PyErr_Clear();
1317 return E_EOF;
1318 }
1319 PyErr_Print();
1320 return -1;
1321 }
1322 m = PyImport_AddModule("__main__");
1323 if (m == NULL) {
1324 PyArena_Free(arena);
1325 return -1;
1326 }
1327 d = PyModule_GetDict(m);
1328 v = run_mod(mod, filename, d, d, flags, arena);
1329 PyArena_Free(arena);
1330 flush_io();
1331 if (v == NULL) {
1332 PyErr_Print();
1333 return -1;
1334 }
1335 Py_DECREF(v);
1336 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001337}
1338
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001339/* Check whether a file maybe a pyc file: Look at the extension,
1340 the file type, and, if we may close it, at the first few bytes. */
1341
1342static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001343maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1346 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 /* Only look into the file if we are allowed to close it, since
1349 it then should also be seekable. */
1350 if (closeit) {
1351 /* Read only two bytes of the magic. If the file was opened in
1352 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1353 be read as they are on disk. */
1354 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1355 unsigned char buf[2];
1356 /* Mess: In case of -x, the stream is NOT at its start now,
1357 and ungetc() was used to push back the first newline,
1358 which makes the current stream position formally undefined,
1359 and a x-platform nightmare.
1360 Unfortunately, we have no direct way to know whether -x
1361 was specified. So we use a terrible hack: if the current
1362 stream position is not 0, we assume -x was specified, and
1363 give up. Bug 132850 on SourceForge spells out the
1364 hopelessness of trying anything else (fseek and ftell
1365 don't work predictably x-platform for text-mode files).
1366 */
1367 int ispyc = 0;
1368 if (ftell(fp) == 0) {
1369 if (fread(buf, 1, 2, fp) == 2 &&
1370 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1371 ispyc = 1;
1372 rewind(fp);
1373 }
1374 return ispyc;
1375 }
1376 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001377}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001378
Guido van Rossum0df002c2000-08-27 19:21:52 +00001379int
Nick Coghlanceda83c2012-07-15 23:18:08 +10001380static set_main_loader(PyObject *d, const char *filename, const char *loader_name)
Nick Coghlan85e729e2012-07-15 18:09:52 +10001381{
1382 PyInterpreterState *interp;
1383 PyThreadState *tstate;
Andrew Svetlov90c0eb22012-11-01 14:51:14 +02001384 PyObject *filename_obj, *loader_type, *loader;
Nick Coghlanb7a58942012-07-15 23:21:08 +10001385 int result = 0;
Andrew Svetlov90c0eb22012-11-01 14:51:14 +02001386
1387 filename_obj = PyUnicode_DecodeFSDefault(filename);
1388 if (filename_obj == NULL)
1389 return -1;
Nick Coghlan85e729e2012-07-15 18:09:52 +10001390 /* Get current thread state and interpreter pointer */
1391 tstate = PyThreadState_GET();
1392 interp = tstate->interp;
Nick Coghlan3f94cbf2012-07-15 19:10:39 +10001393 loader_type = PyObject_GetAttrString(interp->importlib, loader_name);
1394 if (loader_type == NULL) {
Andrew Svetlov90c0eb22012-11-01 14:51:14 +02001395 Py_DECREF(filename_obj);
Nick Coghlan3f94cbf2012-07-15 19:10:39 +10001396 return -1;
1397 }
Andrew Svetlov90c0eb22012-11-01 14:51:14 +02001398 loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
Nick Coghlanb7a58942012-07-15 23:21:08 +10001399 Py_DECREF(loader_type);
1400 if (loader == NULL) {
Nick Coghlan85e729e2012-07-15 18:09:52 +10001401 return -1;
1402 }
Nick Coghlanb7a58942012-07-15 23:21:08 +10001403 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
1404 result = -1;
1405 }
Nick Coghlan85e729e2012-07-15 18:09:52 +10001406 Py_DECREF(loader);
Nick Coghlanb7a58942012-07-15 23:21:08 +10001407 return result;
Nick Coghlan85e729e2012-07-15 18:09:52 +10001408}
1409
1410int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001411PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 PyObject *m, *d, *v;
1415 const char *ext;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001416 int set_file_name = 0, ret = -1;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001417 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 m = PyImport_AddModule("__main__");
1420 if (m == NULL)
1421 return -1;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001422 Py_INCREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 d = PyModule_GetDict(m);
1424 if (PyDict_GetItemString(d, "__file__") == NULL) {
1425 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001426 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 if (f == NULL)
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001428 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1430 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001431 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 }
Barry Warsaw916048d2011-09-20 14:45:44 -04001433 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1434 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001435 goto done;
Barry Warsaw916048d2011-09-20 14:45:44 -04001436 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 set_file_name = 1;
1438 Py_DECREF(f);
1439 }
1440 len = strlen(filename);
1441 ext = filename + len - (len > 4 ? 4 : 0);
1442 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Christian Heimes04ac4c12012-09-11 15:47:28 +02001443 FILE *pyc_fp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 /* Try to run a pyc file. First, re-open in binary */
1445 if (closeit)
1446 fclose(fp);
Christian Heimes04ac4c12012-09-11 15:47:28 +02001447 if ((pyc_fp = fopen(filename, "rb")) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 fprintf(stderr, "python: Can't reopen .pyc file\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 goto done;
1450 }
1451 /* Turn on optimization if a .pyo file is given */
1452 if (strcmp(ext, ".pyo") == 0)
1453 Py_OptimizeFlag = 1;
Nick Coghlan85e729e2012-07-15 18:09:52 +10001454
1455 if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
1456 fprintf(stderr, "python: failed to set __main__.__loader__\n");
1457 ret = -1;
Christian Heimes04ac4c12012-09-11 15:47:28 +02001458 fclose(pyc_fp);
Nick Coghlan85e729e2012-07-15 18:09:52 +10001459 goto done;
1460 }
Christian Heimes04ac4c12012-09-11 15:47:28 +02001461 v = run_pyc_file(pyc_fp, filename, d, d, flags);
1462 fclose(pyc_fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 } else {
Nick Coghlan85e729e2012-07-15 18:09:52 +10001464 /* When running from stdin, leave __main__.__loader__ alone */
1465 if (strcmp(filename, "<stdin>") != 0 &&
1466 set_main_loader(d, filename, "SourceFileLoader") < 0) {
1467 fprintf(stderr, "python: failed to set __main__.__loader__\n");
1468 ret = -1;
1469 goto done;
1470 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1472 closeit, flags);
1473 }
1474 flush_io();
1475 if (v == NULL) {
1476 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 goto done;
1478 }
1479 Py_DECREF(v);
1480 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001481 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1483 PyErr_Clear();
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001484 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001486}
1487
1488int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001489PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 PyObject *m, *d, *v;
1492 m = PyImport_AddModule("__main__");
1493 if (m == NULL)
1494 return -1;
1495 d = PyModule_GetDict(m);
1496 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1497 if (v == NULL) {
1498 PyErr_Print();
1499 return -1;
1500 }
1501 Py_DECREF(v);
1502 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001503}
1504
Barry Warsaw035574d1997-08-29 22:07:17 +00001505static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001506parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 long hold;
1510 PyObject *v;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001511 _Py_IDENTIFIER(msg);
1512 _Py_IDENTIFIER(filename);
1513 _Py_IDENTIFIER(lineno);
1514 _Py_IDENTIFIER(offset);
1515 _Py_IDENTIFIER(text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001516
Benjamin Peterson80d50422012-04-03 00:30:38 -04001517 *message = NULL;
1518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 /* new style errors. `err' is an instance */
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001520 *message = _PyObject_GetAttrId(err, &PyId_msg);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001521 if (!*message)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001523
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001524 v = _PyObject_GetAttrId(err, &PyId_filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001525 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001527 if (v == Py_None) {
1528 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 *filename = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001530 }
1531 else {
1532 *filename = _PyUnicode_AsString(v);
1533 Py_DECREF(v);
1534 if (!*filename)
1535 goto finally;
1536 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001537
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001538 v = _PyObject_GetAttrId(err, &PyId_lineno);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001539 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 goto finally;
1541 hold = PyLong_AsLong(v);
1542 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 if (hold < 0 && PyErr_Occurred())
1544 goto finally;
1545 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001546
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001547 v = _PyObject_GetAttrId(err, &PyId_offset);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001548 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 goto finally;
1550 if (v == Py_None) {
1551 *offset = -1;
1552 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 } else {
1554 hold = PyLong_AsLong(v);
1555 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 if (hold < 0 && PyErr_Occurred())
1557 goto finally;
1558 *offset = (int)hold;
1559 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001560
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001561 v = _PyObject_GetAttrId(err, &PyId_text);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001562 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001564 if (v == Py_None) {
1565 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 *text = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001567 }
1568 else {
1569 *text = _PyUnicode_AsString(v);
1570 Py_DECREF(v);
1571 if (!*text)
1572 goto finally;
1573 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001575
1576finally:
Benjamin Peterson80d50422012-04-03 00:30:38 -04001577 Py_XDECREF(*message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001579}
1580
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001581void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001582PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001585}
1586
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001587static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001588print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 char *nl;
1591 if (offset >= 0) {
Benjamin Petersona95e9772010-10-29 03:28:14 +00001592 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1593 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 for (;;) {
1595 nl = strchr(text, '\n');
1596 if (nl == NULL || nl-text >= offset)
1597 break;
1598 offset -= (int)(nl+1-text);
1599 text = nl+1;
1600 }
1601 while (*text == ' ' || *text == '\t') {
1602 text++;
1603 offset--;
1604 }
1605 }
1606 PyFile_WriteString(" ", f);
1607 PyFile_WriteString(text, f);
1608 if (*text == '\0' || text[strlen(text)-1] != '\n')
1609 PyFile_WriteString("\n", f);
1610 if (offset == -1)
1611 return;
1612 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +00001613 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001616}
1617
Guido van Rossum66e8e862001-03-23 17:54:43 +00001618static void
1619handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 PyObject *exception, *value, *tb;
1622 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 if (Py_InspectFlag)
1625 /* Don't exit if -i flag was given. This flag is set to 0
1626 * when entering interactive mode for inspecting. */
1627 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 PyErr_Fetch(&exception, &value, &tb);
1630 fflush(stdout);
1631 if (value == NULL || value == Py_None)
1632 goto done;
1633 if (PyExceptionInstance_Check(value)) {
1634 /* The error code should be in the `code' attribute. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001635 _Py_IDENTIFIER(code);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001636 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 if (code) {
1638 Py_DECREF(value);
1639 value = code;
1640 if (value == Py_None)
1641 goto done;
1642 }
1643 /* If we failed to dig out the 'code' attribute,
1644 just let the else clause below print the error. */
1645 }
1646 if (PyLong_Check(value))
1647 exitcode = (int)PyLong_AsLong(value);
1648 else {
Victor Stinnere9fb3192010-05-17 08:58:51 +00001649 PyObject *sys_stderr = PySys_GetObject("stderr");
Victor Stinner7126dbc2010-05-21 23:45:42 +00001650 if (sys_stderr != NULL && sys_stderr != Py_None) {
1651 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1652 } else {
1653 PyObject_Print(value, stderr, Py_PRINT_RAW);
1654 fflush(stderr);
1655 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 PySys_WriteStderr("\n");
1657 exitcode = 1;
1658 }
Tim Peterscf615b52003-04-19 18:47:02 +00001659 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 /* Restore and clear the exception info, in order to properly decref
1661 * the exception, value, and traceback. If we just exit instead,
1662 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1663 * some finalizers from running.
1664 */
1665 PyErr_Restore(exception, value, tb);
1666 PyErr_Clear();
1667 Py_Exit(exitcode);
1668 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001669}
1670
1671void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001672PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1677 handle_system_exit();
1678 }
1679 PyErr_Fetch(&exception, &v, &tb);
1680 if (exception == NULL)
1681 return;
1682 PyErr_NormalizeException(&exception, &v, &tb);
1683 if (tb == NULL) {
1684 tb = Py_None;
1685 Py_INCREF(tb);
1686 }
1687 PyException_SetTraceback(v, tb);
1688 if (exception == NULL)
1689 return;
1690 /* Now we know v != NULL too */
1691 if (set_sys_last_vars) {
1692 PySys_SetObject("last_type", exception);
1693 PySys_SetObject("last_value", v);
1694 PySys_SetObject("last_traceback", tb);
1695 }
1696 hook = PySys_GetObject("excepthook");
1697 if (hook) {
1698 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1699 PyObject *result = PyEval_CallObject(hook, args);
1700 if (result == NULL) {
1701 PyObject *exception2, *v2, *tb2;
1702 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1703 handle_system_exit();
1704 }
1705 PyErr_Fetch(&exception2, &v2, &tb2);
1706 PyErr_NormalizeException(&exception2, &v2, &tb2);
1707 /* It should not be possible for exception2 or v2
1708 to be NULL. However PyErr_Display() can't
1709 tolerate NULLs, so just be safe. */
1710 if (exception2 == NULL) {
1711 exception2 = Py_None;
1712 Py_INCREF(exception2);
1713 }
1714 if (v2 == NULL) {
1715 v2 = Py_None;
1716 Py_INCREF(v2);
1717 }
1718 fflush(stdout);
1719 PySys_WriteStderr("Error in sys.excepthook:\n");
1720 PyErr_Display(exception2, v2, tb2);
1721 PySys_WriteStderr("\nOriginal exception was:\n");
1722 PyErr_Display(exception, v, tb);
1723 Py_DECREF(exception2);
1724 Py_DECREF(v2);
1725 Py_XDECREF(tb2);
1726 }
1727 Py_XDECREF(result);
1728 Py_XDECREF(args);
1729 } else {
1730 PySys_WriteStderr("sys.excepthook is missing\n");
1731 PyErr_Display(exception, v, tb);
1732 }
1733 Py_XDECREF(exception);
1734 Py_XDECREF(v);
1735 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001736}
1737
Benjamin Petersone6528212008-07-15 15:32:09 +00001738static void
1739print_exception(PyObject *f, PyObject *value)
1740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 int err = 0;
1742 PyObject *type, *tb;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001743 _Py_IDENTIFIER(print_file_and_line);
Benjamin Petersone6528212008-07-15 15:32:09 +00001744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 if (!PyExceptionInstance_Check(value)) {
1746 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1747 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1748 PyFile_WriteString(" found\n", f);
1749 return;
1750 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 Py_INCREF(value);
1753 fflush(stdout);
1754 type = (PyObject *) Py_TYPE(value);
1755 tb = PyException_GetTraceback(value);
1756 if (tb && tb != Py_None)
1757 err = PyTraceBack_Print(tb, f);
1758 if (err == 0 &&
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001759 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 {
1761 PyObject *message;
1762 const char *filename, *text;
1763 int lineno, offset;
1764 if (!parse_syntax_error(value, &message, &filename,
1765 &lineno, &offset, &text))
1766 PyErr_Clear();
1767 else {
1768 char buf[10];
1769 PyFile_WriteString(" File \"", f);
1770 if (filename == NULL)
1771 PyFile_WriteString("<string>", f);
1772 else
1773 PyFile_WriteString(filename, f);
1774 PyFile_WriteString("\", line ", f);
1775 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1776 PyFile_WriteString(buf, f);
1777 PyFile_WriteString("\n", f);
1778 if (text != NULL)
1779 print_error_text(f, offset, text);
1780 Py_DECREF(value);
1781 value = message;
1782 /* Can't be bothered to check all those
1783 PyFile_WriteString() calls */
1784 if (PyErr_Occurred())
1785 err = -1;
1786 }
1787 }
1788 if (err) {
1789 /* Don't do anything else */
1790 }
1791 else {
1792 PyObject* moduleName;
1793 char* className;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001794 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 assert(PyExceptionClass_Check(type));
1796 className = PyExceptionClass_Name(type);
1797 if (className != NULL) {
1798 char *dot = strrchr(className, '.');
1799 if (dot != NULL)
1800 className = dot+1;
1801 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001802
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001803 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1805 {
Victor Stinner13b21bd2011-05-26 14:25:13 +02001806 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 err = PyFile_WriteString("<unknown>", f);
1808 }
1809 else {
1810 char* modstr = _PyUnicode_AsString(moduleName);
1811 if (modstr && strcmp(modstr, "builtins"))
1812 {
1813 err = PyFile_WriteString(modstr, f);
1814 err += PyFile_WriteString(".", f);
1815 }
1816 Py_DECREF(moduleName);
1817 }
1818 if (err == 0) {
1819 if (className == NULL)
1820 err = PyFile_WriteString("<unknown>", f);
1821 else
1822 err = PyFile_WriteString(className, f);
1823 }
1824 }
1825 if (err == 0 && (value != Py_None)) {
1826 PyObject *s = PyObject_Str(value);
1827 /* only print colon if the str() of the
1828 object is not the empty string
1829 */
1830 if (s == NULL)
1831 err = -1;
1832 else if (!PyUnicode_Check(s) ||
Victor Stinnere251d6d2011-11-20 19:20:00 +01001833 PyUnicode_GetLength(s) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 err = PyFile_WriteString(": ", f);
1835 if (err == 0)
1836 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1837 Py_XDECREF(s);
1838 }
1839 /* try to write a newline in any case */
1840 err += PyFile_WriteString("\n", f);
1841 Py_XDECREF(tb);
1842 Py_DECREF(value);
1843 /* If an error happened here, don't show it.
1844 XXX This is wrong, but too many callers rely on this behavior. */
1845 if (err != 0)
1846 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001847}
1848
1849static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 "\nThe above exception was the direct cause "
1851 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001852
1853static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 "\nDuring handling of the above exception, "
1855 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001856
1857static void
1858print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1859{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 int err = 0, res;
1861 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00001862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 if (seen != NULL) {
1864 /* Exception chaining */
1865 if (PySet_Add(seen, value) == -1)
1866 PyErr_Clear();
1867 else if (PyExceptionInstance_Check(value)) {
1868 cause = PyException_GetCause(value);
1869 context = PyException_GetContext(value);
Benjamin Petersond5a1c442012-05-14 22:09:31 -07001870 if (cause) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 res = PySet_Contains(seen, cause);
1872 if (res == -1)
1873 PyErr_Clear();
1874 if (res == 0) {
1875 print_exception_recursive(
1876 f, cause, seen);
1877 err |= PyFile_WriteString(
1878 cause_message, f);
1879 }
1880 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07001881 else if (context &&
1882 !((PyBaseExceptionObject *)value)->suppress_context) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 res = PySet_Contains(seen, context);
1884 if (res == -1)
1885 PyErr_Clear();
1886 if (res == 0) {
1887 print_exception_recursive(
1888 f, context, seen);
1889 err |= PyFile_WriteString(
1890 context_message, f);
1891 }
1892 }
1893 Py_XDECREF(context);
1894 Py_XDECREF(cause);
1895 }
1896 }
1897 print_exception(f, value);
1898 if (err != 0)
1899 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001900}
1901
Thomas Wouters477c8d52006-05-27 19:21:47 +00001902void
1903PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 PyObject *seen;
1906 PyObject *f = PySys_GetObject("stderr");
1907 if (f == Py_None) {
1908 /* pass */
1909 }
1910 else if (f == NULL) {
1911 _PyObject_Dump(value);
1912 fprintf(stderr, "lost sys.stderr\n");
1913 }
1914 else {
1915 /* We choose to ignore seen being possibly NULL, and report
1916 at least the main exception (it could be a MemoryError).
1917 */
1918 seen = PySet_New(NULL);
1919 if (seen == NULL)
1920 PyErr_Clear();
1921 print_exception_recursive(f, value, seen);
1922 Py_XDECREF(seen);
1923 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001924}
1925
Guido van Rossum82598051997-03-05 00:20:32 +00001926PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001927PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 PyObject *ret = NULL;
1931 mod_ty mod;
1932 PyArena *arena = PyArena_New();
1933 if (arena == NULL)
1934 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1937 if (mod != NULL)
1938 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1939 PyArena_Free(arena);
1940 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001941}
1942
1943PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001944PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 PyObject *ret;
1948 mod_ty mod;
1949 PyArena *arena = PyArena_New();
1950 if (arena == NULL)
1951 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1954 flags, NULL, arena);
1955 if (closeit)
1956 fclose(fp);
1957 if (mod == NULL) {
1958 PyArena_Free(arena);
1959 return NULL;
1960 }
1961 ret = run_mod(mod, filename, globals, locals, flags, arena);
1962 PyArena_Free(arena);
1963 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001964}
1965
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001966static void
1967flush_io(void)
1968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 PyObject *f, *r;
1970 PyObject *type, *value, *traceback;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001971 _Py_IDENTIFIER(flush);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 /* Save the current exception */
1974 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 f = PySys_GetObject("stderr");
1977 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001978 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 if (r)
1980 Py_DECREF(r);
1981 else
1982 PyErr_Clear();
1983 }
1984 f = PySys_GetObject("stdout");
1985 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001986 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 if (r)
1988 Py_DECREF(r);
1989 else
1990 PyErr_Clear();
1991 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001994}
1995
Guido van Rossum82598051997-03-05 00:20:32 +00001996static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 PyCodeObject *co;
2001 PyObject *v;
2002 co = PyAST_Compile(mod, filename, flags, arena);
2003 if (co == NULL)
2004 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002005 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 Py_DECREF(co);
2007 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002008}
2009
Guido van Rossum82598051997-03-05 00:20:32 +00002010static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002011run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00002013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 PyCodeObject *co;
2015 PyObject *v;
2016 long magic;
2017 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 magic = PyMarshal_ReadLongFromFile(fp);
2020 if (magic != PyImport_GetMagicNumber()) {
2021 PyErr_SetString(PyExc_RuntimeError,
2022 "Bad magic number in .pyc file");
2023 return NULL;
2024 }
Antoine Pitrou5136ac02012-01-13 18:52:16 +01002025 /* Skip mtime and size */
2026 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 (void) PyMarshal_ReadLongFromFile(fp);
2028 v = PyMarshal_ReadLastObjectFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 if (v == NULL || !PyCode_Check(v)) {
2030 Py_XDECREF(v);
2031 PyErr_SetString(PyExc_RuntimeError,
2032 "Bad code object in .pyc file");
2033 return NULL;
2034 }
2035 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002036 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 if (v && flags)
2038 flags->cf_flags |= (co->co_flags & PyCF_MASK);
2039 Py_DECREF(co);
2040 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00002041}
2042
Guido van Rossum82598051997-03-05 00:20:32 +00002043PyObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00002044Py_CompileStringExFlags(const char *str, const char *filename, int start,
2045 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00002046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 PyCodeObject *co;
2048 mod_ty mod;
2049 PyArena *arena = PyArena_New();
2050 if (arena == NULL)
2051 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
2054 if (mod == NULL) {
2055 PyArena_Free(arena);
2056 return NULL;
2057 }
2058 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
2059 PyObject *result = PyAST_mod2obj(mod);
2060 PyArena_Free(arena);
2061 return result;
2062 }
Georg Brandl8334fd92010-12-04 10:26:46 +00002063 co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 PyArena_Free(arena);
2065 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00002066}
2067
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002068/* For use in Py_LIMITED_API */
2069#undef Py_CompileString
2070PyObject *
2071PyCompileString(const char *str, const char *filename, int start)
2072{
2073 return Py_CompileStringFlags(str, filename, start, NULL);
2074}
2075
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002076struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002077Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 struct symtable *st;
2080 mod_ty mod;
2081 PyCompilerFlags flags;
2082 PyArena *arena = PyArena_New();
2083 if (arena == NULL)
2084 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 flags.cf_flags = 0;
2087 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
2088 if (mod == NULL) {
2089 PyArena_Free(arena);
2090 return NULL;
2091 }
2092 st = PySymtable_Build(mod, filename, 0);
2093 PyArena_Free(arena);
2094 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002095}
2096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097/* Preferred access to parser is through AST. */
2098mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002099PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 mod_ty mod;
2103 PyCompilerFlags localflags;
2104 perrdetail err;
2105 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00002106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
2108 &_PyParser_Grammar, start, &err,
2109 &iflags);
2110 if (flags == NULL) {
2111 localflags.cf_flags = 0;
2112 flags = &localflags;
2113 }
2114 if (n) {
2115 flags->cf_flags |= iflags & PyCF_MASK;
2116 mod = PyAST_FromNode(n, flags, filename, arena);
2117 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 }
2119 else {
2120 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002121 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002123 err_free(&err);
2124 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125}
2126
2127mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00002128PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 int start, char *ps1,
2130 char *ps2, PyCompilerFlags *flags, int *errcode,
2131 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 mod_ty mod;
2134 PyCompilerFlags localflags;
2135 perrdetail err;
2136 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00002137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
2139 &_PyParser_Grammar,
2140 start, ps1, ps2, &err, &iflags);
2141 if (flags == NULL) {
2142 localflags.cf_flags = 0;
2143 flags = &localflags;
2144 }
2145 if (n) {
2146 flags->cf_flags |= iflags & PyCF_MASK;
2147 mod = PyAST_FromNode(n, flags, filename, arena);
2148 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 }
2150 else {
2151 err_input(&err);
2152 if (errcode)
2153 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02002154 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002156 err_free(&err);
2157 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158}
2159
Guido van Rossuma110aa61994-08-29 12:50:44 +00002160/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002161
Guido van Rossuma110aa61994-08-29 12:50:44 +00002162node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002163PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 perrdetail err;
2166 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
2167 &_PyParser_Grammar,
2168 start, NULL, NULL, &err, flags);
2169 if (n == NULL)
2170 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002171 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002174}
2175
Guido van Rossuma110aa61994-08-29 12:50:44 +00002176/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002177
Guido van Rossuma110aa61994-08-29 12:50:44 +00002178node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002179PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00002180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 perrdetail err;
2182 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
2183 start, &err, flags);
2184 if (n == NULL)
2185 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002186 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00002188}
2189
2190node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002191PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 perrdetail err;
2195 node *n = PyParser_ParseStringFlagsFilename(str, filename,
2196 &_PyParser_Grammar, start, &err, flags);
2197 if (n == NULL)
2198 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002199 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00002201}
2202
2203node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002204PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00002207}
2208
Guido van Rossum66ebd912003-04-17 16:02:26 +00002209/* May want to move a more generalized form of this to parsetok.c or
2210 even parser modules. */
2211
2212void
Victor Stinner7f2fee32011-04-05 00:39:01 +02002213PyParser_ClearError(perrdetail *err)
2214{
2215 err_free(err);
2216}
2217
2218void
Guido van Rossum66ebd912003-04-17 16:02:26 +00002219PyParser_SetError(perrdetail *err)
2220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00002222}
2223
Victor Stinner7f2fee32011-04-05 00:39:01 +02002224static void
2225err_free(perrdetail *err)
2226{
2227 Py_CLEAR(err->filename);
2228}
2229
Guido van Rossuma110aa61994-08-29 12:50:44 +00002230/* Set the error appropriate to the given input error code (see errcode.h) */
2231
2232static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002233err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00002234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 PyObject *v, *w, *errtype, *errtext;
2236 PyObject *msg_obj = NULL;
2237 char *msg = NULL;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 errtype = PyExc_SyntaxError;
2240 switch (err->error) {
2241 case E_ERROR:
2242 return;
2243 case E_SYNTAX:
2244 errtype = PyExc_IndentationError;
2245 if (err->expected == INDENT)
2246 msg = "expected an indented block";
2247 else if (err->token == INDENT)
2248 msg = "unexpected indent";
2249 else if (err->token == DEDENT)
2250 msg = "unexpected unindent";
2251 else {
2252 errtype = PyExc_SyntaxError;
2253 msg = "invalid syntax";
2254 }
2255 break;
2256 case E_TOKEN:
2257 msg = "invalid token";
2258 break;
2259 case E_EOFS:
2260 msg = "EOF while scanning triple-quoted string literal";
2261 break;
2262 case E_EOLS:
2263 msg = "EOL while scanning string literal";
2264 break;
2265 case E_INTR:
2266 if (!PyErr_Occurred())
2267 PyErr_SetNone(PyExc_KeyboardInterrupt);
2268 goto cleanup;
2269 case E_NOMEM:
2270 PyErr_NoMemory();
2271 goto cleanup;
2272 case E_EOF:
2273 msg = "unexpected EOF while parsing";
2274 break;
2275 case E_TABSPACE:
2276 errtype = PyExc_TabError;
2277 msg = "inconsistent use of tabs and spaces in indentation";
2278 break;
2279 case E_OVERFLOW:
2280 msg = "expression too long";
2281 break;
2282 case E_DEDENT:
2283 errtype = PyExc_IndentationError;
2284 msg = "unindent does not match any outer indentation level";
2285 break;
2286 case E_TOODEEP:
2287 errtype = PyExc_IndentationError;
2288 msg = "too many levels of indentation";
2289 break;
2290 case E_DECODE: {
2291 PyObject *type, *value, *tb;
2292 PyErr_Fetch(&type, &value, &tb);
2293 msg = "unknown decode error";
2294 if (value != NULL)
2295 msg_obj = PyObject_Str(value);
2296 Py_XDECREF(type);
2297 Py_XDECREF(value);
2298 Py_XDECREF(tb);
2299 break;
2300 }
2301 case E_LINECONT:
2302 msg = "unexpected character after line continuation character";
2303 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00002304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 case E_IDENTIFIER:
2306 msg = "invalid character in identifier";
2307 break;
Meador Ingefa21bf02012-01-19 01:08:41 -06002308 case E_BADSINGLE:
2309 msg = "multiple statements found while compiling a single statement";
2310 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 default:
2312 fprintf(stderr, "error=%d\n", err->error);
2313 msg = "unknown parsing error";
2314 break;
2315 }
2316 /* err->text may not be UTF-8 in case of decoding errors.
2317 Explicitly convert to an object. */
2318 if (!err->text) {
2319 errtext = Py_None;
2320 Py_INCREF(Py_None);
2321 } else {
2322 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2323 "replace");
2324 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002325 v = Py_BuildValue("(OiiN)", err->filename,
2326 err->lineno, err->offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 if (v != NULL) {
2328 if (msg_obj)
2329 w = Py_BuildValue("(OO)", msg_obj, v);
2330 else
2331 w = Py_BuildValue("(sO)", msg, v);
2332 } else
2333 w = NULL;
2334 Py_XDECREF(v);
2335 PyErr_SetObject(errtype, w);
2336 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002337cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 Py_XDECREF(msg_obj);
2339 if (err->text != NULL) {
2340 PyObject_FREE(err->text);
2341 err->text = NULL;
2342 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002343}
2344
2345/* Print fatal error message and abort */
2346
2347void
Tim Peters7c321a82002-07-09 02:57:01 +00002348Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002349{
Victor Stinner024e37a2011-03-31 01:31:06 +02002350 const int fd = fileno(stderr);
2351 PyThreadState *tstate;
2352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 fprintf(stderr, "Fatal Python error: %s\n", msg);
2354 fflush(stderr); /* it helps in Windows debug build */
2355 if (PyErr_Occurred()) {
Victor Stinner55a5c782010-06-08 21:00:13 +00002356 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 }
Victor Stinner024e37a2011-03-31 01:31:06 +02002358 else {
2359 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
2360 if (tstate != NULL) {
2361 fputc('\n', stderr);
2362 fflush(stderr);
Victor Stinner7bba62f2011-05-07 12:43:00 +02002363 _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
Victor Stinner024e37a2011-03-31 01:31:06 +02002364 }
Victor Stinnerd727e232011-04-01 12:13:55 +02002365 _PyFaulthandler_Fini();
Victor Stinner024e37a2011-03-31 01:31:06 +02002366 }
2367
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002368#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 {
2370 size_t len = strlen(msg);
2371 WCHAR* buffer;
2372 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 /* Convert the message to wchar_t. This uses a simple one-to-one
2375 conversion, assuming that the this error message actually uses ASCII
2376 only. If this ceases to be true, we will have to convert. */
2377 buffer = alloca( (len+1) * (sizeof *buffer));
2378 for( i=0; i<=len; ++i)
2379 buffer[i] = msg[i];
2380 OutputDebugStringW(L"Fatal Python error: ");
2381 OutputDebugStringW(buffer);
2382 OutputDebugStringW(L"\n");
2383 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002384#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002386#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002387#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002389}
2390
2391/* Clean up and exit */
2392
Guido van Rossuma110aa61994-08-29 12:50:44 +00002393#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002394#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002395#endif
2396
Collin Winter670e6922007-03-21 02:57:17 +00002397static void (*pyexitfunc)(void) = NULL;
2398/* For the atexit module. */
2399void _Py_PyAtExit(void (*func)(void))
2400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002402}
2403
2404static void
2405call_py_exitfuncs(void)
2406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 if (pyexitfunc == NULL)
2408 return;
Collin Winter670e6922007-03-21 02:57:17 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 (*pyexitfunc)();
2411 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002412}
2413
Antoine Pitrou011bd622009-10-20 21:52:47 +00002414/* Wait until threading._shutdown completes, provided
2415 the threading module was imported in the first place.
2416 The shutdown routine will wait until all non-daemon
2417 "threading" threads have completed. */
2418static void
2419wait_for_thread_shutdown(void)
2420{
2421#ifdef WITH_THREAD
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002422 _Py_IDENTIFIER(_shutdown);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 PyObject *result;
2424 PyThreadState *tstate = PyThreadState_GET();
2425 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2426 "threading");
2427 if (threading == NULL) {
2428 /* threading not imported */
2429 PyErr_Clear();
2430 return;
2431 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002432 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 if (result == NULL) {
2434 PyErr_WriteUnraisable(threading);
2435 }
2436 else {
2437 Py_DECREF(result);
2438 }
2439 Py_DECREF(threading);
Antoine Pitrou011bd622009-10-20 21:52:47 +00002440#endif
2441}
2442
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002443#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002444static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002445static int nexitfuncs = 0;
2446
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002447int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 if (nexitfuncs >= NEXITFUNCS)
2450 return -1;
2451 exitfuncs[nexitfuncs++] = func;
2452 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002453}
2454
Guido van Rossumcc283f51997-08-05 02:22:03 +00002455static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002456call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 while (nexitfuncs > 0)
2459 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 fflush(stdout);
2462 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002463}
2464
2465void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002466Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002471}
2472
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002473static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002474initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002475{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002476#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002478#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002479#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002481#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002482#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002484#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 PyOS_InitInterrupts(); /* May imply initsignal() */
Victor Stinnerd786ad52013-07-21 13:25:51 +02002486 if (PyErr_Occurred()) {
2487 Py_FatalError("Py_Initialize: can't import signal");
2488 }
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002489}
2490
Guido van Rossum7433b121997-02-14 19:45:36 +00002491
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002492/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2493 *
2494 * All of the code in this function must only use async-signal-safe functions,
2495 * listed at `man 7 signal` or
2496 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2497 */
2498void
2499_Py_RestoreSignals(void)
2500{
2501#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 PyOS_setsig(SIGPIPE, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002503#endif
2504#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 PyOS_setsig(SIGXFZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002506#endif
2507#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 PyOS_setsig(SIGXFSZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002509#endif
2510}
2511
2512
Guido van Rossum7433b121997-02-14 19:45:36 +00002513/*
2514 * The file descriptor fd is considered ``interactive'' if either
2515 * a) isatty(fd) is TRUE, or
2516 * b) the -i flag was given, and the filename associated with
2517 * the descriptor is NULL or "<stdin>" or "???".
2518 */
2519int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002520Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 if (isatty((int)fileno(fp)))
2523 return 1;
2524 if (!Py_InteractiveFlag)
2525 return 0;
2526 return (filename == NULL) ||
2527 (strcmp(filename, "<stdin>") == 0) ||
2528 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002529}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002530
2531
Tim Petersd08e3822003-04-17 15:24:21 +00002532#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002533#if defined(WIN32) && defined(_MSC_VER)
2534
2535/* Stack checking for Microsoft C */
2536
2537#include <malloc.h>
2538#include <excpt.h>
2539
Fred Drakee8de31c2000-08-31 05:38:39 +00002540/*
2541 * Return non-zero when we run out of memory on the stack; zero otherwise.
2542 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002543int
Fred Drake399739f2000-08-31 05:52:44 +00002544PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 __try {
2547 /* alloca throws a stack overflow exception if there's
2548 not enough space left on the stack */
2549 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2550 return 0;
2551 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2552 EXCEPTION_EXECUTE_HANDLER :
2553 EXCEPTION_CONTINUE_SEARCH) {
2554 int errcode = _resetstkoflw();
2555 if (errcode == 0)
2556 {
2557 Py_FatalError("Could not reset the stack!");
2558 }
2559 }
2560 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002561}
2562
2563#endif /* WIN32 && _MSC_VER */
2564
2565/* Alternate implementations can be added here... */
2566
2567#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002568
2569
2570/* Wrappers around sigaction() or signal(). */
2571
2572PyOS_sighandler_t
2573PyOS_getsig(int sig)
2574{
2575#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 struct sigaction context;
2577 if (sigaction(sig, NULL, &context) == -1)
2578 return SIG_ERR;
2579 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002580#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002582/* Special signal handling for the secure CRT in Visual Studio 2005 */
2583#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 switch (sig) {
2585 /* Only these signals are valid */
2586 case SIGINT:
2587 case SIGILL:
2588 case SIGFPE:
2589 case SIGSEGV:
2590 case SIGTERM:
2591 case SIGBREAK:
2592 case SIGABRT:
2593 break;
2594 /* Don't call signal() with other values or it will assert */
2595 default:
2596 return SIG_ERR;
2597 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002598#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 handler = signal(sig, SIG_IGN);
2600 if (handler != SIG_ERR)
2601 signal(sig, handler);
2602 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002603#endif
2604}
2605
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002606/*
2607 * All of the code in this function must only use async-signal-safe functions,
2608 * listed at `man 7 signal` or
2609 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2610 */
Guido van Rossum6f256182000-09-16 16:32:19 +00002611PyOS_sighandler_t
2612PyOS_setsig(int sig, PyOS_sighandler_t handler)
2613{
2614#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 /* Some code in Modules/signalmodule.c depends on sigaction() being
2616 * used here if HAVE_SIGACTION is defined. Fix that if this code
2617 * changes to invalidate that assumption.
2618 */
2619 struct sigaction context, ocontext;
2620 context.sa_handler = handler;
2621 sigemptyset(&context.sa_mask);
2622 context.sa_flags = 0;
2623 if (sigaction(sig, &context, &ocontext) == -1)
2624 return SIG_ERR;
2625 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002626#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 PyOS_sighandler_t oldhandler;
2628 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002629#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002631#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002633#endif
2634}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635
2636/* Deprecated C API functions still provided for binary compatiblity */
2637
2638#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002639PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643}
2644
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002645#undef PyParser_SimpleParseString
2646PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647PyParser_SimpleParseString(const char *str, int start)
2648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002651
2652#undef PyRun_AnyFile
2653PyAPI_FUNC(int)
2654PyRun_AnyFile(FILE *fp, const char *name)
2655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002657}
2658
2659#undef PyRun_AnyFileEx
2660PyAPI_FUNC(int)
2661PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002664}
2665
2666#undef PyRun_AnyFileFlags
2667PyAPI_FUNC(int)
2668PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002671}
2672
2673#undef PyRun_File
2674PyAPI_FUNC(PyObject *)
2675PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002678}
2679
2680#undef PyRun_FileEx
2681PyAPI_FUNC(PyObject *)
2682PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002685}
2686
2687#undef PyRun_FileFlags
2688PyAPI_FUNC(PyObject *)
2689PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002693}
2694
2695#undef PyRun_SimpleFile
2696PyAPI_FUNC(int)
2697PyRun_SimpleFile(FILE *f, const char *p)
2698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002700}
2701
2702#undef PyRun_SimpleFileEx
2703PyAPI_FUNC(int)
2704PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002707}
2708
2709
2710#undef PyRun_String
2711PyAPI_FUNC(PyObject *)
2712PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002715}
2716
2717#undef PyRun_SimpleString
2718PyAPI_FUNC(int)
2719PyRun_SimpleString(const char *s)
2720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002722}
2723
2724#undef Py_CompileString
2725PyAPI_FUNC(PyObject *)
2726Py_CompileString(const char *str, const char *p, int s)
2727{
Georg Brandl8334fd92010-12-04 10:26:46 +00002728 return Py_CompileStringExFlags(str, p, s, NULL, -1);
2729}
2730
2731#undef Py_CompileStringFlags
2732PyAPI_FUNC(PyObject *)
2733Py_CompileStringFlags(const char *str, const char *p, int s,
2734 PyCompilerFlags *flags)
2735{
2736 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002737}
2738
2739#undef PyRun_InteractiveOne
2740PyAPI_FUNC(int)
2741PyRun_InteractiveOne(FILE *f, const char *p)
2742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002744}
2745
2746#undef PyRun_InteractiveLoop
2747PyAPI_FUNC(int)
2748PyRun_InteractiveLoop(FILE *f, const char *p)
2749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002751}
2752
2753#ifdef __cplusplus
2754}
2755#endif