blob: 1121e64db359f9f22b1f4fdbf5004895bdaaeb24 [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"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000014#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000015#include "symtable.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000016#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "ast.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000018#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000019#include "marshal.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000020#include "osdefs.h"
Antoine Pitroucefb3162009-10-20 22:08:36 +000021#include "abstract.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000022
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000024#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000026
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000027#ifdef MS_WINDOWS
Martin v. Löwis5c88d812009-01-02 20:47:48 +000028#include "malloc.h" /* for alloca */
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000029#endif
Martin v. Löwis5c88d812009-01-02 20:47:48 +000030
Martin v. Löwis73d538b2003-03-05 15:13:47 +000031#ifdef HAVE_LANGINFO_H
32#include <locale.h>
33#include <langinfo.h>
34#endif
35
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000036#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000037#undef BYTE
38#include "windows.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000039#define PATH_MAX MAXPATHLEN
Guido van Rossuma44823b1995-03-14 15:01:17 +000040#endif
41
Neal Norwitz4281cef2006-03-04 19:58:13 +000042#ifndef Py_REF_DEBUG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000044#else /* Py_REF_DEBUG */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000045#define PRINT_TOTAL_REFS() fprintf(stderr, \
46 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
47 _Py_GetRefTotal())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000048#endif
49
50#ifdef __cplusplus
51extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000052#endif
53
Martin v. Löwis790465f2008-04-05 20:41:37 +000054extern wchar_t *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000055
Guido van Rossum82598051997-03-05 00:20:32 +000056extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000057
Guido van Rossumb73cc041993-11-01 16:28:59 +000058/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000059static void initmain(void);
60static void initsite(void);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000061static int initstdio(void);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000062static void flush_io(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000063static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000064 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000065static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000066 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000067static void err_input(perrdetail *);
68static void initsigs(void);
Collin Winter670e6922007-03-21 02:57:17 +000069static void call_py_exitfuncs(void);
Antoine Pitroucefb3162009-10-20 22:08:36 +000070static void wait_for_thread_shutdown(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000071static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000072extern void _PyUnicode_Init(void);
73extern void _PyUnicode_Fini(void);
Guido van Rossumddefaf32007-01-14 03:31:43 +000074extern int _PyLong_Init(void);
75extern void PyLong_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 */
Guido van Rossum7433b121997-02-14 19:45:36 +000084int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumd8faa362007-04-27 19:54:29 +000085int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000086int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +000087int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Christian Heimes790c8232008-01-07 21:14:23 +000088int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000089int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000090int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000091int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Christian Heimes8dc226f2008-05-06 23:45:46 +000092int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Antoine Pitrou05608432009-01-09 18:53:14 +000093int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000094
Christian Heimes33fe8092008-04-13 13:53:33 +000095/* PyModule_GetWarningsModule is no longer necessary as of 2.6
96since _warnings is builtin. This API should not be used. */
97PyObject *
98PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000099{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000100 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +0000101}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000102
Guido van Rossum25ce5661997-08-02 03:10:38 +0000103static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000104
Thomas Wouters7e474022000-07-16 12:04:32 +0000105/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000106
107int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000108Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000109{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000110 return initialized;
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000111}
112
Guido van Rossum25ce5661997-08-02 03:10:38 +0000113/* Global initializations. Can be undone by Py_Finalize(). Don't
114 call this twice without an intervening Py_Finalize() call. When
115 initializations fail, a fatal error is issued and the function does
116 not return. On return, the first thread and interpreter state have
117 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000118
Guido van Rossum25ce5661997-08-02 03:10:38 +0000119 Locking: you must hold the interpreter lock while calling this.
120 (If the lock has not yet been initialized, that's equivalent to
121 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000122
Guido van Rossum25ce5661997-08-02 03:10:38 +0000123*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000124
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000125static int
126add_flag(int flag, const char *envs)
127{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000128 int env = atoi(envs);
129 if (flag < env)
130 flag = env;
131 if (flag < 1)
132 flag = 1;
133 return flag;
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000134}
135
Christian Heimes5833a2f2008-10-30 21:40:04 +0000136#if defined(HAVE_LANGINFO_H) && defined(CODESET)
137static char*
138get_codeset(void)
139{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000140 char* codeset;
141 PyObject *codec, *name;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000142
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000143 codeset = nl_langinfo(CODESET);
144 if (!codeset || codeset[0] == '\0')
145 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000146
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000147 codec = _PyCodec_Lookup(codeset);
148 if (!codec)
149 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000150
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000151 name = PyObject_GetAttrString(codec, "name");
152 Py_CLEAR(codec);
153 if (!name)
154 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000155
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000156 codeset = strdup(_PyUnicode_AsString(name));
157 Py_DECREF(name);
158 return codeset;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000159
160error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000161 Py_XDECREF(codec);
162 PyErr_Clear();
163 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000164}
165#endif
166
Guido van Rossuma027efa1997-05-05 20:56:21 +0000167void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000168Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000169{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000170 PyInterpreterState *interp;
171 PyThreadState *tstate;
172 PyObject *bimod, *sysmod, *pstderr;
173 char *p;
Guido van Rossum8d30cc02007-05-03 17:49:24 +0000174#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000175 char *codeset;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000176#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000177 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000178
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000179 if (initialized)
180 return;
181 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000182
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000183#ifdef HAVE_SETLOCALE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000184 /* Set up the LC_CTYPE locale, so we can obtain
185 the locale's charset without having to switch
186 locales. */
187 setlocale(LC_CTYPE, "");
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000188#endif
189
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000190 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
191 Py_DebugFlag = add_flag(Py_DebugFlag, p);
192 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
193 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
194 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
195 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
196 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
197 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000198
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000199 interp = PyInterpreterState_New();
200 if (interp == NULL)
201 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000202
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000203 tstate = PyThreadState_New(interp);
204 if (tstate == NULL)
205 Py_FatalError("Py_Initialize: can't make first thread");
206 (void) PyThreadState_Swap(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000207
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000208 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000209
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000210 if (!_PyFrame_Init())
211 Py_FatalError("Py_Initialize: can't init frames");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000212
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000213 if (!_PyLong_Init())
214 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000215
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000216 if (!PyByteArray_Init())
217 Py_FatalError("Py_Initialize: can't init bytearray");
Neal Norwitz6968b052007-02-27 19:02:19 +0000218
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000219 _PyFloat_Init();
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000220
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000221 interp->modules = PyDict_New();
222 if (interp->modules == NULL)
223 Py_FatalError("Py_Initialize: can't make modules dictionary");
224 interp->modules_reloading = PyDict_New();
225 if (interp->modules_reloading == NULL)
226 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000227
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000228 /* Init Unicode implementation; relies on the codec registry */
229 _PyUnicode_Init();
Guido van Rossumc94044c2000-03-10 23:03:54 +0000230
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000231 bimod = _PyBuiltin_Init();
232 if (bimod == NULL)
233 Py_FatalError("Py_Initialize: can't initialize builtins modules");
234 _PyImport_FixupExtension(bimod, "builtins", "builtins");
235 interp->builtins = PyModule_GetDict(bimod);
236 if (interp->builtins == NULL)
237 Py_FatalError("Py_Initialize: can't initialize builtins dict");
238 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000239
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000240 /* initialize builtin exceptions */
241 _PyExc_Init();
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000242
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000243 sysmod = _PySys_Init();
244 if (sysmod == NULL)
245 Py_FatalError("Py_Initialize: can't initialize sys");
246 interp->sysdict = PyModule_GetDict(sysmod);
247 if (interp->sysdict == NULL)
248 Py_FatalError("Py_Initialize: can't initialize sys dict");
249 Py_INCREF(interp->sysdict);
250 _PyImport_FixupExtension(sysmod, "sys", "sys");
251 PySys_SetPath(Py_GetPath());
252 PyDict_SetItemString(interp->sysdict, "modules",
253 interp->modules);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000254
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000255 /* Set up a preliminary stderr printer until we have enough
256 infrastructure for the io module in place. */
257 pstderr = PyFile_NewStdPrinter(fileno(stderr));
258 if (pstderr == NULL)
259 Py_FatalError("Py_Initialize: can't set preliminary stderr");
260 PySys_SetObject("stderr", pstderr);
261 PySys_SetObject("__stderr__", pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000262
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000263 _PyImport_Init();
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000264
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000265 _PyImportHooks_Init();
Just van Rossum52e14d62002-12-30 22:08:05 +0000266
Martin v. Löwis011e8422009-05-05 04:43:17 +0000267#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000268 /* On Unix, set the file system encoding according to the
269 user's preference, if the CODESET names a well-known
270 Python codec, and Py_FileSystemDefaultEncoding isn't
271 initialized by other means. Also set the encoding of
272 stdin and stdout if these are terminals. */
Martin v. Löwis011e8422009-05-05 04:43:17 +0000273
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000274 codeset = get_codeset();
275 if (codeset) {
276 if (!Py_FileSystemDefaultEncoding)
277 Py_FileSystemDefaultEncoding = codeset;
278 else
279 free(codeset);
280 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000281#endif
282
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000283 if (install_sigs)
284 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000285
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000286 /* Initialize warnings. */
287 _PyWarnings_Init();
288 if (PySys_HasWarnOptions()) {
289 PyObject *warnings_module = PyImport_ImportModule("warnings");
290 if (!warnings_module)
291 PyErr_Clear();
292 Py_XDECREF(warnings_module);
293 }
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000294
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000295 initmain(); /* Module __main__ */
296 if (initstdio() < 0)
297 Py_FatalError(
298 "Py_Initialize: can't initialize sys standard streams");
299
300 /* auto-thread-state API, if available */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000301#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000302 _PyGILState_Init(interp, tstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000303#endif /* WITH_THREAD */
Victor Stinnerffbc2f62010-03-21 21:48:45 +0000304
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000305 if (!Py_NoSiteFlag)
306 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000307}
308
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000309void
310Py_Initialize(void)
311{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000312 Py_InitializeEx(1);
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000313}
314
315
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000316#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000317extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000318#endif
319
Guido van Rossume8432ac2007-07-09 15:04:50 +0000320/* Flush stdout and stderr */
321
Neal Norwitz2bad9702007-08-27 06:19:22 +0000322static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000323flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000324{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000325 PyObject *fout = PySys_GetObject("stdout");
326 PyObject *ferr = PySys_GetObject("stderr");
327 PyObject *tmp;
Guido van Rossume8432ac2007-07-09 15:04:50 +0000328
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000329 if (fout != NULL && fout != Py_None) {
330 tmp = PyObject_CallMethod(fout, "flush", "");
331 if (tmp == NULL)
332 PyErr_Clear();
333 else
334 Py_DECREF(tmp);
335 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000336
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000337 if (ferr != NULL || ferr != Py_None) {
338 tmp = PyObject_CallMethod(ferr, "flush", "");
339 if (tmp == NULL)
340 PyErr_Clear();
341 else
342 Py_DECREF(tmp);
343 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000344}
345
Guido van Rossum25ce5661997-08-02 03:10:38 +0000346/* Undo the effect of Py_Initialize().
347
348 Beware: if multiple interpreter and/or thread states exist, these
349 are not wiped out; only the current thread and interpreter state
350 are deleted. But since everything else is deleted, those other
351 interpreter and thread states should no longer be used.
352
353 (XXX We should do better, e.g. wipe out all interpreters and
354 threads.)
355
356 Locking: as above.
357
358*/
359
360void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000361Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000362{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000363 PyInterpreterState *interp;
364 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000365
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000366 if (!initialized)
367 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000368
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000369 wait_for_thread_shutdown();
Antoine Pitroucefb3162009-10-20 22:08:36 +0000370
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000371 /* The interpreter is still entirely intact at this point, and the
372 * exit funcs may be relying on that. In particular, if some thread
373 * or exit func is still waiting to do an import, the import machinery
374 * expects Py_IsInitialized() to return true. So don't say the
375 * interpreter is uninitialized until after the exit funcs have run.
376 * Note that Threading.py uses an exit func to do a join on all the
377 * threads created thru it, so this also protects pending imports in
378 * the threads created via Threading.
379 */
380 call_py_exitfuncs();
381 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000382
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000383 /* Flush stdout+stderr */
384 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000385
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000386 /* Get current thread state and interpreter pointer */
387 tstate = PyThreadState_GET();
388 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000389
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000390 /* Disable signal handling */
391 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000392
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000393 /* Clear type lookup cache */
394 PyType_ClearCache();
Christian Heimes26855632008-01-27 23:50:43 +0000395
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000396 /* Collect garbage. This may call finalizers; it's nice to call these
397 * before all modules are destroyed.
398 * XXX If a __del__ or weakref callback is triggered here, and tries to
399 * XXX import a module, bad things can happen, because Python no
400 * XXX longer believes it's initialized.
401 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
402 * XXX is easy to provoke that way. I've also seen, e.g.,
403 * XXX Exception exceptions.ImportError: 'No module named sha'
404 * XXX in <function callback at 0x008F5718> ignored
405 * XXX but I'm unclear on exactly how that one happens. In any case,
406 * XXX I haven't seen a real-life report of either of these.
407 */
408 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409#ifdef COUNT_ALLOCS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000410 /* With COUNT_ALLOCS, it helps to run GC multiple times:
411 each collection might release some types from the type
412 list, so they become garbage. */
413 while (PyGC_Collect() > 0)
414 /* nothing */;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000415#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000416
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000417 /* Destroy all modules */
418 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000419
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000420 /* Flush stdout+stderr (again, in case more was printed) */
421 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000422
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000423 /* Collect final garbage. This disposes of cycles created by
424 * new-style class definitions, for example.
425 * XXX This is disabled because it caused too many problems. If
426 * XXX a __del__ or weakref callback triggers here, Python code has
427 * XXX a hard time running, because even the sys module has been
428 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
429 * XXX One symptom is a sequence of information-free messages
430 * XXX coming from threads (if a __del__ or callback is invoked,
431 * XXX other threads can execute too, and any exception they encounter
432 * XXX triggers a comedy of errors as subsystem after subsystem
433 * XXX fails to find what it *expects* to find in sys to help report
434 * XXX the exception and consequent unexpected failures). I've also
435 * XXX seen segfaults then, after adding print statements to the
436 * XXX Python code getting called.
437 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000438#if 0
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000439 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000440#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000441
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000442 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
443 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000444
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000445 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000446#ifdef COUNT_ALLOCS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000447 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000448#endif
449
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000450 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000451
Tim Peters9cf25ce2003-04-17 15:21:01 +0000452#ifdef Py_TRACE_REFS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000453 /* Display all objects still alive -- this can invoke arbitrary
454 * __repr__ overrides, so requires a mostly-intact interpreter.
455 * Alas, a lot of stuff may still be alive now that will be cleaned
456 * up later.
457 */
458 if (Py_GETENV("PYTHONDUMPREFS"))
459 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000460#endif /* Py_TRACE_REFS */
461
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000462 /* Clear interpreter state */
463 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000464
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000465 /* Now we decref the exception classes. After this point nothing
466 can raise an exception. That's okay, because each Fini() method
467 below has been checked to make sure no exceptions are ever
468 raised.
469 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000470
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000471 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000472
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000473 /* Cleanup auto-thread-state */
Christian Heimes7d2ff882007-11-30 14:35:04 +0000474#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000475 _PyGILState_Fini();
Christian Heimes7d2ff882007-11-30 14:35:04 +0000476#endif /* WITH_THREAD */
477
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000478 /* Delete current thread */
479 PyThreadState_Swap(NULL);
480 PyInterpreterState_Delete(interp);
Barry Warsawf242aa02000-05-25 23:09:49 +0000481
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000482 /* Sundry finalizers */
483 PyMethod_Fini();
484 PyFrame_Fini();
485 PyCFunction_Fini();
486 PyTuple_Fini();
487 PyList_Fini();
488 PySet_Fini();
489 PyBytes_Fini();
490 PyByteArray_Fini();
491 PyLong_Fini();
492 PyFloat_Fini();
493 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000494
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000495 /* Cleanup Unicode implementation */
496 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000497
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000498 /* reset file system default encoding */
499 if (!Py_HasFileSystemDefaultEncoding) {
500 free((char*)Py_FileSystemDefaultEncoding);
501 Py_FileSystemDefaultEncoding = NULL;
502 }
Christian Heimesc8967002007-11-30 10:18:26 +0000503
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000504 /* XXX Still allocated:
505 - various static ad-hoc pointers to interned strings
506 - int and float free list blocks
507 - whatever various modules and libraries allocate
508 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000509
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000510 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000511
Tim Peters269b2a62003-04-17 19:52:29 +0000512#ifdef Py_TRACE_REFS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000513 /* Display addresses (& refcnts) of all objects still alive.
514 * An address can be used to find the repr of the object, printed
515 * above by _Py_PrintReferences.
516 */
517 if (Py_GETENV("PYTHONDUMPREFS"))
518 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000519#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000520#ifdef PYMALLOC_DEBUG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000521 if (Py_GETENV("PYTHONMALLOCSTATS"))
522 _PyObject_DebugMallocStats();
Tim Peters0e871182002-04-13 08:29:14 +0000523#endif
524
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000525 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000526}
527
528/* Create and initialize a new interpreter and thread, and return the
529 new thread. This requires that Py_Initialize() has been called
530 first.
531
532 Unsuccessful initialization yields a NULL pointer. Note that *no*
533 exception information is available even in this case -- the
534 exception information is held in the thread, and there is no
535 thread.
536
537 Locking: as above.
538
539*/
540
541PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000542Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000543{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000544 PyInterpreterState *interp;
545 PyThreadState *tstate, *save_tstate;
546 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000547
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000548 if (!initialized)
549 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000550
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000551 interp = PyInterpreterState_New();
552 if (interp == NULL)
553 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000554
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000555 tstate = PyThreadState_New(interp);
556 if (tstate == NULL) {
557 PyInterpreterState_Delete(interp);
558 return NULL;
559 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000560
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000561 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000562
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000563 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000564
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000565 interp->modules = PyDict_New();
566 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000567
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000568 bimod = _PyImport_FindExtension("builtins", "builtins");
569 if (bimod != NULL) {
570 interp->builtins = PyModule_GetDict(bimod);
571 if (interp->builtins == NULL)
572 goto handle_error;
573 Py_INCREF(interp->builtins);
574 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000575
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000576 /* initialize builtin exceptions */
577 _PyExc_Init();
Christian Heimes6a27efa2008-10-30 21:48:26 +0000578
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000579 sysmod = _PyImport_FindExtension("sys", "sys");
580 if (bimod != NULL && sysmod != NULL) {
581 PyObject *pstderr;
582 interp->sysdict = PyModule_GetDict(sysmod);
583 if (interp->sysdict == NULL)
584 goto handle_error;
585 Py_INCREF(interp->sysdict);
586 PySys_SetPath(Py_GetPath());
587 PyDict_SetItemString(interp->sysdict, "modules",
588 interp->modules);
589 /* Set up a preliminary stderr printer until we have enough
590 infrastructure for the io module in place. */
591 pstderr = PyFile_NewStdPrinter(fileno(stderr));
592 if (pstderr == NULL)
593 Py_FatalError("Py_Initialize: can't set preliminary stderr");
594 PySys_SetObject("stderr", pstderr);
595 PySys_SetObject("__stderr__", pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000596
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000597 _PyImportHooks_Init();
598 if (initstdio() < 0)
599 Py_FatalError(
600 "Py_Initialize: can't initialize sys standard streams");
601 initmain();
602 if (!Py_NoSiteFlag)
603 initsite();
604 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000605
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000606 if (!PyErr_Occurred())
607 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000608
Thomas Wouters89f507f2006-12-13 04:49:30 +0000609handle_error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000610 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000611
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000612 PyErr_Print();
613 PyThreadState_Clear(tstate);
614 PyThreadState_Swap(save_tstate);
615 PyThreadState_Delete(tstate);
616 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000617
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000618 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000619}
620
621/* Delete an interpreter and its last thread. This requires that the
622 given thread state is current, that the thread has no remaining
623 frames, and that it is its interpreter's only remaining thread.
624 It is a fatal error to violate these constraints.
625
626 (Py_Finalize() doesn't have these constraints -- it zaps
627 everything, regardless.)
628
629 Locking: as above.
630
631*/
632
633void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000634Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000635{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000636 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000637
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000638 if (tstate != PyThreadState_GET())
639 Py_FatalError("Py_EndInterpreter: thread is not current");
640 if (tstate->frame != NULL)
641 Py_FatalError("Py_EndInterpreter: thread still has a frame");
642 if (tstate != interp->tstate_head || tstate->next != NULL)
643 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000644
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000645 PyImport_Cleanup();
646 PyInterpreterState_Clear(interp);
647 PyThreadState_Swap(NULL);
648 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000649}
650
Martin v. Löwis790465f2008-04-05 20:41:37 +0000651static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000652
653void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000654Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000655{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000656 if (pn && *pn)
657 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000658}
659
Martin v. Löwis790465f2008-04-05 20:41:37 +0000660wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000661Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000662{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000663 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000664}
665
Martin v. Löwis790465f2008-04-05 20:41:37 +0000666static wchar_t *default_home = NULL;
667static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000668
669void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000670Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000671{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000672 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000673}
674
Martin v. Löwis790465f2008-04-05 20:41:37 +0000675wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000676Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000677{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000678 wchar_t *home = default_home;
679 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
680 char* chome = Py_GETENV("PYTHONHOME");
681 if (chome) {
682 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
683 if (r != (size_t)-1 && r <= PATH_MAX)
684 home = env_home;
685 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000686
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000687 }
688 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000689}
690
Guido van Rossum6135a871995-01-09 17:53:26 +0000691/* Create __main__ module */
692
693static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000694initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000695{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000696 PyObject *m, *d;
697 m = PyImport_AddModule("__main__");
698 if (m == NULL)
699 Py_FatalError("can't create __main__ module");
700 d = PyModule_GetDict(m);
701 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
702 PyObject *bimod = PyImport_ImportModule("builtins");
703 if (bimod == NULL ||
704 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
705 Py_FatalError("can't add __builtins__ to __main__");
706 Py_DECREF(bimod);
707 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000708}
709
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000710/* Import the site module (not into __main__ though) */
711
712static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000713initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000714{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000715 PyObject *m, *f;
716 m = PyImport_ImportModule("site");
717 if (m == NULL) {
718 f = PySys_GetObject("stderr");
719 if (f == NULL || f == Py_None)
720 return;
721 if (Py_VerboseFlag) {
722 PyFile_WriteString(
723 "'import site' failed; traceback:\n", f);
724 PyErr_Print();
725 }
726 else {
727 PyFile_WriteString(
728 "'import site' failed; use -v for traceback\n", f);
729 PyErr_Clear();
730 }
731 }
732 else {
733 Py_DECREF(m);
734 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000735}
736
Antoine Pitrou05608432009-01-09 18:53:14 +0000737static PyObject*
738create_stdio(PyObject* io,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000739 int fd, int write_mode, char* name,
740 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +0000741{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000742 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
743 const char* mode;
744 PyObject *line_buffering;
745 int buffering, isatty;
Antoine Pitrou05608432009-01-09 18:53:14 +0000746
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000747 /* stdin is always opened in buffered mode, first because it shouldn't
748 make a difference in common use cases, second because TextIOWrapper
749 depends on the presence of a read1() method which only exists on
750 buffered streams.
751 */
752 if (Py_UnbufferedStdioFlag && write_mode)
753 buffering = 0;
754 else
755 buffering = -1;
756 if (write_mode)
757 mode = "wb";
758 else
759 mode = "rb";
760 buf = PyObject_CallMethod(io, "open", "isiOOOi",
761 fd, mode, buffering,
762 Py_None, Py_None, Py_None, 0);
763 if (buf == NULL)
764 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000765
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000766 if (buffering) {
767 raw = PyObject_GetAttrString(buf, "raw");
768 if (raw == NULL)
769 goto error;
770 }
771 else {
772 raw = buf;
773 Py_INCREF(raw);
774 }
Antoine Pitrou05608432009-01-09 18:53:14 +0000775
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000776 text = PyUnicode_FromString(name);
777 if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0)
778 goto error;
779 res = PyObject_CallMethod(raw, "isatty", "");
780 if (res == NULL)
781 goto error;
782 isatty = PyObject_IsTrue(res);
783 Py_DECREF(res);
784 if (isatty == -1)
785 goto error;
786 if (isatty || Py_UnbufferedStdioFlag)
787 line_buffering = Py_True;
788 else
789 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +0000790
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000791 Py_CLEAR(raw);
792 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +0000793
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000794 stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO",
795 buf, encoding, errors,
796 "\n", line_buffering);
797 Py_CLEAR(buf);
798 if (stream == NULL)
799 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000800
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000801 if (write_mode)
802 mode = "w";
803 else
804 mode = "r";
805 text = PyUnicode_FromString(mode);
806 if (!text || PyObject_SetAttrString(stream, "mode", text) < 0)
807 goto error;
808 Py_CLEAR(text);
809 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +0000810
811error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000812 Py_XDECREF(buf);
813 Py_XDECREF(stream);
814 Py_XDECREF(text);
815 Py_XDECREF(raw);
816 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +0000817}
818
Georg Brandl1a3284e2007-12-02 09:40:06 +0000819/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000820static int
821initstdio(void)
822{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000823 PyObject *iomod = NULL, *wrapper;
824 PyObject *bimod = NULL;
825 PyObject *m;
826 PyObject *std = NULL;
827 int status = 0, fd;
828 PyObject * encoding_attr;
829 char *encoding = NULL, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000830
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000831 /* Hack to avoid a nasty recursion issue when Python is invoked
832 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
833 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
834 goto error;
835 }
836 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000837
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000838 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
839 goto error;
840 }
841 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000842
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000843 if (!(bimod = PyImport_ImportModule("builtins"))) {
844 goto error;
845 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000846
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000847 if (!(iomod = PyImport_ImportModule("io"))) {
848 goto error;
849 }
850 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
851 goto error;
852 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000853
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000854 /* Set builtins.open */
855 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
856 goto error;
857 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000858
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000859 encoding = Py_GETENV("PYTHONIOENCODING");
860 errors = NULL;
861 if (encoding) {
862 encoding = strdup(encoding);
863 errors = strchr(encoding, ':');
864 if (errors) {
865 *errors = '\0';
866 errors++;
867 }
868 }
Martin v. Löwis0f599892008-06-02 11:13:03 +0000869
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000870 /* Set sys.stdin */
871 fd = fileno(stdin);
872 /* Under some conditions stdin, stdout and stderr may not be connected
873 * and fileno() may point to an invalid file descriptor. For example
874 * GUI apps don't have valid standard streams by default.
875 */
876 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000877#ifdef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000878 std = Py_None;
879 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000880#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000881 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000882#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000883 }
884 else {
885 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
886 if (std == NULL)
887 goto error;
888 } /* if (fd < 0) */
889 PySys_SetObject("__stdin__", std);
890 PySys_SetObject("stdin", std);
891 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000892
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000893 /* Set sys.stdout */
894 fd = fileno(stdout);
895 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000896#ifdef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 std = Py_None;
898 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000899#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000900 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000901#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000902 }
903 else {
904 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
905 if (std == NULL)
906 goto error;
907 } /* if (fd < 0) */
908 PySys_SetObject("__stdout__", std);
909 PySys_SetObject("stdout", std);
910 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000911
Guido van Rossum98297ee2007-11-06 21:34:58 +0000912#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000913 /* Set sys.stderr, replaces the preliminary stderr */
914 fd = fileno(stderr);
915 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000916#ifdef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000917 std = Py_None;
918 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000919#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000920 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000921#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000922 }
923 else {
924 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
925 if (std == NULL)
926 goto error;
927 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +0000928
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000929 /* Same as hack above, pre-import stderr's codec to avoid recursion
930 when import.c tries to write to stderr in verbose mode. */
931 encoding_attr = PyObject_GetAttrString(std, "encoding");
932 if (encoding_attr != NULL) {
933 const char * encoding;
934 encoding = _PyUnicode_AsString(encoding_attr);
935 if (encoding != NULL) {
936 _PyCodec_Lookup(encoding);
937 }
938 }
939 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +0000940
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000941 PySys_SetObject("__stderr__", std);
942 PySys_SetObject("stderr", std);
943 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000944#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000945
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000946 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000947 error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000948 status = -1;
949 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000950
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000951 if (encoding)
952 free(encoding);
953 Py_XDECREF(bimod);
954 Py_XDECREF(iomod);
955 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000956}
957
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000958/* Parse input from a file and execute it */
959
960int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000961PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000962 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000963{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000964 if (filename == NULL)
965 filename = "???";
966 if (Py_FdIsInteractive(fp, filename)) {
967 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
968 if (closeit)
969 fclose(fp);
970 return err;
971 }
972 else
973 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000974}
975
976int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000977PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000978{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000979 PyObject *v;
980 int ret;
981 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000982
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000983 if (flags == NULL) {
984 flags = &local_flags;
985 local_flags.cf_flags = 0;
986 }
987 v = PySys_GetObject("ps1");
988 if (v == NULL) {
989 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
990 Py_XDECREF(v);
991 }
992 v = PySys_GetObject("ps2");
993 if (v == NULL) {
994 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
995 Py_XDECREF(v);
996 }
997 for (;;) {
998 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
999 PRINT_TOTAL_REFS();
1000 if (ret == E_EOF)
1001 return 0;
1002 /*
1003 if (ret == E_NOMEM)
1004 return -1;
1005 */
1006 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001007}
1008
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001009/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001010static int PARSER_FLAGS(PyCompilerFlags *flags)
1011{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001012 int parser_flags = 0;
1013 if (!flags)
1014 return 0;
1015 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1016 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1017 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1018 parser_flags |= PyPARSE_IGNORE_COOKIE;
1019 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1020 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1021 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001022}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001023
Thomas Wouters89f507f2006-12-13 04:49:30 +00001024#if 0
1025/* Keep an example of flags with future keyword support. */
1026#define PARSER_FLAGS(flags) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001027 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1028 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1029 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1030 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001031#endif
1032
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001033int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001034PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001035{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001036 PyObject *m, *d, *v, *w, *oenc = NULL;
1037 mod_ty mod;
1038 PyArena *arena;
1039 char *ps1 = "", *ps2 = "", *enc = NULL;
1040 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +00001041
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001042 if (fp == stdin) {
1043 /* Fetch encoding from sys.stdin */
1044 v = PySys_GetObject("stdin");
1045 if (v == NULL || v == Py_None)
1046 return -1;
1047 oenc = PyObject_GetAttrString(v, "encoding");
1048 if (!oenc)
1049 return -1;
1050 enc = _PyUnicode_AsString(oenc);
1051 }
1052 v = PySys_GetObject("ps1");
1053 if (v != NULL) {
1054 v = PyObject_Str(v);
1055 if (v == NULL)
1056 PyErr_Clear();
1057 else if (PyUnicode_Check(v))
1058 ps1 = _PyUnicode_AsString(v);
1059 }
1060 w = PySys_GetObject("ps2");
1061 if (w != NULL) {
1062 w = PyObject_Str(w);
1063 if (w == NULL)
1064 PyErr_Clear();
1065 else if (PyUnicode_Check(w))
1066 ps2 = _PyUnicode_AsString(w);
1067 }
1068 arena = PyArena_New();
1069 if (arena == NULL) {
1070 Py_XDECREF(v);
1071 Py_XDECREF(w);
1072 Py_XDECREF(oenc);
1073 return -1;
1074 }
1075 mod = PyParser_ASTFromFile(fp, filename, enc,
1076 Py_single_input, ps1, ps2,
1077 flags, &errcode, arena);
1078 Py_XDECREF(v);
1079 Py_XDECREF(w);
1080 Py_XDECREF(oenc);
1081 if (mod == NULL) {
1082 PyArena_Free(arena);
1083 if (errcode == E_EOF) {
1084 PyErr_Clear();
1085 return E_EOF;
1086 }
1087 PyErr_Print();
1088 return -1;
1089 }
1090 m = PyImport_AddModule("__main__");
1091 if (m == NULL) {
1092 PyArena_Free(arena);
1093 return -1;
1094 }
1095 d = PyModule_GetDict(m);
1096 v = run_mod(mod, filename, d, d, flags, arena);
1097 PyArena_Free(arena);
1098 flush_io();
1099 if (v == NULL) {
1100 PyErr_Print();
1101 return -1;
1102 }
1103 Py_DECREF(v);
1104 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001105}
1106
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001107/* Check whether a file maybe a pyc file: Look at the extension,
1108 the file type, and, if we may close it, at the first few bytes. */
1109
1110static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001111maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001112{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001113 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1114 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001115
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001116 /* Only look into the file if we are allowed to close it, since
1117 it then should also be seekable. */
1118 if (closeit) {
1119 /* Read only two bytes of the magic. If the file was opened in
1120 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1121 be read as they are on disk. */
1122 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1123 unsigned char buf[2];
1124 /* Mess: In case of -x, the stream is NOT at its start now,
1125 and ungetc() was used to push back the first newline,
1126 which makes the current stream position formally undefined,
1127 and a x-platform nightmare.
1128 Unfortunately, we have no direct way to know whether -x
1129 was specified. So we use a terrible hack: if the current
1130 stream position is not 0, we assume -x was specified, and
1131 give up. Bug 132850 on SourceForge spells out the
1132 hopelessness of trying anything else (fseek and ftell
1133 don't work predictably x-platform for text-mode files).
1134 */
1135 int ispyc = 0;
1136 if (ftell(fp) == 0) {
1137 if (fread(buf, 1, 2, fp) == 2 &&
1138 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1139 ispyc = 1;
1140 rewind(fp);
1141 }
1142 return ispyc;
1143 }
1144 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001145}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001146
Guido van Rossum0df002c2000-08-27 19:21:52 +00001147int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001148PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001149 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001150{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001151 PyObject *m, *d, *v;
1152 const char *ext;
1153 int set_file_name = 0, ret, len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001154
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001155 m = PyImport_AddModule("__main__");
1156 if (m == NULL)
1157 return -1;
1158 d = PyModule_GetDict(m);
1159 if (PyDict_GetItemString(d, "__file__") == NULL) {
1160 PyObject *f;
1161 f = PyUnicode_DecodeFSDefault(filename);
1162 if (f == NULL)
1163 return -1;
1164 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1165 Py_DECREF(f);
1166 return -1;
1167 }
1168 set_file_name = 1;
1169 Py_DECREF(f);
1170 }
1171 len = strlen(filename);
1172 ext = filename + len - (len > 4 ? 4 : 0);
1173 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1174 /* Try to run a pyc file. First, re-open in binary */
1175 if (closeit)
1176 fclose(fp);
1177 if ((fp = fopen(filename, "rb")) == NULL) {
1178 fprintf(stderr, "python: Can't reopen .pyc file\n");
1179 ret = -1;
1180 goto done;
1181 }
1182 /* Turn on optimization if a .pyo file is given */
1183 if (strcmp(ext, ".pyo") == 0)
1184 Py_OptimizeFlag = 1;
1185 v = run_pyc_file(fp, filename, d, d, flags);
1186 } else {
1187 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1188 closeit, flags);
1189 }
1190 flush_io();
1191 if (v == NULL) {
1192 PyErr_Print();
1193 ret = -1;
1194 goto done;
1195 }
1196 Py_DECREF(v);
1197 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001198 done:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001199 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1200 PyErr_Clear();
1201 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001202}
1203
1204int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001205PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001206{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001207 PyObject *m, *d, *v;
1208 m = PyImport_AddModule("__main__");
1209 if (m == NULL)
1210 return -1;
1211 d = PyModule_GetDict(m);
1212 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1213 if (v == NULL) {
1214 PyErr_Print();
1215 return -1;
1216 }
1217 Py_DECREF(v);
1218 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001219}
1220
Barry Warsaw035574d1997-08-29 22:07:17 +00001221static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001222parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001223 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001224{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001225 long hold;
1226 PyObject *v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001227
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001228 /* old style errors */
1229 if (PyTuple_Check(err))
1230 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1231 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001232
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001233 /* new style errors. `err' is an instance */
Barry Warsaw035574d1997-08-29 22:07:17 +00001234
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001235 if (! (v = PyObject_GetAttrString(err, "msg")))
1236 goto finally;
1237 *message = v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001238
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001239 if (!(v = PyObject_GetAttrString(err, "filename")))
1240 goto finally;
1241 if (v == Py_None)
1242 *filename = NULL;
1243 else if (! (*filename = _PyUnicode_AsString(v)))
1244 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001245
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001246 Py_DECREF(v);
1247 if (!(v = PyObject_GetAttrString(err, "lineno")))
1248 goto finally;
1249 hold = PyLong_AsLong(v);
1250 Py_DECREF(v);
1251 v = NULL;
1252 if (hold < 0 && PyErr_Occurred())
1253 goto finally;
1254 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001255
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001256 if (!(v = PyObject_GetAttrString(err, "offset")))
1257 goto finally;
1258 if (v == Py_None) {
1259 *offset = -1;
1260 Py_DECREF(v);
1261 v = NULL;
1262 } else {
1263 hold = PyLong_AsLong(v);
1264 Py_DECREF(v);
1265 v = NULL;
1266 if (hold < 0 && PyErr_Occurred())
1267 goto finally;
1268 *offset = (int)hold;
1269 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001270
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001271 if (!(v = PyObject_GetAttrString(err, "text")))
1272 goto finally;
1273 if (v == Py_None)
1274 *text = NULL;
1275 else if (!PyUnicode_Check(v) ||
1276 !(*text = _PyUnicode_AsString(v)))
1277 goto finally;
1278 Py_DECREF(v);
1279 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001280
1281finally:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001282 Py_XDECREF(v);
1283 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001284}
1285
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001286void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001287PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001288{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001289 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001290}
1291
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001292static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001293print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001294{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001295 char *nl;
1296 if (offset >= 0) {
1297 if (offset > 0 && offset == (int)strlen(text))
1298 offset--;
1299 for (;;) {
1300 nl = strchr(text, '\n');
1301 if (nl == NULL || nl-text >= offset)
1302 break;
1303 offset -= (int)(nl+1-text);
1304 text = nl+1;
1305 }
1306 while (*text == ' ' || *text == '\t') {
1307 text++;
1308 offset--;
1309 }
1310 }
1311 PyFile_WriteString(" ", f);
1312 PyFile_WriteString(text, f);
1313 if (*text == '\0' || text[strlen(text)-1] != '\n')
1314 PyFile_WriteString("\n", f);
1315 if (offset == -1)
1316 return;
1317 PyFile_WriteString(" ", f);
1318 offset--;
1319 while (offset > 0) {
1320 PyFile_WriteString(" ", f);
1321 offset--;
1322 }
1323 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001324}
1325
Guido van Rossum66e8e862001-03-23 17:54:43 +00001326static void
1327handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001328{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001329 PyObject *exception, *value, *tb;
1330 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001331
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001332 if (Py_InspectFlag)
1333 /* Don't exit if -i flag was given. This flag is set to 0
1334 * when entering interactive mode for inspecting. */
1335 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001336
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001337 PyErr_Fetch(&exception, &value, &tb);
1338 fflush(stdout);
1339 if (value == NULL || value == Py_None)
1340 goto done;
1341 if (PyExceptionInstance_Check(value)) {
1342 /* The error code should be in the `code' attribute. */
1343 PyObject *code = PyObject_GetAttrString(value, "code");
1344 if (code) {
1345 Py_DECREF(value);
1346 value = code;
1347 if (value == Py_None)
1348 goto done;
1349 }
1350 /* If we failed to dig out the 'code' attribute,
1351 just let the else clause below print the error. */
1352 }
1353 if (PyLong_Check(value))
1354 exitcode = (int)PyLong_AsLong(value);
1355 else {
1356 PyObject_Print(value, stderr, Py_PRINT_RAW);
1357 PySys_WriteStderr("\n");
1358 exitcode = 1;
1359 }
Tim Peterscf615b52003-04-19 18:47:02 +00001360 done:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001361 /* Restore and clear the exception info, in order to properly decref
1362 * the exception, value, and traceback. If we just exit instead,
1363 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1364 * some finalizers from running.
1365 */
1366 PyErr_Restore(exception, value, tb);
1367 PyErr_Clear();
1368 Py_Exit(exitcode);
1369 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001370}
1371
1372void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001373PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001374{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001375 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001376
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001377 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1378 handle_system_exit();
1379 }
1380 PyErr_Fetch(&exception, &v, &tb);
1381 if (exception == NULL)
1382 return;
1383 PyErr_NormalizeException(&exception, &v, &tb);
1384 if (tb == NULL) {
1385 tb = Py_None;
1386 Py_INCREF(tb);
1387 }
1388 PyException_SetTraceback(v, tb);
1389 if (exception == NULL)
1390 return;
1391 /* Now we know v != NULL too */
1392 if (set_sys_last_vars) {
1393 PySys_SetObject("last_type", exception);
1394 PySys_SetObject("last_value", v);
1395 PySys_SetObject("last_traceback", tb);
1396 }
1397 hook = PySys_GetObject("excepthook");
1398 if (hook) {
1399 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1400 PyObject *result = PyEval_CallObject(hook, args);
1401 if (result == NULL) {
1402 PyObject *exception2, *v2, *tb2;
1403 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1404 handle_system_exit();
1405 }
1406 PyErr_Fetch(&exception2, &v2, &tb2);
1407 PyErr_NormalizeException(&exception2, &v2, &tb2);
1408 /* It should not be possible for exception2 or v2
1409 to be NULL. However PyErr_Display() can't
1410 tolerate NULLs, so just be safe. */
1411 if (exception2 == NULL) {
1412 exception2 = Py_None;
1413 Py_INCREF(exception2);
1414 }
1415 if (v2 == NULL) {
1416 v2 = Py_None;
1417 Py_INCREF(v2);
1418 }
1419 fflush(stdout);
1420 PySys_WriteStderr("Error in sys.excepthook:\n");
1421 PyErr_Display(exception2, v2, tb2);
1422 PySys_WriteStderr("\nOriginal exception was:\n");
1423 PyErr_Display(exception, v, tb);
1424 Py_DECREF(exception2);
1425 Py_DECREF(v2);
1426 Py_XDECREF(tb2);
1427 }
1428 Py_XDECREF(result);
1429 Py_XDECREF(args);
1430 } else {
1431 PySys_WriteStderr("sys.excepthook is missing\n");
1432 PyErr_Display(exception, v, tb);
1433 }
1434 Py_XDECREF(exception);
1435 Py_XDECREF(v);
1436 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001437}
1438
Benjamin Petersone6528212008-07-15 15:32:09 +00001439static void
1440print_exception(PyObject *f, PyObject *value)
1441{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001442 int err = 0;
1443 PyObject *type, *tb;
Benjamin Petersone6528212008-07-15 15:32:09 +00001444
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001445 if (!PyExceptionInstance_Check(value)) {
1446 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1447 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1448 PyFile_WriteString(" found\n", f);
1449 return;
1450 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001451
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001452 Py_INCREF(value);
1453 fflush(stdout);
1454 type = (PyObject *) Py_TYPE(value);
1455 tb = PyException_GetTraceback(value);
1456 if (tb && tb != Py_None)
1457 err = PyTraceBack_Print(tb, f);
1458 if (err == 0 &&
1459 PyObject_HasAttrString(value, "print_file_and_line"))
1460 {
1461 PyObject *message;
1462 const char *filename, *text;
1463 int lineno, offset;
1464 if (!parse_syntax_error(value, &message, &filename,
1465 &lineno, &offset, &text))
1466 PyErr_Clear();
1467 else {
1468 char buf[10];
1469 PyFile_WriteString(" File \"", f);
1470 if (filename == NULL)
1471 PyFile_WriteString("<string>", f);
1472 else
1473 PyFile_WriteString(filename, f);
1474 PyFile_WriteString("\", line ", f);
1475 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1476 PyFile_WriteString(buf, f);
1477 PyFile_WriteString("\n", f);
1478 if (text != NULL)
1479 print_error_text(f, offset, text);
1480 Py_DECREF(value);
1481 value = message;
1482 /* Can't be bothered to check all those
1483 PyFile_WriteString() calls */
1484 if (PyErr_Occurred())
1485 err = -1;
1486 }
1487 }
1488 if (err) {
1489 /* Don't do anything else */
1490 }
1491 else {
1492 PyObject* moduleName;
1493 char* className;
1494 assert(PyExceptionClass_Check(type));
1495 className = PyExceptionClass_Name(type);
1496 if (className != NULL) {
1497 char *dot = strrchr(className, '.');
1498 if (dot != NULL)
1499 className = dot+1;
1500 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001501
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001502 moduleName = PyObject_GetAttrString(type, "__module__");
1503 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1504 {
1505 Py_DECREF(moduleName);
1506 err = PyFile_WriteString("<unknown>", f);
1507 }
1508 else {
1509 char* modstr = _PyUnicode_AsString(moduleName);
1510 if (modstr && strcmp(modstr, "builtins"))
1511 {
1512 err = PyFile_WriteString(modstr, f);
1513 err += PyFile_WriteString(".", f);
1514 }
1515 Py_DECREF(moduleName);
1516 }
1517 if (err == 0) {
1518 if (className == NULL)
1519 err = PyFile_WriteString("<unknown>", f);
1520 else
1521 err = PyFile_WriteString(className, f);
1522 }
1523 }
1524 if (err == 0 && (value != Py_None)) {
1525 PyObject *s = PyObject_Str(value);
1526 /* only print colon if the str() of the
1527 object is not the empty string
1528 */
1529 if (s == NULL)
1530 err = -1;
1531 else if (!PyUnicode_Check(s) ||
1532 PyUnicode_GetSize(s) != 0)
1533 err = PyFile_WriteString(": ", f);
1534 if (err == 0)
1535 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1536 Py_XDECREF(s);
1537 }
1538 /* try to write a newline in any case */
1539 err += PyFile_WriteString("\n", f);
1540 Py_XDECREF(tb);
1541 Py_DECREF(value);
1542 /* If an error happened here, don't show it.
1543 XXX This is wrong, but too many callers rely on this behavior. */
1544 if (err != 0)
1545 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001546}
1547
1548static const char *cause_message =
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001549 "\nThe above exception was the direct cause "
1550 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001551
1552static const char *context_message =
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001553 "\nDuring handling of the above exception, "
1554 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001555
1556static void
1557print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1558{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001559 int err = 0, res;
1560 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00001561
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001562 if (seen != NULL) {
1563 /* Exception chaining */
1564 if (PySet_Add(seen, value) == -1)
1565 PyErr_Clear();
1566 else if (PyExceptionInstance_Check(value)) {
1567 cause = PyException_GetCause(value);
1568 context = PyException_GetContext(value);
1569 if (cause) {
1570 res = PySet_Contains(seen, cause);
1571 if (res == -1)
1572 PyErr_Clear();
1573 if (res == 0) {
1574 print_exception_recursive(
1575 f, cause, seen);
1576 err |= PyFile_WriteString(
1577 cause_message, f);
1578 }
1579 }
1580 else if (context) {
1581 res = PySet_Contains(seen, context);
1582 if (res == -1)
1583 PyErr_Clear();
1584 if (res == 0) {
1585 print_exception_recursive(
1586 f, context, seen);
1587 err |= PyFile_WriteString(
1588 context_message, f);
1589 }
1590 }
1591 Py_XDECREF(context);
1592 Py_XDECREF(cause);
1593 }
1594 }
1595 print_exception(f, value);
1596 if (err != 0)
1597 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001598}
1599
Thomas Wouters477c8d52006-05-27 19:21:47 +00001600void
1601PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001602{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001603 PyObject *seen;
1604 PyObject *f = PySys_GetObject("stderr");
1605 if (f == Py_None) {
1606 /* pass */
1607 }
1608 else if (f == NULL) {
1609 _PyObject_Dump(value);
1610 fprintf(stderr, "lost sys.stderr\n");
1611 }
1612 else {
1613 /* We choose to ignore seen being possibly NULL, and report
1614 at least the main exception (it could be a MemoryError).
1615 */
1616 seen = PySet_New(NULL);
1617 if (seen == NULL)
1618 PyErr_Clear();
1619 print_exception_recursive(f, value, seen);
1620 Py_XDECREF(seen);
1621 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001622}
1623
Guido van Rossum82598051997-03-05 00:20:32 +00001624PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001625PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001626 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001627{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001628 PyObject *ret = NULL;
1629 mod_ty mod;
1630 PyArena *arena = PyArena_New();
1631 if (arena == NULL)
1632 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001633
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001634 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1635 if (mod != NULL)
1636 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1637 PyArena_Free(arena);
1638 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001639}
1640
1641PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001642PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001643 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001644{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001645 PyObject *ret;
1646 mod_ty mod;
1647 PyArena *arena = PyArena_New();
1648 if (arena == NULL)
1649 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001650
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001651 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1652 flags, NULL, arena);
1653 if (closeit)
1654 fclose(fp);
1655 if (mod == NULL) {
1656 PyArena_Free(arena);
1657 return NULL;
1658 }
1659 ret = run_mod(mod, filename, globals, locals, flags, arena);
1660 PyArena_Free(arena);
1661 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001662}
1663
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001664static void
1665flush_io(void)
1666{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001667 PyObject *f, *r;
1668 PyObject *type, *value, *traceback;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001669
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001670 /* Save the current exception */
1671 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001672
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001673 f = PySys_GetObject("stderr");
1674 if (f != NULL) {
1675 r = PyObject_CallMethod(f, "flush", "");
1676 if (r)
1677 Py_DECREF(r);
1678 else
1679 PyErr_Clear();
1680 }
1681 f = PySys_GetObject("stdout");
1682 if (f != NULL) {
1683 r = PyObject_CallMethod(f, "flush", "");
1684 if (r)
1685 Py_DECREF(r);
1686 else
1687 PyErr_Clear();
1688 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001689
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001690 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001691}
1692
Guido van Rossum82598051997-03-05 00:20:32 +00001693static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001695 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001696{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001697 PyCodeObject *co;
1698 PyObject *v;
1699 co = PyAST_Compile(mod, filename, flags, arena);
1700 if (co == NULL)
1701 return NULL;
1702 v = PyEval_EvalCode(co, globals, locals);
1703 Py_DECREF(co);
1704 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001705}
1706
Guido van Rossum82598051997-03-05 00:20:32 +00001707static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001708run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001709 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001710{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001711 PyCodeObject *co;
1712 PyObject *v;
1713 long magic;
1714 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001715
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001716 magic = PyMarshal_ReadLongFromFile(fp);
1717 if (magic != PyImport_GetMagicNumber()) {
1718 PyErr_SetString(PyExc_RuntimeError,
1719 "Bad magic number in .pyc file");
1720 return NULL;
1721 }
1722 (void) PyMarshal_ReadLongFromFile(fp);
1723 v = PyMarshal_ReadLastObjectFromFile(fp);
1724 fclose(fp);
1725 if (v == NULL || !PyCode_Check(v)) {
1726 Py_XDECREF(v);
1727 PyErr_SetString(PyExc_RuntimeError,
1728 "Bad code object in .pyc file");
1729 return NULL;
1730 }
1731 co = (PyCodeObject *)v;
1732 v = PyEval_EvalCode(co, globals, locals);
1733 if (v && flags)
1734 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1735 Py_DECREF(co);
1736 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001737}
1738
Guido van Rossum82598051997-03-05 00:20:32 +00001739PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001740Py_CompileStringFlags(const char *str, const char *filename, int start,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001741 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001742{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001743 PyCodeObject *co;
1744 mod_ty mod;
1745 PyArena *arena = PyArena_New();
1746 if (arena == NULL)
1747 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001748
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001749 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1750 if (mod == NULL) {
1751 PyArena_Free(arena);
1752 return NULL;
1753 }
1754 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1755 PyObject *result = PyAST_mod2obj(mod);
1756 PyArena_Free(arena);
1757 return result;
1758 }
1759 co = PyAST_Compile(mod, filename, flags, arena);
1760 PyArena_Free(arena);
1761 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001762}
1763
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001764struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001765Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001766{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001767 struct symtable *st;
1768 mod_ty mod;
1769 PyCompilerFlags flags;
1770 PyArena *arena = PyArena_New();
1771 if (arena == NULL)
1772 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001773
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001774 flags.cf_flags = 0;
1775 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1776 if (mod == NULL) {
1777 PyArena_Free(arena);
1778 return NULL;
1779 }
1780 st = PySymtable_Build(mod, filename, 0);
1781 PyArena_Free(arena);
1782 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001783}
1784
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785/* Preferred access to parser is through AST. */
1786mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001787PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001788 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001790 mod_ty mod;
1791 PyCompilerFlags localflags;
1792 perrdetail err;
1793 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001794
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001795 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1796 &_PyParser_Grammar, start, &err,
1797 &iflags);
1798 if (flags == NULL) {
1799 localflags.cf_flags = 0;
1800 flags = &localflags;
1801 }
1802 if (n) {
1803 flags->cf_flags |= iflags & PyCF_MASK;
1804 mod = PyAST_FromNode(n, flags, filename, arena);
1805 PyNode_Free(n);
1806 return mod;
1807 }
1808 else {
1809 err_input(&err);
1810 return NULL;
1811 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812}
1813
1814mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001815PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001816 int start, char *ps1,
1817 char *ps2, PyCompilerFlags *flags, int *errcode,
1818 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001820 mod_ty mod;
1821 PyCompilerFlags localflags;
1822 perrdetail err;
1823 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001824
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001825 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
1826 &_PyParser_Grammar,
1827 start, ps1, ps2, &err, &iflags);
1828 if (flags == NULL) {
1829 localflags.cf_flags = 0;
1830 flags = &localflags;
1831 }
1832 if (n) {
1833 flags->cf_flags |= iflags & PyCF_MASK;
1834 mod = PyAST_FromNode(n, flags, filename, arena);
1835 PyNode_Free(n);
1836 return mod;
1837 }
1838 else {
1839 err_input(&err);
1840 if (errcode)
1841 *errcode = err.error;
1842 return NULL;
1843 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844}
1845
Guido van Rossuma110aa61994-08-29 12:50:44 +00001846/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001847
Guido van Rossuma110aa61994-08-29 12:50:44 +00001848node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001849PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001850{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001851 perrdetail err;
1852 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1853 &_PyParser_Grammar,
1854 start, NULL, NULL, &err, flags);
1855 if (n == NULL)
1856 err_input(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001857
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001858 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001859}
1860
Guido van Rossuma110aa61994-08-29 12:50:44 +00001861/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001862
Guido van Rossuma110aa61994-08-29 12:50:44 +00001863node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001864PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001865{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001866 perrdetail err;
1867 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1868 start, &err, flags);
1869 if (n == NULL)
1870 err_input(&err);
1871 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001872}
1873
1874node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001875PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001876 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001877{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001878 perrdetail err;
1879 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1880 &_PyParser_Grammar, start, &err, flags);
1881 if (n == NULL)
1882 err_input(&err);
1883 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001884}
1885
1886node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001887PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001888{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001889 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001890}
1891
Guido van Rossum66ebd912003-04-17 16:02:26 +00001892/* May want to move a more generalized form of this to parsetok.c or
1893 even parser modules. */
1894
1895void
1896PyParser_SetError(perrdetail *err)
1897{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001898 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00001899}
1900
Guido van Rossuma110aa61994-08-29 12:50:44 +00001901/* Set the error appropriate to the given input error code (see errcode.h) */
1902
1903static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001904err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001905{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001906 PyObject *v, *w, *errtype, *errtext;
1907 PyObject* u = NULL;
1908 char *msg = NULL;
1909 errtype = PyExc_SyntaxError;
1910 switch (err->error) {
1911 case E_ERROR:
1912 return;
1913 case E_SYNTAX:
1914 errtype = PyExc_IndentationError;
1915 if (err->expected == INDENT)
1916 msg = "expected an indented block";
1917 else if (err->token == INDENT)
1918 msg = "unexpected indent";
1919 else if (err->token == DEDENT)
1920 msg = "unexpected unindent";
1921 else {
1922 errtype = PyExc_SyntaxError;
1923 msg = "invalid syntax";
1924 }
1925 break;
1926 case E_TOKEN:
1927 msg = "invalid token";
1928 break;
1929 case E_EOFS:
1930 msg = "EOF while scanning triple-quoted string literal";
1931 break;
1932 case E_EOLS:
1933 msg = "EOL while scanning string literal";
1934 break;
1935 case E_INTR:
1936 if (!PyErr_Occurred())
1937 PyErr_SetNone(PyExc_KeyboardInterrupt);
1938 goto cleanup;
1939 case E_NOMEM:
1940 PyErr_NoMemory();
1941 goto cleanup;
1942 case E_EOF:
1943 msg = "unexpected EOF while parsing";
1944 break;
1945 case E_TABSPACE:
1946 errtype = PyExc_TabError;
1947 msg = "inconsistent use of tabs and spaces in indentation";
1948 break;
1949 case E_OVERFLOW:
1950 msg = "expression too long";
1951 break;
1952 case E_DEDENT:
1953 errtype = PyExc_IndentationError;
1954 msg = "unindent does not match any outer indentation level";
1955 break;
1956 case E_TOODEEP:
1957 errtype = PyExc_IndentationError;
1958 msg = "too many levels of indentation";
1959 break;
1960 case E_DECODE: {
1961 PyObject *type, *value, *tb;
1962 PyErr_Fetch(&type, &value, &tb);
1963 if (value != NULL) {
1964 u = PyObject_Str(value);
1965 if (u != NULL) {
1966 msg = _PyUnicode_AsString(u);
1967 }
1968 }
1969 if (msg == NULL)
1970 msg = "unknown decode error";
1971 Py_XDECREF(type);
1972 Py_XDECREF(value);
1973 Py_XDECREF(tb);
1974 break;
1975 }
1976 case E_LINECONT:
1977 msg = "unexpected character after line continuation character";
1978 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00001979
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001980 case E_IDENTIFIER:
1981 msg = "invalid character in identifier";
1982 break;
1983 default:
1984 fprintf(stderr, "error=%d\n", err->error);
1985 msg = "unknown parsing error";
1986 break;
1987 }
1988 /* err->text may not be UTF-8 in case of decoding errors.
1989 Explicitly convert to an object. */
1990 if (!err->text) {
1991 errtext = Py_None;
1992 Py_INCREF(Py_None);
1993 } else {
1994 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
1995 "replace");
1996 }
1997 v = Py_BuildValue("(ziiN)", err->filename,
1998 err->lineno, err->offset, errtext);
1999 w = NULL;
2000 if (v != NULL)
2001 w = Py_BuildValue("(sO)", msg, v);
2002 Py_XDECREF(u);
2003 Py_XDECREF(v);
2004 PyErr_SetObject(errtype, w);
2005 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002006cleanup:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002007 if (err->text != NULL) {
2008 PyObject_FREE(err->text);
2009 err->text = NULL;
2010 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002011}
2012
2013/* Print fatal error message and abort */
2014
2015void
Tim Peters7c321a82002-07-09 02:57:01 +00002016Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002017{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002018 fprintf(stderr, "Fatal Python error: %s\n", msg);
2019 fflush(stderr); /* it helps in Windows debug build */
2020 if (PyErr_Occurred()) {
2021 PyErr_Print();
2022 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002023#ifdef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002024 {
2025 size_t len = strlen(msg);
2026 WCHAR* buffer;
2027 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002028
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002029 /* Convert the message to wchar_t. This uses a simple one-to-one
2030 conversion, assuming that the this error message actually uses ASCII
2031 only. If this ceases to be true, we will have to convert. */
2032 buffer = alloca( (len+1) * (sizeof *buffer));
2033 for( i=0; i<=len; ++i)
2034 buffer[i] = msg[i];
2035 OutputDebugStringW(L"Fatal Python error: ");
2036 OutputDebugStringW(buffer);
2037 OutputDebugStringW(L"\n");
2038 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002039#ifdef _DEBUG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002040 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002041#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002042#endif /* MS_WINDOWS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002043 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002044}
2045
2046/* Clean up and exit */
2047
Guido van Rossuma110aa61994-08-29 12:50:44 +00002048#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002049#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002050#endif
2051
Collin Winter670e6922007-03-21 02:57:17 +00002052static void (*pyexitfunc)(void) = NULL;
2053/* For the atexit module. */
2054void _Py_PyAtExit(void (*func)(void))
2055{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002056 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002057}
2058
2059static void
2060call_py_exitfuncs(void)
2061{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002062 if (pyexitfunc == NULL)
2063 return;
Collin Winter670e6922007-03-21 02:57:17 +00002064
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002065 (*pyexitfunc)();
2066 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002067}
2068
Antoine Pitroucefb3162009-10-20 22:08:36 +00002069/* Wait until threading._shutdown completes, provided
2070 the threading module was imported in the first place.
2071 The shutdown routine will wait until all non-daemon
2072 "threading" threads have completed. */
2073static void
2074wait_for_thread_shutdown(void)
2075{
2076#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002077 PyObject *result;
2078 PyThreadState *tstate = PyThreadState_GET();
2079 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2080 "threading");
2081 if (threading == NULL) {
2082 /* threading not imported */
2083 PyErr_Clear();
2084 return;
2085 }
2086 result = PyObject_CallMethod(threading, "_shutdown", "");
2087 if (result == NULL) {
2088 PyErr_WriteUnraisable(threading);
2089 }
2090 else {
2091 Py_DECREF(result);
2092 }
2093 Py_DECREF(threading);
Antoine Pitroucefb3162009-10-20 22:08:36 +00002094#endif
2095}
2096
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002097#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002098static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002099static int nexitfuncs = 0;
2100
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002101int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002102{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002103 if (nexitfuncs >= NEXITFUNCS)
2104 return -1;
2105 exitfuncs[nexitfuncs++] = func;
2106 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002107}
2108
Guido van Rossumcc283f51997-08-05 02:22:03 +00002109static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002110call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002111{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002112 while (nexitfuncs > 0)
2113 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002114
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002115 fflush(stdout);
2116 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002117}
2118
2119void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002120Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002121{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002122 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002123
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002124 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002125}
2126
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002127static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002128initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002129{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002130#ifdef SIGPIPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002131 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002132#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002133#ifdef SIGXFZ
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002134 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002135#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002136#ifdef SIGXFSZ
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002137 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002138#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002139 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002140}
2141
Guido van Rossum7433b121997-02-14 19:45:36 +00002142
2143/*
2144 * The file descriptor fd is considered ``interactive'' if either
2145 * a) isatty(fd) is TRUE, or
2146 * b) the -i flag was given, and the filename associated with
2147 * the descriptor is NULL or "<stdin>" or "???".
2148 */
2149int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002150Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002151{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002152 if (isatty((int)fileno(fp)))
2153 return 1;
2154 if (!Py_InteractiveFlag)
2155 return 0;
2156 return (filename == NULL) ||
2157 (strcmp(filename, "<stdin>") == 0) ||
2158 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002159}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002160
2161
Tim Petersd08e3822003-04-17 15:24:21 +00002162#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002163#if defined(WIN32) && defined(_MSC_VER)
2164
2165/* Stack checking for Microsoft C */
2166
2167#include <malloc.h>
2168#include <excpt.h>
2169
Fred Drakee8de31c2000-08-31 05:38:39 +00002170/*
2171 * Return non-zero when we run out of memory on the stack; zero otherwise.
2172 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002173int
Fred Drake399739f2000-08-31 05:52:44 +00002174PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002175{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002176 __try {
2177 /* alloca throws a stack overflow exception if there's
2178 not enough space left on the stack */
2179 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2180 return 0;
2181 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2182 EXCEPTION_EXECUTE_HANDLER :
2183 EXCEPTION_CONTINUE_SEARCH) {
2184 int errcode = _resetstkoflw();
2185 if (errcode == 0)
2186 {
2187 Py_FatalError("Could not reset the stack!");
2188 }
2189 }
2190 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002191}
2192
2193#endif /* WIN32 && _MSC_VER */
2194
2195/* Alternate implementations can be added here... */
2196
2197#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002198
2199
2200/* Wrappers around sigaction() or signal(). */
2201
2202PyOS_sighandler_t
2203PyOS_getsig(int sig)
2204{
2205#ifdef HAVE_SIGACTION
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002206 struct sigaction context;
2207 if (sigaction(sig, NULL, &context) == -1)
2208 return SIG_ERR;
2209 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002210#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002211 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002212/* Special signal handling for the secure CRT in Visual Studio 2005 */
2213#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002214 switch (sig) {
2215 /* Only these signals are valid */
2216 case SIGINT:
2217 case SIGILL:
2218 case SIGFPE:
2219 case SIGSEGV:
2220 case SIGTERM:
2221 case SIGBREAK:
2222 case SIGABRT:
2223 break;
2224 /* Don't call signal() with other values or it will assert */
2225 default:
2226 return SIG_ERR;
2227 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002228#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002229 handler = signal(sig, SIG_IGN);
2230 if (handler != SIG_ERR)
2231 signal(sig, handler);
2232 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002233#endif
2234}
2235
2236PyOS_sighandler_t
2237PyOS_setsig(int sig, PyOS_sighandler_t handler)
2238{
2239#ifdef HAVE_SIGACTION
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002240 /* Some code in Modules/signalmodule.c depends on sigaction() being
2241 * used here if HAVE_SIGACTION is defined. Fix that if this code
2242 * changes to invalidate that assumption.
2243 */
2244 struct sigaction context, ocontext;
2245 context.sa_handler = handler;
2246 sigemptyset(&context.sa_mask);
2247 context.sa_flags = 0;
2248 if (sigaction(sig, &context, &ocontext) == -1)
2249 return SIG_ERR;
2250 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002251#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002252 PyOS_sighandler_t oldhandler;
2253 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002254#ifdef HAVE_SIGINTERRUPT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002255 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002256#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002257 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002258#endif
2259}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260
2261/* Deprecated C API functions still provided for binary compatiblity */
2262
2263#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002264PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2266{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002267 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268}
2269
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002270#undef PyParser_SimpleParseString
2271PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272PyParser_SimpleParseString(const char *str, int start)
2273{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002274 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002276
2277#undef PyRun_AnyFile
2278PyAPI_FUNC(int)
2279PyRun_AnyFile(FILE *fp, const char *name)
2280{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002281 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002282}
2283
2284#undef PyRun_AnyFileEx
2285PyAPI_FUNC(int)
2286PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2287{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002288 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002289}
2290
2291#undef PyRun_AnyFileFlags
2292PyAPI_FUNC(int)
2293PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2294{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002295 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002296}
2297
2298#undef PyRun_File
2299PyAPI_FUNC(PyObject *)
2300PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2301{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002302 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002303}
2304
2305#undef PyRun_FileEx
2306PyAPI_FUNC(PyObject *)
2307PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2308{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002309 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002310}
2311
2312#undef PyRun_FileFlags
2313PyAPI_FUNC(PyObject *)
2314PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002315 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002316{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002317 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002318}
2319
2320#undef PyRun_SimpleFile
2321PyAPI_FUNC(int)
2322PyRun_SimpleFile(FILE *f, const char *p)
2323{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002324 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002325}
2326
2327#undef PyRun_SimpleFileEx
2328PyAPI_FUNC(int)
2329PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2330{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002331 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002332}
2333
2334
2335#undef PyRun_String
2336PyAPI_FUNC(PyObject *)
2337PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2338{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002339 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002340}
2341
2342#undef PyRun_SimpleString
2343PyAPI_FUNC(int)
2344PyRun_SimpleString(const char *s)
2345{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002346 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002347}
2348
2349#undef Py_CompileString
2350PyAPI_FUNC(PyObject *)
2351Py_CompileString(const char *str, const char *p, int s)
2352{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002353 return Py_CompileStringFlags(str, p, s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002354}
2355
2356#undef PyRun_InteractiveOne
2357PyAPI_FUNC(int)
2358PyRun_InteractiveOne(FILE *f, const char *p)
2359{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002360 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002361}
2362
2363#undef PyRun_InteractiveLoop
2364PyAPI_FUNC(int)
2365PyRun_InteractiveLoop(FILE *f, const char *p)
2366{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002367 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002368}
2369
2370#ifdef __cplusplus
2371}
2372#endif