blob: 2757eba927b31468b449d362a6bfd6fa365eb3f9 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Guido van Rossumd8faa362007-04-27 19:54:29 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00008#include "grammar.h"
9#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000010#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000014#include "symtable.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015#include "ast.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000016#include "marshal.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000017#include "osdefs.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000018
Thomas Wouters0e3f5912006-08-11 14:57:12 +000019#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000020#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000021#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000022
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000023#ifdef MS_WINDOWS
Martin v. Löwis5c88d812009-01-02 20:47:48 +000024#include "malloc.h" /* for alloca */
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000025#endif
Martin v. Löwis5c88d812009-01-02 20:47:48 +000026
Martin v. Löwis73d538b2003-03-05 15:13:47 +000027#ifdef HAVE_LANGINFO_H
28#include <locale.h>
29#include <langinfo.h>
30#endif
31
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000032#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000033#undef BYTE
34#include "windows.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000035#define PATH_MAX MAXPATHLEN
Guido van Rossuma44823b1995-03-14 15:01:17 +000036#endif
37
Neal Norwitz4281cef2006-03-04 19:58:13 +000038#ifndef Py_REF_DEBUG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000040#else /* Py_REF_DEBUG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041#define PRINT_TOTAL_REFS() fprintf(stderr, \
42 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
43 _Py_GetRefTotal())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000044#endif
45
46#ifdef __cplusplus
47extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000048#endif
49
Martin v. Löwis790465f2008-04-05 20:41:37 +000050extern wchar_t *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000051
Guido van Rossum82598051997-03-05 00:20:32 +000052extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000053
Guido van Rossumb73cc041993-11-01 16:28:59 +000054/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000055static void initmain(void);
Victor Stinner793b5312011-04-27 00:24:21 +020056static int initfsencoding(PyInterpreterState *interp);
Tim Petersdbd9ba62000-07-09 03:09:57 +000057static void initsite(void);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000058static int initstdio(void);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000059static void flush_io(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000060static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000062static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000064static void err_input(perrdetail *);
Victor Stinner7f2fee32011-04-05 00:39:01 +020065static void err_free(perrdetail *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static void initsigs(void);
Collin Winter670e6922007-03-21 02:57:17 +000067static void call_py_exitfuncs(void);
Antoine Pitrou011bd622009-10-20 21:52:47 +000068static void wait_for_thread_shutdown(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000069static void call_ll_exitfuncs(void);
Victor Stinner3a50e702011-10-18 21:21:00 +020070extern int _PyUnicode_Init(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000071extern void _PyUnicode_Fini(void);
Guido van Rossumddefaf32007-01-14 03:31:43 +000072extern int _PyLong_Init(void);
73extern void PyLong_Fini(void);
Victor Stinner024e37a2011-03-31 01:31:06 +020074extern int _PyFaulthandler_Init(void);
75extern void _PyFaulthandler_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000076
Mark Hammond8d98d2c2003-04-19 15:41:53 +000077#ifdef WITH_THREAD
78extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
79extern void _PyGILState_Fini(void);
80#endif /* WITH_THREAD */
81
Guido van Rossum82598051997-03-05 00:20:32 +000082int Py_DebugFlag; /* Needed by parser.c */
83int Py_VerboseFlag; /* Needed by import.c */
Georg Brandl8aa7e992010-12-28 18:30:18 +000084int Py_QuietFlag; /* Needed by sysmodule.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000085int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl0b2489e2011-05-15 08:49:12 +020086int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000087int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +000088int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Christian Heimes790c8232008-01-07 21:14:23 +000089int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000090int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000091int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000092int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Christian Heimes8dc226f2008-05-06 23:45:46 +000093int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Antoine Pitrou05608432009-01-09 18:53:14 +000094int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
Georg Brandl2daf6ae2012-02-20 19:54:16 +010095int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000096
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +020097PyThreadState *_Py_Finalizing = NULL;
98
Christian Heimes33fe8092008-04-13 13:53:33 +000099/* PyModule_GetWarningsModule is no longer necessary as of 2.6
100since _warnings is builtin. This API should not be used. */
101PyObject *
102PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +0000103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +0000105}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000106
Guido van Rossum25ce5661997-08-02 03:10:38 +0000107static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000108
Thomas Wouters7e474022000-07-16 12:04:32 +0000109/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000110
111int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000112Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 return initialized;
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000115}
116
Guido van Rossum25ce5661997-08-02 03:10:38 +0000117/* Global initializations. Can be undone by Py_Finalize(). Don't
118 call this twice without an intervening Py_Finalize() call. When
119 initializations fail, a fatal error is issued and the function does
120 not return. On return, the first thread and interpreter state have
121 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000122
Guido van Rossum25ce5661997-08-02 03:10:38 +0000123 Locking: you must hold the interpreter lock while calling this.
124 (If the lock has not yet been initialized, that's equivalent to
125 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000126
Guido van Rossum25ce5661997-08-02 03:10:38 +0000127*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000128
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000129static int
130add_flag(int flag, const char *envs)
131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 int env = atoi(envs);
133 if (flag < env)
134 flag = env;
135 if (flag < 1)
136 flag = 1;
137 return flag;
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000138}
139
Christian Heimes5833a2f2008-10-30 21:40:04 +0000140static char*
Victor Stinner94908bb2010-08-18 21:23:25 +0000141get_codec_name(const char *encoding)
Christian Heimes5833a2f2008-10-30 21:40:04 +0000142{
Victor Stinner94908bb2010-08-18 21:23:25 +0000143 char *name_utf8, *name_str;
Victor Stinner386fe712010-05-19 00:34:15 +0000144 PyObject *codec, *name = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200145 _Py_IDENTIFIER(name);
Christian Heimes5833a2f2008-10-30 21:40:04 +0000146
Victor Stinner94908bb2010-08-18 21:23:25 +0000147 codec = _PyCodec_Lookup(encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 if (!codec)
149 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000150
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200151 name = _PyObject_GetAttrId(codec, &PyId_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 Py_CLEAR(codec);
153 if (!name)
154 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000155
Victor Stinner94908bb2010-08-18 21:23:25 +0000156 name_utf8 = _PyUnicode_AsString(name);
Victor Stinner4ca28092011-03-20 23:09:03 +0100157 if (name_utf8 == NULL)
Victor Stinner386fe712010-05-19 00:34:15 +0000158 goto error;
Victor Stinner94908bb2010-08-18 21:23:25 +0000159 name_str = strdup(name_utf8);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 Py_DECREF(name);
Victor Stinner94908bb2010-08-18 21:23:25 +0000161 if (name_str == NULL) {
162 PyErr_NoMemory();
163 return NULL;
164 }
165 return name_str;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000166
167error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 Py_XDECREF(codec);
Victor Stinner386fe712010-05-19 00:34:15 +0000169 Py_XDECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000171}
Victor Stinner94908bb2010-08-18 21:23:25 +0000172
Victor Stinner94908bb2010-08-18 21:23:25 +0000173static char*
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200174get_locale_encoding(void)
Victor Stinner94908bb2010-08-18 21:23:25 +0000175{
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200176#ifdef MS_WINDOWS
177 char codepage[100];
178 PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
179 return get_codec_name(codepage);
180#elif defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94908bb2010-08-18 21:23:25 +0000181 char* codeset = nl_langinfo(CODESET);
182 if (!codeset || codeset[0] == '\0') {
183 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
184 return NULL;
185 }
186 return get_codec_name(codeset);
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200187#else
188 PyErr_SetNone(PyExc_NotImplementedError);
189 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000190#endif
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200191}
Christian Heimes5833a2f2008-10-30 21:40:04 +0000192
Brett Cannonfd074152012-04-14 14:10:13 -0400193static void
194import_init(PyInterpreterState *interp, PyObject *sysmod)
195{
196 PyObject *importlib;
197 PyObject *impmod;
198 PyObject *sys_modules;
199 PyObject *value;
200
201 /* Import _importlib through its frozen version, _frozen_importlib. */
202 /* XXX(bcannon): The file path for _frozen_importlib is completely off
203 */
204 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
205 Py_FatalError("Py_Initialize: can't import _frozen_importlib");
206 }
207 else if (Py_VerboseFlag) {
208 PySys_FormatStderr("import _frozen_importlib # frozen\n");
209 }
210 importlib = PyImport_AddModule("_frozen_importlib");
211 if (importlib == NULL) {
212 Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from "
213 "sys.modules");
214 }
215 interp->importlib = importlib;
216 Py_INCREF(interp->importlib);
217
218 /* Install _importlib as __import__ */
219 impmod = PyInit_imp();
220 if (impmod == NULL) {
221 Py_FatalError("Py_Initialize: can't import imp");
222 }
223 else if (Py_VerboseFlag) {
224 PySys_FormatStderr("import imp # builtin\n");
225 }
226 sys_modules = PyImport_GetModuleDict();
227 if (Py_VerboseFlag) {
228 PySys_FormatStderr("import sys # builtin\n");
229 }
230 if (PyDict_SetItemString(sys_modules, "imp", impmod) < 0) {
231 Py_FatalError("Py_Initialize: can't save imp to sys.modules");
232 }
233
234 value = PyObject_CallMethod(importlib, "_setup", "OO", sysmod, impmod);
235 if (value == NULL) {
236 PyErr_Print();
237 Py_FatalError("Py_Initialize: importlib install failed");
238 }
239 Py_DECREF(value);
240
241 _PyImportZip_Init();
242}
243
244
Guido van Rossuma027efa1997-05-05 20:56:21 +0000245void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000246Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 PyInterpreterState *interp;
249 PyThreadState *tstate;
250 PyObject *bimod, *sysmod, *pstderr;
251 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 if (initialized)
255 return;
256 initialized = 1;
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200257 _Py_Finalizing = NULL;
Tim Petersd08e3822003-04-17 15:24:21 +0000258
Andrew M. Kuchlingb2ceb3a2010-02-22 23:26:10 +0000259#if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 /* Set up the LC_CTYPE locale, so we can obtain
261 the locale's charset without having to switch
262 locales. */
263 setlocale(LC_CTYPE, "");
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000264#endif
265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
267 Py_DebugFlag = add_flag(Py_DebugFlag, p);
268 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
269 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
270 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
271 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
272 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
273 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100274 /* The variable is only tested for existence here; _PyRandom_Init will
275 check its value further. */
276 if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
277 Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
278
279 _PyRandom_Init();
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 interp = PyInterpreterState_New();
282 if (interp == NULL)
283 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 tstate = PyThreadState_New(interp);
286 if (tstate == NULL)
287 Py_FatalError("Py_Initialize: can't make first thread");
288 (void) PyThreadState_Swap(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000289
Victor Stinner6961bd62010-08-17 22:26:51 +0000290#ifdef WITH_THREAD
Antoine Pitroub0b384b2010-09-20 20:13:48 +0000291 /* We can't call _PyEval_FiniThreads() in Py_Finalize because
292 destroying the GIL might fail when it is being referenced from
293 another running thread (see issue #9901).
294 Instead we destroy the previously created GIL here, which ensures
295 that we can call Py_Initialize / Py_Finalize multiple times. */
296 _PyEval_FiniThreads();
297
298 /* Auto-thread-state API */
Victor Stinner6961bd62010-08-17 22:26:51 +0000299 _PyGILState_Init(interp, tstate);
300#endif /* WITH_THREAD */
301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 if (!_PyFrame_Init())
305 Py_FatalError("Py_Initialize: can't init frames");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 if (!_PyLong_Init())
308 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (!PyByteArray_Init())
311 Py_FatalError("Py_Initialize: can't init bytearray");
Neal Norwitz6968b052007-02-27 19:02:19 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 _PyFloat_Init();
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 interp->modules = PyDict_New();
316 if (interp->modules == NULL)
317 Py_FatalError("Py_Initialize: can't make modules dictionary");
318 interp->modules_reloading = PyDict_New();
319 if (interp->modules_reloading == NULL)
320 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 /* Init Unicode implementation; relies on the codec registry */
Victor Stinner3a50e702011-10-18 21:21:00 +0200323 if (_PyUnicode_Init() < 0)
324 Py_FatalError("Py_Initialize: can't initialize unicode");
Guido van Rossumc94044c2000-03-10 23:03:54 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 bimod = _PyBuiltin_Init();
327 if (bimod == NULL)
328 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Victor Stinner49d3f252010-10-17 01:24:53 +0000329 _PyImport_FixupBuiltin(bimod, "builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 interp->builtins = PyModule_GetDict(bimod);
331 if (interp->builtins == NULL)
332 Py_FatalError("Py_Initialize: can't initialize builtins dict");
333 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 /* initialize builtin exceptions */
Brett Cannonfd074152012-04-14 14:10:13 -0400336 _PyExc_Init(bimod);
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 sysmod = _PySys_Init();
339 if (sysmod == NULL)
340 Py_FatalError("Py_Initialize: can't initialize sys");
341 interp->sysdict = PyModule_GetDict(sysmod);
342 if (interp->sysdict == NULL)
343 Py_FatalError("Py_Initialize: can't initialize sys dict");
344 Py_INCREF(interp->sysdict);
Victor Stinner49d3f252010-10-17 01:24:53 +0000345 _PyImport_FixupBuiltin(sysmod, "sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 PySys_SetPath(Py_GetPath());
347 PyDict_SetItemString(interp->sysdict, "modules",
348 interp->modules);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 /* Set up a preliminary stderr printer until we have enough
351 infrastructure for the io module in place. */
352 pstderr = PyFile_NewStdPrinter(fileno(stderr));
353 if (pstderr == NULL)
354 Py_FatalError("Py_Initialize: can't set preliminary stderr");
355 PySys_SetObject("stderr", pstderr);
356 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000357 Py_DECREF(pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 _PyImport_Init();
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 _PyImportHooks_Init();
Just van Rossum52e14d62002-12-30 22:08:05 +0000362
Victor Stinner024e37a2011-03-31 01:31:06 +0200363 /* initialize the faulthandler module */
364 if (_PyFaulthandler_Init())
365 Py_FatalError("Py_Initialize: can't initialize faulthandler");
366
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000367 /* Initialize _warnings. */
368 _PyWarnings_Init();
369
Brett Cannonfd074152012-04-14 14:10:13 -0400370 import_init(interp, sysmod);
371
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000372 _PyTime_Init();
373
Victor Stinner793b5312011-04-27 00:24:21 +0200374 if (initfsencoding(interp) < 0)
375 Py_FatalError("Py_Initialize: unable to load the file system codec");
Martin v. Löwis011e8422009-05-05 04:43:17 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (install_sigs)
378 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 initmain(); /* Module __main__ */
381 if (initstdio() < 0)
382 Py_FatalError(
383 "Py_Initialize: can't initialize sys standard streams");
384
Antoine Pitroucf9f9802010-11-10 13:55:25 +0000385 /* Initialize warnings. */
386 if (PySys_HasWarnOptions()) {
387 PyObject *warnings_module = PyImport_ImportModule("warnings");
388 if (warnings_module == NULL) {
389 fprintf(stderr, "'import warnings' failed; traceback:\n");
390 PyErr_Print();
391 }
392 Py_XDECREF(warnings_module);
393 }
394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if (!Py_NoSiteFlag)
396 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000397}
398
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000399void
400Py_Initialize(void)
401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 Py_InitializeEx(1);
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000403}
404
405
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000406#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000407extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000408#endif
409
Guido van Rossume8432ac2007-07-09 15:04:50 +0000410/* Flush stdout and stderr */
411
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100412static int
413file_is_closed(PyObject *fobj)
414{
415 int r;
416 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
417 if (tmp == NULL) {
418 PyErr_Clear();
419 return 0;
420 }
421 r = PyObject_IsTrue(tmp);
422 Py_DECREF(tmp);
423 if (r < 0)
424 PyErr_Clear();
425 return r > 0;
426}
427
Neal Norwitz2bad9702007-08-27 06:19:22 +0000428static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000429flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 PyObject *fout = PySys_GetObject("stdout");
432 PyObject *ferr = PySys_GetObject("stderr");
433 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200434 _Py_IDENTIFIER(flush);
Guido van Rossume8432ac2007-07-09 15:04:50 +0000435
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100436 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200437 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (tmp == NULL)
Antoine Pitroubddc9fe2010-08-08 20:46:42 +0000439 PyErr_WriteUnraisable(fout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 else
441 Py_DECREF(tmp);
442 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000443
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100444 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200445 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (tmp == NULL)
447 PyErr_Clear();
448 else
449 Py_DECREF(tmp);
450 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000451}
452
Guido van Rossum25ce5661997-08-02 03:10:38 +0000453/* Undo the effect of Py_Initialize().
454
455 Beware: if multiple interpreter and/or thread states exist, these
456 are not wiped out; only the current thread and interpreter state
457 are deleted. But since everything else is deleted, those other
458 interpreter and thread states should no longer be used.
459
460 (XXX We should do better, e.g. wipe out all interpreters and
461 threads.)
462
463 Locking: as above.
464
465*/
466
467void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000468Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyInterpreterState *interp;
471 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (!initialized)
474 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 wait_for_thread_shutdown();
Antoine Pitrou011bd622009-10-20 21:52:47 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 /* The interpreter is still entirely intact at this point, and the
479 * exit funcs may be relying on that. In particular, if some thread
480 * or exit func is still waiting to do an import, the import machinery
481 * expects Py_IsInitialized() to return true. So don't say the
482 * interpreter is uninitialized until after the exit funcs have run.
483 * Note that Threading.py uses an exit func to do a join on all the
484 * threads created thru it, so this also protects pending imports in
485 * the threads created via Threading.
486 */
487 call_py_exitfuncs();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* Get current thread state and interpreter pointer */
490 tstate = PyThreadState_GET();
491 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000492
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200493 /* Remaining threads (e.g. daemon threads) will automatically exit
494 after taking the GIL (in PyEval_RestoreThread()). */
495 _Py_Finalizing = tstate;
496 initialized = 0;
497
498 /* Flush stdout+stderr */
499 flush_std_files();
500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 /* Disable signal handling */
502 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 /* Clear type lookup cache */
505 PyType_ClearCache();
Christian Heimes26855632008-01-27 23:50:43 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 /* Collect garbage. This may call finalizers; it's nice to call these
508 * before all modules are destroyed.
509 * XXX If a __del__ or weakref callback is triggered here, and tries to
510 * XXX import a module, bad things can happen, because Python no
511 * XXX longer believes it's initialized.
512 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
513 * XXX is easy to provoke that way. I've also seen, e.g.,
514 * XXX Exception exceptions.ImportError: 'No module named sha'
515 * XXX in <function callback at 0x008F5718> ignored
516 * XXX but I'm unclear on exactly how that one happens. In any case,
517 * XXX I haven't seen a real-life report of either of these.
518 */
519 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 /* With COUNT_ALLOCS, it helps to run GC multiple times:
522 each collection might release some types from the type
523 list, so they become garbage. */
524 while (PyGC_Collect() > 0)
525 /* nothing */;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526#endif
Antoine Pitrou696e0352010-08-08 22:18:46 +0000527 /* We run this while most interpreter state is still alive, so that
528 debug information can be printed out */
529 _PyGC_Fini();
Guido van Rossume13ddc92003-04-17 17:29:22 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 /* Destroy all modules */
532 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 /* Flush stdout+stderr (again, in case more was printed) */
535 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 /* Collect final garbage. This disposes of cycles created by
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100538 * class definitions, for example.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 * XXX This is disabled because it caused too many problems. If
540 * XXX a __del__ or weakref callback triggers here, Python code has
541 * XXX a hard time running, because even the sys module has been
542 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
543 * XXX One symptom is a sequence of information-free messages
544 * XXX coming from threads (if a __del__ or callback is invoked,
545 * XXX other threads can execute too, and any exception they encounter
546 * XXX triggers a comedy of errors as subsystem after subsystem
547 * XXX fails to find what it *expects* to find in sys to help report
548 * XXX the exception and consequent unexpected failures). I've also
549 * XXX seen segfaults then, after adding print statements to the
550 * XXX Python code getting called.
551 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000552#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000554#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
557 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000558
Victor Stinner024e37a2011-03-31 01:31:06 +0200559 /* unload faulthandler module */
560 _PyFaulthandler_Fini();
561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000563#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000565#endif
566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000568
Tim Peters9cf25ce2003-04-17 15:21:01 +0000569#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 /* Display all objects still alive -- this can invoke arbitrary
571 * __repr__ overrides, so requires a mostly-intact interpreter.
572 * Alas, a lot of stuff may still be alive now that will be cleaned
573 * up later.
574 */
575 if (Py_GETENV("PYTHONDUMPREFS"))
576 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000577#endif /* Py_TRACE_REFS */
578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 /* Clear interpreter state */
580 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 /* Now we decref the exception classes. After this point nothing
583 can raise an exception. That's okay, because each Fini() method
584 below has been checked to make sure no exceptions are ever
585 raised.
586 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 /* Cleanup auto-thread-state */
Christian Heimes7d2ff882007-11-30 14:35:04 +0000591#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 _PyGILState_Fini();
Christian Heimes7d2ff882007-11-30 14:35:04 +0000593#endif /* WITH_THREAD */
594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 /* Delete current thread */
596 PyThreadState_Swap(NULL);
597 PyInterpreterState_Delete(interp);
Barry Warsawf242aa02000-05-25 23:09:49 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 /* Sundry finalizers */
600 PyMethod_Fini();
601 PyFrame_Fini();
602 PyCFunction_Fini();
603 PyTuple_Fini();
604 PyList_Fini();
605 PySet_Fini();
606 PyBytes_Fini();
607 PyByteArray_Fini();
608 PyLong_Fini();
609 PyFloat_Fini();
610 PyDict_Fini();
Antoine Pitrouf34a0cd2011-11-18 20:14:34 +0100611 PySlice_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 /* Cleanup Unicode implementation */
614 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 /* reset file system default encoding */
Victor Stinnerb744ba12010-05-15 12:27:16 +0000617 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 free((char*)Py_FileSystemDefaultEncoding);
619 Py_FileSystemDefaultEncoding = NULL;
620 }
Christian Heimesc8967002007-11-30 10:18:26 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 /* XXX Still allocated:
623 - various static ad-hoc pointers to interned strings
624 - int and float free list blocks
625 - whatever various modules and libraries allocate
626 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000629
Tim Peters269b2a62003-04-17 19:52:29 +0000630#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 /* Display addresses (& refcnts) of all objects still alive.
632 * An address can be used to find the repr of the object, printed
633 * above by _Py_PrintReferences.
634 */
635 if (Py_GETENV("PYTHONDUMPREFS"))
636 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000637#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000638#ifdef PYMALLOC_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 if (Py_GETENV("PYTHONMALLOCSTATS"))
640 _PyObject_DebugMallocStats();
Tim Peters0e871182002-04-13 08:29:14 +0000641#endif
642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000644}
645
646/* Create and initialize a new interpreter and thread, and return the
647 new thread. This requires that Py_Initialize() has been called
648 first.
649
650 Unsuccessful initialization yields a NULL pointer. Note that *no*
651 exception information is available even in this case -- the
652 exception information is held in the thread, and there is no
653 thread.
654
655 Locking: as above.
656
657*/
658
659PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000660Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 PyInterpreterState *interp;
663 PyThreadState *tstate, *save_tstate;
664 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 if (!initialized)
667 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 interp = PyInterpreterState_New();
670 if (interp == NULL)
671 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 tstate = PyThreadState_New(interp);
674 if (tstate == NULL) {
675 PyInterpreterState_Delete(interp);
676 return NULL;
677 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 interp->modules = PyDict_New();
684 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000685
Victor Stinner49d3f252010-10-17 01:24:53 +0000686 bimod = _PyImport_FindBuiltin("builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 if (bimod != NULL) {
688 interp->builtins = PyModule_GetDict(bimod);
689 if (interp->builtins == NULL)
690 goto handle_error;
691 Py_INCREF(interp->builtins);
692 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 /* initialize builtin exceptions */
Brett Cannonfd074152012-04-14 14:10:13 -0400695 _PyExc_Init(bimod);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000696
Victor Stinner49d3f252010-10-17 01:24:53 +0000697 sysmod = _PyImport_FindBuiltin("sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 if (bimod != NULL && sysmod != NULL) {
699 PyObject *pstderr;
Brett Cannonfd074152012-04-14 14:10:13 -0400700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 interp->sysdict = PyModule_GetDict(sysmod);
702 if (interp->sysdict == NULL)
703 goto handle_error;
704 Py_INCREF(interp->sysdict);
705 PySys_SetPath(Py_GetPath());
706 PyDict_SetItemString(interp->sysdict, "modules",
707 interp->modules);
708 /* Set up a preliminary stderr printer until we have enough
709 infrastructure for the io module in place. */
710 pstderr = PyFile_NewStdPrinter(fileno(stderr));
711 if (pstderr == NULL)
712 Py_FatalError("Py_Initialize: can't set preliminary stderr");
713 PySys_SetObject("stderr", pstderr);
714 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000715 Py_DECREF(pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 _PyImportHooks_Init();
Victor Stinner793b5312011-04-27 00:24:21 +0200718
Brett Cannonfd074152012-04-14 14:10:13 -0400719 import_init(interp, sysmod);
720
Victor Stinner793b5312011-04-27 00:24:21 +0200721 if (initfsencoding(interp) < 0)
722 goto handle_error;
723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 if (initstdio() < 0)
725 Py_FatalError(
726 "Py_Initialize: can't initialize sys standard streams");
727 initmain();
728 if (!Py_NoSiteFlag)
729 initsite();
730 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 if (!PyErr_Occurred())
733 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000734
Thomas Wouters89f507f2006-12-13 04:49:30 +0000735handle_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000737
Victor Stinnerc40a3502011-04-27 00:20:27 +0200738 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 PyThreadState_Clear(tstate);
740 PyThreadState_Swap(save_tstate);
741 PyThreadState_Delete(tstate);
742 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000745}
746
747/* Delete an interpreter and its last thread. This requires that the
748 given thread state is current, that the thread has no remaining
749 frames, and that it is its interpreter's only remaining thread.
750 It is a fatal error to violate these constraints.
751
752 (Py_Finalize() doesn't have these constraints -- it zaps
753 everything, regardless.)
754
755 Locking: as above.
756
757*/
758
759void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000760Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 if (tstate != PyThreadState_GET())
765 Py_FatalError("Py_EndInterpreter: thread is not current");
766 if (tstate->frame != NULL)
767 Py_FatalError("Py_EndInterpreter: thread still has a frame");
768 if (tstate != interp->tstate_head || tstate->next != NULL)
769 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 PyImport_Cleanup();
772 PyInterpreterState_Clear(interp);
773 PyThreadState_Swap(NULL);
774 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000775}
776
Martin v. Löwis790465f2008-04-05 20:41:37 +0000777static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000778
779void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000780Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 if (pn && *pn)
783 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000784}
785
Martin v. Löwis790465f2008-04-05 20:41:37 +0000786wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000787Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000790}
791
Martin v. Löwis790465f2008-04-05 20:41:37 +0000792static wchar_t *default_home = NULL;
793static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000794
795void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000796Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000799}
800
Martin v. Löwis790465f2008-04-05 20:41:37 +0000801wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000802Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 wchar_t *home = default_home;
805 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
806 char* chome = Py_GETENV("PYTHONHOME");
807 if (chome) {
808 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
809 if (r != (size_t)-1 && r <= PATH_MAX)
810 home = env_home;
811 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 }
814 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000815}
816
Guido van Rossum6135a871995-01-09 17:53:26 +0000817/* Create __main__ module */
818
819static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000820initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 PyObject *m, *d;
823 m = PyImport_AddModule("__main__");
824 if (m == NULL)
825 Py_FatalError("can't create __main__ module");
826 d = PyModule_GetDict(m);
827 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
828 PyObject *bimod = PyImport_ImportModule("builtins");
829 if (bimod == NULL ||
830 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
831 Py_FatalError("can't add __builtins__ to __main__");
832 Py_DECREF(bimod);
833 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000834}
835
Victor Stinner793b5312011-04-27 00:24:21 +0200836static int
837initfsencoding(PyInterpreterState *interp)
Victor Stinnerb744ba12010-05-15 12:27:16 +0000838{
839 PyObject *codec;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000840
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200841 if (Py_FileSystemDefaultEncoding == NULL)
842 {
843 Py_FileSystemDefaultEncoding = get_locale_encoding();
844 if (Py_FileSystemDefaultEncoding == NULL)
Victor Stinnere4743092010-10-19 00:05:51 +0000845 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
Victor Stinnerb744ba12010-05-15 12:27:16 +0000846
Victor Stinnere4743092010-10-19 00:05:51 +0000847 Py_HasFileSystemDefaultEncoding = 0;
Victor Stinner793b5312011-04-27 00:24:21 +0200848 interp->fscodec_initialized = 1;
849 return 0;
Victor Stinner7f84ab52010-06-11 00:36:33 +0000850 }
Victor Stinnerb744ba12010-05-15 12:27:16 +0000851
852 /* the encoding is mbcs, utf-8 or ascii */
853 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
854 if (!codec) {
855 /* Such error can only occurs in critical situations: no more
856 * memory, import a module of the standard library failed,
857 * etc. */
Victor Stinner793b5312011-04-27 00:24:21 +0200858 return -1;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000859 }
Victor Stinner793b5312011-04-27 00:24:21 +0200860 Py_DECREF(codec);
861 interp->fscodec_initialized = 1;
862 return 0;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000863}
864
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000865/* Import the site module (not into __main__ though) */
866
867static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000868initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 PyObject *m;
871 m = PyImport_ImportModule("site");
872 if (m == NULL) {
873 PyErr_Print();
874 Py_Finalize();
875 exit(1);
876 }
877 else {
878 Py_DECREF(m);
879 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000880}
881
Antoine Pitrou05608432009-01-09 18:53:14 +0000882static PyObject*
883create_stdio(PyObject* io,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 int fd, int write_mode, char* name,
885 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +0000886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
888 const char* mode;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000889 const char* newline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 PyObject *line_buffering;
891 int buffering, isatty;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200892 _Py_IDENTIFIER(open);
893 _Py_IDENTIFIER(isatty);
894 _Py_IDENTIFIER(TextIOWrapper);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200895 _Py_IDENTIFIER(name);
896 _Py_IDENTIFIER(mode);
Antoine Pitrou05608432009-01-09 18:53:14 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 /* stdin is always opened in buffered mode, first because it shouldn't
899 make a difference in common use cases, second because TextIOWrapper
900 depends on the presence of a read1() method which only exists on
901 buffered streams.
902 */
903 if (Py_UnbufferedStdioFlag && write_mode)
904 buffering = 0;
905 else
906 buffering = -1;
907 if (write_mode)
908 mode = "wb";
909 else
910 mode = "rb";
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200911 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
912 fd, mode, buffering,
913 Py_None, Py_None, Py_None, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 if (buf == NULL)
915 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 if (buffering) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200918 _Py_IDENTIFIER(raw);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200919 raw = _PyObject_GetAttrId(buf, &PyId_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (raw == NULL)
921 goto error;
922 }
923 else {
924 raw = buf;
925 Py_INCREF(raw);
926 }
Antoine Pitrou05608432009-01-09 18:53:14 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 text = PyUnicode_FromString(name);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200929 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 goto error;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200931 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 if (res == NULL)
933 goto error;
934 isatty = PyObject_IsTrue(res);
935 Py_DECREF(res);
936 if (isatty == -1)
937 goto error;
938 if (isatty || Py_UnbufferedStdioFlag)
939 line_buffering = Py_True;
940 else
941 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 Py_CLEAR(raw);
944 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +0000945
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000946 newline = "\n";
947#ifdef MS_WINDOWS
948 if (!write_mode) {
949 /* translate \r\n to \n for sys.stdin on Windows */
950 newline = NULL;
951 }
952#endif
953
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200954 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
955 buf, encoding, errors,
956 newline, line_buffering);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 Py_CLEAR(buf);
958 if (stream == NULL)
959 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 if (write_mode)
962 mode = "w";
963 else
964 mode = "r";
965 text = PyUnicode_FromString(mode);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200966 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 goto error;
968 Py_CLEAR(text);
969 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +0000970
971error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 Py_XDECREF(buf);
973 Py_XDECREF(stream);
974 Py_XDECREF(text);
975 Py_XDECREF(raw);
976 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +0000977}
978
Antoine Pitrou11942a52011-11-28 19:08:36 +0100979static int
980is_valid_fd(int fd)
981{
982 int dummy_fd;
983 if (fd < 0 || !_PyVerify_fd(fd))
984 return 0;
985 dummy_fd = dup(fd);
986 if (dummy_fd < 0)
987 return 0;
988 close(dummy_fd);
989 return 1;
990}
991
Georg Brandl1a3284e2007-12-02 09:40:06 +0000992/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000993static int
994initstdio(void)
995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 PyObject *iomod = NULL, *wrapper;
997 PyObject *bimod = NULL;
998 PyObject *m;
999 PyObject *std = NULL;
1000 int status = 0, fd;
1001 PyObject * encoding_attr;
1002 char *encoding = NULL, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 /* Hack to avoid a nasty recursion issue when Python is invoked
1005 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
1006 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
1007 goto error;
1008 }
1009 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
1012 goto error;
1013 }
1014 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 if (!(bimod = PyImport_ImportModule("builtins"))) {
1017 goto error;
1018 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 if (!(iomod = PyImport_ImportModule("io"))) {
1021 goto error;
1022 }
1023 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
1024 goto error;
1025 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 /* Set builtins.open */
1028 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
Antoine Pitrou5a96b522010-11-20 19:50:57 +00001029 Py_DECREF(wrapper);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 goto error;
1031 }
Antoine Pitrou5a96b522010-11-20 19:50:57 +00001032 Py_DECREF(wrapper);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 encoding = Py_GETENV("PYTHONIOENCODING");
1035 errors = NULL;
1036 if (encoding) {
1037 encoding = strdup(encoding);
1038 errors = strchr(encoding, ':');
1039 if (errors) {
1040 *errors = '\0';
1041 errors++;
1042 }
1043 }
Martin v. Löwis0f599892008-06-02 11:13:03 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 /* Set sys.stdin */
1046 fd = fileno(stdin);
1047 /* Under some conditions stdin, stdout and stderr may not be connected
1048 * and fileno() may point to an invalid file descriptor. For example
1049 * GUI apps don't have valid standard streams by default.
1050 */
Antoine Pitrou11942a52011-11-28 19:08:36 +01001051 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 std = Py_None;
1053 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 }
1055 else {
1056 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
1057 if (std == NULL)
1058 goto error;
1059 } /* if (fd < 0) */
1060 PySys_SetObject("__stdin__", std);
1061 PySys_SetObject("stdin", std);
1062 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 /* Set sys.stdout */
1065 fd = fileno(stdout);
Antoine Pitrou11942a52011-11-28 19:08:36 +01001066 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 std = Py_None;
1068 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 }
1070 else {
1071 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
1072 if (std == NULL)
1073 goto error;
1074 } /* if (fd < 0) */
1075 PySys_SetObject("__stdout__", std);
1076 PySys_SetObject("stdout", std);
1077 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001078
Guido van Rossum98297ee2007-11-06 21:34:58 +00001079#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 /* Set sys.stderr, replaces the preliminary stderr */
1081 fd = fileno(stderr);
Antoine Pitrou11942a52011-11-28 19:08:36 +01001082 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 std = Py_None;
1084 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 }
1086 else {
1087 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1088 if (std == NULL)
1089 goto error;
1090 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 /* Same as hack above, pre-import stderr's codec to avoid recursion
1093 when import.c tries to write to stderr in verbose mode. */
1094 encoding_attr = PyObject_GetAttrString(std, "encoding");
1095 if (encoding_attr != NULL) {
1096 const char * encoding;
1097 encoding = _PyUnicode_AsString(encoding_attr);
1098 if (encoding != NULL) {
Antoine Pitrou2fabfac2012-01-18 15:14:46 +01001099 PyObject *codec_info = _PyCodec_Lookup(encoding);
1100 Py_XDECREF(codec_info);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 }
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +00001102 Py_DECREF(encoding_attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 }
1104 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 PySys_SetObject("__stderr__", std);
1107 PySys_SetObject("stderr", std);
1108 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001109#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001112 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 status = -1;
1114 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (encoding)
1117 free(encoding);
1118 Py_XDECREF(bimod);
1119 Py_XDECREF(iomod);
1120 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001121}
1122
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001123/* Parse input from a file and execute it */
1124
1125int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001126PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 if (filename == NULL)
1130 filename = "???";
1131 if (Py_FdIsInteractive(fp, filename)) {
1132 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
1133 if (closeit)
1134 fclose(fp);
1135 return err;
1136 }
1137 else
1138 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001139}
1140
1141int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001142PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 PyObject *v;
1145 int ret;
1146 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 if (flags == NULL) {
1149 flags = &local_flags;
1150 local_flags.cf_flags = 0;
1151 }
1152 v = PySys_GetObject("ps1");
1153 if (v == NULL) {
1154 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
1155 Py_XDECREF(v);
1156 }
1157 v = PySys_GetObject("ps2");
1158 if (v == NULL) {
1159 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1160 Py_XDECREF(v);
1161 }
1162 for (;;) {
1163 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
1164 PRINT_TOTAL_REFS();
1165 if (ret == E_EOF)
1166 return 0;
1167 /*
1168 if (ret == E_NOMEM)
1169 return -1;
1170 */
1171 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001172}
1173
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001174/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001175static int PARSER_FLAGS(PyCompilerFlags *flags)
1176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 int parser_flags = 0;
1178 if (!flags)
1179 return 0;
1180 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1181 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1182 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1183 parser_flags |= PyPARSE_IGNORE_COOKIE;
1184 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1185 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1186 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001187}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001188
Thomas Wouters89f507f2006-12-13 04:49:30 +00001189#if 0
1190/* Keep an example of flags with future keyword support. */
1191#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1193 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1194 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1195 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001196#endif
1197
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001198int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001199PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 PyObject *m, *d, *v, *w, *oenc = NULL;
1202 mod_ty mod;
1203 PyArena *arena;
1204 char *ps1 = "", *ps2 = "", *enc = NULL;
1205 int errcode = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001206 _Py_IDENTIFIER(encoding);
Tim Petersfe2127d2001-07-16 05:37:24 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 if (fp == stdin) {
1209 /* Fetch encoding from sys.stdin */
1210 v = PySys_GetObject("stdin");
1211 if (v == NULL || v == Py_None)
1212 return -1;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001213 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (!oenc)
1215 return -1;
1216 enc = _PyUnicode_AsString(oenc);
Victor Stinner386fe712010-05-19 00:34:15 +00001217 if (enc == NULL)
1218 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 }
1220 v = PySys_GetObject("ps1");
1221 if (v != NULL) {
1222 v = PyObject_Str(v);
1223 if (v == NULL)
1224 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001225 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +00001227 if (ps1 == NULL) {
1228 PyErr_Clear();
1229 ps1 = "";
1230 }
1231 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 }
1233 w = PySys_GetObject("ps2");
1234 if (w != NULL) {
1235 w = PyObject_Str(w);
1236 if (w == NULL)
1237 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001238 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +00001240 if (ps2 == NULL) {
1241 PyErr_Clear();
1242 ps2 = "";
1243 }
1244 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 }
1246 arena = PyArena_New();
1247 if (arena == NULL) {
1248 Py_XDECREF(v);
1249 Py_XDECREF(w);
1250 Py_XDECREF(oenc);
1251 return -1;
1252 }
1253 mod = PyParser_ASTFromFile(fp, filename, enc,
1254 Py_single_input, ps1, ps2,
1255 flags, &errcode, arena);
1256 Py_XDECREF(v);
1257 Py_XDECREF(w);
1258 Py_XDECREF(oenc);
1259 if (mod == NULL) {
1260 PyArena_Free(arena);
1261 if (errcode == E_EOF) {
1262 PyErr_Clear();
1263 return E_EOF;
1264 }
1265 PyErr_Print();
1266 return -1;
1267 }
1268 m = PyImport_AddModule("__main__");
1269 if (m == NULL) {
1270 PyArena_Free(arena);
1271 return -1;
1272 }
1273 d = PyModule_GetDict(m);
1274 v = run_mod(mod, filename, d, d, flags, arena);
1275 PyArena_Free(arena);
1276 flush_io();
1277 if (v == NULL) {
1278 PyErr_Print();
1279 return -1;
1280 }
1281 Py_DECREF(v);
1282 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001283}
1284
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001285/* Check whether a file maybe a pyc file: Look at the extension,
1286 the file type, and, if we may close it, at the first few bytes. */
1287
1288static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001289maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1292 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 /* Only look into the file if we are allowed to close it, since
1295 it then should also be seekable. */
1296 if (closeit) {
1297 /* Read only two bytes of the magic. If the file was opened in
1298 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1299 be read as they are on disk. */
1300 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1301 unsigned char buf[2];
1302 /* Mess: In case of -x, the stream is NOT at its start now,
1303 and ungetc() was used to push back the first newline,
1304 which makes the current stream position formally undefined,
1305 and a x-platform nightmare.
1306 Unfortunately, we have no direct way to know whether -x
1307 was specified. So we use a terrible hack: if the current
1308 stream position is not 0, we assume -x was specified, and
1309 give up. Bug 132850 on SourceForge spells out the
1310 hopelessness of trying anything else (fseek and ftell
1311 don't work predictably x-platform for text-mode files).
1312 */
1313 int ispyc = 0;
1314 if (ftell(fp) == 0) {
1315 if (fread(buf, 1, 2, fp) == 2 &&
1316 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1317 ispyc = 1;
1318 rewind(fp);
1319 }
1320 return ispyc;
1321 }
1322 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001323}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001324
Guido van Rossum0df002c2000-08-27 19:21:52 +00001325int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001326PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 PyObject *m, *d, *v;
1330 const char *ext;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001331 int set_file_name = 0, ret;
1332 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 m = PyImport_AddModule("__main__");
1335 if (m == NULL)
1336 return -1;
1337 d = PyModule_GetDict(m);
1338 if (PyDict_GetItemString(d, "__file__") == NULL) {
1339 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001340 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 if (f == NULL)
1342 return -1;
1343 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1344 Py_DECREF(f);
1345 return -1;
1346 }
Barry Warsaw916048d2011-09-20 14:45:44 -04001347 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1348 Py_DECREF(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 return -1;
Barry Warsaw916048d2011-09-20 14:45:44 -04001350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 set_file_name = 1;
1352 Py_DECREF(f);
1353 }
1354 len = strlen(filename);
1355 ext = filename + len - (len > 4 ? 4 : 0);
1356 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1357 /* Try to run a pyc file. First, re-open in binary */
1358 if (closeit)
1359 fclose(fp);
1360 if ((fp = fopen(filename, "rb")) == NULL) {
1361 fprintf(stderr, "python: Can't reopen .pyc file\n");
1362 ret = -1;
1363 goto done;
1364 }
1365 /* Turn on optimization if a .pyo file is given */
1366 if (strcmp(ext, ".pyo") == 0)
1367 Py_OptimizeFlag = 1;
1368 v = run_pyc_file(fp, filename, d, d, flags);
1369 } else {
1370 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1371 closeit, flags);
1372 }
1373 flush_io();
1374 if (v == NULL) {
1375 PyErr_Print();
1376 ret = -1;
1377 goto done;
1378 }
1379 Py_DECREF(v);
1380 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001381 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1383 PyErr_Clear();
1384 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001385}
1386
1387int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001388PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 PyObject *m, *d, *v;
1391 m = PyImport_AddModule("__main__");
1392 if (m == NULL)
1393 return -1;
1394 d = PyModule_GetDict(m);
1395 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1396 if (v == NULL) {
1397 PyErr_Print();
1398 return -1;
1399 }
1400 Py_DECREF(v);
1401 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001402}
1403
Barry Warsaw035574d1997-08-29 22:07:17 +00001404static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001405parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 long hold;
1409 PyObject *v;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001410 _Py_IDENTIFIER(msg);
1411 _Py_IDENTIFIER(filename);
1412 _Py_IDENTIFIER(lineno);
1413 _Py_IDENTIFIER(offset);
1414 _Py_IDENTIFIER(text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001415
Benjamin Peterson80d50422012-04-03 00:30:38 -04001416 *message = NULL;
1417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 /* new style errors. `err' is an instance */
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001419 *message = _PyObject_GetAttrId(err, &PyId_msg);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001420 if (!*message)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001422
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001423 v = _PyObject_GetAttrId(err, &PyId_filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001424 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001426 if (v == Py_None) {
1427 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 *filename = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001429 }
1430 else {
1431 *filename = _PyUnicode_AsString(v);
1432 Py_DECREF(v);
1433 if (!*filename)
1434 goto finally;
1435 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001436
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001437 v = _PyObject_GetAttrId(err, &PyId_lineno);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001438 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 goto finally;
1440 hold = PyLong_AsLong(v);
1441 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if (hold < 0 && PyErr_Occurred())
1443 goto finally;
1444 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001445
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001446 v = _PyObject_GetAttrId(err, &PyId_offset);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001447 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 goto finally;
1449 if (v == Py_None) {
1450 *offset = -1;
1451 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 } else {
1453 hold = PyLong_AsLong(v);
1454 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 if (hold < 0 && PyErr_Occurred())
1456 goto finally;
1457 *offset = (int)hold;
1458 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001459
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001460 v = _PyObject_GetAttrId(err, &PyId_text);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001461 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001463 if (v == Py_None) {
1464 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 *text = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001466 }
1467 else {
1468 *text = _PyUnicode_AsString(v);
1469 Py_DECREF(v);
1470 if (!*text)
1471 goto finally;
1472 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001474
1475finally:
Benjamin Peterson80d50422012-04-03 00:30:38 -04001476 Py_XDECREF(*message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001478}
1479
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001480void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001481PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001484}
1485
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001486static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001487print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 char *nl;
1490 if (offset >= 0) {
Benjamin Petersona95e9772010-10-29 03:28:14 +00001491 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1492 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 for (;;) {
1494 nl = strchr(text, '\n');
1495 if (nl == NULL || nl-text >= offset)
1496 break;
1497 offset -= (int)(nl+1-text);
1498 text = nl+1;
1499 }
1500 while (*text == ' ' || *text == '\t') {
1501 text++;
1502 offset--;
1503 }
1504 }
1505 PyFile_WriteString(" ", f);
1506 PyFile_WriteString(text, f);
1507 if (*text == '\0' || text[strlen(text)-1] != '\n')
1508 PyFile_WriteString("\n", f);
1509 if (offset == -1)
1510 return;
1511 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +00001512 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001515}
1516
Guido van Rossum66e8e862001-03-23 17:54:43 +00001517static void
1518handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 PyObject *exception, *value, *tb;
1521 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if (Py_InspectFlag)
1524 /* Don't exit if -i flag was given. This flag is set to 0
1525 * when entering interactive mode for inspecting. */
1526 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 PyErr_Fetch(&exception, &value, &tb);
1529 fflush(stdout);
1530 if (value == NULL || value == Py_None)
1531 goto done;
1532 if (PyExceptionInstance_Check(value)) {
1533 /* The error code should be in the `code' attribute. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001534 _Py_IDENTIFIER(code);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001535 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 if (code) {
1537 Py_DECREF(value);
1538 value = code;
1539 if (value == Py_None)
1540 goto done;
1541 }
1542 /* If we failed to dig out the 'code' attribute,
1543 just let the else clause below print the error. */
1544 }
1545 if (PyLong_Check(value))
1546 exitcode = (int)PyLong_AsLong(value);
1547 else {
Victor Stinnere9fb3192010-05-17 08:58:51 +00001548 PyObject *sys_stderr = PySys_GetObject("stderr");
Victor Stinner7126dbc2010-05-21 23:45:42 +00001549 if (sys_stderr != NULL && sys_stderr != Py_None) {
1550 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1551 } else {
1552 PyObject_Print(value, stderr, Py_PRINT_RAW);
1553 fflush(stderr);
1554 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 PySys_WriteStderr("\n");
1556 exitcode = 1;
1557 }
Tim Peterscf615b52003-04-19 18:47:02 +00001558 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 /* Restore and clear the exception info, in order to properly decref
1560 * the exception, value, and traceback. If we just exit instead,
1561 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1562 * some finalizers from running.
1563 */
1564 PyErr_Restore(exception, value, tb);
1565 PyErr_Clear();
1566 Py_Exit(exitcode);
1567 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001568}
1569
1570void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001571PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1576 handle_system_exit();
1577 }
1578 PyErr_Fetch(&exception, &v, &tb);
1579 if (exception == NULL)
1580 return;
1581 PyErr_NormalizeException(&exception, &v, &tb);
1582 if (tb == NULL) {
1583 tb = Py_None;
1584 Py_INCREF(tb);
1585 }
1586 PyException_SetTraceback(v, tb);
1587 if (exception == NULL)
1588 return;
1589 /* Now we know v != NULL too */
1590 if (set_sys_last_vars) {
1591 PySys_SetObject("last_type", exception);
1592 PySys_SetObject("last_value", v);
1593 PySys_SetObject("last_traceback", tb);
1594 }
1595 hook = PySys_GetObject("excepthook");
1596 if (hook) {
1597 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1598 PyObject *result = PyEval_CallObject(hook, args);
1599 if (result == NULL) {
1600 PyObject *exception2, *v2, *tb2;
1601 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1602 handle_system_exit();
1603 }
1604 PyErr_Fetch(&exception2, &v2, &tb2);
1605 PyErr_NormalizeException(&exception2, &v2, &tb2);
1606 /* It should not be possible for exception2 or v2
1607 to be NULL. However PyErr_Display() can't
1608 tolerate NULLs, so just be safe. */
1609 if (exception2 == NULL) {
1610 exception2 = Py_None;
1611 Py_INCREF(exception2);
1612 }
1613 if (v2 == NULL) {
1614 v2 = Py_None;
1615 Py_INCREF(v2);
1616 }
1617 fflush(stdout);
1618 PySys_WriteStderr("Error in sys.excepthook:\n");
1619 PyErr_Display(exception2, v2, tb2);
1620 PySys_WriteStderr("\nOriginal exception was:\n");
1621 PyErr_Display(exception, v, tb);
1622 Py_DECREF(exception2);
1623 Py_DECREF(v2);
1624 Py_XDECREF(tb2);
1625 }
1626 Py_XDECREF(result);
1627 Py_XDECREF(args);
1628 } else {
1629 PySys_WriteStderr("sys.excepthook is missing\n");
1630 PyErr_Display(exception, v, tb);
1631 }
1632 Py_XDECREF(exception);
1633 Py_XDECREF(v);
1634 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001635}
1636
Benjamin Petersone6528212008-07-15 15:32:09 +00001637static void
1638print_exception(PyObject *f, PyObject *value)
1639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 int err = 0;
1641 PyObject *type, *tb;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001642 _Py_IDENTIFIER(print_file_and_line);
Benjamin Petersone6528212008-07-15 15:32:09 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 if (!PyExceptionInstance_Check(value)) {
1645 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1646 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1647 PyFile_WriteString(" found\n", f);
1648 return;
1649 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 Py_INCREF(value);
1652 fflush(stdout);
1653 type = (PyObject *) Py_TYPE(value);
1654 tb = PyException_GetTraceback(value);
1655 if (tb && tb != Py_None)
1656 err = PyTraceBack_Print(tb, f);
1657 if (err == 0 &&
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001658 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 {
1660 PyObject *message;
1661 const char *filename, *text;
1662 int lineno, offset;
1663 if (!parse_syntax_error(value, &message, &filename,
1664 &lineno, &offset, &text))
1665 PyErr_Clear();
1666 else {
1667 char buf[10];
1668 PyFile_WriteString(" File \"", f);
1669 if (filename == NULL)
1670 PyFile_WriteString("<string>", f);
1671 else
1672 PyFile_WriteString(filename, f);
1673 PyFile_WriteString("\", line ", f);
1674 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1675 PyFile_WriteString(buf, f);
1676 PyFile_WriteString("\n", f);
1677 if (text != NULL)
1678 print_error_text(f, offset, text);
1679 Py_DECREF(value);
1680 value = message;
1681 /* Can't be bothered to check all those
1682 PyFile_WriteString() calls */
1683 if (PyErr_Occurred())
1684 err = -1;
1685 }
1686 }
1687 if (err) {
1688 /* Don't do anything else */
1689 }
1690 else {
1691 PyObject* moduleName;
1692 char* className;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001693 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 assert(PyExceptionClass_Check(type));
1695 className = PyExceptionClass_Name(type);
1696 if (className != NULL) {
1697 char *dot = strrchr(className, '.');
1698 if (dot != NULL)
1699 className = dot+1;
1700 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001701
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001702 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1704 {
Victor Stinner13b21bd2011-05-26 14:25:13 +02001705 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 err = PyFile_WriteString("<unknown>", f);
1707 }
1708 else {
1709 char* modstr = _PyUnicode_AsString(moduleName);
1710 if (modstr && strcmp(modstr, "builtins"))
1711 {
1712 err = PyFile_WriteString(modstr, f);
1713 err += PyFile_WriteString(".", f);
1714 }
1715 Py_DECREF(moduleName);
1716 }
1717 if (err == 0) {
1718 if (className == NULL)
1719 err = PyFile_WriteString("<unknown>", f);
1720 else
1721 err = PyFile_WriteString(className, f);
1722 }
1723 }
1724 if (err == 0 && (value != Py_None)) {
1725 PyObject *s = PyObject_Str(value);
1726 /* only print colon if the str() of the
1727 object is not the empty string
1728 */
1729 if (s == NULL)
1730 err = -1;
1731 else if (!PyUnicode_Check(s) ||
Victor Stinnere251d6d2011-11-20 19:20:00 +01001732 PyUnicode_GetLength(s) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 err = PyFile_WriteString(": ", f);
1734 if (err == 0)
1735 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1736 Py_XDECREF(s);
1737 }
1738 /* try to write a newline in any case */
1739 err += PyFile_WriteString("\n", f);
1740 Py_XDECREF(tb);
1741 Py_DECREF(value);
1742 /* If an error happened here, don't show it.
1743 XXX This is wrong, but too many callers rely on this behavior. */
1744 if (err != 0)
1745 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001746}
1747
1748static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 "\nThe above exception was the direct cause "
1750 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001751
1752static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 "\nDuring handling of the above exception, "
1754 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001755
1756static void
1757print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 int err = 0, res;
1760 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 if (seen != NULL) {
1763 /* Exception chaining */
1764 if (PySet_Add(seen, value) == -1)
1765 PyErr_Clear();
1766 else if (PyExceptionInstance_Check(value)) {
1767 cause = PyException_GetCause(value);
1768 context = PyException_GetContext(value);
Nick Coghlanab7bf212012-02-26 17:49:52 +10001769 if (cause && cause == Py_None) {
1770 /* print neither cause nor context */
1771 ;
1772 }
1773 else if (cause) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 res = PySet_Contains(seen, cause);
1775 if (res == -1)
1776 PyErr_Clear();
1777 if (res == 0) {
1778 print_exception_recursive(
1779 f, cause, seen);
1780 err |= PyFile_WriteString(
1781 cause_message, f);
1782 }
1783 }
1784 else if (context) {
1785 res = PySet_Contains(seen, context);
1786 if (res == -1)
1787 PyErr_Clear();
1788 if (res == 0) {
1789 print_exception_recursive(
1790 f, context, seen);
1791 err |= PyFile_WriteString(
1792 context_message, f);
1793 }
1794 }
1795 Py_XDECREF(context);
1796 Py_XDECREF(cause);
1797 }
1798 }
1799 print_exception(f, value);
1800 if (err != 0)
1801 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001802}
1803
Thomas Wouters477c8d52006-05-27 19:21:47 +00001804void
1805PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 PyObject *seen;
1808 PyObject *f = PySys_GetObject("stderr");
1809 if (f == Py_None) {
1810 /* pass */
1811 }
1812 else if (f == NULL) {
1813 _PyObject_Dump(value);
1814 fprintf(stderr, "lost sys.stderr\n");
1815 }
1816 else {
1817 /* We choose to ignore seen being possibly NULL, and report
1818 at least the main exception (it could be a MemoryError).
1819 */
1820 seen = PySet_New(NULL);
1821 if (seen == NULL)
1822 PyErr_Clear();
1823 print_exception_recursive(f, value, seen);
1824 Py_XDECREF(seen);
1825 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001826}
1827
Guido van Rossum82598051997-03-05 00:20:32 +00001828PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001829PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 PyObject *ret = NULL;
1833 mod_ty mod;
1834 PyArena *arena = PyArena_New();
1835 if (arena == NULL)
1836 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1839 if (mod != NULL)
1840 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1841 PyArena_Free(arena);
1842 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001843}
1844
1845PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001846PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 PyObject *ret;
1850 mod_ty mod;
1851 PyArena *arena = PyArena_New();
1852 if (arena == NULL)
1853 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1856 flags, NULL, arena);
1857 if (closeit)
1858 fclose(fp);
1859 if (mod == NULL) {
1860 PyArena_Free(arena);
1861 return NULL;
1862 }
1863 ret = run_mod(mod, filename, globals, locals, flags, arena);
1864 PyArena_Free(arena);
1865 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001866}
1867
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001868static void
1869flush_io(void)
1870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 PyObject *f, *r;
1872 PyObject *type, *value, *traceback;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001873 _Py_IDENTIFIER(flush);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 /* Save the current exception */
1876 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 f = PySys_GetObject("stderr");
1879 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001880 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 if (r)
1882 Py_DECREF(r);
1883 else
1884 PyErr_Clear();
1885 }
1886 f = PySys_GetObject("stdout");
1887 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001888 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 if (r)
1890 Py_DECREF(r);
1891 else
1892 PyErr_Clear();
1893 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001896}
1897
Guido van Rossum82598051997-03-05 00:20:32 +00001898static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 PyCodeObject *co;
1903 PyObject *v;
1904 co = PyAST_Compile(mod, filename, flags, arena);
1905 if (co == NULL)
1906 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001907 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 Py_DECREF(co);
1909 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001910}
1911
Guido van Rossum82598051997-03-05 00:20:32 +00001912static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001913run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 PyCodeObject *co;
1917 PyObject *v;
1918 long magic;
1919 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 magic = PyMarshal_ReadLongFromFile(fp);
1922 if (magic != PyImport_GetMagicNumber()) {
1923 PyErr_SetString(PyExc_RuntimeError,
1924 "Bad magic number in .pyc file");
1925 return NULL;
1926 }
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001927 /* Skip mtime and size */
1928 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 (void) PyMarshal_ReadLongFromFile(fp);
1930 v = PyMarshal_ReadLastObjectFromFile(fp);
1931 fclose(fp);
1932 if (v == NULL || !PyCode_Check(v)) {
1933 Py_XDECREF(v);
1934 PyErr_SetString(PyExc_RuntimeError,
1935 "Bad code object in .pyc file");
1936 return NULL;
1937 }
1938 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001939 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 if (v && flags)
1941 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1942 Py_DECREF(co);
1943 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001944}
1945
Guido van Rossum82598051997-03-05 00:20:32 +00001946PyObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00001947Py_CompileStringExFlags(const char *str, const char *filename, int start,
1948 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 PyCodeObject *co;
1951 mod_ty mod;
1952 PyArena *arena = PyArena_New();
1953 if (arena == NULL)
1954 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1957 if (mod == NULL) {
1958 PyArena_Free(arena);
1959 return NULL;
1960 }
1961 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1962 PyObject *result = PyAST_mod2obj(mod);
1963 PyArena_Free(arena);
1964 return result;
1965 }
Georg Brandl8334fd92010-12-04 10:26:46 +00001966 co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 PyArena_Free(arena);
1968 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001969}
1970
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001971/* For use in Py_LIMITED_API */
1972#undef Py_CompileString
1973PyObject *
1974PyCompileString(const char *str, const char *filename, int start)
1975{
1976 return Py_CompileStringFlags(str, filename, start, NULL);
1977}
1978
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001979struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001980Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 struct symtable *st;
1983 mod_ty mod;
1984 PyCompilerFlags flags;
1985 PyArena *arena = PyArena_New();
1986 if (arena == NULL)
1987 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 flags.cf_flags = 0;
1990 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1991 if (mod == NULL) {
1992 PyArena_Free(arena);
1993 return NULL;
1994 }
1995 st = PySymtable_Build(mod, filename, 0);
1996 PyArena_Free(arena);
1997 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001998}
1999
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000/* Preferred access to parser is through AST. */
2001mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002002PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 mod_ty mod;
2006 PyCompilerFlags localflags;
2007 perrdetail err;
2008 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00002009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
2011 &_PyParser_Grammar, start, &err,
2012 &iflags);
2013 if (flags == NULL) {
2014 localflags.cf_flags = 0;
2015 flags = &localflags;
2016 }
2017 if (n) {
2018 flags->cf_flags |= iflags & PyCF_MASK;
2019 mod = PyAST_FromNode(n, flags, filename, arena);
2020 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 }
2022 else {
2023 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002024 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002026 err_free(&err);
2027 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028}
2029
2030mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00002031PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 int start, char *ps1,
2033 char *ps2, PyCompilerFlags *flags, int *errcode,
2034 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 mod_ty mod;
2037 PyCompilerFlags localflags;
2038 perrdetail err;
2039 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00002040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
2042 &_PyParser_Grammar,
2043 start, ps1, ps2, &err, &iflags);
2044 if (flags == NULL) {
2045 localflags.cf_flags = 0;
2046 flags = &localflags;
2047 }
2048 if (n) {
2049 flags->cf_flags |= iflags & PyCF_MASK;
2050 mod = PyAST_FromNode(n, flags, filename, arena);
2051 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 }
2053 else {
2054 err_input(&err);
2055 if (errcode)
2056 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02002057 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002059 err_free(&err);
2060 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061}
2062
Guido van Rossuma110aa61994-08-29 12:50:44 +00002063/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002064
Guido van Rossuma110aa61994-08-29 12:50:44 +00002065node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002066PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 perrdetail err;
2069 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
2070 &_PyParser_Grammar,
2071 start, NULL, NULL, &err, flags);
2072 if (n == NULL)
2073 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002074 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002077}
2078
Guido van Rossuma110aa61994-08-29 12:50:44 +00002079/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002080
Guido van Rossuma110aa61994-08-29 12:50:44 +00002081node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002082PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00002083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 perrdetail err;
2085 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
2086 start, &err, flags);
2087 if (n == NULL)
2088 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002089 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00002091}
2092
2093node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002094PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 perrdetail err;
2098 node *n = PyParser_ParseStringFlagsFilename(str, filename,
2099 &_PyParser_Grammar, start, &err, flags);
2100 if (n == NULL)
2101 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002102 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00002104}
2105
2106node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002107PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00002110}
2111
Guido van Rossum66ebd912003-04-17 16:02:26 +00002112/* May want to move a more generalized form of this to parsetok.c or
2113 even parser modules. */
2114
2115void
Victor Stinner7f2fee32011-04-05 00:39:01 +02002116PyParser_ClearError(perrdetail *err)
2117{
2118 err_free(err);
2119}
2120
2121void
Guido van Rossum66ebd912003-04-17 16:02:26 +00002122PyParser_SetError(perrdetail *err)
2123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00002125}
2126
Victor Stinner7f2fee32011-04-05 00:39:01 +02002127static void
2128err_free(perrdetail *err)
2129{
2130 Py_CLEAR(err->filename);
2131}
2132
Guido van Rossuma110aa61994-08-29 12:50:44 +00002133/* Set the error appropriate to the given input error code (see errcode.h) */
2134
2135static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002136err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00002137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 PyObject *v, *w, *errtype, *errtext;
2139 PyObject *msg_obj = NULL;
2140 char *msg = NULL;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 errtype = PyExc_SyntaxError;
2143 switch (err->error) {
2144 case E_ERROR:
2145 return;
2146 case E_SYNTAX:
2147 errtype = PyExc_IndentationError;
2148 if (err->expected == INDENT)
2149 msg = "expected an indented block";
2150 else if (err->token == INDENT)
2151 msg = "unexpected indent";
2152 else if (err->token == DEDENT)
2153 msg = "unexpected unindent";
2154 else {
2155 errtype = PyExc_SyntaxError;
2156 msg = "invalid syntax";
2157 }
2158 break;
2159 case E_TOKEN:
2160 msg = "invalid token";
2161 break;
2162 case E_EOFS:
2163 msg = "EOF while scanning triple-quoted string literal";
2164 break;
2165 case E_EOLS:
2166 msg = "EOL while scanning string literal";
2167 break;
2168 case E_INTR:
2169 if (!PyErr_Occurred())
2170 PyErr_SetNone(PyExc_KeyboardInterrupt);
2171 goto cleanup;
2172 case E_NOMEM:
2173 PyErr_NoMemory();
2174 goto cleanup;
2175 case E_EOF:
2176 msg = "unexpected EOF while parsing";
2177 break;
2178 case E_TABSPACE:
2179 errtype = PyExc_TabError;
2180 msg = "inconsistent use of tabs and spaces in indentation";
2181 break;
2182 case E_OVERFLOW:
2183 msg = "expression too long";
2184 break;
2185 case E_DEDENT:
2186 errtype = PyExc_IndentationError;
2187 msg = "unindent does not match any outer indentation level";
2188 break;
2189 case E_TOODEEP:
2190 errtype = PyExc_IndentationError;
2191 msg = "too many levels of indentation";
2192 break;
2193 case E_DECODE: {
2194 PyObject *type, *value, *tb;
2195 PyErr_Fetch(&type, &value, &tb);
2196 msg = "unknown decode error";
2197 if (value != NULL)
2198 msg_obj = PyObject_Str(value);
2199 Py_XDECREF(type);
2200 Py_XDECREF(value);
2201 Py_XDECREF(tb);
2202 break;
2203 }
2204 case E_LINECONT:
2205 msg = "unexpected character after line continuation character";
2206 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00002207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 case E_IDENTIFIER:
2209 msg = "invalid character in identifier";
2210 break;
Meador Ingefa21bf02012-01-19 01:08:41 -06002211 case E_BADSINGLE:
2212 msg = "multiple statements found while compiling a single statement";
2213 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 default:
2215 fprintf(stderr, "error=%d\n", err->error);
2216 msg = "unknown parsing error";
2217 break;
2218 }
2219 /* err->text may not be UTF-8 in case of decoding errors.
2220 Explicitly convert to an object. */
2221 if (!err->text) {
2222 errtext = Py_None;
2223 Py_INCREF(Py_None);
2224 } else {
2225 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2226 "replace");
2227 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002228 v = Py_BuildValue("(OiiN)", err->filename,
2229 err->lineno, err->offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 if (v != NULL) {
2231 if (msg_obj)
2232 w = Py_BuildValue("(OO)", msg_obj, v);
2233 else
2234 w = Py_BuildValue("(sO)", msg, v);
2235 } else
2236 w = NULL;
2237 Py_XDECREF(v);
2238 PyErr_SetObject(errtype, w);
2239 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002240cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 Py_XDECREF(msg_obj);
2242 if (err->text != NULL) {
2243 PyObject_FREE(err->text);
2244 err->text = NULL;
2245 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002246}
2247
2248/* Print fatal error message and abort */
2249
2250void
Tim Peters7c321a82002-07-09 02:57:01 +00002251Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002252{
Victor Stinner024e37a2011-03-31 01:31:06 +02002253 const int fd = fileno(stderr);
2254 PyThreadState *tstate;
2255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 fprintf(stderr, "Fatal Python error: %s\n", msg);
2257 fflush(stderr); /* it helps in Windows debug build */
2258 if (PyErr_Occurred()) {
Victor Stinner55a5c782010-06-08 21:00:13 +00002259 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 }
Victor Stinner024e37a2011-03-31 01:31:06 +02002261 else {
2262 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
2263 if (tstate != NULL) {
2264 fputc('\n', stderr);
2265 fflush(stderr);
Victor Stinner7bba62f2011-05-07 12:43:00 +02002266 _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
Victor Stinner024e37a2011-03-31 01:31:06 +02002267 }
Victor Stinnerd727e232011-04-01 12:13:55 +02002268 _PyFaulthandler_Fini();
Victor Stinner024e37a2011-03-31 01:31:06 +02002269 }
2270
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002271#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 {
2273 size_t len = strlen(msg);
2274 WCHAR* buffer;
2275 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 /* Convert the message to wchar_t. This uses a simple one-to-one
2278 conversion, assuming that the this error message actually uses ASCII
2279 only. If this ceases to be true, we will have to convert. */
2280 buffer = alloca( (len+1) * (sizeof *buffer));
2281 for( i=0; i<=len; ++i)
2282 buffer[i] = msg[i];
2283 OutputDebugStringW(L"Fatal Python error: ");
2284 OutputDebugStringW(buffer);
2285 OutputDebugStringW(L"\n");
2286 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002287#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002289#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002290#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002292}
2293
2294/* Clean up and exit */
2295
Guido van Rossuma110aa61994-08-29 12:50:44 +00002296#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002297#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002298#endif
2299
Collin Winter670e6922007-03-21 02:57:17 +00002300static void (*pyexitfunc)(void) = NULL;
2301/* For the atexit module. */
2302void _Py_PyAtExit(void (*func)(void))
2303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002305}
2306
2307static void
2308call_py_exitfuncs(void)
2309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 if (pyexitfunc == NULL)
2311 return;
Collin Winter670e6922007-03-21 02:57:17 +00002312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 (*pyexitfunc)();
2314 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002315}
2316
Antoine Pitrou011bd622009-10-20 21:52:47 +00002317/* Wait until threading._shutdown completes, provided
2318 the threading module was imported in the first place.
2319 The shutdown routine will wait until all non-daemon
2320 "threading" threads have completed. */
2321static void
2322wait_for_thread_shutdown(void)
2323{
2324#ifdef WITH_THREAD
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002325 _Py_IDENTIFIER(_shutdown);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 PyObject *result;
2327 PyThreadState *tstate = PyThreadState_GET();
2328 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2329 "threading");
2330 if (threading == NULL) {
2331 /* threading not imported */
2332 PyErr_Clear();
2333 return;
2334 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002335 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 if (result == NULL) {
2337 PyErr_WriteUnraisable(threading);
2338 }
2339 else {
2340 Py_DECREF(result);
2341 }
2342 Py_DECREF(threading);
Antoine Pitrou011bd622009-10-20 21:52:47 +00002343#endif
2344}
2345
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002346#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002347static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002348static int nexitfuncs = 0;
2349
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002350int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 if (nexitfuncs >= NEXITFUNCS)
2353 return -1;
2354 exitfuncs[nexitfuncs++] = func;
2355 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002356}
2357
Guido van Rossumcc283f51997-08-05 02:22:03 +00002358static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002359call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 while (nexitfuncs > 0)
2362 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 fflush(stdout);
2365 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002366}
2367
2368void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002369Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002374}
2375
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002376static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002377initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002378{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002379#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002381#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002382#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002384#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002385#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002387#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002389}
2390
Guido van Rossum7433b121997-02-14 19:45:36 +00002391
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002392/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2393 *
2394 * All of the code in this function must only use async-signal-safe functions,
2395 * listed at `man 7 signal` or
2396 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2397 */
2398void
2399_Py_RestoreSignals(void)
2400{
2401#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 PyOS_setsig(SIGPIPE, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002403#endif
2404#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 PyOS_setsig(SIGXFZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002406#endif
2407#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 PyOS_setsig(SIGXFSZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002409#endif
2410}
2411
2412
Guido van Rossum7433b121997-02-14 19:45:36 +00002413/*
2414 * The file descriptor fd is considered ``interactive'' if either
2415 * a) isatty(fd) is TRUE, or
2416 * b) the -i flag was given, and the filename associated with
2417 * the descriptor is NULL or "<stdin>" or "???".
2418 */
2419int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002420Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 if (isatty((int)fileno(fp)))
2423 return 1;
2424 if (!Py_InteractiveFlag)
2425 return 0;
2426 return (filename == NULL) ||
2427 (strcmp(filename, "<stdin>") == 0) ||
2428 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002429}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002430
2431
Tim Petersd08e3822003-04-17 15:24:21 +00002432#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002433#if defined(WIN32) && defined(_MSC_VER)
2434
2435/* Stack checking for Microsoft C */
2436
2437#include <malloc.h>
2438#include <excpt.h>
2439
Fred Drakee8de31c2000-08-31 05:38:39 +00002440/*
2441 * Return non-zero when we run out of memory on the stack; zero otherwise.
2442 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002443int
Fred Drake399739f2000-08-31 05:52:44 +00002444PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 __try {
2447 /* alloca throws a stack overflow exception if there's
2448 not enough space left on the stack */
2449 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2450 return 0;
2451 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2452 EXCEPTION_EXECUTE_HANDLER :
2453 EXCEPTION_CONTINUE_SEARCH) {
2454 int errcode = _resetstkoflw();
2455 if (errcode == 0)
2456 {
2457 Py_FatalError("Could not reset the stack!");
2458 }
2459 }
2460 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002461}
2462
2463#endif /* WIN32 && _MSC_VER */
2464
2465/* Alternate implementations can be added here... */
2466
2467#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002468
2469
2470/* Wrappers around sigaction() or signal(). */
2471
2472PyOS_sighandler_t
2473PyOS_getsig(int sig)
2474{
2475#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 struct sigaction context;
2477 if (sigaction(sig, NULL, &context) == -1)
2478 return SIG_ERR;
2479 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002480#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002482/* Special signal handling for the secure CRT in Visual Studio 2005 */
2483#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 switch (sig) {
2485 /* Only these signals are valid */
2486 case SIGINT:
2487 case SIGILL:
2488 case SIGFPE:
2489 case SIGSEGV:
2490 case SIGTERM:
2491 case SIGBREAK:
2492 case SIGABRT:
2493 break;
2494 /* Don't call signal() with other values or it will assert */
2495 default:
2496 return SIG_ERR;
2497 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002498#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 handler = signal(sig, SIG_IGN);
2500 if (handler != SIG_ERR)
2501 signal(sig, handler);
2502 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002503#endif
2504}
2505
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002506/*
2507 * All of the code in this function must only use async-signal-safe functions,
2508 * listed at `man 7 signal` or
2509 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2510 */
Guido van Rossum6f256182000-09-16 16:32:19 +00002511PyOS_sighandler_t
2512PyOS_setsig(int sig, PyOS_sighandler_t handler)
2513{
2514#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 /* Some code in Modules/signalmodule.c depends on sigaction() being
2516 * used here if HAVE_SIGACTION is defined. Fix that if this code
2517 * changes to invalidate that assumption.
2518 */
2519 struct sigaction context, ocontext;
2520 context.sa_handler = handler;
2521 sigemptyset(&context.sa_mask);
2522 context.sa_flags = 0;
2523 if (sigaction(sig, &context, &ocontext) == -1)
2524 return SIG_ERR;
2525 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002526#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 PyOS_sighandler_t oldhandler;
2528 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002529#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002531#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002533#endif
2534}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535
2536/* Deprecated C API functions still provided for binary compatiblity */
2537
2538#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002539PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543}
2544
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002545#undef PyParser_SimpleParseString
2546PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547PyParser_SimpleParseString(const char *str, int start)
2548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002551
2552#undef PyRun_AnyFile
2553PyAPI_FUNC(int)
2554PyRun_AnyFile(FILE *fp, const char *name)
2555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002557}
2558
2559#undef PyRun_AnyFileEx
2560PyAPI_FUNC(int)
2561PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002564}
2565
2566#undef PyRun_AnyFileFlags
2567PyAPI_FUNC(int)
2568PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002571}
2572
2573#undef PyRun_File
2574PyAPI_FUNC(PyObject *)
2575PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002578}
2579
2580#undef PyRun_FileEx
2581PyAPI_FUNC(PyObject *)
2582PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002585}
2586
2587#undef PyRun_FileFlags
2588PyAPI_FUNC(PyObject *)
2589PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002593}
2594
2595#undef PyRun_SimpleFile
2596PyAPI_FUNC(int)
2597PyRun_SimpleFile(FILE *f, const char *p)
2598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002600}
2601
2602#undef PyRun_SimpleFileEx
2603PyAPI_FUNC(int)
2604PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002607}
2608
2609
2610#undef PyRun_String
2611PyAPI_FUNC(PyObject *)
2612PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002615}
2616
2617#undef PyRun_SimpleString
2618PyAPI_FUNC(int)
2619PyRun_SimpleString(const char *s)
2620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002622}
2623
2624#undef Py_CompileString
2625PyAPI_FUNC(PyObject *)
2626Py_CompileString(const char *str, const char *p, int s)
2627{
Georg Brandl8334fd92010-12-04 10:26:46 +00002628 return Py_CompileStringExFlags(str, p, s, NULL, -1);
2629}
2630
2631#undef Py_CompileStringFlags
2632PyAPI_FUNC(PyObject *)
2633Py_CompileStringFlags(const char *str, const char *p, int s,
2634 PyCompilerFlags *flags)
2635{
2636 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002637}
2638
2639#undef PyRun_InteractiveOne
2640PyAPI_FUNC(int)
2641PyRun_InteractiveOne(FILE *f, const char *p)
2642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002644}
2645
2646#undef PyRun_InteractiveLoop
2647PyAPI_FUNC(int)
2648PyRun_InteractiveLoop(FILE *f, const char *p)
2649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002651}
2652
2653#ifdef __cplusplus
2654}
2655#endif